Require cl only when compiling.
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5f5c8ee5
GM
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
a2889657
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
b1d1124b 9the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
a2889657 21
5f5c8ee5
GM
22/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
a2889657 169
18160b98 170#include <config.h>
a2889657 171#include <stdio.h>
5f5c8ee5
GM
172#ifdef STDC_HEADERS
173#include <stdlib.h>
174#endif
a2889657 175#include "lisp.h"
44fa5b1e 176#include "frame.h"
a2889657
JB
177#include "window.h"
178#include "termchar.h"
179#include "dispextern.h"
180#include "buffer.h"
1c9241f5 181#include "charset.h"
a2889657
JB
182#include "indent.h"
183#include "commands.h"
184#include "macros.h"
185#include "disptab.h"
30c566e4 186#include "termhooks.h"
b0a0fbda 187#include "intervals.h"
fe8b0cf8 188#include "keyboard.h"
1c9241f5
KH
189#include "coding.h"
190#include "process.h"
dfcf069d
AS
191#include "region-cache.h"
192
6d55d620 193#ifdef HAVE_X_WINDOWS
dfcf069d
AS
194#include "xterm.h"
195#endif
a2889657 196
5f5c8ee5
GM
197#define min(a, b) ((a) < (b) ? (a) : (b))
198#define max(a, b) ((a) > (b) ? (a) : (b))
199
200#define INFINITY 10000000
201
8f3343d0 202#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
76412d64 203extern void set_frame_menubar ();
cd6dfed6 204extern int pending_menu_activation;
76412d64
RS
205#endif
206
a2889657
JB
207extern int interrupt_input;
208extern int command_loop_level;
209
b6436d4e
RS
210extern int minibuffer_auto_raise;
211
c4628384
RS
212extern Lisp_Object Qface;
213
399164b4
KH
214extern Lisp_Object Voverriding_local_map;
215extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 216extern Lisp_Object Qmenu_item;
399164b4 217
d46fb96a 218Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 219Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 220Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 221Lisp_Object Qinhibit_point_motion_hooks;
5f5c8ee5
GM
222Lisp_Object QCeval, QCwhen;
223Lisp_Object Qfontified;
224
225/* Functions called to fontify regions of text. */
226
227Lisp_Object Vfontification_functions;
228Lisp_Object Qfontification_functions;
229
230/* Non-zero means draw toolbar buttons raised when the mouse moves
231 over them. */
232
233int auto_raise_toolbar_buttons_p;
234
235/* Margin around toolbar buttons in pixels. */
236
237int toolbar_button_margin;
238
239/* Thickness of shadow to draw around toolbar buttons. */
240
241int toolbar_button_relief;
242
243/* Non-zero means automatically resize toolbars so that all toolbar
244 items are visible, and no blank lines remain. */
245
246int auto_resize_toolbars_p;
399164b4 247
735c094c
KH
248/* Non-nil means don't actually do any redisplay. */
249
250Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
251
5f5c8ee5
GM
252/* Names of text properties relevant for redisplay. */
253
254Lisp_Object Qdisplay, Qrelative_width, Qwidth, Qalign_to;
255extern Lisp_Object Qface, Qinvisible, Qimage;
256
257/* Symbols used in text property values. */
258
259Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;
261
262/* Name of the variable controlling the highlighting of trailing
263 whitespace. The implementation uses find_symbol_value to get its
264 value. */
265
266Lisp_Object Qshow_trailing_whitespace;
267
268/* Name of the face used to highlight trailing whitespace. */
269
270Lisp_Object Qtrailing_whitespace;
271
272/* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
274
275Lisp_Object Qimage;
276
277/* Non-zero means print newline to stdout before next mini-buffer
278 message. */
a2889657
JB
279
280int noninteractive_need_newline;
281
5f5c8ee5 282/* Non-zero means print newline to message log before next message. */
f88eb0b6 283
3c6595e0 284static int message_log_need_newline;
f88eb0b6 285
5f5c8ee5
GM
286\f
287/* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
a2889657 291
5f5c8ee5 292static struct text_pos this_line_start_pos;
a2889657 293
5f5c8ee5
GM
294/* Number of characters past the end of the line above, including the
295 terminating newline. */
296
297static struct text_pos this_line_end_pos;
298
299/* The vertical positions and the height of this line. */
a2889657 300
a2889657 301static int this_line_vpos;
5f5c8ee5
GM
302static int this_line_y;
303static int this_line_pixel_height;
304
305/* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
307
308static int this_line_start_x;
a2889657 309
5f5c8ee5 310/* Buffer that this_line_.* variables are referring to. */
a2889657 311
a2889657
JB
312static struct buffer *this_line_buffer;
313
5f5c8ee5
GM
314/* Nonzero means truncate lines in all windows less wide than the
315 frame. */
a2889657 316
a2889657
JB
317int truncate_partial_width_windows;
318
7bbe686f 319/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 320
7bbe686f 321int unibyte_display_via_language_environment;
5f5c8ee5
GM
322
323/* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
7bbe686f 326
d39b6696
KH
327int multiple_frames;
328
a2889657
JB
329Lisp_Object Vglobal_mode_string;
330
331/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 332
a2889657
JB
333Lisp_Object Voverlay_arrow_position;
334
5f5c8ee5
GM
335/* String to display for the arrow. Only used on terminal frames. */
336
a2889657
JB
337Lisp_Object Voverlay_arrow_string;
338
5f5c8ee5
GM
339/* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
342
d45de95b
RS
343static Lisp_Object last_arrow_position, last_arrow_string;
344
5f5c8ee5
GM
345/* Like mode-line-format, but for the title bar on a visible frame. */
346
d39b6696
KH
347Lisp_Object Vframe_title_format;
348
5f5c8ee5
GM
349/* Like mode-line-format, but for the title bar on an iconified frame. */
350
d39b6696
KH
351Lisp_Object Vicon_title_format;
352
08b610e4
RS
353/* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
5f5c8ee5 356
08b610e4
RS
357static Lisp_Object Vwindow_size_change_functions;
358
cf074754
RS
359Lisp_Object Qmenu_bar_update_hook;
360
a2889657 361/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 362
5f5c8ee5 363static int overlay_arrow_seen;
ca26e1c8 364
fba9ce76 365/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 366
5f5c8ee5
GM
367int highlight_nonselected_windows;
368
369/* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
371
14510fee 372static int scroll_step;
a2889657 373
5f5c8ee5
GM
374/* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
376
0789adb2
RS
377static int scroll_conservatively;
378
5f5c8ee5
GM
379/* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
383
9afd2168
RS
384int scroll_margin;
385
5f5c8ee5
GM
386/* Number of characters of overlap to show, when scrolling a one-line
387 window such as a minibuffer. */
388
010494d0
KH
389static int minibuffer_scroll_overlap;
390
5f5c8ee5
GM
391/* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
a2889657 394
a2889657
JB
395int buffer_shared;
396
5f5c8ee5 397/* Vector containing glyphs for an ellipsis `...'. */
a2889657 398
5f5c8ee5 399static Lisp_Object default_invis_vector[3];
a2889657 400
5f5c8ee5 401/* Nonzero means display mode line highlighted. */
a2889657 402
a2889657
JB
403int mode_line_inverse_video;
404
5f5c8ee5
GM
405/* Prompt to display in front of the mini-buffer contents. */
406
8c5b6a0a 407Lisp_Object minibuf_prompt;
a2889657 408
5f5c8ee5
GM
409/* Width of current mini-buffer prompt. Only set after display_line
410 of the line that contains the prompt. */
411
a2889657 412int minibuf_prompt_width;
5f5c8ee5
GM
413int minibuf_prompt_pixel_width;
414
415/* Message to display instead of mini-buffer contents. This is what
416 the functions error and message make, and command echoing uses it
417 as well. It overrides the minibuf_prompt as well as the buffer. */
a2889657 418
a2889657
JB
419char *echo_area_glyphs;
420
5f5c8ee5
GM
421/* A Lisp string to display instead of mini-buffer contents, analogous
422 to echo_area_glyphs. If this is a string, display that string.
423 Otherwise, if echo_area_glyphs is non-null, display that. */
424
425Lisp_Object echo_area_message;
426
427/* This is the length of the message in echo_area_glyphs or
428 echo_area_message. */
429
90adcf20
RS
430int echo_area_glyphs_length;
431
5f5c8ee5
GM
432/* Value of echo_area_glyphs when it was last acted on. If this is
433 nonzero, there is a message on the frame in the mini-buffer and it
434 should be erased as soon as it is no longer requested to appear. */
435
436char *previous_echo_glyphs;
437Lisp_Object previous_echo_area_message;
438static int previous_echo_glyphs_length;
439
440/* This is the window where the echo area message was displayed. It
441 is always a mini-buffer window, but it may not be the same window
442 currently active as a mini-buffer. */
443
73af359d
RS
444Lisp_Object echo_area_window;
445
a3788d53
RS
446/* Nonzero means multibyte characters were enabled when the echo area
447 message was specified. */
5f5c8ee5 448
a3788d53
RS
449int message_enable_multibyte;
450
5f5c8ee5
GM
451/* True if we should redraw the mode lines on the next redisplay. */
452
a2889657
JB
453int update_mode_lines;
454
5f5c8ee5
GM
455/* Smallest number of characters before the gap at any time since last
456 redisplay that finished. Valid for current buffer when
457 try_window_id can be called. */
458
a2889657
JB
459int beg_unchanged;
460
5f5c8ee5
GM
461/* Smallest number of characters after the gap at any time since last
462 redisplay that finished. Valid for current buffer when
463 try_window_id can be called. */
464
a2889657
JB
465int end_unchanged;
466
5f5c8ee5
GM
467/* MODIFF as of last redisplay that finished; if it matches MODIFF,
468 and overlay_unchanged_modified matches OVERLAY_MODIFF, that means
469 beg_unchanged and end_unchanged contain no useful information. */
470
a2889657
JB
471int unchanged_modified;
472
8850a573 473/* OVERLAY_MODIFF as of last redisplay that finished. */
5f5c8ee5 474
8850a573
RS
475int overlay_unchanged_modified;
476
5f5c8ee5
GM
477/* Nonzero if window sizes or contents have changed since last
478 redisplay that finished */
479
a2889657
JB
480int windows_or_buffers_changed;
481
5f5c8ee5
GM
482/* Nonzero after display_mode_line if %l was used and it displayed a
483 line number. */
484
aa6d10fa
RS
485int line_number_displayed;
486
487/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 488
14510fee 489static int line_number_display_limit;
5992c4f7 490
5f5c8ee5
GM
491/* Number of lines to keep in the message log buffer. t means
492 infinite. nil means don't log at all. */
493
5992c4f7 494Lisp_Object Vmessage_log_max;
d45de95b 495
5f5c8ee5
GM
496/* A scratch glyph row with contents used for generating truncation
497 glyphs. Also used in direct_output_for_insert. */
12adba34 498
5f5c8ee5
GM
499#define MAX_SCRATCH_GLYPHS 100
500struct glyph_row scratch_glyph_row;
501static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 502
5f5c8ee5
GM
503/* Ascent and height of the last line processed by move_it_to. */
504
505static int last_max_ascent, last_height;
506
507/* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
512
513#define TEXT_PROP_DISTANCE_LIMIT 100
514
515/* Non-zero means print traces of redisplay if compiled with
516 GLYPH_DEBUG != 0. */
517
518#if GLYPH_DEBUG
519int trace_redisplay_p;
520#endif
521
522/* Value returned from text property handlers (see below). */
523
524enum prop_handled
3c6595e0 525{
5f5c8ee5
GM
526 HANDLED_NORMALLY,
527 HANDLED_RECOMPUTE_PROPS,
528 HANDLED_OVERLAY_STRING_CONSUMED,
529 HANDLED_RETURN
530};
3c6595e0 531
5f5c8ee5
GM
532/* A description of text properties that redisplay is interested
533 in. */
3c6595e0 534
5f5c8ee5
GM
535struct props
536{
537 /* The name of the property. */
538 Lisp_Object *name;
90adcf20 539
5f5c8ee5
GM
540 /* A unique index for the property. */
541 enum prop_idx idx;
542
543 /* A handler function called to set up iterator IT from the property
544 at IT's current position. Value is used to steer handle_stop. */
545 enum prop_handled (*handler) P_ ((struct it *it));
546};
547
548static enum prop_handled handle_face_prop P_ ((struct it *));
549static enum prop_handled handle_invisible_prop P_ ((struct it *));
550static enum prop_handled handle_display_prop P_ ((struct it *));
551static enum prop_handled handle_overlay_change P_ ((struct it *));
552static enum prop_handled handle_fontified_prop P_ ((struct it *));
553
554/* Properties handled by iterators. */
555
556static struct props it_props[] =
5992c4f7 557{
5f5c8ee5
GM
558 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
559 /* Handle `face' before `display' because some sub-properties of
560 `display' need to know the face. */
561 {&Qface, FACE_PROP_IDX, handle_face_prop},
562 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
563 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
564 {NULL, 0, NULL}
565};
5992c4f7 566
5f5c8ee5
GM
567/* Value is the position described by X. If X is a marker, value is
568 the marker_position of X. Otherwise, value is X. */
12adba34 569
5f5c8ee5 570#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 571
5f5c8ee5 572/* Enumeration returned by some move_it_.* functions internally. */
12adba34 573
5f5c8ee5
GM
574enum move_it_result
575{
576 /* Not used. Undefined value. */
577 MOVE_UNDEFINED,
bab29e15 578
5f5c8ee5
GM
579 /* Move ended at the requested buffer position or ZV. */
580 MOVE_POS_MATCH_OR_ZV,
bab29e15 581
5f5c8ee5
GM
582 /* Move ended at the requested X pixel position. */
583 MOVE_X_REACHED,
12adba34 584
5f5c8ee5
GM
585 /* Move within a line ended at the end of a line that must be
586 continued. */
587 MOVE_LINE_CONTINUED,
588
589 /* Move within a line ended at the end of a line that would
590 be displayed truncated. */
591 MOVE_LINE_TRUNCATED,
ff6c30e5 592
5f5c8ee5
GM
593 /* Move within a line ended at a line end. */
594 MOVE_NEWLINE_OR_CR
595};
12adba34 596
ff6c30e5 597
5f5c8ee5
GM
598\f
599/* Function prototypes. */
600
601static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
602 struct text_pos));
603static int compute_window_start_on_continuation_line P_ ((struct window *));
604static Lisp_Object eval_handler P_ ((Lisp_Object));
605static Lisp_Object eval_form P_ ((Lisp_Object));
606static void insert_left_trunc_glyphs P_ ((struct it *));
607static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
608static void extend_face_to_end_of_line P_ ((struct it *));
609static void append_space P_ ((struct it *, int));
610static void make_cursor_line_fully_visible P_ ((struct window *));
611static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
612static int trailing_whitespace_p P_ ((int));
613static int message_log_check_duplicate P_ ((int, int, int, int));
614int invisible_p P_ ((Lisp_Object, Lisp_Object));
615int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
616static void push_it P_ ((struct it *));
617static void pop_it P_ ((struct it *));
618static void sync_frame_with_window_matrix_rows P_ ((struct window *));
619static void redisplay_internal P_ ((int));
620static void echo_area_display P_ ((int));
621static void redisplay_windows P_ ((Lisp_Object));
622static void redisplay_window P_ ((Lisp_Object, int));
623static void update_menu_bar P_ ((struct frame *, int));
624static int try_window_reusing_current_matrix P_ ((struct window *));
625static int try_window_id P_ ((struct window *));
626static int display_line P_ ((struct it *));
627static void display_mode_lines P_ ((struct window *));
628static void display_mode_line P_ ((struct window *, enum face_id,
629 Lisp_Object));
630static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
631static char *decode_mode_spec P_ ((struct window *, char, int, int));
632static void display_menu_bar P_ ((struct window *));
633static int display_count_lines P_ ((int, int, int, int, int *));
634static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
635 int, int, struct it *, int, int, int, int));
636static void compute_line_metrics P_ ((struct it *));
637static void run_redisplay_end_trigger_hook P_ ((struct it *));
638static int get_overlay_strings P_ ((struct it *));
639static void next_overlay_string P_ ((struct it *));
640void set_iterator_to_next P_ ((struct it *));
641static void reseat P_ ((struct it *, struct text_pos, int));
642static void reseat_1 P_ ((struct it *, struct text_pos, int));
643static void back_to_previous_visible_line_start P_ ((struct it *));
644static void reseat_at_previous_visible_line_start P_ ((struct it *));
645static int next_element_from_display_vector P_ ((struct it *));
646static int next_element_from_string P_ ((struct it *));
647static int next_element_from_c_string P_ ((struct it *));
648static int next_element_from_buffer P_ ((struct it *));
649static int next_element_from_image P_ ((struct it *));
650static int next_element_from_stretch P_ ((struct it *));
651static void load_overlay_strings P_ ((struct it *));
652static void init_from_display_pos P_ ((struct it *, struct window *,
653 struct display_pos *));
654static void reseat_to_string P_ ((struct it *, unsigned char *,
655 Lisp_Object, int, int, int, int));
656static int charset_at_position P_ ((struct text_pos));
657static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
658 int, int, int));
659void move_it_vertically_backward P_ ((struct it *, int));
660static void init_to_row_start P_ ((struct it *, struct window *,
661 struct glyph_row *));
662static void init_to_row_end P_ ((struct it *, struct window *,
663 struct glyph_row *));
664static void back_to_previous_line_start P_ ((struct it *));
665static void forward_to_next_line_start P_ ((struct it *));
666static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
667 Lisp_Object, int));
668static struct text_pos string_pos P_ ((int, Lisp_Object));
669static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
670static int number_of_chars P_ ((unsigned char *, int));
671static void compute_stop_pos P_ ((struct it *));
672static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
673 Lisp_Object));
674static int face_before_or_after_it_pos P_ ((struct it *, int));
675static int next_overlay_change P_ ((int));
676static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
677 Lisp_Object, struct text_pos *));
678
679#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
680#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 681
5f5c8ee5 682#ifdef HAVE_WINDOW_SYSTEM
12adba34 683
5f5c8ee5
GM
684static void update_toolbar P_ ((struct frame *, int));
685static void build_desired_toolbar_string P_ ((struct frame *f));
686static int redisplay_toolbar P_ ((struct frame *));
687static void display_toolbar_line P_ ((struct it *));
12adba34 688
5f5c8ee5 689#endif /* HAVE_WINDOW_SYSTEM */
12adba34 690
5f5c8ee5
GM
691\f
692/***********************************************************************
693 Window display dimensions
694 ***********************************************************************/
12adba34 695
5f5c8ee5
GM
696/* Return the window-relative maximum y + 1 for glyph rows displaying
697 text in window W. This is the height of W minus the height of a
698 mode line, if any. */
699
700INLINE int
701window_text_bottom_y (w)
702 struct window *w;
703{
704 struct frame *f = XFRAME (w->frame);
705 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
706
707 if (WINDOW_WANTS_MODELINE_P (w))
708 height -= CURRENT_MODE_LINE_HEIGHT (w);
709 return height;
f88eb0b6
KH
710}
711
f82aff7c 712
5f5c8ee5
GM
713/* Return the pixel width of display area AREA of window W. AREA < 0
714 means return the total width of W, not including bitmap areas to
715 the left and right of the window. */
ff6c30e5 716
5f5c8ee5
GM
717INLINE int
718window_box_width (w, area)
719 struct window *w;
720 int area;
721{
722 struct frame *f = XFRAME (w->frame);
723 int width = XFASTINT (w->width);
724
725 if (!w->pseudo_window_p)
ff6c30e5 726 {
5f5c8ee5
GM
727 width -= FRAME_SCROLL_BAR_WIDTH (f) + 2 * FRAME_FLAGS_AREA_COLS (f);
728
729 if (area == TEXT_AREA)
730 {
731 if (INTEGERP (w->left_margin_width))
732 width -= XFASTINT (w->left_margin_width);
733 if (INTEGERP (w->right_margin_width))
734 width -= XFASTINT (w->right_margin_width);
735 }
736 else if (area == LEFT_MARGIN_AREA)
737 width = (INTEGERP (w->left_margin_width)
738 ? XFASTINT (w->left_margin_width) : 0);
739 else if (area == RIGHT_MARGIN_AREA)
740 width = (INTEGERP (w->right_margin_width)
741 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 742 }
5f5c8ee5
GM
743
744 return width * CANON_X_UNIT (f);
ff6c30e5 745}
1adc55de 746
1adc55de 747
5f5c8ee5
GM
748/* Return the pixel height of the display area of window W, not
749 including mode lines of W, if any.. */
f88eb0b6 750
5f5c8ee5
GM
751INLINE int
752window_box_height (w)
753 struct window *w;
f88eb0b6 754{
5f5c8ee5
GM
755 struct frame *f = XFRAME (w->frame);
756 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
757
758 if (WINDOW_WANTS_MODELINE_P (w))
759 height -= CURRENT_MODE_LINE_HEIGHT (w);
760
761 if (WINDOW_WANTS_TOP_LINE_P (w))
762 height -= CURRENT_TOP_LINE_HEIGHT (w);
763
764 return height;
5992c4f7
KH
765}
766
767
5f5c8ee5
GM
768/* Return the frame-relative coordinate of the left edge of display
769 area AREA of window W. AREA < 0 means return the left edge of the
770 whole window, to the right of any bitmap area at the left side of
771 W. */
5992c4f7 772
5f5c8ee5
GM
773INLINE int
774window_box_left (w, area)
775 struct window *w;
776 int area;
90adcf20 777{
5f5c8ee5
GM
778 struct frame *f = XFRAME (w->frame);
779 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 780
5f5c8ee5 781 if (!w->pseudo_window_p)
90adcf20 782 {
5f5c8ee5
GM
783 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
784 + FRAME_FLAGS_AREA_WIDTH (f));
785
786 if (area == TEXT_AREA)
787 x += window_box_width (w, LEFT_MARGIN_AREA);
788 else if (area == RIGHT_MARGIN_AREA)
789 x += (window_box_width (w, LEFT_MARGIN_AREA)
790 + window_box_width (w, TEXT_AREA));
90adcf20 791 }
73af359d 792
5f5c8ee5
GM
793 return x;
794}
90adcf20 795
b6436d4e 796
5f5c8ee5
GM
797/* Return the frame-relative coordinate of the right edge of display
798 area AREA of window W. AREA < 0 means return the left edge of the
799 whole window, to the left of any bitmap area at the right side of
800 W. */
ded34426 801
5f5c8ee5
GM
802INLINE int
803window_box_right (w, area)
804 struct window *w;
805 int area;
806{
807 return window_box_left (w, area) + window_box_width (w, area);
808}
809
810
811/* Get the bounding box of the display area AREA of window W, without
812 mode lines, in frame-relative coordinates. AREA < 0 means the
813 whole window, not including bitmap areas to the left and right of
814 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
815 coordinates of the upper-left corner of the box. Return in
816 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
817
818INLINE void
819window_box (w, area, box_x, box_y, box_width, box_height)
820 struct window *w;
821 int area;
822 int *box_x, *box_y, *box_width, *box_height;
823{
824 struct frame *f = XFRAME (w->frame);
825
826 *box_width = window_box_width (w, area);
827 *box_height = window_box_height (w);
828 *box_x = window_box_left (w, area);
829 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
830 + XFASTINT (w->top) * CANON_Y_UNIT (f));
831 if (WINDOW_WANTS_TOP_LINE_P (w))
832 *box_y += CURRENT_TOP_LINE_HEIGHT (w);
ded34426 833}
1adc55de 834
1adc55de 835
5f5c8ee5
GM
836/* Get the bounding box of the display area AREA of window W, without
837 mode lines. AREA < 0 means the whole window, not including bitmap
838 areas to the left and right of the window. Return in *TOP_LEFT_X
839 and TOP_LEFT_Y the frame-relative pixel coordinates of the
840 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
841 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
842 box. */
ded34426 843
5f5c8ee5
GM
844INLINE void
845window_box_edges (w, area, top_left_x, top_left_y,
846 bottom_right_x, bottom_right_y)
847 struct window *w;
848 int area;
849 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 850{
5f5c8ee5
GM
851 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
852 bottom_right_y);
853 *bottom_right_x += *top_left_x;
854 *bottom_right_y += *top_left_y;
48ae5f0a
KH
855}
856
5f5c8ee5
GM
857
858\f
859/***********************************************************************
860 Utilities
861 ***********************************************************************/
862
863/* Given a position POS containing a valid character and byte position
864 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
865
866static struct text_pos
867string_pos_nchars_ahead (pos, string, nchars)
868 struct text_pos pos;
869 Lisp_Object string;
870 int nchars;
0b1005ef 871{
5f5c8ee5
GM
872 xassert (STRINGP (string) && nchars >= 0);
873
874 if (STRING_MULTIBYTE (string))
875 {
876 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
877 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
878 int len;
879
880 while (nchars--)
881 {
882 STRING_CHAR_AND_LENGTH (p, rest, len);
883 p += len, rest -= len;
884 xassert (rest >= 0);
885 CHARPOS (pos) += 1;
886 BYTEPOS (pos) += len;
887 }
888 }
889 else
890 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
891
892 return pos;
0a9dc68b
RS
893}
894
0a9dc68b 895
5f5c8ee5
GM
896/* Value is the text position, i.e. character and byte position,
897 for character position CHARPOS in STRING. */
898
899static INLINE struct text_pos
900string_pos (charpos, string)
901 int charpos;
0a9dc68b 902 Lisp_Object string;
0a9dc68b 903{
5f5c8ee5
GM
904 struct text_pos pos;
905 xassert (STRINGP (string));
906 xassert (charpos >= 0);
907 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
908 return pos;
909}
910
911
912/* Value is a text position, i.e. character and byte position, for
913 character position CHARPOS in C string S. MULTIBYTE_P non-zero
914 means recognize multibyte characters. */
915
916static struct text_pos
917c_string_pos (charpos, s, multibyte_p)
918 int charpos;
919 unsigned char *s;
920 int multibyte_p;
921{
922 struct text_pos pos;
923
924 xassert (s != NULL);
925 xassert (charpos >= 0);
926
927 if (multibyte_p)
0a9dc68b 928 {
5f5c8ee5
GM
929 int rest = strlen (s), len;
930
931 SET_TEXT_POS (pos, 0, 0);
932 while (charpos--)
0a9dc68b 933 {
5f5c8ee5
GM
934 STRING_CHAR_AND_LENGTH (s, rest, len);
935 s += len, rest -= len;
936 xassert (rest >= 0);
937 CHARPOS (pos) += 1;
938 BYTEPOS (pos) += len;
0a9dc68b
RS
939 }
940 }
5f5c8ee5
GM
941 else
942 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 943
5f5c8ee5
GM
944 return pos;
945}
0a9dc68b 946
0a9dc68b 947
5f5c8ee5
GM
948/* Value is the number of characters in C string S. MULTIBYTE_P
949 non-zero means recognize multibyte characters. */
0a9dc68b 950
5f5c8ee5
GM
951static int
952number_of_chars (s, multibyte_p)
953 unsigned char *s;
954 int multibyte_p;
955{
956 int nchars;
957
958 if (multibyte_p)
959 {
960 int rest = strlen (s), len;
961 unsigned char *p = (unsigned char *) s;
0a9dc68b 962
5f5c8ee5
GM
963 for (nchars = 0; rest > 0; ++nchars)
964 {
965 STRING_CHAR_AND_LENGTH (p, rest, len);
966 rest -= len, p += len;
0a9dc68b
RS
967 }
968 }
5f5c8ee5
GM
969 else
970 nchars = strlen (s);
971
972 return nchars;
0b1005ef
KH
973}
974
5f5c8ee5
GM
975
976/* Compute byte position NEWPOS->bytepos corresponding to
977 NEWPOS->charpos. POS is a known position in string STRING.
978 NEWPOS->charpos must be >= POS.charpos. */
76412d64 979
5f5c8ee5
GM
980static void
981compute_string_pos (newpos, pos, string)
982 struct text_pos *newpos, pos;
983 Lisp_Object string;
76412d64 984{
5f5c8ee5
GM
985 xassert (STRINGP (string));
986 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
987
988 if (STRING_MULTIBYTE (string))
989 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
990 string);
991 else
992 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
993}
994
9c74a0dd 995
5f5c8ee5
GM
996/* Return the charset of the character at position POS in
997 current_buffer. */
1adc55de 998
5f5c8ee5
GM
999static int
1000charset_at_position (pos)
1001 struct text_pos pos;
a2889657 1002{
5f5c8ee5
GM
1003 int c, multibyte_p;
1004 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1005
1006 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1007 if (multibyte_p)
a2889657 1008 {
5f5c8ee5
GM
1009 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1010 - BYTEPOS (pos));
1011 int len;
1012 c = STRING_CHAR_AND_LENGTH (p, maxlen, len);
a2889657 1013 }
5f5c8ee5
GM
1014 else
1015 c = *p;
1016
1017 return CHAR_CHARSET (c);
1018}
1019
1020
1021\f
1022/***********************************************************************
1023 Lisp form evaluation
1024 ***********************************************************************/
1025
1026/* Error handler for eval_form. */
1027
1028static Lisp_Object
1029eval_handler (arg)
1030 Lisp_Object arg;
1031{
1032 return Qnil;
1033}
1034
1035
1036/* Evaluate SEXPR and return the result, or nil if something went
1037 wrong. */
1038
1039static Lisp_Object
1040eval_form (sexpr)
1041 Lisp_Object sexpr;
1042{
1043 int count = specpdl_ptr - specpdl;
1044 Lisp_Object val;
1045 specbind (Qinhibit_redisplay, Qt);
1046 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1047 return unbind_to (count, val);
1048}
1049
1050
1051\f
1052/***********************************************************************
1053 Debugging
1054 ***********************************************************************/
1055
1056#if 0
1057
1058/* Define CHECK_IT to perform sanity checks on iterators.
1059 This is for debugging. It is too slow to do unconditionally. */
1060
1061static void
1062check_it (it)
1063 struct it *it;
1064{
1065 if (it->method == next_element_from_string)
a2889657 1066 {
5f5c8ee5
GM
1067 xassert (STRINGP (it->string));
1068 xassert (IT_STRING_CHARPOS (*it) >= 0);
1069 }
1070 else if (it->method == next_element_from_buffer)
1071 {
1072 /* Check that character and byte positions agree. */
1073 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1074 }
73af359d 1075
5f5c8ee5
GM
1076 if (it->dpvec)
1077 xassert (it->current.dpvec_index >= 0);
1078 else
1079 xassert (it->current.dpvec_index < 0);
1080}
1f40cad2 1081
5f5c8ee5
GM
1082#define CHECK_IT(IT) check_it ((IT))
1083
1084#else /* not 0 */
1085
1086#define CHECK_IT(IT) (void) 0
1087
1088#endif /* not 0 */
1089
1090
1091#if GLYPH_DEBUG
1092
1093/* Check that the window end of window W is what we expect it
1094 to be---the last row in the current matrix displaying text. */
1095
1096static void
1097check_window_end (w)
1098 struct window *w;
1099{
1100 if (!MINI_WINDOW_P (w)
1101 && !NILP (w->window_end_valid))
1102 {
1103 struct glyph_row *row;
1104 xassert ((row = MATRIX_ROW (w->current_matrix,
1105 XFASTINT (w->window_end_vpos)),
1106 !row->enabled_p
1107 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1108 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1109 }
1110}
1111
1112#define CHECK_WINDOW_END(W) check_window_end ((W))
1113
1114#else /* not GLYPH_DEBUG */
1115
1116#define CHECK_WINDOW_END(W) (void) 0
1117
1118#endif /* not GLYPH_DEBUG */
1119
1120
1121\f
1122/***********************************************************************
1123 Iterator initialization
1124 ***********************************************************************/
1125
1126/* Initialize IT for displaying current_buffer in window W, starting
1127 at character position CHARPOS. CHARPOS < 0 means that no buffer
1128 position is specified which is useful when the iterator is assigned
1129 a position later. BYTEPOS is the byte position corresponding to
1130 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1131
1132 If ROW is not null, calls to produce_glyphs with IT as parameter
1133 will produce glyphs in that row.
1134
1135 BASE_FACE_ID is the id of a base face to use. It must be one of
1136 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1137 TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
1138 displaying the toolbar.
1139
1140 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1141 TOP_LINE_FACE_ID, the iterator will be initialized to use the
1142 corresponding mode line glyph row of the desired matrix of W. */
1143
1144void
1145init_iterator (it, w, charpos, bytepos, row, base_face_id)
1146 struct it *it;
1147 struct window *w;
1148 int charpos, bytepos;
1149 struct glyph_row *row;
1150 enum face_id base_face_id;
1151{
1152 int highlight_region_p;
1153 Lisp_Object value;
1154
1155 /* Some precondition checks. */
1156 xassert (w != NULL && it != NULL);
1157 xassert (charpos < 0 || current_buffer == XBUFFER (w->buffer));
1158 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1159
1160 /* If face attributes have been changed since the last redisplay,
1161 free realized faces now because they depend on face definitions
1162 that might have changed. */
1163 if (face_change_count)
1164 {
1165 face_change_count = 0;
1166 free_all_realized_faces (Qnil);
1167 }
1168
1169 /* Use one of the mode line rows of W's desired matrix if
1170 appropriate. */
1171 if (row == NULL)
1172 {
1173 if (base_face_id == MODE_LINE_FACE_ID)
1174 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1175 else if (base_face_id == TOP_LINE_FACE_ID)
1176 row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
1177 }
1178
1179 /* Clear IT. */
1180 bzero (it, sizeof *it);
1181 it->current.overlay_string_index = -1;
1182 it->current.dpvec_index = -1;
1183 it->charset = CHARSET_ASCII;
1184 it->base_face_id = base_face_id;
1185
1186 /* The window in which we iterate over current_buffer: */
1187 XSETWINDOW (it->window, w);
1188 it->w = w;
1189 it->f = XFRAME (w->frame);
1190
1191 /* If realized faces have been removed, e.g. because of face
1192 attribute changes of named faces, recompute them. */
1193 if (FRAME_FACE_CACHE (it->f)->used == 0)
1194 recompute_basic_faces (it->f);
1195
1196 /* Should we highlight trailing whitespace? */
1197 value = find_symbol_value (Qshow_trailing_whitespace);
1198 it->show_trailing_whitespace_p
1199 = EQ (value, Qunbound) ? 0 : !NILP (value);
1200
1201 /* Current value of the `space-width', and 'height' properties. */
1202 it->space_width = Qnil;
1203 it->font_height = Qnil;
1204
1205 /* Are control characters displayed as `^C'? */
1206 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1207
1208 /* -1 means everything between a CR and the following line end
1209 is invisible. >0 means lines indented more than this value are
1210 invisible. */
1211 it->selective = (INTEGERP (current_buffer->selective_display)
1212 ? XFASTINT (current_buffer->selective_display)
1213 : (!NILP (current_buffer->selective_display)
1214 ? -1 : 0));
1215 it->selective_display_ellipsis_p
1216 = !NILP (current_buffer->selective_display_ellipses);
1217
1218 /* Display table to use. */
1219 it->dp = window_display_table (w);
1220
1221 /* Are multibyte characters enabled in current_buffer? */
1222 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1223
1224 /* Non-zero if we should highlight the region. */
1225 highlight_region_p
1226 = (!NILP (Vtransient_mark_mode)
1227 && !NILP (current_buffer->mark_active)
1228 && XMARKER (current_buffer->mark)->buffer != 0);
1229
1230 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1231 start and end of a visible region in window IT->w. Set both to
1232 -1 to indicate no region. */
1233 if (highlight_region_p
1234 /* Maybe highlight only in selected window. */
1235 && (/* Either show region everywhere. */
1236 highlight_nonselected_windows
1237 /* Or show region in the selected window. */
1238 || w == XWINDOW (selected_window)
1239 /* Or show the region if we are in the mini-buffer and W is
1240 the window the mini-buffer refers to. */
1241 || (MINI_WINDOW_P (XWINDOW (selected_window))
1242 && w == XWINDOW (Vminibuf_scroll_window))))
1243 {
1244 int charpos = marker_position (current_buffer->mark);
1245 it->region_beg_charpos = min (PT, charpos);
1246 it->region_end_charpos = max (PT, charpos);
1247 }
1248 else
1249 it->region_beg_charpos = it->region_end_charpos = -1;
1250
1251 /* Get the position at which the redisplay_end_trigger hook should
1252 be run, if it is to be run at all. */
1253 if (MARKERP (w->redisplay_end_trigger)
1254 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1255 it->redisplay_end_trigger_charpos
1256 = marker_position (w->redisplay_end_trigger);
1257 else if (INTEGERP (w->redisplay_end_trigger))
1258 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1259
1260 /* Correct bogus values of tab_width. */
1261 it->tab_width = XINT (current_buffer->tab_width);
1262 if (it->tab_width <= 0 || it->tab_width > 1000)
1263 it->tab_width = 8;
1264
1265 /* Are lines in the display truncated? */
1266 it->truncate_lines_p
1267 = (base_face_id != DEFAULT_FACE_ID
1268 || XINT (it->w->hscroll)
1269 || (truncate_partial_width_windows
1270 && !WINDOW_FULL_WIDTH_P (it->w))
1271 || !NILP (current_buffer->truncate_lines));
1272
1273 /* Get dimensions of truncation and continuation glyphs. These are
1274 displayed as bitmaps under X, so we don't need them for such
1275 frames. */
1276 if (!FRAME_WINDOW_P (it->f))
1277 {
1278 if (it->truncate_lines_p)
1279 {
1280 /* We will need the truncation glyph. */
1281 xassert (it->glyph_row == NULL);
1282 produce_special_glyphs (it, IT_TRUNCATION);
1283 it->truncation_pixel_width = it->pixel_width;
1284 }
1285 else
1286 {
1287 /* We will need the continuation glyph. */
1288 xassert (it->glyph_row == NULL);
1289 produce_special_glyphs (it, IT_CONTINUATION);
1290 it->continuation_pixel_width = it->pixel_width;
1291 }
1292
1293 /* Reset these values to zero becaue the produce_special_glyphs
1294 above has changed them. */
1295 it->pixel_width = it->ascent = it->descent = 0;
1296 }
1297
1298 /* Set this after getting the dimensions of truncation and
1299 continuation glyphs, so that we don't produce glyphs when calling
1300 produce_special_glyphs, above. */
1301 it->glyph_row = row;
1302 it->area = TEXT_AREA;
1303
1304 /* Get the dimensions of the display area. The display area
1305 consists of the visible window area plus a horizontally scrolled
1306 part to the left of the window. All x-values are relative to the
1307 start of this total display area. */
1308 if (base_face_id != DEFAULT_FACE_ID)
1309 {
1310 /* Mode lines, menu bar in terminal frames. */
1311 it->first_visible_x = 0;
1312 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1313 }
1314 else
1315 {
1316 it->first_visible_x
1317 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1318 it->last_visible_x = (it->first_visible_x
1319 + window_box_width (w, TEXT_AREA));
1320
1321 /* If we truncate lines, leave room for the truncator glyph(s) at
1322 the right margin. Otherwise, leave room for the continuation
1323 glyph(s). Truncation and continuation glyphs are not inserted
1324 for window-based redisplay. */
1325 if (!FRAME_WINDOW_P (it->f))
1326 {
1327 if (it->truncate_lines_p)
1328 it->last_visible_x -= it->truncation_pixel_width;
1329 else
1330 it->last_visible_x -= it->continuation_pixel_width;
1331 }
1332
1333 it->top_line_p = WINDOW_WANTS_TOP_LINE_P (w);
1334 it->current_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w) + w->vscroll;
1335 }
1336
1337 /* Leave room for a border glyph. */
1338 if (!FRAME_WINDOW_P (it->f)
1339 && !WINDOW_RIGHTMOST_P (it->w))
1340 it->last_visible_x -= 1;
1341
1342 it->last_visible_y = window_text_bottom_y (w);
1343
1344 /* For mode lines and alike, arrange for the first glyph having a
1345 left box line if the face specifies a box. */
1346 if (base_face_id != DEFAULT_FACE_ID)
1347 {
1348 struct face *face;
1349
1350 it->face_id = base_face_id;
1351
1352 /* If we have a boxed mode line, make the first character appear
1353 with a left box line. */
1354 face = FACE_FROM_ID (it->f, base_face_id);
1355 if (face->box != FACE_NO_BOX)
1356 it->start_of_box_run_p = 1;
1357 }
1358
1359 /* If a buffer position was specified, set the iterator there,
1360 getting overlays and face properties from that position. */
1361 if (charpos > 0)
1362 {
1363 it->end_charpos = ZV;
1364 it->face_id = -1;
1365 IT_CHARPOS (*it) = charpos;
1366
1367 /* Compute byte position if not specified. */
1368 if (bytepos <= 0)
1369 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1370 else
1371 IT_BYTEPOS (*it) = bytepos;
1372
1373 /* Compute faces etc. */
1374 reseat (it, it->current.pos, 1);
1375 }
1376
1377 CHECK_IT (it);
1378}
1379
1380
1381/* Initialize IT for the display of window W with window start POS. */
1382
1383void
1384start_display (it, w, pos)
1385 struct it *it;
1386 struct window *w;
1387 struct text_pos pos;
1388{
1389 int start_at_line_beg_p;
1390 struct glyph_row *row;
1391 int first_vpos = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
1392 int first_y;
1393
1394 row = w->desired_matrix->rows + first_vpos;
1395 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1396 first_y = it->current_y;
1397
1398 /* If window start is not at a line start, move back to the line
1399 start. This makes sure that we take continuation lines into
1400 account. */
1401 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1402 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1403 if (!start_at_line_beg_p)
1404 reseat_at_previous_visible_line_start (it);
1405
1406#if NO_PROMPT_IN_BUFFER
1407 /* Take the mini-buffer prompt width into account for tab
1408 calculations. */
1409 if (MINI_WINDOW_P (w) && IT_CHARPOS (*it) == BEGV)
1410 {
1411 /* Why is mini-buffer_prompt_width guaranteed to be set here? */
1412 it->prompt_width = minibuf_prompt_pixel_width;
1413 }
1414#endif /* NO_PROMPT_IN_BUFFER */
1415
1416 /* If window start is not at a line start, skip forward to POS to
1417 get the correct continuation_lines_width and current_x. */
1418 if (!start_at_line_beg_p)
1419 {
1420 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1421
1422 /* If lines are continued, this line may end in the middle of a
1423 multi-glyph character (e.g. a control character displayed as
1424 \003, or in the middle of an overlay string). In this case
1425 move_it_to above will not have taken us to the start of
1426 the continuation line but to the end of the continued line. */
1427 if (!it->truncate_lines_p && it->current_x > 0)
1428 {
1429 if (it->current.dpvec_index >= 0
1430 || it->current.overlay_string_index >= 0)
1431 {
1432 set_iterator_to_next (it);
1433 move_it_in_display_line_to (it, -1, -1, 0);
1434 }
1435 it->continuation_lines_width += it->current_x;
1436 }
1437
1438 it->current_y = first_y;
1439 it->vpos = 0;
1440 it->current_x = it->hpos = 0;
1441 }
1442
1443#if 0 /* Don't assert the following because start_display is sometimes
1444 called intentionally with a window start that is not at a
1445 line start. Please leave this code in as a comment. */
1446
1447 /* Window start should be on a line start, now. */
1448 xassert (it->continuation_lines_width
1449 || IT_CHARPOS (it) == BEGV
1450 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1451#endif /* 0 */
1452}
1453
1454
1455/* Initialize IT for stepping through current_buffer in window W,
1456 starting at position POS that includes overlay string and display
1457 vector/ control character translation position information. */
1458
1459static void
1460init_from_display_pos (it, w, pos)
1461 struct it *it;
1462 struct window *w;
1463 struct display_pos *pos;
1464{
1465 /* Keep in mind: the call to reseat in init_iterator skips invisible
1466 text, so we might end up at a position different from POS. This
1467 is only a problem when POS is a row start after a newline and an
1468 overlay starts there with an after-string, and the overlay has an
1469 invisible property. Since we don't skip invisible text in
1470 display_line and elsewhere immediately after consuming the
1471 newline before the row start, such a POS will not be in a string,
1472 but the call to init_iterator below will move us to the
1473 after-string. */
1474 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1475 NULL, DEFAULT_FACE_ID);
1476
1477 /* If position is within an overlay string, set up IT to
1478 the right overlay string. */
1479 if (pos->overlay_string_index >= 0)
1480 {
1481 int relative_index;
1482
1483 /* We already have the first chunk of overlay strings in
1484 IT->overlay_strings. Load more until the one for
1485 pos->overlay_string_index is in IT->overlay_strings. */
1486 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1487 {
1488 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1489 it->current.overlay_string_index = 0;
1490 while (n--)
1491 {
1492 load_overlay_strings (it);
1493 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1494 }
1495 }
1496
1497 it->current.overlay_string_index = pos->overlay_string_index;
1498 relative_index = (it->current.overlay_string_index
1499 % OVERLAY_STRING_CHUNK_SIZE);
1500 it->string = it->overlay_strings[relative_index];
1501 it->current.string_pos = pos->string_pos;
1502 it->method = next_element_from_string;
1503 }
1504 else if (CHARPOS (pos->string_pos) >= 0)
1505 {
1506 /* Recorded position is not in an overlay string, but in another
1507 string. This can only be a string from a `display' property.
1508 IT should already be filled with that string. */
1509 it->current.string_pos = pos->string_pos;
1510 xassert (STRINGP (it->string));
1511 }
1512
1513 /* Restore position in display vector translations or control
1514 character translations. */
1515 if (pos->dpvec_index >= 0)
1516 {
1517 /* This fills IT->dpvec. */
1518 get_next_display_element (it);
1519 xassert (it->dpvec && it->current.dpvec_index == 0);
1520 it->current.dpvec_index = pos->dpvec_index;
1521 }
1522
1523 CHECK_IT (it);
1524}
1525
1526
1527/* Initialize IT for stepping through current_buffer in window W
1528 starting at ROW->start. */
1529
1530static void
1531init_to_row_start (it, w, row)
1532 struct it *it;
1533 struct window *w;
1534 struct glyph_row *row;
1535{
1536 init_from_display_pos (it, w, &row->start);
1537 it->continuation_lines_width = row->continuation_lines_width;
1538 CHECK_IT (it);
1539}
1540
1541
1542/* Initialize IT for stepping through current_buffer in window W
1543 starting in the line following ROW, i.e. starting at ROW->end. */
1544
1545static void
1546init_to_row_end (it, w, row)
1547 struct it *it;
1548 struct window *w;
1549 struct glyph_row *row;
1550{
1551 init_from_display_pos (it, w, &row->end);
1552
1553 if (row->continued_p)
1554 it->continuation_lines_width = (row->continuation_lines_width
1555 + row->pixel_width);
1556 CHECK_IT (it);
1557}
1558
1559
1560
1561\f
1562/***********************************************************************
1563 Text properties
1564 ***********************************************************************/
1565
1566/* Called when IT reaches IT->stop_charpos. Handle text property and
1567 overlay changes. Set IT->stop_charpos to the next position where
1568 to stop. */
1569
1570static void
1571handle_stop (it)
1572 struct it *it;
1573{
1574 enum prop_handled handled;
1575 int handle_overlay_change_p = 1;
1576 struct props *p;
1577
1578 it->dpvec = NULL;
1579 it->current.dpvec_index = -1;
1580
1581 do
1582 {
1583 handled = HANDLED_NORMALLY;
1584
1585 /* Call text property handlers. */
1586 for (p = it_props; p->handler; ++p)
1587 {
1588 handled = p->handler (it);
1589
1590 if (handled == HANDLED_RECOMPUTE_PROPS)
1591 break;
1592 else if (handled == HANDLED_RETURN)
1593 return;
1594 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1595 handle_overlay_change_p = 0;
1596 }
1597
1598 if (handled != HANDLED_RECOMPUTE_PROPS)
1599 {
1600 /* Don't check for overlay strings below when set to deliver
1601 characters from a display vector. */
1602 if (it->method == next_element_from_display_vector)
1603 handle_overlay_change_p = 0;
1604
1605 /* Handle overlay changes. */
1606 if (handle_overlay_change_p)
1607 handled = handle_overlay_change (it);
1608
1609 /* Determine where to stop next. */
1610 if (handled == HANDLED_NORMALLY)
1611 compute_stop_pos (it);
1612 }
1613 }
1614 while (handled == HANDLED_RECOMPUTE_PROPS);
1615}
1616
1617
1618/* Compute IT->stop_charpos from text property and overlay change
1619 information for IT's current position. */
1620
1621static void
1622compute_stop_pos (it)
1623 struct it *it;
1624{
1625 register INTERVAL iv, next_iv;
1626 Lisp_Object object, limit, position;
1627
1628 /* If nowhere else, stop at the end. */
1629 it->stop_charpos = it->end_charpos;
1630
1631 if (STRINGP (it->string))
1632 {
1633 /* Strings are usually short, so don't limit the search for
1634 properties. */
1635 object = it->string;
1636 limit = Qnil;
1637 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1638 }
1639 else
1640 {
1641 int charpos;
1642
1643 /* If next overlay change is in front of the current stop pos
1644 (which is IT->end_charpos), stop there. Note: value of
1645 next_overlay_change is point-max if no overlay change
1646 follows. */
1647 charpos = next_overlay_change (IT_CHARPOS (*it));
1648 if (charpos < it->stop_charpos)
1649 it->stop_charpos = charpos;
1650
1651 /* If showing the region, we have to stop at the region
1652 start or end because the face might change there. */
1653 if (it->region_beg_charpos > 0)
1654 {
1655 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1656 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1657 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1658 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1659 }
1660
1661 /* Set up variables for computing the stop position from text
1662 property changes. */
1663 XSETBUFFER (object, current_buffer);
1664 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1665 XSETFASTINT (position, IT_CHARPOS (*it));
1666
1667 }
1668
1669 /* Get the interval containing IT's position. Value is a null
1670 interval if there isn't such an interval. */
1671 iv = validate_interval_range (object, &position, &position, 0);
1672 if (!NULL_INTERVAL_P (iv))
1673 {
1674 Lisp_Object values_here[LAST_PROP_IDX];
1675 struct props *p;
1676
1677 /* Get properties here. */
1678 for (p = it_props; p->handler; ++p)
1679 values_here[p->idx] = textget (iv->plist, *p->name);
1680
1681 /* Look for an interval following iv that has different
1682 properties. */
1683 for (next_iv = next_interval (iv);
1684 (!NULL_INTERVAL_P (next_iv)
1685 && (NILP (limit)
1686 || XFASTINT (limit) > next_iv->position));
1687 next_iv = next_interval (next_iv))
1688 {
1689 for (p = it_props; p->handler; ++p)
1690 {
1691 Lisp_Object new_value;
1692
1693 new_value = textget (next_iv->plist, *p->name);
1694 if (!EQ (values_here[p->idx], new_value))
1695 break;
1696 }
1697
1698 if (p->handler)
1699 break;
1700 }
1701
1702 if (!NULL_INTERVAL_P (next_iv))
1703 {
1704 if (INTEGERP (limit)
1705 && next_iv->position >= XFASTINT (limit))
1706 /* No text property change up to limit. */
1707 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1708 else
1709 /* Text properties change in next_iv. */
1710 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1711 }
1712 }
1713
1714 xassert (STRINGP (it->string)
1715 || (it->stop_charpos >= BEGV
1716 && it->stop_charpos >= IT_CHARPOS (*it)));
1717}
1718
1719
1720/* Return the position of the next overlay change after POS in
1721 current_buffer. Value is point-max if no overlay change
1722 follows. This is like `next-overlay-change' but doesn't use
1723 xmalloc. */
1724
1725static int
1726next_overlay_change (pos)
1727 int pos;
1728{
1729 int noverlays;
1730 int endpos;
1731 Lisp_Object *overlays;
1732 int len;
1733 int i;
1734
1735 /* Get all overlays at the given position. */
1736 len = 10;
1737 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1738 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1739 if (noverlays > len)
1740 {
1741 len = noverlays;
1742 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1743 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1744 }
1745
1746 /* If any of these overlays ends before endpos,
1747 use its ending point instead. */
1748 for (i = 0; i < noverlays; ++i)
1749 {
1750 Lisp_Object oend;
1751 int oendpos;
1752
1753 oend = OVERLAY_END (overlays[i]);
1754 oendpos = OVERLAY_POSITION (oend);
1755 endpos = min (endpos, oendpos);
1756 }
1757
1758 return endpos;
1759}
1760
1761
1762\f
1763/***********************************************************************
1764 Fontification
1765 ***********************************************************************/
1766
1767/* Handle changes in the `fontified' property of the current buffer by
1768 calling hook functions from Qfontification_functions to fontify
1769 regions of text. */
1770
1771static enum prop_handled
1772handle_fontified_prop (it)
1773 struct it *it;
1774{
1775 Lisp_Object prop, pos;
1776 enum prop_handled handled = HANDLED_NORMALLY;
1777
1778 /* Get the value of the `fontified' property at IT's current buffer
1779 position. (The `fontified' property doesn't have a special
1780 meaning in strings.) If the value is nil, call functions from
1781 Qfontification_functions. */
1782 if (!STRINGP (it->string)
1783 && it->s == NULL
1784 && !NILP (Vfontification_functions)
1785 && (pos = make_number (IT_CHARPOS (*it)),
1786 prop = Fget_char_property (pos, Qfontified, Qnil),
1787 NILP (prop)))
1788 {
1789 Lisp_Object args[2];
1790
1791 /* Run the hook functions. */
1792 args[0] = Qfontification_functions;
1793 args[1] = pos;
1794 Frun_hook_with_args (make_number (2), args);
1795
1796 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1797 something. This avoids an endless loop if they failed to
1798 fontify the text for which reason ever. */
1799 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1800 handled = HANDLED_RECOMPUTE_PROPS;
1801 }
1802
1803 return handled;
1804}
1805
1806
1807\f
1808/***********************************************************************
1809 Faces
1810 ***********************************************************************/
1811
1812/* Set up iterator IT from face properties at its current position.
1813 Called from handle_stop. */
1814
1815static enum prop_handled
1816handle_face_prop (it)
1817 struct it *it;
1818{
1819 int new_face_id, next_stop;
1820
1821 if (!STRINGP (it->string))
1822 {
1823 new_face_id
1824 = face_at_buffer_position (it->w,
1825 IT_CHARPOS (*it),
1826 it->region_beg_charpos,
1827 it->region_end_charpos,
1828 &next_stop,
1829 (IT_CHARPOS (*it)
1830 + TEXT_PROP_DISTANCE_LIMIT),
1831 0);
1832
1833 /* Is this a start of a run of characters with box face?
1834 Caveat: this can be called for a freshly initialized
1835 iterator; face_id is -1 is this case. We know that the new
1836 face will not change until limit, i.e. if the new face has a
1837 box, all characters up to limit will have one. But, as
1838 usual, we don't know whether limit is really the end. */
1839 if (new_face_id != it->face_id)
1840 {
1841 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1842
1843 /* If new face has a box but old face has not, this is
1844 the start of a run of characters with box, i.e. it has
1845 a shadow on the left side. The value of face_id of the
1846 iterator will be -1 if this is the initial call that gets
1847 the face. In this case, we have to look in front of IT's
1848 position and see whether there is a face != new_face_id. */
1849 it->start_of_box_run_p
1850 = (new_face->box != FACE_NO_BOX
1851 && (it->face_id >= 0
1852 || IT_CHARPOS (*it) == BEG
1853 || new_face_id != face_before_it_pos (it)));
1854 it->face_box_p = new_face->box != FACE_NO_BOX;
1855 }
1856 }
1857 else
1858 {
1859 new_face_id
1860 = face_at_string_position (it->w,
1861 it->string,
1862 IT_STRING_CHARPOS (*it),
1863 (it->current.overlay_string_index >= 0
1864 ? IT_CHARPOS (*it)
1865 : 0),
1866 it->region_beg_charpos,
1867 it->region_end_charpos,
1868 &next_stop,
1869 it->base_face_id);
1870
1871#if 0 /* This shouldn't be neccessary. Let's check it. */
1872 /* If IT is used to display a mode line we would really like to
1873 use the mode line face instead of the frame's default face. */
1874 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1875 && new_face_id == DEFAULT_FACE_ID)
1876 new_face_id = MODE_LINE_FACE_ID;
1877#endif
1878
1879 /* Is this a start of a run of characters with box? Caveat:
1880 this can be called for a freshly allocated iterator; face_id
1881 is -1 is this case. We know that the new face will not
1882 change until the next check pos, i.e. if the new face has a
1883 box, all characters up to that position will have a
1884 box. But, as usual, we don't know whether that position
1885 is really the end. */
1886 if (new_face_id != it->face_id)
1887 {
1888 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1889 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1890
1891 /* If new face has a box but old face hasn't, this is the
1892 start of a run of characters with box, i.e. it has a
1893 shadow on the left side. */
1894 it->start_of_box_run_p
1895 = new_face->box && (old_face == NULL || !old_face->box);
1896 it->face_box_p = new_face->box != FACE_NO_BOX;
1897 }
1898 }
1899
1900 it->face_id = new_face_id;
1901 it->charset = CHARSET_ASCII;
1902 return HANDLED_NORMALLY;
1903}
1904
1905
1906/* Compute the face one character before or after the current position
1907 of IT. BEFORE_P non-zero means get the face in front of IT's
1908 position. Value is the id of the face. */
1909
1910static int
1911face_before_or_after_it_pos (it, before_p)
1912 struct it *it;
1913 int before_p;
1914{
1915 int face_id, limit;
1916 int next_check_charpos;
1917 struct text_pos pos;
1918
1919 xassert (it->s == NULL);
1920
1921 if (STRINGP (it->string))
1922 {
1923 /* No face change past the end of the string (for the case
1924 we are padding with spaces). No face change before the
1925 string start. */
1926 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1927 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1928 return it->face_id;
1929
1930 /* Set pos to the position before or after IT's current position. */
1931 if (before_p)
1932 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1933 else
1934 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1935
1936 /* Get the face for ASCII, or unibyte. */
1937 face_id
1938 = face_at_string_position (it->w,
1939 it->string,
1940 CHARPOS (pos),
1941 (it->current.overlay_string_index >= 0
1942 ? IT_CHARPOS (*it)
1943 : 0),
1944 it->region_beg_charpos,
1945 it->region_end_charpos,
1946 &next_check_charpos,
1947 it->base_face_id);
1948
1949 /* Correct the face for charsets different from ASCII. Do it
1950 for the multibyte case only. The face returned above is
1951 suitable for unibyte text if IT->string is unibyte. */
1952 if (STRING_MULTIBYTE (it->string))
1953 {
1954 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1955 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1956 int c, len, charset;
1957
1958 c = STRING_CHAR_AND_LENGTH (p, rest, len);
1959 charset = CHAR_CHARSET (c);
1960 if (charset != CHARSET_ASCII)
1961 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1962 }
1963 }
1964 else
1965 {
1966 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1967 pos = it->current.pos;
1968
1969 if (before_p)
1970 DEC_TEXT_POS (pos);
1971 else
1972 INC_TEXT_POS (pos);
1973
1974 /* Determine face for CHARSET_ASCII, or unibyte. */
1975 face_id = face_at_buffer_position (it->w,
1976 CHARPOS (pos),
1977 it->region_beg_charpos,
1978 it->region_end_charpos,
1979 &next_check_charpos,
1980 limit, 0);
1981
1982 /* Correct the face for charsets different from ASCII. Do it
1983 for the multibyte case only. The face returned above is
1984 suitable for unibyte text if current_buffer is unibyte. */
1985 if (it->multibyte_p)
1986 {
1987 int charset = charset_at_position (pos);
1988 if (charset != CHARSET_ASCII)
1989 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1990 }
1991 }
1992
1993 return face_id;
1994}
1995
1996
1997\f
1998/***********************************************************************
1999 Invisible text
2000 ***********************************************************************/
2001
2002/* Set up iterator IT from invisible properties at its current
2003 position. Called from handle_stop. */
2004
2005static enum prop_handled
2006handle_invisible_prop (it)
2007 struct it *it;
2008{
2009 enum prop_handled handled = HANDLED_NORMALLY;
2010
2011 if (STRINGP (it->string))
2012 {
2013 extern Lisp_Object Qinvisible;
2014 Lisp_Object prop, end_charpos, limit, charpos;
2015
2016 /* Get the value of the invisible text property at the
2017 current position. Value will be nil if there is no such
2018 property. */
2019 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2020 prop = Fget_text_property (charpos, Qinvisible, it->string);
2021
2022 if (!NILP (prop))
2023 {
2024 handled = HANDLED_RECOMPUTE_PROPS;
2025
2026 /* Get the position at which the next change of the
2027 invisible text property can be found in IT->string.
2028 Value will be nil if the property value is the same for
2029 all the rest of IT->string. */
2030 XSETINT (limit, XSTRING (it->string)->size);
2031 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2032 it->string, limit);
2033
2034 /* Text at current position is invisible. The next
2035 change in the property is at position end_charpos.
2036 Move IT's current position to that position. */
2037 if (INTEGERP (end_charpos)
2038 && XFASTINT (end_charpos) < XFASTINT (limit))
2039 {
2040 struct text_pos old;
2041 old = it->current.string_pos;
2042 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2043 compute_string_pos (&it->current.string_pos, old, it->string);
2044 }
2045 else
2046 {
2047 /* The rest of the string is invisible. If this is an
2048 overlay string, proceed with the next overlay string
2049 or whatever comes and return a character from there. */
2050 if (it->current.overlay_string_index >= 0)
2051 {
2052 next_overlay_string (it);
2053 /* Don't check for overlay strings when we just
2054 finished processing them. */
2055 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2056 }
2057 else
2058 {
2059 struct Lisp_String *s = XSTRING (it->string);
2060 IT_STRING_CHARPOS (*it) = s->size;
2061 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2062 }
2063 }
2064 }
2065 }
2066 else
2067 {
2068 int visible_p, newpos, next_stop;
2069 Lisp_Object pos, prop;
2070
2071 /* First of all, is there invisible text at this position? */
2072 XSETFASTINT (pos, IT_CHARPOS (*it));
2073 prop = Fget_char_property (pos, Qinvisible, it->window);
2074
2075 /* If we are on invisible text, skip over it. */
2076 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2077 {
2078 /* Record whether we have to display an ellipsis for the
2079 invisible text. */
2080 int display_ellipsis_p
2081 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2082
2083 handled = HANDLED_RECOMPUTE_PROPS;
2084
2085 /* Loop skipping over invisible text. The loop is left at
2086 ZV or with IT on the first char being visible again. */
2087 do
2088 {
2089 /* Try to skip some invisible text. Return value is the
2090 position reached which can be equal to IT's position
2091 if there is nothing invisible here. This skips both
2092 over invisible text properties and overlays with
2093 invisible property. */
2094 newpos = skip_invisible (IT_CHARPOS (*it),
2095 &next_stop, ZV, it->window);
2096
2097 /* If we skipped nothing at all we weren't at invisible
2098 text in the first place. If everything to the end of
2099 the buffer was skipped, end the loop. */
2100 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2101 visible_p = 1;
2102 else
2103 {
2104 /* We skipped some characters but not necessarily
2105 all there are. Check if we ended up on visible
2106 text. Fget_char_property returns the property of
2107 the char before the given position, i.e. if we
2108 get visible_p = 1, this means that the char at
2109 newpos is visible. */
2110 XSETFASTINT (pos, newpos);
2111 prop = Fget_char_property (pos, Qinvisible, it->window);
2112 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2113 }
2114
2115 /* If we ended up on invisible text, proceed to
2116 skip starting with next_stop. */
2117 if (!visible_p)
2118 IT_CHARPOS (*it) = next_stop;
2119 }
2120 while (!visible_p);
2121
2122 /* The position newpos is now either ZV or on visible text. */
2123 IT_CHARPOS (*it) = newpos;
2124 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2125
2126 /* Maybe return `...' next for the end of the invisible text. */
2127 if (display_ellipsis_p)
2128 {
2129 if (it->dp
2130 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2131 {
2132 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2133 it->dpvec = v->contents;
2134 it->dpend = v->contents + v->size;
2135 }
2136 else
2137 {
2138 /* Default `...'. */
2139 it->dpvec = default_invis_vector;
2140 it->dpend = default_invis_vector + 3;
2141 }
2142
2143 /* The ellipsis display does not replace the display of
2144 the character at the new position. Indicate this by
2145 setting IT->dpvec_char_len to zero. */
2146 it->dpvec_char_len = 0;
2147
2148 it->current.dpvec_index = 0;
2149 it->method = next_element_from_display_vector;
2150 }
2151 }
2152 }
2153
2154 return handled;
2155}
2156
2157
2158\f
2159/***********************************************************************
2160 'display' property
2161 ***********************************************************************/
2162
2163/* Set up iterator IT from `display' property at its current position.
2164 Called from handle_stop. */
2165
2166static enum prop_handled
2167handle_display_prop (it)
2168 struct it *it;
2169{
2170 Lisp_Object prop, object;
2171 struct text_pos *position;
2172 int space_or_image_found_p;
2173
2174 if (STRINGP (it->string))
2175 {
2176 object = it->string;
2177 position = &it->current.string_pos;
2178 }
2179 else
2180 {
2181 object = Qnil;
2182 position = &it->current.pos;
2183 }
2184
2185 /* Reset those iterator values set from display property values. */
2186 it->font_height = Qnil;
2187 it->space_width = Qnil;
2188 it->voffset = 0;
2189
2190 /* We don't support recursive `display' properties, i.e. string
2191 values that have a string `display' property, that have a string
2192 `display' property etc. */
2193 if (!it->string_from_display_prop_p)
2194 it->area = TEXT_AREA;
2195
2196 prop = Fget_char_property (make_number (position->charpos),
2197 Qdisplay, object);
2198 if (NILP (prop))
2199 return HANDLED_NORMALLY;
2200
2201 space_or_image_found_p = 0;
2202 if (CONSP (prop) && CONSP (XCAR (prop)))
2203 {
2204 while (CONSP (prop))
2205 {
2206 if (handle_single_display_prop (it, XCAR (prop), object, position))
2207 space_or_image_found_p = 1;
2208 prop = XCDR (prop);
2209 }
2210 }
2211 else if (VECTORP (prop))
2212 {
2213 int i;
2214 for (i = 0; i < XVECTOR (prop)->size; ++i)
2215 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2216 object, position))
2217 space_or_image_found_p = 1;
2218 }
2219 else
2220 {
2221 if (handle_single_display_prop (it, prop, object, position))
2222 space_or_image_found_p = 1;
2223 }
2224
2225 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2226}
2227
2228
2229/* Value is the position of the end of the `display' property stating
2230 at START_POS in OBJECT. */
2231
2232static struct text_pos
2233display_prop_end (it, object, start_pos)
2234 struct it *it;
2235 Lisp_Object object;
2236 struct text_pos start_pos;
2237{
2238 Lisp_Object end;
2239 struct text_pos end_pos;
2240
2241 /* Characters having this form of property are not displayed, so
2242 we have to find the end of the property. */
2243 end = Fnext_single_property_change (make_number (start_pos.charpos),
2244 Qdisplay, object, Qnil);
2245 if (NILP (end))
2246 {
2247 /* A nil value of `end' means there are no changes of the
2248 property to the end of the buffer or string. */
2249 if (it->current.overlay_string_index >= 0)
2250 end_pos.charpos = XSTRING (it->string)->size;
2251 else
2252 end_pos.charpos = it->end_charpos;
2253 }
2254 else
2255 end_pos.charpos = XFASTINT (end);
2256
2257 if (STRINGP (it->string))
2258 compute_string_pos (&end_pos, start_pos, it->string);
2259 else
2260 end_pos.bytepos = CHAR_TO_BYTE (end_pos.charpos);
2261
2262 return end_pos;
2263}
2264
2265
2266/* Set up IT from a single `display' sub-property value PROP. OBJECT
2267 is the object in which the `display' property was found. *POSITION
2268 is the position at which it was found.
2269
2270 If PROP is a `space' or `image' sub-property, set *POSITION to the
2271 end position of the `display' property.
2272
2273 Value is non-zero if a `space' or `image' property value was found. */
2274
2275static int
2276handle_single_display_prop (it, prop, object, position)
2277 struct it *it;
2278 Lisp_Object prop;
2279 Lisp_Object object;
2280 struct text_pos *position;
2281{
2282 Lisp_Object value;
2283 int space_or_image_found_p = 0;
2284
2285 Lisp_Object form;
2286
2287 /* If PROP is a list of the form `(:when FORM VALUE)', FORM is
2288 evaluated. If the result is nil, VALUE is ignored. */
2289 form = Qt;
2290 if (CONSP (prop) && EQ (XCAR (prop), QCwhen))
2291 {
2292 prop = XCDR (prop);
2293 if (!CONSP (prop))
2294 return 0;
2295 form = XCAR (prop);
2296 prop = XCDR (prop);
2297 if (!CONSP (prop))
2298 return 0;
2299 prop = XCAR (prop);
2300 }
2301
2302 if (!NILP (form) && !EQ (form, Qt))
2303 {
2304 struct gcpro gcpro1;
2305 struct text_pos end_pos, pt;
2306
2307 end_pos = display_prop_end (it, object, *position);
2308 GCPRO1 (form);
2309
2310 /* Temporarily set point to the end position, and then evaluate
2311 the form. This makes `(eolp)' work as FORM. */
2312 CHARPOS (pt) = PT;
2313 BYTEPOS (pt) = PT_BYTE;
2314 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2315 form = eval_form (form);
2316 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2317 UNGCPRO;
2318 }
2319
2320 if (NILP (form))
2321 return 0;
2322
2323 if (CONSP (prop)
2324 && EQ (XCAR (prop), Qheight)
2325 && CONSP (XCDR (prop)))
2326 {
2327 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2328 return 0;
2329
2330 /* `(height HEIGHT)'. */
2331 it->font_height = XCAR (XCDR (prop));
2332 if (!NILP (it->font_height))
2333 {
2334 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2335 int new_height = -1;
2336
2337 if (CONSP (it->font_height)
2338 && (EQ (XCAR (it->font_height), Qplus)
2339 || EQ (XCAR (it->font_height), Qminus))
2340 && CONSP (XCDR (it->font_height))
2341 && INTEGERP (XCAR (XCDR (it->font_height))))
2342 {
2343 /* `(+ N)' or `(- N)' where N is an integer. */
2344 int steps = XINT (XCAR (XCDR (it->font_height)));
2345 if (EQ (XCAR (it->font_height), Qplus))
2346 steps = - steps;
2347 it->face_id = smaller_face (it->f, it->face_id, steps);
2348 }
2349 else if (SYMBOLP (it->font_height))
2350 {
2351 /* Call function with current height as argument.
2352 Value is the new height. */
2353 Lisp_Object form, height;
2354 struct gcpro gcpro1;
2355
2356 height = face->lface[LFACE_HEIGHT_INDEX];
2357 form = Fcons (it->font_height, Fcons (height, Qnil));
2358 GCPRO1 (form);
2359 height = eval_form (form);
2360 if (NUMBERP (height))
2361 new_height = XFLOATINT (height);
2362 UNGCPRO;
2363 }
2364 else if (NUMBERP (it->font_height))
2365 {
2366 /* Value is a multiple of the canonical char height. */
2367 struct face *face;
2368
2369 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2370 new_height = (XFLOATINT (it->font_height)
2371 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2372 }
2373 else
2374 {
2375 /* Evaluate IT->font_height with `height' bound to the
2376 current specified height to get the new height. */
2377 Lisp_Object value;
2378 int count = specpdl_ptr - specpdl;
2379
2380 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2381 value = eval_form (it->font_height);
2382 unbind_to (count, Qnil);
2383
2384 if (NUMBERP (value))
2385 new_height = XFLOATINT (value);
2386 }
2387
2388 if (new_height > 0)
2389 it->face_id = face_with_height (it->f, it->face_id, new_height);
2390 }
2391 }
2392 else if (CONSP (prop)
2393 && EQ (XCAR (prop), Qspace_width)
2394 && CONSP (XCDR (prop)))
2395 {
2396 /* `(space_width WIDTH)'. */
2397 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2398 return 0;
2399
2400 value = XCAR (XCDR (prop));
2401 if (NUMBERP (value) && XFLOATINT (value) > 0)
2402 it->space_width = value;
2403 }
2404 else if (CONSP (prop)
2405 && EQ (XCAR (prop), Qraise)
2406 && CONSP (XCDR (prop)))
2407 {
2408#ifdef HAVE_WINDOW_SYSTEM
2409 /* `(raise FACTOR)'. */
2410 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2411 return 0;
2412
2413 value = XCAR (XCDR (prop));
2414 if (NUMBERP (value))
2415 {
2416 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2417 it->voffset = - (XFLOATINT (value)
2418 * (face->font->ascent + face->font->descent));
2419 }
2420#endif /* HAVE_WINDOW_SYSTEM */
2421 }
2422 else if (!it->string_from_display_prop_p)
2423 {
2424 /* `(left-margin VALUE)' or `(right-margin VALUE)
2425 or `(nil VALUE)' or VALUE. */
2426 Lisp_Object location, value;
2427 struct text_pos start_pos;
2428 int valid_p;
2429
2430 /* Characters having this form of property are not displayed, so
2431 we have to find the end of the property. */
2432 space_or_image_found_p = 1;
2433 start_pos = *position;
2434 *position = display_prop_end (it, object, start_pos);
2435
2436 /* Let's stop at the new position and assume that all
2437 text properties change there. */
2438 it->stop_charpos = position->charpos;
2439
2440 if (CONSP (prop)
2441 && !EQ (XCAR (prop), Qspace)
2442 && !EQ (XCAR (prop), Qimage))
2443 {
2444 location = XCAR (prop);
2445 value = XCDR (prop);
2446 }
2447 else
2448 {
2449 location = Qnil;
2450 value = prop;
2451 }
2452
2453#ifdef HAVE_WINDOW_SYSTEM
2454 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2455 valid_p = STRINGP (value);
2456 else
2457 valid_p = (STRINGP (value)
2458 || (CONSP (value) && EQ (XCAR (value), Qspace))
2459 || valid_image_p (value));
2460#else /* not HAVE_WINDOW_SYSTEM */
2461 valid_p = STRINGP (value);
2462#endif /* not HAVE_WINDOW_SYSTEM */
2463
2464 if ((EQ (location, Qleft_margin)
2465 || EQ (location, Qright_margin)
2466 || NILP (location))
2467 && valid_p)
2468 {
2469 /* Save current settings of IT so that we can restore them
2470 when we are finished with the glyph property value. */
2471 push_it (it);
2472
2473 if (NILP (location))
2474 it->area = TEXT_AREA;
2475 else if (EQ (location, Qleft_margin))
2476 it->area = LEFT_MARGIN_AREA;
2477 else
2478 it->area = RIGHT_MARGIN_AREA;
2479
2480 if (STRINGP (value))
2481 {
2482 it->string = value;
2483 it->multibyte_p = STRING_MULTIBYTE (it->string);
2484 it->current.overlay_string_index = -1;
2485 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2486 it->end_charpos = it->string_nchars
2487 = XSTRING (it->string)->size;
2488 it->method = next_element_from_string;
2489 it->stop_charpos = 0;
2490 it->string_from_display_prop_p = 1;
2491 }
2492 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2493 {
2494 it->method = next_element_from_stretch;
2495 it->object = value;
2496 it->current.pos = it->position = start_pos;
2497 }
2498#ifdef HAVE_WINDOW_SYSTEM
2499 else
2500 {
2501 it->what = IT_IMAGE;
2502 it->image_id = lookup_image (it->f, value);
2503 it->position = start_pos;
2504 it->object = NILP (object) ? it->w->buffer : object;
2505 it->method = next_element_from_image;
2506
2507 /* Say that we don't have consumed the characters with
2508 `display' property yet. The call to pop_it in
2509 set_iterator_to_next will clean this up. */
2510 *position = start_pos;
2511 }
2512#endif /* HAVE_WINDOW_SYSTEM */
2513 }
2514 }
2515
2516 return space_or_image_found_p;
2517}
2518
2519
2520\f
2521/***********************************************************************
2522 Overlay strings
2523 ***********************************************************************/
2524
2525/* The following structure is used to record overlay strings for
2526 later sorting in load_overlay_strings. */
2527
2528struct overlay_entry
2529{
2530 Lisp_Object string;
2531 int priority;
2532 int after_string_p;
2533};
2534
2535
2536/* Set up iterator IT from overlay strings at its current position.
2537 Called from handle_stop. */
2538
2539static enum prop_handled
2540handle_overlay_change (it)
2541 struct it *it;
2542{
2543 /* Overlays are handled in current_buffer only. */
2544 if (STRINGP (it->string))
2545 return HANDLED_NORMALLY;
2546 else
2547 return (get_overlay_strings (it)
2548 ? HANDLED_RECOMPUTE_PROPS
2549 : HANDLED_NORMALLY);
2550}
2551
2552
2553/* Set up the next overlay string for delivery by IT, if there is an
2554 overlay string to deliver. Called by set_iterator_to_next when the
2555 end of the current overlay string is reached. If there are more
2556 overlay strings to display, IT->string and
2557 IT->current.overlay_string_index are set appropriately here.
2558 Otherwise IT->string is set to nil. */
2559
2560static void
2561next_overlay_string (it)
2562 struct it *it;
2563{
2564 ++it->current.overlay_string_index;
2565 if (it->current.overlay_string_index == it->n_overlay_strings)
2566 {
2567 /* No more overlay strings. Restore IT's settings to what
2568 they were before overlay strings were processed, and
2569 continue to deliver from current_buffer. */
2570 pop_it (it);
2571 xassert (it->stop_charpos >= BEGV
2572 && it->stop_charpos <= it->end_charpos);
2573 it->string = Qnil;
2574 it->current.overlay_string_index = -1;
2575 SET_TEXT_POS (it->current.string_pos, -1, -1);
2576 it->n_overlay_strings = 0;
2577 it->method = next_element_from_buffer;
2578 }
2579 else
2580 {
2581 /* There are more overlay strings to process. If
2582 IT->current.overlay_string_index has advanced to a position
2583 where we must load IT->overlay_strings with more strings, do
2584 it. */
2585 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2586
2587 if (it->current.overlay_string_index && i == 0)
2588 load_overlay_strings (it);
2589
2590 /* Initialize IT to deliver display elements from the overlay
2591 string. */
2592 it->string = it->overlay_strings[i];
2593 it->multibyte_p = STRING_MULTIBYTE (it->string);
2594 SET_TEXT_POS (it->current.string_pos, 0, 0);
2595 it->method = next_element_from_string;
2596 it->stop_charpos = 0;
2597 }
2598
2599 CHECK_IT (it);
2600}
2601
2602
2603/* Compare two overlay_entry structures E1 and E2. Used as a
2604 comparison function for qsort in load_overlay_strings. Overlay
2605 strings for the same position are sorted so that
2606
2607 1. All after-strings come in front of before-strings.
2608
2609 2. Within after-strings, strings are sorted so that overlay strings
2610 from overlays with higher priorities come first.
2611
2612 2. Within before-strings, strings are sorted so that overlay
2613 strings from overlays with higher priorities come last.
2614
2615 Value is analogous to strcmp. */
2616
2617
2618static int
2619compare_overlay_entries (e1, e2)
2620 void *e1, *e2;
2621{
2622 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2623 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2624 int result;
2625
2626 if (entry1->after_string_p != entry2->after_string_p)
2627 /* Let after-strings appear in front of before-strings. */
2628 result = entry1->after_string_p ? -1 : 1;
2629 else if (entry1->after_string_p)
2630 /* After-strings sorted in order of decreasing priority. */
2631 result = entry2->priority - entry1->priority;
2632 else
2633 /* Before-strings sorted in order of increasing priority. */
2634 result = entry1->priority - entry2->priority;
2635
2636 return result;
2637}
2638
2639
2640/* Load the vector IT->overlay_strings with overlay strings from IT's
2641 current buffer position. Set IT->n_overlays to the total number of
2642 overlay strings found.
2643
2644 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2645 a time. On entry into load_overlay_strings,
2646 IT->current.overlay_string_index gives the number of overlay
2647 strings that have already been loaded by previous calls to this
2648 function.
2649
2650 Overlay strings are sorted so that after-string strings come in
2651 front of before-string strings. Within before and after-strings,
2652 strings are sorted by overlay priority. See also function
2653 compare_overlay_entries. */
2654
2655static void
2656load_overlay_strings (it)
2657 struct it *it;
2658{
2659 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2660 Lisp_Object ov, overlay, window, str;
2661 int start, end;
2662 int size = 20;
2663 int n = 0, i, j;
2664 struct overlay_entry *entries
2665 = (struct overlay_entry *) alloca (size * sizeof *entries);
2666
2667 /* Append the overlay string STRING of overlay OVERLAY to vector
2668 `entries' which has size `size' and currently contains `n'
2669 elements. AFTER_P non-zero means STRING is an after-string of
2670 OVERLAY. */
2671#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2672 do \
2673 { \
2674 Lisp_Object priority; \
2675 \
2676 if (n == size) \
2677 { \
2678 int new_size = 2 * size; \
2679 struct overlay_entry *old = entries; \
2680 entries = \
2681 (struct overlay_entry *) alloca (new_size \
2682 * sizeof *entries); \
2683 bcopy (old, entries, size * sizeof *entries); \
2684 size = new_size; \
2685 } \
2686 \
2687 entries[n].string = (STRING); \
2688 priority = Foverlay_get ((OVERLAY), Qpriority); \
2689 entries[n].priority \
2690 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2691 entries[n].after_string_p = (AFTER_P); \
2692 ++n; \
2693 } \
2694 while (0)
2695
2696 /* Process overlay before the overlay center. */
2697 for (ov = current_buffer->overlays_before;
2698 CONSP (ov);
2699 ov = XCONS (ov)->cdr)
2700 {
2701 overlay = XCONS (ov)->car;
2702 xassert (OVERLAYP (overlay));
2703 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2704 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2705
2706 if (end < IT_CHARPOS (*it))
2707 break;
2708
2709 /* Skip this overlay if it doesn't start or end at IT's current
2710 position. */
2711 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2712 continue;
2713
2714 /* Skip this overlay if it doesn't apply to IT->w. */
2715 window = Foverlay_get (overlay, Qwindow);
2716 if (WINDOWP (window) && XWINDOW (window) != it->w)
2717 continue;
2718
2719 /* If overlay has a non-empty before-string, record it. */
2720 if (start == IT_CHARPOS (*it)
2721 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2722 && XSTRING (str)->size)
2723 RECORD_OVERLAY_STRING (overlay, str, 0);
2724
2725 /* If overlay has a non-empty after-string, record it. */
2726 if (end == IT_CHARPOS (*it)
2727 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2728 && XSTRING (str)->size)
2729 RECORD_OVERLAY_STRING (overlay, str, 1);
2730 }
2731
2732 /* Process overlays after the overlay center. */
2733 for (ov = current_buffer->overlays_after;
2734 CONSP (ov);
2735 ov = XCONS (ov)->cdr)
2736 {
2737 overlay = XCONS (ov)->car;
2738 xassert (OVERLAYP (overlay));
2739 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2740 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2741
2742 if (start > IT_CHARPOS (*it))
2743 break;
2744
2745 /* Skip this overlay if it doesn't start or end at IT's current
2746 position. */
2747 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2748 continue;
2749
2750 /* Skip this overlay if it doesn't apply to IT->w. */
2751 window = Foverlay_get (overlay, Qwindow);
2752 if (WINDOWP (window) && XWINDOW (window) != it->w)
2753 continue;
2754
2755 /* If overlay has a non-empty before-string, record it. */
2756 if (start == IT_CHARPOS (*it)
2757 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2758 && XSTRING (str)->size)
2759 RECORD_OVERLAY_STRING (overlay, str, 0);
2760
2761 /* If overlay has a non-empty after-string, record it. */
2762 if (end == IT_CHARPOS (*it)
2763 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2764 && XSTRING (str)->size)
2765 RECORD_OVERLAY_STRING (overlay, str, 1);
2766 }
2767
2768#undef RECORD_OVERLAY_STRING
2769
2770 /* Sort entries. */
2771 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2772
2773 /* Record the total number of strings to process. */
2774 it->n_overlay_strings = n;
2775
2776 /* IT->current.overlay_string_index is the number of overlay strings
2777 that have already been consumed by IT. Copy some of the
2778 remaining overlay strings to IT->overlay_strings. */
2779 i = 0;
2780 j = it->current.overlay_string_index;
2781 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2782 it->overlay_strings[i++] = entries[j++].string;
2783
2784 CHECK_IT (it);
2785}
2786
2787
2788/* Get the first chunk of overlay strings at IT's current buffer
2789 position. Value is non-zero if at least one overlay string was
2790 found. */
2791
2792static int
2793get_overlay_strings (it)
2794 struct it *it;
2795{
2796 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2797 process. This fills IT->overlay_strings with strings, and sets
2798 IT->n_overlay_strings to the total number of strings to process.
2799 IT->pos.overlay_string_index has to be set temporarily to zero
2800 because load_overlay_strings needs this; it must be set to -1
2801 when no overlay strings are found because a zero value would
2802 indicate a position in the first overlay string. */
2803 it->current.overlay_string_index = 0;
2804 load_overlay_strings (it);
2805
2806 /* If we found overlay strings, set up IT to deliver display
2807 elements from the first one. Otherwise set up IT to deliver
2808 from current_buffer. */
2809 if (it->n_overlay_strings)
2810 {
2811 /* Make sure we know settings in current_buffer, so that we can
2812 restore meaningful values when we're done with the overlay
2813 strings. */
2814 compute_stop_pos (it);
2815 xassert (it->face_id >= 0);
2816
2817 /* Save IT's settings. They are restored after all overlay
2818 strings have been processed. */
2819 xassert (it->sp == 0);
2820 push_it (it);
2821
2822 /* Set up IT to deliver display elements from the first overlay
2823 string. */
2824 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2825 it->stop_charpos = 0;
2826 it->string = it->overlay_strings[0];
2827 it->multibyte_p = STRING_MULTIBYTE (it->string);
2828 xassert (STRINGP (it->string));
2829 it->method = next_element_from_string;
2830 }
2831 else
2832 {
2833 it->string = Qnil;
2834 it->current.overlay_string_index = -1;
2835 it->method = next_element_from_buffer;
2836 }
2837
2838 CHECK_IT (it);
2839
2840 /* Value is non-zero if we found at least one overlay string. */
2841 return STRINGP (it->string);
2842}
2843
2844
2845\f
2846/***********************************************************************
2847 Saving and restoring state
2848 ***********************************************************************/
2849
2850/* Save current settings of IT on IT->stack. Called, for example,
2851 before setting up IT for an overlay string, to be able to restore
2852 IT's settings to what they were after the overlay string has been
2853 processed. */
2854
2855static void
2856push_it (it)
2857 struct it *it;
2858{
2859 struct iterator_stack_entry *p;
2860
2861 xassert (it->sp < 2);
2862 p = it->stack + it->sp;
2863
2864 p->stop_charpos = it->stop_charpos;
2865 xassert (it->face_id >= 0);
2866 p->face_id = it->face_id;
2867 p->string = it->string;
2868 p->pos = it->current;
2869 p->end_charpos = it->end_charpos;
2870 p->string_nchars = it->string_nchars;
2871 p->area = it->area;
2872 p->multibyte_p = it->multibyte_p;
2873 p->space_width = it->space_width;
2874 p->font_height = it->font_height;
2875 p->voffset = it->voffset;
2876 p->string_from_display_prop_p = it->string_from_display_prop_p;
2877 ++it->sp;
2878}
2879
2880
2881/* Restore IT's settings from IT->stack. Called, for example, when no
2882 more overlay strings must be processed, and we return to delivering
2883 display elements from a buffer, or when the end of a string from a
2884 `display' property is reached and we return to delivering display
2885 elements from an overlay string, or from a buffer. */
2886
2887static void
2888pop_it (it)
2889 struct it *it;
2890{
2891 struct iterator_stack_entry *p;
2892
2893 xassert (it->sp > 0);
2894 --it->sp;
2895 p = it->stack + it->sp;
2896 it->stop_charpos = p->stop_charpos;
2897 it->face_id = p->face_id;
2898 it->string = p->string;
2899 it->current = p->pos;
2900 it->end_charpos = p->end_charpos;
2901 it->string_nchars = p->string_nchars;
2902 it->area = p->area;
2903 it->multibyte_p = p->multibyte_p;
2904 it->space_width = p->space_width;
2905 it->font_height = p->font_height;
2906 it->voffset = p->voffset;
2907 it->string_from_display_prop_p = p->string_from_display_prop_p;
2908}
2909
2910
2911\f
2912/***********************************************************************
2913 Moving over lines
2914 ***********************************************************************/
2915
2916/* Set IT's current position to the previous line start. */
2917
2918static void
2919back_to_previous_line_start (it)
2920 struct it *it;
2921{
2922 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2923 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2924}
2925
2926
2927/* Set IT's current position to the next line start. */
2928
2929static void
2930forward_to_next_line_start (it)
2931 struct it *it;
2932{
2933 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2934 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2935}
2936
2937
2938/* Set IT's current position to the previous visible line start. Skip
2939 invisible text that is so either due to text properties or due to
2940 selective display. Caution: this does not change IT->current_x and
2941 IT->hpos. */
2942
2943static void
2944back_to_previous_visible_line_start (it)
2945 struct it *it;
2946{
2947 int visible_p = 0;
2948
2949 /* Go back one newline if not on BEGV already. */
2950 if (IT_CHARPOS (*it) > BEGV)
2951 back_to_previous_line_start (it);
2952
2953 /* Move over lines that are invisible because of selective display
2954 or text properties. */
2955 while (IT_CHARPOS (*it) > BEGV
2956 && !visible_p)
2957 {
2958 visible_p = 1;
2959
2960 /* If selective > 0, then lines indented more than that values
2961 are invisible. */
2962 if (it->selective > 0
2963 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2964 it->selective))
2965 visible_p = 0;
2966#ifdef USE_TEXT_PROPERTIES
2967 else
2968 {
2969 Lisp_Object prop;
2970
2971 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
2972 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2973 visible_p = 0;
2974 }
2975#endif /* USE_TEXT_PROPERTIES */
2976
2977 /* Back one more newline if the current one is invisible. */
2978 if (!visible_p)
2979 back_to_previous_line_start (it);
2980 }
2981
2982 xassert (IT_CHARPOS (*it) >= BEGV);
2983 xassert (IT_CHARPOS (*it) == BEGV
2984 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
2985 CHECK_IT (it);
2986}
2987
2988
2989/* Reseat iterator IT at the previous visible line start. Skip
2990 invisible text that is so either due to text properties or due to
2991 selective display. At the end, update IT's overlay information,
2992 face information etc. */
2993
2994static void
2995reseat_at_previous_visible_line_start (it)
2996 struct it *it;
2997{
2998 back_to_previous_visible_line_start (it);
2999 reseat (it, it->current.pos, 1);
3000 CHECK_IT (it);
3001}
3002
3003
3004/* Reseat iterator IT on the next visible line start in the current
3005 buffer. Skip over invisible text that is so because of selective
3006 display. Compute faces, overlays etc at the new position. Note
3007 that this function does not skip over text that is invisible
3008 because of text properties. */
3009
3010static void
3011reseat_at_next_visible_line_start (it)
3012 struct it *it;
3013{
3014 /* Restore the buffer position when currently not delivering display
3015 elements from the current buffer. This is the case, for example,
3016 when called at the end of a truncated overlay string. */
3017 while (it->sp)
3018 pop_it (it);
3019 it->method = next_element_from_buffer;
3020
3021 /* Otherwise, scan_buffer would not work. */
3022 if (IT_CHARPOS (*it) < ZV)
3023 {
3024 /* If on a newline, advance past it. Otherwise, find the next
3025 newline which automatically gives us the position following
3026 the newline. */
3027 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3028 {
3029 ++IT_CHARPOS (*it);
3030 ++IT_BYTEPOS (*it);
3031 }
3032 else
3033 forward_to_next_line_start (it);
3034
3035 /* We must either have reached the end of the buffer or end up
3036 after a newline. */
3037 xassert (IT_CHARPOS (*it) == ZV
3038 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3039
3040 /* Skip over lines that are invisible because they are indented
3041 more than the value of IT->selective. */
3042 if (it->selective > 0)
3043 while (IT_CHARPOS (*it) < ZV
3044 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3045 it->selective))
3046 forward_to_next_line_start (it);
3047
3048 /* Set the iterator there. The 0 as the last parameter of
3049 reseat means don't force a text property lookup. The lookup
3050 is then only done if we've skipped past the iterator's
3051 check_charpos'es. This optimization is important because
3052 text property lookups tend to be expensive. */
3053 reseat (it, it->current.pos, 0);
3054 }
3055
3056 CHECK_IT (it);
3057}
3058
3059
3060\f
3061/***********************************************************************
3062 Changing an iterator's position
3063***********************************************************************/
3064
3065/* Change IT's current position to POS in current_buffer. If FORCE_P
3066 is non-zero, always check for text properties at the new position.
3067 Otherwise, text properties are only looked up if POS >=
3068 IT->check_charpos of a property. */
3069
3070static void
3071reseat (it, pos, force_p)
3072 struct it *it;
3073 struct text_pos pos;
3074 int force_p;
3075{
3076 int original_pos = IT_CHARPOS (*it);
3077
3078 reseat_1 (it, pos, 0);
3079
3080 /* Determine where to check text properties. Avoid doing it
3081 where possible because text property lookup is very expensive. */
3082 if (force_p
3083 || CHARPOS (pos) > it->stop_charpos
3084 || CHARPOS (pos) < original_pos)
3085 handle_stop (it);
3086
3087 CHECK_IT (it);
3088}
3089
3090
3091/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3092 IT->stop_pos to POS, also. */
3093
3094static void
3095reseat_1 (it, pos, set_stop_p)
3096 struct it *it;
3097 struct text_pos pos;
3098 int set_stop_p;
3099{
3100 /* Don't call this function when scanning a C string. */
3101 xassert (it->s == NULL);
3102
3103 /* POS must be a reasonable value. */
3104 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3105
3106 it->current.pos = it->position = pos;
3107 XSETBUFFER (it->object, current_buffer);
3108 it->dpvec = NULL;
3109 it->current.dpvec_index = -1;
3110 it->current.overlay_string_index = -1;
3111 IT_STRING_CHARPOS (*it) = -1;
3112 IT_STRING_BYTEPOS (*it) = -1;
3113 it->string = Qnil;
3114 it->method = next_element_from_buffer;
3115 it->sp = 0;
3116
3117 if (set_stop_p)
3118 it->stop_charpos = CHARPOS (pos);
3119}
3120
3121
3122/* Set up IT for displaying a string, starting at CHARPOS in window W.
3123 If S is non-null, it is a C string to iterate over. Otherwise,
3124 STRING gives a Lisp string to iterate over.
3125
3126 If PRECISION > 0, don't return more then PRECISION number of
3127 characters from the string.
3128
3129 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3130 characters have been returned. FIELD_WIDTH < 0 means an infinite
3131 field width.
3132
3133 MULTIBYTE = 0 means disable processing of multibyte characters,
3134 MULTIBYTE > 0 means enable it,
3135 MULTIBYTE < 0 means use IT->multibyte_p.
3136
3137 IT must be initialized via a prior call to init_iterator before
3138 calling this function. */
3139
3140static void
3141reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3142 struct it *it;
3143 unsigned char *s;
3144 Lisp_Object string;
3145 int charpos;
3146 int precision, field_width, multibyte;
3147{
3148 /* No region in strings. */
3149 it->region_beg_charpos = it->region_end_charpos = -1;
3150
3151 /* No text property checks performed by default, but see below. */
3152 it->stop_charpos = -1;
3153
3154 /* Set iterator position and end position. */
3155 bzero (&it->current, sizeof it->current);
3156 it->current.overlay_string_index = -1;
3157 it->current.dpvec_index = -1;
3158 it->charset = CHARSET_ASCII;
3159 xassert (charpos >= 0);
3160
3161 /* Use the setting of MULTIBYTE if specified. */
3162 if (multibyte >= 0)
3163 it->multibyte_p = multibyte > 0;
3164
3165 if (s == NULL)
3166 {
3167 xassert (STRINGP (string));
3168 it->string = string;
3169 it->s = NULL;
3170 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3171 it->method = next_element_from_string;
3172 it->current.string_pos = string_pos (charpos, string);
3173 }
3174 else
3175 {
3176 it->s = s;
3177 it->string = Qnil;
3178
3179 /* Note that we use IT->current.pos, not it->current.string_pos,
3180 for displaying C strings. */
3181 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3182 if (it->multibyte_p)
3183 {
3184 it->current.pos = c_string_pos (charpos, s, 1);
3185 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3186 }
3187 else
3188 {
3189 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3190 it->end_charpos = it->string_nchars = strlen (s);
3191 }
3192
3193 it->method = next_element_from_c_string;
3194 }
3195
3196 /* PRECISION > 0 means don't return more than PRECISION characters
3197 from the string. */
3198 if (precision > 0 && it->end_charpos - charpos > precision)
3199 it->end_charpos = it->string_nchars = charpos + precision;
3200
3201 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3202 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3203 FIELD_WIDTH < 0 means infinite field width. This is useful for
3204 padding with `-' at the end of a mode line. */
3205 if (field_width < 0)
3206 field_width = INFINITY;
3207 if (field_width > it->end_charpos - charpos)
3208 it->end_charpos = charpos + field_width;
3209
3210 /* Use the standard display table for displaying strings. */
3211 if (DISP_TABLE_P (Vstandard_display_table))
3212 it->dp = XCHAR_TABLE (Vstandard_display_table);
3213
3214 it->stop_charpos = charpos;
3215 CHECK_IT (it);
3216}
3217
3218
3219\f
3220/***********************************************************************
3221 Iteration
3222 ***********************************************************************/
3223
3224/* Load IT's display element fields with information about the next
3225 display element from the current position of IT. Value is zero if
3226 end of buffer (or C string) is reached. */
3227
3228int
3229get_next_display_element (it)
3230 struct it *it;
3231{
3232 /* Non-zero means that we found an display element. Zero means that
3233 we hit the end of what we iterate over. Performance note: the
3234 function pointer `method' used here turns out to be faster than
3235 using a sequence of if-statements. */
3236 int success_p = (*it->method) (it);
3237 int charset;
3238
3239 if (it->what == IT_CHARACTER)
3240 {
3241 /* Map via display table or translate control characters.
3242 IT->c, IT->len etc. have been set to the next character by
3243 the function call above. If we have a display table, and it
3244 contains an entry for IT->c, translate it. Don't do this if
3245 IT->c itself comes from a display table, otherwise we could
3246 end up in an infinite recursion. (An alternative could be to
3247 count the recursion depth of this function and signal an
3248 error when a certain maximum depth is reached.) Is it worth
3249 it? */
3250 if (success_p && it->dpvec == NULL)
3251 {
3252 Lisp_Object dv;
3253
3254 if (it->dp
3255 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3256 VECTORP (dv)))
3257 {
3258 struct Lisp_Vector *v = XVECTOR (dv);
3259
3260 /* Return the first character from the display table
3261 entry, if not empty. If empty, don't display the
3262 current character. */
3263 if (v->size)
3264 {
3265 it->dpvec_char_len = it->len;
3266 it->dpvec = v->contents;
3267 it->dpend = v->contents + v->size;
3268 it->current.dpvec_index = 0;
3269 it->method = next_element_from_display_vector;
3270 }
3271
3272 success_p = get_next_display_element (it);
3273 }
3274
3275 /* Translate control characters into `\003' or `^C' form.
3276 Control characters coming from a display table entry are
3277 currently not translated because we use IT->dpvec to hold
3278 the translation. This could easily be changed but I
3279 don't believe that it is worth doing. */
3280 else if ((it->c < ' '
3281 && (it->area != TEXT_AREA
3282 || (it->c != '\n'
3283 && it->c != '\t'
3284 && it->c != '\r')))
54c85a23 3285 || (it->c >= 127
5f5c8ee5
GM
3286 && it->len == 1))
3287 {
3288 /* IT->c is a control character which must be displayed
3289 either as '\003' or as `^C' where the '\\' and '^'
3290 can be defined in the display table. Fill
3291 IT->ctl_chars with glyphs for what we have to
3292 display. Then, set IT->dpvec to these glyphs. */
3293 GLYPH g;
3294
54c85a23 3295 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3296 {
3297 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3298 if (it->dp
3299 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3300 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3301 g = XINT (DISP_CTRL_GLYPH (it->dp));
3302 else
3303 g = FAST_MAKE_GLYPH ('^', 0);
3304 XSETINT (it->ctl_chars[0], g);
3305
3306 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3307 XSETINT (it->ctl_chars[1], g);
3308
3309 /* Set up IT->dpvec and return first character from it. */
3310 it->dpvec_char_len = it->len;
3311 it->dpvec = it->ctl_chars;
3312 it->dpend = it->dpvec + 2;
3313 it->current.dpvec_index = 0;
3314 it->method = next_element_from_display_vector;
3315 get_next_display_element (it);
3316 }
3317 else
3318 {
3319 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3320 if (it->dp
3321 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3322 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3323 g = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3324 else
3325 g = FAST_MAKE_GLYPH ('\\', 0);
3326 XSETINT (it->ctl_chars[0], g);
3327
3328 /* Insert three more glyphs into IT->ctl_chars for
3329 the octal display of the character. */
3330 g = FAST_MAKE_GLYPH (((it->c >> 6) & 7) + '0', 0);
3331 XSETINT (it->ctl_chars[1], g);
3332 g = FAST_MAKE_GLYPH (((it->c >> 3) & 7) + '0', 0);
3333 XSETINT (it->ctl_chars[2], g);
3334 g = FAST_MAKE_GLYPH ((it->c & 7) + '0', 0);
3335 XSETINT (it->ctl_chars[3], g);
3336
3337 /* Set up IT->dpvec and return the first character
3338 from it. */
3339 it->dpvec_char_len = it->len;
3340 it->dpvec = it->ctl_chars;
3341 it->dpend = it->dpvec + 4;
3342 it->current.dpvec_index = 0;
3343 it->method = next_element_from_display_vector;
3344 get_next_display_element (it);
3345 }
3346 }
3347 }
3348
3349 /* Adjust face id if charset changes. There are no charset
3350 changes in unibyte text because Emacs' charsets are not
3351 applicable there. */
3352 if (it->multibyte_p
3353 && success_p
3354 && (charset = CHAR_CHARSET (it->c),
3355 charset != it->charset))
3356 {
3357 it->charset = charset;
3358 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3359 }
3360 }
3361
3362 /* Is this character the last one of a run of characters with
3363 box? If yes, set IT->end_of_box_run_p to 1. */
3364 if (it->face_box_p
3365 && it->s == NULL)
3366 {
3367 int face_id;
3368 struct face *face;
3369
3370 it->end_of_box_run_p
3371 = ((face_id = face_after_it_pos (it),
3372 face_id != it->face_id)
3373 && (face = FACE_FROM_ID (it->f, face_id),
3374 face->box == FACE_NO_BOX));
3375 }
3376
3377 /* Value is 0 if end of buffer or string reached. */
3378 return success_p;
3379}
3380
3381
3382/* Move IT to the next display element.
3383
3384 Functions get_next_display_element and set_iterator_to_next are
3385 separate because I find this arrangement easier to handle than a
3386 get_next_display_element function that also increments IT's
3387 position. The way it is we can first look at an iterator's current
3388 display element, decide whether it fits on a line, and if it does,
3389 increment the iterator position. The other way around we probably
3390 would either need a flag indicating whether the iterator has to be
3391 incremented the next time, or we would have to implement a
3392 decrement position function which would not be easy to write. */
3393
3394void
3395set_iterator_to_next (it)
3396 struct it *it;
3397{
3398 if (it->method == next_element_from_buffer)
3399 {
3400 /* The current display element of IT is a character from
3401 current_buffer. Advance in the buffer, and maybe skip over
3402 invisible lines that are so because of selective display. */
3403 if (ITERATOR_AT_END_OF_LINE_P (it))
3404 reseat_at_next_visible_line_start (it);
3405 else
3406 {
3407 xassert (it->len != 0);
3408 IT_BYTEPOS (*it) += it->len;
3409 IT_CHARPOS (*it) += 1;
3410 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3411 }
3412 }
3413 else if (it->method == next_element_from_c_string)
3414 {
3415 /* Current display element of IT is from a C string. */
3416 IT_BYTEPOS (*it) += it->len;
3417 IT_CHARPOS (*it) += 1;
3418 }
3419 else if (it->method == next_element_from_display_vector)
3420 {
3421 /* Current display element of IT is from a display table entry.
3422 Advance in the display table definition. Reset it to null if
3423 end reached, and continue with characters from buffers/
3424 strings. */
3425 ++it->current.dpvec_index;
3426 it->face_id = it->saved_face_id;
3427 if (it->dpvec + it->current.dpvec_index == it->dpend)
3428 {
3429 if (it->s)
3430 it->method = next_element_from_c_string;
3431 else if (STRINGP (it->string))
3432 it->method = next_element_from_string;
3433 else
3434 it->method = next_element_from_buffer;
3435
3436 it->dpvec = NULL;
3437 it->current.dpvec_index = -1;
3438
3439 /* Consume the character which was displayed via IT->dpvec. */
3440 if (it->dpvec_char_len)
3441 {
3442 it->len = it->dpvec_char_len;
3443 set_iterator_to_next (it);
3444 }
3445 }
3446 }
3447 else if (it->method == next_element_from_string)
3448 {
3449 /* Current display element is a character from a Lisp string. */
3450 xassert (it->s == NULL && STRINGP (it->string));
3451 IT_STRING_BYTEPOS (*it) += it->len;
3452 IT_STRING_CHARPOS (*it) += 1;
3453
3454 consider_string_end:
3455
3456 if (it->current.overlay_string_index >= 0)
3457 {
3458 /* IT->string is an overlay string. Advance to the
3459 next, if there is one. */
3460 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3461 next_overlay_string (it);
3462 }
3463 else
3464 {
3465 /* IT->string is not an overlay string. If we reached
3466 its end, and there is something on IT->stack, proceed
3467 with what is on the stack. This can be either another
3468 string, this time an overlay string, or a buffer. */
3469 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3470 && it->sp > 0)
3471 {
3472 pop_it (it);
3473 if (!STRINGP (it->string))
3474 it->method = next_element_from_buffer;
3475 }
3476 }
3477 }
3478 else if (it->method == next_element_from_image
3479 || it->method == next_element_from_stretch)
3480 {
3481 /* The position etc with which we have to proceed are on
3482 the stack. The position may be at the end of a string,
3483 if the `display' property takes up the whole string. */
3484 pop_it (it);
3485 it->image_id = 0;
3486 if (STRINGP (it->string))
3487 {
3488 it->method = next_element_from_string;
3489 goto consider_string_end;
3490 }
3491 else
3492 it->method = next_element_from_buffer;
3493 }
3494 else
3495 /* There are no other methods defined, so this should be a bug. */
3496 abort ();
3497
3498 /* Reset flags indicating start and end of a sequence of
3499 characters with box. */
3500 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3501
3502 xassert (it->method != next_element_from_string
3503 || (STRINGP (it->string)
3504 && IT_STRING_CHARPOS (*it) >= 0));
3505}
3506
3507
3508/* Load IT's display element fields with information about the next
3509 display element which comes from a display table entry or from the
3510 result of translating a control character to one of the forms `^C'
3511 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3512
3513static int
3514next_element_from_display_vector (it)
3515 struct it *it;
3516{
3517 /* Precondition. */
3518 xassert (it->dpvec && it->current.dpvec_index >= 0);
3519
3520 /* Remember the current face id in case glyphs specify faces.
3521 IT's face is restored in set_iterator_to_next. */
3522 it->saved_face_id = it->face_id;
3523
3524 if (INTEGERP (*it->dpvec)
3525 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3526 {
3527 int lface_id;
3528 GLYPH g;
3529
3530 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3531 it->c = FAST_GLYPH_CHAR (g);
3532 it->len = CHAR_LEN (it->c);
3533
3534 /* The entry may contain a face id to use. Such a face id is
3535 the id of a Lisp face, not a realized face. A face id of
3536 zero means no face. */
3537 lface_id = FAST_GLYPH_FACE (g);
3538 if (lface_id)
3539 {
3540 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3541 if (face_id >= 0)
3542 {
3543 it->face_id = face_id;
3544 it->charset = CHARSET_ASCII;
3545 }
3546 }
3547 }
3548 else
3549 /* Display table entry is invalid. Return a space. */
3550 it->c = ' ', it->len = 1;
3551
3552 /* Don't change position and object of the iterator here. They are
3553 still the values of the character that had this display table
3554 entry or was translated, and that's what we want. */
3555 it->what = IT_CHARACTER;
3556 return 1;
3557}
3558
3559
3560/* Load IT with the next display element from Lisp string IT->string.
3561 IT->current.string_pos is the current position within the string.
3562 If IT->current.overlay_string_index >= 0, the Lisp string is an
3563 overlay string. */
3564
3565static int
3566next_element_from_string (it)
3567 struct it *it;
3568{
3569 struct text_pos position;
3570
3571 xassert (STRINGP (it->string));
3572 xassert (IT_STRING_CHARPOS (*it) >= 0);
3573 position = it->current.string_pos;
3574
3575 /* Time to check for invisible text? */
3576 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3577 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3578 {
3579 handle_stop (it);
3580
3581 /* Since a handler may have changed IT->method, we must
3582 recurse here. */
3583 return get_next_display_element (it);
3584 }
3585
3586 if (it->current.overlay_string_index >= 0)
3587 {
3588 /* Get the next character from an overlay string. In overlay
3589 strings, There is no field width or padding with spaces to
3590 do. */
3591 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3592 {
3593 it->what = IT_EOB;
3594 return 0;
3595 }
3596 else if (STRING_MULTIBYTE (it->string))
3597 {
3598 int remaining = (STRING_BYTES (XSTRING (it->string))
3599 - IT_STRING_BYTEPOS (*it));
3600 unsigned char *s = (XSTRING (it->string)->data
3601 + IT_STRING_BYTEPOS (*it));
3602 it->c = STRING_CHAR_AND_LENGTH (s, remaining, it->len);
3603 }
3604 else
3605 {
3606 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3607 it->len = 1;
3608 }
3609 }
3610 else
3611 {
3612 /* Get the next character from a Lisp string that is not an
3613 overlay string. Such strings come from the mode line, for
3614 example. We may have to pad with spaces, or truncate the
3615 string. See also next_element_from_c_string. */
3616 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3617 {
3618 it->what = IT_EOB;
3619 return 0;
3620 }
3621 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3622 {
3623 /* Pad with spaces. */
3624 it->c = ' ', it->len = 1;
3625 CHARPOS (position) = BYTEPOS (position) = -1;
3626 }
3627 else if (STRING_MULTIBYTE (it->string))
3628 {
3629 int maxlen = (STRING_BYTES (XSTRING (it->string))
3630 - IT_STRING_BYTEPOS (*it));
3631 unsigned char *s = (XSTRING (it->string)->data
3632 + IT_STRING_BYTEPOS (*it));
3633 it->c = STRING_CHAR_AND_LENGTH (s, maxlen, it->len);
3634 }
3635 else
3636 {
3637 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3638 it->len = 1;
3639 }
3640 }
3641
3642 /* Record what we have and where it came from. Note that we store a
3643 buffer position in IT->position although it could arguably be a
3644 string position. */
3645 it->what = IT_CHARACTER;
3646 it->object = it->string;
3647 it->position = position;
3648 return 1;
3649}
3650
3651
3652/* Load IT with next display element from C string IT->s.
3653 IT->string_nchars is the maximum number of characters to return
3654 from the string. IT->end_charpos may be greater than
3655 IT->string_nchars when this function is called, in which case we
3656 may have to return padding spaces. Value is zero if end of string
3657 reached, including padding spaces. */
3658
3659static int
3660next_element_from_c_string (it)
3661 struct it *it;
3662{
3663 int success_p = 1;
3664
3665 xassert (it->s);
3666 it->what = IT_CHARACTER;
3667 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3668 it->object = Qnil;
3669
3670 /* IT's position can be greater IT->string_nchars in case a field
3671 width or precision has been specified when the iterator was
3672 initialized. */
3673 if (IT_CHARPOS (*it) >= it->end_charpos)
3674 {
3675 /* End of the game. */
3676 it->what = IT_EOB;
3677 success_p = 0;
3678 }
3679 else if (IT_CHARPOS (*it) >= it->string_nchars)
3680 {
3681 /* Pad with spaces. */
3682 it->c = ' ', it->len = 1;
3683 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3684 }
3685 else if (it->multibyte_p)
3686 {
3687 /* Implementation note: The calls to strlen apparently aren't a
3688 performance problem because there is no noticeable performance
3689 difference between Emacs running in unibyte or multibyte mode. */
3690 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3691 it->c = STRING_CHAR_AND_LENGTH (it->s + IT_BYTEPOS (*it),
3692 maxlen, it->len);
3693 }
3694 else
3695 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3696
3697 return success_p;
3698}
3699
3700
3701/* Set up IT to return characters from an ellipsis, if appropriate.
3702 The definition of the ellipsis glyphs may come from a display table
3703 entry. This function Fills IT with the first glyph from the
3704 ellipsis if an ellipsis is to be displayed. */
3705
3706static void
3707next_element_from_ellipsis (it)
3708 struct it *it;
3709{
3710 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3711 {
3712 /* Use the display table definition for `...'. Invalid glyphs
3713 will be handled by the method returning elements from dpvec. */
3714 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3715 it->dpvec_char_len = it->len;
3716 it->dpvec = v->contents;
3717 it->dpend = v->contents + v->size;
3718 it->current.dpvec_index = 0;
3719 it->method = next_element_from_display_vector;
3720 get_next_display_element (it);
3721 }
3722 else if (it->selective_display_ellipsis_p)
3723 {
3724 /* Use default `...' which is stored in default_invis_vector. */
3725 it->dpvec_char_len = it->len;
3726 it->dpvec = default_invis_vector;
3727 it->dpend = default_invis_vector + 3;
3728 it->current.dpvec_index = 0;
3729 it->method = next_element_from_display_vector;
3730 get_next_display_element (it);
3731 }
3732}
3733
3734
3735/* Deliver an image display element. The iterator IT is already
3736 filled with image information (done in handle_display_prop). Value
3737 is always 1. */
3738
3739
3740static int
3741next_element_from_image (it)
3742 struct it *it;
3743{
3744 it->what = IT_IMAGE;
3745 return 1;
3746}
3747
3748
3749/* Fill iterator IT with next display element from a stretch glyph
3750 property. IT->object is the value of the text property. Value is
3751 always 1. */
3752
3753static int
3754next_element_from_stretch (it)
3755 struct it *it;
3756{
3757 it->what = IT_STRETCH;
3758 return 1;
3759}
3760
3761
3762/* Load IT with the next display element from current_buffer. Value
3763 is zero if end of buffer reached. IT->stop_charpos is the next
3764 position at which to stop and check for text properties or buffer
3765 end. */
3766
3767static int
3768next_element_from_buffer (it)
3769 struct it *it;
3770{
3771 int success_p = 1;
3772
3773 /* Check this assumption, otherwise, we would never enter the
3774 if-statement, below. */
3775 xassert (IT_CHARPOS (*it) >= BEGV
3776 && IT_CHARPOS (*it) <= it->stop_charpos);
3777
3778 if (IT_CHARPOS (*it) >= it->stop_charpos)
3779 {
3780 if (IT_CHARPOS (*it) >= it->end_charpos)
3781 {
3782 int overlay_strings_follow_p;
3783
3784 /* End of the game, except when overlay strings follow that
3785 haven't been returned yet. */
3786 if (it->overlay_strings_at_end_processed_p)
3787 overlay_strings_follow_p = 0;
3788 else
3789 {
3790 it->overlay_strings_at_end_processed_p = 1;
3791 overlay_strings_follow_p
3792 = get_overlay_strings (it);
3793 }
3794
3795 if (overlay_strings_follow_p)
3796 success_p = get_next_display_element (it);
3797 else
3798 {
3799 it->what = IT_EOB;
3800 it->position = it->current.pos;
3801 success_p = 0;
3802 }
3803 }
3804 else
3805 {
3806 handle_stop (it);
3807 return get_next_display_element (it);
3808 }
3809 }
3810 else
3811 {
3812 /* No face changes, overlays etc. in sight, so just return a
3813 character from current_buffer. */
3814 unsigned char *p;
3815
3816 /* Maybe run the redisplay end trigger hook. Performance note:
3817 This doesn't seem to cost measurable time. */
3818 if (it->redisplay_end_trigger_charpos
3819 && it->glyph_row
3820 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3821 run_redisplay_end_trigger_hook (it);
3822
3823 /* Get the next character, maybe multibyte. */
3824 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3825 if (it->multibyte_p)
3826 {
3827 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3828 - IT_BYTEPOS (*it));
3829 it->c = STRING_CHAR_AND_LENGTH (p, maxlen, it->len);
3830 }
3831 else
3832 it->c = *p, it->len = 1;
3833
3834 /* Record what we have and where it came from. */
3835 it->what = IT_CHARACTER;;
3836 it->object = it->w->buffer;
3837 it->position = it->current.pos;
3838
3839 /* Normally we return the character found above, except when we
3840 really want to return an ellipsis for selective display. */
3841 if (it->selective)
3842 {
3843 if (it->c == '\n')
3844 {
3845 /* A value of selective > 0 means hide lines indented more
3846 than that number of columns. */
3847 if (it->selective > 0
3848 && IT_CHARPOS (*it) + 1 < ZV
3849 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3850 IT_BYTEPOS (*it) + 1,
3851 it->selective))
3852 next_element_from_ellipsis (it);
3853 }
3854 else if (it->c == '\r' && it->selective == -1)
3855 {
3856 /* A value of selective == -1 means that everything from the
3857 CR to the end of the line is invisible, with maybe an
3858 ellipsis displayed for it. */
3859 next_element_from_ellipsis (it);
3860 }
3861 }
3862 }
3863
3864 /* Value is zero if end of buffer reached. */
3865 xassert (!success_p || it->len > 0);
3866 return success_p;
3867}
3868
3869
3870/* Run the redisplay end trigger hook for IT. */
3871
3872static void
3873run_redisplay_end_trigger_hook (it)
3874 struct it *it;
3875{
3876 Lisp_Object args[3];
3877
3878 /* IT->glyph_row should be non-null, i.e. we should be actually
3879 displaying something, or otherwise we should not run the hook. */
3880 xassert (it->glyph_row);
3881
3882 /* Set up hook arguments. */
3883 args[0] = Qredisplay_end_trigger_functions;
3884 args[1] = it->window;
3885 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3886 it->redisplay_end_trigger_charpos = 0;
3887
3888 /* Since we are *trying* to run these functions, don't try to run
3889 them again, even if they get an error. */
3890 it->w->redisplay_end_trigger = Qnil;
3891 Frun_hook_with_args (3, args);
3892
3893 /* Notice if it changed the face of the character we are on. */
3894 handle_face_prop (it);
3895}
3896
3897
3898\f
3899/***********************************************************************
3900 Moving an iterator without producing glyphs
3901 ***********************************************************************/
3902
3903/* Move iterator IT to a specified buffer or X position within one
3904 line on the display without producing glyphs.
3905
3906 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3907 whichever is reached first.
3908
3909 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3910
3911 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3912 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3913 TO_X includes the amount by which a window is horizontally
3914 scrolled.
3915
3916 Value is
3917
3918 MOVE_POS_MATCH_OR_ZV
3919 - when TO_POS or ZV was reached.
3920
3921 MOVE_X_REACHED
3922 -when TO_X was reached before TO_POS or ZV were reached.
3923
3924 MOVE_LINE_CONTINUED
3925 - when we reached the end of the display area and the line must
3926 be continued.
3927
3928 MOVE_LINE_TRUNCATED
3929 - when we reached the end of the display area and the line is
3930 truncated.
3931
3932 MOVE_NEWLINE_OR_CR
3933 - when we stopped at a line end, i.e. a newline or a CR and selective
3934 display is on. */
3935
3936enum move_it_result
3937move_it_in_display_line_to (it, to_charpos, to_x, op)
3938 struct it *it;
3939 int to_charpos, to_x, op;
3940{
3941 enum move_it_result result = MOVE_UNDEFINED;
3942 struct glyph_row *saved_glyph_row;
3943
3944 /* Don't produce glyphs in produce_glyphs. */
3945 saved_glyph_row = it->glyph_row;
3946 it->glyph_row = NULL;
3947
3948#if NO_PROMPT_IN_BUFFER
3949 /* Take a mini-buffer prompt into account. */
3950 if (MINI_WINDOW_P (it->w)
3951 && IT_CHARPOS (*it) == BEGV)
3952 {
3953 it->current_x = minibuf_prompt_pixel_width;
3954 it->hpos = minibuf_prompt_width;
3955 }
3956#endif
3957
3958 while (1)
3959 {
3960 int x, i;
3961
3962 /* Stop when ZV or TO_CHARPOS reached. */
3963 if (!get_next_display_element (it)
3964 || ((op & MOVE_TO_POS) != 0
3965 && BUFFERP (it->object)
3966 && IT_CHARPOS (*it) >= to_charpos))
3967 {
3968 result = MOVE_POS_MATCH_OR_ZV;
3969 break;
3970 }
3971
3972 /* The call to produce_glyphs will get the metrics of the
3973 display element IT is loaded with. We record in x the
3974 x-position before this display element in case it does not
3975 fit on the line. */
3976 x = it->current_x;
3977 PRODUCE_GLYPHS (it);
3978
3979 if (it->area != TEXT_AREA)
3980 {
3981 set_iterator_to_next (it);
3982 continue;
3983 }
3984
3985 /* The number of glyphs we get back in IT->nglyphs will normally
3986 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
3987 character on a terminal frame, or (iii) a line end. For the
3988 second case, IT->nglyphs - 1 padding glyphs will be present
3989 (on X frames, there is only one glyph produced for a
3990 composite character.
3991
3992 The behavior implemented below means, for continuation lines,
3993 that as many spaces of a TAB as fit on the current line are
3994 displayed there. For terminal frames, as many glyphs of a
3995 multi-glyph character are displayed in the current line, too.
3996 This is what the old redisplay code did, and we keep it that
3997 way. Under X, the whole shape of a complex character must
3998 fit on the line or it will be completely displayed in the
3999 next line.
4000
4001 Note that both for tabs and padding glyphs, all glyphs have
4002 the same width. */
4003 if (it->nglyphs)
4004 {
4005 /* More than one glyph or glyph doesn't fit on line. All
4006 glyphs have the same width. */
4007 int single_glyph_width = it->pixel_width / it->nglyphs;
4008 int new_x;
4009
4010 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4011 {
4012 new_x = x + single_glyph_width;
4013
4014 /* We want to leave anything reaching TO_X to the caller. */
4015 if ((op & MOVE_TO_X) && new_x > to_x)
4016 {
4017 it->current_x = x;
4018 result = MOVE_X_REACHED;
4019 break;
4020 }
4021 else if (/* Lines are continued. */
4022 !it->truncate_lines_p
4023 && (/* And glyph doesn't fit on the line. */
4024 new_x > it->last_visible_x
4025 /* Or it fits exactly and we're on a window
4026 system frame. */
4027 || (new_x == it->last_visible_x
4028 && FRAME_WINDOW_P (it->f))))
4029 {
4030 if (/* IT->hpos == 0 means the very first glyph
4031 doesn't fit on the line, e.g. a wide image. */
4032 it->hpos == 0
4033 || (new_x == it->last_visible_x
4034 && FRAME_WINDOW_P (it->f)))
4035 {
4036 ++it->hpos;
4037 it->current_x = new_x;
4038 if (i == it->nglyphs - 1)
4039 set_iterator_to_next (it);
4040 }
4041 else
4042 it->current_x = x;
4043
4044 result = MOVE_LINE_CONTINUED;
4045 break;
4046 }
4047 else if (new_x > it->first_visible_x)
4048 {
4049 /* Glyph is visible. Increment number of glyphs that
4050 would be displayed. */
4051 ++it->hpos;
4052 }
4053 else
4054 {
4055 /* Glyph is completely off the left margin of the display
4056 area. Nothing to do. */
4057 }
4058 }
4059
4060 if (result != MOVE_UNDEFINED)
4061 break;
4062 }
4063 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4064 {
4065 /* Stop when TO_X specified and reached. This check is
4066 necessary here because of lines consisting of a line end,
4067 only. The line end will not produce any glyphs and we
4068 would never get MOVE_X_REACHED. */
4069 xassert (it->nglyphs == 0);
4070 result = MOVE_X_REACHED;
4071 break;
4072 }
4073
4074 /* Is this a line end? If yes, we're done. */
4075 if (ITERATOR_AT_END_OF_LINE_P (it))
4076 {
4077 result = MOVE_NEWLINE_OR_CR;
4078 break;
4079 }
4080
4081 /* The current display element has been consumed. Advance
4082 to the next. */
4083 set_iterator_to_next (it);
4084
4085 /* Stop if lines are truncated and IT's current x-position is
4086 past the right edge of the window now. */
4087 if (it->truncate_lines_p
4088 && it->current_x >= it->last_visible_x)
4089 {
4090 result = MOVE_LINE_TRUNCATED;
4091 break;
4092 }
4093 }
4094
4095 /* Restore the iterator settings altered at the beginning of this
4096 function. */
4097 it->glyph_row = saved_glyph_row;
4098 return result;
4099}
4100
4101
4102/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4103 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4104 the description of enum move_operation_enum.
4105
4106 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4107 screen line, this function will set IT to the next position >
4108 TO_CHARPOS. */
4109
4110void
4111move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4112 struct it *it;
4113 int to_charpos, to_x, to_y, to_vpos;
4114 int op;
4115{
4116 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4117 int line_height;
4118
4119 xassert (XBUFFER (it->w->buffer) == current_buffer);
4120
4121 while (1)
4122 {
4123 if (op & MOVE_TO_VPOS)
4124 {
4125 /* If no TO_CHARPOS and no TO_X specified, stop at the
4126 start of the line TO_VPOS. */
4127 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4128 {
4129 if (it->vpos == to_vpos)
4130 break;
4131 skip = move_it_in_display_line_to (it, -1, -1, 0);
4132 }
4133 else
4134 {
4135 /* TO_VPOS >= 0 means stop at TO_X in the line at
4136 TO_VPOS, or at TO_POS, whichever comes first. */
4137 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4138
4139 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4140 break;
4141 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4142 {
4143 /* We have reached TO_X but not in the line we want. */
4144 skip = move_it_in_display_line_to (it, to_charpos,
4145 -1, MOVE_TO_POS);
4146 if (skip == MOVE_POS_MATCH_OR_ZV)
4147 break;
4148 }
4149 }
4150 }
4151 else if (op & MOVE_TO_Y)
4152 {
4153 struct it it_backup;
4154 int done_p;
4155
4156 /* TO_Y specified means stop at TO_X in the line containing
4157 TO_Y---or at TO_CHARPOS if this is reached first. The
4158 problem is that we can't really tell whether the line
4159 contains TO_Y before we have completely scanned it, and
4160 this may skip past TO_X. What we do is to first scan to
4161 TO_X.
4162
4163 If TO_X is not specified, use a TO_X of zero. The reason
4164 is to make the outcome of this function more predictable.
4165 If we didn't use TO_X == 0, we would stop at the end of
4166 the line which is probably not what a caller would expect
4167 to happen. */
4168 skip = move_it_in_display_line_to (it, to_charpos,
4169 ((op & MOVE_TO_X)
4170 ? to_x : 0),
4171 (MOVE_TO_X
4172 | (op & MOVE_TO_POS)));
4173
4174 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4175 if (skip == MOVE_POS_MATCH_OR_ZV)
4176 break;
4177
4178 /* If TO_X was reached, we would like to know whether TO_Y
4179 is in the line. This can only be said if we know the
4180 total line height which requires us to scan the rest of
4181 the line. */
4182 done_p = 0;
4183 if (skip == MOVE_X_REACHED)
4184 {
4185 it_backup = *it;
4186 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4187 op & MOVE_TO_POS);
4188 }
4189
4190 /* Now, decide whether TO_Y is in this line. */
4191 line_height = it->max_ascent + it->max_descent;
4192
4193 if (to_y >= it->current_y
4194 && to_y < it->current_y + line_height)
4195 {
4196 if (skip == MOVE_X_REACHED)
4197 /* If TO_Y is in this line and TO_X was reached above,
4198 we scanned too far. We have to restore IT's settings
4199 to the ones before skipping. */
4200 *it = it_backup;
4201 done_p = 1;
4202 }
4203 else if (skip == MOVE_X_REACHED)
4204 {
4205 skip = skip2;
4206 if (skip == MOVE_POS_MATCH_OR_ZV)
4207 done_p = 1;
4208 }
4209
4210 if (done_p)
4211 break;
4212 }
4213 else
4214 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4215
4216 switch (skip)
4217 {
4218 case MOVE_POS_MATCH_OR_ZV:
4219 return;
4220
4221 case MOVE_NEWLINE_OR_CR:
4222 set_iterator_to_next (it);
4223 it->continuation_lines_width = 0;
4224 break;
4225
4226 case MOVE_LINE_TRUNCATED:
4227 it->continuation_lines_width = 0;
4228 reseat_at_next_visible_line_start (it);
4229 if ((op & MOVE_TO_POS) != 0
4230 && IT_CHARPOS (*it) > to_charpos)
4231 goto out;
4232 break;
4233
4234 case MOVE_LINE_CONTINUED:
4235 it->continuation_lines_width += it->current_x;
4236 break;
4237
4238 default:
4239 abort ();
4240 }
4241
4242 /* Reset/increment for the next run. */
4243 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4244 it->current_x = it->hpos = 0;
4245 it->current_y += it->max_ascent + it->max_descent;
4246 ++it->vpos;
4247 last_height = it->max_ascent + it->max_descent;
4248 last_max_ascent = it->max_ascent;
4249 it->max_ascent = it->max_descent = 0;
4250 }
4251 out:;
4252}
4253
4254
4255/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4256
4257 If DY > 0, move IT backward at least that many pixels. DY = 0
4258 means move IT backward to the preceding line start or BEGV. This
4259 function may move over more than DY pixels if IT->current_y - DY
4260 ends up in the middle of a line; in this case IT->current_y will be
4261 set to the top of the line moved to. */
4262
4263void
4264move_it_vertically_backward (it, dy)
4265 struct it *it;
4266 int dy;
4267{
4268 int nlines, h, line_height;
4269 struct it it2;
4270 int start_pos = IT_CHARPOS (*it);
4271
4272 xassert (dy >= 0);
4273
4274 /* Estimate how many newlines we must move back. */
4275 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4276
4277 /* Set the iterator's position that many lines back. */
4278 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4279 back_to_previous_visible_line_start (it);
4280
4281 /* Reseat the iterator here. When moving backward, we don't want
4282 reseat to skip forward over invisible text, set up the iterator
4283 to deliver from overlay strings at the new position etc. So,
4284 use reseat_1 here. */
4285 reseat_1 (it, it->current.pos, 1);
4286
4287 /* We are now surely at a line start. */
4288 it->current_x = it->hpos = 0;
4289
4290 /* Move forward and see what y-distance we moved. First move to the
4291 start of the next line so that we get its height. We need this
4292 height to be able to tell whether we reached the specified
4293 y-distance. */
4294 it2 = *it;
4295 it2.max_ascent = it2.max_descent = 0;
4296 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4297 MOVE_TO_POS | MOVE_TO_VPOS);
4298 xassert (IT_CHARPOS (*it) >= BEGV);
4299 line_height = it2.max_ascent + it2.max_descent;
4300 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4301 xassert (IT_CHARPOS (*it) >= BEGV);
4302 h = it2.current_y - it->current_y;
4303 nlines = it2.vpos - it->vpos;
4304
4305 /* Correct IT's y and vpos position. */
4306 it->vpos -= nlines;
4307 it->current_y -= h;
4308
4309 if (dy == 0)
4310 {
4311 /* DY == 0 means move to the start of the screen line. The
4312 value of nlines is > 0 if continuation lines were involved. */
4313 if (nlines > 0)
4314 move_it_by_lines (it, nlines, 1);
4315 xassert (IT_CHARPOS (*it) <= start_pos);
4316 }
4317 else if (nlines)
4318 {
4319 /* The y-position we try to reach. Note that h has been
4320 subtracted in front of the if-statement. */
4321 int target_y = it->current_y + h - dy;
4322
4323 /* If we did not reach target_y, try to move further backward if
4324 we can. If we moved too far backward, try to move forward. */
4325 if (target_y < it->current_y
4326 && IT_CHARPOS (*it) > BEGV)
4327 {
4328 move_it_vertically (it, target_y - it->current_y);
4329 xassert (IT_CHARPOS (*it) >= BEGV);
4330 }
4331 else if (target_y >= it->current_y + line_height
4332 && IT_CHARPOS (*it) < ZV)
4333 {
4334 move_it_vertically (it, target_y - (it->current_y + line_height));
4335 xassert (IT_CHARPOS (*it) >= BEGV);
4336 }
4337 }
4338}
4339
4340
4341/* Move IT by a specified amount of pixel lines DY. DY negative means
4342 move backwards. DY = 0 means move to start of screen line. At the
4343 end, IT will be on the start of a screen line. */
4344
4345void
4346move_it_vertically (it, dy)
4347 struct it *it;
4348 int dy;
4349{
4350 if (dy <= 0)
4351 move_it_vertically_backward (it, -dy);
4352 else if (dy > 0)
4353 {
4354 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4355 MOVE_TO_POS | MOVE_TO_Y);
4356
4357 /* If buffer ends in ZV without a newline, move to the start of
4358 the line to satisfy the post-condition. */
4359 if (IT_CHARPOS (*it) == ZV
4360 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4361 move_it_by_lines (it, 0, 0);
4362 }
4363}
4364
4365
4366/* Return non-zero if some text between buffer positions START_CHARPOS
4367 and END_CHARPOS is invisible. IT->window is the window for text
4368 property lookup. */
4369
4370static int
4371invisible_text_between_p (it, start_charpos, end_charpos)
4372 struct it *it;
4373 int start_charpos, end_charpos;
4374{
4375#ifdef USE_TEXT_PROPERTIES
4376 Lisp_Object prop, limit;
4377 int invisible_found_p;
4378
4379 xassert (it != NULL && start_charpos <= end_charpos);
4380
4381 /* Is text at START invisible? */
4382 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4383 it->window);
4384 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4385 invisible_found_p = 1;
4386 else
4387 {
4388 limit = Fnext_single_property_change (make_number (start_charpos),
4389 Qinvisible,
4390 Fcurrent_buffer (),
4391 make_number (end_charpos));
4392 invisible_found_p = XFASTINT (limit) < end_charpos;
4393 }
4394
4395 return invisible_found_p;
4396
4397#else /* not USE_TEXT_PROPERTIES */
4398 return 0;
4399#endif /* not USE_TEXT_PROPERTIES */
4400}
4401
4402
4403/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4404 negative means move up. DVPOS == 0 means move to the start of the
4405 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4406 NEED_Y_P is zero, IT->current_y will be left unchanged.
4407
4408 Further optimization ideas: If we would know that IT->f doesn't use
4409 a face with proportional font, we could be faster for
4410 truncate-lines nil. */
4411
4412void
4413move_it_by_lines (it, dvpos, need_y_p)
4414 struct it *it;
4415 int dvpos, need_y_p;
4416{
4417 struct position pos;
4418
4419 if (!FRAME_WINDOW_P (it->f))
4420 {
4421 struct text_pos textpos;
4422
4423 /* We can use vmotion on frames without proportional fonts. */
4424 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4425 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4426 reseat (it, textpos, 1);
4427 it->vpos += pos.vpos;
4428 it->current_y += pos.vpos;
4429 }
4430 else if (dvpos == 0)
4431 {
4432 /* DVPOS == 0 means move to the start of the screen line. */
4433 move_it_vertically_backward (it, 0);
4434 xassert (it->current_x == 0 && it->hpos == 0);
4435 }
4436 else if (dvpos > 0)
4437 {
4438 /* If there are no continuation lines, and if there is no
4439 selective display, try the simple method of moving forward
4440 DVPOS newlines, then see where we are. */
4441 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4442 {
4443 int shortage = 0, charpos;
4444
4445 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4446 charpos = IT_CHARPOS (*it) + 1;
4447 else
4448 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4449 &shortage, 0);
4450
4451 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4452 {
4453 struct text_pos pos;
4454 CHARPOS (pos) = charpos;
4455 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4456 reseat (it, pos, 1);
4457 it->vpos += dvpos - shortage;
4458 it->hpos = it->current_x = 0;
4459 return;
4460 }
4461 }
4462
4463 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4464 }
4465 else
4466 {
4467 struct it it2;
4468 int start_charpos, i;
4469
4470 /* If there are no continuation lines, and if there is no
4471 selective display, try the simple method of moving backward
4472 -DVPOS newlines. */
4473 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4474 {
4475 int shortage;
4476 int charpos = IT_CHARPOS (*it);
4477 int bytepos = IT_BYTEPOS (*it);
4478
4479 /* If in the middle of a line, go to its start. */
4480 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4481 {
4482 charpos = find_next_newline_no_quit (charpos, -1);
4483 bytepos = CHAR_TO_BYTE (charpos);
4484 }
4485
4486 if (charpos == BEGV)
4487 {
4488 struct text_pos pos;
4489 CHARPOS (pos) = charpos;
4490 BYTEPOS (pos) = bytepos;
4491 reseat (it, pos, 1);
4492 it->hpos = it->current_x = 0;
4493 return;
4494 }
4495 else
4496 {
4497 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4498 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4499 {
4500 struct text_pos pos;
4501 CHARPOS (pos) = charpos;
4502 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4503 reseat (it, pos, 1);
4504 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4505 it->hpos = it->current_x = 0;
4506 return;
4507 }
4508 }
4509 }
4510
4511 /* Go back -DVPOS visible lines and reseat the iterator there. */
4512 start_charpos = IT_CHARPOS (*it);
4513 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4514 back_to_previous_visible_line_start (it);
4515 reseat (it, it->current.pos, 1);
4516 it->current_x = it->hpos = 0;
4517
4518 /* Above call may have moved too far if continuation lines
4519 are involved. Scan forward and see if it did. */
4520 it2 = *it;
4521 it2.vpos = it2.current_y = 0;
4522 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4523 it->vpos -= it2.vpos;
4524 it->current_y -= it2.current_y;
4525 it->current_x = it->hpos = 0;
4526
4527 /* If we moved too far, move IT some lines forward. */
4528 if (it2.vpos > -dvpos)
4529 {
4530 int delta = it2.vpos + dvpos;
4531 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4532 }
4533 }
4534}
4535
4536
4537\f
4538/***********************************************************************
4539 Messages
4540 ***********************************************************************/
4541
4542
4543/* Output a newline in the *Messages* buffer if "needs" one. */
4544
4545void
4546message_log_maybe_newline ()
4547{
4548 if (message_log_need_newline)
4549 message_dolog ("", 0, 1, 0);
4550}
4551
4552
4553/* Add a string M of length LEN to the message log, optionally
4554 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4555 nonzero, means interpret the contents of M as multibyte. This
4556 function calls low-level routines in order to bypass text property
4557 hooks, etc. which might not be safe to run. */
4558
4559void
4560message_dolog (m, len, nlflag, multibyte)
4561 char *m;
4562 int len, nlflag, multibyte;
4563{
4564 if (!NILP (Vmessage_log_max))
4565 {
4566 struct buffer *oldbuf;
4567 Lisp_Object oldpoint, oldbegv, oldzv;
4568 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4569 int point_at_end = 0;
4570 int zv_at_end = 0;
4571 Lisp_Object old_deactivate_mark, tem;
4572 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4573
4574 old_deactivate_mark = Vdeactivate_mark;
4575 oldbuf = current_buffer;
4576 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4577 current_buffer->undo_list = Qt;
4578
4579 oldpoint = Fpoint_marker ();
4580 oldbegv = Fpoint_min_marker ();
4581 oldzv = Fpoint_max_marker ();
4582 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4583
4584 if (PT == Z)
4585 point_at_end = 1;
4586 if (ZV == Z)
4587 zv_at_end = 1;
4588
4589 BEGV = BEG;
4590 BEGV_BYTE = BEG_BYTE;
4591 ZV = Z;
4592 ZV_BYTE = Z_BYTE;
4593 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4594
4595 /* Insert the string--maybe converting multibyte to single byte
4596 or vice versa, so that all the text fits the buffer. */
4597 if (multibyte
4598 && NILP (current_buffer->enable_multibyte_characters))
4599 {
4600 int i, c, nbytes;
4601 unsigned char work[1];
4602
4603 /* Convert a multibyte string to single-byte
4604 for the *Message* buffer. */
4605 for (i = 0; i < len; i += nbytes)
4606 {
4607 c = STRING_CHAR_AND_LENGTH (m + i, len - i, nbytes);
4608 work[0] = (SINGLE_BYTE_CHAR_P (c)
4609 ? c
4610 : multibyte_char_to_unibyte (c, Qnil));
4611 insert_1_both (work, 1, 1, 1, 0, 0);
4612 }
4613 }
4614 else if (! multibyte
4615 && ! NILP (current_buffer->enable_multibyte_characters))
4616 {
4617 int i, c, nbytes;
4618 unsigned char *msg = (unsigned char *) m;
4619 unsigned char *str, work[4];
4620 /* Convert a single-byte string to multibyte
4621 for the *Message* buffer. */
4622 for (i = 0; i < len; i++)
4623 {
4624 c = unibyte_char_to_multibyte (msg[i]);
4625 nbytes = CHAR_STRING (c, work, str);
4626 insert_1_both (work, 1, nbytes, 1, 0, 0);
4627 }
4628 }
4629 else if (len)
4630 insert_1 (m, len, 1, 0, 0);
4631
4632 if (nlflag)
4633 {
4634 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4635 insert_1 ("\n", 1, 1, 0, 0);
4636
4637 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4638 this_bol = PT;
4639 this_bol_byte = PT_BYTE;
4640
4641 if (this_bol > BEG)
4642 {
4643 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4644 prev_bol = PT;
4645 prev_bol_byte = PT_BYTE;
4646
4647 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4648 this_bol, this_bol_byte);
4649 if (dup)
4650 {
4651 del_range_both (prev_bol, prev_bol_byte,
4652 this_bol, this_bol_byte, 0);
4653 if (dup > 1)
4654 {
4655 char dupstr[40];
4656 int duplen;
4657
4658 /* If you change this format, don't forget to also
4659 change message_log_check_duplicate. */
4660 sprintf (dupstr, " [%d times]", dup);
4661 duplen = strlen (dupstr);
4662 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4663 insert_1 (dupstr, duplen, 1, 0, 1);
4664 }
4665 }
4666 }
4667
4668 if (NATNUMP (Vmessage_log_max))
4669 {
4670 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4671 -XFASTINT (Vmessage_log_max) - 1, 0);
4672 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4673 }
4674 }
4675 BEGV = XMARKER (oldbegv)->charpos;
4676 BEGV_BYTE = marker_byte_position (oldbegv);
4677
4678 if (zv_at_end)
4679 {
4680 ZV = Z;
4681 ZV_BYTE = Z_BYTE;
4682 }
4683 else
4684 {
4685 ZV = XMARKER (oldzv)->charpos;
4686 ZV_BYTE = marker_byte_position (oldzv);
4687 }
4688
4689 if (point_at_end)
4690 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4691 else
4692 /* We can't do Fgoto_char (oldpoint) because it will run some
4693 Lisp code. */
4694 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4695 XMARKER (oldpoint)->bytepos);
4696
4697 UNGCPRO;
4698 free_marker (oldpoint);
4699 free_marker (oldbegv);
4700 free_marker (oldzv);
4701
4702 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4703 set_buffer_internal (oldbuf);
4704 if (NILP (tem))
4705 windows_or_buffers_changed = old_windows_or_buffers_changed;
4706 message_log_need_newline = !nlflag;
4707 Vdeactivate_mark = old_deactivate_mark;
4708 }
4709}
4710
4711
4712/* We are at the end of the buffer after just having inserted a newline.
4713 (Note: We depend on the fact we won't be crossing the gap.)
4714 Check to see if the most recent message looks a lot like the previous one.
4715 Return 0 if different, 1 if the new one should just replace it, or a
4716 value N > 1 if we should also append " [N times]". */
4717
4718static int
4719message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4720 int prev_bol, this_bol;
4721 int prev_bol_byte, this_bol_byte;
4722{
4723 int i;
4724 int len = Z_BYTE - 1 - this_bol_byte;
4725 int seen_dots = 0;
4726 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4727 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4728
4729 for (i = 0; i < len; i++)
4730 {
4731 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4732 && p1[i] != '\n')
4733 seen_dots = 1;
4734 if (p1[i] != p2[i])
4735 return seen_dots;
4736 }
4737 p1 += len;
4738 if (*p1 == '\n')
4739 return 2;
4740 if (*p1++ == ' ' && *p1++ == '[')
4741 {
4742 int n = 0;
4743 while (*p1 >= '0' && *p1 <= '9')
4744 n = n * 10 + *p1++ - '0';
4745 if (strncmp (p1, " times]\n", 8) == 0)
4746 return n+1;
4747 }
4748 return 0;
4749}
4750
4751
4752/* Display an echo area message M with a specified length of LEN
4753 chars. The string may include null characters. If M is 0, clear
4754 out any existing message, and let the mini-buffer text show through.
4755
4756 The buffer M must continue to exist until after the echo area gets
4757 cleared or some other message gets displayed there. This means do
4758 not pass text that is stored in a Lisp string; do not pass text in
4759 a buffer that was alloca'd. */
4760
4761void
4762message2 (m, len, multibyte)
4763 char *m;
4764 int len;
4765 int multibyte;
4766{
4767 /* First flush out any partial line written with print. */
4768 message_log_maybe_newline ();
4769 if (m)
4770 message_dolog (m, len, 1, multibyte);
4771 message2_nolog (m, len, multibyte);
4772}
4773
4774
4775/* The non-logging counterpart of message2. */
4776
4777void
4778message2_nolog (m, len, multibyte)
4779 char *m;
4780 int len;
4781{
4782 message_enable_multibyte = multibyte;
4783
4784 if (noninteractive)
4785 {
4786 if (noninteractive_need_newline)
4787 putc ('\n', stderr);
4788 noninteractive_need_newline = 0;
4789 if (m)
4790 fwrite (m, len, 1, stderr);
4791 if (cursor_in_echo_area == 0)
4792 fprintf (stderr, "\n");
4793 fflush (stderr);
4794 }
4795 /* A null message buffer means that the frame hasn't really been
4796 initialized yet. Error messages get reported properly by
4797 cmd_error, so this must be just an informative message; toss it. */
4798 else if (INTERACTIVE
4799 && selected_frame->glyphs_initialized_p
4800 && FRAME_MESSAGE_BUF (selected_frame))
4801 {
4802 Lisp_Object mini_window;
4803 struct frame *f;
4804
4805 /* Get the frame containing the mini-buffer
4806 that the selected frame is using. */
4807 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4808 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4809
4810 FRAME_SAMPLE_VISIBILITY (f);
4811 if (FRAME_VISIBLE_P (selected_frame)
4812 && ! FRAME_VISIBLE_P (f))
4813 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4814
4815 if (m)
4816 {
4817 echo_area_glyphs = m;
4818 echo_area_glyphs_length = len;
4819 echo_area_message = Qnil;
4820
4821 if (minibuffer_auto_raise)
4822 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4823 }
4824 else
4825 {
4826 echo_area_glyphs = previous_echo_glyphs = NULL;
4827 echo_area_message = previous_echo_area_message = Qnil;
4828 }
4829
4830 do_pending_window_change ();
4831 echo_area_display (1);
4832 do_pending_window_change ();
4833 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4834 (*frame_up_to_date_hook) (f);
4835 }
4836}
4837
4838
4839/* Display an echo area message M with a specified length of LEN
4840 chars. The string may include null characters. If M is not a
4841 string, clear out any existing message, and let the mini-buffer
4842 text show through. */
4843
4844void
4845message3 (m, len, multibyte)
4846 Lisp_Object m;
4847 int len;
4848 int multibyte;
4849{
4850 struct gcpro gcpro1;
4851
4852 GCPRO1 (m);
4853
4854 /* First flush out any partial line written with print. */
4855 message_log_maybe_newline ();
4856 if (STRINGP (m))
4857 message_dolog (XSTRING (m)->data, len, 1, multibyte);
4858 message3_nolog (m, len, multibyte);
4859
4860 UNGCPRO;
4861}
4862
4863
4864/* The non-logging version of message3. */
4865
4866void
4867message3_nolog (m, len, multibyte)
4868 Lisp_Object m;
4869 int len, multibyte;
4870{
4871 message_enable_multibyte = multibyte;
4872
4873 if (noninteractive)
4874 {
4875 if (noninteractive_need_newline)
4876 putc ('\n', stderr);
4877 noninteractive_need_newline = 0;
4878 if (STRINGP (m))
4879 fwrite (XSTRING (m)->data, len, 1, stderr);
4880 if (cursor_in_echo_area == 0)
4881 fprintf (stderr, "\n");
4882 fflush (stderr);
4883 }
4884 /* A null message buffer means that the frame hasn't really been
4885 initialized yet. Error messages get reported properly by
4886 cmd_error, so this must be just an informative message; toss it. */
4887 else if (INTERACTIVE
4888 && selected_frame->glyphs_initialized_p
4889 && FRAME_MESSAGE_BUF (selected_frame))
4890 {
4891 Lisp_Object mini_window;
4892 struct frame *f;
4893
4894 /* Get the frame containing the mini-buffer
4895 that the selected frame is using. */
4896 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4897 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4898
4899 FRAME_SAMPLE_VISIBILITY (f);
4900 if (FRAME_VISIBLE_P (selected_frame)
4901 && ! FRAME_VISIBLE_P (f))
4902 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4903
4904 if (STRINGP (m))
4905 {
4906 echo_area_glyphs = NULL;
4907 echo_area_message = m;
4908 echo_area_glyphs_length = len;
4909
4910 if (minibuffer_auto_raise)
4911 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4912 }
4913 else
4914 {
4915 echo_area_glyphs = previous_echo_glyphs = NULL;
4916 echo_area_message = previous_echo_area_message = Qnil;
4917 }
4918
4919 do_pending_window_change ();
4920 echo_area_display (1);
4921 do_pending_window_change ();
4922 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4923 (*frame_up_to_date_hook) (f);
4924 }
4925}
4926
4927
4928/* Display a null-terminated echo area message M. If M is 0, clear
4929 out any existing message, and let the mini-buffer text show through.
4930
4931 The buffer M must continue to exist until after the echo area gets
4932 cleared or some other message gets displayed there. Do not pass
4933 text that is stored in a Lisp string. Do not pass text in a buffer
4934 that was alloca'd. */
4935
4936void
4937message1 (m)
4938 char *m;
4939{
4940 message2 (m, (m ? strlen (m) : 0), 0);
4941}
4942
4943
4944/* The non-logging counterpart of message1. */
4945
4946void
4947message1_nolog (m)
4948 char *m;
4949{
4950 message2_nolog (m, (m ? strlen (m) : 0), 0);
4951}
4952
4953/* Display a message M which contains a single %s
4954 which gets replaced with STRING. */
4955
4956void
4957message_with_string (m, string, log)
4958 char *m;
4959 Lisp_Object string;
4960 int log;
4961{
4962 if (noninteractive)
4963 {
4964 if (m)
4965 {
4966 if (noninteractive_need_newline)
4967 putc ('\n', stderr);
4968 noninteractive_need_newline = 0;
4969 fprintf (stderr, m, XSTRING (string)->data);
4970 if (cursor_in_echo_area == 0)
4971 fprintf (stderr, "\n");
4972 fflush (stderr);
4973 }
4974 }
4975 else if (INTERACTIVE)
4976 {
4977 /* The frame whose minibuffer we're going to display the message on.
4978 It may be larger than the selected frame, so we need
4979 to use its buffer, not the selected frame's buffer. */
4980 Lisp_Object mini_window;
4981 FRAME_PTR f;
4982
4983 /* Get the frame containing the minibuffer
4984 that the selected frame is using. */
4985 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4986 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4987
4988 /* A null message buffer means that the frame hasn't really been
4989 initialized yet. Error messages get reported properly by
4990 cmd_error, so this must be just an informative message; toss it. */
4991 if (FRAME_MESSAGE_BUF (f))
4992 {
4993 int len;
4994 char *a[1];
4995 a[0] = (char *) XSTRING (string)->data;
4996
4997 len = doprnt (FRAME_MESSAGE_BUF (f),
4998 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
4999
5000 if (log)
5001 message2 (FRAME_MESSAGE_BUF (f), len,
5002 STRING_MULTIBYTE (string));
5003 else
5004 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5005 STRING_MULTIBYTE (string));
5006
5007 /* Print should start at the beginning of the message
5008 buffer next time. */
5009 message_buf_print = 0;
5010 }
5011 }
5012}
5013
5014
5015/* Truncate what will be displayed in the echo area
5016 the next time we display it--but don't redisplay it now. */
5017
5018void
5019truncate_echo_area (len)
5020 int len;
5021{
5022 /* A null message buffer means that the frame hasn't really been
5023 initialized yet. Error messages get reported properly by
5024 cmd_error, so this must be just an informative message; toss it. */
5025 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
5026 echo_area_glyphs_length = len;
5027}
5028
5029
5030/* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by
5031 print; zero if being used by message. */
5032
5033int message_buf_print;
5034
5035
5036/* Dump an informative message to the minibuf. If M is 0, clear out
5037 any existing message, and let the mini-buffer text show through. */
5038
5039/* VARARGS 1 */
5040void
5041message (m, a1, a2, a3)
5042 char *m;
5043 EMACS_INT a1, a2, a3;
5044{
5045 if (noninteractive)
5046 {
5047 if (m)
5048 {
5049 if (noninteractive_need_newline)
5050 putc ('\n', stderr);
5051 noninteractive_need_newline = 0;
5052 fprintf (stderr, m, a1, a2, a3);
5053 if (cursor_in_echo_area == 0)
5054 fprintf (stderr, "\n");
5055 fflush (stderr);
5056 }
5057 }
5058 else if (INTERACTIVE)
5059 {
5060 /* The frame whose mini-buffer we're going to display the message
5061 on. It may be larger than the selected frame, so we need to
5062 use its buffer, not the selected frame's buffer. */
5063 Lisp_Object mini_window;
5064 struct frame *f;
5065
5066 /* Get the frame containing the mini-buffer
5067 that the selected frame is using. */
5068 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5069 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5070
5071 /* A null message buffer means that the frame hasn't really been
5072 initialized yet. Error messages get reported properly by
5073 cmd_error, so this must be just an informative message; toss
5074 it. */
5075 if (FRAME_MESSAGE_BUF (f))
5076 {
5077 if (m)
5078 {
5079 int len;
5080#ifdef NO_ARG_ARRAY
5081 char *a[3];
5082 a[0] = (char *) a1;
5083 a[1] = (char *) a2;
5084 a[2] = (char *) a3;
5085
5086 len = doprnt (FRAME_MESSAGE_BUF (f),
5087 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5088#else
5089 len = doprnt (FRAME_MESSAGE_BUF (f),
5090 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5091 (char **) &a1);
5092#endif /* NO_ARG_ARRAY */
5093
5094 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5095 }
5096 else
5097 message1 (0);
5098
5099 /* Print should start at the beginning of the message
5100 buffer next time. */
5101 message_buf_print = 0;
5102 }
5103 }
5104}
5105
5106
5107/* The non-logging version of message. */
5108
5109void
5110message_nolog (m, a1, a2, a3)
5111 char *m;
5112 EMACS_INT a1, a2, a3;
5113{
5114 Lisp_Object old_log_max;
5115 old_log_max = Vmessage_log_max;
5116 Vmessage_log_max = Qnil;
5117 message (m, a1, a2, a3);
5118 Vmessage_log_max = old_log_max;
5119}
5120
5121
5122/* Display echo_area_message or echo_area_glyphs in the current
5123 mini-buffer. */
5124
5125void
5126update_echo_area ()
5127{
5128 if (STRINGP (echo_area_message))
5129 message3 (echo_area_message, echo_area_glyphs_length,
5130 !NILP (current_buffer->enable_multibyte_characters));
5131 else
5132 message2 (echo_area_glyphs, echo_area_glyphs_length,
5133 !NILP (current_buffer->enable_multibyte_characters));
5134}
5135
5136
5137/* Redisplay the echo area of selected_frame. If UPDATE_FRAME_P is
5138 non-zero update selected_frame. */
5139
5140static void
5141echo_area_display (update_frame_p)
5142 int update_frame_p;
5143{
5144 Lisp_Object mini_window;
5145 struct window *w;
5146 struct frame *f;
5147
5148 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5149 w = XWINDOW (mini_window);
5150 f = XFRAME (WINDOW_FRAME (w));
5151
5152 /* Don't display if frame is invisible or not yet initialized. */
5153 if (!FRAME_VISIBLE_P (f)
5154 || !f->glyphs_initialized_p)
5155 return;
5156
5157 /* When Emacs starts, selected_frame may be a visible terminal
5158 frame, even if we run under a window system. If we let this
5159 through, a message would be displayed on the terminal. */
5160#ifdef HAVE_WINDOW_SYSTEM
5161 if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
5162 return;
5163#endif /* HAVE_WINDOW_SYSTEM */
5164
5165 /* Redraw garbaged frames. */
5166 if (frame_garbaged)
5167 {
5168 /* Old redisplay called redraw_garbaged_frames here which in
5169 turn called redraw_frame which in turn called clear_frame.
5170 The call to clear_frame is a source of flickering. After
5171 checking the places where SET_FRAME_GARBAGED is called, I
5172 believe a clear_frame is not necessary. It should suffice in
5173 the new redisplay to invalidate all current matrices, and
5174 ensure a complete redisplay of all windows. */
5175 Lisp_Object tail, frame;
5176
5177 FOR_EACH_FRAME (tail, frame)
5178 {
5179 struct frame *f = XFRAME (frame);
5180
5181 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5182 {
5183 clear_current_matrices (f);
5184 f->garbaged = 0;
5185 }
5186 }
5187
5188 frame_garbaged = 0;
5189 ++windows_or_buffers_changed;
5190 }
5191
5192 if (echo_area_glyphs
5193 || STRINGP (echo_area_message)
5194 || minibuf_level == 0)
5195 {
5196 struct it it;
5197
5198 echo_area_window = mini_window;
5199 clear_glyph_matrix (w->desired_matrix);
5200 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, DEFAULT_FACE_ID);
5201
5202 if (STRINGP (echo_area_message)
5203 && echo_area_glyphs_length)
5204 {
5205 prepare_desired_row (it.glyph_row);
5206 display_string (NULL, echo_area_message, Qnil, 0, 0,
5207 &it, -1, echo_area_glyphs_length, 0,
5208 message_enable_multibyte);
5209 it.glyph_row->truncated_on_right_p = 0;
5210 compute_line_metrics (&it);
5211 }
5212 else if (echo_area_glyphs
5213 && echo_area_glyphs_length)
5214 {
5215 prepare_desired_row (it.glyph_row);
5216 display_string (echo_area_glyphs, Qnil, Qnil, 0, 0, &it,
5217 -1, echo_area_glyphs_length, 0,
5218 message_enable_multibyte);
5219 it.glyph_row->truncated_on_right_p = 0;
5220 compute_line_metrics (&it);
5221 }
5222 else
5223 blank_row (w, it.glyph_row, 0);
5224
5225 it.glyph_row->y = it.current_y;
5226 it.current_y += it.glyph_row->height;
5227
5228 /* Clear the rest of the lines. */
5229 while (it.current_y < it.last_visible_y)
5230 {
5231 ++it.glyph_row;
5232 blank_row (w, it.glyph_row, it.current_y);
5233 it.current_y += it.glyph_row->height;
5234 }
5235
5236 w->must_be_updated_p = 1;
5237 if (update_frame_p)
5238 {
5239 /* Calling update_single_window is faster when we can use
5240 window-based redisplay. */
5241 if (FRAME_WINDOW_P (f))
5242 {
5243 update_single_window (w, 1);
5244 rif->flush_display (f);
5245 }
5246 else
5247 update_frame (f, 1, 1);
5248 }
5249 }
5250 else if (!EQ (mini_window, selected_window))
5251 windows_or_buffers_changed++;
5252
5253 /* Prevent redisplay optimization in redisplay_internal by resetting
5254 this_line_start_pos. This is done because the mini-buffer now
5255 displays the message instead of its buffer text. */
5256 if (EQ (mini_window, selected_window))
5257 CHARPOS (this_line_start_pos) = 0;
5258
5259 previous_echo_glyphs = echo_area_glyphs;
5260 previous_echo_area_message = echo_area_message;
5261 previous_echo_glyphs_length = echo_area_glyphs_length;
5262}
5263
5264
5265\f
5266/***********************************************************************
5267 Frame Titles
5268 ***********************************************************************/
5269
5270
5271#ifdef HAVE_WINDOW_SYSTEM
5272
5273/* A buffer for constructing frame titles in it; allocated from the
5274 heap in init_xdisp and resized as needed in store_frame_title_char. */
5275
5276static char *frame_title_buf;
5277
5278/* The buffer's end, and a current output position in it. */
5279
5280static char *frame_title_buf_end;
5281static char *frame_title_ptr;
5282
5283
5284/* Store a single character C for the frame title in frame_title_buf.
5285 Re-allocate frame_title_buf if necessary. */
5286
5287static void
5288store_frame_title_char (c)
5289 char c;
5290{
5291 /* If output position has reached the end of the allocated buffer,
5292 double the buffer's size. */
5293 if (frame_title_ptr == frame_title_buf_end)
5294 {
5295 int len = frame_title_ptr - frame_title_buf;
5296 int new_size = 2 * len * sizeof *frame_title_buf;
5297 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5298 frame_title_buf_end = frame_title_buf + new_size;
5299 frame_title_ptr = frame_title_buf + len;
5300 }
5301
5302 *frame_title_ptr++ = c;
5303}
5304
5305
5306/* Store part of a frame title in frame_title_buf, beginning at
5307 frame_title_ptr. STR is the string to store. Do not copy more
5308 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5309 the whole string. Pad with spaces until FIELD_WIDTH number of
5310 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5311 Called from display_mode_element when it is used to build a frame
5312 title. */
5313
5314static int
5315store_frame_title (str, field_width, precision)
5316 unsigned char *str;
5317 int field_width, precision;
5318{
5319 int n = 0;
5320
5321 /* Copy at most PRECISION chars from STR. */
5322 while ((precision <= 0 || n < precision)
5323 && *str)
5324 {
5325 store_frame_title_char (*str++);
5326 ++n;
5327 }
5328
5329 /* Fill up with spaces until FIELD_WIDTH reached. */
5330 while (field_width > 0
5331 && n < field_width)
5332 {
5333 store_frame_title_char (' ');
5334 ++n;
5335 }
5336
5337 return n;
5338}
5339
5340
5341/* Set the title of FRAME, if it has changed. The title format is
5342 Vicon_title_format if FRAME is iconified, otherwise it is
5343 frame_title_format. */
5344
5345static void
5346x_consider_frame_title (frame)
5347 Lisp_Object frame;
5348{
5349 struct frame *f = XFRAME (frame);
5350
5351 if (FRAME_WINDOW_P (f)
5352 || FRAME_MINIBUF_ONLY_P (f)
5353 || f->explicit_name)
5354 {
5355 /* Do we have more than one visible frame on this X display? */
5356 Lisp_Object tail;
5357 Lisp_Object fmt;
5358 struct buffer *obuf;
5359 int len;
5360 struct it it;
5361
5362 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
5363 {
5364 struct frame *tf = XFRAME (XCONS (tail)->car);
5365
5366 if (tf != f
5367 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
5368 && !FRAME_MINIBUF_ONLY_P (tf)
5369 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
5370 break;
5371 }
5372
5373 /* Set global variable indicating that multiple frames exist. */
5374 multiple_frames = CONSP (tail);
5375
5376 /* Switch to the buffer of selected window of the frame. Set up
5377 frame_title_ptr so that display_mode_element will output into it;
5378 then display the title. */
5379 obuf = current_buffer;
5380 Fset_buffer (XWINDOW (f->selected_window)->buffer);
5381 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
5382 frame_title_ptr = frame_title_buf;
5383 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
5384 NULL, DEFAULT_FACE_ID);
5385 len = display_mode_element (&it, 0, -1, -1, fmt);
5386 frame_title_ptr = NULL;
5387 set_buffer_internal (obuf);
5388
5389 /* Set the title only if it's changed. This avoids consing in
5390 the common case where it hasn't. (If it turns out that we've
5391 already wasted too much time by walking through the list with
5392 display_mode_element, then we might need to optimize at a
5393 higher level than this.) */
5394 if (! STRINGP (f->name)
5395 || STRING_BYTES (XSTRING (f->name)) != len
5396 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
5397 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
5398 }
5399}
5400
5401#else /* not HAVE_WINDOW_SYSTEM */
5402
5403#define frame_title_ptr ((char *)0)
5404#define store_frame_title(str, mincol, maxcol) 0
5405
5406#endif /* not HAVE_WINDOW_SYSTEM */
5407
5408
5409
5410\f
5411/***********************************************************************
5412 Menu Bars
5413 ***********************************************************************/
5414
5415
5416/* Prepare for redisplay by updating menu-bar item lists when
5417 appropriate. This can call eval. */
5418
5419void
5420prepare_menu_bars ()
5421{
5422 int all_windows;
5423 struct gcpro gcpro1, gcpro2;
5424 struct frame *f;
5425 struct frame *tooltip_frame;
5426
5427#ifdef HAVE_X_WINDOWS
5428 tooltip_frame = tip_frame;
5429#else
5430 tooltip_frame = NULL;
5431#endif
5432
5433 /* Update all frame titles based on their buffer names, etc. We do
5434 this before the menu bars so that the buffer-menu will show the
5435 up-to-date frame titles. */
5436#ifdef HAVE_WINDOW_SYSTEM
5437 if (windows_or_buffers_changed || update_mode_lines)
5438 {
5439 Lisp_Object tail, frame;
5440
5441 FOR_EACH_FRAME (tail, frame)
5442 {
5443 f = XFRAME (frame);
5444 if (f != tooltip_frame
5445 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
5446 x_consider_frame_title (frame);
5447 }
5448 }
5449#endif /* HAVE_WINDOW_SYSTEM */
5450
5451 /* Update the menu bar item lists, if appropriate. This has to be
5452 done before any actual redisplay or generation of display lines. */
5453 all_windows = (update_mode_lines
5454 || buffer_shared > 1
5455 || windows_or_buffers_changed);
5456 if (all_windows)
5457 {
5458 Lisp_Object tail, frame;
5459 int count = specpdl_ptr - specpdl;
5460
5461 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5462
5463 FOR_EACH_FRAME (tail, frame)
5464 {
5465 f = XFRAME (frame);
5466
5467 /* Ignore tooltip frame. */
5468 if (f == tooltip_frame)
5469 continue;
5470
5471 /* If a window on this frame changed size, report that to
5472 the user and clear the size-change flag. */
5473 if (FRAME_WINDOW_SIZES_CHANGED (f))
5474 {
5475 Lisp_Object functions;
5476
5477 /* Clear flag first in case we get an error below. */
5478 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
5479 functions = Vwindow_size_change_functions;
5480 GCPRO2 (tail, functions);
5481
5482 while (CONSP (functions))
5483 {
5484 call1 (XCAR (functions), frame);
5485 functions = XCDR (functions);
5486 }
5487 UNGCPRO;
5488 }
5489
5490 GCPRO1 (tail);
5491 update_menu_bar (f, 0);
5492#ifdef HAVE_WINDOW_SYSTEM
5493 update_toolbar (f, 0);
5494#endif
5495 UNGCPRO;
5496 }
5497
5498 unbind_to (count, Qnil);
5499 }
5500 else
5501 {
5502 update_menu_bar (selected_frame, 1);
5503#ifdef HAVE_WINDOW_SYSTEM
5504 update_toolbar (selected_frame, 1);
5505#endif
5506 }
5507
5508 /* Motif needs this. See comment in xmenu.c. Turn it off when
5509 pending_menu_activation is not defined. */
5510#ifdef USE_X_TOOLKIT
5511 pending_menu_activation = 0;
5512#endif
5513}
5514
5515
5516/* Update the menu bar item list for frame F. This has to be done
5517 before we start to fill in any display lines, because it can call
5518 eval.
5519
5520 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
5521
5522static void
5523update_menu_bar (f, save_match_data)
5524 struct frame *f;
5525 int save_match_data;
5526{
5527 Lisp_Object window;
5528 register struct window *w;
5529
5530 window = FRAME_SELECTED_WINDOW (f);
5531 w = XWINDOW (window);
5532
5533 if (update_mode_lines)
5534 w->update_mode_line = Qt;
5535
5536 if (FRAME_WINDOW_P (f)
5537 ?
5538#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5539 FRAME_EXTERNAL_MENU_BAR (f)
5540#else
5541 FRAME_MENU_BAR_LINES (f) > 0
5542#endif
5543 : FRAME_MENU_BAR_LINES (f) > 0)
5544 {
5545 /* If the user has switched buffers or windows, we need to
5546 recompute to reflect the new bindings. But we'll
5547 recompute when update_mode_lines is set too; that means
5548 that people can use force-mode-line-update to request
5549 that the menu bar be recomputed. The adverse effect on
5550 the rest of the redisplay algorithm is about the same as
5551 windows_or_buffers_changed anyway. */
5552 if (windows_or_buffers_changed
5553 || !NILP (w->update_mode_line)
5554 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
5555 < BUF_MODIFF (XBUFFER (w->buffer)))
5556 != !NILP (w->last_had_star))
5557 || ((!NILP (Vtransient_mark_mode)
5558 && !NILP (XBUFFER (w->buffer)->mark_active))
5559 != !NILP (w->region_showing)))
5560 {
5561 struct buffer *prev = current_buffer;
5562 int count = specpdl_ptr - specpdl;
5563
5564 set_buffer_internal_1 (XBUFFER (w->buffer));
5565 if (save_match_data)
5566 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5567 if (NILP (Voverriding_local_map_menu_flag))
5568 {
5569 specbind (Qoverriding_terminal_local_map, Qnil);
5570 specbind (Qoverriding_local_map, Qnil);
5571 }
5572
5573 /* Run the Lucid hook. */
5574 call1 (Vrun_hooks, Qactivate_menubar_hook);
5575
5576 /* If it has changed current-menubar from previous value,
5577 really recompute the menu-bar from the value. */
5578 if (! NILP (Vlucid_menu_bar_dirty_flag))
5579 call0 (Qrecompute_lucid_menubar);
5580
5581 safe_run_hooks (Qmenu_bar_update_hook);
5582 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
5583
5584 /* Redisplay the menu bar in case we changed it. */
5585#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5586 if (FRAME_WINDOW_P (f))
5587 set_frame_menubar (f, 0, 0);
5588 else
5589 /* On a terminal screen, the menu bar is an ordinary screen
5590 line, and this makes it get updated. */
5591 w->update_mode_line = Qt;
5592#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
5593 /* In the non-toolkit version, the menu bar is an ordinary screen
5594 line, and this makes it get updated. */
5595 w->update_mode_line = Qt;
5596#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
5597
5598 unbind_to (count, Qnil);
5599 set_buffer_internal_1 (prev);
5600 }
5601 }
5602}
5603
5604
5605\f
5606/***********************************************************************
5607 Toolbars
5608 ***********************************************************************/
5609
5610#ifdef HAVE_WINDOW_SYSTEM
5611
5612/* Update the toolbar item list for frame F. This has to be done
5613 before we start to fill in any display lines. Called from
5614 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
5615 and restore it here. */
5616
5617static void
5618update_toolbar (f, save_match_data)
5619 struct frame *f;
5620 int save_match_data;
5621{
5622 if (WINDOWP (f->toolbar_window)
5623 && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
5624 {
5625 Lisp_Object window;
5626 struct window *w;
5627
5628 window = FRAME_SELECTED_WINDOW (f);
5629 w = XWINDOW (window);
5630
5631 /* If the user has switched buffers or windows, we need to
5632 recompute to reflect the new bindings. But we'll
5633 recompute when update_mode_lines is set too; that means
5634 that people can use force-mode-line-update to request
5635 that the menu bar be recomputed. The adverse effect on
5636 the rest of the redisplay algorithm is about the same as
5637 windows_or_buffers_changed anyway. */
5638 if (windows_or_buffers_changed
5639 || !NILP (w->update_mode_line)
5640 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
5641 < BUF_MODIFF (XBUFFER (w->buffer)))
5642 != !NILP (w->last_had_star))
5643 || ((!NILP (Vtransient_mark_mode)
5644 && !NILP (XBUFFER (w->buffer)->mark_active))
5645 != !NILP (w->region_showing)))
5646 {
5647 struct buffer *prev = current_buffer;
5648 int count = specpdl_ptr - specpdl;
a2889657 5649
5f5c8ee5
GM
5650 /* Set current_buffer to the buffer of the selected
5651 window of the frame, so that we get the right local
5652 keymaps. */
5653 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 5654
5f5c8ee5
GM
5655 /* Save match data, if we must. */
5656 if (save_match_data)
5657 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5658
5659 /* Make sure that we don't accidentally use bogus keymaps. */
5660 if (NILP (Voverriding_local_map_menu_flag))
5661 {
5662 specbind (Qoverriding_terminal_local_map, Qnil);
5663 specbind (Qoverriding_local_map, Qnil);
1f40cad2 5664 }
1f40cad2 5665
5f5c8ee5
GM
5666 /* Build desired toolbar items from keymaps. */
5667 f->desired_toolbar_items
5668 = toolbar_items (f->desired_toolbar_items,
5669 &f->n_desired_toolbar_items);
5670
5671 /* Redisplay the toolbar in case we changed it. */
5672 w->update_mode_line = Qt;
5673
5674 unbind_to (count, Qnil);
5675 set_buffer_internal_1 (prev);
81d478f3 5676 }
a2889657
JB
5677 }
5678}
5679
6c4429a5 5680
5f5c8ee5
GM
5681/* Set F->desired_toolbar_string to a Lisp string representing frame
5682 F's desired toolbar contents. F->desired_toolbar_items must have
5683 been set up previously by calling prepare_menu_bars. */
5684
a2889657 5685static void
5f5c8ee5
GM
5686build_desired_toolbar_string (f)
5687 struct frame *f;
a2889657 5688{
5f5c8ee5
GM
5689 int i, size, size_needed, string_idx;
5690 struct gcpro gcpro1, gcpro2, gcpro3;
5691 Lisp_Object image, plist, props;
a2889657 5692
5f5c8ee5
GM
5693 image = plist = props = Qnil;
5694 GCPRO3 (image, plist, props);
a2889657 5695
5f5c8ee5
GM
5696 /* Prepare F->desired_toolbar_string. If we can reuse it, do so.
5697 Otherwise, make a new string. */
5698
5699 /* The size of the string we might be able to reuse. */
5700 size = (STRINGP (f->desired_toolbar_string)
5701 ? XSTRING (f->desired_toolbar_string)->size
5702 : 0);
5703
5704 /* Each image in the string we build is preceded by a space,
5705 and there is a space at the end. */
5706 size_needed = f->n_desired_toolbar_items + 1;
5707
5708 /* Reuse f->desired_toolbar_string, if possible. */
5709 if (size < size_needed)
5710 f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
5711 else
5712 {
5713 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
5714 Fremove_text_properties (make_number (0), make_number (size),
5715 props, f->desired_toolbar_string);
5716 }
a2889657 5717
5f5c8ee5
GM
5718 /* Put a `display' property on the string for the images to display,
5719 put a `menu_item' property on toolbar items with a value that
5720 is the index of the item in F's toolbar item vector. */
5721 for (i = 0, string_idx = 0;
5722 i < f->n_desired_toolbar_items;
5723 ++i, string_idx += 1)
a2889657 5724 {
5f5c8ee5
GM
5725#define PROP(IDX) \
5726 (XVECTOR (f->desired_toolbar_items) \
5727 ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
5728
5729 int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
5730 int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
5731 int margin, relief;
5732 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
5733 extern Lisp_Object Qlaplace;
5734
5735 /* If image is a vector, choose the image according to the
5736 button state. */
5737 image = PROP (TOOLBAR_ITEM_IMAGES);
5738 if (VECTORP (image))
5739 {
5740 enum toolbar_item_image idx;
5741
5742 if (enabled_p)
5743 idx = (selected_p
5744 ? TOOLBAR_IMAGE_ENABLED_SELECTED
5745 : TOOLBAR_IMAGE_ENABLED_DESELECTED);
5746 else
5747 idx = (selected_p
5748 ? TOOLBAR_IMAGE_DISABLED_SELECTED
5749 : TOOLBAR_IMAGE_DISABLED_DESELECTED);
5750
5751 xassert (XVECTOR (image)->size >= idx);
5752 image = XVECTOR (image)->contents[idx];
5753 }
5754
5755 /* Ignore invalid image specifications. */
5756 if (!valid_image_p (image))
5757 continue;
5758
5759 /* Display the toolbar button pressed, or depressed. */
5760 plist = Fcopy_sequence (XCDR (image));
5761
5762 /* Compute margin and relief to draw. */
5763 relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
5764 margin = relief + max (0, toolbar_button_margin);
5765
5766 if (auto_raise_toolbar_buttons_p)
5767 {
5768 /* Add a `:relief' property to the image spec if the item is
5769 selected. */
5770 if (selected_p)
5771 {
5772 plist = Fplist_put (plist, QCrelief, make_number (-relief));
5773 margin -= relief;
5774 }
5775 }
5776 else
5777 {
5778 /* If image is selected, display it pressed, i.e. with a
5779 negative relief. If it's not selected, display it with a
5780 raised relief. */
5781 plist = Fplist_put (plist, QCrelief,
5782 (selected_p
5783 ? make_number (-relief)
5784 : make_number (relief)));
5785 margin -= relief;
5786 }
5787
5788 /* Put a margin around the image. */
5789 if (margin)
5790 plist = Fplist_put (plist, QCmargin, make_number (margin));
5791
5792 /* If button is not enabled, make the image appear disabled by
5793 applying an appropriate algorithm to it. */
5794 if (!enabled_p)
5795 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
5796
5797 /* Put a `display' text property on the string for the image to
5798 display. Put a `menu-item' property on the string that gives
5799 the start of this item's properties in the toolbar items
5800 vector. */
5801 image = Fcons (Qimage, plist);
5802 props = list4 (Qdisplay, image,
5803 Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
5804 Fadd_text_properties (make_number (string_idx),
5805 make_number (string_idx + 1),
5806 props, f->desired_toolbar_string);
5807#undef PROP
a2889657
JB
5808 }
5809
5f5c8ee5
GM
5810 UNGCPRO;
5811}
5812
5813
5814/* Display one line of the toolbar of frame IT->f. */
5815
5816static void
5817display_toolbar_line (it)
5818 struct it *it;
5819{
5820 struct glyph_row *row = it->glyph_row;
5821 int max_x = it->last_visible_x;
5822 struct glyph *last;
5823
5824 prepare_desired_row (row);
5825 row->y = it->current_y;
5826
5827 while (it->current_x < max_x)
a2889657 5828 {
5f5c8ee5 5829 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 5830
5f5c8ee5
GM
5831 /* Get the next display element. */
5832 if (!get_next_display_element (it))
5833 break;
73af359d 5834
5f5c8ee5
GM
5835 /* Produce glyphs. */
5836 x_before = it->current_x;
5837 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
5838 PRODUCE_GLYPHS (it);
daa37602 5839
5f5c8ee5
GM
5840 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
5841 i = 0;
5842 x = x_before;
5843 while (i < nglyphs)
5844 {
5845 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
5846
5847 if (x + glyph->pixel_width > max_x)
5848 {
5849 /* Glyph doesn't fit on line. */
5850 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
5851 it->current_x = x;
5852 goto out;
5853 }
daa37602 5854
5f5c8ee5
GM
5855 ++it->hpos;
5856 x += glyph->pixel_width;
5857 ++i;
5858 }
5859
5860 /* Stop at line ends. */
5861 if (ITERATOR_AT_END_OF_LINE_P (it))
5862 break;
5863
5864 set_iterator_to_next (it);
a2889657 5865 }
a2889657 5866
5f5c8ee5 5867 out:;
a2889657 5868
5f5c8ee5
GM
5869 row->displays_text_p = row->used[TEXT_AREA] != 0;
5870 extend_face_to_end_of_line (it);
5871 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
5872 last->right_box_line_p = 1;
5873 compute_line_metrics (it);
5874
5875 /* If line is empty, make it occupy the rest of the toolbar. */
5876 if (!row->displays_text_p)
5877 {
5878 row->height = it->last_visible_y - row->y;
5879 row->ascent = 0;
5880 }
5881
5882 row->full_width_p = 1;
5883 row->continued_p = 0;
5884 row->truncated_on_left_p = 0;
5885 row->truncated_on_right_p = 0;
5886
5887 it->current_x = it->hpos = 0;
5888 it->current_y += row->height;
5889 ++it->vpos;
5890 ++it->glyph_row;
a2889657 5891}
96a410bc 5892
5f5c8ee5
GM
5893
5894/* Value is the number of screen lines needed to make all toolbar
5895 items of frame F visible. */
96a410bc 5896
d39b6696 5897static int
5f5c8ee5
GM
5898toolbar_lines_needed (f)
5899 struct frame *f;
d39b6696 5900{
5f5c8ee5
GM
5901 struct window *w = XWINDOW (f->toolbar_window);
5902 struct it it;
5903
5904 /* Initialize an iterator for iteration over F->desired_toolbar_string
5905 in the toolbar window of frame F. */
5906 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
5907 it.first_visible_x = 0;
5908 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
5909 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
5910
5911 while (!ITERATOR_AT_END_P (&it))
5912 {
5913 it.glyph_row = w->desired_matrix->rows;
5914 clear_glyph_row (it.glyph_row);
5915 display_toolbar_line (&it);
5916 }
5917
5918 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 5919}
96a410bc 5920
5f5c8ee5
GM
5921
5922/* Display the toolbar of frame F. Value is non-zero if toolbar's
5923 height should be changed. */
5924
5925static int
5926redisplay_toolbar (f)
5927 struct frame *f;
96a410bc 5928{
5f5c8ee5
GM
5929 struct window *w;
5930 struct it it;
5931 struct glyph_row *row;
5932 int change_height_p = 0;
5933
5934 /* If frame hasn't a toolbar window or if it is zero-height, don't
5935 do anything. This means you must start with toolbar-lines
5936 non-zero to get the auto-sizing effect. Or in other words, you
5937 can turn off toolbars by specifying toolbar-lines zero. */
5938 if (!WINDOWP (f->toolbar_window)
5939 || (w = XWINDOW (f->toolbar_window),
5940 XFASTINT (w->height) == 0))
5941 return 0;
96a410bc 5942
5f5c8ee5
GM
5943 /* Set up an iterator for the toolbar window. */
5944 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
5945 it.first_visible_x = 0;
5946 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
5947 row = it.glyph_row;
3450d04c 5948
5f5c8ee5
GM
5949 /* Build a string that represents the contents of the toolbar. */
5950 build_desired_toolbar_string (f);
5951 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
3450d04c 5952
5f5c8ee5
GM
5953 /* Display as many lines as needed to display all toolbar items. */
5954 while (it.current_y < it.last_visible_y)
5955 display_toolbar_line (&it);
3450d04c 5956
5f5c8ee5
GM
5957 /* It doesn't make much sense to try scrolling in the toolbar
5958 window, so don't do it. */
5959 w->desired_matrix->no_scrolling_p = 1;
5960 w->must_be_updated_p = 1;
3450d04c 5961
5f5c8ee5
GM
5962 if (auto_resize_toolbars_p)
5963 {
5964 int nlines;
5965
5966 /* If there are blank lines at the end, except for a partially
5967 visible blank line at the end that is smaller than
5968 CANON_Y_UNIT, change the toolbar's height. */
5969 row = it.glyph_row - 1;
5970 if (!row->displays_text_p
5971 && row->height >= CANON_Y_UNIT (f))
5972 change_height_p = 1;
5973
5974 /* If row displays toolbar items, but is partially visible,
5975 change the toolbar's height. */
5976 if (row->displays_text_p
5977 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
5978 change_height_p = 1;
5979
5980 /* Resize windows as needed by changing the `toolbar-lines'
5981 frame parameter. */
5982 if (change_height_p
5983 && (nlines = toolbar_lines_needed (f),
5984 nlines != XFASTINT (w->height)))
5985 {
5986 extern Lisp_Object Qtoolbar_lines;
5987 Lisp_Object frame;
5988
5989 XSETFRAME (frame, f);
5990 clear_glyph_matrix (w->desired_matrix);
5991 Fmodify_frame_parameters (frame,
5992 Fcons (Fcons (Qtoolbar_lines,
5993 make_number (nlines)),
5994 Qnil));
5995 fonts_changed_p = 1;
5996 }
5997 }
3450d04c 5998
5f5c8ee5 5999 return change_height_p;
96a410bc 6000}
90adcf20 6001
5f5c8ee5
GM
6002
6003/* Get information about the toolbar item which is displayed in GLYPH
6004 on frame F. Return in *PROP_IDX the index where toolbar item
6005 properties start in F->current_toolbar_items. Value is zero if
6006 GLYPH doesn't display a toolbar item. */
6007
6008int
6009toolbar_item_info (f, glyph, prop_idx)
6010 struct frame *f;
6011 struct glyph *glyph;
6012 int *prop_idx;
90adcf20 6013{
5f5c8ee5
GM
6014 Lisp_Object prop;
6015 int success_p;
6016
6017 /* Get the text property `menu-item' at pos. The value of that
6018 property is the start index of this item's properties in
6019 F->current_toolbar_items. */
6020 prop = Fget_text_property (make_number (glyph->charpos),
6021 Qmenu_item, f->current_toolbar_string);
6022 if (INTEGERP (prop))
6023 {
6024 *prop_idx = XINT (prop);
6025 success_p = 1;
6026 }
6027 else
6028 success_p = 0;
90adcf20 6029
5f5c8ee5
GM
6030 return success_p;
6031}
6032
6033#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6034
feb0c42f 6035
5f5c8ee5
GM
6036\f
6037/************************************************************************
6038 Horizontal scrolling
6039 ************************************************************************/
feb0c42f 6040
5f5c8ee5
GM
6041static int hscroll_window_tree P_ ((Lisp_Object));
6042static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6043
5f5c8ee5
GM
6044/* For all leaf windows in the window tree rooted at WINDOW, set their
6045 hscroll value so that PT is (i) visible in the window, and (ii) so
6046 that it is not within a certain margin at the window's left and
6047 right border. Value is non-zero if any window's hscroll has been
6048 changed. */
6049
6050static int
6051hscroll_window_tree (window)
6052 Lisp_Object window;
6053{
6054 int hscrolled_p = 0;
6055
6056 while (WINDOWP (window))
90adcf20 6057 {
5f5c8ee5
GM
6058 struct window *w = XWINDOW (window);
6059
6060 if (WINDOWP (w->hchild))
6061 hscrolled_p |= hscroll_window_tree (w->hchild);
6062 else if (WINDOWP (w->vchild))
6063 hscrolled_p |= hscroll_window_tree (w->vchild);
6064 else if (w->cursor.vpos >= 0)
6065 {
6066 int hscroll_margin, text_area_x, text_area_y;
6067 int text_area_width, text_area_height;
6068 struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
6069 w->cursor.vpos);
a2725ab2 6070
5f5c8ee5
GM
6071 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6072 &text_area_width, &text_area_height);
90adcf20 6073
5f5c8ee5
GM
6074 /* Scroll when cursor is inside this scroll margin. */
6075 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6076
6077 if ((XFASTINT (w->hscroll)
6078 && w->cursor.x < hscroll_margin)
6079 || (cursor_row->truncated_on_right_p
6080 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 6081 {
5f5c8ee5
GM
6082 struct it it;
6083 int hscroll;
6084 struct buffer *saved_current_buffer;
6085 int pt;
6086
6087 /* Find point in a display of infinite width. */
6088 saved_current_buffer = current_buffer;
6089 current_buffer = XBUFFER (w->buffer);
6090
6091 if (w == XWINDOW (selected_window))
6092 pt = BUF_PT (current_buffer);
6093 else
08b610e4 6094 {
5f5c8ee5
GM
6095 pt = marker_position (w->pointm);
6096 pt = max (BEGV, pt);
6097 pt = min (ZV, pt);
6098 }
6099
6100 /* Move iterator to pt starting at cursor_row->start in
6101 a line with infinite width. */
6102 init_to_row_start (&it, w, cursor_row);
6103 it.last_visible_x = INFINITY;
6104 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6105 current_buffer = saved_current_buffer;
6106
6107 /* Center cursor in window. */
6108 hscroll = (max (0, it.current_x - text_area_width / 2)
6109 / CANON_X_UNIT (it.f));
6110
6111 /* Don't call Fset_window_hscroll if value hasn't
6112 changed because it will prevent redisplay
6113 optimizations. */
6114 if (XFASTINT (w->hscroll) != hscroll)
6115 {
6116 Fset_window_hscroll (window, make_number (hscroll));
6117 hscrolled_p = 1;
08b610e4 6118 }
08b610e4 6119 }
08b610e4 6120 }
a2725ab2 6121
5f5c8ee5 6122 window = w->next;
90adcf20 6123 }
cd6dfed6 6124
5f5c8ee5
GM
6125 /* Value is non-zero if hscroll of any leaf window has been changed. */
6126 return hscrolled_p;
6127}
6128
6129
6130/* Set hscroll so that cursor is visible and not inside horizontal
6131 scroll margins for all windows in the tree rooted at WINDOW. See
6132 also hscroll_window_tree above. Value is non-zero if any window's
6133 hscroll has been changed. If it has, desired matrices on the frame
6134 of WINDOW are cleared. */
6135
6136static int
6137hscroll_windows (window)
6138 Lisp_Object window;
6139{
6140 int hscrolled_p = hscroll_window_tree (window);
6141 if (hscrolled_p)
6142 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6143 return hscrolled_p;
90adcf20 6144}
5f5c8ee5
GM
6145
6146
90adcf20 6147\f
5f5c8ee5
GM
6148/************************************************************************
6149 Redisplay
6150 ************************************************************************/
6151
6152/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6153 to a non-zero value. This is sometimes handy to have in a debugger
6154 session. */
6155
6156#if GLYPH_DEBUG
a2889657 6157
5f5c8ee5
GM
6158/* Values of beg_unchanged and end_unchanged as of last call to
6159 try_window_id. */
6160
6161int debug_beg_unchanged, debug_end_unchanged;
6162
6163/* First and last unchanged row for try_window_id. */
6164
6165int debug_first_unchanged_at_end_vpos;
6166int debug_last_unchanged_at_beg_vpos;
6167
6168/* Delta vpos and y. */
6169
6170int debug_dvpos, debug_dy;
6171
6172/* Delta in characters and bytes for try_window_id. */
6173
6174int debug_delta, debug_delta_bytes;
6175
6176/* Values of window_end_pos and window_end_vpos at the end of
6177 try_window_id. */
6178
6179int debug_end_pos, debug_end_vpos;
6180
6181/* Append a string to W->desired_matrix->method. FMT is a printf
6182 format string. A1...A9 are a supplement for a variable-length
6183 argument list. If trace_redisplay_p is non-zero also printf the
6184 resulting string to stderr. */
6185
6186static void
6187debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6188 struct window *w;
6189 char *fmt;
6190 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6191{
6192 char buffer[512];
6193 char *method = w->desired_matrix->method;
6194 int len = strlen (method);
6195 int size = sizeof w->desired_matrix->method;
6196 int remaining = size - len - 1;
6197
6198 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6199 if (len && remaining)
6200 {
6201 method[len] = '|';
6202 --remaining, ++len;
6203 }
6204
6205 strncpy (method + len, buffer, remaining);
6206
6207 if (trace_redisplay_p)
6208 fprintf (stderr, "%p (%s): %s\n",
6209 w,
6210 ((BUFFERP (w->buffer)
6211 && STRINGP (XBUFFER (w->buffer)->name))
6212 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6213 : "no buffer"),
6214 buffer);
6215}
a2889657 6216
5f5c8ee5 6217#endif /* GLYPH_DEBUG */
90adcf20 6218
a2889657 6219
5f5c8ee5
GM
6220/* This counter is used to clear the face cache every once in a while
6221 in redisplay_internal. It is incremented for each redisplay.
6222 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6223 cleared. */
0d231165 6224
5f5c8ee5 6225#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
6226static int clear_face_cache_count;
6227
20de20dc 6228/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
6229
6230static struct frame *previous_terminal_frame;
6231
6232/* Non-zero while redisplay_internal is in progress. */
6233
6234int redisplaying_p;
6235
6236
6237/* Value is non-zero if all changes in window W, which displays
6238 current_buffer, are in the text between START and END. START is a
6239 buffer position, END is given as a distance from Z. Used in
6240 redisplay_internal for display optimization. */
6241
6242static INLINE int
6243text_outside_line_unchanged_p (w, start, end)
6244 struct window *w;
6245 int start, end;
6246{
6247 int unchanged_p = 1;
6248
6249 /* If text or overlays have changed, see where. */
6250 if (XFASTINT (w->last_modified) < MODIFF
6251 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6252 {
6253 /* Gap in the line? */
6254 if (GPT < start || Z - GPT < end)
6255 unchanged_p = 0;
6256
6257 /* Changes start in front of the line, or end after it? */
6258 if (unchanged_p
6259 && (beg_unchanged < start - 1
6260 || end_unchanged < end))
6261 unchanged_p = 0;
6262
6263 /* If selective display, can't optimize if changes start at the
6264 beginning of the line. */
6265 if (unchanged_p
6266 && INTEGERP (current_buffer->selective_display)
6267 && XINT (current_buffer->selective_display) > 0
6268 && (beg_unchanged < start || GPT <= start))
6269 unchanged_p = 0;
6270 }
6271
6272 return unchanged_p;
6273}
6274
6275
6276/* Do a frame update, taking possible shortcuts into account. This is
6277 the main external entry point for redisplay.
6278
6279 If the last redisplay displayed an echo area message and that message
6280 is no longer requested, we clear the echo area or bring back the
6281 mini-buffer if that is in use. */
20de20dc 6282
a2889657
JB
6283void
6284redisplay ()
e9874cee
RS
6285{
6286 redisplay_internal (0);
6287}
6288
5f5c8ee5
GM
6289
6290/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6291 response to any user action; therefore, we should preserve the echo
6292 area. (Actually, our caller does that job.) Perhaps in the future
6293 avoid recentering windows if it is not necessary; currently that
6294 causes some problems. */
e9874cee
RS
6295
6296static void
6297redisplay_internal (preserve_echo_area)
6298 int preserve_echo_area;
a2889657 6299{
5f5c8ee5
GM
6300 struct window *w = XWINDOW (selected_window);
6301 struct frame *f = XFRAME (w->frame);
6302 int pause;
a2889657 6303 int must_finish = 0;
5f5c8ee5 6304 struct text_pos tlbufpos, tlendpos;
89819bdd 6305 int number_of_visible_frames;
a2889657 6306
5f5c8ee5
GM
6307 /* Non-zero means redisplay has to consider all windows on all
6308 frames. Zero means, only selected_window is considered. */
6309 int consider_all_windows_p;
6310
6311 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6312
6313 /* No redisplay if running in batch mode or frame is not yet fully
6314 initialized, or redisplay is explicitly turned off by setting
6315 Vinhibit_redisplay. */
6316 if (noninteractive
6317 || !NILP (Vinhibit_redisplay)
6318 || !f->glyphs_initialized_p)
a2889657
JB
6319 return;
6320
5f5c8ee5
GM
6321 /* The flag redisplay_performed_directly_p is set by
6322 direct_output_for_insert when it already did the whole screen
6323 update necessary. */
6324 if (redisplay_performed_directly_p)
6325 {
6326 redisplay_performed_directly_p = 0;
6327 if (!hscroll_windows (selected_window))
6328 return;
6329 }
6330
15f0cf78
RS
6331#ifdef USE_X_TOOLKIT
6332 if (popup_activated ())
6333 return;
6334#endif
6335
5f5c8ee5 6336 if (redisplaying_p)
735c094c 6337 return;
5f5c8ee5 6338 ++redisplaying_p;
735c094c 6339
8b32d885
RS
6340 retry:
6341
5f5c8ee5
GM
6342 /* If new fonts have been loaded that make a glyph matrix adjustment
6343 necessary, do it. */
6344 if (fonts_changed_p)
6345 {
6346 adjust_glyphs (NULL);
6347 ++windows_or_buffers_changed;
6348 fonts_changed_p = 0;
6349 }
6350
fd8ff63d 6351 if (! FRAME_WINDOW_P (selected_frame)
20de20dc
RS
6352 && previous_terminal_frame != selected_frame)
6353 {
5f5c8ee5
GM
6354 /* Since frames on an ASCII terminal share the same display
6355 area, displaying a different frame means redisplay the whole
6356 thing. */
20de20dc
RS
6357 windows_or_buffers_changed++;
6358 SET_FRAME_GARBAGED (selected_frame);
6359 XSETFRAME (Vterminal_frame, selected_frame);
6360 }
6361 previous_terminal_frame = selected_frame;
20de20dc 6362
5f5c8ee5
GM
6363 /* Set the visible flags for all frames. Do this before checking
6364 for resized or garbaged frames; they want to know if their frames
6365 are visible. See the comment in frame.h for
6366 FRAME_SAMPLE_VISIBILITY. */
d724d989 6367 {
35f56f96 6368 Lisp_Object tail, frame;
d724d989 6369
89819bdd
RS
6370 number_of_visible_frames = 0;
6371
35f56f96 6372 FOR_EACH_FRAME (tail, frame)
f82aff7c 6373 {
5f5c8ee5
GM
6374 struct frame *f = XFRAME (frame);
6375
6376 FRAME_SAMPLE_VISIBILITY (f);
6377 if (FRAME_VISIBLE_P (f))
6378 ++number_of_visible_frames;
6379 clear_desired_matrices (f);
f82aff7c 6380 }
d724d989
JB
6381 }
6382
44fa5b1e 6383 /* Notice any pending interrupt request to change frame size. */
a2889657
JB
6384 do_pending_window_change ();
6385
5f5c8ee5 6386 /* Clear frames marked as garbaged. */
44fa5b1e 6387 if (frame_garbaged)
a2889657 6388 {
5f5c8ee5
GM
6389 /* Old redisplay called redraw_garbaged_frames here which in
6390 turn called redraw_frame which in turn called clear_frame.
6391 The call to clear_frame is a source of flickering. After
6392 checking the places where SET_FRAME_GARBAGED is called, I
6393 believe a clear_frame is not necessary. It should suffice in
6394 the new redisplay to invalidate all current matrices, and
6395 ensure a complete redisplay of all windows. */
6396 Lisp_Object tail, frame;
6397
6398 FOR_EACH_FRAME (tail, frame)
6399 {
6400 struct frame *f = XFRAME (frame);
6401
6402 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6403 {
6404 clear_current_matrices (f);
6405 f->garbaged = 0;
6406 }
6407 }
6408
44fa5b1e 6409 frame_garbaged = 0;
5f5c8ee5 6410 ++windows_or_buffers_changed;
a2889657
JB
6411 }
6412
5f5c8ee5 6413 /* Build menubar and toolbar items. */
f82aff7c
RS
6414 prepare_menu_bars ();
6415
28995e67 6416 if (windows_or_buffers_changed)
a2889657
JB
6417 update_mode_lines++;
6418
538f13d4
RS
6419 /* Detect case that we need to write or remove a star in the mode line. */
6420 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
6421 {
6422 w->update_mode_line = Qt;
6423 if (buffer_shared > 1)
6424 update_mode_lines++;
6425 }
6426
5f5c8ee5 6427 /* If %c is in the mode line, update it if needed. */
28995e67
RS
6428 if (!NILP (w->column_number_displayed)
6429 /* This alternative quickly identifies a common case
6430 where no change is needed. */
6431 && !(PT == XFASTINT (w->last_point)
8850a573
RS
6432 && XFASTINT (w->last_modified) >= MODIFF
6433 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
6434 && XFASTINT (w->column_number_displayed) != current_column ())
6435 w->update_mode_line = Qt;
6436
44fa5b1e 6437 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 6438
5f5c8ee5
GM
6439 /* The variable buffer_shared is set in redisplay_window and
6440 indicates that we redisplay a buffer in different windows. See
6441 there. */
6442 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
6443
6444 /* If specs for an arrow have changed, do thorough redisplay
6445 to ensure we remove any arrow that should no longer exist. */
d45de95b 6446 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 6447 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 6448 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 6449
90adcf20
RS
6450 /* Normally the message* functions will have already displayed and
6451 updated the echo area, but the frame may have been trashed, or
6452 the update may have been preempted, so display the echo area
6453 again here. */
5f5c8ee5
GM
6454 if (echo_area_glyphs
6455 || STRINGP (echo_area_message)
6456 || previous_echo_glyphs
6457 || STRINGP (previous_echo_area_message))
90adcf20 6458 {
5f5c8ee5 6459 echo_area_display (0);
90adcf20
RS
6460 must_finish = 1;
6461 }
6462
5f5c8ee5
GM
6463 /* If showing the region, and mark has changed, we must redisplay
6464 the whole window. The assignment to this_line_start_pos prevents
6465 the optimization directly below this if-statement. */
bd66d1ba
RS
6466 if (((!NILP (Vtransient_mark_mode)
6467 && !NILP (XBUFFER (w->buffer)->mark_active))
6468 != !NILP (w->region_showing))
82d04750
JB
6469 || (!NILP (w->region_showing)
6470 && !EQ (w->region_showing,
6471 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
6472 CHARPOS (this_line_start_pos) = 0;
6473
6474 /* Optimize the case that only the line containing the cursor in the
6475 selected window has changed. Variables starting with this_ are
6476 set in display_line and record information about the line
6477 containing the cursor. */
6478 tlbufpos = this_line_start_pos;
6479 tlendpos = this_line_end_pos;
6480 if (!consider_all_windows_p
6481 && CHARPOS (tlbufpos) > 0
6482 && NILP (w->update_mode_line)
73af359d 6483 && !current_buffer->clip_changed
44fa5b1e 6484 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 6485 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 6486 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
6487 && this_line_buffer == current_buffer
6488 && current_buffer == XBUFFER (w->buffer)
265a9e55 6489 && NILP (w->force_start)
5f5c8ee5
GM
6490 /* Point must be on the line that we have info recorded about. */
6491 && PT >= CHARPOS (tlbufpos)
6492 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
6493 /* All text outside that line, including its final newline,
6494 must be unchanged */
5f5c8ee5
GM
6495 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
6496 CHARPOS (tlendpos)))
6497 {
6498 if (CHARPOS (tlbufpos) > BEGV
6499 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
6500 && (CHARPOS (tlbufpos) == ZV
6501 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
6502 /* Former continuation line has disappeared by becoming empty */
6503 goto cancel;
6504 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 6505 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
6506 || MINI_WINDOW_P (w))
6507 {
1c9241f5
KH
6508 /* We have to handle the case of continuation around a
6509 wide-column character (See the comment in indent.c around
6510 line 885).
6511
6512 For instance, in the following case:
6513
6514 -------- Insert --------
6515 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
6516 J_I_ ==> J_I_ `^^' are cursors.
6517 ^^ ^^
6518 -------- --------
6519
6520 As we have to redraw the line above, we should goto cancel. */
6521
5f5c8ee5
GM
6522 struct it it;
6523 int line_height_before = this_line_pixel_height;
6524
6525 /* Note that start_display will handle the case that the
6526 line starting at tlbufpos is a continuation lines. */
6527 start_display (&it, w, tlbufpos);
6528
6529 /* Implementation note: It this still necessary? */
6530 if (it.current_x != this_line_start_x)
1c9241f5
KH
6531 goto cancel;
6532
5f5c8ee5
GM
6533 TRACE ((stderr, "trying display optimization 1\n"));
6534 w->cursor.vpos = -1;
a2889657 6535 overlay_arrow_seen = 0;
5f5c8ee5
GM
6536 it.vpos = this_line_vpos;
6537 it.current_y = this_line_y;
6538 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
6539 display_line (&it);
6540
a2889657 6541 /* If line contains point, is not continued,
5f5c8ee5
GM
6542 and ends at same distance from eob as before, we win */
6543 if (w->cursor.vpos >= 0
6544 /* Line is not continued, otherwise this_line_start_pos
6545 would have been set to 0 in display_line. */
6546 && CHARPOS (this_line_start_pos)
6547 /* Line ends as before. */
6548 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
6549 /* Line has same height as before. Otherwise other lines
6550 would have to be shifted up or down. */
6551 && this_line_pixel_height == line_height_before)
a2889657 6552 {
5f5c8ee5
GM
6553 /* If this is not the window's last line, we must adjust
6554 the charstarts of the lines below. */
6555 if (it.current_y < it.last_visible_y)
6556 {
6557 struct glyph_row *row
6558 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
6559 int delta, delta_bytes;
6560
6561 if (Z - CHARPOS (tlendpos) == ZV)
6562 {
6563 /* This line ends at end of (accessible part of)
6564 buffer. There is no newline to count. */
6565 delta = (Z
6566 - CHARPOS (tlendpos)
6567 - MATRIX_ROW_START_CHARPOS (row));
6568 delta_bytes = (Z_BYTE
6569 - BYTEPOS (tlendpos)
6570 - MATRIX_ROW_START_BYTEPOS (row));
6571 }
6572 else
6573 {
6574 /* This line ends in a newline. Must take
6575 account of the newline and the rest of the
6576 text that follows. */
6577 delta = (Z
6578 - CHARPOS (tlendpos)
6579 - MATRIX_ROW_START_CHARPOS (row));
6580 delta_bytes = (Z_BYTE
6581 - BYTEPOS (tlendpos)
6582 - MATRIX_ROW_START_BYTEPOS (row));
6583 }
6584
6585 increment_glyph_matrix_buffer_positions (w->current_matrix,
6586 this_line_vpos + 1,
6587 w->current_matrix->nrows,
6588 delta, delta_bytes);
85bcef6c 6589 }
46db8486 6590
5f5c8ee5
GM
6591 /* If this row displays text now but previously didn't,
6592 or vice versa, w->window_end_vpos may have to be
6593 adjusted. */
6594 if ((it.glyph_row - 1)->displays_text_p)
6595 {
6596 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
6597 XSETINT (w->window_end_vpos, this_line_vpos);
6598 }
6599 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
6600 && this_line_vpos > 0)
6601 XSETINT (w->window_end_vpos, this_line_vpos - 1);
6602 w->window_end_valid = Qnil;
6603
6604 /* Update hint: No need to try to scroll in update_window. */
6605 w->desired_matrix->no_scrolling_p = 1;
6606
6607#if GLYPH_DEBUG
6608 *w->desired_matrix->method = 0;
6609 debug_method_add (w, "optimization 1");
6610#endif
a2889657
JB
6611 goto update;
6612 }
6613 else
6614 goto cancel;
6615 }
5f5c8ee5
GM
6616 else if (/* Cursor position hasn't changed. */
6617 PT == XFASTINT (w->last_point)
b6f0fe04
RS
6618 /* Make sure the cursor was last displayed
6619 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
6620 && 0 <= w->cursor.vpos
6621 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
6622 {
6623 if (!must_finish)
6624 {
6625 do_pending_window_change ();
5f5c8ee5
GM
6626
6627 /* We used to always goto end_of_redisplay here, but this
6628 isn't enough if we have a blinking cursor. */
6629 if (w->cursor_off_p == w->last_cursor_off_p)
6630 goto end_of_redisplay;
a2889657
JB
6631 }
6632 goto update;
6633 }
8b51f1e3
KH
6634 /* If highlighting the region, or if the cursor is in the echo area,
6635 then we can't just move the cursor. */
bd66d1ba
RS
6636 else if (! (!NILP (Vtransient_mark_mode)
6637 && !NILP (current_buffer->mark_active))
293a54ce
RS
6638 && (w == XWINDOW (current_buffer->last_selected_window)
6639 || highlight_nonselected_windows)
8b51f1e3
KH
6640 && NILP (w->region_showing)
6641 && !cursor_in_echo_area)
a2889657 6642 {
5f5c8ee5
GM
6643 struct it it;
6644 struct glyph_row *row;
6645
6646 /* Skip from tlbufpos to PT and see where it is. Note that
6647 PT may be in invisible text. If so, we will end at the
6648 next visible position. */
6649 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
6650 NULL, DEFAULT_FACE_ID);
6651 it.current_x = this_line_start_x;
6652 it.current_y = this_line_y;
6653 it.vpos = this_line_vpos;
6654
6655 /* The call to move_it_to stops in front of PT, but
6656 moves over before-strings. */
6657 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
6658
6659 if (it.vpos == this_line_vpos
6660 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
6661 row->enabled_p))
a2889657 6662 {
5f5c8ee5
GM
6663 xassert (this_line_vpos == it.vpos);
6664 xassert (this_line_y == it.current_y);
6665 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
6666 goto update;
6667 }
6668 else
6669 goto cancel;
6670 }
5f5c8ee5 6671
a2889657 6672 cancel:
5f5c8ee5
GM
6673 /* Text changed drastically or point moved off of line. */
6674 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
6675 }
6676
5f5c8ee5
GM
6677 CHARPOS (this_line_start_pos) = 0;
6678 consider_all_windows_p |= buffer_shared > 1;
6679 ++clear_face_cache_count;
a2889657 6680
5f5c8ee5
GM
6681
6682 /* Build desired matrices. If consider_all_windows_p is non-zero,
6683 do it for all windows on all frames. Otherwise do it for
6684 selected_window, only. */
463f6b91 6685
5f5c8ee5 6686 if (consider_all_windows_p)
a2889657 6687 {
35f56f96 6688 Lisp_Object tail, frame;
a2889657 6689
5f5c8ee5
GM
6690 /* Clear the face cache eventually. */
6691 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 6692 {
5f5c8ee5 6693 clear_face_cache (0);
463f6b91
RS
6694 clear_face_cache_count = 0;
6695 }
31b24551 6696
5f5c8ee5
GM
6697 /* Recompute # windows showing selected buffer. This will be
6698 incremented each time such a window is displayed. */
a2889657
JB
6699 buffer_shared = 0;
6700
35f56f96 6701 FOR_EACH_FRAME (tail, frame)
30c566e4 6702 {
5f5c8ee5 6703 struct frame *f = XFRAME (frame);
fd8ff63d 6704 if (FRAME_WINDOW_P (f) || f == selected_frame)
9769686d 6705 {
5f5c8ee5
GM
6706 /* Mark all the scroll bars to be removed; we'll redeem
6707 the ones we want when we redisplay their windows. */
9769686d
RS
6708 if (condemn_scroll_bars_hook)
6709 (*condemn_scroll_bars_hook) (f);
30c566e4 6710
f21ef775 6711 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 6712 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 6713
5f5c8ee5
GM
6714 /* Any scroll bars which redisplay_windows should have
6715 nuked should now go away. */
9769686d
RS
6716 if (judge_scroll_bars_hook)
6717 (*judge_scroll_bars_hook) (f);
6718 }
30c566e4 6719 }
a2889657 6720 }
5f5c8ee5
GM
6721 else if (FRAME_VISIBLE_P (selected_frame)
6722 && !FRAME_OBSCURED_P (selected_frame))
6723 redisplay_window (selected_window, 1);
6724
6725
6726 /* Compare desired and current matrices, perform output. */
6727
6728update:
6729
6730 /* If fonts changed, display again. */
6731 if (fonts_changed_p)
6732 goto retry;
a2889657 6733
a2889657
JB
6734 /* Prevent various kinds of signals during display update.
6735 stdio is not robust about handling signals,
6736 which can cause an apparent I/O error. */
6737 if (interrupt_input)
6738 unrequest_sigio ();
6739 stop_polling ();
6740
5f5c8ee5 6741 if (consider_all_windows_p)
a2889657
JB
6742 {
6743 Lisp_Object tail;
6744
6745 pause = 0;
6746
44fa5b1e 6747 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 6748 {
5f5c8ee5 6749 struct frame *f;
a2889657 6750
e24c997d 6751 if (!FRAMEP (XCONS (tail)->car))
a2889657
JB
6752 continue;
6753
44fa5b1e 6754 f = XFRAME (XCONS (tail)->car);
1af9f229 6755
fd8ff63d 6756 if ((FRAME_WINDOW_P (f) || f == selected_frame)
f21ef775 6757 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 6758 {
5f5c8ee5
GM
6759 /* Mark all windows as to be updated. */
6760 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 6761 pause |= update_frame (f, 0, 0);
a2889657 6762 if (!pause)
efc63ef0 6763 {
5f5c8ee5
GM
6764 if (hscroll_windows (f->root_window))
6765 goto retry;
6766
efc63ef0
RS
6767 mark_window_display_accurate (f->root_window, 1);
6768 if (frame_up_to_date_hook != 0)
6769 (*frame_up_to_date_hook) (f);
6770 }
a2889657
JB
6771 }
6772 }
6773 }
6774 else
6e8290aa 6775 {
5f5c8ee5
GM
6776 if (FRAME_VISIBLE_P (selected_frame)
6777 && !FRAME_OBSCURED_P (selected_frame))
6778 {
6779 XWINDOW (selected_window)->must_be_updated_p = 1;
6780 pause = update_frame (selected_frame, 0, 0);
6781 if (!pause && hscroll_windows (selected_window))
6782 goto retry;
6783 }
4d641a15
KH
6784 else
6785 pause = 0;
d724d989 6786
8de2d90b 6787 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
6788 function. If the echo area is on another frame, that may
6789 have put text on a frame other than the selected one, so the
6790 above call to update_frame would not have caught it. Catch
8de2d90b
JB
6791 it here. */
6792 {
84faf44c 6793 Lisp_Object mini_window;
5f5c8ee5 6794 struct frame *mini_frame;
84faf44c
RS
6795
6796 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
6797 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 6798
fd8ff63d 6799 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
6800 {
6801 XWINDOW (mini_window)->must_be_updated_p = 1;
6802 pause |= update_frame (mini_frame, 0, 0);
6803 if (!pause && hscroll_windows (mini_window))
6804 goto retry;
6805 }
8de2d90b 6806 }
6e8290aa 6807 }
a2889657 6808
5f5c8ee5
GM
6809 /* If display was paused because of pending input, make sure we do a
6810 thorough update the next time. */
a2889657
JB
6811 if (pause)
6812 {
5f5c8ee5
GM
6813 /* Prevent the optimization at the beginning of
6814 redisplay_internal that tries a single-line update of the
6815 line containing the cursor in the selected window. */
6816 CHARPOS (this_line_start_pos) = 0;
6817
6818 /* Let the overlay arrow be updated the next time. */
265a9e55 6819 if (!NILP (last_arrow_position))
a2889657
JB
6820 {
6821 last_arrow_position = Qt;
6822 last_arrow_string = Qt;
6823 }
5f5c8ee5
GM
6824
6825 /* If we pause after scrolling, some rows in the current
6826 matrices of some windows are not valid. */
6827 if (!WINDOW_FULL_WIDTH_P (w)
6828 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
6829 update_mode_lines = 1;
6830 }
6831
5f5c8ee5
GM
6832 /* Now text on frame agrees with windows, so put info into the
6833 windows for partial redisplay to follow. */
a2889657
JB
6834 if (!pause)
6835 {
6836 register struct buffer *b = XBUFFER (w->buffer);
6837
a2889657 6838 unchanged_modified = BUF_MODIFF (b);
8850a573 6839 overlay_unchanged_modified = BUF_OVERLAY_MODIFF (b);
a2889657
JB
6840 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
6841 end_unchanged = BUF_Z (b) - BUF_GPT (b);
6842
5f5c8ee5 6843 if (consider_all_windows_p)
11e82b76 6844 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
a2889657
JB
6845 else
6846 {
5f5c8ee5
GM
6847 XSETFASTINT (w->last_point, BUF_PT (b));
6848 w->last_cursor = w->cursor;
6849 w->last_cursor_off_p = w->cursor_off_p;
6850
28995e67 6851 b->clip_changed = 0;
a2889657 6852 w->update_mode_line = Qnil;
c2213350 6853 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 6854 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
6855 w->last_had_star
6856 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6857 ? Qt : Qnil);
3ee4159a
RS
6858
6859 /* Record if we are showing a region, so can make sure to
6860 update it fully at next redisplay. */
6861 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
6862 && (w == XWINDOW (current_buffer->last_selected_window)
6863 || highlight_nonselected_windows)
3ee4159a
RS
6864 && !NILP (XBUFFER (w->buffer)->mark_active)
6865 ? Fmarker_position (XBUFFER (w->buffer)->mark)
6866 : Qnil);
6867
d2f84654 6868 w->window_end_valid = w->buffer;
d45de95b 6869 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 6870 last_arrow_string = Voverlay_arrow_string;
efc63ef0
RS
6871 if (frame_up_to_date_hook != 0)
6872 (*frame_up_to_date_hook) (selected_frame);
a2889657 6873 }
5f5c8ee5 6874
a2889657
JB
6875 update_mode_lines = 0;
6876 windows_or_buffers_changed = 0;
6877 }
6878
5f5c8ee5
GM
6879 /* Start SIGIO interrupts coming again. Having them off during the
6880 code above makes it less likely one will discard output, but not
6881 impossible, since there might be stuff in the system buffer here.
a2889657 6882 But it is much hairier to try to do anything about that. */
a2889657
JB
6883 if (interrupt_input)
6884 request_sigio ();
6885 start_polling ();
6886
5f5c8ee5
GM
6887 /* If a frame has become visible which was not before, redisplay
6888 again, so that we display it. Expose events for such a frame
6889 (which it gets when becoming visible) don't call the parts of
6890 redisplay constructing glyphs, so simply exposing a frame won't
6891 display anything in this case. So, we have to display these
6892 frames here explicitly. */
11c52c4f
RS
6893 if (!pause)
6894 {
6895 Lisp_Object tail, frame;
6896 int new_count = 0;
6897
6898 FOR_EACH_FRAME (tail, frame)
6899 {
6900 int this_is_visible = 0;
8e83f802
RS
6901
6902 if (XFRAME (frame)->visible)
6903 this_is_visible = 1;
6904 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
6905 if (XFRAME (frame)->visible)
6906 this_is_visible = 1;
11c52c4f
RS
6907
6908 if (this_is_visible)
6909 new_count++;
6910 }
6911
89819bdd 6912 if (new_count != number_of_visible_frames)
11c52c4f
RS
6913 windows_or_buffers_changed++;
6914 }
6915
44fa5b1e 6916 /* Change frame size now if a change is pending. */
a2889657 6917 do_pending_window_change ();
d8e242fd 6918
8b32d885
RS
6919 /* If we just did a pending size change, or have additional
6920 visible frames, redisplay again. */
3c8c72e0 6921 if (windows_or_buffers_changed && !pause)
8b32d885 6922 goto retry;
5f5c8ee5
GM
6923
6924 end_of_redisplay:;
6925
6926 if (--redisplaying_p < 0)
6927 redisplaying_p = 0;
a2889657
JB
6928}
6929
5f5c8ee5
GM
6930
6931/* Redisplay, but leave alone any recent echo area message unless
6932 another message has been requested in its place.
a2889657
JB
6933
6934 This is useful in situations where you need to redisplay but no
6935 user action has occurred, making it inappropriate for the message
6936 area to be cleared. See tracking_off and
6937 wait_reading_process_input for examples of these situations. */
6938
8991bb31 6939void
a2889657
JB
6940redisplay_preserve_echo_area ()
6941{
5f5c8ee5
GM
6942 if (!echo_area_glyphs
6943 && !STRINGP (echo_area_message)
6944 && (previous_echo_glyphs
6945 || STRINGP (previous_echo_area_message)))
a2889657
JB
6946 {
6947 echo_area_glyphs = previous_echo_glyphs;
5f5c8ee5
GM
6948 echo_area_message = previous_echo_area_message;
6949 echo_area_glyphs_length = previous_echo_glyphs_length;
e9874cee 6950 redisplay_internal (1);
5f5c8ee5
GM
6951 echo_area_glyphs = NULL;
6952 echo_area_message = Qnil;
a2889657
JB
6953 }
6954 else
e9874cee 6955 redisplay_internal (1);
a2889657
JB
6956}
6957
5f5c8ee5
GM
6958
6959/* Mark the display of windows in the window tree rooted at WINDOW as
6960 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
6961 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
6962 the next time redisplay_internal is called. */
6963
a2889657 6964void
5f5c8ee5 6965mark_window_display_accurate (window, accurate_p)
a2889657 6966 Lisp_Object window;
5f5c8ee5 6967 int accurate_p;
a2889657 6968{
5f5c8ee5
GM
6969 struct window *w;
6970
6971 for (; !NILP (window); window = w->next)
a2889657
JB
6972 {
6973 w = XWINDOW (window);
6974
5f5c8ee5 6975 if (BUFFERP (w->buffer))
bd66d1ba 6976 {
5f5c8ee5
GM
6977 struct buffer *b = XBUFFER (w->buffer);
6978
c2213350 6979 XSETFASTINT (w->last_modified,
5f5c8ee5 6980 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 6981 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
6982 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
6983 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
6984 ? Qt : Qnil);
bd66d1ba 6985
5f5c8ee5
GM
6986#if 0 /* I don't think this is necessary because display_line does it.
6987 Let's check it. */
bd66d1ba
RS
6988 /* Record if we are showing a region, so can make sure to
6989 update it fully at next redisplay. */
5f5c8ee5
GM
6990 w->region_showing
6991 = (!NILP (Vtransient_mark_mode)
6992 && (w == XWINDOW (current_buffer->last_selected_window)
6993 || highlight_nonselected_windows)
6994 && (!NILP (b->mark_active)
6995 ? Fmarker_position (b->mark)
6996 : Qnil));
6997#endif
6998
6999 if (accurate_p)
7000 {
7001 b->clip_changed = 0;
7002 w->last_cursor = w->cursor;
7003 w->last_cursor_off_p = w->cursor_off_p;
7004 if (w == XWINDOW (selected_window))
7005 w->last_point = BUF_PT (b);
7006 else
7007 w->last_point = XMARKER (w->pointm)->charpos;
7008 }
bd66d1ba
RS
7009 }
7010
d2f84654 7011 w->window_end_valid = w->buffer;
a2889657
JB
7012 w->update_mode_line = Qnil;
7013
265a9e55 7014 if (!NILP (w->vchild))
5f5c8ee5 7015 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 7016 if (!NILP (w->hchild))
5f5c8ee5 7017 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
7018 }
7019
5f5c8ee5 7020 if (accurate_p)
a2889657 7021 {
d45de95b 7022 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
7023 last_arrow_string = Voverlay_arrow_string;
7024 }
7025 else
7026 {
5f5c8ee5
GM
7027 /* Force a thorough redisplay the next time by setting
7028 last_arrow_position and last_arrow_string to t, which is
7029 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
7030 last_arrow_position = Qt;
7031 last_arrow_string = Qt;
7032 }
7033}
5f5c8ee5
GM
7034
7035
7036/* Return value in display table DP (Lisp_Char_Table *) for character
7037 C. Since a display table doesn't have any parent, we don't have to
7038 follow parent. Do not call this function directly but use the
7039 macro DISP_CHAR_VECTOR. */
7040
7041Lisp_Object
7042disp_char_vector (dp, c)
7043 struct Lisp_Char_Table *dp;
7044 int c;
7045{
7046 int code[4], i;
7047 Lisp_Object val;
7048
7049 if (SINGLE_BYTE_CHAR_P (c))
7050 return (dp->contents[c]);
7051
7052 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7053 if (code[0] != CHARSET_COMPOSITION)
7054 {
7055 if (code[1] < 32)
7056 code[1] = -1;
7057 else if (code[2] < 32)
7058 code[2] = -1;
7059 }
7060
7061 /* Here, the possible range of code[0] (== charset ID) is
7062 128..max_charset. Since the top level char table contains data
7063 for multibyte characters after 256th element, we must increment
7064 code[0] by 128 to get a correct index. */
7065 code[0] += 128;
7066 code[3] = -1; /* anchor */
7067
7068 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7069 {
7070 val = dp->contents[code[i]];
7071 if (!SUB_CHAR_TABLE_P (val))
7072 return (NILP (val) ? dp->defalt : val);
7073 }
7074
7075 /* Here, val is a sub char table. We return the default value of
7076 it. */
7077 return (dp->defalt);
7078}
7079
7080
a2889657 7081\f
5f5c8ee5
GM
7082/***********************************************************************
7083 Window Redisplay
7084 ***********************************************************************/
a2725ab2 7085
5f5c8ee5 7086/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
7087
7088static void
5f5c8ee5
GM
7089redisplay_windows (window)
7090 Lisp_Object window;
90adcf20 7091{
5f5c8ee5
GM
7092 while (!NILP (window))
7093 {
7094 struct window *w = XWINDOW (window);
7095
7096 if (!NILP (w->hchild))
7097 redisplay_windows (w->hchild);
7098 else if (!NILP (w->vchild))
7099 redisplay_windows (w->vchild);
7100 else
7101 redisplay_window (window, 0);
a2725ab2 7102
5f5c8ee5
GM
7103 window = w->next;
7104 }
7105}
7106
7107
7108/* Set cursor position of W. PT is assumed to be displayed in ROW.
7109 DELTA is the number of bytes by which positions recorded in ROW
7110 differ from current buffer positions. */
7111
7112void
7113set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7114 struct window *w;
7115 struct glyph_row *row;
7116 struct glyph_matrix *matrix;
7117 int delta, delta_bytes, dy, dvpos;
7118{
7119 struct glyph *glyph = row->glyphs[TEXT_AREA];
7120 struct glyph *end = glyph + row->used[TEXT_AREA];
7121 int x = row->x;
7122 int pt_old = PT - delta;
7123
7124 /* Skip over glyphs not having an object at the start of the row.
7125 These are special glyphs like truncation marks on terminal
7126 frames. */
7127 if (row->displays_text_p)
7128 while (glyph < end
7129 && !glyph->object
7130 && glyph->charpos < 0)
7131 {
7132 x += glyph->pixel_width;
7133 ++glyph;
7134 }
7135
7136 while (glyph < end
7137 && glyph->object
7138 && (!BUFFERP (glyph->object)
7139 || glyph->charpos < pt_old))
7140 {
7141 x += glyph->pixel_width;
7142 ++glyph;
7143 }
7144
7145 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7146 w->cursor.x = x;
7147 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7148 w->cursor.y = row->y + dy;
7149
7150 if (w == XWINDOW (selected_window))
7151 {
7152 if (!row->continued_p
7153 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7154 && row->x == 0)
7155 {
7156 this_line_buffer = XBUFFER (w->buffer);
7157
7158 CHARPOS (this_line_start_pos)
7159 = MATRIX_ROW_START_CHARPOS (row) + delta;
7160 BYTEPOS (this_line_start_pos)
7161 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7162
7163 CHARPOS (this_line_end_pos)
7164 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7165 BYTEPOS (this_line_end_pos)
7166 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7167
7168 this_line_y = w->cursor.y;
7169 this_line_pixel_height = row->height;
7170 this_line_vpos = w->cursor.vpos;
7171 this_line_start_x = row->x;
7172 }
7173 else
7174 CHARPOS (this_line_start_pos) = 0;
7175 }
7176}
7177
7178
7179/* Run window scroll functions, if any, for WINDOW with new window
7180 start STARTP. Sets the window start of WINDOW to that position. */
7181
7182static INLINE struct text_pos
7183run_window_scroll_functions (window, startp)
7184 Lisp_Object window;
7185 struct text_pos startp;
7186{
7187 struct window *w = XWINDOW (window);
7188 SET_MARKER_FROM_TEXT_POS (w->start, startp);
90adcf20 7189
5f5c8ee5
GM
7190 if (!NILP (Vwindow_scroll_functions))
7191 {
7192 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7193 make_number (CHARPOS (startp)));
7194 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7195 }
90adcf20 7196
5f5c8ee5
GM
7197 return startp;
7198}
7199
7200
7201/* Modify the desired matrix of window W and W->vscroll so that the
7202 line containing the cursor is fully visible. */
7203
7204static void
7205make_cursor_line_fully_visible (w)
7206 struct window *w;
7207{
7208 struct glyph_matrix *matrix;
7209 struct glyph_row *row;
7210 int top_line_height;
7211
7212 /* It's not always possible to find the cursor, e.g, when a window
7213 is full of overlay strings. Don't do anything in that case. */
7214 if (w->cursor.vpos < 0)
7215 return;
7216
7217 matrix = w->desired_matrix;
7218 row = MATRIX_ROW (matrix, w->cursor.vpos);
7219
7220 /* If row->y == top y of window display area, the window isn't tall
7221 enough to display a single line. There is nothing we can do
7222 about it. */
7223 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
7224 if (row->y == top_line_height)
7225 return;
7226
7227 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7228 {
7229 int dy = row->height - row->visible_height;
7230 w->vscroll = 0;
7231 w->cursor.y += dy;
7232 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7233 }
7234 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7235 {
7236 int dy = - (row->height - row->visible_height);
7237 w->vscroll = dy;
7238 w->cursor.y += dy;
7239 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7240 }
7241
7242 /* When we change the cursor y-position of the selected window,
7243 change this_line_y as well so that the display optimization for
7244 the cursor line of the selected window in redisplay_internal uses
7245 the correct y-position. */
7246 if (w == XWINDOW (selected_window))
7247 this_line_y = w->cursor.y;
7248}
7249
7250
7251/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7252 non-zero means only WINDOW is redisplayed in redisplay_internal.
7253 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7254 in redisplay_window to bring a partially visible line into view in
7255 the case that only the cursor has moved.
7256
7257 Value is
7258
7259 1 if scrolling succeeded
7260
7261 0 if scrolling didn't find point.
7262
7263 -1 if new fonts have been loaded so that we must interrupt
7264 redisplay, adjust glyph matrices, and try again. */
7265
7266static int
7267try_scrolling (window, just_this_one_p, scroll_conservatively,
7268 scroll_step, temp_scroll_step)
7269 Lisp_Object window;
7270 int just_this_one_p;
7271 int scroll_conservatively, scroll_step;
7272 int temp_scroll_step;
7273{
7274 struct window *w = XWINDOW (window);
7275 struct frame *f = XFRAME (w->frame);
7276 struct text_pos scroll_margin_pos;
7277 struct text_pos pos;
7278 struct text_pos startp;
7279 struct it it;
7280 Lisp_Object window_end;
7281 int this_scroll_margin;
7282 int dy = 0;
7283 int scroll_max;
7284 int line_height, rc;
7285 int amount_to_scroll = 0;
7286 Lisp_Object aggressive;
7287 int height;
7288
7289#if GLYPH_DEBUG
7290 debug_method_add (w, "try_scrolling");
78614721 7291#endif
5f5c8ee5
GM
7292
7293 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7294
7295 /* Compute scroll margin height in pixels. We scroll when point is
7296 within this distance from the top or bottom of the window. */
7297 if (scroll_margin > 0)
90adcf20 7298 {
5f5c8ee5
GM
7299 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
7300 this_scroll_margin *= CANON_Y_UNIT (f);
7301 }
7302 else
7303 this_scroll_margin = 0;
7304
7305 /* Compute how much we should try to scroll maximally to bring point
7306 into view. */
7307 if (scroll_step)
7308 scroll_max = scroll_step;
7309 else if (scroll_conservatively)
7310 scroll_max = scroll_conservatively;
7311 else if (temp_scroll_step)
7312 scroll_max = temp_scroll_step;
7313 else if (NUMBERP (current_buffer->scroll_down_aggressively)
7314 || NUMBERP (current_buffer->scroll_up_aggressively))
7315 /* We're trying to scroll because of aggressive scrolling
7316 but no scroll_step is set. Choose an arbitrary one. Maybe
7317 there should be a variable for this. */
7318 scroll_max = 10;
7319 else
7320 scroll_max = 0;
7321 scroll_max *= CANON_Y_UNIT (f);
7322
7323 /* Decide whether we have to scroll down. Start at the window end
7324 and move this_scroll_margin up to find the position of the scroll
7325 margin. */
7326 window_end = Fwindow_end (window, Qt);
7327 CHARPOS (scroll_margin_pos) = XINT (window_end);
7328 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
7329 if (this_scroll_margin)
7330 {
7331 start_display (&it, w, scroll_margin_pos);
7332 move_it_vertically (&it, - this_scroll_margin);
7333 scroll_margin_pos = it.current.pos;
7334 }
7335
7336 if (PT >= CHARPOS (scroll_margin_pos))
7337 {
7338 int y0;
7339
7340 /* Point is in the scroll margin at the bottom of the window, or
7341 below. Compute a new window start that makes point visible. */
7342
7343 /* Compute the distance from the scroll margin to PT.
7344 Give up if the distance is greater than scroll_max. */
7345 start_display (&it, w, scroll_margin_pos);
7346 y0 = it.current_y;
7347 move_it_to (&it, PT, 0, it.last_visible_y, -1,
7348 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7349 line_height = (it.max_ascent + it.max_descent
7350 ? it.max_ascent + it.max_descent
7351 : last_height);
7352 dy = it.current_y + line_height - y0;
7353 if (dy > scroll_max)
7354 return 0;
7355
7356 /* Move the window start down. If scrolling conservatively,
7357 move it just enough down to make point visible. If
7358 scroll_step is set, move it down by scroll_step. */
7359 start_display (&it, w, startp);
7360
7361 if (scroll_conservatively)
7362 amount_to_scroll = dy;
7363 else if (scroll_step || temp_scroll_step)
7364 amount_to_scroll = scroll_max;
7365 else
90adcf20 7366 {
5f5c8ee5
GM
7367 aggressive = current_buffer->scroll_down_aggressively;
7368 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
7369 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
7370 if (NUMBERP (aggressive))
7371 amount_to_scroll = XFLOATINT (aggressive) * height;
7372 }
a2725ab2 7373
5f5c8ee5
GM
7374 if (amount_to_scroll <= 0)
7375 return 0;
a2725ab2 7376
5f5c8ee5
GM
7377 move_it_vertically (&it, amount_to_scroll);
7378 startp = it.current.pos;
7379 }
7380 else
7381 {
7382 /* See if point is inside the scroll margin at the top of the
7383 window. */
7384 scroll_margin_pos = startp;
7385 if (this_scroll_margin)
7386 {
7387 start_display (&it, w, startp);
7388 move_it_vertically (&it, this_scroll_margin);
7389 scroll_margin_pos = it.current.pos;
7390 }
7391
7392 if (PT < CHARPOS (scroll_margin_pos))
7393 {
7394 /* Point is in the scroll margin at the top of the window or
7395 above what is displayed in the window. */
7396 int y0;
7397
7398 /* Compute the vertical distance from PT to the scroll
7399 margin position. Give up if distance is greater than
7400 scroll_max. */
7401 SET_TEXT_POS (pos, PT, PT_BYTE);
7402 start_display (&it, w, pos);
7403 y0 = it.current_y;
7404 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
7405 it.last_visible_y, -1,
7406 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7407 dy = it.current_y - y0;
7408 if (dy > scroll_max)
7409 return 0;
7410
7411 /* Compute new window start. */
7412 start_display (&it, w, startp);
7413
7414 if (scroll_conservatively)
7415 amount_to_scroll = dy;
7416 else if (scroll_step || temp_scroll_step)
7417 amount_to_scroll = scroll_max;
538f13d4 7418 else
5f5c8ee5
GM
7419 {
7420 aggressive = current_buffer->scroll_up_aggressively;
7421 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
7422 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
7423 if (NUMBERP (aggressive))
7424 amount_to_scroll = XFLOATINT (aggressive) * height;
7425 }
a2725ab2 7426
5f5c8ee5
GM
7427 if (amount_to_scroll <= 0)
7428 return 0;
7429
7430 move_it_vertically (&it, - amount_to_scroll);
7431 startp = it.current.pos;
90adcf20
RS
7432 }
7433 }
a2889657 7434
5f5c8ee5
GM
7435 /* Run window scroll functions. */
7436 startp = run_window_scroll_functions (window, startp);
90adcf20 7437
5f5c8ee5
GM
7438 /* Display the window. Give up if new fonts are loaded, or if point
7439 doesn't appear. */
7440 if (!try_window (window, startp))
7441 rc = -1;
7442 else if (w->cursor.vpos < 0)
7443 {
7444 clear_glyph_matrix (w->desired_matrix);
7445 rc = 0;
7446 }
7447 else
7448 {
7449 /* Maybe forget recorded base line for line number display. */
7450 if (!just_this_one_p
7451 || current_buffer->clip_changed
7452 || beg_unchanged < CHARPOS (startp))
7453 w->base_line_number = Qnil;
7454
7455 /* If cursor ends up on a partially visible line, shift display
7456 lines up or down. */
7457 make_cursor_line_fully_visible (w);
7458 rc = 1;
7459 }
7460
7461 return rc;
a2889657
JB
7462}
7463
5f5c8ee5
GM
7464
7465/* Compute a suitable window start for window W if display of W starts
7466 on a continuation line. Value is non-zero if a new window start
7467 was computed.
7468
7469 The new window start will be computed, based on W's width, starting
7470 from the start of the continued line. It is the start of the
7471 screen line with the minimum distance from the old start W->start. */
7472
7473static int
7474compute_window_start_on_continuation_line (w)
7475 struct window *w;
1f1ff51d 7476{
5f5c8ee5
GM
7477 struct text_pos pos, start_pos;
7478 int window_start_changed_p = 0;
1f1ff51d 7479
5f5c8ee5 7480 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 7481
5f5c8ee5
GM
7482 /* If window start is on a continuation line... Window start may be
7483 < BEGV in case there's invisible text at the start of the
7484 buffer (M-x rmail, for example). */
7485 if (CHARPOS (start_pos) > BEGV
7486 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 7487 {
5f5c8ee5
GM
7488 struct it it;
7489 struct glyph_row *row;
7490
7491 /* Find the start of the continued line. This should be fast
7492 because scan_buffer is fast (newline cache). */
7493 row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
7494 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
7495 row, DEFAULT_FACE_ID);
7496 reseat_at_previous_visible_line_start (&it);
7497
7498 /* If the line start is "too far" away from the window start,
7499 say it takes too much time to compute a new window start. */
7500 if (CHARPOS (start_pos) - IT_CHARPOS (it)
7501 < XFASTINT (w->height) * XFASTINT (w->width))
7502 {
7503 int min_distance, distance;
7504
7505 /* Move forward by display lines to find the new window
7506 start. If window width was enlarged, the new start can
7507 be expected to be > the old start. If window width was
7508 decreased, the new window start will be < the old start.
7509 So, we're looking for the display line start with the
7510 minimum distance from the old window start. */
7511 pos = it.current.pos;
7512 min_distance = INFINITY;
7513 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
7514 distance < min_distance)
7515 {
7516 min_distance = distance;
7517 pos = it.current.pos;
7518 move_it_by_lines (&it, 1, 0);
7519 }
7520
7521 /* Set the window start there. */
7522 SET_MARKER_FROM_TEXT_POS (w->start, pos);
7523 window_start_changed_p = 1;
7524 }
1f1ff51d 7525 }
5f5c8ee5
GM
7526
7527 return window_start_changed_p;
1f1ff51d
KH
7528}
7529
5f5c8ee5
GM
7530
7531/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
7532 selected_window is redisplayed. */
90adcf20 7533
a2889657 7534static void
5f5c8ee5 7535redisplay_window (window, just_this_one_p)
a2889657 7536 Lisp_Object window;
5f5c8ee5 7537 int just_this_one_p;
a2889657 7538{
5f5c8ee5
GM
7539 struct window *w = XWINDOW (window);
7540 struct frame *f = XFRAME (w->frame);
7541 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 7542 struct buffer *old = current_buffer;
5f5c8ee5 7543 struct text_pos lpoint, opoint, startp;
e481f960 7544 int update_mode_line;
5f5c8ee5
GM
7545 int tem;
7546 struct it it;
7547 /* Record it now because it's overwritten. */
7548 int current_matrix_up_to_date_p = 0;
5ba50c51 7549 int really_switched_buffer = 0;
5f5c8ee5 7550 int temp_scroll_step = 0;
2e54982e 7551 int count = specpdl_ptr - specpdl;
a2889657 7552
5f5c8ee5
GM
7553 SET_TEXT_POS (lpoint, PT, PT_BYTE);
7554 opoint = lpoint;
a2889657 7555
5f5c8ee5
GM
7556 /* W must be a leaf window here. */
7557 xassert (!NILP (w->buffer));
7558#if GLYPH_DEBUG
7559 *w->desired_matrix->method = 0;
7560#endif
2e54982e
RS
7561
7562 specbind (Qinhibit_point_motion_hooks, Qt);
5f5c8ee5
GM
7563
7564 /* Has the mode line to be updated? */
7565 update_mode_line = (!NILP (w->update_mode_line)
7566 || update_mode_lines
7567 || buffer->clip_changed);
8de2d90b
JB
7568
7569 if (MINI_WINDOW_P (w))
7570 {
5f5c8ee5
GM
7571 if (w == XWINDOW (echo_area_window)
7572 && (echo_area_glyphs
7573 || STRINGP (echo_area_message)))
7574 {
7575 if (update_mode_line)
7576 /* We may have to update a tty frame's menu bar or a
7577 toolbar. Example `M-x C-h C-h C-g'. */
7578 goto finish_menu_bars;
7579 else
7580 /* We've already displayed the echo area glyphs in this window. */
7581 goto finish_scroll_bars;
7582 }
73af359d 7583 else if (w != XWINDOW (minibuf_window))
8de2d90b 7584 {
5f5c8ee5
GM
7585 /* W is a mini-buffer window, but it's not the currently
7586 active one, so clear it. */
7587 int yb = window_text_bottom_y (w);
7588 struct glyph_row *row;
7589 int y;
7590
7591 for (y = 0, row = w->desired_matrix->rows;
7592 y < yb;
7593 y += row->height, ++row)
7594 blank_row (w, row, y);
88f22aff 7595 goto finish_scroll_bars;
8de2d90b
JB
7596 }
7597 }
a2889657 7598
5f5c8ee5
GM
7599 /* Otherwise set up data on this window; select its buffer and point
7600 value. */
e481f960 7601 if (update_mode_line)
5ba50c51 7602 {
5f5c8ee5
GM
7603 /* Really select the buffer, for the sake of buffer-local
7604 variables. */
5ba50c51
RS
7605 set_buffer_internal_1 (XBUFFER (w->buffer));
7606 really_switched_buffer = 1;
7607 }
e481f960
RS
7608 else
7609 set_buffer_temp (XBUFFER (w->buffer));
5f5c8ee5
GM
7610 SET_TEXT_POS (opoint, PT, PT_BYTE);
7611
7612 current_matrix_up_to_date_p
7613 = (!NILP (w->window_end_valid)
7614 && !current_buffer->clip_changed
7615 && XFASTINT (w->last_modified) >= MODIFF
7616 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 7617
5f5c8ee5
GM
7618 /* When windows_or_buffers_changed is non-zero, we can't rely on
7619 the window end being valid, so set it to nil there. */
7620 if (windows_or_buffers_changed)
7621 {
7622 /* If window starts on a continuation line, maybe adjust the
7623 window start in case the window's width changed. */
7624 if (XMARKER (w->start)->buffer == current_buffer)
7625 compute_window_start_on_continuation_line (w);
7626
7627 w->window_end_valid = Qnil;
7628 }
12adba34 7629
5f5c8ee5
GM
7630 /* Some sanity checks. */
7631 CHECK_WINDOW_END (w);
7632 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 7633 abort ();
5f5c8ee5 7634 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 7635 abort ();
a2889657 7636
28995e67
RS
7637 /* If %c is in mode line, update it if needed. */
7638 if (!NILP (w->column_number_displayed)
7639 /* This alternative quickly identifies a common case
7640 where no change is needed. */
7641 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7642 && XFASTINT (w->last_modified) >= MODIFF
7643 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7644 && XFASTINT (w->column_number_displayed) != current_column ())
7645 update_mode_line = 1;
7646
5f5c8ee5
GM
7647 /* Count number of windows showing the selected buffer. An indirect
7648 buffer counts as its base buffer. */
7649 if (!just_this_one_p)
42640f83
RS
7650 {
7651 struct buffer *current_base, *window_base;
7652 current_base = current_buffer;
7653 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
7654 if (current_base->base_buffer)
7655 current_base = current_base->base_buffer;
7656 if (window_base->base_buffer)
7657 window_base = window_base->base_buffer;
7658 if (current_base == window_base)
7659 buffer_shared++;
7660 }
a2889657 7661
5f5c8ee5
GM
7662 /* Point refers normally to the selected window. For any other
7663 window, set up appropriate value. */
a2889657
JB
7664 if (!EQ (window, selected_window))
7665 {
12adba34
RS
7666 int new_pt = XMARKER (w->pointm)->charpos;
7667 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 7668 if (new_pt < BEGV)
a2889657 7669 {
f67a0f51 7670 new_pt = BEGV;
12adba34
RS
7671 new_pt_byte = BEGV_BYTE;
7672 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 7673 }
f67a0f51 7674 else if (new_pt > (ZV - 1))
a2889657 7675 {
f67a0f51 7676 new_pt = ZV;
12adba34
RS
7677 new_pt_byte = ZV_BYTE;
7678 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 7679 }
5f5c8ee5 7680
f67a0f51 7681 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 7682 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
7683 }
7684
f4faa47c 7685 /* If any of the character widths specified in the display table
5f5c8ee5
GM
7686 have changed, invalidate the width run cache. It's true that
7687 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
7688 redisplay goes (non-fatally) haywire when the display table is
7689 changed, so why should we worry about doing any better? */
7690 if (current_buffer->width_run_cache)
7691 {
f908610f 7692 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
7693
7694 if (! disptab_matches_widthtab (disptab,
7695 XVECTOR (current_buffer->width_table)))
7696 {
7697 invalidate_region_cache (current_buffer,
7698 current_buffer->width_run_cache,
7699 BEG, Z);
7700 recompute_width_table (current_buffer, disptab);
7701 }
7702 }
7703
a2889657 7704 /* If window-start is screwed up, choose a new one. */
a2889657
JB
7705 if (XMARKER (w->start)->buffer != current_buffer)
7706 goto recenter;
7707
5f5c8ee5 7708 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 7709
cf0df6ab
RS
7710 /* If someone specified a new starting point but did not insist,
7711 check whether it can be used. */
5f5c8ee5 7712 if (!NILP (w->optional_new_start))
cf0df6ab
RS
7713 {
7714 w->optional_new_start = Qnil;
5f5c8ee5
GM
7715 /* This takes a mini-buffer prompt into account. */
7716 start_display (&it, w, startp);
7717 move_it_to (&it, PT, 0, it.last_visible_y, -1,
7718 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7719 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
7720 w->force_start = Qt;
7721 }
7722
8de2d90b 7723 /* Handle case where place to start displaying has been specified,
aa6d10fa 7724 unless the specified location is outside the accessible range. */
265a9e55 7725 if (!NILP (w->force_start))
a2889657 7726 {
e63574d7 7727 w->force_start = Qnil;
5f5c8ee5 7728 w->vscroll = 0;
b5174a51 7729 w->window_end_valid = Qnil;
5f5c8ee5
GM
7730
7731 /* Forget any recorded base line for line number display. */
7732 if (!current_matrix_up_to_date_p
7733 || current_buffer->clip_changed)
7734 w->base_line_number = Qnil;
7735
75c43375
RS
7736 /* Redisplay the mode line. Select the buffer properly for that.
7737 Also, run the hook window-scroll-functions
7738 because we have scrolled. */
e63574d7
RS
7739 /* Note, we do this after clearing force_start because
7740 if there's an error, it is better to forget about force_start
7741 than to get into an infinite loop calling the hook functions
7742 and having them get more errors. */
75c43375
RS
7743 if (!update_mode_line
7744 || ! NILP (Vwindow_scroll_functions))
e481f960 7745 {
5ba50c51
RS
7746 if (!really_switched_buffer)
7747 {
7748 set_buffer_temp (old);
7749 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 7750 really_switched_buffer = 1;
5ba50c51 7751 }
5f5c8ee5 7752
e481f960
RS
7753 update_mode_line = 1;
7754 w->update_mode_line = Qt;
5f5c8ee5 7755 startp = run_window_scroll_functions (window, startp);
e481f960 7756 }
5f5c8ee5 7757
c2213350 7758 XSETFASTINT (w->last_modified, 0);
8850a573 7759 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
7760 if (CHARPOS (startp) < BEGV)
7761 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
7762 else if (CHARPOS (startp) > ZV)
7763 SET_TEXT_POS (startp, ZV, ZV_BYTE);
7764
7765 /* Redisplay, then check if cursor has been set during the
7766 redisplay. Give up if new fonts were loaded. */
7767 if (!try_window (window, startp))
7768 {
7769 w->force_start = Qt;
7770 clear_glyph_matrix (w->desired_matrix);
7771 goto restore_buffers;
7772 }
7773
7774 if (w->cursor.vpos < 0)
7775 {
7776 /* If point does not appear, or on a line that is not fully
7777 visible, move point so it does appear. The desired
7778 matrix has been built above, so we can use it. */
7779 int height = window_box_height (w) / 2;
7780 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
7781
7782 while (row->y < height)
7783 ++row;
7784
7785 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
7786 MATRIX_ROW_START_BYTEPOS (row));
7787
90adcf20 7788 if (w != XWINDOW (selected_window))
12adba34 7789 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
7790 else if (current_buffer == old)
7791 SET_TEXT_POS (lpoint, PT, PT_BYTE);
7792
7793 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
7794
7795 /* If we are highlighting the region, then we just changed
7796 the region, so redisplay to show it. */
df0b5ea1
RS
7797 if (!NILP (Vtransient_mark_mode)
7798 && !NILP (current_buffer->mark_active))
6f27fa9b 7799 {
5f5c8ee5
GM
7800 clear_glyph_matrix (w->desired_matrix);
7801 if (!try_window (window, startp))
7802 goto restore_buffers;
6f27fa9b 7803 }
a2889657 7804 }
5f5c8ee5
GM
7805
7806 make_cursor_line_fully_visible (w);
7807#if GLYPH_DEBUG
7808 debug_method_add (w, "forced window start");
7809#endif
a2889657
JB
7810 goto done;
7811 }
7812
5f5c8ee5
GM
7813 /* Handle case where text has not changed, only point, and it has
7814 not moved off the frame. */
7815 if (current_matrix_up_to_date_p
7816 /* Point may be in this window. */
7817 && PT >= CHARPOS (startp)
7818 /* If we don't check this, we are called to move the cursor in a
7819 horizontally split window with a current matrix that doesn't
7820 fit the display. */
7821 && !windows_or_buffers_changed
7822 /* Selective display hasn't changed. */
7823 && !current_buffer->clip_changed
b1aa6cb3
RS
7824 /* If force-mode-line-update was called, really redisplay;
7825 that's how redisplay is forced after e.g. changing
7826 buffer-invisibility-spec. */
632ab665 7827 && NILP (w->update_mode_line)
5f5c8ee5
GM
7828 /* Can't use this case if highlighting a region. When a
7829 region exists, cursor movement has to do more than just
7830 set the cursor. */
7831 && !(!NILP (Vtransient_mark_mode)
7832 && !NILP (current_buffer->mark_active))
bd66d1ba 7833 && NILP (w->region_showing)
5f5c8ee5
GM
7834 /* Right after splitting windows, last_point may be nil. */
7835 && INTEGERP (w->last_point)
7836 /* This code is not used for mini-buffer for the sake of the case
7837 of redisplaying to replace an echo area message; since in
7838 that case the mini-buffer contents per se are usually
7839 unchanged. This code is of no real use in the mini-buffer
7840 since the handling of this_line_start_pos, etc., in redisplay
7841 handles the same cases. */
d45de95b 7842 && !EQ (window, minibuf_window)
5f5c8ee5
GM
7843 /* When splitting windows or for new windows, it happens that
7844 redisplay is called with a nil window_end_vpos or one being
7845 larger than the window. This should really be fixed in
7846 window.c. I don't have this on my list, now, so we do
7847 approximately the same as the old redisplay code. --gerd. */
7848 && INTEGERP (w->window_end_vpos)
7849 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
7850 && (FRAME_WINDOW_P (f)
7851 || !MARKERP (Voverlay_arrow_position)
377dbd97 7852 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 7853 {
5f5c8ee5
GM
7854 int this_scroll_margin;
7855 struct glyph_row *row;
7856 int scroll_p;
a2889657 7857
5f5c8ee5
GM
7858#if GLYPH_DEBUG
7859 debug_method_add (w, "cursor movement");
7860#endif
9afd2168 7861
5f5c8ee5
GM
7862 /* Scroll if point within this distance from the top or bottom
7863 of the window. This is a pixel value. */
7864 this_scroll_margin = max (0, scroll_margin);
7865 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
7866 this_scroll_margin *= CANON_Y_UNIT (f);
7867
7868 /* Start with the row the cursor was displayed during the last
7869 not paused redisplay. Give up if that row is not valid. */
7870 if (w->last_cursor.vpos >= w->current_matrix->nrows)
7871 goto try_to_scroll;
7872 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
7873 if (row->mode_line_p)
7874 ++row;
7875 if (!row->enabled_p)
7876 goto try_to_scroll;
7877
7878 scroll_p = 0;
7879 if (PT > XFASTINT (w->last_point))
7880 {
7881 /* Point has moved forward. */
7882 int last_y = window_text_bottom_y (w) - this_scroll_margin;
7883
7884 while ((MATRIX_ROW_END_CHARPOS (row) < PT
7885 /* The end position of a row equals the start
7886 position of the next row. If PT is there, we
7887 would rather display it in the next line, except
7888 when this line ends in ZV. */
7889 || (MATRIX_ROW_END_CHARPOS (row) == PT
7890 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
7891 || !row->ends_at_zv_p)))
7892 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
7893 {
7894 xassert (row->enabled_p);
7895 ++row;
7896 }
9afd2168 7897
5f5c8ee5
GM
7898 /* If within the scroll margin, scroll. Note that
7899 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
7900 next line would be drawn, and that this_scroll_margin can
7901 be zero. */
7902 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
7903 || PT > MATRIX_ROW_END_CHARPOS (row)
7904 /* Line is completely visible last line in window and PT
7905 is to be set in the next line. */
7906 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
7907 && PT == MATRIX_ROW_END_CHARPOS (row)
7908 && !row->ends_at_zv_p
7909 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
7910 scroll_p = 1;
7911 }
7912 else if (PT < XFASTINT (w->last_point))
a2889657 7913 {
5f5c8ee5
GM
7914 /* Cursor has to be moved backward. Note that PT >=
7915 CHARPOS (startp) because of the outer if-statement. */
7916 while (!row->mode_line_p
7917 && (MATRIX_ROW_START_CHARPOS (row) > PT
7918 || (MATRIX_ROW_START_CHARPOS (row) == PT
7919 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
7920 && (row->y > this_scroll_margin
7921 || CHARPOS (startp) == BEGV))
a2889657 7922 {
5f5c8ee5
GM
7923 xassert (row->enabled_p);
7924 --row;
a2889657 7925 }
abb4c08f 7926
5f5c8ee5
GM
7927 /* Consider the following case: Window starts at BEGV, there
7928 is invisible, intangible text at BEGV, so that display
7929 starts at some point START > BEGV. It can happen that
7930 we are called with PT somewhere between BEGV and START.
7931 Try to handle that case. */
7932 if (row < w->current_matrix->rows
7933 || row->mode_line_p)
7934 {
7935 row = w->current_matrix->rows;
7936 if (row->mode_line_p)
7937 ++row;
7938 }
7939
7940 /* Due to newlines in overlay strings, we may have to skip
7941 forward over overlay strings. */
7942 while (MATRIX_ROW_END_CHARPOS (row) == PT
7943 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
7944 && !row->ends_at_zv_p)
7945 ++row;
7946
7947 /* If within the scroll margin, scroll. */
7948 if (row->y < this_scroll_margin
7949 && CHARPOS (startp) != BEGV)
7950 scroll_p = 1;
7951 }
7952
7953 /* if PT is not in the glyph row, give up. */
7954 if (PT < MATRIX_ROW_START_CHARPOS (row)
7955 || PT > MATRIX_ROW_END_CHARPOS (row))
7956 goto try_to_scroll;
7957
7958 /* If we end up in a partially visible line, let's make it fully
7959 visible. This can be done most easily by using the existing
7960 scrolling code. */
7961 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
7962 {
7963 temp_scroll_step = 1;
7964 goto try_to_scroll;
a2889657 7965 }
5f5c8ee5
GM
7966 else if (scroll_p)
7967 goto try_to_scroll;
7968
7969 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7970 goto done;
a2889657 7971 }
5f5c8ee5 7972
a2889657
JB
7973 /* If current starting point was originally the beginning of a line
7974 but no longer is, find a new starting point. */
265a9e55 7975 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
7976 && !(CHARPOS (startp) <= BEGV
7977 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 7978 {
5f5c8ee5
GM
7979#if GLYPH_DEBUG
7980 debug_method_add (w, "recenter 1");
7981#endif
a2889657
JB
7982 goto recenter;
7983 }
5f5c8ee5
GM
7984
7985 /* Try scrolling with try_window_id. */
7986 else if (!windows_or_buffers_changed
7987 /* Window must be either use window-based redisplay or
7988 be full width. */
7989 && (FRAME_WINDOW_P (f)
7990 || ((line_ins_del_ok && WINDOW_FULL_WIDTH_P (w))
7991 && just_this_one_p))
7992 && !MINI_WINDOW_P (w)
7993 /* Point is not known NOT to appear in window. */
7994 && PT >= CHARPOS (startp)
a2889657 7995 && XFASTINT (w->last_modified)
5f5c8ee5
GM
7996 /* Window is not hscrolled. */
7997 && XFASTINT (w->hscroll) == 0
7998 /* Selective display has not changed. */
7999 && !current_buffer->clip_changed
8000 /* Current matrix is up to date. */
8001 && !NILP (w->window_end_valid)
8002 /* Can't use this case if highlighting a region because
8003 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
8004 && !(!NILP (Vtransient_mark_mode)
8005 && !NILP (current_buffer->mark_active))
8006 && NILP (w->region_showing)
5f5c8ee5 8007 /* Overlay arrow position and string not changed. */
d45de95b 8008 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 8009 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
8010 /* Value is > 0 if update has been done, it is -1 if we
8011 know that the same window start will not work. It is 0
8012 if unsuccessful for some other reason. */
8013 && (tem = try_window_id (w)) != 0)
a2889657 8014 {
5f5c8ee5
GM
8015#if GLYPH_DEBUG
8016 debug_method_add (w, "try_window_id");
8017#endif
8018
8019 if (fonts_changed_p)
8020 goto restore_buffers;
a2889657
JB
8021 if (tem > 0)
8022 goto done;
5f5c8ee5
GM
8023 /* Otherwise try_window_id has returned -1 which means that we
8024 don't want the alternative below this comment to execute. */
a2889657 8025 }
5f5c8ee5
GM
8026 else if (CHARPOS (startp) >= BEGV
8027 && CHARPOS (startp) <= ZV
8028 && PT >= CHARPOS (startp)
8029 && (CHARPOS (startp) < ZV
e9874cee 8030 /* Avoid starting at end of buffer. */
5f5c8ee5 8031 || CHARPOS (startp) == BEGV
8850a573
RS
8032 || (XFASTINT (w->last_modified) >= MODIFF
8033 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 8034 {
5f5c8ee5
GM
8035#if GLYPH_DEBUG
8036 debug_method_add (w, "same window start");
8037#endif
8038
8039 /* Try to redisplay starting at same place as before.
8040 If point has not moved off frame, accept the results. */
8041 if (!current_matrix_up_to_date_p
8042 /* Don't use try_window_reusing_current_matrix in this case
8043 because it can have changed the buffer. */
8044 || !NILP (Vwindow_scroll_functions)
8045 || MINI_WINDOW_P (w)
8046 || !try_window_reusing_current_matrix (w))
8047 {
8048 IF_DEBUG (debug_method_add (w, "1"));
8049 try_window (window, startp);
8050 }
8051
8052 if (fonts_changed_p)
8053 goto restore_buffers;
8054
8055 if (w->cursor.vpos >= 0)
aa6d10fa 8056 {
5f5c8ee5
GM
8057 if (!just_this_one_p
8058 || current_buffer->clip_changed
8059 || beg_unchanged < CHARPOS (startp))
aa6d10fa
RS
8060 /* Forget any recorded base line for line number display. */
8061 w->base_line_number = Qnil;
5f5c8ee5
GM
8062
8063 make_cursor_line_fully_visible (w);
aa6d10fa
RS
8064 goto done;
8065 }
a2889657 8066 else
5f5c8ee5 8067 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
8068 }
8069
5f5c8ee5
GM
8070 try_to_scroll:
8071
c2213350 8072 XSETFASTINT (w->last_modified, 0);
8850a573 8073 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 8074
e481f960
RS
8075 /* Redisplay the mode line. Select the buffer properly for that. */
8076 if (!update_mode_line)
8077 {
5ba50c51
RS
8078 if (!really_switched_buffer)
8079 {
8080 set_buffer_temp (old);
8081 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8082 really_switched_buffer = 1;
5ba50c51 8083 }
e481f960
RS
8084 update_mode_line = 1;
8085 w->update_mode_line = Qt;
8086 }
a2889657 8087
5f5c8ee5
GM
8088 /* Try to scroll by specified few lines. */
8089 if ((scroll_conservatively
8090 || scroll_step
8091 || temp_scroll_step
8092 || NUMBERP (current_buffer->scroll_up_aggressively)
8093 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 8094 && !current_buffer->clip_changed
5f5c8ee5
GM
8095 && CHARPOS (startp) >= BEGV
8096 && CHARPOS (startp) <= ZV)
0789adb2 8097 {
5f5c8ee5
GM
8098 /* The function returns -1 if new fonts were loaded, 1 if
8099 successful, 0 if not successful. */
8100 int rc = try_scrolling (window, just_this_one_p,
8101 scroll_conservatively,
8102 scroll_step,
8103 temp_scroll_step);
8104 if (rc > 0)
8105 goto done;
8106 else if (rc < 0)
8107 goto restore_buffers;
8108 }
f9c8af06 8109
5f5c8ee5 8110 /* Finally, just choose place to start which centers point */
5936754e 8111
5f5c8ee5 8112 recenter:
44173109 8113
5f5c8ee5
GM
8114#if GLYPH_DEBUG
8115 debug_method_add (w, "recenter");
8116#endif
0789adb2 8117
5f5c8ee5 8118 /* w->vscroll = 0; */
0789adb2 8119
5f5c8ee5
GM
8120 /* Forget any previously recorded base line for line number display. */
8121 if (!current_matrix_up_to_date_p
8122 || current_buffer->clip_changed)
8123 w->base_line_number = Qnil;
8124
8125 /* Move backward half the height of the window. */
8126 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8127 it.current_y = it.last_visible_y;
8128 move_it_vertically_backward (&it, it.last_visible_y / 2);
8129 xassert (IT_CHARPOS (it) >= BEGV);
8130
8131 /* The function move_it_vertically_backward may move over more
8132 than the specified y-distance. If it->w is small, e.g. a
8133 mini-buffer window, we may end up in front of the window's
8134 display area. Start displaying at the start of the line
8135 containing PT in this case. */
8136 if (it.current_y <= 0)
8137 {
8138 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8139 move_it_vertically (&it, 0);
8140 xassert (IT_CHARPOS (it) <= PT);
8141 it.current_y = 0;
0789adb2
RS
8142 }
8143
5f5c8ee5
GM
8144 it.current_x = it.hpos = 0;
8145
8146 /* Set startp here explicitly in case that helps avoid an infinite loop
8147 in case the window-scroll-functions functions get errors. */
8148 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8149
8150 /* Run scroll hooks. */
8151 startp = run_window_scroll_functions (window, it.current.pos);
8152
8153 /* Redisplay the window. */
8154 if (!current_matrix_up_to_date_p
8155 || windows_or_buffers_changed
8156 /* Don't use try_window_reusing_current_matrix in this case
8157 because it can have changed the buffer. */
8158 || !NILP (Vwindow_scroll_functions)
8159 || !just_this_one_p
8160 || MINI_WINDOW_P (w)
8161 || !try_window_reusing_current_matrix (w))
8162 try_window (window, startp);
8163
8164 /* If new fonts have been loaded (due to fontsets), give up. We
8165 have to start a new redisplay since we need to re-adjust glyph
8166 matrices. */
8167 if (fonts_changed_p)
8168 goto restore_buffers;
8169
8170 /* If cursor did not appear assume that the middle of the window is
8171 in the first line of the window. Do it again with the next line.
8172 (Imagine a window of height 100, displaying two lines of height
8173 60. Moving back 50 from it->last_visible_y will end in the first
8174 line.) */
8175 if (w->cursor.vpos < 0)
a2889657 8176 {
5f5c8ee5
GM
8177 if (!NILP (w->window_end_valid)
8178 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 8179 {
5f5c8ee5
GM
8180 clear_glyph_matrix (w->desired_matrix);
8181 move_it_by_lines (&it, 1, 0);
8182 try_window (window, it.current.pos);
a2889657 8183 }
5f5c8ee5 8184 else if (PT < IT_CHARPOS (it))
a2889657 8185 {
5f5c8ee5
GM
8186 clear_glyph_matrix (w->desired_matrix);
8187 move_it_by_lines (&it, -1, 0);
8188 try_window (window, it.current.pos);
8189 }
8190 else
8191 {
8192 /* Not much we can do about it. */
a2889657 8193 }
a2889657 8194 }
010494d0 8195
5f5c8ee5
GM
8196 /* Consider the following case: Window starts at BEGV, there is
8197 invisible, intangible text at BEGV, so that display starts at
8198 some point START > BEGV. It can happen that we are called with
8199 PT somewhere between BEGV and START. Try to handle that case. */
8200 if (w->cursor.vpos < 0)
835766b6 8201 {
5f5c8ee5
GM
8202 struct glyph_row *row = w->current_matrix->rows;
8203 if (row->mode_line_p)
8204 ++row;
8205 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 8206 }
5f5c8ee5
GM
8207
8208 make_cursor_line_fully_visible (w);
b5174a51 8209
5f5c8ee5
GM
8210 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8211 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8212 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8213 ? Qt : Qnil);
a2889657 8214
5f5c8ee5 8215 done:
a2889657 8216
5f5c8ee5 8217 /* Display the mode line, if we must. */
e481f960 8218 if ((update_mode_line
aa6d10fa 8219 /* If window not full width, must redo its mode line
5f5c8ee5
GM
8220 if (a) the window to its side is being redone and
8221 (b) we do a frame-based redisplay. This is a consequence
8222 of how inverted lines are drawn in frame-based redisplay. */
8223 || (!just_this_one_p
8224 && !FRAME_WINDOW_P (f)
8225 && !WINDOW_FULL_WIDTH_P (w))
8226 /* Line number to display. */
155ef550 8227 || INTEGERP (w->base_line_pos)
5f5c8ee5 8228 /* Column number is displayed and different from the one displayed. */
155ef550
KH
8229 || (!NILP (w->column_number_displayed)
8230 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
8231 /* This means that the window has a mode line. */
8232 && (WINDOW_WANTS_MODELINE_P (w)
8233 || WINDOW_WANTS_TOP_LINE_P (w)))
5ba50c51 8234 {
5f5c8ee5
GM
8235 display_mode_lines (w);
8236
8237 /* If mode line height has changed, arrange for a thorough
8238 immediate redisplay using the correct mode line height. */
8239 if (WINDOW_WANTS_MODELINE_P (w)
8240 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 8241 {
5f5c8ee5
GM
8242 fonts_changed_p = 1;
8243 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8244 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 8245 }
5f5c8ee5
GM
8246
8247 /* If top line height has changed, arrange for a thorough
8248 immediate redisplay using the correct mode line height. */
8249 if (WINDOW_WANTS_TOP_LINE_P (w)
8250 && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
8251 {
8252 fonts_changed_p = 1;
8253 MATRIX_TOP_LINE_ROW (w->current_matrix)->height
8254 = DESIRED_TOP_LINE_HEIGHT (w);
8255 }
8256
8257 if (fonts_changed_p)
8258 goto restore_buffers;
5ba50c51 8259 }
5f5c8ee5
GM
8260
8261 if (!line_number_displayed
8262 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
8263 {
8264 w->base_line_pos = Qnil;
8265 w->base_line_number = Qnil;
8266 }
a2889657 8267
5f5c8ee5
GM
8268 finish_menu_bars:
8269
7ce2c095 8270 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 8271 if (update_mode_line
5f5c8ee5
GM
8272 && EQ (FRAME_SELECTED_WINDOW (f), window))
8273 {
8274 int redisplay_menu_p = 0;
8275
8276 if (FRAME_WINDOW_P (f))
8277 {
dc937613 8278#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 8279 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 8280#else
5f5c8ee5 8281 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 8282#endif
5f5c8ee5
GM
8283 }
8284 else
8285 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8286
8287 if (redisplay_menu_p)
8288 display_menu_bar (w);
8289
8290#ifdef HAVE_WINDOW_SYSTEM
8291 if (WINDOWP (f->toolbar_window)
8292 && (FRAME_TOOLBAR_LINES (f) > 0
8293 || auto_resize_toolbars_p))
8294 redisplay_toolbar (f);
8295#endif
8296 }
7ce2c095 8297
88f22aff 8298 finish_scroll_bars:
5f5c8ee5 8299
88f22aff 8300 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 8301 {
b1d1124b 8302 int start, end, whole;
30c566e4 8303
b1d1124b 8304 /* Calculate the start and end positions for the current window.
3505ea70
JB
8305 At some point, it would be nice to choose between scrollbars
8306 which reflect the whole buffer size, with special markers
8307 indicating narrowing, and scrollbars which reflect only the
8308 visible region.
8309
5f5c8ee5 8310 Note that mini-buffers sometimes aren't displaying any text. */
b1d1124b 8311 if (! MINI_WINDOW_P (w)
5f5c8ee5
GM
8312 || (w == XWINDOW (minibuf_window)
8313 && !echo_area_glyphs
8314 && !STRINGP (echo_area_message)))
b1d1124b 8315 {
8a9311d7 8316 whole = ZV - BEGV;
4d641a15 8317 start = marker_position (w->start) - BEGV;
b1d1124b
JB
8318 /* I don't think this is guaranteed to be right. For the
8319 moment, we'll pretend it is. */
5f5c8ee5 8320 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 8321
5f5c8ee5
GM
8322 if (end < start)
8323 end = start;
8324 if (whole < (end - start))
8325 whole = end - start;
b1d1124b
JB
8326 }
8327 else
8328 start = end = whole = 0;
30c566e4 8329
88f22aff 8330 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 8331 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 8332
5f5c8ee5
GM
8333 /* Note that we actually used the scroll bar attached to this
8334 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 8335 (*redeem_scroll_bar_hook) (w);
30c566e4 8336 }
b1d1124b 8337
5f5c8ee5
GM
8338 restore_buffers:
8339
8340 /* Restore current_buffer and value of point in it. */
8341 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
5ba50c51 8342 if (really_switched_buffer)
f72df6ac 8343 set_buffer_internal_1 (old);
e481f960
RS
8344 else
8345 set_buffer_temp (old);
5f5c8ee5 8346 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
8347
8348 unbind_to (count, Qnil);
a2889657 8349}
a2889657 8350
5f5c8ee5
GM
8351
8352/* Build the complete desired matrix of WINDOW with a window start
8353 buffer position POS. Value is non-zero if successful. It is zero
8354 if fonts were loaded during redisplay which makes re-adjusting
8355 glyph matrices necessary. */
8356
8357int
a2889657
JB
8358try_window (window, pos)
8359 Lisp_Object window;
5f5c8ee5
GM
8360 struct text_pos pos;
8361{
8362 struct window *w = XWINDOW (window);
8363 struct it it;
8364 struct glyph_row *last_text_row = NULL;
9cbab4ff 8365
5f5c8ee5
GM
8366 /* Make POS the new window start. */
8367 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 8368
5f5c8ee5
GM
8369 /* Mark cursor position as unknown. No overlay arrow seen. */
8370 w->cursor.vpos = -1;
a2889657 8371 overlay_arrow_seen = 0;
642eefc6 8372
5f5c8ee5
GM
8373 /* Initialize iterator and info to start at POS. */
8374 start_display (&it, w, pos);
a2889657 8375
5f5c8ee5
GM
8376 /* Display all lines of W. */
8377 while (it.current_y < it.last_visible_y)
8378 {
8379 if (display_line (&it))
8380 last_text_row = it.glyph_row - 1;
8381 if (fonts_changed_p)
8382 return 0;
8383 }
a2889657 8384
5f5c8ee5
GM
8385 /* If bottom moved off end of frame, change mode line percentage. */
8386 if (XFASTINT (w->window_end_pos) <= 0
8387 && Z != IT_CHARPOS (it))
a2889657
JB
8388 w->update_mode_line = Qt;
8389
5f5c8ee5
GM
8390 /* Set window_end_pos to the offset of the last character displayed
8391 on the window from the end of current_buffer. Set
8392 window_end_vpos to its row number. */
8393 if (last_text_row)
8394 {
8395 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
8396 w->window_end_bytepos
8397 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8398 XSETFASTINT (w->window_end_pos,
8399 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8400 XSETFASTINT (w->window_end_vpos,
8401 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8402 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
8403 ->displays_text_p);
8404 }
8405 else
8406 {
8407 w->window_end_bytepos = 0;
8408 XSETFASTINT (w->window_end_pos, 0);
8409 XSETFASTINT (w->window_end_vpos, 0);
8410 }
8411
a2889657
JB
8412 /* But that is not valid info until redisplay finishes. */
8413 w->window_end_valid = Qnil;
5f5c8ee5 8414 return 1;
a2889657 8415}
5f5c8ee5
GM
8416
8417
a2889657 8418\f
5f5c8ee5
GM
8419/************************************************************************
8420 Window redisplay reusing current matrix when buffer has not changed
8421 ************************************************************************/
8422
8423/* Try redisplay of window W showing an unchanged buffer with a
8424 different window start than the last time it was displayed by
8425 reusing its current matrix. Value is non-zero if successful.
8426 W->start is the new window start. */
a2889657
JB
8427
8428static int
5f5c8ee5
GM
8429try_window_reusing_current_matrix (w)
8430 struct window *w;
a2889657 8431{
5f5c8ee5
GM
8432 struct frame *f = XFRAME (w->frame);
8433 struct glyph_row *row, *bottom_row;
8434 struct it it;
8435 struct run run;
8436 struct text_pos start, new_start;
8437 int nrows_scrolled, i;
8438 struct glyph_row *last_text_row;
8439 struct glyph_row *last_reused_text_row;
8440 struct glyph_row *start_row;
8441 int start_vpos, min_y, max_y;
8442
8443 /* Right now this function doesn't handle terminal frames. */
8444 if (!FRAME_WINDOW_P (f))
8445 return 0;
a2889657 8446
5f5c8ee5
GM
8447 /* Can't do this if region may have changed. */
8448 if ((!NILP (Vtransient_mark_mode)
8449 && !NILP (current_buffer->mark_active))
8450 || !NILP (w->region_showing))
8451 return 0;
a2889657 8452
5f5c8ee5
GM
8453 /* If top-line visibility has changed, give up. */
8454 if (WINDOW_WANTS_TOP_LINE_P (w)
8455 != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
8456 return 0;
8457
8458 /* Give up if old or new display is scrolled vertically. We could
8459 make this function handle this, but right now it doesn't. */
8460 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8461 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
8462 return 0;
8463
8464 /* The variable new_start now holds the new window start. The old
8465 start `start' can be determined from the current matrix. */
8466 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
8467 start = start_row->start.pos;
8468 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 8469
5f5c8ee5
GM
8470 /* Clear the desired matrix for the display below. */
8471 clear_glyph_matrix (w->desired_matrix);
8472
8473 if (CHARPOS (new_start) <= CHARPOS (start))
8474 {
8475 int first_row_y;
8476
8477 IF_DEBUG (debug_method_add (w, "twu1"));
8478
8479 /* Display up to a row that can be reused. The variable
8480 last_text_row is set to the last row displayed that displays
8481 text. */
8482 start_display (&it, w, new_start);
8483 first_row_y = it.current_y;
8484 w->cursor.vpos = -1;
8485 last_text_row = last_reused_text_row = NULL;
8486 while (it.current_y < it.last_visible_y
8487 && IT_CHARPOS (it) < CHARPOS (start)
8488 && !fonts_changed_p)
8489 if (display_line (&it))
8490 last_text_row = it.glyph_row - 1;
8491
8492 /* A value of current_y < last_visible_y means that we stopped
8493 at the previous window start, which in turn means that we
8494 have at least one reusable row. */
8495 if (it.current_y < it.last_visible_y)
a2889657 8496 {
5f5c8ee5
GM
8497 nrows_scrolled = it.vpos;
8498
8499 /* Find PT if not already found in the lines displayed. */
8500 if (w->cursor.vpos < 0)
a2889657 8501 {
5f5c8ee5
GM
8502 int dy = it.current_y - first_row_y;
8503
8504 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8505 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8506 {
8507 if (PT >= MATRIX_ROW_START_CHARPOS (row)
8508 && PT < MATRIX_ROW_END_CHARPOS (row))
8509 {
8510 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
8511 dy, nrows_scrolled);
8512 break;
8513 }
8514
8515 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
8516 break;
8517
8518 ++row;
8519 }
8520
8521 /* Give up if point was not found. This shouldn't
8522 happen often; not more often than with try_window
8523 itself. */
8524 if (w->cursor.vpos < 0)
8525 {
8526 clear_glyph_matrix (w->desired_matrix);
8527 return 0;
8528 }
a2889657 8529 }
5f5c8ee5
GM
8530
8531 /* Scroll the display. Do it before the current matrix is
8532 changed. The problem here is that update has not yet
8533 run, i.e. part of the current matrix is not up to date.
8534 scroll_run_hook will clear the cursor, and use the
8535 current matrix to get the height of the row the cursor is
8536 in. */
8537 run.current_y = first_row_y;
8538 run.desired_y = it.current_y;
8539 run.height = it.last_visible_y - it.current_y;
8540 if (run.height > 0)
a2889657 8541 {
5f5c8ee5
GM
8542 update_begin (f);
8543 rif->update_window_begin_hook (w);
8544 rif->scroll_run_hook (w, &run);
8545 rif->update_window_end_hook (w, 0);
8546 update_end (f);
a2889657 8547 }
5f5c8ee5
GM
8548
8549 /* Shift current matrix down by nrows_scrolled lines. */
8550 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
8551 rotate_matrix (w->current_matrix,
8552 start_vpos,
8553 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
8554 nrows_scrolled);
8555
8556 /* Disable lines not reused. */
8557 for (i = 0; i < it.vpos; ++i)
8558 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
8559
8560 /* Re-compute Y positions. */
8561 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
8562 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8563 max_y = it.last_visible_y;
8564 while (row < bottom_row)
d2f84654 8565 {
5f5c8ee5
GM
8566 row->y = it.current_y;
8567
8568 if (row->y < min_y)
8569 row->visible_height = row->height - (min_y - row->y);
8570 else if (row->y + row->height > max_y)
8571 row->visible_height
8572 = row->height - (row->y + row->height - max_y);
8573 else
8574 row->visible_height = row->height;
8575
8576 it.current_y += row->height;
8577 ++it.vpos;
8578
8579 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8580 last_reused_text_row = row;
8581 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
8582 break;
8583 ++row;
d2f84654 8584 }
a2889657 8585 }
5f5c8ee5
GM
8586
8587 /* Update window_end_pos etc.; last_reused_text_row is the last
8588 reused row from the current matrix containing text, if any.
8589 The value of last_text_row is the last displayed line
8590 containing text. */
8591 if (last_reused_text_row)
a2889657 8592 {
5f5c8ee5
GM
8593 w->window_end_bytepos
8594 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
8595 XSETFASTINT (w->window_end_pos,
8596 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
8597 XSETFASTINT (w->window_end_vpos,
8598 MATRIX_ROW_VPOS (last_reused_text_row,
8599 w->current_matrix));
a2889657 8600 }
5f5c8ee5
GM
8601 else if (last_text_row)
8602 {
8603 w->window_end_bytepos
8604 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8605 XSETFASTINT (w->window_end_pos,
8606 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8607 XSETFASTINT (w->window_end_vpos,
8608 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8609 }
8610 else
8611 {
8612 /* This window must be completely empty. */
8613 w->window_end_bytepos = 0;
8614 XSETFASTINT (w->window_end_pos, 0);
8615 XSETFASTINT (w->window_end_vpos, 0);
8616 }
8617 w->window_end_valid = Qnil;
a2889657 8618
5f5c8ee5
GM
8619 /* Update hint: don't try scrolling again in update_window. */
8620 w->desired_matrix->no_scrolling_p = 1;
8621
8622#if GLYPH_DEBUG
8623 debug_method_add (w, "try_window_reusing_current_matrix 1");
8624#endif
8625 return 1;
a2889657 8626 }
5f5c8ee5
GM
8627 else if (CHARPOS (new_start) > CHARPOS (start))
8628 {
8629 struct glyph_row *pt_row, *row;
8630 struct glyph_row *first_reusable_row;
8631 struct glyph_row *first_row_to_display;
8632 int dy;
8633 int yb = window_text_bottom_y (w);
8634
8635 IF_DEBUG (debug_method_add (w, "twu2"));
8636
8637 /* Find the row starting at new_start, if there is one. Don't
8638 reuse a partially visible line at the end. */
8639 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8640 while (first_reusable_row->enabled_p
8641 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
8642 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
8643 < CHARPOS (new_start)))
8644 ++first_reusable_row;
8645
8646 /* Give up if there is no row to reuse. */
8647 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
8648 || !first_reusable_row->enabled_p)
8649 return 0;
8650
8651 /* The row we found must start at new_start, or else something
8652 is broken. */
8653 xassert (MATRIX_ROW_START_CHARPOS (first_reusable_row)
8654 == CHARPOS (new_start));
8655
8656 /* We can reuse fully visible rows beginning with
8657 first_reusable_row to the end of the window. Set
8658 first_row_to_display to the first row that cannot be reused.
8659 Set pt_row to the row containing point, if there is any. */
8660 first_row_to_display = first_reusable_row;
8661 pt_row = NULL;
8662 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
8663 {
8664 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
8665 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
8666 pt_row = first_row_to_display;
a2889657 8667
5f5c8ee5
GM
8668 ++first_row_to_display;
8669 }
a2889657 8670
5f5c8ee5
GM
8671 /* Start displaying at the start of first_row_to_display. */
8672 xassert (first_row_to_display->y < yb);
8673 init_to_row_start (&it, w, first_row_to_display);
8674 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
8675 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
8676 - nrows_scrolled);
8677 it.current_y = first_row_to_display->y - first_reusable_row->y;
8678
8679 /* Display lines beginning with first_row_to_display in the
8680 desired matrix. Set last_text_row to the last row displayed
8681 that displays text. */
8682 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
8683 if (pt_row == NULL)
8684 w->cursor.vpos = -1;
8685 last_text_row = NULL;
8686 while (it.current_y < it.last_visible_y && !fonts_changed_p)
8687 if (display_line (&it))
8688 last_text_row = it.glyph_row - 1;
8689
8690 /* Give up If point isn't in a row displayed or reused. */
8691 if (w->cursor.vpos < 0)
8692 {
8693 clear_glyph_matrix (w->desired_matrix);
8694 return 0;
8695 }
12adba34 8696
5f5c8ee5
GM
8697 /* If point is in a reused row, adjust y and vpos of the cursor
8698 position. */
8699 if (pt_row)
8700 {
8701 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
8702 w->current_matrix);
8703 w->cursor.y -= first_reusable_row->y;
a2889657
JB
8704 }
8705
5f5c8ee5
GM
8706 /* Scroll the display. */
8707 run.current_y = first_reusable_row->y;
8708 run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8709 run.height = it.last_visible_y - run.current_y;
8710 if (run.height)
8711 {
8712 struct frame *f = XFRAME (WINDOW_FRAME (w));
8713 update_begin (f);
8714 rif->update_window_begin_hook (w);
8715 rif->scroll_run_hook (w, &run);
8716 rif->update_window_end_hook (w, 0);
8717 update_end (f);
8718 }
a2889657 8719
5f5c8ee5
GM
8720 /* Adjust Y positions of reused rows. */
8721 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
8722 row = first_reusable_row;
8723 dy = first_reusable_row->y;
8724 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8725 max_y = it.last_visible_y;
8726 while (row < first_row_to_display)
8727 {
8728 row->y -= dy;
8729 if (row->y < min_y)
8730 row->visible_height = row->height - (min_y - row->y);
8731 else if (row->y + row->height > max_y)
8732 row->visible_height
8733 = row->height - (row->y + row->height - max_y);
8734 else
8735 row->visible_height = row->height;
8736 ++row;
8737 }
a2889657 8738
5f5c8ee5
GM
8739 /* Disable rows not reused. */
8740 while (row < bottom_row)
8741 {
8742 row->enabled_p = 0;
8743 ++row;
8744 }
8745
8746 /* Scroll the current matrix. */
8747 xassert (nrows_scrolled > 0);
8748 rotate_matrix (w->current_matrix,
8749 start_vpos,
8750 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
8751 -nrows_scrolled);
8752
8753 /* Adjust window end. A null value of last_text_row means that
8754 the window end is in reused rows which in turn means that
8755 only its vpos can have changed. */
8756 if (last_text_row)
8757 {
8758 w->window_end_bytepos
8759 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8760 XSETFASTINT (w->window_end_pos,
8761 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8762 XSETFASTINT (w->window_end_vpos,
8763 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8764 }
8765 else
a2889657 8766 {
e8e536a9 8767 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 8768 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 8769 }
5f5c8ee5
GM
8770
8771 w->window_end_valid = Qnil;
8772 w->desired_matrix->no_scrolling_p = 1;
8773
8774#if GLYPH_DEBUG
8775 debug_method_add (w, "try_window_reusing_current_matrix 2");
8776#endif
8777 return 1;
a2889657 8778 }
5f5c8ee5
GM
8779
8780 return 0;
8781}
a2889657 8782
a2889657 8783
5f5c8ee5
GM
8784\f
8785/************************************************************************
8786 Window redisplay reusing current matrix when buffer has changed
8787 ************************************************************************/
8788
8789static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
8790static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
8791 int *, int *));
8792static struct glyph_row *
8793find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
8794 struct glyph_row *));
8795
8796
8797/* Return the last row in MATRIX displaying text. If row START is
8798 non-null, start searching with that row. IT gives the dimensions
8799 of the display. Value is null if matrix is empty; otherwise it is
8800 a pointer to the row found. */
8801
8802static struct glyph_row *
8803find_last_row_displaying_text (matrix, it, start)
8804 struct glyph_matrix *matrix;
8805 struct it *it;
8806 struct glyph_row *start;
8807{
8808 struct glyph_row *row, *row_found;
8809
8810 /* Set row_found to the last row in IT->w's current matrix
8811 displaying text. The loop looks funny but think of partially
8812 visible lines. */
8813 row_found = NULL;
8814 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
8815 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8816 {
8817 xassert (row->enabled_p);
8818 row_found = row;
8819 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
8820 break;
8821 ++row;
a2889657 8822 }
5f5c8ee5
GM
8823
8824 return row_found;
8825}
8826
a2889657 8827
5f5c8ee5
GM
8828/* Return the last row in the current matrix of W that is not affected
8829 by changes at the start of current_buffer that occurred since the
8830 last time W was redisplayed. Value is null if no such row exists.
a2889657 8831
5f5c8ee5
GM
8832 The global variable beg_unchanged has to contain the number of
8833 bytes unchanged at the start of current_buffer. BEG +
8834 beg_unchanged is the buffer position of the first changed byte in
8835 current_buffer. Characters at positions < BEG + beg_unchanged are
8836 at the same buffer positions as they were when the current matrix
8837 was built. */
8838
8839static struct glyph_row *
8840get_last_unchanged_at_beg_row (w)
8841 struct window *w;
8842{
8843 int first_changed_pos = BEG + beg_unchanged;
8844 struct glyph_row *row;
8845 struct glyph_row *row_found = NULL;
8846 int yb = window_text_bottom_y (w);
8847
8848 /* Find the last row displaying unchanged text. */
8849 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8850 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
8851 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 8852 {
5f5c8ee5
GM
8853 if (/* If row ends before first_changed_pos, it is unchanged,
8854 except in some case. */
8855 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
8856 /* When row ends in ZV and we write at ZV it is not
8857 unchanged. */
8858 && !row->ends_at_zv_p
8859 /* When first_changed_pos is the end of a continued line,
8860 row is not unchanged because it may be no longer
8861 continued. */
8862 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
8863 && row->continued_p))
8864 row_found = row;
8865
8866 /* Stop if last visible row. */
8867 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
8868 break;
8869
8870 ++row;
a2889657
JB
8871 }
8872
5f5c8ee5 8873 return row_found;
a2889657 8874}
5f5c8ee5
GM
8875
8876
8877/* Find the first glyph row in the current matrix of W that is not
8878 affected by changes at the end of current_buffer since the last
8879 time the window was redisplayed. Return in *DELTA the number of
8880 bytes by which buffer positions in unchanged text at the end of
8881 current_buffer must be adjusted. Value is null if no such row
8882 exists, i.e. all rows are affected by changes.
8883
8884 The global variable end_unchanged is assumed to contain the number
8885 of unchanged bytes at the end of current_buffer. The buffer
8886 position of the last changed byte in current_buffer is then Z -
8887 end_unchanged. */
8888
8889static struct glyph_row *
8890get_first_unchanged_at_end_row (w, delta, delta_bytes)
8891 struct window *w;
8892 int *delta, *delta_bytes;
a2889657 8893{
5f5c8ee5
GM
8894 struct glyph_row *row;
8895 struct glyph_row *row_found = NULL;
c581d710 8896
5f5c8ee5 8897 *delta = *delta_bytes = 0;
b2a76982 8898
5f5c8ee5
GM
8899 /* A value of window_end_pos >= end_unchanged means that the window
8900 end is in the range of changed text. If so, there is no
8901 unchanged row at the end of W's current matrix. */
8902 xassert (!NILP (w->window_end_valid));
8903 if (XFASTINT (w->window_end_pos) >= end_unchanged)
8904 return NULL;
8905
8906 /* Set row to the last row in W's current matrix displaying text. */
8907 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
8908
8909 /* End vpos should always be on text, except in an entirely empty
8910 matrix. */
8911 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row)
8912 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0);
8913
8914 /* If matrix is entirely empty, no unchanged row exists. */
8915 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8916 {
8917 /* The value of row is the last glyph row in the matrix having a
8918 meaningful buffer position in it. The end position of row
8919 corresponds to window_end_pos. This allows us to translate
8920 buffer positions in the current matrix to current buffer
8921 positions for characters not in changed text. */
8922 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
8923 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
8924 int last_unchanged_pos, last_unchanged_pos_old;
8925 struct glyph_row *first_text_row
8926 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8927
8928 *delta = Z - Z_old;
8929 *delta_bytes = Z_BYTE - Z_BYTE_old;
8930
8931 /* Set last_unchanged_pos to the buffer position of the last
8932 character in the buffer that has not been changed. Z is the
8933 index + 1 of the last byte in current_buffer, i.e. by
8934 subtracting end_unchanged we get the index of the last
8935 unchanged character, and we have to add BEG to get its buffer
8936 position. */
8937 last_unchanged_pos = Z - end_unchanged + BEG;
8938 last_unchanged_pos_old = last_unchanged_pos - *delta;
8939
8940 /* Search backward from ROW for a row displaying a line that
8941 starts at a minimum position >= last_unchanged_pos_old. */
8942 while (row >= first_text_row)
8943 {
8944 xassert (row->enabled_p);
8945 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
8946
8947 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
8948 row_found = row;
8949 --row;
8950 }
8951 }
8952
8953 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
8954 return row_found;
c581d710
RS
8955}
8956
c581d710 8957
5f5c8ee5
GM
8958/* Make sure that glyph rows in the current matrix of window W
8959 reference the same glyph memory as corresponding rows in the
8960 frame's frame matrix. This function is called after scrolling W's
8961 current matrix on a terminal frame in try_window_id and
8962 try_window_reusing_current_matrix. */
8963
8964static void
8965sync_frame_with_window_matrix_rows (w)
8966 struct window *w;
c581d710 8967{
5f5c8ee5
GM
8968 struct frame *f = XFRAME (w->frame);
8969 struct glyph_row *window_row, *window_row_end, *frame_row;
8970
8971 /* Preconditions: W must be a leaf window and full-width. Its frame
8972 must have a frame matrix. */
8973 xassert (NILP (w->hchild) && NILP (w->vchild));
8974 xassert (WINDOW_FULL_WIDTH_P (w));
8975 xassert (!FRAME_WINDOW_P (f));
8976
8977 /* If W is a full-width window, glyph pointers in W's current matrix
8978 have, by definition, to be the same as glyph pointers in the
8979 corresponding frame matrix. */
8980 window_row = w->current_matrix->rows;
8981 window_row_end = window_row + w->current_matrix->nrows;
8982 frame_row = f->current_matrix->rows + XFASTINT (w->top);
8983 while (window_row < window_row_end)
659a218f 8984 {
5f5c8ee5
GM
8985 int area;
8986 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
8987 frame_row->glyphs[area] = window_row->glyphs[area];
8988 ++window_row, ++frame_row;
659a218f 8989 }
a2889657 8990}
5f5c8ee5
GM
8991
8992
8993/* Try to redisplay window W by reusing its existing display. W's
8994 current matrix must be up to date when this function is called,
8995 i.e. window_end_valid must not be nil.
8996
8997 Value is
8998
8999 1 if display has been updated
9000 0 if otherwise unsuccessful
9001 -1 if redisplay with same window start is known not to succeed
9002
9003 The following steps are performed:
9004
9005 1. Find the last row in the current matrix of W that is not
9006 affected by changes at the start of current_buffer. If no such row
9007 is found, give up.
9008
9009 2. Find the first row in W's current matrix that is not affected by
9010 changes at the end of current_buffer. Maybe there is no such row.
9011
9012 3. Display lines beginning with the row + 1 found in step 1 to the
9013 row found in step 2 or, if step 2 didn't find a row, to the end of
9014 the window.
9015
9016 4. If cursor is not known to appear on the window, give up.
9017
9018 5. If display stopped at the row found in step 2, scroll the
9019 display and current matrix as needed.
9020
9021 6. Maybe display some lines at the end of W, if we must. This can
9022 happen under various circumstances, like a partially visible line
9023 becoming fully visible, or because newly displayed lines are displayed
9024 in smaller font sizes.
9025
9026 7. Update W's window end information. */
9027
9028 /* Check that window end is what we expect it to be. */
12adba34
RS
9029
9030static int
5f5c8ee5 9031try_window_id (w)
12adba34 9032 struct window *w;
12adba34 9033{
5f5c8ee5
GM
9034 struct frame *f = XFRAME (w->frame);
9035 struct glyph_matrix *current_matrix = w->current_matrix;
9036 struct glyph_matrix *desired_matrix = w->desired_matrix;
9037 struct glyph_row *last_unchanged_at_beg_row;
9038 struct glyph_row *first_unchanged_at_end_row;
9039 struct glyph_row *row;
9040 struct glyph_row *bottom_row;
9041 int bottom_vpos;
9042 struct it it;
9043 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9044 struct text_pos start_pos;
9045 struct run run;
9046 int first_unchanged_at_end_vpos = 0;
9047 struct glyph_row *last_text_row, *last_text_row_at_end;
9048 struct text_pos start;
9049
9050 SET_TEXT_POS_FROM_MARKER (start, w->start);
9051
9052 /* Check pre-conditions. Window end must be valid, otherwise
9053 the current matrix would not be up to date. */
9054 xassert (!NILP (w->window_end_valid));
9055 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9056 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9057
9058 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9059 only if buffer has really changed. The reason is that the gap is
9060 initially at Z for freshly visited files. The code below would
9061 set end_unchanged to 0 in that case. */
9062 if (MODIFF > SAVE_MODIFF)
9063 {
9064 if (GPT - BEG < beg_unchanged)
9065 beg_unchanged = GPT - BEG;
9066 if (Z - GPT < end_unchanged)
9067 end_unchanged = Z - GPT;
9068 }
9069
9070 /* If window starts after a line end, and the last change is in
9071 front of that newline, then changes don't affect the display.
9072 This case happens with stealth-fontification. */
9073 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9074 if (CHARPOS (start) > BEGV
9075 && Z - end_unchanged < CHARPOS (start) - 1
9076 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9077 && PT < MATRIX_ROW_END_CHARPOS (row))
9078 {
9079 /* We have to update window end positions because the buffer's
9080 size has changed. */
9081 w->window_end_pos
9082 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9083 w->window_end_bytepos
9084 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9085 return 1;
9086 }
9087
9088 /* Return quickly if changes are all below what is displayed in the
9089 window, and if PT is in the window. */
9090 if (beg_unchanged > MATRIX_ROW_END_CHARPOS (row)
9091 && PT < MATRIX_ROW_END_CHARPOS (row))
9092 {
9093 /* We have to update window end positions because the buffer's
9094 size has changed. */
9095 w->window_end_pos
9096 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9097 w->window_end_bytepos
9098 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9099 return 1;
9100 }
9101
9102 /* Check that window start agrees with the start of the first glyph
9103 row in its current matrix. Check this after we know the window
9104 start is not in changed text, otherwise positions would not be
9105 comparable. */
9106 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9107 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9108 return 0;
9109
9110 /* Remember beg_unchanged and end_unchanged for debugging purposes. */
9111 IF_DEBUG (debug_beg_unchanged = beg_unchanged;
9112 debug_end_unchanged = end_unchanged);
9113
9114 /* Compute the position at which we have to start displaying new
9115 lines. Some of the lines at the top of the window might be
9116 reusable because they are not displaying changed text. Find the
9117 last row in W's current matrix not affected by changes at the
9118 start of current_buffer. Value is null if changes start in the
9119 first line of window. */
9120 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9121 if (last_unchanged_at_beg_row)
9122 {
9123 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9124 start_pos = it.current.pos;
9125
9126 /* Start displaying new lines in the desired matrix at the same
9127 vpos we would use in the current matrix, i.e. below
9128 last_unchanged_at_beg_row. */
9129 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9130 current_matrix);
9131 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9132 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9133
9134 xassert (it.hpos == 0 && it.current_x == 0);
9135 }
9136 else
9137 {
9138 /* There are no reusable lines at the start of the window.
9139 Start displaying in the first line. */
9140 start_display (&it, w, start);
9141 start_pos = it.current.pos;
9142 }
9143
9144 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
9145 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
9146
9147 /* Find the first row that is not affected by changes at the end of
9148 the buffer. Value will be null if there is no unchanged row, in
9149 which case we must redisplay to the end of the window. delta
9150 will be set to the value by which buffer positions beginning with
9151 first_unchanged_at_end_row have to be adjusted due to text
9152 changes. */
9153 first_unchanged_at_end_row
9154 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9155 IF_DEBUG (debug_delta = delta);
9156 IF_DEBUG (debug_delta_bytes = delta_bytes);
9157
9158 /* Set stop_pos to the buffer position up to which we will have to
9159 display new lines. If first_unchanged_at_end_row != NULL, this
9160 is the buffer position of the start of the line displayed in that
9161 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9162 that we don't stop at a buffer position. */
9163 stop_pos = 0;
9164 if (first_unchanged_at_end_row)
9165 {
9166 xassert (last_unchanged_at_beg_row == NULL
9167 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9168
9169 /* If this is a continuation line, move forward to the next one
9170 that isn't. Changes in lines above affect this line.
9171 Caution: this may move first_unchanged_at_end_row to a row
9172 not displaying text. */
9173 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9174 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9175 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9176 < it.last_visible_y))
9177 ++first_unchanged_at_end_row;
9178
9179 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9180 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9181 >= it.last_visible_y))
9182 first_unchanged_at_end_row = NULL;
9183 else
9184 {
9185 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9186 + delta);
9187 first_unchanged_at_end_vpos
9188 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9189 xassert (stop_pos >= Z - end_unchanged);
9190 }
9191 }
9192 else if (last_unchanged_at_beg_row == NULL)
9193 return 0;
9194
9195
9196#if GLYPH_DEBUG
9197
9198 /* Either there is no unchanged row at the end, or the one we have
9199 now displays text. This is a necessary condition for the window
9200 end pos calculation at the end of this function. */
9201 xassert (first_unchanged_at_end_row == NULL
9202 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9203
9204 debug_last_unchanged_at_beg_vpos
9205 = (last_unchanged_at_beg_row
9206 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9207 : -1);
9208 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9209
9210#endif /* GLYPH_DEBUG != 0 */
9211
9212
9213 /* Display new lines. Set last_text_row to the last new line
9214 displayed which has text on it, i.e. might end up as being the
9215 line where the window_end_vpos is. */
9216 w->cursor.vpos = -1;
9217 last_text_row = NULL;
9218 overlay_arrow_seen = 0;
9219 while (it.current_y < it.last_visible_y
9220 && !fonts_changed_p
9221 && (first_unchanged_at_end_row == NULL
9222 || IT_CHARPOS (it) < stop_pos))
9223 {
9224 if (display_line (&it))
9225 last_text_row = it.glyph_row - 1;
9226 }
9227
9228 if (fonts_changed_p)
9229 return -1;
9230
9231
9232 /* Compute differences in buffer positions, y-positions etc. for
9233 lines reused at the bottom of the window. Compute what we can
9234 scroll. */
9235 if (first_unchanged_at_end_row)
9236 {
9237 dvpos = (it.vpos
9238 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
9239 current_matrix));
9240 dy = it.current_y - first_unchanged_at_end_row->y;
9241 run.current_y = first_unchanged_at_end_row->y;
9242 run.desired_y = run.current_y + dy;
9243 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
9244 }
9245 else
9246 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
9247 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
9248
9249
9250 /* Find the cursor if not already found. We have to decide whether
9251 PT will appear on this window (it sometimes doesn't, but this is
9252 not a very frequent case.) This decision has to be made before
9253 the current matrix is altered. A value of cursor.vpos < 0 means
9254 that PT is either in one of the lines beginning at
9255 first_unchanged_at_end_row or below the window. Don't care for
9256 lines that might be displayed later at the window end; as
9257 mentioned, this is not a frequent case. */
9258 if (w->cursor.vpos < 0)
9259 {
9260 int last_y = min (it.last_visible_y, it.last_visible_y + dy);
9261
9262 /* Cursor in unchanged rows at the top? */
9263 if (PT < CHARPOS (start_pos)
9264 && last_unchanged_at_beg_row)
9265 {
9266 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9267 while (row <= last_unchanged_at_beg_row
9268 && MATRIX_ROW_END_CHARPOS (row) <= PT)
9269 ++row;
9270 xassert (row <= last_unchanged_at_beg_row);
9271 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9272 }
9273
9274 /* Start from first_unchanged_at_end_row looking for PT. */
9275 else if (first_unchanged_at_end_row)
9276 {
9277 row = first_unchanged_at_end_row;
9278 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9279 {
9280 if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
9281 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
9282 {
9283 set_cursor_from_row (w, row, w->current_matrix, delta,
9284 delta_bytes, dy, dvpos);
9285 break;
9286 }
9287 else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
9288 break;
9289 ++row;
9290 }
9291 }
9292
9293 /* Give up if cursor was not found. */
9294 if (w->cursor.vpos < 0)
9295 {
9296 clear_glyph_matrix (w->desired_matrix);
9297 return -1;
9298 }
9299 }
9300
9301 /* Don't let the cursor end in the scroll margins. */
9302 {
9303 int this_scroll_margin, cursor_height;
9304
9305 this_scroll_margin = max (0, scroll_margin);
9306 this_scroll_margin = min (this_scroll_margin,
9307 XFASTINT (w->height) / 4);
9308 this_scroll_margin *= CANON_Y_UNIT (it.f);
9309 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
9310
9311 if ((w->cursor.y < this_scroll_margin
9312 && CHARPOS (start) > BEGV)
9313 /* Don't take scroll margin into account at the bottom because
9314 old redisplay didn't do it either. */
9315 || w->cursor.y + cursor_height > it.last_visible_y)
9316 {
9317 w->cursor.vpos = -1;
9318 clear_glyph_matrix (w->desired_matrix);
9319 return -1;
9320 }
9321 }
9322
9323 /* Scroll the display. Do it before changing the current matrix so
9324 that xterm.c doesn't get confused about where the cursor glyph is
9325 found. */
9326 if (dy)
9327 {
9328 update_begin (f);
9329
9330 if (FRAME_WINDOW_P (f))
9331 {
9332 rif->update_window_begin_hook (w);
9333 rif->scroll_run_hook (w, &run);
9334 rif->update_window_end_hook (w, 0);
9335 }
9336 else
9337 {
9338 /* Terminal frame. In this case, dvpos gives the number of
9339 lines to scroll by; dvpos < 0 means scroll up. */
9340 int first_unchanged_at_end_vpos
9341 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
9342 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
9343 int end = XFASTINT (w->top) + window_internal_height (w);
9344
9345 /* Perform the operation on the screen. */
9346 if (dvpos > 0)
9347 {
9348 /* Scroll last_unchanged_at_beg_row to the end of the
9349 window down dvpos lines. */
9350 set_terminal_window (end);
9351
9352 /* On dumb terminals delete dvpos lines at the end
9353 before inserting dvpos empty lines. */
9354 if (!scroll_region_ok)
9355 ins_del_lines (end - dvpos, -dvpos);
9356
9357 /* Insert dvpos empty lines in front of
9358 last_unchanged_at_beg_row. */
9359 ins_del_lines (from, dvpos);
9360 }
9361 else if (dvpos < 0)
9362 {
9363 /* Scroll up last_unchanged_at_beg_vpos to the end of
9364 the window to last_unchanged_at_beg_vpos - |dvpos|. */
9365 set_terminal_window (end);
9366
9367 /* Delete dvpos lines in front of
9368 last_unchanged_at_beg_vpos. ins_del_lines will set
9369 the cursor to the given vpos and emit |dvpos| delete
9370 line sequences. */
9371 ins_del_lines (from + dvpos, dvpos);
9372
9373 /* On a dumb terminal insert dvpos empty lines at the
9374 end. */
9375 if (!scroll_region_ok)
9376 ins_del_lines (end + dvpos, -dvpos);
9377 }
9378
9379 set_terminal_window (0);
9380 }
9381
9382 update_end (f);
9383 }
9384
9385 /* Shift reused rows of the current matrix to the right position. */
9386 if (dvpos < 0)
9387 {
9388 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
9389 bottom_vpos, dvpos);
9390 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
9391 bottom_vpos, 0);
9392 }
9393 else if (dvpos > 0)
9394 {
9395 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
9396 bottom_vpos, dvpos);
9397 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
9398 first_unchanged_at_end_vpos + dvpos, 0);
9399 }
9400
9401 /* For frame-based redisplay, make sure that current frame and window
9402 matrix are in sync with respect to glyph memory. */
9403 if (!FRAME_WINDOW_P (f))
9404 sync_frame_with_window_matrix_rows (w);
9405
9406 /* Adjust buffer positions in reused rows. */
9407 if (delta)
9408 increment_glyph_matrix_buffer_positions (current_matrix,
9409 first_unchanged_at_end_vpos + dvpos,
9410 bottom_vpos, delta, delta_bytes);
9411
9412 /* Adjust Y positions. */
9413 if (dy)
9414 shift_glyph_matrix (w, current_matrix,
9415 first_unchanged_at_end_vpos + dvpos,
9416 bottom_vpos, dy);
9417
9418 if (first_unchanged_at_end_row)
9419 first_unchanged_at_end_row += dvpos;
9420
9421 /* If scrolling up, there may be some lines to display at the end of
9422 the window. */
9423 last_text_row_at_end = NULL;
9424 if (dy < 0)
9425 {
9426 /* Set last_row to the glyph row in the current matrix where the
9427 window end line is found. It has been moved up or down in
9428 the matrix by dvpos. */
9429 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
9430 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
9431
9432 /* If last_row is the window end line, it should display text. */
9433 xassert (last_row->displays_text_p);
9434
9435 /* If window end line was partially visible before, begin
9436 displaying at that line. Otherwise begin displaying with the
9437 line following it. */
9438 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
9439 {
9440 init_to_row_start (&it, w, last_row);
9441 it.vpos = last_vpos;
9442 it.current_y = last_row->y;
9443 }
9444 else
9445 {
9446 init_to_row_end (&it, w, last_row);
9447 it.vpos = 1 + last_vpos;
9448 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
9449 ++last_row;
9450 }
12adba34 9451
5f5c8ee5
GM
9452 /* We may start in a continuation line. If so, we have to get
9453 the right continuation_lines_width and current_x. */
9454 it.continuation_lines_width = last_row->continuation_lines_width;
9455 it.hpos = it.current_x = 0;
9456
9457 /* Display the rest of the lines at the window end. */
9458 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9459 while (it.current_y < it.last_visible_y
9460 && !fonts_changed_p)
9461 {
9462 /* Is it always sure that the display agrees with lines in
9463 the current matrix? I don't think so, so we mark rows
9464 displayed invalid in the current matrix by setting their
9465 enabled_p flag to zero. */
9466 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
9467 if (display_line (&it))
9468 last_text_row_at_end = it.glyph_row - 1;
9469 }
9470 }
12adba34 9471
5f5c8ee5
GM
9472 /* Update window_end_pos and window_end_vpos. */
9473 if (first_unchanged_at_end_row
9474 && first_unchanged_at_end_row->y < it.last_visible_y
9475 && !last_text_row_at_end)
9476 {
9477 /* Window end line if one of the preserved rows from the current
9478 matrix. Set row to the last row displaying text in current
9479 matrix starting at first_unchanged_at_end_row, after
9480 scrolling. */
9481 xassert (first_unchanged_at_end_row->displays_text_p);
9482 row = find_last_row_displaying_text (w->current_matrix, &it,
9483 first_unchanged_at_end_row);
9484 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
9485
9486 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
9487 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
9488 XSETFASTINT (w->window_end_vpos,
9489 MATRIX_ROW_VPOS (row, w->current_matrix));
9490 }
9491 else if (last_text_row_at_end)
9492 {
9493 XSETFASTINT (w->window_end_pos,
9494 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
9495 w->window_end_bytepos
9496 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
9497 XSETFASTINT (w->window_end_vpos,
9498 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
9499 }
9500 else if (last_text_row)
9501 {
9502 /* We have displayed either to the end of the window or at the
9503 end of the window, i.e. the last row with text is to be found
9504 in the desired matrix. */
9505 XSETFASTINT (w->window_end_pos,
9506 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9507 w->window_end_bytepos
9508 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9509 XSETFASTINT (w->window_end_vpos,
9510 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
9511 }
9512 else if (first_unchanged_at_end_row == NULL
9513 && last_text_row == NULL
9514 && last_text_row_at_end == NULL)
9515 {
9516 /* Displayed to end of window, but no line containing text was
9517 displayed. Lines were deleted at the end of the window. */
9518 int vpos;
9519 int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
9520
9521 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
9522 if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
9523 && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
9524 || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
9525 && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
9526 break;
12adba34 9527
5f5c8ee5
GM
9528 w->window_end_vpos = make_number (vpos);
9529 }
9530 else
9531 abort ();
9532
9533 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
9534 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 9535
5f5c8ee5
GM
9536 /* Record that display has not been completed. */
9537 w->window_end_valid = Qnil;
9538 w->desired_matrix->no_scrolling_p = 1;
9539 return 1;
12adba34 9540}
0f9c0ff0 9541
a2889657 9542
5f5c8ee5
GM
9543\f
9544/***********************************************************************
9545 More debugging support
9546 ***********************************************************************/
a2889657 9547
5f5c8ee5 9548#if GLYPH_DEBUG
a2889657 9549
5f5c8ee5
GM
9550 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
9551static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 9552
31b24551 9553
5f5c8ee5
GM
9554/* Dump the contents of glyph matrix MATRIX on stderr. If
9555 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 9556
5f5c8ee5
GM
9557void
9558dump_glyph_matrix (matrix, with_glyphs_p)
9559 struct glyph_matrix *matrix;
9560 int with_glyphs_p;
9561{
efc63ef0 9562 int i;
5f5c8ee5
GM
9563 for (i = 0; i < matrix->nrows; ++i)
9564 dump_glyph_row (matrix, i, with_glyphs_p);
9565}
31b24551 9566
68a37fa8 9567
5f5c8ee5
GM
9568/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9569 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 9570
5f5c8ee5
GM
9571void
9572dump_glyph_row (matrix, vpos, with_glyphs_p)
9573 struct glyph_matrix *matrix;
9574 int vpos, with_glyphs_p;
9575{
9576 struct glyph_row *row;
9577
9578 if (vpos < 0 || vpos >= matrix->nrows)
9579 return;
9580
9581 row = MATRIX_ROW (matrix, vpos);
9582
9583 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
9584 fprintf (stderr, "=============================================\n");
9585
9586 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
9587 row - matrix->rows,
9588 MATRIX_ROW_START_CHARPOS (row),
9589 MATRIX_ROW_END_CHARPOS (row),
9590 row->used[TEXT_AREA],
9591 row->contains_overlapping_glyphs_p,
9592 row->enabled_p,
9593 row->inverse_p,
9594 row->truncated_on_left_p,
9595 row->truncated_on_right_p,
9596 row->overlay_arrow_p,
9597 row->continued_p,
9598 MATRIX_ROW_CONTINUATION_LINE_P (row),
9599 row->displays_text_p,
9600 row->ends_at_zv_p,
9601 row->fill_line_p,
9602 row->x,
9603 row->y,
9604 row->pixel_width);
9605 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
9606 row->end.overlay_string_index);
9607 fprintf (stderr, "%9d %5d\n",
9608 CHARPOS (row->start.string_pos),
9609 CHARPOS (row->end.string_pos));
9610 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
9611 row->end.dpvec_index);
9612
9613 if (with_glyphs_p)
bd66d1ba 9614 {
5f5c8ee5
GM
9615 struct glyph *glyph, *glyph_end;
9616 int prev_had_glyphs_p;
9617
9618 glyph = row->glyphs[TEXT_AREA];
9619 glyph_end = glyph + row->used[TEXT_AREA];
9620
9621 /* Glyph for a line end in text. */
9622 if (glyph == glyph_end && glyph->charpos > 0)
9623 ++glyph_end;
9624
9625 if (glyph < glyph_end)
bd66d1ba 9626 {
5f5c8ee5
GM
9627 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
9628 prev_had_glyphs_p = 1;
bd66d1ba
RS
9629 }
9630 else
5f5c8ee5
GM
9631 prev_had_glyphs_p = 0;
9632
9633 while (glyph < glyph_end)
f7b4b63a 9634 {
5f5c8ee5
GM
9635 if (glyph->type == CHAR_GLYPH)
9636 {
9637 fprintf (stderr,
9638 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9639 glyph - row->glyphs[TEXT_AREA],
9640 'C',
9641 glyph->charpos,
9642 glyph->pixel_width,
9643 glyph->u.ch.code,
9644 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
9645 ? glyph->u.ch.code
9646 : '.'),
9647 glyph->u.ch.face_id,
9648 glyph->left_box_line_p,
9649 glyph->right_box_line_p);
9650 }
9651 else if (glyph->type == STRETCH_GLYPH)
9652 {
9653 fprintf (stderr,
9654 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9655 glyph - row->glyphs[TEXT_AREA],
9656 'S',
9657 glyph->charpos,
9658 glyph->pixel_width,
9659 0,
9660 '.',
9661 glyph->u.stretch.face_id,
9662 glyph->left_box_line_p,
9663 glyph->right_box_line_p);
9664 }
9665 else if (glyph->type == IMAGE_GLYPH)
9666 {
9667 fprintf (stderr,
9668 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9669 glyph - row->glyphs[TEXT_AREA],
9670 'I',
9671 glyph->charpos,
9672 glyph->pixel_width,
9673 glyph->u.img.id,
9674 '.',
9675 glyph->u.img.face_id,
9676 glyph->left_box_line_p,
9677 glyph->right_box_line_p);
9678 }
9679 ++glyph;
f7b4b63a 9680 }
f4faa47c 9681 }
5f5c8ee5 9682}
f4faa47c 9683
a2889657 9684
5f5c8ee5
GM
9685DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
9686 Sdump_glyph_matrix, 0, 1, "p",
9687 "Dump the current matrix of the selected window to stderr.\n\
9688Shows contents of glyph row structures. With non-nil optional\n\
9689parameter WITH-GLYPHS-P, dump glyphs as well.")
9690 (with_glyphs_p)
9691{
9692 struct window *w = XWINDOW (selected_window);
9693 struct buffer *buffer = XBUFFER (w->buffer);
9694
9695 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
9696 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
9697 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
9698 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
9699 fprintf (stderr, "=============================================\n");
9700 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
9701 return Qnil;
9702}
1c2250c2 9703
1fca3fae 9704
5f5c8ee5
GM
9705DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
9706 "Dump glyph row ROW to stderr.")
9707 (row)
9708 Lisp_Object row;
9709{
9710 CHECK_NUMBER (row, 0);
9711 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
9712 return Qnil;
9713}
1fca3fae 9714
67481ae5 9715
5f5c8ee5
GM
9716DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
9717 0, 0, "", "")
9718 ()
9719{
9720 struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
9721 ->current_matrix);
9722 dump_glyph_row (m, 0, 1);
9723 return Qnil;
9724}
ca26e1c8 9725
0f9c0ff0 9726
5f5c8ee5
GM
9727DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
9728 Strace_redisplay_toggle, 0, 0, "",
9729 "Toggle tracing of redisplay.")
9730 ()
9731{
9732 trace_redisplay_p = !trace_redisplay_p;
9733 return Qnil;
9734}
9735
9736
9737#endif /* GLYPH_DEBUG */
ca26e1c8 9738
ca26e1c8 9739
5f5c8ee5
GM
9740\f
9741/***********************************************************************
9742 Building Desired Matrix Rows
9743 ***********************************************************************/
a2889657 9744
5f5c8ee5
GM
9745/* Return a temporary glyph row holding the glyphs of an overlay
9746 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 9747
5f5c8ee5
GM
9748static struct glyph_row *
9749get_overlay_arrow_glyph_row (w)
9750 struct window *w;
9751{
9752 struct frame *f = XFRAME (WINDOW_FRAME (w));
9753 struct buffer *buffer = XBUFFER (w->buffer);
9754 struct buffer *old = current_buffer;
9755 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
9756 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
9757 unsigned char *arrow_end = arrow_string + arrow_len;
9758 unsigned char *p;
9759 struct it it;
9760 int multibyte_p;
9761 int n_glyphs_before;
9762
9763 set_buffer_temp (buffer);
9764 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
9765 it.glyph_row->used[TEXT_AREA] = 0;
9766 SET_TEXT_POS (it.position, 0, 0);
9767
9768 multibyte_p = !NILP (buffer->enable_multibyte_characters);
9769 p = arrow_string;
9770 while (p < arrow_end)
9771 {
9772 Lisp_Object face, ilisp;
9773
9774 /* Get the next character. */
9775 if (multibyte_p)
9776 it.c = STRING_CHAR_AND_LENGTH (p, arrow_len, it.len);
9777 else
9778 it.c = *p, it.len = 1;
9779 p += it.len;
9780
9781 /* Get its face. */
9782 XSETFASTINT (ilisp, p - arrow_string);
9783 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
9784 it.face_id = compute_char_face (f, it.c, face);
9785
9786 /* Compute its width, get its glyphs. */
9787 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
9788 PRODUCE_GLYPHS (&it);
9789
9790 /* If this character doesn't fit any more in the line, we have
9791 to remove some glyphs. */
9792 if (it.current_x > it.last_visible_x)
9793 {
9794 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
9795 break;
9796 }
9797 }
9798
9799 set_buffer_temp (old);
9800 return it.glyph_row;
9801}
ca26e1c8 9802
b0a0fbda 9803
5f5c8ee5
GM
9804/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
9805 glyphs are only inserted for terminal frames since we can't really
9806 win with truncation glyphs when partially visible glyphs are
9807 involved. Which glyphs to insert is determined by
9808 produce_special_glyphs. */
67481ae5 9809
5f5c8ee5
GM
9810static void
9811insert_left_trunc_glyphs (it)
9812 struct it *it;
9813{
9814 struct it truncate_it;
9815 struct glyph *from, *end, *to, *toend;
9816
9817 xassert (!FRAME_WINDOW_P (it->f));
9818
9819 /* Get the truncation glyphs. */
9820 truncate_it = *it;
9821 truncate_it.charset = -1;
9822 truncate_it.current_x = 0;
9823 truncate_it.face_id = DEFAULT_FACE_ID;
9824 truncate_it.glyph_row = &scratch_glyph_row;
9825 truncate_it.glyph_row->used[TEXT_AREA] = 0;
9826 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
9827 truncate_it.object = 0;
9828 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
9829
9830 /* Overwrite glyphs from IT with truncation glyphs. */
9831 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
9832 end = from + truncate_it.glyph_row->used[TEXT_AREA];
9833 to = it->glyph_row->glyphs[TEXT_AREA];
9834 toend = to + it->glyph_row->used[TEXT_AREA];
9835
9836 while (from < end)
9837 *to++ = *from++;
9838
9839 /* There may be padding glyphs left over. Remove them. */
9840 from = to;
9841 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
9842 ++from;
9843 while (from < toend)
9844 *to++ = *from++;
9845
9846 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
9847}
e0bfbde6 9848
e0bfbde6 9849
5f5c8ee5 9850/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 9851
5f5c8ee5
GM
9852 Most of the time, ascent and height of a display line will be equal
9853 to the max_ascent and max_height values of the display iterator
9854 structure. This is not the case if
67481ae5 9855
5f5c8ee5
GM
9856 1. We hit ZV without displaying anything. In this case, max_ascent
9857 and max_height will be zero.
1c9241f5 9858
5f5c8ee5
GM
9859 2. We have some glyphs that don't contribute to the line height.
9860 (The glyph row flag contributes_to_line_height_p is for future
9861 pixmap extensions).
f6fd109b 9862
5f5c8ee5
GM
9863 The first case is easily covered by using default values because in
9864 these cases, the line height does not really matter, except that it
9865 must not be zero. */
67481ae5 9866
5f5c8ee5
GM
9867static void
9868compute_line_metrics (it)
9869 struct it *it;
9870{
9871 struct glyph_row *row = it->glyph_row;
9872 int area, i;
1c2250c2 9873
5f5c8ee5
GM
9874 if (FRAME_WINDOW_P (it->f))
9875 {
9876 int i, top_line_height;
1c2250c2 9877
5f5c8ee5
GM
9878 /* The line may consist of one space only, that was added to
9879 place the cursor on it. If so, the row's height hasn't been
9880 computed yet. */
9881 if (row->height == 0)
9882 {
9883 if (it->max_ascent + it->max_descent == 0)
9884 it->max_descent = CANON_Y_UNIT (it->f);
9885 row->ascent = it->max_ascent;
9886 row->height = it->max_ascent + it->max_descent;
9887 }
9888
9889 /* Compute the width of this line. */
9890 row->pixel_width = row->x;
9891 for (i = 0; i < row->used[TEXT_AREA]; ++i)
9892 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
9893
9894 xassert (row->pixel_width >= 0);
9895 xassert (row->ascent >= 0 && row->height > 0);
9896
9897 /* Compute how much of the line is visible. */
9898 row->visible_height = row->height;
9899
9900 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
9901 if (row->y < top_line_height)
9902 row->visible_height -= top_line_height - row->y;
9903 else
9904 {
9905 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
9906 if (row->y + row->height > max_y)
9907 row->visible_height -= row->y + row->height - max_y;
9908 }
9909 }
9910 else
9911 {
9912 row->pixel_width = row->used[TEXT_AREA];
9913 row->ascent = 0;
9914 row->height = row->visible_height = 1;
9915 }
67481ae5 9916
5f5c8ee5
GM
9917 /* Compute a hash code for this row. */
9918 row->hash = 0;
9919 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9920 for (i = 0; i < row->used[area]; ++i)
9921 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
9922 + row->glyphs[area][i].u.val
9923 + (row->glyphs[area][i].type << 2));
a2889657 9924
5f5c8ee5
GM
9925 it->max_ascent = it->max_descent = 0;
9926}
12adba34 9927
ca26e1c8 9928
5f5c8ee5
GM
9929/* Append one space to the glyph row of iterator IT if doing a
9930 window-based redisplay. DEFAULT_FACE_P non-zero means let the
9931 space have the default face, otherwise let it have the same face as
9932 IT->face_id. This function is called to make sure that there is
9933 always one glyph at the end of a glyph row that the cursor can be
9934 set on under window-systems. (If there weren't such a glyph we
9935 would not know how wide and tall the cursor should be displayed). */
ca26e1c8 9936
5f5c8ee5
GM
9937static void
9938append_space (it, default_face_p)
9939 struct it *it;
9940 int default_face_p;
9941{
9942 if (FRAME_WINDOW_P (it->f))
9943 {
9944 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 9945
5f5c8ee5
GM
9946 if (it->glyph_row->glyphs[TEXT_AREA] + n
9947 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 9948 {
5f5c8ee5
GM
9949 /* Save some values that must not be changed. */
9950 int saved_x = it->current_x;
9951 struct text_pos saved_pos;
9952 int saved_what = it->what;
9953 int saved_face_id = it->face_id;
9954 int saved_charset = it->charset;
9955 Lisp_Object saved_object;
9956
9957 saved_object = it->object;
9958 saved_pos = it->position;
9959
9960 it->what = IT_CHARACTER;
9961 bzero (&it->position, sizeof it->position);
9962 it->object = 0;
9963 it->c = ' ';
9964 it->len = 1;
9965 it->charset = CHARSET_ASCII;
9966
9967 if (default_face_p)
9968 it->face_id = DEFAULT_FACE_ID;
9969 if (it->multibyte_p)
9970 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
9971 else
9972 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
1842fc1a 9973
5f5c8ee5
GM
9974 PRODUCE_GLYPHS (it);
9975
9976 it->current_x = saved_x;
9977 it->object = saved_object;
9978 it->position = saved_pos;
9979 it->what = saved_what;
9980 it->face_id = saved_face_id;
9981 it->charset = saved_charset;
9982 }
9983 }
9984}
12adba34 9985
1842fc1a 9986
5f5c8ee5
GM
9987/* Extend the face of the last glyph in the text area of IT->glyph_row
9988 to the end of the display line. Called from display_line.
9989 If the glyph row is empty, add a space glyph to it so that we
9990 know the face to draw. Set the glyph row flag fill_line_p. */
9991
9992static void
9993extend_face_to_end_of_line (it)
9994 struct it *it;
9995{
9996 struct face *face;
9997 struct frame *f = it->f;
1842fc1a 9998
5f5c8ee5
GM
9999 /* If line is already filled, do nothing. */
10000 if (it->current_x >= it->last_visible_x)
10001 return;
10002
10003 /* Face extension extends the background and box of IT->face_id
10004 to the end of the line. If the background equals the background
10005 of the frame, we haven't to do anything. */
10006 face = FACE_FROM_ID (f, it->face_id);
10007 if (FRAME_WINDOW_P (f)
10008 && face->box == FACE_NO_BOX
10009 && face->background == FRAME_BACKGROUND_PIXEL (f)
10010 && !face->stipple)
10011 return;
1842fc1a 10012
5f5c8ee5
GM
10013 /* Set the glyph row flag indicating that the face of the last glyph
10014 in the text area has to be drawn to the end of the text area. */
10015 it->glyph_row->fill_line_p = 1;
545e04f6 10016
5f5c8ee5
GM
10017 /* If current charset of IT is not ASCII, make sure we have the
10018 ASCII face. This will be automatically undone the next time
10019 get_next_display_element returns a character from a different
10020 charset. Note that the charset will always be ASCII in unibyte
10021 text. */
10022 if (it->charset != CHARSET_ASCII)
10023 {
10024 it->charset = CHARSET_ASCII;
10025 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10026 }
545e04f6 10027
5f5c8ee5
GM
10028 if (FRAME_WINDOW_P (f))
10029 {
10030 /* If the row is empty, add a space with the current face of IT,
10031 so that we know which face to draw. */
10032 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 10033 {
5f5c8ee5
GM
10034 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10035 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10036 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 10037 }
5f5c8ee5
GM
10038 }
10039 else
10040 {
10041 /* Save some values that must not be changed. */
10042 int saved_x = it->current_x;
10043 struct text_pos saved_pos;
10044 Lisp_Object saved_object;
10045 int saved_what = it->what;
10046
10047 saved_object = it->object;
10048 saved_pos = it->position;
10049
10050 it->what = IT_CHARACTER;
10051 bzero (&it->position, sizeof it->position);
10052 it->object = 0;
10053 it->c = ' ';
10054 it->len = 1;
10055
10056 PRODUCE_GLYPHS (it);
10057
10058 while (it->current_x <= it->last_visible_x)
10059 PRODUCE_GLYPHS (it);
10060
10061 /* Don't count these blanks really. It would let us insert a left
10062 truncation glyph below and make us set the cursor on them, maybe. */
10063 it->current_x = saved_x;
10064 it->object = saved_object;
10065 it->position = saved_pos;
10066 it->what = saved_what;
10067 }
10068}
12adba34 10069
545e04f6 10070
5f5c8ee5
GM
10071/* Value is non-zero if text starting at CHARPOS in current_buffer is
10072 trailing whitespace. */
1c9241f5 10073
5f5c8ee5
GM
10074static int
10075trailing_whitespace_p (charpos)
10076 int charpos;
10077{
10078 int bytepos = CHAR_TO_BYTE (charpos);
10079 int c = 0;
7bbe686f 10080
5f5c8ee5
GM
10081 while (bytepos < ZV_BYTE
10082 && (c = FETCH_CHAR (bytepos),
10083 c == ' ' || c == '\t'))
10084 ++bytepos;
0d09d1e6 10085
5f5c8ee5
GM
10086 return bytepos >= ZV_BYTE || c == '\n' || c == '\r';
10087}
31b24551 10088
545e04f6 10089
5f5c8ee5 10090/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 10091
5f5c8ee5
GM
10092void
10093highlight_trailing_whitespace (f, row)
10094 struct frame *f;
10095 struct glyph_row *row;
10096{
10097 int used = row->used[TEXT_AREA];
10098
10099 if (used)
10100 {
10101 struct glyph *start = row->glyphs[TEXT_AREA];
10102 struct glyph *glyph = start + used - 1;
10103
10104 /* Skip over the space glyph inserted to display the
10105 cursor at the end of a line. */
10106 if (glyph->type == CHAR_GLYPH
10107 && glyph->u.ch.code == ' '
10108 && glyph->object == 0)
10109 --glyph;
10110
10111 /* If last glyph is a space or stretch, and it's trailing
10112 whitespace, set the face of all trailing whitespace glyphs in
10113 IT->glyph_row to `trailing-whitespace'. */
10114 if (glyph >= start
10115 && BUFFERP (glyph->object)
10116 && (glyph->type == STRETCH_GLYPH
10117 || (glyph->type == CHAR_GLYPH
10118 && glyph->u.ch.code == ' '))
10119 && trailing_whitespace_p (glyph->charpos))
545e04f6 10120 {
5f5c8ee5
GM
10121 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10122 CHARSET_ASCII);
10123
10124 while (glyph >= start
10125 && BUFFERP (glyph->object)
10126 && (glyph->type == STRETCH_GLYPH
10127 || (glyph->type == CHAR_GLYPH
10128 && glyph->u.ch.code == ' ')))
545e04f6 10129 {
5f5c8ee5
GM
10130 if (glyph->type == STRETCH_GLYPH)
10131 glyph->u.stretch.face_id = face_id;
10132 else
10133 glyph->u.ch.face_id = face_id;
10134 --glyph;
545e04f6
KH
10135 }
10136 }
a2889657 10137 }
5f5c8ee5 10138}
a2889657 10139
5fcbb24d 10140
a2889657 10141
5f5c8ee5
GM
10142/* Construct the glyph row IT->glyph_row in the desired matrix of
10143 IT->w from text at the current position of IT. See dispextern.h
10144 for an overview of struct it. Value is non-zero if
10145 IT->glyph_row displays text, as opposed to a line displaying ZV
10146 only. */
10147
10148static int
10149display_line (it)
10150 struct it *it;
10151{
10152 struct glyph_row *row = it->glyph_row;
10153
10154 /* We always start displaying at hpos zero even if hscrolled. */
10155 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 10156
5f5c8ee5
GM
10157 /* We must not display in a row that's not a text row. */
10158 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10159 < it->w->desired_matrix->nrows);
12adba34 10160
5f5c8ee5
GM
10161 /* Is IT->w showing the region? */
10162 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 10163
5f5c8ee5
GM
10164 /* Clear the result glyph row and enable it. */
10165 prepare_desired_row (row);
12adba34 10166
5f5c8ee5
GM
10167 row->y = it->current_y;
10168 row->start = it->current;
10169 row->continuation_lines_width = it->continuation_lines_width;
10170 row->displays_text_p = 1;
10171
10172 /* Arrange the overlays nicely for our purposes. Usually, we call
10173 display_line on only one line at a time, in which case this
10174 can't really hurt too much, or we call it on lines which appear
10175 one after another in the buffer, in which case all calls to
10176 recenter_overlay_lists but the first will be pretty cheap. */
10177 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10178
10179#if NO_PROMPT_IN_BUFFER
10180 /* Show mini-buffer prompt, if at the beginning of a mini-buffer
10181 window. */
10182 if (MINI_WINDOW_P (it->w)
10183 && MATRIX_ROW_START_CHARPOS (row) == BEG
10184 && it->vpos == 0)
10185 {
10186 if (NILP (minibuf_prompt))
10187 minibuf_prompt_width = minibuf_prompt_pixel_width = 0;
a2889657 10188 else
a2889657 10189 {
5f5c8ee5
GM
10190 /* We would like to truncate the prompt a little bit before
10191 the right margin of the window, so that user input can
10192 start on the first line. Set max_x to this position. */
10193 int max_x = (it->last_visible_x - 4 * CANON_X_UNIT (it->f));
10194
10195 /* We use a temporary iterator different from IT so that
10196 IT's settings are not overwritten when displaying
10197 the prompt. */
10198 struct it ti;
10199
10200 ti = *it;
10201
10202 /* Display the prompt. Set minibuf_prompt_width to the
10203 number of glyphs generated for the prompt, set
10204 minibuf_prompt_pixel_width to its width in pixels. */
10205 xassert (it->current_x == 0);
10206 display_string (NULL, minibuf_prompt, Qnil, 0, 0, &ti,
10207 0, 0, max_x, -1);
10208 minibuf_prompt_width = ti.hpos;
10209 minibuf_prompt_pixel_width = ti.current_x;
10210
10211 /* Transfer pixel and hpos information to IT. */
10212 it->hpos = ti.hpos;
10213 it->current_x = ti.current_x;
10214 }
10215 }
10216#endif /* NO_PROMPT_IN_BUFFER */
10217
10218 /* Move over display elements that are not visible because we are
10219 hscrolled. This may stop at an x-position < IT->first_visible_x
10220 if the first glyph is partially visible or if we hit a line end. */
10221 if (it->current_x < it->first_visible_x)
10222 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10223 MOVE_TO_POS | MOVE_TO_X);
10224
10225 /* Get the initial row height. This is either the height of the
10226 text hscrolled, if there is any, or zero. */
10227 row->ascent = it->max_ascent;
10228 row->height = it->max_ascent + it->max_descent;
10229
10230 /* Loop generating characters. The loop is left with IT on the next
10231 character to display. */
10232 while (1)
10233 {
10234 int n_glyphs_before, hpos_before, x_before;
10235 int x, i, nglyphs;
10236
10237 /* Retrieve the next thing to display. Value is zero if end of
10238 buffer reached. */
10239 if (!get_next_display_element (it))
10240 {
10241 /* Maybe add a space at the end of this line that is used to
10242 display the cursor there under X. */
10243 append_space (it, 1);
10244
10245 /* The position -1 below indicates a blank line not
10246 corresponding to any text, as opposed to an empty line
10247 corresponding to a line end. */
10248 if (row->used[TEXT_AREA] <= 1)
a2889657 10249 {
5f5c8ee5
GM
10250 row->glyphs[TEXT_AREA]->charpos = -1;
10251 row->displays_text_p = 0;
10252
10253 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
10254 row->indicate_empty_line_p = 1;
a2889657 10255 }
5f5c8ee5
GM
10256
10257 it->continuation_lines_width = 0;
10258 row->ends_at_zv_p = 1;
10259 break;
a2889657 10260 }
a2889657 10261
5f5c8ee5
GM
10262 /* Now, get the metrics of what we want to display. This also
10263 generates glyphs in `row' (which is IT->glyph_row). */
10264 n_glyphs_before = row->used[TEXT_AREA];
10265 x = it->current_x;
10266 PRODUCE_GLYPHS (it);
a2889657 10267
5f5c8ee5
GM
10268 /* If this display element was in marginal areas, continue with
10269 the next one. */
10270 if (it->area != TEXT_AREA)
a2889657 10271 {
5f5c8ee5
GM
10272 row->ascent = max (row->ascent, it->max_ascent);
10273 row->height = max (row->height, it->max_ascent + it->max_descent);
10274 set_iterator_to_next (it);
10275 continue;
10276 }
5936754e 10277
5f5c8ee5
GM
10278 /* Does the display element fit on the line? If we truncate
10279 lines, we should draw past the right edge of the window. If
10280 we don't truncate, we want to stop so that we can display the
10281 continuation glyph before the right margin. If lines are
10282 continued, there are two possible strategies for characters
10283 resulting in more than 1 glyph (e.g. tabs): Display as many
10284 glyphs as possible in this line and leave the rest for the
10285 continuation line, or display the whole element in the next
10286 line. Original redisplay did the former, so we do it also. */
10287 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10288 hpos_before = it->hpos;
10289 x_before = x;
10290
10291 if (nglyphs == 1
10292 && it->current_x < it->last_visible_x)
10293 {
10294 ++it->hpos;
10295 row->ascent = max (row->ascent, it->max_ascent);
10296 row->height = max (row->height, it->max_ascent + it->max_descent);
10297 if (it->current_x - it->pixel_width < it->first_visible_x)
10298 row->x = x - it->first_visible_x;
10299 }
10300 else
10301 {
10302 int new_x;
10303 struct glyph *glyph;
10304
10305 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 10306 {
5f5c8ee5
GM
10307 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10308 new_x = x + glyph->pixel_width;
10309
10310 if (/* Lines are continued. */
10311 !it->truncate_lines_p
10312 && (/* Glyph doesn't fit on the line. */
10313 new_x > it->last_visible_x
10314 /* Or it fits exactly on a window system frame. */
10315 || (new_x == it->last_visible_x
10316 && FRAME_WINDOW_P (it->f))))
a2889657 10317 {
5f5c8ee5
GM
10318 /* End of a continued line. */
10319
10320 if (it->hpos == 0
10321 || (new_x == it->last_visible_x
10322 && FRAME_WINDOW_P (it->f)))
10323 {
10324 /* Current glyph fits exactly on the line. We
10325 must continue the line because we can't draw
10326 the cursor after the glyph. */
10327 row->continued_p = 1;
10328 it->current_x = new_x;
10329 it->continuation_lines_width += new_x;
10330 ++it->hpos;
10331 if (i == nglyphs - 1)
10332 set_iterator_to_next (it);
10333 }
10334 else
5936754e 10335 {
5f5c8ee5
GM
10336 /* Display element draws past the right edge of
10337 the window. Restore positions to values
10338 before the element. The next line starts
10339 with current_x before the glyph that could
10340 not be displayed, so that TAB works right. */
10341 row->used[TEXT_AREA] = n_glyphs_before + i;
10342
10343 /* Display continuation glyphs. */
10344 if (!FRAME_WINDOW_P (it->f))
10345 produce_special_glyphs (it, IT_CONTINUATION);
10346 row->continued_p = 1;
10347
10348 it->current_x = x;
10349 it->continuation_lines_width += x;
5936754e 10350 }
5f5c8ee5
GM
10351 break;
10352 }
10353 else if (new_x > it->first_visible_x)
10354 {
10355 /* Increment number of glyphs actually displayed. */
10356 ++it->hpos;
10357
10358 if (x < it->first_visible_x)
10359 /* Glyph is partially visible, i.e. row starts at
10360 negative X position. */
10361 row->x = x - it->first_visible_x;
10362 }
10363 else
10364 {
10365 /* Glyph is completely off the left margin of the
10366 window. This should not happen because of the
10367 move_it_in_display_line at the start of
10368 this function. */
10369 abort ();
a2889657 10370 }
a2889657 10371 }
5f5c8ee5
GM
10372
10373 row->ascent = max (row->ascent, it->max_ascent);
10374 row->height = max (row->height, it->max_ascent + it->max_descent);
10375
10376 /* End of this display line if row is continued. */
10377 if (row->continued_p)
10378 break;
a2889657 10379 }
a2889657 10380
5f5c8ee5
GM
10381 /* Is this a line end? If yes, we're also done, after making
10382 sure that a non-default face is extended up to the right
10383 margin of the window. */
10384 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 10385 {
5f5c8ee5
GM
10386 int used_before = row->used[TEXT_AREA];
10387
10388 /* Add a space at the end of the line that is used to
10389 display the cursor there. */
10390 append_space (it, 0);
10391
10392 /* Extend the face to the end of the line. */
10393 extend_face_to_end_of_line (it);
10394
10395 /* Make sure we have the position. */
10396 if (used_before == 0)
10397 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
10398
10399 /* Consume the line end. This skips over invisible lines. */
10400 set_iterator_to_next (it);
10401 it->continuation_lines_width = 0;
10402 break;
1c9241f5 10403 }
a2889657 10404
5f5c8ee5
GM
10405 /* Proceed with next display element. Note that this skips
10406 over lines invisible because of selective display. */
10407 set_iterator_to_next (it);
b1d1124b 10408
5f5c8ee5
GM
10409 /* If we truncate lines, we are done when the last displayed
10410 glyphs reach past the right margin of the window. */
10411 if (it->truncate_lines_p
10412 && (FRAME_WINDOW_P (it->f)
10413 ? (it->current_x >= it->last_visible_x)
10414 : (it->current_x > it->last_visible_x)))
75d13c64 10415 {
5f5c8ee5
GM
10416 /* Maybe add truncation glyphs. */
10417 if (!FRAME_WINDOW_P (it->f))
10418 {
10419 --it->glyph_row->used[TEXT_AREA];
10420 produce_special_glyphs (it, IT_TRUNCATION);
10421 }
10422
10423 row->truncated_on_right_p = 1;
10424 it->continuation_lines_width = 0;
10425 reseat_at_next_visible_line_start (it);
10426 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
10427 it->hpos = hpos_before;
10428 it->current_x = x_before;
10429 break;
75d13c64 10430 }
a2889657 10431 }
a2889657 10432
5f5c8ee5
GM
10433 /* If line is not empty and hscrolled, maybe insert truncation glyphs
10434 at the left window margin. */
10435 if (it->first_visible_x
10436 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
10437 {
10438 if (!FRAME_WINDOW_P (it->f))
10439 insert_left_trunc_glyphs (it);
10440 row->truncated_on_left_p = 1;
10441 }
a2889657 10442
5f5c8ee5
GM
10443 /* If the start of this line is the overlay arrow-position, then
10444 mark this glyph row as the one containing the overlay arrow.
10445 This is clearly a mess with variable size fonts. It would be
10446 better to let it be displayed like cursors under X. */
e24c997d 10447 if (MARKERP (Voverlay_arrow_position)
a2889657 10448 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
10449 && (MATRIX_ROW_START_CHARPOS (row)
10450 == marker_position (Voverlay_arrow_position))
e24c997d 10451 && STRINGP (Voverlay_arrow_string)
a2889657
JB
10452 && ! overlay_arrow_seen)
10453 {
5f5c8ee5
GM
10454 /* Overlay arrow in window redisplay is a bitmap. */
10455 if (!FRAME_WINDOW_P (it->f))
c4628384 10456 {
5f5c8ee5
GM
10457 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
10458 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
10459 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
10460 struct glyph *p = row->glyphs[TEXT_AREA];
10461 struct glyph *p2, *end;
10462
10463 /* Copy the arrow glyphs. */
10464 while (glyph < arrow_end)
10465 *p++ = *glyph++;
10466
10467 /* Throw away padding glyphs. */
10468 p2 = p;
10469 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
10470 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
10471 ++p2;
10472 if (p2 > p)
212e4f87 10473 {
5f5c8ee5
GM
10474 while (p2 < end)
10475 *p++ = *p2++;
10476 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 10477 }
c4628384 10478 }
5f5c8ee5 10479
a2889657 10480 overlay_arrow_seen = 1;
5f5c8ee5 10481 row->overlay_arrow_p = 1;
a2889657
JB
10482 }
10483
5f5c8ee5
GM
10484 /* Compute pixel dimensions of this line. */
10485 compute_line_metrics (it);
10486
10487 /* Remember the position at which this line ends. */
10488 row->end = it->current;
10489
10490 /* Maybe set the cursor. If you change this, it's probably a good
10491 idea to also change the code in redisplay_window for cursor
10492 movement in an unchanged window. */
10493 if (it->w->cursor.vpos < 0
10494 && PT >= MATRIX_ROW_START_CHARPOS (row)
10495 && MATRIX_ROW_END_CHARPOS (row) >= PT
10496 && !(MATRIX_ROW_END_CHARPOS (row) == PT
10497 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10498 || !row->ends_at_zv_p)))
10499 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
10500
10501 /* Highlight trailing whitespace. */
10502 if (it->show_trailing_whitespace_p)
10503 highlight_trailing_whitespace (it->f, it->glyph_row);
10504
10505 /* Prepare for the next line. This line starts horizontally at (X
10506 HPOS) = (0 0). Vertical positions are incremented. As a
10507 convenience for the caller, IT->glyph_row is set to the next
10508 row to be used. */
10509 it->current_x = it->hpos = 0;
10510 it->current_y += row->height;
10511 ++it->vpos;
10512 ++it->glyph_row;
10513 return row->displays_text_p;
a2889657 10514}
5f5c8ee5
GM
10515
10516
a2889657 10517\f
5f5c8ee5
GM
10518/***********************************************************************
10519 Menu Bar
10520 ***********************************************************************/
10521
10522/* Redisplay the menu bar in the frame for window W.
10523
10524 The menu bar of X frames that don't have X toolkit support is
10525 displayed in a special window W->frame->menu_bar_window.
10526
10527 The menu bar of terminal frames is treated specially as far as
10528 glyph matrices are concerned. Menu bar lines are not part of
10529 windows, so the update is done directly on the frame matrix rows
10530 for the menu bar. */
7ce2c095
RS
10531
10532static void
10533display_menu_bar (w)
10534 struct window *w;
10535{
5f5c8ee5
GM
10536 struct frame *f = XFRAME (WINDOW_FRAME (w));
10537 struct it it;
10538 Lisp_Object items;
8351baf2 10539 int i;
7ce2c095 10540
5f5c8ee5 10541 /* Don't do all this for graphical frames. */
dc937613 10542#ifdef HAVE_NTGUI
d129c4c2
KH
10543 if (!NILP (Vwindow_system))
10544 return;
dc937613 10545#endif
dc937613 10546#ifdef USE_X_TOOLKIT
d3413a53 10547 if (FRAME_X_P (f))
7ce2c095 10548 return;
5f5c8ee5
GM
10549#endif
10550
10551#ifdef USE_X_TOOLKIT
10552 xassert (!FRAME_WINDOW_P (f));
10553 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
10554 it.first_visible_x = 0;
10555 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
10556#else /* not USE_X_TOOLKIT */
10557 if (FRAME_WINDOW_P (f))
10558 {
10559 /* Menu bar lines are displayed in the desired matrix of the
10560 dummy window menu_bar_window. */
10561 struct window *menu_w;
10562 xassert (WINDOWP (f->menu_bar_window));
10563 menu_w = XWINDOW (f->menu_bar_window);
10564 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
10565 MODE_LINE_FACE_ID);
10566 it.first_visible_x = 0;
10567 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
10568 }
10569 else
10570 {
10571 /* This is a TTY frame, i.e. character hpos/vpos are used as
10572 pixel x/y. */
10573 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
10574 MODE_LINE_FACE_ID);
10575 it.first_visible_x = 0;
10576 it.last_visible_x = FRAME_WIDTH (f);
10577 }
10578#endif /* not USE_X_TOOLKIT */
10579
10580 /* Clear all rows of the menu bar. */
10581 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
10582 {
10583 struct glyph_row *row = it.glyph_row + i;
10584 clear_glyph_row (row);
10585 row->enabled_p = 1;
10586 row->full_width_p = 1;
10587 }
7ce2c095 10588
5f5c8ee5
GM
10589 /* Make the first line of the menu bar appear in reverse video. */
10590 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 10591
5f5c8ee5
GM
10592 /* Display all items of the menu bar. */
10593 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 10594 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 10595 {
5f5c8ee5
GM
10596 Lisp_Object string;
10597
10598 /* Stop at nil string. */
8351baf2
RS
10599 string = XVECTOR (items)->contents[i + 1];
10600 if (NILP (string))
10601 break;
2d66ad19 10602
5f5c8ee5
GM
10603 /* Remember where item was displayed. */
10604 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 10605
5f5c8ee5
GM
10606 /* Display the item, pad with one space. */
10607 if (it.current_x < it.last_visible_x)
10608 display_string (NULL, string, Qnil, 0, 0, &it,
10609 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
10610 }
10611
2d66ad19 10612 /* Fill out the line with spaces. */
5f5c8ee5
GM
10613 if (it.current_x < it.last_visible_x)
10614 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 10615
5f5c8ee5
GM
10616 /* Compute the total height of the lines. */
10617 compute_line_metrics (&it);
7ce2c095 10618}
5f5c8ee5
GM
10619
10620
7ce2c095 10621\f
5f5c8ee5
GM
10622/***********************************************************************
10623 Mode Line
10624 ***********************************************************************/
10625
10626/* Display the mode and/or top line of window W. */
a2889657
JB
10627
10628static void
5f5c8ee5 10629display_mode_lines (w)
a2889657
JB
10630 struct window *w;
10631{
5f5c8ee5 10632 /* These will be set while the mode line specs are processed. */
aa6d10fa 10633 line_number_displayed = 0;
155ef550 10634 w->column_number_displayed = Qnil;
aa6d10fa 10635
5f5c8ee5
GM
10636 if (WINDOW_WANTS_MODELINE_P (w))
10637 display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
10638
10639 if (WINDOW_WANTS_TOP_LINE_P (w))
10640 display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
10641}
03b294dc 10642
03b294dc 10643
5f5c8ee5
GM
10644/* Display mode or top line of window W. FACE_ID specifies which line
10645 to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
10646 FORMAT is the mode line format to display. */
03b294dc 10647
5f5c8ee5
GM
10648static void
10649display_mode_line (w, face_id, format)
10650 struct window *w;
10651 enum face_id face_id;
10652 Lisp_Object format;
10653{
10654 struct it it;
10655 struct face *face;
03b294dc 10656
5f5c8ee5
GM
10657 init_iterator (&it, w, -1, -1, NULL, face_id);
10658 prepare_desired_row (it.glyph_row);
10659
10660 /* Temporarily make frame's keyboard the current kboard so that
10661 kboard-local variables in the mode_line_format will get the right
10662 values. */
10663 push_frame_kboard (it.f);
10664 display_mode_element (&it, 0, 0, 0, format);
10665 pop_frame_kboard ();
a2889657 10666
5f5c8ee5
GM
10667 /* Fill up with spaces. */
10668 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
10669
10670 compute_line_metrics (&it);
10671 it.glyph_row->full_width_p = 1;
10672 it.glyph_row->mode_line_p = 1;
10673 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
10674 it.glyph_row->continued_p = 0;
10675 it.glyph_row->truncated_on_left_p = 0;
10676 it.glyph_row->truncated_on_right_p = 0;
10677
10678 /* Make a 3D mode-line have a shadow at its right end. */
10679 face = FACE_FROM_ID (it.f, face_id);
10680 extend_face_to_end_of_line (&it);
10681 if (face->box != FACE_NO_BOX)
d7eb09a0 10682 {
5f5c8ee5
GM
10683 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
10684 + it.glyph_row->used[TEXT_AREA] - 1);
10685 last->right_box_line_p = 1;
d7eb09a0 10686 }
a2889657
JB
10687}
10688
a2889657 10689
5f5c8ee5
GM
10690/* Contribute ELT to the mode line for window IT->w. How it
10691 translates into text depends on its data type.
a2889657 10692
5f5c8ee5 10693 IT describes the display environment in which we display, as usual.
a2889657
JB
10694
10695 DEPTH is the depth in recursion. It is used to prevent
10696 infinite recursion here.
10697
5f5c8ee5
GM
10698 FIELD_WIDTH is the number of characters the display of ELT should
10699 occupy in the mode line, and PRECISION is the maximum number of
10700 characters to display from ELT's representation. See
10701 display_string for details. *
a2889657 10702
5f5c8ee5 10703 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
10704
10705static int
5f5c8ee5
GM
10706display_mode_element (it, depth, field_width, precision, elt)
10707 struct it *it;
a2889657 10708 int depth;
5f5c8ee5
GM
10709 int field_width, precision;
10710 Lisp_Object elt;
a2889657 10711{
5f5c8ee5
GM
10712 int n = 0, field, prec;
10713
a2889657
JB
10714 tail_recurse:
10715 if (depth > 10)
10716 goto invalid;
10717
10718 depth++;
10719
0220c518 10720 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
10721 {
10722 case Lisp_String:
10723 {
10724 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
10725 unsigned char c;
10726 unsigned char *this = XSTRING (elt)->data;
10727 unsigned char *lisp_string = this;
10728
10729 while ((precision <= 0 || n < precision)
10730 && *this
10731 && (frame_title_ptr
10732 || it->current_x < it->last_visible_x))
a2889657
JB
10733 {
10734 unsigned char *last = this;
5f5c8ee5
GM
10735
10736 /* Advance to end of string or next format specifier. */
a2889657
JB
10737 while ((c = *this++) != '\0' && c != '%')
10738 ;
5f5c8ee5 10739
a2889657
JB
10740 if (this - 1 != last)
10741 {
5f5c8ee5
GM
10742 /* Output to end of string or up to '%'. Field width
10743 is length of string. Don't output more than
10744 PRECISION allows us. */
10745 prec = --this - last;
10746 if (precision > 0 && prec > precision - n)
10747 prec = precision - n;
10748
d39b6696 10749 if (frame_title_ptr)
5f5c8ee5 10750 n += store_frame_title (last, prec, prec);
d39b6696 10751 else
5f5c8ee5
GM
10752 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
10753 it, 0, prec, 0, -1);
a2889657
JB
10754 }
10755 else /* c == '%' */
10756 {
5f5c8ee5
GM
10757 unsigned char *percent_position = this;
10758
10759 /* Get the specified minimum width. Zero means
10760 don't pad. */
10761 field = 0;
a2889657 10762 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 10763 field = field * 10 + c - '0';
a2889657 10764
5f5c8ee5
GM
10765 /* Don't pad beyond the total padding allowed. */
10766 if (field_width - n > 0 && field > field_width - n)
10767 field = field_width - n;
a2889657 10768
5f5c8ee5
GM
10769 /* Note that either PRECISION <= 0 or N < PRECISION. */
10770 prec = precision - n;
10771
a2889657 10772 if (c == 'M')
5f5c8ee5
GM
10773 n += display_mode_element (it, depth, field, prec,
10774 Vglobal_mode_string);
a2889657 10775 else if (c != 0)
d39b6696 10776 {
5f5c8ee5
GM
10777 unsigned char *spec
10778 = decode_mode_spec (it->w, c, field, prec);
10779
d39b6696 10780 if (frame_title_ptr)
5f5c8ee5 10781 n += store_frame_title (spec, field, prec);
d39b6696 10782 else
5f5c8ee5
GM
10783 {
10784 int nglyphs_before
10785 = it->glyph_row->used[TEXT_AREA];
10786 int charpos
10787 = percent_position - XSTRING (elt)->data;
10788 int nwritten
10789 = display_string (spec, Qnil, elt, charpos, 0, it,
10790 field, prec, 0, -1);
10791
10792 /* Assign to the glyphs written above the
10793 string where the `%x' came from, position
10794 of the `%'. */
10795 if (nwritten > 0)
10796 {
10797 struct glyph *glyph
10798 = (it->glyph_row->glyphs[TEXT_AREA]
10799 + nglyphs_before);
10800 int i;
10801
10802 for (i = 0; i < nwritten; ++i)
10803 {
10804 glyph[i].object = elt;
10805 glyph[i].charpos = charpos;
10806 }
10807
10808 n += nwritten;
10809 }
10810 }
d39b6696 10811 }
a2889657
JB
10812 }
10813 }
10814 }
10815 break;
10816
10817 case Lisp_Symbol:
10818 /* A symbol: process the value of the symbol recursively
10819 as if it appeared here directly. Avoid error if symbol void.
10820 Special case: if value of symbol is a string, output the string
10821 literally. */
10822 {
10823 register Lisp_Object tem;
10824 tem = Fboundp (elt);
265a9e55 10825 if (!NILP (tem))
a2889657
JB
10826 {
10827 tem = Fsymbol_value (elt);
10828 /* If value is a string, output that string literally:
10829 don't check for % within it. */
e24c997d 10830 if (STRINGP (tem))
d39b6696 10831 {
5f5c8ee5
GM
10832 prec = XSTRING (tem)->size;
10833 if (precision > 0 && prec > precision - n)
10834 prec = precision - n;
d39b6696 10835 if (frame_title_ptr)
5f5c8ee5 10836 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 10837 else
5f5c8ee5
GM
10838 n += display_string (NULL, tem, Qnil, 0, 0, it,
10839 0, prec, 0, -1);
d39b6696 10840 }
a2889657 10841 else if (!EQ (tem, elt))
5f5c8ee5
GM
10842 {
10843 /* Give up right away for nil or t. */
10844 elt = tem;
10845 goto tail_recurse;
10846 }
a2889657
JB
10847 }
10848 }
10849 break;
10850
10851 case Lisp_Cons:
10852 {
10853 register Lisp_Object car, tem;
10854
10855 /* A cons cell: three distinct cases.
10856 If first element is a string or a cons, process all the elements
10857 and effectively concatenate them.
10858 If first element is a negative number, truncate displaying cdr to
10859 at most that many characters. If positive, pad (with spaces)
10860 to at least that many characters.
10861 If first element is a symbol, process the cadr or caddr recursively
10862 according to whether the symbol's value is non-nil or nil. */
10863 car = XCONS (elt)->car;
5f5c8ee5
GM
10864 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
10865 {
10866 /* An element of the form (:eval FORM) means evaluate FORM
10867 and use the result as mode line elements. */
10868 struct gcpro gcpro1;
10869 Lisp_Object spec;
10870
10871 spec = eval_form (XCAR (XCDR (elt)));
10872 GCPRO1 (spec);
10873 n += display_mode_element (it, depth, field_width - n,
10874 precision - n, spec);
10875 UNGCPRO;
10876 }
10877 else if (SYMBOLP (car))
a2889657
JB
10878 {
10879 tem = Fboundp (car);
10880 elt = XCONS (elt)->cdr;
e24c997d 10881 if (!CONSP (elt))
a2889657
JB
10882 goto invalid;
10883 /* elt is now the cdr, and we know it is a cons cell.
10884 Use its car if CAR has a non-nil value. */
265a9e55 10885 if (!NILP (tem))
a2889657
JB
10886 {
10887 tem = Fsymbol_value (car);
265a9e55 10888 if (!NILP (tem))
a2889657
JB
10889 { elt = XCONS (elt)->car; goto tail_recurse; }
10890 }
10891 /* Symbol's value is nil (or symbol is unbound)
10892 Get the cddr of the original list
10893 and if possible find the caddr and use that. */
10894 elt = XCONS (elt)->cdr;
265a9e55 10895 if (NILP (elt))
a2889657 10896 break;
e24c997d 10897 else if (!CONSP (elt))
a2889657
JB
10898 goto invalid;
10899 elt = XCONS (elt)->car;
10900 goto tail_recurse;
10901 }
e24c997d 10902 else if (INTEGERP (car))
a2889657
JB
10903 {
10904 register int lim = XINT (car);
10905 elt = XCONS (elt)->cdr;
10906 if (lim < 0)
5f5c8ee5
GM
10907 {
10908 /* Negative int means reduce maximum width. */
10909 if (precision <= 0)
10910 precision = -lim;
10911 else
10912 precision = min (precision, -lim);
10913 }
a2889657
JB
10914 else if (lim > 0)
10915 {
10916 /* Padding specified. Don't let it be more than
10917 current maximum. */
5f5c8ee5
GM
10918 if (precision > 0)
10919 lim = min (precision, lim);
10920
a2889657
JB
10921 /* If that's more padding than already wanted, queue it.
10922 But don't reduce padding already specified even if
10923 that is beyond the current truncation point. */
5f5c8ee5 10924 field_width = max (lim, field_width);
a2889657
JB
10925 }
10926 goto tail_recurse;
10927 }
e24c997d 10928 else if (STRINGP (car) || CONSP (car))
a2889657
JB
10929 {
10930 register int limit = 50;
5f5c8ee5
GM
10931 /* Limit is to protect against circular lists. */
10932 while (CONSP (elt)
10933 && --limit > 0
10934 && (precision <= 0 || n < precision))
a2889657 10935 {
5f5c8ee5
GM
10936 n += display_mode_element (it, depth, field_width - n,
10937 precision - n, XCONS (elt)->car);
a2889657
JB
10938 elt = XCONS (elt)->cdr;
10939 }
10940 }
10941 }
10942 break;
10943
10944 default:
10945 invalid:
d39b6696 10946 if (frame_title_ptr)
5f5c8ee5 10947 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 10948 else
5f5c8ee5
GM
10949 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
10950 precision - n, 0, 0);
10951 return n;
a2889657
JB
10952 }
10953
5f5c8ee5
GM
10954 /* Pad to FIELD_WIDTH. */
10955 if (field_width > 0 && n < field_width)
10956 {
10957 if (frame_title_ptr)
10958 n += store_frame_title ("", field_width - n, 0);
10959 else
10960 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
10961 0, 0, 0);
10962 }
10963
10964 return n;
a2889657 10965}
5f5c8ee5
GM
10966
10967
766525bc
RS
10968/* Write a null-terminated, right justified decimal representation of
10969 the positive integer D to BUF using a minimal field width WIDTH. */
10970
10971static void
10972pint2str (buf, width, d)
10973 register char *buf;
10974 register int width;
10975 register int d;
10976{
10977 register char *p = buf;
10978
10979 if (d <= 0)
5f5c8ee5 10980 *p++ = '0';
766525bc 10981 else
5f5c8ee5 10982 {
766525bc 10983 while (d > 0)
5f5c8ee5 10984 {
766525bc
RS
10985 *p++ = d % 10 + '0';
10986 d /= 10;
5f5c8ee5
GM
10987 }
10988 }
10989
10990 for (width -= (int) (p - buf); width > 0; --width)
10991 *p++ = ' ';
766525bc
RS
10992 *p-- = '\0';
10993 while (p > buf)
5f5c8ee5 10994 {
766525bc
RS
10995 d = *buf;
10996 *buf++ = *p;
10997 *p-- = d;
5f5c8ee5 10998 }
766525bc
RS
10999}
11000
5f5c8ee5 11001/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
11002 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11003 type of CODING_SYSTEM. Return updated pointer into BUF. */
11004
6693a99a 11005static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 11006
1c9241f5
KH
11007static char *
11008decode_mode_spec_coding (coding_system, buf, eol_flag)
11009 Lisp_Object coding_system;
11010 register char *buf;
11011 int eol_flag;
11012{
1e1078d6 11013 Lisp_Object val;
916848d8 11014 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
11015 unsigned char *eol_str;
11016 int eol_str_len;
11017 /* The EOL conversion we are using. */
11018 Lisp_Object eoltype;
1e1078d6
RS
11019
11020 val = coding_system;
1c9241f5
KH
11021
11022 if (NILP (val)) /* Not yet decided. */
11023 {
916848d8
RS
11024 if (multibyte)
11025 *buf++ = '-';
21e989e3 11026 if (eol_flag)
302f2b38 11027 eoltype = eol_mnemonic_undecided;
1e1078d6 11028 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
11029 }
11030 else
11031 {
1e1078d6
RS
11032 Lisp_Object eolvalue;
11033
11034 eolvalue = Fget (coding_system, Qeol_type);
11035
1c9241f5 11036 while (!NILP (val) && SYMBOLP (val))
1e1078d6
RS
11037 {
11038 val = Fget (val, Qcoding_system);
11039 if (NILP (eolvalue))
b070c1d7 11040 eolvalue = Fget (val, Qeol_type);
1e1078d6
RS
11041 }
11042
916848d8
RS
11043 if (multibyte)
11044 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11045
1c9241f5
KH
11046 if (eol_flag)
11047 {
1e1078d6
RS
11048 /* The EOL conversion that is normal on this system. */
11049
11050 if (NILP (eolvalue)) /* Not yet decided. */
11051 eoltype = eol_mnemonic_undecided;
11052 else if (VECTORP (eolvalue)) /* Not yet decided. */
11053 eoltype = eol_mnemonic_undecided;
11054 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11055 eoltype = (XFASTINT (eolvalue) == 0
11056 ? eol_mnemonic_unix
11057 : (XFASTINT (eolvalue) == 1
11058 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
11059 }
11060 }
5f5c8ee5 11061
302f2b38
EZ
11062 if (eol_flag)
11063 {
11064 /* Mention the EOL conversion if it is not the usual one. */
11065 if (STRINGP (eoltype))
11066 {
11067 eol_str = XSTRING (eoltype)->data;
11068 eol_str_len = XSTRING (eoltype)->size;
11069 }
f30b3499
KH
11070 else if (INTEGERP (eoltype)
11071 && CHAR_VALID_P (XINT (eoltype), 0))
11072 {
11073 int c = XINT (eoltype);
11074 unsigned char work[4];
11075
11076 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11077 }
302f2b38
EZ
11078 else
11079 {
11080 eol_str = invalid_eol_type;
11081 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 11082 }
f30b3499 11083 bcopy (eol_str, buf, eol_str_len);
302f2b38 11084 buf += eol_str_len;
1c9241f5 11085 }
302f2b38 11086
1c9241f5
KH
11087 return buf;
11088}
11089
a2889657 11090/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
11091 generated by character C. PRECISION >= 0 means don't return a
11092 string longer than that value. FIELD_WIDTH > 0 means pad the
11093 string returned with spaces to that value. */
a2889657 11094
11e82b76
JB
11095static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11096
a2889657 11097static char *
5f5c8ee5 11098decode_mode_spec (w, c, field_width, precision)
a2889657
JB
11099 struct window *w;
11100 register char c;
5f5c8ee5 11101 int field_width, precision;
a2889657 11102{
0b67772d 11103 Lisp_Object obj;
5f5c8ee5
GM
11104 struct frame *f = XFRAME (WINDOW_FRAME (w));
11105 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 11106 struct buffer *b = XBUFFER (w->buffer);
a2889657 11107
0b67772d 11108 obj = Qnil;
a2889657
JB
11109
11110 switch (c)
11111 {
1af9f229
RS
11112 case '*':
11113 if (!NILP (b->read_only))
11114 return "%";
11115 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11116 return "*";
11117 return "-";
11118
11119 case '+':
11120 /* This differs from %* only for a modified read-only buffer. */
11121 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11122 return "*";
11123 if (!NILP (b->read_only))
11124 return "%";
11125 return "-";
11126
11127 case '&':
11128 /* This differs from %* in ignoring read-only-ness. */
11129 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11130 return "*";
11131 return "-";
11132
11133 case '%':
11134 return "%";
11135
11136 case '[':
11137 {
11138 int i;
11139 char *p;
11140
11141 if (command_loop_level > 5)
11142 return "[[[... ";
11143 p = decode_mode_spec_buf;
11144 for (i = 0; i < command_loop_level; i++)
11145 *p++ = '[';
11146 *p = 0;
11147 return decode_mode_spec_buf;
11148 }
11149
11150 case ']':
11151 {
11152 int i;
11153 char *p;
11154
11155 if (command_loop_level > 5)
11156 return " ...]]]";
11157 p = decode_mode_spec_buf;
11158 for (i = 0; i < command_loop_level; i++)
11159 *p++ = ']';
11160 *p = 0;
11161 return decode_mode_spec_buf;
11162 }
11163
11164 case '-':
11165 {
1af9f229 11166 register int i;
5f5c8ee5
GM
11167
11168 /* Let lots_of_dashes be a string of infinite length. */
11169 if (field_width <= 0
11170 || field_width > sizeof (lots_of_dashes))
1af9f229 11171 {
5f5c8ee5
GM
11172 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11173 decode_mode_spec_buf[i] = '-';
11174 decode_mode_spec_buf[i] = '\0';
11175 return decode_mode_spec_buf;
1af9f229 11176 }
5f5c8ee5
GM
11177 else
11178 return lots_of_dashes;
1af9f229
RS
11179 }
11180
a2889657 11181 case 'b':
d39b6696 11182 obj = b->name;
a2889657
JB
11183 break;
11184
1af9f229
RS
11185 case 'c':
11186 {
11187 int col = current_column ();
11188 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 11189 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
11190 return decode_mode_spec_buf;
11191 }
11192
11193 case 'F':
11194 /* %F displays the frame name. */
5f5c8ee5 11195 if (!NILP (f->title))
95184b48 11196 return (char *) XSTRING (f->title)->data;
fd8ff63d 11197 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 11198 return (char *) XSTRING (f->name)->data;
9c6da96f 11199 return "Emacs";
1af9f229 11200
a2889657 11201 case 'f':
d39b6696 11202 obj = b->filename;
a2889657
JB
11203 break;
11204
aa6d10fa
RS
11205 case 'l':
11206 {
12adba34
RS
11207 int startpos = XMARKER (w->start)->charpos;
11208 int startpos_byte = marker_byte_position (w->start);
11209 int line, linepos, linepos_byte, topline;
aa6d10fa 11210 int nlines, junk;
aa6d10fa
RS
11211 int height = XFASTINT (w->height);
11212
11213 /* If we decided that this buffer isn't suitable for line numbers,
11214 don't forget that too fast. */
11215 if (EQ (w->base_line_pos, w->buffer))
766525bc 11216 goto no_value;
5300fd39
RS
11217 /* But do forget it, if the window shows a different buffer now. */
11218 else if (BUFFERP (w->base_line_pos))
11219 w->base_line_pos = Qnil;
aa6d10fa
RS
11220
11221 /* If the buffer is very big, don't waste time. */
d39b6696 11222 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
11223 {
11224 w->base_line_pos = Qnil;
11225 w->base_line_number = Qnil;
766525bc 11226 goto no_value;
aa6d10fa
RS
11227 }
11228
11229 if (!NILP (w->base_line_number)
11230 && !NILP (w->base_line_pos)
12adba34 11231 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
11232 {
11233 line = XFASTINT (w->base_line_number);
11234 linepos = XFASTINT (w->base_line_pos);
12adba34 11235 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
11236 }
11237 else
11238 {
11239 line = 1;
d39b6696 11240 linepos = BUF_BEGV (b);
12adba34 11241 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
11242 }
11243
11244 /* Count lines from base line to window start position. */
12adba34
RS
11245 nlines = display_count_lines (linepos, linepos_byte,
11246 startpos_byte,
11247 startpos, &junk);
aa6d10fa
RS
11248
11249 topline = nlines + line;
11250
11251 /* Determine a new base line, if the old one is too close
11252 or too far away, or if we did not have one.
11253 "Too close" means it's plausible a scroll-down would
11254 go back past it. */
d39b6696 11255 if (startpos == BUF_BEGV (b))
aa6d10fa 11256 {
c2213350
KH
11257 XSETFASTINT (w->base_line_number, topline);
11258 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
11259 }
11260 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 11261 || linepos == BUF_BEGV (b))
aa6d10fa 11262 {
d39b6696 11263 int limit = BUF_BEGV (b);
12adba34 11264 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
11265 int position;
11266 int distance = (height * 2 + 30) * 200;
11267
11268 if (startpos - distance > limit)
12adba34
RS
11269 {
11270 limit = startpos - distance;
11271 limit_byte = CHAR_TO_BYTE (limit);
11272 }
aa6d10fa 11273
12adba34
RS
11274 nlines = display_count_lines (startpos, startpos_byte,
11275 limit_byte,
11276 - (height * 2 + 30),
aa6d10fa
RS
11277 &position);
11278 /* If we couldn't find the lines we wanted within
11279 200 chars per line,
11280 give up on line numbers for this window. */
12adba34 11281 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
11282 {
11283 w->base_line_pos = w->buffer;
11284 w->base_line_number = Qnil;
766525bc 11285 goto no_value;
aa6d10fa
RS
11286 }
11287
c2213350 11288 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 11289 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
11290 }
11291
11292 /* Now count lines from the start pos to point. */
12adba34
RS
11293 nlines = display_count_lines (startpos, startpos_byte,
11294 PT_BYTE, PT, &junk);
aa6d10fa
RS
11295
11296 /* Record that we did display the line number. */
11297 line_number_displayed = 1;
11298
11299 /* Make the string to show. */
5f5c8ee5 11300 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 11301 return decode_mode_spec_buf;
766525bc
RS
11302 no_value:
11303 {
11304 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
11305 int pad = field_width - 2;
11306 while (pad-- > 0)
11307 *p++ = ' ';
11308 *p++ = '?';
11309 *p = '?';
766525bc
RS
11310 return decode_mode_spec_buf;
11311 }
aa6d10fa
RS
11312 }
11313 break;
11314
a2889657 11315 case 'm':
d39b6696 11316 obj = b->mode_name;
a2889657
JB
11317 break;
11318
11319 case 'n':
d39b6696 11320 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
11321 return " Narrow";
11322 break;
11323
a2889657
JB
11324 case 'p':
11325 {
11326 int pos = marker_position (w->start);
d39b6696 11327 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 11328
d39b6696 11329 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 11330 {
d39b6696 11331 if (pos <= BUF_BEGV (b))
a2889657
JB
11332 return "All";
11333 else
11334 return "Bottom";
11335 }
d39b6696 11336 else if (pos <= BUF_BEGV (b))
a2889657
JB
11337 return "Top";
11338 else
11339 {
3c7d31b9
RS
11340 if (total > 1000000)
11341 /* Do it differently for a large value, to avoid overflow. */
11342 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11343 else
11344 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
11345 /* We can't normally display a 3-digit number,
11346 so get us a 2-digit number that is close. */
11347 if (total == 100)
11348 total = 99;
11349 sprintf (decode_mode_spec_buf, "%2d%%", total);
11350 return decode_mode_spec_buf;
11351 }
11352 }
11353
8ffcb79f
RS
11354 /* Display percentage of size above the bottom of the screen. */
11355 case 'P':
11356 {
11357 int toppos = marker_position (w->start);
d39b6696
KH
11358 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
11359 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 11360
d39b6696 11361 if (botpos >= BUF_ZV (b))
8ffcb79f 11362 {
d39b6696 11363 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
11364 return "All";
11365 else
11366 return "Bottom";
11367 }
11368 else
11369 {
3c7d31b9
RS
11370 if (total > 1000000)
11371 /* Do it differently for a large value, to avoid overflow. */
11372 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11373 else
11374 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
11375 /* We can't normally display a 3-digit number,
11376 so get us a 2-digit number that is close. */
11377 if (total == 100)
11378 total = 99;
d39b6696 11379 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
11380 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
11381 else
11382 sprintf (decode_mode_spec_buf, "%2d%%", total);
11383 return decode_mode_spec_buf;
11384 }
11385 }
11386
1af9f229
RS
11387 case 's':
11388 /* status of process */
11389 obj = Fget_buffer_process (w->buffer);
11390 if (NILP (obj))
11391 return "no process";
11392#ifdef subprocesses
11393 obj = Fsymbol_name (Fprocess_status (obj));
11394#endif
11395 break;
d39b6696 11396
1af9f229
RS
11397 case 't': /* indicate TEXT or BINARY */
11398#ifdef MODE_LINE_BINARY_TEXT
11399 return MODE_LINE_BINARY_TEXT (b);
11400#else
11401 return "T";
11402#endif
1c9241f5
KH
11403
11404 case 'z':
11405 /* coding-system (not including end-of-line format) */
11406 case 'Z':
11407 /* coding-system (including end-of-line type) */
11408 {
11409 int eol_flag = (c == 'Z');
539b4d41 11410 char *p = decode_mode_spec_buf;
1c9241f5 11411
d30e754b 11412 if (! FRAME_WINDOW_P (f))
1c9241f5 11413 {
11c52c4f
RS
11414 /* No need to mention EOL here--the terminal never needs
11415 to do EOL conversion. */
11416 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
11417 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 11418 }
f13c925f 11419 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 11420 p, eol_flag);
f13c925f 11421
11c52c4f 11422#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
11423#ifdef subprocesses
11424 obj = Fget_buffer_process (Fcurrent_buffer ());
11425 if (PROCESSP (obj))
11426 {
11427 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
11428 p, eol_flag);
11429 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
11430 p, eol_flag);
11431 }
11432#endif /* subprocesses */
11c52c4f 11433#endif /* 0 */
1c9241f5
KH
11434 *p = 0;
11435 return decode_mode_spec_buf;
11436 }
a2889657 11437 }
d39b6696 11438
e24c997d 11439 if (STRINGP (obj))
a2889657
JB
11440 return (char *) XSTRING (obj)->data;
11441 else
11442 return "";
11443}
5f5c8ee5
GM
11444
11445
12adba34
RS
11446/* Count up to COUNT lines starting from START / START_BYTE.
11447 But don't go beyond LIMIT_BYTE.
11448 Return the number of lines thus found (always nonnegative).
59b49f63 11449
12adba34 11450 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
11451
11452static int
12adba34
RS
11453display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
11454 int start, start_byte, limit_byte, count;
11455 int *byte_pos_ptr;
59b49f63 11456{
59b49f63
RS
11457 register unsigned char *cursor;
11458 unsigned char *base;
11459
11460 register int ceiling;
11461 register unsigned char *ceiling_addr;
12adba34 11462 int orig_count = count;
59b49f63
RS
11463
11464 /* If we are not in selective display mode,
11465 check only for newlines. */
12adba34
RS
11466 int selective_display = (!NILP (current_buffer->selective_display)
11467 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
11468
11469 if (count > 0)
12adba34
RS
11470 {
11471 while (start_byte < limit_byte)
11472 {
11473 ceiling = BUFFER_CEILING_OF (start_byte);
11474 ceiling = min (limit_byte - 1, ceiling);
11475 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
11476 base = (cursor = BYTE_POS_ADDR (start_byte));
11477 while (1)
11478 {
11479 if (selective_display)
11480 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
11481 ;
11482 else
11483 while (*cursor != '\n' && ++cursor != ceiling_addr)
11484 ;
11485
11486 if (cursor != ceiling_addr)
11487 {
11488 if (--count == 0)
11489 {
11490 start_byte += cursor - base + 1;
11491 *byte_pos_ptr = start_byte;
11492 return orig_count;
11493 }
11494 else
11495 if (++cursor == ceiling_addr)
11496 break;
11497 }
11498 else
11499 break;
11500 }
11501 start_byte += cursor - base;
11502 }
11503 }
59b49f63
RS
11504 else
11505 {
12adba34
RS
11506 while (start_byte > limit_byte)
11507 {
11508 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
11509 ceiling = max (limit_byte, ceiling);
11510 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
11511 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
11512 while (1)
11513 {
12adba34
RS
11514 if (selective_display)
11515 while (--cursor != ceiling_addr
11516 && *cursor != '\n' && *cursor != 015)
11517 ;
11518 else
11519 while (--cursor != ceiling_addr && *cursor != '\n')
11520 ;
11521
59b49f63
RS
11522 if (cursor != ceiling_addr)
11523 {
11524 if (++count == 0)
11525 {
12adba34
RS
11526 start_byte += cursor - base + 1;
11527 *byte_pos_ptr = start_byte;
11528 /* When scanning backwards, we should
11529 not count the newline posterior to which we stop. */
11530 return - orig_count - 1;
59b49f63
RS
11531 }
11532 }
11533 else
11534 break;
11535 }
12adba34
RS
11536 /* Here we add 1 to compensate for the last decrement
11537 of CURSOR, which took it past the valid range. */
11538 start_byte += cursor - base + 1;
59b49f63
RS
11539 }
11540 }
11541
12adba34 11542 *byte_pos_ptr = limit_byte;
aa6d10fa 11543
12adba34
RS
11544 if (count < 0)
11545 return - orig_count + count;
11546 return orig_count - count;
aa6d10fa 11547
12adba34 11548}
a2889657 11549
a2889657 11550
5f5c8ee5
GM
11551\f
11552/***********************************************************************
11553 Displaying strings
11554 ***********************************************************************/
278feba9 11555
5f5c8ee5 11556/* Display a NUL-terminated string, starting with index START.
a3788d53 11557
5f5c8ee5
GM
11558 If STRING is non-null, display that C string. Otherwise, the Lisp
11559 string LISP_STRING is displayed.
a2889657 11560
5f5c8ee5
GM
11561 If FACE_STRING is not nil, FACE_STRING_POS is a position in
11562 FACE_STRING. Display STRING or LISP_STRING with the face at
11563 FACE_STRING_POS in FACE_STRING:
a2889657 11564
5f5c8ee5
GM
11565 Display the string in the environment given by IT, but use the
11566 standard display table, temporarily.
a3788d53 11567
5f5c8ee5
GM
11568 FIELD_WIDTH is the minimum number of output glyphs to produce.
11569 If STRING has fewer characters than FIELD_WIDTH, pad to the right
11570 with spaces. If STRING has more characters, more than FIELD_WIDTH
11571 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
11572
11573 PRECISION is the maximum number of characters to output from
11574 STRING. PRECISION < 0 means don't truncate the string.
a2889657 11575
5f5c8ee5 11576 This is roughly equivalent to printf format specifiers:
a2889657 11577
5f5c8ee5
GM
11578 FIELD_WIDTH PRECISION PRINTF
11579 ----------------------------------------
11580 -1 -1 %s
11581 -1 10 %.10s
11582 10 -1 %10s
11583 20 10 %20.10s
a2889657 11584
5f5c8ee5
GM
11585 MULTIBYTE zero means do not display multibyte chars, > 0 means do
11586 display them, and < 0 means obey the current buffer's value of
11587 enable_multibyte_characters.
278feba9 11588
5f5c8ee5 11589 Value is the number of glyphs produced. */
b1d1124b 11590
5f5c8ee5
GM
11591static int
11592display_string (string, lisp_string, face_string, face_string_pos,
11593 start, it, field_width, precision, max_x, multibyte)
11594 unsigned char *string;
11595 Lisp_Object lisp_string;
11596 int start;
11597 struct it *it;
11598 int field_width, precision, max_x;
11599 int multibyte;
11600{
11601 int hpos_at_start = it->hpos;
11602 int saved_face_id = it->face_id;
11603 struct glyph_row *row = it->glyph_row;
11604
11605 /* Initialize the iterator IT for iteration over STRING beginning
11606 with index START. We assume that IT may be modified here (which
11607 means that display_line has to do something when displaying a
11608 mini-buffer prompt, which it does). */
11609 reseat_to_string (it, string, lisp_string, start,
11610 precision, field_width, multibyte);
11611
11612 /* If displaying STRING, set up the face of the iterator
11613 from LISP_STRING, if that's given. */
11614 if (STRINGP (face_string))
11615 {
11616 int endptr;
11617 struct face *face;
11618
11619 it->face_id
11620 = face_at_string_position (it->w, face_string, face_string_pos,
11621 0, it->region_beg_charpos,
11622 it->region_end_charpos,
11623 &endptr, it->base_face_id);
11624 face = FACE_FROM_ID (it->f, it->face_id);
11625 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 11626 }
a2889657 11627
5f5c8ee5
GM
11628 /* Set max_x to the maximum allowed X position. Don't let it go
11629 beyond the right edge of the window. */
11630 if (max_x <= 0)
11631 max_x = it->last_visible_x;
11632 else
11633 max_x = min (max_x, it->last_visible_x);
efc63ef0 11634
5f5c8ee5
GM
11635 /* Skip over display elements that are not visible. because IT->w is
11636 hscrolled. */
11637 if (it->current_x < it->first_visible_x)
11638 move_it_in_display_line_to (it, 100000, it->first_visible_x,
11639 MOVE_TO_POS | MOVE_TO_X);
a2889657 11640
5f5c8ee5
GM
11641 row->ascent = it->max_ascent;
11642 row->height = it->max_ascent + it->max_descent;
1c9241f5 11643
5f5c8ee5
GM
11644 /* This condition is for the case that we are called with current_x
11645 past last_visible_x. */
11646 while (it->current_x < max_x)
a2889657 11647 {
5f5c8ee5 11648 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 11649
5f5c8ee5
GM
11650 /* Get the next display element. */
11651 if (!get_next_display_element (it))
90adcf20 11652 break;
1c9241f5 11653
5f5c8ee5
GM
11654 /* Produce glyphs. */
11655 x_before = it->current_x;
11656 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
11657 PRODUCE_GLYPHS (it);
90adcf20 11658
5f5c8ee5
GM
11659 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
11660 i = 0;
11661 x = x_before;
11662 while (i < nglyphs)
a2889657 11663 {
5f5c8ee5
GM
11664 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11665
11666 if (!it->truncate_lines_p
11667 && x + glyph->pixel_width > max_x)
11668 {
11669 /* End of continued line or max_x reached. */
11670 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
11671 it->current_x = x;
11672 break;
11673 }
11674 else if (x + glyph->pixel_width > it->first_visible_x)
11675 {
11676 /* Glyph is at least partially visible. */
11677 ++it->hpos;
11678 if (x < it->first_visible_x)
11679 it->glyph_row->x = x - it->first_visible_x;
11680 }
11681 else
a2889657 11682 {
5f5c8ee5
GM
11683 /* Glyph is off the left margin of the display area.
11684 Should not happen. */
11685 abort ();
a2889657 11686 }
5f5c8ee5
GM
11687
11688 row->ascent = max (row->ascent, it->max_ascent);
11689 row->height = max (row->height, it->max_ascent + it->max_descent);
11690 x += glyph->pixel_width;
11691 ++i;
a2889657 11692 }
5f5c8ee5
GM
11693
11694 /* Stop if max_x reached. */
11695 if (i < nglyphs)
11696 break;
11697
11698 /* Stop at line ends. */
11699 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 11700 {
5f5c8ee5
GM
11701 it->continuation_lines_width = 0;
11702 break;
a2889657 11703 }
1c9241f5 11704
5f5c8ee5 11705 set_iterator_to_next (it);
a688bb24 11706
5f5c8ee5
GM
11707 /* Stop if truncating at the right edge. */
11708 if (it->truncate_lines_p
11709 && it->current_x >= it->last_visible_x)
11710 {
11711 /* Add truncation mark, but don't do it if the line is
11712 truncated at a padding space. */
11713 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 11714 {
5f5c8ee5
GM
11715 if (!FRAME_WINDOW_P (it->f))
11716 produce_special_glyphs (it, IT_TRUNCATION);
11717 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 11718 }
5f5c8ee5 11719 break;
1c9241f5 11720 }
a2889657
JB
11721 }
11722
5f5c8ee5
GM
11723 /* Maybe insert a truncation at the left. */
11724 if (it->first_visible_x
11725 && IT_CHARPOS (*it) > 0)
a2889657 11726 {
5f5c8ee5
GM
11727 if (!FRAME_WINDOW_P (it->f))
11728 insert_left_trunc_glyphs (it);
11729 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
11730 }
11731
5f5c8ee5
GM
11732 it->face_id = saved_face_id;
11733
11734 /* Value is number of columns displayed. */
11735 return it->hpos - hpos_at_start;
11736}
a2889657 11737
a2889657 11738
a2889657 11739\f
5f5c8ee5
GM
11740/* This is like a combination of memq and assq. Return 1 if PROPVAL
11741 appears as an element of LIST or as the car of an element of LIST.
11742 If PROPVAL is a list, compare each element against LIST in that
11743 way, and return 1 if any element of PROPVAL is found in LIST.
11744 Otherwise return 0. This function cannot quit. */
642eefc6
RS
11745
11746int
11747invisible_p (propval, list)
11748 register Lisp_Object propval;
11749 Lisp_Object list;
11750{
af460d46
RS
11751 register Lisp_Object tail, proptail;
11752 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
11753 {
11754 register Lisp_Object tem;
af460d46 11755 tem = XCONS (tail)->car;
642eefc6
RS
11756 if (EQ (propval, tem))
11757 return 1;
11758 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
11759 return 1;
11760 }
af460d46
RS
11761 if (CONSP (propval))
11762 for (proptail = propval; CONSP (proptail);
11763 proptail = XCONS (proptail)->cdr)
11764 {
11765 Lisp_Object propelt;
11766 propelt = XCONS (proptail)->car;
11767 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11768 {
11769 register Lisp_Object tem;
11770 tem = XCONS (tail)->car;
11771 if (EQ (propelt, tem))
11772 return 1;
11773 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
11774 return 1;
11775 }
11776 }
642eefc6
RS
11777 return 0;
11778}
11779
5f5c8ee5
GM
11780
11781/* Return 1 if PROPVAL appears as the car of an element of LIST and
11782 the cdr of that element is non-nil. If PROPVAL is a list, check
11783 each element of PROPVAL in that way, and the first time some
11784 element is found, return 1 if the cdr of that element is non-nil.
11785 Otherwise return 0. This function cannot quit. */
642eefc6
RS
11786
11787int
11788invisible_ellipsis_p (propval, list)
11789 register Lisp_Object propval;
11790 Lisp_Object list;
11791{
af460d46
RS
11792 register Lisp_Object tail, proptail;
11793 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
11794 {
11795 register Lisp_Object tem;
af460d46 11796 tem = XCONS (tail)->car;
642eefc6
RS
11797 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
11798 return ! NILP (XCONS (tem)->cdr);
11799 }
af460d46
RS
11800 if (CONSP (propval))
11801 for (proptail = propval; CONSP (proptail);
11802 proptail = XCONS (proptail)->cdr)
11803 {
11804 Lisp_Object propelt;
11805 propelt = XCONS (proptail)->car;
11806 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11807 {
11808 register Lisp_Object tem;
11809 tem = XCONS (tail)->car;
11810 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
11811 return ! NILP (XCONS (tem)->cdr);
11812 }
11813 }
642eefc6
RS
11814 return 0;
11815}
5f5c8ee5
GM
11816
11817
642eefc6 11818\f
5f5c8ee5
GM
11819/***********************************************************************
11820 Initialization
11821 ***********************************************************************/
11822
a2889657
JB
11823void
11824syms_of_xdisp ()
11825{
5f5c8ee5
GM
11826 echo_area_message = previous_echo_area_message = Qnil;
11827 staticpro (&echo_area_message);
11828 staticpro (&previous_echo_area_message);
11829
735c094c
KH
11830 staticpro (&Qinhibit_redisplay);
11831 Qinhibit_redisplay = intern ("inhibit-redisplay");
11832
5f5c8ee5
GM
11833#if GLYPH_DEBUG
11834 defsubr (&Sdump_glyph_matrix);
11835 defsubr (&Sdump_glyph_row);
11836 defsubr (&Sdump_toolbar_row);
11837 defsubr (&Strace_redisplay_toggle);
11838#endif
11839
cf074754
RS
11840 staticpro (&Qmenu_bar_update_hook);
11841 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
11842
d46fb96a 11843 staticpro (&Qoverriding_terminal_local_map);
7079aefa 11844 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 11845
399164b4
KH
11846 staticpro (&Qoverriding_local_map);
11847 Qoverriding_local_map = intern ("overriding-local-map");
11848
75c43375
RS
11849 staticpro (&Qwindow_scroll_functions);
11850 Qwindow_scroll_functions = intern ("window-scroll-functions");
11851
e0bfbde6
RS
11852 staticpro (&Qredisplay_end_trigger_functions);
11853 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 11854
2e54982e
RS
11855 staticpro (&Qinhibit_point_motion_hooks);
11856 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
11857
5f5c8ee5
GM
11858 staticpro (&Qdisplay);
11859 Qdisplay = intern ("display");
11860 staticpro (&Qleft_margin);
11861 Qspace_width = intern ("space-width");
11862 staticpro (&Qspace_width);
11863 Qheight = intern ("height");
11864 staticpro (&Qheight);
11865 Qraise = intern ("raise");
11866 staticpro (&Qraise);
11867 Qspace = intern ("space");
11868 staticpro (&Qspace);
11869 Qleft_margin = intern ("left-margin");
11870 staticpro (&Qright_margin);
11871 Qright_margin = intern ("right-margin");
11872 Qalign_to = intern ("align-to");
11873 staticpro (&Qalign_to);
11874 QCalign_to = intern (":align-to");
11875 staticpro (&QCalign_to);
11876 Qwidth = intern ("width");
11877 staticpro (&Qwidth);
11878 Qrelative_width = intern ("relative-width");
11879 staticpro (&Qrelative_width);
11880 QCrelative_width = intern (":relative-width");
11881 staticpro (&QCrelative_width);
11882 QCrelative_height = intern (":relative-height");
11883 staticpro (&QCrelative_height);
11884 QCeval = intern (":eval");
11885 staticpro (&QCeval);
11886 QCwhen = intern (":when");
11887 staticpro (&QCwhen);
11888 Qfontified = intern ("fontified");
11889 staticpro (&Qfontified);
11890 Qfontification_functions = intern ("fontification-functions");
11891 staticpro (&Qfontification_functions);
11892 Qshow_trailing_whitespace = intern ("show-trailing-whitespace");
11893 staticpro (&Qshow_trailing_whitespace);
11894 Qtrailing_whitespace = intern ("trailing-whitespace");
11895 staticpro (&Qtrailing_whitespace);
11896 Qimage = intern ("image");
11897 staticpro (&Qimage);
11898
a2889657
JB
11899 staticpro (&last_arrow_position);
11900 staticpro (&last_arrow_string);
11901 last_arrow_position = Qnil;
11902 last_arrow_string = Qnil;
11903
735c094c
KH
11904 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
11905 "Non-nil means don't actually do any redisplay.\n\
11906This is used for internal purposes.");
11907 Vinhibit_redisplay = Qnil;
11908
a2889657 11909 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 11910 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
11911 Vglobal_mode_string = Qnil;
11912
11913 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
11914 "Marker for where to display an arrow on top of the buffer text.\n\
11915This must be the beginning of a line in order to work.\n\
11916See also `overlay-arrow-string'.");
11917 Voverlay_arrow_position = Qnil;
11918
11919 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
11920 "String to display as an arrow. See also `overlay-arrow-position'.");
11921 Voverlay_arrow_string = Qnil;
11922
11923 DEFVAR_INT ("scroll-step", &scroll_step,
11924 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
11925If that fails to bring point back on frame, point is centered instead.\n\
11926If this is zero, point is always centered after it moves off frame.");
a2889657 11927
0789adb2
RS
11928 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
11929 "*Scroll up to this many lines, to bring point back on screen.");
11930 scroll_conservatively = 0;
11931
9afd2168
RS
11932 DEFVAR_INT ("scroll-margin", &scroll_margin,
11933 "*Number of lines of margin at the top and bottom of a window.\n\
11934Recenter the window whenever point gets within this many lines\n\
11935of the top or bottom of the window.");
11936 scroll_margin = 0;
11937
5f5c8ee5 11938#if GLYPH_DEBUG
a2889657 11939 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 11940#endif
a2889657
JB
11941
11942 DEFVAR_BOOL ("truncate-partial-width-windows",
11943 &truncate_partial_width_windows,
44fa5b1e 11944 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
11945 truncate_partial_width_windows = 1;
11946
11947 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
11948 "*Non-nil means use inverse video for the mode line.");
11949 mode_line_inverse_video = 1;
aa6d10fa
RS
11950
11951 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
5f5c8ee5 11952 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 11953If the buffer is bigger than this, the line number does not appear\n\
9f027393 11954in the mode line.");
aa6d10fa 11955 line_number_display_limit = 1000000;
fba9ce76
RS
11956
11957 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
11958 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 11959 highlight_nonselected_windows = 0;
d39b6696
KH
11960
11961 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
11962 "Non-nil if more than one frame is visible on this display.\n\
11963Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
11964This variable is not guaranteed to be accurate except while processing\n\
11965`frame-title-format' and `icon-title-format'.");
d39b6696
KH
11966
11967 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 11968 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
11969\(Assuming the window manager supports this feature.)\n\
11970This variable has the same structure as `mode-line-format' (which see),\n\
11971and is used only on frames for which no explicit name has been set\n\
11972\(see `modify-frame-parameters').");
11973 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 11974 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
11975\(Assuming the window manager supports this feature.)\n\
11976This variable has the same structure as `mode-line-format' (which see),\n\
11977and is used only on frames for which no explicit name has been set\n\
11978\(see `modify-frame-parameters').");
11979 Vicon_title_format
11980 = Vframe_title_format
11981 = Fcons (intern ("multiple-frames"),
11982 Fcons (build_string ("%b"),
11983 Fcons (Fcons (build_string (""),
11984 Fcons (intern ("invocation-name"),
11985 Fcons (build_string ("@"),
11986 Fcons (intern ("system-name"),
11987 Qnil)))),
11988 Qnil)));
5992c4f7
KH
11989
11990 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
11991 "Maximum number of lines to keep in the message log buffer.\n\
11992If nil, disable message logging. If t, log messages but don't truncate\n\
11993the buffer when it becomes large.");
11994 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
11995
11996 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
11997 "Functions called before redisplay, if window sizes have changed.\n\
11998The value should be a list of functions that take one argument.\n\
11999Just before redisplay, for each frame, if any of its windows have changed\n\
12000size since the last redisplay, or have been split or deleted,\n\
12001all the functions in the list are called, with the frame as argument.");
12002 Vwindow_size_change_functions = Qnil;
75c43375
RS
12003
12004 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 12005 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 12006Each function is called with two arguments, the window\n\
8d9583b0
RS
12007and its new display-start position. Note that the value of `window-end'\n\
12008is not valid when these functions are called.");
75c43375 12009 Vwindow_scroll_functions = Qnil;
5f5c8ee5
GM
12010
12011 DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
12012 "*Non-nil means automatically resize toolbars.\n\
12013This increases a toolbar's height if not all toolbar items are visible.\n\
12014It decreases a toolbar's height when it would display blank lines\n\
12015otherwise.");
12016 auto_resize_toolbars_p = 1;
12017
12018 DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
12019 "*Non-nil means raise toolbar buttons when the mouse moves over them.");
12020 auto_raise_toolbar_buttons_p = 1;
12021
12022 DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
12023 "*Margin around toolbar buttons in pixels.");
12024 toolbar_button_margin = 1;
12025
12026 DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
12027 "Relief thickness of toolbar buttons.");
12028 toolbar_button_relief = 3;
12029
12030 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12031 "List of functions to call to fontify regions of text.\n\
12032Each function is called with one argument POS. Functions must\n\
12033fontify a region starting at POS in the current buffer, and give\n\
12034fontified regions the property `fontified'.\n\
12035This variable automatically becomes buffer-local when set.");
12036 Vfontification_functions = Qnil;
12037 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
12038
12039 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
12040 &unibyte_display_via_language_environment,
12041 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
12042Specifically this means that unibyte non-ASCII characters\n\
12043are displayed by converting them to the equivalent multibyte characters\n\
12044according to the current language environment. As a result, they are\n\
12045displayed according to the current fontset.");
12046 unibyte_display_via_language_environment = 0;
a2889657
JB
12047}
12048
5f5c8ee5
GM
12049
12050/* Initialize this module when Emacs starts. */
12051
dfcf069d 12052void
a2889657
JB
12053init_xdisp ()
12054{
12055 Lisp_Object root_window;
5f5c8ee5 12056 struct window *mini_w;
a2889657 12057
5f5c8ee5 12058 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
12059
12060 mini_w = XWINDOW (minibuf_window);
11e82b76 12061 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657
JB
12062
12063 echo_area_glyphs = 0;
12064 previous_echo_glyphs = 0;
5f5c8ee5 12065 echo_area_message = previous_echo_area_message = Qnil;
a2889657
JB
12066
12067 if (!noninteractive)
12068 {
5f5c8ee5
GM
12069 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12070 int i;
12071
12072 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 12073 set_window_height (root_window,
5f5c8ee5 12074 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 12075 0);
c2213350 12076 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
12077 set_window_height (minibuf_window, 1, 0);
12078
c2213350
KH
12079 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12080 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
12081
12082 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12083 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12084 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12085
12086 /* The default ellipsis glyphs `...'. */
12087 for (i = 0; i < 3; ++i)
12088 XSETFASTINT (default_invis_vector[i], '.');
a2889657 12089 }
5f5c8ee5
GM
12090
12091#ifdef HAVE_WINDOW_SYSTEM
12092 {
12093 /* Allocate the buffer for frame titles. */
12094 int size = 100;
12095 frame_title_buf = (char *) xmalloc (size);
12096 frame_title_buf_end = frame_title_buf + size;
12097 frame_title_ptr = NULL;
12098 }
12099#endif /* HAVE_WINDOW_SYSTEM */
a2889657 12100}
5f5c8ee5
GM
12101
12102