*** empty log message ***
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
a0315a63 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
5f5c8ee5 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"
546a4f00 173#include "keyboard.h"
44fa5b1e 174#include "frame.h"
a2889657
JB
175#include "window.h"
176#include "termchar.h"
177#include "dispextern.h"
178#include "buffer.h"
1c9241f5 179#include "charset.h"
a2889657
JB
180#include "indent.h"
181#include "commands.h"
182#include "macros.h"
183#include "disptab.h"
30c566e4 184#include "termhooks.h"
b0a0fbda 185#include "intervals.h"
1c9241f5
KH
186#include "coding.h"
187#include "process.h"
dfcf069d 188#include "region-cache.h"
0ef75e87 189#include "fontset.h"
dfcf069d 190
6d55d620 191#ifdef HAVE_X_WINDOWS
dfcf069d
AS
192#include "xterm.h"
193#endif
a75dfea0
AI
194#ifdef WINDOWSNT
195#include "w32term.h"
196#endif
a2889657 197
5f5c8ee5
GM
198#define min(a, b) ((a) < (b) ? (a) : (b))
199#define max(a, b) ((a) > (b) ? (a) : (b))
200
201#define INFINITY 10000000
202
8f3343d0 203#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
2e621225 204extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 205extern int pending_menu_activation;
76412d64
RS
206#endif
207
a2889657
JB
208extern int interrupt_input;
209extern int command_loop_level;
210
b6436d4e
RS
211extern int minibuffer_auto_raise;
212
c4628384
RS
213extern Lisp_Object Qface;
214
399164b4
KH
215extern Lisp_Object Voverriding_local_map;
216extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 217extern Lisp_Object Qmenu_item;
399164b4 218
d46fb96a 219Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 220Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 221Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 222Lisp_Object Qinhibit_point_motion_hooks;
9499d71b 223Lisp_Object QCeval, Qwhen, QCfile, QCdata;
5f5c8ee5
GM
224Lisp_Object Qfontified;
225
226/* Functions called to fontify regions of text. */
227
228Lisp_Object Vfontification_functions;
229Lisp_Object Qfontification_functions;
230
e037b9ec 231/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
232 over them. */
233
e037b9ec 234int auto_raise_tool_bar_buttons_p;
5f5c8ee5 235
e037b9ec 236/* Margin around tool bar buttons in pixels. */
5f5c8ee5 237
e037b9ec 238int tool_bar_button_margin;
5f5c8ee5 239
e037b9ec 240/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 241
e037b9ec 242int tool_bar_button_relief;
5f5c8ee5 243
e037b9ec 244/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
245 items are visible, and no blank lines remain. */
246
e037b9ec 247int auto_resize_tool_bars_p;
399164b4 248
735c094c
KH
249/* Non-nil means don't actually do any redisplay. */
250
251Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
252
5f5c8ee5
GM
253/* Names of text properties relevant for redisplay. */
254
a7e27ef7
DL
255Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
5f5c8ee5
GM
257
258/* Symbols used in text property values. */
259
260Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 261Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 262Lisp_Object Qmargin;
a7e27ef7 263extern Lisp_Object Qheight;
5f5c8ee5 264
8f897821 265/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 266
8f897821 267Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
268
269/* Name of the face used to highlight trailing whitespace. */
270
271Lisp_Object Qtrailing_whitespace;
272
273/* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
275
276Lisp_Object Qimage;
277
278/* Non-zero means print newline to stdout before next mini-buffer
279 message. */
a2889657
JB
280
281int noninteractive_need_newline;
282
5f5c8ee5 283/* Non-zero means print newline to message log before next message. */
f88eb0b6 284
3c6595e0 285static int message_log_need_newline;
f88eb0b6 286
5f5c8ee5
GM
287\f
288/* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
a2889657 292
5f5c8ee5 293static struct text_pos this_line_start_pos;
a2889657 294
5f5c8ee5
GM
295/* Number of characters past the end of the line above, including the
296 terminating newline. */
297
298static struct text_pos this_line_end_pos;
299
300/* The vertical positions and the height of this line. */
a2889657 301
a2889657 302static int this_line_vpos;
5f5c8ee5
GM
303static int this_line_y;
304static int this_line_pixel_height;
305
306/* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
308
309static int this_line_start_x;
a2889657 310
5f5c8ee5 311/* Buffer that this_line_.* variables are referring to. */
a2889657 312
a2889657
JB
313static struct buffer *this_line_buffer;
314
5f5c8ee5
GM
315/* Nonzero means truncate lines in all windows less wide than the
316 frame. */
a2889657 317
a2889657
JB
318int truncate_partial_width_windows;
319
7bbe686f 320/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 321
7bbe686f 322int unibyte_display_via_language_environment;
5f5c8ee5
GM
323
324/* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
7bbe686f 327
d39b6696
KH
328int multiple_frames;
329
a2889657
JB
330Lisp_Object Vglobal_mode_string;
331
332/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 333
a2889657
JB
334Lisp_Object Voverlay_arrow_position;
335
5f5c8ee5
GM
336/* String to display for the arrow. Only used on terminal frames. */
337
a2889657
JB
338Lisp_Object Voverlay_arrow_string;
339
5f5c8ee5
GM
340/* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
343
d45de95b
RS
344static Lisp_Object last_arrow_position, last_arrow_string;
345
5f5c8ee5
GM
346/* Like mode-line-format, but for the title bar on a visible frame. */
347
d39b6696
KH
348Lisp_Object Vframe_title_format;
349
5f5c8ee5
GM
350/* Like mode-line-format, but for the title bar on an iconified frame. */
351
d39b6696
KH
352Lisp_Object Vicon_title_format;
353
08b610e4
RS
354/* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
5f5c8ee5 357
08b610e4
RS
358static Lisp_Object Vwindow_size_change_functions;
359
cf074754
RS
360Lisp_Object Qmenu_bar_update_hook;
361
a2889657 362/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 363
5f5c8ee5 364static int overlay_arrow_seen;
ca26e1c8 365
fba9ce76 366/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 367
5f5c8ee5
GM
368int highlight_nonselected_windows;
369
370/* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
372
14510fee 373static int scroll_step;
a2889657 374
5f5c8ee5
GM
375/* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
377
0789adb2
RS
378static int scroll_conservatively;
379
5f5c8ee5
GM
380/* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
384
9afd2168
RS
385int scroll_margin;
386
5f5c8ee5
GM
387/* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
a2889657 390
a2889657
JB
391int buffer_shared;
392
5f5c8ee5 393/* Vector containing glyphs for an ellipsis `...'. */
a2889657 394
5f5c8ee5 395static Lisp_Object default_invis_vector[3];
a2889657 396
5f5c8ee5 397/* Nonzero means display mode line highlighted. */
a2889657 398
a2889657
JB
399int mode_line_inverse_video;
400
5f5c8ee5
GM
401/* Prompt to display in front of the mini-buffer contents. */
402
8c5b6a0a 403Lisp_Object minibuf_prompt;
a2889657 404
5f5c8ee5
GM
405/* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
407
a2889657 408int minibuf_prompt_width;
5f5c8ee5
GM
409int minibuf_prompt_pixel_width;
410
5f5c8ee5
GM
411/* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
414
73af359d
RS
415Lisp_Object echo_area_window;
416
c6e89d6c
GM
417/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
421
422Lisp_Object Vmessage_stack;
423
a3788d53
RS
424/* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
5f5c8ee5 426
a3788d53
RS
427int message_enable_multibyte;
428
5f5c8ee5
GM
429/* True if we should redraw the mode lines on the next redisplay. */
430
a2889657
JB
431int update_mode_lines;
432
5f5c8ee5
GM
433/* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
435
a2889657
JB
436int windows_or_buffers_changed;
437
5f5c8ee5
GM
438/* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
440
aa6d10fa
RS
441int line_number_displayed;
442
443/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 444
090703f4 445Lisp_Object Vline_number_display_limit;
5992c4f7 446
5d121aec
KH
447/* line width to consider when repostioning for line number display */
448
449static int line_number_display_limit_width;
450
5f5c8ee5
GM
451/* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
453
5992c4f7 454Lisp_Object Vmessage_log_max;
d45de95b 455
6a94510a
GM
456/* The name of the *Messages* buffer, a string. */
457
458static Lisp_Object Vmessages_buffer_name;
459
c6e89d6c
GM
460/* Current, index 0, and last displayed echo area message. Either
461 buffers from echo_buffers, or nil to indicate no message. */
462
463Lisp_Object echo_area_buffer[2];
464
465/* The buffers referenced from echo_area_buffer. */
466
467static Lisp_Object echo_buffer[2];
468
469/* A vector saved used in with_area_buffer to reduce consing. */
470
471static Lisp_Object Vwith_echo_area_save_vector;
472
473/* Non-zero means display_echo_area should display the last echo area
474 message again. Set by redisplay_preserve_echo_area. */
475
476static int display_last_displayed_message_p;
477
478/* Nonzero if echo area is being used by print; zero if being used by
479 message. */
480
481int message_buf_print;
482
9142dd5b
GM
483/* Maximum height for resizing mini-windows. Either a float
484 specifying a fraction of the available height, or an integer
485 specifying a number of lines. */
c6e89d6c 486
ad4f174e
GM
487Lisp_Object Vmax_mini_window_height;
488
489/* Non-zero means messages should be displayed with truncated
490 lines instead of being continued. */
491
492int message_truncate_lines;
493Lisp_Object Qmessage_truncate_lines;
c6e89d6c 494
d6d26ed3
GM
495/* Non-zero means we want a hollow cursor in windows that are not
496 selected. Zero means there's no cursor in such windows. */
497
498int cursor_in_non_selected_windows;
499
5f5c8ee5
GM
500/* A scratch glyph row with contents used for generating truncation
501 glyphs. Also used in direct_output_for_insert. */
12adba34 502
5f5c8ee5
GM
503#define MAX_SCRATCH_GLYPHS 100
504struct glyph_row scratch_glyph_row;
505static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 506
5f5c8ee5
GM
507/* Ascent and height of the last line processed by move_it_to. */
508
509static int last_max_ascent, last_height;
510
511/* The maximum distance to look ahead for text properties. Values
512 that are too small let us call compute_char_face and similar
513 functions too often which is expensive. Values that are too large
514 let us call compute_char_face and alike too often because we
515 might not be interested in text properties that far away. */
516
517#define TEXT_PROP_DISTANCE_LIMIT 100
518
47589c8c
GM
519#if GLYPH_DEBUG
520
5f5c8ee5
GM
521/* Non-zero means print traces of redisplay if compiled with
522 GLYPH_DEBUG != 0. */
523
5f5c8ee5 524int trace_redisplay_p;
47589c8c 525
546a4f00 526#endif /* GLYPH_DEBUG */
47589c8c 527
546a4f00
AI
528#ifdef DEBUG_TRACE_MOVE
529/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
530int trace_move;
531
47589c8c
GM
532#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
533#else
91c3f500 534#define TRACE_MOVE(x) (void) 0
5f5c8ee5 535#endif
546a4f00 536
d475bcb8
GM
537/* Non-zero means automatically scroll windows horizontally to make
538 point visible. */
539
540int automatic_hscrolling_p;
541
e00daaa0
GM
542/* A list of symbols, one for each supported image type. */
543
544Lisp_Object Vimage_types;
545
5f5c8ee5
GM
546/* Value returned from text property handlers (see below). */
547
548enum prop_handled
3c6595e0 549{
5f5c8ee5
GM
550 HANDLED_NORMALLY,
551 HANDLED_RECOMPUTE_PROPS,
552 HANDLED_OVERLAY_STRING_CONSUMED,
553 HANDLED_RETURN
554};
3c6595e0 555
5f5c8ee5
GM
556/* A description of text properties that redisplay is interested
557 in. */
3c6595e0 558
5f5c8ee5
GM
559struct props
560{
561 /* The name of the property. */
562 Lisp_Object *name;
90adcf20 563
5f5c8ee5
GM
564 /* A unique index for the property. */
565 enum prop_idx idx;
566
567 /* A handler function called to set up iterator IT from the property
568 at IT's current position. Value is used to steer handle_stop. */
569 enum prop_handled (*handler) P_ ((struct it *it));
570};
571
572static enum prop_handled handle_face_prop P_ ((struct it *));
573static enum prop_handled handle_invisible_prop P_ ((struct it *));
574static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 575static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
576static enum prop_handled handle_overlay_change P_ ((struct it *));
577static enum prop_handled handle_fontified_prop P_ ((struct it *));
578
579/* Properties handled by iterators. */
580
581static struct props it_props[] =
5992c4f7 582{
5f5c8ee5
GM
583 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
584 /* Handle `face' before `display' because some sub-properties of
585 `display' need to know the face. */
586 {&Qface, FACE_PROP_IDX, handle_face_prop},
587 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
588 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 589 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
590 {NULL, 0, NULL}
591};
5992c4f7 592
5f5c8ee5
GM
593/* Value is the position described by X. If X is a marker, value is
594 the marker_position of X. Otherwise, value is X. */
12adba34 595
5f5c8ee5 596#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 597
5f5c8ee5 598/* Enumeration returned by some move_it_.* functions internally. */
12adba34 599
5f5c8ee5
GM
600enum move_it_result
601{
602 /* Not used. Undefined value. */
603 MOVE_UNDEFINED,
bab29e15 604
5f5c8ee5
GM
605 /* Move ended at the requested buffer position or ZV. */
606 MOVE_POS_MATCH_OR_ZV,
bab29e15 607
5f5c8ee5
GM
608 /* Move ended at the requested X pixel position. */
609 MOVE_X_REACHED,
12adba34 610
5f5c8ee5
GM
611 /* Move within a line ended at the end of a line that must be
612 continued. */
613 MOVE_LINE_CONTINUED,
614
615 /* Move within a line ended at the end of a line that would
616 be displayed truncated. */
617 MOVE_LINE_TRUNCATED,
ff6c30e5 618
5f5c8ee5
GM
619 /* Move within a line ended at a line end. */
620 MOVE_NEWLINE_OR_CR
621};
12adba34 622
ff6c30e5 623
5f5c8ee5
GM
624\f
625/* Function prototypes. */
626
715e84c9 627static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225
GM
628static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
629static int invisible_text_between_p P_ ((struct it *, int, int));
630static int next_element_from_ellipsis P_ ((struct it *));
631static void pint2str P_ ((char *, int, int));
632static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
633 struct text_pos));
634static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
635static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
636static void store_frame_title_char P_ ((char));
637static int store_frame_title P_ ((unsigned char *, int, int));
638static void x_consider_frame_title P_ ((Lisp_Object));
639static void handle_stop P_ ((struct it *));
640static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 641static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 642static void ensure_echo_area_buffers P_ ((void));
e037b9ec
GM
643static struct glyph_row *row_containing_pos P_ ((struct window *, int,
644 struct glyph_row *,
645 struct glyph_row *));
c6e89d6c
GM
646static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
647static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 648static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
649 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
650 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 651static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
652static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
653static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
654static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 655static int display_echo_area P_ ((struct window *));
23dd2d97
KR
656static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
657static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 658static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 659static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
660static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
661 struct text_pos));
662static int compute_window_start_on_continuation_line P_ ((struct window *));
663static Lisp_Object eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
664static void insert_left_trunc_glyphs P_ ((struct it *));
665static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
666static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 667static int append_space P_ ((struct it *, int));
5f5c8ee5
GM
668static void make_cursor_line_fully_visible P_ ((struct window *));
669static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
47589c8c 670static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
671static int trailing_whitespace_p P_ ((int));
672static int message_log_check_duplicate P_ ((int, int, int, int));
673int invisible_p P_ ((Lisp_Object, Lisp_Object));
674int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
675static void push_it P_ ((struct it *));
676static void pop_it P_ ((struct it *));
677static void sync_frame_with_window_matrix_rows P_ ((struct window *));
678static void redisplay_internal P_ ((int));
c6e89d6c 679static int echo_area_display P_ ((int));
5f5c8ee5
GM
680static void redisplay_windows P_ ((Lisp_Object));
681static void redisplay_window P_ ((Lisp_Object, int));
682static void update_menu_bar P_ ((struct frame *, int));
683static int try_window_reusing_current_matrix P_ ((struct window *));
684static int try_window_id P_ ((struct window *));
685static int display_line P_ ((struct it *));
715e84c9 686static int display_mode_lines P_ ((struct window *));
5f5c8ee5
GM
687static void display_mode_line P_ ((struct window *, enum face_id,
688 Lisp_Object));
689static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 690static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
691static void display_menu_bar P_ ((struct window *));
692static int display_count_lines P_ ((int, int, int, int, int *));
693static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
694 int, int, struct it *, int, int, int, int));
695static void compute_line_metrics P_ ((struct it *));
696static void run_redisplay_end_trigger_hook P_ ((struct it *));
697static int get_overlay_strings P_ ((struct it *));
698static void next_overlay_string P_ ((struct it *));
699void set_iterator_to_next P_ ((struct it *));
700static void reseat P_ ((struct it *, struct text_pos, int));
701static void reseat_1 P_ ((struct it *, struct text_pos, int));
702static void back_to_previous_visible_line_start P_ ((struct it *));
703static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 704static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
705static int next_element_from_display_vector P_ ((struct it *));
706static int next_element_from_string P_ ((struct it *));
707static int next_element_from_c_string P_ ((struct it *));
708static int next_element_from_buffer P_ ((struct it *));
260a86a0 709static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
710static int next_element_from_image P_ ((struct it *));
711static int next_element_from_stretch P_ ((struct it *));
712static void load_overlay_strings P_ ((struct it *));
713static void init_from_display_pos P_ ((struct it *, struct window *,
714 struct display_pos *));
715static void reseat_to_string P_ ((struct it *, unsigned char *,
716 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
717static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
718 int, int, int));
719void move_it_vertically_backward P_ ((struct it *, int));
720static void init_to_row_start P_ ((struct it *, struct window *,
721 struct glyph_row *));
722static void init_to_row_end P_ ((struct it *, struct window *,
723 struct glyph_row *));
724static void back_to_previous_line_start P_ ((struct it *));
725static void forward_to_next_line_start P_ ((struct it *));
726static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
727 Lisp_Object, int));
728static struct text_pos string_pos P_ ((int, Lisp_Object));
729static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
730static int number_of_chars P_ ((unsigned char *, int));
731static void compute_stop_pos P_ ((struct it *));
732static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
733 Lisp_Object));
734static int face_before_or_after_it_pos P_ ((struct it *, int));
735static int next_overlay_change P_ ((int));
736static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
737 Lisp_Object, struct text_pos *));
738
739#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
740#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 741
5f5c8ee5 742#ifdef HAVE_WINDOW_SYSTEM
12adba34 743
e037b9ec
GM
744static void update_tool_bar P_ ((struct frame *, int));
745static void build_desired_tool_bar_string P_ ((struct frame *f));
746static int redisplay_tool_bar P_ ((struct frame *));
747static void display_tool_bar_line P_ ((struct it *));
12adba34 748
5f5c8ee5 749#endif /* HAVE_WINDOW_SYSTEM */
12adba34 750
5f5c8ee5
GM
751\f
752/***********************************************************************
753 Window display dimensions
754 ***********************************************************************/
12adba34 755
5f5c8ee5
GM
756/* Return the window-relative maximum y + 1 for glyph rows displaying
757 text in window W. This is the height of W minus the height of a
758 mode line, if any. */
759
760INLINE int
761window_text_bottom_y (w)
762 struct window *w;
763{
764 struct frame *f = XFRAME (w->frame);
765 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
766
767 if (WINDOW_WANTS_MODELINE_P (w))
768 height -= CURRENT_MODE_LINE_HEIGHT (w);
769 return height;
f88eb0b6
KH
770}
771
f82aff7c 772
5f5c8ee5
GM
773/* Return the pixel width of display area AREA of window W. AREA < 0
774 means return the total width of W, not including bitmap areas to
775 the left and right of the window. */
ff6c30e5 776
5f5c8ee5
GM
777INLINE int
778window_box_width (w, area)
779 struct window *w;
780 int area;
781{
782 struct frame *f = XFRAME (w->frame);
783 int width = XFASTINT (w->width);
784
785 if (!w->pseudo_window_p)
ff6c30e5 786 {
050d82d7 787 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
788
789 if (area == TEXT_AREA)
790 {
791 if (INTEGERP (w->left_margin_width))
792 width -= XFASTINT (w->left_margin_width);
793 if (INTEGERP (w->right_margin_width))
794 width -= XFASTINT (w->right_margin_width);
795 }
796 else if (area == LEFT_MARGIN_AREA)
797 width = (INTEGERP (w->left_margin_width)
798 ? XFASTINT (w->left_margin_width) : 0);
799 else if (area == RIGHT_MARGIN_AREA)
800 width = (INTEGERP (w->right_margin_width)
801 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 802 }
5f5c8ee5
GM
803
804 return width * CANON_X_UNIT (f);
ff6c30e5 805}
1adc55de 806
1adc55de 807
5f5c8ee5
GM
808/* Return the pixel height of the display area of window W, not
809 including mode lines of W, if any.. */
f88eb0b6 810
5f5c8ee5
GM
811INLINE int
812window_box_height (w)
813 struct window *w;
f88eb0b6 814{
5f5c8ee5
GM
815 struct frame *f = XFRAME (w->frame);
816 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
817
818 if (WINDOW_WANTS_MODELINE_P (w))
819 height -= CURRENT_MODE_LINE_HEIGHT (w);
820
045dee35
GM
821 if (WINDOW_WANTS_HEADER_LINE_P (w))
822 height -= CURRENT_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
823
824 return height;
5992c4f7
KH
825}
826
827
5f5c8ee5
GM
828/* Return the frame-relative coordinate of the left edge of display
829 area AREA of window W. AREA < 0 means return the left edge of the
830 whole window, to the right of any bitmap area at the left side of
831 W. */
5992c4f7 832
5f5c8ee5
GM
833INLINE int
834window_box_left (w, area)
835 struct window *w;
836 int area;
90adcf20 837{
5f5c8ee5
GM
838 struct frame *f = XFRAME (w->frame);
839 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 840
5f5c8ee5 841 if (!w->pseudo_window_p)
90adcf20 842 {
5f5c8ee5 843 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 844 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
845
846 if (area == TEXT_AREA)
847 x += window_box_width (w, LEFT_MARGIN_AREA);
848 else if (area == RIGHT_MARGIN_AREA)
849 x += (window_box_width (w, LEFT_MARGIN_AREA)
850 + window_box_width (w, TEXT_AREA));
90adcf20 851 }
73af359d 852
5f5c8ee5
GM
853 return x;
854}
90adcf20 855
b6436d4e 856
5f5c8ee5
GM
857/* Return the frame-relative coordinate of the right edge of display
858 area AREA of window W. AREA < 0 means return the left edge of the
859 whole window, to the left of any bitmap area at the right side of
860 W. */
ded34426 861
5f5c8ee5
GM
862INLINE int
863window_box_right (w, area)
864 struct window *w;
865 int area;
866{
867 return window_box_left (w, area) + window_box_width (w, area);
868}
869
870
871/* Get the bounding box of the display area AREA of window W, without
872 mode lines, in frame-relative coordinates. AREA < 0 means the
873 whole window, not including bitmap areas to the left and right of
874 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
875 coordinates of the upper-left corner of the box. Return in
876 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
877
878INLINE void
879window_box (w, area, box_x, box_y, box_width, box_height)
880 struct window *w;
881 int area;
882 int *box_x, *box_y, *box_width, *box_height;
883{
884 struct frame *f = XFRAME (w->frame);
885
886 *box_width = window_box_width (w, area);
887 *box_height = window_box_height (w);
888 *box_x = window_box_left (w, area);
889 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
890 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
891 if (WINDOW_WANTS_HEADER_LINE_P (w))
892 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 893}
1adc55de 894
1adc55de 895
5f5c8ee5
GM
896/* Get the bounding box of the display area AREA of window W, without
897 mode lines. AREA < 0 means the whole window, not including bitmap
898 areas to the left and right of the window. Return in *TOP_LEFT_X
899 and TOP_LEFT_Y the frame-relative pixel coordinates of the
900 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
901 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
902 box. */
ded34426 903
5f5c8ee5
GM
904INLINE void
905window_box_edges (w, area, top_left_x, top_left_y,
906 bottom_right_x, bottom_right_y)
907 struct window *w;
908 int area;
909 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 910{
5f5c8ee5
GM
911 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
912 bottom_right_y);
913 *bottom_right_x += *top_left_x;
914 *bottom_right_y += *top_left_y;
48ae5f0a
KH
915}
916
5f5c8ee5
GM
917
918\f
919/***********************************************************************
920 Utilities
921 ***********************************************************************/
922
4fdb80f2
GM
923/* Return the next character from STR which is MAXLEN bytes long.
924 Return in *LEN the length of the character. This is like
925 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 926 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
927 character. */
928
929static INLINE int
7a5b8a93 930string_char_and_length (str, maxlen, len)
4fdb80f2 931 unsigned char *str;
7a5b8a93 932 int maxlen, *len;
4fdb80f2
GM
933{
934 int c;
935
936 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
937 if (!CHAR_VALID_P (c, 1))
938 /* We may not change the length here because other places in Emacs
9ab8560d 939 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
940 characters. */
941 c = '?';
942
943 return c;
944}
945
946
947
5f5c8ee5
GM
948/* Given a position POS containing a valid character and byte position
949 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
950
951static struct text_pos
952string_pos_nchars_ahead (pos, string, nchars)
953 struct text_pos pos;
954 Lisp_Object string;
955 int nchars;
0b1005ef 956{
5f5c8ee5
GM
957 xassert (STRINGP (string) && nchars >= 0);
958
959 if (STRING_MULTIBYTE (string))
960 {
961 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
962 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
963 int len;
964
965 while (nchars--)
966 {
4fdb80f2 967 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
968 p += len, rest -= len;
969 xassert (rest >= 0);
970 CHARPOS (pos) += 1;
971 BYTEPOS (pos) += len;
972 }
973 }
974 else
975 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
976
977 return pos;
0a9dc68b
RS
978}
979
0a9dc68b 980
5f5c8ee5
GM
981/* Value is the text position, i.e. character and byte position,
982 for character position CHARPOS in STRING. */
983
984static INLINE struct text_pos
985string_pos (charpos, string)
986 int charpos;
0a9dc68b 987 Lisp_Object string;
0a9dc68b 988{
5f5c8ee5
GM
989 struct text_pos pos;
990 xassert (STRINGP (string));
991 xassert (charpos >= 0);
992 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
993 return pos;
994}
995
996
997/* Value is a text position, i.e. character and byte position, for
998 character position CHARPOS in C string S. MULTIBYTE_P non-zero
999 means recognize multibyte characters. */
1000
1001static struct text_pos
1002c_string_pos (charpos, s, multibyte_p)
1003 int charpos;
1004 unsigned char *s;
1005 int multibyte_p;
1006{
1007 struct text_pos pos;
1008
1009 xassert (s != NULL);
1010 xassert (charpos >= 0);
1011
1012 if (multibyte_p)
0a9dc68b 1013 {
5f5c8ee5
GM
1014 int rest = strlen (s), len;
1015
1016 SET_TEXT_POS (pos, 0, 0);
1017 while (charpos--)
0a9dc68b 1018 {
4fdb80f2 1019 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1020 s += len, rest -= len;
1021 xassert (rest >= 0);
1022 CHARPOS (pos) += 1;
1023 BYTEPOS (pos) += len;
0a9dc68b
RS
1024 }
1025 }
5f5c8ee5
GM
1026 else
1027 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1028
5f5c8ee5
GM
1029 return pos;
1030}
0a9dc68b 1031
0a9dc68b 1032
5f5c8ee5
GM
1033/* Value is the number of characters in C string S. MULTIBYTE_P
1034 non-zero means recognize multibyte characters. */
0a9dc68b 1035
5f5c8ee5
GM
1036static int
1037number_of_chars (s, multibyte_p)
1038 unsigned char *s;
1039 int multibyte_p;
1040{
1041 int nchars;
1042
1043 if (multibyte_p)
1044 {
1045 int rest = strlen (s), len;
1046 unsigned char *p = (unsigned char *) s;
0a9dc68b 1047
5f5c8ee5
GM
1048 for (nchars = 0; rest > 0; ++nchars)
1049 {
4fdb80f2 1050 string_char_and_length (p, rest, &len);
5f5c8ee5 1051 rest -= len, p += len;
0a9dc68b
RS
1052 }
1053 }
5f5c8ee5
GM
1054 else
1055 nchars = strlen (s);
1056
1057 return nchars;
0b1005ef
KH
1058}
1059
5f5c8ee5
GM
1060
1061/* Compute byte position NEWPOS->bytepos corresponding to
1062 NEWPOS->charpos. POS is a known position in string STRING.
1063 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1064
5f5c8ee5
GM
1065static void
1066compute_string_pos (newpos, pos, string)
1067 struct text_pos *newpos, pos;
1068 Lisp_Object string;
76412d64 1069{
5f5c8ee5
GM
1070 xassert (STRINGP (string));
1071 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1072
1073 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1074 *newpos = string_pos_nchars_ahead (pos, string,
1075 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1076 else
1077 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1078}
1079
9c74a0dd 1080
5f5c8ee5
GM
1081\f
1082/***********************************************************************
1083 Lisp form evaluation
1084 ***********************************************************************/
1085
0d8b31c0 1086/* Error handler for eval_form and call_function. */
5f5c8ee5
GM
1087
1088static Lisp_Object
1089eval_handler (arg)
1090 Lisp_Object arg;
1091{
1092 return Qnil;
1093}
1094
1095
1096/* Evaluate SEXPR and return the result, or nil if something went
1097 wrong. */
1098
71e5b1b8 1099Lisp_Object
5f5c8ee5
GM
1100eval_form (sexpr)
1101 Lisp_Object sexpr;
1102{
1103 int count = specpdl_ptr - specpdl;
0d8b31c0 1104 struct gcpro gcpro1;
5f5c8ee5 1105 Lisp_Object val;
0d8b31c0
GM
1106
1107 GCPRO1 (sexpr);
5f5c8ee5
GM
1108 specbind (Qinhibit_redisplay, Qt);
1109 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
0d8b31c0
GM
1110 UNGCPRO;
1111 return unbind_to (count, val);
1112}
1113
1114
1115/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1116 Return the result, or nil if something went wrong. */
1117
1118Lisp_Object
1119call_function (nargs, args)
1120 int nargs;
1121 Lisp_Object *args;
1122{
1123 int count = specpdl_ptr - specpdl;
1124 Lisp_Object val;
1125 struct gcpro gcpro1;
1126
1127 GCPRO1 (args[0]);
1128 gcpro1.nvars = nargs;
1129 specbind (Qinhibit_redisplay, Qt);
1130 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1131 eval_handler);
1132 UNGCPRO;
5f5c8ee5
GM
1133 return unbind_to (count, val);
1134}
1135
1136
1137\f
1138/***********************************************************************
1139 Debugging
1140 ***********************************************************************/
1141
1142#if 0
1143
1144/* Define CHECK_IT to perform sanity checks on iterators.
1145 This is for debugging. It is too slow to do unconditionally. */
1146
1147static void
1148check_it (it)
1149 struct it *it;
1150{
1151 if (it->method == next_element_from_string)
a2889657 1152 {
5f5c8ee5
GM
1153 xassert (STRINGP (it->string));
1154 xassert (IT_STRING_CHARPOS (*it) >= 0);
1155 }
1156 else if (it->method == next_element_from_buffer)
1157 {
1158 /* Check that character and byte positions agree. */
1159 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1160 }
73af359d 1161
5f5c8ee5
GM
1162 if (it->dpvec)
1163 xassert (it->current.dpvec_index >= 0);
1164 else
1165 xassert (it->current.dpvec_index < 0);
1166}
1f40cad2 1167
5f5c8ee5
GM
1168#define CHECK_IT(IT) check_it ((IT))
1169
1170#else /* not 0 */
1171
1172#define CHECK_IT(IT) (void) 0
1173
1174#endif /* not 0 */
1175
1176
1177#if GLYPH_DEBUG
1178
1179/* Check that the window end of window W is what we expect it
1180 to be---the last row in the current matrix displaying text. */
1181
1182static void
1183check_window_end (w)
1184 struct window *w;
1185{
1186 if (!MINI_WINDOW_P (w)
1187 && !NILP (w->window_end_valid))
1188 {
1189 struct glyph_row *row;
1190 xassert ((row = MATRIX_ROW (w->current_matrix,
1191 XFASTINT (w->window_end_vpos)),
1192 !row->enabled_p
1193 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1194 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1195 }
1196}
1197
1198#define CHECK_WINDOW_END(W) check_window_end ((W))
1199
1200#else /* not GLYPH_DEBUG */
1201
1202#define CHECK_WINDOW_END(W) (void) 0
1203
1204#endif /* not GLYPH_DEBUG */
1205
1206
1207\f
1208/***********************************************************************
1209 Iterator initialization
1210 ***********************************************************************/
1211
1212/* Initialize IT for displaying current_buffer in window W, starting
1213 at character position CHARPOS. CHARPOS < 0 means that no buffer
1214 position is specified which is useful when the iterator is assigned
1215 a position later. BYTEPOS is the byte position corresponding to
1216 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1217
1218 If ROW is not null, calls to produce_glyphs with IT as parameter
1219 will produce glyphs in that row.
1220
1221 BASE_FACE_ID is the id of a base face to use. It must be one of
1222 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
045dee35 1223 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1224 displaying the tool-bar.
5f5c8ee5
GM
1225
1226 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1227 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
1228 corresponding mode line glyph row of the desired matrix of W. */
1229
1230void
1231init_iterator (it, w, charpos, bytepos, row, base_face_id)
1232 struct it *it;
1233 struct window *w;
1234 int charpos, bytepos;
1235 struct glyph_row *row;
1236 enum face_id base_face_id;
1237{
1238 int highlight_region_p;
5f5c8ee5
GM
1239
1240 /* Some precondition checks. */
1241 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1242 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1243
1244 /* If face attributes have been changed since the last redisplay,
1245 free realized faces now because they depend on face definitions
1246 that might have changed. */
1247 if (face_change_count)
1248 {
1249 face_change_count = 0;
1250 free_all_realized_faces (Qnil);
1251 }
1252
1253 /* Use one of the mode line rows of W's desired matrix if
1254 appropriate. */
1255 if (row == NULL)
1256 {
1257 if (base_face_id == MODE_LINE_FACE_ID)
1258 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1259 else if (base_face_id == HEADER_LINE_FACE_ID)
1260 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1261 }
1262
1263 /* Clear IT. */
1264 bzero (it, sizeof *it);
1265 it->current.overlay_string_index = -1;
1266 it->current.dpvec_index = -1;
5f5c8ee5
GM
1267 it->base_face_id = base_face_id;
1268
1269 /* The window in which we iterate over current_buffer: */
1270 XSETWINDOW (it->window, w);
1271 it->w = w;
1272 it->f = XFRAME (w->frame);
1273
d475bcb8
GM
1274 /* Extra space between lines (on window systems only). */
1275 if (base_face_id == DEFAULT_FACE_ID
1276 && FRAME_WINDOW_P (it->f))
1277 {
1278 if (NATNUMP (current_buffer->extra_line_spacing))
1279 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1280 else if (it->f->extra_line_spacing > 0)
1281 it->extra_line_spacing = it->f->extra_line_spacing;
1282 }
1283
5f5c8ee5
GM
1284 /* If realized faces have been removed, e.g. because of face
1285 attribute changes of named faces, recompute them. */
1286 if (FRAME_FACE_CACHE (it->f)->used == 0)
1287 recompute_basic_faces (it->f);
1288
5f5c8ee5
GM
1289 /* Current value of the `space-width', and 'height' properties. */
1290 it->space_width = Qnil;
1291 it->font_height = Qnil;
1292
1293 /* Are control characters displayed as `^C'? */
1294 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1295
1296 /* -1 means everything between a CR and the following line end
1297 is invisible. >0 means lines indented more than this value are
1298 invisible. */
1299 it->selective = (INTEGERP (current_buffer->selective_display)
1300 ? XFASTINT (current_buffer->selective_display)
1301 : (!NILP (current_buffer->selective_display)
1302 ? -1 : 0));
1303 it->selective_display_ellipsis_p
1304 = !NILP (current_buffer->selective_display_ellipses);
1305
1306 /* Display table to use. */
1307 it->dp = window_display_table (w);
1308
1309 /* Are multibyte characters enabled in current_buffer? */
1310 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1311
1312 /* Non-zero if we should highlight the region. */
1313 highlight_region_p
1314 = (!NILP (Vtransient_mark_mode)
1315 && !NILP (current_buffer->mark_active)
1316 && XMARKER (current_buffer->mark)->buffer != 0);
1317
1318 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1319 start and end of a visible region in window IT->w. Set both to
1320 -1 to indicate no region. */
1321 if (highlight_region_p
1322 /* Maybe highlight only in selected window. */
1323 && (/* Either show region everywhere. */
1324 highlight_nonselected_windows
1325 /* Or show region in the selected window. */
1326 || w == XWINDOW (selected_window)
1327 /* Or show the region if we are in the mini-buffer and W is
1328 the window the mini-buffer refers to. */
1329 || (MINI_WINDOW_P (XWINDOW (selected_window))
1330 && w == XWINDOW (Vminibuf_scroll_window))))
1331 {
1332 int charpos = marker_position (current_buffer->mark);
1333 it->region_beg_charpos = min (PT, charpos);
1334 it->region_end_charpos = max (PT, charpos);
1335 }
1336 else
1337 it->region_beg_charpos = it->region_end_charpos = -1;
1338
1339 /* Get the position at which the redisplay_end_trigger hook should
1340 be run, if it is to be run at all. */
1341 if (MARKERP (w->redisplay_end_trigger)
1342 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1343 it->redisplay_end_trigger_charpos
1344 = marker_position (w->redisplay_end_trigger);
1345 else if (INTEGERP (w->redisplay_end_trigger))
1346 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1347
1348 /* Correct bogus values of tab_width. */
1349 it->tab_width = XINT (current_buffer->tab_width);
1350 if (it->tab_width <= 0 || it->tab_width > 1000)
1351 it->tab_width = 8;
1352
1353 /* Are lines in the display truncated? */
1354 it->truncate_lines_p
1355 = (base_face_id != DEFAULT_FACE_ID
1356 || XINT (it->w->hscroll)
1357 || (truncate_partial_width_windows
1358 && !WINDOW_FULL_WIDTH_P (it->w))
1359 || !NILP (current_buffer->truncate_lines));
1360
1361 /* Get dimensions of truncation and continuation glyphs. These are
1362 displayed as bitmaps under X, so we don't need them for such
1363 frames. */
1364 if (!FRAME_WINDOW_P (it->f))
1365 {
1366 if (it->truncate_lines_p)
1367 {
1368 /* We will need the truncation glyph. */
1369 xassert (it->glyph_row == NULL);
1370 produce_special_glyphs (it, IT_TRUNCATION);
1371 it->truncation_pixel_width = it->pixel_width;
1372 }
1373 else
1374 {
1375 /* We will need the continuation glyph. */
1376 xassert (it->glyph_row == NULL);
1377 produce_special_glyphs (it, IT_CONTINUATION);
1378 it->continuation_pixel_width = it->pixel_width;
1379 }
1380
1381 /* Reset these values to zero becaue the produce_special_glyphs
1382 above has changed them. */
1383 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1384 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1385 }
1386
1387 /* Set this after getting the dimensions of truncation and
1388 continuation glyphs, so that we don't produce glyphs when calling
1389 produce_special_glyphs, above. */
1390 it->glyph_row = row;
1391 it->area = TEXT_AREA;
1392
1393 /* Get the dimensions of the display area. The display area
1394 consists of the visible window area plus a horizontally scrolled
1395 part to the left of the window. All x-values are relative to the
1396 start of this total display area. */
1397 if (base_face_id != DEFAULT_FACE_ID)
1398 {
1399 /* Mode lines, menu bar in terminal frames. */
1400 it->first_visible_x = 0;
1401 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1402 }
1403 else
1404 {
1405 it->first_visible_x
1406 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1407 it->last_visible_x = (it->first_visible_x
1408 + window_box_width (w, TEXT_AREA));
1409
1410 /* If we truncate lines, leave room for the truncator glyph(s) at
1411 the right margin. Otherwise, leave room for the continuation
1412 glyph(s). Truncation and continuation glyphs are not inserted
1413 for window-based redisplay. */
1414 if (!FRAME_WINDOW_P (it->f))
1415 {
1416 if (it->truncate_lines_p)
1417 it->last_visible_x -= it->truncation_pixel_width;
1418 else
1419 it->last_visible_x -= it->continuation_pixel_width;
1420 }
1421
045dee35
GM
1422 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1423 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1424 }
1425
1426 /* Leave room for a border glyph. */
1427 if (!FRAME_WINDOW_P (it->f)
1428 && !WINDOW_RIGHTMOST_P (it->w))
1429 it->last_visible_x -= 1;
1430
1431 it->last_visible_y = window_text_bottom_y (w);
1432
1433 /* For mode lines and alike, arrange for the first glyph having a
1434 left box line if the face specifies a box. */
1435 if (base_face_id != DEFAULT_FACE_ID)
1436 {
1437 struct face *face;
1438
1439 it->face_id = base_face_id;
1440
1441 /* If we have a boxed mode line, make the first character appear
1442 with a left box line. */
1443 face = FACE_FROM_ID (it->f, base_face_id);
1444 if (face->box != FACE_NO_BOX)
1445 it->start_of_box_run_p = 1;
1446 }
1447
1448 /* If a buffer position was specified, set the iterator there,
1449 getting overlays and face properties from that position. */
1450 if (charpos > 0)
1451 {
1452 it->end_charpos = ZV;
1453 it->face_id = -1;
1454 IT_CHARPOS (*it) = charpos;
1455
1456 /* Compute byte position if not specified. */
1457 if (bytepos <= 0)
1458 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1459 else
1460 IT_BYTEPOS (*it) = bytepos;
1461
1462 /* Compute faces etc. */
1463 reseat (it, it->current.pos, 1);
1464 }
1465
1466 CHECK_IT (it);
1467}
1468
1469
1470/* Initialize IT for the display of window W with window start POS. */
1471
1472void
1473start_display (it, w, pos)
1474 struct it *it;
1475 struct window *w;
1476 struct text_pos pos;
1477{
1478 int start_at_line_beg_p;
1479 struct glyph_row *row;
045dee35 1480 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1481 int first_y;
1482
1483 row = w->desired_matrix->rows + first_vpos;
1484 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1485 first_y = it->current_y;
1486
1487 /* If window start is not at a line start, move back to the line
1488 start. This makes sure that we take continuation lines into
1489 account. */
1490 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1491 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1492 if (!start_at_line_beg_p)
1493 reseat_at_previous_visible_line_start (it);
1494
5f5c8ee5
GM
1495 /* If window start is not at a line start, skip forward to POS to
1496 get the correct continuation_lines_width and current_x. */
1497 if (!start_at_line_beg_p)
1498 {
1499 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1500
1501 /* If lines are continued, this line may end in the middle of a
1502 multi-glyph character (e.g. a control character displayed as
1503 \003, or in the middle of an overlay string). In this case
1504 move_it_to above will not have taken us to the start of
1505 the continuation line but to the end of the continued line. */
b28cb6ed 1506 if (!it->truncate_lines_p)
5f5c8ee5 1507 {
b28cb6ed 1508 if (it->current_x > 0)
5f5c8ee5 1509 {
b28cb6ed
GM
1510 if (it->current.dpvec_index >= 0
1511 || it->current.overlay_string_index >= 0)
1512 {
1513 set_iterator_to_next (it);
1514 move_it_in_display_line_to (it, -1, -1, 0);
1515 }
1516
1517 it->continuation_lines_width += it->current_x;
5f5c8ee5 1518 }
b28cb6ed
GM
1519
1520 /* We're starting a new display line, not affected by the
1521 height of the continued line, so clear the appropriate
1522 fields in the iterator structure. */
1523 it->max_ascent = it->max_descent = 0;
1524 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5
GM
1525 }
1526
1527 it->current_y = first_y;
1528 it->vpos = 0;
1529 it->current_x = it->hpos = 0;
1530 }
1531
1532#if 0 /* Don't assert the following because start_display is sometimes
1533 called intentionally with a window start that is not at a
1534 line start. Please leave this code in as a comment. */
1535
1536 /* Window start should be on a line start, now. */
1537 xassert (it->continuation_lines_width
1538 || IT_CHARPOS (it) == BEGV
1539 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1540#endif /* 0 */
1541}
1542
1543
1544/* Initialize IT for stepping through current_buffer in window W,
1545 starting at position POS that includes overlay string and display
1546 vector/ control character translation position information. */
1547
1548static void
1549init_from_display_pos (it, w, pos)
1550 struct it *it;
1551 struct window *w;
1552 struct display_pos *pos;
1553{
1554 /* Keep in mind: the call to reseat in init_iterator skips invisible
1555 text, so we might end up at a position different from POS. This
1556 is only a problem when POS is a row start after a newline and an
1557 overlay starts there with an after-string, and the overlay has an
1558 invisible property. Since we don't skip invisible text in
1559 display_line and elsewhere immediately after consuming the
1560 newline before the row start, such a POS will not be in a string,
1561 but the call to init_iterator below will move us to the
1562 after-string. */
1563 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1564 NULL, DEFAULT_FACE_ID);
1565
1566 /* If position is within an overlay string, set up IT to
1567 the right overlay string. */
1568 if (pos->overlay_string_index >= 0)
1569 {
1570 int relative_index;
1571
1572 /* We already have the first chunk of overlay strings in
1573 IT->overlay_strings. Load more until the one for
1574 pos->overlay_string_index is in IT->overlay_strings. */
1575 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1576 {
1577 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1578 it->current.overlay_string_index = 0;
1579 while (n--)
1580 {
1581 load_overlay_strings (it);
1582 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1583 }
1584 }
1585
1586 it->current.overlay_string_index = pos->overlay_string_index;
1587 relative_index = (it->current.overlay_string_index
1588 % OVERLAY_STRING_CHUNK_SIZE);
1589 it->string = it->overlay_strings[relative_index];
1590 it->current.string_pos = pos->string_pos;
1591 it->method = next_element_from_string;
1592 }
1593 else if (CHARPOS (pos->string_pos) >= 0)
1594 {
1595 /* Recorded position is not in an overlay string, but in another
1596 string. This can only be a string from a `display' property.
1597 IT should already be filled with that string. */
1598 it->current.string_pos = pos->string_pos;
1599 xassert (STRINGP (it->string));
1600 }
1601
1602 /* Restore position in display vector translations or control
1603 character translations. */
1604 if (pos->dpvec_index >= 0)
1605 {
1606 /* This fills IT->dpvec. */
1607 get_next_display_element (it);
1608 xassert (it->dpvec && it->current.dpvec_index == 0);
1609 it->current.dpvec_index = pos->dpvec_index;
1610 }
1611
1612 CHECK_IT (it);
1613}
1614
1615
1616/* Initialize IT for stepping through current_buffer in window W
1617 starting at ROW->start. */
1618
1619static void
1620init_to_row_start (it, w, row)
1621 struct it *it;
1622 struct window *w;
1623 struct glyph_row *row;
1624{
1625 init_from_display_pos (it, w, &row->start);
1626 it->continuation_lines_width = row->continuation_lines_width;
1627 CHECK_IT (it);
1628}
1629
1630
1631/* Initialize IT for stepping through current_buffer in window W
1632 starting in the line following ROW, i.e. starting at ROW->end. */
1633
1634static void
1635init_to_row_end (it, w, row)
1636 struct it *it;
1637 struct window *w;
1638 struct glyph_row *row;
1639{
1640 init_from_display_pos (it, w, &row->end);
1641
1642 if (row->continued_p)
1643 it->continuation_lines_width = (row->continuation_lines_width
1644 + row->pixel_width);
1645 CHECK_IT (it);
1646}
1647
1648
1649
1650\f
1651/***********************************************************************
1652 Text properties
1653 ***********************************************************************/
1654
1655/* Called when IT reaches IT->stop_charpos. Handle text property and
1656 overlay changes. Set IT->stop_charpos to the next position where
1657 to stop. */
1658
1659static void
1660handle_stop (it)
1661 struct it *it;
1662{
1663 enum prop_handled handled;
1664 int handle_overlay_change_p = 1;
1665 struct props *p;
1666
1667 it->dpvec = NULL;
1668 it->current.dpvec_index = -1;
2970b9be 1669 it->add_overlay_start = 0;
5f5c8ee5
GM
1670
1671 do
1672 {
1673 handled = HANDLED_NORMALLY;
1674
1675 /* Call text property handlers. */
1676 for (p = it_props; p->handler; ++p)
1677 {
1678 handled = p->handler (it);
2970b9be 1679
5f5c8ee5
GM
1680 if (handled == HANDLED_RECOMPUTE_PROPS)
1681 break;
1682 else if (handled == HANDLED_RETURN)
1683 return;
1684 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1685 handle_overlay_change_p = 0;
1686 }
1687
1688 if (handled != HANDLED_RECOMPUTE_PROPS)
1689 {
1690 /* Don't check for overlay strings below when set to deliver
1691 characters from a display vector. */
1692 if (it->method == next_element_from_display_vector)
1693 handle_overlay_change_p = 0;
1694
1695 /* Handle overlay changes. */
1696 if (handle_overlay_change_p)
1697 handled = handle_overlay_change (it);
1698
1699 /* Determine where to stop next. */
1700 if (handled == HANDLED_NORMALLY)
1701 compute_stop_pos (it);
1702 }
1703 }
1704 while (handled == HANDLED_RECOMPUTE_PROPS);
1705}
1706
1707
1708/* Compute IT->stop_charpos from text property and overlay change
1709 information for IT's current position. */
1710
1711static void
1712compute_stop_pos (it)
1713 struct it *it;
1714{
1715 register INTERVAL iv, next_iv;
1716 Lisp_Object object, limit, position;
1717
1718 /* If nowhere else, stop at the end. */
1719 it->stop_charpos = it->end_charpos;
1720
1721 if (STRINGP (it->string))
1722 {
1723 /* Strings are usually short, so don't limit the search for
1724 properties. */
1725 object = it->string;
1726 limit = Qnil;
1727 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1728 }
1729 else
1730 {
1731 int charpos;
1732
1733 /* If next overlay change is in front of the current stop pos
1734 (which is IT->end_charpos), stop there. Note: value of
1735 next_overlay_change is point-max if no overlay change
1736 follows. */
1737 charpos = next_overlay_change (IT_CHARPOS (*it));
1738 if (charpos < it->stop_charpos)
1739 it->stop_charpos = charpos;
1740
1741 /* If showing the region, we have to stop at the region
1742 start or end because the face might change there. */
1743 if (it->region_beg_charpos > 0)
1744 {
1745 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1746 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1747 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1748 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1749 }
1750
1751 /* Set up variables for computing the stop position from text
1752 property changes. */
1753 XSETBUFFER (object, current_buffer);
1754 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1755 XSETFASTINT (position, IT_CHARPOS (*it));
1756
1757 }
1758
1759 /* Get the interval containing IT's position. Value is a null
1760 interval if there isn't such an interval. */
1761 iv = validate_interval_range (object, &position, &position, 0);
1762 if (!NULL_INTERVAL_P (iv))
1763 {
1764 Lisp_Object values_here[LAST_PROP_IDX];
1765 struct props *p;
1766
1767 /* Get properties here. */
1768 for (p = it_props; p->handler; ++p)
1769 values_here[p->idx] = textget (iv->plist, *p->name);
1770
1771 /* Look for an interval following iv that has different
1772 properties. */
1773 for (next_iv = next_interval (iv);
1774 (!NULL_INTERVAL_P (next_iv)
1775 && (NILP (limit)
1776 || XFASTINT (limit) > next_iv->position));
1777 next_iv = next_interval (next_iv))
1778 {
1779 for (p = it_props; p->handler; ++p)
1780 {
1781 Lisp_Object new_value;
1782
1783 new_value = textget (next_iv->plist, *p->name);
1784 if (!EQ (values_here[p->idx], new_value))
1785 break;
1786 }
1787
1788 if (p->handler)
1789 break;
1790 }
1791
1792 if (!NULL_INTERVAL_P (next_iv))
1793 {
1794 if (INTEGERP (limit)
1795 && next_iv->position >= XFASTINT (limit))
1796 /* No text property change up to limit. */
1797 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1798 else
1799 /* Text properties change in next_iv. */
1800 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1801 }
1802 }
1803
1804 xassert (STRINGP (it->string)
1805 || (it->stop_charpos >= BEGV
1806 && it->stop_charpos >= IT_CHARPOS (*it)));
1807}
1808
1809
1810/* Return the position of the next overlay change after POS in
1811 current_buffer. Value is point-max if no overlay change
1812 follows. This is like `next-overlay-change' but doesn't use
1813 xmalloc. */
1814
1815static int
1816next_overlay_change (pos)
1817 int pos;
1818{
1819 int noverlays;
1820 int endpos;
1821 Lisp_Object *overlays;
1822 int len;
1823 int i;
1824
1825 /* Get all overlays at the given position. */
1826 len = 10;
1827 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 1828 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
1829 if (noverlays > len)
1830 {
1831 len = noverlays;
1832 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 1833 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
1834 }
1835
1836 /* If any of these overlays ends before endpos,
1837 use its ending point instead. */
1838 for (i = 0; i < noverlays; ++i)
1839 {
1840 Lisp_Object oend;
1841 int oendpos;
1842
1843 oend = OVERLAY_END (overlays[i]);
1844 oendpos = OVERLAY_POSITION (oend);
1845 endpos = min (endpos, oendpos);
1846 }
1847
1848 return endpos;
1849}
1850
1851
1852\f
1853/***********************************************************************
1854 Fontification
1855 ***********************************************************************/
1856
1857/* Handle changes in the `fontified' property of the current buffer by
1858 calling hook functions from Qfontification_functions to fontify
1859 regions of text. */
1860
1861static enum prop_handled
1862handle_fontified_prop (it)
1863 struct it *it;
1864{
1865 Lisp_Object prop, pos;
1866 enum prop_handled handled = HANDLED_NORMALLY;
1867
1868 /* Get the value of the `fontified' property at IT's current buffer
1869 position. (The `fontified' property doesn't have a special
1870 meaning in strings.) If the value is nil, call functions from
1871 Qfontification_functions. */
1872 if (!STRINGP (it->string)
1873 && it->s == NULL
1874 && !NILP (Vfontification_functions)
085536c2 1875 && !NILP (Vrun_hooks)
5f5c8ee5
GM
1876 && (pos = make_number (IT_CHARPOS (*it)),
1877 prop = Fget_char_property (pos, Qfontified, Qnil),
1878 NILP (prop)))
1879 {
085536c2
GM
1880 int count = specpdl_ptr - specpdl;
1881 Lisp_Object val;
1882
1883 val = Vfontification_functions;
1884 specbind (Qfontification_functions, Qnil);
1885 specbind (Qafter_change_functions, Qnil);
1886
1887 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
1888 call1 (val, pos);
1889 else
1890 {
1891 Lisp_Object globals, fn;
1892 struct gcpro gcpro1, gcpro2;
5f5c8ee5 1893
085536c2
GM
1894 globals = Qnil;
1895 GCPRO2 (val, globals);
1896
1897 for (; CONSP (val); val = XCDR (val))
1898 {
1899 fn = XCAR (val);
1900
1901 if (EQ (fn, Qt))
1902 {
1903 /* A value of t indicates this hook has a local
1904 binding; it means to run the global binding too.
1905 In a global value, t should not occur. If it
1906 does, we must ignore it to avoid an endless
1907 loop. */
1908 for (globals = Fdefault_value (Qfontification_functions);
1909 CONSP (globals);
1910 globals = XCDR (globals))
1911 {
1912 fn = XCAR (globals);
1913 if (!EQ (fn, Qt))
1914 call1 (fn, pos);
1915 }
1916 }
1917 else
1918 call1 (fn, pos);
1919 }
1920
1921 UNGCPRO;
1922 }
1923
1924 unbind_to (count, Qnil);
5f5c8ee5
GM
1925
1926 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1927 something. This avoids an endless loop if they failed to
1928 fontify the text for which reason ever. */
1929 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1930 handled = HANDLED_RECOMPUTE_PROPS;
1931 }
1932
1933 return handled;
1934}
1935
1936
1937\f
1938/***********************************************************************
1939 Faces
1940 ***********************************************************************/
1941
1942/* Set up iterator IT from face properties at its current position.
1943 Called from handle_stop. */
1944
1945static enum prop_handled
1946handle_face_prop (it)
1947 struct it *it;
1948{
1949 int new_face_id, next_stop;
1950
1951 if (!STRINGP (it->string))
1952 {
1953 new_face_id
1954 = face_at_buffer_position (it->w,
1955 IT_CHARPOS (*it),
1956 it->region_beg_charpos,
1957 it->region_end_charpos,
1958 &next_stop,
1959 (IT_CHARPOS (*it)
1960 + TEXT_PROP_DISTANCE_LIMIT),
1961 0);
1962
1963 /* Is this a start of a run of characters with box face?
1964 Caveat: this can be called for a freshly initialized
1965 iterator; face_id is -1 is this case. We know that the new
1966 face will not change until limit, i.e. if the new face has a
1967 box, all characters up to limit will have one. But, as
1968 usual, we don't know whether limit is really the end. */
1969 if (new_face_id != it->face_id)
1970 {
1971 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1972
1973 /* If new face has a box but old face has not, this is
1974 the start of a run of characters with box, i.e. it has
1975 a shadow on the left side. The value of face_id of the
1976 iterator will be -1 if this is the initial call that gets
1977 the face. In this case, we have to look in front of IT's
1978 position and see whether there is a face != new_face_id. */
1979 it->start_of_box_run_p
1980 = (new_face->box != FACE_NO_BOX
1981 && (it->face_id >= 0
1982 || IT_CHARPOS (*it) == BEG
1983 || new_face_id != face_before_it_pos (it)));
1984 it->face_box_p = new_face->box != FACE_NO_BOX;
1985 }
1986 }
1987 else
1988 {
1989 new_face_id
1990 = face_at_string_position (it->w,
1991 it->string,
1992 IT_STRING_CHARPOS (*it),
1993 (it->current.overlay_string_index >= 0
1994 ? IT_CHARPOS (*it)
1995 : 0),
1996 it->region_beg_charpos,
1997 it->region_end_charpos,
1998 &next_stop,
1999 it->base_face_id);
2000
2001#if 0 /* This shouldn't be neccessary. Let's check it. */
2002 /* If IT is used to display a mode line we would really like to
2003 use the mode line face instead of the frame's default face. */
2004 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2005 && new_face_id == DEFAULT_FACE_ID)
2006 new_face_id = MODE_LINE_FACE_ID;
2007#endif
2008
2009 /* Is this a start of a run of characters with box? Caveat:
2010 this can be called for a freshly allocated iterator; face_id
2011 is -1 is this case. We know that the new face will not
2012 change until the next check pos, i.e. if the new face has a
2013 box, all characters up to that position will have a
2014 box. But, as usual, we don't know whether that position
2015 is really the end. */
2016 if (new_face_id != it->face_id)
2017 {
2018 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2019 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2020
2021 /* If new face has a box but old face hasn't, this is the
2022 start of a run of characters with box, i.e. it has a
2023 shadow on the left side. */
2024 it->start_of_box_run_p
2025 = new_face->box && (old_face == NULL || !old_face->box);
2026 it->face_box_p = new_face->box != FACE_NO_BOX;
2027 }
2028 }
2029
2030 it->face_id = new_face_id;
5f5c8ee5
GM
2031 return HANDLED_NORMALLY;
2032}
2033
2034
2035/* Compute the face one character before or after the current position
2036 of IT. BEFORE_P non-zero means get the face in front of IT's
2037 position. Value is the id of the face. */
2038
2039static int
2040face_before_or_after_it_pos (it, before_p)
2041 struct it *it;
2042 int before_p;
2043{
2044 int face_id, limit;
2045 int next_check_charpos;
2046 struct text_pos pos;
2047
2048 xassert (it->s == NULL);
2049
2050 if (STRINGP (it->string))
2051 {
2052 /* No face change past the end of the string (for the case
2053 we are padding with spaces). No face change before the
2054 string start. */
2055 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2056 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2057 return it->face_id;
2058
2059 /* Set pos to the position before or after IT's current position. */
2060 if (before_p)
2061 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2062 else
260a86a0
KH
2063 /* For composition, we must check the character after the
2064 composition. */
2065 pos = (it->what == IT_COMPOSITION
2066 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2067 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5
GM
2068
2069 /* Get the face for ASCII, or unibyte. */
2070 face_id
2071 = face_at_string_position (it->w,
2072 it->string,
2073 CHARPOS (pos),
2074 (it->current.overlay_string_index >= 0
2075 ? IT_CHARPOS (*it)
2076 : 0),
2077 it->region_beg_charpos,
2078 it->region_end_charpos,
2079 &next_check_charpos,
2080 it->base_face_id);
2081
2082 /* Correct the face for charsets different from ASCII. Do it
2083 for the multibyte case only. The face returned above is
2084 suitable for unibyte text if IT->string is unibyte. */
2085 if (STRING_MULTIBYTE (it->string))
2086 {
2087 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2088 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
2089 int c, len;
2090 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 2091
4fdb80f2 2092 c = string_char_and_length (p, rest, &len);
980806b6 2093 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2094 }
2095 }
2096 else
2097 {
70851746
GM
2098 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2099 || (IT_CHARPOS (*it) <= BEGV && before_p))
2100 return it->face_id;
2101
5f5c8ee5
GM
2102 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2103 pos = it->current.pos;
2104
2105 if (before_p)
c1005d06 2106 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2107 else
260a86a0
KH
2108 {
2109 if (it->what == IT_COMPOSITION)
2110 /* For composition, we must check the position after the
2111 composition. */
2112 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2113 else
c1005d06 2114 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2115 }
5f5c8ee5
GM
2116 /* Determine face for CHARSET_ASCII, or unibyte. */
2117 face_id = face_at_buffer_position (it->w,
2118 CHARPOS (pos),
2119 it->region_beg_charpos,
2120 it->region_end_charpos,
2121 &next_check_charpos,
2122 limit, 0);
2123
2124 /* Correct the face for charsets different from ASCII. Do it
2125 for the multibyte case only. The face returned above is
2126 suitable for unibyte text if current_buffer is unibyte. */
2127 if (it->multibyte_p)
2128 {
980806b6
KH
2129 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2130 struct face *face = FACE_FROM_ID (it->f, face_id);
2131 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2132 }
2133 }
2134
2135 return face_id;
2136}
2137
2138
2139\f
2140/***********************************************************************
2141 Invisible text
2142 ***********************************************************************/
2143
2144/* Set up iterator IT from invisible properties at its current
2145 position. Called from handle_stop. */
2146
2147static enum prop_handled
2148handle_invisible_prop (it)
2149 struct it *it;
2150{
2151 enum prop_handled handled = HANDLED_NORMALLY;
2152
2153 if (STRINGP (it->string))
2154 {
2155 extern Lisp_Object Qinvisible;
2156 Lisp_Object prop, end_charpos, limit, charpos;
2157
2158 /* Get the value of the invisible text property at the
2159 current position. Value will be nil if there is no such
2160 property. */
2161 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2162 prop = Fget_text_property (charpos, Qinvisible, it->string);
2163
eadc0bf8
GM
2164 if (!NILP (prop)
2165 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2166 {
2167 handled = HANDLED_RECOMPUTE_PROPS;
2168
2169 /* Get the position at which the next change of the
2170 invisible text property can be found in IT->string.
2171 Value will be nil if the property value is the same for
2172 all the rest of IT->string. */
2173 XSETINT (limit, XSTRING (it->string)->size);
2174 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2175 it->string, limit);
2176
2177 /* Text at current position is invisible. The next
2178 change in the property is at position end_charpos.
2179 Move IT's current position to that position. */
2180 if (INTEGERP (end_charpos)
2181 && XFASTINT (end_charpos) < XFASTINT (limit))
2182 {
2183 struct text_pos old;
2184 old = it->current.string_pos;
2185 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2186 compute_string_pos (&it->current.string_pos, old, it->string);
2187 }
2188 else
2189 {
2190 /* The rest of the string is invisible. If this is an
2191 overlay string, proceed with the next overlay string
2192 or whatever comes and return a character from there. */
2193 if (it->current.overlay_string_index >= 0)
2194 {
2195 next_overlay_string (it);
2196 /* Don't check for overlay strings when we just
2197 finished processing them. */
2198 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2199 }
2200 else
2201 {
2202 struct Lisp_String *s = XSTRING (it->string);
2203 IT_STRING_CHARPOS (*it) = s->size;
2204 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2205 }
2206 }
2207 }
2208 }
2209 else
2210 {
2211 int visible_p, newpos, next_stop;
2212 Lisp_Object pos, prop;
2213
2214 /* First of all, is there invisible text at this position? */
2215 XSETFASTINT (pos, IT_CHARPOS (*it));
2216 prop = Fget_char_property (pos, Qinvisible, it->window);
2217
2218 /* If we are on invisible text, skip over it. */
eadc0bf8
GM
2219 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2220 && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2221 {
2222 /* Record whether we have to display an ellipsis for the
2223 invisible text. */
2224 int display_ellipsis_p
2225 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2226
2227 handled = HANDLED_RECOMPUTE_PROPS;
2970b9be 2228 it->add_overlay_start = IT_CHARPOS (*it);
5f5c8ee5
GM
2229
2230 /* Loop skipping over invisible text. The loop is left at
2231 ZV or with IT on the first char being visible again. */
2232 do
2233 {
2234 /* Try to skip some invisible text. Return value is the
2235 position reached which can be equal to IT's position
2236 if there is nothing invisible here. This skips both
2237 over invisible text properties and overlays with
2238 invisible property. */
2239 newpos = skip_invisible (IT_CHARPOS (*it),
2240 &next_stop, ZV, it->window);
2241
2242 /* If we skipped nothing at all we weren't at invisible
2243 text in the first place. If everything to the end of
2244 the buffer was skipped, end the loop. */
2245 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2246 visible_p = 1;
2247 else
2248 {
2249 /* We skipped some characters but not necessarily
2250 all there are. Check if we ended up on visible
2251 text. Fget_char_property returns the property of
2252 the char before the given position, i.e. if we
2253 get visible_p = 1, this means that the char at
2254 newpos is visible. */
2255 XSETFASTINT (pos, newpos);
2256 prop = Fget_char_property (pos, Qinvisible, it->window);
2257 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2258 }
2259
2260 /* If we ended up on invisible text, proceed to
2261 skip starting with next_stop. */
2262 if (!visible_p)
2263 IT_CHARPOS (*it) = next_stop;
2264 }
2265 while (!visible_p);
2266
2267 /* The position newpos is now either ZV or on visible text. */
2268 IT_CHARPOS (*it) = newpos;
2269 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2270
2271 /* Maybe return `...' next for the end of the invisible text. */
2272 if (display_ellipsis_p)
2273 {
2274 if (it->dp
2275 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2276 {
2277 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2278 it->dpvec = v->contents;
2279 it->dpend = v->contents + v->size;
2280 }
2281 else
2282 {
2283 /* Default `...'. */
2284 it->dpvec = default_invis_vector;
2285 it->dpend = default_invis_vector + 3;
2286 }
2287
2288 /* The ellipsis display does not replace the display of
2289 the character at the new position. Indicate this by
2290 setting IT->dpvec_char_len to zero. */
2291 it->dpvec_char_len = 0;
2292
2293 it->current.dpvec_index = 0;
2294 it->method = next_element_from_display_vector;
2295 }
2296 }
2297 }
2298
2299 return handled;
2300}
2301
2302
2303\f
2304/***********************************************************************
2305 'display' property
2306 ***********************************************************************/
2307
2308/* Set up iterator IT from `display' property at its current position.
2309 Called from handle_stop. */
2310
2311static enum prop_handled
2312handle_display_prop (it)
2313 struct it *it;
2314{
2315 Lisp_Object prop, object;
2316 struct text_pos *position;
2317 int space_or_image_found_p;
2318
2319 if (STRINGP (it->string))
2320 {
2321 object = it->string;
2322 position = &it->current.string_pos;
2323 }
2324 else
2325 {
2326 object = Qnil;
2327 position = &it->current.pos;
2328 }
2329
2330 /* Reset those iterator values set from display property values. */
2331 it->font_height = Qnil;
2332 it->space_width = Qnil;
2333 it->voffset = 0;
2334
2335 /* We don't support recursive `display' properties, i.e. string
2336 values that have a string `display' property, that have a string
2337 `display' property etc. */
2338 if (!it->string_from_display_prop_p)
2339 it->area = TEXT_AREA;
2340
2341 prop = Fget_char_property (make_number (position->charpos),
2342 Qdisplay, object);
2343 if (NILP (prop))
2344 return HANDLED_NORMALLY;
2345
2346 space_or_image_found_p = 0;
f3751a65
GM
2347 if (CONSP (prop)
2348 && CONSP (XCAR (prop))
2349 && !EQ (Qmargin, XCAR (XCAR (prop))))
5f5c8ee5 2350 {
f3751a65 2351 /* A list of sub-properties. */
5f5c8ee5
GM
2352 while (CONSP (prop))
2353 {
2354 if (handle_single_display_prop (it, XCAR (prop), object, position))
2355 space_or_image_found_p = 1;
2356 prop = XCDR (prop);
2357 }
2358 }
2359 else if (VECTORP (prop))
2360 {
2361 int i;
2362 for (i = 0; i < XVECTOR (prop)->size; ++i)
2363 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2364 object, position))
2365 space_or_image_found_p = 1;
2366 }
2367 else
2368 {
2369 if (handle_single_display_prop (it, prop, object, position))
2370 space_or_image_found_p = 1;
2371 }
2372
2373 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2374}
2375
2376
6c577098 2377/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2378 at START_POS in OBJECT. */
2379
2380static struct text_pos
2381display_prop_end (it, object, start_pos)
2382 struct it *it;
2383 Lisp_Object object;
2384 struct text_pos start_pos;
2385{
2386 Lisp_Object end;
2387 struct text_pos end_pos;
5f5c8ee5 2388
016b5642
MB
2389 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2390 Qdisplay, object, Qnil);
6c577098
GM
2391 CHARPOS (end_pos) = XFASTINT (end);
2392 if (STRINGP (object))
5f5c8ee5
GM
2393 compute_string_pos (&end_pos, start_pos, it->string);
2394 else
6c577098 2395 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2396
2397 return end_pos;
2398}
2399
2400
2401/* Set up IT from a single `display' sub-property value PROP. OBJECT
2402 is the object in which the `display' property was found. *POSITION
2403 is the position at which it was found.
2404
2405 If PROP is a `space' or `image' sub-property, set *POSITION to the
2406 end position of the `display' property.
2407
2408 Value is non-zero if a `space' or `image' property value was found. */
2409
2410static int
2411handle_single_display_prop (it, prop, object, position)
2412 struct it *it;
2413 Lisp_Object prop;
2414 Lisp_Object object;
2415 struct text_pos *position;
2416{
2417 Lisp_Object value;
2418 int space_or_image_found_p = 0;
5f5c8ee5
GM
2419 Lisp_Object form;
2420
d3acf96b 2421 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2422 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2423 form = Qt;
d3acf96b 2424 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2425 {
2426 prop = XCDR (prop);
2427 if (!CONSP (prop))
2428 return 0;
2429 form = XCAR (prop);
2430 prop = XCDR (prop);
5f5c8ee5
GM
2431 }
2432
2433 if (!NILP (form) && !EQ (form, Qt))
2434 {
2435 struct gcpro gcpro1;
2436 struct text_pos end_pos, pt;
2437
5f5c8ee5 2438 GCPRO1 (form);
c7aca1ad 2439 end_pos = display_prop_end (it, object, *position);
5f5c8ee5
GM
2440
2441 /* Temporarily set point to the end position, and then evaluate
2442 the form. This makes `(eolp)' work as FORM. */
c7aca1ad
GM
2443 if (BUFFERP (object))
2444 {
2445 CHARPOS (pt) = PT;
2446 BYTEPOS (pt) = PT_BYTE;
2447 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2448 }
2449
5f5c8ee5 2450 form = eval_form (form);
c7aca1ad
GM
2451
2452 if (BUFFERP (object))
2453 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
5f5c8ee5
GM
2454 UNGCPRO;
2455 }
2456
2457 if (NILP (form))
2458 return 0;
2459
2460 if (CONSP (prop)
2461 && EQ (XCAR (prop), Qheight)
2462 && CONSP (XCDR (prop)))
2463 {
2464 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2465 return 0;
2466
2467 /* `(height HEIGHT)'. */
2468 it->font_height = XCAR (XCDR (prop));
2469 if (!NILP (it->font_height))
2470 {
2471 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2472 int new_height = -1;
2473
2474 if (CONSP (it->font_height)
2475 && (EQ (XCAR (it->font_height), Qplus)
2476 || EQ (XCAR (it->font_height), Qminus))
2477 && CONSP (XCDR (it->font_height))
2478 && INTEGERP (XCAR (XCDR (it->font_height))))
2479 {
2480 /* `(+ N)' or `(- N)' where N is an integer. */
2481 int steps = XINT (XCAR (XCDR (it->font_height)));
2482 if (EQ (XCAR (it->font_height), Qplus))
2483 steps = - steps;
2484 it->face_id = smaller_face (it->f, it->face_id, steps);
2485 }
0d8b31c0 2486 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2487 {
2488 /* Call function with current height as argument.
2489 Value is the new height. */
0d8b31c0
GM
2490 Lisp_Object args[2], height;
2491
2492 args[0] = it->font_height;
2493 args[1] = face->lface[LFACE_HEIGHT_INDEX];
2494 height = call_function (2, args);
5f5c8ee5 2495
5f5c8ee5
GM
2496 if (NUMBERP (height))
2497 new_height = XFLOATINT (height);
5f5c8ee5
GM
2498 }
2499 else if (NUMBERP (it->font_height))
2500 {
2501 /* Value is a multiple of the canonical char height. */
2502 struct face *face;
2503
2504 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2505 new_height = (XFLOATINT (it->font_height)
2506 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2507 }
2508 else
2509 {
2510 /* Evaluate IT->font_height with `height' bound to the
2511 current specified height to get the new height. */
2512 Lisp_Object value;
2513 int count = specpdl_ptr - specpdl;
2514
2515 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2516 value = eval_form (it->font_height);
2517 unbind_to (count, Qnil);
2518
2519 if (NUMBERP (value))
2520 new_height = XFLOATINT (value);
2521 }
2522
2523 if (new_height > 0)
2524 it->face_id = face_with_height (it->f, it->face_id, new_height);
2525 }
2526 }
2527 else if (CONSP (prop)
2528 && EQ (XCAR (prop), Qspace_width)
2529 && CONSP (XCDR (prop)))
2530 {
2531 /* `(space_width WIDTH)'. */
2532 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2533 return 0;
2534
2535 value = XCAR (XCDR (prop));
2536 if (NUMBERP (value) && XFLOATINT (value) > 0)
2537 it->space_width = value;
2538 }
2539 else if (CONSP (prop)
2540 && EQ (XCAR (prop), Qraise)
2541 && CONSP (XCDR (prop)))
2542 {
5f5c8ee5
GM
2543 /* `(raise FACTOR)'. */
2544 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2545 return 0;
2546
fb3842a8 2547#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2548 value = XCAR (XCDR (prop));
2549 if (NUMBERP (value))
2550 {
2551 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2552 it->voffset = - (XFLOATINT (value)
1ab3e082 2553 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2554 }
2555#endif /* HAVE_WINDOW_SYSTEM */
2556 }
2557 else if (!it->string_from_display_prop_p)
2558 {
f3751a65
GM
2559 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2560 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2561 Lisp_Object location, value;
2562 struct text_pos start_pos;
2563 int valid_p;
2564
2565 /* Characters having this form of property are not displayed, so
2566 we have to find the end of the property. */
5f5c8ee5
GM
2567 start_pos = *position;
2568 *position = display_prop_end (it, object, start_pos);
15e26c76 2569 value = Qnil;
5f5c8ee5
GM
2570
2571 /* Let's stop at the new position and assume that all
2572 text properties change there. */
2573 it->stop_charpos = position->charpos;
2574
f3751a65
GM
2575 location = Qunbound;
2576 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2577 {
f3751a65
GM
2578 Lisp_Object tem;
2579
5f5c8ee5 2580 value = XCDR (prop);
f3751a65
GM
2581 if (CONSP (value))
2582 value = XCAR (value);
2583
2584 tem = XCAR (prop);
2585 if (EQ (XCAR (tem), Qmargin)
2586 && (tem = XCDR (tem),
2587 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2588 (NILP (tem)
2589 || EQ (tem, Qleft_margin)
2590 || EQ (tem, Qright_margin))))
2591 location = tem;
5f5c8ee5 2592 }
f3751a65
GM
2593
2594 if (EQ (location, Qunbound))
5f5c8ee5
GM
2595 {
2596 location = Qnil;
2597 value = prop;
2598 }
2599
2600#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 2601 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
2602 valid_p = STRINGP (value);
2603 else
2604 valid_p = (STRINGP (value)
2605 || (CONSP (value) && EQ (XCAR (value), Qspace))
2606 || valid_image_p (value));
2607#else /* not HAVE_WINDOW_SYSTEM */
2608 valid_p = STRINGP (value);
2609#endif /* not HAVE_WINDOW_SYSTEM */
2610
2611 if ((EQ (location, Qleft_margin)
2612 || EQ (location, Qright_margin)
2613 || NILP (location))
2614 && valid_p)
2615 {
a21a3943
GM
2616 space_or_image_found_p = 1;
2617
5f5c8ee5
GM
2618 /* Save current settings of IT so that we can restore them
2619 when we are finished with the glyph property value. */
2620 push_it (it);
2621
2622 if (NILP (location))
2623 it->area = TEXT_AREA;
2624 else if (EQ (location, Qleft_margin))
2625 it->area = LEFT_MARGIN_AREA;
2626 else
2627 it->area = RIGHT_MARGIN_AREA;
2628
2629 if (STRINGP (value))
2630 {
2631 it->string = value;
2632 it->multibyte_p = STRING_MULTIBYTE (it->string);
2633 it->current.overlay_string_index = -1;
2634 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2635 it->end_charpos = it->string_nchars
2636 = XSTRING (it->string)->size;
2637 it->method = next_element_from_string;
2638 it->stop_charpos = 0;
2639 it->string_from_display_prop_p = 1;
2640 }
2641 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2642 {
2643 it->method = next_element_from_stretch;
2644 it->object = value;
2645 it->current.pos = it->position = start_pos;
2646 }
2647#ifdef HAVE_WINDOW_SYSTEM
2648 else
2649 {
2650 it->what = IT_IMAGE;
2651 it->image_id = lookup_image (it->f, value);
2652 it->position = start_pos;
2653 it->object = NILP (object) ? it->w->buffer : object;
2654 it->method = next_element_from_image;
2655
02513cdd 2656 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
2657 `display' property yet. The call to pop_it in
2658 set_iterator_to_next will clean this up. */
2659 *position = start_pos;
2660 }
2661#endif /* HAVE_WINDOW_SYSTEM */
2662 }
a21a3943
GM
2663 else
2664 /* Invalid property or property not supported. Restore
2665 the position to what it was before. */
2666 *position = start_pos;
5f5c8ee5
GM
2667 }
2668
2669 return space_or_image_found_p;
2670}
2671
2672
06568bbf
GM
2673/* Check if PROP is a display sub-property value whose text should be
2674 treated as intangible. */
2675
2676static int
2677single_display_prop_intangible_p (prop)
2678 Lisp_Object prop;
2679{
2680 /* Skip over `when FORM'. */
2681 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2682 {
2683 prop = XCDR (prop);
2684 if (!CONSP (prop))
2685 return 0;
2686 prop = XCDR (prop);
2687 }
2688
2689 if (!CONSP (prop))
2690 return 0;
2691
2692 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2693 we don't need to treat text as intangible. */
2694 if (EQ (XCAR (prop), Qmargin))
2695 {
2696 prop = XCDR (prop);
2697 if (!CONSP (prop))
2698 return 0;
2699
2700 prop = XCDR (prop);
2701 if (!CONSP (prop)
2702 || EQ (XCAR (prop), Qleft_margin)
2703 || EQ (XCAR (prop), Qright_margin))
2704 return 0;
2705 }
2706
2707 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2708}
2709
2710
2711/* Check if PROP is a display property value whose text should be
2712 treated as intangible. */
2713
2714int
2715display_prop_intangible_p (prop)
2716 Lisp_Object prop;
2717{
2718 if (CONSP (prop)
2719 && CONSP (XCAR (prop))
2720 && !EQ (Qmargin, XCAR (XCAR (prop))))
2721 {
2722 /* A list of sub-properties. */
2723 while (CONSP (prop))
2724 {
2725 if (single_display_prop_intangible_p (XCAR (prop)))
2726 return 1;
2727 prop = XCDR (prop);
2728 }
2729 }
2730 else if (VECTORP (prop))
2731 {
2732 /* A vector of sub-properties. */
2733 int i;
2734 for (i = 0; i < XVECTOR (prop)->size; ++i)
2735 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2736 return 1;
2737 }
2738 else
2739 return single_display_prop_intangible_p (prop);
2740
2741 return 0;
2742}
2743
5f5c8ee5 2744\f
260a86a0
KH
2745/***********************************************************************
2746 `composition' property
2747 ***********************************************************************/
2748
2749/* Set up iterator IT from `composition' property at its current
2750 position. Called from handle_stop. */
2751
2752static enum prop_handled
2753handle_composition_prop (it)
2754 struct it *it;
2755{
2756 Lisp_Object prop, string;
2757 int pos, pos_byte, end;
2758 enum prop_handled handled = HANDLED_NORMALLY;
2759
2760 if (STRINGP (it->string))
2761 {
2762 pos = IT_STRING_CHARPOS (*it);
2763 pos_byte = IT_STRING_BYTEPOS (*it);
2764 string = it->string;
2765 }
2766 else
2767 {
2768 pos = IT_CHARPOS (*it);
2769 pos_byte = IT_BYTEPOS (*it);
2770 string = Qnil;
2771 }
2772
2773 /* If there's a valid composition and point is not inside of the
2774 composition (in the case that the composition is from the current
2775 buffer), draw a glyph composed from the composition components. */
2776 if (find_composition (pos, -1, &pos, &end, &prop, string)
2777 && COMPOSITION_VALID_P (pos, end, prop)
2778 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2779 {
2780 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2781
2782 if (id >= 0)
2783 {
2784 it->method = next_element_from_composition;
2785 it->cmp_id = id;
2786 it->cmp_len = COMPOSITION_LENGTH (prop);
2787 /* For a terminal, draw only the first character of the
2788 components. */
2789 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2790 it->len = (STRINGP (it->string)
2791 ? string_char_to_byte (it->string, end)
2792 : CHAR_TO_BYTE (end)) - pos_byte;
2793 it->stop_charpos = end;
2794 handled = HANDLED_RETURN;
2795 }
2796 }
2797
2798 return handled;
2799}
2800
2801
2802\f
5f5c8ee5
GM
2803/***********************************************************************
2804 Overlay strings
2805 ***********************************************************************/
2806
2807/* The following structure is used to record overlay strings for
2808 later sorting in load_overlay_strings. */
2809
2810struct overlay_entry
2811{
2970b9be 2812 Lisp_Object overlay;
5f5c8ee5
GM
2813 Lisp_Object string;
2814 int priority;
2815 int after_string_p;
2816};
2817
2818
2819/* Set up iterator IT from overlay strings at its current position.
2820 Called from handle_stop. */
2821
2822static enum prop_handled
2823handle_overlay_change (it)
2824 struct it *it;
2825{
2970b9be
GM
2826 if (!STRINGP (it->string) && get_overlay_strings (it))
2827 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 2828 else
2970b9be 2829 return HANDLED_NORMALLY;
5f5c8ee5
GM
2830}
2831
2832
2833/* Set up the next overlay string for delivery by IT, if there is an
2834 overlay string to deliver. Called by set_iterator_to_next when the
2835 end of the current overlay string is reached. If there are more
2836 overlay strings to display, IT->string and
2837 IT->current.overlay_string_index are set appropriately here.
2838 Otherwise IT->string is set to nil. */
2839
2840static void
2841next_overlay_string (it)
2842 struct it *it;
2843{
2844 ++it->current.overlay_string_index;
2845 if (it->current.overlay_string_index == it->n_overlay_strings)
2846 {
2847 /* No more overlay strings. Restore IT's settings to what
2848 they were before overlay strings were processed, and
2849 continue to deliver from current_buffer. */
2850 pop_it (it);
2851 xassert (it->stop_charpos >= BEGV
2852 && it->stop_charpos <= it->end_charpos);
2853 it->string = Qnil;
2854 it->current.overlay_string_index = -1;
2855 SET_TEXT_POS (it->current.string_pos, -1, -1);
2856 it->n_overlay_strings = 0;
2857 it->method = next_element_from_buffer;
2970b9be
GM
2858
2859 /* If we're at the end of the buffer, record that we have
2860 processed the overlay strings there already, so that
2861 next_element_from_buffer doesn't try it again. */
2862 if (IT_CHARPOS (*it) >= it->end_charpos)
2863 it->overlay_strings_at_end_processed_p = 1;
5f5c8ee5
GM
2864 }
2865 else
2866 {
2867 /* There are more overlay strings to process. If
2868 IT->current.overlay_string_index has advanced to a position
2869 where we must load IT->overlay_strings with more strings, do
2870 it. */
2871 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2872
2873 if (it->current.overlay_string_index && i == 0)
2874 load_overlay_strings (it);
2875
2876 /* Initialize IT to deliver display elements from the overlay
2877 string. */
2878 it->string = it->overlay_strings[i];
2879 it->multibyte_p = STRING_MULTIBYTE (it->string);
2880 SET_TEXT_POS (it->current.string_pos, 0, 0);
2881 it->method = next_element_from_string;
2882 it->stop_charpos = 0;
2883 }
2884
2885 CHECK_IT (it);
2886}
2887
2888
2889/* Compare two overlay_entry structures E1 and E2. Used as a
2890 comparison function for qsort in load_overlay_strings. Overlay
2891 strings for the same position are sorted so that
2892
2970b9be
GM
2893 1. All after-strings come in front of before-strings, except
2894 when they come from the same overlay.
5f5c8ee5
GM
2895
2896 2. Within after-strings, strings are sorted so that overlay strings
2897 from overlays with higher priorities come first.
2898
2899 2. Within before-strings, strings are sorted so that overlay
2900 strings from overlays with higher priorities come last.
2901
2902 Value is analogous to strcmp. */
2903
2904
2905static int
2906compare_overlay_entries (e1, e2)
2907 void *e1, *e2;
2908{
2909 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2910 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2911 int result;
2912
2913 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
2914 {
2915 /* Let after-strings appear in front of before-strings if
2916 they come from different overlays. */
2917 if (EQ (entry1->overlay, entry2->overlay))
2918 result = entry1->after_string_p ? 1 : -1;
2919 else
2920 result = entry1->after_string_p ? -1 : 1;
2921 }
5f5c8ee5
GM
2922 else if (entry1->after_string_p)
2923 /* After-strings sorted in order of decreasing priority. */
2924 result = entry2->priority - entry1->priority;
2925 else
2926 /* Before-strings sorted in order of increasing priority. */
2927 result = entry1->priority - entry2->priority;
2928
2929 return result;
2930}
2931
2932
2933/* Load the vector IT->overlay_strings with overlay strings from IT's
2934 current buffer position. Set IT->n_overlays to the total number of
2935 overlay strings found.
2936
2937 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2938 a time. On entry into load_overlay_strings,
2939 IT->current.overlay_string_index gives the number of overlay
2940 strings that have already been loaded by previous calls to this
2941 function.
2942
2970b9be
GM
2943 IT->add_overlay_start contains an additional overlay start
2944 position to consider for taking overlay strings from, if non-zero.
2945 This position comes into play when the overlay has an `invisible'
2946 property, and both before and after-strings. When we've skipped to
2947 the end of the overlay, because of its `invisible' property, we
2948 nevertheless want its before-string to appear.
2949 IT->add_overlay_start will contain the overlay start position
2950 in this case.
2951
5f5c8ee5
GM
2952 Overlay strings are sorted so that after-string strings come in
2953 front of before-string strings. Within before and after-strings,
2954 strings are sorted by overlay priority. See also function
2955 compare_overlay_entries. */
2956
2957static void
2958load_overlay_strings (it)
2959 struct it *it;
2960{
2961 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2962 Lisp_Object ov, overlay, window, str;
2963 int start, end;
2964 int size = 20;
2965 int n = 0, i, j;
2966 struct overlay_entry *entries
2967 = (struct overlay_entry *) alloca (size * sizeof *entries);
2968
2969 /* Append the overlay string STRING of overlay OVERLAY to vector
2970 `entries' which has size `size' and currently contains `n'
2971 elements. AFTER_P non-zero means STRING is an after-string of
2972 OVERLAY. */
2973#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2974 do \
2975 { \
2976 Lisp_Object priority; \
2977 \
2978 if (n == size) \
2979 { \
2980 int new_size = 2 * size; \
2981 struct overlay_entry *old = entries; \
2982 entries = \
2983 (struct overlay_entry *) alloca (new_size \
2984 * sizeof *entries); \
2985 bcopy (old, entries, size * sizeof *entries); \
2986 size = new_size; \
2987 } \
2988 \
2989 entries[n].string = (STRING); \
2970b9be 2990 entries[n].overlay = (OVERLAY); \
5f5c8ee5 2991 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 2992 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
2993 entries[n].after_string_p = (AFTER_P); \
2994 ++n; \
2995 } \
2996 while (0)
2997
2998 /* Process overlay before the overlay center. */
2970b9be 2999 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3000 {
9472f927 3001 overlay = XCAR (ov);
5f5c8ee5
GM
3002 xassert (OVERLAYP (overlay));
3003 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3004 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3005
3006 if (end < IT_CHARPOS (*it))
3007 break;
3008
3009 /* Skip this overlay if it doesn't start or end at IT's current
3010 position. */
2970b9be
GM
3011 if (end != IT_CHARPOS (*it)
3012 && start != IT_CHARPOS (*it)
3013 && it->add_overlay_start != IT_CHARPOS (*it))
5f5c8ee5
GM
3014 continue;
3015
3016 /* Skip this overlay if it doesn't apply to IT->w. */
3017 window = Foverlay_get (overlay, Qwindow);
3018 if (WINDOWP (window) && XWINDOW (window) != it->w)
3019 continue;
3020
3021 /* If overlay has a non-empty before-string, record it. */
2970b9be
GM
3022 if ((start == IT_CHARPOS (*it)
3023 || start == it->add_overlay_start)
5f5c8ee5
GM
3024 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3025 && XSTRING (str)->size)
3026 RECORD_OVERLAY_STRING (overlay, str, 0);
3027
3028 /* If overlay has a non-empty after-string, record it. */
3029 if (end == IT_CHARPOS (*it)
3030 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3031 && XSTRING (str)->size)
3032 RECORD_OVERLAY_STRING (overlay, str, 1);
3033 }
3034
3035 /* Process overlays after the overlay center. */
2970b9be 3036 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3037 {
9472f927 3038 overlay = XCAR (ov);
5f5c8ee5
GM
3039 xassert (OVERLAYP (overlay));
3040 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3041 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3042
3043 if (start > IT_CHARPOS (*it))
3044 break;
3045
3046 /* Skip this overlay if it doesn't start or end at IT's current
3047 position. */
2970b9be
GM
3048 if (end != IT_CHARPOS (*it)
3049 && start != IT_CHARPOS (*it)
3050 && it->add_overlay_start != IT_CHARPOS (*it))
5f5c8ee5
GM
3051 continue;
3052
3053 /* Skip this overlay if it doesn't apply to IT->w. */
3054 window = Foverlay_get (overlay, Qwindow);
3055 if (WINDOWP (window) && XWINDOW (window) != it->w)
3056 continue;
3057
3058 /* If overlay has a non-empty before-string, record it. */
2970b9be
GM
3059 if ((start == IT_CHARPOS (*it)
3060 || start == it->add_overlay_start)
5f5c8ee5
GM
3061 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3062 && XSTRING (str)->size)
3063 RECORD_OVERLAY_STRING (overlay, str, 0);
3064
3065 /* If overlay has a non-empty after-string, record it. */
3066 if (end == IT_CHARPOS (*it)
3067 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3068 && XSTRING (str)->size)
3069 RECORD_OVERLAY_STRING (overlay, str, 1);
3070 }
3071
3072#undef RECORD_OVERLAY_STRING
3073
3074 /* Sort entries. */
2970b9be
GM
3075 if (n)
3076 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3077
3078 /* Record the total number of strings to process. */
3079 it->n_overlay_strings = n;
3080
3081 /* IT->current.overlay_string_index is the number of overlay strings
3082 that have already been consumed by IT. Copy some of the
3083 remaining overlay strings to IT->overlay_strings. */
3084 i = 0;
3085 j = it->current.overlay_string_index;
3086 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3087 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3088
5f5c8ee5
GM
3089 CHECK_IT (it);
3090}
3091
3092
3093/* Get the first chunk of overlay strings at IT's current buffer
3094 position. Value is non-zero if at least one overlay string was
3095 found. */
3096
3097static int
3098get_overlay_strings (it)
3099 struct it *it;
3100{
3101 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3102 process. This fills IT->overlay_strings with strings, and sets
3103 IT->n_overlay_strings to the total number of strings to process.
3104 IT->pos.overlay_string_index has to be set temporarily to zero
3105 because load_overlay_strings needs this; it must be set to -1
3106 when no overlay strings are found because a zero value would
3107 indicate a position in the first overlay string. */
3108 it->current.overlay_string_index = 0;
3109 load_overlay_strings (it);
3110
3111 /* If we found overlay strings, set up IT to deliver display
3112 elements from the first one. Otherwise set up IT to deliver
3113 from current_buffer. */
3114 if (it->n_overlay_strings)
3115 {
3116 /* Make sure we know settings in current_buffer, so that we can
3117 restore meaningful values when we're done with the overlay
3118 strings. */
3119 compute_stop_pos (it);
3120 xassert (it->face_id >= 0);
3121
3122 /* Save IT's settings. They are restored after all overlay
3123 strings have been processed. */
3124 xassert (it->sp == 0);
3125 push_it (it);
3126
3127 /* Set up IT to deliver display elements from the first overlay
3128 string. */
3129 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3130 it->stop_charpos = 0;
3131 it->string = it->overlay_strings[0];
3132 it->multibyte_p = STRING_MULTIBYTE (it->string);
3133 xassert (STRINGP (it->string));
3134 it->method = next_element_from_string;
3135 }
3136 else
3137 {
3138 it->string = Qnil;
3139 it->current.overlay_string_index = -1;
3140 it->method = next_element_from_buffer;
3141 }
3142
3143 CHECK_IT (it);
3144
3145 /* Value is non-zero if we found at least one overlay string. */
3146 return STRINGP (it->string);
3147}
3148
3149
3150\f
3151/***********************************************************************
3152 Saving and restoring state
3153 ***********************************************************************/
3154
3155/* Save current settings of IT on IT->stack. Called, for example,
3156 before setting up IT for an overlay string, to be able to restore
3157 IT's settings to what they were after the overlay string has been
3158 processed. */
3159
3160static void
3161push_it (it)
3162 struct it *it;
3163{
3164 struct iterator_stack_entry *p;
3165
3166 xassert (it->sp < 2);
3167 p = it->stack + it->sp;
3168
3169 p->stop_charpos = it->stop_charpos;
3170 xassert (it->face_id >= 0);
3171 p->face_id = it->face_id;
3172 p->string = it->string;
3173 p->pos = it->current;
3174 p->end_charpos = it->end_charpos;
3175 p->string_nchars = it->string_nchars;
3176 p->area = it->area;
3177 p->multibyte_p = it->multibyte_p;
3178 p->space_width = it->space_width;
3179 p->font_height = it->font_height;
3180 p->voffset = it->voffset;
3181 p->string_from_display_prop_p = it->string_from_display_prop_p;
3182 ++it->sp;
3183}
3184
3185
3186/* Restore IT's settings from IT->stack. Called, for example, when no
3187 more overlay strings must be processed, and we return to delivering
3188 display elements from a buffer, or when the end of a string from a
3189 `display' property is reached and we return to delivering display
3190 elements from an overlay string, or from a buffer. */
3191
3192static void
3193pop_it (it)
3194 struct it *it;
3195{
3196 struct iterator_stack_entry *p;
3197
3198 xassert (it->sp > 0);
3199 --it->sp;
3200 p = it->stack + it->sp;
3201 it->stop_charpos = p->stop_charpos;
3202 it->face_id = p->face_id;
3203 it->string = p->string;
3204 it->current = p->pos;
3205 it->end_charpos = p->end_charpos;
3206 it->string_nchars = p->string_nchars;
3207 it->area = p->area;
3208 it->multibyte_p = p->multibyte_p;
3209 it->space_width = p->space_width;
3210 it->font_height = p->font_height;
3211 it->voffset = p->voffset;
3212 it->string_from_display_prop_p = p->string_from_display_prop_p;
3213}
3214
3215
3216\f
3217/***********************************************************************
3218 Moving over lines
3219 ***********************************************************************/
3220
3221/* Set IT's current position to the previous line start. */
3222
3223static void
3224back_to_previous_line_start (it)
3225 struct it *it;
3226{
3227 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3228 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3229}
3230
3231
3232/* Set IT's current position to the next line start. */
3233
3234static void
3235forward_to_next_line_start (it)
3236 struct it *it;
3237{
3238 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3239 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3240}
3241
3242
3243/* Set IT's current position to the previous visible line start. Skip
3244 invisible text that is so either due to text properties or due to
3245 selective display. Caution: this does not change IT->current_x and
3246 IT->hpos. */
3247
3248static void
3249back_to_previous_visible_line_start (it)
3250 struct it *it;
3251{
3252 int visible_p = 0;
3253
3254 /* Go back one newline if not on BEGV already. */
3255 if (IT_CHARPOS (*it) > BEGV)
3256 back_to_previous_line_start (it);
3257
3258 /* Move over lines that are invisible because of selective display
3259 or text properties. */
3260 while (IT_CHARPOS (*it) > BEGV
3261 && !visible_p)
3262 {
3263 visible_p = 1;
3264
3265 /* If selective > 0, then lines indented more than that values
3266 are invisible. */
3267 if (it->selective > 0
3268 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3269 it->selective))
3270 visible_p = 0;
5f5c8ee5
GM
3271 else
3272 {
3273 Lisp_Object prop;
3274
6fc556fd
KR
3275 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3276 Qinvisible, it->window);
5f5c8ee5
GM
3277 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3278 visible_p = 0;
3279 }
5f5c8ee5
GM
3280
3281 /* Back one more newline if the current one is invisible. */
3282 if (!visible_p)
3283 back_to_previous_line_start (it);
3284 }
3285
3286 xassert (IT_CHARPOS (*it) >= BEGV);
3287 xassert (IT_CHARPOS (*it) == BEGV
3288 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3289 CHECK_IT (it);
3290}
3291
3292
3293/* Reseat iterator IT at the previous visible line start. Skip
3294 invisible text that is so either due to text properties or due to
3295 selective display. At the end, update IT's overlay information,
3296 face information etc. */
3297
3298static void
3299reseat_at_previous_visible_line_start (it)
3300 struct it *it;
3301{
3302 back_to_previous_visible_line_start (it);
3303 reseat (it, it->current.pos, 1);
3304 CHECK_IT (it);
3305}
3306
3307
3308/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3309 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3310 preceding the line start. Skip over invisible text that is so
3311 because of selective display. Compute faces, overlays etc at the
3312 new position. Note that this function does not skip over text that
3313 is invisible because of text properties. */
5f5c8ee5
GM
3314
3315static void
312246d1 3316reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3317 struct it *it;
312246d1 3318 int on_newline_p;
5f5c8ee5
GM
3319{
3320 /* Restore the buffer position when currently not delivering display
3321 elements from the current buffer. This is the case, for example,
3322 when called at the end of a truncated overlay string. */
3323 while (it->sp)
3324 pop_it (it);
3325 it->method = next_element_from_buffer;
3326
3327 /* Otherwise, scan_buffer would not work. */
3328 if (IT_CHARPOS (*it) < ZV)
3329 {
3330 /* If on a newline, advance past it. Otherwise, find the next
3331 newline which automatically gives us the position following
3332 the newline. */
3333 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3334 {
3335 ++IT_CHARPOS (*it);
3336 ++IT_BYTEPOS (*it);
3337 }
3338 else
3339 forward_to_next_line_start (it);
3340
3341 /* We must either have reached the end of the buffer or end up
3342 after a newline. */
3343 xassert (IT_CHARPOS (*it) == ZV
3344 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3345
3346 /* Skip over lines that are invisible because they are indented
3347 more than the value of IT->selective. */
3348 if (it->selective > 0)
3349 while (IT_CHARPOS (*it) < ZV
3350 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3351 it->selective))
3352 forward_to_next_line_start (it);
312246d1
GM
3353
3354 /* Position on the newline if we should. */
13f19968
GM
3355 if (on_newline_p
3356 && IT_CHARPOS (*it) > BEGV
3357 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
312246d1
GM
3358 {
3359 --IT_CHARPOS (*it);
3360 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3361 }
5f5c8ee5
GM
3362
3363 /* Set the iterator there. The 0 as the last parameter of
3364 reseat means don't force a text property lookup. The lookup
3365 is then only done if we've skipped past the iterator's
3366 check_charpos'es. This optimization is important because
3367 text property lookups tend to be expensive. */
3368 reseat (it, it->current.pos, 0);
3369 }
3370
3371 CHECK_IT (it);
3372}
3373
3374
3375\f
3376/***********************************************************************
3377 Changing an iterator's position
3378***********************************************************************/
3379
3380/* Change IT's current position to POS in current_buffer. If FORCE_P
3381 is non-zero, always check for text properties at the new position.
3382 Otherwise, text properties are only looked up if POS >=
3383 IT->check_charpos of a property. */
3384
3385static void
3386reseat (it, pos, force_p)
3387 struct it *it;
3388 struct text_pos pos;
3389 int force_p;
3390{
3391 int original_pos = IT_CHARPOS (*it);
3392
3393 reseat_1 (it, pos, 0);
3394
3395 /* Determine where to check text properties. Avoid doing it
3396 where possible because text property lookup is very expensive. */
3397 if (force_p
3398 || CHARPOS (pos) > it->stop_charpos
3399 || CHARPOS (pos) < original_pos)
3400 handle_stop (it);
3401
3402 CHECK_IT (it);
3403}
3404
3405
3406/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3407 IT->stop_pos to POS, also. */
3408
3409static void
3410reseat_1 (it, pos, set_stop_p)
3411 struct it *it;
3412 struct text_pos pos;
3413 int set_stop_p;
3414{
3415 /* Don't call this function when scanning a C string. */
3416 xassert (it->s == NULL);
3417
3418 /* POS must be a reasonable value. */
3419 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3420
3421 it->current.pos = it->position = pos;
3422 XSETBUFFER (it->object, current_buffer);
3423 it->dpvec = NULL;
3424 it->current.dpvec_index = -1;
3425 it->current.overlay_string_index = -1;
3426 IT_STRING_CHARPOS (*it) = -1;
3427 IT_STRING_BYTEPOS (*it) = -1;
3428 it->string = Qnil;
3429 it->method = next_element_from_buffer;
3430 it->sp = 0;
3431
3432 if (set_stop_p)
3433 it->stop_charpos = CHARPOS (pos);
3434}
3435
3436
3437/* Set up IT for displaying a string, starting at CHARPOS in window W.
3438 If S is non-null, it is a C string to iterate over. Otherwise,
3439 STRING gives a Lisp string to iterate over.
3440
3441 If PRECISION > 0, don't return more then PRECISION number of
3442 characters from the string.
3443
3444 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3445 characters have been returned. FIELD_WIDTH < 0 means an infinite
3446 field width.
3447
3448 MULTIBYTE = 0 means disable processing of multibyte characters,
3449 MULTIBYTE > 0 means enable it,
3450 MULTIBYTE < 0 means use IT->multibyte_p.
3451
3452 IT must be initialized via a prior call to init_iterator before
3453 calling this function. */
3454
3455static void
3456reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3457 struct it *it;
3458 unsigned char *s;
3459 Lisp_Object string;
3460 int charpos;
3461 int precision, field_width, multibyte;
3462{
3463 /* No region in strings. */
3464 it->region_beg_charpos = it->region_end_charpos = -1;
3465
3466 /* No text property checks performed by default, but see below. */
3467 it->stop_charpos = -1;
3468
3469 /* Set iterator position and end position. */
3470 bzero (&it->current, sizeof it->current);
3471 it->current.overlay_string_index = -1;
3472 it->current.dpvec_index = -1;
5f5c8ee5
GM
3473 xassert (charpos >= 0);
3474
3475 /* Use the setting of MULTIBYTE if specified. */
3476 if (multibyte >= 0)
3477 it->multibyte_p = multibyte > 0;
3478
3479 if (s == NULL)
3480 {
3481 xassert (STRINGP (string));
3482 it->string = string;
3483 it->s = NULL;
3484 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3485 it->method = next_element_from_string;
3486 it->current.string_pos = string_pos (charpos, string);
3487 }
3488 else
3489 {
3490 it->s = s;
3491 it->string = Qnil;
3492
3493 /* Note that we use IT->current.pos, not it->current.string_pos,
3494 for displaying C strings. */
3495 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3496 if (it->multibyte_p)
3497 {
3498 it->current.pos = c_string_pos (charpos, s, 1);
3499 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3500 }
3501 else
3502 {
3503 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3504 it->end_charpos = it->string_nchars = strlen (s);
3505 }
3506
3507 it->method = next_element_from_c_string;
3508 }
3509
3510 /* PRECISION > 0 means don't return more than PRECISION characters
3511 from the string. */
3512 if (precision > 0 && it->end_charpos - charpos > precision)
3513 it->end_charpos = it->string_nchars = charpos + precision;
3514
3515 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3516 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3517 FIELD_WIDTH < 0 means infinite field width. This is useful for
3518 padding with `-' at the end of a mode line. */
3519 if (field_width < 0)
3520 field_width = INFINITY;
3521 if (field_width > it->end_charpos - charpos)
3522 it->end_charpos = charpos + field_width;
3523
3524 /* Use the standard display table for displaying strings. */
3525 if (DISP_TABLE_P (Vstandard_display_table))
3526 it->dp = XCHAR_TABLE (Vstandard_display_table);
3527
3528 it->stop_charpos = charpos;
3529 CHECK_IT (it);
3530}
3531
3532
3533\f
3534/***********************************************************************
3535 Iteration
3536 ***********************************************************************/
3537
3538/* Load IT's display element fields with information about the next
3539 display element from the current position of IT. Value is zero if
3540 end of buffer (or C string) is reached. */
3541
3542int
3543get_next_display_element (it)
3544 struct it *it;
3545{
3546 /* Non-zero means that we found an display element. Zero means that
3547 we hit the end of what we iterate over. Performance note: the
3548 function pointer `method' used here turns out to be faster than
3549 using a sequence of if-statements. */
3550 int success_p = (*it->method) (it);
5f5c8ee5
GM
3551
3552 if (it->what == IT_CHARACTER)
3553 {
3554 /* Map via display table or translate control characters.
3555 IT->c, IT->len etc. have been set to the next character by
3556 the function call above. If we have a display table, and it
3557 contains an entry for IT->c, translate it. Don't do this if
3558 IT->c itself comes from a display table, otherwise we could
3559 end up in an infinite recursion. (An alternative could be to
3560 count the recursion depth of this function and signal an
3561 error when a certain maximum depth is reached.) Is it worth
3562 it? */
3563 if (success_p && it->dpvec == NULL)
3564 {
3565 Lisp_Object dv;
3566
3567 if (it->dp
3568 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3569 VECTORP (dv)))
3570 {
3571 struct Lisp_Vector *v = XVECTOR (dv);
3572
3573 /* Return the first character from the display table
3574 entry, if not empty. If empty, don't display the
3575 current character. */
3576 if (v->size)
3577 {
3578 it->dpvec_char_len = it->len;
3579 it->dpvec = v->contents;
3580 it->dpend = v->contents + v->size;
3581 it->current.dpvec_index = 0;
3582 it->method = next_element_from_display_vector;
3583 }
3584
3585 success_p = get_next_display_element (it);
3586 }
3587
3588 /* Translate control characters into `\003' or `^C' form.
3589 Control characters coming from a display table entry are
3590 currently not translated because we use IT->dpvec to hold
3591 the translation. This could easily be changed but I
197516c2
KH
3592 don't believe that it is worth doing.
3593
3594 Non-printable multibyte characters are also translated
3595 octal form. */
5f5c8ee5
GM
3596 else if ((it->c < ' '
3597 && (it->area != TEXT_AREA
c6e89d6c 3598 || (it->c != '\n' && it->c != '\t')))
54c85a23 3599 || (it->c >= 127
197516c2
KH
3600 && it->len == 1)
3601 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
3602 {
3603 /* IT->c is a control character which must be displayed
3604 either as '\003' or as `^C' where the '\\' and '^'
3605 can be defined in the display table. Fill
3606 IT->ctl_chars with glyphs for what we have to
3607 display. Then, set IT->dpvec to these glyphs. */
3608 GLYPH g;
3609
54c85a23 3610 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3611 {
3612 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3613 if (it->dp
3614 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3615 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3616 g = XINT (DISP_CTRL_GLYPH (it->dp));
3617 else
3618 g = FAST_MAKE_GLYPH ('^', 0);
3619 XSETINT (it->ctl_chars[0], g);
3620
3621 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3622 XSETINT (it->ctl_chars[1], g);
3623
3624 /* Set up IT->dpvec and return first character from it. */
3625 it->dpvec_char_len = it->len;
3626 it->dpvec = it->ctl_chars;
3627 it->dpend = it->dpvec + 2;
3628 it->current.dpvec_index = 0;
3629 it->method = next_element_from_display_vector;
3630 get_next_display_element (it);
3631 }
3632 else
3633 {
260a86a0 3634 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 3635 int len;
197516c2
KH
3636 int i;
3637 GLYPH escape_glyph;
3638
5f5c8ee5
GM
3639 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3640 if (it->dp
3641 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3642 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 3643 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 3644 else
197516c2
KH
3645 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3646
c5924f47
KH
3647 if (SINGLE_BYTE_CHAR_P (it->c))
3648 str[0] = it->c, len = 1;
3649 else
3650 len = CHAR_STRING (it->c, str);
3651
197516c2
KH
3652 for (i = 0; i < len; i++)
3653 {
3654 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3655 /* Insert three more glyphs into IT->ctl_chars for
3656 the octal display of the character. */
3657 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3658 XSETINT (it->ctl_chars[i * 4 + 1], g);
3659 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3660 XSETINT (it->ctl_chars[i * 4 + 2], g);
3661 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3662 XSETINT (it->ctl_chars[i * 4 + 3], g);
3663 }
5f5c8ee5
GM
3664
3665 /* Set up IT->dpvec and return the first character
3666 from it. */
3667 it->dpvec_char_len = it->len;
3668 it->dpvec = it->ctl_chars;
197516c2 3669 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
3670 it->current.dpvec_index = 0;
3671 it->method = next_element_from_display_vector;
3672 get_next_display_element (it);
3673 }
3674 }
3675 }
3676
980806b6
KH
3677 /* Adjust face id for a multibyte character. There are no
3678 multibyte character in unibyte text. */
5f5c8ee5
GM
3679 if (it->multibyte_p
3680 && success_p
980806b6 3681 && FRAME_WINDOW_P (it->f))
5f5c8ee5 3682 {
980806b6
KH
3683 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3684 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
3685 }
3686 }
3687
3688 /* Is this character the last one of a run of characters with
3689 box? If yes, set IT->end_of_box_run_p to 1. */
3690 if (it->face_box_p
3691 && it->s == NULL)
3692 {
3693 int face_id;
3694 struct face *face;
3695
3696 it->end_of_box_run_p
3697 = ((face_id = face_after_it_pos (it),
3698 face_id != it->face_id)
3699 && (face = FACE_FROM_ID (it->f, face_id),
3700 face->box == FACE_NO_BOX));
3701 }
3702
3703 /* Value is 0 if end of buffer or string reached. */
3704 return success_p;
3705}
3706
3707
3708/* Move IT to the next display element.
3709
3710 Functions get_next_display_element and set_iterator_to_next are
3711 separate because I find this arrangement easier to handle than a
3712 get_next_display_element function that also increments IT's
3713 position. The way it is we can first look at an iterator's current
3714 display element, decide whether it fits on a line, and if it does,
3715 increment the iterator position. The other way around we probably
3716 would either need a flag indicating whether the iterator has to be
3717 incremented the next time, or we would have to implement a
3718 decrement position function which would not be easy to write. */
3719
3720void
3721set_iterator_to_next (it)
3722 struct it *it;
3723{
3724 if (it->method == next_element_from_buffer)
3725 {
3726 /* The current display element of IT is a character from
3727 current_buffer. Advance in the buffer, and maybe skip over
3728 invisible lines that are so because of selective display. */
3729 if (ITERATOR_AT_END_OF_LINE_P (it))
312246d1 3730 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
3731 else
3732 {
3733 xassert (it->len != 0);
3734 IT_BYTEPOS (*it) += it->len;
3735 IT_CHARPOS (*it) += 1;
3736 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3737 }
3738 }
260a86a0
KH
3739 else if (it->method == next_element_from_composition)
3740 {
3741 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3742 if (STRINGP (it->string))
3743 {
3744 IT_STRING_BYTEPOS (*it) += it->len;
3745 IT_STRING_CHARPOS (*it) += it->cmp_len;
3746 it->method = next_element_from_string;
3747 goto consider_string_end;
3748 }
3749 else
3750 {
3751 IT_BYTEPOS (*it) += it->len;
3752 IT_CHARPOS (*it) += it->cmp_len;
3753 it->method = next_element_from_buffer;
3754 }
3755 }
5f5c8ee5
GM
3756 else if (it->method == next_element_from_c_string)
3757 {
3758 /* Current display element of IT is from a C string. */
3759 IT_BYTEPOS (*it) += it->len;
3760 IT_CHARPOS (*it) += 1;
3761 }
3762 else if (it->method == next_element_from_display_vector)
3763 {
3764 /* Current display element of IT is from a display table entry.
3765 Advance in the display table definition. Reset it to null if
3766 end reached, and continue with characters from buffers/
3767 strings. */
3768 ++it->current.dpvec_index;
286bcbc9 3769
980806b6
KH
3770 /* Restore face of the iterator to what they were before the
3771 display vector entry (these entries may contain faces). */
5f5c8ee5 3772 it->face_id = it->saved_face_id;
286bcbc9 3773
5f5c8ee5
GM
3774 if (it->dpvec + it->current.dpvec_index == it->dpend)
3775 {
3776 if (it->s)
3777 it->method = next_element_from_c_string;
3778 else if (STRINGP (it->string))
3779 it->method = next_element_from_string;
3780 else
3781 it->method = next_element_from_buffer;
3782
3783 it->dpvec = NULL;
3784 it->current.dpvec_index = -1;
3785
312246d1
GM
3786 /* Skip over characters which were displayed via IT->dpvec. */
3787 if (it->dpvec_char_len < 0)
3788 reseat_at_next_visible_line_start (it, 1);
3789 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
3790 {
3791 it->len = it->dpvec_char_len;
3792 set_iterator_to_next (it);
3793 }
3794 }
3795 }
3796 else if (it->method == next_element_from_string)
3797 {
3798 /* Current display element is a character from a Lisp string. */
3799 xassert (it->s == NULL && STRINGP (it->string));
3800 IT_STRING_BYTEPOS (*it) += it->len;
3801 IT_STRING_CHARPOS (*it) += 1;
3802
3803 consider_string_end:
3804
3805 if (it->current.overlay_string_index >= 0)
3806 {
3807 /* IT->string is an overlay string. Advance to the
3808 next, if there is one. */
3809 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3810 next_overlay_string (it);
3811 }
3812 else
3813 {
3814 /* IT->string is not an overlay string. If we reached
3815 its end, and there is something on IT->stack, proceed
3816 with what is on the stack. This can be either another
3817 string, this time an overlay string, or a buffer. */
3818 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3819 && it->sp > 0)
3820 {
3821 pop_it (it);
3822 if (!STRINGP (it->string))
3823 it->method = next_element_from_buffer;
3824 }
3825 }
3826 }
3827 else if (it->method == next_element_from_image
3828 || it->method == next_element_from_stretch)
3829 {
3830 /* The position etc with which we have to proceed are on
3831 the stack. The position may be at the end of a string,
3832 if the `display' property takes up the whole string. */
3833 pop_it (it);
3834 it->image_id = 0;
3835 if (STRINGP (it->string))
3836 {
3837 it->method = next_element_from_string;
3838 goto consider_string_end;
3839 }
3840 else
3841 it->method = next_element_from_buffer;
3842 }
3843 else
3844 /* There are no other methods defined, so this should be a bug. */
3845 abort ();
3846
3847 /* Reset flags indicating start and end of a sequence of
3848 characters with box. */
3849 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3850
3851 xassert (it->method != next_element_from_string
3852 || (STRINGP (it->string)
3853 && IT_STRING_CHARPOS (*it) >= 0));
3854}
3855
3856
3857/* Load IT's display element fields with information about the next
3858 display element which comes from a display table entry or from the
3859 result of translating a control character to one of the forms `^C'
3860 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3861
3862static int
3863next_element_from_display_vector (it)
3864 struct it *it;
3865{
3866 /* Precondition. */
3867 xassert (it->dpvec && it->current.dpvec_index >= 0);
3868
3869 /* Remember the current face id in case glyphs specify faces.
3870 IT's face is restored in set_iterator_to_next. */
3871 it->saved_face_id = it->face_id;
3872
3873 if (INTEGERP (*it->dpvec)
3874 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3875 {
3876 int lface_id;
3877 GLYPH g;
3878
3879 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3880 it->c = FAST_GLYPH_CHAR (g);
c5924f47 3881 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
3882
3883 /* The entry may contain a face id to use. Such a face id is
3884 the id of a Lisp face, not a realized face. A face id of
969065c3 3885 zero means no face is specified. */
5f5c8ee5
GM
3886 lface_id = FAST_GLYPH_FACE (g);
3887 if (lface_id)
3888 {
969065c3 3889 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
3890 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3891 if (face_id >= 0)
969065c3 3892 it->face_id = face_id;
5f5c8ee5
GM
3893 }
3894 }
3895 else
3896 /* Display table entry is invalid. Return a space. */
3897 it->c = ' ', it->len = 1;
3898
3899 /* Don't change position and object of the iterator here. They are
3900 still the values of the character that had this display table
3901 entry or was translated, and that's what we want. */
3902 it->what = IT_CHARACTER;
3903 return 1;
3904}
3905
3906
3907/* Load IT with the next display element from Lisp string IT->string.
3908 IT->current.string_pos is the current position within the string.
3909 If IT->current.overlay_string_index >= 0, the Lisp string is an
3910 overlay string. */
3911
3912static int
3913next_element_from_string (it)
3914 struct it *it;
3915{
3916 struct text_pos position;
3917
3918 xassert (STRINGP (it->string));
3919 xassert (IT_STRING_CHARPOS (*it) >= 0);
3920 position = it->current.string_pos;
3921
3922 /* Time to check for invisible text? */
3923 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3924 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3925 {
3926 handle_stop (it);
3927
3928 /* Since a handler may have changed IT->method, we must
3929 recurse here. */
3930 return get_next_display_element (it);
3931 }
3932
3933 if (it->current.overlay_string_index >= 0)
3934 {
3935 /* Get the next character from an overlay string. In overlay
3936 strings, There is no field width or padding with spaces to
3937 do. */
3938 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3939 {
3940 it->what = IT_EOB;
3941 return 0;
3942 }
3943 else if (STRING_MULTIBYTE (it->string))
3944 {
3945 int remaining = (STRING_BYTES (XSTRING (it->string))
3946 - IT_STRING_BYTEPOS (*it));
3947 unsigned char *s = (XSTRING (it->string)->data
3948 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3949 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
3950 }
3951 else
3952 {
3953 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3954 it->len = 1;
3955 }
3956 }
3957 else
3958 {
3959 /* Get the next character from a Lisp string that is not an
3960 overlay string. Such strings come from the mode line, for
3961 example. We may have to pad with spaces, or truncate the
3962 string. See also next_element_from_c_string. */
3963 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3964 {
3965 it->what = IT_EOB;
3966 return 0;
3967 }
3968 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3969 {
3970 /* Pad with spaces. */
3971 it->c = ' ', it->len = 1;
3972 CHARPOS (position) = BYTEPOS (position) = -1;
3973 }
3974 else if (STRING_MULTIBYTE (it->string))
3975 {
3976 int maxlen = (STRING_BYTES (XSTRING (it->string))
3977 - IT_STRING_BYTEPOS (*it));
3978 unsigned char *s = (XSTRING (it->string)->data
3979 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3980 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
3981 }
3982 else
3983 {
3984 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3985 it->len = 1;
3986 }
3987 }
3988
3989 /* Record what we have and where it came from. Note that we store a
3990 buffer position in IT->position although it could arguably be a
3991 string position. */
3992 it->what = IT_CHARACTER;
3993 it->object = it->string;
3994 it->position = position;
3995 return 1;
3996}
3997
3998
3999/* Load IT with next display element from C string IT->s.
4000 IT->string_nchars is the maximum number of characters to return
4001 from the string. IT->end_charpos may be greater than
4002 IT->string_nchars when this function is called, in which case we
4003 may have to return padding spaces. Value is zero if end of string
4004 reached, including padding spaces. */
4005
4006static int
4007next_element_from_c_string (it)
4008 struct it *it;
4009{
4010 int success_p = 1;
4011
4012 xassert (it->s);
4013 it->what = IT_CHARACTER;
4014 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4015 it->object = Qnil;
4016
4017 /* IT's position can be greater IT->string_nchars in case a field
4018 width or precision has been specified when the iterator was
4019 initialized. */
4020 if (IT_CHARPOS (*it) >= it->end_charpos)
4021 {
4022 /* End of the game. */
4023 it->what = IT_EOB;
4024 success_p = 0;
4025 }
4026 else if (IT_CHARPOS (*it) >= it->string_nchars)
4027 {
4028 /* Pad with spaces. */
4029 it->c = ' ', it->len = 1;
4030 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4031 }
4032 else if (it->multibyte_p)
4033 {
4034 /* Implementation note: The calls to strlen apparently aren't a
4035 performance problem because there is no noticeable performance
4036 difference between Emacs running in unibyte or multibyte mode. */
4037 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4038 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4039 maxlen, &it->len);
5f5c8ee5
GM
4040 }
4041 else
4042 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4043
4044 return success_p;
4045}
4046
4047
4048/* Set up IT to return characters from an ellipsis, if appropriate.
4049 The definition of the ellipsis glyphs may come from a display table
4050 entry. This function Fills IT with the first glyph from the
4051 ellipsis if an ellipsis is to be displayed. */
4052
13f19968 4053static int
5f5c8ee5
GM
4054next_element_from_ellipsis (it)
4055 struct it *it;
4056{
13f19968 4057 if (it->selective_display_ellipsis_p)
5f5c8ee5 4058 {
13f19968
GM
4059 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4060 {
4061 /* Use the display table definition for `...'. Invalid glyphs
4062 will be handled by the method returning elements from dpvec. */
4063 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4064 it->dpvec_char_len = it->len;
4065 it->dpvec = v->contents;
4066 it->dpend = v->contents + v->size;
4067 it->current.dpvec_index = 0;
4068 it->method = next_element_from_display_vector;
4069 }
4070 else
4071 {
4072 /* Use default `...' which is stored in default_invis_vector. */
4073 it->dpvec_char_len = it->len;
4074 it->dpvec = default_invis_vector;
4075 it->dpend = default_invis_vector + 3;
4076 it->current.dpvec_index = 0;
4077 it->method = next_element_from_display_vector;
4078 }
5f5c8ee5 4079 }
13f19968
GM
4080 else
4081 reseat_at_next_visible_line_start (it, 1);
4082
4083 return get_next_display_element (it);
5f5c8ee5
GM
4084}
4085
4086
4087/* Deliver an image display element. The iterator IT is already
4088 filled with image information (done in handle_display_prop). Value
4089 is always 1. */
4090
4091
4092static int
4093next_element_from_image (it)
4094 struct it *it;
4095{
4096 it->what = IT_IMAGE;
4097 return 1;
4098}
4099
4100
4101/* Fill iterator IT with next display element from a stretch glyph
4102 property. IT->object is the value of the text property. Value is
4103 always 1. */
4104
4105static int
4106next_element_from_stretch (it)
4107 struct it *it;
4108{
4109 it->what = IT_STRETCH;
4110 return 1;
4111}
4112
4113
4114/* Load IT with the next display element from current_buffer. Value
4115 is zero if end of buffer reached. IT->stop_charpos is the next
4116 position at which to stop and check for text properties or buffer
4117 end. */
4118
4119static int
4120next_element_from_buffer (it)
4121 struct it *it;
4122{
4123 int success_p = 1;
4124
4125 /* Check this assumption, otherwise, we would never enter the
4126 if-statement, below. */
4127 xassert (IT_CHARPOS (*it) >= BEGV
4128 && IT_CHARPOS (*it) <= it->stop_charpos);
4129
4130 if (IT_CHARPOS (*it) >= it->stop_charpos)
4131 {
4132 if (IT_CHARPOS (*it) >= it->end_charpos)
4133 {
4134 int overlay_strings_follow_p;
4135
4136 /* End of the game, except when overlay strings follow that
4137 haven't been returned yet. */
4138 if (it->overlay_strings_at_end_processed_p)
4139 overlay_strings_follow_p = 0;
4140 else
4141 {
4142 it->overlay_strings_at_end_processed_p = 1;
c880678e 4143 overlay_strings_follow_p = get_overlay_strings (it);
5f5c8ee5
GM
4144 }
4145
4146 if (overlay_strings_follow_p)
4147 success_p = get_next_display_element (it);
4148 else
4149 {
4150 it->what = IT_EOB;
4151 it->position = it->current.pos;
4152 success_p = 0;
4153 }
4154 }
4155 else
4156 {
4157 handle_stop (it);
4158 return get_next_display_element (it);
4159 }
4160 }
4161 else
4162 {
4163 /* No face changes, overlays etc. in sight, so just return a
4164 character from current_buffer. */
4165 unsigned char *p;
4166
4167 /* Maybe run the redisplay end trigger hook. Performance note:
4168 This doesn't seem to cost measurable time. */
4169 if (it->redisplay_end_trigger_charpos
4170 && it->glyph_row
4171 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4172 run_redisplay_end_trigger_hook (it);
4173
4174 /* Get the next character, maybe multibyte. */
4175 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4176 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4177 {
4178 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4179 - IT_BYTEPOS (*it));
4fdb80f2 4180 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4181 }
4182 else
4183 it->c = *p, it->len = 1;
4184
4185 /* Record what we have and where it came from. */
4186 it->what = IT_CHARACTER;;
4187 it->object = it->w->buffer;
4188 it->position = it->current.pos;
4189
4190 /* Normally we return the character found above, except when we
4191 really want to return an ellipsis for selective display. */
4192 if (it->selective)
4193 {
4194 if (it->c == '\n')
4195 {
4196 /* A value of selective > 0 means hide lines indented more
4197 than that number of columns. */
4198 if (it->selective > 0
4199 && IT_CHARPOS (*it) + 1 < ZV
4200 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4201 IT_BYTEPOS (*it) + 1,
4202 it->selective))
312246d1 4203 {
13f19968 4204 success_p = next_element_from_ellipsis (it);
312246d1
GM
4205 it->dpvec_char_len = -1;
4206 }
5f5c8ee5
GM
4207 }
4208 else if (it->c == '\r' && it->selective == -1)
4209 {
4210 /* A value of selective == -1 means that everything from the
4211 CR to the end of the line is invisible, with maybe an
4212 ellipsis displayed for it. */
13f19968 4213 success_p = next_element_from_ellipsis (it);
312246d1 4214 it->dpvec_char_len = -1;
5f5c8ee5
GM
4215 }
4216 }
4217 }
4218
4219 /* Value is zero if end of buffer reached. */
c880678e 4220 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4221 return success_p;
4222}
4223
4224
4225/* Run the redisplay end trigger hook for IT. */
4226
4227static void
4228run_redisplay_end_trigger_hook (it)
4229 struct it *it;
4230{
4231 Lisp_Object args[3];
4232
4233 /* IT->glyph_row should be non-null, i.e. we should be actually
4234 displaying something, or otherwise we should not run the hook. */
4235 xassert (it->glyph_row);
4236
4237 /* Set up hook arguments. */
4238 args[0] = Qredisplay_end_trigger_functions;
4239 args[1] = it->window;
4240 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4241 it->redisplay_end_trigger_charpos = 0;
4242
4243 /* Since we are *trying* to run these functions, don't try to run
4244 them again, even if they get an error. */
4245 it->w->redisplay_end_trigger = Qnil;
4246 Frun_hook_with_args (3, args);
4247
4248 /* Notice if it changed the face of the character we are on. */
4249 handle_face_prop (it);
4250}
4251
4252
260a86a0
KH
4253/* Deliver a composition display element. The iterator IT is already
4254 filled with composition information (done in
4255 handle_composition_prop). Value is always 1. */
4256
4257static int
4258next_element_from_composition (it)
4259 struct it *it;
4260{
4261 it->what = IT_COMPOSITION;
4262 it->position = (STRINGP (it->string)
4263 ? it->current.string_pos
4264 : it->current.pos);
4265 return 1;
4266}
4267
4268
5f5c8ee5
GM
4269\f
4270/***********************************************************************
4271 Moving an iterator without producing glyphs
4272 ***********************************************************************/
4273
4274/* Move iterator IT to a specified buffer or X position within one
4275 line on the display without producing glyphs.
4276
4277 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4278 whichever is reached first.
4279
4280 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4281
4282 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4283 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4284 TO_X includes the amount by which a window is horizontally
4285 scrolled.
4286
4287 Value is
4288
4289 MOVE_POS_MATCH_OR_ZV
4290 - when TO_POS or ZV was reached.
4291
4292 MOVE_X_REACHED
4293 -when TO_X was reached before TO_POS or ZV were reached.
4294
4295 MOVE_LINE_CONTINUED
4296 - when we reached the end of the display area and the line must
4297 be continued.
4298
4299 MOVE_LINE_TRUNCATED
4300 - when we reached the end of the display area and the line is
4301 truncated.
4302
4303 MOVE_NEWLINE_OR_CR
4304 - when we stopped at a line end, i.e. a newline or a CR and selective
4305 display is on. */
4306
701552dd 4307static enum move_it_result
5f5c8ee5
GM
4308move_it_in_display_line_to (it, to_charpos, to_x, op)
4309 struct it *it;
4310 int to_charpos, to_x, op;
4311{
4312 enum move_it_result result = MOVE_UNDEFINED;
4313 struct glyph_row *saved_glyph_row;
4314
4315 /* Don't produce glyphs in produce_glyphs. */
4316 saved_glyph_row = it->glyph_row;
4317 it->glyph_row = NULL;
4318
5f5c8ee5
GM
4319 while (1)
4320 {
ae26e27d 4321 int x, i, ascent = 0, descent = 0;
5f5c8ee5
GM
4322
4323 /* Stop when ZV or TO_CHARPOS reached. */
4324 if (!get_next_display_element (it)
4325 || ((op & MOVE_TO_POS) != 0
4326 && BUFFERP (it->object)
4327 && IT_CHARPOS (*it) >= to_charpos))
4328 {
4329 result = MOVE_POS_MATCH_OR_ZV;
4330 break;
4331 }
4332
4333 /* The call to produce_glyphs will get the metrics of the
4334 display element IT is loaded with. We record in x the
4335 x-position before this display element in case it does not
4336 fit on the line. */
4337 x = it->current_x;
47589c8c
GM
4338
4339 /* Remember the line height so far in case the next element doesn't
4340 fit on the line. */
4341 if (!it->truncate_lines_p)
4342 {
4343 ascent = it->max_ascent;
4344 descent = it->max_descent;
4345 }
4346
5f5c8ee5
GM
4347 PRODUCE_GLYPHS (it);
4348
4349 if (it->area != TEXT_AREA)
4350 {
4351 set_iterator_to_next (it);
4352 continue;
4353 }
4354
4355 /* The number of glyphs we get back in IT->nglyphs will normally
4356 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4357 character on a terminal frame, or (iii) a line end. For the
4358 second case, IT->nglyphs - 1 padding glyphs will be present
4359 (on X frames, there is only one glyph produced for a
4360 composite character.
4361
4362 The behavior implemented below means, for continuation lines,
4363 that as many spaces of a TAB as fit on the current line are
4364 displayed there. For terminal frames, as many glyphs of a
4365 multi-glyph character are displayed in the current line, too.
4366 This is what the old redisplay code did, and we keep it that
4367 way. Under X, the whole shape of a complex character must
4368 fit on the line or it will be completely displayed in the
4369 next line.
4370
4371 Note that both for tabs and padding glyphs, all glyphs have
4372 the same width. */
4373 if (it->nglyphs)
4374 {
4375 /* More than one glyph or glyph doesn't fit on line. All
4376 glyphs have the same width. */
4377 int single_glyph_width = it->pixel_width / it->nglyphs;
4378 int new_x;
4379
4380 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4381 {
4382 new_x = x + single_glyph_width;
4383
4384 /* We want to leave anything reaching TO_X to the caller. */
4385 if ((op & MOVE_TO_X) && new_x > to_x)
4386 {
4387 it->current_x = x;
4388 result = MOVE_X_REACHED;
4389 break;
4390 }
4391 else if (/* Lines are continued. */
4392 !it->truncate_lines_p
4393 && (/* And glyph doesn't fit on the line. */
4394 new_x > it->last_visible_x
4395 /* Or it fits exactly and we're on a window
4396 system frame. */
4397 || (new_x == it->last_visible_x
4398 && FRAME_WINDOW_P (it->f))))
4399 {
4400 if (/* IT->hpos == 0 means the very first glyph
4401 doesn't fit on the line, e.g. a wide image. */
4402 it->hpos == 0
4403 || (new_x == it->last_visible_x
4404 && FRAME_WINDOW_P (it->f)))
4405 {
4406 ++it->hpos;
4407 it->current_x = new_x;
4408 if (i == it->nglyphs - 1)
4409 set_iterator_to_next (it);
4410 }
4411 else
47589c8c
GM
4412 {
4413 it->current_x = x;
4414 it->max_ascent = ascent;
4415 it->max_descent = descent;
4416 }
4417
4418 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4419 IT_CHARPOS (*it)));
5f5c8ee5
GM
4420 result = MOVE_LINE_CONTINUED;
4421 break;
4422 }
4423 else if (new_x > it->first_visible_x)
4424 {
4425 /* Glyph is visible. Increment number of glyphs that
4426 would be displayed. */
4427 ++it->hpos;
4428 }
4429 else
4430 {
4431 /* Glyph is completely off the left margin of the display
4432 area. Nothing to do. */
4433 }
4434 }
4435
4436 if (result != MOVE_UNDEFINED)
4437 break;
4438 }
4439 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4440 {
4441 /* Stop when TO_X specified and reached. This check is
4442 necessary here because of lines consisting of a line end,
4443 only. The line end will not produce any glyphs and we
4444 would never get MOVE_X_REACHED. */
4445 xassert (it->nglyphs == 0);
4446 result = MOVE_X_REACHED;
4447 break;
4448 }
4449
4450 /* Is this a line end? If yes, we're done. */
4451 if (ITERATOR_AT_END_OF_LINE_P (it))
4452 {
4453 result = MOVE_NEWLINE_OR_CR;
4454 break;
4455 }
4456
4457 /* The current display element has been consumed. Advance
4458 to the next. */
4459 set_iterator_to_next (it);
4460
4461 /* Stop if lines are truncated and IT's current x-position is
4462 past the right edge of the window now. */
4463 if (it->truncate_lines_p
4464 && it->current_x >= it->last_visible_x)
4465 {
4466 result = MOVE_LINE_TRUNCATED;
4467 break;
4468 }
4469 }
4470
4471 /* Restore the iterator settings altered at the beginning of this
4472 function. */
4473 it->glyph_row = saved_glyph_row;
4474 return result;
4475}
4476
4477
4478/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4479 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4480 the description of enum move_operation_enum.
4481
4482 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4483 screen line, this function will set IT to the next position >
4484 TO_CHARPOS. */
4485
4486void
4487move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4488 struct it *it;
4489 int to_charpos, to_x, to_y, to_vpos;
4490 int op;
4491{
4492 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4493 int line_height;
47589c8c 4494 int reached = 0;
5f5c8ee5 4495
47589c8c 4496 for (;;)
5f5c8ee5
GM
4497 {
4498 if (op & MOVE_TO_VPOS)
4499 {
4500 /* If no TO_CHARPOS and no TO_X specified, stop at the
4501 start of the line TO_VPOS. */
4502 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4503 {
4504 if (it->vpos == to_vpos)
47589c8c
GM
4505 {
4506 reached = 1;
4507 break;
4508 }
4509 else
4510 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
4511 }
4512 else
4513 {
4514 /* TO_VPOS >= 0 means stop at TO_X in the line at
4515 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
4516 if (it->vpos == to_vpos)
4517 {
4518 reached = 2;
4519 break;
4520 }
4521
5f5c8ee5
GM
4522 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4523
4524 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
4525 {
4526 reached = 3;
4527 break;
4528 }
5f5c8ee5
GM
4529 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4530 {
4531 /* We have reached TO_X but not in the line we want. */
4532 skip = move_it_in_display_line_to (it, to_charpos,
4533 -1, MOVE_TO_POS);
4534 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
4535 {
4536 reached = 4;
4537 break;
4538 }
5f5c8ee5
GM
4539 }
4540 }
4541 }
4542 else if (op & MOVE_TO_Y)
4543 {
4544 struct it it_backup;
5f5c8ee5
GM
4545
4546 /* TO_Y specified means stop at TO_X in the line containing
4547 TO_Y---or at TO_CHARPOS if this is reached first. The
4548 problem is that we can't really tell whether the line
4549 contains TO_Y before we have completely scanned it, and
4550 this may skip past TO_X. What we do is to first scan to
4551 TO_X.
4552
4553 If TO_X is not specified, use a TO_X of zero. The reason
4554 is to make the outcome of this function more predictable.
4555 If we didn't use TO_X == 0, we would stop at the end of
4556 the line which is probably not what a caller would expect
4557 to happen. */
4558 skip = move_it_in_display_line_to (it, to_charpos,
4559 ((op & MOVE_TO_X)
4560 ? to_x : 0),
4561 (MOVE_TO_X
4562 | (op & MOVE_TO_POS)));
4563
4564 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4565 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
4566 {
4567 reached = 5;
4568 break;
4569 }
5f5c8ee5
GM
4570
4571 /* If TO_X was reached, we would like to know whether TO_Y
4572 is in the line. This can only be said if we know the
4573 total line height which requires us to scan the rest of
4574 the line. */
5f5c8ee5
GM
4575 if (skip == MOVE_X_REACHED)
4576 {
4577 it_backup = *it;
47589c8c 4578 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
4579 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4580 op & MOVE_TO_POS);
47589c8c 4581 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
4582 }
4583
4584 /* Now, decide whether TO_Y is in this line. */
4585 line_height = it->max_ascent + it->max_descent;
47589c8c 4586 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5f5c8ee5
GM
4587
4588 if (to_y >= it->current_y
4589 && to_y < it->current_y + line_height)
4590 {
4591 if (skip == MOVE_X_REACHED)
4592 /* If TO_Y is in this line and TO_X was reached above,
4593 we scanned too far. We have to restore IT's settings
4594 to the ones before skipping. */
4595 *it = it_backup;
47589c8c 4596 reached = 6;
5f5c8ee5
GM
4597 }
4598 else if (skip == MOVE_X_REACHED)
4599 {
4600 skip = skip2;
4601 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 4602 reached = 7;
5f5c8ee5
GM
4603 }
4604
47589c8c 4605 if (reached)
5f5c8ee5
GM
4606 break;
4607 }
4608 else
4609 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4610
4611 switch (skip)
4612 {
4613 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
4614 reached = 8;
4615 goto out;
5f5c8ee5
GM
4616
4617 case MOVE_NEWLINE_OR_CR:
4618 set_iterator_to_next (it);
4619 it->continuation_lines_width = 0;
4620 break;
4621
4622 case MOVE_LINE_TRUNCATED:
4623 it->continuation_lines_width = 0;
312246d1 4624 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4625 if ((op & MOVE_TO_POS) != 0
4626 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
4627 {
4628 reached = 9;
4629 goto out;
4630 }
5f5c8ee5
GM
4631 break;
4632
4633 case MOVE_LINE_CONTINUED:
4634 it->continuation_lines_width += it->current_x;
4635 break;
4636
4637 default:
4638 abort ();
4639 }
4640
4641 /* Reset/increment for the next run. */
4642 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4643 it->current_x = it->hpos = 0;
4644 it->current_y += it->max_ascent + it->max_descent;
4645 ++it->vpos;
4646 last_height = it->max_ascent + it->max_descent;
4647 last_max_ascent = it->max_ascent;
4648 it->max_ascent = it->max_descent = 0;
4649 }
47589c8c
GM
4650
4651 out:
4652
4653 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
4654}
4655
4656
4657/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4658
4659 If DY > 0, move IT backward at least that many pixels. DY = 0
4660 means move IT backward to the preceding line start or BEGV. This
4661 function may move over more than DY pixels if IT->current_y - DY
4662 ends up in the middle of a line; in this case IT->current_y will be
4663 set to the top of the line moved to. */
4664
4665void
4666move_it_vertically_backward (it, dy)
4667 struct it *it;
4668 int dy;
4669{
4670 int nlines, h, line_height;
4671 struct it it2;
4672 int start_pos = IT_CHARPOS (*it);
4673
4674 xassert (dy >= 0);
4675
4676 /* Estimate how many newlines we must move back. */
4677 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4678
4679 /* Set the iterator's position that many lines back. */
4680 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4681 back_to_previous_visible_line_start (it);
4682
4683 /* Reseat the iterator here. When moving backward, we don't want
4684 reseat to skip forward over invisible text, set up the iterator
4685 to deliver from overlay strings at the new position etc. So,
4686 use reseat_1 here. */
4687 reseat_1 (it, it->current.pos, 1);
4688
4689 /* We are now surely at a line start. */
4690 it->current_x = it->hpos = 0;
4691
4692 /* Move forward and see what y-distance we moved. First move to the
4693 start of the next line so that we get its height. We need this
4694 height to be able to tell whether we reached the specified
4695 y-distance. */
4696 it2 = *it;
4697 it2.max_ascent = it2.max_descent = 0;
4698 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4699 MOVE_TO_POS | MOVE_TO_VPOS);
4700 xassert (IT_CHARPOS (*it) >= BEGV);
4701 line_height = it2.max_ascent + it2.max_descent;
47589c8c 4702
5f5c8ee5
GM
4703 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4704 xassert (IT_CHARPOS (*it) >= BEGV);
4705 h = it2.current_y - it->current_y;
4706 nlines = it2.vpos - it->vpos;
4707
4708 /* Correct IT's y and vpos position. */
4709 it->vpos -= nlines;
4710 it->current_y -= h;
4711
4712 if (dy == 0)
4713 {
4714 /* DY == 0 means move to the start of the screen line. The
4715 value of nlines is > 0 if continuation lines were involved. */
4716 if (nlines > 0)
4717 move_it_by_lines (it, nlines, 1);
4718 xassert (IT_CHARPOS (*it) <= start_pos);
4719 }
4720 else if (nlines)
4721 {
4722 /* The y-position we try to reach. Note that h has been
4723 subtracted in front of the if-statement. */
4724 int target_y = it->current_y + h - dy;
4725
4726 /* If we did not reach target_y, try to move further backward if
4727 we can. If we moved too far backward, try to move forward. */
4728 if (target_y < it->current_y
4729 && IT_CHARPOS (*it) > BEGV)
4730 {
4731 move_it_vertically (it, target_y - it->current_y);
4732 xassert (IT_CHARPOS (*it) >= BEGV);
4733 }
4734 else if (target_y >= it->current_y + line_height
4735 && IT_CHARPOS (*it) < ZV)
4736 {
4737 move_it_vertically (it, target_y - (it->current_y + line_height));
4738 xassert (IT_CHARPOS (*it) >= BEGV);
4739 }
4740 }
4741}
4742
4743
4744/* Move IT by a specified amount of pixel lines DY. DY negative means
4745 move backwards. DY = 0 means move to start of screen line. At the
4746 end, IT will be on the start of a screen line. */
4747
4748void
4749move_it_vertically (it, dy)
4750 struct it *it;
4751 int dy;
4752{
4753 if (dy <= 0)
4754 move_it_vertically_backward (it, -dy);
4755 else if (dy > 0)
4756 {
47589c8c 4757 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
4758 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4759 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 4760 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
4761
4762 /* If buffer ends in ZV without a newline, move to the start of
4763 the line to satisfy the post-condition. */
4764 if (IT_CHARPOS (*it) == ZV
4765 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4766 move_it_by_lines (it, 0, 0);
4767 }
4768}
4769
4770
4771/* Return non-zero if some text between buffer positions START_CHARPOS
4772 and END_CHARPOS is invisible. IT->window is the window for text
4773 property lookup. */
4774
4775static int
4776invisible_text_between_p (it, start_charpos, end_charpos)
4777 struct it *it;
4778 int start_charpos, end_charpos;
4779{
5f5c8ee5
GM
4780 Lisp_Object prop, limit;
4781 int invisible_found_p;
4782
4783 xassert (it != NULL && start_charpos <= end_charpos);
4784
4785 /* Is text at START invisible? */
4786 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4787 it->window);
4788 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4789 invisible_found_p = 1;
4790 else
4791 {
016b5642
MB
4792 limit = Fnext_single_char_property_change (make_number (start_charpos),
4793 Qinvisible, Qnil,
4794 make_number (end_charpos));
5f5c8ee5
GM
4795 invisible_found_p = XFASTINT (limit) < end_charpos;
4796 }
4797
4798 return invisible_found_p;
5f5c8ee5
GM
4799}
4800
4801
4802/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4803 negative means move up. DVPOS == 0 means move to the start of the
4804 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4805 NEED_Y_P is zero, IT->current_y will be left unchanged.
4806
4807 Further optimization ideas: If we would know that IT->f doesn't use
4808 a face with proportional font, we could be faster for
4809 truncate-lines nil. */
4810
4811void
4812move_it_by_lines (it, dvpos, need_y_p)
4813 struct it *it;
4814 int dvpos, need_y_p;
4815{
4816 struct position pos;
4817
4818 if (!FRAME_WINDOW_P (it->f))
4819 {
4820 struct text_pos textpos;
4821
4822 /* We can use vmotion on frames without proportional fonts. */
4823 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4824 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4825 reseat (it, textpos, 1);
4826 it->vpos += pos.vpos;
4827 it->current_y += pos.vpos;
4828 }
4829 else if (dvpos == 0)
4830 {
4831 /* DVPOS == 0 means move to the start of the screen line. */
4832 move_it_vertically_backward (it, 0);
4833 xassert (it->current_x == 0 && it->hpos == 0);
4834 }
4835 else if (dvpos > 0)
4836 {
4837 /* If there are no continuation lines, and if there is no
4838 selective display, try the simple method of moving forward
4839 DVPOS newlines, then see where we are. */
4840 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4841 {
4842 int shortage = 0, charpos;
4843
4844 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4845 charpos = IT_CHARPOS (*it) + 1;
4846 else
4847 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4848 &shortage, 0);
4849
4850 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4851 {
4852 struct text_pos pos;
4853 CHARPOS (pos) = charpos;
4854 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4855 reseat (it, pos, 1);
4856 it->vpos += dvpos - shortage;
4857 it->hpos = it->current_x = 0;
4858 return;
4859 }
4860 }
4861
4862 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4863 }
4864 else
4865 {
4866 struct it it2;
4867 int start_charpos, i;
4868
4869 /* If there are no continuation lines, and if there is no
4870 selective display, try the simple method of moving backward
4871 -DVPOS newlines. */
4872 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4873 {
4874 int shortage;
4875 int charpos = IT_CHARPOS (*it);
4876 int bytepos = IT_BYTEPOS (*it);
4877
4878 /* If in the middle of a line, go to its start. */
4879 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4880 {
4881 charpos = find_next_newline_no_quit (charpos, -1);
4882 bytepos = CHAR_TO_BYTE (charpos);
4883 }
4884
4885 if (charpos == BEGV)
4886 {
4887 struct text_pos pos;
4888 CHARPOS (pos) = charpos;
4889 BYTEPOS (pos) = bytepos;
4890 reseat (it, pos, 1);
4891 it->hpos = it->current_x = 0;
4892 return;
4893 }
4894 else
4895 {
4896 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4897 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4898 {
4899 struct text_pos pos;
4900 CHARPOS (pos) = charpos;
4901 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4902 reseat (it, pos, 1);
4903 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4904 it->hpos = it->current_x = 0;
4905 return;
4906 }
4907 }
4908 }
4909
4910 /* Go back -DVPOS visible lines and reseat the iterator there. */
4911 start_charpos = IT_CHARPOS (*it);
4912 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4913 back_to_previous_visible_line_start (it);
4914 reseat (it, it->current.pos, 1);
4915 it->current_x = it->hpos = 0;
4916
4917 /* Above call may have moved too far if continuation lines
4918 are involved. Scan forward and see if it did. */
4919 it2 = *it;
4920 it2.vpos = it2.current_y = 0;
4921 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4922 it->vpos -= it2.vpos;
4923 it->current_y -= it2.current_y;
4924 it->current_x = it->hpos = 0;
4925
4926 /* If we moved too far, move IT some lines forward. */
4927 if (it2.vpos > -dvpos)
4928 {
4929 int delta = it2.vpos + dvpos;
4930 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4931 }
4932 }
4933}
4934
4935
4936\f
4937/***********************************************************************
4938 Messages
4939 ***********************************************************************/
4940
4941
937248bc
GM
4942/* Add a message with format string FORMAT and arguments ARG1 and ARG2
4943 to *Messages*. */
4944
4945void
4946add_to_log (format, arg1, arg2)
4947 char *format;
4948 Lisp_Object arg1, arg2;
4949{
4950 Lisp_Object args[3];
4951 Lisp_Object msg, fmt;
4952 char *buffer;
4953 int len;
4954 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4955
4956 fmt = msg = Qnil;
4957 GCPRO4 (fmt, msg, arg1, arg2);
4958
4959 args[0] = fmt = build_string (format);
4960 args[1] = arg1;
4961 args[2] = arg2;
6fc556fd 4962 msg = Fformat (3, args);
937248bc
GM
4963
4964 len = STRING_BYTES (XSTRING (msg)) + 1;
4965 buffer = (char *) alloca (len);
4966 strcpy (buffer, XSTRING (msg)->data);
4967
796184bc 4968 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
4969 UNGCPRO;
4970}
4971
4972
5f5c8ee5
GM
4973/* Output a newline in the *Messages* buffer if "needs" one. */
4974
4975void
4976message_log_maybe_newline ()
4977{
4978 if (message_log_need_newline)
4979 message_dolog ("", 0, 1, 0);
4980}
4981
4982
4983/* Add a string M of length LEN to the message log, optionally
4984 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4985 nonzero, means interpret the contents of M as multibyte. This
4986 function calls low-level routines in order to bypass text property
4987 hooks, etc. which might not be safe to run. */
4988
4989void
4990message_dolog (m, len, nlflag, multibyte)
4991 char *m;
4992 int len, nlflag, multibyte;
4993{
4994 if (!NILP (Vmessage_log_max))
4995 {
4996 struct buffer *oldbuf;
4997 Lisp_Object oldpoint, oldbegv, oldzv;
4998 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4999 int point_at_end = 0;
5000 int zv_at_end = 0;
5001 Lisp_Object old_deactivate_mark, tem;
5002 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5003
5004 old_deactivate_mark = Vdeactivate_mark;
5005 oldbuf = current_buffer;
6a94510a 5006 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5007 current_buffer->undo_list = Qt;
5008
5009 oldpoint = Fpoint_marker ();
5010 oldbegv = Fpoint_min_marker ();
5011 oldzv = Fpoint_max_marker ();
5012 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5013
5014 if (PT == Z)
5015 point_at_end = 1;
5016 if (ZV == Z)
5017 zv_at_end = 1;
5018
5019 BEGV = BEG;
5020 BEGV_BYTE = BEG_BYTE;
5021 ZV = Z;
5022 ZV_BYTE = Z_BYTE;
5023 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5024
5025 /* Insert the string--maybe converting multibyte to single byte
5026 or vice versa, so that all the text fits the buffer. */
5027 if (multibyte
5028 && NILP (current_buffer->enable_multibyte_characters))
5029 {
5030 int i, c, nbytes;
5031 unsigned char work[1];
5032
5033 /* Convert a multibyte string to single-byte
5034 for the *Message* buffer. */
5035 for (i = 0; i < len; i += nbytes)
5036 {
4fdb80f2 5037 c = string_char_and_length (m + i, len - i, &nbytes);
5f5c8ee5
GM
5038 work[0] = (SINGLE_BYTE_CHAR_P (c)
5039 ? c
5040 : multibyte_char_to_unibyte (c, Qnil));
5041 insert_1_both (work, 1, 1, 1, 0, 0);
5042 }
5043 }
5044 else if (! multibyte
5045 && ! NILP (current_buffer->enable_multibyte_characters))
5046 {
5047 int i, c, nbytes;
5048 unsigned char *msg = (unsigned char *) m;
260a86a0 5049 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5050 /* Convert a single-byte string to multibyte
5051 for the *Message* buffer. */
5052 for (i = 0; i < len; i++)
5053 {
5054 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
5055 nbytes = CHAR_STRING (c, str);
5056 insert_1_both (str, 1, nbytes, 1, 0, 0);
5f5c8ee5
GM
5057 }
5058 }
5059 else if (len)
5060 insert_1 (m, len, 1, 0, 0);
5061
5062 if (nlflag)
5063 {
5064 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5065 insert_1 ("\n", 1, 1, 0, 0);
5066
5067 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5068 this_bol = PT;
5069 this_bol_byte = PT_BYTE;
5070
5071 if (this_bol > BEG)
5072 {
5073 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5074 prev_bol = PT;
5075 prev_bol_byte = PT_BYTE;
5076
5077 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5078 this_bol, this_bol_byte);
5079 if (dup)
5080 {
5081 del_range_both (prev_bol, prev_bol_byte,
5082 this_bol, this_bol_byte, 0);
5083 if (dup > 1)
5084 {
5085 char dupstr[40];
5086 int duplen;
5087
5088 /* If you change this format, don't forget to also
5089 change message_log_check_duplicate. */
5090 sprintf (dupstr, " [%d times]", dup);
5091 duplen = strlen (dupstr);
5092 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5093 insert_1 (dupstr, duplen, 1, 0, 1);
5094 }
5095 }
5096 }
5097
5098 if (NATNUMP (Vmessage_log_max))
5099 {
5100 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5101 -XFASTINT (Vmessage_log_max) - 1, 0);
5102 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5103 }
5104 }
5105 BEGV = XMARKER (oldbegv)->charpos;
5106 BEGV_BYTE = marker_byte_position (oldbegv);
5107
5108 if (zv_at_end)
5109 {
5110 ZV = Z;
5111 ZV_BYTE = Z_BYTE;
5112 }
5113 else
5114 {
5115 ZV = XMARKER (oldzv)->charpos;
5116 ZV_BYTE = marker_byte_position (oldzv);
5117 }
5118
5119 if (point_at_end)
5120 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5121 else
5122 /* We can't do Fgoto_char (oldpoint) because it will run some
5123 Lisp code. */
5124 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5125 XMARKER (oldpoint)->bytepos);
5126
5127 UNGCPRO;
5128 free_marker (oldpoint);
5129 free_marker (oldbegv);
5130 free_marker (oldzv);
5131
5132 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5133 set_buffer_internal (oldbuf);
5134 if (NILP (tem))
5135 windows_or_buffers_changed = old_windows_or_buffers_changed;
5136 message_log_need_newline = !nlflag;
5137 Vdeactivate_mark = old_deactivate_mark;
5138 }
5139}
5140
5141
5142/* We are at the end of the buffer after just having inserted a newline.
5143 (Note: We depend on the fact we won't be crossing the gap.)
5144 Check to see if the most recent message looks a lot like the previous one.
5145 Return 0 if different, 1 if the new one should just replace it, or a
5146 value N > 1 if we should also append " [N times]". */
5147
5148static int
5149message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5150 int prev_bol, this_bol;
5151 int prev_bol_byte, this_bol_byte;
5152{
5153 int i;
5154 int len = Z_BYTE - 1 - this_bol_byte;
5155 int seen_dots = 0;
5156 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5157 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5158
5159 for (i = 0; i < len; i++)
5160 {
5161 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5162 && p1[i] != '\n')
5163 seen_dots = 1;
5164 if (p1[i] != p2[i])
5165 return seen_dots;
5166 }
5167 p1 += len;
5168 if (*p1 == '\n')
5169 return 2;
5170 if (*p1++ == ' ' && *p1++ == '[')
5171 {
5172 int n = 0;
5173 while (*p1 >= '0' && *p1 <= '9')
5174 n = n * 10 + *p1++ - '0';
5175 if (strncmp (p1, " times]\n", 8) == 0)
5176 return n+1;
5177 }
5178 return 0;
5179}
5180
5181
5182/* Display an echo area message M with a specified length of LEN
5183 chars. The string may include null characters. If M is 0, clear
5184 out any existing message, and let the mini-buffer text show through.
5185
5186 The buffer M must continue to exist until after the echo area gets
5187 cleared or some other message gets displayed there. This means do
5188 not pass text that is stored in a Lisp string; do not pass text in
5189 a buffer that was alloca'd. */
5190
5191void
5192message2 (m, len, multibyte)
5193 char *m;
5194 int len;
5195 int multibyte;
5196{
5197 /* First flush out any partial line written with print. */
5198 message_log_maybe_newline ();
5199 if (m)
5200 message_dolog (m, len, 1, multibyte);
5201 message2_nolog (m, len, multibyte);
5202}
5203
5204
5205/* The non-logging counterpart of message2. */
5206
5207void
5208message2_nolog (m, len, multibyte)
5209 char *m;
5210 int len;
5211{
886bd6f2 5212 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5213 message_enable_multibyte = multibyte;
5214
5215 if (noninteractive)
5216 {
5217 if (noninteractive_need_newline)
5218 putc ('\n', stderr);
5219 noninteractive_need_newline = 0;
5220 if (m)
5221 fwrite (m, len, 1, stderr);
5222 if (cursor_in_echo_area == 0)
5223 fprintf (stderr, "\n");
5224 fflush (stderr);
5225 }
5226 /* A null message buffer means that the frame hasn't really been
5227 initialized yet. Error messages get reported properly by
5228 cmd_error, so this must be just an informative message; toss it. */
5229 else if (INTERACTIVE
886bd6f2
GM
5230 && sf->glyphs_initialized_p
5231 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5232 {
5233 Lisp_Object mini_window;
5234 struct frame *f;
5235
5236 /* Get the frame containing the mini-buffer
5237 that the selected frame is using. */
886bd6f2 5238 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5239 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5240
5241 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5242 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5243 && ! FRAME_VISIBLE_P (f))
5244 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5245
5246 if (m)
5247 {
c6e89d6c 5248 set_message (m, Qnil, len, multibyte);
5f5c8ee5
GM
5249 if (minibuffer_auto_raise)
5250 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5251 }
5252 else
c6e89d6c 5253 clear_message (1, 1);
5f5c8ee5 5254
c6e89d6c 5255 do_pending_window_change (0);
5f5c8ee5 5256 echo_area_display (1);
c6e89d6c 5257 do_pending_window_change (0);
5f5c8ee5
GM
5258 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5259 (*frame_up_to_date_hook) (f);
5260 }
5261}
5262
5263
c6e89d6c
GM
5264/* Display an echo area message M with a specified length of NBYTES
5265 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5266 string, clear out any existing message, and let the mini-buffer
5267 text show through. */
5268
5269void
c6e89d6c 5270message3 (m, nbytes, multibyte)
5f5c8ee5 5271 Lisp_Object m;
c6e89d6c 5272 int nbytes;
5f5c8ee5
GM
5273 int multibyte;
5274{
5275 struct gcpro gcpro1;
5276
5277 GCPRO1 (m);
5278
5279 /* First flush out any partial line written with print. */
5280 message_log_maybe_newline ();
5281 if (STRINGP (m))
c6e89d6c
GM
5282 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5283 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5284
5285 UNGCPRO;
5286}
5287
5288
5289/* The non-logging version of message3. */
5290
5291void
c6e89d6c 5292message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5293 Lisp_Object m;
c6e89d6c 5294 int nbytes, multibyte;
5f5c8ee5 5295{
886bd6f2 5296 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5297 message_enable_multibyte = multibyte;
5298
5299 if (noninteractive)
5300 {
5301 if (noninteractive_need_newline)
5302 putc ('\n', stderr);
5303 noninteractive_need_newline = 0;
5304 if (STRINGP (m))
c6e89d6c 5305 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5306 if (cursor_in_echo_area == 0)
5307 fprintf (stderr, "\n");
5308 fflush (stderr);
5309 }
5310 /* A null message buffer means that the frame hasn't really been
5311 initialized yet. Error messages get reported properly by
5312 cmd_error, so this must be just an informative message; toss it. */
5313 else if (INTERACTIVE
886bd6f2
GM
5314 && sf->glyphs_initialized_p
5315 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5316 {
5317 Lisp_Object mini_window;
c6e89d6c 5318 Lisp_Object frame;
5f5c8ee5
GM
5319 struct frame *f;
5320
5321 /* Get the frame containing the mini-buffer
5322 that the selected frame is using. */
886bd6f2 5323 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5324 frame = XWINDOW (mini_window)->frame;
5325 f = XFRAME (frame);
5f5c8ee5
GM
5326
5327 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5328 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5329 && !FRAME_VISIBLE_P (f))
5330 Fmake_frame_visible (frame);
5f5c8ee5 5331
c6e89d6c 5332 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5333 {
c6e89d6c 5334 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5335 if (minibuffer_auto_raise)
5336 Fraise_frame (frame);
5f5c8ee5
GM
5337 }
5338 else
c6e89d6c 5339 clear_message (1, 1);
5f5c8ee5 5340
c6e89d6c 5341 do_pending_window_change (0);
5f5c8ee5 5342 echo_area_display (1);
c6e89d6c 5343 do_pending_window_change (0);
5f5c8ee5
GM
5344 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5345 (*frame_up_to_date_hook) (f);
5346 }
5347}
5348
5349
5350/* Display a null-terminated echo area message M. If M is 0, clear
5351 out any existing message, and let the mini-buffer text show through.
5352
5353 The buffer M must continue to exist until after the echo area gets
5354 cleared or some other message gets displayed there. Do not pass
5355 text that is stored in a Lisp string. Do not pass text in a buffer
5356 that was alloca'd. */
5357
5358void
5359message1 (m)
5360 char *m;
5361{
5362 message2 (m, (m ? strlen (m) : 0), 0);
5363}
5364
5365
5366/* The non-logging counterpart of message1. */
5367
5368void
5369message1_nolog (m)
5370 char *m;
5371{
5372 message2_nolog (m, (m ? strlen (m) : 0), 0);
5373}
5374
5375/* Display a message M which contains a single %s
5376 which gets replaced with STRING. */
5377
5378void
5379message_with_string (m, string, log)
5380 char *m;
5381 Lisp_Object string;
5382 int log;
5383{
5384 if (noninteractive)
5385 {
5386 if (m)
5387 {
5388 if (noninteractive_need_newline)
5389 putc ('\n', stderr);
5390 noninteractive_need_newline = 0;
5391 fprintf (stderr, m, XSTRING (string)->data);
5392 if (cursor_in_echo_area == 0)
5393 fprintf (stderr, "\n");
5394 fflush (stderr);
5395 }
5396 }
5397 else if (INTERACTIVE)
5398 {
5399 /* The frame whose minibuffer we're going to display the message on.
5400 It may be larger than the selected frame, so we need
5401 to use its buffer, not the selected frame's buffer. */
5402 Lisp_Object mini_window;
886bd6f2 5403 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5404
5405 /* Get the frame containing the minibuffer
5406 that the selected frame is using. */
886bd6f2 5407 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5408 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5409
5410 /* A null message buffer means that the frame hasn't really been
5411 initialized yet. Error messages get reported properly by
5412 cmd_error, so this must be just an informative message; toss it. */
5413 if (FRAME_MESSAGE_BUF (f))
5414 {
5415 int len;
5416 char *a[1];
5417 a[0] = (char *) XSTRING (string)->data;
5418
5419 len = doprnt (FRAME_MESSAGE_BUF (f),
5420 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5421
5422 if (log)
5423 message2 (FRAME_MESSAGE_BUF (f), len,
5424 STRING_MULTIBYTE (string));
5425 else
5426 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5427 STRING_MULTIBYTE (string));
5428
5429 /* Print should start at the beginning of the message
5430 buffer next time. */
5431 message_buf_print = 0;
5432 }
5433 }
5434}
5435
5436
5f5c8ee5
GM
5437/* Dump an informative message to the minibuf. If M is 0, clear out
5438 any existing message, and let the mini-buffer text show through. */
5439
5440/* VARARGS 1 */
5441void
5442message (m, a1, a2, a3)
5443 char *m;
5444 EMACS_INT a1, a2, a3;
5445{
5446 if (noninteractive)
5447 {
5448 if (m)
5449 {
5450 if (noninteractive_need_newline)
5451 putc ('\n', stderr);
5452 noninteractive_need_newline = 0;
5453 fprintf (stderr, m, a1, a2, a3);
5454 if (cursor_in_echo_area == 0)
5455 fprintf (stderr, "\n");
5456 fflush (stderr);
5457 }
5458 }
5459 else if (INTERACTIVE)
5460 {
5461 /* The frame whose mini-buffer we're going to display the message
5462 on. It may be larger than the selected frame, so we need to
5463 use its buffer, not the selected frame's buffer. */
5464 Lisp_Object mini_window;
886bd6f2 5465 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5466
5467 /* Get the frame containing the mini-buffer
5468 that the selected frame is using. */
886bd6f2 5469 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5470 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5471
5472 /* A null message buffer means that the frame hasn't really been
5473 initialized yet. Error messages get reported properly by
5474 cmd_error, so this must be just an informative message; toss
5475 it. */
5476 if (FRAME_MESSAGE_BUF (f))
5477 {
5478 if (m)
5479 {
5480 int len;
5481#ifdef NO_ARG_ARRAY
5482 char *a[3];
5483 a[0] = (char *) a1;
5484 a[1] = (char *) a2;
5485 a[2] = (char *) a3;
5486
5487 len = doprnt (FRAME_MESSAGE_BUF (f),
5488 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5489#else
5490 len = doprnt (FRAME_MESSAGE_BUF (f),
5491 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5492 (char **) &a1);
5493#endif /* NO_ARG_ARRAY */
5494
5495 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5496 }
5497 else
5498 message1 (0);
5499
5500 /* Print should start at the beginning of the message
5501 buffer next time. */
5502 message_buf_print = 0;
5503 }
5504 }
5505}
5506
5507
5508/* The non-logging version of message. */
5509
5510void
5511message_nolog (m, a1, a2, a3)
5512 char *m;
5513 EMACS_INT a1, a2, a3;
5514{
5515 Lisp_Object old_log_max;
5516 old_log_max = Vmessage_log_max;
5517 Vmessage_log_max = Qnil;
5518 message (m, a1, a2, a3);
5519 Vmessage_log_max = old_log_max;
5520}
5521
5522
c6e89d6c
GM
5523/* Display the current message in the current mini-buffer. This is
5524 only called from error handlers in process.c, and is not time
5525 critical. */
5f5c8ee5
GM
5526
5527void
5528update_echo_area ()
5529{
c6e89d6c
GM
5530 if (!NILP (echo_area_buffer[0]))
5531 {
5532 Lisp_Object string;
5533 string = Fcurrent_message ();
5534 message3 (string, XSTRING (string)->size,
5535 !NILP (current_buffer->enable_multibyte_characters));
5536 }
5537}
5538
5539
5bcfeb49
GM
5540/* Make sure echo area buffers in echo_buffers[] are life. If they
5541 aren't, make new ones. */
5542
5543static void
5544ensure_echo_area_buffers ()
5545{
5546 int i;
5547
5548 for (i = 0; i < 2; ++i)
5549 if (!BUFFERP (echo_buffer[i])
5550 || NILP (XBUFFER (echo_buffer[i])->name))
5551 {
5552 char name[30];
ff3d9573
GM
5553 Lisp_Object old_buffer;
5554 int j;
5555
5556 old_buffer = echo_buffer[i];
5bcfeb49
GM
5557 sprintf (name, " *Echo Area %d*", i);
5558 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 5559 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
5560
5561 for (j = 0; j < 2; ++j)
5562 if (EQ (old_buffer, echo_area_buffer[j]))
5563 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
5564 }
5565}
5566
5567
23a96c77 5568/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
5569 echo_area_buffer as current buffer.
5570
5571 WHICH zero means use the current message buffer
5572 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5573 from echo_buffer[] and clear it.
5574
5575 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5576 suitable buffer from echo_buffer[] and clear it.
5577
5578 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5579 that the current message becomes the last displayed one, make
5580 choose a suitable buffer for echo_area_buffer[0], and clear it.
5581
5582 Value is what FN returns. */
5583
5584static int
23a96c77 5585with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
5586 struct window *w;
5587 int which;
23dd2d97
KR
5588 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5589 EMACS_INT a1;
5590 Lisp_Object a2;
5591 EMACS_INT a3, a4;
c6e89d6c
GM
5592{
5593 Lisp_Object buffer;
15e26c76 5594 int this_one, the_other, clear_buffer_p, rc;
c6e89d6c
GM
5595 int count = specpdl_ptr - specpdl;
5596
5597 /* If buffers aren't life, make new ones. */
5bcfeb49 5598 ensure_echo_area_buffers ();
c6e89d6c
GM
5599
5600 clear_buffer_p = 0;
5601
5602 if (which == 0)
5603 this_one = 0, the_other = 1;
5604 else if (which > 0)
5605 this_one = 1, the_other = 0;
5f5c8ee5 5606 else
c6e89d6c
GM
5607 {
5608 this_one = 0, the_other = 1;
5609 clear_buffer_p = 1;
5610
5611 /* We need a fresh one in case the current echo buffer equals
5612 the one containing the last displayed echo area message. */
5613 if (!NILP (echo_area_buffer[this_one])
5614 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5615 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
5616 }
5617
5618 /* Choose a suitable buffer from echo_buffer[] is we don't
5619 have one. */
5620 if (NILP (echo_area_buffer[this_one]))
5621 {
5622 echo_area_buffer[this_one]
5623 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5624 ? echo_buffer[the_other]
5625 : echo_buffer[this_one]);
5626 clear_buffer_p = 1;
5627 }
5628
5629 buffer = echo_area_buffer[this_one];
5630
5631 record_unwind_protect (unwind_with_echo_area_buffer,
5632 with_echo_area_buffer_unwind_data (w));
5633
5634 /* Make the echo area buffer current. Note that for display
5635 purposes, it is not necessary that the displayed window's buffer
5636 == current_buffer, except for text property lookup. So, let's
5637 only set that buffer temporarily here without doing a full
5638 Fset_window_buffer. We must also change w->pointm, though,
5639 because otherwise an assertions in unshow_buffer fails, and Emacs
5640 aborts. */
9142dd5b 5641 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5642 if (w)
5643 {
5644 w->buffer = buffer;
5645 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5646 }
ad4f174e 5647
c6e89d6c
GM
5648 current_buffer->undo_list = Qt;
5649 current_buffer->read_only = Qnil;
5650
5651 if (clear_buffer_p && Z > BEG)
5652 del_range (BEG, Z);
5653
5654 xassert (BEGV >= BEG);
5655 xassert (ZV <= Z && ZV >= BEGV);
5656
23a96c77 5657 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
5658
5659 xassert (BEGV >= BEG);
5660 xassert (ZV <= Z && ZV >= BEGV);
5661
5662 unbind_to (count, Qnil);
5663 return rc;
5f5c8ee5
GM
5664}
5665
5666
c6e89d6c
GM
5667/* Save state that should be preserved around the call to the function
5668 FN called in with_echo_area_buffer. */
5f5c8ee5 5669
c6e89d6c
GM
5670static Lisp_Object
5671with_echo_area_buffer_unwind_data (w)
5672 struct window *w;
5f5c8ee5 5673{
c6e89d6c
GM
5674 int i = 0;
5675 Lisp_Object vector;
5f5c8ee5 5676
c6e89d6c
GM
5677 /* Reduce consing by keeping one vector in
5678 Vwith_echo_area_save_vector. */
5679 vector = Vwith_echo_area_save_vector;
5680 Vwith_echo_area_save_vector = Qnil;
5681
5682 if (NILP (vector))
9142dd5b 5683 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5684
5685 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5686 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5687 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5688
5689 if (w)
5690 {
5691 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5692 XVECTOR (vector)->contents[i++] = w->buffer;
5693 XVECTOR (vector)->contents[i++]
5694 = make_number (XMARKER (w->pointm)->charpos);
5695 XVECTOR (vector)->contents[i++]
5696 = make_number (XMARKER (w->pointm)->bytepos);
5697 }
5698 else
5699 {
5700 int end = i + 4;
5701 while (i < end)
5702 XVECTOR (vector)->contents[i++] = Qnil;
5703 }
5f5c8ee5 5704
c6e89d6c
GM
5705 xassert (i == XVECTOR (vector)->size);
5706 return vector;
5707}
5f5c8ee5 5708
5f5c8ee5 5709
c6e89d6c
GM
5710/* Restore global state from VECTOR which was created by
5711 with_echo_area_buffer_unwind_data. */
5712
5713static Lisp_Object
5714unwind_with_echo_area_buffer (vector)
5715 Lisp_Object vector;
5716{
5717 int i = 0;
5718
9142dd5b 5719 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
c6e89d6c
GM
5720 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5721 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
c6e89d6c
GM
5722
5723 if (WINDOWP (XVECTOR (vector)->contents[i]))
5724 {
5725 struct window *w;
5726 Lisp_Object buffer, charpos, bytepos;
5727
5728 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5729 buffer = XVECTOR (vector)->contents[i]; ++i;
5730 charpos = XVECTOR (vector)->contents[i]; ++i;
5731 bytepos = XVECTOR (vector)->contents[i]; ++i;
5732
5733 w->buffer = buffer;
5734 set_marker_both (w->pointm, buffer,
5735 XFASTINT (charpos), XFASTINT (bytepos));
5736 }
5737
5738 Vwith_echo_area_save_vector = vector;
5739 return Qnil;
5740}
5741
5742
5743/* Set up the echo area for use by print functions. MULTIBYTE_P
5744 non-zero means we will print multibyte. */
5745
5746void
5747setup_echo_area_for_printing (multibyte_p)
5748 int multibyte_p;
5749{
5bcfeb49
GM
5750 ensure_echo_area_buffers ();
5751
c6e89d6c
GM
5752 if (!message_buf_print)
5753 {
5754 /* A message has been output since the last time we printed.
5755 Choose a fresh echo area buffer. */
5756 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5757 echo_area_buffer[0] = echo_buffer[1];
5758 else
5759 echo_area_buffer[0] = echo_buffer[0];
5760
5761 /* Switch to that buffer and clear it. */
5762 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5763 if (Z > BEG)
5764 del_range (BEG, Z);
5765 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5766
5767 /* Set up the buffer for the multibyteness we need. */
5768 if (multibyte_p
5769 != !NILP (current_buffer->enable_multibyte_characters))
5770 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5771
5772 /* Raise the frame containing the echo area. */
5773 if (minibuffer_auto_raise)
5774 {
886bd6f2 5775 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 5776 Lisp_Object mini_window;
886bd6f2 5777 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5778 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5779 }
5780
8a4e3c0c 5781 message_log_maybe_newline ();
c6e89d6c
GM
5782 message_buf_print = 1;
5783 }
fa77249f
GM
5784 else
5785 {
5786 if (NILP (echo_area_buffer[0]))
5787 {
5788 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5789 echo_area_buffer[0] = echo_buffer[1];
5790 else
5791 echo_area_buffer[0] = echo_buffer[0];
5792 }
5793
5794 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5795 /* Someone switched buffers between print requests. */
5796 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5797 }
c6e89d6c
GM
5798}
5799
5800
dd2eb166
GM
5801/* Display an echo area message in window W. Value is non-zero if W's
5802 height is changed. If display_last_displayed_message_p is
5803 non-zero, display the message that was last displayed, otherwise
5804 display the current message. */
c6e89d6c
GM
5805
5806static int
5807display_echo_area (w)
5808 struct window *w;
5809{
25edb08f
GM
5810 int i, no_message_p, window_height_changed_p, count;
5811
5812 /* Temporarily disable garbage collections while displaying the echo
5813 area. This is done because a GC can print a message itself.
5814 That message would modify the echo area buffer's contents while a
5815 redisplay of the buffer is going on, and seriously confuse
5816 redisplay. */
5817 count = inhibit_garbage_collection ();
dd2eb166
GM
5818
5819 /* If there is no message, we must call display_echo_area_1
5820 nevertheless because it resizes the window. But we will have to
5821 reset the echo_area_buffer in question to nil at the end because
5822 with_echo_area_buffer will sets it to an empty buffer. */
5823 i = display_last_displayed_message_p ? 1 : 0;
5824 no_message_p = NILP (echo_area_buffer[i]);
5825
5826 window_height_changed_p
5827 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 5828 display_echo_area_1,
23dd2d97 5829 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
5830
5831 if (no_message_p)
5832 echo_area_buffer[i] = Qnil;
25edb08f
GM
5833
5834 unbind_to (count, Qnil);
dd2eb166 5835 return window_height_changed_p;
c6e89d6c
GM
5836}
5837
5838
5839/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
5840 contains the current echo area message in window W, a mini-window,
5841 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
5842 Change the height of W so that all of the message is displayed.
5843 Value is non-zero if height of W was changed. */
5844
5845static int
23a96c77 5846display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
5847 EMACS_INT a1;
5848 Lisp_Object a2;
5849 EMACS_INT a3, a4;
c6e89d6c 5850{
23a96c77 5851 struct window *w = (struct window *) a1;
c6e89d6c 5852 Lisp_Object window;
c6e89d6c
GM
5853 struct text_pos start;
5854 int window_height_changed_p = 0;
5855
5856 /* Do this before displaying, so that we have a large enough glyph
5857 matrix for the display. */
92a90e89 5858 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
5859
5860 /* Display. */
5861 clear_glyph_matrix (w->desired_matrix);
5862 XSETWINDOW (window, w);
5863 SET_TEXT_POS (start, BEG, BEG_BYTE);
5864 try_window (window, start);
5865
c6e89d6c
GM
5866 return window_height_changed_p;
5867}
5868
5869
92a90e89
GM
5870/* Resize the echo area window to exactly the size needed for the
5871 currently displayed message, if there is one. */
5872
5873void
5874resize_echo_area_axactly ()
5875{
5876 if (BUFFERP (echo_area_buffer[0])
5877 && WINDOWP (echo_area_window))
5878 {
5879 struct window *w = XWINDOW (echo_area_window);
5880 int resized_p;
5881
23a96c77 5882 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
23dd2d97 5883 (EMACS_INT) w, Qnil, 0, 0);
92a90e89
GM
5884 if (resized_p)
5885 {
5886 ++windows_or_buffers_changed;
5887 ++update_mode_lines;
5888 redisplay_internal (0);
5889 }
5890 }
5891}
5892
5893
23a96c77
GM
5894/* Callback function for with_echo_area_buffer, when used from
5895 resize_echo_area_axactly. A1 contains a pointer to the window to
5896 resize, A2 to A4 are not used. Value is what resize_mini_window
5897 returns. */
5898
5899static int
5900resize_mini_window_1 (a1, a2, a3, a4)
23dd2d97
KR
5901 EMACS_INT a1;
5902 Lisp_Object a2;
5903 EMACS_INT a3, a4;
23a96c77
GM
5904{
5905 return resize_mini_window ((struct window *) a1, 1);
5906}
5907
5908
92a90e89
GM
5909/* Resize mini-window W to fit the size of its contents. EXACT:P
5910 means size the window exactly to the size needed. Otherwise, it's
5911 only enlarged until W's buffer is empty. Value is non-zero if
5912 the window height has been changed. */
c6e89d6c 5913
9472f927 5914int
92a90e89 5915resize_mini_window (w, exact_p)
c6e89d6c 5916 struct window *w;
92a90e89 5917 int exact_p;
c6e89d6c
GM
5918{
5919 struct frame *f = XFRAME (w->frame);
5920 int window_height_changed_p = 0;
5921
5922 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
5923
5924 /* Nil means don't try to resize. */
00f6d59e
GM
5925 if (NILP (Vmax_mini_window_height)
5926 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 5927 return 0;
c6e89d6c
GM
5928
5929 if (!FRAME_MINIBUF_ONLY_P (f))
5930 {
5931 struct it it;
dd2eb166
GM
5932 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5933 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5934 int height, max_height;
5935 int unit = CANON_Y_UNIT (f);
5936 struct text_pos start;
9142dd5b 5937
c6e89d6c 5938 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 5939
dd2eb166
GM
5940 /* Compute the max. number of lines specified by the user. */
5941 if (FLOATP (Vmax_mini_window_height))
5942 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5943 else if (INTEGERP (Vmax_mini_window_height))
5944 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
5945 else
5946 max_height = total_height / 4;
dd2eb166
GM
5947
5948 /* Correct that max. height if it's bogus. */
5949 max_height = max (1, max_height);
5950 max_height = min (total_height, max_height);
5951
5952 /* Find out the height of the text in the window. */
ad4f174e
GM
5953 if (it.truncate_lines_p)
5954 height = 1;
55b064bd 5955 else
ad4f174e
GM
5956 {
5957 last_height = 0;
5958 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5959 if (it.max_ascent == 0 && it.max_descent == 0)
5960 height = it.current_y + last_height;
5961 else
5962 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 5963 height -= it.extra_line_spacing;
ad4f174e
GM
5964 height = (height + unit - 1) / unit;
5965 }
dd2eb166
GM
5966
5967 /* Compute a suitable window start. */
5968 if (height > max_height)
5969 {
5970 height = max_height;
5971 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5972 move_it_vertically_backward (&it, (height - 1) * unit);
5973 start = it.current.pos;
5974 }
5975 else
5976 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5977 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 5978
9472f927
GM
5979 /* Let it grow only, until we display an empty message, in which
5980 case the window shrinks again. */
da448723 5981 if (height > XFASTINT (w->height))
dd2eb166 5982 {
d2c48e86 5983 int old_height = XFASTINT (w->height);
da448723
GM
5984 freeze_window_starts (f, 1);
5985 grow_mini_window (w, height - XFASTINT (w->height));
5986 window_height_changed_p = XFASTINT (w->height) != old_height;
5987 }
5988 else if (height < XFASTINT (w->height)
5989 && (exact_p || BEGV == ZV))
5990 {
5991 int old_height = XFASTINT (w->height);
5992 freeze_window_starts (f, 0);
5993 shrink_mini_window (w);
d2c48e86 5994 window_height_changed_p = XFASTINT (w->height) != old_height;
9142dd5b 5995 }
c6e89d6c
GM
5996 }
5997
5998 return window_height_changed_p;
5999}
6000
6001
6002/* Value is the current message, a string, or nil if there is no
6003 current message. */
6004
6005Lisp_Object
6006current_message ()
6007{
6008 Lisp_Object msg;
6009
6010 if (NILP (echo_area_buffer[0]))
6011 msg = Qnil;
6012 else
6013 {
23a96c77 6014 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6015 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6016 if (NILP (msg))
6017 echo_area_buffer[0] = Qnil;
6018 }
6019
6020 return msg;
6021}
6022
6023
6024static int
23a96c77 6025current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6026 EMACS_INT a1;
6027 Lisp_Object a2;
6028 EMACS_INT a3, a4;
c6e89d6c 6029{
23a96c77
GM
6030 Lisp_Object *msg = (Lisp_Object *) a1;
6031
c6e89d6c
GM
6032 if (Z > BEG)
6033 *msg = make_buffer_string (BEG, Z, 1);
6034 else
6035 *msg = Qnil;
6036 return 0;
6037}
6038
6039
6040/* Push the current message on Vmessage_stack for later restauration
6041 by restore_message. Value is non-zero if the current message isn't
6042 empty. This is a relatively infrequent operation, so it's not
6043 worth optimizing. */
6044
6045int
6046push_message ()
6047{
6048 Lisp_Object msg;
6049 msg = current_message ();
6050 Vmessage_stack = Fcons (msg, Vmessage_stack);
6051 return STRINGP (msg);
6052}
6053
6054
6055/* Restore message display from the top of Vmessage_stack. */
6056
6057void
6058restore_message ()
6059{
6060 Lisp_Object msg;
6061
6062 xassert (CONSP (Vmessage_stack));
6063 msg = XCAR (Vmessage_stack);
6064 if (STRINGP (msg))
6065 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6066 else
6067 message3_nolog (msg, 0, 0);
6068}
6069
6070
6071/* Pop the top-most entry off Vmessage_stack. */
6072
6073void
6074pop_message ()
6075{
6076 xassert (CONSP (Vmessage_stack));
6077 Vmessage_stack = XCDR (Vmessage_stack);
6078}
6079
6080
6081/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6082 exits. If the stack is not empty, we have a missing pop_message
6083 somewhere. */
6084
6085void
6086check_message_stack ()
6087{
6088 if (!NILP (Vmessage_stack))
6089 abort ();
6090}
6091
6092
6093/* Truncate to NCHARS what will be displayed in the echo area the next
6094 time we display it---but don't redisplay it now. */
6095
6096void
6097truncate_echo_area (nchars)
6098 int nchars;
6099{
6100 if (nchars == 0)
6101 echo_area_buffer[0] = Qnil;
6102 /* A null message buffer means that the frame hasn't really been
6103 initialized yet. Error messages get reported properly by
6104 cmd_error, so this must be just an informative message; toss it. */
6105 else if (!noninteractive
6106 && INTERACTIVE
c6e89d6c 6107 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6108 {
6109 struct frame *sf = SELECTED_FRAME ();
6110 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6111 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6112 }
c6e89d6c
GM
6113}
6114
6115
6116/* Helper function for truncate_echo_area. Truncate the current
6117 message to at most NCHARS characters. */
6118
6119static int
23a96c77 6120truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6121 EMACS_INT nchars;
6122 Lisp_Object a2;
6123 EMACS_INT a3, a4;
c6e89d6c
GM
6124{
6125 if (BEG + nchars < Z)
6126 del_range (BEG + nchars, Z);
6127 if (Z == BEG)
6128 echo_area_buffer[0] = Qnil;
6129 return 0;
6130}
6131
6132
6133/* Set the current message to a substring of S or STRING.
6134
6135 If STRING is a Lisp string, set the message to the first NBYTES
6136 bytes from STRING. NBYTES zero means use the whole string. If
6137 STRING is multibyte, the message will be displayed multibyte.
6138
6139 If S is not null, set the message to the first LEN bytes of S. LEN
6140 zero means use the whole string. MULTIBYTE_P non-zero means S is
6141 multibyte. Display the message multibyte in that case. */
6142
6143void
6144set_message (s, string, nbytes, multibyte_p)
6145 char *s;
6146 Lisp_Object string;
6147 int nbytes;
6148{
6149 message_enable_multibyte
6150 = ((s && multibyte_p)
6151 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6152
23a96c77
GM
6153 with_echo_area_buffer (0, -1, set_message_1,
6154 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c
GM
6155 message_buf_print = 0;
6156}
6157
6158
6159/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6160 as there, with A1 corresponding to S and A2 corresponding to STRING
6161 This function is called with the echo area buffer being
c6e89d6c
GM
6162 current. */
6163
6164static int
23a96c77 6165set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6166 EMACS_INT a1;
6167 Lisp_Object a2;
6168 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6169{
23a96c77 6170 char *s = (char *) a1;
23dd2d97 6171 Lisp_Object string = a2;
23a96c77 6172
c6e89d6c
GM
6173 xassert (BEG == Z);
6174
6175 /* Change multibyteness of the echo buffer appropriately. */
6176 if (message_enable_multibyte
6177 != !NILP (current_buffer->enable_multibyte_characters))
6178 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6179
ad4f174e
GM
6180 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6181
c6e89d6c
GM
6182 /* Insert new message at BEG. */
6183 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6184
6185 if (STRINGP (string))
6186 {
6187 int nchars;
6188
6189 if (nbytes == 0)
6190 nbytes = XSTRING (string)->size_byte;
6191 nchars = string_byte_to_char (string, nbytes);
6192
6193 /* This function takes care of single/multibyte conversion. We
6194 just have to ensure that the echo area buffer has the right
6195 setting of enable_multibyte_characters. */
6196 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6197 }
6198 else if (s)
6199 {
6200 if (nbytes == 0)
6201 nbytes = strlen (s);
6202
6203 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6204 {
6205 /* Convert from multi-byte to single-byte. */
6206 int i, c, n;
6207 unsigned char work[1];
6208
6209 /* Convert a multibyte string to single-byte. */
6210 for (i = 0; i < nbytes; i += n)
6211 {
6212 c = string_char_and_length (s + i, nbytes - i, &n);
6213 work[0] = (SINGLE_BYTE_CHAR_P (c)
6214 ? c
6215 : multibyte_char_to_unibyte (c, Qnil));
6216 insert_1_both (work, 1, 1, 1, 0, 0);
6217 }
6218 }
6219 else if (!multibyte_p
6220 && !NILP (current_buffer->enable_multibyte_characters))
6221 {
6222 /* Convert from single-byte to multi-byte. */
6223 int i, c, n;
6224 unsigned char *msg = (unsigned char *) s;
260a86a0 6225 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6226
6227 /* Convert a single-byte string to multibyte. */
6228 for (i = 0; i < nbytes; i++)
6229 {
6230 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6231 n = CHAR_STRING (c, str);
6232 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6233 }
6234 }
6235 else
6236 insert_1 (s, nbytes, 1, 0, 0);
6237 }
6238
6239 return 0;
6240}
6241
6242
6243/* Clear messages. CURRENT_P non-zero means clear the current
6244 message. LAST_DISPLAYED_P non-zero means clear the message
6245 last displayed. */
6246
6247void
6248clear_message (current_p, last_displayed_p)
6249 int current_p, last_displayed_p;
6250{
6251 if (current_p)
6252 echo_area_buffer[0] = Qnil;
6253
6254 if (last_displayed_p)
6255 echo_area_buffer[1] = Qnil;
6256
6257 message_buf_print = 0;
6258}
6259
6260/* Clear garbaged frames.
6261
6262 This function is used where the old redisplay called
6263 redraw_garbaged_frames which in turn called redraw_frame which in
6264 turn called clear_frame. The call to clear_frame was a source of
6265 flickering. I believe a clear_frame is not necessary. It should
6266 suffice in the new redisplay to invalidate all current matrices,
6267 and ensure a complete redisplay of all windows. */
6268
6269static void
6270clear_garbaged_frames ()
6271{
5f5c8ee5
GM
6272 if (frame_garbaged)
6273 {
5f5c8ee5
GM
6274 Lisp_Object tail, frame;
6275
6276 FOR_EACH_FRAME (tail, frame)
6277 {
6278 struct frame *f = XFRAME (frame);
6279
6280 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6281 {
6282 clear_current_matrices (f);
6283 f->garbaged = 0;
6284 }
6285 }
6286
6287 frame_garbaged = 0;
6288 ++windows_or_buffers_changed;
6289 }
c6e89d6c 6290}
5f5c8ee5 6291
5f5c8ee5 6292
886bd6f2
GM
6293/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6294 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 6295 mini-windows height has been changed. */
5f5c8ee5 6296
c6e89d6c
GM
6297static int
6298echo_area_display (update_frame_p)
6299 int update_frame_p;
6300{
6301 Lisp_Object mini_window;
6302 struct window *w;
6303 struct frame *f;
6304 int window_height_changed_p = 0;
886bd6f2 6305 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6306
886bd6f2 6307 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6308 w = XWINDOW (mini_window);
6309 f = XFRAME (WINDOW_FRAME (w));
6310
6311 /* Don't display if frame is invisible or not yet initialized. */
6312 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6313 return 0;
5f5c8ee5 6314
1ab3e082 6315#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
6316 /* When Emacs starts, selected_frame may be a visible terminal
6317 frame, even if we run under a window system. If we let this
6318 through, a message would be displayed on the terminal. */
622e3754
GM
6319 if (EQ (selected_frame, Vterminal_frame)
6320 && !NILP (Vwindow_system))
c6e89d6c 6321 return 0;
1ab3e082 6322#endif /* HAVE_WINDOW_SYSTEM */
c6e89d6c
GM
6323
6324 /* Redraw garbaged frames. */
6325 if (frame_garbaged)
6326 clear_garbaged_frames ();
6327
6328 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6329 {
6330 echo_area_window = mini_window;
6331 window_height_changed_p = display_echo_area (w);
5f5c8ee5 6332 w->must_be_updated_p = 1;
c59c668a 6333
715e84c9 6334 /* Update the display, unless called from redisplay_internal. */
5f5c8ee5
GM
6335 if (update_frame_p)
6336 {
715e84c9 6337 int n = 0;
edc68111 6338
715e84c9
GM
6339 /* If the display update has been interrupted by pending
6340 input, update mode lines in the frame. Due to the
6341 pending input, it might have been that redisplay hasn't
6342 been called, so that mode lines above the echo area are
6343 garbaged. This looks odd, so we prevent it here. */
6344 if (!display_completed)
6345 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6346
6347 if (window_height_changed_p)
c59c668a 6348 {
715e84c9 6349 /* Must update other windows. */
edc68111 6350 windows_or_buffers_changed = 1;
c59c668a
GM
6351 redisplay_internal (0);
6352 }
715e84c9 6353 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 6354 {
edc68111 6355 /* Window configuration is the same as before.
715e84c9
GM
6356 Can do with a display update of the echo area,
6357 unless we displayed some mode lines. */
5f5c8ee5
GM
6358 update_single_window (w, 1);
6359 rif->flush_display (f);
6360 }
6361 else
6362 update_frame (f, 1, 1);
6363 }
6364 }
6365 else if (!EQ (mini_window, selected_window))
6366 windows_or_buffers_changed++;
c59c668a
GM
6367
6368 /* Last displayed message is now the current message. */
dd2eb166
GM
6369 echo_area_buffer[1] = echo_area_buffer[0];
6370
5f5c8ee5
GM
6371 /* Prevent redisplay optimization in redisplay_internal by resetting
6372 this_line_start_pos. This is done because the mini-buffer now
6373 displays the message instead of its buffer text. */
6374 if (EQ (mini_window, selected_window))
6375 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
6376
6377 return window_height_changed_p;
5f5c8ee5
GM
6378}
6379
6380
6381\f
6382/***********************************************************************
6383 Frame Titles
6384 ***********************************************************************/
6385
6386
6387#ifdef HAVE_WINDOW_SYSTEM
6388
6389/* A buffer for constructing frame titles in it; allocated from the
6390 heap in init_xdisp and resized as needed in store_frame_title_char. */
6391
6392static char *frame_title_buf;
6393
6394/* The buffer's end, and a current output position in it. */
6395
6396static char *frame_title_buf_end;
6397static char *frame_title_ptr;
6398
6399
6400/* Store a single character C for the frame title in frame_title_buf.
6401 Re-allocate frame_title_buf if necessary. */
6402
6403static void
6404store_frame_title_char (c)
6405 char c;
6406{
6407 /* If output position has reached the end of the allocated buffer,
6408 double the buffer's size. */
6409 if (frame_title_ptr == frame_title_buf_end)
6410 {
6411 int len = frame_title_ptr - frame_title_buf;
6412 int new_size = 2 * len * sizeof *frame_title_buf;
6413 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6414 frame_title_buf_end = frame_title_buf + new_size;
6415 frame_title_ptr = frame_title_buf + len;
6416 }
6417
6418 *frame_title_ptr++ = c;
6419}
6420
6421
6422/* Store part of a frame title in frame_title_buf, beginning at
6423 frame_title_ptr. STR is the string to store. Do not copy more
6424 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6425 the whole string. Pad with spaces until FIELD_WIDTH number of
6426 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6427 Called from display_mode_element when it is used to build a frame
6428 title. */
6429
6430static int
6431store_frame_title (str, field_width, precision)
6432 unsigned char *str;
6433 int field_width, precision;
6434{
6435 int n = 0;
6436
6437 /* Copy at most PRECISION chars from STR. */
6438 while ((precision <= 0 || n < precision)
6439 && *str)
6440 {
6441 store_frame_title_char (*str++);
6442 ++n;
6443 }
6444
6445 /* Fill up with spaces until FIELD_WIDTH reached. */
6446 while (field_width > 0
6447 && n < field_width)
6448 {
6449 store_frame_title_char (' ');
6450 ++n;
6451 }
6452
6453 return n;
6454}
6455
6456
6457/* Set the title of FRAME, if it has changed. The title format is
6458 Vicon_title_format if FRAME is iconified, otherwise it is
6459 frame_title_format. */
6460
6461static void
6462x_consider_frame_title (frame)
6463 Lisp_Object frame;
6464{
6465 struct frame *f = XFRAME (frame);
6466
6467 if (FRAME_WINDOW_P (f)
6468 || FRAME_MINIBUF_ONLY_P (f)
6469 || f->explicit_name)
6470 {
6471 /* Do we have more than one visible frame on this X display? */
6472 Lisp_Object tail;
6473 Lisp_Object fmt;
6474 struct buffer *obuf;
6475 int len;
6476 struct it it;
6477
9472f927 6478 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6479 {
9472f927 6480 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
6481
6482 if (tf != f
6483 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6484 && !FRAME_MINIBUF_ONLY_P (tf)
6485 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6486 break;
6487 }
6488
6489 /* Set global variable indicating that multiple frames exist. */
6490 multiple_frames = CONSP (tail);
6491
6492 /* Switch to the buffer of selected window of the frame. Set up
6493 frame_title_ptr so that display_mode_element will output into it;
6494 then display the title. */
6495 obuf = current_buffer;
6496 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6497 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6498 frame_title_ptr = frame_title_buf;
6499 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6500 NULL, DEFAULT_FACE_ID);
6501 len = display_mode_element (&it, 0, -1, -1, fmt);
6502 frame_title_ptr = NULL;
6503 set_buffer_internal (obuf);
6504
6505 /* Set the title only if it's changed. This avoids consing in
6506 the common case where it hasn't. (If it turns out that we've
6507 already wasted too much time by walking through the list with
6508 display_mode_element, then we might need to optimize at a
6509 higher level than this.) */
6510 if (! STRINGP (f->name)
6511 || STRING_BYTES (XSTRING (f->name)) != len
6512 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6513 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6514 }
6515}
6516
6517#else /* not HAVE_WINDOW_SYSTEM */
6518
6519#define frame_title_ptr ((char *)0)
6520#define store_frame_title(str, mincol, maxcol) 0
6521
6522#endif /* not HAVE_WINDOW_SYSTEM */
6523
6524
6525
6526\f
6527/***********************************************************************
6528 Menu Bars
6529 ***********************************************************************/
6530
6531
6532/* Prepare for redisplay by updating menu-bar item lists when
6533 appropriate. This can call eval. */
6534
6535void
6536prepare_menu_bars ()
6537{
6538 int all_windows;
6539 struct gcpro gcpro1, gcpro2;
6540 struct frame *f;
6541 struct frame *tooltip_frame;
6542
6543#ifdef HAVE_X_WINDOWS
6544 tooltip_frame = tip_frame;
6545#else
6546 tooltip_frame = NULL;
6547#endif
6548
6549 /* Update all frame titles based on their buffer names, etc. We do
6550 this before the menu bars so that the buffer-menu will show the
6551 up-to-date frame titles. */
6552#ifdef HAVE_WINDOW_SYSTEM
6553 if (windows_or_buffers_changed || update_mode_lines)
6554 {
6555 Lisp_Object tail, frame;
6556
6557 FOR_EACH_FRAME (tail, frame)
6558 {
6559 f = XFRAME (frame);
6560 if (f != tooltip_frame
6561 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6562 x_consider_frame_title (frame);
6563 }
6564 }
6565#endif /* HAVE_WINDOW_SYSTEM */
6566
6567 /* Update the menu bar item lists, if appropriate. This has to be
6568 done before any actual redisplay or generation of display lines. */
6569 all_windows = (update_mode_lines
6570 || buffer_shared > 1
6571 || windows_or_buffers_changed);
6572 if (all_windows)
6573 {
6574 Lisp_Object tail, frame;
6575 int count = specpdl_ptr - specpdl;
6576
6577 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6578
6579 FOR_EACH_FRAME (tail, frame)
6580 {
6581 f = XFRAME (frame);
6582
6583 /* Ignore tooltip frame. */
6584 if (f == tooltip_frame)
6585 continue;
6586
6587 /* If a window on this frame changed size, report that to
6588 the user and clear the size-change flag. */
6589 if (FRAME_WINDOW_SIZES_CHANGED (f))
6590 {
6591 Lisp_Object functions;
6592
6593 /* Clear flag first in case we get an error below. */
6594 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6595 functions = Vwindow_size_change_functions;
6596 GCPRO2 (tail, functions);
6597
6598 while (CONSP (functions))
6599 {
6600 call1 (XCAR (functions), frame);
6601 functions = XCDR (functions);
6602 }
6603 UNGCPRO;
6604 }
6605
6606 GCPRO1 (tail);
6607 update_menu_bar (f, 0);
6608#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 6609 update_tool_bar (f, 0);
5f5c8ee5
GM
6610#endif
6611 UNGCPRO;
6612 }
6613
6614 unbind_to (count, Qnil);
6615 }
6616 else
6617 {
886bd6f2
GM
6618 struct frame *sf = SELECTED_FRAME ();
6619 update_menu_bar (sf, 1);
5f5c8ee5 6620#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 6621 update_tool_bar (sf, 1);
5f5c8ee5
GM
6622#endif
6623 }
6624
6625 /* Motif needs this. See comment in xmenu.c. Turn it off when
6626 pending_menu_activation is not defined. */
6627#ifdef USE_X_TOOLKIT
6628 pending_menu_activation = 0;
6629#endif
6630}
6631
6632
6633/* Update the menu bar item list for frame F. This has to be done
6634 before we start to fill in any display lines, because it can call
6635 eval.
6636
6637 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6638
6639static void
6640update_menu_bar (f, save_match_data)
6641 struct frame *f;
6642 int save_match_data;
6643{
6644 Lisp_Object window;
6645 register struct window *w;
6646
6647 window = FRAME_SELECTED_WINDOW (f);
6648 w = XWINDOW (window);
6649
6650 if (update_mode_lines)
6651 w->update_mode_line = Qt;
6652
6653 if (FRAME_WINDOW_P (f)
6654 ?
6655#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6656 FRAME_EXTERNAL_MENU_BAR (f)
6657#else
6658 FRAME_MENU_BAR_LINES (f) > 0
6659#endif
6660 : FRAME_MENU_BAR_LINES (f) > 0)
6661 {
6662 /* If the user has switched buffers or windows, we need to
6663 recompute to reflect the new bindings. But we'll
6664 recompute when update_mode_lines is set too; that means
6665 that people can use force-mode-line-update to request
6666 that the menu bar be recomputed. The adverse effect on
6667 the rest of the redisplay algorithm is about the same as
6668 windows_or_buffers_changed anyway. */
6669 if (windows_or_buffers_changed
6670 || !NILP (w->update_mode_line)
6671 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6672 < BUF_MODIFF (XBUFFER (w->buffer)))
6673 != !NILP (w->last_had_star))
6674 || ((!NILP (Vtransient_mark_mode)
6675 && !NILP (XBUFFER (w->buffer)->mark_active))
6676 != !NILP (w->region_showing)))
6677 {
6678 struct buffer *prev = current_buffer;
6679 int count = specpdl_ptr - specpdl;
6680
6681 set_buffer_internal_1 (XBUFFER (w->buffer));
6682 if (save_match_data)
6683 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6684 if (NILP (Voverriding_local_map_menu_flag))
6685 {
6686 specbind (Qoverriding_terminal_local_map, Qnil);
6687 specbind (Qoverriding_local_map, Qnil);
6688 }
6689
6690 /* Run the Lucid hook. */
6691 call1 (Vrun_hooks, Qactivate_menubar_hook);
6692
6693 /* If it has changed current-menubar from previous value,
6694 really recompute the menu-bar from the value. */
6695 if (! NILP (Vlucid_menu_bar_dirty_flag))
6696 call0 (Qrecompute_lucid_menubar);
6697
6698 safe_run_hooks (Qmenu_bar_update_hook);
6699 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6700
6701 /* Redisplay the menu bar in case we changed it. */
6702#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6703 if (FRAME_WINDOW_P (f))
6704 set_frame_menubar (f, 0, 0);
6705 else
6706 /* On a terminal screen, the menu bar is an ordinary screen
6707 line, and this makes it get updated. */
6708 w->update_mode_line = Qt;
6709#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6710 /* In the non-toolkit version, the menu bar is an ordinary screen
6711 line, and this makes it get updated. */
6712 w->update_mode_line = Qt;
6713#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6714
6715 unbind_to (count, Qnil);
6716 set_buffer_internal_1 (prev);
6717 }
6718 }
6719}
6720
6721
6722\f
6723/***********************************************************************
e037b9ec 6724 Tool-bars
5f5c8ee5
GM
6725 ***********************************************************************/
6726
6727#ifdef HAVE_WINDOW_SYSTEM
6728
e037b9ec 6729/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
6730 before we start to fill in any display lines. Called from
6731 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6732 and restore it here. */
6733
6734static void
e037b9ec 6735update_tool_bar (f, save_match_data)
5f5c8ee5
GM
6736 struct frame *f;
6737 int save_match_data;
6738{
e037b9ec
GM
6739 if (WINDOWP (f->tool_bar_window)
6740 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
6741 {
6742 Lisp_Object window;
6743 struct window *w;
6744
6745 window = FRAME_SELECTED_WINDOW (f);
6746 w = XWINDOW (window);
6747
6748 /* If the user has switched buffers or windows, we need to
6749 recompute to reflect the new bindings. But we'll
6750 recompute when update_mode_lines is set too; that means
6751 that people can use force-mode-line-update to request
6752 that the menu bar be recomputed. The adverse effect on
6753 the rest of the redisplay algorithm is about the same as
6754 windows_or_buffers_changed anyway. */
6755 if (windows_or_buffers_changed
6756 || !NILP (w->update_mode_line)
6757 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6758 < BUF_MODIFF (XBUFFER (w->buffer)))
6759 != !NILP (w->last_had_star))
6760 || ((!NILP (Vtransient_mark_mode)
6761 && !NILP (XBUFFER (w->buffer)->mark_active))
6762 != !NILP (w->region_showing)))
6763 {
6764 struct buffer *prev = current_buffer;
6765 int count = specpdl_ptr - specpdl;
a2889657 6766
5f5c8ee5
GM
6767 /* Set current_buffer to the buffer of the selected
6768 window of the frame, so that we get the right local
6769 keymaps. */
6770 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 6771
5f5c8ee5
GM
6772 /* Save match data, if we must. */
6773 if (save_match_data)
6774 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6775
6776 /* Make sure that we don't accidentally use bogus keymaps. */
6777 if (NILP (Voverriding_local_map_menu_flag))
6778 {
6779 specbind (Qoverriding_terminal_local_map, Qnil);
6780 specbind (Qoverriding_local_map, Qnil);
1f40cad2 6781 }
1f40cad2 6782
e037b9ec
GM
6783 /* Build desired tool-bar items from keymaps. */
6784 f->desired_tool_bar_items
6785 = tool_bar_items (f->desired_tool_bar_items,
6786 &f->n_desired_tool_bar_items);
5f5c8ee5 6787
e037b9ec 6788 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
6789 w->update_mode_line = Qt;
6790
6791 unbind_to (count, Qnil);
6792 set_buffer_internal_1 (prev);
81d478f3 6793 }
a2889657
JB
6794 }
6795}
6796
6c4429a5 6797
e037b9ec
GM
6798/* Set F->desired_tool_bar_string to a Lisp string representing frame
6799 F's desired tool-bar contents. F->desired_tool_bar_items must have
5f5c8ee5
GM
6800 been set up previously by calling prepare_menu_bars. */
6801
a2889657 6802static void
e037b9ec 6803build_desired_tool_bar_string (f)
5f5c8ee5 6804 struct frame *f;
a2889657 6805{
5f5c8ee5
GM
6806 int i, size, size_needed, string_idx;
6807 struct gcpro gcpro1, gcpro2, gcpro3;
6808 Lisp_Object image, plist, props;
a2889657 6809
5f5c8ee5
GM
6810 image = plist = props = Qnil;
6811 GCPRO3 (image, plist, props);
a2889657 6812
e037b9ec 6813 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
6814 Otherwise, make a new string. */
6815
6816 /* The size of the string we might be able to reuse. */
e037b9ec
GM
6817 size = (STRINGP (f->desired_tool_bar_string)
6818 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
6819 : 0);
6820
6821 /* Each image in the string we build is preceded by a space,
6822 and there is a space at the end. */
e037b9ec 6823 size_needed = f->n_desired_tool_bar_items + 1;
5f5c8ee5 6824
e037b9ec 6825 /* Reuse f->desired_tool_bar_string, if possible. */
5f5c8ee5 6826 if (size < size_needed)
6fc556fd
KR
6827 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6828 make_number (' '));
5f5c8ee5
GM
6829 else
6830 {
6831 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6832 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 6833 props, f->desired_tool_bar_string);
5f5c8ee5 6834 }
a2889657 6835
5f5c8ee5 6836 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
6837 put a `menu_item' property on tool-bar items with a value that
6838 is the index of the item in F's tool-bar item vector. */
5f5c8ee5 6839 for (i = 0, string_idx = 0;
e037b9ec 6840 i < f->n_desired_tool_bar_items;
5f5c8ee5 6841 ++i, string_idx += 1)
a2889657 6842 {
5f5c8ee5 6843#define PROP(IDX) \
e037b9ec
GM
6844 (XVECTOR (f->desired_tool_bar_items) \
6845 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
5f5c8ee5 6846
e037b9ec
GM
6847 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6848 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
37e4e482 6849 int margin, relief, idx;
5f5c8ee5
GM
6850 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6851 extern Lisp_Object Qlaplace;
6852
6853 /* If image is a vector, choose the image according to the
6854 button state. */
e037b9ec 6855 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
6856 if (VECTORP (image))
6857 {
5f5c8ee5
GM
6858 if (enabled_p)
6859 idx = (selected_p
e037b9ec
GM
6860 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6861 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
6862 else
6863 idx = (selected_p
e037b9ec
GM
6864 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6865 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5 6866
37e4e482
GM
6867 xassert (ASIZE (image) >= idx);
6868 image = AREF (image, idx);
5f5c8ee5 6869 }
37e4e482
GM
6870 else
6871 idx = -1;
5f5c8ee5
GM
6872
6873 /* Ignore invalid image specifications. */
6874 if (!valid_image_p (image))
6875 continue;
6876
e037b9ec 6877 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
6878 plist = Fcopy_sequence (XCDR (image));
6879
6880 /* Compute margin and relief to draw. */
e037b9ec
GM
6881 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6882 margin = relief + max (0, tool_bar_button_margin);
5f5c8ee5 6883
e037b9ec 6884 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
6885 {
6886 /* Add a `:relief' property to the image spec if the item is
6887 selected. */
6888 if (selected_p)
6889 {
6890 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6891 margin -= relief;
6892 }
6893 }
6894 else
6895 {
6896 /* If image is selected, display it pressed, i.e. with a
6897 negative relief. If it's not selected, display it with a
6898 raised relief. */
6899 plist = Fplist_put (plist, QCrelief,
6900 (selected_p
6901 ? make_number (-relief)
6902 : make_number (relief)));
6903 margin -= relief;
6904 }
6905
6906 /* Put a margin around the image. */
6907 if (margin)
6908 plist = Fplist_put (plist, QCmargin, make_number (margin));
6909
37e4e482
GM
6910 /* If button is not enabled, and we don't have special images
6911 for the disabled state, make the image appear disabled by
5f5c8ee5 6912 applying an appropriate algorithm to it. */
37e4e482
GM
6913 if (!enabled_p && idx < 0)
6914 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
5f5c8ee5
GM
6915
6916 /* Put a `display' text property on the string for the image to
6917 display. Put a `menu-item' property on the string that gives
e037b9ec 6918 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
6919 vector. */
6920 image = Fcons (Qimage, plist);
6921 props = list4 (Qdisplay, image,
e037b9ec 6922 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
5f5c8ee5
GM
6923 Fadd_text_properties (make_number (string_idx),
6924 make_number (string_idx + 1),
e037b9ec 6925 props, f->desired_tool_bar_string);
5f5c8ee5 6926#undef PROP
a2889657
JB
6927 }
6928
5f5c8ee5
GM
6929 UNGCPRO;
6930}
6931
6932
e037b9ec 6933/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
6934
6935static void
e037b9ec 6936display_tool_bar_line (it)
5f5c8ee5
GM
6937 struct it *it;
6938{
6939 struct glyph_row *row = it->glyph_row;
6940 int max_x = it->last_visible_x;
6941 struct glyph *last;
6942
6943 prepare_desired_row (row);
6944 row->y = it->current_y;
6945
6946 while (it->current_x < max_x)
a2889657 6947 {
5f5c8ee5 6948 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 6949
5f5c8ee5
GM
6950 /* Get the next display element. */
6951 if (!get_next_display_element (it))
6952 break;
73af359d 6953
5f5c8ee5
GM
6954 /* Produce glyphs. */
6955 x_before = it->current_x;
6956 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6957 PRODUCE_GLYPHS (it);
daa37602 6958
5f5c8ee5
GM
6959 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6960 i = 0;
6961 x = x_before;
6962 while (i < nglyphs)
6963 {
6964 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6965
6966 if (x + glyph->pixel_width > max_x)
6967 {
6968 /* Glyph doesn't fit on line. */
6969 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6970 it->current_x = x;
6971 goto out;
6972 }
daa37602 6973
5f5c8ee5
GM
6974 ++it->hpos;
6975 x += glyph->pixel_width;
6976 ++i;
6977 }
6978
6979 /* Stop at line ends. */
6980 if (ITERATOR_AT_END_OF_LINE_P (it))
6981 break;
6982
6983 set_iterator_to_next (it);
a2889657 6984 }
a2889657 6985
5f5c8ee5 6986 out:;
a2889657 6987
5f5c8ee5
GM
6988 row->displays_text_p = row->used[TEXT_AREA] != 0;
6989 extend_face_to_end_of_line (it);
6990 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6991 last->right_box_line_p = 1;
6992 compute_line_metrics (it);
6993
e037b9ec 6994 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
6995 if (!row->displays_text_p)
6996 {
312246d1
GM
6997 row->height = row->phys_height = it->last_visible_y - row->y;
6998 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
6999 }
7000
7001 row->full_width_p = 1;
7002 row->continued_p = 0;
7003 row->truncated_on_left_p = 0;
7004 row->truncated_on_right_p = 0;
7005
7006 it->current_x = it->hpos = 0;
7007 it->current_y += row->height;
7008 ++it->vpos;
7009 ++it->glyph_row;
a2889657 7010}
96a410bc 7011
5f5c8ee5 7012
e037b9ec 7013/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7014 items of frame F visible. */
96a410bc 7015
d39b6696 7016static int
e037b9ec 7017tool_bar_lines_needed (f)
5f5c8ee5 7018 struct frame *f;
d39b6696 7019{
e037b9ec 7020 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
7021 struct it it;
7022
e037b9ec
GM
7023 /* Initialize an iterator for iteration over
7024 F->desired_tool_bar_string in the tool-bar window of frame F. */
7025 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7026 it.first_visible_x = 0;
7027 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7028 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7029
7030 while (!ITERATOR_AT_END_P (&it))
7031 {
7032 it.glyph_row = w->desired_matrix->rows;
7033 clear_glyph_row (it.glyph_row);
e037b9ec 7034 display_tool_bar_line (&it);
5f5c8ee5
GM
7035 }
7036
7037 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7038}
96a410bc 7039
5f5c8ee5 7040
e037b9ec 7041/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7042 height should be changed. */
7043
7044static int
e037b9ec 7045redisplay_tool_bar (f)
5f5c8ee5 7046 struct frame *f;
96a410bc 7047{
5f5c8ee5
GM
7048 struct window *w;
7049 struct it it;
7050 struct glyph_row *row;
7051 int change_height_p = 0;
7052
e037b9ec
GM
7053 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7054 do anything. This means you must start with tool-bar-lines
5f5c8ee5 7055 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
7056 can turn off tool-bars by specifying tool-bar-lines zero. */
7057 if (!WINDOWP (f->tool_bar_window)
7058 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
7059 XFASTINT (w->height) == 0))
7060 return 0;
96a410bc 7061
e037b9ec
GM
7062 /* Set up an iterator for the tool-bar window. */
7063 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7064 it.first_visible_x = 0;
7065 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7066 row = it.glyph_row;
3450d04c 7067
e037b9ec
GM
7068 /* Build a string that represents the contents of the tool-bar. */
7069 build_desired_tool_bar_string (f);
7070 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 7071
e037b9ec 7072 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 7073 while (it.current_y < it.last_visible_y)
e037b9ec 7074 display_tool_bar_line (&it);
3450d04c 7075
e037b9ec 7076 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
7077 window, so don't do it. */
7078 w->desired_matrix->no_scrolling_p = 1;
7079 w->must_be_updated_p = 1;
3450d04c 7080
e037b9ec 7081 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
7082 {
7083 int nlines;
7084
7085 /* If there are blank lines at the end, except for a partially
7086 visible blank line at the end that is smaller than
e037b9ec 7087 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
7088 row = it.glyph_row - 1;
7089 if (!row->displays_text_p
7090 && row->height >= CANON_Y_UNIT (f))
7091 change_height_p = 1;
7092
e037b9ec
GM
7093 /* If row displays tool-bar items, but is partially visible,
7094 change the tool-bar's height. */
5f5c8ee5
GM
7095 if (row->displays_text_p
7096 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7097 change_height_p = 1;
7098
e037b9ec 7099 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
7100 frame parameter. */
7101 if (change_height_p
e037b9ec 7102 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
7103 nlines != XFASTINT (w->height)))
7104 {
e037b9ec 7105 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5
GM
7106 Lisp_Object frame;
7107
7108 XSETFRAME (frame, f);
7109 clear_glyph_matrix (w->desired_matrix);
7110 Fmodify_frame_parameters (frame,
e037b9ec 7111 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
7112 make_number (nlines)),
7113 Qnil));
7114 fonts_changed_p = 1;
7115 }
7116 }
3450d04c 7117
5f5c8ee5 7118 return change_height_p;
96a410bc 7119}
90adcf20 7120
5f5c8ee5 7121
e037b9ec
GM
7122/* Get information about the tool-bar item which is displayed in GLYPH
7123 on frame F. Return in *PROP_IDX the index where tool-bar item
7124 properties start in F->current_tool_bar_items. Value is zero if
7125 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
7126
7127int
e037b9ec 7128tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
7129 struct frame *f;
7130 struct glyph *glyph;
7131 int *prop_idx;
90adcf20 7132{
5f5c8ee5
GM
7133 Lisp_Object prop;
7134 int success_p;
7135
7136 /* Get the text property `menu-item' at pos. The value of that
7137 property is the start index of this item's properties in
e037b9ec 7138 F->current_tool_bar_items. */
5f5c8ee5 7139 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 7140 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
7141 if (INTEGERP (prop))
7142 {
7143 *prop_idx = XINT (prop);
7144 success_p = 1;
7145 }
7146 else
7147 success_p = 0;
90adcf20 7148
5f5c8ee5
GM
7149 return success_p;
7150}
7151
7152#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 7153
feb0c42f 7154
5f5c8ee5
GM
7155\f
7156/************************************************************************
7157 Horizontal scrolling
7158 ************************************************************************/
feb0c42f 7159
5f5c8ee5
GM
7160static int hscroll_window_tree P_ ((Lisp_Object));
7161static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 7162
5f5c8ee5
GM
7163/* For all leaf windows in the window tree rooted at WINDOW, set their
7164 hscroll value so that PT is (i) visible in the window, and (ii) so
7165 that it is not within a certain margin at the window's left and
7166 right border. Value is non-zero if any window's hscroll has been
7167 changed. */
7168
7169static int
7170hscroll_window_tree (window)
7171 Lisp_Object window;
7172{
7173 int hscrolled_p = 0;
7174
7175 while (WINDOWP (window))
90adcf20 7176 {
5f5c8ee5
GM
7177 struct window *w = XWINDOW (window);
7178
7179 if (WINDOWP (w->hchild))
7180 hscrolled_p |= hscroll_window_tree (w->hchild);
7181 else if (WINDOWP (w->vchild))
7182 hscrolled_p |= hscroll_window_tree (w->vchild);
7183 else if (w->cursor.vpos >= 0)
7184 {
7185 int hscroll_margin, text_area_x, text_area_y;
7186 int text_area_width, text_area_height;
92a90e89
GM
7187 struct glyph_row *current_cursor_row
7188 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7189 struct glyph_row *desired_cursor_row
7190 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7191 struct glyph_row *cursor_row
7192 = (desired_cursor_row->enabled_p
7193 ? desired_cursor_row
7194 : current_cursor_row);
a2725ab2 7195
5f5c8ee5
GM
7196 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7197 &text_area_width, &text_area_height);
90adcf20 7198
5f5c8ee5
GM
7199 /* Scroll when cursor is inside this scroll margin. */
7200 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7201
7202 if ((XFASTINT (w->hscroll)
7203 && w->cursor.x < hscroll_margin)
92a90e89
GM
7204 || (cursor_row->enabled_p
7205 && cursor_row->truncated_on_right_p
5f5c8ee5 7206 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 7207 {
5f5c8ee5
GM
7208 struct it it;
7209 int hscroll;
7210 struct buffer *saved_current_buffer;
7211 int pt;
7212
7213 /* Find point in a display of infinite width. */
7214 saved_current_buffer = current_buffer;
7215 current_buffer = XBUFFER (w->buffer);
7216
7217 if (w == XWINDOW (selected_window))
7218 pt = BUF_PT (current_buffer);
7219 else
08b610e4 7220 {
5f5c8ee5
GM
7221 pt = marker_position (w->pointm);
7222 pt = max (BEGV, pt);
7223 pt = min (ZV, pt);
7224 }
7225
7226 /* Move iterator to pt starting at cursor_row->start in
7227 a line with infinite width. */
7228 init_to_row_start (&it, w, cursor_row);
7229 it.last_visible_x = INFINITY;
7230 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7231 current_buffer = saved_current_buffer;
7232
7233 /* Center cursor in window. */
7234 hscroll = (max (0, it.current_x - text_area_width / 2)
7235 / CANON_X_UNIT (it.f));
7236
7237 /* Don't call Fset_window_hscroll if value hasn't
7238 changed because it will prevent redisplay
7239 optimizations. */
7240 if (XFASTINT (w->hscroll) != hscroll)
7241 {
7242 Fset_window_hscroll (window, make_number (hscroll));
7243 hscrolled_p = 1;
08b610e4 7244 }
08b610e4 7245 }
08b610e4 7246 }
a2725ab2 7247
5f5c8ee5 7248 window = w->next;
90adcf20 7249 }
cd6dfed6 7250
5f5c8ee5
GM
7251 /* Value is non-zero if hscroll of any leaf window has been changed. */
7252 return hscrolled_p;
7253}
7254
7255
7256/* Set hscroll so that cursor is visible and not inside horizontal
7257 scroll margins for all windows in the tree rooted at WINDOW. See
7258 also hscroll_window_tree above. Value is non-zero if any window's
7259 hscroll has been changed. If it has, desired matrices on the frame
7260 of WINDOW are cleared. */
7261
7262static int
7263hscroll_windows (window)
7264 Lisp_Object window;
7265{
d475bcb8
GM
7266 int hscrolled_p;
7267
7268 if (automatic_hscrolling_p)
7269 {
7270 hscrolled_p = hscroll_window_tree (window);
7271 if (hscrolled_p)
7272 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7273 }
7274 else
7275 hscrolled_p = 0;
5f5c8ee5 7276 return hscrolled_p;
90adcf20 7277}
5f5c8ee5
GM
7278
7279
90adcf20 7280\f
5f5c8ee5
GM
7281/************************************************************************
7282 Redisplay
7283 ************************************************************************/
7284
7285/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7286 to a non-zero value. This is sometimes handy to have in a debugger
7287 session. */
7288
7289#if GLYPH_DEBUG
a2889657 7290
5f5c8ee5
GM
7291/* First and last unchanged row for try_window_id. */
7292
7293int debug_first_unchanged_at_end_vpos;
7294int debug_last_unchanged_at_beg_vpos;
7295
7296/* Delta vpos and y. */
7297
7298int debug_dvpos, debug_dy;
7299
7300/* Delta in characters and bytes for try_window_id. */
7301
7302int debug_delta, debug_delta_bytes;
7303
7304/* Values of window_end_pos and window_end_vpos at the end of
7305 try_window_id. */
7306
7307int debug_end_pos, debug_end_vpos;
7308
7309/* Append a string to W->desired_matrix->method. FMT is a printf
7310 format string. A1...A9 are a supplement for a variable-length
7311 argument list. If trace_redisplay_p is non-zero also printf the
7312 resulting string to stderr. */
7313
7314static void
7315debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7316 struct window *w;
7317 char *fmt;
7318 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7319{
7320 char buffer[512];
7321 char *method = w->desired_matrix->method;
7322 int len = strlen (method);
7323 int size = sizeof w->desired_matrix->method;
7324 int remaining = size - len - 1;
7325
7326 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7327 if (len && remaining)
7328 {
7329 method[len] = '|';
7330 --remaining, ++len;
7331 }
7332
7333 strncpy (method + len, buffer, remaining);
7334
7335 if (trace_redisplay_p)
7336 fprintf (stderr, "%p (%s): %s\n",
7337 w,
7338 ((BUFFERP (w->buffer)
7339 && STRINGP (XBUFFER (w->buffer)->name))
7340 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7341 : "no buffer"),
7342 buffer);
7343}
a2889657 7344
5f5c8ee5 7345#endif /* GLYPH_DEBUG */
90adcf20 7346
a2889657 7347
5f5c8ee5
GM
7348/* This counter is used to clear the face cache every once in a while
7349 in redisplay_internal. It is incremented for each redisplay.
7350 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7351 cleared. */
0d231165 7352
5f5c8ee5 7353#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
7354static int clear_face_cache_count;
7355
20de20dc 7356/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
7357
7358static struct frame *previous_terminal_frame;
7359
7360/* Non-zero while redisplay_internal is in progress. */
7361
7362int redisplaying_p;
7363
7364
7365/* Value is non-zero if all changes in window W, which displays
7366 current_buffer, are in the text between START and END. START is a
7367 buffer position, END is given as a distance from Z. Used in
7368 redisplay_internal for display optimization. */
7369
7370static INLINE int
7371text_outside_line_unchanged_p (w, start, end)
7372 struct window *w;
7373 int start, end;
7374{
7375 int unchanged_p = 1;
7376
7377 /* If text or overlays have changed, see where. */
7378 if (XFASTINT (w->last_modified) < MODIFF
7379 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7380 {
7381 /* Gap in the line? */
7382 if (GPT < start || Z - GPT < end)
7383 unchanged_p = 0;
7384
7385 /* Changes start in front of the line, or end after it? */
7386 if (unchanged_p
9142dd5b
GM
7387 && (BEG_UNCHANGED < start - 1
7388 || END_UNCHANGED < end))
5f5c8ee5
GM
7389 unchanged_p = 0;
7390
7391 /* If selective display, can't optimize if changes start at the
7392 beginning of the line. */
7393 if (unchanged_p
7394 && INTEGERP (current_buffer->selective_display)
7395 && XINT (current_buffer->selective_display) > 0
9142dd5b 7396 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
7397 unchanged_p = 0;
7398 }
7399
7400 return unchanged_p;
7401}
7402
7403
7404/* Do a frame update, taking possible shortcuts into account. This is
7405 the main external entry point for redisplay.
7406
7407 If the last redisplay displayed an echo area message and that message
7408 is no longer requested, we clear the echo area or bring back the
7409 mini-buffer if that is in use. */
20de20dc 7410
a2889657
JB
7411void
7412redisplay ()
e9874cee
RS
7413{
7414 redisplay_internal (0);
7415}
7416
260a86a0
KH
7417/* Return 1 if point moved out of or into a composition. Otherwise
7418 return 0. PREV_BUF and PREV_PT are the last point buffer and
7419 position. BUF and PT are the current point buffer and position. */
7420
7421int
7422check_point_in_composition (prev_buf, prev_pt, buf, pt)
7423 struct buffer *prev_buf, *buf;
7424 int prev_pt, pt;
7425{
7426 int start, end;
7427 Lisp_Object prop;
7428 Lisp_Object buffer;
7429
7430 XSETBUFFER (buffer, buf);
7431 /* Check a composition at the last point if point moved within the
7432 same buffer. */
7433 if (prev_buf == buf)
7434 {
7435 if (prev_pt == pt)
7436 /* Point didn't move. */
7437 return 0;
7438
7439 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7440 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7441 && COMPOSITION_VALID_P (start, end, prop)
7442 && start < prev_pt && end > prev_pt)
7443 /* The last point was within the composition. Return 1 iff
7444 point moved out of the composition. */
7445 return (pt <= start || pt >= end);
7446 }
7447
7448 /* Check a composition at the current point. */
7449 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7450 && find_composition (pt, -1, &start, &end, &prop, buffer)
7451 && COMPOSITION_VALID_P (start, end, prop)
7452 && start < pt && end > pt);
7453}
5f5c8ee5 7454
9142dd5b
GM
7455/* Reconsider the setting of B->clip_changed which is displayed
7456 in window W. */
7457
7458static INLINE void
7459reconsider_clip_changes (w, b)
7460 struct window *w;
7461 struct buffer *b;
7462{
7463 if (b->prevent_redisplay_optimizations_p)
7464 b->clip_changed = 1;
7465 else if (b->clip_changed
7466 && !NILP (w->window_end_valid)
7467 && w->current_matrix->buffer == b
7468 && w->current_matrix->zv == BUF_ZV (b)
7469 && w->current_matrix->begv == BUF_BEGV (b))
7470 b->clip_changed = 0;
260a86a0
KH
7471
7472 /* If display wasn't paused, and W is not a tool bar window, see if
7473 point has been moved into or out of a composition. In that case,
7474 we set b->clip_changed to 1 to force updating the screen. If
7475 b->clip_changed has already been set to 1, we can skip this
7476 check. */
7477 if (!b->clip_changed
7478 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7479 {
7480 int pt;
7481
7482 if (w == XWINDOW (selected_window))
7483 pt = BUF_PT (current_buffer);
7484 else
7485 pt = marker_position (w->pointm);
7486
7487 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 7488 || pt != XINT (w->last_point))
260a86a0 7489 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 7490 XINT (w->last_point),
260a86a0
KH
7491 XBUFFER (w->buffer), pt))
7492 b->clip_changed = 1;
7493 }
9142dd5b
GM
7494}
7495
7496
5f5c8ee5
GM
7497/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7498 response to any user action; therefore, we should preserve the echo
7499 area. (Actually, our caller does that job.) Perhaps in the future
7500 avoid recentering windows if it is not necessary; currently that
7501 causes some problems. */
e9874cee
RS
7502
7503static void
7504redisplay_internal (preserve_echo_area)
7505 int preserve_echo_area;
a2889657 7506{
5f5c8ee5
GM
7507 struct window *w = XWINDOW (selected_window);
7508 struct frame *f = XFRAME (w->frame);
7509 int pause;
a2889657 7510 int must_finish = 0;
5f5c8ee5 7511 struct text_pos tlbufpos, tlendpos;
89819bdd 7512 int number_of_visible_frames;
28514cd9 7513 int count;
886bd6f2 7514 struct frame *sf = SELECTED_FRAME ();
a2889657 7515
5f5c8ee5
GM
7516 /* Non-zero means redisplay has to consider all windows on all
7517 frames. Zero means, only selected_window is considered. */
7518 int consider_all_windows_p;
7519
7520 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7521
7522 /* No redisplay if running in batch mode or frame is not yet fully
7523 initialized, or redisplay is explicitly turned off by setting
7524 Vinhibit_redisplay. */
7525 if (noninteractive
7526 || !NILP (Vinhibit_redisplay)
7527 || !f->glyphs_initialized_p)
a2889657
JB
7528 return;
7529
5f5c8ee5
GM
7530 /* The flag redisplay_performed_directly_p is set by
7531 direct_output_for_insert when it already did the whole screen
7532 update necessary. */
7533 if (redisplay_performed_directly_p)
7534 {
7535 redisplay_performed_directly_p = 0;
7536 if (!hscroll_windows (selected_window))
7537 return;
7538 }
7539
15f0cf78
RS
7540#ifdef USE_X_TOOLKIT
7541 if (popup_activated ())
7542 return;
7543#endif
7544
28514cd9 7545 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 7546 if (redisplaying_p)
735c094c
KH
7547 return;
7548
28514cd9
GM
7549 /* Record a function that resets redisplaying_p to its old value
7550 when we leave this function. */
7551 count = specpdl_ptr - specpdl;
7552 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7553 ++redisplaying_p;
7554
8b32d885 7555 retry:
bd9d0f3f 7556 pause = 0;
9142dd5b
GM
7557 reconsider_clip_changes (w, current_buffer);
7558
5f5c8ee5
GM
7559 /* If new fonts have been loaded that make a glyph matrix adjustment
7560 necessary, do it. */
7561 if (fonts_changed_p)
7562 {
7563 adjust_glyphs (NULL);
7564 ++windows_or_buffers_changed;
7565 fonts_changed_p = 0;
7566 }
7567
886bd6f2
GM
7568 if (! FRAME_WINDOW_P (sf)
7569 && previous_terminal_frame != sf)
20de20dc 7570 {
5f5c8ee5
GM
7571 /* Since frames on an ASCII terminal share the same display
7572 area, displaying a different frame means redisplay the whole
7573 thing. */
20de20dc 7574 windows_or_buffers_changed++;
886bd6f2
GM
7575 SET_FRAME_GARBAGED (sf);
7576 XSETFRAME (Vterminal_frame, sf);
20de20dc 7577 }
886bd6f2 7578 previous_terminal_frame = sf;
20de20dc 7579
5f5c8ee5
GM
7580 /* Set the visible flags for all frames. Do this before checking
7581 for resized or garbaged frames; they want to know if their frames
7582 are visible. See the comment in frame.h for
7583 FRAME_SAMPLE_VISIBILITY. */
d724d989 7584 {
35f56f96 7585 Lisp_Object tail, frame;
d724d989 7586
89819bdd
RS
7587 number_of_visible_frames = 0;
7588
35f56f96 7589 FOR_EACH_FRAME (tail, frame)
f82aff7c 7590 {
5f5c8ee5
GM
7591 struct frame *f = XFRAME (frame);
7592
7593 FRAME_SAMPLE_VISIBILITY (f);
7594 if (FRAME_VISIBLE_P (f))
7595 ++number_of_visible_frames;
7596 clear_desired_matrices (f);
f82aff7c 7597 }
d724d989
JB
7598 }
7599
44fa5b1e 7600 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 7601 do_pending_window_change (1);
a2889657 7602
5f5c8ee5 7603 /* Clear frames marked as garbaged. */
44fa5b1e 7604 if (frame_garbaged)
c6e89d6c 7605 clear_garbaged_frames ();
a2889657 7606
e037b9ec 7607 /* Build menubar and tool-bar items. */
f82aff7c
RS
7608 prepare_menu_bars ();
7609
28995e67 7610 if (windows_or_buffers_changed)
a2889657
JB
7611 update_mode_lines++;
7612
538f13d4
RS
7613 /* Detect case that we need to write or remove a star in the mode line. */
7614 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
7615 {
7616 w->update_mode_line = Qt;
7617 if (buffer_shared > 1)
7618 update_mode_lines++;
7619 }
7620
5f5c8ee5 7621 /* If %c is in the mode line, update it if needed. */
28995e67
RS
7622 if (!NILP (w->column_number_displayed)
7623 /* This alternative quickly identifies a common case
7624 where no change is needed. */
7625 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7626 && XFASTINT (w->last_modified) >= MODIFF
7627 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7628 && XFASTINT (w->column_number_displayed) != current_column ())
7629 w->update_mode_line = Qt;
7630
44fa5b1e 7631 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 7632
5f5c8ee5
GM
7633 /* The variable buffer_shared is set in redisplay_window and
7634 indicates that we redisplay a buffer in different windows. See
7635 there. */
7636 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
7637
7638 /* If specs for an arrow have changed, do thorough redisplay
7639 to ensure we remove any arrow that should no longer exist. */
d45de95b 7640 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 7641 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 7642 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 7643
90adcf20
RS
7644 /* Normally the message* functions will have already displayed and
7645 updated the echo area, but the frame may have been trashed, or
7646 the update may have been preempted, so display the echo area
c6e89d6c
GM
7647 again here. Checking both message buffers captures the case that
7648 the echo area should be cleared. */
7649 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 7650 {
c6e89d6c 7651 int window_height_changed_p = echo_area_display (0);
90adcf20 7652 must_finish = 1;
dd2eb166 7653
c6e89d6c
GM
7654 if (fonts_changed_p)
7655 goto retry;
7656 else if (window_height_changed_p)
7657 {
7658 consider_all_windows_p = 1;
7659 ++update_mode_lines;
7660 ++windows_or_buffers_changed;
9142dd5b
GM
7661
7662 /* If window configuration was changed, frames may have been
7663 marked garbaged. Clear them or we will experience
7664 surprises wrt scrolling. */
7665 if (frame_garbaged)
7666 clear_garbaged_frames ();
c6e89d6c 7667 }
90adcf20 7668 }
97a36635 7669 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
7670 && (current_buffer->clip_changed
7671 || XFASTINT (w->last_modified) < MODIFF
7672 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 7673 && resize_mini_window (w, 0))
c6e89d6c
GM
7674 {
7675 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
7676 showing if its contents might have changed. */
7677 must_finish = 1;
7678 consider_all_windows_p = 1;
c6e89d6c 7679 ++windows_or_buffers_changed;
dd2eb166 7680 ++update_mode_lines;
9142dd5b
GM
7681
7682 /* If window configuration was changed, frames may have been
7683 marked garbaged. Clear them or we will experience
7684 surprises wrt scrolling. */
7685 if (frame_garbaged)
7686 clear_garbaged_frames ();
c6e89d6c
GM
7687 }
7688
90adcf20 7689
5f5c8ee5
GM
7690 /* If showing the region, and mark has changed, we must redisplay
7691 the whole window. The assignment to this_line_start_pos prevents
7692 the optimization directly below this if-statement. */
bd66d1ba
RS
7693 if (((!NILP (Vtransient_mark_mode)
7694 && !NILP (XBUFFER (w->buffer)->mark_active))
7695 != !NILP (w->region_showing))
82d04750
JB
7696 || (!NILP (w->region_showing)
7697 && !EQ (w->region_showing,
7698 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
7699 CHARPOS (this_line_start_pos) = 0;
7700
7701 /* Optimize the case that only the line containing the cursor in the
7702 selected window has changed. Variables starting with this_ are
7703 set in display_line and record information about the line
7704 containing the cursor. */
7705 tlbufpos = this_line_start_pos;
7706 tlendpos = this_line_end_pos;
7707 if (!consider_all_windows_p
7708 && CHARPOS (tlbufpos) > 0
7709 && NILP (w->update_mode_line)
73af359d 7710 && !current_buffer->clip_changed
44fa5b1e 7711 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 7712 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 7713 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
7714 && this_line_buffer == current_buffer
7715 && current_buffer == XBUFFER (w->buffer)
265a9e55 7716 && NILP (w->force_start)
5f5c8ee5
GM
7717 /* Point must be on the line that we have info recorded about. */
7718 && PT >= CHARPOS (tlbufpos)
7719 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
7720 /* All text outside that line, including its final newline,
7721 must be unchanged */
5f5c8ee5
GM
7722 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7723 CHARPOS (tlendpos)))
7724 {
7725 if (CHARPOS (tlbufpos) > BEGV
7726 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7727 && (CHARPOS (tlbufpos) == ZV
7728 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
7729 /* Former continuation line has disappeared by becoming empty */
7730 goto cancel;
7731 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 7732 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
7733 || MINI_WINDOW_P (w))
7734 {
1c9241f5
KH
7735 /* We have to handle the case of continuation around a
7736 wide-column character (See the comment in indent.c around
7737 line 885).
7738
7739 For instance, in the following case:
7740
7741 -------- Insert --------
7742 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7743 J_I_ ==> J_I_ `^^' are cursors.
7744 ^^ ^^
7745 -------- --------
7746
7747 As we have to redraw the line above, we should goto cancel. */
7748
5f5c8ee5
GM
7749 struct it it;
7750 int line_height_before = this_line_pixel_height;
7751
7752 /* Note that start_display will handle the case that the
7753 line starting at tlbufpos is a continuation lines. */
7754 start_display (&it, w, tlbufpos);
7755
7756 /* Implementation note: It this still necessary? */
7757 if (it.current_x != this_line_start_x)
1c9241f5
KH
7758 goto cancel;
7759
5f5c8ee5
GM
7760 TRACE ((stderr, "trying display optimization 1\n"));
7761 w->cursor.vpos = -1;
a2889657 7762 overlay_arrow_seen = 0;
5f5c8ee5
GM
7763 it.vpos = this_line_vpos;
7764 it.current_y = this_line_y;
7765 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7766 display_line (&it);
7767
a2889657 7768 /* If line contains point, is not continued,
5f5c8ee5
GM
7769 and ends at same distance from eob as before, we win */
7770 if (w->cursor.vpos >= 0
7771 /* Line is not continued, otherwise this_line_start_pos
7772 would have been set to 0 in display_line. */
7773 && CHARPOS (this_line_start_pos)
7774 /* Line ends as before. */
7775 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7776 /* Line has same height as before. Otherwise other lines
7777 would have to be shifted up or down. */
7778 && this_line_pixel_height == line_height_before)
a2889657 7779 {
5f5c8ee5
GM
7780 /* If this is not the window's last line, we must adjust
7781 the charstarts of the lines below. */
7782 if (it.current_y < it.last_visible_y)
7783 {
7784 struct glyph_row *row
7785 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7786 int delta, delta_bytes;
7787
7788 if (Z - CHARPOS (tlendpos) == ZV)
7789 {
7790 /* This line ends at end of (accessible part of)
7791 buffer. There is no newline to count. */
7792 delta = (Z
7793 - CHARPOS (tlendpos)
7794 - MATRIX_ROW_START_CHARPOS (row));
7795 delta_bytes = (Z_BYTE
7796 - BYTEPOS (tlendpos)
7797 - MATRIX_ROW_START_BYTEPOS (row));
7798 }
7799 else
7800 {
7801 /* This line ends in a newline. Must take
7802 account of the newline and the rest of the
7803 text that follows. */
7804 delta = (Z
7805 - CHARPOS (tlendpos)
7806 - MATRIX_ROW_START_CHARPOS (row));
7807 delta_bytes = (Z_BYTE
7808 - BYTEPOS (tlendpos)
7809 - MATRIX_ROW_START_BYTEPOS (row));
7810 }
7811
f2d86d7a
GM
7812 increment_matrix_positions (w->current_matrix,
7813 this_line_vpos + 1,
7814 w->current_matrix->nrows,
7815 delta, delta_bytes);
85bcef6c 7816 }
46db8486 7817
5f5c8ee5
GM
7818 /* If this row displays text now but previously didn't,
7819 or vice versa, w->window_end_vpos may have to be
7820 adjusted. */
7821 if ((it.glyph_row - 1)->displays_text_p)
7822 {
7823 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7824 XSETINT (w->window_end_vpos, this_line_vpos);
7825 }
7826 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7827 && this_line_vpos > 0)
7828 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7829 w->window_end_valid = Qnil;
7830
7831 /* Update hint: No need to try to scroll in update_window. */
7832 w->desired_matrix->no_scrolling_p = 1;
7833
7834#if GLYPH_DEBUG
7835 *w->desired_matrix->method = 0;
7836 debug_method_add (w, "optimization 1");
7837#endif
a2889657
JB
7838 goto update;
7839 }
7840 else
7841 goto cancel;
7842 }
5f5c8ee5
GM
7843 else if (/* Cursor position hasn't changed. */
7844 PT == XFASTINT (w->last_point)
b6f0fe04
RS
7845 /* Make sure the cursor was last displayed
7846 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
7847 && 0 <= w->cursor.vpos
7848 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
7849 {
7850 if (!must_finish)
7851 {
c6e89d6c 7852 do_pending_window_change (1);
5f5c8ee5
GM
7853
7854 /* We used to always goto end_of_redisplay here, but this
7855 isn't enough if we have a blinking cursor. */
7856 if (w->cursor_off_p == w->last_cursor_off_p)
7857 goto end_of_redisplay;
a2889657
JB
7858 }
7859 goto update;
7860 }
8b51f1e3
KH
7861 /* If highlighting the region, or if the cursor is in the echo area,
7862 then we can't just move the cursor. */
bd66d1ba
RS
7863 else if (! (!NILP (Vtransient_mark_mode)
7864 && !NILP (current_buffer->mark_active))
97a36635 7865 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 7866 || highlight_nonselected_windows)
8b51f1e3 7867 && NILP (w->region_showing)
8f897821 7868 && NILP (Vshow_trailing_whitespace)
8b51f1e3 7869 && !cursor_in_echo_area)
a2889657 7870 {
5f5c8ee5
GM
7871 struct it it;
7872 struct glyph_row *row;
7873
7874 /* Skip from tlbufpos to PT and see where it is. Note that
7875 PT may be in invisible text. If so, we will end at the
7876 next visible position. */
7877 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7878 NULL, DEFAULT_FACE_ID);
7879 it.current_x = this_line_start_x;
7880 it.current_y = this_line_y;
7881 it.vpos = this_line_vpos;
7882
7883 /* The call to move_it_to stops in front of PT, but
7884 moves over before-strings. */
7885 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7886
7887 if (it.vpos == this_line_vpos
7888 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7889 row->enabled_p))
a2889657 7890 {
5f5c8ee5
GM
7891 xassert (this_line_vpos == it.vpos);
7892 xassert (this_line_y == it.current_y);
7893 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
7894 goto update;
7895 }
7896 else
7897 goto cancel;
7898 }
5f5c8ee5 7899
a2889657 7900 cancel:
5f5c8ee5
GM
7901 /* Text changed drastically or point moved off of line. */
7902 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
7903 }
7904
5f5c8ee5
GM
7905 CHARPOS (this_line_start_pos) = 0;
7906 consider_all_windows_p |= buffer_shared > 1;
7907 ++clear_face_cache_count;
a2889657 7908
5f5c8ee5 7909
bd9d0f3f
GM
7910 /* Build desired matrices, and update the display. If
7911 consider_all_windows_p is non-zero, do it for all windows on all
7912 frames. Otherwise do it for selected_window, only. */
463f6b91 7913
5f5c8ee5 7914 if (consider_all_windows_p)
a2889657 7915 {
35f56f96 7916 Lisp_Object tail, frame;
a2889657 7917
5f5c8ee5
GM
7918 /* Clear the face cache eventually. */
7919 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 7920 {
5f5c8ee5 7921 clear_face_cache (0);
463f6b91
RS
7922 clear_face_cache_count = 0;
7923 }
31b24551 7924
5f5c8ee5
GM
7925 /* Recompute # windows showing selected buffer. This will be
7926 incremented each time such a window is displayed. */
a2889657
JB
7927 buffer_shared = 0;
7928
35f56f96 7929 FOR_EACH_FRAME (tail, frame)
30c566e4 7930 {
5f5c8ee5 7931 struct frame *f = XFRAME (frame);
bd9d0f3f 7932
886bd6f2 7933 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 7934 {
5f5c8ee5
GM
7935 /* Mark all the scroll bars to be removed; we'll redeem
7936 the ones we want when we redisplay their windows. */
9769686d
RS
7937 if (condemn_scroll_bars_hook)
7938 (*condemn_scroll_bars_hook) (f);
30c566e4 7939
f21ef775 7940 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 7941 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 7942
5f5c8ee5
GM
7943 /* Any scroll bars which redisplay_windows should have
7944 nuked should now go away. */
9769686d
RS
7945 if (judge_scroll_bars_hook)
7946 (*judge_scroll_bars_hook) (f);
bd9d0f3f
GM
7947
7948 /* If fonts changed, display again. */
7949 if (fonts_changed_p)
7950 goto retry;
7951
7952 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7953 {
7954 /* See if we have to hscroll. */
7955 if (hscroll_windows (f->root_window))
7956 goto retry;
7957
7958 /* Prevent various kinds of signals during display
7959 update. stdio is not robust about handling
7960 signals, which can cause an apparent I/O
7961 error. */
7962 if (interrupt_input)
7963 unrequest_sigio ();
7964 stop_polling ();
7965
7966 /* Update the display. */
7967 set_window_update_flags (XWINDOW (f->root_window), 1);
7968 pause |= update_frame (f, 0, 0);
7969 if (pause)
7970 break;
7971
7972 mark_window_display_accurate (f->root_window, 1);
7973 if (frame_up_to_date_hook)
7974 frame_up_to_date_hook (f);
7975 }
9769686d 7976 }
30c566e4 7977 }
a2889657 7978 }
bd9d0f3f
GM
7979 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
7980 {
7981 Lisp_Object mini_window;
7982 struct frame *mini_frame;
5f5c8ee5 7983
bd9d0f3f 7984 redisplay_window (selected_window, 1);
5f5c8ee5 7985
bd9d0f3f
GM
7986 /* Compare desired and current matrices, perform output. */
7987 update:
5f5c8ee5 7988
bd9d0f3f
GM
7989 /* If fonts changed, display again. */
7990 if (fonts_changed_p)
92a90e89 7991 goto retry;
a2889657 7992
bd9d0f3f
GM
7993 /* Prevent various kinds of signals during display update.
7994 stdio is not robust about handling signals,
7995 which can cause an apparent I/O error. */
7996 if (interrupt_input)
7997 unrequest_sigio ();
7998 stop_polling ();
1af9f229 7999
bd9d0f3f 8000 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 8001 {
92a90e89
GM
8002 if (hscroll_windows (selected_window))
8003 goto retry;
8004
5f5c8ee5 8005 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 8006 pause = update_frame (sf, 0, 0);
5f5c8ee5 8007 }
d724d989 8008
8de2d90b 8009 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
8010 function. If the echo area is on another frame, that may
8011 have put text on a frame other than the selected one, so the
8012 above call to update_frame would not have caught it. Catch
8de2d90b 8013 it here. */
bd9d0f3f
GM
8014 mini_window = FRAME_MINIBUF_WINDOW (sf);
8015 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 8016
bd9d0f3f
GM
8017 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8018 {
8019 XWINDOW (mini_window)->must_be_updated_p = 1;
8020 pause |= update_frame (mini_frame, 0, 0);
8021 if (!pause && hscroll_windows (mini_window))
8022 goto retry;
8023 }
6e8290aa 8024 }
a2889657 8025
5f5c8ee5
GM
8026 /* If display was paused because of pending input, make sure we do a
8027 thorough update the next time. */
a2889657
JB
8028 if (pause)
8029 {
5f5c8ee5
GM
8030 /* Prevent the optimization at the beginning of
8031 redisplay_internal that tries a single-line update of the
8032 line containing the cursor in the selected window. */
8033 CHARPOS (this_line_start_pos) = 0;
8034
8035 /* Let the overlay arrow be updated the next time. */
265a9e55 8036 if (!NILP (last_arrow_position))
a2889657
JB
8037 {
8038 last_arrow_position = Qt;
8039 last_arrow_string = Qt;
8040 }
5f5c8ee5
GM
8041
8042 /* If we pause after scrolling, some rows in the current
8043 matrices of some windows are not valid. */
8044 if (!WINDOW_FULL_WIDTH_P (w)
8045 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
8046 update_mode_lines = 1;
8047 }
8048
5f5c8ee5
GM
8049 /* Now text on frame agrees with windows, so put info into the
8050 windows for partial redisplay to follow. */
a2889657
JB
8051 if (!pause)
8052 {
8053 register struct buffer *b = XBUFFER (w->buffer);
8054
9142dd5b
GM
8055 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8056 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8057 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8058 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 8059
5f5c8ee5 8060 if (consider_all_windows_p)
886bd6f2 8061 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
a2889657
JB
8062 else
8063 {
5f5c8ee5
GM
8064 XSETFASTINT (w->last_point, BUF_PT (b));
8065 w->last_cursor = w->cursor;
8066 w->last_cursor_off_p = w->cursor_off_p;
8067
28995e67 8068 b->clip_changed = 0;
9142dd5b 8069 b->prevent_redisplay_optimizations_p = 0;
a2889657 8070 w->update_mode_line = Qnil;
c2213350 8071 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 8072 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
8073 w->last_had_star
8074 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8075 ? Qt : Qnil);
3ee4159a
RS
8076
8077 /* Record if we are showing a region, so can make sure to
8078 update it fully at next redisplay. */
8079 w->region_showing = (!NILP (Vtransient_mark_mode)
97a36635
GM
8080 && (EQ (selected_window,
8081 current_buffer->last_selected_window)
293a54ce 8082 || highlight_nonselected_windows)
3ee4159a
RS
8083 && !NILP (XBUFFER (w->buffer)->mark_active)
8084 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8085 : Qnil);
8086
d2f84654 8087 w->window_end_valid = w->buffer;
d45de95b 8088 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 8089 last_arrow_string = Voverlay_arrow_string;
efc63ef0 8090 if (frame_up_to_date_hook != 0)
886bd6f2 8091 (*frame_up_to_date_hook) (sf);
9142dd5b
GM
8092
8093 w->current_matrix->buffer = b;
8094 w->current_matrix->begv = BUF_BEGV (b);
8095 w->current_matrix->zv = BUF_ZV (b);
a2889657 8096 }
15e26c76 8097
a2889657
JB
8098 update_mode_lines = 0;
8099 windows_or_buffers_changed = 0;
8100 }
8101
5f5c8ee5
GM
8102 /* Start SIGIO interrupts coming again. Having them off during the
8103 code above makes it less likely one will discard output, but not
8104 impossible, since there might be stuff in the system buffer here.
a2889657 8105 But it is much hairier to try to do anything about that. */
a2889657
JB
8106 if (interrupt_input)
8107 request_sigio ();
8108 start_polling ();
8109
5f5c8ee5
GM
8110 /* If a frame has become visible which was not before, redisplay
8111 again, so that we display it. Expose events for such a frame
8112 (which it gets when becoming visible) don't call the parts of
8113 redisplay constructing glyphs, so simply exposing a frame won't
8114 display anything in this case. So, we have to display these
8115 frames here explicitly. */
11c52c4f
RS
8116 if (!pause)
8117 {
8118 Lisp_Object tail, frame;
8119 int new_count = 0;
8120
8121 FOR_EACH_FRAME (tail, frame)
8122 {
8123 int this_is_visible = 0;
8e83f802
RS
8124
8125 if (XFRAME (frame)->visible)
8126 this_is_visible = 1;
8127 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8128 if (XFRAME (frame)->visible)
8129 this_is_visible = 1;
11c52c4f
RS
8130
8131 if (this_is_visible)
8132 new_count++;
8133 }
8134
89819bdd 8135 if (new_count != number_of_visible_frames)
11c52c4f
RS
8136 windows_or_buffers_changed++;
8137 }
8138
44fa5b1e 8139 /* Change frame size now if a change is pending. */
c6e89d6c 8140 do_pending_window_change (1);
d8e242fd 8141
8b32d885
RS
8142 /* If we just did a pending size change, or have additional
8143 visible frames, redisplay again. */
3c8c72e0 8144 if (windows_or_buffers_changed && !pause)
8b32d885 8145 goto retry;
5f5c8ee5
GM
8146
8147 end_of_redisplay:;
c6e89d6c 8148
28514cd9 8149 unbind_to (count, Qnil);
a2889657
JB
8150}
8151
5f5c8ee5
GM
8152
8153/* Redisplay, but leave alone any recent echo area message unless
8154 another message has been requested in its place.
a2889657
JB
8155
8156 This is useful in situations where you need to redisplay but no
8157 user action has occurred, making it inappropriate for the message
8158 area to be cleared. See tracking_off and
8159 wait_reading_process_input for examples of these situations. */
8160
8991bb31 8161void
a2889657
JB
8162redisplay_preserve_echo_area ()
8163{
c6e89d6c 8164 if (!NILP (echo_area_buffer[1]))
a2889657 8165 {
c6e89d6c
GM
8166 /* We have a previously displayed message, but no current
8167 message. Redisplay the previous message. */
8168 display_last_displayed_message_p = 1;
e9874cee 8169 redisplay_internal (1);
c6e89d6c 8170 display_last_displayed_message_p = 0;
a2889657
JB
8171 }
8172 else
e9874cee 8173 redisplay_internal (1);
a2889657
JB
8174}
8175
5f5c8ee5 8176
28514cd9
GM
8177/* Function registered with record_unwind_protect in
8178 redisplay_internal. Clears the flag indicating that a redisplay is
8179 in progress. */
8180
8181static Lisp_Object
8182unwind_redisplay (old_redisplaying_p)
8183 Lisp_Object old_redisplaying_p;
8184{
8185 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 8186 return Qnil;
28514cd9
GM
8187}
8188
8189
5f5c8ee5
GM
8190/* Mark the display of windows in the window tree rooted at WINDOW as
8191 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8192 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8193 the next time redisplay_internal is called. */
8194
a2889657 8195void
5f5c8ee5 8196mark_window_display_accurate (window, accurate_p)
a2889657 8197 Lisp_Object window;
5f5c8ee5 8198 int accurate_p;
a2889657 8199{
5f5c8ee5
GM
8200 struct window *w;
8201
8202 for (; !NILP (window); window = w->next)
a2889657
JB
8203 {
8204 w = XWINDOW (window);
8205
5f5c8ee5 8206 if (BUFFERP (w->buffer))
bd66d1ba 8207 {
5f5c8ee5
GM
8208 struct buffer *b = XBUFFER (w->buffer);
8209
c2213350 8210 XSETFASTINT (w->last_modified,
5f5c8ee5 8211 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 8212 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
8213 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8214 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8215 ? Qt : Qnil);
bd66d1ba 8216
5f5c8ee5
GM
8217#if 0 /* I don't think this is necessary because display_line does it.
8218 Let's check it. */
bd66d1ba
RS
8219 /* Record if we are showing a region, so can make sure to
8220 update it fully at next redisplay. */
5f5c8ee5
GM
8221 w->region_showing
8222 = (!NILP (Vtransient_mark_mode)
8223 && (w == XWINDOW (current_buffer->last_selected_window)
8224 || highlight_nonselected_windows)
8225 && (!NILP (b->mark_active)
8226 ? Fmarker_position (b->mark)
8227 : Qnil));
8228#endif
8229
8230 if (accurate_p)
8231 {
8232 b->clip_changed = 0;
9142dd5b
GM
8233 b->prevent_redisplay_optimizations_p = 0;
8234 w->current_matrix->buffer = b;
8235 w->current_matrix->begv = BUF_BEGV (b);
8236 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
8237 w->last_cursor = w->cursor;
8238 w->last_cursor_off_p = w->cursor_off_p;
8239 if (w == XWINDOW (selected_window))
6fc556fd 8240 w->last_point = make_number (BUF_PT (b));
5f5c8ee5 8241 else
6fc556fd 8242 w->last_point = make_number (XMARKER (w->pointm)->charpos);
5f5c8ee5 8243 }
bd66d1ba
RS
8244 }
8245
d2f84654 8246 w->window_end_valid = w->buffer;
a2889657
JB
8247 w->update_mode_line = Qnil;
8248
265a9e55 8249 if (!NILP (w->vchild))
5f5c8ee5 8250 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 8251 if (!NILP (w->hchild))
5f5c8ee5 8252 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
8253 }
8254
5f5c8ee5 8255 if (accurate_p)
a2889657 8256 {
d45de95b 8257 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
8258 last_arrow_string = Voverlay_arrow_string;
8259 }
8260 else
8261 {
5f5c8ee5
GM
8262 /* Force a thorough redisplay the next time by setting
8263 last_arrow_position and last_arrow_string to t, which is
8264 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
8265 last_arrow_position = Qt;
8266 last_arrow_string = Qt;
8267 }
8268}
5f5c8ee5
GM
8269
8270
8271/* Return value in display table DP (Lisp_Char_Table *) for character
8272 C. Since a display table doesn't have any parent, we don't have to
8273 follow parent. Do not call this function directly but use the
8274 macro DISP_CHAR_VECTOR. */
8275
8276Lisp_Object
8277disp_char_vector (dp, c)
8278 struct Lisp_Char_Table *dp;
8279 int c;
8280{
8281 int code[4], i;
8282 Lisp_Object val;
8283
8284 if (SINGLE_BYTE_CHAR_P (c))
8285 return (dp->contents[c]);
8286
c5924f47 8287 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
8288 if (code[1] < 32)
8289 code[1] = -1;
8290 else if (code[2] < 32)
8291 code[2] = -1;
5f5c8ee5
GM
8292
8293 /* Here, the possible range of code[0] (== charset ID) is
8294 128..max_charset. Since the top level char table contains data
8295 for multibyte characters after 256th element, we must increment
8296 code[0] by 128 to get a correct index. */
8297 code[0] += 128;
8298 code[3] = -1; /* anchor */
8299
8300 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8301 {
8302 val = dp->contents[code[i]];
8303 if (!SUB_CHAR_TABLE_P (val))
8304 return (NILP (val) ? dp->defalt : val);
8305 }
8306
8307 /* Here, val is a sub char table. We return the default value of
8308 it. */
8309 return (dp->defalt);
8310}
8311
8312
a2889657 8313\f
5f5c8ee5
GM
8314/***********************************************************************
8315 Window Redisplay
8316 ***********************************************************************/
a2725ab2 8317
5f5c8ee5 8318/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
8319
8320static void
5f5c8ee5
GM
8321redisplay_windows (window)
8322 Lisp_Object window;
90adcf20 8323{
5f5c8ee5
GM
8324 while (!NILP (window))
8325 {
8326 struct window *w = XWINDOW (window);
8327
8328 if (!NILP (w->hchild))
8329 redisplay_windows (w->hchild);
8330 else if (!NILP (w->vchild))
8331 redisplay_windows (w->vchild);
8332 else
8333 redisplay_window (window, 0);
a2725ab2 8334
5f5c8ee5
GM
8335 window = w->next;
8336 }
8337}
8338
8339
8340/* Set cursor position of W. PT is assumed to be displayed in ROW.
8341 DELTA is the number of bytes by which positions recorded in ROW
8342 differ from current buffer positions. */
8343
8344void
8345set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8346 struct window *w;
8347 struct glyph_row *row;
8348 struct glyph_matrix *matrix;
8349 int delta, delta_bytes, dy, dvpos;
8350{
8351 struct glyph *glyph = row->glyphs[TEXT_AREA];
8352 struct glyph *end = glyph + row->used[TEXT_AREA];
8353 int x = row->x;
8354 int pt_old = PT - delta;
8355
8356 /* Skip over glyphs not having an object at the start of the row.
8357 These are special glyphs like truncation marks on terminal
8358 frames. */
8359 if (row->displays_text_p)
8360 while (glyph < end
6fc556fd 8361 && INTEGERP (glyph->object)
5f5c8ee5
GM
8362 && glyph->charpos < 0)
8363 {
8364 x += glyph->pixel_width;
8365 ++glyph;
8366 }
8367
8368 while (glyph < end
6fc556fd 8369 && !INTEGERP (glyph->object)
5f5c8ee5
GM
8370 && (!BUFFERP (glyph->object)
8371 || glyph->charpos < pt_old))
8372 {
8373 x += glyph->pixel_width;
8374 ++glyph;
8375 }
8376
8377 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8378 w->cursor.x = x;
8379 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8380 w->cursor.y = row->y + dy;
8381
8382 if (w == XWINDOW (selected_window))
8383 {
8384 if (!row->continued_p
8385 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8386 && row->x == 0)
8387 {
8388 this_line_buffer = XBUFFER (w->buffer);
8389
8390 CHARPOS (this_line_start_pos)
8391 = MATRIX_ROW_START_CHARPOS (row) + delta;
8392 BYTEPOS (this_line_start_pos)
8393 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8394
8395 CHARPOS (this_line_end_pos)
8396 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8397 BYTEPOS (this_line_end_pos)
8398 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8399
8400 this_line_y = w->cursor.y;
8401 this_line_pixel_height = row->height;
8402 this_line_vpos = w->cursor.vpos;
8403 this_line_start_x = row->x;
8404 }
8405 else
8406 CHARPOS (this_line_start_pos) = 0;
8407 }
8408}
8409
8410
8411/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
8412 start STARTP. Sets the window start of WINDOW to that position.
8413
8414 We assume that the window's buffer is really current. */
5f5c8ee5
GM
8415
8416static INLINE struct text_pos
8417run_window_scroll_functions (window, startp)
8418 Lisp_Object window;
8419 struct text_pos startp;
8420{
8421 struct window *w = XWINDOW (window);
8422 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
8423
8424 if (current_buffer != XBUFFER (w->buffer))
8425 abort ();
8426
5f5c8ee5
GM
8427 if (!NILP (Vwindow_scroll_functions))
8428 {
8429 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8430 make_number (CHARPOS (startp)));
8431 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
8432 /* In case the hook functions switch buffers. */
8433 if (current_buffer != XBUFFER (w->buffer))
8434 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8435 }
90adcf20 8436
5f5c8ee5
GM
8437 return startp;
8438}
8439
8440
8441/* Modify the desired matrix of window W and W->vscroll so that the
8442 line containing the cursor is fully visible. */
8443
8444static void
8445make_cursor_line_fully_visible (w)
8446 struct window *w;
8447{
8448 struct glyph_matrix *matrix;
8449 struct glyph_row *row;
b28cb6ed 8450 int window_height, header_line_height;
5f5c8ee5
GM
8451
8452 /* It's not always possible to find the cursor, e.g, when a window
8453 is full of overlay strings. Don't do anything in that case. */
8454 if (w->cursor.vpos < 0)
8455 return;
8456
8457 matrix = w->desired_matrix;
8458 row = MATRIX_ROW (matrix, w->cursor.vpos);
8459
b28cb6ed
GM
8460 /* If the cursor row is not partially visible, there's nothing
8461 to do. */
8462 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8463 return;
8464
8465 /* If the row the cursor is in is taller than the window's height,
8466 it's not clear what to do, so do nothing. */
8467 window_height = window_box_height (w);
8468 if (row->height >= window_height)
8469 return;
8470
8471 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
8472 {
8473 int dy = row->height - row->visible_height;
8474 w->vscroll = 0;
8475 w->cursor.y += dy;
8476 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8477 }
b28cb6ed 8478 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
8479 {
8480 int dy = - (row->height - row->visible_height);
8481 w->vscroll = dy;
8482 w->cursor.y += dy;
8483 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8484 }
b28cb6ed 8485
5f5c8ee5
GM
8486 /* When we change the cursor y-position of the selected window,
8487 change this_line_y as well so that the display optimization for
8488 the cursor line of the selected window in redisplay_internal uses
8489 the correct y-position. */
8490 if (w == XWINDOW (selected_window))
8491 this_line_y = w->cursor.y;
8492}
8493
8494
8495/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8496 non-zero means only WINDOW is redisplayed in redisplay_internal.
8497 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8498 in redisplay_window to bring a partially visible line into view in
8499 the case that only the cursor has moved.
8500
8501 Value is
8502
8503 1 if scrolling succeeded
8504
8505 0 if scrolling didn't find point.
8506
8507 -1 if new fonts have been loaded so that we must interrupt
8508 redisplay, adjust glyph matrices, and try again. */
8509
8510static int
8511try_scrolling (window, just_this_one_p, scroll_conservatively,
8512 scroll_step, temp_scroll_step)
8513 Lisp_Object window;
8514 int just_this_one_p;
8515 int scroll_conservatively, scroll_step;
8516 int temp_scroll_step;
8517{
8518 struct window *w = XWINDOW (window);
8519 struct frame *f = XFRAME (w->frame);
8520 struct text_pos scroll_margin_pos;
8521 struct text_pos pos;
8522 struct text_pos startp;
8523 struct it it;
8524 Lisp_Object window_end;
8525 int this_scroll_margin;
8526 int dy = 0;
8527 int scroll_max;
8528 int line_height, rc;
8529 int amount_to_scroll = 0;
8530 Lisp_Object aggressive;
8531 int height;
8532
8533#if GLYPH_DEBUG
8534 debug_method_add (w, "try_scrolling");
78614721 8535#endif
5f5c8ee5
GM
8536
8537 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8538
8539 /* Compute scroll margin height in pixels. We scroll when point is
8540 within this distance from the top or bottom of the window. */
8541 if (scroll_margin > 0)
90adcf20 8542 {
5f5c8ee5
GM
8543 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8544 this_scroll_margin *= CANON_Y_UNIT (f);
8545 }
8546 else
8547 this_scroll_margin = 0;
8548
8549 /* Compute how much we should try to scroll maximally to bring point
8550 into view. */
8551 if (scroll_step)
8552 scroll_max = scroll_step;
8553 else if (scroll_conservatively)
8554 scroll_max = scroll_conservatively;
8555 else if (temp_scroll_step)
8556 scroll_max = temp_scroll_step;
8557 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8558 || NUMBERP (current_buffer->scroll_up_aggressively))
8559 /* We're trying to scroll because of aggressive scrolling
8560 but no scroll_step is set. Choose an arbitrary one. Maybe
8561 there should be a variable for this. */
8562 scroll_max = 10;
8563 else
8564 scroll_max = 0;
8565 scroll_max *= CANON_Y_UNIT (f);
8566
8567 /* Decide whether we have to scroll down. Start at the window end
8568 and move this_scroll_margin up to find the position of the scroll
8569 margin. */
8570 window_end = Fwindow_end (window, Qt);
8571 CHARPOS (scroll_margin_pos) = XINT (window_end);
8572 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8573 if (this_scroll_margin)
8574 {
8575 start_display (&it, w, scroll_margin_pos);
8576 move_it_vertically (&it, - this_scroll_margin);
8577 scroll_margin_pos = it.current.pos;
8578 }
8579
8580 if (PT >= CHARPOS (scroll_margin_pos))
8581 {
8582 int y0;
8583
8584 /* Point is in the scroll margin at the bottom of the window, or
8585 below. Compute a new window start that makes point visible. */
47589c8c 8586
5f5c8ee5
GM
8587 /* Compute the distance from the scroll margin to PT.
8588 Give up if the distance is greater than scroll_max. */
8589 start_display (&it, w, scroll_margin_pos);
8590 y0 = it.current_y;
8591 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8592 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8593 line_height = (it.max_ascent + it.max_descent
8594 ? it.max_ascent + it.max_descent
8595 : last_height);
8596 dy = it.current_y + line_height - y0;
47589c8c 8597
5f5c8ee5
GM
8598 if (dy > scroll_max)
8599 return 0;
8600
8601 /* Move the window start down. If scrolling conservatively,
8602 move it just enough down to make point visible. If
8603 scroll_step is set, move it down by scroll_step. */
8604 start_display (&it, w, startp);
8605
8606 if (scroll_conservatively)
8607 amount_to_scroll = dy;
8608 else if (scroll_step || temp_scroll_step)
8609 amount_to_scroll = scroll_max;
8610 else
90adcf20 8611 {
5f5c8ee5
GM
8612 aggressive = current_buffer->scroll_down_aggressively;
8613 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8614 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8615 if (NUMBERP (aggressive))
8616 amount_to_scroll = XFLOATINT (aggressive) * height;
8617 }
a2725ab2 8618
5f5c8ee5
GM
8619 if (amount_to_scroll <= 0)
8620 return 0;
a2725ab2 8621
5f5c8ee5
GM
8622 move_it_vertically (&it, amount_to_scroll);
8623 startp = it.current.pos;
8624 }
8625 else
8626 {
8627 /* See if point is inside the scroll margin at the top of the
8628 window. */
8629 scroll_margin_pos = startp;
8630 if (this_scroll_margin)
8631 {
8632 start_display (&it, w, startp);
8633 move_it_vertically (&it, this_scroll_margin);
8634 scroll_margin_pos = it.current.pos;
8635 }
8636
8637 if (PT < CHARPOS (scroll_margin_pos))
8638 {
8639 /* Point is in the scroll margin at the top of the window or
8640 above what is displayed in the window. */
8641 int y0;
8642
8643 /* Compute the vertical distance from PT to the scroll
8644 margin position. Give up if distance is greater than
8645 scroll_max. */
8646 SET_TEXT_POS (pos, PT, PT_BYTE);
8647 start_display (&it, w, pos);
8648 y0 = it.current_y;
8649 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8650 it.last_visible_y, -1,
8651 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8652 dy = it.current_y - y0;
8653 if (dy > scroll_max)
8654 return 0;
8655
8656 /* Compute new window start. */
8657 start_display (&it, w, startp);
8658
8659 if (scroll_conservatively)
8660 amount_to_scroll = dy;
8661 else if (scroll_step || temp_scroll_step)
8662 amount_to_scroll = scroll_max;
538f13d4 8663 else
5f5c8ee5
GM
8664 {
8665 aggressive = current_buffer->scroll_up_aggressively;
8666 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8667 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8668 if (NUMBERP (aggressive))
8669 amount_to_scroll = XFLOATINT (aggressive) * height;
8670 }
a2725ab2 8671
5f5c8ee5
GM
8672 if (amount_to_scroll <= 0)
8673 return 0;
8674
8675 move_it_vertically (&it, - amount_to_scroll);
8676 startp = it.current.pos;
90adcf20
RS
8677 }
8678 }
a2889657 8679
5f5c8ee5
GM
8680 /* Run window scroll functions. */
8681 startp = run_window_scroll_functions (window, startp);
90adcf20 8682
5f5c8ee5
GM
8683 /* Display the window. Give up if new fonts are loaded, or if point
8684 doesn't appear. */
8685 if (!try_window (window, startp))
8686 rc = -1;
8687 else if (w->cursor.vpos < 0)
8688 {
8689 clear_glyph_matrix (w->desired_matrix);
8690 rc = 0;
8691 }
8692 else
8693 {
8694 /* Maybe forget recorded base line for line number display. */
8695 if (!just_this_one_p
8696 || current_buffer->clip_changed
9142dd5b 8697 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
8698 w->base_line_number = Qnil;
8699
8700 /* If cursor ends up on a partially visible line, shift display
8701 lines up or down. */
8702 make_cursor_line_fully_visible (w);
8703 rc = 1;
8704 }
8705
8706 return rc;
a2889657
JB
8707}
8708
5f5c8ee5
GM
8709
8710/* Compute a suitable window start for window W if display of W starts
8711 on a continuation line. Value is non-zero if a new window start
8712 was computed.
8713
8714 The new window start will be computed, based on W's width, starting
8715 from the start of the continued line. It is the start of the
8716 screen line with the minimum distance from the old start W->start. */
8717
8718static int
8719compute_window_start_on_continuation_line (w)
8720 struct window *w;
1f1ff51d 8721{
5f5c8ee5
GM
8722 struct text_pos pos, start_pos;
8723 int window_start_changed_p = 0;
1f1ff51d 8724
5f5c8ee5 8725 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 8726
5f5c8ee5
GM
8727 /* If window start is on a continuation line... Window start may be
8728 < BEGV in case there's invisible text at the start of the
8729 buffer (M-x rmail, for example). */
8730 if (CHARPOS (start_pos) > BEGV
8731 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 8732 {
5f5c8ee5
GM
8733 struct it it;
8734 struct glyph_row *row;
f3751a65
GM
8735
8736 /* Handle the case that the window start is out of range. */
8737 if (CHARPOS (start_pos) < BEGV)
8738 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8739 else if (CHARPOS (start_pos) > ZV)
8740 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
8741
8742 /* Find the start of the continued line. This should be fast
8743 because scan_buffer is fast (newline cache). */
045dee35 8744 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
8745 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8746 row, DEFAULT_FACE_ID);
8747 reseat_at_previous_visible_line_start (&it);
8748
8749 /* If the line start is "too far" away from the window start,
8750 say it takes too much time to compute a new window start. */
8751 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8752 < XFASTINT (w->height) * XFASTINT (w->width))
8753 {
8754 int min_distance, distance;
8755
8756 /* Move forward by display lines to find the new window
8757 start. If window width was enlarged, the new start can
8758 be expected to be > the old start. If window width was
8759 decreased, the new window start will be < the old start.
8760 So, we're looking for the display line start with the
8761 minimum distance from the old window start. */
8762 pos = it.current.pos;
8763 min_distance = INFINITY;
8764 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8765 distance < min_distance)
8766 {
8767 min_distance = distance;
8768 pos = it.current.pos;
8769 move_it_by_lines (&it, 1, 0);
8770 }
8771
8772 /* Set the window start there. */
8773 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8774 window_start_changed_p = 1;
8775 }
1f1ff51d 8776 }
5f5c8ee5
GM
8777
8778 return window_start_changed_p;
1f1ff51d
KH
8779}
8780
5f5c8ee5 8781
47589c8c
GM
8782/* Try cursor movement in case text has not changes in window WINDOW,
8783 with window start STARTP. Value is
8784
8785 1 if successful
8786
8787 0 if this method cannot be used
8788
8789 -1 if we know we have to scroll the display. *SCROLL_STEP is
8790 set to 1, under certain circumstances, if we want to scroll as
8791 if scroll-step were set to 1. See the code. */
8792
8793static int
8794try_cursor_movement (window, startp, scroll_step)
8795 Lisp_Object window;
8796 struct text_pos startp;
8797 int *scroll_step;
8798{
8799 struct window *w = XWINDOW (window);
8800 struct frame *f = XFRAME (w->frame);
8801 int rc = 0;
8802
8803 /* Handle case where text has not changed, only point, and it has
8804 not moved off the frame. */
8805 if (/* Point may be in this window. */
8806 PT >= CHARPOS (startp)
8807 /* If we don't check this, we are called to move the cursor in a
8808 horizontally split window with a current matrix that doesn't
8809 fit the display. */
8810 && !windows_or_buffers_changed
8811 /* Selective display hasn't changed. */
8812 && !current_buffer->clip_changed
8813 /* If force-mode-line-update was called, really redisplay;
8814 that's how redisplay is forced after e.g. changing
8815 buffer-invisibility-spec. */
8816 && NILP (w->update_mode_line)
8817 /* Can't use this case if highlighting a region. When a
8818 region exists, cursor movement has to do more than just
8819 set the cursor. */
8820 && !(!NILP (Vtransient_mark_mode)
8821 && !NILP (current_buffer->mark_active))
8822 && NILP (w->region_showing)
8823 && NILP (Vshow_trailing_whitespace)
8824 /* Right after splitting windows, last_point may be nil. */
8825 && INTEGERP (w->last_point)
8826 /* This code is not used for mini-buffer for the sake of the case
8827 of redisplaying to replace an echo area message; since in
8828 that case the mini-buffer contents per se are usually
8829 unchanged. This code is of no real use in the mini-buffer
8830 since the handling of this_line_start_pos, etc., in redisplay
8831 handles the same cases. */
8832 && !EQ (window, minibuf_window)
8833 /* When splitting windows or for new windows, it happens that
8834 redisplay is called with a nil window_end_vpos or one being
8835 larger than the window. This should really be fixed in
8836 window.c. I don't have this on my list, now, so we do
8837 approximately the same as the old redisplay code. --gerd. */
8838 && INTEGERP (w->window_end_vpos)
8839 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8840 && (FRAME_WINDOW_P (f)
8841 || !MARKERP (Voverlay_arrow_position)
8842 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8843 {
8844 int this_scroll_margin;
8845 struct glyph_row *row;
8846
8847#if GLYPH_DEBUG
8848 debug_method_add (w, "cursor movement");
8849#endif
8850
8851 /* Scroll if point within this distance from the top or bottom
8852 of the window. This is a pixel value. */
8853 this_scroll_margin = max (0, scroll_margin);
8854 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8855 this_scroll_margin *= CANON_Y_UNIT (f);
8856
8857 /* Start with the row the cursor was displayed during the last
8858 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
8859 if (w->last_cursor.vpos < 0
8860 || w->last_cursor.vpos >= w->current_matrix->nrows)
47589c8c
GM
8861 rc = -1;
8862 else
8863 {
8864 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8865 if (row->mode_line_p)
8866 ++row;
8867 if (!row->enabled_p)
8868 rc = -1;
8869 }
8870
8871 if (rc == 0)
8872 {
8873 int scroll_p = 0;
8874
8875 if (PT > XFASTINT (w->last_point))
8876 {
8877 /* Point has moved forward. */
8878 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8879
8880 while (MATRIX_ROW_END_CHARPOS (row) < PT
8881 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8882 {
8883 xassert (row->enabled_p);
8884 ++row;
8885 }
8886
8887 /* The end position of a row equals the start position
8888 of the next row. If PT is there, we would rather
8889 display it in the next line. Exceptions are when the
8890 row ends in the middle of a character, or ends in
8891 ZV. */
8892 if (MATRIX_ROW_BOTTOM_Y (row) < last_y
8893 && MATRIX_ROW_END_CHARPOS (row) == PT
8894 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8895 && !row->ends_at_zv_p)
8896 {
8897 xassert (row->enabled_p);
8898 ++row;
8899 }
8900
8901 /* If within the scroll margin, scroll. Note that
8902 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
8903 the next line would be drawn, and that
8904 this_scroll_margin can be zero. */
8905 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8906 || PT > MATRIX_ROW_END_CHARPOS (row)
8907 /* Line is completely visible last line in window
8908 and PT is to be set in the next line. */
8909 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8910 && PT == MATRIX_ROW_END_CHARPOS (row)
8911 && !row->ends_at_zv_p
8912 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8913 scroll_p = 1;
8914 }
8915 else if (PT < XFASTINT (w->last_point))
8916 {
8917 /* Cursor has to be moved backward. Note that PT >=
8918 CHARPOS (startp) because of the outer
8919 if-statement. */
8920 while (!row->mode_line_p
8921 && (MATRIX_ROW_START_CHARPOS (row) > PT
8922 || (MATRIX_ROW_START_CHARPOS (row) == PT
8923 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8924 && (row->y > this_scroll_margin
8925 || CHARPOS (startp) == BEGV))
8926 {
8927 xassert (row->enabled_p);
8928 --row;
8929 }
8930
8931 /* Consider the following case: Window starts at BEGV,
8932 there is invisible, intangible text at BEGV, so that
8933 display starts at some point START > BEGV. It can
8934 happen that we are called with PT somewhere between
8935 BEGV and START. Try to handle that case. */
8936 if (row < w->current_matrix->rows
8937 || row->mode_line_p)
8938 {
8939 row = w->current_matrix->rows;
8940 if (row->mode_line_p)
8941 ++row;
8942 }
8943
8944 /* Due to newlines in overlay strings, we may have to
8945 skip forward over overlay strings. */
8946 while (MATRIX_ROW_END_CHARPOS (row) == PT
8947 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8948 && !row->ends_at_zv_p)
8949 ++row;
8950
8951 /* If within the scroll margin, scroll. */
8952 if (row->y < this_scroll_margin
8953 && CHARPOS (startp) != BEGV)
8954 scroll_p = 1;
8955 }
8956
8957 if (PT < MATRIX_ROW_START_CHARPOS (row)
8958 || PT > MATRIX_ROW_END_CHARPOS (row))
8959 {
8960 /* if PT is not in the glyph row, give up. */
8961 rc = -1;
8962 }
440fc135 8963 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c
GM
8964 {
8965 /* If we end up in a partially visible line, let's make it
8966 fully visible, except when it's taller than the window,
8967 in which case we can't do much about it. */
440fc135
GM
8968 if (row->height > window_box_height (w))
8969 {
8970 *scroll_step = 1;
8971 rc = -1;
8972 }
8973 else
8974 {
8975 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8976 try_window (window, startp);
8977 make_cursor_line_fully_visible (w);
8978 rc = 1;
8979 }
47589c8c
GM
8980 }
8981 else if (scroll_p)
8982 rc = -1;
8983 else
8984 {
8985 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8986 rc = 1;
8987 }
8988 }
8989 }
8990
8991 return rc;
8992}
8993
8994
5f5c8ee5
GM
8995/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8996 selected_window is redisplayed. */
90adcf20 8997
a2889657 8998static void
5f5c8ee5 8999redisplay_window (window, just_this_one_p)
a2889657 9000 Lisp_Object window;
5f5c8ee5 9001 int just_this_one_p;
a2889657 9002{
5f5c8ee5
GM
9003 struct window *w = XWINDOW (window);
9004 struct frame *f = XFRAME (w->frame);
9005 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 9006 struct buffer *old = current_buffer;
5f5c8ee5 9007 struct text_pos lpoint, opoint, startp;
e481f960 9008 int update_mode_line;
5f5c8ee5
GM
9009 int tem;
9010 struct it it;
9011 /* Record it now because it's overwritten. */
9012 int current_matrix_up_to_date_p = 0;
5f5c8ee5 9013 int temp_scroll_step = 0;
2e54982e 9014 int count = specpdl_ptr - specpdl;
47589c8c 9015 int rc;
a2889657 9016
5f5c8ee5
GM
9017 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9018 opoint = lpoint;
a2889657 9019
5f5c8ee5
GM
9020 /* W must be a leaf window here. */
9021 xassert (!NILP (w->buffer));
9022#if GLYPH_DEBUG
9023 *w->desired_matrix->method = 0;
9024#endif
2e54982e
RS
9025
9026 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
9027
9028 reconsider_clip_changes (w, buffer);
9029
5f5c8ee5
GM
9030 /* Has the mode line to be updated? */
9031 update_mode_line = (!NILP (w->update_mode_line)
9032 || update_mode_lines
9033 || buffer->clip_changed);
8de2d90b
JB
9034
9035 if (MINI_WINDOW_P (w))
9036 {
5f5c8ee5 9037 if (w == XWINDOW (echo_area_window)
c6e89d6c 9038 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
9039 {
9040 if (update_mode_line)
9041 /* We may have to update a tty frame's menu bar or a
e037b9ec 9042 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
9043 goto finish_menu_bars;
9044 else
9045 /* We've already displayed the echo area glyphs in this window. */
9046 goto finish_scroll_bars;
9047 }
73af359d 9048 else if (w != XWINDOW (minibuf_window))
8de2d90b 9049 {
5f5c8ee5
GM
9050 /* W is a mini-buffer window, but it's not the currently
9051 active one, so clear it. */
9052 int yb = window_text_bottom_y (w);
9053 struct glyph_row *row;
9054 int y;
9055
9056 for (y = 0, row = w->desired_matrix->rows;
9057 y < yb;
9058 y += row->height, ++row)
9059 blank_row (w, row, y);
88f22aff 9060 goto finish_scroll_bars;
8de2d90b
JB
9061 }
9062 }
a2889657 9063
5f5c8ee5
GM
9064 /* Otherwise set up data on this window; select its buffer and point
9065 value. */
6a93695f
GM
9066 /* Really select the buffer, for the sake of buffer-local
9067 variables. */
9068 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
9069 SET_TEXT_POS (opoint, PT, PT_BYTE);
9070
9071 current_matrix_up_to_date_p
9072 = (!NILP (w->window_end_valid)
9073 && !current_buffer->clip_changed
9074 && XFASTINT (w->last_modified) >= MODIFF
9075 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 9076
5f5c8ee5
GM
9077 /* When windows_or_buffers_changed is non-zero, we can't rely on
9078 the window end being valid, so set it to nil there. */
9079 if (windows_or_buffers_changed)
9080 {
9081 /* If window starts on a continuation line, maybe adjust the
9082 window start in case the window's width changed. */
9083 if (XMARKER (w->start)->buffer == current_buffer)
9084 compute_window_start_on_continuation_line (w);
9085
9086 w->window_end_valid = Qnil;
9087 }
12adba34 9088
5f5c8ee5
GM
9089 /* Some sanity checks. */
9090 CHECK_WINDOW_END (w);
9091 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 9092 abort ();
5f5c8ee5 9093 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 9094 abort ();
a2889657 9095
28995e67
RS
9096 /* If %c is in mode line, update it if needed. */
9097 if (!NILP (w->column_number_displayed)
9098 /* This alternative quickly identifies a common case
9099 where no change is needed. */
9100 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9101 && XFASTINT (w->last_modified) >= MODIFF
9102 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
9103 && XFASTINT (w->column_number_displayed) != current_column ())
9104 update_mode_line = 1;
9105
5f5c8ee5
GM
9106 /* Count number of windows showing the selected buffer. An indirect
9107 buffer counts as its base buffer. */
9108 if (!just_this_one_p)
42640f83
RS
9109 {
9110 struct buffer *current_base, *window_base;
9111 current_base = current_buffer;
9112 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9113 if (current_base->base_buffer)
9114 current_base = current_base->base_buffer;
9115 if (window_base->base_buffer)
9116 window_base = window_base->base_buffer;
9117 if (current_base == window_base)
9118 buffer_shared++;
9119 }
a2889657 9120
5f5c8ee5
GM
9121 /* Point refers normally to the selected window. For any other
9122 window, set up appropriate value. */
a2889657
JB
9123 if (!EQ (window, selected_window))
9124 {
12adba34
RS
9125 int new_pt = XMARKER (w->pointm)->charpos;
9126 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 9127 if (new_pt < BEGV)
a2889657 9128 {
f67a0f51 9129 new_pt = BEGV;
12adba34
RS
9130 new_pt_byte = BEGV_BYTE;
9131 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 9132 }
f67a0f51 9133 else if (new_pt > (ZV - 1))
a2889657 9134 {
f67a0f51 9135 new_pt = ZV;
12adba34
RS
9136 new_pt_byte = ZV_BYTE;
9137 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 9138 }
5f5c8ee5 9139
f67a0f51 9140 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 9141 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
9142 }
9143
f4faa47c 9144 /* If any of the character widths specified in the display table
5f5c8ee5
GM
9145 have changed, invalidate the width run cache. It's true that
9146 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
9147 redisplay goes (non-fatally) haywire when the display table is
9148 changed, so why should we worry about doing any better? */
9149 if (current_buffer->width_run_cache)
9150 {
f908610f 9151 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
9152
9153 if (! disptab_matches_widthtab (disptab,
9154 XVECTOR (current_buffer->width_table)))
9155 {
9156 invalidate_region_cache (current_buffer,
9157 current_buffer->width_run_cache,
9158 BEG, Z);
9159 recompute_width_table (current_buffer, disptab);
9160 }
9161 }
9162
a2889657 9163 /* If window-start is screwed up, choose a new one. */
a2889657
JB
9164 if (XMARKER (w->start)->buffer != current_buffer)
9165 goto recenter;
9166
5f5c8ee5 9167 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 9168
cf0df6ab
RS
9169 /* If someone specified a new starting point but did not insist,
9170 check whether it can be used. */
cfad01b4
GM
9171 if (!NILP (w->optional_new_start)
9172 && CHARPOS (startp) >= BEGV
9173 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
9174 {
9175 w->optional_new_start = Qnil;
5f5c8ee5
GM
9176 start_display (&it, w, startp);
9177 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9178 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9179 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
9180 w->force_start = Qt;
9181 }
9182
8de2d90b 9183 /* Handle case where place to start displaying has been specified,
aa6d10fa 9184 unless the specified location is outside the accessible range. */
9472f927
GM
9185 if (!NILP (w->force_start)
9186 || w->frozen_window_start_p)
a2889657 9187 {
e63574d7 9188 w->force_start = Qnil;
5f5c8ee5 9189 w->vscroll = 0;
b5174a51 9190 w->window_end_valid = Qnil;
5f5c8ee5
GM
9191
9192 /* Forget any recorded base line for line number display. */
9193 if (!current_matrix_up_to_date_p
9194 || current_buffer->clip_changed)
9195 w->base_line_number = Qnil;
9196
75c43375
RS
9197 /* Redisplay the mode line. Select the buffer properly for that.
9198 Also, run the hook window-scroll-functions
9199 because we have scrolled. */
e63574d7
RS
9200 /* Note, we do this after clearing force_start because
9201 if there's an error, it is better to forget about force_start
9202 than to get into an infinite loop calling the hook functions
9203 and having them get more errors. */
75c43375
RS
9204 if (!update_mode_line
9205 || ! NILP (Vwindow_scroll_functions))
e481f960 9206 {
e481f960
RS
9207 update_mode_line = 1;
9208 w->update_mode_line = Qt;
5f5c8ee5 9209 startp = run_window_scroll_functions (window, startp);
e481f960 9210 }
5f5c8ee5 9211
c2213350 9212 XSETFASTINT (w->last_modified, 0);
8850a573 9213 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
9214 if (CHARPOS (startp) < BEGV)
9215 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9216 else if (CHARPOS (startp) > ZV)
9217 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9218
9219 /* Redisplay, then check if cursor has been set during the
9220 redisplay. Give up if new fonts were loaded. */
9221 if (!try_window (window, startp))
9222 {
9223 w->force_start = Qt;
9224 clear_glyph_matrix (w->desired_matrix);
9225 goto restore_buffers;
9226 }
9227
9472f927 9228 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 9229 {
b28cb6ed
GM
9230 /* If point does not appear, try to move point so it does
9231 appear. The desired matrix has been built above, so we
9232 can use it here. */
9233 int window_height;
9234 struct glyph_row *row;
9235
9236 window_height = window_box_height (w) / 2;
9237 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9238 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
5f5c8ee5
GM
9239 ++row;
9240
9241 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9242 MATRIX_ROW_START_BYTEPOS (row));
9243
90adcf20 9244 if (w != XWINDOW (selected_window))
12adba34 9245 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
9246 else if (current_buffer == old)
9247 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9248
9249 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9250
9251 /* If we are highlighting the region, then we just changed
9252 the region, so redisplay to show it. */
df0b5ea1
RS
9253 if (!NILP (Vtransient_mark_mode)
9254 && !NILP (current_buffer->mark_active))
6f27fa9b 9255 {
5f5c8ee5
GM
9256 clear_glyph_matrix (w->desired_matrix);
9257 if (!try_window (window, startp))
9258 goto restore_buffers;
6f27fa9b 9259 }
a2889657 9260 }
5f5c8ee5
GM
9261
9262 make_cursor_line_fully_visible (w);
9263#if GLYPH_DEBUG
9264 debug_method_add (w, "forced window start");
9265#endif
a2889657
JB
9266 goto done;
9267 }
9268
5f5c8ee5
GM
9269 /* Handle case where text has not changed, only point, and it has
9270 not moved off the frame. */
9271 if (current_matrix_up_to_date_p
47589c8c
GM
9272 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9273 rc != 0))
a2889657 9274 {
47589c8c 9275 if (rc == -1)
5f5c8ee5 9276 goto try_to_scroll;
47589c8c
GM
9277 else
9278 goto done;
a2889657
JB
9279 }
9280 /* If current starting point was originally the beginning of a line
9281 but no longer is, find a new starting point. */
265a9e55 9282 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
9283 && !(CHARPOS (startp) <= BEGV
9284 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 9285 {
5f5c8ee5
GM
9286#if GLYPH_DEBUG
9287 debug_method_add (w, "recenter 1");
9288#endif
a2889657
JB
9289 goto recenter;
9290 }
5f5c8ee5
GM
9291
9292 /* Try scrolling with try_window_id. */
9142dd5b
GM
9293 else if (/* Windows and buffers haven't changed. */
9294 !windows_or_buffers_changed
5f5c8ee5
GM
9295 /* Window must be either use window-based redisplay or
9296 be full width. */
9297 && (FRAME_WINDOW_P (f)
c59c668a 9298 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
9299 && !MINI_WINDOW_P (w)
9300 /* Point is not known NOT to appear in window. */
9301 && PT >= CHARPOS (startp)
a2889657 9302 && XFASTINT (w->last_modified)
5f5c8ee5
GM
9303 /* Window is not hscrolled. */
9304 && XFASTINT (w->hscroll) == 0
9305 /* Selective display has not changed. */
9306 && !current_buffer->clip_changed
9307 /* Current matrix is up to date. */
9308 && !NILP (w->window_end_valid)
9309 /* Can't use this case if highlighting a region because
9310 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
9311 && !(!NILP (Vtransient_mark_mode)
9312 && !NILP (current_buffer->mark_active))
9313 && NILP (w->region_showing)
8f897821 9314 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 9315 /* Overlay arrow position and string not changed. */
d45de95b 9316 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 9317 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
9318 /* Value is > 0 if update has been done, it is -1 if we
9319 know that the same window start will not work. It is 0
9320 if unsuccessful for some other reason. */
9321 && (tem = try_window_id (w)) != 0)
a2889657 9322 {
5f5c8ee5 9323#if GLYPH_DEBUG
ef121659 9324 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
9325#endif
9326
9327 if (fonts_changed_p)
9328 goto restore_buffers;
a2889657
JB
9329 if (tem > 0)
9330 goto done;
ef121659 9331
5f5c8ee5
GM
9332 /* Otherwise try_window_id has returned -1 which means that we
9333 don't want the alternative below this comment to execute. */
a2889657 9334 }
5f5c8ee5
GM
9335 else if (CHARPOS (startp) >= BEGV
9336 && CHARPOS (startp) <= ZV
9337 && PT >= CHARPOS (startp)
9338 && (CHARPOS (startp) < ZV
e9874cee 9339 /* Avoid starting at end of buffer. */
5f5c8ee5 9340 || CHARPOS (startp) == BEGV
8850a573
RS
9341 || (XFASTINT (w->last_modified) >= MODIFF
9342 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 9343 {
5f5c8ee5
GM
9344#if GLYPH_DEBUG
9345 debug_method_add (w, "same window start");
9346#endif
9347
9348 /* Try to redisplay starting at same place as before.
9349 If point has not moved off frame, accept the results. */
9350 if (!current_matrix_up_to_date_p
9351 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
9352 because a window scroll function can have changed the
9353 buffer. */
5f5c8ee5
GM
9354 || !NILP (Vwindow_scroll_functions)
9355 || MINI_WINDOW_P (w)
9356 || !try_window_reusing_current_matrix (w))
9357 {
9358 IF_DEBUG (debug_method_add (w, "1"));
9359 try_window (window, startp);
9360 }
9361
9362 if (fonts_changed_p)
9363 goto restore_buffers;
9364
9365 if (w->cursor.vpos >= 0)
aa6d10fa 9366 {
5f5c8ee5
GM
9367 if (!just_this_one_p
9368 || current_buffer->clip_changed
9142dd5b 9369 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
9370 /* Forget any recorded base line for line number display. */
9371 w->base_line_number = Qnil;
5f5c8ee5
GM
9372
9373 make_cursor_line_fully_visible (w);
aa6d10fa
RS
9374 goto done;
9375 }
a2889657 9376 else
5f5c8ee5 9377 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
9378 }
9379
5f5c8ee5
GM
9380 try_to_scroll:
9381
c2213350 9382 XSETFASTINT (w->last_modified, 0);
8850a573 9383 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 9384
e481f960
RS
9385 /* Redisplay the mode line. Select the buffer properly for that. */
9386 if (!update_mode_line)
9387 {
e481f960
RS
9388 update_mode_line = 1;
9389 w->update_mode_line = Qt;
9390 }
a2889657 9391
5f5c8ee5
GM
9392 /* Try to scroll by specified few lines. */
9393 if ((scroll_conservatively
9394 || scroll_step
9395 || temp_scroll_step
9396 || NUMBERP (current_buffer->scroll_up_aggressively)
9397 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 9398 && !current_buffer->clip_changed
5f5c8ee5
GM
9399 && CHARPOS (startp) >= BEGV
9400 && CHARPOS (startp) <= ZV)
0789adb2 9401 {
5f5c8ee5
GM
9402 /* The function returns -1 if new fonts were loaded, 1 if
9403 successful, 0 if not successful. */
9404 int rc = try_scrolling (window, just_this_one_p,
9405 scroll_conservatively,
9406 scroll_step,
9407 temp_scroll_step);
9408 if (rc > 0)
9409 goto done;
9410 else if (rc < 0)
9411 goto restore_buffers;
9412 }
f9c8af06 9413
5f5c8ee5 9414 /* Finally, just choose place to start which centers point */
5936754e 9415
5f5c8ee5 9416 recenter:
44173109 9417
5f5c8ee5
GM
9418#if GLYPH_DEBUG
9419 debug_method_add (w, "recenter");
9420#endif
0789adb2 9421
5f5c8ee5 9422 /* w->vscroll = 0; */
0789adb2 9423
5f5c8ee5
GM
9424 /* Forget any previously recorded base line for line number display. */
9425 if (!current_matrix_up_to_date_p
9426 || current_buffer->clip_changed)
9427 w->base_line_number = Qnil;
9428
9429 /* Move backward half the height of the window. */
9430 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9431 it.current_y = it.last_visible_y;
9432 move_it_vertically_backward (&it, it.last_visible_y / 2);
9433 xassert (IT_CHARPOS (it) >= BEGV);
9434
9435 /* The function move_it_vertically_backward may move over more
9436 than the specified y-distance. If it->w is small, e.g. a
9437 mini-buffer window, we may end up in front of the window's
9438 display area. Start displaying at the start of the line
9439 containing PT in this case. */
9440 if (it.current_y <= 0)
9441 {
9442 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9443 move_it_vertically (&it, 0);
9444 xassert (IT_CHARPOS (it) <= PT);
9445 it.current_y = 0;
0789adb2
RS
9446 }
9447
5f5c8ee5
GM
9448 it.current_x = it.hpos = 0;
9449
9450 /* Set startp here explicitly in case that helps avoid an infinite loop
9451 in case the window-scroll-functions functions get errors. */
9452 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9453
9454 /* Run scroll hooks. */
9455 startp = run_window_scroll_functions (window, it.current.pos);
9456
9457 /* Redisplay the window. */
9458 if (!current_matrix_up_to_date_p
9459 || windows_or_buffers_changed
9460 /* Don't use try_window_reusing_current_matrix in this case
9461 because it can have changed the buffer. */
9462 || !NILP (Vwindow_scroll_functions)
9463 || !just_this_one_p
9464 || MINI_WINDOW_P (w)
9465 || !try_window_reusing_current_matrix (w))
9466 try_window (window, startp);
9467
9468 /* If new fonts have been loaded (due to fontsets), give up. We
9469 have to start a new redisplay since we need to re-adjust glyph
9470 matrices. */
9471 if (fonts_changed_p)
9472 goto restore_buffers;
9473
9474 /* If cursor did not appear assume that the middle of the window is
9475 in the first line of the window. Do it again with the next line.
9476 (Imagine a window of height 100, displaying two lines of height
9477 60. Moving back 50 from it->last_visible_y will end in the first
9478 line.) */
9479 if (w->cursor.vpos < 0)
a2889657 9480 {
5f5c8ee5
GM
9481 if (!NILP (w->window_end_valid)
9482 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 9483 {
5f5c8ee5
GM
9484 clear_glyph_matrix (w->desired_matrix);
9485 move_it_by_lines (&it, 1, 0);
9486 try_window (window, it.current.pos);
a2889657 9487 }
5f5c8ee5 9488 else if (PT < IT_CHARPOS (it))
a2889657 9489 {
5f5c8ee5
GM
9490 clear_glyph_matrix (w->desired_matrix);
9491 move_it_by_lines (&it, -1, 0);
9492 try_window (window, it.current.pos);
9493 }
9494 else
9495 {
9496 /* Not much we can do about it. */
a2889657 9497 }
a2889657 9498 }
010494d0 9499
5f5c8ee5
GM
9500 /* Consider the following case: Window starts at BEGV, there is
9501 invisible, intangible text at BEGV, so that display starts at
9502 some point START > BEGV. It can happen that we are called with
9503 PT somewhere between BEGV and START. Try to handle that case. */
9504 if (w->cursor.vpos < 0)
835766b6 9505 {
5f5c8ee5
GM
9506 struct glyph_row *row = w->current_matrix->rows;
9507 if (row->mode_line_p)
9508 ++row;
9509 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 9510 }
5f5c8ee5
GM
9511
9512 make_cursor_line_fully_visible (w);
b5174a51 9513
74d481ac
GM
9514 done:
9515
5f5c8ee5
GM
9516 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9517 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9518 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9519 ? Qt : Qnil);
a2889657 9520
5f5c8ee5 9521 /* Display the mode line, if we must. */
e481f960 9522 if ((update_mode_line
aa6d10fa 9523 /* If window not full width, must redo its mode line
5f5c8ee5
GM
9524 if (a) the window to its side is being redone and
9525 (b) we do a frame-based redisplay. This is a consequence
9526 of how inverted lines are drawn in frame-based redisplay. */
9527 || (!just_this_one_p
9528 && !FRAME_WINDOW_P (f)
9529 && !WINDOW_FULL_WIDTH_P (w))
9530 /* Line number to display. */
155ef550 9531 || INTEGERP (w->base_line_pos)
5f5c8ee5 9532 /* Column number is displayed and different from the one displayed. */
155ef550
KH
9533 || (!NILP (w->column_number_displayed)
9534 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
9535 /* This means that the window has a mode line. */
9536 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 9537 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 9538 {
5da11938
GM
9539 Lisp_Object old_selected_frame;
9540
9541 old_selected_frame = selected_frame;
9542
5da11938 9543 XSETFRAME (selected_frame, f);
5f5c8ee5 9544 display_mode_lines (w);
5da11938 9545 selected_frame = old_selected_frame;
5f5c8ee5
GM
9546
9547 /* If mode line height has changed, arrange for a thorough
9548 immediate redisplay using the correct mode line height. */
9549 if (WINDOW_WANTS_MODELINE_P (w)
9550 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 9551 {
5f5c8ee5
GM
9552 fonts_changed_p = 1;
9553 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9554 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 9555 }
5f5c8ee5
GM
9556
9557 /* If top line height has changed, arrange for a thorough
9558 immediate redisplay using the correct mode line height. */
045dee35
GM
9559 if (WINDOW_WANTS_HEADER_LINE_P (w)
9560 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
9561 {
9562 fonts_changed_p = 1;
045dee35
GM
9563 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9564 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9565 }
9566
9567 if (fonts_changed_p)
9568 goto restore_buffers;
5ba50c51 9569 }
5f5c8ee5
GM
9570
9571 if (!line_number_displayed
9572 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
9573 {
9574 w->base_line_pos = Qnil;
9575 w->base_line_number = Qnil;
9576 }
a2889657 9577
5f5c8ee5
GM
9578 finish_menu_bars:
9579
7ce2c095 9580 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 9581 if (update_mode_line
5f5c8ee5
GM
9582 && EQ (FRAME_SELECTED_WINDOW (f), window))
9583 {
9584 int redisplay_menu_p = 0;
9585
9586 if (FRAME_WINDOW_P (f))
9587 {
dc937613 9588#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 9589 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 9590#else
5f5c8ee5 9591 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 9592#endif
5f5c8ee5
GM
9593 }
9594 else
9595 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9596
9597 if (redisplay_menu_p)
9598 display_menu_bar (w);
9599
9600#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
9601 if (WINDOWP (f->tool_bar_window)
9602 && (FRAME_TOOL_BAR_LINES (f) > 0
9603 || auto_resize_tool_bars_p))
9604 redisplay_tool_bar (f);
5f5c8ee5
GM
9605#endif
9606 }
7ce2c095 9607
88f22aff 9608 finish_scroll_bars:
5f5c8ee5 9609
88f22aff 9610 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 9611 {
b1d1124b 9612 int start, end, whole;
30c566e4 9613
b1d1124b 9614 /* Calculate the start and end positions for the current window.
3505ea70
JB
9615 At some point, it would be nice to choose between scrollbars
9616 which reflect the whole buffer size, with special markers
9617 indicating narrowing, and scrollbars which reflect only the
9618 visible region.
9619
5f5c8ee5 9620 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 9621 if (!MINI_WINDOW_P (w)
5f5c8ee5 9622 || (w == XWINDOW (minibuf_window)
c6e89d6c 9623 && NILP (echo_area_buffer[0])))
b1d1124b 9624 {
8a9311d7 9625 whole = ZV - BEGV;
4d641a15 9626 start = marker_position (w->start) - BEGV;
b1d1124b
JB
9627 /* I don't think this is guaranteed to be right. For the
9628 moment, we'll pretend it is. */
5f5c8ee5 9629 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 9630
5f5c8ee5
GM
9631 if (end < start)
9632 end = start;
9633 if (whole < (end - start))
9634 whole = end - start;
b1d1124b
JB
9635 }
9636 else
9637 start = end = whole = 0;
30c566e4 9638
88f22aff 9639 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 9640 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 9641
5f5c8ee5
GM
9642 /* Note that we actually used the scroll bar attached to this
9643 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 9644 (*redeem_scroll_bar_hook) (w);
30c566e4 9645 }
b1d1124b 9646
5f5c8ee5
GM
9647 restore_buffers:
9648
9649 /* Restore current_buffer and value of point in it. */
9650 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 9651 set_buffer_internal_1 (old);
5f5c8ee5 9652 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
9653
9654 unbind_to (count, Qnil);
a2889657 9655}
a2889657 9656
5f5c8ee5
GM
9657
9658/* Build the complete desired matrix of WINDOW with a window start
9659 buffer position POS. Value is non-zero if successful. It is zero
9660 if fonts were loaded during redisplay which makes re-adjusting
9661 glyph matrices necessary. */
9662
9663int
a2889657
JB
9664try_window (window, pos)
9665 Lisp_Object window;
5f5c8ee5
GM
9666 struct text_pos pos;
9667{
9668 struct window *w = XWINDOW (window);
9669 struct it it;
9670 struct glyph_row *last_text_row = NULL;
9cbab4ff 9671
5f5c8ee5
GM
9672 /* Make POS the new window start. */
9673 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 9674
5f5c8ee5
GM
9675 /* Mark cursor position as unknown. No overlay arrow seen. */
9676 w->cursor.vpos = -1;
a2889657 9677 overlay_arrow_seen = 0;
642eefc6 9678
5f5c8ee5
GM
9679 /* Initialize iterator and info to start at POS. */
9680 start_display (&it, w, pos);
a2889657 9681
5f5c8ee5
GM
9682 /* Display all lines of W. */
9683 while (it.current_y < it.last_visible_y)
9684 {
9685 if (display_line (&it))
9686 last_text_row = it.glyph_row - 1;
9687 if (fonts_changed_p)
9688 return 0;
9689 }
a2889657 9690
5f5c8ee5
GM
9691 /* If bottom moved off end of frame, change mode line percentage. */
9692 if (XFASTINT (w->window_end_pos) <= 0
9693 && Z != IT_CHARPOS (it))
a2889657
JB
9694 w->update_mode_line = Qt;
9695
5f5c8ee5
GM
9696 /* Set window_end_pos to the offset of the last character displayed
9697 on the window from the end of current_buffer. Set
9698 window_end_vpos to its row number. */
9699 if (last_text_row)
9700 {
9701 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9702 w->window_end_bytepos
9703 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9704 XSETFASTINT (w->window_end_pos,
9705 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9706 XSETFASTINT (w->window_end_vpos,
9707 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9708 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9709 ->displays_text_p);
9710 }
9711 else
9712 {
9713 w->window_end_bytepos = 0;
9714 XSETFASTINT (w->window_end_pos, 0);
9715 XSETFASTINT (w->window_end_vpos, 0);
9716 }
9717
a2889657
JB
9718 /* But that is not valid info until redisplay finishes. */
9719 w->window_end_valid = Qnil;
5f5c8ee5 9720 return 1;
a2889657 9721}
5f5c8ee5
GM
9722
9723
a2889657 9724\f
5f5c8ee5
GM
9725/************************************************************************
9726 Window redisplay reusing current matrix when buffer has not changed
9727 ************************************************************************/
9728
9729/* Try redisplay of window W showing an unchanged buffer with a
9730 different window start than the last time it was displayed by
9731 reusing its current matrix. Value is non-zero if successful.
9732 W->start is the new window start. */
a2889657
JB
9733
9734static int
5f5c8ee5
GM
9735try_window_reusing_current_matrix (w)
9736 struct window *w;
a2889657 9737{
5f5c8ee5
GM
9738 struct frame *f = XFRAME (w->frame);
9739 struct glyph_row *row, *bottom_row;
9740 struct it it;
9741 struct run run;
9742 struct text_pos start, new_start;
9743 int nrows_scrolled, i;
9744 struct glyph_row *last_text_row;
9745 struct glyph_row *last_reused_text_row;
9746 struct glyph_row *start_row;
9747 int start_vpos, min_y, max_y;
9748
d18354b6
GM
9749
9750 if (/* This function doesn't handle terminal frames. */
9751 !FRAME_WINDOW_P (f)
9752 /* Don't try to reuse the display if windows have been split
9753 or such. */
9754 || windows_or_buffers_changed)
5f5c8ee5 9755 return 0;
a2889657 9756
5f5c8ee5
GM
9757 /* Can't do this if region may have changed. */
9758 if ((!NILP (Vtransient_mark_mode)
9759 && !NILP (current_buffer->mark_active))
8f897821
GM
9760 || !NILP (w->region_showing)
9761 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 9762 return 0;
a2889657 9763
5f5c8ee5 9764 /* If top-line visibility has changed, give up. */
045dee35
GM
9765 if (WINDOW_WANTS_HEADER_LINE_P (w)
9766 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
9767 return 0;
9768
9769 /* Give up if old or new display is scrolled vertically. We could
9770 make this function handle this, but right now it doesn't. */
9771 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9772 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9773 return 0;
9774
9775 /* The variable new_start now holds the new window start. The old
9776 start `start' can be determined from the current matrix. */
9777 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9778 start = start_row->start.pos;
9779 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 9780
5f5c8ee5
GM
9781 /* Clear the desired matrix for the display below. */
9782 clear_glyph_matrix (w->desired_matrix);
9783
9784 if (CHARPOS (new_start) <= CHARPOS (start))
9785 {
9786 int first_row_y;
9787
9788 IF_DEBUG (debug_method_add (w, "twu1"));
9789
9790 /* Display up to a row that can be reused. The variable
9791 last_text_row is set to the last row displayed that displays
9792 text. */
9793 start_display (&it, w, new_start);
9794 first_row_y = it.current_y;
9795 w->cursor.vpos = -1;
9796 last_text_row = last_reused_text_row = NULL;
9797 while (it.current_y < it.last_visible_y
9798 && IT_CHARPOS (it) < CHARPOS (start)
9799 && !fonts_changed_p)
9800 if (display_line (&it))
9801 last_text_row = it.glyph_row - 1;
9802
9803 /* A value of current_y < last_visible_y means that we stopped
9804 at the previous window start, which in turn means that we
9805 have at least one reusable row. */
9806 if (it.current_y < it.last_visible_y)
a2889657 9807 {
5f5c8ee5
GM
9808 nrows_scrolled = it.vpos;
9809
9810 /* Find PT if not already found in the lines displayed. */
9811 if (w->cursor.vpos < 0)
a2889657 9812 {
5f5c8ee5
GM
9813 int dy = it.current_y - first_row_y;
9814
9815 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9816 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9817 {
9818 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9819 && PT < MATRIX_ROW_END_CHARPOS (row))
9820 {
9821 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9822 dy, nrows_scrolled);
9823 break;
9824 }
9825
9826 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9827 break;
9828
9829 ++row;
9830 }
9831
9832 /* Give up if point was not found. This shouldn't
9833 happen often; not more often than with try_window
9834 itself. */
9835 if (w->cursor.vpos < 0)
9836 {
9837 clear_glyph_matrix (w->desired_matrix);
9838 return 0;
9839 }
a2889657 9840 }
5f5c8ee5
GM
9841
9842 /* Scroll the display. Do it before the current matrix is
9843 changed. The problem here is that update has not yet
9844 run, i.e. part of the current matrix is not up to date.
9845 scroll_run_hook will clear the cursor, and use the
9846 current matrix to get the height of the row the cursor is
9847 in. */
9848 run.current_y = first_row_y;
9849 run.desired_y = it.current_y;
9850 run.height = it.last_visible_y - it.current_y;
15e26c76
GM
9851 if (run.height > 0
9852 && run.current_y != run.desired_y)
a2889657 9853 {
5f5c8ee5
GM
9854 update_begin (f);
9855 rif->update_window_begin_hook (w);
64d1e7d3 9856 rif->clear_mouse_face (w);
5f5c8ee5 9857 rif->scroll_run_hook (w, &run);
64d1e7d3 9858 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 9859 update_end (f);
a2889657 9860 }
5f5c8ee5
GM
9861
9862 /* Shift current matrix down by nrows_scrolled lines. */
9863 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9864 rotate_matrix (w->current_matrix,
9865 start_vpos,
9866 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9867 nrows_scrolled);
9868
9869 /* Disable lines not reused. */
9870 for (i = 0; i < it.vpos; ++i)
9871 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9872
9873 /* Re-compute Y positions. */
9874 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
045dee35 9875 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9876 max_y = it.last_visible_y;
9877 while (row < bottom_row)
d2f84654 9878 {
5f5c8ee5
GM
9879 row->y = it.current_y;
9880
9881 if (row->y < min_y)
9882 row->visible_height = row->height - (min_y - row->y);
9883 else if (row->y + row->height > max_y)
9884 row->visible_height
9885 = row->height - (row->y + row->height - max_y);
9886 else
9887 row->visible_height = row->height;
9888
9889 it.current_y += row->height;
9890 ++it.vpos;
9891
9892 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9893 last_reused_text_row = row;
9894 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9895 break;
9896 ++row;
d2f84654 9897 }
a2889657 9898 }
5f5c8ee5
GM
9899
9900 /* Update window_end_pos etc.; last_reused_text_row is the last
9901 reused row from the current matrix containing text, if any.
9902 The value of last_text_row is the last displayed line
9903 containing text. */
9904 if (last_reused_text_row)
a2889657 9905 {
5f5c8ee5
GM
9906 w->window_end_bytepos
9907 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9908 XSETFASTINT (w->window_end_pos,
9909 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9910 XSETFASTINT (w->window_end_vpos,
9911 MATRIX_ROW_VPOS (last_reused_text_row,
9912 w->current_matrix));
a2889657 9913 }
5f5c8ee5
GM
9914 else if (last_text_row)
9915 {
9916 w->window_end_bytepos
9917 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9918 XSETFASTINT (w->window_end_pos,
9919 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9920 XSETFASTINT (w->window_end_vpos,
9921 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9922 }
9923 else
9924 {
9925 /* This window must be completely empty. */
9926 w->window_end_bytepos = 0;
9927 XSETFASTINT (w->window_end_pos, 0);
9928 XSETFASTINT (w->window_end_vpos, 0);
9929 }
9930 w->window_end_valid = Qnil;
a2889657 9931
5f5c8ee5
GM
9932 /* Update hint: don't try scrolling again in update_window. */
9933 w->desired_matrix->no_scrolling_p = 1;
9934
9935#if GLYPH_DEBUG
9936 debug_method_add (w, "try_window_reusing_current_matrix 1");
9937#endif
9938 return 1;
a2889657 9939 }
5f5c8ee5
GM
9940 else if (CHARPOS (new_start) > CHARPOS (start))
9941 {
9942 struct glyph_row *pt_row, *row;
9943 struct glyph_row *first_reusable_row;
9944 struct glyph_row *first_row_to_display;
9945 int dy;
9946 int yb = window_text_bottom_y (w);
9947
9948 IF_DEBUG (debug_method_add (w, "twu2"));
9949
9950 /* Find the row starting at new_start, if there is one. Don't
9951 reuse a partially visible line at the end. */
9952 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9953 while (first_reusable_row->enabled_p
9954 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9955 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9956 < CHARPOS (new_start)))
9957 ++first_reusable_row;
9958
9959 /* Give up if there is no row to reuse. */
9960 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
9961 || !first_reusable_row->enabled_p
9962 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9963 != CHARPOS (new_start)))
5f5c8ee5
GM
9964 return 0;
9965
5f5c8ee5
GM
9966 /* We can reuse fully visible rows beginning with
9967 first_reusable_row to the end of the window. Set
9968 first_row_to_display to the first row that cannot be reused.
9969 Set pt_row to the row containing point, if there is any. */
9970 first_row_to_display = first_reusable_row;
9971 pt_row = NULL;
9972 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9973 {
9974 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9975 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9976 pt_row = first_row_to_display;
a2889657 9977
5f5c8ee5
GM
9978 ++first_row_to_display;
9979 }
a2889657 9980
5f5c8ee5
GM
9981 /* Start displaying at the start of first_row_to_display. */
9982 xassert (first_row_to_display->y < yb);
9983 init_to_row_start (&it, w, first_row_to_display);
9984 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9985 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9986 - nrows_scrolled);
9987 it.current_y = first_row_to_display->y - first_reusable_row->y;
9988
9989 /* Display lines beginning with first_row_to_display in the
9990 desired matrix. Set last_text_row to the last row displayed
9991 that displays text. */
9992 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9993 if (pt_row == NULL)
9994 w->cursor.vpos = -1;
9995 last_text_row = NULL;
9996 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9997 if (display_line (&it))
9998 last_text_row = it.glyph_row - 1;
9999
10000 /* Give up If point isn't in a row displayed or reused. */
10001 if (w->cursor.vpos < 0)
10002 {
10003 clear_glyph_matrix (w->desired_matrix);
10004 return 0;
10005 }
12adba34 10006
5f5c8ee5
GM
10007 /* If point is in a reused row, adjust y and vpos of the cursor
10008 position. */
10009 if (pt_row)
10010 {
10011 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10012 w->current_matrix);
10013 w->cursor.y -= first_reusable_row->y;
a2889657
JB
10014 }
10015
5f5c8ee5
GM
10016 /* Scroll the display. */
10017 run.current_y = first_reusable_row->y;
045dee35 10018 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10019 run.height = it.last_visible_y - run.current_y;
10020 if (run.height)
10021 {
10022 struct frame *f = XFRAME (WINDOW_FRAME (w));
10023 update_begin (f);
10024 rif->update_window_begin_hook (w);
64d1e7d3 10025 rif->clear_mouse_face (w);
5f5c8ee5 10026 rif->scroll_run_hook (w, &run);
64d1e7d3 10027 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
10028 update_end (f);
10029 }
a2889657 10030
5f5c8ee5
GM
10031 /* Adjust Y positions of reused rows. */
10032 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10033 row = first_reusable_row;
10034 dy = first_reusable_row->y;
045dee35 10035 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10036 max_y = it.last_visible_y;
10037 while (row < first_row_to_display)
10038 {
10039 row->y -= dy;
10040 if (row->y < min_y)
10041 row->visible_height = row->height - (min_y - row->y);
10042 else if (row->y + row->height > max_y)
10043 row->visible_height
10044 = row->height - (row->y + row->height - max_y);
10045 else
10046 row->visible_height = row->height;
10047 ++row;
10048 }
a2889657 10049
5f5c8ee5
GM
10050 /* Disable rows not reused. */
10051 while (row < bottom_row)
10052 {
10053 row->enabled_p = 0;
10054 ++row;
10055 }
10056
10057 /* Scroll the current matrix. */
10058 xassert (nrows_scrolled > 0);
10059 rotate_matrix (w->current_matrix,
10060 start_vpos,
10061 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10062 -nrows_scrolled);
10063
10064 /* Adjust window end. A null value of last_text_row means that
10065 the window end is in reused rows which in turn means that
10066 only its vpos can have changed. */
10067 if (last_text_row)
10068 {
10069 w->window_end_bytepos
10070 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10071 XSETFASTINT (w->window_end_pos,
10072 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10073 XSETFASTINT (w->window_end_vpos,
10074 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10075 }
10076 else
a2889657 10077 {
e8e536a9 10078 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 10079 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 10080 }
5f5c8ee5
GM
10081
10082 w->window_end_valid = Qnil;
10083 w->desired_matrix->no_scrolling_p = 1;
10084
10085#if GLYPH_DEBUG
10086 debug_method_add (w, "try_window_reusing_current_matrix 2");
10087#endif
10088 return 1;
a2889657 10089 }
5f5c8ee5
GM
10090
10091 return 0;
10092}
a2889657 10093
a2889657 10094
5f5c8ee5
GM
10095\f
10096/************************************************************************
10097 Window redisplay reusing current matrix when buffer has changed
10098 ************************************************************************/
10099
10100static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
10101static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
10102 int *, int *));
10103static struct glyph_row *
10104find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10105 struct glyph_row *));
10106
10107
10108/* Return the last row in MATRIX displaying text. If row START is
10109 non-null, start searching with that row. IT gives the dimensions
10110 of the display. Value is null if matrix is empty; otherwise it is
10111 a pointer to the row found. */
10112
10113static struct glyph_row *
10114find_last_row_displaying_text (matrix, it, start)
10115 struct glyph_matrix *matrix;
10116 struct it *it;
10117 struct glyph_row *start;
10118{
10119 struct glyph_row *row, *row_found;
10120
10121 /* Set row_found to the last row in IT->w's current matrix
10122 displaying text. The loop looks funny but think of partially
10123 visible lines. */
10124 row_found = NULL;
10125 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10126 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10127 {
10128 xassert (row->enabled_p);
10129 row_found = row;
10130 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10131 break;
10132 ++row;
a2889657 10133 }
5f5c8ee5
GM
10134
10135 return row_found;
10136}
10137
a2889657 10138
5f5c8ee5
GM
10139/* Return the last row in the current matrix of W that is not affected
10140 by changes at the start of current_buffer that occurred since the
10141 last time W was redisplayed. Value is null if no such row exists.
a2889657 10142
5f5c8ee5
GM
10143 The global variable beg_unchanged has to contain the number of
10144 bytes unchanged at the start of current_buffer. BEG +
10145 beg_unchanged is the buffer position of the first changed byte in
10146 current_buffer. Characters at positions < BEG + beg_unchanged are
10147 at the same buffer positions as they were when the current matrix
10148 was built. */
10149
10150static struct glyph_row *
10151get_last_unchanged_at_beg_row (w)
10152 struct window *w;
10153{
9142dd5b 10154 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
10155 struct glyph_row *row;
10156 struct glyph_row *row_found = NULL;
10157 int yb = window_text_bottom_y (w);
10158
10159 /* Find the last row displaying unchanged text. */
10160 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10161 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10162 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 10163 {
5f5c8ee5
GM
10164 if (/* If row ends before first_changed_pos, it is unchanged,
10165 except in some case. */
10166 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10167 /* When row ends in ZV and we write at ZV it is not
10168 unchanged. */
10169 && !row->ends_at_zv_p
10170 /* When first_changed_pos is the end of a continued line,
10171 row is not unchanged because it may be no longer
10172 continued. */
10173 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10174 && row->continued_p))
10175 row_found = row;
10176
10177 /* Stop if last visible row. */
10178 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10179 break;
10180
10181 ++row;
a2889657
JB
10182 }
10183
5f5c8ee5 10184 return row_found;
a2889657 10185}
5f5c8ee5
GM
10186
10187
10188/* Find the first glyph row in the current matrix of W that is not
10189 affected by changes at the end of current_buffer since the last
10190 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
10191 chars by which buffer positions in unchanged text at the end of
10192 current_buffer must be adjusted. Return in *DELTA_BYTES the
10193 corresponding number of bytes. Value is null if no such row
10194 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
10195
10196static struct glyph_row *
10197get_first_unchanged_at_end_row (w, delta, delta_bytes)
10198 struct window *w;
10199 int *delta, *delta_bytes;
a2889657 10200{
5f5c8ee5
GM
10201 struct glyph_row *row;
10202 struct glyph_row *row_found = NULL;
c581d710 10203
5f5c8ee5 10204 *delta = *delta_bytes = 0;
b2a76982 10205
5f5c8ee5
GM
10206 /* A value of window_end_pos >= end_unchanged means that the window
10207 end is in the range of changed text. If so, there is no
10208 unchanged row at the end of W's current matrix. */
10209 xassert (!NILP (w->window_end_valid));
9142dd5b 10210 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
10211 return NULL;
10212
10213 /* Set row to the last row in W's current matrix displaying text. */
10214 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10215
5f5c8ee5
GM
10216 /* If matrix is entirely empty, no unchanged row exists. */
10217 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10218 {
10219 /* The value of row is the last glyph row in the matrix having a
10220 meaningful buffer position in it. The end position of row
10221 corresponds to window_end_pos. This allows us to translate
10222 buffer positions in the current matrix to current buffer
10223 positions for characters not in changed text. */
10224 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10225 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10226 int last_unchanged_pos, last_unchanged_pos_old;
10227 struct glyph_row *first_text_row
10228 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10229
10230 *delta = Z - Z_old;
10231 *delta_bytes = Z_BYTE - Z_BYTE_old;
10232
10233 /* Set last_unchanged_pos to the buffer position of the last
10234 character in the buffer that has not been changed. Z is the
10235 index + 1 of the last byte in current_buffer, i.e. by
10236 subtracting end_unchanged we get the index of the last
10237 unchanged character, and we have to add BEG to get its buffer
10238 position. */
9142dd5b 10239 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
10240 last_unchanged_pos_old = last_unchanged_pos - *delta;
10241
10242 /* Search backward from ROW for a row displaying a line that
10243 starts at a minimum position >= last_unchanged_pos_old. */
10244 while (row >= first_text_row)
10245 {
10246 xassert (row->enabled_p);
10247 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
10248
10249 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10250 row_found = row;
10251 --row;
10252 }
10253 }
10254
10255 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
10256 return row_found;
c581d710
RS
10257}
10258
c581d710 10259
5f5c8ee5
GM
10260/* Make sure that glyph rows in the current matrix of window W
10261 reference the same glyph memory as corresponding rows in the
10262 frame's frame matrix. This function is called after scrolling W's
10263 current matrix on a terminal frame in try_window_id and
10264 try_window_reusing_current_matrix. */
10265
10266static void
10267sync_frame_with_window_matrix_rows (w)
10268 struct window *w;
c581d710 10269{
5f5c8ee5
GM
10270 struct frame *f = XFRAME (w->frame);
10271 struct glyph_row *window_row, *window_row_end, *frame_row;
10272
10273 /* Preconditions: W must be a leaf window and full-width. Its frame
10274 must have a frame matrix. */
10275 xassert (NILP (w->hchild) && NILP (w->vchild));
10276 xassert (WINDOW_FULL_WIDTH_P (w));
10277 xassert (!FRAME_WINDOW_P (f));
10278
10279 /* If W is a full-width window, glyph pointers in W's current matrix
10280 have, by definition, to be the same as glyph pointers in the
10281 corresponding frame matrix. */
10282 window_row = w->current_matrix->rows;
10283 window_row_end = window_row + w->current_matrix->nrows;
10284 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10285 while (window_row < window_row_end)
659a218f 10286 {
5f5c8ee5 10287 int area;
f002db93 10288
5f5c8ee5
GM
10289 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10290 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
10291
10292 /* Disable frame rows whose corresponding window rows have
10293 been disabled in try_window_id. */
10294 if (!window_row->enabled_p)
10295 frame_row->enabled_p = 0;
10296
5f5c8ee5 10297 ++window_row, ++frame_row;
659a218f 10298 }
a2889657 10299}
5f5c8ee5
GM
10300
10301
e037b9ec
GM
10302/* Find the glyph row in window W containing CHARPOS. Consider all
10303 rows between START and END (not inclusive). END null means search
10304 all rows to the end of the display area of W. Value is the row
10305 containing CHARPOS or null. */
10306
10307static struct glyph_row *
10308row_containing_pos (w, charpos, start, end)
10309 struct window *w;
10310 int charpos;
10311 struct glyph_row *start, *end;
10312{
10313 struct glyph_row *row = start;
10314 int last_y;
10315
10316 /* If we happen to start on a header-line, skip that. */
10317 if (row->mode_line_p)
10318 ++row;
10319
10320 if ((end && row >= end) || !row->enabled_p)
10321 return NULL;
10322
10323 last_y = window_text_bottom_y (w);
10324
10325 while ((end == NULL || row < end)
10326 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10327 /* The end position of a row equals the start
10328 position of the next row. If CHARPOS is there, we
10329 would rather display it in the next line, except
10330 when this line ends in ZV. */
10331 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10332 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10333 || !row->ends_at_zv_p)))
10334 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10335 ++row;
10336
10337 /* Give up if CHARPOS not found. */
10338 if ((end && row >= end)
10339 || charpos < MATRIX_ROW_START_CHARPOS (row)
10340 || charpos > MATRIX_ROW_END_CHARPOS (row))
10341 row = NULL;
10342
10343 return row;
10344}
10345
10346
5f5c8ee5
GM
10347/* Try to redisplay window W by reusing its existing display. W's
10348 current matrix must be up to date when this function is called,
10349 i.e. window_end_valid must not be nil.
10350
10351 Value is
10352
10353 1 if display has been updated
10354 0 if otherwise unsuccessful
10355 -1 if redisplay with same window start is known not to succeed
10356
10357 The following steps are performed:
10358
10359 1. Find the last row in the current matrix of W that is not
10360 affected by changes at the start of current_buffer. If no such row
10361 is found, give up.
10362
10363 2. Find the first row in W's current matrix that is not affected by
10364 changes at the end of current_buffer. Maybe there is no such row.
10365
10366 3. Display lines beginning with the row + 1 found in step 1 to the
10367 row found in step 2 or, if step 2 didn't find a row, to the end of
10368 the window.
10369
10370 4. If cursor is not known to appear on the window, give up.
10371
10372 5. If display stopped at the row found in step 2, scroll the
10373 display and current matrix as needed.
10374
10375 6. Maybe display some lines at the end of W, if we must. This can
10376 happen under various circumstances, like a partially visible line
10377 becoming fully visible, or because newly displayed lines are displayed
10378 in smaller font sizes.
10379
10380 7. Update W's window end information. */
10381
10382 /* Check that window end is what we expect it to be. */
12adba34
RS
10383
10384static int
5f5c8ee5 10385try_window_id (w)
12adba34 10386 struct window *w;
12adba34 10387{
5f5c8ee5
GM
10388 struct frame *f = XFRAME (w->frame);
10389 struct glyph_matrix *current_matrix = w->current_matrix;
10390 struct glyph_matrix *desired_matrix = w->desired_matrix;
10391 struct glyph_row *last_unchanged_at_beg_row;
10392 struct glyph_row *first_unchanged_at_end_row;
10393 struct glyph_row *row;
10394 struct glyph_row *bottom_row;
10395 int bottom_vpos;
10396 struct it it;
10397 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10398 struct text_pos start_pos;
10399 struct run run;
10400 int first_unchanged_at_end_vpos = 0;
10401 struct glyph_row *last_text_row, *last_text_row_at_end;
10402 struct text_pos start;
10403
10404 SET_TEXT_POS_FROM_MARKER (start, w->start);
10405
10406 /* Check pre-conditions. Window end must be valid, otherwise
10407 the current matrix would not be up to date. */
10408 xassert (!NILP (w->window_end_valid));
10409 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10410 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10411
10412 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10413 only if buffer has really changed. The reason is that the gap is
10414 initially at Z for freshly visited files. The code below would
10415 set end_unchanged to 0 in that case. */
28ee91c0
GM
10416 if (MODIFF > SAVE_MODIFF
10417 /* This seems to happen sometimes after saving a buffer. */
10418 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 10419 {
9142dd5b
GM
10420 if (GPT - BEG < BEG_UNCHANGED)
10421 BEG_UNCHANGED = GPT - BEG;
10422 if (Z - GPT < END_UNCHANGED)
10423 END_UNCHANGED = Z - GPT;
5f5c8ee5 10424 }
bf9249e3 10425
5f5c8ee5
GM
10426 /* If window starts after a line end, and the last change is in
10427 front of that newline, then changes don't affect the display.
f2d86d7a
GM
10428 This case happens with stealth-fontification. Note that although
10429 the display is unchanged, glyph positions in the matrix have to
10430 be adjusted, of course. */
5f5c8ee5
GM
10431 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10432 if (CHARPOS (start) > BEGV
9142dd5b 10433 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
10434 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10435 && PT < MATRIX_ROW_END_CHARPOS (row))
10436 {
f2d86d7a
GM
10437 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10438 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10439
10440 if (delta)
10441 {
10442 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10443 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10444
10445 increment_matrix_positions (w->current_matrix,
10446 MATRIX_ROW_VPOS (r0, current_matrix),
10447 MATRIX_ROW_VPOS (r1, current_matrix),
10448 delta, delta_bytes);
10449 }
10450
10451#if 0 /* If changes are all in front of the window start, the
10452 distance of the last displayed glyph from Z hasn't
10453 changed. */
5f5c8ee5
GM
10454 w->window_end_pos
10455 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10456 w->window_end_bytepos
6fc556fd 10457 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
f2d86d7a
GM
10458#endif
10459
5f5c8ee5
GM
10460 return 1;
10461 }
10462
10463 /* Return quickly if changes are all below what is displayed in the
10464 window, and if PT is in the window. */
9142dd5b 10465 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
10466 && PT < MATRIX_ROW_END_CHARPOS (row))
10467 {
10468 /* We have to update window end positions because the buffer's
10469 size has changed. */
10470 w->window_end_pos
10471 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10472 w->window_end_bytepos
6fc556fd 10473 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ef121659
GM
10474
10475 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10476 row = row_containing_pos (w, PT, row, NULL);
10477 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10478 return 2;
5f5c8ee5
GM
10479 }
10480
10481 /* Check that window start agrees with the start of the first glyph
10482 row in its current matrix. Check this after we know the window
10483 start is not in changed text, otherwise positions would not be
10484 comparable. */
10485 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10486 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10487 return 0;
10488
5f5c8ee5
GM
10489 /* Compute the position at which we have to start displaying new
10490 lines. Some of the lines at the top of the window might be
10491 reusable because they are not displaying changed text. Find the
10492 last row in W's current matrix not affected by changes at the
10493 start of current_buffer. Value is null if changes start in the
10494 first line of window. */
10495 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10496 if (last_unchanged_at_beg_row)
10497 {
10498 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10499 start_pos = it.current.pos;
10500
10501 /* Start displaying new lines in the desired matrix at the same
10502 vpos we would use in the current matrix, i.e. below
10503 last_unchanged_at_beg_row. */
10504 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10505 current_matrix);
10506 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10507 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10508
10509 xassert (it.hpos == 0 && it.current_x == 0);
10510 }
10511 else
10512 {
10513 /* There are no reusable lines at the start of the window.
10514 Start displaying in the first line. */
10515 start_display (&it, w, start);
10516 start_pos = it.current.pos;
10517 }
10518
5f5c8ee5
GM
10519 /* Find the first row that is not affected by changes at the end of
10520 the buffer. Value will be null if there is no unchanged row, in
10521 which case we must redisplay to the end of the window. delta
10522 will be set to the value by which buffer positions beginning with
10523 first_unchanged_at_end_row have to be adjusted due to text
10524 changes. */
10525 first_unchanged_at_end_row
10526 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10527 IF_DEBUG (debug_delta = delta);
10528 IF_DEBUG (debug_delta_bytes = delta_bytes);
10529
10530 /* Set stop_pos to the buffer position up to which we will have to
10531 display new lines. If first_unchanged_at_end_row != NULL, this
10532 is the buffer position of the start of the line displayed in that
10533 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10534 that we don't stop at a buffer position. */
10535 stop_pos = 0;
10536 if (first_unchanged_at_end_row)
10537 {
10538 xassert (last_unchanged_at_beg_row == NULL
10539 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10540
10541 /* If this is a continuation line, move forward to the next one
10542 that isn't. Changes in lines above affect this line.
10543 Caution: this may move first_unchanged_at_end_row to a row
10544 not displaying text. */
10545 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10546 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10547 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10548 < it.last_visible_y))
10549 ++first_unchanged_at_end_row;
10550
10551 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10552 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10553 >= it.last_visible_y))
10554 first_unchanged_at_end_row = NULL;
10555 else
10556 {
10557 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10558 + delta);
10559 first_unchanged_at_end_vpos
10560 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 10561 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
10562 }
10563 }
10564 else if (last_unchanged_at_beg_row == NULL)
10565 return 0;
10566
10567
10568#if GLYPH_DEBUG
10569
10570 /* Either there is no unchanged row at the end, or the one we have
10571 now displays text. This is a necessary condition for the window
10572 end pos calculation at the end of this function. */
10573 xassert (first_unchanged_at_end_row == NULL
10574 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10575
10576 debug_last_unchanged_at_beg_vpos
10577 = (last_unchanged_at_beg_row
10578 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10579 : -1);
10580 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10581
10582#endif /* GLYPH_DEBUG != 0 */
10583
10584
10585 /* Display new lines. Set last_text_row to the last new line
10586 displayed which has text on it, i.e. might end up as being the
10587 line where the window_end_vpos is. */
10588 w->cursor.vpos = -1;
10589 last_text_row = NULL;
10590 overlay_arrow_seen = 0;
10591 while (it.current_y < it.last_visible_y
10592 && !fonts_changed_p
10593 && (first_unchanged_at_end_row == NULL
10594 || IT_CHARPOS (it) < stop_pos))
10595 {
10596 if (display_line (&it))
10597 last_text_row = it.glyph_row - 1;
10598 }
10599
10600 if (fonts_changed_p)
10601 return -1;
10602
10603
10604 /* Compute differences in buffer positions, y-positions etc. for
10605 lines reused at the bottom of the window. Compute what we can
10606 scroll. */
ca42b2e8
GM
10607 if (first_unchanged_at_end_row
10608 /* No lines reused because we displayed everything up to the
10609 bottom of the window. */
10610 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
10611 {
10612 dvpos = (it.vpos
10613 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10614 current_matrix));
10615 dy = it.current_y - first_unchanged_at_end_row->y;
10616 run.current_y = first_unchanged_at_end_row->y;
10617 run.desired_y = run.current_y + dy;
10618 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10619 }
10620 else
ca42b2e8
GM
10621 {
10622 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10623 first_unchanged_at_end_row = NULL;
10624 }
5f5c8ee5
GM
10625 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10626
8f8ba186 10627
5f5c8ee5
GM
10628 /* Find the cursor if not already found. We have to decide whether
10629 PT will appear on this window (it sometimes doesn't, but this is
10630 not a very frequent case.) This decision has to be made before
10631 the current matrix is altered. A value of cursor.vpos < 0 means
10632 that PT is either in one of the lines beginning at
10633 first_unchanged_at_end_row or below the window. Don't care for
10634 lines that might be displayed later at the window end; as
10635 mentioned, this is not a frequent case. */
10636 if (w->cursor.vpos < 0)
10637 {
5f5c8ee5
GM
10638 /* Cursor in unchanged rows at the top? */
10639 if (PT < CHARPOS (start_pos)
10640 && last_unchanged_at_beg_row)
10641 {
e037b9ec
GM
10642 row = row_containing_pos (w, PT,
10643 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10644 last_unchanged_at_beg_row + 1);
bfe0ee88
GM
10645 if (row)
10646 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
10647 }
10648
10649 /* Start from first_unchanged_at_end_row looking for PT. */
10650 else if (first_unchanged_at_end_row)
10651 {
e037b9ec
GM
10652 row = row_containing_pos (w, PT - delta,
10653 first_unchanged_at_end_row, NULL);
10654 if (row)
468155d7
GM
10655 set_cursor_from_row (w, row, w->current_matrix, delta,
10656 delta_bytes, dy, dvpos);
5f5c8ee5
GM
10657 }
10658
10659 /* Give up if cursor was not found. */
10660 if (w->cursor.vpos < 0)
10661 {
10662 clear_glyph_matrix (w->desired_matrix);
10663 return -1;
10664 }
10665 }
10666
10667 /* Don't let the cursor end in the scroll margins. */
10668 {
10669 int this_scroll_margin, cursor_height;
10670
10671 this_scroll_margin = max (0, scroll_margin);
10672 this_scroll_margin = min (this_scroll_margin,
10673 XFASTINT (w->height) / 4);
10674 this_scroll_margin *= CANON_Y_UNIT (it.f);
10675 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10676
10677 if ((w->cursor.y < this_scroll_margin
10678 && CHARPOS (start) > BEGV)
10679 /* Don't take scroll margin into account at the bottom because
10680 old redisplay didn't do it either. */
10681 || w->cursor.y + cursor_height > it.last_visible_y)
10682 {
10683 w->cursor.vpos = -1;
10684 clear_glyph_matrix (w->desired_matrix);
10685 return -1;
10686 }
10687 }
10688
10689 /* Scroll the display. Do it before changing the current matrix so
10690 that xterm.c doesn't get confused about where the cursor glyph is
10691 found. */
fa77249f 10692 if (dy && run.height)
5f5c8ee5
GM
10693 {
10694 update_begin (f);
10695
10696 if (FRAME_WINDOW_P (f))
10697 {
10698 rif->update_window_begin_hook (w);
64d1e7d3 10699 rif->clear_mouse_face (w);
5f5c8ee5 10700 rif->scroll_run_hook (w, &run);
64d1e7d3 10701 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
10702 }
10703 else
10704 {
10705 /* Terminal frame. In this case, dvpos gives the number of
10706 lines to scroll by; dvpos < 0 means scroll up. */
10707 int first_unchanged_at_end_vpos
10708 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10709 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10710 int end = XFASTINT (w->top) + window_internal_height (w);
10711
10712 /* Perform the operation on the screen. */
10713 if (dvpos > 0)
10714 {
10715 /* Scroll last_unchanged_at_beg_row to the end of the
10716 window down dvpos lines. */
10717 set_terminal_window (end);
10718
10719 /* On dumb terminals delete dvpos lines at the end
10720 before inserting dvpos empty lines. */
10721 if (!scroll_region_ok)
10722 ins_del_lines (end - dvpos, -dvpos);
10723
10724 /* Insert dvpos empty lines in front of
10725 last_unchanged_at_beg_row. */
10726 ins_del_lines (from, dvpos);
10727 }
10728 else if (dvpos < 0)
10729 {
10730 /* Scroll up last_unchanged_at_beg_vpos to the end of
10731 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10732 set_terminal_window (end);
10733
10734 /* Delete dvpos lines in front of
10735 last_unchanged_at_beg_vpos. ins_del_lines will set
10736 the cursor to the given vpos and emit |dvpos| delete
10737 line sequences. */
10738 ins_del_lines (from + dvpos, dvpos);
10739
10740 /* On a dumb terminal insert dvpos empty lines at the
10741 end. */
10742 if (!scroll_region_ok)
10743 ins_del_lines (end + dvpos, -dvpos);
10744 }
10745
10746 set_terminal_window (0);
10747 }
10748
10749 update_end (f);
10750 }
10751
ca42b2e8
GM
10752 /* Shift reused rows of the current matrix to the right position.
10753 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10754 text. */
10755 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10756 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
10757 if (dvpos < 0)
10758 {
10759 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10760 bottom_vpos, dvpos);
10761 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10762 bottom_vpos, 0);
10763 }
10764 else if (dvpos > 0)
10765 {
10766 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10767 bottom_vpos, dvpos);
10768 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10769 first_unchanged_at_end_vpos + dvpos, 0);
10770 }
10771
10772 /* For frame-based redisplay, make sure that current frame and window
10773 matrix are in sync with respect to glyph memory. */
10774 if (!FRAME_WINDOW_P (f))
10775 sync_frame_with_window_matrix_rows (w);
10776
10777 /* Adjust buffer positions in reused rows. */
10778 if (delta)
f2d86d7a
GM
10779 increment_matrix_positions (current_matrix,
10780 first_unchanged_at_end_vpos + dvpos,
10781 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
10782
10783 /* Adjust Y positions. */
10784 if (dy)
10785 shift_glyph_matrix (w, current_matrix,
10786 first_unchanged_at_end_vpos + dvpos,
10787 bottom_vpos, dy);
10788
10789 if (first_unchanged_at_end_row)
10790 first_unchanged_at_end_row += dvpos;
10791
10792 /* If scrolling up, there may be some lines to display at the end of
10793 the window. */
10794 last_text_row_at_end = NULL;
10795 if (dy < 0)
10796 {
10797 /* Set last_row to the glyph row in the current matrix where the
10798 window end line is found. It has been moved up or down in
10799 the matrix by dvpos. */
10800 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10801 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10802
10803 /* If last_row is the window end line, it should display text. */
10804 xassert (last_row->displays_text_p);
10805
10806 /* If window end line was partially visible before, begin
10807 displaying at that line. Otherwise begin displaying with the
10808 line following it. */
10809 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10810 {
10811 init_to_row_start (&it, w, last_row);
10812 it.vpos = last_vpos;
10813 it.current_y = last_row->y;
10814 }
10815 else
10816 {
10817 init_to_row_end (&it, w, last_row);
10818 it.vpos = 1 + last_vpos;
10819 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10820 ++last_row;
10821 }
12adba34 10822
5f5c8ee5
GM
10823 /* We may start in a continuation line. If so, we have to get
10824 the right continuation_lines_width and current_x. */
10825 it.continuation_lines_width = last_row->continuation_lines_width;
10826 it.hpos = it.current_x = 0;
10827
10828 /* Display the rest of the lines at the window end. */
10829 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10830 while (it.current_y < it.last_visible_y
10831 && !fonts_changed_p)
10832 {
10833 /* Is it always sure that the display agrees with lines in
10834 the current matrix? I don't think so, so we mark rows
10835 displayed invalid in the current matrix by setting their
10836 enabled_p flag to zero. */
10837 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10838 if (display_line (&it))
10839 last_text_row_at_end = it.glyph_row - 1;
10840 }
10841 }
12adba34 10842
5f5c8ee5
GM
10843 /* Update window_end_pos and window_end_vpos. */
10844 if (first_unchanged_at_end_row
10845 && first_unchanged_at_end_row->y < it.last_visible_y
10846 && !last_text_row_at_end)
10847 {
10848 /* Window end line if one of the preserved rows from the current
10849 matrix. Set row to the last row displaying text in current
10850 matrix starting at first_unchanged_at_end_row, after
10851 scrolling. */
10852 xassert (first_unchanged_at_end_row->displays_text_p);
10853 row = find_last_row_displaying_text (w->current_matrix, &it,
10854 first_unchanged_at_end_row);
10855 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10856
10857 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10858 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10859 XSETFASTINT (w->window_end_vpos,
10860 MATRIX_ROW_VPOS (row, w->current_matrix));
10861 }
10862 else if (last_text_row_at_end)
10863 {
10864 XSETFASTINT (w->window_end_pos,
10865 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10866 w->window_end_bytepos
10867 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10868 XSETFASTINT (w->window_end_vpos,
10869 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10870 }
10871 else if (last_text_row)
10872 {
10873 /* We have displayed either to the end of the window or at the
10874 end of the window, i.e. the last row with text is to be found
10875 in the desired matrix. */
10876 XSETFASTINT (w->window_end_pos,
10877 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10878 w->window_end_bytepos
10879 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10880 XSETFASTINT (w->window_end_vpos,
10881 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10882 }
10883 else if (first_unchanged_at_end_row == NULL
10884 && last_text_row == NULL
10885 && last_text_row_at_end == NULL)
10886 {
10887 /* Displayed to end of window, but no line containing text was
10888 displayed. Lines were deleted at the end of the window. */
10889 int vpos;
045dee35 10890 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
10891
10892 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
045dee35
GM
10893 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10894 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10895 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10896 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
5f5c8ee5 10897 break;
12adba34 10898
5f5c8ee5
GM
10899 w->window_end_vpos = make_number (vpos);
10900 }
10901 else
10902 abort ();
10903
10904 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10905 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 10906
5f5c8ee5
GM
10907 /* Record that display has not been completed. */
10908 w->window_end_valid = Qnil;
10909 w->desired_matrix->no_scrolling_p = 1;
ef121659 10910 return 3;
12adba34 10911}
0f9c0ff0 10912
a2889657 10913
5f5c8ee5
GM
10914\f
10915/***********************************************************************
10916 More debugging support
10917 ***********************************************************************/
a2889657 10918
5f5c8ee5 10919#if GLYPH_DEBUG
a2889657 10920
5f5c8ee5
GM
10921 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10922static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 10923
31b24551 10924
5f5c8ee5
GM
10925/* Dump the contents of glyph matrix MATRIX on stderr. If
10926 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 10927
5f85fa4e 10928static void
5f5c8ee5
GM
10929dump_glyph_matrix (matrix, with_glyphs_p)
10930 struct glyph_matrix *matrix;
10931 int with_glyphs_p;
10932{
efc63ef0 10933 int i;
5f5c8ee5
GM
10934 for (i = 0; i < matrix->nrows; ++i)
10935 dump_glyph_row (matrix, i, with_glyphs_p);
10936}
31b24551 10937
68a37fa8 10938
5f5c8ee5
GM
10939/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10940 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 10941
5f5c8ee5
GM
10942void
10943dump_glyph_row (matrix, vpos, with_glyphs_p)
10944 struct glyph_matrix *matrix;
10945 int vpos, with_glyphs_p;
10946{
10947 struct glyph_row *row;
10948
10949 if (vpos < 0 || vpos >= matrix->nrows)
10950 return;
10951
10952 row = MATRIX_ROW (matrix, vpos);
10953
b28cb6ed
GM
10954 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
10955 fprintf (stderr, "=======================================================================\n");
5f5c8ee5 10956
ae26e27d
GM
10957 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
10958%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
5f5c8ee5
GM
10959 row - matrix->rows,
10960 MATRIX_ROW_START_CHARPOS (row),
10961 MATRIX_ROW_END_CHARPOS (row),
10962 row->used[TEXT_AREA],
10963 row->contains_overlapping_glyphs_p,
10964 row->enabled_p,
10965 row->inverse_p,
10966 row->truncated_on_left_p,
10967 row->truncated_on_right_p,
10968 row->overlay_arrow_p,
10969 row->continued_p,
10970 MATRIX_ROW_CONTINUATION_LINE_P (row),
10971 row->displays_text_p,
10972 row->ends_at_zv_p,
10973 row->fill_line_p,
91004049
GM
10974 row->ends_in_middle_of_char_p,
10975 row->starts_in_middle_of_char_p,
5f5c8ee5
GM
10976 row->x,
10977 row->y,
b28cb6ed
GM
10978 row->pixel_width,
10979 row->height,
10980 row->visible_height,
10981 row->ascent,
10982 row->phys_ascent);
5f5c8ee5
GM
10983 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10984 row->end.overlay_string_index);
10985 fprintf (stderr, "%9d %5d\n",
10986 CHARPOS (row->start.string_pos),
10987 CHARPOS (row->end.string_pos));
10988 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10989 row->end.dpvec_index);
10990
10991 if (with_glyphs_p)
bd66d1ba 10992 {
5f5c8ee5
GM
10993 struct glyph *glyph, *glyph_end;
10994 int prev_had_glyphs_p;
10995
10996 glyph = row->glyphs[TEXT_AREA];
10997 glyph_end = glyph + row->used[TEXT_AREA];
10998
10999 /* Glyph for a line end in text. */
11000 if (glyph == glyph_end && glyph->charpos > 0)
11001 ++glyph_end;
11002
11003 if (glyph < glyph_end)
bd66d1ba 11004 {
06568bbf 11005 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 11006 prev_had_glyphs_p = 1;
bd66d1ba
RS
11007 }
11008 else
5f5c8ee5
GM
11009 prev_had_glyphs_p = 0;
11010
11011 while (glyph < glyph_end)
f7b4b63a 11012 {
5f5c8ee5
GM
11013 if (glyph->type == CHAR_GLYPH)
11014 {
11015 fprintf (stderr,
06568bbf 11016 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
11017 glyph - row->glyphs[TEXT_AREA],
11018 'C',
11019 glyph->charpos,
06568bbf
GM
11020 (BUFFERP (glyph->object)
11021 ? 'B'
11022 : (STRINGP (glyph->object)
11023 ? 'S'
11024 : '-')),
5f5c8ee5 11025 glyph->pixel_width,
43d120d8
KH
11026 glyph->u.ch,
11027 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11028 ? glyph->u.ch
5f5c8ee5 11029 : '.'),
43d120d8 11030 glyph->face_id,
5f5c8ee5
GM
11031 glyph->left_box_line_p,
11032 glyph->right_box_line_p);
11033 }
11034 else if (glyph->type == STRETCH_GLYPH)
11035 {
11036 fprintf (stderr,
06568bbf 11037 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
11038 glyph - row->glyphs[TEXT_AREA],
11039 'S',
11040 glyph->charpos,
06568bbf
GM
11041 (BUFFERP (glyph->object)
11042 ? 'B'
11043 : (STRINGP (glyph->object)
11044 ? 'S'
11045 : '-')),
5f5c8ee5
GM
11046 glyph->pixel_width,
11047 0,
11048 '.',
bcdda9a4 11049 glyph->face_id,
5f5c8ee5
GM
11050 glyph->left_box_line_p,
11051 glyph->right_box_line_p);
11052 }
11053 else if (glyph->type == IMAGE_GLYPH)
11054 {
11055 fprintf (stderr,
06568bbf 11056 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
11057 glyph - row->glyphs[TEXT_AREA],
11058 'I',
11059 glyph->charpos,
06568bbf
GM
11060 (BUFFERP (glyph->object)
11061 ? 'B'
11062 : (STRINGP (glyph->object)
11063 ? 'S'
11064 : '-')),
5f5c8ee5 11065 glyph->pixel_width,
43d120d8 11066 glyph->u.img_id,
5f5c8ee5 11067 '.',
bcdda9a4 11068 glyph->face_id,
5f5c8ee5
GM
11069 glyph->left_box_line_p,
11070 glyph->right_box_line_p);
11071 }
11072 ++glyph;
f7b4b63a 11073 }
f4faa47c 11074 }
5f5c8ee5 11075}
f4faa47c 11076
a2889657 11077
5f5c8ee5
GM
11078DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11079 Sdump_glyph_matrix, 0, 1, "p",
11080 "Dump the current matrix of the selected window to stderr.\n\
11081Shows contents of glyph row structures. With non-nil optional\n\
11082parameter WITH-GLYPHS-P, dump glyphs as well.")
11083 (with_glyphs_p)
02513cdd 11084 Lisp_Object with_glyphs_p;
5f5c8ee5
GM
11085{
11086 struct window *w = XWINDOW (selected_window);
11087 struct buffer *buffer = XBUFFER (w->buffer);
11088
11089 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11090 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11091 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11092 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11093 fprintf (stderr, "=============================================\n");
11094 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11095 return Qnil;
11096}
1c2250c2 11097
1fca3fae 11098
5f5c8ee5
GM
11099DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11100 "Dump glyph row ROW to stderr.")
11101 (row)
11102 Lisp_Object row;
11103{
11104 CHECK_NUMBER (row, 0);
11105 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11106 return Qnil;
11107}
1fca3fae 11108
67481ae5 11109
e037b9ec 11110DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
5f5c8ee5
GM
11111 0, 0, "", "")
11112 ()
11113{
886bd6f2
GM
11114 struct frame *sf = SELECTED_FRAME ();
11115 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
5f5c8ee5
GM
11116 ->current_matrix);
11117 dump_glyph_row (m, 0, 1);
11118 return Qnil;
11119}
ca26e1c8 11120
0f9c0ff0 11121
5f5c8ee5
GM
11122DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11123 Strace_redisplay_toggle, 0, 0, "",
11124 "Toggle tracing of redisplay.")
11125 ()
11126{
11127 trace_redisplay_p = !trace_redisplay_p;
11128 return Qnil;
11129}
bf9249e3
GM
11130
11131
11132DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11133 "Print STRING to stderr.")
11134 (string)
11135 Lisp_Object string;
11136{
11137 CHECK_STRING (string, 0);
11138 fprintf (stderr, "%s", XSTRING (string)->data);
11139 return Qnil;
11140}
5f5c8ee5
GM
11141
11142#endif /* GLYPH_DEBUG */
ca26e1c8 11143
ca26e1c8 11144
5f5c8ee5
GM
11145\f
11146/***********************************************************************
11147 Building Desired Matrix Rows
11148 ***********************************************************************/
a2889657 11149
5f5c8ee5
GM
11150/* Return a temporary glyph row holding the glyphs of an overlay
11151 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 11152
5f5c8ee5
GM
11153static struct glyph_row *
11154get_overlay_arrow_glyph_row (w)
11155 struct window *w;
11156{
11157 struct frame *f = XFRAME (WINDOW_FRAME (w));
11158 struct buffer *buffer = XBUFFER (w->buffer);
11159 struct buffer *old = current_buffer;
11160 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11161 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11162 unsigned char *arrow_end = arrow_string + arrow_len;
11163 unsigned char *p;
11164 struct it it;
11165 int multibyte_p;
11166 int n_glyphs_before;
11167
11168 set_buffer_temp (buffer);
11169 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11170 it.glyph_row->used[TEXT_AREA] = 0;
11171 SET_TEXT_POS (it.position, 0, 0);
11172
11173 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11174 p = arrow_string;
11175 while (p < arrow_end)
11176 {
11177 Lisp_Object face, ilisp;
11178
11179 /* Get the next character. */
11180 if (multibyte_p)
4fdb80f2 11181 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
11182 else
11183 it.c = *p, it.len = 1;
11184 p += it.len;
11185
11186 /* Get its face. */
11187 XSETFASTINT (ilisp, p - arrow_string);
11188 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11189 it.face_id = compute_char_face (f, it.c, face);
11190
11191 /* Compute its width, get its glyphs. */
11192 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 11193 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
11194 PRODUCE_GLYPHS (&it);
11195
11196 /* If this character doesn't fit any more in the line, we have
11197 to remove some glyphs. */
11198 if (it.current_x > it.last_visible_x)
11199 {
11200 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11201 break;
11202 }
11203 }
11204
11205 set_buffer_temp (old);
11206 return it.glyph_row;
11207}
ca26e1c8 11208
b0a0fbda 11209
5f5c8ee5
GM
11210/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11211 glyphs are only inserted for terminal frames since we can't really
11212 win with truncation glyphs when partially visible glyphs are
11213 involved. Which glyphs to insert is determined by
11214 produce_special_glyphs. */
67481ae5 11215
5f5c8ee5
GM
11216static void
11217insert_left_trunc_glyphs (it)
11218 struct it *it;
11219{
11220 struct it truncate_it;
11221 struct glyph *from, *end, *to, *toend;
11222
11223 xassert (!FRAME_WINDOW_P (it->f));
11224
11225 /* Get the truncation glyphs. */
11226 truncate_it = *it;
5f5c8ee5
GM
11227 truncate_it.current_x = 0;
11228 truncate_it.face_id = DEFAULT_FACE_ID;
11229 truncate_it.glyph_row = &scratch_glyph_row;
11230 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11231 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 11232 truncate_it.object = make_number (0);
5f5c8ee5
GM
11233 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11234
11235 /* Overwrite glyphs from IT with truncation glyphs. */
11236 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11237 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11238 to = it->glyph_row->glyphs[TEXT_AREA];
11239 toend = to + it->glyph_row->used[TEXT_AREA];
11240
11241 while (from < end)
11242 *to++ = *from++;
11243
11244 /* There may be padding glyphs left over. Remove them. */
11245 from = to;
11246 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11247 ++from;
11248 while (from < toend)
11249 *to++ = *from++;
11250
11251 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11252}
e0bfbde6 11253
e0bfbde6 11254
5f5c8ee5 11255/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 11256
5f5c8ee5
GM
11257 Most of the time, ascent and height of a display line will be equal
11258 to the max_ascent and max_height values of the display iterator
11259 structure. This is not the case if
67481ae5 11260
5f5c8ee5
GM
11261 1. We hit ZV without displaying anything. In this case, max_ascent
11262 and max_height will be zero.
1c9241f5 11263
5f5c8ee5
GM
11264 2. We have some glyphs that don't contribute to the line height.
11265 (The glyph row flag contributes_to_line_height_p is for future
11266 pixmap extensions).
f6fd109b 11267
5f5c8ee5
GM
11268 The first case is easily covered by using default values because in
11269 these cases, the line height does not really matter, except that it
11270 must not be zero. */
67481ae5 11271
5f5c8ee5
GM
11272static void
11273compute_line_metrics (it)
11274 struct it *it;
11275{
11276 struct glyph_row *row = it->glyph_row;
11277 int area, i;
1c2250c2 11278
5f5c8ee5
GM
11279 if (FRAME_WINDOW_P (it->f))
11280 {
045dee35 11281 int i, header_line_height;
1c2250c2 11282
5f5c8ee5
GM
11283 /* The line may consist of one space only, that was added to
11284 place the cursor on it. If so, the row's height hasn't been
11285 computed yet. */
11286 if (row->height == 0)
11287 {
11288 if (it->max_ascent + it->max_descent == 0)
312246d1 11289 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
11290 row->ascent = it->max_ascent;
11291 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11292 row->phys_ascent = it->max_phys_ascent;
11293 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
11294 }
11295
11296 /* Compute the width of this line. */
11297 row->pixel_width = row->x;
11298 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11299 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11300
11301 xassert (row->pixel_width >= 0);
11302 xassert (row->ascent >= 0 && row->height > 0);
11303
312246d1
GM
11304 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11305 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11306
11307 /* If first line's physical ascent is larger than its logical
11308 ascent, use the physical ascent, and make the row taller.
11309 This makes accented characters fully visible. */
b28cb6ed 11310 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
11311 && row->phys_ascent > row->ascent)
11312 {
11313 row->height += row->phys_ascent - row->ascent;
11314 row->ascent = row->phys_ascent;
11315 }
11316
5f5c8ee5
GM
11317 /* Compute how much of the line is visible. */
11318 row->visible_height = row->height;
11319
045dee35
GM
11320 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11321 if (row->y < header_line_height)
11322 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
11323 else
11324 {
11325 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11326 if (row->y + row->height > max_y)
11327 row->visible_height -= row->y + row->height - max_y;
11328 }
11329 }
11330 else
11331 {
11332 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
11333 row->ascent = row->phys_ascent = 0;
11334 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 11335 }
67481ae5 11336
5f5c8ee5
GM
11337 /* Compute a hash code for this row. */
11338 row->hash = 0;
11339 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11340 for (i = 0; i < row->used[area]; ++i)
11341 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11342 + row->glyphs[area][i].u.val
43d120d8
KH
11343 + row->glyphs[area][i].face_id
11344 + row->glyphs[area][i].padding_p
5f5c8ee5 11345 + (row->glyphs[area][i].type << 2));
a2889657 11346
5f5c8ee5 11347 it->max_ascent = it->max_descent = 0;
312246d1 11348 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 11349}
12adba34 11350
ca26e1c8 11351
5f5c8ee5
GM
11352/* Append one space to the glyph row of iterator IT if doing a
11353 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11354 space have the default face, otherwise let it have the same face as
80c6cb1f 11355 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
11356
11357 This function is called to make sure that there is always one glyph
11358 at the end of a glyph row that the cursor can be set on under
11359 window-systems. (If there weren't such a glyph we would not know
11360 how wide and tall a box cursor should be displayed).
11361
11362 At the same time this space let's a nicely handle clearing to the
11363 end of the line if the row ends in italic text. */
ca26e1c8 11364
80c6cb1f 11365static int
5f5c8ee5
GM
11366append_space (it, default_face_p)
11367 struct it *it;
11368 int default_face_p;
11369{
11370 if (FRAME_WINDOW_P (it->f))
11371 {
11372 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 11373
5f5c8ee5
GM
11374 if (it->glyph_row->glyphs[TEXT_AREA] + n
11375 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 11376 {
5f5c8ee5
GM
11377 /* Save some values that must not be changed. */
11378 int saved_x = it->current_x;
11379 struct text_pos saved_pos;
11380 int saved_what = it->what;
11381 int saved_face_id = it->face_id;
5f5c8ee5 11382 Lisp_Object saved_object;
980806b6 11383 struct face *face;
5f5c8ee5
GM
11384
11385 saved_object = it->object;
11386 saved_pos = it->position;
11387
11388 it->what = IT_CHARACTER;
11389 bzero (&it->position, sizeof it->position);
6fc556fd 11390 it->object = make_number (0);
5f5c8ee5
GM
11391 it->c = ' ';
11392 it->len = 1;
5f5c8ee5
GM
11393
11394 if (default_face_p)
11395 it->face_id = DEFAULT_FACE_ID;
980806b6
KH
11396 face = FACE_FROM_ID (it->f, it->face_id);
11397 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 11398
5f5c8ee5
GM
11399 PRODUCE_GLYPHS (it);
11400
11401 it->current_x = saved_x;
11402 it->object = saved_object;
11403 it->position = saved_pos;
11404 it->what = saved_what;
11405 it->face_id = saved_face_id;
80c6cb1f 11406 return 1;
5f5c8ee5
GM
11407 }
11408 }
80c6cb1f
GM
11409
11410 return 0;
5f5c8ee5 11411}
12adba34 11412
1842fc1a 11413
5f5c8ee5
GM
11414/* Extend the face of the last glyph in the text area of IT->glyph_row
11415 to the end of the display line. Called from display_line.
11416 If the glyph row is empty, add a space glyph to it so that we
11417 know the face to draw. Set the glyph row flag fill_line_p. */
11418
11419static void
11420extend_face_to_end_of_line (it)
11421 struct it *it;
11422{
11423 struct face *face;
11424 struct frame *f = it->f;
1842fc1a 11425
5f5c8ee5
GM
11426 /* If line is already filled, do nothing. */
11427 if (it->current_x >= it->last_visible_x)
11428 return;
11429
11430 /* Face extension extends the background and box of IT->face_id
11431 to the end of the line. If the background equals the background
11432 of the frame, we haven't to do anything. */
11433 face = FACE_FROM_ID (f, it->face_id);
11434 if (FRAME_WINDOW_P (f)
11435 && face->box == FACE_NO_BOX
11436 && face->background == FRAME_BACKGROUND_PIXEL (f)
11437 && !face->stipple)
11438 return;
1842fc1a 11439
5f5c8ee5
GM
11440 /* Set the glyph row flag indicating that the face of the last glyph
11441 in the text area has to be drawn to the end of the text area. */
11442 it->glyph_row->fill_line_p = 1;
545e04f6 11443
980806b6
KH
11444 /* If current character of IT is not ASCII, make sure we have the
11445 ASCII face. This will be automatically undone the next time
11446 get_next_display_element returns a multibyte character. Note
11447 that the character will always be single byte in unibyte text. */
11448 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 11449 {
980806b6 11450 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 11451 }
545e04f6 11452
5f5c8ee5
GM
11453 if (FRAME_WINDOW_P (f))
11454 {
11455 /* If the row is empty, add a space with the current face of IT,
11456 so that we know which face to draw. */
11457 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 11458 {
5f5c8ee5 11459 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 11460 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 11461 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 11462 }
5f5c8ee5
GM
11463 }
11464 else
11465 {
11466 /* Save some values that must not be changed. */
11467 int saved_x = it->current_x;
11468 struct text_pos saved_pos;
11469 Lisp_Object saved_object;
11470 int saved_what = it->what;
11471
11472 saved_object = it->object;
11473 saved_pos = it->position;
11474
11475 it->what = IT_CHARACTER;
11476 bzero (&it->position, sizeof it->position);
6fc556fd 11477 it->object = make_number (0);
5f5c8ee5
GM
11478 it->c = ' ';
11479 it->len = 1;
11480
11481 PRODUCE_GLYPHS (it);
11482
11483 while (it->current_x <= it->last_visible_x)
11484 PRODUCE_GLYPHS (it);
11485
11486 /* Don't count these blanks really. It would let us insert a left
11487 truncation glyph below and make us set the cursor on them, maybe. */
11488 it->current_x = saved_x;
11489 it->object = saved_object;
11490 it->position = saved_pos;
11491 it->what = saved_what;
11492 }
11493}
12adba34 11494
545e04f6 11495
5f5c8ee5
GM
11496/* Value is non-zero if text starting at CHARPOS in current_buffer is
11497 trailing whitespace. */
1c9241f5 11498
5f5c8ee5
GM
11499static int
11500trailing_whitespace_p (charpos)
11501 int charpos;
11502{
11503 int bytepos = CHAR_TO_BYTE (charpos);
11504 int c = 0;
7bbe686f 11505
5f5c8ee5
GM
11506 while (bytepos < ZV_BYTE
11507 && (c = FETCH_CHAR (bytepos),
11508 c == ' ' || c == '\t'))
11509 ++bytepos;
0d09d1e6 11510
8f897821
GM
11511 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11512 {
11513 if (bytepos != PT_BYTE)
11514 return 1;
11515 }
11516 return 0;
5f5c8ee5 11517}
31b24551 11518
545e04f6 11519
5f5c8ee5 11520/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 11521
5f5c8ee5
GM
11522void
11523highlight_trailing_whitespace (f, row)
11524 struct frame *f;
11525 struct glyph_row *row;
11526{
11527 int used = row->used[TEXT_AREA];
11528
11529 if (used)
11530 {
11531 struct glyph *start = row->glyphs[TEXT_AREA];
11532 struct glyph *glyph = start + used - 1;
11533
11534 /* Skip over the space glyph inserted to display the
11535 cursor at the end of a line. */
11536 if (glyph->type == CHAR_GLYPH
43d120d8 11537 && glyph->u.ch == ' '
6fc556fd 11538 && INTEGERP (glyph->object))
5f5c8ee5
GM
11539 --glyph;
11540
11541 /* If last glyph is a space or stretch, and it's trailing
11542 whitespace, set the face of all trailing whitespace glyphs in
11543 IT->glyph_row to `trailing-whitespace'. */
11544 if (glyph >= start
11545 && BUFFERP (glyph->object)
11546 && (glyph->type == STRETCH_GLYPH
11547 || (glyph->type == CHAR_GLYPH
43d120d8 11548 && glyph->u.ch == ' '))
5f5c8ee5 11549 && trailing_whitespace_p (glyph->charpos))
545e04f6 11550 {
980806b6 11551 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
11552
11553 while (glyph >= start
11554 && BUFFERP (glyph->object)
11555 && (glyph->type == STRETCH_GLYPH
11556 || (glyph->type == CHAR_GLYPH
43d120d8
KH
11557 && glyph->u.ch == ' ')))
11558 (glyph--)->face_id = face_id;
545e04f6 11559 }
a2889657 11560 }
5f5c8ee5 11561}
a2889657 11562
5fcbb24d 11563
5f5c8ee5
GM
11564/* Construct the glyph row IT->glyph_row in the desired matrix of
11565 IT->w from text at the current position of IT. See dispextern.h
11566 for an overview of struct it. Value is non-zero if
11567 IT->glyph_row displays text, as opposed to a line displaying ZV
11568 only. */
11569
11570static int
11571display_line (it)
11572 struct it *it;
11573{
11574 struct glyph_row *row = it->glyph_row;
11575
11576 /* We always start displaying at hpos zero even if hscrolled. */
11577 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 11578
5f5c8ee5
GM
11579 /* We must not display in a row that's not a text row. */
11580 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11581 < it->w->desired_matrix->nrows);
12adba34 11582
5f5c8ee5
GM
11583 /* Is IT->w showing the region? */
11584 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 11585
5f5c8ee5
GM
11586 /* Clear the result glyph row and enable it. */
11587 prepare_desired_row (row);
12adba34 11588
5f5c8ee5
GM
11589 row->y = it->current_y;
11590 row->start = it->current;
11591 row->continuation_lines_width = it->continuation_lines_width;
11592 row->displays_text_p = 1;
91004049
GM
11593 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11594 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
11595
11596 /* Arrange the overlays nicely for our purposes. Usually, we call
11597 display_line on only one line at a time, in which case this
11598 can't really hurt too much, or we call it on lines which appear
11599 one after another in the buffer, in which case all calls to
11600 recenter_overlay_lists but the first will be pretty cheap. */
11601 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11602
5f5c8ee5
GM
11603 /* Move over display elements that are not visible because we are
11604 hscrolled. This may stop at an x-position < IT->first_visible_x
11605 if the first glyph is partially visible or if we hit a line end. */
11606 if (it->current_x < it->first_visible_x)
11607 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11608 MOVE_TO_POS | MOVE_TO_X);
11609
11610 /* Get the initial row height. This is either the height of the
11611 text hscrolled, if there is any, or zero. */
11612 row->ascent = it->max_ascent;
11613 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11614 row->phys_ascent = it->max_phys_ascent;
11615 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
11616
11617 /* Loop generating characters. The loop is left with IT on the next
11618 character to display. */
11619 while (1)
11620 {
11621 int n_glyphs_before, hpos_before, x_before;
11622 int x, i, nglyphs;
ae26e27d 11623 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 11624
5f5c8ee5
GM
11625 /* Retrieve the next thing to display. Value is zero if end of
11626 buffer reached. */
11627 if (!get_next_display_element (it))
11628 {
11629 /* Maybe add a space at the end of this line that is used to
1b335865
GM
11630 display the cursor there under X. Set the charpos of the
11631 first glyph of blank lines not corresponding to any text
11632 to -1. */
11633 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11634 || row->used[TEXT_AREA] == 0)
a2889657 11635 {
5f5c8ee5
GM
11636 row->glyphs[TEXT_AREA]->charpos = -1;
11637 row->displays_text_p = 0;
11638
11639 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11640 row->indicate_empty_line_p = 1;
a2889657 11641 }
5f5c8ee5
GM
11642
11643 it->continuation_lines_width = 0;
11644 row->ends_at_zv_p = 1;
11645 break;
a2889657 11646 }
a2889657 11647
5f5c8ee5
GM
11648 /* Now, get the metrics of what we want to display. This also
11649 generates glyphs in `row' (which is IT->glyph_row). */
11650 n_glyphs_before = row->used[TEXT_AREA];
11651 x = it->current_x;
e6819faf
GM
11652
11653 /* Remember the line height so far in case the next element doesn't
11654 fit on the line. */
11655 if (!it->truncate_lines_p)
11656 {
11657 ascent = it->max_ascent;
11658 descent = it->max_descent;
11659 phys_ascent = it->max_phys_ascent;
11660 phys_descent = it->max_phys_descent;
11661 }
11662
5f5c8ee5 11663 PRODUCE_GLYPHS (it);
a2889657 11664
5f5c8ee5
GM
11665 /* If this display element was in marginal areas, continue with
11666 the next one. */
11667 if (it->area != TEXT_AREA)
a2889657 11668 {
5f5c8ee5
GM
11669 row->ascent = max (row->ascent, it->max_ascent);
11670 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11671 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11672 row->phys_height = max (row->phys_height,
11673 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11674 set_iterator_to_next (it);
11675 continue;
11676 }
5936754e 11677
5f5c8ee5
GM
11678 /* Does the display element fit on the line? If we truncate
11679 lines, we should draw past the right edge of the window. If
11680 we don't truncate, we want to stop so that we can display the
11681 continuation glyph before the right margin. If lines are
11682 continued, there are two possible strategies for characters
11683 resulting in more than 1 glyph (e.g. tabs): Display as many
11684 glyphs as possible in this line and leave the rest for the
11685 continuation line, or display the whole element in the next
11686 line. Original redisplay did the former, so we do it also. */
11687 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11688 hpos_before = it->hpos;
11689 x_before = x;
11690
11691 if (nglyphs == 1
11692 && it->current_x < it->last_visible_x)
11693 {
11694 ++it->hpos;
11695 row->ascent = max (row->ascent, it->max_ascent);
11696 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11697 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11698 row->phys_height = max (row->phys_height,
11699 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11700 if (it->current_x - it->pixel_width < it->first_visible_x)
11701 row->x = x - it->first_visible_x;
11702 }
11703 else
11704 {
11705 int new_x;
11706 struct glyph *glyph;
11707
11708 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 11709 {
5f5c8ee5
GM
11710 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11711 new_x = x + glyph->pixel_width;
11712
11713 if (/* Lines are continued. */
11714 !it->truncate_lines_p
11715 && (/* Glyph doesn't fit on the line. */
11716 new_x > it->last_visible_x
11717 /* Or it fits exactly on a window system frame. */
11718 || (new_x == it->last_visible_x
11719 && FRAME_WINDOW_P (it->f))))
a2889657 11720 {
5f5c8ee5
GM
11721 /* End of a continued line. */
11722
11723 if (it->hpos == 0
11724 || (new_x == it->last_visible_x
11725 && FRAME_WINDOW_P (it->f)))
11726 {
e6819faf
GM
11727 /* Current glyph is the only one on the line or
11728 fits exactly on the line. We must continue
11729 the line because we can't draw the cursor
11730 after the glyph. */
5f5c8ee5
GM
11731 row->continued_p = 1;
11732 it->current_x = new_x;
11733 it->continuation_lines_width += new_x;
11734 ++it->hpos;
11735 if (i == nglyphs - 1)
11736 set_iterator_to_next (it);
11737 }
3b3c4bf0
GM
11738 else if (CHAR_GLYPH_PADDING_P (*glyph)
11739 && !FRAME_WINDOW_P (it->f))
11740 {
11741 /* A padding glyph that doesn't fit on this line.
11742 This means the whole character doesn't fit
11743 on the line. */
11744 row->used[TEXT_AREA] = n_glyphs_before;
11745
11746 /* Fill the rest of the row with continuation
11747 glyphs like in 20.x. */
11748 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11749 < row->glyphs[1 + TEXT_AREA])
11750 produce_special_glyphs (it, IT_CONTINUATION);
11751
11752 row->continued_p = 1;
11753 it->current_x = x_before;
11754 it->continuation_lines_width += x_before;
11755
11756 /* Restore the height to what it was before the
11757 element not fitting on the line. */
11758 it->max_ascent = ascent;
11759 it->max_descent = descent;
11760 it->max_phys_ascent = phys_ascent;
11761 it->max_phys_descent = phys_descent;
11762 }
5f5c8ee5 11763 else
5936754e 11764 {
5f5c8ee5
GM
11765 /* Display element draws past the right edge of
11766 the window. Restore positions to values
11767 before the element. The next line starts
11768 with current_x before the glyph that could
11769 not be displayed, so that TAB works right. */
11770 row->used[TEXT_AREA] = n_glyphs_before + i;
11771
11772 /* Display continuation glyphs. */
11773 if (!FRAME_WINDOW_P (it->f))
11774 produce_special_glyphs (it, IT_CONTINUATION);
11775 row->continued_p = 1;
11776
11777 it->current_x = x;
11778 it->continuation_lines_width += x;
91004049
GM
11779 if (nglyphs > 1 && i > 0)
11780 {
11781 row->ends_in_middle_of_char_p = 1;
11782 it->starts_in_middle_of_char_p = 1;
11783 }
e6819faf
GM
11784
11785 /* Restore the height to what it was before the
11786 element not fitting on the line. */
11787 it->max_ascent = ascent;
11788 it->max_descent = descent;
11789 it->max_phys_ascent = phys_ascent;
11790 it->max_phys_descent = phys_descent;
5936754e 11791 }
e6819faf 11792
5f5c8ee5
GM
11793 break;
11794 }
11795 else if (new_x > it->first_visible_x)
11796 {
11797 /* Increment number of glyphs actually displayed. */
11798 ++it->hpos;
11799
11800 if (x < it->first_visible_x)
11801 /* Glyph is partially visible, i.e. row starts at
11802 negative X position. */
11803 row->x = x - it->first_visible_x;
11804 }
11805 else
11806 {
11807 /* Glyph is completely off the left margin of the
11808 window. This should not happen because of the
11809 move_it_in_display_line at the start of
11810 this function. */
11811 abort ();
a2889657 11812 }
a2889657 11813 }
5f5c8ee5
GM
11814
11815 row->ascent = max (row->ascent, it->max_ascent);
11816 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11817 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11818 row->phys_height = max (row->phys_height,
11819 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11820
11821 /* End of this display line if row is continued. */
11822 if (row->continued_p)
11823 break;
a2889657 11824 }
a2889657 11825
5f5c8ee5
GM
11826 /* Is this a line end? If yes, we're also done, after making
11827 sure that a non-default face is extended up to the right
11828 margin of the window. */
11829 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 11830 {
5f5c8ee5
GM
11831 int used_before = row->used[TEXT_AREA];
11832
11833 /* Add a space at the end of the line that is used to
11834 display the cursor there. */
11835 append_space (it, 0);
11836
11837 /* Extend the face to the end of the line. */
11838 extend_face_to_end_of_line (it);
11839
11840 /* Make sure we have the position. */
11841 if (used_before == 0)
11842 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11843
11844 /* Consume the line end. This skips over invisible lines. */
11845 set_iterator_to_next (it);
11846 it->continuation_lines_width = 0;
11847 break;
1c9241f5 11848 }
a2889657 11849
5f5c8ee5
GM
11850 /* Proceed with next display element. Note that this skips
11851 over lines invisible because of selective display. */
11852 set_iterator_to_next (it);
b1d1124b 11853
5f5c8ee5
GM
11854 /* If we truncate lines, we are done when the last displayed
11855 glyphs reach past the right margin of the window. */
11856 if (it->truncate_lines_p
11857 && (FRAME_WINDOW_P (it->f)
11858 ? (it->current_x >= it->last_visible_x)
11859 : (it->current_x > it->last_visible_x)))
75d13c64 11860 {
5f5c8ee5
GM
11861 /* Maybe add truncation glyphs. */
11862 if (!FRAME_WINDOW_P (it->f))
11863 {
11864 --it->glyph_row->used[TEXT_AREA];
11865 produce_special_glyphs (it, IT_TRUNCATION);
11866 }
11867
11868 row->truncated_on_right_p = 1;
11869 it->continuation_lines_width = 0;
312246d1 11870 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
11871 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11872 it->hpos = hpos_before;
11873 it->current_x = x_before;
11874 break;
75d13c64 11875 }
a2889657 11876 }
a2889657 11877
5f5c8ee5
GM
11878 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11879 at the left window margin. */
11880 if (it->first_visible_x
11881 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11882 {
11883 if (!FRAME_WINDOW_P (it->f))
11884 insert_left_trunc_glyphs (it);
11885 row->truncated_on_left_p = 1;
11886 }
a2889657 11887
5f5c8ee5
GM
11888 /* If the start of this line is the overlay arrow-position, then
11889 mark this glyph row as the one containing the overlay arrow.
11890 This is clearly a mess with variable size fonts. It would be
11891 better to let it be displayed like cursors under X. */
e24c997d 11892 if (MARKERP (Voverlay_arrow_position)
a2889657 11893 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
11894 && (MATRIX_ROW_START_CHARPOS (row)
11895 == marker_position (Voverlay_arrow_position))
e24c997d 11896 && STRINGP (Voverlay_arrow_string)
a2889657
JB
11897 && ! overlay_arrow_seen)
11898 {
5f5c8ee5
GM
11899 /* Overlay arrow in window redisplay is a bitmap. */
11900 if (!FRAME_WINDOW_P (it->f))
c4628384 11901 {
5f5c8ee5
GM
11902 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11903 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11904 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11905 struct glyph *p = row->glyphs[TEXT_AREA];
11906 struct glyph *p2, *end;
11907
11908 /* Copy the arrow glyphs. */
11909 while (glyph < arrow_end)
11910 *p++ = *glyph++;
11911
11912 /* Throw away padding glyphs. */
11913 p2 = p;
11914 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11915 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11916 ++p2;
11917 if (p2 > p)
212e4f87 11918 {
5f5c8ee5
GM
11919 while (p2 < end)
11920 *p++ = *p2++;
11921 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 11922 }
c4628384 11923 }
5f5c8ee5 11924
a2889657 11925 overlay_arrow_seen = 1;
5f5c8ee5 11926 row->overlay_arrow_p = 1;
a2889657
JB
11927 }
11928
5f5c8ee5
GM
11929 /* Compute pixel dimensions of this line. */
11930 compute_line_metrics (it);
11931
11932 /* Remember the position at which this line ends. */
11933 row->end = it->current;
11934
173cbca8 11935 /* Maybe set the cursor. */
5f5c8ee5
GM
11936 if (it->w->cursor.vpos < 0
11937 && PT >= MATRIX_ROW_START_CHARPOS (row)
173cbca8
GM
11938 && PT <= MATRIX_ROW_END_CHARPOS (row))
11939 {
11940 /* Also see redisplay_window, case cursor movement in unchanged
11941 window. */
11942 if (MATRIX_ROW_END_CHARPOS (row) == PT
11943 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11944 && !row->ends_at_zv_p)
11945 ;
11946 else
11947 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11948 }
5f5c8ee5
GM
11949
11950 /* Highlight trailing whitespace. */
8f897821 11951 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
11952 highlight_trailing_whitespace (it->f, it->glyph_row);
11953
11954 /* Prepare for the next line. This line starts horizontally at (X
11955 HPOS) = (0 0). Vertical positions are incremented. As a
11956 convenience for the caller, IT->glyph_row is set to the next
11957 row to be used. */
11958 it->current_x = it->hpos = 0;
11959 it->current_y += row->height;
11960 ++it->vpos;
11961 ++it->glyph_row;
11962 return row->displays_text_p;
a2889657 11963}
5f5c8ee5
GM
11964
11965
a2889657 11966\f
5f5c8ee5
GM
11967/***********************************************************************
11968 Menu Bar
11969 ***********************************************************************/
11970
11971/* Redisplay the menu bar in the frame for window W.
11972
11973 The menu bar of X frames that don't have X toolkit support is
11974 displayed in a special window W->frame->menu_bar_window.
11975
11976 The menu bar of terminal frames is treated specially as far as
11977 glyph matrices are concerned. Menu bar lines are not part of
11978 windows, so the update is done directly on the frame matrix rows
11979 for the menu bar. */
7ce2c095
RS
11980
11981static void
11982display_menu_bar (w)
11983 struct window *w;
11984{
5f5c8ee5
GM
11985 struct frame *f = XFRAME (WINDOW_FRAME (w));
11986 struct it it;
11987 Lisp_Object items;
8351baf2 11988 int i;
7ce2c095 11989
5f5c8ee5 11990 /* Don't do all this for graphical frames. */
dc937613 11991#ifdef HAVE_NTGUI
d129c4c2
KH
11992 if (!NILP (Vwindow_system))
11993 return;
dc937613 11994#endif
dc937613 11995#ifdef USE_X_TOOLKIT
d3413a53 11996 if (FRAME_X_P (f))
7ce2c095 11997 return;
5f5c8ee5
GM
11998#endif
11999
12000#ifdef USE_X_TOOLKIT
12001 xassert (!FRAME_WINDOW_P (f));
52377a47 12002 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
12003 it.first_visible_x = 0;
12004 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12005#else /* not USE_X_TOOLKIT */
12006 if (FRAME_WINDOW_P (f))
12007 {
12008 /* Menu bar lines are displayed in the desired matrix of the
12009 dummy window menu_bar_window. */
12010 struct window *menu_w;
12011 xassert (WINDOWP (f->menu_bar_window));
12012 menu_w = XWINDOW (f->menu_bar_window);
12013 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 12014 MENU_FACE_ID);
5f5c8ee5
GM
12015 it.first_visible_x = 0;
12016 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12017 }
12018 else
12019 {
12020 /* This is a TTY frame, i.e. character hpos/vpos are used as
12021 pixel x/y. */
12022 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 12023 MENU_FACE_ID);
5f5c8ee5
GM
12024 it.first_visible_x = 0;
12025 it.last_visible_x = FRAME_WIDTH (f);
12026 }
12027#endif /* not USE_X_TOOLKIT */
12028
12029 /* Clear all rows of the menu bar. */
12030 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12031 {
12032 struct glyph_row *row = it.glyph_row + i;
12033 clear_glyph_row (row);
12034 row->enabled_p = 1;
12035 row->full_width_p = 1;
12036 }
7ce2c095 12037
5f5c8ee5
GM
12038 /* Make the first line of the menu bar appear in reverse video. */
12039 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 12040
5f5c8ee5
GM
12041 /* Display all items of the menu bar. */
12042 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 12043 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 12044 {
5f5c8ee5
GM
12045 Lisp_Object string;
12046
12047 /* Stop at nil string. */
8351baf2
RS
12048 string = XVECTOR (items)->contents[i + 1];
12049 if (NILP (string))
12050 break;
2d66ad19 12051
5f5c8ee5
GM
12052 /* Remember where item was displayed. */
12053 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 12054
5f5c8ee5
GM
12055 /* Display the item, pad with one space. */
12056 if (it.current_x < it.last_visible_x)
12057 display_string (NULL, string, Qnil, 0, 0, &it,
12058 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
12059 }
12060
2d66ad19 12061 /* Fill out the line with spaces. */
5f5c8ee5
GM
12062 if (it.current_x < it.last_visible_x)
12063 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 12064
5f5c8ee5
GM
12065 /* Compute the total height of the lines. */
12066 compute_line_metrics (&it);
7ce2c095 12067}
5f5c8ee5
GM
12068
12069
7ce2c095 12070\f
5f5c8ee5
GM
12071/***********************************************************************
12072 Mode Line
12073 ***********************************************************************/
12074
715e84c9
GM
12075/* Redisplay mode lines in the window tree whose root is WINDOW. If
12076 FORCE is non-zero, redisplay mode lines unconditionally.
12077 Otherwise, redisplay only mode lines that are garbaged. Value is
12078 the number of windows whose mode lines were redisplayed. */
a2889657 12079
715e84c9
GM
12080static int
12081redisplay_mode_lines (window, force)
12082 Lisp_Object window;
12083 int force;
12084{
12085 int nwindows = 0;
12086
12087 while (!NILP (window))
12088 {
12089 struct window *w = XWINDOW (window);
12090
12091 if (WINDOWP (w->hchild))
12092 nwindows += redisplay_mode_lines (w->hchild, force);
12093 else if (WINDOWP (w->vchild))
12094 nwindows += redisplay_mode_lines (w->vchild, force);
12095 else if (force
12096 || FRAME_GARBAGED_P (XFRAME (w->frame))
12097 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12098 {
12099 Lisp_Object old_selected_frame;
12100 struct text_pos lpoint;
12101 struct buffer *old = current_buffer;
12102
12103 /* Set the window's buffer for the mode line display. */
12104 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12105 set_buffer_internal_1 (XBUFFER (w->buffer));
12106
12107 /* Point refers normally to the selected window. For any
12108 other window, set up appropriate value. */
12109 if (!EQ (window, selected_window))
12110 {
12111 struct text_pos pt;
12112
12113 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12114 if (CHARPOS (pt) < BEGV)
12115 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12116 else if (CHARPOS (pt) > (ZV - 1))
12117 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12118 else
12119 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12120 }
12121
12122 /* Temporarily set up the selected frame. */
12123 old_selected_frame = selected_frame;
12124 selected_frame = w->frame;
12125
12126 /* Display mode lines. */
12127 clear_glyph_matrix (w->desired_matrix);
12128 if (display_mode_lines (w))
12129 {
12130 ++nwindows;
12131 w->must_be_updated_p = 1;
12132 }
12133
12134 /* Restore old settings. */
12135 selected_frame = old_selected_frame;
12136 set_buffer_internal_1 (old);
12137 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12138 }
12139
12140 window = w->next;
12141 }
12142
12143 return nwindows;
12144}
12145
12146
12147/* Display the mode and/or top line of window W. Value is the number
12148 of mode lines displayed. */
12149
12150static int
5f5c8ee5 12151display_mode_lines (w)
a2889657
JB
12152 struct window *w;
12153{
715e84c9
GM
12154 int n = 0;
12155
5f5c8ee5 12156 /* These will be set while the mode line specs are processed. */
aa6d10fa 12157 line_number_displayed = 0;
155ef550 12158 w->column_number_displayed = Qnil;
aa6d10fa 12159
5f5c8ee5 12160 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9
GM
12161 {
12162 display_mode_line (w, MODE_LINE_FACE_ID,
12163 current_buffer->mode_line_format);
12164 ++n;
12165 }
5f5c8ee5 12166
045dee35 12167 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
12168 {
12169 display_mode_line (w, HEADER_LINE_FACE_ID,
12170 current_buffer->header_line_format);
12171 ++n;
12172 }
12173
12174 return n;
5f5c8ee5 12175}
03b294dc 12176
03b294dc 12177
5f5c8ee5 12178/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 12179 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
5f5c8ee5 12180 FORMAT is the mode line format to display. */
03b294dc 12181
5f5c8ee5
GM
12182static void
12183display_mode_line (w, face_id, format)
12184 struct window *w;
12185 enum face_id face_id;
12186 Lisp_Object format;
12187{
12188 struct it it;
12189 struct face *face;
03b294dc 12190
5f5c8ee5
GM
12191 init_iterator (&it, w, -1, -1, NULL, face_id);
12192 prepare_desired_row (it.glyph_row);
12193
12194 /* Temporarily make frame's keyboard the current kboard so that
12195 kboard-local variables in the mode_line_format will get the right
12196 values. */
12197 push_frame_kboard (it.f);
12198 display_mode_element (&it, 0, 0, 0, format);
12199 pop_frame_kboard ();
a2889657 12200
5f5c8ee5
GM
12201 /* Fill up with spaces. */
12202 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12203
12204 compute_line_metrics (&it);
12205 it.glyph_row->full_width_p = 1;
12206 it.glyph_row->mode_line_p = 1;
12207 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12208 it.glyph_row->continued_p = 0;
12209 it.glyph_row->truncated_on_left_p = 0;
12210 it.glyph_row->truncated_on_right_p = 0;
12211
12212 /* Make a 3D mode-line have a shadow at its right end. */
12213 face = FACE_FROM_ID (it.f, face_id);
12214 extend_face_to_end_of_line (&it);
12215 if (face->box != FACE_NO_BOX)
d7eb09a0 12216 {
5f5c8ee5
GM
12217 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12218 + it.glyph_row->used[TEXT_AREA] - 1);
12219 last->right_box_line_p = 1;
d7eb09a0 12220 }
a2889657
JB
12221}
12222
a2889657 12223
5f5c8ee5
GM
12224/* Contribute ELT to the mode line for window IT->w. How it
12225 translates into text depends on its data type.
a2889657 12226
5f5c8ee5 12227 IT describes the display environment in which we display, as usual.
a2889657
JB
12228
12229 DEPTH is the depth in recursion. It is used to prevent
12230 infinite recursion here.
12231
5f5c8ee5
GM
12232 FIELD_WIDTH is the number of characters the display of ELT should
12233 occupy in the mode line, and PRECISION is the maximum number of
12234 characters to display from ELT's representation. See
12235 display_string for details. *
a2889657 12236
5f5c8ee5 12237 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
12238
12239static int
5f5c8ee5
GM
12240display_mode_element (it, depth, field_width, precision, elt)
12241 struct it *it;
a2889657 12242 int depth;
5f5c8ee5
GM
12243 int field_width, precision;
12244 Lisp_Object elt;
a2889657 12245{
5f5c8ee5
GM
12246 int n = 0, field, prec;
12247
a2889657
JB
12248 tail_recurse:
12249 if (depth > 10)
12250 goto invalid;
12251
12252 depth++;
12253
0220c518 12254 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
12255 {
12256 case Lisp_String:
12257 {
12258 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
12259 unsigned char c;
12260 unsigned char *this = XSTRING (elt)->data;
12261 unsigned char *lisp_string = this;
12262
12263 while ((precision <= 0 || n < precision)
12264 && *this
12265 && (frame_title_ptr
12266 || it->current_x < it->last_visible_x))
a2889657
JB
12267 {
12268 unsigned char *last = this;
5f5c8ee5
GM
12269
12270 /* Advance to end of string or next format specifier. */
a2889657
JB
12271 while ((c = *this++) != '\0' && c != '%')
12272 ;
5f5c8ee5 12273
a2889657
JB
12274 if (this - 1 != last)
12275 {
5f5c8ee5
GM
12276 /* Output to end of string or up to '%'. Field width
12277 is length of string. Don't output more than
12278 PRECISION allows us. */
12279 prec = --this - last;
12280 if (precision > 0 && prec > precision - n)
12281 prec = precision - n;
12282
d39b6696 12283 if (frame_title_ptr)
5f5c8ee5 12284 n += store_frame_title (last, prec, prec);
d39b6696 12285 else
5f5c8ee5
GM
12286 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12287 it, 0, prec, 0, -1);
a2889657
JB
12288 }
12289 else /* c == '%' */
12290 {
5f5c8ee5
GM
12291 unsigned char *percent_position = this;
12292
12293 /* Get the specified minimum width. Zero means
12294 don't pad. */
12295 field = 0;
a2889657 12296 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 12297 field = field * 10 + c - '0';
a2889657 12298
5f5c8ee5
GM
12299 /* Don't pad beyond the total padding allowed. */
12300 if (field_width - n > 0 && field > field_width - n)
12301 field = field_width - n;
a2889657 12302
5f5c8ee5
GM
12303 /* Note that either PRECISION <= 0 or N < PRECISION. */
12304 prec = precision - n;
12305
a2889657 12306 if (c == 'M')
5f5c8ee5
GM
12307 n += display_mode_element (it, depth, field, prec,
12308 Vglobal_mode_string);
a2889657 12309 else if (c != 0)
d39b6696 12310 {
5f5c8ee5
GM
12311 unsigned char *spec
12312 = decode_mode_spec (it->w, c, field, prec);
12313
d39b6696 12314 if (frame_title_ptr)
5f5c8ee5 12315 n += store_frame_title (spec, field, prec);
d39b6696 12316 else
5f5c8ee5
GM
12317 {
12318 int nglyphs_before
12319 = it->glyph_row->used[TEXT_AREA];
12320 int charpos
12321 = percent_position - XSTRING (elt)->data;
12322 int nwritten
12323 = display_string (spec, Qnil, elt, charpos, 0, it,
12324 field, prec, 0, -1);
12325
12326 /* Assign to the glyphs written above the
12327 string where the `%x' came from, position
12328 of the `%'. */
12329 if (nwritten > 0)
12330 {
12331 struct glyph *glyph
12332 = (it->glyph_row->glyphs[TEXT_AREA]
12333 + nglyphs_before);
12334 int i;
12335
12336 for (i = 0; i < nwritten; ++i)
12337 {
12338 glyph[i].object = elt;
12339 glyph[i].charpos = charpos;
12340 }
12341
12342 n += nwritten;
12343 }
12344 }
d39b6696 12345 }
a2889657
JB
12346 }
12347 }
12348 }
12349 break;
12350
12351 case Lisp_Symbol:
12352 /* A symbol: process the value of the symbol recursively
12353 as if it appeared here directly. Avoid error if symbol void.
12354 Special case: if value of symbol is a string, output the string
12355 literally. */
12356 {
12357 register Lisp_Object tem;
12358 tem = Fboundp (elt);
265a9e55 12359 if (!NILP (tem))
a2889657
JB
12360 {
12361 tem = Fsymbol_value (elt);
12362 /* If value is a string, output that string literally:
12363 don't check for % within it. */
e24c997d 12364 if (STRINGP (tem))
d39b6696 12365 {
5f5c8ee5
GM
12366 prec = XSTRING (tem)->size;
12367 if (precision > 0 && prec > precision - n)
12368 prec = precision - n;
d39b6696 12369 if (frame_title_ptr)
5f5c8ee5 12370 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 12371 else
5f5c8ee5
GM
12372 n += display_string (NULL, tem, Qnil, 0, 0, it,
12373 0, prec, 0, -1);
d39b6696 12374 }
a2889657 12375 else if (!EQ (tem, elt))
5f5c8ee5
GM
12376 {
12377 /* Give up right away for nil or t. */
12378 elt = tem;
12379 goto tail_recurse;
12380 }
a2889657
JB
12381 }
12382 }
12383 break;
12384
12385 case Lisp_Cons:
12386 {
12387 register Lisp_Object car, tem;
12388
12389 /* A cons cell: three distinct cases.
12390 If first element is a string or a cons, process all the elements
12391 and effectively concatenate them.
12392 If first element is a negative number, truncate displaying cdr to
12393 at most that many characters. If positive, pad (with spaces)
12394 to at least that many characters.
12395 If first element is a symbol, process the cadr or caddr recursively
12396 according to whether the symbol's value is non-nil or nil. */
9472f927 12397 car = XCAR (elt);
5f5c8ee5
GM
12398 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12399 {
12400 /* An element of the form (:eval FORM) means evaluate FORM
12401 and use the result as mode line elements. */
12402 struct gcpro gcpro1;
12403 Lisp_Object spec;
12404
12405 spec = eval_form (XCAR (XCDR (elt)));
12406 GCPRO1 (spec);
12407 n += display_mode_element (it, depth, field_width - n,
12408 precision - n, spec);
12409 UNGCPRO;
12410 }
12411 else if (SYMBOLP (car))
a2889657
JB
12412 {
12413 tem = Fboundp (car);
9472f927 12414 elt = XCDR (elt);
e24c997d 12415 if (!CONSP (elt))
a2889657
JB
12416 goto invalid;
12417 /* elt is now the cdr, and we know it is a cons cell.
12418 Use its car if CAR has a non-nil value. */
265a9e55 12419 if (!NILP (tem))
a2889657
JB
12420 {
12421 tem = Fsymbol_value (car);
265a9e55 12422 if (!NILP (tem))
9472f927
GM
12423 {
12424 elt = XCAR (elt);
12425 goto tail_recurse;
12426 }
a2889657
JB
12427 }
12428 /* Symbol's value is nil (or symbol is unbound)
12429 Get the cddr of the original list
12430 and if possible find the caddr and use that. */
9472f927 12431 elt = XCDR (elt);
265a9e55 12432 if (NILP (elt))
a2889657 12433 break;
e24c997d 12434 else if (!CONSP (elt))
a2889657 12435 goto invalid;
9472f927 12436 elt = XCAR (elt);
a2889657
JB
12437 goto tail_recurse;
12438 }
e24c997d 12439 else if (INTEGERP (car))
a2889657
JB
12440 {
12441 register int lim = XINT (car);
9472f927 12442 elt = XCDR (elt);
a2889657 12443 if (lim < 0)
5f5c8ee5
GM
12444 {
12445 /* Negative int means reduce maximum width. */
12446 if (precision <= 0)
12447 precision = -lim;
12448 else
12449 precision = min (precision, -lim);
12450 }
a2889657
JB
12451 else if (lim > 0)
12452 {
12453 /* Padding specified. Don't let it be more than
12454 current maximum. */
5f5c8ee5
GM
12455 if (precision > 0)
12456 lim = min (precision, lim);
12457
a2889657
JB
12458 /* If that's more padding than already wanted, queue it.
12459 But don't reduce padding already specified even if
12460 that is beyond the current truncation point. */
5f5c8ee5 12461 field_width = max (lim, field_width);
a2889657
JB
12462 }
12463 goto tail_recurse;
12464 }
e24c997d 12465 else if (STRINGP (car) || CONSP (car))
a2889657
JB
12466 {
12467 register int limit = 50;
5f5c8ee5
GM
12468 /* Limit is to protect against circular lists. */
12469 while (CONSP (elt)
12470 && --limit > 0
12471 && (precision <= 0 || n < precision))
a2889657 12472 {
5f5c8ee5 12473 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
12474 precision - n, XCAR (elt));
12475 elt = XCDR (elt);
a2889657
JB
12476 }
12477 }
12478 }
12479 break;
12480
12481 default:
12482 invalid:
d39b6696 12483 if (frame_title_ptr)
5f5c8ee5 12484 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 12485 else
5f5c8ee5
GM
12486 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12487 precision - n, 0, 0);
12488 return n;
a2889657
JB
12489 }
12490
5f5c8ee5
GM
12491 /* Pad to FIELD_WIDTH. */
12492 if (field_width > 0 && n < field_width)
12493 {
12494 if (frame_title_ptr)
12495 n += store_frame_title ("", field_width - n, 0);
12496 else
12497 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12498 0, 0, 0);
12499 }
12500
12501 return n;
a2889657 12502}
5f5c8ee5
GM
12503
12504
766525bc
RS
12505/* Write a null-terminated, right justified decimal representation of
12506 the positive integer D to BUF using a minimal field width WIDTH. */
12507
12508static void
12509pint2str (buf, width, d)
12510 register char *buf;
12511 register int width;
12512 register int d;
12513{
12514 register char *p = buf;
12515
12516 if (d <= 0)
5f5c8ee5 12517 *p++ = '0';
766525bc 12518 else
5f5c8ee5 12519 {
766525bc 12520 while (d > 0)
5f5c8ee5 12521 {
766525bc
RS
12522 *p++ = d % 10 + '0';
12523 d /= 10;
5f5c8ee5
GM
12524 }
12525 }
12526
12527 for (width -= (int) (p - buf); width > 0; --width)
12528 *p++ = ' ';
766525bc
RS
12529 *p-- = '\0';
12530 while (p > buf)
5f5c8ee5 12531 {
766525bc
RS
12532 d = *buf;
12533 *buf++ = *p;
12534 *p-- = d;
5f5c8ee5 12535 }
766525bc
RS
12536}
12537
5f5c8ee5 12538/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
12539 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12540 type of CODING_SYSTEM. Return updated pointer into BUF. */
12541
6693a99a 12542static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 12543
1c9241f5
KH
12544static char *
12545decode_mode_spec_coding (coding_system, buf, eol_flag)
12546 Lisp_Object coding_system;
12547 register char *buf;
12548 int eol_flag;
12549{
1e1078d6 12550 Lisp_Object val;
916848d8 12551 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
12552 unsigned char *eol_str;
12553 int eol_str_len;
12554 /* The EOL conversion we are using. */
12555 Lisp_Object eoltype;
1e1078d6 12556
4a09dee0 12557 val = Fget (coding_system, Qcoding_system);
085536c2 12558 eoltype = Qnil;
1c9241f5 12559
4a09dee0 12560 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 12561 {
916848d8
RS
12562 if (multibyte)
12563 *buf++ = '-';
21e989e3 12564 if (eol_flag)
302f2b38 12565 eoltype = eol_mnemonic_undecided;
1e1078d6 12566 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
12567 }
12568 else
12569 {
1e1078d6
RS
12570 Lisp_Object eolvalue;
12571
12572 eolvalue = Fget (coding_system, Qeol_type);
12573
916848d8
RS
12574 if (multibyte)
12575 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12576
1c9241f5
KH
12577 if (eol_flag)
12578 {
1e1078d6
RS
12579 /* The EOL conversion that is normal on this system. */
12580
12581 if (NILP (eolvalue)) /* Not yet decided. */
12582 eoltype = eol_mnemonic_undecided;
12583 else if (VECTORP (eolvalue)) /* Not yet decided. */
12584 eoltype = eol_mnemonic_undecided;
12585 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12586 eoltype = (XFASTINT (eolvalue) == 0
12587 ? eol_mnemonic_unix
12588 : (XFASTINT (eolvalue) == 1
12589 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
12590 }
12591 }
5f5c8ee5 12592
302f2b38
EZ
12593 if (eol_flag)
12594 {
12595 /* Mention the EOL conversion if it is not the usual one. */
12596 if (STRINGP (eoltype))
12597 {
12598 eol_str = XSTRING (eoltype)->data;
12599 eol_str_len = XSTRING (eoltype)->size;
12600 }
f30b3499
KH
12601 else if (INTEGERP (eoltype)
12602 && CHAR_VALID_P (XINT (eoltype), 0))
12603 {
4a09dee0 12604 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 12605 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 12606 }
302f2b38
EZ
12607 else
12608 {
12609 eol_str = invalid_eol_type;
12610 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 12611 }
f30b3499 12612 bcopy (eol_str, buf, eol_str_len);
302f2b38 12613 buf += eol_str_len;
1c9241f5 12614 }
302f2b38 12615
1c9241f5
KH
12616 return buf;
12617}
12618
a2889657 12619/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
12620 generated by character C. PRECISION >= 0 means don't return a
12621 string longer than that value. FIELD_WIDTH > 0 means pad the
12622 string returned with spaces to that value. */
a2889657 12623
11e82b76
JB
12624static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12625
a2889657 12626static char *
5f5c8ee5 12627decode_mode_spec (w, c, field_width, precision)
a2889657 12628 struct window *w;
68c45bf0 12629 register int c;
5f5c8ee5 12630 int field_width, precision;
a2889657 12631{
0b67772d 12632 Lisp_Object obj;
5f5c8ee5
GM
12633 struct frame *f = XFRAME (WINDOW_FRAME (w));
12634 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 12635 struct buffer *b = XBUFFER (w->buffer);
a2889657 12636
0b67772d 12637 obj = Qnil;
a2889657
JB
12638
12639 switch (c)
12640 {
1af9f229
RS
12641 case '*':
12642 if (!NILP (b->read_only))
12643 return "%";
12644 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12645 return "*";
12646 return "-";
12647
12648 case '+':
12649 /* This differs from %* only for a modified read-only buffer. */
12650 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12651 return "*";
12652 if (!NILP (b->read_only))
12653 return "%";
12654 return "-";
12655
12656 case '&':
12657 /* This differs from %* in ignoring read-only-ness. */
12658 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12659 return "*";
12660 return "-";
12661
12662 case '%':
12663 return "%";
12664
12665 case '[':
12666 {
12667 int i;
12668 char *p;
12669
12670 if (command_loop_level > 5)
12671 return "[[[... ";
12672 p = decode_mode_spec_buf;
12673 for (i = 0; i < command_loop_level; i++)
12674 *p++ = '[';
12675 *p = 0;
12676 return decode_mode_spec_buf;
12677 }
12678
12679 case ']':
12680 {
12681 int i;
12682 char *p;
12683
12684 if (command_loop_level > 5)
12685 return " ...]]]";
12686 p = decode_mode_spec_buf;
12687 for (i = 0; i < command_loop_level; i++)
12688 *p++ = ']';
12689 *p = 0;
12690 return decode_mode_spec_buf;
12691 }
12692
12693 case '-':
12694 {
1af9f229 12695 register int i;
5f5c8ee5
GM
12696
12697 /* Let lots_of_dashes be a string of infinite length. */
12698 if (field_width <= 0
12699 || field_width > sizeof (lots_of_dashes))
1af9f229 12700 {
5f5c8ee5
GM
12701 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12702 decode_mode_spec_buf[i] = '-';
12703 decode_mode_spec_buf[i] = '\0';
12704 return decode_mode_spec_buf;
1af9f229 12705 }
5f5c8ee5
GM
12706 else
12707 return lots_of_dashes;
1af9f229
RS
12708 }
12709
a2889657 12710 case 'b':
d39b6696 12711 obj = b->name;
a2889657
JB
12712 break;
12713
1af9f229
RS
12714 case 'c':
12715 {
12716 int col = current_column ();
12717 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 12718 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
12719 return decode_mode_spec_buf;
12720 }
12721
12722 case 'F':
12723 /* %F displays the frame name. */
5f5c8ee5 12724 if (!NILP (f->title))
95184b48 12725 return (char *) XSTRING (f->title)->data;
fd8ff63d 12726 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 12727 return (char *) XSTRING (f->name)->data;
9c6da96f 12728 return "Emacs";
1af9f229 12729
a2889657 12730 case 'f':
d39b6696 12731 obj = b->filename;
a2889657
JB
12732 break;
12733
aa6d10fa
RS
12734 case 'l':
12735 {
12adba34
RS
12736 int startpos = XMARKER (w->start)->charpos;
12737 int startpos_byte = marker_byte_position (w->start);
12738 int line, linepos, linepos_byte, topline;
aa6d10fa 12739 int nlines, junk;
aa6d10fa
RS
12740 int height = XFASTINT (w->height);
12741
12742 /* If we decided that this buffer isn't suitable for line numbers,
12743 don't forget that too fast. */
12744 if (EQ (w->base_line_pos, w->buffer))
766525bc 12745 goto no_value;
5300fd39
RS
12746 /* But do forget it, if the window shows a different buffer now. */
12747 else if (BUFFERP (w->base_line_pos))
12748 w->base_line_pos = Qnil;
aa6d10fa
RS
12749
12750 /* If the buffer is very big, don't waste time. */
37d034d3 12751 if (INTEGERP (Vline_number_display_limit)
090703f4 12752 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
12753 {
12754 w->base_line_pos = Qnil;
12755 w->base_line_number = Qnil;
766525bc 12756 goto no_value;
aa6d10fa
RS
12757 }
12758
12759 if (!NILP (w->base_line_number)
12760 && !NILP (w->base_line_pos)
12adba34 12761 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
12762 {
12763 line = XFASTINT (w->base_line_number);
12764 linepos = XFASTINT (w->base_line_pos);
12adba34 12765 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
12766 }
12767 else
12768 {
12769 line = 1;
d39b6696 12770 linepos = BUF_BEGV (b);
12adba34 12771 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
12772 }
12773
12774 /* Count lines from base line to window start position. */
12adba34
RS
12775 nlines = display_count_lines (linepos, linepos_byte,
12776 startpos_byte,
12777 startpos, &junk);
aa6d10fa
RS
12778
12779 topline = nlines + line;
12780
12781 /* Determine a new base line, if the old one is too close
12782 or too far away, or if we did not have one.
12783 "Too close" means it's plausible a scroll-down would
12784 go back past it. */
d39b6696 12785 if (startpos == BUF_BEGV (b))
aa6d10fa 12786 {
c2213350
KH
12787 XSETFASTINT (w->base_line_number, topline);
12788 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
12789 }
12790 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 12791 || linepos == BUF_BEGV (b))
aa6d10fa 12792 {
d39b6696 12793 int limit = BUF_BEGV (b);
12adba34 12794 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 12795 int position;
5d121aec 12796 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
12797
12798 if (startpos - distance > limit)
12adba34
RS
12799 {
12800 limit = startpos - distance;
12801 limit_byte = CHAR_TO_BYTE (limit);
12802 }
aa6d10fa 12803
12adba34
RS
12804 nlines = display_count_lines (startpos, startpos_byte,
12805 limit_byte,
12806 - (height * 2 + 30),
aa6d10fa
RS
12807 &position);
12808 /* If we couldn't find the lines we wanted within
5d121aec 12809 line_number_display_limit_width chars per line,
aa6d10fa 12810 give up on line numbers for this window. */
12adba34 12811 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
12812 {
12813 w->base_line_pos = w->buffer;
12814 w->base_line_number = Qnil;
766525bc 12815 goto no_value;
aa6d10fa
RS
12816 }
12817
c2213350 12818 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 12819 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
12820 }
12821
12822 /* Now count lines from the start pos to point. */
12adba34
RS
12823 nlines = display_count_lines (startpos, startpos_byte,
12824 PT_BYTE, PT, &junk);
aa6d10fa
RS
12825
12826 /* Record that we did display the line number. */
12827 line_number_displayed = 1;
12828
12829 /* Make the string to show. */
5f5c8ee5 12830 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 12831 return decode_mode_spec_buf;
766525bc
RS
12832 no_value:
12833 {
12834 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
12835 int pad = field_width - 2;
12836 while (pad-- > 0)
12837 *p++ = ' ';
12838 *p++ = '?';
b357b9d4
KR
12839 *p++ = '?';
12840 *p = '\0';
766525bc
RS
12841 return decode_mode_spec_buf;
12842 }
aa6d10fa
RS
12843 }
12844 break;
12845
a2889657 12846 case 'm':
d39b6696 12847 obj = b->mode_name;
a2889657
JB
12848 break;
12849
12850 case 'n':
d39b6696 12851 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
12852 return " Narrow";
12853 break;
12854
a2889657
JB
12855 case 'p':
12856 {
12857 int pos = marker_position (w->start);
d39b6696 12858 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 12859
d39b6696 12860 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 12861 {
d39b6696 12862 if (pos <= BUF_BEGV (b))
a2889657
JB
12863 return "All";
12864 else
12865 return "Bottom";
12866 }
d39b6696 12867 else if (pos <= BUF_BEGV (b))
a2889657
JB
12868 return "Top";
12869 else
12870 {
3c7d31b9
RS
12871 if (total > 1000000)
12872 /* Do it differently for a large value, to avoid overflow. */
12873 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12874 else
12875 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
12876 /* We can't normally display a 3-digit number,
12877 so get us a 2-digit number that is close. */
12878 if (total == 100)
12879 total = 99;
12880 sprintf (decode_mode_spec_buf, "%2d%%", total);
12881 return decode_mode_spec_buf;
12882 }
12883 }
12884
8ffcb79f
RS
12885 /* Display percentage of size above the bottom of the screen. */
12886 case 'P':
12887 {
12888 int toppos = marker_position (w->start);
d39b6696
KH
12889 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12890 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 12891
d39b6696 12892 if (botpos >= BUF_ZV (b))
8ffcb79f 12893 {
d39b6696 12894 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12895 return "All";
12896 else
12897 return "Bottom";
12898 }
12899 else
12900 {
3c7d31b9
RS
12901 if (total > 1000000)
12902 /* Do it differently for a large value, to avoid overflow. */
12903 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12904 else
12905 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
12906 /* We can't normally display a 3-digit number,
12907 so get us a 2-digit number that is close. */
12908 if (total == 100)
12909 total = 99;
d39b6696 12910 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12911 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12912 else
12913 sprintf (decode_mode_spec_buf, "%2d%%", total);
12914 return decode_mode_spec_buf;
12915 }
12916 }
12917
1af9f229
RS
12918 case 's':
12919 /* status of process */
12920 obj = Fget_buffer_process (w->buffer);
12921 if (NILP (obj))
12922 return "no process";
12923#ifdef subprocesses
12924 obj = Fsymbol_name (Fprocess_status (obj));
12925#endif
12926 break;
d39b6696 12927
1af9f229
RS
12928 case 't': /* indicate TEXT or BINARY */
12929#ifdef MODE_LINE_BINARY_TEXT
12930 return MODE_LINE_BINARY_TEXT (b);
12931#else
12932 return "T";
12933#endif
1c9241f5
KH
12934
12935 case 'z':
12936 /* coding-system (not including end-of-line format) */
12937 case 'Z':
12938 /* coding-system (including end-of-line type) */
12939 {
12940 int eol_flag = (c == 'Z');
539b4d41 12941 char *p = decode_mode_spec_buf;
1c9241f5 12942
d30e754b 12943 if (! FRAME_WINDOW_P (f))
1c9241f5 12944 {
11c52c4f
RS
12945 /* No need to mention EOL here--the terminal never needs
12946 to do EOL conversion. */
12947 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12948 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 12949 }
f13c925f 12950 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 12951 p, eol_flag);
f13c925f 12952
11c52c4f 12953#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
12954#ifdef subprocesses
12955 obj = Fget_buffer_process (Fcurrent_buffer ());
12956 if (PROCESSP (obj))
12957 {
12958 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12959 p, eol_flag);
12960 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12961 p, eol_flag);
12962 }
12963#endif /* subprocesses */
11c52c4f 12964#endif /* 0 */
1c9241f5
KH
12965 *p = 0;
12966 return decode_mode_spec_buf;
12967 }
a2889657 12968 }
d39b6696 12969
e24c997d 12970 if (STRINGP (obj))
a2889657
JB
12971 return (char *) XSTRING (obj)->data;
12972 else
12973 return "";
12974}
5f5c8ee5
GM
12975
12976
12adba34
RS
12977/* Count up to COUNT lines starting from START / START_BYTE.
12978 But don't go beyond LIMIT_BYTE.
12979 Return the number of lines thus found (always nonnegative).
59b49f63 12980
12adba34 12981 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
12982
12983static int
12adba34
RS
12984display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12985 int start, start_byte, limit_byte, count;
12986 int *byte_pos_ptr;
59b49f63 12987{
59b49f63
RS
12988 register unsigned char *cursor;
12989 unsigned char *base;
12990
12991 register int ceiling;
12992 register unsigned char *ceiling_addr;
12adba34 12993 int orig_count = count;
59b49f63
RS
12994
12995 /* If we are not in selective display mode,
12996 check only for newlines. */
12adba34
RS
12997 int selective_display = (!NILP (current_buffer->selective_display)
12998 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
12999
13000 if (count > 0)
12adba34
RS
13001 {
13002 while (start_byte < limit_byte)
13003 {
13004 ceiling = BUFFER_CEILING_OF (start_byte);
13005 ceiling = min (limit_byte - 1, ceiling);
13006 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13007 base = (cursor = BYTE_POS_ADDR (start_byte));
13008 while (1)
13009 {
13010 if (selective_display)
13011 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13012 ;
13013 else
13014 while (*cursor != '\n' && ++cursor != ceiling_addr)
13015 ;
13016
13017 if (cursor != ceiling_addr)
13018 {
13019 if (--count == 0)
13020 {
13021 start_byte += cursor - base + 1;
13022 *byte_pos_ptr = start_byte;
13023 return orig_count;
13024 }
13025 else
13026 if (++cursor == ceiling_addr)
13027 break;
13028 }
13029 else
13030 break;
13031 }
13032 start_byte += cursor - base;
13033 }
13034 }
59b49f63
RS
13035 else
13036 {
12adba34
RS
13037 while (start_byte > limit_byte)
13038 {
13039 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13040 ceiling = max (limit_byte, ceiling);
13041 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13042 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
13043 while (1)
13044 {
12adba34
RS
13045 if (selective_display)
13046 while (--cursor != ceiling_addr
13047 && *cursor != '\n' && *cursor != 015)
13048 ;
13049 else
13050 while (--cursor != ceiling_addr && *cursor != '\n')
13051 ;
13052
59b49f63
RS
13053 if (cursor != ceiling_addr)
13054 {
13055 if (++count == 0)
13056 {
12adba34
RS
13057 start_byte += cursor - base + 1;
13058 *byte_pos_ptr = start_byte;
13059 /* When scanning backwards, we should
13060 not count the newline posterior to which we stop. */
13061 return - orig_count - 1;
59b49f63
RS
13062 }
13063 }
13064 else
13065 break;
13066 }
12adba34
RS
13067 /* Here we add 1 to compensate for the last decrement
13068 of CURSOR, which took it past the valid range. */
13069 start_byte += cursor - base + 1;
59b49f63
RS
13070 }
13071 }
13072
12adba34 13073 *byte_pos_ptr = limit_byte;
aa6d10fa 13074
12adba34
RS
13075 if (count < 0)
13076 return - orig_count + count;
13077 return orig_count - count;
aa6d10fa 13078
12adba34 13079}
a2889657 13080
a2889657 13081
5f5c8ee5
GM
13082\f
13083/***********************************************************************
13084 Displaying strings
13085 ***********************************************************************/
278feba9 13086
5f5c8ee5 13087/* Display a NUL-terminated string, starting with index START.
a3788d53 13088
5f5c8ee5
GM
13089 If STRING is non-null, display that C string. Otherwise, the Lisp
13090 string LISP_STRING is displayed.
a2889657 13091
5f5c8ee5
GM
13092 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13093 FACE_STRING. Display STRING or LISP_STRING with the face at
13094 FACE_STRING_POS in FACE_STRING:
a2889657 13095
5f5c8ee5
GM
13096 Display the string in the environment given by IT, but use the
13097 standard display table, temporarily.
a3788d53 13098
5f5c8ee5
GM
13099 FIELD_WIDTH is the minimum number of output glyphs to produce.
13100 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13101 with spaces. If STRING has more characters, more than FIELD_WIDTH
13102 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13103
13104 PRECISION is the maximum number of characters to output from
13105 STRING. PRECISION < 0 means don't truncate the string.
a2889657 13106
5f5c8ee5 13107 This is roughly equivalent to printf format specifiers:
a2889657 13108
5f5c8ee5
GM
13109 FIELD_WIDTH PRECISION PRINTF
13110 ----------------------------------------
13111 -1 -1 %s
13112 -1 10 %.10s
13113 10 -1 %10s
13114 20 10 %20.10s
a2889657 13115
5f5c8ee5
GM
13116 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13117 display them, and < 0 means obey the current buffer's value of
13118 enable_multibyte_characters.
278feba9 13119
5f5c8ee5 13120 Value is the number of glyphs produced. */
b1d1124b 13121
5f5c8ee5
GM
13122static int
13123display_string (string, lisp_string, face_string, face_string_pos,
13124 start, it, field_width, precision, max_x, multibyte)
13125 unsigned char *string;
13126 Lisp_Object lisp_string;
68c45bf0
PE
13127 Lisp_Object face_string;
13128 int face_string_pos;
5f5c8ee5
GM
13129 int start;
13130 struct it *it;
13131 int field_width, precision, max_x;
13132 int multibyte;
13133{
13134 int hpos_at_start = it->hpos;
13135 int saved_face_id = it->face_id;
13136 struct glyph_row *row = it->glyph_row;
13137
13138 /* Initialize the iterator IT for iteration over STRING beginning
13139 with index START. We assume that IT may be modified here (which
13140 means that display_line has to do something when displaying a
13141 mini-buffer prompt, which it does). */
13142 reseat_to_string (it, string, lisp_string, start,
13143 precision, field_width, multibyte);
13144
13145 /* If displaying STRING, set up the face of the iterator
13146 from LISP_STRING, if that's given. */
13147 if (STRINGP (face_string))
13148 {
13149 int endptr;
13150 struct face *face;
13151
13152 it->face_id
13153 = face_at_string_position (it->w, face_string, face_string_pos,
13154 0, it->region_beg_charpos,
13155 it->region_end_charpos,
13156 &endptr, it->base_face_id);
13157 face = FACE_FROM_ID (it->f, it->face_id);
13158 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 13159 }
a2889657 13160
5f5c8ee5
GM
13161 /* Set max_x to the maximum allowed X position. Don't let it go
13162 beyond the right edge of the window. */
13163 if (max_x <= 0)
13164 max_x = it->last_visible_x;
13165 else
13166 max_x = min (max_x, it->last_visible_x);
efc63ef0 13167
5f5c8ee5
GM
13168 /* Skip over display elements that are not visible. because IT->w is
13169 hscrolled. */
13170 if (it->current_x < it->first_visible_x)
13171 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13172 MOVE_TO_POS | MOVE_TO_X);
a2889657 13173
5f5c8ee5
GM
13174 row->ascent = it->max_ascent;
13175 row->height = it->max_ascent + it->max_descent;
312246d1
GM
13176 row->phys_ascent = it->max_phys_ascent;
13177 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 13178
5f5c8ee5
GM
13179 /* This condition is for the case that we are called with current_x
13180 past last_visible_x. */
13181 while (it->current_x < max_x)
a2889657 13182 {
5f5c8ee5 13183 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 13184
5f5c8ee5
GM
13185 /* Get the next display element. */
13186 if (!get_next_display_element (it))
90adcf20 13187 break;
1c9241f5 13188
5f5c8ee5
GM
13189 /* Produce glyphs. */
13190 x_before = it->current_x;
13191 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13192 PRODUCE_GLYPHS (it);
90adcf20 13193
5f5c8ee5
GM
13194 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13195 i = 0;
13196 x = x_before;
13197 while (i < nglyphs)
a2889657 13198 {
5f5c8ee5
GM
13199 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13200
13201 if (!it->truncate_lines_p
13202 && x + glyph->pixel_width > max_x)
13203 {
13204 /* End of continued line or max_x reached. */
13205 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13206 it->current_x = x;
13207 break;
13208 }
13209 else if (x + glyph->pixel_width > it->first_visible_x)
13210 {
13211 /* Glyph is at least partially visible. */
13212 ++it->hpos;
13213 if (x < it->first_visible_x)
13214 it->glyph_row->x = x - it->first_visible_x;
13215 }
13216 else
a2889657 13217 {
5f5c8ee5
GM
13218 /* Glyph is off the left margin of the display area.
13219 Should not happen. */
13220 abort ();
a2889657 13221 }
5f5c8ee5
GM
13222
13223 row->ascent = max (row->ascent, it->max_ascent);
13224 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13225 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13226 row->phys_height = max (row->phys_height,
13227 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13228 x += glyph->pixel_width;
13229 ++i;
a2889657 13230 }
5f5c8ee5
GM
13231
13232 /* Stop if max_x reached. */
13233 if (i < nglyphs)
13234 break;
13235
13236 /* Stop at line ends. */
13237 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 13238 {
5f5c8ee5
GM
13239 it->continuation_lines_width = 0;
13240 break;
a2889657 13241 }
1c9241f5 13242
5f5c8ee5 13243 set_iterator_to_next (it);
a688bb24 13244
5f5c8ee5
GM
13245 /* Stop if truncating at the right edge. */
13246 if (it->truncate_lines_p
13247 && it->current_x >= it->last_visible_x)
13248 {
13249 /* Add truncation mark, but don't do it if the line is
13250 truncated at a padding space. */
13251 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 13252 {
5f5c8ee5
GM
13253 if (!FRAME_WINDOW_P (it->f))
13254 produce_special_glyphs (it, IT_TRUNCATION);
13255 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 13256 }
5f5c8ee5 13257 break;
1c9241f5 13258 }
a2889657
JB
13259 }
13260
5f5c8ee5
GM
13261 /* Maybe insert a truncation at the left. */
13262 if (it->first_visible_x
13263 && IT_CHARPOS (*it) > 0)
a2889657 13264 {
5f5c8ee5
GM
13265 if (!FRAME_WINDOW_P (it->f))
13266 insert_left_trunc_glyphs (it);
13267 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
13268 }
13269
5f5c8ee5
GM
13270 it->face_id = saved_face_id;
13271
13272 /* Value is number of columns displayed. */
13273 return it->hpos - hpos_at_start;
13274}
a2889657 13275
a2889657 13276
a2889657 13277\f
5f5c8ee5
GM
13278/* This is like a combination of memq and assq. Return 1 if PROPVAL
13279 appears as an element of LIST or as the car of an element of LIST.
13280 If PROPVAL is a list, compare each element against LIST in that
13281 way, and return 1 if any element of PROPVAL is found in LIST.
13282 Otherwise return 0. This function cannot quit. */
642eefc6
RS
13283
13284int
13285invisible_p (propval, list)
13286 register Lisp_Object propval;
13287 Lisp_Object list;
13288{
af460d46 13289 register Lisp_Object tail, proptail;
715e84c9 13290
9472f927 13291 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
13292 {
13293 register Lisp_Object tem;
9472f927 13294 tem = XCAR (tail);
642eefc6
RS
13295 if (EQ (propval, tem))
13296 return 1;
9472f927 13297 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
13298 return 1;
13299 }
715e84c9 13300
af460d46 13301 if (CONSP (propval))
715e84c9
GM
13302 {
13303 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13304 {
13305 Lisp_Object propelt;
13306 propelt = XCAR (proptail);
13307 for (tail = list; CONSP (tail); tail = XCDR (tail))
13308 {
13309 register Lisp_Object tem;
13310 tem = XCAR (tail);
13311 if (EQ (propelt, tem))
13312 return 1;
13313 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13314 return 1;
13315 }
13316 }
13317 }
13318
642eefc6
RS
13319 return 0;
13320}
13321
5f5c8ee5
GM
13322
13323/* Return 1 if PROPVAL appears as the car of an element of LIST and
13324 the cdr of that element is non-nil. If PROPVAL is a list, check
13325 each element of PROPVAL in that way, and the first time some
13326 element is found, return 1 if the cdr of that element is non-nil.
13327 Otherwise return 0. This function cannot quit. */
642eefc6
RS
13328
13329int
13330invisible_ellipsis_p (propval, list)
13331 register Lisp_Object propval;
13332 Lisp_Object list;
13333{
af460d46 13334 register Lisp_Object tail, proptail;
9472f927
GM
13335
13336 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
13337 {
13338 register Lisp_Object tem;
9472f927
GM
13339 tem = XCAR (tail);
13340 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13341 return ! NILP (XCDR (tem));
642eefc6 13342 }
9472f927 13343
af460d46 13344 if (CONSP (propval))
9472f927 13345 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
13346 {
13347 Lisp_Object propelt;
9472f927
GM
13348 propelt = XCAR (proptail);
13349 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
13350 {
13351 register Lisp_Object tem;
9472f927
GM
13352 tem = XCAR (tail);
13353 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13354 return ! NILP (XCDR (tem));
af460d46
RS
13355 }
13356 }
9472f927 13357
642eefc6
RS
13358 return 0;
13359}
5f5c8ee5
GM
13360
13361
642eefc6 13362\f
5f5c8ee5
GM
13363/***********************************************************************
13364 Initialization
13365 ***********************************************************************/
13366
a2889657
JB
13367void
13368syms_of_xdisp ()
13369{
c6e89d6c
GM
13370 Vwith_echo_area_save_vector = Qnil;
13371 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 13372
c6e89d6c
GM
13373 Vmessage_stack = Qnil;
13374 staticpro (&Vmessage_stack);
13375
735c094c 13376 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 13377 staticpro (&Qinhibit_redisplay);
735c094c 13378
5f5c8ee5
GM
13379#if GLYPH_DEBUG
13380 defsubr (&Sdump_glyph_matrix);
13381 defsubr (&Sdump_glyph_row);
e037b9ec 13382 defsubr (&Sdump_tool_bar_row);
5f5c8ee5 13383 defsubr (&Strace_redisplay_toggle);
bf9249e3 13384 defsubr (&Strace_to_stderr);
5f5c8ee5
GM
13385#endif
13386
cf074754
RS
13387 staticpro (&Qmenu_bar_update_hook);
13388 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13389
d46fb96a 13390 staticpro (&Qoverriding_terminal_local_map);
7079aefa 13391 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 13392
399164b4
KH
13393 staticpro (&Qoverriding_local_map);
13394 Qoverriding_local_map = intern ("overriding-local-map");
13395
75c43375
RS
13396 staticpro (&Qwindow_scroll_functions);
13397 Qwindow_scroll_functions = intern ("window-scroll-functions");
13398
e0bfbde6
RS
13399 staticpro (&Qredisplay_end_trigger_functions);
13400 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 13401
2e54982e
RS
13402 staticpro (&Qinhibit_point_motion_hooks);
13403 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13404
9499d71b
GM
13405 QCdata = intern (":data");
13406 staticpro (&QCdata);
5f5c8ee5 13407 Qdisplay = intern ("display");
f3751a65 13408 staticpro (&Qdisplay);
5f5c8ee5
GM
13409 Qspace_width = intern ("space-width");
13410 staticpro (&Qspace_width);
5f5c8ee5
GM
13411 Qraise = intern ("raise");
13412 staticpro (&Qraise);
13413 Qspace = intern ("space");
13414 staticpro (&Qspace);
f3751a65
GM
13415 Qmargin = intern ("margin");
13416 staticpro (&Qmargin);
5f5c8ee5 13417 Qleft_margin = intern ("left-margin");
f3751a65 13418 staticpro (&Qleft_margin);
5f5c8ee5 13419 Qright_margin = intern ("right-margin");
f3751a65 13420 staticpro (&Qright_margin);
5f5c8ee5
GM
13421 Qalign_to = intern ("align-to");
13422 staticpro (&Qalign_to);
13423 QCalign_to = intern (":align-to");
13424 staticpro (&QCalign_to);
5f5c8ee5
GM
13425 Qrelative_width = intern ("relative-width");
13426 staticpro (&Qrelative_width);
13427 QCrelative_width = intern (":relative-width");
13428 staticpro (&QCrelative_width);
13429 QCrelative_height = intern (":relative-height");
13430 staticpro (&QCrelative_height);
13431 QCeval = intern (":eval");
13432 staticpro (&QCeval);
d3acf96b 13433 Qwhen = intern ("when");
f3751a65 13434 staticpro (&Qwhen);
886bd6f2
GM
13435 QCfile = intern (":file");
13436 staticpro (&QCfile);
5f5c8ee5
GM
13437 Qfontified = intern ("fontified");
13438 staticpro (&Qfontified);
13439 Qfontification_functions = intern ("fontification-functions");
13440 staticpro (&Qfontification_functions);
5f5c8ee5
GM
13441 Qtrailing_whitespace = intern ("trailing-whitespace");
13442 staticpro (&Qtrailing_whitespace);
13443 Qimage = intern ("image");
13444 staticpro (&Qimage);
ad4f174e
GM
13445 Qmessage_truncate_lines = intern ("message-truncate-lines");
13446 staticpro (&Qmessage_truncate_lines);
5f5c8ee5 13447
a2889657
JB
13448 last_arrow_position = Qnil;
13449 last_arrow_string = Qnil;
f3751a65
GM
13450 staticpro (&last_arrow_position);
13451 staticpro (&last_arrow_string);
c6e89d6c
GM
13452
13453 echo_buffer[0] = echo_buffer[1] = Qnil;
13454 staticpro (&echo_buffer[0]);
13455 staticpro (&echo_buffer[1]);
13456
13457 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13458 staticpro (&echo_area_buffer[0]);
13459 staticpro (&echo_area_buffer[1]);
a2889657 13460
6a94510a
GM
13461 Vmessages_buffer_name = build_string ("*Messages*");
13462 staticpro (&Vmessages_buffer_name);
13463
8f897821
GM
13464 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13465 "Non-nil means highlight trailing whitespace.\n\
13466The face used for trailing whitespace is `trailing-whitespace'.");
13467 Vshow_trailing_whitespace = Qnil;
13468
735c094c
KH
13469 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13470 "Non-nil means don't actually do any redisplay.\n\
13471This is used for internal purposes.");
13472 Vinhibit_redisplay = Qnil;
13473
a2889657 13474 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 13475 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
13476 Vglobal_mode_string = Qnil;
13477
13478 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13479 "Marker for where to display an arrow on top of the buffer text.\n\
13480This must be the beginning of a line in order to work.\n\
13481See also `overlay-arrow-string'.");
13482 Voverlay_arrow_position = Qnil;
13483
13484 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13485 "String to display as an arrow. See also `overlay-arrow-position'.");
13486 Voverlay_arrow_string = Qnil;
13487
13488 DEFVAR_INT ("scroll-step", &scroll_step,
13489 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
13490If that fails to bring point back on frame, point is centered instead.\n\
13491If this is zero, point is always centered after it moves off frame.");
a2889657 13492
0789adb2 13493 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
090f7baa
GM
13494 "*Scroll up to this many lines, to bring point back on screen.\n\
13495A value of zero means to scroll the text to center point vertically\n\
13496in the window.");
0789adb2
RS
13497 scroll_conservatively = 0;
13498
9afd2168
RS
13499 DEFVAR_INT ("scroll-margin", &scroll_margin,
13500 "*Number of lines of margin at the top and bottom of a window.\n\
13501Recenter the window whenever point gets within this many lines\n\
13502of the top or bottom of the window.");
13503 scroll_margin = 0;
13504
5f5c8ee5 13505#if GLYPH_DEBUG
a2889657 13506 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 13507#endif
a2889657
JB
13508
13509 DEFVAR_BOOL ("truncate-partial-width-windows",
13510 &truncate_partial_width_windows,
44fa5b1e 13511 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
13512 truncate_partial_width_windows = 1;
13513
13514 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13515 "*Non-nil means use inverse video for the mode line.");
13516 mode_line_inverse_video = 1;
aa6d10fa 13517
090703f4 13518 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
5f5c8ee5 13519 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 13520If the buffer is bigger than this, the line number does not appear\n\
090703f4
GM
13521in the mode line. A value of nil means no limit.");
13522 Vline_number_display_limit = Qnil;
fba9ce76 13523
090703f4
GM
13524 DEFVAR_INT ("line-number-display-limit-width",
13525 &line_number_display_limit_width,
5d121aec
KH
13526 "*Maximum line width (in characters) for line number display.\n\
13527If the average length of the lines near point is bigger than this, then the\n\
13528line number may be omitted from the mode line.");
13529 line_number_display_limit_width = 200;
13530
fba9ce76
RS
13531 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13532 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 13533 highlight_nonselected_windows = 0;
d39b6696
KH
13534
13535 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
13536 "Non-nil if more than one frame is visible on this display.\n\
13537Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
13538This variable is not guaranteed to be accurate except while processing\n\
13539`frame-title-format' and `icon-title-format'.");
d39b6696
KH
13540
13541 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 13542 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
13543\(Assuming the window manager supports this feature.)\n\
13544This variable has the same structure as `mode-line-format' (which see),\n\
13545and is used only on frames for which no explicit name has been set\n\
13546\(see `modify-frame-parameters').");
13547 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 13548 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
13549\(Assuming the window manager supports this feature.)\n\
13550This variable has the same structure as `mode-line-format' (which see),\n\
13551and is used only on frames for which no explicit name has been set\n\
13552\(see `modify-frame-parameters').");
13553 Vicon_title_format
13554 = Vframe_title_format
13555 = Fcons (intern ("multiple-frames"),
13556 Fcons (build_string ("%b"),
13557 Fcons (Fcons (build_string (""),
13558 Fcons (intern ("invocation-name"),
13559 Fcons (build_string ("@"),
13560 Fcons (intern ("system-name"),
13561 Qnil)))),
13562 Qnil)));
5992c4f7
KH
13563
13564 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13565 "Maximum number of lines to keep in the message log buffer.\n\
13566If nil, disable message logging. If t, log messages but don't truncate\n\
13567the buffer when it becomes large.");
13568 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
13569
13570 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13571 "Functions called before redisplay, if window sizes have changed.\n\
13572The value should be a list of functions that take one argument.\n\
13573Just before redisplay, for each frame, if any of its windows have changed\n\
13574size since the last redisplay, or have been split or deleted,\n\
13575all the functions in the list are called, with the frame as argument.");
13576 Vwindow_size_change_functions = Qnil;
75c43375
RS
13577
13578 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 13579 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 13580Each function is called with two arguments, the window\n\
8d9583b0
RS
13581and its new display-start position. Note that the value of `window-end'\n\
13582is not valid when these functions are called.");
75c43375 13583 Vwindow_scroll_functions = Qnil;
5f5c8ee5 13584
e037b9ec
GM
13585 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13586 "*Non-nil means automatically resize tool-bars.\n\
13587This increases a tool-bar's height if not all tool-bar items are visible.\n\
13588It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 13589otherwise.");
e037b9ec 13590 auto_resize_tool_bars_p = 1;
5f5c8ee5 13591
e037b9ec
GM
13592 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13593 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13594 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 13595
e037b9ec
GM
13596 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13597 "*Margin around tool-bar buttons in pixels.");
13598 tool_bar_button_margin = 1;
5f5c8ee5 13599
e037b9ec
GM
13600 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13601 "Relief thickness of tool-bar buttons.");
13602 tool_bar_button_relief = 3;
5f5c8ee5
GM
13603
13604 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13605 "List of functions to call to fontify regions of text.\n\
13606Each function is called with one argument POS. Functions must\n\
13607fontify a region starting at POS in the current buffer, and give\n\
13608fontified regions the property `fontified'.\n\
13609This variable automatically becomes buffer-local when set.");
13610 Vfontification_functions = Qnil;
13611 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
13612
13613 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
13614 &unibyte_display_via_language_environment,
13615 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
13616Specifically this means that unibyte non-ASCII characters\n\
13617are displayed by converting them to the equivalent multibyte characters\n\
13618according to the current language environment. As a result, they are\n\
13619displayed according to the current fontset.");
13620 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
13621
13622 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13623 "*Maximum height for resizing mini-windows.\n\
13624If a float, it specifies a fraction of the mini-window frame's height.\n\
97cafc0f
GM
13625If an integer, it specifies a number of lines.\n\
13626If nil, don't resize.");
c6e89d6c 13627 Vmax_mini_window_height = make_float (0.25);
d6d26ed3
GM
13628
13629 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13630 &cursor_in_non_selected_windows,
13631 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13632Nil means don't display a cursor there.");
13633 cursor_in_non_selected_windows = 1;
d475bcb8
GM
13634
13635 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13636 "*Non-nil means scroll the display automatically to make point visible.");
13637 automatic_hscrolling_p = 1;
e00daaa0
GM
13638
13639 DEFVAR_LISP ("image-types", &Vimage_types,
ad4f174e 13640 "List of supported image types.\n\
e00daaa0
GM
13641Each element of the list is a symbol for a supported image type.");
13642 Vimage_types = Qnil;
ad4f174e
GM
13643
13644 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13645 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13646Bind this around calls to `message' to let it take effect.");
13647 message_truncate_lines = 0;
a2889657
JB
13648}
13649
5f5c8ee5
GM
13650
13651/* Initialize this module when Emacs starts. */
13652
dfcf069d 13653void
a2889657
JB
13654init_xdisp ()
13655{
13656 Lisp_Object root_window;
5f5c8ee5 13657 struct window *mini_w;
a2889657 13658
5f5c8ee5 13659 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
13660
13661 mini_w = XWINDOW (minibuf_window);
11e82b76 13662 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 13663
a2889657
JB
13664 if (!noninteractive)
13665 {
5f5c8ee5
GM
13666 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13667 int i;
13668
13669 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 13670 set_window_height (root_window,
5f5c8ee5 13671 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 13672 0);
c2213350 13673 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
13674 set_window_height (minibuf_window, 1, 0);
13675
c2213350
KH
13676 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13677 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
13678
13679 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13680 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13681 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13682
13683 /* The default ellipsis glyphs `...'. */
13684 for (i = 0; i < 3; ++i)
13685 XSETFASTINT (default_invis_vector[i], '.');
a2889657 13686 }
5f5c8ee5
GM
13687
13688#ifdef HAVE_WINDOW_SYSTEM
13689 {
13690 /* Allocate the buffer for frame titles. */
13691 int size = 100;
13692 frame_title_buf = (char *) xmalloc (size);
13693 frame_title_buf_end = frame_title_buf + size;
13694 frame_title_ptr = NULL;
13695 }
13696#endif /* HAVE_WINDOW_SYSTEM */
a2889657 13697}
5f5c8ee5
GM
13698
13699