* window.c (CURBEG, CURSIZE): Don't overload lisp object lvalues
[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>
a2889657 172#include "lisp.h"
44fa5b1e 173#include "frame.h"
a2889657
JB
174#include "window.h"
175#include "termchar.h"
176#include "dispextern.h"
177#include "buffer.h"
1c9241f5 178#include "charset.h"
a2889657
JB
179#include "indent.h"
180#include "commands.h"
181#include "macros.h"
182#include "disptab.h"
30c566e4 183#include "termhooks.h"
b0a0fbda 184#include "intervals.h"
fe8b0cf8 185#include "keyboard.h"
1c9241f5
KH
186#include "coding.h"
187#include "process.h"
dfcf069d
AS
188#include "region-cache.h"
189
6d55d620 190#ifdef HAVE_X_WINDOWS
dfcf069d
AS
191#include "xterm.h"
192#endif
a75dfea0
AI
193#ifdef WINDOWSNT
194#include "w32term.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;
9499d71b 222Lisp_Object QCeval, Qwhen, QCfile, QCdata;
5f5c8ee5
GM
223Lisp_Object Qfontified;
224
225/* Functions called to fontify regions of text. */
226
227Lisp_Object Vfontification_functions;
228Lisp_Object Qfontification_functions;
229
e037b9ec 230/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
231 over them. */
232
e037b9ec 233int auto_raise_tool_bar_buttons_p;
5f5c8ee5 234
e037b9ec 235/* Margin around tool bar buttons in pixels. */
5f5c8ee5 236
e037b9ec 237int tool_bar_button_margin;
5f5c8ee5 238
e037b9ec 239/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 240
e037b9ec 241int tool_bar_button_relief;
5f5c8ee5 242
e037b9ec 243/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
244 items are visible, and no blank lines remain. */
245
e037b9ec 246int auto_resize_tool_bars_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
a7e27ef7
DL
254Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
255extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
5f5c8ee5
GM
256
257/* Symbols used in text property values. */
258
259Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 260Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 261Lisp_Object Qmargin;
a7e27ef7 262extern Lisp_Object Qheight;
5f5c8ee5 263
8f897821 264/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 265
8f897821 266Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
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 windows showing the buffer of the selected window (or
387 another buffer with the same base buffer). keyboard.c refers to
388 this. */
a2889657 389
a2889657
JB
390int buffer_shared;
391
5f5c8ee5 392/* Vector containing glyphs for an ellipsis `...'. */
a2889657 393
5f5c8ee5 394static Lisp_Object default_invis_vector[3];
a2889657 395
5f5c8ee5 396/* Nonzero means display mode line highlighted. */
a2889657 397
a2889657
JB
398int mode_line_inverse_video;
399
5f5c8ee5
GM
400/* Prompt to display in front of the mini-buffer contents. */
401
8c5b6a0a 402Lisp_Object minibuf_prompt;
a2889657 403
5f5c8ee5
GM
404/* Width of current mini-buffer prompt. Only set after display_line
405 of the line that contains the prompt. */
406
a2889657 407int minibuf_prompt_width;
5f5c8ee5
GM
408int minibuf_prompt_pixel_width;
409
5f5c8ee5
GM
410/* This is the window where the echo area message was displayed. It
411 is always a mini-buffer window, but it may not be the same window
412 currently active as a mini-buffer. */
413
73af359d
RS
414Lisp_Object echo_area_window;
415
c6e89d6c
GM
416/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
417 pushes the current message and the value of
418 message_enable_multibyte on the stack, the function restore_message
419 pops the stack and displays MESSAGE again. */
420
421Lisp_Object Vmessage_stack;
422
a3788d53
RS
423/* Nonzero means multibyte characters were enabled when the echo area
424 message was specified. */
5f5c8ee5 425
a3788d53
RS
426int message_enable_multibyte;
427
5f5c8ee5
GM
428/* True if we should redraw the mode lines on the next redisplay. */
429
a2889657
JB
430int update_mode_lines;
431
5f5c8ee5
GM
432/* Nonzero if window sizes or contents have changed since last
433 redisplay that finished */
434
a2889657
JB
435int windows_or_buffers_changed;
436
5f5c8ee5
GM
437/* Nonzero after display_mode_line if %l was used and it displayed a
438 line number. */
439
aa6d10fa
RS
440int line_number_displayed;
441
442/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 443
14510fee 444static int line_number_display_limit;
5992c4f7 445
5d121aec
KH
446/* line width to consider when repostioning for line number display */
447
448static int line_number_display_limit_width;
449
5f5c8ee5
GM
450/* Number of lines to keep in the message log buffer. t means
451 infinite. nil means don't log at all. */
452
5992c4f7 453Lisp_Object Vmessage_log_max;
d45de95b 454
c6e89d6c
GM
455/* Current, index 0, and last displayed echo area message. Either
456 buffers from echo_buffers, or nil to indicate no message. */
457
458Lisp_Object echo_area_buffer[2];
459
460/* The buffers referenced from echo_area_buffer. */
461
462static Lisp_Object echo_buffer[2];
463
464/* A vector saved used in with_area_buffer to reduce consing. */
465
466static Lisp_Object Vwith_echo_area_save_vector;
467
468/* Non-zero means display_echo_area should display the last echo area
469 message again. Set by redisplay_preserve_echo_area. */
470
471static int display_last_displayed_message_p;
472
473/* Nonzero if echo area is being used by print; zero if being used by
474 message. */
475
476int message_buf_print;
477
9142dd5b
GM
478/* Maximum height for resizing mini-windows. Either a float
479 specifying a fraction of the available height, or an integer
480 specifying a number of lines. */
c6e89d6c
GM
481
482static Lisp_Object Vmax_mini_window_height;
483
d6d26ed3
GM
484/* Non-zero means we want a hollow cursor in windows that are not
485 selected. Zero means there's no cursor in such windows. */
486
487int cursor_in_non_selected_windows;
488
5f5c8ee5
GM
489/* A scratch glyph row with contents used for generating truncation
490 glyphs. Also used in direct_output_for_insert. */
12adba34 491
5f5c8ee5
GM
492#define MAX_SCRATCH_GLYPHS 100
493struct glyph_row scratch_glyph_row;
494static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 495
5f5c8ee5
GM
496/* Ascent and height of the last line processed by move_it_to. */
497
498static int last_max_ascent, last_height;
499
500/* The maximum distance to look ahead for text properties. Values
501 that are too small let us call compute_char_face and similar
502 functions too often which is expensive. Values that are too large
503 let us call compute_char_face and alike too often because we
504 might not be interested in text properties that far away. */
505
506#define TEXT_PROP_DISTANCE_LIMIT 100
507
508/* Non-zero means print traces of redisplay if compiled with
509 GLYPH_DEBUG != 0. */
510
511#if GLYPH_DEBUG
512int trace_redisplay_p;
513#endif
514
515/* Value returned from text property handlers (see below). */
516
517enum prop_handled
3c6595e0 518{
5f5c8ee5
GM
519 HANDLED_NORMALLY,
520 HANDLED_RECOMPUTE_PROPS,
521 HANDLED_OVERLAY_STRING_CONSUMED,
522 HANDLED_RETURN
523};
3c6595e0 524
5f5c8ee5
GM
525/* A description of text properties that redisplay is interested
526 in. */
3c6595e0 527
5f5c8ee5
GM
528struct props
529{
530 /* The name of the property. */
531 Lisp_Object *name;
90adcf20 532
5f5c8ee5
GM
533 /* A unique index for the property. */
534 enum prop_idx idx;
535
536 /* A handler function called to set up iterator IT from the property
537 at IT's current position. Value is used to steer handle_stop. */
538 enum prop_handled (*handler) P_ ((struct it *it));
539};
540
541static enum prop_handled handle_face_prop P_ ((struct it *));
542static enum prop_handled handle_invisible_prop P_ ((struct it *));
543static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 544static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
545static enum prop_handled handle_overlay_change P_ ((struct it *));
546static enum prop_handled handle_fontified_prop P_ ((struct it *));
547
548/* Properties handled by iterators. */
549
550static struct props it_props[] =
5992c4f7 551{
5f5c8ee5
GM
552 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
553 /* Handle `face' before `display' because some sub-properties of
554 `display' need to know the face. */
555 {&Qface, FACE_PROP_IDX, handle_face_prop},
556 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
557 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 558 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
559 {NULL, 0, NULL}
560};
5992c4f7 561
5f5c8ee5
GM
562/* Value is the position described by X. If X is a marker, value is
563 the marker_position of X. Otherwise, value is X. */
12adba34 564
5f5c8ee5 565#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 566
5f5c8ee5 567/* Enumeration returned by some move_it_.* functions internally. */
12adba34 568
5f5c8ee5
GM
569enum move_it_result
570{
571 /* Not used. Undefined value. */
572 MOVE_UNDEFINED,
bab29e15 573
5f5c8ee5
GM
574 /* Move ended at the requested buffer position or ZV. */
575 MOVE_POS_MATCH_OR_ZV,
bab29e15 576
5f5c8ee5
GM
577 /* Move ended at the requested X pixel position. */
578 MOVE_X_REACHED,
12adba34 579
5f5c8ee5
GM
580 /* Move within a line ended at the end of a line that must be
581 continued. */
582 MOVE_LINE_CONTINUED,
583
584 /* Move within a line ended at the end of a line that would
585 be displayed truncated. */
586 MOVE_LINE_TRUNCATED,
ff6c30e5 587
5f5c8ee5
GM
588 /* Move within a line ended at a line end. */
589 MOVE_NEWLINE_OR_CR
590};
12adba34 591
ff6c30e5 592
5f5c8ee5
GM
593\f
594/* Function prototypes. */
595
5bcfeb49 596static void ensure_echo_area_buffers P_ ((void));
e037b9ec
GM
597static struct glyph_row *row_containing_pos P_ ((struct window *, int,
598 struct glyph_row *,
599 struct glyph_row *));
c6e89d6c
GM
600static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
601static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
c6e89d6c
GM
602static void clear_garbaged_frames P_ ((void));
603static int current_message_1 P_ ((Lisp_Object *));
604static int truncate_message_1 P_ ((int));
605static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
606static int display_echo_area P_ ((struct window *));
607static int display_echo_area_1 P_ ((struct window *));
28514cd9 608static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 609static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
610static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
611 struct text_pos));
612static int compute_window_start_on_continuation_line P_ ((struct window *));
613static Lisp_Object eval_handler P_ ((Lisp_Object));
614static Lisp_Object eval_form P_ ((Lisp_Object));
615static void insert_left_trunc_glyphs P_ ((struct it *));
616static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
617static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 618static int append_space P_ ((struct it *, int));
5f5c8ee5
GM
619static void make_cursor_line_fully_visible P_ ((struct window *));
620static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
621static int trailing_whitespace_p P_ ((int));
622static int message_log_check_duplicate P_ ((int, int, int, int));
623int invisible_p P_ ((Lisp_Object, Lisp_Object));
624int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
625static void push_it P_ ((struct it *));
626static void pop_it P_ ((struct it *));
627static void sync_frame_with_window_matrix_rows P_ ((struct window *));
628static void redisplay_internal P_ ((int));
c6e89d6c 629static int echo_area_display P_ ((int));
5f5c8ee5
GM
630static void redisplay_windows P_ ((Lisp_Object));
631static void redisplay_window P_ ((Lisp_Object, int));
632static void update_menu_bar P_ ((struct frame *, int));
633static int try_window_reusing_current_matrix P_ ((struct window *));
634static int try_window_id P_ ((struct window *));
635static int display_line P_ ((struct it *));
636static void display_mode_lines P_ ((struct window *));
637static void display_mode_line P_ ((struct window *, enum face_id,
638 Lisp_Object));
639static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 640static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
641static void display_menu_bar P_ ((struct window *));
642static int display_count_lines P_ ((int, int, int, int, int *));
643static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
644 int, int, struct it *, int, int, int, int));
645static void compute_line_metrics P_ ((struct it *));
646static void run_redisplay_end_trigger_hook P_ ((struct it *));
647static int get_overlay_strings P_ ((struct it *));
648static void next_overlay_string P_ ((struct it *));
649void set_iterator_to_next P_ ((struct it *));
650static void reseat P_ ((struct it *, struct text_pos, int));
651static void reseat_1 P_ ((struct it *, struct text_pos, int));
652static void back_to_previous_visible_line_start P_ ((struct it *));
653static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 654static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
655static int next_element_from_display_vector P_ ((struct it *));
656static int next_element_from_string P_ ((struct it *));
657static int next_element_from_c_string P_ ((struct it *));
658static int next_element_from_buffer P_ ((struct it *));
260a86a0 659static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
660static int next_element_from_image P_ ((struct it *));
661static int next_element_from_stretch P_ ((struct it *));
662static void load_overlay_strings P_ ((struct it *));
663static void init_from_display_pos P_ ((struct it *, struct window *,
664 struct display_pos *));
665static void reseat_to_string P_ ((struct it *, unsigned char *,
666 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
667static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
668 int, int, int));
669void move_it_vertically_backward P_ ((struct it *, int));
670static void init_to_row_start P_ ((struct it *, struct window *,
671 struct glyph_row *));
672static void init_to_row_end P_ ((struct it *, struct window *,
673 struct glyph_row *));
674static void back_to_previous_line_start P_ ((struct it *));
675static void forward_to_next_line_start P_ ((struct it *));
676static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
677 Lisp_Object, int));
678static struct text_pos string_pos P_ ((int, Lisp_Object));
679static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
680static int number_of_chars P_ ((unsigned char *, int));
681static void compute_stop_pos P_ ((struct it *));
682static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
683 Lisp_Object));
684static int face_before_or_after_it_pos P_ ((struct it *, int));
685static int next_overlay_change P_ ((int));
686static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
687 Lisp_Object, struct text_pos *));
688
689#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
690#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 691
5f5c8ee5 692#ifdef HAVE_WINDOW_SYSTEM
12adba34 693
e037b9ec
GM
694static void update_tool_bar P_ ((struct frame *, int));
695static void build_desired_tool_bar_string P_ ((struct frame *f));
696static int redisplay_tool_bar P_ ((struct frame *));
697static void display_tool_bar_line P_ ((struct it *));
12adba34 698
5f5c8ee5 699#endif /* HAVE_WINDOW_SYSTEM */
12adba34 700
5f5c8ee5
GM
701\f
702/***********************************************************************
703 Window display dimensions
704 ***********************************************************************/
12adba34 705
5f5c8ee5
GM
706/* Return the window-relative maximum y + 1 for glyph rows displaying
707 text in window W. This is the height of W minus the height of a
708 mode line, if any. */
709
710INLINE int
711window_text_bottom_y (w)
712 struct window *w;
713{
714 struct frame *f = XFRAME (w->frame);
715 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
716
717 if (WINDOW_WANTS_MODELINE_P (w))
718 height -= CURRENT_MODE_LINE_HEIGHT (w);
719 return height;
f88eb0b6
KH
720}
721
f82aff7c 722
5f5c8ee5
GM
723/* Return the pixel width of display area AREA of window W. AREA < 0
724 means return the total width of W, not including bitmap areas to
725 the left and right of the window. */
ff6c30e5 726
5f5c8ee5
GM
727INLINE int
728window_box_width (w, area)
729 struct window *w;
730 int area;
731{
732 struct frame *f = XFRAME (w->frame);
733 int width = XFASTINT (w->width);
734
735 if (!w->pseudo_window_p)
ff6c30e5 736 {
050d82d7 737 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
738
739 if (area == TEXT_AREA)
740 {
741 if (INTEGERP (w->left_margin_width))
742 width -= XFASTINT (w->left_margin_width);
743 if (INTEGERP (w->right_margin_width))
744 width -= XFASTINT (w->right_margin_width);
745 }
746 else if (area == LEFT_MARGIN_AREA)
747 width = (INTEGERP (w->left_margin_width)
748 ? XFASTINT (w->left_margin_width) : 0);
749 else if (area == RIGHT_MARGIN_AREA)
750 width = (INTEGERP (w->right_margin_width)
751 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 752 }
5f5c8ee5
GM
753
754 return width * CANON_X_UNIT (f);
ff6c30e5 755}
1adc55de 756
1adc55de 757
5f5c8ee5
GM
758/* Return the pixel height of the display area of window W, not
759 including mode lines of W, if any.. */
f88eb0b6 760
5f5c8ee5
GM
761INLINE int
762window_box_height (w)
763 struct window *w;
f88eb0b6 764{
5f5c8ee5
GM
765 struct frame *f = XFRAME (w->frame);
766 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
767
768 if (WINDOW_WANTS_MODELINE_P (w))
769 height -= CURRENT_MODE_LINE_HEIGHT (w);
770
045dee35
GM
771 if (WINDOW_WANTS_HEADER_LINE_P (w))
772 height -= CURRENT_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
773
774 return height;
5992c4f7
KH
775}
776
777
5f5c8ee5
GM
778/* Return the frame-relative coordinate of the left edge of display
779 area AREA of window W. AREA < 0 means return the left edge of the
780 whole window, to the right of any bitmap area at the left side of
781 W. */
5992c4f7 782
5f5c8ee5
GM
783INLINE int
784window_box_left (w, area)
785 struct window *w;
786 int area;
90adcf20 787{
5f5c8ee5
GM
788 struct frame *f = XFRAME (w->frame);
789 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 790
5f5c8ee5 791 if (!w->pseudo_window_p)
90adcf20 792 {
5f5c8ee5 793 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 794 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
795
796 if (area == TEXT_AREA)
797 x += window_box_width (w, LEFT_MARGIN_AREA);
798 else if (area == RIGHT_MARGIN_AREA)
799 x += (window_box_width (w, LEFT_MARGIN_AREA)
800 + window_box_width (w, TEXT_AREA));
90adcf20 801 }
73af359d 802
5f5c8ee5
GM
803 return x;
804}
90adcf20 805
b6436d4e 806
5f5c8ee5
GM
807/* Return the frame-relative coordinate of the right edge of display
808 area AREA of window W. AREA < 0 means return the left edge of the
809 whole window, to the left of any bitmap area at the right side of
810 W. */
ded34426 811
5f5c8ee5
GM
812INLINE int
813window_box_right (w, area)
814 struct window *w;
815 int area;
816{
817 return window_box_left (w, area) + window_box_width (w, area);
818}
819
820
821/* Get the bounding box of the display area AREA of window W, without
822 mode lines, in frame-relative coordinates. AREA < 0 means the
823 whole window, not including bitmap areas to the left and right of
824 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
825 coordinates of the upper-left corner of the box. Return in
826 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
827
828INLINE void
829window_box (w, area, box_x, box_y, box_width, box_height)
830 struct window *w;
831 int area;
832 int *box_x, *box_y, *box_width, *box_height;
833{
834 struct frame *f = XFRAME (w->frame);
835
836 *box_width = window_box_width (w, area);
837 *box_height = window_box_height (w);
838 *box_x = window_box_left (w, area);
839 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
840 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
841 if (WINDOW_WANTS_HEADER_LINE_P (w))
842 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 843}
1adc55de 844
1adc55de 845
5f5c8ee5
GM
846/* Get the bounding box of the display area AREA of window W, without
847 mode lines. AREA < 0 means the whole window, not including bitmap
848 areas to the left and right of the window. Return in *TOP_LEFT_X
849 and TOP_LEFT_Y the frame-relative pixel coordinates of the
850 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
851 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
852 box. */
ded34426 853
5f5c8ee5
GM
854INLINE void
855window_box_edges (w, area, top_left_x, top_left_y,
856 bottom_right_x, bottom_right_y)
857 struct window *w;
858 int area;
859 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 860{
5f5c8ee5
GM
861 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
862 bottom_right_y);
863 *bottom_right_x += *top_left_x;
864 *bottom_right_y += *top_left_y;
48ae5f0a
KH
865}
866
5f5c8ee5
GM
867
868\f
869/***********************************************************************
870 Utilities
871 ***********************************************************************/
872
4fdb80f2
GM
873/* Return the next character from STR which is MAXLEN bytes long.
874 Return in *LEN the length of the character. This is like
875 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
876 we find one, we return a `?', but with the length of the illegal
877 character. */
878
879static INLINE int
7a5b8a93 880string_char_and_length (str, maxlen, len)
4fdb80f2 881 unsigned char *str;
7a5b8a93 882 int maxlen, *len;
4fdb80f2
GM
883{
884 int c;
885
886 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
887 if (!CHAR_VALID_P (c, 1))
888 /* We may not change the length here because other places in Emacs
889 don't use this function, i.e. they silently accept illegal
890 characters. */
891 c = '?';
892
893 return c;
894}
895
896
897
5f5c8ee5
GM
898/* Given a position POS containing a valid character and byte position
899 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
900
901static struct text_pos
902string_pos_nchars_ahead (pos, string, nchars)
903 struct text_pos pos;
904 Lisp_Object string;
905 int nchars;
0b1005ef 906{
5f5c8ee5
GM
907 xassert (STRINGP (string) && nchars >= 0);
908
909 if (STRING_MULTIBYTE (string))
910 {
911 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
912 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
913 int len;
914
915 while (nchars--)
916 {
4fdb80f2 917 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
918 p += len, rest -= len;
919 xassert (rest >= 0);
920 CHARPOS (pos) += 1;
921 BYTEPOS (pos) += len;
922 }
923 }
924 else
925 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
926
927 return pos;
0a9dc68b
RS
928}
929
0a9dc68b 930
5f5c8ee5
GM
931/* Value is the text position, i.e. character and byte position,
932 for character position CHARPOS in STRING. */
933
934static INLINE struct text_pos
935string_pos (charpos, string)
936 int charpos;
0a9dc68b 937 Lisp_Object string;
0a9dc68b 938{
5f5c8ee5
GM
939 struct text_pos pos;
940 xassert (STRINGP (string));
941 xassert (charpos >= 0);
942 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
943 return pos;
944}
945
946
947/* Value is a text position, i.e. character and byte position, for
948 character position CHARPOS in C string S. MULTIBYTE_P non-zero
949 means recognize multibyte characters. */
950
951static struct text_pos
952c_string_pos (charpos, s, multibyte_p)
953 int charpos;
954 unsigned char *s;
955 int multibyte_p;
956{
957 struct text_pos pos;
958
959 xassert (s != NULL);
960 xassert (charpos >= 0);
961
962 if (multibyte_p)
0a9dc68b 963 {
5f5c8ee5
GM
964 int rest = strlen (s), len;
965
966 SET_TEXT_POS (pos, 0, 0);
967 while (charpos--)
0a9dc68b 968 {
4fdb80f2 969 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
970 s += len, rest -= len;
971 xassert (rest >= 0);
972 CHARPOS (pos) += 1;
973 BYTEPOS (pos) += len;
0a9dc68b
RS
974 }
975 }
5f5c8ee5
GM
976 else
977 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 978
5f5c8ee5
GM
979 return pos;
980}
0a9dc68b 981
0a9dc68b 982
5f5c8ee5
GM
983/* Value is the number of characters in C string S. MULTIBYTE_P
984 non-zero means recognize multibyte characters. */
0a9dc68b 985
5f5c8ee5
GM
986static int
987number_of_chars (s, multibyte_p)
988 unsigned char *s;
989 int multibyte_p;
990{
991 int nchars;
992
993 if (multibyte_p)
994 {
995 int rest = strlen (s), len;
996 unsigned char *p = (unsigned char *) s;
0a9dc68b 997
5f5c8ee5
GM
998 for (nchars = 0; rest > 0; ++nchars)
999 {
4fdb80f2 1000 string_char_and_length (p, rest, &len);
5f5c8ee5 1001 rest -= len, p += len;
0a9dc68b
RS
1002 }
1003 }
5f5c8ee5
GM
1004 else
1005 nchars = strlen (s);
1006
1007 return nchars;
0b1005ef
KH
1008}
1009
5f5c8ee5
GM
1010
1011/* Compute byte position NEWPOS->bytepos corresponding to
1012 NEWPOS->charpos. POS is a known position in string STRING.
1013 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1014
5f5c8ee5
GM
1015static void
1016compute_string_pos (newpos, pos, string)
1017 struct text_pos *newpos, pos;
1018 Lisp_Object string;
76412d64 1019{
5f5c8ee5
GM
1020 xassert (STRINGP (string));
1021 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1022
1023 if (STRING_MULTIBYTE (string))
1024 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1025 string);
1026 else
1027 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1028}
1029
9c74a0dd 1030
5f5c8ee5
GM
1031\f
1032/***********************************************************************
1033 Lisp form evaluation
1034 ***********************************************************************/
1035
1036/* Error handler for eval_form. */
1037
1038static Lisp_Object
1039eval_handler (arg)
1040 Lisp_Object arg;
1041{
1042 return Qnil;
1043}
1044
1045
1046/* Evaluate SEXPR and return the result, or nil if something went
1047 wrong. */
1048
1049static Lisp_Object
1050eval_form (sexpr)
1051 Lisp_Object sexpr;
1052{
1053 int count = specpdl_ptr - specpdl;
1054 Lisp_Object val;
1055 specbind (Qinhibit_redisplay, Qt);
1056 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1057 return unbind_to (count, val);
1058}
1059
1060
1061\f
1062/***********************************************************************
1063 Debugging
1064 ***********************************************************************/
1065
1066#if 0
1067
1068/* Define CHECK_IT to perform sanity checks on iterators.
1069 This is for debugging. It is too slow to do unconditionally. */
1070
1071static void
1072check_it (it)
1073 struct it *it;
1074{
1075 if (it->method == next_element_from_string)
a2889657 1076 {
5f5c8ee5
GM
1077 xassert (STRINGP (it->string));
1078 xassert (IT_STRING_CHARPOS (*it) >= 0);
1079 }
1080 else if (it->method == next_element_from_buffer)
1081 {
1082 /* Check that character and byte positions agree. */
1083 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1084 }
73af359d 1085
5f5c8ee5
GM
1086 if (it->dpvec)
1087 xassert (it->current.dpvec_index >= 0);
1088 else
1089 xassert (it->current.dpvec_index < 0);
1090}
1f40cad2 1091
5f5c8ee5
GM
1092#define CHECK_IT(IT) check_it ((IT))
1093
1094#else /* not 0 */
1095
1096#define CHECK_IT(IT) (void) 0
1097
1098#endif /* not 0 */
1099
1100
1101#if GLYPH_DEBUG
1102
1103/* Check that the window end of window W is what we expect it
1104 to be---the last row in the current matrix displaying text. */
1105
1106static void
1107check_window_end (w)
1108 struct window *w;
1109{
1110 if (!MINI_WINDOW_P (w)
1111 && !NILP (w->window_end_valid))
1112 {
1113 struct glyph_row *row;
1114 xassert ((row = MATRIX_ROW (w->current_matrix,
1115 XFASTINT (w->window_end_vpos)),
1116 !row->enabled_p
1117 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1118 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1119 }
1120}
1121
1122#define CHECK_WINDOW_END(W) check_window_end ((W))
1123
1124#else /* not GLYPH_DEBUG */
1125
1126#define CHECK_WINDOW_END(W) (void) 0
1127
1128#endif /* not GLYPH_DEBUG */
1129
1130
1131\f
1132/***********************************************************************
1133 Iterator initialization
1134 ***********************************************************************/
1135
1136/* Initialize IT for displaying current_buffer in window W, starting
1137 at character position CHARPOS. CHARPOS < 0 means that no buffer
1138 position is specified which is useful when the iterator is assigned
1139 a position later. BYTEPOS is the byte position corresponding to
1140 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1141
1142 If ROW is not null, calls to produce_glyphs with IT as parameter
1143 will produce glyphs in that row.
1144
1145 BASE_FACE_ID is the id of a base face to use. It must be one of
1146 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
045dee35 1147 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1148 displaying the tool-bar.
5f5c8ee5
GM
1149
1150 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1151 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
1152 corresponding mode line glyph row of the desired matrix of W. */
1153
1154void
1155init_iterator (it, w, charpos, bytepos, row, base_face_id)
1156 struct it *it;
1157 struct window *w;
1158 int charpos, bytepos;
1159 struct glyph_row *row;
1160 enum face_id base_face_id;
1161{
1162 int highlight_region_p;
5f5c8ee5
GM
1163
1164 /* Some precondition checks. */
1165 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1166 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1167
1168 /* If face attributes have been changed since the last redisplay,
1169 free realized faces now because they depend on face definitions
1170 that might have changed. */
1171 if (face_change_count)
1172 {
1173 face_change_count = 0;
1174 free_all_realized_faces (Qnil);
1175 }
1176
1177 /* Use one of the mode line rows of W's desired matrix if
1178 appropriate. */
1179 if (row == NULL)
1180 {
1181 if (base_face_id == MODE_LINE_FACE_ID)
1182 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1183 else if (base_face_id == HEADER_LINE_FACE_ID)
1184 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1185 }
1186
1187 /* Clear IT. */
1188 bzero (it, sizeof *it);
1189 it->current.overlay_string_index = -1;
1190 it->current.dpvec_index = -1;
5f5c8ee5
GM
1191 it->base_face_id = base_face_id;
1192
1193 /* The window in which we iterate over current_buffer: */
1194 XSETWINDOW (it->window, w);
1195 it->w = w;
1196 it->f = XFRAME (w->frame);
1197
1198 /* If realized faces have been removed, e.g. because of face
1199 attribute changes of named faces, recompute them. */
1200 if (FRAME_FACE_CACHE (it->f)->used == 0)
1201 recompute_basic_faces (it->f);
1202
5f5c8ee5
GM
1203 /* Current value of the `space-width', and 'height' properties. */
1204 it->space_width = Qnil;
1205 it->font_height = Qnil;
1206
1207 /* Are control characters displayed as `^C'? */
1208 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1209
1210 /* -1 means everything between a CR and the following line end
1211 is invisible. >0 means lines indented more than this value are
1212 invisible. */
1213 it->selective = (INTEGERP (current_buffer->selective_display)
1214 ? XFASTINT (current_buffer->selective_display)
1215 : (!NILP (current_buffer->selective_display)
1216 ? -1 : 0));
1217 it->selective_display_ellipsis_p
1218 = !NILP (current_buffer->selective_display_ellipses);
1219
1220 /* Display table to use. */
1221 it->dp = window_display_table (w);
1222
1223 /* Are multibyte characters enabled in current_buffer? */
1224 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1225
1226 /* Non-zero if we should highlight the region. */
1227 highlight_region_p
1228 = (!NILP (Vtransient_mark_mode)
1229 && !NILP (current_buffer->mark_active)
1230 && XMARKER (current_buffer->mark)->buffer != 0);
1231
1232 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1233 start and end of a visible region in window IT->w. Set both to
1234 -1 to indicate no region. */
1235 if (highlight_region_p
1236 /* Maybe highlight only in selected window. */
1237 && (/* Either show region everywhere. */
1238 highlight_nonselected_windows
1239 /* Or show region in the selected window. */
1240 || w == XWINDOW (selected_window)
1241 /* Or show the region if we are in the mini-buffer and W is
1242 the window the mini-buffer refers to. */
1243 || (MINI_WINDOW_P (XWINDOW (selected_window))
1244 && w == XWINDOW (Vminibuf_scroll_window))))
1245 {
1246 int charpos = marker_position (current_buffer->mark);
1247 it->region_beg_charpos = min (PT, charpos);
1248 it->region_end_charpos = max (PT, charpos);
1249 }
1250 else
1251 it->region_beg_charpos = it->region_end_charpos = -1;
1252
1253 /* Get the position at which the redisplay_end_trigger hook should
1254 be run, if it is to be run at all. */
1255 if (MARKERP (w->redisplay_end_trigger)
1256 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1257 it->redisplay_end_trigger_charpos
1258 = marker_position (w->redisplay_end_trigger);
1259 else if (INTEGERP (w->redisplay_end_trigger))
1260 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1261
1262 /* Correct bogus values of tab_width. */
1263 it->tab_width = XINT (current_buffer->tab_width);
1264 if (it->tab_width <= 0 || it->tab_width > 1000)
1265 it->tab_width = 8;
1266
1267 /* Are lines in the display truncated? */
1268 it->truncate_lines_p
1269 = (base_face_id != DEFAULT_FACE_ID
1270 || XINT (it->w->hscroll)
1271 || (truncate_partial_width_windows
1272 && !WINDOW_FULL_WIDTH_P (it->w))
1273 || !NILP (current_buffer->truncate_lines));
1274
1275 /* Get dimensions of truncation and continuation glyphs. These are
1276 displayed as bitmaps under X, so we don't need them for such
1277 frames. */
1278 if (!FRAME_WINDOW_P (it->f))
1279 {
1280 if (it->truncate_lines_p)
1281 {
1282 /* We will need the truncation glyph. */
1283 xassert (it->glyph_row == NULL);
1284 produce_special_glyphs (it, IT_TRUNCATION);
1285 it->truncation_pixel_width = it->pixel_width;
1286 }
1287 else
1288 {
1289 /* We will need the continuation glyph. */
1290 xassert (it->glyph_row == NULL);
1291 produce_special_glyphs (it, IT_CONTINUATION);
1292 it->continuation_pixel_width = it->pixel_width;
1293 }
1294
1295 /* Reset these values to zero becaue the produce_special_glyphs
1296 above has changed them. */
1297 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1298 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1299 }
1300
1301 /* Set this after getting the dimensions of truncation and
1302 continuation glyphs, so that we don't produce glyphs when calling
1303 produce_special_glyphs, above. */
1304 it->glyph_row = row;
1305 it->area = TEXT_AREA;
1306
1307 /* Get the dimensions of the display area. The display area
1308 consists of the visible window area plus a horizontally scrolled
1309 part to the left of the window. All x-values are relative to the
1310 start of this total display area. */
1311 if (base_face_id != DEFAULT_FACE_ID)
1312 {
1313 /* Mode lines, menu bar in terminal frames. */
1314 it->first_visible_x = 0;
1315 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1316 }
1317 else
1318 {
1319 it->first_visible_x
1320 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1321 it->last_visible_x = (it->first_visible_x
1322 + window_box_width (w, TEXT_AREA));
1323
1324 /* If we truncate lines, leave room for the truncator glyph(s) at
1325 the right margin. Otherwise, leave room for the continuation
1326 glyph(s). Truncation and continuation glyphs are not inserted
1327 for window-based redisplay. */
1328 if (!FRAME_WINDOW_P (it->f))
1329 {
1330 if (it->truncate_lines_p)
1331 it->last_visible_x -= it->truncation_pixel_width;
1332 else
1333 it->last_visible_x -= it->continuation_pixel_width;
1334 }
1335
045dee35
GM
1336 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1337 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1338 }
1339
1340 /* Leave room for a border glyph. */
1341 if (!FRAME_WINDOW_P (it->f)
1342 && !WINDOW_RIGHTMOST_P (it->w))
1343 it->last_visible_x -= 1;
1344
1345 it->last_visible_y = window_text_bottom_y (w);
1346
1347 /* For mode lines and alike, arrange for the first glyph having a
1348 left box line if the face specifies a box. */
1349 if (base_face_id != DEFAULT_FACE_ID)
1350 {
1351 struct face *face;
1352
1353 it->face_id = base_face_id;
1354
1355 /* If we have a boxed mode line, make the first character appear
1356 with a left box line. */
1357 face = FACE_FROM_ID (it->f, base_face_id);
1358 if (face->box != FACE_NO_BOX)
1359 it->start_of_box_run_p = 1;
1360 }
1361
1362 /* If a buffer position was specified, set the iterator there,
1363 getting overlays and face properties from that position. */
1364 if (charpos > 0)
1365 {
1366 it->end_charpos = ZV;
1367 it->face_id = -1;
1368 IT_CHARPOS (*it) = charpos;
1369
1370 /* Compute byte position if not specified. */
1371 if (bytepos <= 0)
1372 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1373 else
1374 IT_BYTEPOS (*it) = bytepos;
1375
1376 /* Compute faces etc. */
1377 reseat (it, it->current.pos, 1);
1378 }
1379
1380 CHECK_IT (it);
1381}
1382
1383
1384/* Initialize IT for the display of window W with window start POS. */
1385
1386void
1387start_display (it, w, pos)
1388 struct it *it;
1389 struct window *w;
1390 struct text_pos pos;
1391{
1392 int start_at_line_beg_p;
1393 struct glyph_row *row;
045dee35 1394 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1395 int first_y;
1396
1397 row = w->desired_matrix->rows + first_vpos;
1398 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1399 first_y = it->current_y;
1400
1401 /* If window start is not at a line start, move back to the line
1402 start. This makes sure that we take continuation lines into
1403 account. */
1404 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1405 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1406 if (!start_at_line_beg_p)
1407 reseat_at_previous_visible_line_start (it);
1408
5f5c8ee5
GM
1409 /* If window start is not at a line start, skip forward to POS to
1410 get the correct continuation_lines_width and current_x. */
1411 if (!start_at_line_beg_p)
1412 {
1413 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1414
1415 /* If lines are continued, this line may end in the middle of a
1416 multi-glyph character (e.g. a control character displayed as
1417 \003, or in the middle of an overlay string). In this case
1418 move_it_to above will not have taken us to the start of
1419 the continuation line but to the end of the continued line. */
1420 if (!it->truncate_lines_p && it->current_x > 0)
1421 {
1422 if (it->current.dpvec_index >= 0
1423 || it->current.overlay_string_index >= 0)
1424 {
1425 set_iterator_to_next (it);
1426 move_it_in_display_line_to (it, -1, -1, 0);
1427 }
1428 it->continuation_lines_width += it->current_x;
1429 }
1430
1431 it->current_y = first_y;
1432 it->vpos = 0;
1433 it->current_x = it->hpos = 0;
1434 }
1435
1436#if 0 /* Don't assert the following because start_display is sometimes
1437 called intentionally with a window start that is not at a
1438 line start. Please leave this code in as a comment. */
1439
1440 /* Window start should be on a line start, now. */
1441 xassert (it->continuation_lines_width
1442 || IT_CHARPOS (it) == BEGV
1443 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1444#endif /* 0 */
1445}
1446
1447
1448/* Initialize IT for stepping through current_buffer in window W,
1449 starting at position POS that includes overlay string and display
1450 vector/ control character translation position information. */
1451
1452static void
1453init_from_display_pos (it, w, pos)
1454 struct it *it;
1455 struct window *w;
1456 struct display_pos *pos;
1457{
1458 /* Keep in mind: the call to reseat in init_iterator skips invisible
1459 text, so we might end up at a position different from POS. This
1460 is only a problem when POS is a row start after a newline and an
1461 overlay starts there with an after-string, and the overlay has an
1462 invisible property. Since we don't skip invisible text in
1463 display_line and elsewhere immediately after consuming the
1464 newline before the row start, such a POS will not be in a string,
1465 but the call to init_iterator below will move us to the
1466 after-string. */
1467 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1468 NULL, DEFAULT_FACE_ID);
1469
1470 /* If position is within an overlay string, set up IT to
1471 the right overlay string. */
1472 if (pos->overlay_string_index >= 0)
1473 {
1474 int relative_index;
1475
1476 /* We already have the first chunk of overlay strings in
1477 IT->overlay_strings. Load more until the one for
1478 pos->overlay_string_index is in IT->overlay_strings. */
1479 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1480 {
1481 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1482 it->current.overlay_string_index = 0;
1483 while (n--)
1484 {
1485 load_overlay_strings (it);
1486 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1487 }
1488 }
1489
1490 it->current.overlay_string_index = pos->overlay_string_index;
1491 relative_index = (it->current.overlay_string_index
1492 % OVERLAY_STRING_CHUNK_SIZE);
1493 it->string = it->overlay_strings[relative_index];
1494 it->current.string_pos = pos->string_pos;
1495 it->method = next_element_from_string;
1496 }
1497 else if (CHARPOS (pos->string_pos) >= 0)
1498 {
1499 /* Recorded position is not in an overlay string, but in another
1500 string. This can only be a string from a `display' property.
1501 IT should already be filled with that string. */
1502 it->current.string_pos = pos->string_pos;
1503 xassert (STRINGP (it->string));
1504 }
1505
1506 /* Restore position in display vector translations or control
1507 character translations. */
1508 if (pos->dpvec_index >= 0)
1509 {
1510 /* This fills IT->dpvec. */
1511 get_next_display_element (it);
1512 xassert (it->dpvec && it->current.dpvec_index == 0);
1513 it->current.dpvec_index = pos->dpvec_index;
1514 }
1515
1516 CHECK_IT (it);
1517}
1518
1519
1520/* Initialize IT for stepping through current_buffer in window W
1521 starting at ROW->start. */
1522
1523static void
1524init_to_row_start (it, w, row)
1525 struct it *it;
1526 struct window *w;
1527 struct glyph_row *row;
1528{
1529 init_from_display_pos (it, w, &row->start);
1530 it->continuation_lines_width = row->continuation_lines_width;
1531 CHECK_IT (it);
1532}
1533
1534
1535/* Initialize IT for stepping through current_buffer in window W
1536 starting in the line following ROW, i.e. starting at ROW->end. */
1537
1538static void
1539init_to_row_end (it, w, row)
1540 struct it *it;
1541 struct window *w;
1542 struct glyph_row *row;
1543{
1544 init_from_display_pos (it, w, &row->end);
1545
1546 if (row->continued_p)
1547 it->continuation_lines_width = (row->continuation_lines_width
1548 + row->pixel_width);
1549 CHECK_IT (it);
1550}
1551
1552
1553
1554\f
1555/***********************************************************************
1556 Text properties
1557 ***********************************************************************/
1558
1559/* Called when IT reaches IT->stop_charpos. Handle text property and
1560 overlay changes. Set IT->stop_charpos to the next position where
1561 to stop. */
1562
1563static void
1564handle_stop (it)
1565 struct it *it;
1566{
1567 enum prop_handled handled;
1568 int handle_overlay_change_p = 1;
1569 struct props *p;
1570
1571 it->dpvec = NULL;
1572 it->current.dpvec_index = -1;
1573
1574 do
1575 {
1576 handled = HANDLED_NORMALLY;
1577
1578 /* Call text property handlers. */
1579 for (p = it_props; p->handler; ++p)
1580 {
1581 handled = p->handler (it);
1582
1583 if (handled == HANDLED_RECOMPUTE_PROPS)
1584 break;
1585 else if (handled == HANDLED_RETURN)
1586 return;
1587 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1588 handle_overlay_change_p = 0;
1589 }
1590
1591 if (handled != HANDLED_RECOMPUTE_PROPS)
1592 {
1593 /* Don't check for overlay strings below when set to deliver
1594 characters from a display vector. */
1595 if (it->method == next_element_from_display_vector)
1596 handle_overlay_change_p = 0;
1597
1598 /* Handle overlay changes. */
1599 if (handle_overlay_change_p)
1600 handled = handle_overlay_change (it);
1601
1602 /* Determine where to stop next. */
1603 if (handled == HANDLED_NORMALLY)
1604 compute_stop_pos (it);
1605 }
1606 }
1607 while (handled == HANDLED_RECOMPUTE_PROPS);
1608}
1609
1610
1611/* Compute IT->stop_charpos from text property and overlay change
1612 information for IT's current position. */
1613
1614static void
1615compute_stop_pos (it)
1616 struct it *it;
1617{
1618 register INTERVAL iv, next_iv;
1619 Lisp_Object object, limit, position;
1620
1621 /* If nowhere else, stop at the end. */
1622 it->stop_charpos = it->end_charpos;
1623
1624 if (STRINGP (it->string))
1625 {
1626 /* Strings are usually short, so don't limit the search for
1627 properties. */
1628 object = it->string;
1629 limit = Qnil;
1630 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1631 }
1632 else
1633 {
1634 int charpos;
1635
1636 /* If next overlay change is in front of the current stop pos
1637 (which is IT->end_charpos), stop there. Note: value of
1638 next_overlay_change is point-max if no overlay change
1639 follows. */
1640 charpos = next_overlay_change (IT_CHARPOS (*it));
1641 if (charpos < it->stop_charpos)
1642 it->stop_charpos = charpos;
1643
1644 /* If showing the region, we have to stop at the region
1645 start or end because the face might change there. */
1646 if (it->region_beg_charpos > 0)
1647 {
1648 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1649 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1650 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1651 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1652 }
1653
1654 /* Set up variables for computing the stop position from text
1655 property changes. */
1656 XSETBUFFER (object, current_buffer);
1657 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1658 XSETFASTINT (position, IT_CHARPOS (*it));
1659
1660 }
1661
1662 /* Get the interval containing IT's position. Value is a null
1663 interval if there isn't such an interval. */
1664 iv = validate_interval_range (object, &position, &position, 0);
1665 if (!NULL_INTERVAL_P (iv))
1666 {
1667 Lisp_Object values_here[LAST_PROP_IDX];
1668 struct props *p;
1669
1670 /* Get properties here. */
1671 for (p = it_props; p->handler; ++p)
1672 values_here[p->idx] = textget (iv->plist, *p->name);
1673
1674 /* Look for an interval following iv that has different
1675 properties. */
1676 for (next_iv = next_interval (iv);
1677 (!NULL_INTERVAL_P (next_iv)
1678 && (NILP (limit)
1679 || XFASTINT (limit) > next_iv->position));
1680 next_iv = next_interval (next_iv))
1681 {
1682 for (p = it_props; p->handler; ++p)
1683 {
1684 Lisp_Object new_value;
1685
1686 new_value = textget (next_iv->plist, *p->name);
1687 if (!EQ (values_here[p->idx], new_value))
1688 break;
1689 }
1690
1691 if (p->handler)
1692 break;
1693 }
1694
1695 if (!NULL_INTERVAL_P (next_iv))
1696 {
1697 if (INTEGERP (limit)
1698 && next_iv->position >= XFASTINT (limit))
1699 /* No text property change up to limit. */
1700 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1701 else
1702 /* Text properties change in next_iv. */
1703 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1704 }
1705 }
1706
1707 xassert (STRINGP (it->string)
1708 || (it->stop_charpos >= BEGV
1709 && it->stop_charpos >= IT_CHARPOS (*it)));
1710}
1711
1712
1713/* Return the position of the next overlay change after POS in
1714 current_buffer. Value is point-max if no overlay change
1715 follows. This is like `next-overlay-change' but doesn't use
1716 xmalloc. */
1717
1718static int
1719next_overlay_change (pos)
1720 int pos;
1721{
1722 int noverlays;
1723 int endpos;
1724 Lisp_Object *overlays;
1725 int len;
1726 int i;
1727
1728 /* Get all overlays at the given position. */
1729 len = 10;
1730 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1731 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1732 if (noverlays > len)
1733 {
1734 len = noverlays;
1735 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1736 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1737 }
1738
1739 /* If any of these overlays ends before endpos,
1740 use its ending point instead. */
1741 for (i = 0; i < noverlays; ++i)
1742 {
1743 Lisp_Object oend;
1744 int oendpos;
1745
1746 oend = OVERLAY_END (overlays[i]);
1747 oendpos = OVERLAY_POSITION (oend);
1748 endpos = min (endpos, oendpos);
1749 }
1750
1751 return endpos;
1752}
1753
1754
1755\f
1756/***********************************************************************
1757 Fontification
1758 ***********************************************************************/
1759
1760/* Handle changes in the `fontified' property of the current buffer by
1761 calling hook functions from Qfontification_functions to fontify
1762 regions of text. */
1763
1764static enum prop_handled
1765handle_fontified_prop (it)
1766 struct it *it;
1767{
1768 Lisp_Object prop, pos;
1769 enum prop_handled handled = HANDLED_NORMALLY;
2bf6fa4b 1770 struct gcpro gcpro1;
5f5c8ee5
GM
1771
1772 /* Get the value of the `fontified' property at IT's current buffer
1773 position. (The `fontified' property doesn't have a special
1774 meaning in strings.) If the value is nil, call functions from
1775 Qfontification_functions. */
1776 if (!STRINGP (it->string)
1777 && it->s == NULL
1778 && !NILP (Vfontification_functions)
1779 && (pos = make_number (IT_CHARPOS (*it)),
1780 prop = Fget_char_property (pos, Qfontified, Qnil),
1781 NILP (prop)))
1782 {
1783 Lisp_Object args[2];
1784
2bf6fa4b 1785 GCPRO1 (pos);
5f5c8ee5
GM
1786 /* Run the hook functions. */
1787 args[0] = Qfontification_functions;
1788 args[1] = pos;
1789 Frun_hook_with_args (make_number (2), args);
1790
1791 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1792 something. This avoids an endless loop if they failed to
1793 fontify the text for which reason ever. */
1794 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1795 handled = HANDLED_RECOMPUTE_PROPS;
2bf6fa4b 1796 UNGCPRO;
5f5c8ee5
GM
1797 }
1798
1799 return handled;
1800}
1801
1802
1803\f
1804/***********************************************************************
1805 Faces
1806 ***********************************************************************/
1807
1808/* Set up iterator IT from face properties at its current position.
1809 Called from handle_stop. */
1810
1811static enum prop_handled
1812handle_face_prop (it)
1813 struct it *it;
1814{
1815 int new_face_id, next_stop;
1816
1817 if (!STRINGP (it->string))
1818 {
1819 new_face_id
1820 = face_at_buffer_position (it->w,
1821 IT_CHARPOS (*it),
1822 it->region_beg_charpos,
1823 it->region_end_charpos,
1824 &next_stop,
1825 (IT_CHARPOS (*it)
1826 + TEXT_PROP_DISTANCE_LIMIT),
1827 0);
1828
1829 /* Is this a start of a run of characters with box face?
1830 Caveat: this can be called for a freshly initialized
1831 iterator; face_id is -1 is this case. We know that the new
1832 face will not change until limit, i.e. if the new face has a
1833 box, all characters up to limit will have one. But, as
1834 usual, we don't know whether limit is really the end. */
1835 if (new_face_id != it->face_id)
1836 {
1837 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1838
1839 /* If new face has a box but old face has not, this is
1840 the start of a run of characters with box, i.e. it has
1841 a shadow on the left side. The value of face_id of the
1842 iterator will be -1 if this is the initial call that gets
1843 the face. In this case, we have to look in front of IT's
1844 position and see whether there is a face != new_face_id. */
1845 it->start_of_box_run_p
1846 = (new_face->box != FACE_NO_BOX
1847 && (it->face_id >= 0
1848 || IT_CHARPOS (*it) == BEG
1849 || new_face_id != face_before_it_pos (it)));
1850 it->face_box_p = new_face->box != FACE_NO_BOX;
1851 }
1852 }
1853 else
1854 {
1855 new_face_id
1856 = face_at_string_position (it->w,
1857 it->string,
1858 IT_STRING_CHARPOS (*it),
1859 (it->current.overlay_string_index >= 0
1860 ? IT_CHARPOS (*it)
1861 : 0),
1862 it->region_beg_charpos,
1863 it->region_end_charpos,
1864 &next_stop,
1865 it->base_face_id);
1866
1867#if 0 /* This shouldn't be neccessary. Let's check it. */
1868 /* If IT is used to display a mode line we would really like to
1869 use the mode line face instead of the frame's default face. */
1870 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1871 && new_face_id == DEFAULT_FACE_ID)
1872 new_face_id = MODE_LINE_FACE_ID;
1873#endif
1874
1875 /* Is this a start of a run of characters with box? Caveat:
1876 this can be called for a freshly allocated iterator; face_id
1877 is -1 is this case. We know that the new face will not
1878 change until the next check pos, i.e. if the new face has a
1879 box, all characters up to that position will have a
1880 box. But, as usual, we don't know whether that position
1881 is really the end. */
1882 if (new_face_id != it->face_id)
1883 {
1884 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1885 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1886
1887 /* If new face has a box but old face hasn't, this is the
1888 start of a run of characters with box, i.e. it has a
1889 shadow on the left side. */
1890 it->start_of_box_run_p
1891 = new_face->box && (old_face == NULL || !old_face->box);
1892 it->face_box_p = new_face->box != FACE_NO_BOX;
1893 }
1894 }
1895
1896 it->face_id = new_face_id;
5f5c8ee5
GM
1897 return HANDLED_NORMALLY;
1898}
1899
1900
1901/* Compute the face one character before or after the current position
1902 of IT. BEFORE_P non-zero means get the face in front of IT's
1903 position. Value is the id of the face. */
1904
1905static int
1906face_before_or_after_it_pos (it, before_p)
1907 struct it *it;
1908 int before_p;
1909{
1910 int face_id, limit;
1911 int next_check_charpos;
1912 struct text_pos pos;
1913
1914 xassert (it->s == NULL);
1915
1916 if (STRINGP (it->string))
1917 {
1918 /* No face change past the end of the string (for the case
1919 we are padding with spaces). No face change before the
1920 string start. */
1921 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1922 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1923 return it->face_id;
1924
1925 /* Set pos to the position before or after IT's current position. */
1926 if (before_p)
1927 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1928 else
260a86a0
KH
1929 /* For composition, we must check the character after the
1930 composition. */
1931 pos = (it->what == IT_COMPOSITION
1932 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1933 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5
GM
1934
1935 /* Get the face for ASCII, or unibyte. */
1936 face_id
1937 = face_at_string_position (it->w,
1938 it->string,
1939 CHARPOS (pos),
1940 (it->current.overlay_string_index >= 0
1941 ? IT_CHARPOS (*it)
1942 : 0),
1943 it->region_beg_charpos,
1944 it->region_end_charpos,
1945 &next_check_charpos,
1946 it->base_face_id);
1947
1948 /* Correct the face for charsets different from ASCII. Do it
1949 for the multibyte case only. The face returned above is
1950 suitable for unibyte text if IT->string is unibyte. */
1951 if (STRING_MULTIBYTE (it->string))
1952 {
1953 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1954 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
1955 int c, len;
1956 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 1957
4fdb80f2 1958 c = string_char_and_length (p, rest, &len);
980806b6 1959 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
1960 }
1961 }
1962 else
1963 {
70851746
GM
1964 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1965 || (IT_CHARPOS (*it) <= BEGV && before_p))
1966 return it->face_id;
1967
5f5c8ee5
GM
1968 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1969 pos = it->current.pos;
1970
1971 if (before_p)
c1005d06 1972 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 1973 else
260a86a0
KH
1974 {
1975 if (it->what == IT_COMPOSITION)
1976 /* For composition, we must check the position after the
1977 composition. */
1978 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1979 else
c1005d06 1980 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 1981 }
5f5c8ee5
GM
1982 /* Determine face for CHARSET_ASCII, or unibyte. */
1983 face_id = face_at_buffer_position (it->w,
1984 CHARPOS (pos),
1985 it->region_beg_charpos,
1986 it->region_end_charpos,
1987 &next_check_charpos,
1988 limit, 0);
1989
1990 /* Correct the face for charsets different from ASCII. Do it
1991 for the multibyte case only. The face returned above is
1992 suitable for unibyte text if current_buffer is unibyte. */
1993 if (it->multibyte_p)
1994 {
980806b6
KH
1995 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
1996 struct face *face = FACE_FROM_ID (it->f, face_id);
1997 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
1998 }
1999 }
2000
2001 return face_id;
2002}
2003
2004
2005\f
2006/***********************************************************************
2007 Invisible text
2008 ***********************************************************************/
2009
2010/* Set up iterator IT from invisible properties at its current
2011 position. Called from handle_stop. */
2012
2013static enum prop_handled
2014handle_invisible_prop (it)
2015 struct it *it;
2016{
2017 enum prop_handled handled = HANDLED_NORMALLY;
2018
2019 if (STRINGP (it->string))
2020 {
2021 extern Lisp_Object Qinvisible;
2022 Lisp_Object prop, end_charpos, limit, charpos;
2023
2024 /* Get the value of the invisible text property at the
2025 current position. Value will be nil if there is no such
2026 property. */
2027 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2028 prop = Fget_text_property (charpos, Qinvisible, it->string);
2029
2030 if (!NILP (prop))
2031 {
2032 handled = HANDLED_RECOMPUTE_PROPS;
2033
2034 /* Get the position at which the next change of the
2035 invisible text property can be found in IT->string.
2036 Value will be nil if the property value is the same for
2037 all the rest of IT->string. */
2038 XSETINT (limit, XSTRING (it->string)->size);
2039 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2040 it->string, limit);
2041
2042 /* Text at current position is invisible. The next
2043 change in the property is at position end_charpos.
2044 Move IT's current position to that position. */
2045 if (INTEGERP (end_charpos)
2046 && XFASTINT (end_charpos) < XFASTINT (limit))
2047 {
2048 struct text_pos old;
2049 old = it->current.string_pos;
2050 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2051 compute_string_pos (&it->current.string_pos, old, it->string);
2052 }
2053 else
2054 {
2055 /* The rest of the string is invisible. If this is an
2056 overlay string, proceed with the next overlay string
2057 or whatever comes and return a character from there. */
2058 if (it->current.overlay_string_index >= 0)
2059 {
2060 next_overlay_string (it);
2061 /* Don't check for overlay strings when we just
2062 finished processing them. */
2063 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2064 }
2065 else
2066 {
2067 struct Lisp_String *s = XSTRING (it->string);
2068 IT_STRING_CHARPOS (*it) = s->size;
2069 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2070 }
2071 }
2072 }
2073 }
2074 else
2075 {
2076 int visible_p, newpos, next_stop;
2077 Lisp_Object pos, prop;
2078
2079 /* First of all, is there invisible text at this position? */
2080 XSETFASTINT (pos, IT_CHARPOS (*it));
2081 prop = Fget_char_property (pos, Qinvisible, it->window);
2082
2083 /* If we are on invisible text, skip over it. */
2084 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2085 {
2086 /* Record whether we have to display an ellipsis for the
2087 invisible text. */
2088 int display_ellipsis_p
2089 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2090
2091 handled = HANDLED_RECOMPUTE_PROPS;
2092
2093 /* Loop skipping over invisible text. The loop is left at
2094 ZV or with IT on the first char being visible again. */
2095 do
2096 {
2097 /* Try to skip some invisible text. Return value is the
2098 position reached which can be equal to IT's position
2099 if there is nothing invisible here. This skips both
2100 over invisible text properties and overlays with
2101 invisible property. */
2102 newpos = skip_invisible (IT_CHARPOS (*it),
2103 &next_stop, ZV, it->window);
2104
2105 /* If we skipped nothing at all we weren't at invisible
2106 text in the first place. If everything to the end of
2107 the buffer was skipped, end the loop. */
2108 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2109 visible_p = 1;
2110 else
2111 {
2112 /* We skipped some characters but not necessarily
2113 all there are. Check if we ended up on visible
2114 text. Fget_char_property returns the property of
2115 the char before the given position, i.e. if we
2116 get visible_p = 1, this means that the char at
2117 newpos is visible. */
2118 XSETFASTINT (pos, newpos);
2119 prop = Fget_char_property (pos, Qinvisible, it->window);
2120 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2121 }
2122
2123 /* If we ended up on invisible text, proceed to
2124 skip starting with next_stop. */
2125 if (!visible_p)
2126 IT_CHARPOS (*it) = next_stop;
2127 }
2128 while (!visible_p);
2129
2130 /* The position newpos is now either ZV or on visible text. */
2131 IT_CHARPOS (*it) = newpos;
2132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2133
2134 /* Maybe return `...' next for the end of the invisible text. */
2135 if (display_ellipsis_p)
2136 {
2137 if (it->dp
2138 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2139 {
2140 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2141 it->dpvec = v->contents;
2142 it->dpend = v->contents + v->size;
2143 }
2144 else
2145 {
2146 /* Default `...'. */
2147 it->dpvec = default_invis_vector;
2148 it->dpend = default_invis_vector + 3;
2149 }
2150
2151 /* The ellipsis display does not replace the display of
2152 the character at the new position. Indicate this by
2153 setting IT->dpvec_char_len to zero. */
2154 it->dpvec_char_len = 0;
2155
2156 it->current.dpvec_index = 0;
2157 it->method = next_element_from_display_vector;
2158 }
2159 }
2160 }
2161
2162 return handled;
2163}
2164
2165
2166\f
2167/***********************************************************************
2168 'display' property
2169 ***********************************************************************/
2170
2171/* Set up iterator IT from `display' property at its current position.
2172 Called from handle_stop. */
2173
2174static enum prop_handled
2175handle_display_prop (it)
2176 struct it *it;
2177{
2178 Lisp_Object prop, object;
2179 struct text_pos *position;
2180 int space_or_image_found_p;
2181
2182 if (STRINGP (it->string))
2183 {
2184 object = it->string;
2185 position = &it->current.string_pos;
2186 }
2187 else
2188 {
2189 object = Qnil;
2190 position = &it->current.pos;
2191 }
2192
2193 /* Reset those iterator values set from display property values. */
2194 it->font_height = Qnil;
2195 it->space_width = Qnil;
2196 it->voffset = 0;
2197
2198 /* We don't support recursive `display' properties, i.e. string
2199 values that have a string `display' property, that have a string
2200 `display' property etc. */
2201 if (!it->string_from_display_prop_p)
2202 it->area = TEXT_AREA;
2203
2204 prop = Fget_char_property (make_number (position->charpos),
2205 Qdisplay, object);
2206 if (NILP (prop))
2207 return HANDLED_NORMALLY;
2208
2209 space_or_image_found_p = 0;
f3751a65
GM
2210 if (CONSP (prop)
2211 && CONSP (XCAR (prop))
2212 && !EQ (Qmargin, XCAR (XCAR (prop))))
5f5c8ee5 2213 {
f3751a65 2214 /* A list of sub-properties. */
5f5c8ee5
GM
2215 while (CONSP (prop))
2216 {
2217 if (handle_single_display_prop (it, XCAR (prop), object, position))
2218 space_or_image_found_p = 1;
2219 prop = XCDR (prop);
2220 }
2221 }
2222 else if (VECTORP (prop))
2223 {
2224 int i;
2225 for (i = 0; i < XVECTOR (prop)->size; ++i)
2226 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2227 object, position))
2228 space_or_image_found_p = 1;
2229 }
2230 else
2231 {
2232 if (handle_single_display_prop (it, prop, object, position))
2233 space_or_image_found_p = 1;
2234 }
2235
2236 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2237}
2238
2239
6c577098 2240/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2241 at START_POS in OBJECT. */
2242
2243static struct text_pos
2244display_prop_end (it, object, start_pos)
2245 struct it *it;
2246 Lisp_Object object;
2247 struct text_pos start_pos;
2248{
2249 Lisp_Object end;
2250 struct text_pos end_pos;
5f5c8ee5 2251
6c577098
GM
2252 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2253 Qdisplay, object, Qnil);
2254 CHARPOS (end_pos) = XFASTINT (end);
2255 if (STRINGP (object))
5f5c8ee5
GM
2256 compute_string_pos (&end_pos, start_pos, it->string);
2257 else
6c577098 2258 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2259
2260 return end_pos;
2261}
2262
2263
2264/* Set up IT from a single `display' sub-property value PROP. OBJECT
2265 is the object in which the `display' property was found. *POSITION
2266 is the position at which it was found.
2267
2268 If PROP is a `space' or `image' sub-property, set *POSITION to the
2269 end position of the `display' property.
2270
2271 Value is non-zero if a `space' or `image' property value was found. */
2272
2273static int
2274handle_single_display_prop (it, prop, object, position)
2275 struct it *it;
2276 Lisp_Object prop;
2277 Lisp_Object object;
2278 struct text_pos *position;
2279{
2280 Lisp_Object value;
2281 int space_or_image_found_p = 0;
2282
2283 Lisp_Object form;
2284
d3acf96b 2285 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2286 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2287 form = Qt;
d3acf96b 2288 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2289 {
2290 prop = XCDR (prop);
2291 if (!CONSP (prop))
2292 return 0;
2293 form = XCAR (prop);
2294 prop = XCDR (prop);
5f5c8ee5
GM
2295 }
2296
2297 if (!NILP (form) && !EQ (form, Qt))
2298 {
2299 struct gcpro gcpro1;
2300 struct text_pos end_pos, pt;
2301
2302 end_pos = display_prop_end (it, object, *position);
2303 GCPRO1 (form);
2304
2305 /* Temporarily set point to the end position, and then evaluate
2306 the form. This makes `(eolp)' work as FORM. */
2307 CHARPOS (pt) = PT;
2308 BYTEPOS (pt) = PT_BYTE;
2309 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2310 form = eval_form (form);
2311 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2312 UNGCPRO;
2313 }
2314
2315 if (NILP (form))
2316 return 0;
2317
2318 if (CONSP (prop)
2319 && EQ (XCAR (prop), Qheight)
2320 && CONSP (XCDR (prop)))
2321 {
2322 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2323 return 0;
2324
2325 /* `(height HEIGHT)'. */
2326 it->font_height = XCAR (XCDR (prop));
2327 if (!NILP (it->font_height))
2328 {
2329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2330 int new_height = -1;
2331
2332 if (CONSP (it->font_height)
2333 && (EQ (XCAR (it->font_height), Qplus)
2334 || EQ (XCAR (it->font_height), Qminus))
2335 && CONSP (XCDR (it->font_height))
2336 && INTEGERP (XCAR (XCDR (it->font_height))))
2337 {
2338 /* `(+ N)' or `(- N)' where N is an integer. */
2339 int steps = XINT (XCAR (XCDR (it->font_height)));
2340 if (EQ (XCAR (it->font_height), Qplus))
2341 steps = - steps;
2342 it->face_id = smaller_face (it->f, it->face_id, steps);
2343 }
2344 else if (SYMBOLP (it->font_height))
2345 {
2346 /* Call function with current height as argument.
2347 Value is the new height. */
2348 Lisp_Object form, height;
2349 struct gcpro gcpro1;
2350
2351 height = face->lface[LFACE_HEIGHT_INDEX];
2352 form = Fcons (it->font_height, Fcons (height, Qnil));
2353 GCPRO1 (form);
2354 height = eval_form (form);
2355 if (NUMBERP (height))
2356 new_height = XFLOATINT (height);
2357 UNGCPRO;
2358 }
2359 else if (NUMBERP (it->font_height))
2360 {
2361 /* Value is a multiple of the canonical char height. */
2362 struct face *face;
2363
2364 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2365 new_height = (XFLOATINT (it->font_height)
2366 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2367 }
2368 else
2369 {
2370 /* Evaluate IT->font_height with `height' bound to the
2371 current specified height to get the new height. */
2372 Lisp_Object value;
2373 int count = specpdl_ptr - specpdl;
2374
2375 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2376 value = eval_form (it->font_height);
2377 unbind_to (count, Qnil);
2378
2379 if (NUMBERP (value))
2380 new_height = XFLOATINT (value);
2381 }
2382
2383 if (new_height > 0)
2384 it->face_id = face_with_height (it->f, it->face_id, new_height);
2385 }
2386 }
2387 else if (CONSP (prop)
2388 && EQ (XCAR (prop), Qspace_width)
2389 && CONSP (XCDR (prop)))
2390 {
2391 /* `(space_width WIDTH)'. */
2392 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2393 return 0;
2394
2395 value = XCAR (XCDR (prop));
2396 if (NUMBERP (value) && XFLOATINT (value) > 0)
2397 it->space_width = value;
2398 }
2399 else if (CONSP (prop)
2400 && EQ (XCAR (prop), Qraise)
2401 && CONSP (XCDR (prop)))
2402 {
5f5c8ee5
GM
2403 /* `(raise FACTOR)'. */
2404 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2405 return 0;
2406
fb3842a8 2407#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2408 value = XCAR (XCDR (prop));
2409 if (NUMBERP (value))
2410 {
2411 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2412 it->voffset = - (XFLOATINT (value)
1ab3e082 2413 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2414 }
2415#endif /* HAVE_WINDOW_SYSTEM */
2416 }
2417 else if (!it->string_from_display_prop_p)
2418 {
f3751a65
GM
2419 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2420 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2421 Lisp_Object location, value;
2422 struct text_pos start_pos;
2423 int valid_p;
2424
2425 /* Characters having this form of property are not displayed, so
2426 we have to find the end of the property. */
2427 space_or_image_found_p = 1;
2428 start_pos = *position;
2429 *position = display_prop_end (it, object, start_pos);
15e26c76 2430 value = Qnil;
5f5c8ee5
GM
2431
2432 /* Let's stop at the new position and assume that all
2433 text properties change there. */
2434 it->stop_charpos = position->charpos;
2435
f3751a65
GM
2436 location = Qunbound;
2437 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2438 {
f3751a65
GM
2439 Lisp_Object tem;
2440
5f5c8ee5 2441 value = XCDR (prop);
f3751a65
GM
2442 if (CONSP (value))
2443 value = XCAR (value);
2444
2445 tem = XCAR (prop);
2446 if (EQ (XCAR (tem), Qmargin)
2447 && (tem = XCDR (tem),
2448 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2449 (NILP (tem)
2450 || EQ (tem, Qleft_margin)
2451 || EQ (tem, Qright_margin))))
2452 location = tem;
5f5c8ee5 2453 }
f3751a65
GM
2454
2455 if (EQ (location, Qunbound))
5f5c8ee5
GM
2456 {
2457 location = Qnil;
2458 value = prop;
2459 }
2460
2461#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 2462 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
2463 valid_p = STRINGP (value);
2464 else
2465 valid_p = (STRINGP (value)
2466 || (CONSP (value) && EQ (XCAR (value), Qspace))
2467 || valid_image_p (value));
2468#else /* not HAVE_WINDOW_SYSTEM */
2469 valid_p = STRINGP (value);
2470#endif /* not HAVE_WINDOW_SYSTEM */
2471
2472 if ((EQ (location, Qleft_margin)
2473 || EQ (location, Qright_margin)
2474 || NILP (location))
2475 && valid_p)
2476 {
2477 /* Save current settings of IT so that we can restore them
2478 when we are finished with the glyph property value. */
2479 push_it (it);
2480
2481 if (NILP (location))
2482 it->area = TEXT_AREA;
2483 else if (EQ (location, Qleft_margin))
2484 it->area = LEFT_MARGIN_AREA;
2485 else
2486 it->area = RIGHT_MARGIN_AREA;
2487
2488 if (STRINGP (value))
2489 {
2490 it->string = value;
2491 it->multibyte_p = STRING_MULTIBYTE (it->string);
2492 it->current.overlay_string_index = -1;
2493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2494 it->end_charpos = it->string_nchars
2495 = XSTRING (it->string)->size;
2496 it->method = next_element_from_string;
2497 it->stop_charpos = 0;
2498 it->string_from_display_prop_p = 1;
2499 }
2500 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2501 {
2502 it->method = next_element_from_stretch;
2503 it->object = value;
2504 it->current.pos = it->position = start_pos;
2505 }
2506#ifdef HAVE_WINDOW_SYSTEM
2507 else
2508 {
2509 it->what = IT_IMAGE;
2510 it->image_id = lookup_image (it->f, value);
2511 it->position = start_pos;
2512 it->object = NILP (object) ? it->w->buffer : object;
2513 it->method = next_element_from_image;
2514
2515 /* Say that we don't have consumed the characters with
2516 `display' property yet. The call to pop_it in
2517 set_iterator_to_next will clean this up. */
2518 *position = start_pos;
2519 }
2520#endif /* HAVE_WINDOW_SYSTEM */
2521 }
2522 }
2523
2524 return space_or_image_found_p;
2525}
2526
2527
2528\f
260a86a0
KH
2529/***********************************************************************
2530 `composition' property
2531 ***********************************************************************/
2532
2533/* Set up iterator IT from `composition' property at its current
2534 position. Called from handle_stop. */
2535
2536static enum prop_handled
2537handle_composition_prop (it)
2538 struct it *it;
2539{
2540 Lisp_Object prop, string;
2541 int pos, pos_byte, end;
2542 enum prop_handled handled = HANDLED_NORMALLY;
2543
2544 if (STRINGP (it->string))
2545 {
2546 pos = IT_STRING_CHARPOS (*it);
2547 pos_byte = IT_STRING_BYTEPOS (*it);
2548 string = it->string;
2549 }
2550 else
2551 {
2552 pos = IT_CHARPOS (*it);
2553 pos_byte = IT_BYTEPOS (*it);
2554 string = Qnil;
2555 }
2556
2557 /* If there's a valid composition and point is not inside of the
2558 composition (in the case that the composition is from the current
2559 buffer), draw a glyph composed from the composition components. */
2560 if (find_composition (pos, -1, &pos, &end, &prop, string)
2561 && COMPOSITION_VALID_P (pos, end, prop)
2562 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2563 {
2564 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2565
2566 if (id >= 0)
2567 {
2568 it->method = next_element_from_composition;
2569 it->cmp_id = id;
2570 it->cmp_len = COMPOSITION_LENGTH (prop);
2571 /* For a terminal, draw only the first character of the
2572 components. */
2573 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2574 it->len = (STRINGP (it->string)
2575 ? string_char_to_byte (it->string, end)
2576 : CHAR_TO_BYTE (end)) - pos_byte;
2577 it->stop_charpos = end;
2578 handled = HANDLED_RETURN;
2579 }
2580 }
2581
2582 return handled;
2583}
2584
2585
2586\f
5f5c8ee5
GM
2587/***********************************************************************
2588 Overlay strings
2589 ***********************************************************************/
2590
2591/* The following structure is used to record overlay strings for
2592 later sorting in load_overlay_strings. */
2593
2594struct overlay_entry
2595{
2596 Lisp_Object string;
2597 int priority;
2598 int after_string_p;
2599};
2600
2601
2602/* Set up iterator IT from overlay strings at its current position.
2603 Called from handle_stop. */
2604
2605static enum prop_handled
2606handle_overlay_change (it)
2607 struct it *it;
2608{
2609 /* Overlays are handled in current_buffer only. */
2610 if (STRINGP (it->string))
2611 return HANDLED_NORMALLY;
2612 else
2613 return (get_overlay_strings (it)
2614 ? HANDLED_RECOMPUTE_PROPS
2615 : HANDLED_NORMALLY);
2616}
2617
2618
2619/* Set up the next overlay string for delivery by IT, if there is an
2620 overlay string to deliver. Called by set_iterator_to_next when the
2621 end of the current overlay string is reached. If there are more
2622 overlay strings to display, IT->string and
2623 IT->current.overlay_string_index are set appropriately here.
2624 Otherwise IT->string is set to nil. */
2625
2626static void
2627next_overlay_string (it)
2628 struct it *it;
2629{
2630 ++it->current.overlay_string_index;
2631 if (it->current.overlay_string_index == it->n_overlay_strings)
2632 {
2633 /* No more overlay strings. Restore IT's settings to what
2634 they were before overlay strings were processed, and
2635 continue to deliver from current_buffer. */
2636 pop_it (it);
2637 xassert (it->stop_charpos >= BEGV
2638 && it->stop_charpos <= it->end_charpos);
2639 it->string = Qnil;
2640 it->current.overlay_string_index = -1;
2641 SET_TEXT_POS (it->current.string_pos, -1, -1);
2642 it->n_overlay_strings = 0;
2643 it->method = next_element_from_buffer;
2644 }
2645 else
2646 {
2647 /* There are more overlay strings to process. If
2648 IT->current.overlay_string_index has advanced to a position
2649 where we must load IT->overlay_strings with more strings, do
2650 it. */
2651 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2652
2653 if (it->current.overlay_string_index && i == 0)
2654 load_overlay_strings (it);
2655
2656 /* Initialize IT to deliver display elements from the overlay
2657 string. */
2658 it->string = it->overlay_strings[i];
2659 it->multibyte_p = STRING_MULTIBYTE (it->string);
2660 SET_TEXT_POS (it->current.string_pos, 0, 0);
2661 it->method = next_element_from_string;
2662 it->stop_charpos = 0;
2663 }
2664
2665 CHECK_IT (it);
2666}
2667
2668
2669/* Compare two overlay_entry structures E1 and E2. Used as a
2670 comparison function for qsort in load_overlay_strings. Overlay
2671 strings for the same position are sorted so that
2672
2673 1. All after-strings come in front of before-strings.
2674
2675 2. Within after-strings, strings are sorted so that overlay strings
2676 from overlays with higher priorities come first.
2677
2678 2. Within before-strings, strings are sorted so that overlay
2679 strings from overlays with higher priorities come last.
2680
2681 Value is analogous to strcmp. */
2682
2683
2684static int
2685compare_overlay_entries (e1, e2)
2686 void *e1, *e2;
2687{
2688 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2689 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2690 int result;
2691
2692 if (entry1->after_string_p != entry2->after_string_p)
2693 /* Let after-strings appear in front of before-strings. */
2694 result = entry1->after_string_p ? -1 : 1;
2695 else if (entry1->after_string_p)
2696 /* After-strings sorted in order of decreasing priority. */
2697 result = entry2->priority - entry1->priority;
2698 else
2699 /* Before-strings sorted in order of increasing priority. */
2700 result = entry1->priority - entry2->priority;
2701
2702 return result;
2703}
2704
2705
2706/* Load the vector IT->overlay_strings with overlay strings from IT's
2707 current buffer position. Set IT->n_overlays to the total number of
2708 overlay strings found.
2709
2710 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2711 a time. On entry into load_overlay_strings,
2712 IT->current.overlay_string_index gives the number of overlay
2713 strings that have already been loaded by previous calls to this
2714 function.
2715
2716 Overlay strings are sorted so that after-string strings come in
2717 front of before-string strings. Within before and after-strings,
2718 strings are sorted by overlay priority. See also function
2719 compare_overlay_entries. */
2720
2721static void
2722load_overlay_strings (it)
2723 struct it *it;
2724{
2725 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2726 Lisp_Object ov, overlay, window, str;
2727 int start, end;
2728 int size = 20;
2729 int n = 0, i, j;
2730 struct overlay_entry *entries
2731 = (struct overlay_entry *) alloca (size * sizeof *entries);
2732
2733 /* Append the overlay string STRING of overlay OVERLAY to vector
2734 `entries' which has size `size' and currently contains `n'
2735 elements. AFTER_P non-zero means STRING is an after-string of
2736 OVERLAY. */
2737#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2738 do \
2739 { \
2740 Lisp_Object priority; \
2741 \
2742 if (n == size) \
2743 { \
2744 int new_size = 2 * size; \
2745 struct overlay_entry *old = entries; \
2746 entries = \
2747 (struct overlay_entry *) alloca (new_size \
2748 * sizeof *entries); \
2749 bcopy (old, entries, size * sizeof *entries); \
2750 size = new_size; \
2751 } \
2752 \
2753 entries[n].string = (STRING); \
2754 priority = Foverlay_get ((OVERLAY), Qpriority); \
2755 entries[n].priority \
2756 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2757 entries[n].after_string_p = (AFTER_P); \
2758 ++n; \
2759 } \
2760 while (0)
2761
2762 /* Process overlay before the overlay center. */
2763 for (ov = current_buffer->overlays_before;
2764 CONSP (ov);
9472f927 2765 ov = XCDR (ov))
5f5c8ee5 2766 {
9472f927 2767 overlay = XCAR (ov);
5f5c8ee5
GM
2768 xassert (OVERLAYP (overlay));
2769 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2770 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2771
2772 if (end < IT_CHARPOS (*it))
2773 break;
2774
2775 /* Skip this overlay if it doesn't start or end at IT's current
2776 position. */
2777 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2778 continue;
2779
2780 /* Skip this overlay if it doesn't apply to IT->w. */
2781 window = Foverlay_get (overlay, Qwindow);
2782 if (WINDOWP (window) && XWINDOW (window) != it->w)
2783 continue;
2784
2785 /* If overlay has a non-empty before-string, record it. */
2786 if (start == IT_CHARPOS (*it)
2787 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2788 && XSTRING (str)->size)
2789 RECORD_OVERLAY_STRING (overlay, str, 0);
2790
2791 /* If overlay has a non-empty after-string, record it. */
2792 if (end == IT_CHARPOS (*it)
2793 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2794 && XSTRING (str)->size)
2795 RECORD_OVERLAY_STRING (overlay, str, 1);
2796 }
2797
2798 /* Process overlays after the overlay center. */
2799 for (ov = current_buffer->overlays_after;
2800 CONSP (ov);
9472f927 2801 ov = XCDR (ov))
5f5c8ee5 2802 {
9472f927 2803 overlay = XCAR (ov);
5f5c8ee5
GM
2804 xassert (OVERLAYP (overlay));
2805 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2806 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2807
2808 if (start > IT_CHARPOS (*it))
2809 break;
2810
2811 /* Skip this overlay if it doesn't start or end at IT's current
2812 position. */
2813 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2814 continue;
2815
2816 /* Skip this overlay if it doesn't apply to IT->w. */
2817 window = Foverlay_get (overlay, Qwindow);
2818 if (WINDOWP (window) && XWINDOW (window) != it->w)
2819 continue;
2820
2821 /* If overlay has a non-empty before-string, record it. */
2822 if (start == IT_CHARPOS (*it)
2823 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2824 && XSTRING (str)->size)
2825 RECORD_OVERLAY_STRING (overlay, str, 0);
2826
2827 /* If overlay has a non-empty after-string, record it. */
2828 if (end == IT_CHARPOS (*it)
2829 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2830 && XSTRING (str)->size)
2831 RECORD_OVERLAY_STRING (overlay, str, 1);
2832 }
2833
2834#undef RECORD_OVERLAY_STRING
2835
2836 /* Sort entries. */
2837 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2838
2839 /* Record the total number of strings to process. */
2840 it->n_overlay_strings = n;
2841
2842 /* IT->current.overlay_string_index is the number of overlay strings
2843 that have already been consumed by IT. Copy some of the
2844 remaining overlay strings to IT->overlay_strings. */
2845 i = 0;
2846 j = it->current.overlay_string_index;
2847 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2848 it->overlay_strings[i++] = entries[j++].string;
2849
2850 CHECK_IT (it);
2851}
2852
2853
2854/* Get the first chunk of overlay strings at IT's current buffer
2855 position. Value is non-zero if at least one overlay string was
2856 found. */
2857
2858static int
2859get_overlay_strings (it)
2860 struct it *it;
2861{
2862 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2863 process. This fills IT->overlay_strings with strings, and sets
2864 IT->n_overlay_strings to the total number of strings to process.
2865 IT->pos.overlay_string_index has to be set temporarily to zero
2866 because load_overlay_strings needs this; it must be set to -1
2867 when no overlay strings are found because a zero value would
2868 indicate a position in the first overlay string. */
2869 it->current.overlay_string_index = 0;
2870 load_overlay_strings (it);
2871
2872 /* If we found overlay strings, set up IT to deliver display
2873 elements from the first one. Otherwise set up IT to deliver
2874 from current_buffer. */
2875 if (it->n_overlay_strings)
2876 {
2877 /* Make sure we know settings in current_buffer, so that we can
2878 restore meaningful values when we're done with the overlay
2879 strings. */
2880 compute_stop_pos (it);
2881 xassert (it->face_id >= 0);
2882
2883 /* Save IT's settings. They are restored after all overlay
2884 strings have been processed. */
2885 xassert (it->sp == 0);
2886 push_it (it);
2887
2888 /* Set up IT to deliver display elements from the first overlay
2889 string. */
2890 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2891 it->stop_charpos = 0;
2892 it->string = it->overlay_strings[0];
2893 it->multibyte_p = STRING_MULTIBYTE (it->string);
2894 xassert (STRINGP (it->string));
2895 it->method = next_element_from_string;
2896 }
2897 else
2898 {
2899 it->string = Qnil;
2900 it->current.overlay_string_index = -1;
2901 it->method = next_element_from_buffer;
2902 }
2903
2904 CHECK_IT (it);
2905
2906 /* Value is non-zero if we found at least one overlay string. */
2907 return STRINGP (it->string);
2908}
2909
2910
2911\f
2912/***********************************************************************
2913 Saving and restoring state
2914 ***********************************************************************/
2915
2916/* Save current settings of IT on IT->stack. Called, for example,
2917 before setting up IT for an overlay string, to be able to restore
2918 IT's settings to what they were after the overlay string has been
2919 processed. */
2920
2921static void
2922push_it (it)
2923 struct it *it;
2924{
2925 struct iterator_stack_entry *p;
2926
2927 xassert (it->sp < 2);
2928 p = it->stack + it->sp;
2929
2930 p->stop_charpos = it->stop_charpos;
2931 xassert (it->face_id >= 0);
2932 p->face_id = it->face_id;
2933 p->string = it->string;
2934 p->pos = it->current;
2935 p->end_charpos = it->end_charpos;
2936 p->string_nchars = it->string_nchars;
2937 p->area = it->area;
2938 p->multibyte_p = it->multibyte_p;
2939 p->space_width = it->space_width;
2940 p->font_height = it->font_height;
2941 p->voffset = it->voffset;
2942 p->string_from_display_prop_p = it->string_from_display_prop_p;
2943 ++it->sp;
2944}
2945
2946
2947/* Restore IT's settings from IT->stack. Called, for example, when no
2948 more overlay strings must be processed, and we return to delivering
2949 display elements from a buffer, or when the end of a string from a
2950 `display' property is reached and we return to delivering display
2951 elements from an overlay string, or from a buffer. */
2952
2953static void
2954pop_it (it)
2955 struct it *it;
2956{
2957 struct iterator_stack_entry *p;
2958
2959 xassert (it->sp > 0);
2960 --it->sp;
2961 p = it->stack + it->sp;
2962 it->stop_charpos = p->stop_charpos;
2963 it->face_id = p->face_id;
2964 it->string = p->string;
2965 it->current = p->pos;
2966 it->end_charpos = p->end_charpos;
2967 it->string_nchars = p->string_nchars;
2968 it->area = p->area;
2969 it->multibyte_p = p->multibyte_p;
2970 it->space_width = p->space_width;
2971 it->font_height = p->font_height;
2972 it->voffset = p->voffset;
2973 it->string_from_display_prop_p = p->string_from_display_prop_p;
2974}
2975
2976
2977\f
2978/***********************************************************************
2979 Moving over lines
2980 ***********************************************************************/
2981
2982/* Set IT's current position to the previous line start. */
2983
2984static void
2985back_to_previous_line_start (it)
2986 struct it *it;
2987{
2988 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2989 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2990}
2991
2992
2993/* Set IT's current position to the next line start. */
2994
2995static void
2996forward_to_next_line_start (it)
2997 struct it *it;
2998{
2999 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3000 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3001}
3002
3003
3004/* Set IT's current position to the previous visible line start. Skip
3005 invisible text that is so either due to text properties or due to
3006 selective display. Caution: this does not change IT->current_x and
3007 IT->hpos. */
3008
3009static void
3010back_to_previous_visible_line_start (it)
3011 struct it *it;
3012{
3013 int visible_p = 0;
3014
3015 /* Go back one newline if not on BEGV already. */
3016 if (IT_CHARPOS (*it) > BEGV)
3017 back_to_previous_line_start (it);
3018
3019 /* Move over lines that are invisible because of selective display
3020 or text properties. */
3021 while (IT_CHARPOS (*it) > BEGV
3022 && !visible_p)
3023 {
3024 visible_p = 1;
3025
3026 /* If selective > 0, then lines indented more than that values
3027 are invisible. */
3028 if (it->selective > 0
3029 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3030 it->selective))
3031 visible_p = 0;
5f5c8ee5
GM
3032 else
3033 {
3034 Lisp_Object prop;
3035
3036 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
3037 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3038 visible_p = 0;
3039 }
5f5c8ee5
GM
3040
3041 /* Back one more newline if the current one is invisible. */
3042 if (!visible_p)
3043 back_to_previous_line_start (it);
3044 }
3045
3046 xassert (IT_CHARPOS (*it) >= BEGV);
3047 xassert (IT_CHARPOS (*it) == BEGV
3048 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3049 CHECK_IT (it);
3050}
3051
3052
3053/* Reseat iterator IT at the previous visible line start. Skip
3054 invisible text that is so either due to text properties or due to
3055 selective display. At the end, update IT's overlay information,
3056 face information etc. */
3057
3058static void
3059reseat_at_previous_visible_line_start (it)
3060 struct it *it;
3061{
3062 back_to_previous_visible_line_start (it);
3063 reseat (it, it->current.pos, 1);
3064 CHECK_IT (it);
3065}
3066
3067
3068/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3069 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3070 preceding the line start. Skip over invisible text that is so
3071 because of selective display. Compute faces, overlays etc at the
3072 new position. Note that this function does not skip over text that
3073 is invisible because of text properties. */
5f5c8ee5
GM
3074
3075static void
312246d1 3076reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3077 struct it *it;
312246d1 3078 int on_newline_p;
5f5c8ee5
GM
3079{
3080 /* Restore the buffer position when currently not delivering display
3081 elements from the current buffer. This is the case, for example,
3082 when called at the end of a truncated overlay string. */
3083 while (it->sp)
3084 pop_it (it);
3085 it->method = next_element_from_buffer;
3086
3087 /* Otherwise, scan_buffer would not work. */
3088 if (IT_CHARPOS (*it) < ZV)
3089 {
3090 /* If on a newline, advance past it. Otherwise, find the next
3091 newline which automatically gives us the position following
3092 the newline. */
3093 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3094 {
3095 ++IT_CHARPOS (*it);
3096 ++IT_BYTEPOS (*it);
3097 }
3098 else
3099 forward_to_next_line_start (it);
3100
3101 /* We must either have reached the end of the buffer or end up
3102 after a newline. */
3103 xassert (IT_CHARPOS (*it) == ZV
3104 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3105
3106 /* Skip over lines that are invisible because they are indented
3107 more than the value of IT->selective. */
3108 if (it->selective > 0)
3109 while (IT_CHARPOS (*it) < ZV
3110 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3111 it->selective))
3112 forward_to_next_line_start (it);
312246d1
GM
3113
3114 /* Position on the newline if we should. */
13f19968
GM
3115 if (on_newline_p
3116 && IT_CHARPOS (*it) > BEGV
3117 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
312246d1
GM
3118 {
3119 --IT_CHARPOS (*it);
3120 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3121 }
5f5c8ee5
GM
3122
3123 /* Set the iterator there. The 0 as the last parameter of
3124 reseat means don't force a text property lookup. The lookup
3125 is then only done if we've skipped past the iterator's
3126 check_charpos'es. This optimization is important because
3127 text property lookups tend to be expensive. */
3128 reseat (it, it->current.pos, 0);
3129 }
3130
3131 CHECK_IT (it);
3132}
3133
3134
3135\f
3136/***********************************************************************
3137 Changing an iterator's position
3138***********************************************************************/
3139
3140/* Change IT's current position to POS in current_buffer. If FORCE_P
3141 is non-zero, always check for text properties at the new position.
3142 Otherwise, text properties are only looked up if POS >=
3143 IT->check_charpos of a property. */
3144
3145static void
3146reseat (it, pos, force_p)
3147 struct it *it;
3148 struct text_pos pos;
3149 int force_p;
3150{
3151 int original_pos = IT_CHARPOS (*it);
3152
3153 reseat_1 (it, pos, 0);
3154
3155 /* Determine where to check text properties. Avoid doing it
3156 where possible because text property lookup is very expensive. */
3157 if (force_p
3158 || CHARPOS (pos) > it->stop_charpos
3159 || CHARPOS (pos) < original_pos)
3160 handle_stop (it);
3161
3162 CHECK_IT (it);
3163}
3164
3165
3166/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3167 IT->stop_pos to POS, also. */
3168
3169static void
3170reseat_1 (it, pos, set_stop_p)
3171 struct it *it;
3172 struct text_pos pos;
3173 int set_stop_p;
3174{
3175 /* Don't call this function when scanning a C string. */
3176 xassert (it->s == NULL);
3177
3178 /* POS must be a reasonable value. */
3179 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3180
3181 it->current.pos = it->position = pos;
3182 XSETBUFFER (it->object, current_buffer);
3183 it->dpvec = NULL;
3184 it->current.dpvec_index = -1;
3185 it->current.overlay_string_index = -1;
3186 IT_STRING_CHARPOS (*it) = -1;
3187 IT_STRING_BYTEPOS (*it) = -1;
3188 it->string = Qnil;
3189 it->method = next_element_from_buffer;
3190 it->sp = 0;
3191
3192 if (set_stop_p)
3193 it->stop_charpos = CHARPOS (pos);
3194}
3195
3196
3197/* Set up IT for displaying a string, starting at CHARPOS in window W.
3198 If S is non-null, it is a C string to iterate over. Otherwise,
3199 STRING gives a Lisp string to iterate over.
3200
3201 If PRECISION > 0, don't return more then PRECISION number of
3202 characters from the string.
3203
3204 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3205 characters have been returned. FIELD_WIDTH < 0 means an infinite
3206 field width.
3207
3208 MULTIBYTE = 0 means disable processing of multibyte characters,
3209 MULTIBYTE > 0 means enable it,
3210 MULTIBYTE < 0 means use IT->multibyte_p.
3211
3212 IT must be initialized via a prior call to init_iterator before
3213 calling this function. */
3214
3215static void
3216reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3217 struct it *it;
3218 unsigned char *s;
3219 Lisp_Object string;
3220 int charpos;
3221 int precision, field_width, multibyte;
3222{
3223 /* No region in strings. */
3224 it->region_beg_charpos = it->region_end_charpos = -1;
3225
3226 /* No text property checks performed by default, but see below. */
3227 it->stop_charpos = -1;
3228
3229 /* Set iterator position and end position. */
3230 bzero (&it->current, sizeof it->current);
3231 it->current.overlay_string_index = -1;
3232 it->current.dpvec_index = -1;
5f5c8ee5
GM
3233 xassert (charpos >= 0);
3234
3235 /* Use the setting of MULTIBYTE if specified. */
3236 if (multibyte >= 0)
3237 it->multibyte_p = multibyte > 0;
3238
3239 if (s == NULL)
3240 {
3241 xassert (STRINGP (string));
3242 it->string = string;
3243 it->s = NULL;
3244 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3245 it->method = next_element_from_string;
3246 it->current.string_pos = string_pos (charpos, string);
3247 }
3248 else
3249 {
3250 it->s = s;
3251 it->string = Qnil;
3252
3253 /* Note that we use IT->current.pos, not it->current.string_pos,
3254 for displaying C strings. */
3255 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3256 if (it->multibyte_p)
3257 {
3258 it->current.pos = c_string_pos (charpos, s, 1);
3259 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3260 }
3261 else
3262 {
3263 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3264 it->end_charpos = it->string_nchars = strlen (s);
3265 }
3266
3267 it->method = next_element_from_c_string;
3268 }
3269
3270 /* PRECISION > 0 means don't return more than PRECISION characters
3271 from the string. */
3272 if (precision > 0 && it->end_charpos - charpos > precision)
3273 it->end_charpos = it->string_nchars = charpos + precision;
3274
3275 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3276 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3277 FIELD_WIDTH < 0 means infinite field width. This is useful for
3278 padding with `-' at the end of a mode line. */
3279 if (field_width < 0)
3280 field_width = INFINITY;
3281 if (field_width > it->end_charpos - charpos)
3282 it->end_charpos = charpos + field_width;
3283
3284 /* Use the standard display table for displaying strings. */
3285 if (DISP_TABLE_P (Vstandard_display_table))
3286 it->dp = XCHAR_TABLE (Vstandard_display_table);
3287
3288 it->stop_charpos = charpos;
3289 CHECK_IT (it);
3290}
3291
3292
3293\f
3294/***********************************************************************
3295 Iteration
3296 ***********************************************************************/
3297
3298/* Load IT's display element fields with information about the next
3299 display element from the current position of IT. Value is zero if
3300 end of buffer (or C string) is reached. */
3301
3302int
3303get_next_display_element (it)
3304 struct it *it;
3305{
3306 /* Non-zero means that we found an display element. Zero means that
3307 we hit the end of what we iterate over. Performance note: the
3308 function pointer `method' used here turns out to be faster than
3309 using a sequence of if-statements. */
3310 int success_p = (*it->method) (it);
5f5c8ee5
GM
3311
3312 if (it->what == IT_CHARACTER)
3313 {
3314 /* Map via display table or translate control characters.
3315 IT->c, IT->len etc. have been set to the next character by
3316 the function call above. If we have a display table, and it
3317 contains an entry for IT->c, translate it. Don't do this if
3318 IT->c itself comes from a display table, otherwise we could
3319 end up in an infinite recursion. (An alternative could be to
3320 count the recursion depth of this function and signal an
3321 error when a certain maximum depth is reached.) Is it worth
3322 it? */
3323 if (success_p && it->dpvec == NULL)
3324 {
3325 Lisp_Object dv;
3326
3327 if (it->dp
3328 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3329 VECTORP (dv)))
3330 {
3331 struct Lisp_Vector *v = XVECTOR (dv);
3332
3333 /* Return the first character from the display table
3334 entry, if not empty. If empty, don't display the
3335 current character. */
3336 if (v->size)
3337 {
3338 it->dpvec_char_len = it->len;
3339 it->dpvec = v->contents;
3340 it->dpend = v->contents + v->size;
3341 it->current.dpvec_index = 0;
3342 it->method = next_element_from_display_vector;
3343 }
3344
3345 success_p = get_next_display_element (it);
3346 }
3347
3348 /* Translate control characters into `\003' or `^C' form.
3349 Control characters coming from a display table entry are
3350 currently not translated because we use IT->dpvec to hold
3351 the translation. This could easily be changed but I
197516c2
KH
3352 don't believe that it is worth doing.
3353
3354 Non-printable multibyte characters are also translated
3355 octal form. */
5f5c8ee5
GM
3356 else if ((it->c < ' '
3357 && (it->area != TEXT_AREA
c6e89d6c 3358 || (it->c != '\n' && it->c != '\t')))
54c85a23 3359 || (it->c >= 127
197516c2
KH
3360 && it->len == 1)
3361 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
3362 {
3363 /* IT->c is a control character which must be displayed
3364 either as '\003' or as `^C' where the '\\' and '^'
3365 can be defined in the display table. Fill
3366 IT->ctl_chars with glyphs for what we have to
3367 display. Then, set IT->dpvec to these glyphs. */
3368 GLYPH g;
3369
54c85a23 3370 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3371 {
3372 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3373 if (it->dp
3374 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3375 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3376 g = XINT (DISP_CTRL_GLYPH (it->dp));
3377 else
3378 g = FAST_MAKE_GLYPH ('^', 0);
3379 XSETINT (it->ctl_chars[0], g);
3380
3381 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3382 XSETINT (it->ctl_chars[1], g);
3383
3384 /* Set up IT->dpvec and return first character from it. */
3385 it->dpvec_char_len = it->len;
3386 it->dpvec = it->ctl_chars;
3387 it->dpend = it->dpvec + 2;
3388 it->current.dpvec_index = 0;
3389 it->method = next_element_from_display_vector;
3390 get_next_display_element (it);
3391 }
3392 else
3393 {
260a86a0
KH
3394 unsigned char str[MAX_MULTIBYTE_LENGTH];
3395 int len = CHAR_STRING (it->c, str);
197516c2
KH
3396 int i;
3397 GLYPH escape_glyph;
3398
5f5c8ee5
GM
3399 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3400 if (it->dp
3401 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3402 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 3403 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 3404 else
197516c2
KH
3405 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3406
3407 for (i = 0; i < len; i++)
3408 {
3409 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3410 /* Insert three more glyphs into IT->ctl_chars for
3411 the octal display of the character. */
3412 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3413 XSETINT (it->ctl_chars[i * 4 + 1], g);
3414 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3415 XSETINT (it->ctl_chars[i * 4 + 2], g);
3416 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3417 XSETINT (it->ctl_chars[i * 4 + 3], g);
3418 }
5f5c8ee5
GM
3419
3420 /* Set up IT->dpvec and return the first character
3421 from it. */
3422 it->dpvec_char_len = it->len;
3423 it->dpvec = it->ctl_chars;
197516c2 3424 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
3425 it->current.dpvec_index = 0;
3426 it->method = next_element_from_display_vector;
3427 get_next_display_element (it);
3428 }
3429 }
3430 }
3431
980806b6
KH
3432 /* Adjust face id for a multibyte character. There are no
3433 multibyte character in unibyte text. */
5f5c8ee5
GM
3434 if (it->multibyte_p
3435 && success_p
980806b6 3436 && FRAME_WINDOW_P (it->f))
5f5c8ee5 3437 {
980806b6
KH
3438 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3439 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
3440 }
3441 }
3442
3443 /* Is this character the last one of a run of characters with
3444 box? If yes, set IT->end_of_box_run_p to 1. */
3445 if (it->face_box_p
3446 && it->s == NULL)
3447 {
3448 int face_id;
3449 struct face *face;
3450
3451 it->end_of_box_run_p
3452 = ((face_id = face_after_it_pos (it),
3453 face_id != it->face_id)
3454 && (face = FACE_FROM_ID (it->f, face_id),
3455 face->box == FACE_NO_BOX));
3456 }
3457
3458 /* Value is 0 if end of buffer or string reached. */
3459 return success_p;
3460}
3461
3462
3463/* Move IT to the next display element.
3464
3465 Functions get_next_display_element and set_iterator_to_next are
3466 separate because I find this arrangement easier to handle than a
3467 get_next_display_element function that also increments IT's
3468 position. The way it is we can first look at an iterator's current
3469 display element, decide whether it fits on a line, and if it does,
3470 increment the iterator position. The other way around we probably
3471 would either need a flag indicating whether the iterator has to be
3472 incremented the next time, or we would have to implement a
3473 decrement position function which would not be easy to write. */
3474
3475void
3476set_iterator_to_next (it)
3477 struct it *it;
3478{
3479 if (it->method == next_element_from_buffer)
3480 {
3481 /* The current display element of IT is a character from
3482 current_buffer. Advance in the buffer, and maybe skip over
3483 invisible lines that are so because of selective display. */
3484 if (ITERATOR_AT_END_OF_LINE_P (it))
312246d1 3485 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
3486 else
3487 {
3488 xassert (it->len != 0);
3489 IT_BYTEPOS (*it) += it->len;
3490 IT_CHARPOS (*it) += 1;
3491 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3492 }
3493 }
260a86a0
KH
3494 else if (it->method == next_element_from_composition)
3495 {
3496 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3497 if (STRINGP (it->string))
3498 {
3499 IT_STRING_BYTEPOS (*it) += it->len;
3500 IT_STRING_CHARPOS (*it) += it->cmp_len;
3501 it->method = next_element_from_string;
3502 goto consider_string_end;
3503 }
3504 else
3505 {
3506 IT_BYTEPOS (*it) += it->len;
3507 IT_CHARPOS (*it) += it->cmp_len;
3508 it->method = next_element_from_buffer;
3509 }
3510 }
5f5c8ee5
GM
3511 else if (it->method == next_element_from_c_string)
3512 {
3513 /* Current display element of IT is from a C string. */
3514 IT_BYTEPOS (*it) += it->len;
3515 IT_CHARPOS (*it) += 1;
3516 }
3517 else if (it->method == next_element_from_display_vector)
3518 {
3519 /* Current display element of IT is from a display table entry.
3520 Advance in the display table definition. Reset it to null if
3521 end reached, and continue with characters from buffers/
3522 strings. */
3523 ++it->current.dpvec_index;
286bcbc9 3524
980806b6
KH
3525 /* Restore face of the iterator to what they were before the
3526 display vector entry (these entries may contain faces). */
5f5c8ee5 3527 it->face_id = it->saved_face_id;
286bcbc9 3528
5f5c8ee5
GM
3529 if (it->dpvec + it->current.dpvec_index == it->dpend)
3530 {
3531 if (it->s)
3532 it->method = next_element_from_c_string;
3533 else if (STRINGP (it->string))
3534 it->method = next_element_from_string;
3535 else
3536 it->method = next_element_from_buffer;
3537
3538 it->dpvec = NULL;
3539 it->current.dpvec_index = -1;
3540
312246d1
GM
3541 /* Skip over characters which were displayed via IT->dpvec. */
3542 if (it->dpvec_char_len < 0)
3543 reseat_at_next_visible_line_start (it, 1);
3544 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
3545 {
3546 it->len = it->dpvec_char_len;
3547 set_iterator_to_next (it);
3548 }
3549 }
3550 }
3551 else if (it->method == next_element_from_string)
3552 {
3553 /* Current display element is a character from a Lisp string. */
3554 xassert (it->s == NULL && STRINGP (it->string));
3555 IT_STRING_BYTEPOS (*it) += it->len;
3556 IT_STRING_CHARPOS (*it) += 1;
3557
3558 consider_string_end:
3559
3560 if (it->current.overlay_string_index >= 0)
3561 {
3562 /* IT->string is an overlay string. Advance to the
3563 next, if there is one. */
3564 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3565 next_overlay_string (it);
3566 }
3567 else
3568 {
3569 /* IT->string is not an overlay string. If we reached
3570 its end, and there is something on IT->stack, proceed
3571 with what is on the stack. This can be either another
3572 string, this time an overlay string, or a buffer. */
3573 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3574 && it->sp > 0)
3575 {
3576 pop_it (it);
3577 if (!STRINGP (it->string))
3578 it->method = next_element_from_buffer;
3579 }
3580 }
3581 }
3582 else if (it->method == next_element_from_image
3583 || it->method == next_element_from_stretch)
3584 {
3585 /* The position etc with which we have to proceed are on
3586 the stack. The position may be at the end of a string,
3587 if the `display' property takes up the whole string. */
3588 pop_it (it);
3589 it->image_id = 0;
3590 if (STRINGP (it->string))
3591 {
3592 it->method = next_element_from_string;
3593 goto consider_string_end;
3594 }
3595 else
3596 it->method = next_element_from_buffer;
3597 }
3598 else
3599 /* There are no other methods defined, so this should be a bug. */
3600 abort ();
3601
3602 /* Reset flags indicating start and end of a sequence of
3603 characters with box. */
3604 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3605
3606 xassert (it->method != next_element_from_string
3607 || (STRINGP (it->string)
3608 && IT_STRING_CHARPOS (*it) >= 0));
3609}
3610
3611
3612/* Load IT's display element fields with information about the next
3613 display element which comes from a display table entry or from the
3614 result of translating a control character to one of the forms `^C'
3615 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3616
3617static int
3618next_element_from_display_vector (it)
3619 struct it *it;
3620{
3621 /* Precondition. */
3622 xassert (it->dpvec && it->current.dpvec_index >= 0);
3623
3624 /* Remember the current face id in case glyphs specify faces.
3625 IT's face is restored in set_iterator_to_next. */
3626 it->saved_face_id = it->face_id;
3627
3628 if (INTEGERP (*it->dpvec)
3629 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3630 {
3631 int lface_id;
3632 GLYPH g;
3633
3634 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3635 it->c = FAST_GLYPH_CHAR (g);
3636 it->len = CHAR_LEN (it->c);
3637
3638 /* The entry may contain a face id to use. Such a face id is
3639 the id of a Lisp face, not a realized face. A face id of
3640 zero means no face. */
3641 lface_id = FAST_GLYPH_FACE (g);
3642 if (lface_id)
3643 {
3644 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3645 if (face_id >= 0)
3646 {
3647 it->face_id = face_id;
5f5c8ee5
GM
3648 }
3649 }
3650 }
3651 else
3652 /* Display table entry is invalid. Return a space. */
3653 it->c = ' ', it->len = 1;
3654
3655 /* Don't change position and object of the iterator here. They are
3656 still the values of the character that had this display table
3657 entry or was translated, and that's what we want. */
3658 it->what = IT_CHARACTER;
3659 return 1;
3660}
3661
3662
3663/* Load IT with the next display element from Lisp string IT->string.
3664 IT->current.string_pos is the current position within the string.
3665 If IT->current.overlay_string_index >= 0, the Lisp string is an
3666 overlay string. */
3667
3668static int
3669next_element_from_string (it)
3670 struct it *it;
3671{
3672 struct text_pos position;
3673
3674 xassert (STRINGP (it->string));
3675 xassert (IT_STRING_CHARPOS (*it) >= 0);
3676 position = it->current.string_pos;
3677
3678 /* Time to check for invisible text? */
3679 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3680 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3681 {
3682 handle_stop (it);
3683
3684 /* Since a handler may have changed IT->method, we must
3685 recurse here. */
3686 return get_next_display_element (it);
3687 }
3688
3689 if (it->current.overlay_string_index >= 0)
3690 {
3691 /* Get the next character from an overlay string. In overlay
3692 strings, There is no field width or padding with spaces to
3693 do. */
3694 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3695 {
3696 it->what = IT_EOB;
3697 return 0;
3698 }
3699 else if (STRING_MULTIBYTE (it->string))
3700 {
3701 int remaining = (STRING_BYTES (XSTRING (it->string))
3702 - IT_STRING_BYTEPOS (*it));
3703 unsigned char *s = (XSTRING (it->string)->data
3704 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3705 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
3706 }
3707 else
3708 {
3709 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3710 it->len = 1;
3711 }
3712 }
3713 else
3714 {
3715 /* Get the next character from a Lisp string that is not an
3716 overlay string. Such strings come from the mode line, for
3717 example. We may have to pad with spaces, or truncate the
3718 string. See also next_element_from_c_string. */
3719 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3720 {
3721 it->what = IT_EOB;
3722 return 0;
3723 }
3724 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3725 {
3726 /* Pad with spaces. */
3727 it->c = ' ', it->len = 1;
3728 CHARPOS (position) = BYTEPOS (position) = -1;
3729 }
3730 else if (STRING_MULTIBYTE (it->string))
3731 {
3732 int maxlen = (STRING_BYTES (XSTRING (it->string))
3733 - IT_STRING_BYTEPOS (*it));
3734 unsigned char *s = (XSTRING (it->string)->data
3735 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3736 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
3737 }
3738 else
3739 {
3740 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3741 it->len = 1;
3742 }
3743 }
3744
3745 /* Record what we have and where it came from. Note that we store a
3746 buffer position in IT->position although it could arguably be a
3747 string position. */
3748 it->what = IT_CHARACTER;
3749 it->object = it->string;
3750 it->position = position;
3751 return 1;
3752}
3753
3754
3755/* Load IT with next display element from C string IT->s.
3756 IT->string_nchars is the maximum number of characters to return
3757 from the string. IT->end_charpos may be greater than
3758 IT->string_nchars when this function is called, in which case we
3759 may have to return padding spaces. Value is zero if end of string
3760 reached, including padding spaces. */
3761
3762static int
3763next_element_from_c_string (it)
3764 struct it *it;
3765{
3766 int success_p = 1;
3767
3768 xassert (it->s);
3769 it->what = IT_CHARACTER;
3770 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3771 it->object = Qnil;
3772
3773 /* IT's position can be greater IT->string_nchars in case a field
3774 width or precision has been specified when the iterator was
3775 initialized. */
3776 if (IT_CHARPOS (*it) >= it->end_charpos)
3777 {
3778 /* End of the game. */
3779 it->what = IT_EOB;
3780 success_p = 0;
3781 }
3782 else if (IT_CHARPOS (*it) >= it->string_nchars)
3783 {
3784 /* Pad with spaces. */
3785 it->c = ' ', it->len = 1;
3786 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3787 }
3788 else if (it->multibyte_p)
3789 {
3790 /* Implementation note: The calls to strlen apparently aren't a
3791 performance problem because there is no noticeable performance
3792 difference between Emacs running in unibyte or multibyte mode. */
3793 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
3794 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3795 maxlen, &it->len);
5f5c8ee5
GM
3796 }
3797 else
3798 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3799
3800 return success_p;
3801}
3802
3803
3804/* Set up IT to return characters from an ellipsis, if appropriate.
3805 The definition of the ellipsis glyphs may come from a display table
3806 entry. This function Fills IT with the first glyph from the
3807 ellipsis if an ellipsis is to be displayed. */
3808
13f19968 3809static int
5f5c8ee5
GM
3810next_element_from_ellipsis (it)
3811 struct it *it;
3812{
13f19968 3813 if (it->selective_display_ellipsis_p)
5f5c8ee5 3814 {
13f19968
GM
3815 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3816 {
3817 /* Use the display table definition for `...'. Invalid glyphs
3818 will be handled by the method returning elements from dpvec. */
3819 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3820 it->dpvec_char_len = it->len;
3821 it->dpvec = v->contents;
3822 it->dpend = v->contents + v->size;
3823 it->current.dpvec_index = 0;
3824 it->method = next_element_from_display_vector;
3825 }
3826 else
3827 {
3828 /* Use default `...' which is stored in default_invis_vector. */
3829 it->dpvec_char_len = it->len;
3830 it->dpvec = default_invis_vector;
3831 it->dpend = default_invis_vector + 3;
3832 it->current.dpvec_index = 0;
3833 it->method = next_element_from_display_vector;
3834 }
5f5c8ee5 3835 }
13f19968
GM
3836 else
3837 reseat_at_next_visible_line_start (it, 1);
3838
3839 return get_next_display_element (it);
5f5c8ee5
GM
3840}
3841
3842
3843/* Deliver an image display element. The iterator IT is already
3844 filled with image information (done in handle_display_prop). Value
3845 is always 1. */
3846
3847
3848static int
3849next_element_from_image (it)
3850 struct it *it;
3851{
3852 it->what = IT_IMAGE;
3853 return 1;
3854}
3855
3856
3857/* Fill iterator IT with next display element from a stretch glyph
3858 property. IT->object is the value of the text property. Value is
3859 always 1. */
3860
3861static int
3862next_element_from_stretch (it)
3863 struct it *it;
3864{
3865 it->what = IT_STRETCH;
3866 return 1;
3867}
3868
3869
3870/* Load IT with the next display element from current_buffer. Value
3871 is zero if end of buffer reached. IT->stop_charpos is the next
3872 position at which to stop and check for text properties or buffer
3873 end. */
3874
3875static int
3876next_element_from_buffer (it)
3877 struct it *it;
3878{
3879 int success_p = 1;
3880
3881 /* Check this assumption, otherwise, we would never enter the
3882 if-statement, below. */
3883 xassert (IT_CHARPOS (*it) >= BEGV
3884 && IT_CHARPOS (*it) <= it->stop_charpos);
3885
3886 if (IT_CHARPOS (*it) >= it->stop_charpos)
3887 {
3888 if (IT_CHARPOS (*it) >= it->end_charpos)
3889 {
3890 int overlay_strings_follow_p;
3891
3892 /* End of the game, except when overlay strings follow that
3893 haven't been returned yet. */
3894 if (it->overlay_strings_at_end_processed_p)
3895 overlay_strings_follow_p = 0;
3896 else
3897 {
3898 it->overlay_strings_at_end_processed_p = 1;
c880678e 3899 overlay_strings_follow_p = get_overlay_strings (it);
5f5c8ee5
GM
3900 }
3901
3902 if (overlay_strings_follow_p)
3903 success_p = get_next_display_element (it);
3904 else
3905 {
3906 it->what = IT_EOB;
3907 it->position = it->current.pos;
3908 success_p = 0;
3909 }
3910 }
3911 else
3912 {
3913 handle_stop (it);
3914 return get_next_display_element (it);
3915 }
3916 }
3917 else
3918 {
3919 /* No face changes, overlays etc. in sight, so just return a
3920 character from current_buffer. */
3921 unsigned char *p;
3922
3923 /* Maybe run the redisplay end trigger hook. Performance note:
3924 This doesn't seem to cost measurable time. */
3925 if (it->redisplay_end_trigger_charpos
3926 && it->glyph_row
3927 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3928 run_redisplay_end_trigger_hook (it);
3929
3930 /* Get the next character, maybe multibyte. */
3931 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 3932 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
3933 {
3934 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3935 - IT_BYTEPOS (*it));
4fdb80f2 3936 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
3937 }
3938 else
3939 it->c = *p, it->len = 1;
3940
3941 /* Record what we have and where it came from. */
3942 it->what = IT_CHARACTER;;
3943 it->object = it->w->buffer;
3944 it->position = it->current.pos;
3945
3946 /* Normally we return the character found above, except when we
3947 really want to return an ellipsis for selective display. */
3948 if (it->selective)
3949 {
3950 if (it->c == '\n')
3951 {
3952 /* A value of selective > 0 means hide lines indented more
3953 than that number of columns. */
3954 if (it->selective > 0
3955 && IT_CHARPOS (*it) + 1 < ZV
3956 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3957 IT_BYTEPOS (*it) + 1,
3958 it->selective))
312246d1 3959 {
13f19968 3960 success_p = next_element_from_ellipsis (it);
312246d1
GM
3961 it->dpvec_char_len = -1;
3962 }
5f5c8ee5
GM
3963 }
3964 else if (it->c == '\r' && it->selective == -1)
3965 {
3966 /* A value of selective == -1 means that everything from the
3967 CR to the end of the line is invisible, with maybe an
3968 ellipsis displayed for it. */
13f19968 3969 success_p = next_element_from_ellipsis (it);
312246d1 3970 it->dpvec_char_len = -1;
5f5c8ee5
GM
3971 }
3972 }
3973 }
3974
3975 /* Value is zero if end of buffer reached. */
c880678e 3976 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
3977 return success_p;
3978}
3979
3980
3981/* Run the redisplay end trigger hook for IT. */
3982
3983static void
3984run_redisplay_end_trigger_hook (it)
3985 struct it *it;
3986{
3987 Lisp_Object args[3];
3988
3989 /* IT->glyph_row should be non-null, i.e. we should be actually
3990 displaying something, or otherwise we should not run the hook. */
3991 xassert (it->glyph_row);
3992
3993 /* Set up hook arguments. */
3994 args[0] = Qredisplay_end_trigger_functions;
3995 args[1] = it->window;
3996 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3997 it->redisplay_end_trigger_charpos = 0;
3998
3999 /* Since we are *trying* to run these functions, don't try to run
4000 them again, even if they get an error. */
4001 it->w->redisplay_end_trigger = Qnil;
4002 Frun_hook_with_args (3, args);
4003
4004 /* Notice if it changed the face of the character we are on. */
4005 handle_face_prop (it);
4006}
4007
4008
260a86a0
KH
4009/* Deliver a composition display element. The iterator IT is already
4010 filled with composition information (done in
4011 handle_composition_prop). Value is always 1. */
4012
4013static int
4014next_element_from_composition (it)
4015 struct it *it;
4016{
4017 it->what = IT_COMPOSITION;
4018 it->position = (STRINGP (it->string)
4019 ? it->current.string_pos
4020 : it->current.pos);
4021 return 1;
4022}
4023
4024
5f5c8ee5
GM
4025\f
4026/***********************************************************************
4027 Moving an iterator without producing glyphs
4028 ***********************************************************************/
4029
4030/* Move iterator IT to a specified buffer or X position within one
4031 line on the display without producing glyphs.
4032
4033 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4034 whichever is reached first.
4035
4036 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4037
4038 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4039 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4040 TO_X includes the amount by which a window is horizontally
4041 scrolled.
4042
4043 Value is
4044
4045 MOVE_POS_MATCH_OR_ZV
4046 - when TO_POS or ZV was reached.
4047
4048 MOVE_X_REACHED
4049 -when TO_X was reached before TO_POS or ZV were reached.
4050
4051 MOVE_LINE_CONTINUED
4052 - when we reached the end of the display area and the line must
4053 be continued.
4054
4055 MOVE_LINE_TRUNCATED
4056 - when we reached the end of the display area and the line is
4057 truncated.
4058
4059 MOVE_NEWLINE_OR_CR
4060 - when we stopped at a line end, i.e. a newline or a CR and selective
4061 display is on. */
4062
701552dd 4063static enum move_it_result
5f5c8ee5
GM
4064move_it_in_display_line_to (it, to_charpos, to_x, op)
4065 struct it *it;
4066 int to_charpos, to_x, op;
4067{
4068 enum move_it_result result = MOVE_UNDEFINED;
4069 struct glyph_row *saved_glyph_row;
4070
4071 /* Don't produce glyphs in produce_glyphs. */
4072 saved_glyph_row = it->glyph_row;
4073 it->glyph_row = NULL;
4074
5f5c8ee5
GM
4075 while (1)
4076 {
4077 int x, i;
4078
4079 /* Stop when ZV or TO_CHARPOS reached. */
4080 if (!get_next_display_element (it)
4081 || ((op & MOVE_TO_POS) != 0
4082 && BUFFERP (it->object)
4083 && IT_CHARPOS (*it) >= to_charpos))
4084 {
4085 result = MOVE_POS_MATCH_OR_ZV;
4086 break;
4087 }
4088
4089 /* The call to produce_glyphs will get the metrics of the
4090 display element IT is loaded with. We record in x the
4091 x-position before this display element in case it does not
4092 fit on the line. */
4093 x = it->current_x;
4094 PRODUCE_GLYPHS (it);
4095
4096 if (it->area != TEXT_AREA)
4097 {
4098 set_iterator_to_next (it);
4099 continue;
4100 }
4101
4102 /* The number of glyphs we get back in IT->nglyphs will normally
4103 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4104 character on a terminal frame, or (iii) a line end. For the
4105 second case, IT->nglyphs - 1 padding glyphs will be present
4106 (on X frames, there is only one glyph produced for a
4107 composite character.
4108
4109 The behavior implemented below means, for continuation lines,
4110 that as many spaces of a TAB as fit on the current line are
4111 displayed there. For terminal frames, as many glyphs of a
4112 multi-glyph character are displayed in the current line, too.
4113 This is what the old redisplay code did, and we keep it that
4114 way. Under X, the whole shape of a complex character must
4115 fit on the line or it will be completely displayed in the
4116 next line.
4117
4118 Note that both for tabs and padding glyphs, all glyphs have
4119 the same width. */
4120 if (it->nglyphs)
4121 {
4122 /* More than one glyph or glyph doesn't fit on line. All
4123 glyphs have the same width. */
4124 int single_glyph_width = it->pixel_width / it->nglyphs;
4125 int new_x;
4126
4127 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4128 {
4129 new_x = x + single_glyph_width;
4130
4131 /* We want to leave anything reaching TO_X to the caller. */
4132 if ((op & MOVE_TO_X) && new_x > to_x)
4133 {
4134 it->current_x = x;
4135 result = MOVE_X_REACHED;
4136 break;
4137 }
4138 else if (/* Lines are continued. */
4139 !it->truncate_lines_p
4140 && (/* And glyph doesn't fit on the line. */
4141 new_x > it->last_visible_x
4142 /* Or it fits exactly and we're on a window
4143 system frame. */
4144 || (new_x == it->last_visible_x
4145 && FRAME_WINDOW_P (it->f))))
4146 {
4147 if (/* IT->hpos == 0 means the very first glyph
4148 doesn't fit on the line, e.g. a wide image. */
4149 it->hpos == 0
4150 || (new_x == it->last_visible_x
4151 && FRAME_WINDOW_P (it->f)))
4152 {
4153 ++it->hpos;
4154 it->current_x = new_x;
4155 if (i == it->nglyphs - 1)
4156 set_iterator_to_next (it);
4157 }
4158 else
4159 it->current_x = x;
4160
4161 result = MOVE_LINE_CONTINUED;
4162 break;
4163 }
4164 else if (new_x > it->first_visible_x)
4165 {
4166 /* Glyph is visible. Increment number of glyphs that
4167 would be displayed. */
4168 ++it->hpos;
4169 }
4170 else
4171 {
4172 /* Glyph is completely off the left margin of the display
4173 area. Nothing to do. */
4174 }
4175 }
4176
4177 if (result != MOVE_UNDEFINED)
4178 break;
4179 }
4180 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4181 {
4182 /* Stop when TO_X specified and reached. This check is
4183 necessary here because of lines consisting of a line end,
4184 only. The line end will not produce any glyphs and we
4185 would never get MOVE_X_REACHED. */
4186 xassert (it->nglyphs == 0);
4187 result = MOVE_X_REACHED;
4188 break;
4189 }
4190
4191 /* Is this a line end? If yes, we're done. */
4192 if (ITERATOR_AT_END_OF_LINE_P (it))
4193 {
4194 result = MOVE_NEWLINE_OR_CR;
4195 break;
4196 }
4197
4198 /* The current display element has been consumed. Advance
4199 to the next. */
4200 set_iterator_to_next (it);
4201
4202 /* Stop if lines are truncated and IT's current x-position is
4203 past the right edge of the window now. */
4204 if (it->truncate_lines_p
4205 && it->current_x >= it->last_visible_x)
4206 {
4207 result = MOVE_LINE_TRUNCATED;
4208 break;
4209 }
4210 }
4211
4212 /* Restore the iterator settings altered at the beginning of this
4213 function. */
4214 it->glyph_row = saved_glyph_row;
4215 return result;
4216}
4217
4218
4219/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4220 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4221 the description of enum move_operation_enum.
4222
4223 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4224 screen line, this function will set IT to the next position >
4225 TO_CHARPOS. */
4226
4227void
4228move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4229 struct it *it;
4230 int to_charpos, to_x, to_y, to_vpos;
4231 int op;
4232{
4233 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4234 int line_height;
4235
5f5c8ee5
GM
4236 while (1)
4237 {
4238 if (op & MOVE_TO_VPOS)
4239 {
4240 /* If no TO_CHARPOS and no TO_X specified, stop at the
4241 start of the line TO_VPOS. */
4242 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4243 {
4244 if (it->vpos == to_vpos)
4245 break;
4246 skip = move_it_in_display_line_to (it, -1, -1, 0);
4247 }
4248 else
4249 {
4250 /* TO_VPOS >= 0 means stop at TO_X in the line at
4251 TO_VPOS, or at TO_POS, whichever comes first. */
4252 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4253
4254 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4255 break;
4256 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4257 {
4258 /* We have reached TO_X but not in the line we want. */
4259 skip = move_it_in_display_line_to (it, to_charpos,
4260 -1, MOVE_TO_POS);
4261 if (skip == MOVE_POS_MATCH_OR_ZV)
4262 break;
4263 }
4264 }
4265 }
4266 else if (op & MOVE_TO_Y)
4267 {
4268 struct it it_backup;
4269 int done_p;
4270
4271 /* TO_Y specified means stop at TO_X in the line containing
4272 TO_Y---or at TO_CHARPOS if this is reached first. The
4273 problem is that we can't really tell whether the line
4274 contains TO_Y before we have completely scanned it, and
4275 this may skip past TO_X. What we do is to first scan to
4276 TO_X.
4277
4278 If TO_X is not specified, use a TO_X of zero. The reason
4279 is to make the outcome of this function more predictable.
4280 If we didn't use TO_X == 0, we would stop at the end of
4281 the line which is probably not what a caller would expect
4282 to happen. */
4283 skip = move_it_in_display_line_to (it, to_charpos,
4284 ((op & MOVE_TO_X)
4285 ? to_x : 0),
4286 (MOVE_TO_X
4287 | (op & MOVE_TO_POS)));
4288
4289 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4290 if (skip == MOVE_POS_MATCH_OR_ZV)
4291 break;
4292
4293 /* If TO_X was reached, we would like to know whether TO_Y
4294 is in the line. This can only be said if we know the
4295 total line height which requires us to scan the rest of
4296 the line. */
4297 done_p = 0;
4298 if (skip == MOVE_X_REACHED)
4299 {
4300 it_backup = *it;
4301 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4302 op & MOVE_TO_POS);
4303 }
4304
4305 /* Now, decide whether TO_Y is in this line. */
4306 line_height = it->max_ascent + it->max_descent;
4307
4308 if (to_y >= it->current_y
4309 && to_y < it->current_y + line_height)
4310 {
4311 if (skip == MOVE_X_REACHED)
4312 /* If TO_Y is in this line and TO_X was reached above,
4313 we scanned too far. We have to restore IT's settings
4314 to the ones before skipping. */
4315 *it = it_backup;
4316 done_p = 1;
4317 }
4318 else if (skip == MOVE_X_REACHED)
4319 {
4320 skip = skip2;
4321 if (skip == MOVE_POS_MATCH_OR_ZV)
4322 done_p = 1;
4323 }
4324
4325 if (done_p)
4326 break;
4327 }
4328 else
4329 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4330
4331 switch (skip)
4332 {
4333 case MOVE_POS_MATCH_OR_ZV:
4334 return;
4335
4336 case MOVE_NEWLINE_OR_CR:
4337 set_iterator_to_next (it);
4338 it->continuation_lines_width = 0;
4339 break;
4340
4341 case MOVE_LINE_TRUNCATED:
4342 it->continuation_lines_width = 0;
312246d1 4343 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4344 if ((op & MOVE_TO_POS) != 0
4345 && IT_CHARPOS (*it) > to_charpos)
4346 goto out;
4347 break;
4348
4349 case MOVE_LINE_CONTINUED:
4350 it->continuation_lines_width += it->current_x;
4351 break;
4352
4353 default:
4354 abort ();
4355 }
4356
4357 /* Reset/increment for the next run. */
4358 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4359 it->current_x = it->hpos = 0;
4360 it->current_y += it->max_ascent + it->max_descent;
4361 ++it->vpos;
4362 last_height = it->max_ascent + it->max_descent;
4363 last_max_ascent = it->max_ascent;
4364 it->max_ascent = it->max_descent = 0;
4365 }
4366 out:;
4367}
4368
4369
4370/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4371
4372 If DY > 0, move IT backward at least that many pixels. DY = 0
4373 means move IT backward to the preceding line start or BEGV. This
4374 function may move over more than DY pixels if IT->current_y - DY
4375 ends up in the middle of a line; in this case IT->current_y will be
4376 set to the top of the line moved to. */
4377
4378void
4379move_it_vertically_backward (it, dy)
4380 struct it *it;
4381 int dy;
4382{
4383 int nlines, h, line_height;
4384 struct it it2;
4385 int start_pos = IT_CHARPOS (*it);
4386
4387 xassert (dy >= 0);
4388
4389 /* Estimate how many newlines we must move back. */
4390 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4391
4392 /* Set the iterator's position that many lines back. */
4393 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4394 back_to_previous_visible_line_start (it);
4395
4396 /* Reseat the iterator here. When moving backward, we don't want
4397 reseat to skip forward over invisible text, set up the iterator
4398 to deliver from overlay strings at the new position etc. So,
4399 use reseat_1 here. */
4400 reseat_1 (it, it->current.pos, 1);
4401
4402 /* We are now surely at a line start. */
4403 it->current_x = it->hpos = 0;
4404
4405 /* Move forward and see what y-distance we moved. First move to the
4406 start of the next line so that we get its height. We need this
4407 height to be able to tell whether we reached the specified
4408 y-distance. */
4409 it2 = *it;
4410 it2.max_ascent = it2.max_descent = 0;
4411 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4412 MOVE_TO_POS | MOVE_TO_VPOS);
4413 xassert (IT_CHARPOS (*it) >= BEGV);
4414 line_height = it2.max_ascent + it2.max_descent;
4415 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4416 xassert (IT_CHARPOS (*it) >= BEGV);
4417 h = it2.current_y - it->current_y;
4418 nlines = it2.vpos - it->vpos;
4419
4420 /* Correct IT's y and vpos position. */
4421 it->vpos -= nlines;
4422 it->current_y -= h;
4423
4424 if (dy == 0)
4425 {
4426 /* DY == 0 means move to the start of the screen line. The
4427 value of nlines is > 0 if continuation lines were involved. */
4428 if (nlines > 0)
4429 move_it_by_lines (it, nlines, 1);
4430 xassert (IT_CHARPOS (*it) <= start_pos);
4431 }
4432 else if (nlines)
4433 {
4434 /* The y-position we try to reach. Note that h has been
4435 subtracted in front of the if-statement. */
4436 int target_y = it->current_y + h - dy;
4437
4438 /* If we did not reach target_y, try to move further backward if
4439 we can. If we moved too far backward, try to move forward. */
4440 if (target_y < it->current_y
4441 && IT_CHARPOS (*it) > BEGV)
4442 {
4443 move_it_vertically (it, target_y - it->current_y);
4444 xassert (IT_CHARPOS (*it) >= BEGV);
4445 }
4446 else if (target_y >= it->current_y + line_height
4447 && IT_CHARPOS (*it) < ZV)
4448 {
4449 move_it_vertically (it, target_y - (it->current_y + line_height));
4450 xassert (IT_CHARPOS (*it) >= BEGV);
4451 }
4452 }
4453}
4454
4455
4456/* Move IT by a specified amount of pixel lines DY. DY negative means
4457 move backwards. DY = 0 means move to start of screen line. At the
4458 end, IT will be on the start of a screen line. */
4459
4460void
4461move_it_vertically (it, dy)
4462 struct it *it;
4463 int dy;
4464{
4465 if (dy <= 0)
4466 move_it_vertically_backward (it, -dy);
4467 else if (dy > 0)
4468 {
4469 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4470 MOVE_TO_POS | MOVE_TO_Y);
4471
4472 /* If buffer ends in ZV without a newline, move to the start of
4473 the line to satisfy the post-condition. */
4474 if (IT_CHARPOS (*it) == ZV
4475 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4476 move_it_by_lines (it, 0, 0);
4477 }
4478}
4479
4480
4481/* Return non-zero if some text between buffer positions START_CHARPOS
4482 and END_CHARPOS is invisible. IT->window is the window for text
4483 property lookup. */
4484
4485static int
4486invisible_text_between_p (it, start_charpos, end_charpos)
4487 struct it *it;
4488 int start_charpos, end_charpos;
4489{
5f5c8ee5
GM
4490 Lisp_Object prop, limit;
4491 int invisible_found_p;
4492
4493 xassert (it != NULL && start_charpos <= end_charpos);
4494
4495 /* Is text at START invisible? */
4496 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4497 it->window);
4498 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4499 invisible_found_p = 1;
4500 else
4501 {
6c577098
GM
4502 limit = next_single_char_property_change (make_number (start_charpos),
4503 Qinvisible, Qnil,
4504 make_number (end_charpos));
5f5c8ee5
GM
4505 invisible_found_p = XFASTINT (limit) < end_charpos;
4506 }
4507
4508 return invisible_found_p;
5f5c8ee5
GM
4509}
4510
4511
4512/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4513 negative means move up. DVPOS == 0 means move to the start of the
4514 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4515 NEED_Y_P is zero, IT->current_y will be left unchanged.
4516
4517 Further optimization ideas: If we would know that IT->f doesn't use
4518 a face with proportional font, we could be faster for
4519 truncate-lines nil. */
4520
4521void
4522move_it_by_lines (it, dvpos, need_y_p)
4523 struct it *it;
4524 int dvpos, need_y_p;
4525{
4526 struct position pos;
4527
4528 if (!FRAME_WINDOW_P (it->f))
4529 {
4530 struct text_pos textpos;
4531
4532 /* We can use vmotion on frames without proportional fonts. */
4533 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4534 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4535 reseat (it, textpos, 1);
4536 it->vpos += pos.vpos;
4537 it->current_y += pos.vpos;
4538 }
4539 else if (dvpos == 0)
4540 {
4541 /* DVPOS == 0 means move to the start of the screen line. */
4542 move_it_vertically_backward (it, 0);
4543 xassert (it->current_x == 0 && it->hpos == 0);
4544 }
4545 else if (dvpos > 0)
4546 {
4547 /* If there are no continuation lines, and if there is no
4548 selective display, try the simple method of moving forward
4549 DVPOS newlines, then see where we are. */
4550 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4551 {
4552 int shortage = 0, charpos;
4553
4554 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4555 charpos = IT_CHARPOS (*it) + 1;
4556 else
4557 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4558 &shortage, 0);
4559
4560 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4561 {
4562 struct text_pos pos;
4563 CHARPOS (pos) = charpos;
4564 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4565 reseat (it, pos, 1);
4566 it->vpos += dvpos - shortage;
4567 it->hpos = it->current_x = 0;
4568 return;
4569 }
4570 }
4571
4572 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4573 }
4574 else
4575 {
4576 struct it it2;
4577 int start_charpos, i;
4578
4579 /* If there are no continuation lines, and if there is no
4580 selective display, try the simple method of moving backward
4581 -DVPOS newlines. */
4582 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4583 {
4584 int shortage;
4585 int charpos = IT_CHARPOS (*it);
4586 int bytepos = IT_BYTEPOS (*it);
4587
4588 /* If in the middle of a line, go to its start. */
4589 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4590 {
4591 charpos = find_next_newline_no_quit (charpos, -1);
4592 bytepos = CHAR_TO_BYTE (charpos);
4593 }
4594
4595 if (charpos == BEGV)
4596 {
4597 struct text_pos pos;
4598 CHARPOS (pos) = charpos;
4599 BYTEPOS (pos) = bytepos;
4600 reseat (it, pos, 1);
4601 it->hpos = it->current_x = 0;
4602 return;
4603 }
4604 else
4605 {
4606 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4607 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4608 {
4609 struct text_pos pos;
4610 CHARPOS (pos) = charpos;
4611 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4612 reseat (it, pos, 1);
4613 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4614 it->hpos = it->current_x = 0;
4615 return;
4616 }
4617 }
4618 }
4619
4620 /* Go back -DVPOS visible lines and reseat the iterator there. */
4621 start_charpos = IT_CHARPOS (*it);
4622 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4623 back_to_previous_visible_line_start (it);
4624 reseat (it, it->current.pos, 1);
4625 it->current_x = it->hpos = 0;
4626
4627 /* Above call may have moved too far if continuation lines
4628 are involved. Scan forward and see if it did. */
4629 it2 = *it;
4630 it2.vpos = it2.current_y = 0;
4631 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4632 it->vpos -= it2.vpos;
4633 it->current_y -= it2.current_y;
4634 it->current_x = it->hpos = 0;
4635
4636 /* If we moved too far, move IT some lines forward. */
4637 if (it2.vpos > -dvpos)
4638 {
4639 int delta = it2.vpos + dvpos;
4640 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4641 }
4642 }
4643}
4644
4645
4646\f
4647/***********************************************************************
4648 Messages
4649 ***********************************************************************/
4650
4651
937248bc
GM
4652/* Add a message with format string FORMAT and arguments ARG1 and ARG2
4653 to *Messages*. */
4654
4655void
4656add_to_log (format, arg1, arg2)
4657 char *format;
4658 Lisp_Object arg1, arg2;
4659{
4660 Lisp_Object args[3];
4661 Lisp_Object msg, fmt;
4662 char *buffer;
4663 int len;
4664 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4665
4666 fmt = msg = Qnil;
4667 GCPRO4 (fmt, msg, arg1, arg2);
4668
4669 args[0] = fmt = build_string (format);
4670 args[1] = arg1;
4671 args[2] = arg2;
4672 msg = Fformat (make_number (3), args);
4673
4674 len = STRING_BYTES (XSTRING (msg)) + 1;
4675 buffer = (char *) alloca (len);
4676 strcpy (buffer, XSTRING (msg)->data);
4677
4678 message_dolog (buffer, len, 1, 0);
4679 UNGCPRO;
4680}
4681
4682
5f5c8ee5
GM
4683/* Output a newline in the *Messages* buffer if "needs" one. */
4684
4685void
4686message_log_maybe_newline ()
4687{
4688 if (message_log_need_newline)
4689 message_dolog ("", 0, 1, 0);
4690}
4691
4692
4693/* Add a string M of length LEN to the message log, optionally
4694 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4695 nonzero, means interpret the contents of M as multibyte. This
4696 function calls low-level routines in order to bypass text property
4697 hooks, etc. which might not be safe to run. */
4698
4699void
4700message_dolog (m, len, nlflag, multibyte)
4701 char *m;
4702 int len, nlflag, multibyte;
4703{
4704 if (!NILP (Vmessage_log_max))
4705 {
4706 struct buffer *oldbuf;
4707 Lisp_Object oldpoint, oldbegv, oldzv;
4708 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4709 int point_at_end = 0;
4710 int zv_at_end = 0;
4711 Lisp_Object old_deactivate_mark, tem;
4712 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4713
4714 old_deactivate_mark = Vdeactivate_mark;
4715 oldbuf = current_buffer;
4716 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4717 current_buffer->undo_list = Qt;
4718
4719 oldpoint = Fpoint_marker ();
4720 oldbegv = Fpoint_min_marker ();
4721 oldzv = Fpoint_max_marker ();
4722 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4723
4724 if (PT == Z)
4725 point_at_end = 1;
4726 if (ZV == Z)
4727 zv_at_end = 1;
4728
4729 BEGV = BEG;
4730 BEGV_BYTE = BEG_BYTE;
4731 ZV = Z;
4732 ZV_BYTE = Z_BYTE;
4733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4734
4735 /* Insert the string--maybe converting multibyte to single byte
4736 or vice versa, so that all the text fits the buffer. */
4737 if (multibyte
4738 && NILP (current_buffer->enable_multibyte_characters))
4739 {
4740 int i, c, nbytes;
4741 unsigned char work[1];
4742
4743 /* Convert a multibyte string to single-byte
4744 for the *Message* buffer. */
4745 for (i = 0; i < len; i += nbytes)
4746 {
4fdb80f2 4747 c = string_char_and_length (m + i, len - i, &nbytes);
5f5c8ee5
GM
4748 work[0] = (SINGLE_BYTE_CHAR_P (c)
4749 ? c
4750 : multibyte_char_to_unibyte (c, Qnil));
4751 insert_1_both (work, 1, 1, 1, 0, 0);
4752 }
4753 }
4754 else if (! multibyte
4755 && ! NILP (current_buffer->enable_multibyte_characters))
4756 {
4757 int i, c, nbytes;
4758 unsigned char *msg = (unsigned char *) m;
260a86a0 4759 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
4760 /* Convert a single-byte string to multibyte
4761 for the *Message* buffer. */
4762 for (i = 0; i < len; i++)
4763 {
4764 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
4765 nbytes = CHAR_STRING (c, str);
4766 insert_1_both (str, 1, nbytes, 1, 0, 0);
5f5c8ee5
GM
4767 }
4768 }
4769 else if (len)
4770 insert_1 (m, len, 1, 0, 0);
4771
4772 if (nlflag)
4773 {
4774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4775 insert_1 ("\n", 1, 1, 0, 0);
4776
4777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4778 this_bol = PT;
4779 this_bol_byte = PT_BYTE;
4780
4781 if (this_bol > BEG)
4782 {
4783 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4784 prev_bol = PT;
4785 prev_bol_byte = PT_BYTE;
4786
4787 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4788 this_bol, this_bol_byte);
4789 if (dup)
4790 {
4791 del_range_both (prev_bol, prev_bol_byte,
4792 this_bol, this_bol_byte, 0);
4793 if (dup > 1)
4794 {
4795 char dupstr[40];
4796 int duplen;
4797
4798 /* If you change this format, don't forget to also
4799 change message_log_check_duplicate. */
4800 sprintf (dupstr, " [%d times]", dup);
4801 duplen = strlen (dupstr);
4802 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4803 insert_1 (dupstr, duplen, 1, 0, 1);
4804 }
4805 }
4806 }
4807
4808 if (NATNUMP (Vmessage_log_max))
4809 {
4810 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4811 -XFASTINT (Vmessage_log_max) - 1, 0);
4812 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4813 }
4814 }
4815 BEGV = XMARKER (oldbegv)->charpos;
4816 BEGV_BYTE = marker_byte_position (oldbegv);
4817
4818 if (zv_at_end)
4819 {
4820 ZV = Z;
4821 ZV_BYTE = Z_BYTE;
4822 }
4823 else
4824 {
4825 ZV = XMARKER (oldzv)->charpos;
4826 ZV_BYTE = marker_byte_position (oldzv);
4827 }
4828
4829 if (point_at_end)
4830 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4831 else
4832 /* We can't do Fgoto_char (oldpoint) because it will run some
4833 Lisp code. */
4834 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4835 XMARKER (oldpoint)->bytepos);
4836
4837 UNGCPRO;
4838 free_marker (oldpoint);
4839 free_marker (oldbegv);
4840 free_marker (oldzv);
4841
4842 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4843 set_buffer_internal (oldbuf);
4844 if (NILP (tem))
4845 windows_or_buffers_changed = old_windows_or_buffers_changed;
4846 message_log_need_newline = !nlflag;
4847 Vdeactivate_mark = old_deactivate_mark;
4848 }
4849}
4850
4851
4852/* We are at the end of the buffer after just having inserted a newline.
4853 (Note: We depend on the fact we won't be crossing the gap.)
4854 Check to see if the most recent message looks a lot like the previous one.
4855 Return 0 if different, 1 if the new one should just replace it, or a
4856 value N > 1 if we should also append " [N times]". */
4857
4858static int
4859message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4860 int prev_bol, this_bol;
4861 int prev_bol_byte, this_bol_byte;
4862{
4863 int i;
4864 int len = Z_BYTE - 1 - this_bol_byte;
4865 int seen_dots = 0;
4866 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4867 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4868
4869 for (i = 0; i < len; i++)
4870 {
4871 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4872 && p1[i] != '\n')
4873 seen_dots = 1;
4874 if (p1[i] != p2[i])
4875 return seen_dots;
4876 }
4877 p1 += len;
4878 if (*p1 == '\n')
4879 return 2;
4880 if (*p1++ == ' ' && *p1++ == '[')
4881 {
4882 int n = 0;
4883 while (*p1 >= '0' && *p1 <= '9')
4884 n = n * 10 + *p1++ - '0';
4885 if (strncmp (p1, " times]\n", 8) == 0)
4886 return n+1;
4887 }
4888 return 0;
4889}
4890
4891
4892/* Display an echo area message M with a specified length of LEN
4893 chars. The string may include null characters. If M is 0, clear
4894 out any existing message, and let the mini-buffer text show through.
4895
4896 The buffer M must continue to exist until after the echo area gets
4897 cleared or some other message gets displayed there. This means do
4898 not pass text that is stored in a Lisp string; do not pass text in
4899 a buffer that was alloca'd. */
4900
4901void
4902message2 (m, len, multibyte)
4903 char *m;
4904 int len;
4905 int multibyte;
4906{
4907 /* First flush out any partial line written with print. */
4908 message_log_maybe_newline ();
4909 if (m)
4910 message_dolog (m, len, 1, multibyte);
4911 message2_nolog (m, len, multibyte);
4912}
4913
4914
4915/* The non-logging counterpart of message2. */
4916
4917void
4918message2_nolog (m, len, multibyte)
4919 char *m;
4920 int len;
4921{
886bd6f2 4922 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
4923 message_enable_multibyte = multibyte;
4924
4925 if (noninteractive)
4926 {
4927 if (noninteractive_need_newline)
4928 putc ('\n', stderr);
4929 noninteractive_need_newline = 0;
4930 if (m)
4931 fwrite (m, len, 1, stderr);
4932 if (cursor_in_echo_area == 0)
4933 fprintf (stderr, "\n");
4934 fflush (stderr);
4935 }
4936 /* A null message buffer means that the frame hasn't really been
4937 initialized yet. Error messages get reported properly by
4938 cmd_error, so this must be just an informative message; toss it. */
4939 else if (INTERACTIVE
886bd6f2
GM
4940 && sf->glyphs_initialized_p
4941 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
4942 {
4943 Lisp_Object mini_window;
4944 struct frame *f;
4945
4946 /* Get the frame containing the mini-buffer
4947 that the selected frame is using. */
886bd6f2 4948 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
4949 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4950
4951 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 4952 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
4953 && ! FRAME_VISIBLE_P (f))
4954 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4955
4956 if (m)
4957 {
c6e89d6c 4958 set_message (m, Qnil, len, multibyte);
5f5c8ee5
GM
4959 if (minibuffer_auto_raise)
4960 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4961 }
4962 else
c6e89d6c 4963 clear_message (1, 1);
5f5c8ee5 4964
c6e89d6c 4965 do_pending_window_change (0);
5f5c8ee5 4966 echo_area_display (1);
c6e89d6c 4967 do_pending_window_change (0);
5f5c8ee5
GM
4968 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4969 (*frame_up_to_date_hook) (f);
4970 }
4971}
4972
4973
c6e89d6c
GM
4974/* Display an echo area message M with a specified length of NBYTES
4975 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
4976 string, clear out any existing message, and let the mini-buffer
4977 text show through. */
4978
4979void
c6e89d6c 4980message3 (m, nbytes, multibyte)
5f5c8ee5 4981 Lisp_Object m;
c6e89d6c 4982 int nbytes;
5f5c8ee5
GM
4983 int multibyte;
4984{
4985 struct gcpro gcpro1;
4986
4987 GCPRO1 (m);
4988
4989 /* First flush out any partial line written with print. */
4990 message_log_maybe_newline ();
4991 if (STRINGP (m))
c6e89d6c
GM
4992 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4993 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
4994
4995 UNGCPRO;
4996}
4997
4998
4999/* The non-logging version of message3. */
5000
5001void
c6e89d6c 5002message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5003 Lisp_Object m;
c6e89d6c 5004 int nbytes, multibyte;
5f5c8ee5 5005{
886bd6f2 5006 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5007 message_enable_multibyte = multibyte;
5008
5009 if (noninteractive)
5010 {
5011 if (noninteractive_need_newline)
5012 putc ('\n', stderr);
5013 noninteractive_need_newline = 0;
5014 if (STRINGP (m))
c6e89d6c 5015 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5016 if (cursor_in_echo_area == 0)
5017 fprintf (stderr, "\n");
5018 fflush (stderr);
5019 }
5020 /* A null message buffer means that the frame hasn't really been
5021 initialized yet. Error messages get reported properly by
5022 cmd_error, so this must be just an informative message; toss it. */
5023 else if (INTERACTIVE
886bd6f2
GM
5024 && sf->glyphs_initialized_p
5025 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5026 {
5027 Lisp_Object mini_window;
c6e89d6c 5028 Lisp_Object frame;
5f5c8ee5
GM
5029 struct frame *f;
5030
5031 /* Get the frame containing the mini-buffer
5032 that the selected frame is using. */
886bd6f2 5033 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5034 frame = XWINDOW (mini_window)->frame;
5035 f = XFRAME (frame);
5f5c8ee5
GM
5036
5037 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5038 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5039 && !FRAME_VISIBLE_P (f))
5040 Fmake_frame_visible (frame);
5f5c8ee5 5041
c6e89d6c 5042 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5043 {
c6e89d6c 5044 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5045 if (minibuffer_auto_raise)
5046 Fraise_frame (frame);
5f5c8ee5
GM
5047 }
5048 else
c6e89d6c 5049 clear_message (1, 1);
5f5c8ee5 5050
c6e89d6c 5051 do_pending_window_change (0);
5f5c8ee5 5052 echo_area_display (1);
c6e89d6c 5053 do_pending_window_change (0);
5f5c8ee5
GM
5054 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5055 (*frame_up_to_date_hook) (f);
5056 }
5057}
5058
5059
5060/* Display a null-terminated echo area message M. If M is 0, clear
5061 out any existing message, and let the mini-buffer text show through.
5062
5063 The buffer M must continue to exist until after the echo area gets
5064 cleared or some other message gets displayed there. Do not pass
5065 text that is stored in a Lisp string. Do not pass text in a buffer
5066 that was alloca'd. */
5067
5068void
5069message1 (m)
5070 char *m;
5071{
5072 message2 (m, (m ? strlen (m) : 0), 0);
5073}
5074
5075
5076/* The non-logging counterpart of message1. */
5077
5078void
5079message1_nolog (m)
5080 char *m;
5081{
5082 message2_nolog (m, (m ? strlen (m) : 0), 0);
5083}
5084
5085/* Display a message M which contains a single %s
5086 which gets replaced with STRING. */
5087
5088void
5089message_with_string (m, string, log)
5090 char *m;
5091 Lisp_Object string;
5092 int log;
5093{
5094 if (noninteractive)
5095 {
5096 if (m)
5097 {
5098 if (noninteractive_need_newline)
5099 putc ('\n', stderr);
5100 noninteractive_need_newline = 0;
5101 fprintf (stderr, m, XSTRING (string)->data);
5102 if (cursor_in_echo_area == 0)
5103 fprintf (stderr, "\n");
5104 fflush (stderr);
5105 }
5106 }
5107 else if (INTERACTIVE)
5108 {
5109 /* The frame whose minibuffer we're going to display the message on.
5110 It may be larger than the selected frame, so we need
5111 to use its buffer, not the selected frame's buffer. */
5112 Lisp_Object mini_window;
886bd6f2 5113 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5114
5115 /* Get the frame containing the minibuffer
5116 that the selected frame is using. */
886bd6f2 5117 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5119
5120 /* A null message buffer means that the frame hasn't really been
5121 initialized yet. Error messages get reported properly by
5122 cmd_error, so this must be just an informative message; toss it. */
5123 if (FRAME_MESSAGE_BUF (f))
5124 {
5125 int len;
5126 char *a[1];
5127 a[0] = (char *) XSTRING (string)->data;
5128
5129 len = doprnt (FRAME_MESSAGE_BUF (f),
5130 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5131
5132 if (log)
5133 message2 (FRAME_MESSAGE_BUF (f), len,
5134 STRING_MULTIBYTE (string));
5135 else
5136 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5137 STRING_MULTIBYTE (string));
5138
5139 /* Print should start at the beginning of the message
5140 buffer next time. */
5141 message_buf_print = 0;
5142 }
5143 }
5144}
5145
5146
5f5c8ee5
GM
5147/* Dump an informative message to the minibuf. If M is 0, clear out
5148 any existing message, and let the mini-buffer text show through. */
5149
5150/* VARARGS 1 */
5151void
5152message (m, a1, a2, a3)
5153 char *m;
5154 EMACS_INT a1, a2, a3;
5155{
5156 if (noninteractive)
5157 {
5158 if (m)
5159 {
5160 if (noninteractive_need_newline)
5161 putc ('\n', stderr);
5162 noninteractive_need_newline = 0;
5163 fprintf (stderr, m, a1, a2, a3);
5164 if (cursor_in_echo_area == 0)
5165 fprintf (stderr, "\n");
5166 fflush (stderr);
5167 }
5168 }
5169 else if (INTERACTIVE)
5170 {
5171 /* The frame whose mini-buffer we're going to display the message
5172 on. It may be larger than the selected frame, so we need to
5173 use its buffer, not the selected frame's buffer. */
5174 Lisp_Object mini_window;
886bd6f2 5175 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5176
5177 /* Get the frame containing the mini-buffer
5178 that the selected frame is using. */
886bd6f2 5179 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5180 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5181
5182 /* A null message buffer means that the frame hasn't really been
5183 initialized yet. Error messages get reported properly by
5184 cmd_error, so this must be just an informative message; toss
5185 it. */
5186 if (FRAME_MESSAGE_BUF (f))
5187 {
5188 if (m)
5189 {
5190 int len;
5191#ifdef NO_ARG_ARRAY
5192 char *a[3];
5193 a[0] = (char *) a1;
5194 a[1] = (char *) a2;
5195 a[2] = (char *) a3;
5196
5197 len = doprnt (FRAME_MESSAGE_BUF (f),
5198 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5199#else
5200 len = doprnt (FRAME_MESSAGE_BUF (f),
5201 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5202 (char **) &a1);
5203#endif /* NO_ARG_ARRAY */
5204
5205 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5206 }
5207 else
5208 message1 (0);
5209
5210 /* Print should start at the beginning of the message
5211 buffer next time. */
5212 message_buf_print = 0;
5213 }
5214 }
5215}
5216
5217
5218/* The non-logging version of message. */
5219
5220void
5221message_nolog (m, a1, a2, a3)
5222 char *m;
5223 EMACS_INT a1, a2, a3;
5224{
5225 Lisp_Object old_log_max;
5226 old_log_max = Vmessage_log_max;
5227 Vmessage_log_max = Qnil;
5228 message (m, a1, a2, a3);
5229 Vmessage_log_max = old_log_max;
5230}
5231
5232
c6e89d6c
GM
5233/* Display the current message in the current mini-buffer. This is
5234 only called from error handlers in process.c, and is not time
5235 critical. */
5f5c8ee5
GM
5236
5237void
5238update_echo_area ()
5239{
c6e89d6c
GM
5240 if (!NILP (echo_area_buffer[0]))
5241 {
5242 Lisp_Object string;
5243 string = Fcurrent_message ();
5244 message3 (string, XSTRING (string)->size,
5245 !NILP (current_buffer->enable_multibyte_characters));
5246 }
5247}
5248
5249
5bcfeb49
GM
5250/* Make sure echo area buffers in echo_buffers[] are life. If they
5251 aren't, make new ones. */
5252
5253static void
5254ensure_echo_area_buffers ()
5255{
5256 int i;
5257
5258 for (i = 0; i < 2; ++i)
5259 if (!BUFFERP (echo_buffer[i])
5260 || NILP (XBUFFER (echo_buffer[i])->name))
5261 {
5262 char name[30];
5263 sprintf (name, " *Echo Area %d*", i);
5264 echo_buffer[i] = Fget_buffer_create (build_string (name));
5265 }
5266}
5267
5268
c6e89d6c
GM
5269/* Call FN with args A1..A5 with either the current or last displayed
5270 echo_area_buffer as current buffer.
5271
5272 WHICH zero means use the current message buffer
5273 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5274 from echo_buffer[] and clear it.
5275
5276 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5277 suitable buffer from echo_buffer[] and clear it.
5278
5279 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5280 that the current message becomes the last displayed one, make
5281 choose a suitable buffer for echo_area_buffer[0], and clear it.
5282
5283 Value is what FN returns. */
5284
5285static int
5286with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5287 struct window *w;
5288 int which;
5289 int (*fn) ();
5290 int a1, a2, a3, a4, a5;
5291{
5292 Lisp_Object buffer;
15e26c76 5293 int this_one, the_other, clear_buffer_p, rc;
c6e89d6c
GM
5294 int count = specpdl_ptr - specpdl;
5295
5296 /* If buffers aren't life, make new ones. */
5bcfeb49 5297 ensure_echo_area_buffers ();
c6e89d6c
GM
5298
5299 clear_buffer_p = 0;
5300
5301 if (which == 0)
5302 this_one = 0, the_other = 1;
5303 else if (which > 0)
5304 this_one = 1, the_other = 0;
5f5c8ee5 5305 else
c6e89d6c
GM
5306 {
5307 this_one = 0, the_other = 1;
5308 clear_buffer_p = 1;
5309
5310 /* We need a fresh one in case the current echo buffer equals
5311 the one containing the last displayed echo area message. */
5312 if (!NILP (echo_area_buffer[this_one])
5313 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5314 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
5315 }
5316
5317 /* Choose a suitable buffer from echo_buffer[] is we don't
5318 have one. */
5319 if (NILP (echo_area_buffer[this_one]))
5320 {
5321 echo_area_buffer[this_one]
5322 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5323 ? echo_buffer[the_other]
5324 : echo_buffer[this_one]);
5325 clear_buffer_p = 1;
5326 }
5327
5328 buffer = echo_area_buffer[this_one];
5329
5330 record_unwind_protect (unwind_with_echo_area_buffer,
5331 with_echo_area_buffer_unwind_data (w));
5332
5333 /* Make the echo area buffer current. Note that for display
5334 purposes, it is not necessary that the displayed window's buffer
5335 == current_buffer, except for text property lookup. So, let's
5336 only set that buffer temporarily here without doing a full
5337 Fset_window_buffer. We must also change w->pointm, though,
5338 because otherwise an assertions in unshow_buffer fails, and Emacs
5339 aborts. */
9142dd5b 5340 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5341 if (w)
5342 {
5343 w->buffer = buffer;
5344 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5345 }
5346 current_buffer->truncate_lines = Qnil;
5347 current_buffer->undo_list = Qt;
5348 current_buffer->read_only = Qnil;
5349
5350 if (clear_buffer_p && Z > BEG)
5351 del_range (BEG, Z);
5352
5353 xassert (BEGV >= BEG);
5354 xassert (ZV <= Z && ZV >= BEGV);
5355
5356 rc = fn (a1, a2, a3, a4, a5);
5357
5358 xassert (BEGV >= BEG);
5359 xassert (ZV <= Z && ZV >= BEGV);
5360
5361 unbind_to (count, Qnil);
5362 return rc;
5f5c8ee5
GM
5363}
5364
5365
c6e89d6c
GM
5366/* Save state that should be preserved around the call to the function
5367 FN called in with_echo_area_buffer. */
5f5c8ee5 5368
c6e89d6c
GM
5369static Lisp_Object
5370with_echo_area_buffer_unwind_data (w)
5371 struct window *w;
5f5c8ee5 5372{
c6e89d6c
GM
5373 int i = 0;
5374 Lisp_Object vector;
5f5c8ee5 5375
c6e89d6c
GM
5376 /* Reduce consing by keeping one vector in
5377 Vwith_echo_area_save_vector. */
5378 vector = Vwith_echo_area_save_vector;
5379 Vwith_echo_area_save_vector = Qnil;
5380
5381 if (NILP (vector))
9142dd5b 5382 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5383
5384 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5385 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5386 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5387
5388 if (w)
5389 {
5390 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5391 XVECTOR (vector)->contents[i++] = w->buffer;
5392 XVECTOR (vector)->contents[i++]
5393 = make_number (XMARKER (w->pointm)->charpos);
5394 XVECTOR (vector)->contents[i++]
5395 = make_number (XMARKER (w->pointm)->bytepos);
5396 }
5397 else
5398 {
5399 int end = i + 4;
5400 while (i < end)
5401 XVECTOR (vector)->contents[i++] = Qnil;
5402 }
5f5c8ee5 5403
c6e89d6c
GM
5404 xassert (i == XVECTOR (vector)->size);
5405 return vector;
5406}
5f5c8ee5 5407
5f5c8ee5 5408
c6e89d6c
GM
5409/* Restore global state from VECTOR which was created by
5410 with_echo_area_buffer_unwind_data. */
5411
5412static Lisp_Object
5413unwind_with_echo_area_buffer (vector)
5414 Lisp_Object vector;
5415{
5416 int i = 0;
5417
9142dd5b 5418 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
c6e89d6c
GM
5419 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5420 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
c6e89d6c
GM
5421
5422 if (WINDOWP (XVECTOR (vector)->contents[i]))
5423 {
5424 struct window *w;
5425 Lisp_Object buffer, charpos, bytepos;
5426
5427 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5428 buffer = XVECTOR (vector)->contents[i]; ++i;
5429 charpos = XVECTOR (vector)->contents[i]; ++i;
5430 bytepos = XVECTOR (vector)->contents[i]; ++i;
5431
5432 w->buffer = buffer;
5433 set_marker_both (w->pointm, buffer,
5434 XFASTINT (charpos), XFASTINT (bytepos));
5435 }
5436
5437 Vwith_echo_area_save_vector = vector;
5438 return Qnil;
5439}
5440
5441
5442/* Set up the echo area for use by print functions. MULTIBYTE_P
5443 non-zero means we will print multibyte. */
5444
5445void
5446setup_echo_area_for_printing (multibyte_p)
5447 int multibyte_p;
5448{
5bcfeb49
GM
5449 ensure_echo_area_buffers ();
5450
c6e89d6c
GM
5451 if (!message_buf_print)
5452 {
5453 /* A message has been output since the last time we printed.
5454 Choose a fresh echo area buffer. */
5455 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5456 echo_area_buffer[0] = echo_buffer[1];
5457 else
5458 echo_area_buffer[0] = echo_buffer[0];
5459
5460 /* Switch to that buffer and clear it. */
5461 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5462 if (Z > BEG)
5463 del_range (BEG, Z);
5464 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5465
5466 /* Set up the buffer for the multibyteness we need. */
5467 if (multibyte_p
5468 != !NILP (current_buffer->enable_multibyte_characters))
5469 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5470
5471 /* Raise the frame containing the echo area. */
5472 if (minibuffer_auto_raise)
5473 {
886bd6f2 5474 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 5475 Lisp_Object mini_window;
886bd6f2 5476 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5477 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5478 }
5479
5480 message_buf_print = 1;
5481 }
5482 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5483 /* Someone switched buffers between print requests. */
5484 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5485}
5486
5487
dd2eb166
GM
5488/* Display an echo area message in window W. Value is non-zero if W's
5489 height is changed. If display_last_displayed_message_p is
5490 non-zero, display the message that was last displayed, otherwise
5491 display the current message. */
c6e89d6c
GM
5492
5493static int
5494display_echo_area (w)
5495 struct window *w;
5496{
25edb08f
GM
5497 int i, no_message_p, window_height_changed_p, count;
5498
5499 /* Temporarily disable garbage collections while displaying the echo
5500 area. This is done because a GC can print a message itself.
5501 That message would modify the echo area buffer's contents while a
5502 redisplay of the buffer is going on, and seriously confuse
5503 redisplay. */
5504 count = inhibit_garbage_collection ();
dd2eb166
GM
5505
5506 /* If there is no message, we must call display_echo_area_1
5507 nevertheless because it resizes the window. But we will have to
5508 reset the echo_area_buffer in question to nil at the end because
5509 with_echo_area_buffer will sets it to an empty buffer. */
5510 i = display_last_displayed_message_p ? 1 : 0;
5511 no_message_p = NILP (echo_area_buffer[i]);
5512
5513 window_height_changed_p
5514 = with_echo_area_buffer (w, display_last_displayed_message_p,
5515 (int (*) ()) display_echo_area_1, w);
5516
5517 if (no_message_p)
5518 echo_area_buffer[i] = Qnil;
25edb08f
GM
5519
5520 unbind_to (count, Qnil);
dd2eb166 5521 return window_height_changed_p;
c6e89d6c
GM
5522}
5523
5524
5525/* Helper for display_echo_area. Display the current buffer which
5526 contains the current echo area message in window W, a mini-window.
5527 Change the height of W so that all of the message is displayed.
5528 Value is non-zero if height of W was changed. */
5529
5530static int
5531display_echo_area_1 (w)
5532 struct window *w;
5533{
5534 Lisp_Object window;
c6e89d6c
GM
5535 struct text_pos start;
5536 int window_height_changed_p = 0;
5537
5538 /* Do this before displaying, so that we have a large enough glyph
5539 matrix for the display. */
92a90e89 5540 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
5541
5542 /* Display. */
5543 clear_glyph_matrix (w->desired_matrix);
5544 XSETWINDOW (window, w);
5545 SET_TEXT_POS (start, BEG, BEG_BYTE);
5546 try_window (window, start);
5547
c6e89d6c
GM
5548 return window_height_changed_p;
5549}
5550
5551
92a90e89
GM
5552/* Resize the echo area window to exactly the size needed for the
5553 currently displayed message, if there is one. */
5554
5555void
5556resize_echo_area_axactly ()
5557{
5558 if (BUFFERP (echo_area_buffer[0])
5559 && WINDOWP (echo_area_window))
5560 {
5561 struct window *w = XWINDOW (echo_area_window);
5562 int resized_p;
5563
5564 resized_p = with_echo_area_buffer (w, 0,
5565 (int (*) ()) resize_mini_window,
5566 w, 1);
5567 if (resized_p)
5568 {
5569 ++windows_or_buffers_changed;
5570 ++update_mode_lines;
5571 redisplay_internal (0);
5572 }
5573 }
5574}
5575
5576
5577/* Resize mini-window W to fit the size of its contents. EXACT:P
5578 means size the window exactly to the size needed. Otherwise, it's
5579 only enlarged until W's buffer is empty. Value is non-zero if
5580 the window height has been changed. */
c6e89d6c 5581
9472f927 5582int
92a90e89 5583resize_mini_window (w, exact_p)
c6e89d6c 5584 struct window *w;
92a90e89 5585 int exact_p;
c6e89d6c
GM
5586{
5587 struct frame *f = XFRAME (w->frame);
5588 int window_height_changed_p = 0;
5589
5590 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
5591
5592 /* Nil means don't try to resize. */
00f6d59e
GM
5593 if (NILP (Vmax_mini_window_height)
5594 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 5595 return 0;
c6e89d6c
GM
5596
5597 if (!FRAME_MINIBUF_ONLY_P (f))
5598 {
5599 struct it it;
dd2eb166
GM
5600 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5601 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5602 int height, max_height;
5603 int unit = CANON_Y_UNIT (f);
5604 struct text_pos start;
9142dd5b 5605
c6e89d6c 5606 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 5607
dd2eb166
GM
5608 /* Compute the max. number of lines specified by the user. */
5609 if (FLOATP (Vmax_mini_window_height))
5610 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5611 else if (INTEGERP (Vmax_mini_window_height))
5612 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
5613 else
5614 max_height = total_height / 4;
dd2eb166
GM
5615
5616 /* Correct that max. height if it's bogus. */
5617 max_height = max (1, max_height);
5618 max_height = min (total_height, max_height);
5619
5620 /* Find out the height of the text in the window. */
55b064bd 5621 last_height = 0;
dd2eb166 5622 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
55b064bd
GM
5623 if (it.max_ascent == 0 && it.max_descent == 0)
5624 height = it.current_y + last_height;
5625 else
5626 height = it.current_y + it.max_ascent + it.max_descent;
5627 height = (height + unit - 1) / unit;
dd2eb166
GM
5628
5629 /* Compute a suitable window start. */
5630 if (height > max_height)
5631 {
5632 height = max_height;
5633 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5634 move_it_vertically_backward (&it, (height - 1) * unit);
5635 start = it.current.pos;
5636 }
5637 else
5638 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5639 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 5640
9472f927
GM
5641 /* Let it grow only, until we display an empty message, in which
5642 case the window shrinks again. */
da448723 5643 if (height > XFASTINT (w->height))
dd2eb166 5644 {
d2c48e86 5645 int old_height = XFASTINT (w->height);
da448723
GM
5646 freeze_window_starts (f, 1);
5647 grow_mini_window (w, height - XFASTINT (w->height));
5648 window_height_changed_p = XFASTINT (w->height) != old_height;
5649 }
5650 else if (height < XFASTINT (w->height)
5651 && (exact_p || BEGV == ZV))
5652 {
5653 int old_height = XFASTINT (w->height);
5654 freeze_window_starts (f, 0);
5655 shrink_mini_window (w);
d2c48e86 5656 window_height_changed_p = XFASTINT (w->height) != old_height;
9142dd5b 5657 }
c6e89d6c
GM
5658 }
5659
5660 return window_height_changed_p;
5661}
5662
5663
5664/* Value is the current message, a string, or nil if there is no
5665 current message. */
5666
5667Lisp_Object
5668current_message ()
5669{
5670 Lisp_Object msg;
5671
5672 if (NILP (echo_area_buffer[0]))
5673 msg = Qnil;
5674 else
5675 {
5676 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5677 if (NILP (msg))
5678 echo_area_buffer[0] = Qnil;
5679 }
5680
5681 return msg;
5682}
5683
5684
5685static int
5686current_message_1 (msg)
5687 Lisp_Object *msg;
5688{
5689 if (Z > BEG)
5690 *msg = make_buffer_string (BEG, Z, 1);
5691 else
5692 *msg = Qnil;
5693 return 0;
5694}
5695
5696
5697/* Push the current message on Vmessage_stack for later restauration
5698 by restore_message. Value is non-zero if the current message isn't
5699 empty. This is a relatively infrequent operation, so it's not
5700 worth optimizing. */
5701
5702int
5703push_message ()
5704{
5705 Lisp_Object msg;
5706 msg = current_message ();
5707 Vmessage_stack = Fcons (msg, Vmessage_stack);
5708 return STRINGP (msg);
5709}
5710
5711
5712/* Restore message display from the top of Vmessage_stack. */
5713
5714void
5715restore_message ()
5716{
5717 Lisp_Object msg;
5718
5719 xassert (CONSP (Vmessage_stack));
5720 msg = XCAR (Vmessage_stack);
5721 if (STRINGP (msg))
5722 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5723 else
5724 message3_nolog (msg, 0, 0);
5725}
5726
5727
5728/* Pop the top-most entry off Vmessage_stack. */
5729
5730void
5731pop_message ()
5732{
5733 xassert (CONSP (Vmessage_stack));
5734 Vmessage_stack = XCDR (Vmessage_stack);
5735}
5736
5737
5738/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5739 exits. If the stack is not empty, we have a missing pop_message
5740 somewhere. */
5741
5742void
5743check_message_stack ()
5744{
5745 if (!NILP (Vmessage_stack))
5746 abort ();
5747}
5748
5749
5750/* Truncate to NCHARS what will be displayed in the echo area the next
5751 time we display it---but don't redisplay it now. */
5752
5753void
5754truncate_echo_area (nchars)
5755 int nchars;
5756{
5757 if (nchars == 0)
5758 echo_area_buffer[0] = Qnil;
5759 /* A null message buffer means that the frame hasn't really been
5760 initialized yet. Error messages get reported properly by
5761 cmd_error, so this must be just an informative message; toss it. */
5762 else if (!noninteractive
5763 && INTERACTIVE
c6e89d6c 5764 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
5765 {
5766 struct frame *sf = SELECTED_FRAME ();
5767 if (FRAME_MESSAGE_BUF (sf))
5768 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5769 }
c6e89d6c
GM
5770}
5771
5772
5773/* Helper function for truncate_echo_area. Truncate the current
5774 message to at most NCHARS characters. */
5775
5776static int
5777truncate_message_1 (nchars)
5778 int nchars;
5779{
5780 if (BEG + nchars < Z)
5781 del_range (BEG + nchars, Z);
5782 if (Z == BEG)
5783 echo_area_buffer[0] = Qnil;
5784 return 0;
5785}
5786
5787
5788/* Set the current message to a substring of S or STRING.
5789
5790 If STRING is a Lisp string, set the message to the first NBYTES
5791 bytes from STRING. NBYTES zero means use the whole string. If
5792 STRING is multibyte, the message will be displayed multibyte.
5793
5794 If S is not null, set the message to the first LEN bytes of S. LEN
5795 zero means use the whole string. MULTIBYTE_P non-zero means S is
5796 multibyte. Display the message multibyte in that case. */
5797
5798void
5799set_message (s, string, nbytes, multibyte_p)
5800 char *s;
5801 Lisp_Object string;
5802 int nbytes;
5803{
5804 message_enable_multibyte
5805 = ((s && multibyte_p)
5806 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5807
5808 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5809 s, string, nbytes, multibyte_p);
5810 message_buf_print = 0;
5811}
5812
5813
5814/* Helper function for set_message. Arguments have the same meaning
5815 as there. This function is called with the echo area buffer being
5816 current. */
5817
5818static int
5819set_message_1 (s, string, nbytes, multibyte_p)
5820 char *s;
5821 Lisp_Object string;
5822 int nbytes, multibyte_p;
5823{
5824 xassert (BEG == Z);
5825
5826 /* Change multibyteness of the echo buffer appropriately. */
5827 if (message_enable_multibyte
5828 != !NILP (current_buffer->enable_multibyte_characters))
5829 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5830
5831 /* Insert new message at BEG. */
5832 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5833
5834 if (STRINGP (string))
5835 {
5836 int nchars;
5837
5838 if (nbytes == 0)
5839 nbytes = XSTRING (string)->size_byte;
5840 nchars = string_byte_to_char (string, nbytes);
5841
5842 /* This function takes care of single/multibyte conversion. We
5843 just have to ensure that the echo area buffer has the right
5844 setting of enable_multibyte_characters. */
5845 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5846 }
5847 else if (s)
5848 {
5849 if (nbytes == 0)
5850 nbytes = strlen (s);
5851
5852 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5853 {
5854 /* Convert from multi-byte to single-byte. */
5855 int i, c, n;
5856 unsigned char work[1];
5857
5858 /* Convert a multibyte string to single-byte. */
5859 for (i = 0; i < nbytes; i += n)
5860 {
5861 c = string_char_and_length (s + i, nbytes - i, &n);
5862 work[0] = (SINGLE_BYTE_CHAR_P (c)
5863 ? c
5864 : multibyte_char_to_unibyte (c, Qnil));
5865 insert_1_both (work, 1, 1, 1, 0, 0);
5866 }
5867 }
5868 else if (!multibyte_p
5869 && !NILP (current_buffer->enable_multibyte_characters))
5870 {
5871 /* Convert from single-byte to multi-byte. */
5872 int i, c, n;
5873 unsigned char *msg = (unsigned char *) s;
260a86a0 5874 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
5875
5876 /* Convert a single-byte string to multibyte. */
5877 for (i = 0; i < nbytes; i++)
5878 {
5879 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
5880 n = CHAR_STRING (c, str);
5881 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
5882 }
5883 }
5884 else
5885 insert_1 (s, nbytes, 1, 0, 0);
5886 }
5887
5888 return 0;
5889}
5890
5891
5892/* Clear messages. CURRENT_P non-zero means clear the current
5893 message. LAST_DISPLAYED_P non-zero means clear the message
5894 last displayed. */
5895
5896void
5897clear_message (current_p, last_displayed_p)
5898 int current_p, last_displayed_p;
5899{
5900 if (current_p)
5901 echo_area_buffer[0] = Qnil;
5902
5903 if (last_displayed_p)
5904 echo_area_buffer[1] = Qnil;
5905
5906 message_buf_print = 0;
5907}
5908
5909/* Clear garbaged frames.
5910
5911 This function is used where the old redisplay called
5912 redraw_garbaged_frames which in turn called redraw_frame which in
5913 turn called clear_frame. The call to clear_frame was a source of
5914 flickering. I believe a clear_frame is not necessary. It should
5915 suffice in the new redisplay to invalidate all current matrices,
5916 and ensure a complete redisplay of all windows. */
5917
5918static void
5919clear_garbaged_frames ()
5920{
5f5c8ee5
GM
5921 if (frame_garbaged)
5922 {
5f5c8ee5
GM
5923 Lisp_Object tail, frame;
5924
5925 FOR_EACH_FRAME (tail, frame)
5926 {
5927 struct frame *f = XFRAME (frame);
5928
5929 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5930 {
5931 clear_current_matrices (f);
5932 f->garbaged = 0;
5933 }
5934 }
5935
5936 frame_garbaged = 0;
5937 ++windows_or_buffers_changed;
5938 }
c6e89d6c 5939}
5f5c8ee5 5940
5f5c8ee5 5941
886bd6f2
GM
5942/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5943 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 5944 mini-windows height has been changed. */
5f5c8ee5 5945
c6e89d6c
GM
5946static int
5947echo_area_display (update_frame_p)
5948 int update_frame_p;
5949{
5950 Lisp_Object mini_window;
5951 struct window *w;
5952 struct frame *f;
5953 int window_height_changed_p = 0;
886bd6f2 5954 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 5955
886bd6f2 5956 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5957 w = XWINDOW (mini_window);
5958 f = XFRAME (WINDOW_FRAME (w));
5959
5960 /* Don't display if frame is invisible or not yet initialized. */
5961 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5962 return 0;
5f5c8ee5 5963
1ab3e082 5964#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
5965 /* When Emacs starts, selected_frame may be a visible terminal
5966 frame, even if we run under a window system. If we let this
5967 through, a message would be displayed on the terminal. */
622e3754
GM
5968 if (EQ (selected_frame, Vterminal_frame)
5969 && !NILP (Vwindow_system))
c6e89d6c 5970 return 0;
1ab3e082 5971#endif /* HAVE_WINDOW_SYSTEM */
c6e89d6c
GM
5972
5973 /* Redraw garbaged frames. */
5974 if (frame_garbaged)
5975 clear_garbaged_frames ();
5976
5977 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5978 {
5979 echo_area_window = mini_window;
5980 window_height_changed_p = display_echo_area (w);
5f5c8ee5 5981 w->must_be_updated_p = 1;
c59c668a 5982
5f5c8ee5
GM
5983 if (update_frame_p)
5984 {
c59c668a
GM
5985 /* Not called from redisplay_internal. If we changed
5986 window configuration, we must redisplay thoroughly.
5987 Otherwise, we can do with updating what we displayed
5988 above. */
5989 if (window_height_changed_p)
5990 {
5991 ++windows_or_buffers_changed;
5992 ++update_mode_lines;
5993 redisplay_internal (0);
5994 }
5995 else if (FRAME_WINDOW_P (f))
5f5c8ee5
GM
5996 {
5997 update_single_window (w, 1);
5998 rif->flush_display (f);
5999 }
6000 else
6001 update_frame (f, 1, 1);
6002 }
6003 }
6004 else if (!EQ (mini_window, selected_window))
6005 windows_or_buffers_changed++;
c59c668a
GM
6006
6007 /* Last displayed message is now the current message. */
dd2eb166
GM
6008 echo_area_buffer[1] = echo_area_buffer[0];
6009
5f5c8ee5
GM
6010 /* Prevent redisplay optimization in redisplay_internal by resetting
6011 this_line_start_pos. This is done because the mini-buffer now
6012 displays the message instead of its buffer text. */
6013 if (EQ (mini_window, selected_window))
6014 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
6015
6016 return window_height_changed_p;
5f5c8ee5
GM
6017}
6018
6019
6020\f
6021/***********************************************************************
6022 Frame Titles
6023 ***********************************************************************/
6024
6025
6026#ifdef HAVE_WINDOW_SYSTEM
6027
6028/* A buffer for constructing frame titles in it; allocated from the
6029 heap in init_xdisp and resized as needed in store_frame_title_char. */
6030
6031static char *frame_title_buf;
6032
6033/* The buffer's end, and a current output position in it. */
6034
6035static char *frame_title_buf_end;
6036static char *frame_title_ptr;
6037
6038
6039/* Store a single character C for the frame title in frame_title_buf.
6040 Re-allocate frame_title_buf if necessary. */
6041
6042static void
6043store_frame_title_char (c)
6044 char c;
6045{
6046 /* If output position has reached the end of the allocated buffer,
6047 double the buffer's size. */
6048 if (frame_title_ptr == frame_title_buf_end)
6049 {
6050 int len = frame_title_ptr - frame_title_buf;
6051 int new_size = 2 * len * sizeof *frame_title_buf;
6052 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6053 frame_title_buf_end = frame_title_buf + new_size;
6054 frame_title_ptr = frame_title_buf + len;
6055 }
6056
6057 *frame_title_ptr++ = c;
6058}
6059
6060
6061/* Store part of a frame title in frame_title_buf, beginning at
6062 frame_title_ptr. STR is the string to store. Do not copy more
6063 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6064 the whole string. Pad with spaces until FIELD_WIDTH number of
6065 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6066 Called from display_mode_element when it is used to build a frame
6067 title. */
6068
6069static int
6070store_frame_title (str, field_width, precision)
6071 unsigned char *str;
6072 int field_width, precision;
6073{
6074 int n = 0;
6075
6076 /* Copy at most PRECISION chars from STR. */
6077 while ((precision <= 0 || n < precision)
6078 && *str)
6079 {
6080 store_frame_title_char (*str++);
6081 ++n;
6082 }
6083
6084 /* Fill up with spaces until FIELD_WIDTH reached. */
6085 while (field_width > 0
6086 && n < field_width)
6087 {
6088 store_frame_title_char (' ');
6089 ++n;
6090 }
6091
6092 return n;
6093}
6094
6095
6096/* Set the title of FRAME, if it has changed. The title format is
6097 Vicon_title_format if FRAME is iconified, otherwise it is
6098 frame_title_format. */
6099
6100static void
6101x_consider_frame_title (frame)
6102 Lisp_Object frame;
6103{
6104 struct frame *f = XFRAME (frame);
6105
6106 if (FRAME_WINDOW_P (f)
6107 || FRAME_MINIBUF_ONLY_P (f)
6108 || f->explicit_name)
6109 {
6110 /* Do we have more than one visible frame on this X display? */
6111 Lisp_Object tail;
6112 Lisp_Object fmt;
6113 struct buffer *obuf;
6114 int len;
6115 struct it it;
6116
9472f927 6117 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6118 {
9472f927 6119 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
6120
6121 if (tf != f
6122 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6123 && !FRAME_MINIBUF_ONLY_P (tf)
6124 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6125 break;
6126 }
6127
6128 /* Set global variable indicating that multiple frames exist. */
6129 multiple_frames = CONSP (tail);
6130
6131 /* Switch to the buffer of selected window of the frame. Set up
6132 frame_title_ptr so that display_mode_element will output into it;
6133 then display the title. */
6134 obuf = current_buffer;
6135 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6136 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6137 frame_title_ptr = frame_title_buf;
6138 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6139 NULL, DEFAULT_FACE_ID);
6140 len = display_mode_element (&it, 0, -1, -1, fmt);
6141 frame_title_ptr = NULL;
6142 set_buffer_internal (obuf);
6143
6144 /* Set the title only if it's changed. This avoids consing in
6145 the common case where it hasn't. (If it turns out that we've
6146 already wasted too much time by walking through the list with
6147 display_mode_element, then we might need to optimize at a
6148 higher level than this.) */
6149 if (! STRINGP (f->name)
6150 || STRING_BYTES (XSTRING (f->name)) != len
6151 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6152 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6153 }
6154}
6155
6156#else /* not HAVE_WINDOW_SYSTEM */
6157
6158#define frame_title_ptr ((char *)0)
6159#define store_frame_title(str, mincol, maxcol) 0
6160
6161#endif /* not HAVE_WINDOW_SYSTEM */
6162
6163
6164
6165\f
6166/***********************************************************************
6167 Menu Bars
6168 ***********************************************************************/
6169
6170
6171/* Prepare for redisplay by updating menu-bar item lists when
6172 appropriate. This can call eval. */
6173
6174void
6175prepare_menu_bars ()
6176{
6177 int all_windows;
6178 struct gcpro gcpro1, gcpro2;
6179 struct frame *f;
6180 struct frame *tooltip_frame;
6181
6182#ifdef HAVE_X_WINDOWS
6183 tooltip_frame = tip_frame;
6184#else
6185 tooltip_frame = NULL;
6186#endif
6187
6188 /* Update all frame titles based on their buffer names, etc. We do
6189 this before the menu bars so that the buffer-menu will show the
6190 up-to-date frame titles. */
6191#ifdef HAVE_WINDOW_SYSTEM
6192 if (windows_or_buffers_changed || update_mode_lines)
6193 {
6194 Lisp_Object tail, frame;
6195
6196 FOR_EACH_FRAME (tail, frame)
6197 {
6198 f = XFRAME (frame);
6199 if (f != tooltip_frame
6200 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6201 x_consider_frame_title (frame);
6202 }
6203 }
6204#endif /* HAVE_WINDOW_SYSTEM */
6205
6206 /* Update the menu bar item lists, if appropriate. This has to be
6207 done before any actual redisplay or generation of display lines. */
6208 all_windows = (update_mode_lines
6209 || buffer_shared > 1
6210 || windows_or_buffers_changed);
6211 if (all_windows)
6212 {
6213 Lisp_Object tail, frame;
6214 int count = specpdl_ptr - specpdl;
6215
6216 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6217
6218 FOR_EACH_FRAME (tail, frame)
6219 {
6220 f = XFRAME (frame);
6221
6222 /* Ignore tooltip frame. */
6223 if (f == tooltip_frame)
6224 continue;
6225
6226 /* If a window on this frame changed size, report that to
6227 the user and clear the size-change flag. */
6228 if (FRAME_WINDOW_SIZES_CHANGED (f))
6229 {
6230 Lisp_Object functions;
6231
6232 /* Clear flag first in case we get an error below. */
6233 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6234 functions = Vwindow_size_change_functions;
6235 GCPRO2 (tail, functions);
6236
6237 while (CONSP (functions))
6238 {
6239 call1 (XCAR (functions), frame);
6240 functions = XCDR (functions);
6241 }
6242 UNGCPRO;
6243 }
6244
6245 GCPRO1 (tail);
6246 update_menu_bar (f, 0);
6247#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 6248 update_tool_bar (f, 0);
5f5c8ee5
GM
6249#endif
6250 UNGCPRO;
6251 }
6252
6253 unbind_to (count, Qnil);
6254 }
6255 else
6256 {
886bd6f2
GM
6257 struct frame *sf = SELECTED_FRAME ();
6258 update_menu_bar (sf, 1);
5f5c8ee5 6259#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 6260 update_tool_bar (sf, 1);
5f5c8ee5
GM
6261#endif
6262 }
6263
6264 /* Motif needs this. See comment in xmenu.c. Turn it off when
6265 pending_menu_activation is not defined. */
6266#ifdef USE_X_TOOLKIT
6267 pending_menu_activation = 0;
6268#endif
6269}
6270
6271
6272/* Update the menu bar item list for frame F. This has to be done
6273 before we start to fill in any display lines, because it can call
6274 eval.
6275
6276 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6277
6278static void
6279update_menu_bar (f, save_match_data)
6280 struct frame *f;
6281 int save_match_data;
6282{
6283 Lisp_Object window;
6284 register struct window *w;
6285
6286 window = FRAME_SELECTED_WINDOW (f);
6287 w = XWINDOW (window);
6288
6289 if (update_mode_lines)
6290 w->update_mode_line = Qt;
6291
6292 if (FRAME_WINDOW_P (f)
6293 ?
6294#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6295 FRAME_EXTERNAL_MENU_BAR (f)
6296#else
6297 FRAME_MENU_BAR_LINES (f) > 0
6298#endif
6299 : FRAME_MENU_BAR_LINES (f) > 0)
6300 {
6301 /* If the user has switched buffers or windows, we need to
6302 recompute to reflect the new bindings. But we'll
6303 recompute when update_mode_lines is set too; that means
6304 that people can use force-mode-line-update to request
6305 that the menu bar be recomputed. The adverse effect on
6306 the rest of the redisplay algorithm is about the same as
6307 windows_or_buffers_changed anyway. */
6308 if (windows_or_buffers_changed
6309 || !NILP (w->update_mode_line)
6310 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6311 < BUF_MODIFF (XBUFFER (w->buffer)))
6312 != !NILP (w->last_had_star))
6313 || ((!NILP (Vtransient_mark_mode)
6314 && !NILP (XBUFFER (w->buffer)->mark_active))
6315 != !NILP (w->region_showing)))
6316 {
6317 struct buffer *prev = current_buffer;
6318 int count = specpdl_ptr - specpdl;
6319
6320 set_buffer_internal_1 (XBUFFER (w->buffer));
6321 if (save_match_data)
6322 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6323 if (NILP (Voverriding_local_map_menu_flag))
6324 {
6325 specbind (Qoverriding_terminal_local_map, Qnil);
6326 specbind (Qoverriding_local_map, Qnil);
6327 }
6328
6329 /* Run the Lucid hook. */
6330 call1 (Vrun_hooks, Qactivate_menubar_hook);
6331
6332 /* If it has changed current-menubar from previous value,
6333 really recompute the menu-bar from the value. */
6334 if (! NILP (Vlucid_menu_bar_dirty_flag))
6335 call0 (Qrecompute_lucid_menubar);
6336
6337 safe_run_hooks (Qmenu_bar_update_hook);
6338 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6339
6340 /* Redisplay the menu bar in case we changed it. */
6341#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6342 if (FRAME_WINDOW_P (f))
6343 set_frame_menubar (f, 0, 0);
6344 else
6345 /* On a terminal screen, the menu bar is an ordinary screen
6346 line, and this makes it get updated. */
6347 w->update_mode_line = Qt;
6348#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6349 /* In the non-toolkit version, the menu bar is an ordinary screen
6350 line, and this makes it get updated. */
6351 w->update_mode_line = Qt;
6352#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6353
6354 unbind_to (count, Qnil);
6355 set_buffer_internal_1 (prev);
6356 }
6357 }
6358}
6359
6360
6361\f
6362/***********************************************************************
e037b9ec 6363 Tool-bars
5f5c8ee5
GM
6364 ***********************************************************************/
6365
6366#ifdef HAVE_WINDOW_SYSTEM
6367
e037b9ec 6368/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
6369 before we start to fill in any display lines. Called from
6370 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6371 and restore it here. */
6372
6373static void
e037b9ec 6374update_tool_bar (f, save_match_data)
5f5c8ee5
GM
6375 struct frame *f;
6376 int save_match_data;
6377{
e037b9ec
GM
6378 if (WINDOWP (f->tool_bar_window)
6379 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
6380 {
6381 Lisp_Object window;
6382 struct window *w;
6383
6384 window = FRAME_SELECTED_WINDOW (f);
6385 w = XWINDOW (window);
6386
6387 /* If the user has switched buffers or windows, we need to
6388 recompute to reflect the new bindings. But we'll
6389 recompute when update_mode_lines is set too; that means
6390 that people can use force-mode-line-update to request
6391 that the menu bar be recomputed. The adverse effect on
6392 the rest of the redisplay algorithm is about the same as
6393 windows_or_buffers_changed anyway. */
6394 if (windows_or_buffers_changed
6395 || !NILP (w->update_mode_line)
6396 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6397 < BUF_MODIFF (XBUFFER (w->buffer)))
6398 != !NILP (w->last_had_star))
6399 || ((!NILP (Vtransient_mark_mode)
6400 && !NILP (XBUFFER (w->buffer)->mark_active))
6401 != !NILP (w->region_showing)))
6402 {
6403 struct buffer *prev = current_buffer;
6404 int count = specpdl_ptr - specpdl;
a2889657 6405
5f5c8ee5
GM
6406 /* Set current_buffer to the buffer of the selected
6407 window of the frame, so that we get the right local
6408 keymaps. */
6409 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 6410
5f5c8ee5
GM
6411 /* Save match data, if we must. */
6412 if (save_match_data)
6413 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6414
6415 /* Make sure that we don't accidentally use bogus keymaps. */
6416 if (NILP (Voverriding_local_map_menu_flag))
6417 {
6418 specbind (Qoverriding_terminal_local_map, Qnil);
6419 specbind (Qoverriding_local_map, Qnil);
1f40cad2 6420 }
1f40cad2 6421
e037b9ec
GM
6422 /* Build desired tool-bar items from keymaps. */
6423 f->desired_tool_bar_items
6424 = tool_bar_items (f->desired_tool_bar_items,
6425 &f->n_desired_tool_bar_items);
5f5c8ee5 6426
e037b9ec 6427 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
6428 w->update_mode_line = Qt;
6429
6430 unbind_to (count, Qnil);
6431 set_buffer_internal_1 (prev);
81d478f3 6432 }
a2889657
JB
6433 }
6434}
6435
6c4429a5 6436
e037b9ec
GM
6437/* Set F->desired_tool_bar_string to a Lisp string representing frame
6438 F's desired tool-bar contents. F->desired_tool_bar_items must have
5f5c8ee5
GM
6439 been set up previously by calling prepare_menu_bars. */
6440
a2889657 6441static void
e037b9ec 6442build_desired_tool_bar_string (f)
5f5c8ee5 6443 struct frame *f;
a2889657 6444{
5f5c8ee5
GM
6445 int i, size, size_needed, string_idx;
6446 struct gcpro gcpro1, gcpro2, gcpro3;
6447 Lisp_Object image, plist, props;
a2889657 6448
5f5c8ee5
GM
6449 image = plist = props = Qnil;
6450 GCPRO3 (image, plist, props);
a2889657 6451
e037b9ec 6452 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
6453 Otherwise, make a new string. */
6454
6455 /* The size of the string we might be able to reuse. */
e037b9ec
GM
6456 size = (STRINGP (f->desired_tool_bar_string)
6457 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
6458 : 0);
6459
6460 /* Each image in the string we build is preceded by a space,
6461 and there is a space at the end. */
e037b9ec 6462 size_needed = f->n_desired_tool_bar_items + 1;
5f5c8ee5 6463
e037b9ec 6464 /* Reuse f->desired_tool_bar_string, if possible. */
5f5c8ee5 6465 if (size < size_needed)
e037b9ec 6466 f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
5f5c8ee5
GM
6467 else
6468 {
6469 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6470 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 6471 props, f->desired_tool_bar_string);
5f5c8ee5 6472 }
a2889657 6473
5f5c8ee5 6474 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
6475 put a `menu_item' property on tool-bar items with a value that
6476 is the index of the item in F's tool-bar item vector. */
5f5c8ee5 6477 for (i = 0, string_idx = 0;
e037b9ec 6478 i < f->n_desired_tool_bar_items;
5f5c8ee5 6479 ++i, string_idx += 1)
a2889657 6480 {
5f5c8ee5 6481#define PROP(IDX) \
e037b9ec
GM
6482 (XVECTOR (f->desired_tool_bar_items) \
6483 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
5f5c8ee5 6484
e037b9ec
GM
6485 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6486 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
5f5c8ee5
GM
6487 int margin, relief;
6488 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6489 extern Lisp_Object Qlaplace;
6490
6491 /* If image is a vector, choose the image according to the
6492 button state. */
e037b9ec 6493 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
6494 if (VECTORP (image))
6495 {
e037b9ec 6496 enum tool_bar_item_image idx;
5f5c8ee5
GM
6497
6498 if (enabled_p)
6499 idx = (selected_p
e037b9ec
GM
6500 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6501 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
6502 else
6503 idx = (selected_p
e037b9ec
GM
6504 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6505 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5
GM
6506
6507 xassert (XVECTOR (image)->size >= idx);
6508 image = XVECTOR (image)->contents[idx];
6509 }
6510
6511 /* Ignore invalid image specifications. */
6512 if (!valid_image_p (image))
6513 continue;
6514
e037b9ec 6515 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
6516 plist = Fcopy_sequence (XCDR (image));
6517
6518 /* Compute margin and relief to draw. */
e037b9ec
GM
6519 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6520 margin = relief + max (0, tool_bar_button_margin);
5f5c8ee5 6521
e037b9ec 6522 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
6523 {
6524 /* Add a `:relief' property to the image spec if the item is
6525 selected. */
6526 if (selected_p)
6527 {
6528 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6529 margin -= relief;
6530 }
6531 }
6532 else
6533 {
6534 /* If image is selected, display it pressed, i.e. with a
6535 negative relief. If it's not selected, display it with a
6536 raised relief. */
6537 plist = Fplist_put (plist, QCrelief,
6538 (selected_p
6539 ? make_number (-relief)
6540 : make_number (relief)));
6541 margin -= relief;
6542 }
6543
6544 /* Put a margin around the image. */
6545 if (margin)
6546 plist = Fplist_put (plist, QCmargin, make_number (margin));
6547
6548 /* If button is not enabled, make the image appear disabled by
6549 applying an appropriate algorithm to it. */
6550 if (!enabled_p)
6551 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6552
6553 /* Put a `display' text property on the string for the image to
6554 display. Put a `menu-item' property on the string that gives
e037b9ec 6555 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
6556 vector. */
6557 image = Fcons (Qimage, plist);
6558 props = list4 (Qdisplay, image,
e037b9ec 6559 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
5f5c8ee5
GM
6560 Fadd_text_properties (make_number (string_idx),
6561 make_number (string_idx + 1),
e037b9ec 6562 props, f->desired_tool_bar_string);
5f5c8ee5 6563#undef PROP
a2889657
JB
6564 }
6565
5f5c8ee5
GM
6566 UNGCPRO;
6567}
6568
6569
e037b9ec 6570/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
6571
6572static void
e037b9ec 6573display_tool_bar_line (it)
5f5c8ee5
GM
6574 struct it *it;
6575{
6576 struct glyph_row *row = it->glyph_row;
6577 int max_x = it->last_visible_x;
6578 struct glyph *last;
6579
6580 prepare_desired_row (row);
6581 row->y = it->current_y;
6582
6583 while (it->current_x < max_x)
a2889657 6584 {
5f5c8ee5 6585 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 6586
5f5c8ee5
GM
6587 /* Get the next display element. */
6588 if (!get_next_display_element (it))
6589 break;
73af359d 6590
5f5c8ee5
GM
6591 /* Produce glyphs. */
6592 x_before = it->current_x;
6593 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6594 PRODUCE_GLYPHS (it);
daa37602 6595
5f5c8ee5
GM
6596 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6597 i = 0;
6598 x = x_before;
6599 while (i < nglyphs)
6600 {
6601 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6602
6603 if (x + glyph->pixel_width > max_x)
6604 {
6605 /* Glyph doesn't fit on line. */
6606 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6607 it->current_x = x;
6608 goto out;
6609 }
daa37602 6610
5f5c8ee5
GM
6611 ++it->hpos;
6612 x += glyph->pixel_width;
6613 ++i;
6614 }
6615
6616 /* Stop at line ends. */
6617 if (ITERATOR_AT_END_OF_LINE_P (it))
6618 break;
6619
6620 set_iterator_to_next (it);
a2889657 6621 }
a2889657 6622
5f5c8ee5 6623 out:;
a2889657 6624
5f5c8ee5
GM
6625 row->displays_text_p = row->used[TEXT_AREA] != 0;
6626 extend_face_to_end_of_line (it);
6627 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6628 last->right_box_line_p = 1;
6629 compute_line_metrics (it);
6630
e037b9ec 6631 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
6632 if (!row->displays_text_p)
6633 {
312246d1
GM
6634 row->height = row->phys_height = it->last_visible_y - row->y;
6635 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
6636 }
6637
6638 row->full_width_p = 1;
6639 row->continued_p = 0;
6640 row->truncated_on_left_p = 0;
6641 row->truncated_on_right_p = 0;
6642
6643 it->current_x = it->hpos = 0;
6644 it->current_y += row->height;
6645 ++it->vpos;
6646 ++it->glyph_row;
a2889657 6647}
96a410bc 6648
5f5c8ee5 6649
e037b9ec 6650/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 6651 items of frame F visible. */
96a410bc 6652
d39b6696 6653static int
e037b9ec 6654tool_bar_lines_needed (f)
5f5c8ee5 6655 struct frame *f;
d39b6696 6656{
e037b9ec 6657 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
6658 struct it it;
6659
e037b9ec
GM
6660 /* Initialize an iterator for iteration over
6661 F->desired_tool_bar_string in the tool-bar window of frame F. */
6662 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
6663 it.first_visible_x = 0;
6664 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 6665 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
6666
6667 while (!ITERATOR_AT_END_P (&it))
6668 {
6669 it.glyph_row = w->desired_matrix->rows;
6670 clear_glyph_row (it.glyph_row);
e037b9ec 6671 display_tool_bar_line (&it);
5f5c8ee5
GM
6672 }
6673
6674 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 6675}
96a410bc 6676
5f5c8ee5 6677
e037b9ec 6678/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
6679 height should be changed. */
6680
6681static int
e037b9ec 6682redisplay_tool_bar (f)
5f5c8ee5 6683 struct frame *f;
96a410bc 6684{
5f5c8ee5
GM
6685 struct window *w;
6686 struct it it;
6687 struct glyph_row *row;
6688 int change_height_p = 0;
6689
e037b9ec
GM
6690 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6691 do anything. This means you must start with tool-bar-lines
5f5c8ee5 6692 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
6693 can turn off tool-bars by specifying tool-bar-lines zero. */
6694 if (!WINDOWP (f->tool_bar_window)
6695 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
6696 XFASTINT (w->height) == 0))
6697 return 0;
96a410bc 6698
e037b9ec
GM
6699 /* Set up an iterator for the tool-bar window. */
6700 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
6701 it.first_visible_x = 0;
6702 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6703 row = it.glyph_row;
3450d04c 6704
e037b9ec
GM
6705 /* Build a string that represents the contents of the tool-bar. */
6706 build_desired_tool_bar_string (f);
6707 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 6708
e037b9ec 6709 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 6710 while (it.current_y < it.last_visible_y)
e037b9ec 6711 display_tool_bar_line (&it);
3450d04c 6712
e037b9ec 6713 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
6714 window, so don't do it. */
6715 w->desired_matrix->no_scrolling_p = 1;
6716 w->must_be_updated_p = 1;
3450d04c 6717
e037b9ec 6718 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
6719 {
6720 int nlines;
6721
6722 /* If there are blank lines at the end, except for a partially
6723 visible blank line at the end that is smaller than
e037b9ec 6724 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
6725 row = it.glyph_row - 1;
6726 if (!row->displays_text_p
6727 && row->height >= CANON_Y_UNIT (f))
6728 change_height_p = 1;
6729
e037b9ec
GM
6730 /* If row displays tool-bar items, but is partially visible,
6731 change the tool-bar's height. */
5f5c8ee5
GM
6732 if (row->displays_text_p
6733 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6734 change_height_p = 1;
6735
e037b9ec 6736 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
6737 frame parameter. */
6738 if (change_height_p
e037b9ec 6739 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
6740 nlines != XFASTINT (w->height)))
6741 {
e037b9ec 6742 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5
GM
6743 Lisp_Object frame;
6744
6745 XSETFRAME (frame, f);
6746 clear_glyph_matrix (w->desired_matrix);
6747 Fmodify_frame_parameters (frame,
e037b9ec 6748 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
6749 make_number (nlines)),
6750 Qnil));
6751 fonts_changed_p = 1;
6752 }
6753 }
3450d04c 6754
5f5c8ee5 6755 return change_height_p;
96a410bc 6756}
90adcf20 6757
5f5c8ee5 6758
e037b9ec
GM
6759/* Get information about the tool-bar item which is displayed in GLYPH
6760 on frame F. Return in *PROP_IDX the index where tool-bar item
6761 properties start in F->current_tool_bar_items. Value is zero if
6762 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
6763
6764int
e037b9ec 6765tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
6766 struct frame *f;
6767 struct glyph *glyph;
6768 int *prop_idx;
90adcf20 6769{
5f5c8ee5
GM
6770 Lisp_Object prop;
6771 int success_p;
6772
6773 /* Get the text property `menu-item' at pos. The value of that
6774 property is the start index of this item's properties in
e037b9ec 6775 F->current_tool_bar_items. */
5f5c8ee5 6776 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 6777 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
6778 if (INTEGERP (prop))
6779 {
6780 *prop_idx = XINT (prop);
6781 success_p = 1;
6782 }
6783 else
6784 success_p = 0;
90adcf20 6785
5f5c8ee5
GM
6786 return success_p;
6787}
6788
6789#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6790
feb0c42f 6791
5f5c8ee5
GM
6792\f
6793/************************************************************************
6794 Horizontal scrolling
6795 ************************************************************************/
feb0c42f 6796
5f5c8ee5
GM
6797static int hscroll_window_tree P_ ((Lisp_Object));
6798static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6799
5f5c8ee5
GM
6800/* For all leaf windows in the window tree rooted at WINDOW, set their
6801 hscroll value so that PT is (i) visible in the window, and (ii) so
6802 that it is not within a certain margin at the window's left and
6803 right border. Value is non-zero if any window's hscroll has been
6804 changed. */
6805
6806static int
6807hscroll_window_tree (window)
6808 Lisp_Object window;
6809{
6810 int hscrolled_p = 0;
6811
6812 while (WINDOWP (window))
90adcf20 6813 {
5f5c8ee5
GM
6814 struct window *w = XWINDOW (window);
6815
6816 if (WINDOWP (w->hchild))
6817 hscrolled_p |= hscroll_window_tree (w->hchild);
6818 else if (WINDOWP (w->vchild))
6819 hscrolled_p |= hscroll_window_tree (w->vchild);
6820 else if (w->cursor.vpos >= 0)
6821 {
6822 int hscroll_margin, text_area_x, text_area_y;
6823 int text_area_width, text_area_height;
92a90e89
GM
6824 struct glyph_row *current_cursor_row
6825 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6826 struct glyph_row *desired_cursor_row
6827 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6828 struct glyph_row *cursor_row
6829 = (desired_cursor_row->enabled_p
6830 ? desired_cursor_row
6831 : current_cursor_row);
a2725ab2 6832
5f5c8ee5
GM
6833 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6834 &text_area_width, &text_area_height);
90adcf20 6835
5f5c8ee5
GM
6836 /* Scroll when cursor is inside this scroll margin. */
6837 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6838
6839 if ((XFASTINT (w->hscroll)
6840 && w->cursor.x < hscroll_margin)
92a90e89
GM
6841 || (cursor_row->enabled_p
6842 && cursor_row->truncated_on_right_p
5f5c8ee5 6843 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 6844 {
5f5c8ee5
GM
6845 struct it it;
6846 int hscroll;
6847 struct buffer *saved_current_buffer;
6848 int pt;
6849
6850 /* Find point in a display of infinite width. */
6851 saved_current_buffer = current_buffer;
6852 current_buffer = XBUFFER (w->buffer);
6853
6854 if (w == XWINDOW (selected_window))
6855 pt = BUF_PT (current_buffer);
6856 else
08b610e4 6857 {
5f5c8ee5
GM
6858 pt = marker_position (w->pointm);
6859 pt = max (BEGV, pt);
6860 pt = min (ZV, pt);
6861 }
6862
6863 /* Move iterator to pt starting at cursor_row->start in
6864 a line with infinite width. */
6865 init_to_row_start (&it, w, cursor_row);
6866 it.last_visible_x = INFINITY;
6867 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6868 current_buffer = saved_current_buffer;
6869
6870 /* Center cursor in window. */
6871 hscroll = (max (0, it.current_x - text_area_width / 2)
6872 / CANON_X_UNIT (it.f));
6873
6874 /* Don't call Fset_window_hscroll if value hasn't
6875 changed because it will prevent redisplay
6876 optimizations. */
6877 if (XFASTINT (w->hscroll) != hscroll)
6878 {
6879 Fset_window_hscroll (window, make_number (hscroll));
6880 hscrolled_p = 1;
08b610e4 6881 }
08b610e4 6882 }
08b610e4 6883 }
a2725ab2 6884
5f5c8ee5 6885 window = w->next;
90adcf20 6886 }
cd6dfed6 6887
5f5c8ee5
GM
6888 /* Value is non-zero if hscroll of any leaf window has been changed. */
6889 return hscrolled_p;
6890}
6891
6892
6893/* Set hscroll so that cursor is visible and not inside horizontal
6894 scroll margins for all windows in the tree rooted at WINDOW. See
6895 also hscroll_window_tree above. Value is non-zero if any window's
6896 hscroll has been changed. If it has, desired matrices on the frame
6897 of WINDOW are cleared. */
6898
6899static int
6900hscroll_windows (window)
6901 Lisp_Object window;
6902{
6903 int hscrolled_p = hscroll_window_tree (window);
6904 if (hscrolled_p)
6905 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6906 return hscrolled_p;
90adcf20 6907}
5f5c8ee5
GM
6908
6909
90adcf20 6910\f
5f5c8ee5
GM
6911/************************************************************************
6912 Redisplay
6913 ************************************************************************/
6914
6915/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6916 to a non-zero value. This is sometimes handy to have in a debugger
6917 session. */
6918
6919#if GLYPH_DEBUG
a2889657 6920
5f5c8ee5
GM
6921/* First and last unchanged row for try_window_id. */
6922
6923int debug_first_unchanged_at_end_vpos;
6924int debug_last_unchanged_at_beg_vpos;
6925
6926/* Delta vpos and y. */
6927
6928int debug_dvpos, debug_dy;
6929
6930/* Delta in characters and bytes for try_window_id. */
6931
6932int debug_delta, debug_delta_bytes;
6933
6934/* Values of window_end_pos and window_end_vpos at the end of
6935 try_window_id. */
6936
6937int debug_end_pos, debug_end_vpos;
6938
6939/* Append a string to W->desired_matrix->method. FMT is a printf
6940 format string. A1...A9 are a supplement for a variable-length
6941 argument list. If trace_redisplay_p is non-zero also printf the
6942 resulting string to stderr. */
6943
6944static void
6945debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6946 struct window *w;
6947 char *fmt;
6948 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6949{
6950 char buffer[512];
6951 char *method = w->desired_matrix->method;
6952 int len = strlen (method);
6953 int size = sizeof w->desired_matrix->method;
6954 int remaining = size - len - 1;
6955
6956 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6957 if (len && remaining)
6958 {
6959 method[len] = '|';
6960 --remaining, ++len;
6961 }
6962
6963 strncpy (method + len, buffer, remaining);
6964
6965 if (trace_redisplay_p)
6966 fprintf (stderr, "%p (%s): %s\n",
6967 w,
6968 ((BUFFERP (w->buffer)
6969 && STRINGP (XBUFFER (w->buffer)->name))
6970 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6971 : "no buffer"),
6972 buffer);
6973}
a2889657 6974
5f5c8ee5 6975#endif /* GLYPH_DEBUG */
90adcf20 6976
a2889657 6977
5f5c8ee5
GM
6978/* This counter is used to clear the face cache every once in a while
6979 in redisplay_internal. It is incremented for each redisplay.
6980 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6981 cleared. */
0d231165 6982
5f5c8ee5 6983#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
6984static int clear_face_cache_count;
6985
20de20dc 6986/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
6987
6988static struct frame *previous_terminal_frame;
6989
6990/* Non-zero while redisplay_internal is in progress. */
6991
6992int redisplaying_p;
6993
6994
6995/* Value is non-zero if all changes in window W, which displays
6996 current_buffer, are in the text between START and END. START is a
6997 buffer position, END is given as a distance from Z. Used in
6998 redisplay_internal for display optimization. */
6999
7000static INLINE int
7001text_outside_line_unchanged_p (w, start, end)
7002 struct window *w;
7003 int start, end;
7004{
7005 int unchanged_p = 1;
7006
7007 /* If text or overlays have changed, see where. */
7008 if (XFASTINT (w->last_modified) < MODIFF
7009 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7010 {
7011 /* Gap in the line? */
7012 if (GPT < start || Z - GPT < end)
7013 unchanged_p = 0;
7014
7015 /* Changes start in front of the line, or end after it? */
7016 if (unchanged_p
9142dd5b
GM
7017 && (BEG_UNCHANGED < start - 1
7018 || END_UNCHANGED < end))
5f5c8ee5
GM
7019 unchanged_p = 0;
7020
7021 /* If selective display, can't optimize if changes start at the
7022 beginning of the line. */
7023 if (unchanged_p
7024 && INTEGERP (current_buffer->selective_display)
7025 && XINT (current_buffer->selective_display) > 0
9142dd5b 7026 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
7027 unchanged_p = 0;
7028 }
7029
7030 return unchanged_p;
7031}
7032
7033
7034/* Do a frame update, taking possible shortcuts into account. This is
7035 the main external entry point for redisplay.
7036
7037 If the last redisplay displayed an echo area message and that message
7038 is no longer requested, we clear the echo area or bring back the
7039 mini-buffer if that is in use. */
20de20dc 7040
a2889657
JB
7041void
7042redisplay ()
e9874cee
RS
7043{
7044 redisplay_internal (0);
7045}
7046
260a86a0
KH
7047/* Return 1 if point moved out of or into a composition. Otherwise
7048 return 0. PREV_BUF and PREV_PT are the last point buffer and
7049 position. BUF and PT are the current point buffer and position. */
7050
7051int
7052check_point_in_composition (prev_buf, prev_pt, buf, pt)
7053 struct buffer *prev_buf, *buf;
7054 int prev_pt, pt;
7055{
7056 int start, end;
7057 Lisp_Object prop;
7058 Lisp_Object buffer;
7059
7060 XSETBUFFER (buffer, buf);
7061 /* Check a composition at the last point if point moved within the
7062 same buffer. */
7063 if (prev_buf == buf)
7064 {
7065 if (prev_pt == pt)
7066 /* Point didn't move. */
7067 return 0;
7068
7069 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7070 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7071 && COMPOSITION_VALID_P (start, end, prop)
7072 && start < prev_pt && end > prev_pt)
7073 /* The last point was within the composition. Return 1 iff
7074 point moved out of the composition. */
7075 return (pt <= start || pt >= end);
7076 }
7077
7078 /* Check a composition at the current point. */
7079 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7080 && find_composition (pt, -1, &start, &end, &prop, buffer)
7081 && COMPOSITION_VALID_P (start, end, prop)
7082 && start < pt && end > pt);
7083}
5f5c8ee5 7084
9142dd5b
GM
7085/* Reconsider the setting of B->clip_changed which is displayed
7086 in window W. */
7087
7088static INLINE void
7089reconsider_clip_changes (w, b)
7090 struct window *w;
7091 struct buffer *b;
7092{
7093 if (b->prevent_redisplay_optimizations_p)
7094 b->clip_changed = 1;
7095 else if (b->clip_changed
7096 && !NILP (w->window_end_valid)
7097 && w->current_matrix->buffer == b
7098 && w->current_matrix->zv == BUF_ZV (b)
7099 && w->current_matrix->begv == BUF_BEGV (b))
7100 b->clip_changed = 0;
260a86a0
KH
7101
7102 /* If display wasn't paused, and W is not a tool bar window, see if
7103 point has been moved into or out of a composition. In that case,
7104 we set b->clip_changed to 1 to force updating the screen. If
7105 b->clip_changed has already been set to 1, we can skip this
7106 check. */
7107 if (!b->clip_changed
7108 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7109 {
7110 int pt;
7111
7112 if (w == XWINDOW (selected_window))
7113 pt = BUF_PT (current_buffer);
7114 else
7115 pt = marker_position (w->pointm);
7116
7117 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7118 || pt != w->last_point)
7119 && check_point_in_composition (w->current_matrix->buffer,
7120 w->last_point,
7121 XBUFFER (w->buffer), pt))
7122 b->clip_changed = 1;
7123 }
9142dd5b
GM
7124}
7125
7126
5f5c8ee5
GM
7127/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7128 response to any user action; therefore, we should preserve the echo
7129 area. (Actually, our caller does that job.) Perhaps in the future
7130 avoid recentering windows if it is not necessary; currently that
7131 causes some problems. */
e9874cee
RS
7132
7133static void
7134redisplay_internal (preserve_echo_area)
7135 int preserve_echo_area;
a2889657 7136{
5f5c8ee5
GM
7137 struct window *w = XWINDOW (selected_window);
7138 struct frame *f = XFRAME (w->frame);
7139 int pause;
a2889657 7140 int must_finish = 0;
5f5c8ee5 7141 struct text_pos tlbufpos, tlendpos;
89819bdd 7142 int number_of_visible_frames;
28514cd9 7143 int count;
886bd6f2 7144 struct frame *sf = SELECTED_FRAME ();
a2889657 7145
5f5c8ee5
GM
7146 /* Non-zero means redisplay has to consider all windows on all
7147 frames. Zero means, only selected_window is considered. */
7148 int consider_all_windows_p;
7149
7150 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7151
7152 /* No redisplay if running in batch mode or frame is not yet fully
7153 initialized, or redisplay is explicitly turned off by setting
7154 Vinhibit_redisplay. */
7155 if (noninteractive
7156 || !NILP (Vinhibit_redisplay)
7157 || !f->glyphs_initialized_p)
a2889657
JB
7158 return;
7159
5f5c8ee5
GM
7160 /* The flag redisplay_performed_directly_p is set by
7161 direct_output_for_insert when it already did the whole screen
7162 update necessary. */
7163 if (redisplay_performed_directly_p)
7164 {
7165 redisplay_performed_directly_p = 0;
7166 if (!hscroll_windows (selected_window))
7167 return;
7168 }
7169
15f0cf78
RS
7170#ifdef USE_X_TOOLKIT
7171 if (popup_activated ())
7172 return;
7173#endif
7174
28514cd9 7175 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 7176 if (redisplaying_p)
735c094c
KH
7177 return;
7178
28514cd9
GM
7179 /* Record a function that resets redisplaying_p to its old value
7180 when we leave this function. */
7181 count = specpdl_ptr - specpdl;
7182 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7183 ++redisplaying_p;
7184
8b32d885
RS
7185 retry:
7186
9142dd5b
GM
7187 reconsider_clip_changes (w, current_buffer);
7188
5f5c8ee5
GM
7189 /* If new fonts have been loaded that make a glyph matrix adjustment
7190 necessary, do it. */
7191 if (fonts_changed_p)
7192 {
7193 adjust_glyphs (NULL);
7194 ++windows_or_buffers_changed;
7195 fonts_changed_p = 0;
7196 }
7197
886bd6f2
GM
7198 if (! FRAME_WINDOW_P (sf)
7199 && previous_terminal_frame != sf)
20de20dc 7200 {
5f5c8ee5
GM
7201 /* Since frames on an ASCII terminal share the same display
7202 area, displaying a different frame means redisplay the whole
7203 thing. */
20de20dc 7204 windows_or_buffers_changed++;
886bd6f2
GM
7205 SET_FRAME_GARBAGED (sf);
7206 XSETFRAME (Vterminal_frame, sf);
20de20dc 7207 }
886bd6f2 7208 previous_terminal_frame = sf;
20de20dc 7209
5f5c8ee5
GM
7210 /* Set the visible flags for all frames. Do this before checking
7211 for resized or garbaged frames; they want to know if their frames
7212 are visible. See the comment in frame.h for
7213 FRAME_SAMPLE_VISIBILITY. */
d724d989 7214 {
35f56f96 7215 Lisp_Object tail, frame;
d724d989 7216
89819bdd
RS
7217 number_of_visible_frames = 0;
7218
35f56f96 7219 FOR_EACH_FRAME (tail, frame)
f82aff7c 7220 {
5f5c8ee5
GM
7221 struct frame *f = XFRAME (frame);
7222
7223 FRAME_SAMPLE_VISIBILITY (f);
7224 if (FRAME_VISIBLE_P (f))
7225 ++number_of_visible_frames;
7226 clear_desired_matrices (f);
f82aff7c 7227 }
d724d989
JB
7228 }
7229
44fa5b1e 7230 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 7231 do_pending_window_change (1);
a2889657 7232
5f5c8ee5 7233 /* Clear frames marked as garbaged. */
44fa5b1e 7234 if (frame_garbaged)
c6e89d6c 7235 clear_garbaged_frames ();
a2889657 7236
e037b9ec 7237 /* Build menubar and tool-bar items. */
f82aff7c
RS
7238 prepare_menu_bars ();
7239
28995e67 7240 if (windows_or_buffers_changed)
a2889657
JB
7241 update_mode_lines++;
7242
538f13d4
RS
7243 /* Detect case that we need to write or remove a star in the mode line. */
7244 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
7245 {
7246 w->update_mode_line = Qt;
7247 if (buffer_shared > 1)
7248 update_mode_lines++;
7249 }
7250
5f5c8ee5 7251 /* If %c is in the mode line, update it if needed. */
28995e67
RS
7252 if (!NILP (w->column_number_displayed)
7253 /* This alternative quickly identifies a common case
7254 where no change is needed. */
7255 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7256 && XFASTINT (w->last_modified) >= MODIFF
7257 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7258 && XFASTINT (w->column_number_displayed) != current_column ())
7259 w->update_mode_line = Qt;
7260
44fa5b1e 7261 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 7262
5f5c8ee5
GM
7263 /* The variable buffer_shared is set in redisplay_window and
7264 indicates that we redisplay a buffer in different windows. See
7265 there. */
7266 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
7267
7268 /* If specs for an arrow have changed, do thorough redisplay
7269 to ensure we remove any arrow that should no longer exist. */
d45de95b 7270 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 7271 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 7272 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 7273
90adcf20
RS
7274 /* Normally the message* functions will have already displayed and
7275 updated the echo area, but the frame may have been trashed, or
7276 the update may have been preempted, so display the echo area
c6e89d6c
GM
7277 again here. Checking both message buffers captures the case that
7278 the echo area should be cleared. */
7279 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 7280 {
c6e89d6c 7281 int window_height_changed_p = echo_area_display (0);
90adcf20 7282 must_finish = 1;
dd2eb166 7283
c6e89d6c
GM
7284 if (fonts_changed_p)
7285 goto retry;
7286 else if (window_height_changed_p)
7287 {
7288 consider_all_windows_p = 1;
7289 ++update_mode_lines;
7290 ++windows_or_buffers_changed;
9142dd5b
GM
7291
7292 /* If window configuration was changed, frames may have been
7293 marked garbaged. Clear them or we will experience
7294 surprises wrt scrolling. */
7295 if (frame_garbaged)
7296 clear_garbaged_frames ();
c6e89d6c 7297 }
90adcf20 7298 }
dd2eb166
GM
7299 else if (w == XWINDOW (minibuf_window)
7300 && (current_buffer->clip_changed
7301 || XFASTINT (w->last_modified) < MODIFF
7302 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 7303 && resize_mini_window (w, 0))
c6e89d6c
GM
7304 {
7305 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
7306 showing if its contents might have changed. */
7307 must_finish = 1;
7308 consider_all_windows_p = 1;
c6e89d6c 7309 ++windows_or_buffers_changed;
dd2eb166 7310 ++update_mode_lines;
9142dd5b
GM
7311
7312 /* If window configuration was changed, frames may have been
7313 marked garbaged. Clear them or we will experience
7314 surprises wrt scrolling. */
7315 if (frame_garbaged)
7316 clear_garbaged_frames ();
c6e89d6c
GM
7317 }
7318
90adcf20 7319
5f5c8ee5
GM
7320 /* If showing the region, and mark has changed, we must redisplay
7321 the whole window. The assignment to this_line_start_pos prevents
7322 the optimization directly below this if-statement. */
bd66d1ba
RS
7323 if (((!NILP (Vtransient_mark_mode)
7324 && !NILP (XBUFFER (w->buffer)->mark_active))
7325 != !NILP (w->region_showing))
82d04750
JB
7326 || (!NILP (w->region_showing)
7327 && !EQ (w->region_showing,
7328 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
7329 CHARPOS (this_line_start_pos) = 0;
7330
7331 /* Optimize the case that only the line containing the cursor in the
7332 selected window has changed. Variables starting with this_ are
7333 set in display_line and record information about the line
7334 containing the cursor. */
7335 tlbufpos = this_line_start_pos;
7336 tlendpos = this_line_end_pos;
7337 if (!consider_all_windows_p
7338 && CHARPOS (tlbufpos) > 0
7339 && NILP (w->update_mode_line)
73af359d 7340 && !current_buffer->clip_changed
44fa5b1e 7341 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 7342 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 7343 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
7344 && this_line_buffer == current_buffer
7345 && current_buffer == XBUFFER (w->buffer)
265a9e55 7346 && NILP (w->force_start)
5f5c8ee5
GM
7347 /* Point must be on the line that we have info recorded about. */
7348 && PT >= CHARPOS (tlbufpos)
7349 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
7350 /* All text outside that line, including its final newline,
7351 must be unchanged */
5f5c8ee5
GM
7352 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7353 CHARPOS (tlendpos)))
7354 {
7355 if (CHARPOS (tlbufpos) > BEGV
7356 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7357 && (CHARPOS (tlbufpos) == ZV
7358 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
7359 /* Former continuation line has disappeared by becoming empty */
7360 goto cancel;
7361 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 7362 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
7363 || MINI_WINDOW_P (w))
7364 {
1c9241f5
KH
7365 /* We have to handle the case of continuation around a
7366 wide-column character (See the comment in indent.c around
7367 line 885).
7368
7369 For instance, in the following case:
7370
7371 -------- Insert --------
7372 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7373 J_I_ ==> J_I_ `^^' are cursors.
7374 ^^ ^^
7375 -------- --------
7376
7377 As we have to redraw the line above, we should goto cancel. */
7378
5f5c8ee5
GM
7379 struct it it;
7380 int line_height_before = this_line_pixel_height;
7381
7382 /* Note that start_display will handle the case that the
7383 line starting at tlbufpos is a continuation lines. */
7384 start_display (&it, w, tlbufpos);
7385
7386 /* Implementation note: It this still necessary? */
7387 if (it.current_x != this_line_start_x)
1c9241f5
KH
7388 goto cancel;
7389
5f5c8ee5
GM
7390 TRACE ((stderr, "trying display optimization 1\n"));
7391 w->cursor.vpos = -1;
a2889657 7392 overlay_arrow_seen = 0;
5f5c8ee5
GM
7393 it.vpos = this_line_vpos;
7394 it.current_y = this_line_y;
7395 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7396 display_line (&it);
7397
a2889657 7398 /* If line contains point, is not continued,
5f5c8ee5
GM
7399 and ends at same distance from eob as before, we win */
7400 if (w->cursor.vpos >= 0
7401 /* Line is not continued, otherwise this_line_start_pos
7402 would have been set to 0 in display_line. */
7403 && CHARPOS (this_line_start_pos)
7404 /* Line ends as before. */
7405 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7406 /* Line has same height as before. Otherwise other lines
7407 would have to be shifted up or down. */
7408 && this_line_pixel_height == line_height_before)
a2889657 7409 {
5f5c8ee5
GM
7410 /* If this is not the window's last line, we must adjust
7411 the charstarts of the lines below. */
7412 if (it.current_y < it.last_visible_y)
7413 {
7414 struct glyph_row *row
7415 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7416 int delta, delta_bytes;
7417
7418 if (Z - CHARPOS (tlendpos) == ZV)
7419 {
7420 /* This line ends at end of (accessible part of)
7421 buffer. There is no newline to count. */
7422 delta = (Z
7423 - CHARPOS (tlendpos)
7424 - MATRIX_ROW_START_CHARPOS (row));
7425 delta_bytes = (Z_BYTE
7426 - BYTEPOS (tlendpos)
7427 - MATRIX_ROW_START_BYTEPOS (row));
7428 }
7429 else
7430 {
7431 /* This line ends in a newline. Must take
7432 account of the newline and the rest of the
7433 text that follows. */
7434 delta = (Z
7435 - CHARPOS (tlendpos)
7436 - MATRIX_ROW_START_CHARPOS (row));
7437 delta_bytes = (Z_BYTE
7438 - BYTEPOS (tlendpos)
7439 - MATRIX_ROW_START_BYTEPOS (row));
7440 }
7441
7442 increment_glyph_matrix_buffer_positions (w->current_matrix,
7443 this_line_vpos + 1,
7444 w->current_matrix->nrows,
7445 delta, delta_bytes);
85bcef6c 7446 }
46db8486 7447
5f5c8ee5
GM
7448 /* If this row displays text now but previously didn't,
7449 or vice versa, w->window_end_vpos may have to be
7450 adjusted. */
7451 if ((it.glyph_row - 1)->displays_text_p)
7452 {
7453 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7454 XSETINT (w->window_end_vpos, this_line_vpos);
7455 }
7456 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7457 && this_line_vpos > 0)
7458 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7459 w->window_end_valid = Qnil;
7460
7461 /* Update hint: No need to try to scroll in update_window. */
7462 w->desired_matrix->no_scrolling_p = 1;
7463
7464#if GLYPH_DEBUG
7465 *w->desired_matrix->method = 0;
7466 debug_method_add (w, "optimization 1");
7467#endif
a2889657
JB
7468 goto update;
7469 }
7470 else
7471 goto cancel;
7472 }
5f5c8ee5
GM
7473 else if (/* Cursor position hasn't changed. */
7474 PT == XFASTINT (w->last_point)
b6f0fe04
RS
7475 /* Make sure the cursor was last displayed
7476 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
7477 && 0 <= w->cursor.vpos
7478 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
7479 {
7480 if (!must_finish)
7481 {
c6e89d6c 7482 do_pending_window_change (1);
5f5c8ee5
GM
7483
7484 /* We used to always goto end_of_redisplay here, but this
7485 isn't enough if we have a blinking cursor. */
7486 if (w->cursor_off_p == w->last_cursor_off_p)
7487 goto end_of_redisplay;
a2889657
JB
7488 }
7489 goto update;
7490 }
8b51f1e3
KH
7491 /* If highlighting the region, or if the cursor is in the echo area,
7492 then we can't just move the cursor. */
bd66d1ba
RS
7493 else if (! (!NILP (Vtransient_mark_mode)
7494 && !NILP (current_buffer->mark_active))
293a54ce
RS
7495 && (w == XWINDOW (current_buffer->last_selected_window)
7496 || highlight_nonselected_windows)
8b51f1e3 7497 && NILP (w->region_showing)
8f897821 7498 && NILP (Vshow_trailing_whitespace)
8b51f1e3 7499 && !cursor_in_echo_area)
a2889657 7500 {
5f5c8ee5
GM
7501 struct it it;
7502 struct glyph_row *row;
7503
7504 /* Skip from tlbufpos to PT and see where it is. Note that
7505 PT may be in invisible text. If so, we will end at the
7506 next visible position. */
7507 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7508 NULL, DEFAULT_FACE_ID);
7509 it.current_x = this_line_start_x;
7510 it.current_y = this_line_y;
7511 it.vpos = this_line_vpos;
7512
7513 /* The call to move_it_to stops in front of PT, but
7514 moves over before-strings. */
7515 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7516
7517 if (it.vpos == this_line_vpos
7518 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7519 row->enabled_p))
a2889657 7520 {
5f5c8ee5
GM
7521 xassert (this_line_vpos == it.vpos);
7522 xassert (this_line_y == it.current_y);
7523 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
7524 goto update;
7525 }
7526 else
7527 goto cancel;
7528 }
5f5c8ee5 7529
a2889657 7530 cancel:
5f5c8ee5
GM
7531 /* Text changed drastically or point moved off of line. */
7532 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
7533 }
7534
5f5c8ee5
GM
7535 CHARPOS (this_line_start_pos) = 0;
7536 consider_all_windows_p |= buffer_shared > 1;
7537 ++clear_face_cache_count;
a2889657 7538
5f5c8ee5
GM
7539
7540 /* Build desired matrices. If consider_all_windows_p is non-zero,
7541 do it for all windows on all frames. Otherwise do it for
7542 selected_window, only. */
463f6b91 7543
5f5c8ee5 7544 if (consider_all_windows_p)
a2889657 7545 {
35f56f96 7546 Lisp_Object tail, frame;
a2889657 7547
5f5c8ee5
GM
7548 /* Clear the face cache eventually. */
7549 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 7550 {
5f5c8ee5 7551 clear_face_cache (0);
463f6b91
RS
7552 clear_face_cache_count = 0;
7553 }
31b24551 7554
5f5c8ee5
GM
7555 /* Recompute # windows showing selected buffer. This will be
7556 incremented each time such a window is displayed. */
a2889657
JB
7557 buffer_shared = 0;
7558
35f56f96 7559 FOR_EACH_FRAME (tail, frame)
30c566e4 7560 {
5f5c8ee5 7561 struct frame *f = XFRAME (frame);
886bd6f2 7562 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 7563 {
5f5c8ee5
GM
7564 /* Mark all the scroll bars to be removed; we'll redeem
7565 the ones we want when we redisplay their windows. */
9769686d
RS
7566 if (condemn_scroll_bars_hook)
7567 (*condemn_scroll_bars_hook) (f);
30c566e4 7568
f21ef775 7569 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 7570 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 7571
5f5c8ee5
GM
7572 /* Any scroll bars which redisplay_windows should have
7573 nuked should now go away. */
9769686d
RS
7574 if (judge_scroll_bars_hook)
7575 (*judge_scroll_bars_hook) (f);
7576 }
30c566e4 7577 }
a2889657 7578 }
886bd6f2
GM
7579 else if (FRAME_VISIBLE_P (sf)
7580 && !FRAME_OBSCURED_P (sf))
5f5c8ee5
GM
7581 redisplay_window (selected_window, 1);
7582
7583
7584 /* Compare desired and current matrices, perform output. */
7585
7586update:
7587
7588 /* If fonts changed, display again. */
7589 if (fonts_changed_p)
7590 goto retry;
a2889657 7591
a2889657
JB
7592 /* Prevent various kinds of signals during display update.
7593 stdio is not robust about handling signals,
7594 which can cause an apparent I/O error. */
7595 if (interrupt_input)
7596 unrequest_sigio ();
7597 stop_polling ();
7598
5f5c8ee5 7599 if (consider_all_windows_p)
a2889657
JB
7600 {
7601 Lisp_Object tail;
92a90e89
GM
7602 struct frame *f;
7603 int hscrolled_p;
a2889657
JB
7604
7605 pause = 0;
92a90e89 7606 hscrolled_p = 0;
a2889657 7607
92a90e89 7608 /* See if we have to hscroll. */
9472f927 7609 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
92a90e89
GM
7610 if (FRAMEP (XCAR (tail)))
7611 {
7612 f = XFRAME (XCAR (tail));
7613
7614 if ((FRAME_WINDOW_P (f)
886bd6f2 7615 || f == sf)
92a90e89
GM
7616 && FRAME_VISIBLE_P (f)
7617 && !FRAME_OBSCURED_P (f)
7618 && hscroll_windows (f->root_window))
7619 hscrolled_p = 1;
7620 }
7621
7622 if (hscrolled_p)
7623 goto retry;
a2889657 7624
92a90e89
GM
7625 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7626 {
9472f927 7627 if (!FRAMEP (XCAR (tail)))
a2889657
JB
7628 continue;
7629
9472f927 7630 f = XFRAME (XCAR (tail));
1af9f229 7631
886bd6f2 7632 if ((FRAME_WINDOW_P (f) || f == sf)
f21ef775 7633 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 7634 {
5f5c8ee5
GM
7635 /* Mark all windows as to be updated. */
7636 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 7637 pause |= update_frame (f, 0, 0);
a2889657 7638 if (!pause)
efc63ef0
RS
7639 {
7640 mark_window_display_accurate (f->root_window, 1);
7641 if (frame_up_to_date_hook != 0)
7642 (*frame_up_to_date_hook) (f);
7643 }
a2889657
JB
7644 }
7645 }
7646 }
7647 else
6e8290aa 7648 {
886bd6f2
GM
7649 if (FRAME_VISIBLE_P (sf)
7650 && !FRAME_OBSCURED_P (sf))
5f5c8ee5 7651 {
92a90e89
GM
7652 if (hscroll_windows (selected_window))
7653 goto retry;
7654
5f5c8ee5 7655 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 7656 pause = update_frame (sf, 0, 0);
5f5c8ee5 7657 }
4d641a15
KH
7658 else
7659 pause = 0;
d724d989 7660
8de2d90b 7661 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
7662 function. If the echo area is on another frame, that may
7663 have put text on a frame other than the selected one, so the
7664 above call to update_frame would not have caught it. Catch
8de2d90b
JB
7665 it here. */
7666 {
84faf44c 7667 Lisp_Object mini_window;
5f5c8ee5 7668 struct frame *mini_frame;
84faf44c 7669
886bd6f2 7670 mini_window = FRAME_MINIBUF_WINDOW (sf);
84faf44c 7671 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 7672
886bd6f2 7673 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
7674 {
7675 XWINDOW (mini_window)->must_be_updated_p = 1;
7676 pause |= update_frame (mini_frame, 0, 0);
7677 if (!pause && hscroll_windows (mini_window))
7678 goto retry;
7679 }
8de2d90b 7680 }
6e8290aa 7681 }
a2889657 7682
5f5c8ee5
GM
7683 /* If display was paused because of pending input, make sure we do a
7684 thorough update the next time. */
a2889657
JB
7685 if (pause)
7686 {
5f5c8ee5
GM
7687 /* Prevent the optimization at the beginning of
7688 redisplay_internal that tries a single-line update of the
7689 line containing the cursor in the selected window. */
7690 CHARPOS (this_line_start_pos) = 0;
7691
7692 /* Let the overlay arrow be updated the next time. */
265a9e55 7693 if (!NILP (last_arrow_position))
a2889657
JB
7694 {
7695 last_arrow_position = Qt;
7696 last_arrow_string = Qt;
7697 }
5f5c8ee5
GM
7698
7699 /* If we pause after scrolling, some rows in the current
7700 matrices of some windows are not valid. */
7701 if (!WINDOW_FULL_WIDTH_P (w)
7702 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
7703 update_mode_lines = 1;
7704 }
7705
5f5c8ee5
GM
7706 /* Now text on frame agrees with windows, so put info into the
7707 windows for partial redisplay to follow. */
a2889657
JB
7708 if (!pause)
7709 {
7710 register struct buffer *b = XBUFFER (w->buffer);
7711
9142dd5b
GM
7712 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7713 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7714 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7715 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 7716
5f5c8ee5 7717 if (consider_all_windows_p)
886bd6f2 7718 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
a2889657
JB
7719 else
7720 {
5f5c8ee5
GM
7721 XSETFASTINT (w->last_point, BUF_PT (b));
7722 w->last_cursor = w->cursor;
7723 w->last_cursor_off_p = w->cursor_off_p;
7724
28995e67 7725 b->clip_changed = 0;
9142dd5b 7726 b->prevent_redisplay_optimizations_p = 0;
a2889657 7727 w->update_mode_line = Qnil;
c2213350 7728 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 7729 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
7730 w->last_had_star
7731 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7732 ? Qt : Qnil);
3ee4159a
RS
7733
7734 /* Record if we are showing a region, so can make sure to
7735 update it fully at next redisplay. */
7736 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
7737 && (w == XWINDOW (current_buffer->last_selected_window)
7738 || highlight_nonselected_windows)
3ee4159a
RS
7739 && !NILP (XBUFFER (w->buffer)->mark_active)
7740 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7741 : Qnil);
7742
d2f84654 7743 w->window_end_valid = w->buffer;
d45de95b 7744 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 7745 last_arrow_string = Voverlay_arrow_string;
efc63ef0 7746 if (frame_up_to_date_hook != 0)
886bd6f2 7747 (*frame_up_to_date_hook) (sf);
9142dd5b
GM
7748
7749 w->current_matrix->buffer = b;
7750 w->current_matrix->begv = BUF_BEGV (b);
7751 w->current_matrix->zv = BUF_ZV (b);
a2889657 7752 }
15e26c76 7753
a2889657
JB
7754 update_mode_lines = 0;
7755 windows_or_buffers_changed = 0;
7756 }
7757
5f5c8ee5
GM
7758 /* Start SIGIO interrupts coming again. Having them off during the
7759 code above makes it less likely one will discard output, but not
7760 impossible, since there might be stuff in the system buffer here.
a2889657 7761 But it is much hairier to try to do anything about that. */
a2889657
JB
7762 if (interrupt_input)
7763 request_sigio ();
7764 start_polling ();
7765
5f5c8ee5
GM
7766 /* If a frame has become visible which was not before, redisplay
7767 again, so that we display it. Expose events for such a frame
7768 (which it gets when becoming visible) don't call the parts of
7769 redisplay constructing glyphs, so simply exposing a frame won't
7770 display anything in this case. So, we have to display these
7771 frames here explicitly. */
11c52c4f
RS
7772 if (!pause)
7773 {
7774 Lisp_Object tail, frame;
7775 int new_count = 0;
7776
7777 FOR_EACH_FRAME (tail, frame)
7778 {
7779 int this_is_visible = 0;
8e83f802
RS
7780
7781 if (XFRAME (frame)->visible)
7782 this_is_visible = 1;
7783 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7784 if (XFRAME (frame)->visible)
7785 this_is_visible = 1;
11c52c4f
RS
7786
7787 if (this_is_visible)
7788 new_count++;
7789 }
7790
89819bdd 7791 if (new_count != number_of_visible_frames)
11c52c4f
RS
7792 windows_or_buffers_changed++;
7793 }
7794
44fa5b1e 7795 /* Change frame size now if a change is pending. */
c6e89d6c 7796 do_pending_window_change (1);
d8e242fd 7797
8b32d885
RS
7798 /* If we just did a pending size change, or have additional
7799 visible frames, redisplay again. */
3c8c72e0 7800 if (windows_or_buffers_changed && !pause)
8b32d885 7801 goto retry;
5f5c8ee5
GM
7802
7803 end_of_redisplay:;
c6e89d6c 7804
28514cd9 7805 unbind_to (count, Qnil);
a2889657
JB
7806}
7807
5f5c8ee5
GM
7808
7809/* Redisplay, but leave alone any recent echo area message unless
7810 another message has been requested in its place.
a2889657
JB
7811
7812 This is useful in situations where you need to redisplay but no
7813 user action has occurred, making it inappropriate for the message
7814 area to be cleared. See tracking_off and
7815 wait_reading_process_input for examples of these situations. */
7816
8991bb31 7817void
a2889657
JB
7818redisplay_preserve_echo_area ()
7819{
c6e89d6c 7820 if (!NILP (echo_area_buffer[1]))
a2889657 7821 {
c6e89d6c
GM
7822 /* We have a previously displayed message, but no current
7823 message. Redisplay the previous message. */
7824 display_last_displayed_message_p = 1;
e9874cee 7825 redisplay_internal (1);
c6e89d6c 7826 display_last_displayed_message_p = 0;
a2889657
JB
7827 }
7828 else
e9874cee 7829 redisplay_internal (1);
a2889657
JB
7830}
7831
5f5c8ee5 7832
28514cd9
GM
7833/* Function registered with record_unwind_protect in
7834 redisplay_internal. Clears the flag indicating that a redisplay is
7835 in progress. */
7836
7837static Lisp_Object
7838unwind_redisplay (old_redisplaying_p)
7839 Lisp_Object old_redisplaying_p;
7840{
7841 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 7842 return Qnil;
28514cd9
GM
7843}
7844
7845
5f5c8ee5
GM
7846/* Mark the display of windows in the window tree rooted at WINDOW as
7847 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7848 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7849 the next time redisplay_internal is called. */
7850
a2889657 7851void
5f5c8ee5 7852mark_window_display_accurate (window, accurate_p)
a2889657 7853 Lisp_Object window;
5f5c8ee5 7854 int accurate_p;
a2889657 7855{
5f5c8ee5
GM
7856 struct window *w;
7857
7858 for (; !NILP (window); window = w->next)
a2889657
JB
7859 {
7860 w = XWINDOW (window);
7861
5f5c8ee5 7862 if (BUFFERP (w->buffer))
bd66d1ba 7863 {
5f5c8ee5
GM
7864 struct buffer *b = XBUFFER (w->buffer);
7865
c2213350 7866 XSETFASTINT (w->last_modified,
5f5c8ee5 7867 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 7868 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
7869 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7870 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7871 ? Qt : Qnil);
bd66d1ba 7872
5f5c8ee5
GM
7873#if 0 /* I don't think this is necessary because display_line does it.
7874 Let's check it. */
bd66d1ba
RS
7875 /* Record if we are showing a region, so can make sure to
7876 update it fully at next redisplay. */
5f5c8ee5
GM
7877 w->region_showing
7878 = (!NILP (Vtransient_mark_mode)
7879 && (w == XWINDOW (current_buffer->last_selected_window)
7880 || highlight_nonselected_windows)
7881 && (!NILP (b->mark_active)
7882 ? Fmarker_position (b->mark)
7883 : Qnil));
7884#endif
7885
7886 if (accurate_p)
7887 {
7888 b->clip_changed = 0;
9142dd5b
GM
7889 b->prevent_redisplay_optimizations_p = 0;
7890 w->current_matrix->buffer = b;
7891 w->current_matrix->begv = BUF_BEGV (b);
7892 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
7893 w->last_cursor = w->cursor;
7894 w->last_cursor_off_p = w->cursor_off_p;
7895 if (w == XWINDOW (selected_window))
7896 w->last_point = BUF_PT (b);
7897 else
7898 w->last_point = XMARKER (w->pointm)->charpos;
7899 }
bd66d1ba
RS
7900 }
7901
d2f84654 7902 w->window_end_valid = w->buffer;
a2889657
JB
7903 w->update_mode_line = Qnil;
7904
265a9e55 7905 if (!NILP (w->vchild))
5f5c8ee5 7906 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 7907 if (!NILP (w->hchild))
5f5c8ee5 7908 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
7909 }
7910
5f5c8ee5 7911 if (accurate_p)
a2889657 7912 {
d45de95b 7913 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
7914 last_arrow_string = Voverlay_arrow_string;
7915 }
7916 else
7917 {
5f5c8ee5
GM
7918 /* Force a thorough redisplay the next time by setting
7919 last_arrow_position and last_arrow_string to t, which is
7920 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
7921 last_arrow_position = Qt;
7922 last_arrow_string = Qt;
7923 }
7924}
5f5c8ee5
GM
7925
7926
7927/* Return value in display table DP (Lisp_Char_Table *) for character
7928 C. Since a display table doesn't have any parent, we don't have to
7929 follow parent. Do not call this function directly but use the
7930 macro DISP_CHAR_VECTOR. */
7931
7932Lisp_Object
7933disp_char_vector (dp, c)
7934 struct Lisp_Char_Table *dp;
7935 int c;
7936{
7937 int code[4], i;
7938 Lisp_Object val;
7939
7940 if (SINGLE_BYTE_CHAR_P (c))
7941 return (dp->contents[c]);
7942
7943 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
7944 if (code[1] < 32)
7945 code[1] = -1;
7946 else if (code[2] < 32)
7947 code[2] = -1;
5f5c8ee5
GM
7948
7949 /* Here, the possible range of code[0] (== charset ID) is
7950 128..max_charset. Since the top level char table contains data
7951 for multibyte characters after 256th element, we must increment
7952 code[0] by 128 to get a correct index. */
7953 code[0] += 128;
7954 code[3] = -1; /* anchor */
7955
7956 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7957 {
7958 val = dp->contents[code[i]];
7959 if (!SUB_CHAR_TABLE_P (val))
7960 return (NILP (val) ? dp->defalt : val);
7961 }
7962
7963 /* Here, val is a sub char table. We return the default value of
7964 it. */
7965 return (dp->defalt);
7966}
7967
7968
a2889657 7969\f
5f5c8ee5
GM
7970/***********************************************************************
7971 Window Redisplay
7972 ***********************************************************************/
a2725ab2 7973
5f5c8ee5 7974/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
7975
7976static void
5f5c8ee5
GM
7977redisplay_windows (window)
7978 Lisp_Object window;
90adcf20 7979{
5f5c8ee5
GM
7980 while (!NILP (window))
7981 {
7982 struct window *w = XWINDOW (window);
7983
7984 if (!NILP (w->hchild))
7985 redisplay_windows (w->hchild);
7986 else if (!NILP (w->vchild))
7987 redisplay_windows (w->vchild);
7988 else
7989 redisplay_window (window, 0);
a2725ab2 7990
5f5c8ee5
GM
7991 window = w->next;
7992 }
7993}
7994
7995
7996/* Set cursor position of W. PT is assumed to be displayed in ROW.
7997 DELTA is the number of bytes by which positions recorded in ROW
7998 differ from current buffer positions. */
7999
8000void
8001set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8002 struct window *w;
8003 struct glyph_row *row;
8004 struct glyph_matrix *matrix;
8005 int delta, delta_bytes, dy, dvpos;
8006{
8007 struct glyph *glyph = row->glyphs[TEXT_AREA];
8008 struct glyph *end = glyph + row->used[TEXT_AREA];
8009 int x = row->x;
8010 int pt_old = PT - delta;
8011
8012 /* Skip over glyphs not having an object at the start of the row.
8013 These are special glyphs like truncation marks on terminal
8014 frames. */
8015 if (row->displays_text_p)
8016 while (glyph < end
8017 && !glyph->object
8018 && glyph->charpos < 0)
8019 {
8020 x += glyph->pixel_width;
8021 ++glyph;
8022 }
8023
8024 while (glyph < end
8025 && glyph->object
8026 && (!BUFFERP (glyph->object)
8027 || glyph->charpos < pt_old))
8028 {
8029 x += glyph->pixel_width;
8030 ++glyph;
8031 }
8032
8033 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8034 w->cursor.x = x;
8035 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8036 w->cursor.y = row->y + dy;
8037
8038 if (w == XWINDOW (selected_window))
8039 {
8040 if (!row->continued_p
8041 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8042 && row->x == 0)
8043 {
8044 this_line_buffer = XBUFFER (w->buffer);
8045
8046 CHARPOS (this_line_start_pos)
8047 = MATRIX_ROW_START_CHARPOS (row) + delta;
8048 BYTEPOS (this_line_start_pos)
8049 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8050
8051 CHARPOS (this_line_end_pos)
8052 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8053 BYTEPOS (this_line_end_pos)
8054 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8055
8056 this_line_y = w->cursor.y;
8057 this_line_pixel_height = row->height;
8058 this_line_vpos = w->cursor.vpos;
8059 this_line_start_x = row->x;
8060 }
8061 else
8062 CHARPOS (this_line_start_pos) = 0;
8063 }
8064}
8065
8066
8067/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
8068 start STARTP. Sets the window start of WINDOW to that position.
8069
8070 We assume that the window's buffer is really current. */
5f5c8ee5
GM
8071
8072static INLINE struct text_pos
8073run_window_scroll_functions (window, startp)
8074 Lisp_Object window;
8075 struct text_pos startp;
8076{
8077 struct window *w = XWINDOW (window);
8078 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
8079
8080 if (current_buffer != XBUFFER (w->buffer))
8081 abort ();
8082
5f5c8ee5
GM
8083 if (!NILP (Vwindow_scroll_functions))
8084 {
8085 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8086 make_number (CHARPOS (startp)));
8087 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
8088 /* In case the hook functions switch buffers. */
8089 if (current_buffer != XBUFFER (w->buffer))
8090 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8091 }
90adcf20 8092
5f5c8ee5
GM
8093 return startp;
8094}
8095
8096
8097/* Modify the desired matrix of window W and W->vscroll so that the
8098 line containing the cursor is fully visible. */
8099
8100static void
8101make_cursor_line_fully_visible (w)
8102 struct window *w;
8103{
8104 struct glyph_matrix *matrix;
8105 struct glyph_row *row;
045dee35 8106 int header_line_height;
5f5c8ee5
GM
8107
8108 /* It's not always possible to find the cursor, e.g, when a window
8109 is full of overlay strings. Don't do anything in that case. */
8110 if (w->cursor.vpos < 0)
8111 return;
8112
8113 matrix = w->desired_matrix;
8114 row = MATRIX_ROW (matrix, w->cursor.vpos);
8115
8116 /* If row->y == top y of window display area, the window isn't tall
8117 enough to display a single line. There is nothing we can do
8118 about it. */
045dee35
GM
8119 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8120 if (row->y == header_line_height)
5f5c8ee5
GM
8121 return;
8122
8123 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8124 {
8125 int dy = row->height - row->visible_height;
8126 w->vscroll = 0;
8127 w->cursor.y += dy;
8128 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8129 }
8130 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
8131 {
8132 int dy = - (row->height - row->visible_height);
8133 w->vscroll = dy;
8134 w->cursor.y += dy;
8135 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8136 }
8137
8138 /* When we change the cursor y-position of the selected window,
8139 change this_line_y as well so that the display optimization for
8140 the cursor line of the selected window in redisplay_internal uses
8141 the correct y-position. */
8142 if (w == XWINDOW (selected_window))
8143 this_line_y = w->cursor.y;
8144}
8145
8146
8147/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8148 non-zero means only WINDOW is redisplayed in redisplay_internal.
8149 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8150 in redisplay_window to bring a partially visible line into view in
8151 the case that only the cursor has moved.
8152
8153 Value is
8154
8155 1 if scrolling succeeded
8156
8157 0 if scrolling didn't find point.
8158
8159 -1 if new fonts have been loaded so that we must interrupt
8160 redisplay, adjust glyph matrices, and try again. */
8161
8162static int
8163try_scrolling (window, just_this_one_p, scroll_conservatively,
8164 scroll_step, temp_scroll_step)
8165 Lisp_Object window;
8166 int just_this_one_p;
8167 int scroll_conservatively, scroll_step;
8168 int temp_scroll_step;
8169{
8170 struct window *w = XWINDOW (window);
8171 struct frame *f = XFRAME (w->frame);
8172 struct text_pos scroll_margin_pos;
8173 struct text_pos pos;
8174 struct text_pos startp;
8175 struct it it;
8176 Lisp_Object window_end;
8177 int this_scroll_margin;
8178 int dy = 0;
8179 int scroll_max;
8180 int line_height, rc;
8181 int amount_to_scroll = 0;
8182 Lisp_Object aggressive;
8183 int height;
8184
8185#if GLYPH_DEBUG
8186 debug_method_add (w, "try_scrolling");
78614721 8187#endif
5f5c8ee5
GM
8188
8189 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8190
8191 /* Compute scroll margin height in pixels. We scroll when point is
8192 within this distance from the top or bottom of the window. */
8193 if (scroll_margin > 0)
90adcf20 8194 {
5f5c8ee5
GM
8195 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8196 this_scroll_margin *= CANON_Y_UNIT (f);
8197 }
8198 else
8199 this_scroll_margin = 0;
8200
8201 /* Compute how much we should try to scroll maximally to bring point
8202 into view. */
8203 if (scroll_step)
8204 scroll_max = scroll_step;
8205 else if (scroll_conservatively)
8206 scroll_max = scroll_conservatively;
8207 else if (temp_scroll_step)
8208 scroll_max = temp_scroll_step;
8209 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8210 || NUMBERP (current_buffer->scroll_up_aggressively))
8211 /* We're trying to scroll because of aggressive scrolling
8212 but no scroll_step is set. Choose an arbitrary one. Maybe
8213 there should be a variable for this. */
8214 scroll_max = 10;
8215 else
8216 scroll_max = 0;
8217 scroll_max *= CANON_Y_UNIT (f);
8218
8219 /* Decide whether we have to scroll down. Start at the window end
8220 and move this_scroll_margin up to find the position of the scroll
8221 margin. */
8222 window_end = Fwindow_end (window, Qt);
8223 CHARPOS (scroll_margin_pos) = XINT (window_end);
8224 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8225 if (this_scroll_margin)
8226 {
8227 start_display (&it, w, scroll_margin_pos);
8228 move_it_vertically (&it, - this_scroll_margin);
8229 scroll_margin_pos = it.current.pos;
8230 }
8231
8232 if (PT >= CHARPOS (scroll_margin_pos))
8233 {
8234 int y0;
8235
8236 /* Point is in the scroll margin at the bottom of the window, or
8237 below. Compute a new window start that makes point visible. */
8238
8239 /* Compute the distance from the scroll margin to PT.
8240 Give up if the distance is greater than scroll_max. */
8241 start_display (&it, w, scroll_margin_pos);
8242 y0 = it.current_y;
8243 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8244 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8245 line_height = (it.max_ascent + it.max_descent
8246 ? it.max_ascent + it.max_descent
8247 : last_height);
8248 dy = it.current_y + line_height - y0;
8249 if (dy > scroll_max)
8250 return 0;
8251
8252 /* Move the window start down. If scrolling conservatively,
8253 move it just enough down to make point visible. If
8254 scroll_step is set, move it down by scroll_step. */
8255 start_display (&it, w, startp);
8256
8257 if (scroll_conservatively)
8258 amount_to_scroll = dy;
8259 else if (scroll_step || temp_scroll_step)
8260 amount_to_scroll = scroll_max;
8261 else
90adcf20 8262 {
5f5c8ee5
GM
8263 aggressive = current_buffer->scroll_down_aggressively;
8264 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8265 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8266 if (NUMBERP (aggressive))
8267 amount_to_scroll = XFLOATINT (aggressive) * height;
8268 }
a2725ab2 8269
5f5c8ee5
GM
8270 if (amount_to_scroll <= 0)
8271 return 0;
a2725ab2 8272
5f5c8ee5
GM
8273 move_it_vertically (&it, amount_to_scroll);
8274 startp = it.current.pos;
8275 }
8276 else
8277 {
8278 /* See if point is inside the scroll margin at the top of the
8279 window. */
8280 scroll_margin_pos = startp;
8281 if (this_scroll_margin)
8282 {
8283 start_display (&it, w, startp);
8284 move_it_vertically (&it, this_scroll_margin);
8285 scroll_margin_pos = it.current.pos;
8286 }
8287
8288 if (PT < CHARPOS (scroll_margin_pos))
8289 {
8290 /* Point is in the scroll margin at the top of the window or
8291 above what is displayed in the window. */
8292 int y0;
8293
8294 /* Compute the vertical distance from PT to the scroll
8295 margin position. Give up if distance is greater than
8296 scroll_max. */
8297 SET_TEXT_POS (pos, PT, PT_BYTE);
8298 start_display (&it, w, pos);
8299 y0 = it.current_y;
8300 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8301 it.last_visible_y, -1,
8302 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8303 dy = it.current_y - y0;
8304 if (dy > scroll_max)
8305 return 0;
8306
8307 /* Compute new window start. */
8308 start_display (&it, w, startp);
8309
8310 if (scroll_conservatively)
8311 amount_to_scroll = dy;
8312 else if (scroll_step || temp_scroll_step)
8313 amount_to_scroll = scroll_max;
538f13d4 8314 else
5f5c8ee5
GM
8315 {
8316 aggressive = current_buffer->scroll_up_aggressively;
8317 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8318 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8319 if (NUMBERP (aggressive))
8320 amount_to_scroll = XFLOATINT (aggressive) * height;
8321 }
a2725ab2 8322
5f5c8ee5
GM
8323 if (amount_to_scroll <= 0)
8324 return 0;
8325
8326 move_it_vertically (&it, - amount_to_scroll);
8327 startp = it.current.pos;
90adcf20
RS
8328 }
8329 }
a2889657 8330
5f5c8ee5
GM
8331 /* Run window scroll functions. */
8332 startp = run_window_scroll_functions (window, startp);
90adcf20 8333
5f5c8ee5
GM
8334 /* Display the window. Give up if new fonts are loaded, or if point
8335 doesn't appear. */
8336 if (!try_window (window, startp))
8337 rc = -1;
8338 else if (w->cursor.vpos < 0)
8339 {
8340 clear_glyph_matrix (w->desired_matrix);
8341 rc = 0;
8342 }
8343 else
8344 {
8345 /* Maybe forget recorded base line for line number display. */
8346 if (!just_this_one_p
8347 || current_buffer->clip_changed
9142dd5b 8348 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
8349 w->base_line_number = Qnil;
8350
8351 /* If cursor ends up on a partially visible line, shift display
8352 lines up or down. */
8353 make_cursor_line_fully_visible (w);
8354 rc = 1;
8355 }
8356
8357 return rc;
a2889657
JB
8358}
8359
5f5c8ee5
GM
8360
8361/* Compute a suitable window start for window W if display of W starts
8362 on a continuation line. Value is non-zero if a new window start
8363 was computed.
8364
8365 The new window start will be computed, based on W's width, starting
8366 from the start of the continued line. It is the start of the
8367 screen line with the minimum distance from the old start W->start. */
8368
8369static int
8370compute_window_start_on_continuation_line (w)
8371 struct window *w;
1f1ff51d 8372{
5f5c8ee5
GM
8373 struct text_pos pos, start_pos;
8374 int window_start_changed_p = 0;
1f1ff51d 8375
5f5c8ee5 8376 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 8377
5f5c8ee5
GM
8378 /* If window start is on a continuation line... Window start may be
8379 < BEGV in case there's invisible text at the start of the
8380 buffer (M-x rmail, for example). */
8381 if (CHARPOS (start_pos) > BEGV
8382 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 8383 {
5f5c8ee5
GM
8384 struct it it;
8385 struct glyph_row *row;
f3751a65
GM
8386
8387 /* Handle the case that the window start is out of range. */
8388 if (CHARPOS (start_pos) < BEGV)
8389 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8390 else if (CHARPOS (start_pos) > ZV)
8391 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
8392
8393 /* Find the start of the continued line. This should be fast
8394 because scan_buffer is fast (newline cache). */
045dee35 8395 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
8396 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8397 row, DEFAULT_FACE_ID);
8398 reseat_at_previous_visible_line_start (&it);
8399
8400 /* If the line start is "too far" away from the window start,
8401 say it takes too much time to compute a new window start. */
8402 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8403 < XFASTINT (w->height) * XFASTINT (w->width))
8404 {
8405 int min_distance, distance;
8406
8407 /* Move forward by display lines to find the new window
8408 start. If window width was enlarged, the new start can
8409 be expected to be > the old start. If window width was
8410 decreased, the new window start will be < the old start.
8411 So, we're looking for the display line start with the
8412 minimum distance from the old window start. */
8413 pos = it.current.pos;
8414 min_distance = INFINITY;
8415 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8416 distance < min_distance)
8417 {
8418 min_distance = distance;
8419 pos = it.current.pos;
8420 move_it_by_lines (&it, 1, 0);
8421 }
8422
8423 /* Set the window start there. */
8424 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8425 window_start_changed_p = 1;
8426 }
1f1ff51d 8427 }
5f5c8ee5
GM
8428
8429 return window_start_changed_p;
1f1ff51d
KH
8430}
8431
5f5c8ee5
GM
8432
8433/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8434 selected_window is redisplayed. */
90adcf20 8435
a2889657 8436static void
5f5c8ee5 8437redisplay_window (window, just_this_one_p)
a2889657 8438 Lisp_Object window;
5f5c8ee5 8439 int just_this_one_p;
a2889657 8440{
5f5c8ee5
GM
8441 struct window *w = XWINDOW (window);
8442 struct frame *f = XFRAME (w->frame);
8443 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 8444 struct buffer *old = current_buffer;
5f5c8ee5 8445 struct text_pos lpoint, opoint, startp;
e481f960 8446 int update_mode_line;
5f5c8ee5
GM
8447 int tem;
8448 struct it it;
8449 /* Record it now because it's overwritten. */
8450 int current_matrix_up_to_date_p = 0;
5ba50c51 8451 int really_switched_buffer = 0;
5f5c8ee5 8452 int temp_scroll_step = 0;
2e54982e 8453 int count = specpdl_ptr - specpdl;
a2889657 8454
5f5c8ee5
GM
8455 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8456 opoint = lpoint;
a2889657 8457
5f5c8ee5
GM
8458 /* W must be a leaf window here. */
8459 xassert (!NILP (w->buffer));
8460#if GLYPH_DEBUG
8461 *w->desired_matrix->method = 0;
8462#endif
2e54982e
RS
8463
8464 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
8465
8466 reconsider_clip_changes (w, buffer);
8467
5f5c8ee5
GM
8468 /* Has the mode line to be updated? */
8469 update_mode_line = (!NILP (w->update_mode_line)
8470 || update_mode_lines
8471 || buffer->clip_changed);
8de2d90b
JB
8472
8473 if (MINI_WINDOW_P (w))
8474 {
5f5c8ee5 8475 if (w == XWINDOW (echo_area_window)
c6e89d6c 8476 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
8477 {
8478 if (update_mode_line)
8479 /* We may have to update a tty frame's menu bar or a
e037b9ec 8480 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
8481 goto finish_menu_bars;
8482 else
8483 /* We've already displayed the echo area glyphs in this window. */
8484 goto finish_scroll_bars;
8485 }
73af359d 8486 else if (w != XWINDOW (minibuf_window))
8de2d90b 8487 {
5f5c8ee5
GM
8488 /* W is a mini-buffer window, but it's not the currently
8489 active one, so clear it. */
8490 int yb = window_text_bottom_y (w);
8491 struct glyph_row *row;
8492 int y;
8493
8494 for (y = 0, row = w->desired_matrix->rows;
8495 y < yb;
8496 y += row->height, ++row)
8497 blank_row (w, row, y);
88f22aff 8498 goto finish_scroll_bars;
8de2d90b
JB
8499 }
8500 }
a2889657 8501
5f5c8ee5
GM
8502 /* Otherwise set up data on this window; select its buffer and point
8503 value. */
e481f960 8504 if (update_mode_line)
5ba50c51 8505 {
5f5c8ee5
GM
8506 /* Really select the buffer, for the sake of buffer-local
8507 variables. */
5ba50c51
RS
8508 set_buffer_internal_1 (XBUFFER (w->buffer));
8509 really_switched_buffer = 1;
8510 }
e481f960
RS
8511 else
8512 set_buffer_temp (XBUFFER (w->buffer));
5f5c8ee5
GM
8513 SET_TEXT_POS (opoint, PT, PT_BYTE);
8514
8515 current_matrix_up_to_date_p
8516 = (!NILP (w->window_end_valid)
8517 && !current_buffer->clip_changed
8518 && XFASTINT (w->last_modified) >= MODIFF
8519 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 8520
5f5c8ee5
GM
8521 /* When windows_or_buffers_changed is non-zero, we can't rely on
8522 the window end being valid, so set it to nil there. */
8523 if (windows_or_buffers_changed)
8524 {
8525 /* If window starts on a continuation line, maybe adjust the
8526 window start in case the window's width changed. */
8527 if (XMARKER (w->start)->buffer == current_buffer)
8528 compute_window_start_on_continuation_line (w);
8529
8530 w->window_end_valid = Qnil;
8531 }
12adba34 8532
5f5c8ee5
GM
8533 /* Some sanity checks. */
8534 CHECK_WINDOW_END (w);
8535 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 8536 abort ();
5f5c8ee5 8537 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 8538 abort ();
a2889657 8539
28995e67
RS
8540 /* If %c is in mode line, update it if needed. */
8541 if (!NILP (w->column_number_displayed)
8542 /* This alternative quickly identifies a common case
8543 where no change is needed. */
8544 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8545 && XFASTINT (w->last_modified) >= MODIFF
8546 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8547 && XFASTINT (w->column_number_displayed) != current_column ())
8548 update_mode_line = 1;
8549
5f5c8ee5
GM
8550 /* Count number of windows showing the selected buffer. An indirect
8551 buffer counts as its base buffer. */
8552 if (!just_this_one_p)
42640f83
RS
8553 {
8554 struct buffer *current_base, *window_base;
8555 current_base = current_buffer;
8556 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8557 if (current_base->base_buffer)
8558 current_base = current_base->base_buffer;
8559 if (window_base->base_buffer)
8560 window_base = window_base->base_buffer;
8561 if (current_base == window_base)
8562 buffer_shared++;
8563 }
a2889657 8564
5f5c8ee5
GM
8565 /* Point refers normally to the selected window. For any other
8566 window, set up appropriate value. */
a2889657
JB
8567 if (!EQ (window, selected_window))
8568 {
12adba34
RS
8569 int new_pt = XMARKER (w->pointm)->charpos;
8570 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 8571 if (new_pt < BEGV)
a2889657 8572 {
f67a0f51 8573 new_pt = BEGV;
12adba34
RS
8574 new_pt_byte = BEGV_BYTE;
8575 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 8576 }
f67a0f51 8577 else if (new_pt > (ZV - 1))
a2889657 8578 {
f67a0f51 8579 new_pt = ZV;
12adba34
RS
8580 new_pt_byte = ZV_BYTE;
8581 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 8582 }
5f5c8ee5 8583
f67a0f51 8584 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 8585 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
8586 }
8587
f4faa47c 8588 /* If any of the character widths specified in the display table
5f5c8ee5
GM
8589 have changed, invalidate the width run cache. It's true that
8590 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
8591 redisplay goes (non-fatally) haywire when the display table is
8592 changed, so why should we worry about doing any better? */
8593 if (current_buffer->width_run_cache)
8594 {
f908610f 8595 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
8596
8597 if (! disptab_matches_widthtab (disptab,
8598 XVECTOR (current_buffer->width_table)))
8599 {
8600 invalidate_region_cache (current_buffer,
8601 current_buffer->width_run_cache,
8602 BEG, Z);
8603 recompute_width_table (current_buffer, disptab);
8604 }
8605 }
8606
a2889657 8607 /* If window-start is screwed up, choose a new one. */
a2889657
JB
8608 if (XMARKER (w->start)->buffer != current_buffer)
8609 goto recenter;
8610
5f5c8ee5 8611 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 8612
cf0df6ab
RS
8613 /* If someone specified a new starting point but did not insist,
8614 check whether it can be used. */
cfad01b4
GM
8615 if (!NILP (w->optional_new_start)
8616 && CHARPOS (startp) >= BEGV
8617 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
8618 {
8619 w->optional_new_start = Qnil;
5f5c8ee5
GM
8620 /* This takes a mini-buffer prompt into account. */
8621 start_display (&it, w, startp);
8622 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8623 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8624 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
8625 w->force_start = Qt;
8626 }
8627
8de2d90b 8628 /* Handle case where place to start displaying has been specified,
aa6d10fa 8629 unless the specified location is outside the accessible range. */
9472f927
GM
8630 if (!NILP (w->force_start)
8631 || w->frozen_window_start_p)
a2889657 8632 {
e63574d7 8633 w->force_start = Qnil;
5f5c8ee5 8634 w->vscroll = 0;
b5174a51 8635 w->window_end_valid = Qnil;
5f5c8ee5
GM
8636
8637 /* Forget any recorded base line for line number display. */
8638 if (!current_matrix_up_to_date_p
8639 || current_buffer->clip_changed)
8640 w->base_line_number = Qnil;
8641
75c43375
RS
8642 /* Redisplay the mode line. Select the buffer properly for that.
8643 Also, run the hook window-scroll-functions
8644 because we have scrolled. */
e63574d7
RS
8645 /* Note, we do this after clearing force_start because
8646 if there's an error, it is better to forget about force_start
8647 than to get into an infinite loop calling the hook functions
8648 and having them get more errors. */
75c43375
RS
8649 if (!update_mode_line
8650 || ! NILP (Vwindow_scroll_functions))
e481f960 8651 {
5ba50c51
RS
8652 if (!really_switched_buffer)
8653 {
8654 set_buffer_temp (old);
8655 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8656 really_switched_buffer = 1;
5ba50c51 8657 }
5f5c8ee5 8658
e481f960
RS
8659 update_mode_line = 1;
8660 w->update_mode_line = Qt;
5f5c8ee5 8661 startp = run_window_scroll_functions (window, startp);
e481f960 8662 }
5f5c8ee5 8663
c2213350 8664 XSETFASTINT (w->last_modified, 0);
8850a573 8665 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
8666 if (CHARPOS (startp) < BEGV)
8667 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8668 else if (CHARPOS (startp) > ZV)
8669 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8670
8671 /* Redisplay, then check if cursor has been set during the
8672 redisplay. Give up if new fonts were loaded. */
8673 if (!try_window (window, startp))
8674 {
8675 w->force_start = Qt;
8676 clear_glyph_matrix (w->desired_matrix);
8677 goto restore_buffers;
8678 }
8679
9472f927 8680 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5
GM
8681 {
8682 /* If point does not appear, or on a line that is not fully
8683 visible, move point so it does appear. The desired
8684 matrix has been built above, so we can use it. */
8685 int height = window_box_height (w) / 2;
8686 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8687
8688 while (row->y < height)
8689 ++row;
8690
8691 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8692 MATRIX_ROW_START_BYTEPOS (row));
8693
90adcf20 8694 if (w != XWINDOW (selected_window))
12adba34 8695 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
8696 else if (current_buffer == old)
8697 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8698
8699 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8700
8701 /* If we are highlighting the region, then we just changed
8702 the region, so redisplay to show it. */
df0b5ea1
RS
8703 if (!NILP (Vtransient_mark_mode)
8704 && !NILP (current_buffer->mark_active))
6f27fa9b 8705 {
5f5c8ee5
GM
8706 clear_glyph_matrix (w->desired_matrix);
8707 if (!try_window (window, startp))
8708 goto restore_buffers;
6f27fa9b 8709 }
a2889657 8710 }
5f5c8ee5
GM
8711
8712 make_cursor_line_fully_visible (w);
8713#if GLYPH_DEBUG
8714 debug_method_add (w, "forced window start");
8715#endif
a2889657
JB
8716 goto done;
8717 }
8718
5f5c8ee5
GM
8719 /* Handle case where text has not changed, only point, and it has
8720 not moved off the frame. */
8721 if (current_matrix_up_to_date_p
8722 /* Point may be in this window. */
8723 && PT >= CHARPOS (startp)
8724 /* If we don't check this, we are called to move the cursor in a
8725 horizontally split window with a current matrix that doesn't
8726 fit the display. */
8727 && !windows_or_buffers_changed
8728 /* Selective display hasn't changed. */
8729 && !current_buffer->clip_changed
b1aa6cb3
RS
8730 /* If force-mode-line-update was called, really redisplay;
8731 that's how redisplay is forced after e.g. changing
8732 buffer-invisibility-spec. */
632ab665 8733 && NILP (w->update_mode_line)
5f5c8ee5
GM
8734 /* Can't use this case if highlighting a region. When a
8735 region exists, cursor movement has to do more than just
8736 set the cursor. */
8737 && !(!NILP (Vtransient_mark_mode)
8738 && !NILP (current_buffer->mark_active))
bd66d1ba 8739 && NILP (w->region_showing)
8f897821 8740 && NILP (Vshow_trailing_whitespace)
5f5c8ee5
GM
8741 /* Right after splitting windows, last_point may be nil. */
8742 && INTEGERP (w->last_point)
8743 /* This code is not used for mini-buffer for the sake of the case
8744 of redisplaying to replace an echo area message; since in
8745 that case the mini-buffer contents per se are usually
8746 unchanged. This code is of no real use in the mini-buffer
8747 since the handling of this_line_start_pos, etc., in redisplay
8748 handles the same cases. */
d45de95b 8749 && !EQ (window, minibuf_window)
5f5c8ee5
GM
8750 /* When splitting windows or for new windows, it happens that
8751 redisplay is called with a nil window_end_vpos or one being
8752 larger than the window. This should really be fixed in
8753 window.c. I don't have this on my list, now, so we do
8754 approximately the same as the old redisplay code. --gerd. */
8755 && INTEGERP (w->window_end_vpos)
8756 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8757 && (FRAME_WINDOW_P (f)
8758 || !MARKERP (Voverlay_arrow_position)
377dbd97 8759 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 8760 {
5f5c8ee5
GM
8761 int this_scroll_margin;
8762 struct glyph_row *row;
8763 int scroll_p;
a2889657 8764
5f5c8ee5
GM
8765#if GLYPH_DEBUG
8766 debug_method_add (w, "cursor movement");
8767#endif
9afd2168 8768
5f5c8ee5
GM
8769 /* Scroll if point within this distance from the top or bottom
8770 of the window. This is a pixel value. */
8771 this_scroll_margin = max (0, scroll_margin);
8772 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8773 this_scroll_margin *= CANON_Y_UNIT (f);
8774
8775 /* Start with the row the cursor was displayed during the last
8776 not paused redisplay. Give up if that row is not valid. */
8777 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8778 goto try_to_scroll;
8779 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8780 if (row->mode_line_p)
8781 ++row;
8782 if (!row->enabled_p)
8783 goto try_to_scroll;
8784
8785 scroll_p = 0;
8786 if (PT > XFASTINT (w->last_point))
8787 {
8788 /* Point has moved forward. */
8789 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8790
8791 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8792 /* The end position of a row equals the start
8793 position of the next row. If PT is there, we
8794 would rather display it in the next line, except
8795 when this line ends in ZV. */
8796 || (MATRIX_ROW_END_CHARPOS (row) == PT
8797 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8798 || !row->ends_at_zv_p)))
8799 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8800 {
8801 xassert (row->enabled_p);
8802 ++row;
8803 }
9afd2168 8804
5f5c8ee5
GM
8805 /* If within the scroll margin, scroll. Note that
8806 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8807 next line would be drawn, and that this_scroll_margin can
8808 be zero. */
8809 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8810 || PT > MATRIX_ROW_END_CHARPOS (row)
8811 /* Line is completely visible last line in window and PT
8812 is to be set in the next line. */
8813 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8814 && PT == MATRIX_ROW_END_CHARPOS (row)
8815 && !row->ends_at_zv_p
8816 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8817 scroll_p = 1;
8818 }
8819 else if (PT < XFASTINT (w->last_point))
a2889657 8820 {
5f5c8ee5
GM
8821 /* Cursor has to be moved backward. Note that PT >=
8822 CHARPOS (startp) because of the outer if-statement. */
8823 while (!row->mode_line_p
8824 && (MATRIX_ROW_START_CHARPOS (row) > PT
8825 || (MATRIX_ROW_START_CHARPOS (row) == PT
8826 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8827 && (row->y > this_scroll_margin
8828 || CHARPOS (startp) == BEGV))
a2889657 8829 {
5f5c8ee5
GM
8830 xassert (row->enabled_p);
8831 --row;
a2889657 8832 }
abb4c08f 8833
5f5c8ee5
GM
8834 /* Consider the following case: Window starts at BEGV, there
8835 is invisible, intangible text at BEGV, so that display
8836 starts at some point START > BEGV. It can happen that
8837 we are called with PT somewhere between BEGV and START.
8838 Try to handle that case. */
8839 if (row < w->current_matrix->rows
8840 || row->mode_line_p)
8841 {
8842 row = w->current_matrix->rows;
8843 if (row->mode_line_p)
8844 ++row;
8845 }
8846
8847 /* Due to newlines in overlay strings, we may have to skip
8848 forward over overlay strings. */
8849 while (MATRIX_ROW_END_CHARPOS (row) == PT
8850 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8851 && !row->ends_at_zv_p)
8852 ++row;
8853
8854 /* If within the scroll margin, scroll. */
8855 if (row->y < this_scroll_margin
8856 && CHARPOS (startp) != BEGV)
8857 scroll_p = 1;
8858 }
8859
8860 /* if PT is not in the glyph row, give up. */
8861 if (PT < MATRIX_ROW_START_CHARPOS (row)
8862 || PT > MATRIX_ROW_END_CHARPOS (row))
8863 goto try_to_scroll;
8864
8865 /* If we end up in a partially visible line, let's make it fully
8866 visible. This can be done most easily by using the existing
8867 scrolling code. */
8868 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8869 {
8870 temp_scroll_step = 1;
8871 goto try_to_scroll;
a2889657 8872 }
5f5c8ee5
GM
8873 else if (scroll_p)
8874 goto try_to_scroll;
8875
8876 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8877 goto done;
a2889657 8878 }
5f5c8ee5 8879
a2889657
JB
8880 /* If current starting point was originally the beginning of a line
8881 but no longer is, find a new starting point. */
265a9e55 8882 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
8883 && !(CHARPOS (startp) <= BEGV
8884 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 8885 {
5f5c8ee5
GM
8886#if GLYPH_DEBUG
8887 debug_method_add (w, "recenter 1");
8888#endif
a2889657
JB
8889 goto recenter;
8890 }
5f5c8ee5
GM
8891
8892 /* Try scrolling with try_window_id. */
9142dd5b
GM
8893 else if (/* Windows and buffers haven't changed. */
8894 !windows_or_buffers_changed
5f5c8ee5
GM
8895 /* Window must be either use window-based redisplay or
8896 be full width. */
8897 && (FRAME_WINDOW_P (f)
c59c668a 8898 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
8899 && !MINI_WINDOW_P (w)
8900 /* Point is not known NOT to appear in window. */
8901 && PT >= CHARPOS (startp)
a2889657 8902 && XFASTINT (w->last_modified)
5f5c8ee5
GM
8903 /* Window is not hscrolled. */
8904 && XFASTINT (w->hscroll) == 0
8905 /* Selective display has not changed. */
8906 && !current_buffer->clip_changed
8907 /* Current matrix is up to date. */
8908 && !NILP (w->window_end_valid)
8909 /* Can't use this case if highlighting a region because
8910 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
8911 && !(!NILP (Vtransient_mark_mode)
8912 && !NILP (current_buffer->mark_active))
8913 && NILP (w->region_showing)
8f897821 8914 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 8915 /* Overlay arrow position and string not changed. */
d45de95b 8916 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 8917 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
8918 /* Value is > 0 if update has been done, it is -1 if we
8919 know that the same window start will not work. It is 0
8920 if unsuccessful for some other reason. */
8921 && (tem = try_window_id (w)) != 0)
a2889657 8922 {
5f5c8ee5
GM
8923#if GLYPH_DEBUG
8924 debug_method_add (w, "try_window_id");
8925#endif
8926
8927 if (fonts_changed_p)
8928 goto restore_buffers;
a2889657
JB
8929 if (tem > 0)
8930 goto done;
5f5c8ee5
GM
8931 /* Otherwise try_window_id has returned -1 which means that we
8932 don't want the alternative below this comment to execute. */
a2889657 8933 }
5f5c8ee5
GM
8934 else if (CHARPOS (startp) >= BEGV
8935 && CHARPOS (startp) <= ZV
8936 && PT >= CHARPOS (startp)
8937 && (CHARPOS (startp) < ZV
e9874cee 8938 /* Avoid starting at end of buffer. */
5f5c8ee5 8939 || CHARPOS (startp) == BEGV
8850a573
RS
8940 || (XFASTINT (w->last_modified) >= MODIFF
8941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 8942 {
5f5c8ee5
GM
8943#if GLYPH_DEBUG
8944 debug_method_add (w, "same window start");
8945#endif
8946
8947 /* Try to redisplay starting at same place as before.
8948 If point has not moved off frame, accept the results. */
8949 if (!current_matrix_up_to_date_p
8950 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
8951 because a window scroll function can have changed the
8952 buffer. */
5f5c8ee5
GM
8953 || !NILP (Vwindow_scroll_functions)
8954 || MINI_WINDOW_P (w)
8955 || !try_window_reusing_current_matrix (w))
8956 {
8957 IF_DEBUG (debug_method_add (w, "1"));
8958 try_window (window, startp);
8959 }
8960
8961 if (fonts_changed_p)
8962 goto restore_buffers;
8963
8964 if (w->cursor.vpos >= 0)
aa6d10fa 8965 {
5f5c8ee5
GM
8966 if (!just_this_one_p
8967 || current_buffer->clip_changed
9142dd5b 8968 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
8969 /* Forget any recorded base line for line number display. */
8970 w->base_line_number = Qnil;
5f5c8ee5
GM
8971
8972 make_cursor_line_fully_visible (w);
aa6d10fa
RS
8973 goto done;
8974 }
a2889657 8975 else
5f5c8ee5 8976 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
8977 }
8978
5f5c8ee5
GM
8979 try_to_scroll:
8980
c2213350 8981 XSETFASTINT (w->last_modified, 0);
8850a573 8982 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 8983
e481f960
RS
8984 /* Redisplay the mode line. Select the buffer properly for that. */
8985 if (!update_mode_line)
8986 {
5ba50c51
RS
8987 if (!really_switched_buffer)
8988 {
8989 set_buffer_temp (old);
8990 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8991 really_switched_buffer = 1;
5ba50c51 8992 }
e481f960
RS
8993 update_mode_line = 1;
8994 w->update_mode_line = Qt;
8995 }
a2889657 8996
5f5c8ee5
GM
8997 /* Try to scroll by specified few lines. */
8998 if ((scroll_conservatively
8999 || scroll_step
9000 || temp_scroll_step
9001 || NUMBERP (current_buffer->scroll_up_aggressively)
9002 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 9003 && !current_buffer->clip_changed
5f5c8ee5
GM
9004 && CHARPOS (startp) >= BEGV
9005 && CHARPOS (startp) <= ZV)
0789adb2 9006 {
5f5c8ee5
GM
9007 /* The function returns -1 if new fonts were loaded, 1 if
9008 successful, 0 if not successful. */
9009 int rc = try_scrolling (window, just_this_one_p,
9010 scroll_conservatively,
9011 scroll_step,
9012 temp_scroll_step);
9013 if (rc > 0)
9014 goto done;
9015 else if (rc < 0)
9016 goto restore_buffers;
9017 }
f9c8af06 9018
5f5c8ee5 9019 /* Finally, just choose place to start which centers point */
5936754e 9020
5f5c8ee5 9021 recenter:
44173109 9022
5f5c8ee5
GM
9023#if GLYPH_DEBUG
9024 debug_method_add (w, "recenter");
9025#endif
0789adb2 9026
5f5c8ee5 9027 /* w->vscroll = 0; */
0789adb2 9028
5f5c8ee5
GM
9029 /* Forget any previously recorded base line for line number display. */
9030 if (!current_matrix_up_to_date_p
9031 || current_buffer->clip_changed)
9032 w->base_line_number = Qnil;
9033
9034 /* Move backward half the height of the window. */
9035 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9036 it.current_y = it.last_visible_y;
9037 move_it_vertically_backward (&it, it.last_visible_y / 2);
9038 xassert (IT_CHARPOS (it) >= BEGV);
9039
9040 /* The function move_it_vertically_backward may move over more
9041 than the specified y-distance. If it->w is small, e.g. a
9042 mini-buffer window, we may end up in front of the window's
9043 display area. Start displaying at the start of the line
9044 containing PT in this case. */
9045 if (it.current_y <= 0)
9046 {
9047 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9048 move_it_vertically (&it, 0);
9049 xassert (IT_CHARPOS (it) <= PT);
9050 it.current_y = 0;
0789adb2
RS
9051 }
9052
5f5c8ee5
GM
9053 it.current_x = it.hpos = 0;
9054
9055 /* Set startp here explicitly in case that helps avoid an infinite loop
9056 in case the window-scroll-functions functions get errors. */
9057 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9058
9059 /* Run scroll hooks. */
9060 startp = run_window_scroll_functions (window, it.current.pos);
9061
9062 /* Redisplay the window. */
9063 if (!current_matrix_up_to_date_p
9064 || windows_or_buffers_changed
9065 /* Don't use try_window_reusing_current_matrix in this case
9066 because it can have changed the buffer. */
9067 || !NILP (Vwindow_scroll_functions)
9068 || !just_this_one_p
9069 || MINI_WINDOW_P (w)
9070 || !try_window_reusing_current_matrix (w))
9071 try_window (window, startp);
9072
9073 /* If new fonts have been loaded (due to fontsets), give up. We
9074 have to start a new redisplay since we need to re-adjust glyph
9075 matrices. */
9076 if (fonts_changed_p)
9077 goto restore_buffers;
9078
9079 /* If cursor did not appear assume that the middle of the window is
9080 in the first line of the window. Do it again with the next line.
9081 (Imagine a window of height 100, displaying two lines of height
9082 60. Moving back 50 from it->last_visible_y will end in the first
9083 line.) */
9084 if (w->cursor.vpos < 0)
a2889657 9085 {
5f5c8ee5
GM
9086 if (!NILP (w->window_end_valid)
9087 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 9088 {
5f5c8ee5
GM
9089 clear_glyph_matrix (w->desired_matrix);
9090 move_it_by_lines (&it, 1, 0);
9091 try_window (window, it.current.pos);
a2889657 9092 }
5f5c8ee5 9093 else if (PT < IT_CHARPOS (it))
a2889657 9094 {
5f5c8ee5
GM
9095 clear_glyph_matrix (w->desired_matrix);
9096 move_it_by_lines (&it, -1, 0);
9097 try_window (window, it.current.pos);
9098 }
9099 else
9100 {
9101 /* Not much we can do about it. */
a2889657 9102 }
a2889657 9103 }
010494d0 9104
5f5c8ee5
GM
9105 /* Consider the following case: Window starts at BEGV, there is
9106 invisible, intangible text at BEGV, so that display starts at
9107 some point START > BEGV. It can happen that we are called with
9108 PT somewhere between BEGV and START. Try to handle that case. */
9109 if (w->cursor.vpos < 0)
835766b6 9110 {
5f5c8ee5
GM
9111 struct glyph_row *row = w->current_matrix->rows;
9112 if (row->mode_line_p)
9113 ++row;
9114 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 9115 }
5f5c8ee5
GM
9116
9117 make_cursor_line_fully_visible (w);
b5174a51 9118
74d481ac
GM
9119 done:
9120
5f5c8ee5
GM
9121 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9122 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9123 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9124 ? Qt : Qnil);
a2889657 9125
5f5c8ee5 9126 /* Display the mode line, if we must. */
e481f960 9127 if ((update_mode_line
aa6d10fa 9128 /* If window not full width, must redo its mode line
5f5c8ee5
GM
9129 if (a) the window to its side is being redone and
9130 (b) we do a frame-based redisplay. This is a consequence
9131 of how inverted lines are drawn in frame-based redisplay. */
9132 || (!just_this_one_p
9133 && !FRAME_WINDOW_P (f)
9134 && !WINDOW_FULL_WIDTH_P (w))
9135 /* Line number to display. */
155ef550 9136 || INTEGERP (w->base_line_pos)
5f5c8ee5 9137 /* Column number is displayed and different from the one displayed. */
155ef550
KH
9138 || (!NILP (w->column_number_displayed)
9139 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
9140 /* This means that the window has a mode line. */
9141 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 9142 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 9143 {
5f5c8ee5
GM
9144 display_mode_lines (w);
9145
9146 /* If mode line height has changed, arrange for a thorough
9147 immediate redisplay using the correct mode line height. */
9148 if (WINDOW_WANTS_MODELINE_P (w)
9149 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 9150 {
5f5c8ee5
GM
9151 fonts_changed_p = 1;
9152 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9153 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 9154 }
5f5c8ee5
GM
9155
9156 /* If top line height has changed, arrange for a thorough
9157 immediate redisplay using the correct mode line height. */
045dee35
GM
9158 if (WINDOW_WANTS_HEADER_LINE_P (w)
9159 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
9160 {
9161 fonts_changed_p = 1;
045dee35
GM
9162 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9163 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9164 }
9165
9166 if (fonts_changed_p)
9167 goto restore_buffers;
5ba50c51 9168 }
5f5c8ee5
GM
9169
9170 if (!line_number_displayed
9171 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
9172 {
9173 w->base_line_pos = Qnil;
9174 w->base_line_number = Qnil;
9175 }
a2889657 9176
5f5c8ee5
GM
9177 finish_menu_bars:
9178
7ce2c095 9179 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 9180 if (update_mode_line
5f5c8ee5
GM
9181 && EQ (FRAME_SELECTED_WINDOW (f), window))
9182 {
9183 int redisplay_menu_p = 0;
9184
9185 if (FRAME_WINDOW_P (f))
9186 {
dc937613 9187#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 9188 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 9189#else
5f5c8ee5 9190 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 9191#endif
5f5c8ee5
GM
9192 }
9193 else
9194 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9195
9196 if (redisplay_menu_p)
9197 display_menu_bar (w);
9198
9199#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
9200 if (WINDOWP (f->tool_bar_window)
9201 && (FRAME_TOOL_BAR_LINES (f) > 0
9202 || auto_resize_tool_bars_p))
9203 redisplay_tool_bar (f);
5f5c8ee5
GM
9204#endif
9205 }
7ce2c095 9206
88f22aff 9207 finish_scroll_bars:
5f5c8ee5 9208
88f22aff 9209 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 9210 {
b1d1124b 9211 int start, end, whole;
30c566e4 9212
b1d1124b 9213 /* Calculate the start and end positions for the current window.
3505ea70
JB
9214 At some point, it would be nice to choose between scrollbars
9215 which reflect the whole buffer size, with special markers
9216 indicating narrowing, and scrollbars which reflect only the
9217 visible region.
9218
5f5c8ee5 9219 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 9220 if (!MINI_WINDOW_P (w)
5f5c8ee5 9221 || (w == XWINDOW (minibuf_window)
c6e89d6c 9222 && NILP (echo_area_buffer[0])))
b1d1124b 9223 {
8a9311d7 9224 whole = ZV - BEGV;
4d641a15 9225 start = marker_position (w->start) - BEGV;
b1d1124b
JB
9226 /* I don't think this is guaranteed to be right. For the
9227 moment, we'll pretend it is. */
5f5c8ee5 9228 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 9229
5f5c8ee5
GM
9230 if (end < start)
9231 end = start;
9232 if (whole < (end - start))
9233 whole = end - start;
b1d1124b
JB
9234 }
9235 else
9236 start = end = whole = 0;
30c566e4 9237
88f22aff 9238 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 9239 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 9240
5f5c8ee5
GM
9241 /* Note that we actually used the scroll bar attached to this
9242 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 9243 (*redeem_scroll_bar_hook) (w);
30c566e4 9244 }
b1d1124b 9245
5f5c8ee5
GM
9246 restore_buffers:
9247
9248 /* Restore current_buffer and value of point in it. */
9249 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
5ba50c51 9250 if (really_switched_buffer)
f72df6ac 9251 set_buffer_internal_1 (old);
e481f960
RS
9252 else
9253 set_buffer_temp (old);
5f5c8ee5 9254 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
9255
9256 unbind_to (count, Qnil);
a2889657 9257}
a2889657 9258
5f5c8ee5
GM
9259
9260/* Build the complete desired matrix of WINDOW with a window start
9261 buffer position POS. Value is non-zero if successful. It is zero
9262 if fonts were loaded during redisplay which makes re-adjusting
9263 glyph matrices necessary. */
9264
9265int
a2889657
JB
9266try_window (window, pos)
9267 Lisp_Object window;
5f5c8ee5
GM
9268 struct text_pos pos;
9269{
9270 struct window *w = XWINDOW (window);
9271 struct it it;
9272 struct glyph_row *last_text_row = NULL;
9cbab4ff 9273
5f5c8ee5
GM
9274 /* Make POS the new window start. */
9275 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 9276
5f5c8ee5
GM
9277 /* Mark cursor position as unknown. No overlay arrow seen. */
9278 w->cursor.vpos = -1;
a2889657 9279 overlay_arrow_seen = 0;
642eefc6 9280
5f5c8ee5
GM
9281 /* Initialize iterator and info to start at POS. */
9282 start_display (&it, w, pos);
a2889657 9283
5f5c8ee5
GM
9284 /* Display all lines of W. */
9285 while (it.current_y < it.last_visible_y)
9286 {
9287 if (display_line (&it))
9288 last_text_row = it.glyph_row - 1;
9289 if (fonts_changed_p)
9290 return 0;
9291 }
a2889657 9292
5f5c8ee5
GM
9293 /* If bottom moved off end of frame, change mode line percentage. */
9294 if (XFASTINT (w->window_end_pos) <= 0
9295 && Z != IT_CHARPOS (it))
a2889657
JB
9296 w->update_mode_line = Qt;
9297
5f5c8ee5
GM
9298 /* Set window_end_pos to the offset of the last character displayed
9299 on the window from the end of current_buffer. Set
9300 window_end_vpos to its row number. */
9301 if (last_text_row)
9302 {
9303 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9304 w->window_end_bytepos
9305 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9306 XSETFASTINT (w->window_end_pos,
9307 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9308 XSETFASTINT (w->window_end_vpos,
9309 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9310 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9311 ->displays_text_p);
9312 }
9313 else
9314 {
9315 w->window_end_bytepos = 0;
9316 XSETFASTINT (w->window_end_pos, 0);
9317 XSETFASTINT (w->window_end_vpos, 0);
9318 }
9319
a2889657
JB
9320 /* But that is not valid info until redisplay finishes. */
9321 w->window_end_valid = Qnil;
5f5c8ee5 9322 return 1;
a2889657 9323}
5f5c8ee5
GM
9324
9325
a2889657 9326\f
5f5c8ee5
GM
9327/************************************************************************
9328 Window redisplay reusing current matrix when buffer has not changed
9329 ************************************************************************/
9330
9331/* Try redisplay of window W showing an unchanged buffer with a
9332 different window start than the last time it was displayed by
9333 reusing its current matrix. Value is non-zero if successful.
9334 W->start is the new window start. */
a2889657
JB
9335
9336static int
5f5c8ee5
GM
9337try_window_reusing_current_matrix (w)
9338 struct window *w;
a2889657 9339{
5f5c8ee5
GM
9340 struct frame *f = XFRAME (w->frame);
9341 struct glyph_row *row, *bottom_row;
9342 struct it it;
9343 struct run run;
9344 struct text_pos start, new_start;
9345 int nrows_scrolled, i;
9346 struct glyph_row *last_text_row;
9347 struct glyph_row *last_reused_text_row;
9348 struct glyph_row *start_row;
9349 int start_vpos, min_y, max_y;
9350
9351 /* Right now this function doesn't handle terminal frames. */
9352 if (!FRAME_WINDOW_P (f))
9353 return 0;
a2889657 9354
5f5c8ee5
GM
9355 /* Can't do this if region may have changed. */
9356 if ((!NILP (Vtransient_mark_mode)
9357 && !NILP (current_buffer->mark_active))
8f897821
GM
9358 || !NILP (w->region_showing)
9359 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 9360 return 0;
a2889657 9361
5f5c8ee5 9362 /* If top-line visibility has changed, give up. */
045dee35
GM
9363 if (WINDOW_WANTS_HEADER_LINE_P (w)
9364 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
9365 return 0;
9366
9367 /* Give up if old or new display is scrolled vertically. We could
9368 make this function handle this, but right now it doesn't. */
9369 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9370 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9371 return 0;
9372
9373 /* The variable new_start now holds the new window start. The old
9374 start `start' can be determined from the current matrix. */
9375 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9376 start = start_row->start.pos;
9377 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 9378
5f5c8ee5
GM
9379 /* Clear the desired matrix for the display below. */
9380 clear_glyph_matrix (w->desired_matrix);
9381
9382 if (CHARPOS (new_start) <= CHARPOS (start))
9383 {
9384 int first_row_y;
9385
9386 IF_DEBUG (debug_method_add (w, "twu1"));
9387
9388 /* Display up to a row that can be reused. The variable
9389 last_text_row is set to the last row displayed that displays
9390 text. */
9391 start_display (&it, w, new_start);
9392 first_row_y = it.current_y;
9393 w->cursor.vpos = -1;
9394 last_text_row = last_reused_text_row = NULL;
9395 while (it.current_y < it.last_visible_y
9396 && IT_CHARPOS (it) < CHARPOS (start)
9397 && !fonts_changed_p)
9398 if (display_line (&it))
9399 last_text_row = it.glyph_row - 1;
9400
9401 /* A value of current_y < last_visible_y means that we stopped
9402 at the previous window start, which in turn means that we
9403 have at least one reusable row. */
9404 if (it.current_y < it.last_visible_y)
a2889657 9405 {
5f5c8ee5
GM
9406 nrows_scrolled = it.vpos;
9407
9408 /* Find PT if not already found in the lines displayed. */
9409 if (w->cursor.vpos < 0)
a2889657 9410 {
5f5c8ee5
GM
9411 int dy = it.current_y - first_row_y;
9412
9413 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9414 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9415 {
9416 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9417 && PT < MATRIX_ROW_END_CHARPOS (row))
9418 {
9419 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9420 dy, nrows_scrolled);
9421 break;
9422 }
9423
9424 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9425 break;
9426
9427 ++row;
9428 }
9429
9430 /* Give up if point was not found. This shouldn't
9431 happen often; not more often than with try_window
9432 itself. */
9433 if (w->cursor.vpos < 0)
9434 {
9435 clear_glyph_matrix (w->desired_matrix);
9436 return 0;
9437 }
a2889657 9438 }
5f5c8ee5
GM
9439
9440 /* Scroll the display. Do it before the current matrix is
9441 changed. The problem here is that update has not yet
9442 run, i.e. part of the current matrix is not up to date.
9443 scroll_run_hook will clear the cursor, and use the
9444 current matrix to get the height of the row the cursor is
9445 in. */
9446 run.current_y = first_row_y;
9447 run.desired_y = it.current_y;
9448 run.height = it.last_visible_y - it.current_y;
15e26c76
GM
9449 if (run.height > 0
9450 && run.current_y != run.desired_y)
a2889657 9451 {
5f5c8ee5
GM
9452 update_begin (f);
9453 rif->update_window_begin_hook (w);
9454 rif->scroll_run_hook (w, &run);
9455 rif->update_window_end_hook (w, 0);
9456 update_end (f);
a2889657 9457 }
5f5c8ee5
GM
9458
9459 /* Shift current matrix down by nrows_scrolled lines. */
9460 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9461 rotate_matrix (w->current_matrix,
9462 start_vpos,
9463 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9464 nrows_scrolled);
9465
9466 /* Disable lines not reused. */
9467 for (i = 0; i < it.vpos; ++i)
9468 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9469
9470 /* Re-compute Y positions. */
9471 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
045dee35 9472 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9473 max_y = it.last_visible_y;
9474 while (row < bottom_row)
d2f84654 9475 {
5f5c8ee5
GM
9476 row->y = it.current_y;
9477
9478 if (row->y < min_y)
9479 row->visible_height = row->height - (min_y - row->y);
9480 else if (row->y + row->height > max_y)
9481 row->visible_height
9482 = row->height - (row->y + row->height - max_y);
9483 else
9484 row->visible_height = row->height;
9485
9486 it.current_y += row->height;
9487 ++it.vpos;
9488
9489 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9490 last_reused_text_row = row;
9491 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9492 break;
9493 ++row;
d2f84654 9494 }
a2889657 9495 }
5f5c8ee5
GM
9496
9497 /* Update window_end_pos etc.; last_reused_text_row is the last
9498 reused row from the current matrix containing text, if any.
9499 The value of last_text_row is the last displayed line
9500 containing text. */
9501 if (last_reused_text_row)
a2889657 9502 {
5f5c8ee5
GM
9503 w->window_end_bytepos
9504 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9505 XSETFASTINT (w->window_end_pos,
9506 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9507 XSETFASTINT (w->window_end_vpos,
9508 MATRIX_ROW_VPOS (last_reused_text_row,
9509 w->current_matrix));
a2889657 9510 }
5f5c8ee5
GM
9511 else if (last_text_row)
9512 {
9513 w->window_end_bytepos
9514 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9515 XSETFASTINT (w->window_end_pos,
9516 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9517 XSETFASTINT (w->window_end_vpos,
9518 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9519 }
9520 else
9521 {
9522 /* This window must be completely empty. */
9523 w->window_end_bytepos = 0;
9524 XSETFASTINT (w->window_end_pos, 0);
9525 XSETFASTINT (w->window_end_vpos, 0);
9526 }
9527 w->window_end_valid = Qnil;
a2889657 9528
5f5c8ee5
GM
9529 /* Update hint: don't try scrolling again in update_window. */
9530 w->desired_matrix->no_scrolling_p = 1;
9531
9532#if GLYPH_DEBUG
9533 debug_method_add (w, "try_window_reusing_current_matrix 1");
9534#endif
9535 return 1;
a2889657 9536 }
5f5c8ee5
GM
9537 else if (CHARPOS (new_start) > CHARPOS (start))
9538 {
9539 struct glyph_row *pt_row, *row;
9540 struct glyph_row *first_reusable_row;
9541 struct glyph_row *first_row_to_display;
9542 int dy;
9543 int yb = window_text_bottom_y (w);
9544
9545 IF_DEBUG (debug_method_add (w, "twu2"));
9546
9547 /* Find the row starting at new_start, if there is one. Don't
9548 reuse a partially visible line at the end. */
9549 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9550 while (first_reusable_row->enabled_p
9551 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9552 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9553 < CHARPOS (new_start)))
9554 ++first_reusable_row;
9555
9556 /* Give up if there is no row to reuse. */
9557 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
9558 || !first_reusable_row->enabled_p
9559 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9560 != CHARPOS (new_start)))
5f5c8ee5
GM
9561 return 0;
9562
5f5c8ee5
GM
9563 /* We can reuse fully visible rows beginning with
9564 first_reusable_row to the end of the window. Set
9565 first_row_to_display to the first row that cannot be reused.
9566 Set pt_row to the row containing point, if there is any. */
9567 first_row_to_display = first_reusable_row;
9568 pt_row = NULL;
9569 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9570 {
9571 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9572 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9573 pt_row = first_row_to_display;
a2889657 9574
5f5c8ee5
GM
9575 ++first_row_to_display;
9576 }
a2889657 9577
5f5c8ee5
GM
9578 /* Start displaying at the start of first_row_to_display. */
9579 xassert (first_row_to_display->y < yb);
9580 init_to_row_start (&it, w, first_row_to_display);
9581 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9582 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9583 - nrows_scrolled);
9584 it.current_y = first_row_to_display->y - first_reusable_row->y;
9585
9586 /* Display lines beginning with first_row_to_display in the
9587 desired matrix. Set last_text_row to the last row displayed
9588 that displays text. */
9589 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9590 if (pt_row == NULL)
9591 w->cursor.vpos = -1;
9592 last_text_row = NULL;
9593 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9594 if (display_line (&it))
9595 last_text_row = it.glyph_row - 1;
9596
9597 /* Give up If point isn't in a row displayed or reused. */
9598 if (w->cursor.vpos < 0)
9599 {
9600 clear_glyph_matrix (w->desired_matrix);
9601 return 0;
9602 }
12adba34 9603
5f5c8ee5
GM
9604 /* If point is in a reused row, adjust y and vpos of the cursor
9605 position. */
9606 if (pt_row)
9607 {
9608 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9609 w->current_matrix);
9610 w->cursor.y -= first_reusable_row->y;
a2889657
JB
9611 }
9612
5f5c8ee5
GM
9613 /* Scroll the display. */
9614 run.current_y = first_reusable_row->y;
045dee35 9615 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9616 run.height = it.last_visible_y - run.current_y;
9617 if (run.height)
9618 {
9619 struct frame *f = XFRAME (WINDOW_FRAME (w));
9620 update_begin (f);
9621 rif->update_window_begin_hook (w);
9622 rif->scroll_run_hook (w, &run);
9623 rif->update_window_end_hook (w, 0);
9624 update_end (f);
9625 }
a2889657 9626
5f5c8ee5
GM
9627 /* Adjust Y positions of reused rows. */
9628 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9629 row = first_reusable_row;
9630 dy = first_reusable_row->y;
045dee35 9631 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9632 max_y = it.last_visible_y;
9633 while (row < first_row_to_display)
9634 {
9635 row->y -= dy;
9636 if (row->y < min_y)
9637 row->visible_height = row->height - (min_y - row->y);
9638 else if (row->y + row->height > max_y)
9639 row->visible_height
9640 = row->height - (row->y + row->height - max_y);
9641 else
9642 row->visible_height = row->height;
9643 ++row;
9644 }
a2889657 9645
5f5c8ee5
GM
9646 /* Disable rows not reused. */
9647 while (row < bottom_row)
9648 {
9649 row->enabled_p = 0;
9650 ++row;
9651 }
9652
9653 /* Scroll the current matrix. */
9654 xassert (nrows_scrolled > 0);
9655 rotate_matrix (w->current_matrix,
9656 start_vpos,
9657 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9658 -nrows_scrolled);
9659
9660 /* Adjust window end. A null value of last_text_row means that
9661 the window end is in reused rows which in turn means that
9662 only its vpos can have changed. */
9663 if (last_text_row)
9664 {
9665 w->window_end_bytepos
9666 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9667 XSETFASTINT (w->window_end_pos,
9668 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9669 XSETFASTINT (w->window_end_vpos,
9670 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9671 }
9672 else
a2889657 9673 {
e8e536a9 9674 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 9675 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 9676 }
5f5c8ee5
GM
9677
9678 w->window_end_valid = Qnil;
9679 w->desired_matrix->no_scrolling_p = 1;
9680
9681#if GLYPH_DEBUG
9682 debug_method_add (w, "try_window_reusing_current_matrix 2");
9683#endif
9684 return 1;
a2889657 9685 }
5f5c8ee5
GM
9686
9687 return 0;
9688}
a2889657 9689
a2889657 9690
5f5c8ee5
GM
9691\f
9692/************************************************************************
9693 Window redisplay reusing current matrix when buffer has changed
9694 ************************************************************************/
9695
9696static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9697static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9698 int *, int *));
9699static struct glyph_row *
9700find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9701 struct glyph_row *));
9702
9703
9704/* Return the last row in MATRIX displaying text. If row START is
9705 non-null, start searching with that row. IT gives the dimensions
9706 of the display. Value is null if matrix is empty; otherwise it is
9707 a pointer to the row found. */
9708
9709static struct glyph_row *
9710find_last_row_displaying_text (matrix, it, start)
9711 struct glyph_matrix *matrix;
9712 struct it *it;
9713 struct glyph_row *start;
9714{
9715 struct glyph_row *row, *row_found;
9716
9717 /* Set row_found to the last row in IT->w's current matrix
9718 displaying text. The loop looks funny but think of partially
9719 visible lines. */
9720 row_found = NULL;
9721 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9722 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9723 {
9724 xassert (row->enabled_p);
9725 row_found = row;
9726 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9727 break;
9728 ++row;
a2889657 9729 }
5f5c8ee5
GM
9730
9731 return row_found;
9732}
9733
a2889657 9734
5f5c8ee5
GM
9735/* Return the last row in the current matrix of W that is not affected
9736 by changes at the start of current_buffer that occurred since the
9737 last time W was redisplayed. Value is null if no such row exists.
a2889657 9738
5f5c8ee5
GM
9739 The global variable beg_unchanged has to contain the number of
9740 bytes unchanged at the start of current_buffer. BEG +
9741 beg_unchanged is the buffer position of the first changed byte in
9742 current_buffer. Characters at positions < BEG + beg_unchanged are
9743 at the same buffer positions as they were when the current matrix
9744 was built. */
9745
9746static struct glyph_row *
9747get_last_unchanged_at_beg_row (w)
9748 struct window *w;
9749{
9142dd5b 9750 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
9751 struct glyph_row *row;
9752 struct glyph_row *row_found = NULL;
9753 int yb = window_text_bottom_y (w);
9754
9755 /* Find the last row displaying unchanged text. */
9756 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9757 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9758 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 9759 {
5f5c8ee5
GM
9760 if (/* If row ends before first_changed_pos, it is unchanged,
9761 except in some case. */
9762 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9763 /* When row ends in ZV and we write at ZV it is not
9764 unchanged. */
9765 && !row->ends_at_zv_p
9766 /* When first_changed_pos is the end of a continued line,
9767 row is not unchanged because it may be no longer
9768 continued. */
9769 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9770 && row->continued_p))
9771 row_found = row;
9772
9773 /* Stop if last visible row. */
9774 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9775 break;
9776
9777 ++row;
a2889657
JB
9778 }
9779
5f5c8ee5 9780 return row_found;
a2889657 9781}
5f5c8ee5
GM
9782
9783
9784/* Find the first glyph row in the current matrix of W that is not
9785 affected by changes at the end of current_buffer since the last
9786 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
9787 chars by which buffer positions in unchanged text at the end of
9788 current_buffer must be adjusted. Return in *DELTA_BYTES the
9789 corresponding number of bytes. Value is null if no such row
9790 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
9791
9792static struct glyph_row *
9793get_first_unchanged_at_end_row (w, delta, delta_bytes)
9794 struct window *w;
9795 int *delta, *delta_bytes;
a2889657 9796{
5f5c8ee5
GM
9797 struct glyph_row *row;
9798 struct glyph_row *row_found = NULL;
c581d710 9799
5f5c8ee5 9800 *delta = *delta_bytes = 0;
b2a76982 9801
5f5c8ee5
GM
9802 /* A value of window_end_pos >= end_unchanged means that the window
9803 end is in the range of changed text. If so, there is no
9804 unchanged row at the end of W's current matrix. */
9805 xassert (!NILP (w->window_end_valid));
9142dd5b 9806 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
9807 return NULL;
9808
9809 /* Set row to the last row in W's current matrix displaying text. */
9810 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9811
5f5c8ee5
GM
9812 /* If matrix is entirely empty, no unchanged row exists. */
9813 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9814 {
9815 /* The value of row is the last glyph row in the matrix having a
9816 meaningful buffer position in it. The end position of row
9817 corresponds to window_end_pos. This allows us to translate
9818 buffer positions in the current matrix to current buffer
9819 positions for characters not in changed text. */
9820 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9821 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9822 int last_unchanged_pos, last_unchanged_pos_old;
9823 struct glyph_row *first_text_row
9824 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9825
9826 *delta = Z - Z_old;
9827 *delta_bytes = Z_BYTE - Z_BYTE_old;
9828
9829 /* Set last_unchanged_pos to the buffer position of the last
9830 character in the buffer that has not been changed. Z is the
9831 index + 1 of the last byte in current_buffer, i.e. by
9832 subtracting end_unchanged we get the index of the last
9833 unchanged character, and we have to add BEG to get its buffer
9834 position. */
9142dd5b 9835 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
9836 last_unchanged_pos_old = last_unchanged_pos - *delta;
9837
9838 /* Search backward from ROW for a row displaying a line that
9839 starts at a minimum position >= last_unchanged_pos_old. */
9840 while (row >= first_text_row)
9841 {
9842 xassert (row->enabled_p);
9843 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9844
9845 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9846 row_found = row;
9847 --row;
9848 }
9849 }
9850
9851 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9852 return row_found;
c581d710
RS
9853}
9854
c581d710 9855
5f5c8ee5
GM
9856/* Make sure that glyph rows in the current matrix of window W
9857 reference the same glyph memory as corresponding rows in the
9858 frame's frame matrix. This function is called after scrolling W's
9859 current matrix on a terminal frame in try_window_id and
9860 try_window_reusing_current_matrix. */
9861
9862static void
9863sync_frame_with_window_matrix_rows (w)
9864 struct window *w;
c581d710 9865{
5f5c8ee5
GM
9866 struct frame *f = XFRAME (w->frame);
9867 struct glyph_row *window_row, *window_row_end, *frame_row;
9868
9869 /* Preconditions: W must be a leaf window and full-width. Its frame
9870 must have a frame matrix. */
9871 xassert (NILP (w->hchild) && NILP (w->vchild));
9872 xassert (WINDOW_FULL_WIDTH_P (w));
9873 xassert (!FRAME_WINDOW_P (f));
9874
9875 /* If W is a full-width window, glyph pointers in W's current matrix
9876 have, by definition, to be the same as glyph pointers in the
9877 corresponding frame matrix. */
9878 window_row = w->current_matrix->rows;
9879 window_row_end = window_row + w->current_matrix->nrows;
9880 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9881 while (window_row < window_row_end)
659a218f 9882 {
5f5c8ee5 9883 int area;
f002db93 9884
5f5c8ee5
GM
9885 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9886 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
9887
9888 /* Disable frame rows whose corresponding window rows have
9889 been disabled in try_window_id. */
9890 if (!window_row->enabled_p)
9891 frame_row->enabled_p = 0;
9892
5f5c8ee5 9893 ++window_row, ++frame_row;
659a218f 9894 }
a2889657 9895}
5f5c8ee5
GM
9896
9897
e037b9ec
GM
9898/* Find the glyph row in window W containing CHARPOS. Consider all
9899 rows between START and END (not inclusive). END null means search
9900 all rows to the end of the display area of W. Value is the row
9901 containing CHARPOS or null. */
9902
9903static struct glyph_row *
9904row_containing_pos (w, charpos, start, end)
9905 struct window *w;
9906 int charpos;
9907 struct glyph_row *start, *end;
9908{
9909 struct glyph_row *row = start;
9910 int last_y;
9911
9912 /* If we happen to start on a header-line, skip that. */
9913 if (row->mode_line_p)
9914 ++row;
9915
9916 if ((end && row >= end) || !row->enabled_p)
9917 return NULL;
9918
9919 last_y = window_text_bottom_y (w);
9920
9921 while ((end == NULL || row < end)
9922 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9923 /* The end position of a row equals the start
9924 position of the next row. If CHARPOS is there, we
9925 would rather display it in the next line, except
9926 when this line ends in ZV. */
9927 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9928 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9929 || !row->ends_at_zv_p)))
9930 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9931 ++row;
9932
9933 /* Give up if CHARPOS not found. */
9934 if ((end && row >= end)
9935 || charpos < MATRIX_ROW_START_CHARPOS (row)
9936 || charpos > MATRIX_ROW_END_CHARPOS (row))
9937 row = NULL;
9938
9939 return row;
9940}
9941
9942
5f5c8ee5
GM
9943/* Try to redisplay window W by reusing its existing display. W's
9944 current matrix must be up to date when this function is called,
9945 i.e. window_end_valid must not be nil.
9946
9947 Value is
9948
9949 1 if display has been updated
9950 0 if otherwise unsuccessful
9951 -1 if redisplay with same window start is known not to succeed
9952
9953 The following steps are performed:
9954
9955 1. Find the last row in the current matrix of W that is not
9956 affected by changes at the start of current_buffer. If no such row
9957 is found, give up.
9958
9959 2. Find the first row in W's current matrix that is not affected by
9960 changes at the end of current_buffer. Maybe there is no such row.
9961
9962 3. Display lines beginning with the row + 1 found in step 1 to the
9963 row found in step 2 or, if step 2 didn't find a row, to the end of
9964 the window.
9965
9966 4. If cursor is not known to appear on the window, give up.
9967
9968 5. If display stopped at the row found in step 2, scroll the
9969 display and current matrix as needed.
9970
9971 6. Maybe display some lines at the end of W, if we must. This can
9972 happen under various circumstances, like a partially visible line
9973 becoming fully visible, or because newly displayed lines are displayed
9974 in smaller font sizes.
9975
9976 7. Update W's window end information. */
9977
9978 /* Check that window end is what we expect it to be. */
12adba34
RS
9979
9980static int
5f5c8ee5 9981try_window_id (w)
12adba34 9982 struct window *w;
12adba34 9983{
5f5c8ee5
GM
9984 struct frame *f = XFRAME (w->frame);
9985 struct glyph_matrix *current_matrix = w->current_matrix;
9986 struct glyph_matrix *desired_matrix = w->desired_matrix;
9987 struct glyph_row *last_unchanged_at_beg_row;
9988 struct glyph_row *first_unchanged_at_end_row;
9989 struct glyph_row *row;
9990 struct glyph_row *bottom_row;
9991 int bottom_vpos;
9992 struct it it;
9993 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9994 struct text_pos start_pos;
9995 struct run run;
9996 int first_unchanged_at_end_vpos = 0;
9997 struct glyph_row *last_text_row, *last_text_row_at_end;
9998 struct text_pos start;
9999
10000 SET_TEXT_POS_FROM_MARKER (start, w->start);
10001
10002 /* Check pre-conditions. Window end must be valid, otherwise
10003 the current matrix would not be up to date. */
10004 xassert (!NILP (w->window_end_valid));
10005 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10006 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10007
10008 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10009 only if buffer has really changed. The reason is that the gap is
10010 initially at Z for freshly visited files. The code below would
10011 set end_unchanged to 0 in that case. */
28ee91c0
GM
10012 if (MODIFF > SAVE_MODIFF
10013 /* This seems to happen sometimes after saving a buffer. */
10014 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 10015 {
9142dd5b
GM
10016 if (GPT - BEG < BEG_UNCHANGED)
10017 BEG_UNCHANGED = GPT - BEG;
10018 if (Z - GPT < END_UNCHANGED)
10019 END_UNCHANGED = Z - GPT;
5f5c8ee5 10020 }
bf9249e3 10021
5f5c8ee5
GM
10022 /* If window starts after a line end, and the last change is in
10023 front of that newline, then changes don't affect the display.
10024 This case happens with stealth-fontification. */
10025 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10026 if (CHARPOS (start) > BEGV
9142dd5b 10027 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
10028 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10029 && PT < MATRIX_ROW_END_CHARPOS (row))
10030 {
10031 /* We have to update window end positions because the buffer's
10032 size has changed. */
10033 w->window_end_pos
10034 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10035 w->window_end_bytepos
10036 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
10037 return 1;
10038 }
10039
10040 /* Return quickly if changes are all below what is displayed in the
10041 window, and if PT is in the window. */
9142dd5b 10042 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
10043 && PT < MATRIX_ROW_END_CHARPOS (row))
10044 {
10045 /* We have to update window end positions because the buffer's
10046 size has changed. */
10047 w->window_end_pos
10048 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10049 w->window_end_bytepos
10050 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
10051 return 1;
10052 }
10053
10054 /* Check that window start agrees with the start of the first glyph
10055 row in its current matrix. Check this after we know the window
10056 start is not in changed text, otherwise positions would not be
10057 comparable. */
10058 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10059 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10060 return 0;
10061
5f5c8ee5
GM
10062 /* Compute the position at which we have to start displaying new
10063 lines. Some of the lines at the top of the window might be
10064 reusable because they are not displaying changed text. Find the
10065 last row in W's current matrix not affected by changes at the
10066 start of current_buffer. Value is null if changes start in the
10067 first line of window. */
10068 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10069 if (last_unchanged_at_beg_row)
10070 {
10071 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10072 start_pos = it.current.pos;
10073
10074 /* Start displaying new lines in the desired matrix at the same
10075 vpos we would use in the current matrix, i.e. below
10076 last_unchanged_at_beg_row. */
10077 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10078 current_matrix);
10079 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10080 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10081
10082 xassert (it.hpos == 0 && it.current_x == 0);
10083 }
10084 else
10085 {
10086 /* There are no reusable lines at the start of the window.
10087 Start displaying in the first line. */
10088 start_display (&it, w, start);
10089 start_pos = it.current.pos;
10090 }
10091
5f5c8ee5
GM
10092 /* Find the first row that is not affected by changes at the end of
10093 the buffer. Value will be null if there is no unchanged row, in
10094 which case we must redisplay to the end of the window. delta
10095 will be set to the value by which buffer positions beginning with
10096 first_unchanged_at_end_row have to be adjusted due to text
10097 changes. */
10098 first_unchanged_at_end_row
10099 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10100 IF_DEBUG (debug_delta = delta);
10101 IF_DEBUG (debug_delta_bytes = delta_bytes);
10102
10103 /* Set stop_pos to the buffer position up to which we will have to
10104 display new lines. If first_unchanged_at_end_row != NULL, this
10105 is the buffer position of the start of the line displayed in that
10106 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10107 that we don't stop at a buffer position. */
10108 stop_pos = 0;
10109 if (first_unchanged_at_end_row)
10110 {
10111 xassert (last_unchanged_at_beg_row == NULL
10112 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10113
10114 /* If this is a continuation line, move forward to the next one
10115 that isn't. Changes in lines above affect this line.
10116 Caution: this may move first_unchanged_at_end_row to a row
10117 not displaying text. */
10118 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10119 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10120 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10121 < it.last_visible_y))
10122 ++first_unchanged_at_end_row;
10123
10124 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10125 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10126 >= it.last_visible_y))
10127 first_unchanged_at_end_row = NULL;
10128 else
10129 {
10130 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10131 + delta);
10132 first_unchanged_at_end_vpos
10133 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 10134 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
10135 }
10136 }
10137 else if (last_unchanged_at_beg_row == NULL)
10138 return 0;
10139
10140
10141#if GLYPH_DEBUG
10142
10143 /* Either there is no unchanged row at the end, or the one we have
10144 now displays text. This is a necessary condition for the window
10145 end pos calculation at the end of this function. */
10146 xassert (first_unchanged_at_end_row == NULL
10147 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10148
10149 debug_last_unchanged_at_beg_vpos
10150 = (last_unchanged_at_beg_row
10151 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10152 : -1);
10153 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10154
10155#endif /* GLYPH_DEBUG != 0 */
10156
10157
10158 /* Display new lines. Set last_text_row to the last new line
10159 displayed which has text on it, i.e. might end up as being the
10160 line where the window_end_vpos is. */
10161 w->cursor.vpos = -1;
10162 last_text_row = NULL;
10163 overlay_arrow_seen = 0;
10164 while (it.current_y < it.last_visible_y
10165 && !fonts_changed_p
10166 && (first_unchanged_at_end_row == NULL
10167 || IT_CHARPOS (it) < stop_pos))
10168 {
10169 if (display_line (&it))
10170 last_text_row = it.glyph_row - 1;
10171 }
10172
10173 if (fonts_changed_p)
10174 return -1;
10175
10176
10177 /* Compute differences in buffer positions, y-positions etc. for
10178 lines reused at the bottom of the window. Compute what we can
10179 scroll. */
ca42b2e8
GM
10180 if (first_unchanged_at_end_row
10181 /* No lines reused because we displayed everything up to the
10182 bottom of the window. */
10183 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
10184 {
10185 dvpos = (it.vpos
10186 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10187 current_matrix));
10188 dy = it.current_y - first_unchanged_at_end_row->y;
10189 run.current_y = first_unchanged_at_end_row->y;
10190 run.desired_y = run.current_y + dy;
10191 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10192 }
10193 else
ca42b2e8
GM
10194 {
10195 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10196 first_unchanged_at_end_row = NULL;
10197 }
5f5c8ee5
GM
10198 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10199
8f8ba186 10200
5f5c8ee5
GM
10201 /* Find the cursor if not already found. We have to decide whether
10202 PT will appear on this window (it sometimes doesn't, but this is
10203 not a very frequent case.) This decision has to be made before
10204 the current matrix is altered. A value of cursor.vpos < 0 means
10205 that PT is either in one of the lines beginning at
10206 first_unchanged_at_end_row or below the window. Don't care for
10207 lines that might be displayed later at the window end; as
10208 mentioned, this is not a frequent case. */
10209 if (w->cursor.vpos < 0)
10210 {
5f5c8ee5
GM
10211 /* Cursor in unchanged rows at the top? */
10212 if (PT < CHARPOS (start_pos)
10213 && last_unchanged_at_beg_row)
10214 {
e037b9ec
GM
10215 row = row_containing_pos (w, PT,
10216 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10217 last_unchanged_at_beg_row + 1);
10218 xassert (row && row <= last_unchanged_at_beg_row);
5f5c8ee5
GM
10219 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10220 }
10221
10222 /* Start from first_unchanged_at_end_row looking for PT. */
10223 else if (first_unchanged_at_end_row)
10224 {
e037b9ec
GM
10225 row = row_containing_pos (w, PT - delta,
10226 first_unchanged_at_end_row, NULL);
10227 if (row)
468155d7
GM
10228 set_cursor_from_row (w, row, w->current_matrix, delta,
10229 delta_bytes, dy, dvpos);
5f5c8ee5
GM
10230 }
10231
10232 /* Give up if cursor was not found. */
10233 if (w->cursor.vpos < 0)
10234 {
10235 clear_glyph_matrix (w->desired_matrix);
10236 return -1;
10237 }
10238 }
10239
10240 /* Don't let the cursor end in the scroll margins. */
10241 {
10242 int this_scroll_margin, cursor_height;
10243
10244 this_scroll_margin = max (0, scroll_margin);
10245 this_scroll_margin = min (this_scroll_margin,
10246 XFASTINT (w->height) / 4);
10247 this_scroll_margin *= CANON_Y_UNIT (it.f);
10248 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10249
10250 if ((w->cursor.y < this_scroll_margin
10251 && CHARPOS (start) > BEGV)
10252 /* Don't take scroll margin into account at the bottom because
10253 old redisplay didn't do it either. */
10254 || w->cursor.y + cursor_height > it.last_visible_y)
10255 {
10256 w->cursor.vpos = -1;
10257 clear_glyph_matrix (w->desired_matrix);
10258 return -1;
10259 }
10260 }
10261
10262 /* Scroll the display. Do it before changing the current matrix so
10263 that xterm.c doesn't get confused about where the cursor glyph is
10264 found. */
10265 if (dy)
10266 {
10267 update_begin (f);
10268
10269 if (FRAME_WINDOW_P (f))
10270 {
10271 rif->update_window_begin_hook (w);
10272 rif->scroll_run_hook (w, &run);
10273 rif->update_window_end_hook (w, 0);
10274 }
10275 else
10276 {
10277 /* Terminal frame. In this case, dvpos gives the number of
10278 lines to scroll by; dvpos < 0 means scroll up. */
10279 int first_unchanged_at_end_vpos
10280 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10281 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10282 int end = XFASTINT (w->top) + window_internal_height (w);
10283
10284 /* Perform the operation on the screen. */
10285 if (dvpos > 0)
10286 {
10287 /* Scroll last_unchanged_at_beg_row to the end of the
10288 window down dvpos lines. */
10289 set_terminal_window (end);
10290
10291 /* On dumb terminals delete dvpos lines at the end
10292 before inserting dvpos empty lines. */
10293 if (!scroll_region_ok)
10294 ins_del_lines (end - dvpos, -dvpos);
10295
10296 /* Insert dvpos empty lines in front of
10297 last_unchanged_at_beg_row. */
10298 ins_del_lines (from, dvpos);
10299 }
10300 else if (dvpos < 0)
10301 {
10302 /* Scroll up last_unchanged_at_beg_vpos to the end of
10303 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10304 set_terminal_window (end);
10305
10306 /* Delete dvpos lines in front of
10307 last_unchanged_at_beg_vpos. ins_del_lines will set
10308 the cursor to the given vpos and emit |dvpos| delete
10309 line sequences. */
10310 ins_del_lines (from + dvpos, dvpos);
10311
10312 /* On a dumb terminal insert dvpos empty lines at the
10313 end. */
10314 if (!scroll_region_ok)
10315 ins_del_lines (end + dvpos, -dvpos);
10316 }
10317
10318 set_terminal_window (0);
10319 }
10320
10321 update_end (f);
10322 }
10323
ca42b2e8
GM
10324 /* Shift reused rows of the current matrix to the right position.
10325 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10326 text. */
10327 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10328 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
10329 if (dvpos < 0)
10330 {
10331 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10332 bottom_vpos, dvpos);
10333 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10334 bottom_vpos, 0);
10335 }
10336 else if (dvpos > 0)
10337 {
10338 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10339 bottom_vpos, dvpos);
10340 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10341 first_unchanged_at_end_vpos + dvpos, 0);
10342 }
10343
10344 /* For frame-based redisplay, make sure that current frame and window
10345 matrix are in sync with respect to glyph memory. */
10346 if (!FRAME_WINDOW_P (f))
10347 sync_frame_with_window_matrix_rows (w);
10348
10349 /* Adjust buffer positions in reused rows. */
10350 if (delta)
10351 increment_glyph_matrix_buffer_positions (current_matrix,
10352 first_unchanged_at_end_vpos + dvpos,
10353 bottom_vpos, delta, delta_bytes);
10354
10355 /* Adjust Y positions. */
10356 if (dy)
10357 shift_glyph_matrix (w, current_matrix,
10358 first_unchanged_at_end_vpos + dvpos,
10359 bottom_vpos, dy);
10360
10361 if (first_unchanged_at_end_row)
10362 first_unchanged_at_end_row += dvpos;
10363
10364 /* If scrolling up, there may be some lines to display at the end of
10365 the window. */
10366 last_text_row_at_end = NULL;
10367 if (dy < 0)
10368 {
10369 /* Set last_row to the glyph row in the current matrix where the
10370 window end line is found. It has been moved up or down in
10371 the matrix by dvpos. */
10372 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10373 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10374
10375 /* If last_row is the window end line, it should display text. */
10376 xassert (last_row->displays_text_p);
10377
10378 /* If window end line was partially visible before, begin
10379 displaying at that line. Otherwise begin displaying with the
10380 line following it. */
10381 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10382 {
10383 init_to_row_start (&it, w, last_row);
10384 it.vpos = last_vpos;
10385 it.current_y = last_row->y;
10386 }
10387 else
10388 {
10389 init_to_row_end (&it, w, last_row);
10390 it.vpos = 1 + last_vpos;
10391 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10392 ++last_row;
10393 }
12adba34 10394
5f5c8ee5
GM
10395 /* We may start in a continuation line. If so, we have to get
10396 the right continuation_lines_width and current_x. */
10397 it.continuation_lines_width = last_row->continuation_lines_width;
10398 it.hpos = it.current_x = 0;
10399
10400 /* Display the rest of the lines at the window end. */
10401 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10402 while (it.current_y < it.last_visible_y
10403 && !fonts_changed_p)
10404 {
10405 /* Is it always sure that the display agrees with lines in
10406 the current matrix? I don't think so, so we mark rows
10407 displayed invalid in the current matrix by setting their
10408 enabled_p flag to zero. */
10409 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10410 if (display_line (&it))
10411 last_text_row_at_end = it.glyph_row - 1;
10412 }
10413 }
12adba34 10414
5f5c8ee5
GM
10415 /* Update window_end_pos and window_end_vpos. */
10416 if (first_unchanged_at_end_row
10417 && first_unchanged_at_end_row->y < it.last_visible_y
10418 && !last_text_row_at_end)
10419 {
10420 /* Window end line if one of the preserved rows from the current
10421 matrix. Set row to the last row displaying text in current
10422 matrix starting at first_unchanged_at_end_row, after
10423 scrolling. */
10424 xassert (first_unchanged_at_end_row->displays_text_p);
10425 row = find_last_row_displaying_text (w->current_matrix, &it,
10426 first_unchanged_at_end_row);
10427 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10428
10429 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10430 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10431 XSETFASTINT (w->window_end_vpos,
10432 MATRIX_ROW_VPOS (row, w->current_matrix));
10433 }
10434 else if (last_text_row_at_end)
10435 {
10436 XSETFASTINT (w->window_end_pos,
10437 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10438 w->window_end_bytepos
10439 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10440 XSETFASTINT (w->window_end_vpos,
10441 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10442 }
10443 else if (last_text_row)
10444 {
10445 /* We have displayed either to the end of the window or at the
10446 end of the window, i.e. the last row with text is to be found
10447 in the desired matrix. */
10448 XSETFASTINT (w->window_end_pos,
10449 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10450 w->window_end_bytepos
10451 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10452 XSETFASTINT (w->window_end_vpos,
10453 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10454 }
10455 else if (first_unchanged_at_end_row == NULL
10456 && last_text_row == NULL
10457 && last_text_row_at_end == NULL)
10458 {
10459 /* Displayed to end of window, but no line containing text was
10460 displayed. Lines were deleted at the end of the window. */
10461 int vpos;
045dee35 10462 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
10463
10464 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
045dee35
GM
10465 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10466 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10467 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10468 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
5f5c8ee5 10469 break;
12adba34 10470
5f5c8ee5
GM
10471 w->window_end_vpos = make_number (vpos);
10472 }
10473 else
10474 abort ();
10475
10476 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10477 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 10478
5f5c8ee5
GM
10479 /* Record that display has not been completed. */
10480 w->window_end_valid = Qnil;
10481 w->desired_matrix->no_scrolling_p = 1;
10482 return 1;
12adba34 10483}
0f9c0ff0 10484
a2889657 10485
5f5c8ee5
GM
10486\f
10487/***********************************************************************
10488 More debugging support
10489 ***********************************************************************/
a2889657 10490
5f5c8ee5 10491#if GLYPH_DEBUG
a2889657 10492
5f5c8ee5
GM
10493 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10494static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 10495
31b24551 10496
5f5c8ee5
GM
10497/* Dump the contents of glyph matrix MATRIX on stderr. If
10498 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 10499
5f5c8ee5
GM
10500void
10501dump_glyph_matrix (matrix, with_glyphs_p)
10502 struct glyph_matrix *matrix;
10503 int with_glyphs_p;
10504{
efc63ef0 10505 int i;
5f5c8ee5
GM
10506 for (i = 0; i < matrix->nrows; ++i)
10507 dump_glyph_row (matrix, i, with_glyphs_p);
10508}
31b24551 10509
68a37fa8 10510
5f5c8ee5
GM
10511/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10512 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 10513
5f5c8ee5
GM
10514void
10515dump_glyph_row (matrix, vpos, with_glyphs_p)
10516 struct glyph_matrix *matrix;
10517 int vpos, with_glyphs_p;
10518{
10519 struct glyph_row *row;
10520
10521 if (vpos < 0 || vpos >= matrix->nrows)
10522 return;
10523
10524 row = MATRIX_ROW (matrix, vpos);
10525
10526 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10527 fprintf (stderr, "=============================================\n");
10528
10529 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",
10530 row - matrix->rows,
10531 MATRIX_ROW_START_CHARPOS (row),
10532 MATRIX_ROW_END_CHARPOS (row),
10533 row->used[TEXT_AREA],
10534 row->contains_overlapping_glyphs_p,
10535 row->enabled_p,
10536 row->inverse_p,
10537 row->truncated_on_left_p,
10538 row->truncated_on_right_p,
10539 row->overlay_arrow_p,
10540 row->continued_p,
10541 MATRIX_ROW_CONTINUATION_LINE_P (row),
10542 row->displays_text_p,
10543 row->ends_at_zv_p,
10544 row->fill_line_p,
10545 row->x,
10546 row->y,
10547 row->pixel_width);
10548 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10549 row->end.overlay_string_index);
10550 fprintf (stderr, "%9d %5d\n",
10551 CHARPOS (row->start.string_pos),
10552 CHARPOS (row->end.string_pos));
10553 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10554 row->end.dpvec_index);
10555
10556 if (with_glyphs_p)
bd66d1ba 10557 {
5f5c8ee5
GM
10558 struct glyph *glyph, *glyph_end;
10559 int prev_had_glyphs_p;
10560
10561 glyph = row->glyphs[TEXT_AREA];
10562 glyph_end = glyph + row->used[TEXT_AREA];
10563
10564 /* Glyph for a line end in text. */
10565 if (glyph == glyph_end && glyph->charpos > 0)
10566 ++glyph_end;
10567
10568 if (glyph < glyph_end)
bd66d1ba 10569 {
5f5c8ee5
GM
10570 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10571 prev_had_glyphs_p = 1;
bd66d1ba
RS
10572 }
10573 else
5f5c8ee5
GM
10574 prev_had_glyphs_p = 0;
10575
10576 while (glyph < glyph_end)
f7b4b63a 10577 {
5f5c8ee5
GM
10578 if (glyph->type == CHAR_GLYPH)
10579 {
10580 fprintf (stderr,
10581 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10582 glyph - row->glyphs[TEXT_AREA],
10583 'C',
10584 glyph->charpos,
10585 glyph->pixel_width,
43d120d8
KH
10586 glyph->u.ch,
10587 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10588 ? glyph->u.ch
5f5c8ee5 10589 : '.'),
43d120d8 10590 glyph->face_id,
5f5c8ee5
GM
10591 glyph->left_box_line_p,
10592 glyph->right_box_line_p);
10593 }
10594 else if (glyph->type == STRETCH_GLYPH)
10595 {
10596 fprintf (stderr,
10597 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10598 glyph - row->glyphs[TEXT_AREA],
10599 'S',
10600 glyph->charpos,
10601 glyph->pixel_width,
10602 0,
10603 '.',
bcdda9a4 10604 glyph->face_id,
5f5c8ee5
GM
10605 glyph->left_box_line_p,
10606 glyph->right_box_line_p);
10607 }
10608 else if (glyph->type == IMAGE_GLYPH)
10609 {
10610 fprintf (stderr,
10611 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10612 glyph - row->glyphs[TEXT_AREA],
10613 'I',
10614 glyph->charpos,
10615 glyph->pixel_width,
43d120d8 10616 glyph->u.img_id,
5f5c8ee5 10617 '.',
bcdda9a4 10618 glyph->face_id,
5f5c8ee5
GM
10619 glyph->left_box_line_p,
10620 glyph->right_box_line_p);
10621 }
10622 ++glyph;
f7b4b63a 10623 }
f4faa47c 10624 }
5f5c8ee5 10625}
f4faa47c 10626
a2889657 10627
5f5c8ee5
GM
10628DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10629 Sdump_glyph_matrix, 0, 1, "p",
10630 "Dump the current matrix of the selected window to stderr.\n\
10631Shows contents of glyph row structures. With non-nil optional\n\
10632parameter WITH-GLYPHS-P, dump glyphs as well.")
10633 (with_glyphs_p)
10634{
10635 struct window *w = XWINDOW (selected_window);
10636 struct buffer *buffer = XBUFFER (w->buffer);
10637
10638 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10639 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10640 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10641 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10642 fprintf (stderr, "=============================================\n");
10643 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10644 return Qnil;
10645}
1c2250c2 10646
1fca3fae 10647
5f5c8ee5
GM
10648DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10649 "Dump glyph row ROW to stderr.")
10650 (row)
10651 Lisp_Object row;
10652{
10653 CHECK_NUMBER (row, 0);
10654 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10655 return Qnil;
10656}
1fca3fae 10657
67481ae5 10658
e037b9ec 10659DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
5f5c8ee5
GM
10660 0, 0, "", "")
10661 ()
10662{
886bd6f2
GM
10663 struct frame *sf = SELECTED_FRAME ();
10664 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
5f5c8ee5
GM
10665 ->current_matrix);
10666 dump_glyph_row (m, 0, 1);
10667 return Qnil;
10668}
ca26e1c8 10669
0f9c0ff0 10670
5f5c8ee5
GM
10671DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10672 Strace_redisplay_toggle, 0, 0, "",
10673 "Toggle tracing of redisplay.")
10674 ()
10675{
10676 trace_redisplay_p = !trace_redisplay_p;
10677 return Qnil;
10678}
bf9249e3
GM
10679
10680
10681DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10682 "Print STRING to stderr.")
10683 (string)
10684 Lisp_Object string;
10685{
10686 CHECK_STRING (string, 0);
10687 fprintf (stderr, "%s", XSTRING (string)->data);
10688 return Qnil;
10689}
5f5c8ee5
GM
10690
10691#endif /* GLYPH_DEBUG */
ca26e1c8 10692
ca26e1c8 10693
5f5c8ee5
GM
10694\f
10695/***********************************************************************
10696 Building Desired Matrix Rows
10697 ***********************************************************************/
a2889657 10698
5f5c8ee5
GM
10699/* Return a temporary glyph row holding the glyphs of an overlay
10700 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 10701
5f5c8ee5
GM
10702static struct glyph_row *
10703get_overlay_arrow_glyph_row (w)
10704 struct window *w;
10705{
10706 struct frame *f = XFRAME (WINDOW_FRAME (w));
10707 struct buffer *buffer = XBUFFER (w->buffer);
10708 struct buffer *old = current_buffer;
10709 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10710 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10711 unsigned char *arrow_end = arrow_string + arrow_len;
10712 unsigned char *p;
10713 struct it it;
10714 int multibyte_p;
10715 int n_glyphs_before;
10716
10717 set_buffer_temp (buffer);
10718 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10719 it.glyph_row->used[TEXT_AREA] = 0;
10720 SET_TEXT_POS (it.position, 0, 0);
10721
10722 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10723 p = arrow_string;
10724 while (p < arrow_end)
10725 {
10726 Lisp_Object face, ilisp;
10727
10728 /* Get the next character. */
10729 if (multibyte_p)
4fdb80f2 10730 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
10731 else
10732 it.c = *p, it.len = 1;
10733 p += it.len;
10734
10735 /* Get its face. */
10736 XSETFASTINT (ilisp, p - arrow_string);
10737 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10738 it.face_id = compute_char_face (f, it.c, face);
10739
10740 /* Compute its width, get its glyphs. */
10741 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 10742 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
10743 PRODUCE_GLYPHS (&it);
10744
10745 /* If this character doesn't fit any more in the line, we have
10746 to remove some glyphs. */
10747 if (it.current_x > it.last_visible_x)
10748 {
10749 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10750 break;
10751 }
10752 }
10753
10754 set_buffer_temp (old);
10755 return it.glyph_row;
10756}
ca26e1c8 10757
b0a0fbda 10758
5f5c8ee5
GM
10759/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10760 glyphs are only inserted for terminal frames since we can't really
10761 win with truncation glyphs when partially visible glyphs are
10762 involved. Which glyphs to insert is determined by
10763 produce_special_glyphs. */
67481ae5 10764
5f5c8ee5
GM
10765static void
10766insert_left_trunc_glyphs (it)
10767 struct it *it;
10768{
10769 struct it truncate_it;
10770 struct glyph *from, *end, *to, *toend;
10771
10772 xassert (!FRAME_WINDOW_P (it->f));
10773
10774 /* Get the truncation glyphs. */
10775 truncate_it = *it;
5f5c8ee5
GM
10776 truncate_it.current_x = 0;
10777 truncate_it.face_id = DEFAULT_FACE_ID;
10778 truncate_it.glyph_row = &scratch_glyph_row;
10779 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10780 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10781 truncate_it.object = 0;
10782 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10783
10784 /* Overwrite glyphs from IT with truncation glyphs. */
10785 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10786 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10787 to = it->glyph_row->glyphs[TEXT_AREA];
10788 toend = to + it->glyph_row->used[TEXT_AREA];
10789
10790 while (from < end)
10791 *to++ = *from++;
10792
10793 /* There may be padding glyphs left over. Remove them. */
10794 from = to;
10795 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10796 ++from;
10797 while (from < toend)
10798 *to++ = *from++;
10799
10800 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10801}
e0bfbde6 10802
e0bfbde6 10803
5f5c8ee5 10804/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 10805
5f5c8ee5
GM
10806 Most of the time, ascent and height of a display line will be equal
10807 to the max_ascent and max_height values of the display iterator
10808 structure. This is not the case if
67481ae5 10809
5f5c8ee5
GM
10810 1. We hit ZV without displaying anything. In this case, max_ascent
10811 and max_height will be zero.
1c9241f5 10812
5f5c8ee5
GM
10813 2. We have some glyphs that don't contribute to the line height.
10814 (The glyph row flag contributes_to_line_height_p is for future
10815 pixmap extensions).
f6fd109b 10816
5f5c8ee5
GM
10817 The first case is easily covered by using default values because in
10818 these cases, the line height does not really matter, except that it
10819 must not be zero. */
67481ae5 10820
5f5c8ee5
GM
10821static void
10822compute_line_metrics (it)
10823 struct it *it;
10824{
10825 struct glyph_row *row = it->glyph_row;
10826 int area, i;
1c2250c2 10827
5f5c8ee5
GM
10828 if (FRAME_WINDOW_P (it->f))
10829 {
045dee35 10830 int i, header_line_height;
1c2250c2 10831
5f5c8ee5
GM
10832 /* The line may consist of one space only, that was added to
10833 place the cursor on it. If so, the row's height hasn't been
10834 computed yet. */
10835 if (row->height == 0)
10836 {
10837 if (it->max_ascent + it->max_descent == 0)
312246d1 10838 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
10839 row->ascent = it->max_ascent;
10840 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10841 row->phys_ascent = it->max_phys_ascent;
10842 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10843 }
10844
10845 /* Compute the width of this line. */
10846 row->pixel_width = row->x;
10847 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10848 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10849
10850 xassert (row->pixel_width >= 0);
10851 xassert (row->ascent >= 0 && row->height > 0);
10852
312246d1
GM
10853 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10854 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10855
10856 /* If first line's physical ascent is larger than its logical
10857 ascent, use the physical ascent, and make the row taller.
10858 This makes accented characters fully visible. */
10859 if (row == it->w->desired_matrix->rows
10860 && row->phys_ascent > row->ascent)
10861 {
10862 row->height += row->phys_ascent - row->ascent;
10863 row->ascent = row->phys_ascent;
10864 }
10865
5f5c8ee5
GM
10866 /* Compute how much of the line is visible. */
10867 row->visible_height = row->height;
10868
045dee35
GM
10869 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10870 if (row->y < header_line_height)
10871 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
10872 else
10873 {
10874 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10875 if (row->y + row->height > max_y)
10876 row->visible_height -= row->y + row->height - max_y;
10877 }
10878 }
10879 else
10880 {
10881 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
10882 row->ascent = row->phys_ascent = 0;
10883 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 10884 }
67481ae5 10885
5f5c8ee5
GM
10886 /* Compute a hash code for this row. */
10887 row->hash = 0;
10888 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10889 for (i = 0; i < row->used[area]; ++i)
10890 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10891 + row->glyphs[area][i].u.val
43d120d8
KH
10892 + row->glyphs[area][i].face_id
10893 + row->glyphs[area][i].padding_p
5f5c8ee5 10894 + (row->glyphs[area][i].type << 2));
a2889657 10895
5f5c8ee5 10896 it->max_ascent = it->max_descent = 0;
312246d1 10897 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 10898}
12adba34 10899
ca26e1c8 10900
5f5c8ee5
GM
10901/* Append one space to the glyph row of iterator IT if doing a
10902 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10903 space have the default face, otherwise let it have the same face as
80c6cb1f 10904 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
10905
10906 This function is called to make sure that there is always one glyph
10907 at the end of a glyph row that the cursor can be set on under
10908 window-systems. (If there weren't such a glyph we would not know
10909 how wide and tall a box cursor should be displayed).
10910
10911 At the same time this space let's a nicely handle clearing to the
10912 end of the line if the row ends in italic text. */
ca26e1c8 10913
80c6cb1f 10914static int
5f5c8ee5
GM
10915append_space (it, default_face_p)
10916 struct it *it;
10917 int default_face_p;
10918{
10919 if (FRAME_WINDOW_P (it->f))
10920 {
10921 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 10922
5f5c8ee5
GM
10923 if (it->glyph_row->glyphs[TEXT_AREA] + n
10924 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 10925 {
5f5c8ee5
GM
10926 /* Save some values that must not be changed. */
10927 int saved_x = it->current_x;
10928 struct text_pos saved_pos;
10929 int saved_what = it->what;
10930 int saved_face_id = it->face_id;
5f5c8ee5 10931 Lisp_Object saved_object;
980806b6 10932 struct face *face;
5f5c8ee5
GM
10933
10934 saved_object = it->object;
10935 saved_pos = it->position;
10936
10937 it->what = IT_CHARACTER;
10938 bzero (&it->position, sizeof it->position);
10939 it->object = 0;
10940 it->c = ' ';
10941 it->len = 1;
5f5c8ee5
GM
10942
10943 if (default_face_p)
10944 it->face_id = DEFAULT_FACE_ID;
980806b6
KH
10945 face = FACE_FROM_ID (it->f, it->face_id);
10946 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 10947
5f5c8ee5
GM
10948 PRODUCE_GLYPHS (it);
10949
10950 it->current_x = saved_x;
10951 it->object = saved_object;
10952 it->position = saved_pos;
10953 it->what = saved_what;
10954 it->face_id = saved_face_id;
80c6cb1f 10955 return 1;
5f5c8ee5
GM
10956 }
10957 }
80c6cb1f
GM
10958
10959 return 0;
5f5c8ee5 10960}
12adba34 10961
1842fc1a 10962
5f5c8ee5
GM
10963/* Extend the face of the last glyph in the text area of IT->glyph_row
10964 to the end of the display line. Called from display_line.
10965 If the glyph row is empty, add a space glyph to it so that we
10966 know the face to draw. Set the glyph row flag fill_line_p. */
10967
10968static void
10969extend_face_to_end_of_line (it)
10970 struct it *it;
10971{
10972 struct face *face;
10973 struct frame *f = it->f;
1842fc1a 10974
5f5c8ee5
GM
10975 /* If line is already filled, do nothing. */
10976 if (it->current_x >= it->last_visible_x)
10977 return;
10978
10979 /* Face extension extends the background and box of IT->face_id
10980 to the end of the line. If the background equals the background
10981 of the frame, we haven't to do anything. */
10982 face = FACE_FROM_ID (f, it->face_id);
10983 if (FRAME_WINDOW_P (f)
10984 && face->box == FACE_NO_BOX
10985 && face->background == FRAME_BACKGROUND_PIXEL (f)
10986 && !face->stipple)
10987 return;
1842fc1a 10988
5f5c8ee5
GM
10989 /* Set the glyph row flag indicating that the face of the last glyph
10990 in the text area has to be drawn to the end of the text area. */
10991 it->glyph_row->fill_line_p = 1;
545e04f6 10992
980806b6
KH
10993 /* If current character of IT is not ASCII, make sure we have the
10994 ASCII face. This will be automatically undone the next time
10995 get_next_display_element returns a multibyte character. Note
10996 that the character will always be single byte in unibyte text. */
10997 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 10998 {
980806b6 10999 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 11000 }
545e04f6 11001
5f5c8ee5
GM
11002 if (FRAME_WINDOW_P (f))
11003 {
11004 /* If the row is empty, add a space with the current face of IT,
11005 so that we know which face to draw. */
11006 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 11007 {
5f5c8ee5 11008 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 11009 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 11010 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 11011 }
5f5c8ee5
GM
11012 }
11013 else
11014 {
11015 /* Save some values that must not be changed. */
11016 int saved_x = it->current_x;
11017 struct text_pos saved_pos;
11018 Lisp_Object saved_object;
11019 int saved_what = it->what;
11020
11021 saved_object = it->object;
11022 saved_pos = it->position;
11023
11024 it->what = IT_CHARACTER;
11025 bzero (&it->position, sizeof it->position);
11026 it->object = 0;
11027 it->c = ' ';
11028 it->len = 1;
11029
11030 PRODUCE_GLYPHS (it);
11031
11032 while (it->current_x <= it->last_visible_x)
11033 PRODUCE_GLYPHS (it);
11034
11035 /* Don't count these blanks really. It would let us insert a left
11036 truncation glyph below and make us set the cursor on them, maybe. */
11037 it->current_x = saved_x;
11038 it->object = saved_object;
11039 it->position = saved_pos;
11040 it->what = saved_what;
11041 }
11042}
12adba34 11043
545e04f6 11044
5f5c8ee5
GM
11045/* Value is non-zero if text starting at CHARPOS in current_buffer is
11046 trailing whitespace. */
1c9241f5 11047
5f5c8ee5
GM
11048static int
11049trailing_whitespace_p (charpos)
11050 int charpos;
11051{
11052 int bytepos = CHAR_TO_BYTE (charpos);
11053 int c = 0;
7bbe686f 11054
5f5c8ee5
GM
11055 while (bytepos < ZV_BYTE
11056 && (c = FETCH_CHAR (bytepos),
11057 c == ' ' || c == '\t'))
11058 ++bytepos;
0d09d1e6 11059
8f897821
GM
11060 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11061 {
11062 if (bytepos != PT_BYTE)
11063 return 1;
11064 }
11065 return 0;
5f5c8ee5 11066}
31b24551 11067
545e04f6 11068
5f5c8ee5 11069/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 11070
5f5c8ee5
GM
11071void
11072highlight_trailing_whitespace (f, row)
11073 struct frame *f;
11074 struct glyph_row *row;
11075{
11076 int used = row->used[TEXT_AREA];
11077
11078 if (used)
11079 {
11080 struct glyph *start = row->glyphs[TEXT_AREA];
11081 struct glyph *glyph = start + used - 1;
11082
11083 /* Skip over the space glyph inserted to display the
11084 cursor at the end of a line. */
11085 if (glyph->type == CHAR_GLYPH
43d120d8 11086 && glyph->u.ch == ' '
5f5c8ee5
GM
11087 && glyph->object == 0)
11088 --glyph;
11089
11090 /* If last glyph is a space or stretch, and it's trailing
11091 whitespace, set the face of all trailing whitespace glyphs in
11092 IT->glyph_row to `trailing-whitespace'. */
11093 if (glyph >= start
11094 && BUFFERP (glyph->object)
11095 && (glyph->type == STRETCH_GLYPH
11096 || (glyph->type == CHAR_GLYPH
43d120d8 11097 && glyph->u.ch == ' '))
5f5c8ee5 11098 && trailing_whitespace_p (glyph->charpos))
545e04f6 11099 {
980806b6 11100 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
11101
11102 while (glyph >= start
11103 && BUFFERP (glyph->object)
11104 && (glyph->type == STRETCH_GLYPH
11105 || (glyph->type == CHAR_GLYPH
43d120d8
KH
11106 && glyph->u.ch == ' ')))
11107 (glyph--)->face_id = face_id;
545e04f6 11108 }
a2889657 11109 }
5f5c8ee5 11110}
a2889657 11111
5fcbb24d 11112
5f5c8ee5
GM
11113/* Construct the glyph row IT->glyph_row in the desired matrix of
11114 IT->w from text at the current position of IT. See dispextern.h
11115 for an overview of struct it. Value is non-zero if
11116 IT->glyph_row displays text, as opposed to a line displaying ZV
11117 only. */
11118
11119static int
11120display_line (it)
11121 struct it *it;
11122{
11123 struct glyph_row *row = it->glyph_row;
11124
11125 /* We always start displaying at hpos zero even if hscrolled. */
11126 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 11127
5f5c8ee5
GM
11128 /* We must not display in a row that's not a text row. */
11129 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11130 < it->w->desired_matrix->nrows);
12adba34 11131
5f5c8ee5
GM
11132 /* Is IT->w showing the region? */
11133 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 11134
5f5c8ee5
GM
11135 /* Clear the result glyph row and enable it. */
11136 prepare_desired_row (row);
12adba34 11137
5f5c8ee5
GM
11138 row->y = it->current_y;
11139 row->start = it->current;
11140 row->continuation_lines_width = it->continuation_lines_width;
11141 row->displays_text_p = 1;
11142
11143 /* Arrange the overlays nicely for our purposes. Usually, we call
11144 display_line on only one line at a time, in which case this
11145 can't really hurt too much, or we call it on lines which appear
11146 one after another in the buffer, in which case all calls to
11147 recenter_overlay_lists but the first will be pretty cheap. */
11148 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11149
5f5c8ee5
GM
11150 /* Move over display elements that are not visible because we are
11151 hscrolled. This may stop at an x-position < IT->first_visible_x
11152 if the first glyph is partially visible or if we hit a line end. */
11153 if (it->current_x < it->first_visible_x)
11154 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11155 MOVE_TO_POS | MOVE_TO_X);
11156
11157 /* Get the initial row height. This is either the height of the
11158 text hscrolled, if there is any, or zero. */
11159 row->ascent = it->max_ascent;
11160 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11161 row->phys_ascent = it->max_phys_ascent;
11162 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
11163
11164 /* Loop generating characters. The loop is left with IT on the next
11165 character to display. */
11166 while (1)
11167 {
11168 int n_glyphs_before, hpos_before, x_before;
11169 int x, i, nglyphs;
11170
11171 /* Retrieve the next thing to display. Value is zero if end of
11172 buffer reached. */
11173 if (!get_next_display_element (it))
11174 {
11175 /* Maybe add a space at the end of this line that is used to
1b335865
GM
11176 display the cursor there under X. Set the charpos of the
11177 first glyph of blank lines not corresponding to any text
11178 to -1. */
11179 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11180 || row->used[TEXT_AREA] == 0)
a2889657 11181 {
5f5c8ee5
GM
11182 row->glyphs[TEXT_AREA]->charpos = -1;
11183 row->displays_text_p = 0;
11184
11185 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11186 row->indicate_empty_line_p = 1;
a2889657 11187 }
5f5c8ee5
GM
11188
11189 it->continuation_lines_width = 0;
11190 row->ends_at_zv_p = 1;
11191 break;
a2889657 11192 }
a2889657 11193
5f5c8ee5
GM
11194 /* Now, get the metrics of what we want to display. This also
11195 generates glyphs in `row' (which is IT->glyph_row). */
11196 n_glyphs_before = row->used[TEXT_AREA];
11197 x = it->current_x;
11198 PRODUCE_GLYPHS (it);
a2889657 11199
5f5c8ee5
GM
11200 /* If this display element was in marginal areas, continue with
11201 the next one. */
11202 if (it->area != TEXT_AREA)
a2889657 11203 {
5f5c8ee5
GM
11204 row->ascent = max (row->ascent, it->max_ascent);
11205 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11206 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11207 row->phys_height = max (row->phys_height,
11208 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11209 set_iterator_to_next (it);
11210 continue;
11211 }
5936754e 11212
5f5c8ee5
GM
11213 /* Does the display element fit on the line? If we truncate
11214 lines, we should draw past the right edge of the window. If
11215 we don't truncate, we want to stop so that we can display the
11216 continuation glyph before the right margin. If lines are
11217 continued, there are two possible strategies for characters
11218 resulting in more than 1 glyph (e.g. tabs): Display as many
11219 glyphs as possible in this line and leave the rest for the
11220 continuation line, or display the whole element in the next
11221 line. Original redisplay did the former, so we do it also. */
11222 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11223 hpos_before = it->hpos;
11224 x_before = x;
11225
11226 if (nglyphs == 1
11227 && it->current_x < it->last_visible_x)
11228 {
11229 ++it->hpos;
11230 row->ascent = max (row->ascent, it->max_ascent);
11231 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11232 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11233 row->phys_height = max (row->phys_height,
11234 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11235 if (it->current_x - it->pixel_width < it->first_visible_x)
11236 row->x = x - it->first_visible_x;
11237 }
11238 else
11239 {
11240 int new_x;
11241 struct glyph *glyph;
11242
11243 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 11244 {
5f5c8ee5
GM
11245 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11246 new_x = x + glyph->pixel_width;
11247
11248 if (/* Lines are continued. */
11249 !it->truncate_lines_p
11250 && (/* Glyph doesn't fit on the line. */
11251 new_x > it->last_visible_x
11252 /* Or it fits exactly on a window system frame. */
11253 || (new_x == it->last_visible_x
11254 && FRAME_WINDOW_P (it->f))))
a2889657 11255 {
5f5c8ee5
GM
11256 /* End of a continued line. */
11257
11258 if (it->hpos == 0
11259 || (new_x == it->last_visible_x
11260 && FRAME_WINDOW_P (it->f)))
11261 {
11262 /* Current glyph fits exactly on the line. We
11263 must continue the line because we can't draw
11264 the cursor after the glyph. */
11265 row->continued_p = 1;
11266 it->current_x = new_x;
11267 it->continuation_lines_width += new_x;
11268 ++it->hpos;
11269 if (i == nglyphs - 1)
11270 set_iterator_to_next (it);
11271 }
11272 else
5936754e 11273 {
5f5c8ee5
GM
11274 /* Display element draws past the right edge of
11275 the window. Restore positions to values
11276 before the element. The next line starts
11277 with current_x before the glyph that could
11278 not be displayed, so that TAB works right. */
11279 row->used[TEXT_AREA] = n_glyphs_before + i;
11280
11281 /* Display continuation glyphs. */
11282 if (!FRAME_WINDOW_P (it->f))
11283 produce_special_glyphs (it, IT_CONTINUATION);
11284 row->continued_p = 1;
11285
11286 it->current_x = x;
11287 it->continuation_lines_width += x;
5936754e 11288 }
5f5c8ee5
GM
11289 break;
11290 }
11291 else if (new_x > it->first_visible_x)
11292 {
11293 /* Increment number of glyphs actually displayed. */
11294 ++it->hpos;
11295
11296 if (x < it->first_visible_x)
11297 /* Glyph is partially visible, i.e. row starts at
11298 negative X position. */
11299 row->x = x - it->first_visible_x;
11300 }
11301 else
11302 {
11303 /* Glyph is completely off the left margin of the
11304 window. This should not happen because of the
11305 move_it_in_display_line at the start of
11306 this function. */
11307 abort ();
a2889657 11308 }
a2889657 11309 }
5f5c8ee5
GM
11310
11311 row->ascent = max (row->ascent, it->max_ascent);
11312 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11313 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11314 row->phys_height = max (row->phys_height,
11315 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11316
11317 /* End of this display line if row is continued. */
11318 if (row->continued_p)
11319 break;
a2889657 11320 }
a2889657 11321
5f5c8ee5
GM
11322 /* Is this a line end? If yes, we're also done, after making
11323 sure that a non-default face is extended up to the right
11324 margin of the window. */
11325 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 11326 {
5f5c8ee5
GM
11327 int used_before = row->used[TEXT_AREA];
11328
11329 /* Add a space at the end of the line that is used to
11330 display the cursor there. */
11331 append_space (it, 0);
11332
11333 /* Extend the face to the end of the line. */
11334 extend_face_to_end_of_line (it);
11335
11336 /* Make sure we have the position. */
11337 if (used_before == 0)
11338 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11339
11340 /* Consume the line end. This skips over invisible lines. */
11341 set_iterator_to_next (it);
11342 it->continuation_lines_width = 0;
11343 break;
1c9241f5 11344 }
a2889657 11345
5f5c8ee5
GM
11346 /* Proceed with next display element. Note that this skips
11347 over lines invisible because of selective display. */
11348 set_iterator_to_next (it);
b1d1124b 11349
5f5c8ee5
GM
11350 /* If we truncate lines, we are done when the last displayed
11351 glyphs reach past the right margin of the window. */
11352 if (it->truncate_lines_p
11353 && (FRAME_WINDOW_P (it->f)
11354 ? (it->current_x >= it->last_visible_x)
11355 : (it->current_x > it->last_visible_x)))
75d13c64 11356 {
5f5c8ee5
GM
11357 /* Maybe add truncation glyphs. */
11358 if (!FRAME_WINDOW_P (it->f))
11359 {
11360 --it->glyph_row->used[TEXT_AREA];
11361 produce_special_glyphs (it, IT_TRUNCATION);
11362 }
11363
11364 row->truncated_on_right_p = 1;
11365 it->continuation_lines_width = 0;
312246d1 11366 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
11367 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11368 it->hpos = hpos_before;
11369 it->current_x = x_before;
11370 break;
75d13c64 11371 }
a2889657 11372 }
a2889657 11373
5f5c8ee5
GM
11374 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11375 at the left window margin. */
11376 if (it->first_visible_x
11377 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11378 {
11379 if (!FRAME_WINDOW_P (it->f))
11380 insert_left_trunc_glyphs (it);
11381 row->truncated_on_left_p = 1;
11382 }
a2889657 11383
5f5c8ee5
GM
11384 /* If the start of this line is the overlay arrow-position, then
11385 mark this glyph row as the one containing the overlay arrow.
11386 This is clearly a mess with variable size fonts. It would be
11387 better to let it be displayed like cursors under X. */
e24c997d 11388 if (MARKERP (Voverlay_arrow_position)
a2889657 11389 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
11390 && (MATRIX_ROW_START_CHARPOS (row)
11391 == marker_position (Voverlay_arrow_position))
e24c997d 11392 && STRINGP (Voverlay_arrow_string)
a2889657
JB
11393 && ! overlay_arrow_seen)
11394 {
5f5c8ee5
GM
11395 /* Overlay arrow in window redisplay is a bitmap. */
11396 if (!FRAME_WINDOW_P (it->f))
c4628384 11397 {
5f5c8ee5
GM
11398 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11399 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11400 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11401 struct glyph *p = row->glyphs[TEXT_AREA];
11402 struct glyph *p2, *end;
11403
11404 /* Copy the arrow glyphs. */
11405 while (glyph < arrow_end)
11406 *p++ = *glyph++;
11407
11408 /* Throw away padding glyphs. */
11409 p2 = p;
11410 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11411 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11412 ++p2;
11413 if (p2 > p)
212e4f87 11414 {
5f5c8ee5
GM
11415 while (p2 < end)
11416 *p++ = *p2++;
11417 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 11418 }
c4628384 11419 }
5f5c8ee5 11420
a2889657 11421 overlay_arrow_seen = 1;
5f5c8ee5 11422 row->overlay_arrow_p = 1;
a2889657
JB
11423 }
11424
5f5c8ee5
GM
11425 /* Compute pixel dimensions of this line. */
11426 compute_line_metrics (it);
11427
11428 /* Remember the position at which this line ends. */
11429 row->end = it->current;
11430
11431 /* Maybe set the cursor. If you change this, it's probably a good
11432 idea to also change the code in redisplay_window for cursor
11433 movement in an unchanged window. */
11434 if (it->w->cursor.vpos < 0
11435 && PT >= MATRIX_ROW_START_CHARPOS (row)
11436 && MATRIX_ROW_END_CHARPOS (row) >= PT
11437 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11438 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11439 || !row->ends_at_zv_p)))
11440 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11441
11442 /* Highlight trailing whitespace. */
8f897821 11443 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
11444 highlight_trailing_whitespace (it->f, it->glyph_row);
11445
11446 /* Prepare for the next line. This line starts horizontally at (X
11447 HPOS) = (0 0). Vertical positions are incremented. As a
11448 convenience for the caller, IT->glyph_row is set to the next
11449 row to be used. */
11450 it->current_x = it->hpos = 0;
11451 it->current_y += row->height;
11452 ++it->vpos;
11453 ++it->glyph_row;
11454 return row->displays_text_p;
a2889657 11455}
5f5c8ee5
GM
11456
11457
a2889657 11458\f
5f5c8ee5
GM
11459/***********************************************************************
11460 Menu Bar
11461 ***********************************************************************/
11462
11463/* Redisplay the menu bar in the frame for window W.
11464
11465 The menu bar of X frames that don't have X toolkit support is
11466 displayed in a special window W->frame->menu_bar_window.
11467
11468 The menu bar of terminal frames is treated specially as far as
11469 glyph matrices are concerned. Menu bar lines are not part of
11470 windows, so the update is done directly on the frame matrix rows
11471 for the menu bar. */
7ce2c095
RS
11472
11473static void
11474display_menu_bar (w)
11475 struct window *w;
11476{
5f5c8ee5
GM
11477 struct frame *f = XFRAME (WINDOW_FRAME (w));
11478 struct it it;
11479 Lisp_Object items;
8351baf2 11480 int i;
7ce2c095 11481
5f5c8ee5 11482 /* Don't do all this for graphical frames. */
dc937613 11483#ifdef HAVE_NTGUI
d129c4c2
KH
11484 if (!NILP (Vwindow_system))
11485 return;
dc937613 11486#endif
dc937613 11487#ifdef USE_X_TOOLKIT
d3413a53 11488 if (FRAME_X_P (f))
7ce2c095 11489 return;
5f5c8ee5
GM
11490#endif
11491
11492#ifdef USE_X_TOOLKIT
11493 xassert (!FRAME_WINDOW_P (f));
52377a47 11494 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
11495 it.first_visible_x = 0;
11496 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11497#else /* not USE_X_TOOLKIT */
11498 if (FRAME_WINDOW_P (f))
11499 {
11500 /* Menu bar lines are displayed in the desired matrix of the
11501 dummy window menu_bar_window. */
11502 struct window *menu_w;
11503 xassert (WINDOWP (f->menu_bar_window));
11504 menu_w = XWINDOW (f->menu_bar_window);
11505 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 11506 MENU_FACE_ID);
5f5c8ee5
GM
11507 it.first_visible_x = 0;
11508 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11509 }
11510 else
11511 {
11512 /* This is a TTY frame, i.e. character hpos/vpos are used as
11513 pixel x/y. */
11514 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 11515 MENU_FACE_ID);
5f5c8ee5
GM
11516 it.first_visible_x = 0;
11517 it.last_visible_x = FRAME_WIDTH (f);
11518 }
11519#endif /* not USE_X_TOOLKIT */
11520
11521 /* Clear all rows of the menu bar. */
11522 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11523 {
11524 struct glyph_row *row = it.glyph_row + i;
11525 clear_glyph_row (row);
11526 row->enabled_p = 1;
11527 row->full_width_p = 1;
11528 }
7ce2c095 11529
5f5c8ee5
GM
11530 /* Make the first line of the menu bar appear in reverse video. */
11531 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 11532
5f5c8ee5
GM
11533 /* Display all items of the menu bar. */
11534 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 11535 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 11536 {
5f5c8ee5
GM
11537 Lisp_Object string;
11538
11539 /* Stop at nil string. */
8351baf2
RS
11540 string = XVECTOR (items)->contents[i + 1];
11541 if (NILP (string))
11542 break;
2d66ad19 11543
5f5c8ee5
GM
11544 /* Remember where item was displayed. */
11545 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 11546
5f5c8ee5
GM
11547 /* Display the item, pad with one space. */
11548 if (it.current_x < it.last_visible_x)
11549 display_string (NULL, string, Qnil, 0, 0, &it,
11550 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
11551 }
11552
2d66ad19 11553 /* Fill out the line with spaces. */
5f5c8ee5
GM
11554 if (it.current_x < it.last_visible_x)
11555 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 11556
5f5c8ee5
GM
11557 /* Compute the total height of the lines. */
11558 compute_line_metrics (&it);
7ce2c095 11559}
5f5c8ee5
GM
11560
11561
7ce2c095 11562\f
5f5c8ee5
GM
11563/***********************************************************************
11564 Mode Line
11565 ***********************************************************************/
11566
11567/* Display the mode and/or top line of window W. */
a2889657
JB
11568
11569static void
5f5c8ee5 11570display_mode_lines (w)
a2889657
JB
11571 struct window *w;
11572{
5f5c8ee5 11573 /* These will be set while the mode line specs are processed. */
aa6d10fa 11574 line_number_displayed = 0;
155ef550 11575 w->column_number_displayed = Qnil;
aa6d10fa 11576
5f5c8ee5 11577 if (WINDOW_WANTS_MODELINE_P (w))
045dee35
GM
11578 display_mode_line (w, MODE_LINE_FACE_ID,
11579 current_buffer->mode_line_format);
5f5c8ee5 11580
045dee35
GM
11581 if (WINDOW_WANTS_HEADER_LINE_P (w))
11582 display_mode_line (w, HEADER_LINE_FACE_ID,
11583 current_buffer->header_line_format);
5f5c8ee5 11584}
03b294dc 11585
03b294dc 11586
5f5c8ee5 11587/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 11588 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
5f5c8ee5 11589 FORMAT is the mode line format to display. */
03b294dc 11590
5f5c8ee5
GM
11591static void
11592display_mode_line (w, face_id, format)
11593 struct window *w;
11594 enum face_id face_id;
11595 Lisp_Object format;
11596{
11597 struct it it;
11598 struct face *face;
03b294dc 11599
5f5c8ee5
GM
11600 init_iterator (&it, w, -1, -1, NULL, face_id);
11601 prepare_desired_row (it.glyph_row);
11602
11603 /* Temporarily make frame's keyboard the current kboard so that
11604 kboard-local variables in the mode_line_format will get the right
11605 values. */
11606 push_frame_kboard (it.f);
11607 display_mode_element (&it, 0, 0, 0, format);
11608 pop_frame_kboard ();
a2889657 11609
5f5c8ee5
GM
11610 /* Fill up with spaces. */
11611 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11612
11613 compute_line_metrics (&it);
11614 it.glyph_row->full_width_p = 1;
11615 it.glyph_row->mode_line_p = 1;
11616 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11617 it.glyph_row->continued_p = 0;
11618 it.glyph_row->truncated_on_left_p = 0;
11619 it.glyph_row->truncated_on_right_p = 0;
11620
11621 /* Make a 3D mode-line have a shadow at its right end. */
11622 face = FACE_FROM_ID (it.f, face_id);
11623 extend_face_to_end_of_line (&it);
11624 if (face->box != FACE_NO_BOX)
d7eb09a0 11625 {
5f5c8ee5
GM
11626 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11627 + it.glyph_row->used[TEXT_AREA] - 1);
11628 last->right_box_line_p = 1;
d7eb09a0 11629 }
a2889657
JB
11630}
11631
a2889657 11632
5f5c8ee5
GM
11633/* Contribute ELT to the mode line for window IT->w. How it
11634 translates into text depends on its data type.
a2889657 11635
5f5c8ee5 11636 IT describes the display environment in which we display, as usual.
a2889657
JB
11637
11638 DEPTH is the depth in recursion. It is used to prevent
11639 infinite recursion here.
11640
5f5c8ee5
GM
11641 FIELD_WIDTH is the number of characters the display of ELT should
11642 occupy in the mode line, and PRECISION is the maximum number of
11643 characters to display from ELT's representation. See
11644 display_string for details. *
a2889657 11645
5f5c8ee5 11646 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
11647
11648static int
5f5c8ee5
GM
11649display_mode_element (it, depth, field_width, precision, elt)
11650 struct it *it;
a2889657 11651 int depth;
5f5c8ee5
GM
11652 int field_width, precision;
11653 Lisp_Object elt;
a2889657 11654{
5f5c8ee5
GM
11655 int n = 0, field, prec;
11656
a2889657
JB
11657 tail_recurse:
11658 if (depth > 10)
11659 goto invalid;
11660
11661 depth++;
11662
0220c518 11663 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
11664 {
11665 case Lisp_String:
11666 {
11667 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
11668 unsigned char c;
11669 unsigned char *this = XSTRING (elt)->data;
11670 unsigned char *lisp_string = this;
11671
11672 while ((precision <= 0 || n < precision)
11673 && *this
11674 && (frame_title_ptr
11675 || it->current_x < it->last_visible_x))
a2889657
JB
11676 {
11677 unsigned char *last = this;
5f5c8ee5
GM
11678
11679 /* Advance to end of string or next format specifier. */
a2889657
JB
11680 while ((c = *this++) != '\0' && c != '%')
11681 ;
5f5c8ee5 11682
a2889657
JB
11683 if (this - 1 != last)
11684 {
5f5c8ee5
GM
11685 /* Output to end of string or up to '%'. Field width
11686 is length of string. Don't output more than
11687 PRECISION allows us. */
11688 prec = --this - last;
11689 if (precision > 0 && prec > precision - n)
11690 prec = precision - n;
11691
d39b6696 11692 if (frame_title_ptr)
5f5c8ee5 11693 n += store_frame_title (last, prec, prec);
d39b6696 11694 else
5f5c8ee5
GM
11695 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11696 it, 0, prec, 0, -1);
a2889657
JB
11697 }
11698 else /* c == '%' */
11699 {
5f5c8ee5
GM
11700 unsigned char *percent_position = this;
11701
11702 /* Get the specified minimum width. Zero means
11703 don't pad. */
11704 field = 0;
a2889657 11705 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 11706 field = field * 10 + c - '0';
a2889657 11707
5f5c8ee5
GM
11708 /* Don't pad beyond the total padding allowed. */
11709 if (field_width - n > 0 && field > field_width - n)
11710 field = field_width - n;
a2889657 11711
5f5c8ee5
GM
11712 /* Note that either PRECISION <= 0 or N < PRECISION. */
11713 prec = precision - n;
11714
a2889657 11715 if (c == 'M')
5f5c8ee5
GM
11716 n += display_mode_element (it, depth, field, prec,
11717 Vglobal_mode_string);
a2889657 11718 else if (c != 0)
d39b6696 11719 {
5f5c8ee5
GM
11720 unsigned char *spec
11721 = decode_mode_spec (it->w, c, field, prec);
11722
d39b6696 11723 if (frame_title_ptr)
5f5c8ee5 11724 n += store_frame_title (spec, field, prec);
d39b6696 11725 else
5f5c8ee5
GM
11726 {
11727 int nglyphs_before
11728 = it->glyph_row->used[TEXT_AREA];
11729 int charpos
11730 = percent_position - XSTRING (elt)->data;
11731 int nwritten
11732 = display_string (spec, Qnil, elt, charpos, 0, it,
11733 field, prec, 0, -1);
11734
11735 /* Assign to the glyphs written above the
11736 string where the `%x' came from, position
11737 of the `%'. */
11738 if (nwritten > 0)
11739 {
11740 struct glyph *glyph
11741 = (it->glyph_row->glyphs[TEXT_AREA]
11742 + nglyphs_before);
11743 int i;
11744
11745 for (i = 0; i < nwritten; ++i)
11746 {
11747 glyph[i].object = elt;
11748 glyph[i].charpos = charpos;
11749 }
11750
11751 n += nwritten;
11752 }
11753 }
d39b6696 11754 }
a2889657
JB
11755 }
11756 }
11757 }
11758 break;
11759
11760 case Lisp_Symbol:
11761 /* A symbol: process the value of the symbol recursively
11762 as if it appeared here directly. Avoid error if symbol void.
11763 Special case: if value of symbol is a string, output the string
11764 literally. */
11765 {
11766 register Lisp_Object tem;
11767 tem = Fboundp (elt);
265a9e55 11768 if (!NILP (tem))
a2889657
JB
11769 {
11770 tem = Fsymbol_value (elt);
11771 /* If value is a string, output that string literally:
11772 don't check for % within it. */
e24c997d 11773 if (STRINGP (tem))
d39b6696 11774 {
5f5c8ee5
GM
11775 prec = XSTRING (tem)->size;
11776 if (precision > 0 && prec > precision - n)
11777 prec = precision - n;
d39b6696 11778 if (frame_title_ptr)
5f5c8ee5 11779 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 11780 else
5f5c8ee5
GM
11781 n += display_string (NULL, tem, Qnil, 0, 0, it,
11782 0, prec, 0, -1);
d39b6696 11783 }
a2889657 11784 else if (!EQ (tem, elt))
5f5c8ee5
GM
11785 {
11786 /* Give up right away for nil or t. */
11787 elt = tem;
11788 goto tail_recurse;
11789 }
a2889657
JB
11790 }
11791 }
11792 break;
11793
11794 case Lisp_Cons:
11795 {
11796 register Lisp_Object car, tem;
11797
11798 /* A cons cell: three distinct cases.
11799 If first element is a string or a cons, process all the elements
11800 and effectively concatenate them.
11801 If first element is a negative number, truncate displaying cdr to
11802 at most that many characters. If positive, pad (with spaces)
11803 to at least that many characters.
11804 If first element is a symbol, process the cadr or caddr recursively
11805 according to whether the symbol's value is non-nil or nil. */
9472f927 11806 car = XCAR (elt);
5f5c8ee5
GM
11807 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11808 {
11809 /* An element of the form (:eval FORM) means evaluate FORM
11810 and use the result as mode line elements. */
11811 struct gcpro gcpro1;
11812 Lisp_Object spec;
11813
11814 spec = eval_form (XCAR (XCDR (elt)));
11815 GCPRO1 (spec);
11816 n += display_mode_element (it, depth, field_width - n,
11817 precision - n, spec);
11818 UNGCPRO;
11819 }
11820 else if (SYMBOLP (car))
a2889657
JB
11821 {
11822 tem = Fboundp (car);
9472f927 11823 elt = XCDR (elt);
e24c997d 11824 if (!CONSP (elt))
a2889657
JB
11825 goto invalid;
11826 /* elt is now the cdr, and we know it is a cons cell.
11827 Use its car if CAR has a non-nil value. */
265a9e55 11828 if (!NILP (tem))
a2889657
JB
11829 {
11830 tem = Fsymbol_value (car);
265a9e55 11831 if (!NILP (tem))
9472f927
GM
11832 {
11833 elt = XCAR (elt);
11834 goto tail_recurse;
11835 }
a2889657
JB
11836 }
11837 /* Symbol's value is nil (or symbol is unbound)
11838 Get the cddr of the original list
11839 and if possible find the caddr and use that. */
9472f927 11840 elt = XCDR (elt);
265a9e55 11841 if (NILP (elt))
a2889657 11842 break;
e24c997d 11843 else if (!CONSP (elt))
a2889657 11844 goto invalid;
9472f927 11845 elt = XCAR (elt);
a2889657
JB
11846 goto tail_recurse;
11847 }
e24c997d 11848 else if (INTEGERP (car))
a2889657
JB
11849 {
11850 register int lim = XINT (car);
9472f927 11851 elt = XCDR (elt);
a2889657 11852 if (lim < 0)
5f5c8ee5
GM
11853 {
11854 /* Negative int means reduce maximum width. */
11855 if (precision <= 0)
11856 precision = -lim;
11857 else
11858 precision = min (precision, -lim);
11859 }
a2889657
JB
11860 else if (lim > 0)
11861 {
11862 /* Padding specified. Don't let it be more than
11863 current maximum. */
5f5c8ee5
GM
11864 if (precision > 0)
11865 lim = min (precision, lim);
11866
a2889657
JB
11867 /* If that's more padding than already wanted, queue it.
11868 But don't reduce padding already specified even if
11869 that is beyond the current truncation point. */
5f5c8ee5 11870 field_width = max (lim, field_width);
a2889657
JB
11871 }
11872 goto tail_recurse;
11873 }
e24c997d 11874 else if (STRINGP (car) || CONSP (car))
a2889657
JB
11875 {
11876 register int limit = 50;
5f5c8ee5
GM
11877 /* Limit is to protect against circular lists. */
11878 while (CONSP (elt)
11879 && --limit > 0
11880 && (precision <= 0 || n < precision))
a2889657 11881 {
5f5c8ee5 11882 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
11883 precision - n, XCAR (elt));
11884 elt = XCDR (elt);
a2889657
JB
11885 }
11886 }
11887 }
11888 break;
11889
11890 default:
11891 invalid:
d39b6696 11892 if (frame_title_ptr)
5f5c8ee5 11893 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 11894 else
5f5c8ee5
GM
11895 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11896 precision - n, 0, 0);
11897 return n;
a2889657
JB
11898 }
11899
5f5c8ee5
GM
11900 /* Pad to FIELD_WIDTH. */
11901 if (field_width > 0 && n < field_width)
11902 {
11903 if (frame_title_ptr)
11904 n += store_frame_title ("", field_width - n, 0);
11905 else
11906 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11907 0, 0, 0);
11908 }
11909
11910 return n;
a2889657 11911}
5f5c8ee5
GM
11912
11913
766525bc
RS
11914/* Write a null-terminated, right justified decimal representation of
11915 the positive integer D to BUF using a minimal field width WIDTH. */
11916
11917static void
11918pint2str (buf, width, d)
11919 register char *buf;
11920 register int width;
11921 register int d;
11922{
11923 register char *p = buf;
11924
11925 if (d <= 0)
5f5c8ee5 11926 *p++ = '0';
766525bc 11927 else
5f5c8ee5 11928 {
766525bc 11929 while (d > 0)
5f5c8ee5 11930 {
766525bc
RS
11931 *p++ = d % 10 + '0';
11932 d /= 10;
5f5c8ee5
GM
11933 }
11934 }
11935
11936 for (width -= (int) (p - buf); width > 0; --width)
11937 *p++ = ' ';
766525bc
RS
11938 *p-- = '\0';
11939 while (p > buf)
5f5c8ee5 11940 {
766525bc
RS
11941 d = *buf;
11942 *buf++ = *p;
11943 *p-- = d;
5f5c8ee5 11944 }
766525bc
RS
11945}
11946
5f5c8ee5 11947/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
11948 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11949 type of CODING_SYSTEM. Return updated pointer into BUF. */
11950
6693a99a 11951static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 11952
1c9241f5
KH
11953static char *
11954decode_mode_spec_coding (coding_system, buf, eol_flag)
11955 Lisp_Object coding_system;
11956 register char *buf;
11957 int eol_flag;
11958{
1e1078d6 11959 Lisp_Object val;
916848d8 11960 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
11961 unsigned char *eol_str;
11962 int eol_str_len;
11963 /* The EOL conversion we are using. */
11964 Lisp_Object eoltype;
1e1078d6 11965
4a09dee0 11966 val = Fget (coding_system, Qcoding_system);
1c9241f5 11967
4a09dee0 11968 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 11969 {
916848d8
RS
11970 if (multibyte)
11971 *buf++ = '-';
21e989e3 11972 if (eol_flag)
302f2b38 11973 eoltype = eol_mnemonic_undecided;
1e1078d6 11974 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
11975 }
11976 else
11977 {
1e1078d6
RS
11978 Lisp_Object eolvalue;
11979
11980 eolvalue = Fget (coding_system, Qeol_type);
11981
916848d8
RS
11982 if (multibyte)
11983 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11984
1c9241f5
KH
11985 if (eol_flag)
11986 {
1e1078d6
RS
11987 /* The EOL conversion that is normal on this system. */
11988
11989 if (NILP (eolvalue)) /* Not yet decided. */
11990 eoltype = eol_mnemonic_undecided;
11991 else if (VECTORP (eolvalue)) /* Not yet decided. */
11992 eoltype = eol_mnemonic_undecided;
11993 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11994 eoltype = (XFASTINT (eolvalue) == 0
11995 ? eol_mnemonic_unix
11996 : (XFASTINT (eolvalue) == 1
11997 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
11998 }
11999 }
5f5c8ee5 12000
302f2b38
EZ
12001 if (eol_flag)
12002 {
12003 /* Mention the EOL conversion if it is not the usual one. */
12004 if (STRINGP (eoltype))
12005 {
12006 eol_str = XSTRING (eoltype)->data;
12007 eol_str_len = XSTRING (eoltype)->size;
12008 }
f30b3499
KH
12009 else if (INTEGERP (eoltype)
12010 && CHAR_VALID_P (XINT (eoltype), 0))
12011 {
4a09dee0 12012 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 12013 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 12014 }
302f2b38
EZ
12015 else
12016 {
12017 eol_str = invalid_eol_type;
12018 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 12019 }
f30b3499 12020 bcopy (eol_str, buf, eol_str_len);
302f2b38 12021 buf += eol_str_len;
1c9241f5 12022 }
302f2b38 12023
1c9241f5
KH
12024 return buf;
12025}
12026
a2889657 12027/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
12028 generated by character C. PRECISION >= 0 means don't return a
12029 string longer than that value. FIELD_WIDTH > 0 means pad the
12030 string returned with spaces to that value. */
a2889657 12031
11e82b76
JB
12032static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12033
a2889657 12034static char *
5f5c8ee5 12035decode_mode_spec (w, c, field_width, precision)
a2889657 12036 struct window *w;
68c45bf0 12037 register int c;
5f5c8ee5 12038 int field_width, precision;
a2889657 12039{
0b67772d 12040 Lisp_Object obj;
5f5c8ee5
GM
12041 struct frame *f = XFRAME (WINDOW_FRAME (w));
12042 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 12043 struct buffer *b = XBUFFER (w->buffer);
a2889657 12044
0b67772d 12045 obj = Qnil;
a2889657
JB
12046
12047 switch (c)
12048 {
1af9f229
RS
12049 case '*':
12050 if (!NILP (b->read_only))
12051 return "%";
12052 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12053 return "*";
12054 return "-";
12055
12056 case '+':
12057 /* This differs from %* only for a modified read-only buffer. */
12058 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12059 return "*";
12060 if (!NILP (b->read_only))
12061 return "%";
12062 return "-";
12063
12064 case '&':
12065 /* This differs from %* in ignoring read-only-ness. */
12066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12067 return "*";
12068 return "-";
12069
12070 case '%':
12071 return "%";
12072
12073 case '[':
12074 {
12075 int i;
12076 char *p;
12077
12078 if (command_loop_level > 5)
12079 return "[[[... ";
12080 p = decode_mode_spec_buf;
12081 for (i = 0; i < command_loop_level; i++)
12082 *p++ = '[';
12083 *p = 0;
12084 return decode_mode_spec_buf;
12085 }
12086
12087 case ']':
12088 {
12089 int i;
12090 char *p;
12091
12092 if (command_loop_level > 5)
12093 return " ...]]]";
12094 p = decode_mode_spec_buf;
12095 for (i = 0; i < command_loop_level; i++)
12096 *p++ = ']';
12097 *p = 0;
12098 return decode_mode_spec_buf;
12099 }
12100
12101 case '-':
12102 {
1af9f229 12103 register int i;
5f5c8ee5
GM
12104
12105 /* Let lots_of_dashes be a string of infinite length. */
12106 if (field_width <= 0
12107 || field_width > sizeof (lots_of_dashes))
1af9f229 12108 {
5f5c8ee5
GM
12109 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12110 decode_mode_spec_buf[i] = '-';
12111 decode_mode_spec_buf[i] = '\0';
12112 return decode_mode_spec_buf;
1af9f229 12113 }
5f5c8ee5
GM
12114 else
12115 return lots_of_dashes;
1af9f229
RS
12116 }
12117
a2889657 12118 case 'b':
d39b6696 12119 obj = b->name;
a2889657
JB
12120 break;
12121
1af9f229
RS
12122 case 'c':
12123 {
12124 int col = current_column ();
12125 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 12126 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
12127 return decode_mode_spec_buf;
12128 }
12129
12130 case 'F':
12131 /* %F displays the frame name. */
5f5c8ee5 12132 if (!NILP (f->title))
95184b48 12133 return (char *) XSTRING (f->title)->data;
fd8ff63d 12134 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 12135 return (char *) XSTRING (f->name)->data;
9c6da96f 12136 return "Emacs";
1af9f229 12137
a2889657 12138 case 'f':
d39b6696 12139 obj = b->filename;
a2889657
JB
12140 break;
12141
aa6d10fa
RS
12142 case 'l':
12143 {
12adba34
RS
12144 int startpos = XMARKER (w->start)->charpos;
12145 int startpos_byte = marker_byte_position (w->start);
12146 int line, linepos, linepos_byte, topline;
aa6d10fa 12147 int nlines, junk;
aa6d10fa
RS
12148 int height = XFASTINT (w->height);
12149
12150 /* If we decided that this buffer isn't suitable for line numbers,
12151 don't forget that too fast. */
12152 if (EQ (w->base_line_pos, w->buffer))
766525bc 12153 goto no_value;
5300fd39
RS
12154 /* But do forget it, if the window shows a different buffer now. */
12155 else if (BUFFERP (w->base_line_pos))
12156 w->base_line_pos = Qnil;
aa6d10fa
RS
12157
12158 /* If the buffer is very big, don't waste time. */
d39b6696 12159 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
12160 {
12161 w->base_line_pos = Qnil;
12162 w->base_line_number = Qnil;
766525bc 12163 goto no_value;
aa6d10fa
RS
12164 }
12165
12166 if (!NILP (w->base_line_number)
12167 && !NILP (w->base_line_pos)
12adba34 12168 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
12169 {
12170 line = XFASTINT (w->base_line_number);
12171 linepos = XFASTINT (w->base_line_pos);
12adba34 12172 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
12173 }
12174 else
12175 {
12176 line = 1;
d39b6696 12177 linepos = BUF_BEGV (b);
12adba34 12178 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
12179 }
12180
12181 /* Count lines from base line to window start position. */
12adba34
RS
12182 nlines = display_count_lines (linepos, linepos_byte,
12183 startpos_byte,
12184 startpos, &junk);
aa6d10fa
RS
12185
12186 topline = nlines + line;
12187
12188 /* Determine a new base line, if the old one is too close
12189 or too far away, or if we did not have one.
12190 "Too close" means it's plausible a scroll-down would
12191 go back past it. */
d39b6696 12192 if (startpos == BUF_BEGV (b))
aa6d10fa 12193 {
c2213350
KH
12194 XSETFASTINT (w->base_line_number, topline);
12195 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
12196 }
12197 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 12198 || linepos == BUF_BEGV (b))
aa6d10fa 12199 {
d39b6696 12200 int limit = BUF_BEGV (b);
12adba34 12201 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 12202 int position;
5d121aec 12203 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
12204
12205 if (startpos - distance > limit)
12adba34
RS
12206 {
12207 limit = startpos - distance;
12208 limit_byte = CHAR_TO_BYTE (limit);
12209 }
aa6d10fa 12210
12adba34
RS
12211 nlines = display_count_lines (startpos, startpos_byte,
12212 limit_byte,
12213 - (height * 2 + 30),
aa6d10fa
RS
12214 &position);
12215 /* If we couldn't find the lines we wanted within
5d121aec 12216 line_number_display_limit_width chars per line,
aa6d10fa 12217 give up on line numbers for this window. */
12adba34 12218 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
12219 {
12220 w->base_line_pos = w->buffer;
12221 w->base_line_number = Qnil;
766525bc 12222 goto no_value;
aa6d10fa
RS
12223 }
12224
c2213350 12225 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 12226 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
12227 }
12228
12229 /* Now count lines from the start pos to point. */
12adba34
RS
12230 nlines = display_count_lines (startpos, startpos_byte,
12231 PT_BYTE, PT, &junk);
aa6d10fa
RS
12232
12233 /* Record that we did display the line number. */
12234 line_number_displayed = 1;
12235
12236 /* Make the string to show. */
5f5c8ee5 12237 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 12238 return decode_mode_spec_buf;
766525bc
RS
12239 no_value:
12240 {
12241 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
12242 int pad = field_width - 2;
12243 while (pad-- > 0)
12244 *p++ = ' ';
12245 *p++ = '?';
12246 *p = '?';
766525bc
RS
12247 return decode_mode_spec_buf;
12248 }
aa6d10fa
RS
12249 }
12250 break;
12251
a2889657 12252 case 'm':
d39b6696 12253 obj = b->mode_name;
a2889657
JB
12254 break;
12255
12256 case 'n':
d39b6696 12257 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
12258 return " Narrow";
12259 break;
12260
a2889657
JB
12261 case 'p':
12262 {
12263 int pos = marker_position (w->start);
d39b6696 12264 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 12265
d39b6696 12266 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 12267 {
d39b6696 12268 if (pos <= BUF_BEGV (b))
a2889657
JB
12269 return "All";
12270 else
12271 return "Bottom";
12272 }
d39b6696 12273 else if (pos <= BUF_BEGV (b))
a2889657
JB
12274 return "Top";
12275 else
12276 {
3c7d31b9
RS
12277 if (total > 1000000)
12278 /* Do it differently for a large value, to avoid overflow. */
12279 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12280 else
12281 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
12282 /* We can't normally display a 3-digit number,
12283 so get us a 2-digit number that is close. */
12284 if (total == 100)
12285 total = 99;
12286 sprintf (decode_mode_spec_buf, "%2d%%", total);
12287 return decode_mode_spec_buf;
12288 }
12289 }
12290
8ffcb79f
RS
12291 /* Display percentage of size above the bottom of the screen. */
12292 case 'P':
12293 {
12294 int toppos = marker_position (w->start);
d39b6696
KH
12295 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12296 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 12297
d39b6696 12298 if (botpos >= BUF_ZV (b))
8ffcb79f 12299 {
d39b6696 12300 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12301 return "All";
12302 else
12303 return "Bottom";
12304 }
12305 else
12306 {
3c7d31b9
RS
12307 if (total > 1000000)
12308 /* Do it differently for a large value, to avoid overflow. */
12309 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12310 else
12311 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
12312 /* We can't normally display a 3-digit number,
12313 so get us a 2-digit number that is close. */
12314 if (total == 100)
12315 total = 99;
d39b6696 12316 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12317 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12318 else
12319 sprintf (decode_mode_spec_buf, "%2d%%", total);
12320 return decode_mode_spec_buf;
12321 }
12322 }
12323
1af9f229
RS
12324 case 's':
12325 /* status of process */
12326 obj = Fget_buffer_process (w->buffer);
12327 if (NILP (obj))
12328 return "no process";
12329#ifdef subprocesses
12330 obj = Fsymbol_name (Fprocess_status (obj));
12331#endif
12332 break;
d39b6696 12333
1af9f229
RS
12334 case 't': /* indicate TEXT or BINARY */
12335#ifdef MODE_LINE_BINARY_TEXT
12336 return MODE_LINE_BINARY_TEXT (b);
12337#else
12338 return "T";
12339#endif
1c9241f5
KH
12340
12341 case 'z':
12342 /* coding-system (not including end-of-line format) */
12343 case 'Z':
12344 /* coding-system (including end-of-line type) */
12345 {
12346 int eol_flag = (c == 'Z');
539b4d41 12347 char *p = decode_mode_spec_buf;
1c9241f5 12348
d30e754b 12349 if (! FRAME_WINDOW_P (f))
1c9241f5 12350 {
11c52c4f
RS
12351 /* No need to mention EOL here--the terminal never needs
12352 to do EOL conversion. */
12353 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12354 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 12355 }
f13c925f 12356 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 12357 p, eol_flag);
f13c925f 12358
11c52c4f 12359#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
12360#ifdef subprocesses
12361 obj = Fget_buffer_process (Fcurrent_buffer ());
12362 if (PROCESSP (obj))
12363 {
12364 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12365 p, eol_flag);
12366 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12367 p, eol_flag);
12368 }
12369#endif /* subprocesses */
11c52c4f 12370#endif /* 0 */
1c9241f5
KH
12371 *p = 0;
12372 return decode_mode_spec_buf;
12373 }
a2889657 12374 }
d39b6696 12375
e24c997d 12376 if (STRINGP (obj))
a2889657
JB
12377 return (char *) XSTRING (obj)->data;
12378 else
12379 return "";
12380}
5f5c8ee5
GM
12381
12382
12adba34
RS
12383/* Count up to COUNT lines starting from START / START_BYTE.
12384 But don't go beyond LIMIT_BYTE.
12385 Return the number of lines thus found (always nonnegative).
59b49f63 12386
12adba34 12387 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
12388
12389static int
12adba34
RS
12390display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12391 int start, start_byte, limit_byte, count;
12392 int *byte_pos_ptr;
59b49f63 12393{
59b49f63
RS
12394 register unsigned char *cursor;
12395 unsigned char *base;
12396
12397 register int ceiling;
12398 register unsigned char *ceiling_addr;
12adba34 12399 int orig_count = count;
59b49f63
RS
12400
12401 /* If we are not in selective display mode,
12402 check only for newlines. */
12adba34
RS
12403 int selective_display = (!NILP (current_buffer->selective_display)
12404 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
12405
12406 if (count > 0)
12adba34
RS
12407 {
12408 while (start_byte < limit_byte)
12409 {
12410 ceiling = BUFFER_CEILING_OF (start_byte);
12411 ceiling = min (limit_byte - 1, ceiling);
12412 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12413 base = (cursor = BYTE_POS_ADDR (start_byte));
12414 while (1)
12415 {
12416 if (selective_display)
12417 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12418 ;
12419 else
12420 while (*cursor != '\n' && ++cursor != ceiling_addr)
12421 ;
12422
12423 if (cursor != ceiling_addr)
12424 {
12425 if (--count == 0)
12426 {
12427 start_byte += cursor - base + 1;
12428 *byte_pos_ptr = start_byte;
12429 return orig_count;
12430 }
12431 else
12432 if (++cursor == ceiling_addr)
12433 break;
12434 }
12435 else
12436 break;
12437 }
12438 start_byte += cursor - base;
12439 }
12440 }
59b49f63
RS
12441 else
12442 {
12adba34
RS
12443 while (start_byte > limit_byte)
12444 {
12445 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12446 ceiling = max (limit_byte, ceiling);
12447 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12448 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
12449 while (1)
12450 {
12adba34
RS
12451 if (selective_display)
12452 while (--cursor != ceiling_addr
12453 && *cursor != '\n' && *cursor != 015)
12454 ;
12455 else
12456 while (--cursor != ceiling_addr && *cursor != '\n')
12457 ;
12458
59b49f63
RS
12459 if (cursor != ceiling_addr)
12460 {
12461 if (++count == 0)
12462 {
12adba34
RS
12463 start_byte += cursor - base + 1;
12464 *byte_pos_ptr = start_byte;
12465 /* When scanning backwards, we should
12466 not count the newline posterior to which we stop. */
12467 return - orig_count - 1;
59b49f63
RS
12468 }
12469 }
12470 else
12471 break;
12472 }
12adba34
RS
12473 /* Here we add 1 to compensate for the last decrement
12474 of CURSOR, which took it past the valid range. */
12475 start_byte += cursor - base + 1;
59b49f63
RS
12476 }
12477 }
12478
12adba34 12479 *byte_pos_ptr = limit_byte;
aa6d10fa 12480
12adba34
RS
12481 if (count < 0)
12482 return - orig_count + count;
12483 return orig_count - count;
aa6d10fa 12484
12adba34 12485}
a2889657 12486
a2889657 12487
5f5c8ee5
GM
12488\f
12489/***********************************************************************
12490 Displaying strings
12491 ***********************************************************************/
278feba9 12492
5f5c8ee5 12493/* Display a NUL-terminated string, starting with index START.
a3788d53 12494
5f5c8ee5
GM
12495 If STRING is non-null, display that C string. Otherwise, the Lisp
12496 string LISP_STRING is displayed.
a2889657 12497
5f5c8ee5
GM
12498 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12499 FACE_STRING. Display STRING or LISP_STRING with the face at
12500 FACE_STRING_POS in FACE_STRING:
a2889657 12501
5f5c8ee5
GM
12502 Display the string in the environment given by IT, but use the
12503 standard display table, temporarily.
a3788d53 12504
5f5c8ee5
GM
12505 FIELD_WIDTH is the minimum number of output glyphs to produce.
12506 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12507 with spaces. If STRING has more characters, more than FIELD_WIDTH
12508 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12509
12510 PRECISION is the maximum number of characters to output from
12511 STRING. PRECISION < 0 means don't truncate the string.
a2889657 12512
5f5c8ee5 12513 This is roughly equivalent to printf format specifiers:
a2889657 12514
5f5c8ee5
GM
12515 FIELD_WIDTH PRECISION PRINTF
12516 ----------------------------------------
12517 -1 -1 %s
12518 -1 10 %.10s
12519 10 -1 %10s
12520 20 10 %20.10s
a2889657 12521
5f5c8ee5
GM
12522 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12523 display them, and < 0 means obey the current buffer's value of
12524 enable_multibyte_characters.
278feba9 12525
5f5c8ee5 12526 Value is the number of glyphs produced. */
b1d1124b 12527
5f5c8ee5
GM
12528static int
12529display_string (string, lisp_string, face_string, face_string_pos,
12530 start, it, field_width, precision, max_x, multibyte)
12531 unsigned char *string;
12532 Lisp_Object lisp_string;
68c45bf0
PE
12533 Lisp_Object face_string;
12534 int face_string_pos;
5f5c8ee5
GM
12535 int start;
12536 struct it *it;
12537 int field_width, precision, max_x;
12538 int multibyte;
12539{
12540 int hpos_at_start = it->hpos;
12541 int saved_face_id = it->face_id;
12542 struct glyph_row *row = it->glyph_row;
12543
12544 /* Initialize the iterator IT for iteration over STRING beginning
12545 with index START. We assume that IT may be modified here (which
12546 means that display_line has to do something when displaying a
12547 mini-buffer prompt, which it does). */
12548 reseat_to_string (it, string, lisp_string, start,
12549 precision, field_width, multibyte);
12550
12551 /* If displaying STRING, set up the face of the iterator
12552 from LISP_STRING, if that's given. */
12553 if (STRINGP (face_string))
12554 {
12555 int endptr;
12556 struct face *face;
12557
12558 it->face_id
12559 = face_at_string_position (it->w, face_string, face_string_pos,
12560 0, it->region_beg_charpos,
12561 it->region_end_charpos,
12562 &endptr, it->base_face_id);
12563 face = FACE_FROM_ID (it->f, it->face_id);
12564 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 12565 }
a2889657 12566
5f5c8ee5
GM
12567 /* Set max_x to the maximum allowed X position. Don't let it go
12568 beyond the right edge of the window. */
12569 if (max_x <= 0)
12570 max_x = it->last_visible_x;
12571 else
12572 max_x = min (max_x, it->last_visible_x);
efc63ef0 12573
5f5c8ee5
GM
12574 /* Skip over display elements that are not visible. because IT->w is
12575 hscrolled. */
12576 if (it->current_x < it->first_visible_x)
12577 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12578 MOVE_TO_POS | MOVE_TO_X);
a2889657 12579
5f5c8ee5
GM
12580 row->ascent = it->max_ascent;
12581 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12582 row->phys_ascent = it->max_phys_ascent;
12583 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 12584
5f5c8ee5
GM
12585 /* This condition is for the case that we are called with current_x
12586 past last_visible_x. */
12587 while (it->current_x < max_x)
a2889657 12588 {
5f5c8ee5 12589 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 12590
5f5c8ee5
GM
12591 /* Get the next display element. */
12592 if (!get_next_display_element (it))
90adcf20 12593 break;
1c9241f5 12594
5f5c8ee5
GM
12595 /* Produce glyphs. */
12596 x_before = it->current_x;
12597 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12598 PRODUCE_GLYPHS (it);
90adcf20 12599
5f5c8ee5
GM
12600 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12601 i = 0;
12602 x = x_before;
12603 while (i < nglyphs)
a2889657 12604 {
5f5c8ee5
GM
12605 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12606
12607 if (!it->truncate_lines_p
12608 && x + glyph->pixel_width > max_x)
12609 {
12610 /* End of continued line or max_x reached. */
12611 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12612 it->current_x = x;
12613 break;
12614 }
12615 else if (x + glyph->pixel_width > it->first_visible_x)
12616 {
12617 /* Glyph is at least partially visible. */
12618 ++it->hpos;
12619 if (x < it->first_visible_x)
12620 it->glyph_row->x = x - it->first_visible_x;
12621 }
12622 else
a2889657 12623 {
5f5c8ee5
GM
12624 /* Glyph is off the left margin of the display area.
12625 Should not happen. */
12626 abort ();
a2889657 12627 }
5f5c8ee5
GM
12628
12629 row->ascent = max (row->ascent, it->max_ascent);
12630 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12631 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12632 row->phys_height = max (row->phys_height,
12633 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12634 x += glyph->pixel_width;
12635 ++i;
a2889657 12636 }
5f5c8ee5
GM
12637
12638 /* Stop if max_x reached. */
12639 if (i < nglyphs)
12640 break;
12641
12642 /* Stop at line ends. */
12643 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 12644 {
5f5c8ee5
GM
12645 it->continuation_lines_width = 0;
12646 break;
a2889657 12647 }
1c9241f5 12648
5f5c8ee5 12649 set_iterator_to_next (it);
a688bb24 12650
5f5c8ee5
GM
12651 /* Stop if truncating at the right edge. */
12652 if (it->truncate_lines_p
12653 && it->current_x >= it->last_visible_x)
12654 {
12655 /* Add truncation mark, but don't do it if the line is
12656 truncated at a padding space. */
12657 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 12658 {
5f5c8ee5
GM
12659 if (!FRAME_WINDOW_P (it->f))
12660 produce_special_glyphs (it, IT_TRUNCATION);
12661 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 12662 }
5f5c8ee5 12663 break;
1c9241f5 12664 }
a2889657
JB
12665 }
12666
5f5c8ee5
GM
12667 /* Maybe insert a truncation at the left. */
12668 if (it->first_visible_x
12669 && IT_CHARPOS (*it) > 0)
a2889657 12670 {
5f5c8ee5
GM
12671 if (!FRAME_WINDOW_P (it->f))
12672 insert_left_trunc_glyphs (it);
12673 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
12674 }
12675
5f5c8ee5
GM
12676 it->face_id = saved_face_id;
12677
12678 /* Value is number of columns displayed. */
12679 return it->hpos - hpos_at_start;
12680}
a2889657 12681
a2889657 12682
a2889657 12683\f
5f5c8ee5
GM
12684/* This is like a combination of memq and assq. Return 1 if PROPVAL
12685 appears as an element of LIST or as the car of an element of LIST.
12686 If PROPVAL is a list, compare each element against LIST in that
12687 way, and return 1 if any element of PROPVAL is found in LIST.
12688 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12689
12690int
12691invisible_p (propval, list)
12692 register Lisp_Object propval;
12693 Lisp_Object list;
12694{
af460d46 12695 register Lisp_Object tail, proptail;
9472f927 12696 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
12697 {
12698 register Lisp_Object tem;
9472f927 12699 tem = XCAR (tail);
642eefc6
RS
12700 if (EQ (propval, tem))
12701 return 1;
9472f927 12702 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
12703 return 1;
12704 }
af460d46
RS
12705 if (CONSP (propval))
12706 for (proptail = propval; CONSP (proptail);
9472f927 12707 proptail = XCDR (proptail))
af460d46
RS
12708 {
12709 Lisp_Object propelt;
9472f927
GM
12710 propelt = XCAR (proptail);
12711 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
12712 {
12713 register Lisp_Object tem;
9472f927 12714 tem = XCAR (tail);
af460d46
RS
12715 if (EQ (propelt, tem))
12716 return 1;
9472f927 12717 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
af460d46
RS
12718 return 1;
12719 }
12720 }
642eefc6
RS
12721 return 0;
12722}
12723
5f5c8ee5
GM
12724
12725/* Return 1 if PROPVAL appears as the car of an element of LIST and
12726 the cdr of that element is non-nil. If PROPVAL is a list, check
12727 each element of PROPVAL in that way, and the first time some
12728 element is found, return 1 if the cdr of that element is non-nil.
12729 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12730
12731int
12732invisible_ellipsis_p (propval, list)
12733 register Lisp_Object propval;
12734 Lisp_Object list;
12735{
af460d46 12736 register Lisp_Object tail, proptail;
9472f927
GM
12737
12738 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
12739 {
12740 register Lisp_Object tem;
9472f927
GM
12741 tem = XCAR (tail);
12742 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12743 return ! NILP (XCDR (tem));
642eefc6 12744 }
9472f927 12745
af460d46 12746 if (CONSP (propval))
9472f927 12747 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
12748 {
12749 Lisp_Object propelt;
9472f927
GM
12750 propelt = XCAR (proptail);
12751 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
12752 {
12753 register Lisp_Object tem;
9472f927
GM
12754 tem = XCAR (tail);
12755 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12756 return ! NILP (XCDR (tem));
af460d46
RS
12757 }
12758 }
9472f927 12759
642eefc6
RS
12760 return 0;
12761}
5f5c8ee5
GM
12762
12763
642eefc6 12764\f
5f5c8ee5
GM
12765/***********************************************************************
12766 Initialization
12767 ***********************************************************************/
12768
a2889657
JB
12769void
12770syms_of_xdisp ()
12771{
c6e89d6c
GM
12772 Vwith_echo_area_save_vector = Qnil;
12773 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 12774
c6e89d6c
GM
12775 Vmessage_stack = Qnil;
12776 staticpro (&Vmessage_stack);
12777
735c094c 12778 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 12779 staticpro (&Qinhibit_redisplay);
735c094c 12780
5f5c8ee5
GM
12781#if GLYPH_DEBUG
12782 defsubr (&Sdump_glyph_matrix);
12783 defsubr (&Sdump_glyph_row);
e037b9ec 12784 defsubr (&Sdump_tool_bar_row);
5f5c8ee5 12785 defsubr (&Strace_redisplay_toggle);
bf9249e3 12786 defsubr (&Strace_to_stderr);
5f5c8ee5
GM
12787#endif
12788
cf074754
RS
12789 staticpro (&Qmenu_bar_update_hook);
12790 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12791
d46fb96a 12792 staticpro (&Qoverriding_terminal_local_map);
7079aefa 12793 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 12794
399164b4
KH
12795 staticpro (&Qoverriding_local_map);
12796 Qoverriding_local_map = intern ("overriding-local-map");
12797
75c43375
RS
12798 staticpro (&Qwindow_scroll_functions);
12799 Qwindow_scroll_functions = intern ("window-scroll-functions");
12800
e0bfbde6
RS
12801 staticpro (&Qredisplay_end_trigger_functions);
12802 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 12803
2e54982e
RS
12804 staticpro (&Qinhibit_point_motion_hooks);
12805 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12806
9499d71b
GM
12807 QCdata = intern (":data");
12808 staticpro (&QCdata);
5f5c8ee5 12809 Qdisplay = intern ("display");
f3751a65 12810 staticpro (&Qdisplay);
5f5c8ee5
GM
12811 Qspace_width = intern ("space-width");
12812 staticpro (&Qspace_width);
5f5c8ee5
GM
12813 Qraise = intern ("raise");
12814 staticpro (&Qraise);
12815 Qspace = intern ("space");
12816 staticpro (&Qspace);
f3751a65
GM
12817 Qmargin = intern ("margin");
12818 staticpro (&Qmargin);
5f5c8ee5 12819 Qleft_margin = intern ("left-margin");
f3751a65 12820 staticpro (&Qleft_margin);
5f5c8ee5 12821 Qright_margin = intern ("right-margin");
f3751a65 12822 staticpro (&Qright_margin);
5f5c8ee5
GM
12823 Qalign_to = intern ("align-to");
12824 staticpro (&Qalign_to);
12825 QCalign_to = intern (":align-to");
12826 staticpro (&QCalign_to);
5f5c8ee5
GM
12827 Qrelative_width = intern ("relative-width");
12828 staticpro (&Qrelative_width);
12829 QCrelative_width = intern (":relative-width");
12830 staticpro (&QCrelative_width);
12831 QCrelative_height = intern (":relative-height");
12832 staticpro (&QCrelative_height);
12833 QCeval = intern (":eval");
12834 staticpro (&QCeval);
d3acf96b 12835 Qwhen = intern ("when");
f3751a65 12836 staticpro (&Qwhen);
886bd6f2
GM
12837 QCfile = intern (":file");
12838 staticpro (&QCfile);
5f5c8ee5
GM
12839 Qfontified = intern ("fontified");
12840 staticpro (&Qfontified);
12841 Qfontification_functions = intern ("fontification-functions");
12842 staticpro (&Qfontification_functions);
5f5c8ee5
GM
12843 Qtrailing_whitespace = intern ("trailing-whitespace");
12844 staticpro (&Qtrailing_whitespace);
12845 Qimage = intern ("image");
12846 staticpro (&Qimage);
12847
a2889657
JB
12848 last_arrow_position = Qnil;
12849 last_arrow_string = Qnil;
f3751a65
GM
12850 staticpro (&last_arrow_position);
12851 staticpro (&last_arrow_string);
c6e89d6c
GM
12852
12853 echo_buffer[0] = echo_buffer[1] = Qnil;
12854 staticpro (&echo_buffer[0]);
12855 staticpro (&echo_buffer[1]);
12856
12857 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12858 staticpro (&echo_area_buffer[0]);
12859 staticpro (&echo_area_buffer[1]);
a2889657 12860
8f897821
GM
12861 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12862 "Non-nil means highlight trailing whitespace.\n\
12863The face used for trailing whitespace is `trailing-whitespace'.");
12864 Vshow_trailing_whitespace = Qnil;
12865
735c094c
KH
12866 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12867 "Non-nil means don't actually do any redisplay.\n\
12868This is used for internal purposes.");
12869 Vinhibit_redisplay = Qnil;
12870
a2889657 12871 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 12872 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
12873 Vglobal_mode_string = Qnil;
12874
12875 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12876 "Marker for where to display an arrow on top of the buffer text.\n\
12877This must be the beginning of a line in order to work.\n\
12878See also `overlay-arrow-string'.");
12879 Voverlay_arrow_position = Qnil;
12880
12881 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12882 "String to display as an arrow. See also `overlay-arrow-position'.");
12883 Voverlay_arrow_string = Qnil;
12884
12885 DEFVAR_INT ("scroll-step", &scroll_step,
12886 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
12887If that fails to bring point back on frame, point is centered instead.\n\
12888If this is zero, point is always centered after it moves off frame.");
a2889657 12889
0789adb2 12890 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
090f7baa
GM
12891 "*Scroll up to this many lines, to bring point back on screen.\n\
12892A value of zero means to scroll the text to center point vertically\n\
12893in the window.");
0789adb2
RS
12894 scroll_conservatively = 0;
12895
9afd2168
RS
12896 DEFVAR_INT ("scroll-margin", &scroll_margin,
12897 "*Number of lines of margin at the top and bottom of a window.\n\
12898Recenter the window whenever point gets within this many lines\n\
12899of the top or bottom of the window.");
12900 scroll_margin = 0;
12901
5f5c8ee5 12902#if GLYPH_DEBUG
a2889657 12903 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 12904#endif
a2889657
JB
12905
12906 DEFVAR_BOOL ("truncate-partial-width-windows",
12907 &truncate_partial_width_windows,
44fa5b1e 12908 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
12909 truncate_partial_width_windows = 1;
12910
12911 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12912 "*Non-nil means use inverse video for the mode line.");
12913 mode_line_inverse_video = 1;
aa6d10fa
RS
12914
12915 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
5f5c8ee5 12916 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 12917If the buffer is bigger than this, the line number does not appear\n\
9f027393 12918in the mode line.");
aa6d10fa 12919 line_number_display_limit = 1000000;
fba9ce76 12920
5d121aec
KH
12921 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12922 "*Maximum line width (in characters) for line number display.\n\
12923If the average length of the lines near point is bigger than this, then the\n\
12924line number may be omitted from the mode line.");
12925 line_number_display_limit_width = 200;
12926
fba9ce76
RS
12927 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12928 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 12929 highlight_nonselected_windows = 0;
d39b6696
KH
12930
12931 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
12932 "Non-nil if more than one frame is visible on this display.\n\
12933Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
12934This variable is not guaranteed to be accurate except while processing\n\
12935`frame-title-format' and `icon-title-format'.");
d39b6696
KH
12936
12937 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 12938 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
12939\(Assuming the window manager supports this feature.)\n\
12940This variable has the same structure as `mode-line-format' (which see),\n\
12941and is used only on frames for which no explicit name has been set\n\
12942\(see `modify-frame-parameters').");
12943 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 12944 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
12945\(Assuming the window manager supports this feature.)\n\
12946This variable has the same structure as `mode-line-format' (which see),\n\
12947and is used only on frames for which no explicit name has been set\n\
12948\(see `modify-frame-parameters').");
12949 Vicon_title_format
12950 = Vframe_title_format
12951 = Fcons (intern ("multiple-frames"),
12952 Fcons (build_string ("%b"),
12953 Fcons (Fcons (build_string (""),
12954 Fcons (intern ("invocation-name"),
12955 Fcons (build_string ("@"),
12956 Fcons (intern ("system-name"),
12957 Qnil)))),
12958 Qnil)));
5992c4f7
KH
12959
12960 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12961 "Maximum number of lines to keep in the message log buffer.\n\
12962If nil, disable message logging. If t, log messages but don't truncate\n\
12963the buffer when it becomes large.");
12964 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
12965
12966 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12967 "Functions called before redisplay, if window sizes have changed.\n\
12968The value should be a list of functions that take one argument.\n\
12969Just before redisplay, for each frame, if any of its windows have changed\n\
12970size since the last redisplay, or have been split or deleted,\n\
12971all the functions in the list are called, with the frame as argument.");
12972 Vwindow_size_change_functions = Qnil;
75c43375
RS
12973
12974 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 12975 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 12976Each function is called with two arguments, the window\n\
8d9583b0
RS
12977and its new display-start position. Note that the value of `window-end'\n\
12978is not valid when these functions are called.");
75c43375 12979 Vwindow_scroll_functions = Qnil;
5f5c8ee5 12980
e037b9ec
GM
12981 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
12982 "*Non-nil means automatically resize tool-bars.\n\
12983This increases a tool-bar's height if not all tool-bar items are visible.\n\
12984It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 12985otherwise.");
e037b9ec 12986 auto_resize_tool_bars_p = 1;
5f5c8ee5 12987
e037b9ec
GM
12988 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
12989 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
12990 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 12991
e037b9ec
GM
12992 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
12993 "*Margin around tool-bar buttons in pixels.");
12994 tool_bar_button_margin = 1;
5f5c8ee5 12995
e037b9ec
GM
12996 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
12997 "Relief thickness of tool-bar buttons.");
12998 tool_bar_button_relief = 3;
5f5c8ee5
GM
12999
13000 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13001 "List of functions to call to fontify regions of text.\n\
13002Each function is called with one argument POS. Functions must\n\
13003fontify a region starting at POS in the current buffer, and give\n\
13004fontified regions the property `fontified'.\n\
13005This variable automatically becomes buffer-local when set.");
13006 Vfontification_functions = Qnil;
13007 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
13008
13009 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
13010 &unibyte_display_via_language_environment,
13011 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
13012Specifically this means that unibyte non-ASCII characters\n\
13013are displayed by converting them to the equivalent multibyte characters\n\
13014according to the current language environment. As a result, they are\n\
13015displayed according to the current fontset.");
13016 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
13017
13018 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13019 "*Maximum height for resizing mini-windows.\n\
13020If a float, it specifies a fraction of the mini-window frame's height.\n\
97cafc0f
GM
13021If an integer, it specifies a number of lines.\n\
13022If nil, don't resize.");
c6e89d6c 13023 Vmax_mini_window_height = make_float (0.25);
d6d26ed3
GM
13024
13025 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13026 &cursor_in_non_selected_windows,
13027 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13028Nil means don't display a cursor there.");
13029 cursor_in_non_selected_windows = 1;
a2889657
JB
13030}
13031
5f5c8ee5
GM
13032
13033/* Initialize this module when Emacs starts. */
13034
dfcf069d 13035void
a2889657
JB
13036init_xdisp ()
13037{
13038 Lisp_Object root_window;
5f5c8ee5 13039 struct window *mini_w;
a2889657 13040
5f5c8ee5 13041 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
13042
13043 mini_w = XWINDOW (minibuf_window);
11e82b76 13044 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 13045
a2889657
JB
13046 if (!noninteractive)
13047 {
5f5c8ee5
GM
13048 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13049 int i;
13050
13051 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 13052 set_window_height (root_window,
5f5c8ee5 13053 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 13054 0);
c2213350 13055 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
13056 set_window_height (minibuf_window, 1, 0);
13057
c2213350
KH
13058 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13059 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
13060
13061 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13062 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13063 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13064
13065 /* The default ellipsis glyphs `...'. */
13066 for (i = 0; i < 3; ++i)
13067 XSETFASTINT (default_invis_vector[i], '.');
a2889657 13068 }
5f5c8ee5
GM
13069
13070#ifdef HAVE_WINDOW_SYSTEM
13071 {
13072 /* Allocate the buffer for frame titles. */
13073 int size = 100;
13074 frame_title_buf = (char *) xmalloc (size);
13075 frame_title_buf_end = frame_title_buf + size;
13076 frame_title_ptr = NULL;
13077 }
13078#endif /* HAVE_WINDOW_SYSTEM */
a2889657 13079}
5f5c8ee5
GM
13080
13081