(message_with_string): Use Fformat instead of doprnt and
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5d335845 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
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,
48400103 36 let's say almost---see the description of direct update
4b41cebb 37 operations, below.)
5f5c8ee5
GM
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
82a7ab23 55 +--------------+ redisplay +----------------+
5f5c8ee5
GM
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
48400103 91 You will find a lot of redisplay optimizations when you start
5f5c8ee5
GM
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
48400103 126 Iteration over things to be displayed is then simple. It is
1178ddde
GM
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
5f5c8ee5
GM
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
a2889657 168
18160b98 169#include <config.h>
a2889657 170#include <stdio.h>
7ee72033 171
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
1a578e9b
AC
197#ifdef macintosh
198#include "macterm.h"
199#endif
a2889657 200
5f5c8ee5
GM
201#define INFINITY 10000000
202
1a578e9b 203#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
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;
0fcf414f 223Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
5f5c8ee5 224Lisp_Object Qfontified;
6422c1d7 225Lisp_Object Qgrow_only;
869fb12c 226Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 227Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5
GM
228
229/* Functions called to fontify regions of text. */
230
231Lisp_Object Vfontification_functions;
232Lisp_Object Qfontification_functions;
233
e037b9ec 234/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
235 over them. */
236
e037b9ec 237int auto_raise_tool_bar_buttons_p;
5f5c8ee5 238
e037b9ec 239/* Margin around tool bar buttons in pixels. */
5f5c8ee5 240
35a41507 241Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 242
e037b9ec 243/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 244
31ade731 245EMACS_INT tool_bar_button_relief;
5f5c8ee5 246
e037b9ec 247/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
248 items are visible, and no blank lines remain. */
249
e037b9ec 250int auto_resize_tool_bars_p;
399164b4 251
735c094c
KH
252/* Non-nil means don't actually do any redisplay. */
253
254Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
255
30a3f61c
GM
256/* Non-zero means Lisp evaluation during redisplay is inhibited. */
257
869fb12c 258int inhibit_eval_during_redisplay;
30a3f61c 259
5f5c8ee5
GM
260/* Names of text properties relevant for redisplay. */
261
a7e27ef7 262Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
4b41cebb 263extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
264
265/* Symbols used in text property values. */
266
267Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 268Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 269Lisp_Object Qmargin;
a7e27ef7 270extern Lisp_Object Qheight;
5f5c8ee5 271
8f897821 272/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 273
8f897821 274Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
275
276/* Name of the face used to highlight trailing whitespace. */
277
278Lisp_Object Qtrailing_whitespace;
279
280/* The symbol `image' which is the car of the lists used to represent
281 images in Lisp. */
282
283Lisp_Object Qimage;
284
285/* Non-zero means print newline to stdout before next mini-buffer
286 message. */
a2889657
JB
287
288int noninteractive_need_newline;
289
5f5c8ee5 290/* Non-zero means print newline to message log before next message. */
f88eb0b6 291
3c6595e0 292static int message_log_need_newline;
f88eb0b6 293
b14bc55e
RS
294/* Three markers that message_dolog uses.
295 It could allocate them itself, but that causes trouble
296 in handling memory-full errors. */
297static Lisp_Object message_dolog_marker1;
298static Lisp_Object message_dolog_marker2;
299static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
300\f
301/* The buffer position of the first character appearing entirely or
302 partially on the line of the selected window which contains the
303 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
304 redisplay optimization in redisplay_internal. */
a2889657 305
5f5c8ee5 306static struct text_pos this_line_start_pos;
a2889657 307
5f5c8ee5
GM
308/* Number of characters past the end of the line above, including the
309 terminating newline. */
310
311static struct text_pos this_line_end_pos;
312
313/* The vertical positions and the height of this line. */
a2889657 314
a2889657 315static int this_line_vpos;
5f5c8ee5
GM
316static int this_line_y;
317static int this_line_pixel_height;
318
319/* X position at which this display line starts. Usually zero;
320 negative if first character is partially visible. */
321
322static int this_line_start_x;
a2889657 323
5f5c8ee5 324/* Buffer that this_line_.* variables are referring to. */
a2889657 325
a2889657
JB
326static struct buffer *this_line_buffer;
327
5f5c8ee5
GM
328/* Nonzero means truncate lines in all windows less wide than the
329 frame. */
a2889657 330
a2889657
JB
331int truncate_partial_width_windows;
332
7bbe686f 333/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 334
7bbe686f 335int unibyte_display_via_language_environment;
5f5c8ee5
GM
336
337/* Nonzero means we have more than one non-mini-buffer-only frame.
338 Not guaranteed to be accurate except while parsing
339 frame-title-format. */
7bbe686f 340
d39b6696
KH
341int multiple_frames;
342
a2889657
JB
343Lisp_Object Vglobal_mode_string;
344
345/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 346
a2889657
JB
347Lisp_Object Voverlay_arrow_position;
348
5f5c8ee5
GM
349/* String to display for the arrow. Only used on terminal frames. */
350
a2889657
JB
351Lisp_Object Voverlay_arrow_string;
352
5f5c8ee5
GM
353/* Values of those variables at last redisplay. However, if
354 Voverlay_arrow_position is a marker, last_arrow_position is its
355 numerical position. */
356
d45de95b
RS
357static Lisp_Object last_arrow_position, last_arrow_string;
358
5f5c8ee5
GM
359/* Like mode-line-format, but for the title bar on a visible frame. */
360
d39b6696
KH
361Lisp_Object Vframe_title_format;
362
5f5c8ee5
GM
363/* Like mode-line-format, but for the title bar on an iconified frame. */
364
d39b6696
KH
365Lisp_Object Vicon_title_format;
366
08b610e4
RS
367/* List of functions to call when a window's size changes. These
368 functions get one arg, a frame on which one or more windows' sizes
369 have changed. */
5f5c8ee5 370
08b610e4
RS
371static Lisp_Object Vwindow_size_change_functions;
372
0bca8940 373Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 374
a2889657 375/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 376
5f5c8ee5 377static int overlay_arrow_seen;
ca26e1c8 378
fba9ce76 379/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 380
5f5c8ee5
GM
381int highlight_nonselected_windows;
382
383/* If cursor motion alone moves point off frame, try scrolling this
384 many lines up or down if that will bring it back. */
385
31ade731 386static EMACS_INT scroll_step;
a2889657 387
4b41cebb 388/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
389 screen, when appropriate. */
390
31ade731 391static EMACS_INT scroll_conservatively;
0789adb2 392
5f5c8ee5
GM
393/* Recenter the window whenever point gets within this many lines of
394 the top or bottom of the window. This value is translated into a
395 pixel value by multiplying it with CANON_Y_UNIT, which means that
396 there is really a fixed pixel height scroll margin. */
397
31ade731 398EMACS_INT scroll_margin;
9afd2168 399
5f5c8ee5
GM
400/* Number of windows showing the buffer of the selected window (or
401 another buffer with the same base buffer). keyboard.c refers to
402 this. */
a2889657 403
a2889657
JB
404int buffer_shared;
405
5f5c8ee5 406/* Vector containing glyphs for an ellipsis `...'. */
a2889657 407
5f5c8ee5 408static Lisp_Object default_invis_vector[3];
a2889657 409
1862a24e
MB
410/* Zero means display the mode-line/header-line/menu-bar in the default face
411 (this slightly odd definition is for compatibility with previous versions
412 of emacs), non-zero means display them using their respective faces.
413
414 This variable is deprecated. */
a2889657 415
a2889657
JB
416int mode_line_inverse_video;
417
5f5c8ee5
GM
418/* Prompt to display in front of the mini-buffer contents. */
419
8c5b6a0a 420Lisp_Object minibuf_prompt;
a2889657 421
5f5c8ee5
GM
422/* Width of current mini-buffer prompt. Only set after display_line
423 of the line that contains the prompt. */
424
a2889657 425int minibuf_prompt_width;
5f5c8ee5
GM
426int minibuf_prompt_pixel_width;
427
5f5c8ee5
GM
428/* This is the window where the echo area message was displayed. It
429 is always a mini-buffer window, but it may not be the same window
430 currently active as a mini-buffer. */
431
73af359d
RS
432Lisp_Object echo_area_window;
433
c6e89d6c
GM
434/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
435 pushes the current message and the value of
436 message_enable_multibyte on the stack, the function restore_message
437 pops the stack and displays MESSAGE again. */
438
439Lisp_Object Vmessage_stack;
440
a3788d53
RS
441/* Nonzero means multibyte characters were enabled when the echo area
442 message was specified. */
5f5c8ee5 443
a3788d53
RS
444int message_enable_multibyte;
445
4b41cebb 446/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 447
a2889657
JB
448int update_mode_lines;
449
5f5c8ee5 450/* Nonzero if window sizes or contents have changed since last
4b41cebb 451 redisplay that finished. */
5f5c8ee5 452
a2889657
JB
453int windows_or_buffers_changed;
454
5f5c8ee5
GM
455/* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
457
aa6d10fa
RS
458int line_number_displayed;
459
460/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 461
090703f4 462Lisp_Object Vline_number_display_limit;
5992c4f7 463
4b41cebb 464/* Line width to consider when repositioning for line number display. */
5d121aec 465
31ade731 466static EMACS_INT line_number_display_limit_width;
5d121aec 467
5f5c8ee5
GM
468/* Number of lines to keep in the message log buffer. t means
469 infinite. nil means don't log at all. */
470
5992c4f7 471Lisp_Object Vmessage_log_max;
d45de95b 472
6a94510a
GM
473/* The name of the *Messages* buffer, a string. */
474
475static Lisp_Object Vmessages_buffer_name;
476
c6e89d6c
GM
477/* Current, index 0, and last displayed echo area message. Either
478 buffers from echo_buffers, or nil to indicate no message. */
479
480Lisp_Object echo_area_buffer[2];
481
482/* The buffers referenced from echo_area_buffer. */
483
484static Lisp_Object echo_buffer[2];
485
486/* A vector saved used in with_area_buffer to reduce consing. */
487
488static Lisp_Object Vwith_echo_area_save_vector;
489
490/* Non-zero means display_echo_area should display the last echo area
491 message again. Set by redisplay_preserve_echo_area. */
492
493static int display_last_displayed_message_p;
494
495/* Nonzero if echo area is being used by print; zero if being used by
496 message. */
497
498int message_buf_print;
499
e1477f43
GM
500/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
501
502Lisp_Object Qinhibit_menubar_update;
503int inhibit_menubar_update;
504
9142dd5b
GM
505/* Maximum height for resizing mini-windows. Either a float
506 specifying a fraction of the available height, or an integer
507 specifying a number of lines. */
c6e89d6c 508
ad4f174e
GM
509Lisp_Object Vmax_mini_window_height;
510
511/* Non-zero means messages should be displayed with truncated
512 lines instead of being continued. */
513
514int message_truncate_lines;
515Lisp_Object Qmessage_truncate_lines;
c6e89d6c 516
6e019995
GM
517/* Set to 1 in clear_message to make redisplay_internal aware
518 of an emptied echo area. */
519
520static int message_cleared_p;
521
d6d26ed3
GM
522/* Non-zero means we want a hollow cursor in windows that are not
523 selected. Zero means there's no cursor in such windows. */
524
525int cursor_in_non_selected_windows;
af79bccb 526Lisp_Object Qcursor_in_non_selected_windows;
d6d26ed3 527
5f5c8ee5
GM
528/* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
12adba34 530
5f5c8ee5
GM
531#define MAX_SCRATCH_GLYPHS 100
532struct glyph_row scratch_glyph_row;
533static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 534
5f5c8ee5
GM
535/* Ascent and height of the last line processed by move_it_to. */
536
537static int last_max_ascent, last_height;
538
21fdfb65
GM
539/* Non-zero if there's a help-echo in the echo area. */
540
541int help_echo_showing_p;
542
04612a64
GM
543/* If >= 0, computed, exact values of mode-line and header-line height
544 to use in the macros CURRENT_MODE_LINE_HEIGHT and
545 CURRENT_HEADER_LINE_HEIGHT. */
546
547int current_mode_line_height, current_header_line_height;
548
5f5c8ee5
GM
549/* The maximum distance to look ahead for text properties. Values
550 that are too small let us call compute_char_face and similar
551 functions too often which is expensive. Values that are too large
552 let us call compute_char_face and alike too often because we
553 might not be interested in text properties that far away. */
554
555#define TEXT_PROP_DISTANCE_LIMIT 100
556
47589c8c
GM
557#if GLYPH_DEBUG
558
76cb5e06
GM
559/* Variables to turn off display optimizations from Lisp. */
560
561int inhibit_try_window_id, inhibit_try_window_reusing;
562int inhibit_try_cursor_movement;
563
5f5c8ee5
GM
564/* Non-zero means print traces of redisplay if compiled with
565 GLYPH_DEBUG != 0. */
566
5f5c8ee5 567int trace_redisplay_p;
47589c8c 568
546a4f00 569#endif /* GLYPH_DEBUG */
47589c8c 570
546a4f00
AI
571#ifdef DEBUG_TRACE_MOVE
572/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
573int trace_move;
574
47589c8c
GM
575#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
576#else
91c3f500 577#define TRACE_MOVE(x) (void) 0
5f5c8ee5 578#endif
546a4f00 579
d475bcb8
GM
580/* Non-zero means automatically scroll windows horizontally to make
581 point visible. */
582
583int automatic_hscrolling_p;
584
1df7e8f0
EZ
585/* How close to the margin can point get before the window is scrolled
586 horizontally. */
5d335845 587EMACS_INT hscroll_margin;
1df7e8f0
EZ
588
589/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 590Lisp_Object Vhscroll_step;
1df7e8f0 591
e00daaa0
GM
592/* A list of symbols, one for each supported image type. */
593
594Lisp_Object Vimage_types;
595
6422c1d7 596/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 597 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
598 display. If `grow-only', let mini-windows grow only until they
599 become empty. */
600
601Lisp_Object Vresize_mini_windows;
602
82a7ab23
RS
603/* Buffer being redisplayed -- for redisplay_window_error. */
604
605struct buffer *displayed_buffer;
606
5f5c8ee5
GM
607/* Value returned from text property handlers (see below). */
608
609enum prop_handled
3c6595e0 610{
5f5c8ee5
GM
611 HANDLED_NORMALLY,
612 HANDLED_RECOMPUTE_PROPS,
613 HANDLED_OVERLAY_STRING_CONSUMED,
614 HANDLED_RETURN
615};
3c6595e0 616
5f5c8ee5
GM
617/* A description of text properties that redisplay is interested
618 in. */
3c6595e0 619
5f5c8ee5
GM
620struct props
621{
622 /* The name of the property. */
623 Lisp_Object *name;
90adcf20 624
5f5c8ee5
GM
625 /* A unique index for the property. */
626 enum prop_idx idx;
627
628 /* A handler function called to set up iterator IT from the property
629 at IT's current position. Value is used to steer handle_stop. */
630 enum prop_handled (*handler) P_ ((struct it *it));
631};
632
633static enum prop_handled handle_face_prop P_ ((struct it *));
634static enum prop_handled handle_invisible_prop P_ ((struct it *));
635static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 636static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
637static enum prop_handled handle_overlay_change P_ ((struct it *));
638static enum prop_handled handle_fontified_prop P_ ((struct it *));
639
640/* Properties handled by iterators. */
641
642static struct props it_props[] =
5992c4f7 643{
5f5c8ee5
GM
644 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
645 /* Handle `face' before `display' because some sub-properties of
646 `display' need to know the face. */
647 {&Qface, FACE_PROP_IDX, handle_face_prop},
648 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
649 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 650 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
651 {NULL, 0, NULL}
652};
5992c4f7 653
5f5c8ee5
GM
654/* Value is the position described by X. If X is a marker, value is
655 the marker_position of X. Otherwise, value is X. */
12adba34 656
5f5c8ee5 657#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 658
5f5c8ee5 659/* Enumeration returned by some move_it_.* functions internally. */
12adba34 660
5f5c8ee5
GM
661enum move_it_result
662{
663 /* Not used. Undefined value. */
664 MOVE_UNDEFINED,
bab29e15 665
5f5c8ee5
GM
666 /* Move ended at the requested buffer position or ZV. */
667 MOVE_POS_MATCH_OR_ZV,
bab29e15 668
5f5c8ee5
GM
669 /* Move ended at the requested X pixel position. */
670 MOVE_X_REACHED,
12adba34 671
5f5c8ee5
GM
672 /* Move within a line ended at the end of a line that must be
673 continued. */
674 MOVE_LINE_CONTINUED,
675
676 /* Move within a line ended at the end of a line that would
677 be displayed truncated. */
678 MOVE_LINE_TRUNCATED,
ff6c30e5 679
5f5c8ee5
GM
680 /* Move within a line ended at a line end. */
681 MOVE_NEWLINE_OR_CR
682};
12adba34 683
ff6c30e5 684
5f5c8ee5
GM
685\f
686/* Function prototypes. */
687
5a08cbaf 688static void setup_for_ellipsis P_ ((struct it *));
43c09969 689static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
690static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
691static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 692static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 693static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 694static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
695
696#if 0
2e621225 697static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
698#endif
699
2e621225
GM
700static int next_element_from_ellipsis P_ ((struct it *));
701static void pint2str P_ ((char *, int, int));
702static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
703 struct text_pos));
704static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
705static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
706static void store_frame_title_char P_ ((char));
707static int store_frame_title P_ ((unsigned char *, int, int));
708static void x_consider_frame_title P_ ((Lisp_Object));
709static void handle_stop P_ ((struct it *));
710static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 711static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 712static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
713static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
714static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 715static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
716 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
717 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 718static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
719static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
720static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 722static int display_echo_area P_ ((struct window *));
23dd2d97
KR
723static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
724static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 725static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 726static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
727static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
728 struct text_pos));
729static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 730static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
731static void insert_left_trunc_glyphs P_ ((struct it *));
732static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
733static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 734static int append_space P_ ((struct it *, int));
cb617e7c 735static int make_cursor_line_fully_visible P_ ((struct window *));
31ade731 736static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
47589c8c 737static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
738static int trailing_whitespace_p P_ ((int));
739static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
740static void push_it P_ ((struct it *));
741static void pop_it P_ ((struct it *));
742static void sync_frame_with_window_matrix_rows P_ ((struct window *));
743static void redisplay_internal P_ ((int));
c6e89d6c 744static int echo_area_display P_ ((int));
5f5c8ee5
GM
745static void redisplay_windows P_ ((Lisp_Object));
746static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
747static Lisp_Object redisplay_window_error ();
748static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
749static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
750static void update_menu_bar P_ ((struct frame *, int));
751static int try_window_reusing_current_matrix P_ ((struct window *));
752static int try_window_id P_ ((struct window *));
753static int display_line P_ ((struct it *));
715e84c9 754static int display_mode_lines P_ ((struct window *));
04612a64 755static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
0fcf414f 756static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
72f62cb5 757static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
758static void display_menu_bar P_ ((struct window *));
759static int display_count_lines P_ ((int, int, int, int, int *));
760static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
761 int, int, struct it *, int, int, int, int));
762static void compute_line_metrics P_ ((struct it *));
763static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 764static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 765static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
766static void reseat P_ ((struct it *, struct text_pos, int));
767static void reseat_1 P_ ((struct it *, struct text_pos, int));
768static void back_to_previous_visible_line_start P_ ((struct it *));
769static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 770static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
771static int next_element_from_display_vector P_ ((struct it *));
772static int next_element_from_string P_ ((struct it *));
773static int next_element_from_c_string P_ ((struct it *));
774static int next_element_from_buffer P_ ((struct it *));
260a86a0 775static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
776static int next_element_from_image P_ ((struct it *));
777static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 778static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
779static int init_from_display_pos P_ ((struct it *, struct window *,
780 struct display_pos *));
5f5c8ee5
GM
781static void reseat_to_string P_ ((struct it *, unsigned char *,
782 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
783static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
784 int, int, int));
785void move_it_vertically_backward P_ ((struct it *, int));
786static void init_to_row_start P_ ((struct it *, struct window *,
787 struct glyph_row *));
47d57b22
GM
788static int init_to_row_end P_ ((struct it *, struct window *,
789 struct glyph_row *));
5f5c8ee5 790static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 791static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
792static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
793 Lisp_Object, int));
794static struct text_pos string_pos P_ ((int, Lisp_Object));
795static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
796static int number_of_chars P_ ((unsigned char *, int));
797static void compute_stop_pos P_ ((struct it *));
798static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
799 Lisp_Object));
800static int face_before_or_after_it_pos P_ ((struct it *, int));
801static int next_overlay_change P_ ((int));
802static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
803 Lisp_Object, struct text_pos *,
804 int));
06a12811 805static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
806static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
807 struct window *));
5f5c8ee5
GM
808
809#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
810#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 811
5f5c8ee5 812#ifdef HAVE_WINDOW_SYSTEM
12adba34 813
e037b9ec
GM
814static void update_tool_bar P_ ((struct frame *, int));
815static void build_desired_tool_bar_string P_ ((struct frame *f));
816static int redisplay_tool_bar P_ ((struct frame *));
817static void display_tool_bar_line P_ ((struct it *));
12adba34 818
5f5c8ee5 819#endif /* HAVE_WINDOW_SYSTEM */
12adba34 820
5f5c8ee5
GM
821\f
822/***********************************************************************
823 Window display dimensions
824 ***********************************************************************/
12adba34 825
5f5c8ee5
GM
826/* Return the window-relative maximum y + 1 for glyph rows displaying
827 text in window W. This is the height of W minus the height of a
828 mode line, if any. */
829
830INLINE int
831window_text_bottom_y (w)
832 struct window *w;
833{
834 struct frame *f = XFRAME (w->frame);
835 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 836
5f5c8ee5
GM
837 if (WINDOW_WANTS_MODELINE_P (w))
838 height -= CURRENT_MODE_LINE_HEIGHT (w);
839 return height;
f88eb0b6
KH
840}
841
f82aff7c 842
5f5c8ee5 843/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 844 means return the total width of W, not including fringes to
5f5c8ee5 845 the left and right of the window. */
ff6c30e5 846
5f5c8ee5
GM
847INLINE int
848window_box_width (w, area)
849 struct window *w;
850 int area;
851{
852 struct frame *f = XFRAME (w->frame);
853 int width = XFASTINT (w->width);
854
855 if (!w->pseudo_window_p)
ff6c30e5 856 {
b46952ae 857 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
5f5c8ee5
GM
858
859 if (area == TEXT_AREA)
860 {
861 if (INTEGERP (w->left_margin_width))
862 width -= XFASTINT (w->left_margin_width);
863 if (INTEGERP (w->right_margin_width))
864 width -= XFASTINT (w->right_margin_width);
865 }
866 else if (area == LEFT_MARGIN_AREA)
867 width = (INTEGERP (w->left_margin_width)
868 ? XFASTINT (w->left_margin_width) : 0);
869 else if (area == RIGHT_MARGIN_AREA)
870 width = (INTEGERP (w->right_margin_width)
871 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 872 }
5f5c8ee5
GM
873
874 return width * CANON_X_UNIT (f);
ff6c30e5 875}
1adc55de 876
1adc55de 877
5f5c8ee5 878/* Return the pixel height of the display area of window W, not
4b41cebb 879 including mode lines of W, if any. */
f88eb0b6 880
5f5c8ee5
GM
881INLINE int
882window_box_height (w)
883 struct window *w;
f88eb0b6 884{
5f5c8ee5
GM
885 struct frame *f = XFRAME (w->frame);
886 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
887
888 xassert (height >= 0);
5f5c8ee5 889
d9c9e99c
MB
890 /* Note: the code below that determines the mode-line/header-line
891 height is essentially the same as that contained in the macro
892 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
893 the appropriate glyph row has its `mode_line_p' flag set,
894 and if it doesn't, uses estimate_mode_line_height instead. */
895
5f5c8ee5 896 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
897 {
898 struct glyph_row *ml_row
899 = (w->current_matrix && w->current_matrix->rows
900 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
901 : 0);
902 if (ml_row && ml_row->mode_line_p)
903 height -= ml_row->height;
904 else
96d2320f 905 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 906 }
5f5c8ee5 907
045dee35 908 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
909 {
910 struct glyph_row *hl_row
911 = (w->current_matrix && w->current_matrix->rows
912 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
913 : 0);
914 if (hl_row && hl_row->mode_line_p)
915 height -= hl_row->height;
916 else
917 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
918 }
5f5c8ee5 919
7c75be36
PJ
920 /* With a very small font and a mode-line that's taller than
921 default, we might end up with a negative height. */
922 return max (0, height);
5992c4f7
KH
923}
924
925
5f5c8ee5
GM
926/* Return the frame-relative coordinate of the left edge of display
927 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 928 whole window, to the right of the left fringe of W. */
5992c4f7 929
5f5c8ee5
GM
930INLINE int
931window_box_left (w, area)
932 struct window *w;
933 int area;
90adcf20 934{
5f5c8ee5
GM
935 struct frame *f = XFRAME (w->frame);
936 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 937
5f5c8ee5 938 if (!w->pseudo_window_p)
90adcf20 939 {
5f5c8ee5 940 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
b46952ae 941 + FRAME_LEFT_FRINGE_WIDTH (f));
5f5c8ee5
GM
942
943 if (area == TEXT_AREA)
944 x += window_box_width (w, LEFT_MARGIN_AREA);
945 else if (area == RIGHT_MARGIN_AREA)
946 x += (window_box_width (w, LEFT_MARGIN_AREA)
947 + window_box_width (w, TEXT_AREA));
90adcf20 948 }
73af359d 949
5f5c8ee5
GM
950 return x;
951}
90adcf20 952
b6436d4e 953
5f5c8ee5
GM
954/* Return the frame-relative coordinate of the right edge of display
955 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 956 whole window, to the left of the right fringe of W. */
ded34426 957
5f5c8ee5
GM
958INLINE int
959window_box_right (w, area)
960 struct window *w;
961 int area;
962{
963 return window_box_left (w, area) + window_box_width (w, area);
964}
965
966
967/* Get the bounding box of the display area AREA of window W, without
968 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 969 whole window, not including the left and right fringes of
5f5c8ee5
GM
970 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
971 coordinates of the upper-left corner of the box. Return in
972 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
973
974INLINE void
975window_box (w, area, box_x, box_y, box_width, box_height)
976 struct window *w;
977 int area;
978 int *box_x, *box_y, *box_width, *box_height;
979{
980 struct frame *f = XFRAME (w->frame);
981
982 *box_width = window_box_width (w, area);
983 *box_height = window_box_height (w);
984 *box_x = window_box_left (w, area);
985 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
986 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
987 if (WINDOW_WANTS_HEADER_LINE_P (w))
988 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 989}
1adc55de 990
1adc55de 991
5f5c8ee5 992/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
993 mode lines. AREA < 0 means the whole window, not including the
994 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
995 and TOP_LEFT_Y the frame-relative pixel coordinates of the
996 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
997 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
998 box. */
ded34426 999
5f5c8ee5
GM
1000INLINE void
1001window_box_edges (w, area, top_left_x, top_left_y,
1002 bottom_right_x, bottom_right_y)
1003 struct window *w;
1004 int area;
1005 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1006{
5f5c8ee5
GM
1007 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1008 bottom_right_y);
1009 *bottom_right_x += *top_left_x;
1010 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1011}
1012
5f5c8ee5
GM
1013
1014\f
1015/***********************************************************************
1016 Utilities
1017 ***********************************************************************/
1018
8b6ea97f
GM
1019/* Return the bottom y-position of the line the iterator IT is in.
1020 This can modify IT's settings. */
1021
1022int
1023line_bottom_y (it)
1024 struct it *it;
1025{
1026 int line_height = it->max_ascent + it->max_descent;
1027 int line_top_y = it->current_y;
1028
1029 if (line_height == 0)
1030 {
1031 if (last_height)
1032 line_height = last_height;
1033 else if (IT_CHARPOS (*it) < ZV)
1034 {
1035 move_it_by_lines (it, 1, 1);
1036 line_height = (it->max_ascent || it->max_descent
1037 ? it->max_ascent + it->max_descent
1038 : last_height);
1039 }
1040 else
1041 {
1042 struct glyph_row *row = it->glyph_row;
1043
1044 /* Use the default character height. */
1045 it->glyph_row = NULL;
1046 it->what = IT_CHARACTER;
1047 it->c = ' ';
1048 it->len = 1;
1049 PRODUCE_GLYPHS (it);
1050 line_height = it->ascent + it->descent;
1051 it->glyph_row = row;
1052 }
1053 }
1054
1055 return line_top_y + line_height;
1056}
1057
1058
0db95684
GM
1059/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1060 1 if POS is visible and the line containing POS is fully visible.
1061 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1062 and header-lines heights. */
3a641a69
GM
1063
1064int
04612a64 1065pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1066 struct window *w;
04612a64 1067 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1068{
1069 struct it it;
1070 struct text_pos top;
fcab1954
GM
1071 int visible_p;
1072 struct buffer *old_buffer = NULL;
1073
1074 if (XBUFFER (w->buffer) != current_buffer)
1075 {
1076 old_buffer = current_buffer;
1077 set_buffer_internal_1 (XBUFFER (w->buffer));
1078 }
3a641a69
GM
1079
1080 *fully = visible_p = 0;
1081 SET_TEXT_POS_FROM_MARKER (top, w->start);
04612a64
GM
1082
1083 /* Compute exact mode line heights, if requested. */
1084 if (exact_mode_line_heights_p)
1085 {
1086 if (WINDOW_WANTS_MODELINE_P (w))
1087 current_mode_line_height
96d2320f 1088 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64
GM
1089 current_buffer->mode_line_format);
1090
1091 if (WINDOW_WANTS_HEADER_LINE_P (w))
1092 current_header_line_height
1093 = display_mode_line (w, HEADER_LINE_FACE_ID,
1094 current_buffer->header_line_format);
1095 }
3a641a69 1096
04612a64 1097 start_display (&it, w, top);
3a641a69
GM
1098 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1099 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1100
1101 /* Note that we may overshoot because of invisible text. */
1102 if (IT_CHARPOS (it) >= charpos)
3a641a69 1103 {
8b6ea97f
GM
1104 int top_y = it.current_y;
1105 int bottom_y = line_bottom_y (&it);
fcab1954 1106 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
3a641a69 1107
8b6ea97f
GM
1108 if (top_y < window_top_y)
1109 visible_p = bottom_y > window_top_y;
1110 else if (top_y < it.last_visible_y)
fcab1954
GM
1111 {
1112 visible_p = 1;
8b6ea97f 1113 *fully = bottom_y <= it.last_visible_y;
fcab1954 1114 }
3a641a69
GM
1115 }
1116 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1117 {
1118 move_it_by_lines (&it, 1, 0);
1119 if (charpos < IT_CHARPOS (it))
1120 {
1121 visible_p = 1;
1122 *fully = 0;
1123 }
1124 }
fcab1954
GM
1125
1126 if (old_buffer)
1127 set_buffer_internal_1 (old_buffer);
04612a64
GM
1128
1129 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1130 return visible_p;
1131}
1132
1133
4fdb80f2
GM
1134/* Return the next character from STR which is MAXLEN bytes long.
1135 Return in *LEN the length of the character. This is like
1136 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1137 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1138 character. */
1139
1140static INLINE int
7a5b8a93 1141string_char_and_length (str, maxlen, len)
4fdb80f2 1142 unsigned char *str;
7a5b8a93 1143 int maxlen, *len;
4fdb80f2
GM
1144{
1145 int c;
1146
1147 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1148 if (!CHAR_VALID_P (c, 1))
1149 /* We may not change the length here because other places in Emacs
9ab8560d 1150 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1151 characters. */
1152 c = '?';
1153
1154 return c;
1155}
1156
1157
1158
5f5c8ee5
GM
1159/* Given a position POS containing a valid character and byte position
1160 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1161
1162static struct text_pos
1163string_pos_nchars_ahead (pos, string, nchars)
1164 struct text_pos pos;
1165 Lisp_Object string;
1166 int nchars;
0b1005ef 1167{
5f5c8ee5
GM
1168 xassert (STRINGP (string) && nchars >= 0);
1169
1170 if (STRING_MULTIBYTE (string))
1171 {
1172 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1173 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1174 int len;
1175
1176 while (nchars--)
1177 {
4fdb80f2 1178 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1179 p += len, rest -= len;
1180 xassert (rest >= 0);
1181 CHARPOS (pos) += 1;
1182 BYTEPOS (pos) += len;
1183 }
1184 }
1185 else
1186 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1187
1188 return pos;
0a9dc68b
RS
1189}
1190
0a9dc68b 1191
5f5c8ee5
GM
1192/* Value is the text position, i.e. character and byte position,
1193 for character position CHARPOS in STRING. */
1194
1195static INLINE struct text_pos
1196string_pos (charpos, string)
1197 int charpos;
0a9dc68b 1198 Lisp_Object string;
0a9dc68b 1199{
5f5c8ee5
GM
1200 struct text_pos pos;
1201 xassert (STRINGP (string));
1202 xassert (charpos >= 0);
1203 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1204 return pos;
1205}
1206
1207
1208/* Value is a text position, i.e. character and byte position, for
1209 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1210 means recognize multibyte characters. */
1211
1212static struct text_pos
1213c_string_pos (charpos, s, multibyte_p)
1214 int charpos;
1215 unsigned char *s;
1216 int multibyte_p;
1217{
1218 struct text_pos pos;
1219
1220 xassert (s != NULL);
1221 xassert (charpos >= 0);
1222
1223 if (multibyte_p)
0a9dc68b 1224 {
5f5c8ee5
GM
1225 int rest = strlen (s), len;
1226
1227 SET_TEXT_POS (pos, 0, 0);
1228 while (charpos--)
0a9dc68b 1229 {
4fdb80f2 1230 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1231 s += len, rest -= len;
1232 xassert (rest >= 0);
1233 CHARPOS (pos) += 1;
1234 BYTEPOS (pos) += len;
0a9dc68b
RS
1235 }
1236 }
5f5c8ee5
GM
1237 else
1238 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1239
5f5c8ee5
GM
1240 return pos;
1241}
0a9dc68b 1242
0a9dc68b 1243
5f5c8ee5
GM
1244/* Value is the number of characters in C string S. MULTIBYTE_P
1245 non-zero means recognize multibyte characters. */
0a9dc68b 1246
5f5c8ee5
GM
1247static int
1248number_of_chars (s, multibyte_p)
1249 unsigned char *s;
1250 int multibyte_p;
1251{
1252 int nchars;
1253
1254 if (multibyte_p)
1255 {
1256 int rest = strlen (s), len;
1257 unsigned char *p = (unsigned char *) s;
0a9dc68b 1258
5f5c8ee5
GM
1259 for (nchars = 0; rest > 0; ++nchars)
1260 {
4fdb80f2 1261 string_char_and_length (p, rest, &len);
5f5c8ee5 1262 rest -= len, p += len;
0a9dc68b
RS
1263 }
1264 }
5f5c8ee5
GM
1265 else
1266 nchars = strlen (s);
1267
1268 return nchars;
0b1005ef
KH
1269}
1270
5f5c8ee5
GM
1271
1272/* Compute byte position NEWPOS->bytepos corresponding to
1273 NEWPOS->charpos. POS is a known position in string STRING.
1274 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1275
5f5c8ee5
GM
1276static void
1277compute_string_pos (newpos, pos, string)
1278 struct text_pos *newpos, pos;
1279 Lisp_Object string;
76412d64 1280{
5f5c8ee5
GM
1281 xassert (STRINGP (string));
1282 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1283
1284 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1285 *newpos = string_pos_nchars_ahead (pos, string,
1286 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1287 else
1288 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1289}
1290
9c74a0dd 1291
5f5c8ee5
GM
1292\f
1293/***********************************************************************
1294 Lisp form evaluation
1295 ***********************************************************************/
1296
116d6f5c 1297/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1298
1299static Lisp_Object
116d6f5c 1300safe_eval_handler (arg)
5f5c8ee5
GM
1301 Lisp_Object arg;
1302{
0dbf9fd2 1303 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1304 return Qnil;
1305}
1306
1307
1308/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1309 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1310
71e5b1b8 1311Lisp_Object
116d6f5c 1312safe_eval (sexpr)
5f5c8ee5
GM
1313 Lisp_Object sexpr;
1314{
5f5c8ee5 1315 Lisp_Object val;
30a3f61c
GM
1316
1317 if (inhibit_eval_during_redisplay)
1318 val = Qnil;
1319 else
1320 {
1321 int count = BINDING_STACK_SIZE ();
1322 struct gcpro gcpro1;
0d8b31c0 1323
30a3f61c
GM
1324 GCPRO1 (sexpr);
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1330 }
1331
1332 return val;
0d8b31c0
GM
1333}
1334
1335
1336/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1337 Return the result, or nil if something went wrong. Prevent
1338 redisplay during the evaluation. */
0d8b31c0
GM
1339
1340Lisp_Object
116d6f5c 1341safe_call (nargs, args)
0d8b31c0
GM
1342 int nargs;
1343 Lisp_Object *args;
1344{
0d8b31c0 1345 Lisp_Object val;
30a3f61c
GM
1346
1347 if (inhibit_eval_during_redisplay)
1348 val = Qnil;
1349 else
1350 {
1351 int count = BINDING_STACK_SIZE ();
1352 struct gcpro gcpro1;
0d8b31c0 1353
30a3f61c
GM
1354 GCPRO1 (args[0]);
1355 gcpro1.nvars = nargs;
1356 specbind (Qinhibit_redisplay, Qt);
1357 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1358 safe_eval_handler);
1359 UNGCPRO;
1360 val = unbind_to (count, val);
1361 }
1362
1363 return val;
5f5c8ee5
GM
1364}
1365
1366
116d6f5c
GM
1367/* Call function FN with one argument ARG.
1368 Return the result, or nil if something went wrong. */
1369
1370Lisp_Object
1371safe_call1 (fn, arg)
1372 Lisp_Object fn, arg;
1373{
1374 Lisp_Object args[2];
1375 args[0] = fn;
1376 args[1] = arg;
1377 return safe_call (2, args);
1378}
1379
1380
5f5c8ee5
GM
1381\f
1382/***********************************************************************
1383 Debugging
1384 ***********************************************************************/
1385
1386#if 0
1387
1388/* Define CHECK_IT to perform sanity checks on iterators.
1389 This is for debugging. It is too slow to do unconditionally. */
1390
1391static void
1392check_it (it)
1393 struct it *it;
1394{
1395 if (it->method == next_element_from_string)
a2889657 1396 {
5f5c8ee5
GM
1397 xassert (STRINGP (it->string));
1398 xassert (IT_STRING_CHARPOS (*it) >= 0);
1399 }
1400 else if (it->method == next_element_from_buffer)
1401 {
1402 /* Check that character and byte positions agree. */
1403 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1404 }
73af359d 1405
5f5c8ee5
GM
1406 if (it->dpvec)
1407 xassert (it->current.dpvec_index >= 0);
1408 else
1409 xassert (it->current.dpvec_index < 0);
1410}
1f40cad2 1411
5f5c8ee5
GM
1412#define CHECK_IT(IT) check_it ((IT))
1413
1414#else /* not 0 */
1415
1416#define CHECK_IT(IT) (void) 0
1417
1418#endif /* not 0 */
1419
1420
1421#if GLYPH_DEBUG
1422
1423/* Check that the window end of window W is what we expect it
1424 to be---the last row in the current matrix displaying text. */
1425
1426static void
1427check_window_end (w)
1428 struct window *w;
1429{
1430 if (!MINI_WINDOW_P (w)
1431 && !NILP (w->window_end_valid))
1432 {
1433 struct glyph_row *row;
1434 xassert ((row = MATRIX_ROW (w->current_matrix,
1435 XFASTINT (w->window_end_vpos)),
1436 !row->enabled_p
1437 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1438 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1439 }
1440}
1441
1442#define CHECK_WINDOW_END(W) check_window_end ((W))
1443
1444#else /* not GLYPH_DEBUG */
1445
1446#define CHECK_WINDOW_END(W) (void) 0
1447
1448#endif /* not GLYPH_DEBUG */
1449
1450
1451\f
1452/***********************************************************************
1453 Iterator initialization
1454 ***********************************************************************/
1455
1456/* Initialize IT for displaying current_buffer in window W, starting
1457 at character position CHARPOS. CHARPOS < 0 means that no buffer
1458 position is specified which is useful when the iterator is assigned
1459 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1460 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1461
1462 If ROW is not null, calls to produce_glyphs with IT as parameter
1463 will produce glyphs in that row.
1464
1465 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1466 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1467 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1468 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
5f5c8ee5 1469
96d2320f
KS
1470 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1471 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1472 will be initialized to use the corresponding mode line glyph row of
1473 the desired matrix of W. */
5f5c8ee5
GM
1474
1475void
1476init_iterator (it, w, charpos, bytepos, row, base_face_id)
1477 struct it *it;
1478 struct window *w;
1479 int charpos, bytepos;
1480 struct glyph_row *row;
1481 enum face_id base_face_id;
1482{
1483 int highlight_region_p;
5f5c8ee5
GM
1484
1485 /* Some precondition checks. */
1486 xassert (w != NULL && it != NULL);
3b6b6db7
SM
1487 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1488 && charpos <= ZV));
5f5c8ee5
GM
1489
1490 /* If face attributes have been changed since the last redisplay,
1491 free realized faces now because they depend on face definitions
1492 that might have changed. */
1493 if (face_change_count)
1494 {
1495 face_change_count = 0;
1496 free_all_realized_faces (Qnil);
1497 }
1498
1499 /* Use one of the mode line rows of W's desired matrix if
1500 appropriate. */
1501 if (row == NULL)
1502 {
96d2320f
KS
1503 if (base_face_id == MODE_LINE_FACE_ID
1504 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 1505 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1506 else if (base_face_id == HEADER_LINE_FACE_ID)
1507 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1508 }
1509
1510 /* Clear IT. */
1511 bzero (it, sizeof *it);
1512 it->current.overlay_string_index = -1;
1513 it->current.dpvec_index = -1;
5f5c8ee5
GM
1514 it->base_face_id = base_face_id;
1515
1516 /* The window in which we iterate over current_buffer: */
1517 XSETWINDOW (it->window, w);
1518 it->w = w;
1519 it->f = XFRAME (w->frame);
1520
d475bcb8
GM
1521 /* Extra space between lines (on window systems only). */
1522 if (base_face_id == DEFAULT_FACE_ID
1523 && FRAME_WINDOW_P (it->f))
1524 {
1525 if (NATNUMP (current_buffer->extra_line_spacing))
1526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1527 else if (it->f->extra_line_spacing > 0)
1528 it->extra_line_spacing = it->f->extra_line_spacing;
1529 }
1530
5f5c8ee5 1531 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1532 attribute changes of named faces, recompute them. When running
1533 in batch mode, the face cache of Vterminal_frame is null. If
1534 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1535 if (
1536#ifndef WINDOWSNT
1537 noninteractive &&
1538#endif
1539 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1540 init_frame_faces (it->f);
5f5c8ee5
GM
1541 if (FRAME_FACE_CACHE (it->f)->used == 0)
1542 recompute_basic_faces (it->f);
1543
5f5c8ee5
GM
1544 /* Current value of the `space-width', and 'height' properties. */
1545 it->space_width = Qnil;
1546 it->font_height = Qnil;
1547
1548 /* Are control characters displayed as `^C'? */
1549 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1550
1551 /* -1 means everything between a CR and the following line end
1552 is invisible. >0 means lines indented more than this value are
1553 invisible. */
1554 it->selective = (INTEGERP (current_buffer->selective_display)
1555 ? XFASTINT (current_buffer->selective_display)
1556 : (!NILP (current_buffer->selective_display)
1557 ? -1 : 0));
1558 it->selective_display_ellipsis_p
1559 = !NILP (current_buffer->selective_display_ellipses);
1560
1561 /* Display table to use. */
1562 it->dp = window_display_table (w);
1563
1564 /* Are multibyte characters enabled in current_buffer? */
1565 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1566
1567 /* Non-zero if we should highlight the region. */
1568 highlight_region_p
3ebf0ea9 1569 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
1570 && !NILP (current_buffer->mark_active)
1571 && XMARKER (current_buffer->mark)->buffer != 0);
1572
1573 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1574 start and end of a visible region in window IT->w. Set both to
1575 -1 to indicate no region. */
1576 if (highlight_region_p
1577 /* Maybe highlight only in selected window. */
1578 && (/* Either show region everywhere. */
1579 highlight_nonselected_windows
1580 /* Or show region in the selected window. */
1581 || w == XWINDOW (selected_window)
1582 /* Or show the region if we are in the mini-buffer and W is
1583 the window the mini-buffer refers to. */
1584 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
1585 && WINDOWP (minibuf_selected_window)
1586 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
1587 {
1588 int charpos = marker_position (current_buffer->mark);
1589 it->region_beg_charpos = min (PT, charpos);
1590 it->region_end_charpos = max (PT, charpos);
1591 }
1592 else
1593 it->region_beg_charpos = it->region_end_charpos = -1;
1594
1595 /* Get the position at which the redisplay_end_trigger hook should
1596 be run, if it is to be run at all. */
1597 if (MARKERP (w->redisplay_end_trigger)
1598 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1599 it->redisplay_end_trigger_charpos
1600 = marker_position (w->redisplay_end_trigger);
1601 else if (INTEGERP (w->redisplay_end_trigger))
1602 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1603
1604 /* Correct bogus values of tab_width. */
1605 it->tab_width = XINT (current_buffer->tab_width);
1606 if (it->tab_width <= 0 || it->tab_width > 1000)
1607 it->tab_width = 8;
1608
1609 /* Are lines in the display truncated? */
1610 it->truncate_lines_p
1611 = (base_face_id != DEFAULT_FACE_ID
1612 || XINT (it->w->hscroll)
1613 || (truncate_partial_width_windows
1614 && !WINDOW_FULL_WIDTH_P (it->w))
1615 || !NILP (current_buffer->truncate_lines));
1616
1617 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 1618 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
1619 frames. */
1620 if (!FRAME_WINDOW_P (it->f))
1621 {
1622 if (it->truncate_lines_p)
1623 {
1624 /* We will need the truncation glyph. */
1625 xassert (it->glyph_row == NULL);
1626 produce_special_glyphs (it, IT_TRUNCATION);
1627 it->truncation_pixel_width = it->pixel_width;
1628 }
1629 else
1630 {
1631 /* We will need the continuation glyph. */
1632 xassert (it->glyph_row == NULL);
1633 produce_special_glyphs (it, IT_CONTINUATION);
1634 it->continuation_pixel_width = it->pixel_width;
1635 }
1636
153c2160 1637 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
1638 above has changed them. */
1639 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1640 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1641 }
1642
1643 /* Set this after getting the dimensions of truncation and
1644 continuation glyphs, so that we don't produce glyphs when calling
1645 produce_special_glyphs, above. */
1646 it->glyph_row = row;
1647 it->area = TEXT_AREA;
1648
1649 /* Get the dimensions of the display area. The display area
1650 consists of the visible window area plus a horizontally scrolled
1651 part to the left of the window. All x-values are relative to the
1652 start of this total display area. */
1653 if (base_face_id != DEFAULT_FACE_ID)
1654 {
1655 /* Mode lines, menu bar in terminal frames. */
1656 it->first_visible_x = 0;
1657 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1658 }
1659 else
1660 {
1661 it->first_visible_x
1662 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1663 it->last_visible_x = (it->first_visible_x
1664 + window_box_width (w, TEXT_AREA));
1665
1666 /* If we truncate lines, leave room for the truncator glyph(s) at
1667 the right margin. Otherwise, leave room for the continuation
1668 glyph(s). Truncation and continuation glyphs are not inserted
1669 for window-based redisplay. */
1670 if (!FRAME_WINDOW_P (it->f))
1671 {
1672 if (it->truncate_lines_p)
1673 it->last_visible_x -= it->truncation_pixel_width;
1674 else
1675 it->last_visible_x -= it->continuation_pixel_width;
1676 }
1677
045dee35
GM
1678 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1679 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1680 }
1681
1682 /* Leave room for a border glyph. */
1683 if (!FRAME_WINDOW_P (it->f)
1684 && !WINDOW_RIGHTMOST_P (it->w))
1685 it->last_visible_x -= 1;
1686
1687 it->last_visible_y = window_text_bottom_y (w);
1688
1689 /* For mode lines and alike, arrange for the first glyph having a
1690 left box line if the face specifies a box. */
1691 if (base_face_id != DEFAULT_FACE_ID)
1692 {
1693 struct face *face;
1694
1695 it->face_id = base_face_id;
1696
1697 /* If we have a boxed mode line, make the first character appear
1698 with a left box line. */
1699 face = FACE_FROM_ID (it->f, base_face_id);
1700 if (face->box != FACE_NO_BOX)
1701 it->start_of_box_run_p = 1;
1702 }
1703
1704 /* If a buffer position was specified, set the iterator there,
1705 getting overlays and face properties from that position. */
3ebf0ea9 1706 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
1707 {
1708 it->end_charpos = ZV;
1709 it->face_id = -1;
1710 IT_CHARPOS (*it) = charpos;
1711
1712 /* Compute byte position if not specified. */
3ebf0ea9 1713 if (bytepos < charpos)
5f5c8ee5
GM
1714 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1715 else
1716 IT_BYTEPOS (*it) = bytepos;
1717
1718 /* Compute faces etc. */
1719 reseat (it, it->current.pos, 1);
1720 }
1721
1722 CHECK_IT (it);
1723}
1724
1725
1726/* Initialize IT for the display of window W with window start POS. */
1727
1728void
1729start_display (it, w, pos)
1730 struct it *it;
1731 struct window *w;
1732 struct text_pos pos;
1733{
5f5c8ee5 1734 struct glyph_row *row;
045dee35 1735 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1736
1737 row = w->desired_matrix->rows + first_vpos;
1738 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
17fdcfc8
GM
1739
1740 if (!it->truncate_lines_p)
5f5c8ee5 1741 {
17fdcfc8
GM
1742 int start_at_line_beg_p;
1743 int first_y = it->current_y;
1744
1745 /* If window start is not at a line start, skip forward to POS to
1746 get the correct continuation lines width. */
1747 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1748 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1749 if (!start_at_line_beg_p)
5f5c8ee5 1750 {
17fdcfc8
GM
1751 reseat_at_previous_visible_line_start (it);
1752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1753
1754 /* If lines are continued, this line may end in the middle
1755 of a multi-glyph character (e.g. a control character
1756 displayed as \003, or in the middle of an overlay
1757 string). In this case move_it_to above will not have
1758 taken us to the start of the continuation line but to the
1759 end of the continued line. */
b28cb6ed 1760 if (it->current_x > 0)
5f5c8ee5 1761 {
b28cb6ed
GM
1762 if (it->current.dpvec_index >= 0
1763 || it->current.overlay_string_index >= 0)
1764 {
cafafe0b 1765 set_iterator_to_next (it, 1);
b28cb6ed
GM
1766 move_it_in_display_line_to (it, -1, -1, 0);
1767 }
17fdcfc8 1768
b28cb6ed 1769 it->continuation_lines_width += it->current_x;
5f5c8ee5 1770 }
b28cb6ed
GM
1771
1772 /* We're starting a new display line, not affected by the
1773 height of the continued line, so clear the appropriate
1774 fields in the iterator structure. */
1775 it->max_ascent = it->max_descent = 0;
1776 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 1777
17fdcfc8
GM
1778 it->current_y = first_y;
1779 it->vpos = 0;
1780 it->current_x = it->hpos = 0;
1781 }
5f5c8ee5
GM
1782 }
1783
1784#if 0 /* Don't assert the following because start_display is sometimes
1785 called intentionally with a window start that is not at a
1786 line start. Please leave this code in as a comment. */
1787
1788 /* Window start should be on a line start, now. */
1789 xassert (it->continuation_lines_width
1790 || IT_CHARPOS (it) == BEGV
1791 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1792#endif /* 0 */
1793}
1794
1795
4bde0ebb
GM
1796/* Return 1 if POS is a position in ellipses displayed for invisible
1797 text. W is the window we display, for text property lookup. */
5f5c8ee5 1798
4bde0ebb
GM
1799static int
1800in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 1801 struct display_pos *pos;
4bde0ebb 1802 struct window *w;
5f5c8ee5 1803{
ac90c44f 1804 Lisp_Object prop, window;
4bde0ebb
GM
1805 int ellipses_p = 0;
1806 int charpos = CHARPOS (pos->pos);
ac90c44f
GM
1807
1808 /* If POS specifies a position in a display vector, this might
1809 be for an ellipsis displayed for invisible text. We won't
1810 get the iterator set up for delivering that ellipsis unless
1811 we make sure that it gets aware of the invisible text. */
1812 if (pos->dpvec_index >= 0
1813 && pos->overlay_string_index < 0
1814 && CHARPOS (pos->string_pos) < 0
1815 && charpos > BEGV
1816 && (XSETWINDOW (window, w),
1817 prop = Fget_char_property (make_number (charpos),
1818 Qinvisible, window),
20fbd925 1819 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
1820 {
1821 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1822 window);
8580a4e3 1823 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
1824 }
1825
1826 return ellipses_p;
1827}
1828
1829
1830/* Initialize IT for stepping through current_buffer in window W,
1831 starting at position POS that includes overlay string and display
47d57b22
GM
1832 vector/ control character translation position information. Value
1833 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 1834
47d57b22 1835static int
4bde0ebb
GM
1836init_from_display_pos (it, w, pos)
1837 struct it *it;
1838 struct window *w;
1839 struct display_pos *pos;
1840{
1841 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 1842 int i, overlay_strings_with_newlines = 0;
4bde0ebb
GM
1843
1844 /* If POS specifies a position in a display vector, this might
1845 be for an ellipsis displayed for invisible text. We won't
1846 get the iterator set up for delivering that ellipsis unless
1847 we make sure that it gets aware of the invisible text. */
1848 if (in_ellipses_for_invisible_text_p (pos, w))
1849 {
1850 --charpos;
1851 bytepos = 0;
ac90c44f
GM
1852 }
1853
5f5c8ee5
GM
1854 /* Keep in mind: the call to reseat in init_iterator skips invisible
1855 text, so we might end up at a position different from POS. This
1856 is only a problem when POS is a row start after a newline and an
1857 overlay starts there with an after-string, and the overlay has an
1858 invisible property. Since we don't skip invisible text in
1859 display_line and elsewhere immediately after consuming the
1860 newline before the row start, such a POS will not be in a string,
1861 but the call to init_iterator below will move us to the
1862 after-string. */
ac90c44f 1863 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 1864
47d57b22 1865 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf
GM
1866 {
1867 char *s = XSTRING (it->overlay_strings[i])->data;
1868 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1869
1870 while (s < e && *s != '\n')
1871 ++s;
1872
1873 if (s < e)
1874 {
1875 overlay_strings_with_newlines = 1;
1876 break;
1877 }
1878 }
47d57b22 1879
75c5350a
GM
1880 /* If position is within an overlay string, set up IT to the right
1881 overlay string. */
5f5c8ee5
GM
1882 if (pos->overlay_string_index >= 0)
1883 {
1884 int relative_index;
75c5350a
GM
1885
1886 /* If the first overlay string happens to have a `display'
1887 property for an image, the iterator will be set up for that
1888 image, and we have to undo that setup first before we can
1889 correct the overlay string index. */
1890 if (it->method == next_element_from_image)
1891 pop_it (it);
5f5c8ee5
GM
1892
1893 /* We already have the first chunk of overlay strings in
1894 IT->overlay_strings. Load more until the one for
1895 pos->overlay_string_index is in IT->overlay_strings. */
1896 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1897 {
1898 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1899 it->current.overlay_string_index = 0;
1900 while (n--)
1901 {
5a08cbaf 1902 load_overlay_strings (it, 0);
5f5c8ee5
GM
1903 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1904 }
1905 }
1906
1907 it->current.overlay_string_index = pos->overlay_string_index;
1908 relative_index = (it->current.overlay_string_index
1909 % OVERLAY_STRING_CHUNK_SIZE);
1910 it->string = it->overlay_strings[relative_index];
cafafe0b 1911 xassert (STRINGP (it->string));
5f5c8ee5
GM
1912 it->current.string_pos = pos->string_pos;
1913 it->method = next_element_from_string;
1914 }
1e1e5daf
GM
1915
1916#if 0 /* This is bogus because POS not having an overlay string
1917 position does not mean it's after the string. Example: A
1918 line starting with a before-string and initialization of IT
1919 to the previous row's end position. */
2be8f184
GM
1920 else if (it->current.overlay_string_index >= 0)
1921 {
1922 /* If POS says we're already after an overlay string ending at
1923 POS, make sure to pop the iterator because it will be in
1924 front of that overlay string. When POS is ZV, we've thereby
1925 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1926 while (it->sp)
1927 pop_it (it);
2be8f184
GM
1928 it->current.overlay_string_index = -1;
1929 it->method = next_element_from_buffer;
1930 if (CHARPOS (pos->pos) == ZV)
1931 it->overlay_strings_at_end_processed_p = 1;
1932 }
1e1e5daf 1933#endif /* 0 */
2be8f184
GM
1934
1935 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1936 {
1937 /* Recorded position is not in an overlay string, but in another
1938 string. This can only be a string from a `display' property.
1939 IT should already be filled with that string. */
1940 it->current.string_pos = pos->string_pos;
1941 xassert (STRINGP (it->string));
1942 }
1943
ac90c44f
GM
1944 /* Restore position in display vector translations, control
1945 character translations or ellipses. */
5f5c8ee5
GM
1946 if (pos->dpvec_index >= 0)
1947 {
ac90c44f
GM
1948 if (it->dpvec == NULL)
1949 get_next_display_element (it);
5f5c8ee5
GM
1950 xassert (it->dpvec && it->current.dpvec_index == 0);
1951 it->current.dpvec_index = pos->dpvec_index;
1952 }
1953
1954 CHECK_IT (it);
47d57b22 1955 return !overlay_strings_with_newlines;
5f5c8ee5
GM
1956}
1957
1958
1959/* Initialize IT for stepping through current_buffer in window W
1960 starting at ROW->start. */
1961
1962static void
1963init_to_row_start (it, w, row)
1964 struct it *it;
1965 struct window *w;
1966 struct glyph_row *row;
1967{
1968 init_from_display_pos (it, w, &row->start);
1969 it->continuation_lines_width = row->continuation_lines_width;
1970 CHECK_IT (it);
1971}
1972
1973
1974/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
1975 starting in the line following ROW, i.e. starting at ROW->end.
1976 Value is zero if there are overlay strings with newlines at ROW's
1977 end position. */
5f5c8ee5 1978
47d57b22 1979static int
5f5c8ee5
GM
1980init_to_row_end (it, w, row)
1981 struct it *it;
1982 struct window *w;
1983 struct glyph_row *row;
1984{
47d57b22
GM
1985 int success = 0;
1986
1987 if (init_from_display_pos (it, w, &row->end))
1988 {
1989 if (row->continued_p)
1990 it->continuation_lines_width
1991 = row->continuation_lines_width + row->pixel_width;
1992 CHECK_IT (it);
1993 success = 1;
1994 }
1995
1996 return success;
5f5c8ee5
GM
1997}
1998
1999
2000
2001\f
2002/***********************************************************************
2003 Text properties
2004 ***********************************************************************/
2005
2006/* Called when IT reaches IT->stop_charpos. Handle text property and
2007 overlay changes. Set IT->stop_charpos to the next position where
2008 to stop. */
2009
2010static void
2011handle_stop (it)
2012 struct it *it;
2013{
2014 enum prop_handled handled;
2015 int handle_overlay_change_p = 1;
2016 struct props *p;
2017
2018 it->dpvec = NULL;
2019 it->current.dpvec_index = -1;
2020
2021 do
2022 {
2023 handled = HANDLED_NORMALLY;
2024
2025 /* Call text property handlers. */
2026 for (p = it_props; p->handler; ++p)
2027 {
2028 handled = p->handler (it);
2970b9be 2029
5f5c8ee5
GM
2030 if (handled == HANDLED_RECOMPUTE_PROPS)
2031 break;
2032 else if (handled == HANDLED_RETURN)
2033 return;
2034 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2035 handle_overlay_change_p = 0;
2036 }
2037
2038 if (handled != HANDLED_RECOMPUTE_PROPS)
2039 {
2040 /* Don't check for overlay strings below when set to deliver
2041 characters from a display vector. */
2042 if (it->method == next_element_from_display_vector)
2043 handle_overlay_change_p = 0;
2044
2045 /* Handle overlay changes. */
2046 if (handle_overlay_change_p)
2047 handled = handle_overlay_change (it);
2048
2049 /* Determine where to stop next. */
2050 if (handled == HANDLED_NORMALLY)
2051 compute_stop_pos (it);
2052 }
2053 }
2054 while (handled == HANDLED_RECOMPUTE_PROPS);
2055}
2056
2057
2058/* Compute IT->stop_charpos from text property and overlay change
2059 information for IT's current position. */
2060
2061static void
2062compute_stop_pos (it)
2063 struct it *it;
2064{
2065 register INTERVAL iv, next_iv;
2066 Lisp_Object object, limit, position;
2067
2068 /* If nowhere else, stop at the end. */
2069 it->stop_charpos = it->end_charpos;
2070
2071 if (STRINGP (it->string))
2072 {
2073 /* Strings are usually short, so don't limit the search for
2074 properties. */
2075 object = it->string;
2076 limit = Qnil;
ac90c44f 2077 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2078 }
2079 else
2080 {
2081 int charpos;
2082
2083 /* If next overlay change is in front of the current stop pos
2084 (which is IT->end_charpos), stop there. Note: value of
2085 next_overlay_change is point-max if no overlay change
2086 follows. */
2087 charpos = next_overlay_change (IT_CHARPOS (*it));
2088 if (charpos < it->stop_charpos)
2089 it->stop_charpos = charpos;
2090
2091 /* If showing the region, we have to stop at the region
2092 start or end because the face might change there. */
2093 if (it->region_beg_charpos > 0)
2094 {
2095 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2096 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2097 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2098 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2099 }
2100
2101 /* Set up variables for computing the stop position from text
2102 property changes. */
2103 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2104 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2105 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2106
2107 }
2108
2109 /* Get the interval containing IT's position. Value is a null
2110 interval if there isn't such an interval. */
2111 iv = validate_interval_range (object, &position, &position, 0);
2112 if (!NULL_INTERVAL_P (iv))
2113 {
2114 Lisp_Object values_here[LAST_PROP_IDX];
2115 struct props *p;
2116
2117 /* Get properties here. */
2118 for (p = it_props; p->handler; ++p)
2119 values_here[p->idx] = textget (iv->plist, *p->name);
2120
2121 /* Look for an interval following iv that has different
2122 properties. */
2123 for (next_iv = next_interval (iv);
2124 (!NULL_INTERVAL_P (next_iv)
2125 && (NILP (limit)
2126 || XFASTINT (limit) > next_iv->position));
2127 next_iv = next_interval (next_iv))
2128 {
2129 for (p = it_props; p->handler; ++p)
2130 {
2131 Lisp_Object new_value;
2132
2133 new_value = textget (next_iv->plist, *p->name);
2134 if (!EQ (values_here[p->idx], new_value))
2135 break;
2136 }
2137
2138 if (p->handler)
2139 break;
2140 }
2141
2142 if (!NULL_INTERVAL_P (next_iv))
2143 {
2144 if (INTEGERP (limit)
2145 && next_iv->position >= XFASTINT (limit))
2146 /* No text property change up to limit. */
2147 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2148 else
2149 /* Text properties change in next_iv. */
2150 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2151 }
2152 }
2153
2154 xassert (STRINGP (it->string)
2155 || (it->stop_charpos >= BEGV
2156 && it->stop_charpos >= IT_CHARPOS (*it)));
2157}
2158
2159
2160/* Return the position of the next overlay change after POS in
2161 current_buffer. Value is point-max if no overlay change
2162 follows. This is like `next-overlay-change' but doesn't use
2163 xmalloc. */
2164
2165static int
2166next_overlay_change (pos)
2167 int pos;
2168{
2169 int noverlays;
2170 int endpos;
2171 Lisp_Object *overlays;
2172 int len;
2173 int i;
2174
2175 /* Get all overlays at the given position. */
2176 len = 10;
2177 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2178 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2179 if (noverlays > len)
2180 {
2181 len = noverlays;
2182 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2183 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2184 }
2185
2186 /* If any of these overlays ends before endpos,
2187 use its ending point instead. */
2188 for (i = 0; i < noverlays; ++i)
2189 {
2190 Lisp_Object oend;
2191 int oendpos;
2192
2193 oend = OVERLAY_END (overlays[i]);
2194 oendpos = OVERLAY_POSITION (oend);
2195 endpos = min (endpos, oendpos);
2196 }
2197
2198 return endpos;
2199}
2200
2201
2202\f
2203/***********************************************************************
2204 Fontification
2205 ***********************************************************************/
2206
2207/* Handle changes in the `fontified' property of the current buffer by
2208 calling hook functions from Qfontification_functions to fontify
2209 regions of text. */
2210
2211static enum prop_handled
2212handle_fontified_prop (it)
2213 struct it *it;
2214{
2215 Lisp_Object prop, pos;
2216 enum prop_handled handled = HANDLED_NORMALLY;
2217
2218 /* Get the value of the `fontified' property at IT's current buffer
2219 position. (The `fontified' property doesn't have a special
2220 meaning in strings.) If the value is nil, call functions from
2221 Qfontification_functions. */
2222 if (!STRINGP (it->string)
2223 && it->s == NULL
2224 && !NILP (Vfontification_functions)
085536c2 2225 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2226 && (pos = make_number (IT_CHARPOS (*it)),
2227 prop = Fget_char_property (pos, Qfontified, Qnil),
2228 NILP (prop)))
2229 {
2d27dae2 2230 int count = BINDING_STACK_SIZE ();
085536c2
GM
2231 Lisp_Object val;
2232
2233 val = Vfontification_functions;
2234 specbind (Qfontification_functions, Qnil);
085536c2
GM
2235
2236 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2237 safe_call1 (val, pos);
085536c2
GM
2238 else
2239 {
2240 Lisp_Object globals, fn;
2241 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2242
085536c2
GM
2243 globals = Qnil;
2244 GCPRO2 (val, globals);
2245
2246 for (; CONSP (val); val = XCDR (val))
2247 {
2248 fn = XCAR (val);
2249
2250 if (EQ (fn, Qt))
2251 {
2252 /* A value of t indicates this hook has a local
2253 binding; it means to run the global binding too.
2254 In a global value, t should not occur. If it
2255 does, we must ignore it to avoid an endless
2256 loop. */
2257 for (globals = Fdefault_value (Qfontification_functions);
2258 CONSP (globals);
2259 globals = XCDR (globals))
2260 {
2261 fn = XCAR (globals);
2262 if (!EQ (fn, Qt))
116d6f5c 2263 safe_call1 (fn, pos);
085536c2
GM
2264 }
2265 }
2266 else
116d6f5c 2267 safe_call1 (fn, pos);
085536c2
GM
2268 }
2269
2270 UNGCPRO;
2271 }
2272
2273 unbind_to (count, Qnil);
5f5c8ee5
GM
2274
2275 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2276 something. This avoids an endless loop if they failed to
2277 fontify the text for which reason ever. */
2278 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2279 handled = HANDLED_RECOMPUTE_PROPS;
2280 }
2281
2282 return handled;
2283}
2284
2285
2286\f
2287/***********************************************************************
2288 Faces
2289 ***********************************************************************/
2290
2291/* Set up iterator IT from face properties at its current position.
2292 Called from handle_stop. */
2293
2294static enum prop_handled
2295handle_face_prop (it)
2296 struct it *it;
2297{
2298 int new_face_id, next_stop;
2299
2300 if (!STRINGP (it->string))
2301 {
2302 new_face_id
2303 = face_at_buffer_position (it->w,
2304 IT_CHARPOS (*it),
2305 it->region_beg_charpos,
2306 it->region_end_charpos,
2307 &next_stop,
2308 (IT_CHARPOS (*it)
2309 + TEXT_PROP_DISTANCE_LIMIT),
2310 0);
2311
2312 /* Is this a start of a run of characters with box face?
2313 Caveat: this can be called for a freshly initialized
4b41cebb 2314 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2315 face will not change until limit, i.e. if the new face has a
2316 box, all characters up to limit will have one. But, as
2317 usual, we don't know whether limit is really the end. */
2318 if (new_face_id != it->face_id)
2319 {
2320 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2321
2322 /* If new face has a box but old face has not, this is
2323 the start of a run of characters with box, i.e. it has
2324 a shadow on the left side. The value of face_id of the
2325 iterator will be -1 if this is the initial call that gets
2326 the face. In this case, we have to look in front of IT's
2327 position and see whether there is a face != new_face_id. */
2328 it->start_of_box_run_p
2329 = (new_face->box != FACE_NO_BOX
2330 && (it->face_id >= 0
2331 || IT_CHARPOS (*it) == BEG
2332 || new_face_id != face_before_it_pos (it)));
2333 it->face_box_p = new_face->box != FACE_NO_BOX;
2334 }
2335 }
2336 else
2337 {
06a12811
GM
2338 int base_face_id, bufpos;
2339
2340 if (it->current.overlay_string_index >= 0)
2341 bufpos = IT_CHARPOS (*it);
2342 else
2343 bufpos = 0;
2344
2345 /* For strings from a buffer, i.e. overlay strings or strings
2346 from a `display' property, use the face at IT's current
2347 buffer position as the base face to merge with, so that
2348 overlay strings appear in the same face as surrounding
2349 text, unless they specify their own faces. */
2350 base_face_id = underlying_face_id (it);
2351
2352 new_face_id = face_at_string_position (it->w,
2353 it->string,
2354 IT_STRING_CHARPOS (*it),
2355 bufpos,
2356 it->region_beg_charpos,
2357 it->region_end_charpos,
2358 &next_stop,
5de7c6f2 2359 base_face_id, 0);
5f5c8ee5
GM
2360
2361#if 0 /* This shouldn't be neccessary. Let's check it. */
2362 /* If IT is used to display a mode line we would really like to
2363 use the mode line face instead of the frame's default face. */
2364 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2365 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2366 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5
GM
2367#endif
2368
2369 /* Is this a start of a run of characters with box? Caveat:
2370 this can be called for a freshly allocated iterator; face_id
2371 is -1 is this case. We know that the new face will not
2372 change until the next check pos, i.e. if the new face has a
2373 box, all characters up to that position will have a
2374 box. But, as usual, we don't know whether that position
2375 is really the end. */
2376 if (new_face_id != it->face_id)
2377 {
2378 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2379 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2380
2381 /* If new face has a box but old face hasn't, this is the
2382 start of a run of characters with box, i.e. it has a
2383 shadow on the left side. */
2384 it->start_of_box_run_p
2385 = new_face->box && (old_face == NULL || !old_face->box);
2386 it->face_box_p = new_face->box != FACE_NO_BOX;
2387 }
2388 }
2389
2390 it->face_id = new_face_id;
5f5c8ee5
GM
2391 return HANDLED_NORMALLY;
2392}
2393
2394
06a12811
GM
2395/* Return the ID of the face ``underlying'' IT's current position,
2396 which is in a string. If the iterator is associated with a
2397 buffer, return the face at IT's current buffer position.
2398 Otherwise, use the iterator's base_face_id. */
2399
2400static int
2401underlying_face_id (it)
2402 struct it *it;
2403{
2404 int face_id = it->base_face_id, i;
2405
2406 xassert (STRINGP (it->string));
2407
2408 for (i = it->sp - 1; i >= 0; --i)
2409 if (NILP (it->stack[i].string))
2410 face_id = it->stack[i].face_id;
2411
2412 return face_id;
2413}
2414
2415
5f5c8ee5
GM
2416/* Compute the face one character before or after the current position
2417 of IT. BEFORE_P non-zero means get the face in front of IT's
2418 position. Value is the id of the face. */
2419
2420static int
2421face_before_or_after_it_pos (it, before_p)
2422 struct it *it;
2423 int before_p;
2424{
2425 int face_id, limit;
2426 int next_check_charpos;
2427 struct text_pos pos;
2428
2429 xassert (it->s == NULL);
2430
2431 if (STRINGP (it->string))
2432 {
06a12811
GM
2433 int bufpos, base_face_id;
2434
5f5c8ee5
GM
2435 /* No face change past the end of the string (for the case
2436 we are padding with spaces). No face change before the
2437 string start. */
2438 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2439 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2440 return it->face_id;
2441
2442 /* Set pos to the position before or after IT's current position. */
2443 if (before_p)
2444 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2445 else
260a86a0
KH
2446 /* For composition, we must check the character after the
2447 composition. */
2448 pos = (it->what == IT_COMPOSITION
2449 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2450 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2451
06a12811
GM
2452 if (it->current.overlay_string_index >= 0)
2453 bufpos = IT_CHARPOS (*it);
2454 else
2455 bufpos = 0;
2456
2457 base_face_id = underlying_face_id (it);
2458
5f5c8ee5 2459 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2460 face_id = face_at_string_position (it->w,
2461 it->string,
2462 CHARPOS (pos),
2463 bufpos,
2464 it->region_beg_charpos,
2465 it->region_end_charpos,
2466 &next_check_charpos,
5de7c6f2 2467 base_face_id, 0);
5f5c8ee5
GM
2468
2469 /* Correct the face for charsets different from ASCII. Do it
2470 for the multibyte case only. The face returned above is
2471 suitable for unibyte text if IT->string is unibyte. */
2472 if (STRING_MULTIBYTE (it->string))
2473 {
2474 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2475 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
2476 int c, len;
2477 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 2478
4fdb80f2 2479 c = string_char_and_length (p, rest, &len);
980806b6 2480 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2481 }
2482 }
2483 else
2484 {
70851746
GM
2485 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2486 || (IT_CHARPOS (*it) <= BEGV && before_p))
2487 return it->face_id;
2488
5f5c8ee5
GM
2489 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2490 pos = it->current.pos;
2491
2492 if (before_p)
c1005d06 2493 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2494 else
260a86a0
KH
2495 {
2496 if (it->what == IT_COMPOSITION)
2497 /* For composition, we must check the position after the
2498 composition. */
2499 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2500 else
c1005d06 2501 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2502 }
06a12811 2503
5f5c8ee5
GM
2504 /* Determine face for CHARSET_ASCII, or unibyte. */
2505 face_id = face_at_buffer_position (it->w,
2506 CHARPOS (pos),
2507 it->region_beg_charpos,
2508 it->region_end_charpos,
2509 &next_check_charpos,
2510 limit, 0);
2511
2512 /* Correct the face for charsets different from ASCII. Do it
2513 for the multibyte case only. The face returned above is
2514 suitable for unibyte text if current_buffer is unibyte. */
2515 if (it->multibyte_p)
2516 {
980806b6
KH
2517 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2518 struct face *face = FACE_FROM_ID (it->f, face_id);
2519 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2520 }
2521 }
2522
2523 return face_id;
2524}
2525
2526
2527\f
2528/***********************************************************************
2529 Invisible text
2530 ***********************************************************************/
2531
2532/* Set up iterator IT from invisible properties at its current
2533 position. Called from handle_stop. */
2534
2535static enum prop_handled
2536handle_invisible_prop (it)
2537 struct it *it;
2538{
2539 enum prop_handled handled = HANDLED_NORMALLY;
2540
2541 if (STRINGP (it->string))
2542 {
2543 extern Lisp_Object Qinvisible;
2544 Lisp_Object prop, end_charpos, limit, charpos;
2545
2546 /* Get the value of the invisible text property at the
2547 current position. Value will be nil if there is no such
2548 property. */
ac90c44f 2549 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2550 prop = Fget_text_property (charpos, Qinvisible, it->string);
2551
eadc0bf8
GM
2552 if (!NILP (prop)
2553 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2554 {
2555 handled = HANDLED_RECOMPUTE_PROPS;
2556
2557 /* Get the position at which the next change of the
2558 invisible text property can be found in IT->string.
2559 Value will be nil if the property value is the same for
2560 all the rest of IT->string. */
2561 XSETINT (limit, XSTRING (it->string)->size);
2562 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2563 it->string, limit);
2564
2565 /* Text at current position is invisible. The next
2566 change in the property is at position end_charpos.
2567 Move IT's current position to that position. */
2568 if (INTEGERP (end_charpos)
2569 && XFASTINT (end_charpos) < XFASTINT (limit))
2570 {
2571 struct text_pos old;
2572 old = it->current.string_pos;
2573 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2574 compute_string_pos (&it->current.string_pos, old, it->string);
2575 }
2576 else
2577 {
2578 /* The rest of the string is invisible. If this is an
2579 overlay string, proceed with the next overlay string
2580 or whatever comes and return a character from there. */
2581 if (it->current.overlay_string_index >= 0)
2582 {
2583 next_overlay_string (it);
2584 /* Don't check for overlay strings when we just
2585 finished processing them. */
2586 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2587 }
2588 else
2589 {
2590 struct Lisp_String *s = XSTRING (it->string);
2591 IT_STRING_CHARPOS (*it) = s->size;
2592 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2593 }
2594 }
2595 }
2596 }
2597 else
2598 {
8580a4e3 2599 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 2600 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
2601
2602 /* First of all, is there invisible text at this position? */
5a08cbaf 2603 start_charpos = IT_CHARPOS (*it);
ac90c44f 2604 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
2605 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2606 &overlay);
8580a4e3
SM
2607 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2608
5f5c8ee5 2609 /* If we are on invisible text, skip over it. */
8580a4e3 2610 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2611 {
2612 /* Record whether we have to display an ellipsis for the
2613 invisible text. */
8580a4e3 2614 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
2615
2616 handled = HANDLED_RECOMPUTE_PROPS;
2617
2618 /* Loop skipping over invisible text. The loop is left at
2619 ZV or with IT on the first char being visible again. */
2620 do
2621 {
2622 /* Try to skip some invisible text. Return value is the
2623 position reached which can be equal to IT's position
2624 if there is nothing invisible here. This skips both
2625 over invisible text properties and overlays with
2626 invisible property. */
2627 newpos = skip_invisible (IT_CHARPOS (*it),
2628 &next_stop, ZV, it->window);
2629
2630 /* If we skipped nothing at all we weren't at invisible
2631 text in the first place. If everything to the end of
2632 the buffer was skipped, end the loop. */
2633 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 2634 invis_p = 0;
5f5c8ee5
GM
2635 else
2636 {
2637 /* We skipped some characters but not necessarily
2638 all there are. Check if we ended up on visible
2639 text. Fget_char_property returns the property of
2640 the char before the given position, i.e. if we
8580a4e3 2641 get invis_p = 0, this means that the char at
5f5c8ee5 2642 newpos is visible. */
ac90c44f 2643 pos = make_number (newpos);
5f5c8ee5 2644 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 2645 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5
GM
2646 }
2647
2648 /* If we ended up on invisible text, proceed to
2649 skip starting with next_stop. */
8580a4e3 2650 if (invis_p)
5f5c8ee5
GM
2651 IT_CHARPOS (*it) = next_stop;
2652 }
8580a4e3 2653 while (invis_p);
5a08cbaf 2654
5f5c8ee5
GM
2655 /* The position newpos is now either ZV or on visible text. */
2656 IT_CHARPOS (*it) = newpos;
2657 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2658
5a08cbaf
GM
2659 /* If there are before-strings at the start of invisible
2660 text, and the text is invisible because of a text
2661 property, arrange to show before-strings because 20.x did
2662 it that way. (If the text is invisible because of an
2663 overlay property instead of a text property, this is
2664 already handled in the overlay code.) */
2665 if (NILP (overlay)
2666 && get_overlay_strings (it, start_charpos))
5f5c8ee5 2667 {
5a08cbaf
GM
2668 handled = HANDLED_RECOMPUTE_PROPS;
2669 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 2670 }
5a08cbaf
GM
2671 else if (display_ellipsis_p)
2672 setup_for_ellipsis (it);
5f5c8ee5
GM
2673 }
2674 }
2675
2676 return handled;
2677}
2678
2679
5a08cbaf
GM
2680/* Make iterator IT return `...' next. */
2681
2682static void
2683setup_for_ellipsis (it)
2684 struct it *it;
2685{
2686 if (it->dp
2687 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2688 {
2689 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2690 it->dpvec = v->contents;
2691 it->dpend = v->contents + v->size;
2692 }
2693 else
2694 {
2695 /* Default `...'. */
2696 it->dpvec = default_invis_vector;
2697 it->dpend = default_invis_vector + 3;
2698 }
2699
2700 /* The ellipsis display does not replace the display of the
2701 character at the new position. Indicate this by setting
2702 IT->dpvec_char_len to zero. */
2703 it->dpvec_char_len = 0;
2704
2705 it->current.dpvec_index = 0;
2706 it->method = next_element_from_display_vector;
2707}
2708
2709
5f5c8ee5
GM
2710\f
2711/***********************************************************************
2712 'display' property
2713 ***********************************************************************/
2714
2715/* Set up iterator IT from `display' property at its current position.
2716 Called from handle_stop. */
2717
2718static enum prop_handled
2719handle_display_prop (it)
2720 struct it *it;
2721{
2722 Lisp_Object prop, object;
2723 struct text_pos *position;
a61b7058 2724 int display_replaced_p = 0;
5f5c8ee5
GM
2725
2726 if (STRINGP (it->string))
2727 {
2728 object = it->string;
2729 position = &it->current.string_pos;
2730 }
2731 else
2732 {
221dd3e7 2733 object = it->w->buffer;
5f5c8ee5
GM
2734 position = &it->current.pos;
2735 }
2736
2737 /* Reset those iterator values set from display property values. */
2738 it->font_height = Qnil;
2739 it->space_width = Qnil;
2740 it->voffset = 0;
2741
2742 /* We don't support recursive `display' properties, i.e. string
2743 values that have a string `display' property, that have a string
2744 `display' property etc. */
2745 if (!it->string_from_display_prop_p)
2746 it->area = TEXT_AREA;
2747
2748 prop = Fget_char_property (make_number (position->charpos),
2749 Qdisplay, object);
2750 if (NILP (prop))
2751 return HANDLED_NORMALLY;
2752
f3751a65 2753 if (CONSP (prop)
12700f40
GM
2754 /* Simple properties. */
2755 && !EQ (XCAR (prop), Qimage)
2756 && !EQ (XCAR (prop), Qspace)
2757 && !EQ (XCAR (prop), Qwhen)
2758 && !EQ (XCAR (prop), Qspace_width)
2759 && !EQ (XCAR (prop), Qheight)
2760 && !EQ (XCAR (prop), Qraise)
2761 /* Marginal area specifications. */
2762 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2763 && !NILP (XCAR (prop)))
5f5c8ee5 2764 {
a61b7058 2765 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 2766 {
a61b7058
GM
2767 if (handle_single_display_prop (it, XCAR (prop), object,
2768 position, display_replaced_p))
2769 display_replaced_p = 1;
5f5c8ee5
GM
2770 }
2771 }
2772 else if (VECTORP (prop))
2773 {
2774 int i;
a61b7058
GM
2775 for (i = 0; i < ASIZE (prop); ++i)
2776 if (handle_single_display_prop (it, AREF (prop, i), object,
2777 position, display_replaced_p))
2778 display_replaced_p = 1;
5f5c8ee5
GM
2779 }
2780 else
2781 {
a61b7058
GM
2782 if (handle_single_display_prop (it, prop, object, position, 0))
2783 display_replaced_p = 1;
5f5c8ee5
GM
2784 }
2785
a61b7058 2786 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
2787}
2788
2789
6c577098 2790/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2791 at START_POS in OBJECT. */
2792
2793static struct text_pos
2794display_prop_end (it, object, start_pos)
2795 struct it *it;
2796 Lisp_Object object;
2797 struct text_pos start_pos;
2798{
2799 Lisp_Object end;
2800 struct text_pos end_pos;
5f5c8ee5 2801
016b5642
MB
2802 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2803 Qdisplay, object, Qnil);
6c577098
GM
2804 CHARPOS (end_pos) = XFASTINT (end);
2805 if (STRINGP (object))
5f5c8ee5
GM
2806 compute_string_pos (&end_pos, start_pos, it->string);
2807 else
6c577098 2808 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2809
2810 return end_pos;
2811}
2812
2813
2814/* Set up IT from a single `display' sub-property value PROP. OBJECT
2815 is the object in which the `display' property was found. *POSITION
a61b7058
GM
2816 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2817 means that we previously saw a display sub-property which already
2818 replaced text display with something else, for example an image;
2819 ignore such properties after the first one has been processed.
5f5c8ee5
GM
2820
2821 If PROP is a `space' or `image' sub-property, set *POSITION to the
2822 end position of the `display' property.
2823
4b41cebb 2824 Value is non-zero if something was found which replaces the display
a61b7058 2825 of buffer or string text. */
5f5c8ee5
GM
2826
2827static int
a61b7058
GM
2828handle_single_display_prop (it, prop, object, position,
2829 display_replaced_before_p)
5f5c8ee5
GM
2830 struct it *it;
2831 Lisp_Object prop;
2832 Lisp_Object object;
2833 struct text_pos *position;
a61b7058 2834 int display_replaced_before_p;
5f5c8ee5
GM
2835{
2836 Lisp_Object value;
a61b7058 2837 int replaces_text_display_p = 0;
5f5c8ee5
GM
2838 Lisp_Object form;
2839
d3acf96b 2840 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 2841 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2842 form = Qt;
d3acf96b 2843 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2844 {
2845 prop = XCDR (prop);
2846 if (!CONSP (prop))
2847 return 0;
2848 form = XCAR (prop);
2849 prop = XCDR (prop);
5f5c8ee5
GM
2850 }
2851
2852 if (!NILP (form) && !EQ (form, Qt))
2853 {
b384d6f8 2854 int count = BINDING_STACK_SIZE ();
5f5c8ee5 2855 struct gcpro gcpro1;
5f5c8ee5 2856
b384d6f8
GM
2857 /* Bind `object' to the object having the `display' property, a
2858 buffer or string. Bind `position' to the position in the
2859 object where the property was found, and `buffer-position'
2860 to the current position in the buffer. */
2861 specbind (Qobject, object);
31ac723b
SM
2862 specbind (Qposition, make_number (CHARPOS (*position)));
2863 specbind (Qbuffer_position,
2864 make_number (STRINGP (object)
2865 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 2866 GCPRO1 (form);
116d6f5c 2867 form = safe_eval (form);
b384d6f8
GM
2868 UNGCPRO;
2869 unbind_to (count, Qnil);
5f5c8ee5
GM
2870 }
2871
2872 if (NILP (form))
2873 return 0;
2874
2875 if (CONSP (prop)
2876 && EQ (XCAR (prop), Qheight)
2877 && CONSP (XCDR (prop)))
2878 {
e4093f38 2879 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5
GM
2880 return 0;
2881
2882 /* `(height HEIGHT)'. */
2883 it->font_height = XCAR (XCDR (prop));
2884 if (!NILP (it->font_height))
2885 {
2886 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2887 int new_height = -1;
2888
2889 if (CONSP (it->font_height)
2890 && (EQ (XCAR (it->font_height), Qplus)
2891 || EQ (XCAR (it->font_height), Qminus))
2892 && CONSP (XCDR (it->font_height))
2893 && INTEGERP (XCAR (XCDR (it->font_height))))
2894 {
2895 /* `(+ N)' or `(- N)' where N is an integer. */
2896 int steps = XINT (XCAR (XCDR (it->font_height)));
2897 if (EQ (XCAR (it->font_height), Qplus))
2898 steps = - steps;
2899 it->face_id = smaller_face (it->f, it->face_id, steps);
2900 }
0d8b31c0 2901 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2902 {
2903 /* Call function with current height as argument.
2904 Value is the new height. */
116d6f5c
GM
2905 Lisp_Object height;
2906 height = safe_call1 (it->font_height,
2907 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2908 if (NUMBERP (height))
2909 new_height = XFLOATINT (height);
5f5c8ee5
GM
2910 }
2911 else if (NUMBERP (it->font_height))
2912 {
2913 /* Value is a multiple of the canonical char height. */
2914 struct face *face;
2915
2916 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2917 new_height = (XFLOATINT (it->font_height)
2918 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2919 }
2920 else
2921 {
2922 /* Evaluate IT->font_height with `height' bound to the
2923 current specified height to get the new height. */
2924 Lisp_Object value;
2d27dae2 2925 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
2926
2927 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2928 value = safe_eval (it->font_height);
5f5c8ee5
GM
2929 unbind_to (count, Qnil);
2930
2931 if (NUMBERP (value))
2932 new_height = XFLOATINT (value);
2933 }
2934
2935 if (new_height > 0)
2936 it->face_id = face_with_height (it->f, it->face_id, new_height);
2937 }
2938 }
2939 else if (CONSP (prop)
2940 && EQ (XCAR (prop), Qspace_width)
2941 && CONSP (XCDR (prop)))
2942 {
2943 /* `(space_width WIDTH)'. */
e4093f38 2944 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2945 return 0;
5f5c8ee5
GM
2946
2947 value = XCAR (XCDR (prop));
2948 if (NUMBERP (value) && XFLOATINT (value) > 0)
2949 it->space_width = value;
2950 }
2951 else if (CONSP (prop)
2952 && EQ (XCAR (prop), Qraise)
2953 && CONSP (XCDR (prop)))
2954 {
5f5c8ee5 2955 /* `(raise FACTOR)'. */
e4093f38 2956 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2957 return 0;
5f5c8ee5 2958
fb3842a8 2959#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2960 value = XCAR (XCDR (prop));
2961 if (NUMBERP (value))
2962 {
2963 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2964 it->voffset = - (XFLOATINT (value)
1ab3e082 2965 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2966 }
2967#endif /* HAVE_WINDOW_SYSTEM */
2968 }
2969 else if (!it->string_from_display_prop_p)
2970 {
f3751a65 2971 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 2972 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2973 Lisp_Object location, value;
2974 struct text_pos start_pos;
2975 int valid_p;
2976
2977 /* Characters having this form of property are not displayed, so
2978 we have to find the end of the property. */
5f5c8ee5
GM
2979 start_pos = *position;
2980 *position = display_prop_end (it, object, start_pos);
15e26c76 2981 value = Qnil;
5f5c8ee5
GM
2982
2983 /* Let's stop at the new position and assume that all
2984 text properties change there. */
2985 it->stop_charpos = position->charpos;
2986
f3751a65
GM
2987 location = Qunbound;
2988 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2989 {
f3751a65
GM
2990 Lisp_Object tem;
2991
5f5c8ee5 2992 value = XCDR (prop);
f3751a65
GM
2993 if (CONSP (value))
2994 value = XCAR (value);
2995
2996 tem = XCAR (prop);
2997 if (EQ (XCAR (tem), Qmargin)
2998 && (tem = XCDR (tem),
2999 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3000 (NILP (tem)
3001 || EQ (tem, Qleft_margin)
3002 || EQ (tem, Qright_margin))))
3003 location = tem;
5f5c8ee5 3004 }
f3751a65
GM
3005
3006 if (EQ (location, Qunbound))
5f5c8ee5
GM
3007 {
3008 location = Qnil;
3009 value = prop;
3010 }
3011
3012#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 3013 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
3014 valid_p = STRINGP (value);
3015 else
3016 valid_p = (STRINGP (value)
3017 || (CONSP (value) && EQ (XCAR (value), Qspace))
3018 || valid_image_p (value));
3019#else /* not HAVE_WINDOW_SYSTEM */
3020 valid_p = STRINGP (value);
3021#endif /* not HAVE_WINDOW_SYSTEM */
3022
3023 if ((EQ (location, Qleft_margin)
3024 || EQ (location, Qright_margin)
3025 || NILP (location))
a61b7058
GM
3026 && valid_p
3027 && !display_replaced_before_p)
5f5c8ee5 3028 {
a61b7058 3029 replaces_text_display_p = 1;
a21a3943 3030
5f5c8ee5
GM
3031 /* Save current settings of IT so that we can restore them
3032 when we are finished with the glyph property value. */
3033 push_it (it);
3034
3035 if (NILP (location))
3036 it->area = TEXT_AREA;
3037 else if (EQ (location, Qleft_margin))
3038 it->area = LEFT_MARGIN_AREA;
3039 else
3040 it->area = RIGHT_MARGIN_AREA;
3041
3042 if (STRINGP (value))
3043 {
3044 it->string = value;
3045 it->multibyte_p = STRING_MULTIBYTE (it->string);
3046 it->current.overlay_string_index = -1;
3047 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3048 it->end_charpos = it->string_nchars
3049 = XSTRING (it->string)->size;
3050 it->method = next_element_from_string;
3051 it->stop_charpos = 0;
3052 it->string_from_display_prop_p = 1;
e187cf71
GM
3053 /* Say that we haven't consumed the characters with
3054 `display' property yet. The call to pop_it in
3055 set_iterator_to_next will clean this up. */
3056 *position = start_pos;
5f5c8ee5
GM
3057 }
3058 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3059 {
3060 it->method = next_element_from_stretch;
3061 it->object = value;
3062 it->current.pos = it->position = start_pos;
3063 }
3064#ifdef HAVE_WINDOW_SYSTEM
3065 else
3066 {
3067 it->what = IT_IMAGE;
3068 it->image_id = lookup_image (it->f, value);
3069 it->position = start_pos;
3070 it->object = NILP (object) ? it->w->buffer : object;
3071 it->method = next_element_from_image;
3072
02513cdd 3073 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3074 `display' property yet. The call to pop_it in
3075 set_iterator_to_next will clean this up. */
3076 *position = start_pos;
3077 }
3078#endif /* HAVE_WINDOW_SYSTEM */
3079 }
a21a3943
GM
3080 else
3081 /* Invalid property or property not supported. Restore
3082 the position to what it was before. */
3083 *position = start_pos;
5f5c8ee5
GM
3084 }
3085
a61b7058 3086 return replaces_text_display_p;
5f5c8ee5
GM
3087}
3088
3089
06568bbf
GM
3090/* Check if PROP is a display sub-property value whose text should be
3091 treated as intangible. */
3092
3093static int
3094single_display_prop_intangible_p (prop)
3095 Lisp_Object prop;
3096{
3097 /* Skip over `when FORM'. */
3098 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3099 {
3100 prop = XCDR (prop);
3101 if (!CONSP (prop))
3102 return 0;
3103 prop = XCDR (prop);
3104 }
3105
3106 if (!CONSP (prop))
3107 return 0;
3108
3109 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3110 we don't need to treat text as intangible. */
3111 if (EQ (XCAR (prop), Qmargin))
3112 {
3113 prop = XCDR (prop);
3114 if (!CONSP (prop))
3115 return 0;
3116
3117 prop = XCDR (prop);
3118 if (!CONSP (prop)
3119 || EQ (XCAR (prop), Qleft_margin)
3120 || EQ (XCAR (prop), Qright_margin))
3121 return 0;
3122 }
3123
3124 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3125}
3126
3127
3128/* Check if PROP is a display property value whose text should be
3129 treated as intangible. */
3130
3131int
3132display_prop_intangible_p (prop)
3133 Lisp_Object prop;
3134{
3135 if (CONSP (prop)
3136 && CONSP (XCAR (prop))
3137 && !EQ (Qmargin, XCAR (XCAR (prop))))
3138 {
3139 /* A list of sub-properties. */
3140 while (CONSP (prop))
3141 {
3142 if (single_display_prop_intangible_p (XCAR (prop)))
3143 return 1;
3144 prop = XCDR (prop);
3145 }
3146 }
3147 else if (VECTORP (prop))
3148 {
3149 /* A vector of sub-properties. */
3150 int i;
a61b7058
GM
3151 for (i = 0; i < ASIZE (prop); ++i)
3152 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3153 return 1;
3154 }
3155 else
3156 return single_display_prop_intangible_p (prop);
3157
3158 return 0;
3159}
3160
74bd6d65
GM
3161
3162/* Return 1 if PROP is a display sub-property value containing STRING. */
3163
3164static int
3165single_display_prop_string_p (prop, string)
3166 Lisp_Object prop, string;
3167{
74bd6d65
GM
3168 if (EQ (string, prop))
3169 return 1;
3170
3171 /* Skip over `when FORM'. */
3172 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3173 {
3174 prop = XCDR (prop);
3175 if (!CONSP (prop))
3176 return 0;
3177 prop = XCDR (prop);
3178 }
3179
3180 if (CONSP (prop))
3181 /* Skip over `margin LOCATION'. */
3182 if (EQ (XCAR (prop), Qmargin))
3183 {
3184 prop = XCDR (prop);
3185 if (!CONSP (prop))
3186 return 0;
3187
3188 prop = XCDR (prop);
3189 if (!CONSP (prop))
3190 return 0;
3191 }
3192
3193 return CONSP (prop) && EQ (XCAR (prop), string);
3194}
3195
3196
3197/* Return 1 if STRING appears in the `display' property PROP. */
3198
3199static int
3200display_prop_string_p (prop, string)
3201 Lisp_Object prop, string;
3202{
74bd6d65
GM
3203 if (CONSP (prop)
3204 && CONSP (XCAR (prop))
3205 && !EQ (Qmargin, XCAR (XCAR (prop))))
3206 {
3207 /* A list of sub-properties. */
3208 while (CONSP (prop))
3209 {
3210 if (single_display_prop_string_p (XCAR (prop), string))
3211 return 1;
3212 prop = XCDR (prop);
3213 }
3214 }
3215 else if (VECTORP (prop))
3216 {
3217 /* A vector of sub-properties. */
3218 int i;
3219 for (i = 0; i < ASIZE (prop); ++i)
3220 if (single_display_prop_string_p (AREF (prop, i), string))
3221 return 1;
3222 }
3223 else
3224 return single_display_prop_string_p (prop, string);
3225
3226 return 0;
3227}
3228
3229
3230/* Determine from which buffer position in W's buffer STRING comes
3231 from. AROUND_CHARPOS is an approximate position where it could
3232 be from. Value is the buffer position or 0 if it couldn't be
3233 determined.
3234
3235 W's buffer must be current.
3236
3237 This function is necessary because we don't record buffer positions
3238 in glyphs generated from strings (to keep struct glyph small).
3239 This function may only use code that doesn't eval because it is
3240 called asynchronously from note_mouse_highlight. */
3241
3242int
3243string_buffer_position (w, string, around_charpos)
3244 struct window *w;
3245 Lisp_Object string;
3246 int around_charpos;
3247{
74bd6d65
GM
3248 Lisp_Object limit, prop, pos;
3249 const int MAX_DISTANCE = 1000;
3250 int found = 0;
3251
d8731202 3252 pos = make_number (around_charpos);
74bd6d65
GM
3253 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3254 while (!found && !EQ (pos, limit))
3255 {
3256 prop = Fget_char_property (pos, Qdisplay, Qnil);
3257 if (!NILP (prop) && display_prop_string_p (prop, string))
3258 found = 1;
3259 else
134d9283 3260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3261 }
3262
3263 if (!found)
3264 {
d8731202 3265 pos = make_number (around_charpos);
74bd6d65
GM
3266 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3267 while (!found && !EQ (pos, limit))
3268 {
3269 prop = Fget_char_property (pos, Qdisplay, Qnil);
3270 if (!NILP (prop) && display_prop_string_p (prop, string))
3271 found = 1;
3272 else
134d9283
GM
3273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3274 limit);
74bd6d65
GM
3275 }
3276 }
3277
3278 return found ? XINT (pos) : 0;
3279}
3280
3281
5f5c8ee5 3282\f
260a86a0
KH
3283/***********************************************************************
3284 `composition' property
3285 ***********************************************************************/
3286
3287/* Set up iterator IT from `composition' property at its current
3288 position. Called from handle_stop. */
3289
3290static enum prop_handled
3291handle_composition_prop (it)
3292 struct it *it;
3293{
3294 Lisp_Object prop, string;
3295 int pos, pos_byte, end;
3296 enum prop_handled handled = HANDLED_NORMALLY;
3297
3298 if (STRINGP (it->string))
3299 {
3300 pos = IT_STRING_CHARPOS (*it);
3301 pos_byte = IT_STRING_BYTEPOS (*it);
3302 string = it->string;
3303 }
3304 else
3305 {
3306 pos = IT_CHARPOS (*it);
3307 pos_byte = IT_BYTEPOS (*it);
3308 string = Qnil;
3309 }
3310
3311 /* If there's a valid composition and point is not inside of the
3312 composition (in the case that the composition is from the current
3313 buffer), draw a glyph composed from the composition components. */
3314 if (find_composition (pos, -1, &pos, &end, &prop, string)
3315 && COMPOSITION_VALID_P (pos, end, prop)
3316 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3317 {
3318 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3319
3320 if (id >= 0)
3321 {
3322 it->method = next_element_from_composition;
3323 it->cmp_id = id;
3324 it->cmp_len = COMPOSITION_LENGTH (prop);
3325 /* For a terminal, draw only the first character of the
3326 components. */
3327 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3328 it->len = (STRINGP (it->string)
3329 ? string_char_to_byte (it->string, end)
3330 : CHAR_TO_BYTE (end)) - pos_byte;
3331 it->stop_charpos = end;
3332 handled = HANDLED_RETURN;
3333 }
3334 }
3335
3336 return handled;
3337}
3338
3339
3340\f
5f5c8ee5
GM
3341/***********************************************************************
3342 Overlay strings
3343 ***********************************************************************/
3344
3345/* The following structure is used to record overlay strings for
3346 later sorting in load_overlay_strings. */
3347
3348struct overlay_entry
3349{
2970b9be 3350 Lisp_Object overlay;
5f5c8ee5
GM
3351 Lisp_Object string;
3352 int priority;
3353 int after_string_p;
3354};
3355
3356
3357/* Set up iterator IT from overlay strings at its current position.
3358 Called from handle_stop. */
3359
3360static enum prop_handled
3361handle_overlay_change (it)
3362 struct it *it;
3363{
5a08cbaf 3364 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3365 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3366 else
2970b9be 3367 return HANDLED_NORMALLY;
5f5c8ee5
GM
3368}
3369
3370
3371/* Set up the next overlay string for delivery by IT, if there is an
3372 overlay string to deliver. Called by set_iterator_to_next when the
3373 end of the current overlay string is reached. If there are more
3374 overlay strings to display, IT->string and
3375 IT->current.overlay_string_index are set appropriately here.
3376 Otherwise IT->string is set to nil. */
3377
3378static void
3379next_overlay_string (it)
3380 struct it *it;
3381{
3382 ++it->current.overlay_string_index;
3383 if (it->current.overlay_string_index == it->n_overlay_strings)
3384 {
3385 /* No more overlay strings. Restore IT's settings to what
3386 they were before overlay strings were processed, and
3387 continue to deliver from current_buffer. */
5a08cbaf
GM
3388 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3389
5f5c8ee5
GM
3390 pop_it (it);
3391 xassert (it->stop_charpos >= BEGV
3392 && it->stop_charpos <= it->end_charpos);
3393 it->string = Qnil;
3394 it->current.overlay_string_index = -1;
3395 SET_TEXT_POS (it->current.string_pos, -1, -1);
3396 it->n_overlay_strings = 0;
3397 it->method = next_element_from_buffer;
2970b9be
GM
3398
3399 /* If we're at the end of the buffer, record that we have
3400 processed the overlay strings there already, so that
3401 next_element_from_buffer doesn't try it again. */
3402 if (IT_CHARPOS (*it) >= it->end_charpos)
3403 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
3404
3405 /* If we have to display `...' for invisible text, set
3406 the iterator up for that. */
3407 if (display_ellipsis_p)
3408 setup_for_ellipsis (it);
5f5c8ee5
GM
3409 }
3410 else
3411 {
3412 /* There are more overlay strings to process. If
3413 IT->current.overlay_string_index has advanced to a position
3414 where we must load IT->overlay_strings with more strings, do
3415 it. */
3416 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3417
3418 if (it->current.overlay_string_index && i == 0)
5a08cbaf 3419 load_overlay_strings (it, 0);
5f5c8ee5
GM
3420
3421 /* Initialize IT to deliver display elements from the overlay
3422 string. */
3423 it->string = it->overlay_strings[i];
3424 it->multibyte_p = STRING_MULTIBYTE (it->string);
3425 SET_TEXT_POS (it->current.string_pos, 0, 0);
3426 it->method = next_element_from_string;
3427 it->stop_charpos = 0;
3428 }
3429
3430 CHECK_IT (it);
3431}
3432
3433
3434/* Compare two overlay_entry structures E1 and E2. Used as a
3435 comparison function for qsort in load_overlay_strings. Overlay
3436 strings for the same position are sorted so that
3437
2970b9be
GM
3438 1. All after-strings come in front of before-strings, except
3439 when they come from the same overlay.
5f5c8ee5
GM
3440
3441 2. Within after-strings, strings are sorted so that overlay strings
3442 from overlays with higher priorities come first.
3443
3444 2. Within before-strings, strings are sorted so that overlay
3445 strings from overlays with higher priorities come last.
3446
3447 Value is analogous to strcmp. */
3448
3449
3450static int
3451compare_overlay_entries (e1, e2)
3452 void *e1, *e2;
3453{
3454 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3455 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3456 int result;
3457
3458 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3459 {
3460 /* Let after-strings appear in front of before-strings if
3461 they come from different overlays. */
3462 if (EQ (entry1->overlay, entry2->overlay))
3463 result = entry1->after_string_p ? 1 : -1;
3464 else
3465 result = entry1->after_string_p ? -1 : 1;
3466 }
5f5c8ee5
GM
3467 else if (entry1->after_string_p)
3468 /* After-strings sorted in order of decreasing priority. */
3469 result = entry2->priority - entry1->priority;
3470 else
3471 /* Before-strings sorted in order of increasing priority. */
3472 result = entry1->priority - entry2->priority;
3473
3474 return result;
3475}
3476
3477
3478/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
3479 current buffer position, or from CHARPOS if that is > 0. Set
3480 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
3481
3482 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3483 a time. On entry into load_overlay_strings,
3484 IT->current.overlay_string_index gives the number of overlay
3485 strings that have already been loaded by previous calls to this
3486 function.
3487
2970b9be
GM
3488 IT->add_overlay_start contains an additional overlay start
3489 position to consider for taking overlay strings from, if non-zero.
3490 This position comes into play when the overlay has an `invisible'
3491 property, and both before and after-strings. When we've skipped to
3492 the end of the overlay, because of its `invisible' property, we
3493 nevertheless want its before-string to appear.
3494 IT->add_overlay_start will contain the overlay start position
3495 in this case.
3496
5f5c8ee5
GM
3497 Overlay strings are sorted so that after-string strings come in
3498 front of before-string strings. Within before and after-strings,
3499 strings are sorted by overlay priority. See also function
3500 compare_overlay_entries. */
3501
3502static void
5a08cbaf 3503load_overlay_strings (it, charpos)
5f5c8ee5 3504 struct it *it;
5a08cbaf 3505 int charpos;
5f5c8ee5
GM
3506{
3507 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3508 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3509 int start, end;
3510 int size = 20;
cafafe0b 3511 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3512 struct overlay_entry *entries
3513 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
3514
3515 if (charpos <= 0)
3516 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3517
3518 /* Append the overlay string STRING of overlay OVERLAY to vector
3519 `entries' which has size `size' and currently contains `n'
3520 elements. AFTER_P non-zero means STRING is an after-string of
3521 OVERLAY. */
3522#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3523 do \
3524 { \
3525 Lisp_Object priority; \
3526 \
3527 if (n == size) \
3528 { \
3529 int new_size = 2 * size; \
3530 struct overlay_entry *old = entries; \
3531 entries = \
3532 (struct overlay_entry *) alloca (new_size \
3533 * sizeof *entries); \
3534 bcopy (old, entries, size * sizeof *entries); \
3535 size = new_size; \
3536 } \
3537 \
3538 entries[n].string = (STRING); \
2970b9be 3539 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3540 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3541 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3542 entries[n].after_string_p = (AFTER_P); \
3543 ++n; \
3544 } \
3545 while (0)
3546
3547 /* Process overlay before the overlay center. */
2970b9be 3548 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3549 {
9472f927 3550 overlay = XCAR (ov);
5f5c8ee5
GM
3551 xassert (OVERLAYP (overlay));
3552 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3553 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3554
cafafe0b 3555 if (end < charpos)
5f5c8ee5
GM
3556 break;
3557
3558 /* Skip this overlay if it doesn't start or end at IT's current
3559 position. */
cafafe0b 3560 if (end != charpos && start != charpos)
5f5c8ee5
GM
3561 continue;
3562
3563 /* Skip this overlay if it doesn't apply to IT->w. */
3564 window = Foverlay_get (overlay, Qwindow);
3565 if (WINDOWP (window) && XWINDOW (window) != it->w)
3566 continue;
3567
cafafe0b
GM
3568 /* If the text ``under'' the overlay is invisible, both before-
3569 and after-strings from this overlay are visible; start and
3570 end position are indistinguishable. */
3571 invisible = Foverlay_get (overlay, Qinvisible);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3573
5f5c8ee5 3574 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3575 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3576 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3577 && XSTRING (str)->size)
3578 RECORD_OVERLAY_STRING (overlay, str, 0);
3579
3580 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3581 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3582 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3583 && XSTRING (str)->size)
3584 RECORD_OVERLAY_STRING (overlay, str, 1);
3585 }
3586
3587 /* Process overlays after the overlay center. */
2970b9be 3588 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3589 {
9472f927 3590 overlay = XCAR (ov);
5f5c8ee5
GM
3591 xassert (OVERLAYP (overlay));
3592 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3593 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3594
cafafe0b 3595 if (start > charpos)
5f5c8ee5
GM
3596 break;
3597
3598 /* Skip this overlay if it doesn't start or end at IT's current
3599 position. */
cafafe0b 3600 if (end != charpos && start != charpos)
5f5c8ee5
GM
3601 continue;
3602
3603 /* Skip this overlay if it doesn't apply to IT->w. */
3604 window = Foverlay_get (overlay, Qwindow);
3605 if (WINDOWP (window) && XWINDOW (window) != it->w)
3606 continue;
3607
cafafe0b
GM
3608 /* If the text ``under'' the overlay is invisible, it has a zero
3609 dimension, and both before- and after-strings apply. */
3610 invisible = Foverlay_get (overlay, Qinvisible);
3611 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3612
5f5c8ee5 3613 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3614 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3615 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3616 && XSTRING (str)->size)
3617 RECORD_OVERLAY_STRING (overlay, str, 0);
3618
3619 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3620 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3621 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3622 && XSTRING (str)->size)
3623 RECORD_OVERLAY_STRING (overlay, str, 1);
3624 }
3625
3626#undef RECORD_OVERLAY_STRING
3627
3628 /* Sort entries. */
cafafe0b 3629 if (n > 1)
2970b9be 3630 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3631
3632 /* Record the total number of strings to process. */
3633 it->n_overlay_strings = n;
3634
3635 /* IT->current.overlay_string_index is the number of overlay strings
3636 that have already been consumed by IT. Copy some of the
3637 remaining overlay strings to IT->overlay_strings. */
3638 i = 0;
3639 j = it->current.overlay_string_index;
3640 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3641 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3642
5f5c8ee5
GM
3643 CHECK_IT (it);
3644}
3645
3646
3647/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
3648 position, or at CHARPOS if that is > 0. Value is non-zero if at
3649 least one overlay string was found. */
5f5c8ee5
GM
3650
3651static int
5a08cbaf 3652get_overlay_strings (it, charpos)
5f5c8ee5 3653 struct it *it;
5a08cbaf 3654 int charpos;
5f5c8ee5
GM
3655{
3656 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3657 process. This fills IT->overlay_strings with strings, and sets
3658 IT->n_overlay_strings to the total number of strings to process.
3659 IT->pos.overlay_string_index has to be set temporarily to zero
3660 because load_overlay_strings needs this; it must be set to -1
3661 when no overlay strings are found because a zero value would
3662 indicate a position in the first overlay string. */
3663 it->current.overlay_string_index = 0;
5a08cbaf 3664 load_overlay_strings (it, charpos);
5f5c8ee5
GM
3665
3666 /* If we found overlay strings, set up IT to deliver display
3667 elements from the first one. Otherwise set up IT to deliver
3668 from current_buffer. */
3669 if (it->n_overlay_strings)
3670 {
3671 /* Make sure we know settings in current_buffer, so that we can
3672 restore meaningful values when we're done with the overlay
3673 strings. */
3674 compute_stop_pos (it);
3675 xassert (it->face_id >= 0);
3676
3677 /* Save IT's settings. They are restored after all overlay
3678 strings have been processed. */
3679 xassert (it->sp == 0);
3680 push_it (it);
3681
3682 /* Set up IT to deliver display elements from the first overlay
3683 string. */
3684 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 3685 it->string = it->overlay_strings[0];
b2046df8
GM
3686 it->stop_charpos = 0;
3687 it->end_charpos = XSTRING (it->string)->size;
5f5c8ee5
GM
3688 it->multibyte_p = STRING_MULTIBYTE (it->string);
3689 xassert (STRINGP (it->string));
3690 it->method = next_element_from_string;
3691 }
3692 else
3693 {
3694 it->string = Qnil;
3695 it->current.overlay_string_index = -1;
3696 it->method = next_element_from_buffer;
3697 }
3698
3699 CHECK_IT (it);
3700
3701 /* Value is non-zero if we found at least one overlay string. */
3702 return STRINGP (it->string);
3703}
3704
3705
3706\f
3707/***********************************************************************
3708 Saving and restoring state
3709 ***********************************************************************/
3710
3711/* Save current settings of IT on IT->stack. Called, for example,
3712 before setting up IT for an overlay string, to be able to restore
3713 IT's settings to what they were after the overlay string has been
3714 processed. */
3715
3716static void
3717push_it (it)
3718 struct it *it;
3719{
3720 struct iterator_stack_entry *p;
3721
3722 xassert (it->sp < 2);
3723 p = it->stack + it->sp;
3724
3725 p->stop_charpos = it->stop_charpos;
3726 xassert (it->face_id >= 0);
3727 p->face_id = it->face_id;
3728 p->string = it->string;
3729 p->pos = it->current;
3730 p->end_charpos = it->end_charpos;
3731 p->string_nchars = it->string_nchars;
3732 p->area = it->area;
3733 p->multibyte_p = it->multibyte_p;
3734 p->space_width = it->space_width;
3735 p->font_height = it->font_height;
3736 p->voffset = it->voffset;
3737 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 3738 p->display_ellipsis_p = 0;
5f5c8ee5
GM
3739 ++it->sp;
3740}
3741
3742
3743/* Restore IT's settings from IT->stack. Called, for example, when no
3744 more overlay strings must be processed, and we return to delivering
3745 display elements from a buffer, or when the end of a string from a
3746 `display' property is reached and we return to delivering display
3747 elements from an overlay string, or from a buffer. */
3748
3749static void
3750pop_it (it)
3751 struct it *it;
3752{
3753 struct iterator_stack_entry *p;
3754
3755 xassert (it->sp > 0);
3756 --it->sp;
3757 p = it->stack + it->sp;
3758 it->stop_charpos = p->stop_charpos;
3759 it->face_id = p->face_id;
3760 it->string = p->string;
3761 it->current = p->pos;
3762 it->end_charpos = p->end_charpos;
3763 it->string_nchars = p->string_nchars;
3764 it->area = p->area;
3765 it->multibyte_p = p->multibyte_p;
3766 it->space_width = p->space_width;
3767 it->font_height = p->font_height;
3768 it->voffset = p->voffset;
3769 it->string_from_display_prop_p = p->string_from_display_prop_p;
3770}
3771
3772
3773\f
3774/***********************************************************************
3775 Moving over lines
3776 ***********************************************************************/
3777
3778/* Set IT's current position to the previous line start. */
3779
3780static void
3781back_to_previous_line_start (it)
3782 struct it *it;
3783{
3784 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3785 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3786}
3787
3788
cafafe0b
GM
3789/* Move IT to the next line start.
3790
3791 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3792 we skipped over part of the text (as opposed to moving the iterator
3793 continuously over the text). Otherwise, don't change the value
3794 of *SKIPPED_P.
3795
3796 Newlines may come from buffer text, overlay strings, or strings
3797 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3798 simply use find_next_newline_no_quit.
3799
3800 Note that this function may not skip over invisible text that is so
3801 because of text properties and immediately follows a newline. If
3802 it would, function reseat_at_next_visible_line_start, when called
3803 from set_iterator_to_next, would effectively make invisible
3804 characters following a newline part of the wrong glyph row, which
3805 leads to wrong cursor motion. */
5f5c8ee5 3806
cafafe0b
GM
3807static int
3808forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3809 struct it *it;
cafafe0b 3810 int *skipped_p;
5f5c8ee5 3811{
54918e2b 3812 int old_selective, newline_found_p, n;
cafafe0b
GM
3813 const int MAX_NEWLINE_DISTANCE = 500;
3814
0fd37545
GM
3815 /* If already on a newline, just consume it to avoid unintended
3816 skipping over invisible text below. */
51695746
GM
3817 if (it->what == IT_CHARACTER
3818 && it->c == '\n'
3819 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
3820 {
3821 set_iterator_to_next (it, 0);
a77dc1ec 3822 it->c = 0;
0fd37545
GM
3823 return 1;
3824 }
3825
54918e2b 3826 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3827 unnecessary because it's done by the caller, and (b) leads to an
3828 infinite recursion because next_element_from_ellipsis indirectly
3829 calls this function. */
54918e2b
GM
3830 old_selective = it->selective;
3831 it->selective = 0;
3832
cafafe0b
GM
3833 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3834 from buffer text. */
3aec8722
GM
3835 for (n = newline_found_p = 0;
3836 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3837 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3838 {
8692ca92
GM
3839 if (!get_next_display_element (it))
3840 break;
d02f1cb8 3841 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3842 set_iterator_to_next (it, 0);
cafafe0b
GM
3843 }
3844
3845 /* If we didn't find a newline near enough, see if we can use a
3846 short-cut. */
3aec8722 3847 if (n == MAX_NEWLINE_DISTANCE)
cafafe0b
GM
3848 {
3849 int start = IT_CHARPOS (*it);
3850 int limit = find_next_newline_no_quit (start, 1);
3851 Lisp_Object pos;
3852
3853 xassert (!STRINGP (it->string));
3854
3855 /* If there isn't any `display' property in sight, and no
3856 overlays, we can just use the position of the newline in
3857 buffer text. */
3858 if (it->stop_charpos >= limit
3859 || ((pos = Fnext_single_property_change (make_number (start),
3860 Qdisplay,
3861 Qnil, make_number (limit)),
3862 NILP (pos))
3863 && next_overlay_change (start) == ZV))
3864 {
3865 IT_CHARPOS (*it) = limit;
3866 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3867 *skipped_p = newline_found_p = 1;
3868 }
3869 else
3870 {
3871 while (get_next_display_element (it)
3872 && !newline_found_p)
3873 {
3874 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3875 set_iterator_to_next (it, 0);
3876 }
3877 }
3878 }
3879
54918e2b 3880 it->selective = old_selective;
cafafe0b 3881 return newline_found_p;
5f5c8ee5
GM
3882}
3883
3884
3885/* Set IT's current position to the previous visible line start. Skip
3886 invisible text that is so either due to text properties or due to
3887 selective display. Caution: this does not change IT->current_x and
3888 IT->hpos. */
3889
3890static void
3891back_to_previous_visible_line_start (it)
3892 struct it *it;
3893{
3894 int visible_p = 0;
3895
3896 /* Go back one newline if not on BEGV already. */
3897 if (IT_CHARPOS (*it) > BEGV)
3898 back_to_previous_line_start (it);
3899
3900 /* Move over lines that are invisible because of selective display
3901 or text properties. */
3902 while (IT_CHARPOS (*it) > BEGV
3903 && !visible_p)
3904 {
3905 visible_p = 1;
3906
3907 /* If selective > 0, then lines indented more than that values
3908 are invisible. */
3909 if (it->selective > 0
3910 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3911 it->selective))
3912 visible_p = 0;
5f5c8ee5
GM
3913 else
3914 {
3915 Lisp_Object prop;
3916
6fc556fd
KR
3917 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3918 Qinvisible, it->window);
5f5c8ee5
GM
3919 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3920 visible_p = 0;
3921 }
5f5c8ee5
GM
3922
3923 /* Back one more newline if the current one is invisible. */
3924 if (!visible_p)
3925 back_to_previous_line_start (it);
3926 }
3927
3928 xassert (IT_CHARPOS (*it) >= BEGV);
3929 xassert (IT_CHARPOS (*it) == BEGV
3930 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3931 CHECK_IT (it);
3932}
3933
3934
3935/* Reseat iterator IT at the previous visible line start. Skip
3936 invisible text that is so either due to text properties or due to
3937 selective display. At the end, update IT's overlay information,
3938 face information etc. */
3939
3940static void
3941reseat_at_previous_visible_line_start (it)
3942 struct it *it;
3943{
3944 back_to_previous_visible_line_start (it);
3945 reseat (it, it->current.pos, 1);
3946 CHECK_IT (it);
3947}
3948
3949
3950/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3951 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3952 preceding the line start. Skip over invisible text that is so
3953 because of selective display. Compute faces, overlays etc at the
3954 new position. Note that this function does not skip over text that
3955 is invisible because of text properties. */
5f5c8ee5
GM
3956
3957static void
312246d1 3958reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3959 struct it *it;
312246d1 3960 int on_newline_p;
5f5c8ee5 3961{
cafafe0b
GM
3962 int newline_found_p, skipped_p = 0;
3963
3964 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3965
3966 /* Skip over lines that are invisible because they are indented
3967 more than the value of IT->selective. */
3968 if (it->selective > 0)
3969 while (IT_CHARPOS (*it) < ZV
a77dc1ec 3970 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
cafafe0b 3971 it->selective))
a77dc1ec
GM
3972 {
3973 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3974 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3975 }
cafafe0b
GM
3976
3977 /* Position on the newline if that's what's requested. */
3978 if (on_newline_p && newline_found_p)
5f5c8ee5 3979 {
cafafe0b 3980 if (STRINGP (it->string))
5f5c8ee5 3981 {
cafafe0b
GM
3982 if (IT_STRING_CHARPOS (*it) > 0)
3983 {
3984 --IT_STRING_CHARPOS (*it);
3985 --IT_STRING_BYTEPOS (*it);
3986 }
5f5c8ee5 3987 }
cafafe0b 3988 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
3989 {
3990 --IT_CHARPOS (*it);
cafafe0b
GM
3991 --IT_BYTEPOS (*it);
3992 reseat (it, it->current.pos, 0);
312246d1 3993 }
5f5c8ee5 3994 }
cafafe0b
GM
3995 else if (skipped_p)
3996 reseat (it, it->current.pos, 0);
5f5c8ee5
GM
3997
3998 CHECK_IT (it);
3999}
4000
4001
4002\f
4003/***********************************************************************
4004 Changing an iterator's position
4005***********************************************************************/
4006
4007/* Change IT's current position to POS in current_buffer. If FORCE_P
4008 is non-zero, always check for text properties at the new position.
4009 Otherwise, text properties are only looked up if POS >=
4010 IT->check_charpos of a property. */
4011
4012static void
4013reseat (it, pos, force_p)
4014 struct it *it;
4015 struct text_pos pos;
4016 int force_p;
4017{
4018 int original_pos = IT_CHARPOS (*it);
4019
4020 reseat_1 (it, pos, 0);
4021
4022 /* Determine where to check text properties. Avoid doing it
4023 where possible because text property lookup is very expensive. */
4024 if (force_p
4025 || CHARPOS (pos) > it->stop_charpos
4026 || CHARPOS (pos) < original_pos)
4027 handle_stop (it);
4028
4029 CHECK_IT (it);
4030}
4031
4032
4033/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4034 IT->stop_pos to POS, also. */
4035
4036static void
4037reseat_1 (it, pos, set_stop_p)
4038 struct it *it;
4039 struct text_pos pos;
4040 int set_stop_p;
4041{
4042 /* Don't call this function when scanning a C string. */
4043 xassert (it->s == NULL);
4044
4045 /* POS must be a reasonable value. */
4046 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4047
4048 it->current.pos = it->position = pos;
4049 XSETBUFFER (it->object, current_buffer);
78c663d8 4050 it->end_charpos = ZV;
5f5c8ee5
GM
4051 it->dpvec = NULL;
4052 it->current.dpvec_index = -1;
4053 it->current.overlay_string_index = -1;
4054 IT_STRING_CHARPOS (*it) = -1;
4055 IT_STRING_BYTEPOS (*it) = -1;
4056 it->string = Qnil;
4057 it->method = next_element_from_buffer;
06fd3792 4058 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4059 it->sp = 0;
4aad61f8 4060 it->face_before_selective_p = 0;
5f5c8ee5
GM
4061
4062 if (set_stop_p)
4063 it->stop_charpos = CHARPOS (pos);
4064}
4065
4066
4067/* Set up IT for displaying a string, starting at CHARPOS in window W.
4068 If S is non-null, it is a C string to iterate over. Otherwise,
4069 STRING gives a Lisp string to iterate over.
4070
4071 If PRECISION > 0, don't return more then PRECISION number of
4072 characters from the string.
4073
4074 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4075 characters have been returned. FIELD_WIDTH < 0 means an infinite
4076 field width.
4077
4078 MULTIBYTE = 0 means disable processing of multibyte characters,
4079 MULTIBYTE > 0 means enable it,
4080 MULTIBYTE < 0 means use IT->multibyte_p.
4081
4082 IT must be initialized via a prior call to init_iterator before
4083 calling this function. */
4084
4085static void
4086reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4087 struct it *it;
4088 unsigned char *s;
4089 Lisp_Object string;
4090 int charpos;
4091 int precision, field_width, multibyte;
4092{
4093 /* No region in strings. */
4094 it->region_beg_charpos = it->region_end_charpos = -1;
4095
4096 /* No text property checks performed by default, but see below. */
4097 it->stop_charpos = -1;
4098
4099 /* Set iterator position and end position. */
4100 bzero (&it->current, sizeof it->current);
4101 it->current.overlay_string_index = -1;
4102 it->current.dpvec_index = -1;
5f5c8ee5
GM
4103 xassert (charpos >= 0);
4104
e719f5ae
GM
4105 /* If STRING is specified, use its multibyteness, otherwise use the
4106 setting of MULTIBYTE, if specified. */
cabf45da 4107 if (multibyte >= 0)
5f5c8ee5
GM
4108 it->multibyte_p = multibyte > 0;
4109
4110 if (s == NULL)
4111 {
4112 xassert (STRINGP (string));
4113 it->string = string;
4114 it->s = NULL;
4115 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4116 it->method = next_element_from_string;
4117 it->current.string_pos = string_pos (charpos, string);
4118 }
4119 else
4120 {
4121 it->s = s;
4122 it->string = Qnil;
4123
4124 /* Note that we use IT->current.pos, not it->current.string_pos,
4125 for displaying C strings. */
4126 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4127 if (it->multibyte_p)
4128 {
4129 it->current.pos = c_string_pos (charpos, s, 1);
4130 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4131 }
4132 else
4133 {
4134 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4135 it->end_charpos = it->string_nchars = strlen (s);
4136 }
4137
4138 it->method = next_element_from_c_string;
4139 }
4140
4141 /* PRECISION > 0 means don't return more than PRECISION characters
4142 from the string. */
4143 if (precision > 0 && it->end_charpos - charpos > precision)
4144 it->end_charpos = it->string_nchars = charpos + precision;
4145
4146 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4147 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4148 FIELD_WIDTH < 0 means infinite field width. This is useful for
4149 padding with `-' at the end of a mode line. */
4150 if (field_width < 0)
4151 field_width = INFINITY;
4152 if (field_width > it->end_charpos - charpos)
4153 it->end_charpos = charpos + field_width;
4154
4155 /* Use the standard display table for displaying strings. */
4156 if (DISP_TABLE_P (Vstandard_display_table))
4157 it->dp = XCHAR_TABLE (Vstandard_display_table);
4158
4159 it->stop_charpos = charpos;
4160 CHECK_IT (it);
4161}
4162
4163
4164\f
4165/***********************************************************************
4166 Iteration
4167 ***********************************************************************/
4168
4169/* Load IT's display element fields with information about the next
4170 display element from the current position of IT. Value is zero if
4171 end of buffer (or C string) is reached. */
4172
4173int
4174get_next_display_element (it)
4175 struct it *it;
4176{
4177 /* Non-zero means that we found an display element. Zero means that
4178 we hit the end of what we iterate over. Performance note: the
4179 function pointer `method' used here turns out to be faster than
4180 using a sequence of if-statements. */
4181 int success_p = (*it->method) (it);
5f5c8ee5
GM
4182
4183 if (it->what == IT_CHARACTER)
4184 {
4185 /* Map via display table or translate control characters.
4186 IT->c, IT->len etc. have been set to the next character by
4187 the function call above. If we have a display table, and it
4188 contains an entry for IT->c, translate it. Don't do this if
4189 IT->c itself comes from a display table, otherwise we could
4190 end up in an infinite recursion. (An alternative could be to
4191 count the recursion depth of this function and signal an
4192 error when a certain maximum depth is reached.) Is it worth
4193 it? */
4194 if (success_p && it->dpvec == NULL)
4195 {
4196 Lisp_Object dv;
4197
4198 if (it->dp
4199 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4200 VECTORP (dv)))
4201 {
4202 struct Lisp_Vector *v = XVECTOR (dv);
4203
4204 /* Return the first character from the display table
4205 entry, if not empty. If empty, don't display the
4206 current character. */
4207 if (v->size)
4208 {
4209 it->dpvec_char_len = it->len;
4210 it->dpvec = v->contents;
4211 it->dpend = v->contents + v->size;
4212 it->current.dpvec_index = 0;
4213 it->method = next_element_from_display_vector;
7eee47cc
GM
4214 success_p = get_next_display_element (it);
4215 }
4216 else
4217 {
4218 set_iterator_to_next (it, 0);
4219 success_p = get_next_display_element (it);
5f5c8ee5 4220 }
5f5c8ee5
GM
4221 }
4222
4223 /* Translate control characters into `\003' or `^C' form.
4224 Control characters coming from a display table entry are
4225 currently not translated because we use IT->dpvec to hold
4226 the translation. This could easily be changed but I
197516c2
KH
4227 don't believe that it is worth doing.
4228
4229 Non-printable multibyte characters are also translated
4230 octal form. */
5f5c8ee5
GM
4231 else if ((it->c < ' '
4232 && (it->area != TEXT_AREA
c6e89d6c 4233 || (it->c != '\n' && it->c != '\t')))
54c85a23 4234 || (it->c >= 127
197516c2
KH
4235 && it->len == 1)
4236 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
4237 {
4238 /* IT->c is a control character which must be displayed
4239 either as '\003' or as `^C' where the '\\' and '^'
4240 can be defined in the display table. Fill
4241 IT->ctl_chars with glyphs for what we have to
4242 display. Then, set IT->dpvec to these glyphs. */
4243 GLYPH g;
4244
54c85a23 4245 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4246 {
4247 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4248 if (it->dp
4249 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4250 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4251 g = XINT (DISP_CTRL_GLYPH (it->dp));
4252 else
4253 g = FAST_MAKE_GLYPH ('^', 0);
4254 XSETINT (it->ctl_chars[0], g);
4255
4256 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4257 XSETINT (it->ctl_chars[1], g);
4258
4259 /* Set up IT->dpvec and return first character from it. */
4260 it->dpvec_char_len = it->len;
4261 it->dpvec = it->ctl_chars;
4262 it->dpend = it->dpvec + 2;
4263 it->current.dpvec_index = 0;
4264 it->method = next_element_from_display_vector;
4265 get_next_display_element (it);
4266 }
4267 else
4268 {
260a86a0 4269 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4270 int len;
197516c2
KH
4271 int i;
4272 GLYPH escape_glyph;
4273
5f5c8ee5
GM
4274 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4275 if (it->dp
4276 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4277 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4278 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4279 else
197516c2
KH
4280 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4281
c5924f47
KH
4282 if (SINGLE_BYTE_CHAR_P (it->c))
4283 str[0] = it->c, len = 1;
4284 else
ea9dd091
GM
4285 {
4286 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4287 if (len < 0)
4288 {
4289 /* It's an invalid character, which
4290 shouldn't happen actually, but due to
4291 bugs it may happen. Let's print the char
4292 as is, there's not much meaningful we can
4293 do with it. */
4294 str[0] = it->c;
4295 str[1] = it->c >> 8;
4296 str[2] = it->c >> 16;
4297 str[3] = it->c >> 24;
4298 len = 4;
4299 }
4300 }
c5924f47 4301
197516c2
KH
4302 for (i = 0; i < len; i++)
4303 {
4304 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4305 /* Insert three more glyphs into IT->ctl_chars for
4306 the octal display of the character. */
4307 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4308 XSETINT (it->ctl_chars[i * 4 + 1], g);
4309 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4310 XSETINT (it->ctl_chars[i * 4 + 2], g);
4311 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4312 XSETINT (it->ctl_chars[i * 4 + 3], g);
4313 }
5f5c8ee5
GM
4314
4315 /* Set up IT->dpvec and return the first character
4316 from it. */
4317 it->dpvec_char_len = it->len;
4318 it->dpvec = it->ctl_chars;
197516c2 4319 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4320 it->current.dpvec_index = 0;
4321 it->method = next_element_from_display_vector;
4322 get_next_display_element (it);
4323 }
4324 }
4325 }
4326
980806b6
KH
4327 /* Adjust face id for a multibyte character. There are no
4328 multibyte character in unibyte text. */
5f5c8ee5
GM
4329 if (it->multibyte_p
4330 && success_p
980806b6 4331 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4332 {
980806b6
KH
4333 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4334 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4335 }
4336 }
4337
4338 /* Is this character the last one of a run of characters with
4339 box? If yes, set IT->end_of_box_run_p to 1. */
4340 if (it->face_box_p
4341 && it->s == NULL)
4342 {
4343 int face_id;
4344 struct face *face;
4345
4346 it->end_of_box_run_p
4347 = ((face_id = face_after_it_pos (it),
4348 face_id != it->face_id)
4349 && (face = FACE_FROM_ID (it->f, face_id),
4350 face->box == FACE_NO_BOX));
4351 }
4352
4353 /* Value is 0 if end of buffer or string reached. */
4354 return success_p;
4355}
4356
4357
4358/* Move IT to the next display element.
4359
cafafe0b
GM
4360 RESEAT_P non-zero means if called on a newline in buffer text,
4361 skip to the next visible line start.
4362
5f5c8ee5
GM
4363 Functions get_next_display_element and set_iterator_to_next are
4364 separate because I find this arrangement easier to handle than a
4365 get_next_display_element function that also increments IT's
4366 position. The way it is we can first look at an iterator's current
4367 display element, decide whether it fits on a line, and if it does,
4368 increment the iterator position. The other way around we probably
4369 would either need a flag indicating whether the iterator has to be
4370 incremented the next time, or we would have to implement a
4371 decrement position function which would not be easy to write. */
4372
4373void
cafafe0b 4374set_iterator_to_next (it, reseat_p)
5f5c8ee5 4375 struct it *it;
cafafe0b 4376 int reseat_p;
5f5c8ee5 4377{
483de32b
GM
4378 /* Reset flags indicating start and end of a sequence of characters
4379 with box. Reset them at the start of this function because
4380 moving the iterator to a new position might set them. */
4381 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4382
5f5c8ee5
GM
4383 if (it->method == next_element_from_buffer)
4384 {
4385 /* The current display element of IT is a character from
4386 current_buffer. Advance in the buffer, and maybe skip over
4387 invisible lines that are so because of selective display. */
cafafe0b 4388 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4389 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4390 else
4391 {
4392 xassert (it->len != 0);
4393 IT_BYTEPOS (*it) += it->len;
4394 IT_CHARPOS (*it) += 1;
4395 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4396 }
4397 }
260a86a0
KH
4398 else if (it->method == next_element_from_composition)
4399 {
4400 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4401 if (STRINGP (it->string))
4402 {
4403 IT_STRING_BYTEPOS (*it) += it->len;
4404 IT_STRING_CHARPOS (*it) += it->cmp_len;
4405 it->method = next_element_from_string;
4406 goto consider_string_end;
4407 }
4408 else
4409 {
4410 IT_BYTEPOS (*it) += it->len;
4411 IT_CHARPOS (*it) += it->cmp_len;
4412 it->method = next_element_from_buffer;
4413 }
4414 }
5f5c8ee5
GM
4415 else if (it->method == next_element_from_c_string)
4416 {
4417 /* Current display element of IT is from a C string. */
4418 IT_BYTEPOS (*it) += it->len;
4419 IT_CHARPOS (*it) += 1;
4420 }
4421 else if (it->method == next_element_from_display_vector)
4422 {
4423 /* Current display element of IT is from a display table entry.
4424 Advance in the display table definition. Reset it to null if
4425 end reached, and continue with characters from buffers/
4426 strings. */
4427 ++it->current.dpvec_index;
286bcbc9 4428
980806b6
KH
4429 /* Restore face of the iterator to what they were before the
4430 display vector entry (these entries may contain faces). */
5f5c8ee5 4431 it->face_id = it->saved_face_id;
286bcbc9 4432
5f5c8ee5
GM
4433 if (it->dpvec + it->current.dpvec_index == it->dpend)
4434 {
4435 if (it->s)
4436 it->method = next_element_from_c_string;
4437 else if (STRINGP (it->string))
4438 it->method = next_element_from_string;
4439 else
4440 it->method = next_element_from_buffer;
4441
4442 it->dpvec = NULL;
4443 it->current.dpvec_index = -1;
4444
312246d1
GM
4445 /* Skip over characters which were displayed via IT->dpvec. */
4446 if (it->dpvec_char_len < 0)
4447 reseat_at_next_visible_line_start (it, 1);
4448 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4449 {
4450 it->len = it->dpvec_char_len;
cafafe0b 4451 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4452 }
4453 }
4454 }
4455 else if (it->method == next_element_from_string)
4456 {
4457 /* Current display element is a character from a Lisp string. */
4458 xassert (it->s == NULL && STRINGP (it->string));
4459 IT_STRING_BYTEPOS (*it) += it->len;
4460 IT_STRING_CHARPOS (*it) += 1;
4461
4462 consider_string_end:
4463
4464 if (it->current.overlay_string_index >= 0)
4465 {
4466 /* IT->string is an overlay string. Advance to the
4467 next, if there is one. */
4468 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4469 next_overlay_string (it);
4470 }
4471 else
4472 {
4473 /* IT->string is not an overlay string. If we reached
4474 its end, and there is something on IT->stack, proceed
4475 with what is on the stack. This can be either another
4476 string, this time an overlay string, or a buffer. */
4477 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4478 && it->sp > 0)
4479 {
4480 pop_it (it);
4481 if (!STRINGP (it->string))
4482 it->method = next_element_from_buffer;
b2046df8
GM
4483 else
4484 goto consider_string_end;
5f5c8ee5
GM
4485 }
4486 }
4487 }
4488 else if (it->method == next_element_from_image
4489 || it->method == next_element_from_stretch)
4490 {
4491 /* The position etc with which we have to proceed are on
4492 the stack. The position may be at the end of a string,
4493 if the `display' property takes up the whole string. */
4494 pop_it (it);
4495 it->image_id = 0;
4496 if (STRINGP (it->string))
4497 {
4498 it->method = next_element_from_string;
4499 goto consider_string_end;
4500 }
4501 else
4502 it->method = next_element_from_buffer;
4503 }
4504 else
4505 /* There are no other methods defined, so this should be a bug. */
4506 abort ();
4507
5f5c8ee5
GM
4508 xassert (it->method != next_element_from_string
4509 || (STRINGP (it->string)
4510 && IT_STRING_CHARPOS (*it) >= 0));
4511}
4512
4513
4514/* Load IT's display element fields with information about the next
4515 display element which comes from a display table entry or from the
4516 result of translating a control character to one of the forms `^C'
4517 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4518
4519static int
4520next_element_from_display_vector (it)
4521 struct it *it;
4522{
4523 /* Precondition. */
4524 xassert (it->dpvec && it->current.dpvec_index >= 0);
4525
4526 /* Remember the current face id in case glyphs specify faces.
4527 IT's face is restored in set_iterator_to_next. */
4528 it->saved_face_id = it->face_id;
4529
4530 if (INTEGERP (*it->dpvec)
4531 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4532 {
4533 int lface_id;
4534 GLYPH g;
4535
4536 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4537 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4538 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4539
4540 /* The entry may contain a face id to use. Such a face id is
4541 the id of a Lisp face, not a realized face. A face id of
969065c3 4542 zero means no face is specified. */
5f5c8ee5
GM
4543 lface_id = FAST_GLYPH_FACE (g);
4544 if (lface_id)
4545 {
969065c3 4546 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4547 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4548 if (face_id >= 0)
969065c3 4549 it->face_id = face_id;
5f5c8ee5
GM
4550 }
4551 }
4552 else
4553 /* Display table entry is invalid. Return a space. */
4554 it->c = ' ', it->len = 1;
4555
4556 /* Don't change position and object of the iterator here. They are
4557 still the values of the character that had this display table
4558 entry or was translated, and that's what we want. */
4559 it->what = IT_CHARACTER;
4560 return 1;
4561}
4562
4563
4564/* Load IT with the next display element from Lisp string IT->string.
4565 IT->current.string_pos is the current position within the string.
4566 If IT->current.overlay_string_index >= 0, the Lisp string is an
4567 overlay string. */
4568
4569static int
4570next_element_from_string (it)
4571 struct it *it;
4572{
4573 struct text_pos position;
4574
4575 xassert (STRINGP (it->string));
4576 xassert (IT_STRING_CHARPOS (*it) >= 0);
4577 position = it->current.string_pos;
4578
4579 /* Time to check for invisible text? */
4580 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4581 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4582 {
4583 handle_stop (it);
4584
4585 /* Since a handler may have changed IT->method, we must
4586 recurse here. */
4587 return get_next_display_element (it);
4588 }
4589
4590 if (it->current.overlay_string_index >= 0)
4591 {
4592 /* Get the next character from an overlay string. In overlay
4593 strings, There is no field width or padding with spaces to
4594 do. */
4595 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4596 {
4597 it->what = IT_EOB;
4598 return 0;
4599 }
4600 else if (STRING_MULTIBYTE (it->string))
4601 {
4602 int remaining = (STRING_BYTES (XSTRING (it->string))
4603 - IT_STRING_BYTEPOS (*it));
4604 unsigned char *s = (XSTRING (it->string)->data
4605 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4606 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4607 }
4608 else
4609 {
4610 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4611 it->len = 1;
4612 }
4613 }
4614 else
4615 {
4616 /* Get the next character from a Lisp string that is not an
4617 overlay string. Such strings come from the mode line, for
4618 example. We may have to pad with spaces, or truncate the
4619 string. See also next_element_from_c_string. */
4620 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4621 {
4622 it->what = IT_EOB;
4623 return 0;
4624 }
4625 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4626 {
4627 /* Pad with spaces. */
4628 it->c = ' ', it->len = 1;
4629 CHARPOS (position) = BYTEPOS (position) = -1;
4630 }
4631 else if (STRING_MULTIBYTE (it->string))
4632 {
4633 int maxlen = (STRING_BYTES (XSTRING (it->string))
4634 - IT_STRING_BYTEPOS (*it));
4635 unsigned char *s = (XSTRING (it->string)->data
4636 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4637 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4638 }
4639 else
4640 {
4641 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4642 it->len = 1;
4643 }
4644 }
4645
4646 /* Record what we have and where it came from. Note that we store a
4647 buffer position in IT->position although it could arguably be a
4648 string position. */
4649 it->what = IT_CHARACTER;
4650 it->object = it->string;
4651 it->position = position;
4652 return 1;
4653}
4654
4655
4656/* Load IT with next display element from C string IT->s.
4657 IT->string_nchars is the maximum number of characters to return
4658 from the string. IT->end_charpos may be greater than
4659 IT->string_nchars when this function is called, in which case we
4660 may have to return padding spaces. Value is zero if end of string
4661 reached, including padding spaces. */
4662
4663static int
4664next_element_from_c_string (it)
4665 struct it *it;
4666{
4667 int success_p = 1;
4668
4669 xassert (it->s);
4670 it->what = IT_CHARACTER;
4671 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4672 it->object = Qnil;
4673
4674 /* IT's position can be greater IT->string_nchars in case a field
4675 width or precision has been specified when the iterator was
4676 initialized. */
4677 if (IT_CHARPOS (*it) >= it->end_charpos)
4678 {
4679 /* End of the game. */
4680 it->what = IT_EOB;
4681 success_p = 0;
4682 }
4683 else if (IT_CHARPOS (*it) >= it->string_nchars)
4684 {
4685 /* Pad with spaces. */
4686 it->c = ' ', it->len = 1;
4687 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4688 }
4689 else if (it->multibyte_p)
4690 {
4691 /* Implementation note: The calls to strlen apparently aren't a
4692 performance problem because there is no noticeable performance
4693 difference between Emacs running in unibyte or multibyte mode. */
4694 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4695 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4696 maxlen, &it->len);
5f5c8ee5
GM
4697 }
4698 else
4699 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4700
4701 return success_p;
4702}
4703
4704
4705/* Set up IT to return characters from an ellipsis, if appropriate.
4706 The definition of the ellipsis glyphs may come from a display table
4707 entry. This function Fills IT with the first glyph from the
4708 ellipsis if an ellipsis is to be displayed. */
4709
13f19968 4710static int
5f5c8ee5
GM
4711next_element_from_ellipsis (it)
4712 struct it *it;
4713{
13f19968 4714 if (it->selective_display_ellipsis_p)
5f5c8ee5 4715 {
13f19968
GM
4716 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4717 {
4718 /* Use the display table definition for `...'. Invalid glyphs
4719 will be handled by the method returning elements from dpvec. */
4720 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4721 it->dpvec_char_len = it->len;
4722 it->dpvec = v->contents;
4723 it->dpend = v->contents + v->size;
4724 it->current.dpvec_index = 0;
4725 it->method = next_element_from_display_vector;
4726 }
4727 else
4728 {
4729 /* Use default `...' which is stored in default_invis_vector. */
4730 it->dpvec_char_len = it->len;
4731 it->dpvec = default_invis_vector;
4732 it->dpend = default_invis_vector + 3;
4733 it->current.dpvec_index = 0;
4734 it->method = next_element_from_display_vector;
4735 }
5f5c8ee5 4736 }
13f19968 4737 else
54918e2b 4738 {
4aad61f8
GM
4739 /* The face at the current position may be different from the
4740 face we find after the invisible text. Remember what it
4741 was in IT->saved_face_id, and signal that it's there by
4742 setting face_before_selective_p. */
4743 it->saved_face_id = it->face_id;
54918e2b
GM
4744 it->method = next_element_from_buffer;
4745 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4746 it->face_before_selective_p = 1;
54918e2b 4747 }
13f19968
GM
4748
4749 return get_next_display_element (it);
5f5c8ee5
GM
4750}
4751
4752
4753/* Deliver an image display element. The iterator IT is already
4754 filled with image information (done in handle_display_prop). Value
4755 is always 1. */
4756
4757
4758static int
4759next_element_from_image (it)
4760 struct it *it;
4761{
4762 it->what = IT_IMAGE;
4763 return 1;
4764}
4765
4766
4767/* Fill iterator IT with next display element from a stretch glyph
4768 property. IT->object is the value of the text property. Value is
4769 always 1. */
4770
4771static int
4772next_element_from_stretch (it)
4773 struct it *it;
4774{
4775 it->what = IT_STRETCH;
4776 return 1;
4777}
4778
4779
4780/* Load IT with the next display element from current_buffer. Value
4781 is zero if end of buffer reached. IT->stop_charpos is the next
4782 position at which to stop and check for text properties or buffer
4783 end. */
4784
4785static int
4786next_element_from_buffer (it)
4787 struct it *it;
4788{
4789 int success_p = 1;
4790
4791 /* Check this assumption, otherwise, we would never enter the
4792 if-statement, below. */
4793 xassert (IT_CHARPOS (*it) >= BEGV
4794 && IT_CHARPOS (*it) <= it->stop_charpos);
4795
4796 if (IT_CHARPOS (*it) >= it->stop_charpos)
4797 {
4798 if (IT_CHARPOS (*it) >= it->end_charpos)
4799 {
4800 int overlay_strings_follow_p;
4801
4802 /* End of the game, except when overlay strings follow that
4803 haven't been returned yet. */
4804 if (it->overlay_strings_at_end_processed_p)
4805 overlay_strings_follow_p = 0;
4806 else
4807 {
4808 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 4809 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
4810 }
4811
4812 if (overlay_strings_follow_p)
4813 success_p = get_next_display_element (it);
4814 else
4815 {
4816 it->what = IT_EOB;
4817 it->position = it->current.pos;
4818 success_p = 0;
4819 }
4820 }
4821 else
4822 {
4823 handle_stop (it);
4824 return get_next_display_element (it);
4825 }
4826 }
4827 else
4828 {
4829 /* No face changes, overlays etc. in sight, so just return a
4830 character from current_buffer. */
4831 unsigned char *p;
4832
4833 /* Maybe run the redisplay end trigger hook. Performance note:
4834 This doesn't seem to cost measurable time. */
4835 if (it->redisplay_end_trigger_charpos
4836 && it->glyph_row
4837 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4838 run_redisplay_end_trigger_hook (it);
4839
4840 /* Get the next character, maybe multibyte. */
4841 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4842 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4843 {
4844 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4845 - IT_BYTEPOS (*it));
4fdb80f2 4846 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4847 }
4848 else
4849 it->c = *p, it->len = 1;
4850
4851 /* Record what we have and where it came from. */
4852 it->what = IT_CHARACTER;;
4853 it->object = it->w->buffer;
4854 it->position = it->current.pos;
4855
4856 /* Normally we return the character found above, except when we
4857 really want to return an ellipsis for selective display. */
4858 if (it->selective)
4859 {
4860 if (it->c == '\n')
4861 {
4862 /* A value of selective > 0 means hide lines indented more
4863 than that number of columns. */
4864 if (it->selective > 0
4865 && IT_CHARPOS (*it) + 1 < ZV
4866 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4867 IT_BYTEPOS (*it) + 1,
4868 it->selective))
312246d1 4869 {
13f19968 4870 success_p = next_element_from_ellipsis (it);
312246d1
GM
4871 it->dpvec_char_len = -1;
4872 }
5f5c8ee5
GM
4873 }
4874 else if (it->c == '\r' && it->selective == -1)
4875 {
4876 /* A value of selective == -1 means that everything from the
4877 CR to the end of the line is invisible, with maybe an
4878 ellipsis displayed for it. */
13f19968 4879 success_p = next_element_from_ellipsis (it);
312246d1 4880 it->dpvec_char_len = -1;
5f5c8ee5
GM
4881 }
4882 }
4883 }
4884
4885 /* Value is zero if end of buffer reached. */
c880678e 4886 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4887 return success_p;
4888}
4889
4890
4891/* Run the redisplay end trigger hook for IT. */
4892
4893static void
4894run_redisplay_end_trigger_hook (it)
4895 struct it *it;
4896{
4897 Lisp_Object args[3];
4898
4899 /* IT->glyph_row should be non-null, i.e. we should be actually
4900 displaying something, or otherwise we should not run the hook. */
4901 xassert (it->glyph_row);
4902
4903 /* Set up hook arguments. */
4904 args[0] = Qredisplay_end_trigger_functions;
4905 args[1] = it->window;
4906 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4907 it->redisplay_end_trigger_charpos = 0;
4908
4909 /* Since we are *trying* to run these functions, don't try to run
4910 them again, even if they get an error. */
4911 it->w->redisplay_end_trigger = Qnil;
4912 Frun_hook_with_args (3, args);
4913
4914 /* Notice if it changed the face of the character we are on. */
4915 handle_face_prop (it);
4916}
4917
4918
260a86a0
KH
4919/* Deliver a composition display element. The iterator IT is already
4920 filled with composition information (done in
4921 handle_composition_prop). Value is always 1. */
4922
4923static int
4924next_element_from_composition (it)
4925 struct it *it;
4926{
4927 it->what = IT_COMPOSITION;
4928 it->position = (STRINGP (it->string)
4929 ? it->current.string_pos
4930 : it->current.pos);
4931 return 1;
4932}
4933
4934
5f5c8ee5
GM
4935\f
4936/***********************************************************************
4937 Moving an iterator without producing glyphs
4938 ***********************************************************************/
4939
4940/* Move iterator IT to a specified buffer or X position within one
4941 line on the display without producing glyphs.
4942
4943 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4944 whichever is reached first.
4945
4946 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4947
4948 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4949 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4950 TO_X includes the amount by which a window is horizontally
4951 scrolled.
4952
4953 Value is
4954
4955 MOVE_POS_MATCH_OR_ZV
4956 - when TO_POS or ZV was reached.
4957
4958 MOVE_X_REACHED
4959 -when TO_X was reached before TO_POS or ZV were reached.
4960
4961 MOVE_LINE_CONTINUED
4962 - when we reached the end of the display area and the line must
4963 be continued.
4964
4965 MOVE_LINE_TRUNCATED
4966 - when we reached the end of the display area and the line is
4967 truncated.
4968
4969 MOVE_NEWLINE_OR_CR
4970 - when we stopped at a line end, i.e. a newline or a CR and selective
4971 display is on. */
4972
701552dd 4973static enum move_it_result
5f5c8ee5
GM
4974move_it_in_display_line_to (it, to_charpos, to_x, op)
4975 struct it *it;
4976 int to_charpos, to_x, op;
4977{
4978 enum move_it_result result = MOVE_UNDEFINED;
4979 struct glyph_row *saved_glyph_row;
4980
4981 /* Don't produce glyphs in produce_glyphs. */
4982 saved_glyph_row = it->glyph_row;
4983 it->glyph_row = NULL;
4984
5f5c8ee5
GM
4985 while (1)
4986 {
ae26e27d 4987 int x, i, ascent = 0, descent = 0;
5f5c8ee5
GM
4988
4989 /* Stop when ZV or TO_CHARPOS reached. */
4990 if (!get_next_display_element (it)
4991 || ((op & MOVE_TO_POS) != 0
4992 && BUFFERP (it->object)
4993 && IT_CHARPOS (*it) >= to_charpos))
4994 {
4995 result = MOVE_POS_MATCH_OR_ZV;
4996 break;
4997 }
4998
4999 /* The call to produce_glyphs will get the metrics of the
5000 display element IT is loaded with. We record in x the
5001 x-position before this display element in case it does not
5002 fit on the line. */
5003 x = it->current_x;
47589c8c
GM
5004
5005 /* Remember the line height so far in case the next element doesn't
5006 fit on the line. */
5007 if (!it->truncate_lines_p)
5008 {
5009 ascent = it->max_ascent;
5010 descent = it->max_descent;
5011 }
5012
5f5c8ee5
GM
5013 PRODUCE_GLYPHS (it);
5014
5015 if (it->area != TEXT_AREA)
5016 {
cafafe0b 5017 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5018 continue;
5019 }
5020
5021 /* The number of glyphs we get back in IT->nglyphs will normally
5022 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5023 character on a terminal frame, or (iii) a line end. For the
5024 second case, IT->nglyphs - 1 padding glyphs will be present
5025 (on X frames, there is only one glyph produced for a
5026 composite character.
5027
5028 The behavior implemented below means, for continuation lines,
5029 that as many spaces of a TAB as fit on the current line are
5030 displayed there. For terminal frames, as many glyphs of a
5031 multi-glyph character are displayed in the current line, too.
5032 This is what the old redisplay code did, and we keep it that
5033 way. Under X, the whole shape of a complex character must
5034 fit on the line or it will be completely displayed in the
5035 next line.
5036
5037 Note that both for tabs and padding glyphs, all glyphs have
5038 the same width. */
5039 if (it->nglyphs)
5040 {
5041 /* More than one glyph or glyph doesn't fit on line. All
5042 glyphs have the same width. */
5043 int single_glyph_width = it->pixel_width / it->nglyphs;
5044 int new_x;
5045
5046 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5047 {
5048 new_x = x + single_glyph_width;
5049
5050 /* We want to leave anything reaching TO_X to the caller. */
5051 if ((op & MOVE_TO_X) && new_x > to_x)
5052 {
5053 it->current_x = x;
5054 result = MOVE_X_REACHED;
5055 break;
5056 }
5057 else if (/* Lines are continued. */
5058 !it->truncate_lines_p
5059 && (/* And glyph doesn't fit on the line. */
5060 new_x > it->last_visible_x
5061 /* Or it fits exactly and we're on a window
5062 system frame. */
5063 || (new_x == it->last_visible_x
5064 && FRAME_WINDOW_P (it->f))))
5065 {
5066 if (/* IT->hpos == 0 means the very first glyph
5067 doesn't fit on the line, e.g. a wide image. */
5068 it->hpos == 0
5069 || (new_x == it->last_visible_x
5070 && FRAME_WINDOW_P (it->f)))
5071 {
5072 ++it->hpos;
5073 it->current_x = new_x;
5074 if (i == it->nglyphs - 1)
cafafe0b 5075 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5076 }
5077 else
47589c8c
GM
5078 {
5079 it->current_x = x;
5080 it->max_ascent = ascent;
5081 it->max_descent = descent;
5082 }
5083
5084 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5085 IT_CHARPOS (*it)));
5f5c8ee5
GM
5086 result = MOVE_LINE_CONTINUED;
5087 break;
5088 }
5089 else if (new_x > it->first_visible_x)
5090 {
5091 /* Glyph is visible. Increment number of glyphs that
5092 would be displayed. */
5093 ++it->hpos;
5094 }
5095 else
5096 {
5097 /* Glyph is completely off the left margin of the display
5098 area. Nothing to do. */
5099 }
5100 }
5101
5102 if (result != MOVE_UNDEFINED)
5103 break;
5104 }
5105 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5106 {
5107 /* Stop when TO_X specified and reached. This check is
5108 necessary here because of lines consisting of a line end,
5109 only. The line end will not produce any glyphs and we
5110 would never get MOVE_X_REACHED. */
5111 xassert (it->nglyphs == 0);
5112 result = MOVE_X_REACHED;
5113 break;
5114 }
5115
5116 /* Is this a line end? If yes, we're done. */
5117 if (ITERATOR_AT_END_OF_LINE_P (it))
5118 {
5119 result = MOVE_NEWLINE_OR_CR;
5120 break;
5121 }
5122
5123 /* The current display element has been consumed. Advance
5124 to the next. */
cafafe0b 5125 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5126
5127 /* Stop if lines are truncated and IT's current x-position is
5128 past the right edge of the window now. */
5129 if (it->truncate_lines_p
5130 && it->current_x >= it->last_visible_x)
5131 {
5132 result = MOVE_LINE_TRUNCATED;
5133 break;
5134 }
5135 }
5136
5137 /* Restore the iterator settings altered at the beginning of this
5138 function. */
5139 it->glyph_row = saved_glyph_row;
5140 return result;
5141}
5142
5143
5144/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5145 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5146 the description of enum move_operation_enum.
5147
5148 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5149 screen line, this function will set IT to the next position >
5150 TO_CHARPOS. */
5151
5152void
5153move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5154 struct it *it;
5155 int to_charpos, to_x, to_y, to_vpos;
5156 int op;
5157{
5158 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5159 int line_height;
47589c8c 5160 int reached = 0;
5f5c8ee5 5161
47589c8c 5162 for (;;)
5f5c8ee5
GM
5163 {
5164 if (op & MOVE_TO_VPOS)
5165 {
5166 /* If no TO_CHARPOS and no TO_X specified, stop at the
5167 start of the line TO_VPOS. */
5168 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5169 {
5170 if (it->vpos == to_vpos)
47589c8c
GM
5171 {
5172 reached = 1;
5173 break;
5174 }
5175 else
5176 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5177 }
5178 else
5179 {
5180 /* TO_VPOS >= 0 means stop at TO_X in the line at
5181 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5182 if (it->vpos == to_vpos)
5183 {
5184 reached = 2;
5185 break;
5186 }
5187
5f5c8ee5
GM
5188 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5189
5190 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5191 {
5192 reached = 3;
5193 break;
5194 }
5f5c8ee5
GM
5195 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5196 {
5197 /* We have reached TO_X but not in the line we want. */
5198 skip = move_it_in_display_line_to (it, to_charpos,
5199 -1, MOVE_TO_POS);
5200 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5201 {
5202 reached = 4;
5203 break;
5204 }
5f5c8ee5
GM
5205 }
5206 }
5207 }
5208 else if (op & MOVE_TO_Y)
5209 {
5210 struct it it_backup;
5f5c8ee5
GM
5211
5212 /* TO_Y specified means stop at TO_X in the line containing
5213 TO_Y---or at TO_CHARPOS if this is reached first. The
5214 problem is that we can't really tell whether the line
5215 contains TO_Y before we have completely scanned it, and
5216 this may skip past TO_X. What we do is to first scan to
5217 TO_X.
5218
5219 If TO_X is not specified, use a TO_X of zero. The reason
5220 is to make the outcome of this function more predictable.
5221 If we didn't use TO_X == 0, we would stop at the end of
5222 the line which is probably not what a caller would expect
5223 to happen. */
5224 skip = move_it_in_display_line_to (it, to_charpos,
5225 ((op & MOVE_TO_X)
5226 ? to_x : 0),
5227 (MOVE_TO_X
5228 | (op & MOVE_TO_POS)));
5229
5230 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5231 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5232 {
5233 reached = 5;
5234 break;
5235 }
5f5c8ee5
GM
5236
5237 /* If TO_X was reached, we would like to know whether TO_Y
5238 is in the line. This can only be said if we know the
5239 total line height which requires us to scan the rest of
5240 the line. */
5f5c8ee5
GM
5241 if (skip == MOVE_X_REACHED)
5242 {
5243 it_backup = *it;
47589c8c 5244 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5245 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5246 op & MOVE_TO_POS);
47589c8c 5247 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5248 }
5249
5250 /* Now, decide whether TO_Y is in this line. */
5251 line_height = it->max_ascent + it->max_descent;
47589c8c 5252 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5f5c8ee5
GM
5253
5254 if (to_y >= it->current_y
5255 && to_y < it->current_y + line_height)
5256 {
5257 if (skip == MOVE_X_REACHED)
5258 /* If TO_Y is in this line and TO_X was reached above,
5259 we scanned too far. We have to restore IT's settings
5260 to the ones before skipping. */
5261 *it = it_backup;
47589c8c 5262 reached = 6;
5f5c8ee5
GM
5263 }
5264 else if (skip == MOVE_X_REACHED)
5265 {
5266 skip = skip2;
5267 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5268 reached = 7;
5f5c8ee5
GM
5269 }
5270
47589c8c 5271 if (reached)
5f5c8ee5
GM
5272 break;
5273 }
5274 else
5275 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5276
5277 switch (skip)
5278 {
5279 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5280 reached = 8;
5281 goto out;
5f5c8ee5
GM
5282
5283 case MOVE_NEWLINE_OR_CR:
cafafe0b 5284 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5285 it->continuation_lines_width = 0;
5286 break;
5287
5288 case MOVE_LINE_TRUNCATED:
5289 it->continuation_lines_width = 0;
312246d1 5290 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5291 if ((op & MOVE_TO_POS) != 0
5292 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5293 {
5294 reached = 9;
5295 goto out;
5296 }
5f5c8ee5
GM
5297 break;
5298
5299 case MOVE_LINE_CONTINUED:
5300 it->continuation_lines_width += it->current_x;
5301 break;
5302
5303 default:
5304 abort ();
5305 }
5306
5307 /* Reset/increment for the next run. */
5308 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5309 it->current_x = it->hpos = 0;
5310 it->current_y += it->max_ascent + it->max_descent;
5311 ++it->vpos;
5312 last_height = it->max_ascent + it->max_descent;
5313 last_max_ascent = it->max_ascent;
5314 it->max_ascent = it->max_descent = 0;
5315 }
47589c8c
GM
5316
5317 out:
5318
5319 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5320}
5321
5322
5323/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5324
5325 If DY > 0, move IT backward at least that many pixels. DY = 0
5326 means move IT backward to the preceding line start or BEGV. This
5327 function may move over more than DY pixels if IT->current_y - DY
5328 ends up in the middle of a line; in this case IT->current_y will be
5329 set to the top of the line moved to. */
5330
5331void
5332move_it_vertically_backward (it, dy)
5333 struct it *it;
5334 int dy;
5335{
79ddf6f7
GM
5336 int nlines, h;
5337 struct it it2, it3;
5f5c8ee5
GM
5338 int start_pos = IT_CHARPOS (*it);
5339
5340 xassert (dy >= 0);
5341
5342 /* Estimate how many newlines we must move back. */
5343 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5344
5345 /* Set the iterator's position that many lines back. */
5346 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5347 back_to_previous_visible_line_start (it);
5348
5349 /* Reseat the iterator here. When moving backward, we don't want
5350 reseat to skip forward over invisible text, set up the iterator
5351 to deliver from overlay strings at the new position etc. So,
5352 use reseat_1 here. */
5353 reseat_1 (it, it->current.pos, 1);
5354
5355 /* We are now surely at a line start. */
5356 it->current_x = it->hpos = 0;
5357
5358 /* Move forward and see what y-distance we moved. First move to the
5359 start of the next line so that we get its height. We need this
5360 height to be able to tell whether we reached the specified
5361 y-distance. */
5362 it2 = *it;
5363 it2.max_ascent = it2.max_descent = 0;
5364 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5365 MOVE_TO_POS | MOVE_TO_VPOS);
5366 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 5367 it3 = it2;
47589c8c 5368
5f5c8ee5
GM
5369 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5370 xassert (IT_CHARPOS (*it) >= BEGV);
5371 h = it2.current_y - it->current_y;
5372 nlines = it2.vpos - it->vpos;
5373
5374 /* Correct IT's y and vpos position. */
5375 it->vpos -= nlines;
5376 it->current_y -= h;
5377
5378 if (dy == 0)
5379 {
5380 /* DY == 0 means move to the start of the screen line. The
5381 value of nlines is > 0 if continuation lines were involved. */
5382 if (nlines > 0)
5383 move_it_by_lines (it, nlines, 1);
5384 xassert (IT_CHARPOS (*it) <= start_pos);
5385 }
5386 else if (nlines)
5387 {
5388 /* The y-position we try to reach. Note that h has been
5389 subtracted in front of the if-statement. */
5390 int target_y = it->current_y + h - dy;
79ddf6f7
GM
5391 int y0 = it3.current_y;
5392 int y1 = line_bottom_y (&it3);
5393 int line_height = y1 - y0;
f7ccfc8c 5394
5f5c8ee5
GM
5395 /* If we did not reach target_y, try to move further backward if
5396 we can. If we moved too far backward, try to move forward. */
5397 if (target_y < it->current_y
79ddf6f7
GM
5398 /* This is heuristic. In a window that's 3 lines high, with
5399 a line height of 13 pixels each, recentering with point
5400 on the bottom line will try to move -39/2 = 19 pixels
5401 backward. Try to avoid moving into the first line. */
798dbe1f 5402 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
5403 && IT_CHARPOS (*it) > BEGV)
5404 {
f7ccfc8c
GM
5405 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5406 target_y - it->current_y));
5f5c8ee5
GM
5407 move_it_vertically (it, target_y - it->current_y);
5408 xassert (IT_CHARPOS (*it) >= BEGV);
5409 }
5410 else if (target_y >= it->current_y + line_height
5411 && IT_CHARPOS (*it) < ZV)
5412 {
f7ccfc8c
GM
5413 /* Should move forward by at least one line, maybe more. */
5414 do
5415 {
5416 move_it_by_lines (it, 1, 1);
5417 }
5418 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5419
5f5c8ee5
GM
5420 xassert (IT_CHARPOS (*it) >= BEGV);
5421 }
5422 }
5423}
5424
5425
5426/* Move IT by a specified amount of pixel lines DY. DY negative means
5427 move backwards. DY = 0 means move to start of screen line. At the
5428 end, IT will be on the start of a screen line. */
5429
5430void
5431move_it_vertically (it, dy)
5432 struct it *it;
5433 int dy;
5434{
5435 if (dy <= 0)
5436 move_it_vertically_backward (it, -dy);
5437 else if (dy > 0)
5438 {
47589c8c 5439 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5440 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5441 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5442 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5443
5444 /* If buffer ends in ZV without a newline, move to the start of
5445 the line to satisfy the post-condition. */
5446 if (IT_CHARPOS (*it) == ZV
5447 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5448 move_it_by_lines (it, 0, 0);
5449 }
5450}
5451
5452
47fc2c10
GM
5453/* Move iterator IT past the end of the text line it is in. */
5454
5455void
5456move_it_past_eol (it)
5457 struct it *it;
5458{
5459 enum move_it_result rc;
5460
5461 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5462 if (rc == MOVE_NEWLINE_OR_CR)
5463 set_iterator_to_next (it, 0);
5464}
5465
5466
2c79b732
GM
5467#if 0 /* Currently not used. */
5468
5f5c8ee5
GM
5469/* Return non-zero if some text between buffer positions START_CHARPOS
5470 and END_CHARPOS is invisible. IT->window is the window for text
5471 property lookup. */
5472
5473static int
5474invisible_text_between_p (it, start_charpos, end_charpos)
5475 struct it *it;
5476 int start_charpos, end_charpos;
5477{
5f5c8ee5
GM
5478 Lisp_Object prop, limit;
5479 int invisible_found_p;
5480
5481 xassert (it != NULL && start_charpos <= end_charpos);
5482
5483 /* Is text at START invisible? */
5484 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5485 it->window);
5486 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5487 invisible_found_p = 1;
5488 else
5489 {
016b5642
MB
5490 limit = Fnext_single_char_property_change (make_number (start_charpos),
5491 Qinvisible, Qnil,
5492 make_number (end_charpos));
5f5c8ee5
GM
5493 invisible_found_p = XFASTINT (limit) < end_charpos;
5494 }
5495
5496 return invisible_found_p;
5f5c8ee5
GM
5497}
5498
2c79b732
GM
5499#endif /* 0 */
5500
5f5c8ee5
GM
5501
5502/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5503 negative means move up. DVPOS == 0 means move to the start of the
5504 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5505 NEED_Y_P is zero, IT->current_y will be left unchanged.
5506
5507 Further optimization ideas: If we would know that IT->f doesn't use
5508 a face with proportional font, we could be faster for
5509 truncate-lines nil. */
5510
5511void
5512move_it_by_lines (it, dvpos, need_y_p)
5513 struct it *it;
5514 int dvpos, need_y_p;
5515{
5516 struct position pos;
5517
5518 if (!FRAME_WINDOW_P (it->f))
5519 {
5520 struct text_pos textpos;
5521
5522 /* We can use vmotion on frames without proportional fonts. */
5523 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5524 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5525 reseat (it, textpos, 1);
5526 it->vpos += pos.vpos;
5527 it->current_y += pos.vpos;
5528 }
5529 else if (dvpos == 0)
5530 {
5531 /* DVPOS == 0 means move to the start of the screen line. */
5532 move_it_vertically_backward (it, 0);
5533 xassert (it->current_x == 0 && it->hpos == 0);
5534 }
5535 else if (dvpos > 0)
2c79b732 5536 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5537 else
5538 {
5539 struct it it2;
5540 int start_charpos, i;
2c79b732 5541
e8660d73
GM
5542 /* Start at the beginning of the screen line containing IT's
5543 position. */
5544 move_it_vertically_backward (it, 0);
b0e619b4 5545
5f5c8ee5
GM
5546 /* Go back -DVPOS visible lines and reseat the iterator there. */
5547 start_charpos = IT_CHARPOS (*it);
5548 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5549 back_to_previous_visible_line_start (it);
5550 reseat (it, it->current.pos, 1);
5551 it->current_x = it->hpos = 0;
5552
5553 /* Above call may have moved too far if continuation lines
5554 are involved. Scan forward and see if it did. */
5555 it2 = *it;
5556 it2.vpos = it2.current_y = 0;
5557 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5558 it->vpos -= it2.vpos;
5559 it->current_y -= it2.current_y;
5560 it->current_x = it->hpos = 0;
5561
5562 /* If we moved too far, move IT some lines forward. */
5563 if (it2.vpos > -dvpos)
5564 {
5565 int delta = it2.vpos + dvpos;
5566 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5567 }
5568 }
5569}
5570
5571
5572\f
5573/***********************************************************************
5574 Messages
5575 ***********************************************************************/
5576
5577
937248bc
GM
5578/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5579 to *Messages*. */
5580
5581void
5582add_to_log (format, arg1, arg2)
5583 char *format;
5584 Lisp_Object arg1, arg2;
5585{
5586 Lisp_Object args[3];
5587 Lisp_Object msg, fmt;
5588 char *buffer;
5589 int len;
5590 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5591
ae794295
GM
5592 /* Do nothing if called asynchronously. Inserting text into
5593 a buffer may call after-change-functions and alike and
5594 that would means running Lisp asynchronously. */
5595 if (handling_signal)
5596 return;
5597
937248bc
GM
5598 fmt = msg = Qnil;
5599 GCPRO4 (fmt, msg, arg1, arg2);
5600
5601 args[0] = fmt = build_string (format);
5602 args[1] = arg1;
5603 args[2] = arg2;
6fc556fd 5604 msg = Fformat (3, args);
937248bc
GM
5605
5606 len = STRING_BYTES (XSTRING (msg)) + 1;
5607 buffer = (char *) alloca (len);
1e1e5daf 5608 bcopy (XSTRING (msg)->data, buffer, len);
937248bc 5609
796184bc 5610 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5611 UNGCPRO;
5612}
5613
5614
5f5c8ee5
GM
5615/* Output a newline in the *Messages* buffer if "needs" one. */
5616
5617void
5618message_log_maybe_newline ()
5619{
5620 if (message_log_need_newline)
5621 message_dolog ("", 0, 1, 0);
5622}
5623
5624
1e313f28 5625/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5626 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5627 nonzero, means interpret the contents of M as multibyte. This
5628 function calls low-level routines in order to bypass text property
5629 hooks, etc. which might not be safe to run. */
5630
5631void
1e313f28 5632message_dolog (m, nbytes, nlflag, multibyte)
5f5c8ee5 5633 char *m;
1e313f28 5634 int nbytes, nlflag, multibyte;
5f5c8ee5
GM
5635{
5636 if (!NILP (Vmessage_log_max))
5637 {
5638 struct buffer *oldbuf;
5639 Lisp_Object oldpoint, oldbegv, oldzv;
5640 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5641 int point_at_end = 0;
5642 int zv_at_end = 0;
5643 Lisp_Object old_deactivate_mark, tem;
6052529b 5644 struct gcpro gcpro1;
5f5c8ee5
GM
5645
5646 old_deactivate_mark = Vdeactivate_mark;
5647 oldbuf = current_buffer;
6a94510a 5648 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5649 current_buffer->undo_list = Qt;
5650
b14bc55e
RS
5651 oldpoint = message_dolog_marker1;
5652 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5653 oldbegv = message_dolog_marker2;
5654 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5655 oldzv = message_dolog_marker3;
5656 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5657 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
5658
5659 if (PT == Z)
5660 point_at_end = 1;
5661 if (ZV == Z)
5662 zv_at_end = 1;
5663
5664 BEGV = BEG;
5665 BEGV_BYTE = BEG_BYTE;
5666 ZV = Z;
5667 ZV_BYTE = Z_BYTE;
5668 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5669
5670 /* Insert the string--maybe converting multibyte to single byte
5671 or vice versa, so that all the text fits the buffer. */
5672 if (multibyte
5673 && NILP (current_buffer->enable_multibyte_characters))
5674 {
1e313f28 5675 int i, c, char_bytes;
5f5c8ee5
GM
5676 unsigned char work[1];
5677
5678 /* Convert a multibyte string to single-byte
5679 for the *Message* buffer. */
1e313f28 5680 for (i = 0; i < nbytes; i += nbytes)
5f5c8ee5 5681 {
1e313f28 5682 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5683 work[0] = (SINGLE_BYTE_CHAR_P (c)
5684 ? c
5685 : multibyte_char_to_unibyte (c, Qnil));
5686 insert_1_both (work, 1, 1, 1, 0, 0);
5687 }
5688 }
5689 else if (! multibyte
5690 && ! NILP (current_buffer->enable_multibyte_characters))
5691 {
1e313f28 5692 int i, c, char_bytes;
5f5c8ee5 5693 unsigned char *msg = (unsigned char *) m;
260a86a0 5694 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5695 /* Convert a single-byte string to multibyte
5696 for the *Message* buffer. */
1e313f28 5697 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5698 {
5699 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5700 char_bytes = CHAR_STRING (c, str);
5701 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5702 }
5703 }
1e313f28
GM
5704 else if (nbytes)
5705 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5706
5707 if (nlflag)
5708 {
5709 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5710 insert_1 ("\n", 1, 1, 0, 0);
5711
5712 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5713 this_bol = PT;
5714 this_bol_byte = PT_BYTE;
5715
b14bc55e
RS
5716 /* See if this line duplicates the previous one.
5717 If so, combine duplicates. */
5f5c8ee5
GM
5718 if (this_bol > BEG)
5719 {
5720 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5721 prev_bol = PT;
5722 prev_bol_byte = PT_BYTE;
5723
5724 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5725 this_bol, this_bol_byte);
5726 if (dup)
5727 {
5728 del_range_both (prev_bol, prev_bol_byte,
5729 this_bol, this_bol_byte, 0);
5730 if (dup > 1)
5731 {
5732 char dupstr[40];
5733 int duplen;
5734
5735 /* If you change this format, don't forget to also
5736 change message_log_check_duplicate. */
5737 sprintf (dupstr, " [%d times]", dup);
5738 duplen = strlen (dupstr);
5739 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5740 insert_1 (dupstr, duplen, 1, 0, 1);
5741 }
5742 }
5743 }
5744
b14bc55e
RS
5745 /* If we have more than the desired maximum number of lines
5746 in the *Messages* buffer now, delete the oldest ones.
5747 This is safe because we don't have undo in this buffer. */
5748
5f5c8ee5
GM
5749 if (NATNUMP (Vmessage_log_max))
5750 {
5751 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5752 -XFASTINT (Vmessage_log_max) - 1, 0);
5753 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5754 }
5755 }
5756 BEGV = XMARKER (oldbegv)->charpos;
5757 BEGV_BYTE = marker_byte_position (oldbegv);
5758
5759 if (zv_at_end)
5760 {
5761 ZV = Z;
5762 ZV_BYTE = Z_BYTE;
5763 }
5764 else
5765 {
5766 ZV = XMARKER (oldzv)->charpos;
5767 ZV_BYTE = marker_byte_position (oldzv);
5768 }
5769
5770 if (point_at_end)
5771 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5772 else
5773 /* We can't do Fgoto_char (oldpoint) because it will run some
5774 Lisp code. */
5775 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5776 XMARKER (oldpoint)->bytepos);
5777
5778 UNGCPRO;
b14bc55e
RS
5779 unchain_marker (oldpoint);
5780 unchain_marker (oldbegv);
5781 unchain_marker (oldzv);
5f5c8ee5
GM
5782
5783 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5784 set_buffer_internal (oldbuf);
5785 if (NILP (tem))
5786 windows_or_buffers_changed = old_windows_or_buffers_changed;
5787 message_log_need_newline = !nlflag;
5788 Vdeactivate_mark = old_deactivate_mark;
5789 }
5790}
5791
5792
5793/* We are at the end of the buffer after just having inserted a newline.
5794 (Note: We depend on the fact we won't be crossing the gap.)
5795 Check to see if the most recent message looks a lot like the previous one.
5796 Return 0 if different, 1 if the new one should just replace it, or a
5797 value N > 1 if we should also append " [N times]". */
5798
5799static int
5800message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5801 int prev_bol, this_bol;
5802 int prev_bol_byte, this_bol_byte;
5803{
5804 int i;
5805 int len = Z_BYTE - 1 - this_bol_byte;
5806 int seen_dots = 0;
5807 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5808 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5809
5810 for (i = 0; i < len; i++)
5811 {
509633e3 5812 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5813 seen_dots = 1;
5814 if (p1[i] != p2[i])
5815 return seen_dots;
5816 }
5817 p1 += len;
5818 if (*p1 == '\n')
5819 return 2;
5820 if (*p1++ == ' ' && *p1++ == '[')
5821 {
5822 int n = 0;
5823 while (*p1 >= '0' && *p1 <= '9')
5824 n = n * 10 + *p1++ - '0';
5825 if (strncmp (p1, " times]\n", 8) == 0)
5826 return n+1;
5827 }
5828 return 0;
5829}
5830
5831
1e313f28
GM
5832/* Display an echo area message M with a specified length of NBYTES
5833 bytes. The string may include null characters. If M is 0, clear
5834 out any existing message, and let the mini-buffer text show
5835 through.
5f5c8ee5
GM
5836
5837 The buffer M must continue to exist until after the echo area gets
5838 cleared or some other message gets displayed there. This means do
5839 not pass text that is stored in a Lisp string; do not pass text in
5840 a buffer that was alloca'd. */
5841
5842void
1e313f28 5843message2 (m, nbytes, multibyte)
5f5c8ee5 5844 char *m;
1e313f28 5845 int nbytes;
5f5c8ee5
GM
5846 int multibyte;
5847{
5848 /* First flush out any partial line written with print. */
5849 message_log_maybe_newline ();
5850 if (m)
1e313f28
GM
5851 message_dolog (m, nbytes, 1, multibyte);
5852 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5853}
5854
5855
5856/* The non-logging counterpart of message2. */
5857
5858void
1e313f28 5859message2_nolog (m, nbytes, multibyte)
5f5c8ee5 5860 char *m;
1e313f28 5861 int nbytes;
5f5c8ee5 5862{
886bd6f2 5863 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5864 message_enable_multibyte = multibyte;
5865
5866 if (noninteractive)
5867 {
5868 if (noninteractive_need_newline)
5869 putc ('\n', stderr);
5870 noninteractive_need_newline = 0;
5871 if (m)
1e313f28 5872 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
5873 if (cursor_in_echo_area == 0)
5874 fprintf (stderr, "\n");
5875 fflush (stderr);
5876 }
5877 /* A null message buffer means that the frame hasn't really been
5878 initialized yet. Error messages get reported properly by
5879 cmd_error, so this must be just an informative message; toss it. */
5880 else if (INTERACTIVE
886bd6f2
GM
5881 && sf->glyphs_initialized_p
5882 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5883 {
5884 Lisp_Object mini_window;
5885 struct frame *f;
5886
5887 /* Get the frame containing the mini-buffer
5888 that the selected frame is using. */
886bd6f2 5889 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5890 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5891
5892 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5893 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5894 && ! FRAME_VISIBLE_P (f))
5895 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5896
5897 if (m)
5898 {
1e313f28 5899 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5900 if (minibuffer_auto_raise)
5901 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5902 }
5903 else
c6e89d6c 5904 clear_message (1, 1);
5f5c8ee5 5905
c6e89d6c 5906 do_pending_window_change (0);
5f5c8ee5 5907 echo_area_display (1);
c6e89d6c 5908 do_pending_window_change (0);
5f5c8ee5
GM
5909 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5910 (*frame_up_to_date_hook) (f);
5911 }
5912}
5913
5914
c6e89d6c
GM
5915/* Display an echo area message M with a specified length of NBYTES
5916 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5917 string, clear out any existing message, and let the mini-buffer
5918 text show through. */
5919
5920void
c6e89d6c 5921message3 (m, nbytes, multibyte)
5f5c8ee5 5922 Lisp_Object m;
c6e89d6c 5923 int nbytes;
5f5c8ee5
GM
5924 int multibyte;
5925{
5926 struct gcpro gcpro1;
5927
5928 GCPRO1 (m);
5929
5930 /* First flush out any partial line written with print. */
5931 message_log_maybe_newline ();
5932 if (STRINGP (m))
c6e89d6c
GM
5933 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5934 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5935
5936 UNGCPRO;
5937}
5938
5939
5940/* The non-logging version of message3. */
5941
5942void
c6e89d6c 5943message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5944 Lisp_Object m;
c6e89d6c 5945 int nbytes, multibyte;
5f5c8ee5 5946{
886bd6f2 5947 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5948 message_enable_multibyte = multibyte;
5949
5950 if (noninteractive)
5951 {
5952 if (noninteractive_need_newline)
5953 putc ('\n', stderr);
5954 noninteractive_need_newline = 0;
5955 if (STRINGP (m))
c6e89d6c 5956 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5957 if (cursor_in_echo_area == 0)
5958 fprintf (stderr, "\n");
5959 fflush (stderr);
5960 }
5961 /* A null message buffer means that the frame hasn't really been
5962 initialized yet. Error messages get reported properly by
5963 cmd_error, so this must be just an informative message; toss it. */
5964 else if (INTERACTIVE
886bd6f2
GM
5965 && sf->glyphs_initialized_p
5966 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5967 {
5968 Lisp_Object mini_window;
c6e89d6c 5969 Lisp_Object frame;
5f5c8ee5
GM
5970 struct frame *f;
5971
5972 /* Get the frame containing the mini-buffer
5973 that the selected frame is using. */
886bd6f2 5974 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5975 frame = XWINDOW (mini_window)->frame;
5976 f = XFRAME (frame);
5f5c8ee5
GM
5977
5978 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5979 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5980 && !FRAME_VISIBLE_P (f))
5981 Fmake_frame_visible (frame);
5f5c8ee5 5982
c6e89d6c 5983 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5984 {
c6e89d6c 5985 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5986 if (minibuffer_auto_raise)
5987 Fraise_frame (frame);
5f5c8ee5
GM
5988 }
5989 else
c6e89d6c 5990 clear_message (1, 1);
5f5c8ee5 5991
c6e89d6c 5992 do_pending_window_change (0);
5f5c8ee5 5993 echo_area_display (1);
c6e89d6c 5994 do_pending_window_change (0);
5f5c8ee5
GM
5995 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5996 (*frame_up_to_date_hook) (f);
5997 }
5998}
5999
6000
6001/* Display a null-terminated echo area message M. If M is 0, clear
6002 out any existing message, and let the mini-buffer text show through.
6003
6004 The buffer M must continue to exist until after the echo area gets
6005 cleared or some other message gets displayed there. Do not pass
6006 text that is stored in a Lisp string. Do not pass text in a buffer
6007 that was alloca'd. */
6008
6009void
6010message1 (m)
6011 char *m;
6012{
6013 message2 (m, (m ? strlen (m) : 0), 0);
6014}
6015
6016
6017/* The non-logging counterpart of message1. */
6018
6019void
6020message1_nolog (m)
6021 char *m;
6022{
6023 message2_nolog (m, (m ? strlen (m) : 0), 0);
6024}
6025
6026/* Display a message M which contains a single %s
6027 which gets replaced with STRING. */
6028
6029void
6030message_with_string (m, string, log)
6031 char *m;
6032 Lisp_Object string;
6033 int log;
6034{
6035 if (noninteractive)
6036 {
6037 if (m)
6038 {
6039 if (noninteractive_need_newline)
6040 putc ('\n', stderr);
6041 noninteractive_need_newline = 0;
6042 fprintf (stderr, m, XSTRING (string)->data);
6043 if (cursor_in_echo_area == 0)
6044 fprintf (stderr, "\n");
6045 fflush (stderr);
6046 }
6047 }
6048 else if (INTERACTIVE)
6049 {
6050 /* The frame whose minibuffer we're going to display the message on.
6051 It may be larger than the selected frame, so we need
6052 to use its buffer, not the selected frame's buffer. */
6053 Lisp_Object mini_window;
886bd6f2 6054 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6055
6056 /* Get the frame containing the minibuffer
6057 that the selected frame is using. */
886bd6f2 6058 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6059 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6060
6061 /* A null message buffer means that the frame hasn't really been
6062 initialized yet. Error messages get reported properly by
6063 cmd_error, so this must be just an informative message; toss it. */
6064 if (FRAME_MESSAGE_BUF (f))
6065 {
eb484132
GM
6066 Lisp_Object args[2], message;
6067 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6068
eb484132
GM
6069 args[0] = build_string (m);
6070 args[1] = message = string;
6071 GCPRO2 (args, message);
6072 gcpro1.nvars = 2;
6073
6074 message = Fformat (2, args);
5f5c8ee5
GM
6075
6076 if (log)
eb484132
GM
6077 message3 (message, STRING_BYTES (XSTRING (message)),
6078 STRING_MULTIBYTE (message));
5f5c8ee5 6079 else
eb484132
GM
6080 message3_nolog (message, STRING_BYTES (XSTRING (message)),
6081 STRING_MULTIBYTE (message));
6082
6083 UNGCPRO;
5f5c8ee5
GM
6084
6085 /* Print should start at the beginning of the message
6086 buffer next time. */
6087 message_buf_print = 0;
6088 }
6089 }
6090}
6091
6092
5f5c8ee5
GM
6093/* Dump an informative message to the minibuf. If M is 0, clear out
6094 any existing message, and let the mini-buffer text show through. */
6095
6096/* VARARGS 1 */
6097void
6098message (m, a1, a2, a3)
6099 char *m;
6100 EMACS_INT a1, a2, a3;
6101{
6102 if (noninteractive)
6103 {
6104 if (m)
6105 {
6106 if (noninteractive_need_newline)
6107 putc ('\n', stderr);
6108 noninteractive_need_newline = 0;
6109 fprintf (stderr, m, a1, a2, a3);
6110 if (cursor_in_echo_area == 0)
6111 fprintf (stderr, "\n");
6112 fflush (stderr);
6113 }
6114 }
6115 else if (INTERACTIVE)
6116 {
6117 /* The frame whose mini-buffer we're going to display the message
6118 on. It may be larger than the selected frame, so we need to
6119 use its buffer, not the selected frame's buffer. */
6120 Lisp_Object mini_window;
886bd6f2 6121 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6122
6123 /* Get the frame containing the mini-buffer
6124 that the selected frame is using. */
886bd6f2 6125 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6126 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6127
6128 /* A null message buffer means that the frame hasn't really been
6129 initialized yet. Error messages get reported properly by
6130 cmd_error, so this must be just an informative message; toss
6131 it. */
6132 if (FRAME_MESSAGE_BUF (f))
6133 {
6134 if (m)
6135 {
6136 int len;
6137#ifdef NO_ARG_ARRAY
6138 char *a[3];
6139 a[0] = (char *) a1;
6140 a[1] = (char *) a2;
6141 a[2] = (char *) a3;
6142
6143 len = doprnt (FRAME_MESSAGE_BUF (f),
6144 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6145#else
6146 len = doprnt (FRAME_MESSAGE_BUF (f),
6147 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6148 (char **) &a1);
6149#endif /* NO_ARG_ARRAY */
6150
6151 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6152 }
6153 else
6154 message1 (0);
6155
6156 /* Print should start at the beginning of the message
6157 buffer next time. */
6158 message_buf_print = 0;
6159 }
6160 }
6161}
6162
6163
6164/* The non-logging version of message. */
6165
6166void
6167message_nolog (m, a1, a2, a3)
6168 char *m;
6169 EMACS_INT a1, a2, a3;
6170{
6171 Lisp_Object old_log_max;
6172 old_log_max = Vmessage_log_max;
6173 Vmessage_log_max = Qnil;
6174 message (m, a1, a2, a3);
6175 Vmessage_log_max = old_log_max;
6176}
6177
6178
c6e89d6c
GM
6179/* Display the current message in the current mini-buffer. This is
6180 only called from error handlers in process.c, and is not time
6181 critical. */
5f5c8ee5
GM
6182
6183void
6184update_echo_area ()
6185{
c6e89d6c
GM
6186 if (!NILP (echo_area_buffer[0]))
6187 {
6188 Lisp_Object string;
6189 string = Fcurrent_message ();
6190 message3 (string, XSTRING (string)->size,
6191 !NILP (current_buffer->enable_multibyte_characters));
6192 }
6193}
6194
6195
5bcfeb49
GM
6196/* Make sure echo area buffers in echo_buffers[] are life. If they
6197 aren't, make new ones. */
6198
6199static void
6200ensure_echo_area_buffers ()
6201{
6202 int i;
6203
6204 for (i = 0; i < 2; ++i)
6205 if (!BUFFERP (echo_buffer[i])
6206 || NILP (XBUFFER (echo_buffer[i])->name))
6207 {
6208 char name[30];
ff3d9573
GM
6209 Lisp_Object old_buffer;
6210 int j;
6211
6212 old_buffer = echo_buffer[i];
5bcfeb49
GM
6213 sprintf (name, " *Echo Area %d*", i);
6214 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6215 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6216
6217 for (j = 0; j < 2; ++j)
6218 if (EQ (old_buffer, echo_area_buffer[j]))
6219 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6220 }
6221}
6222
6223
23a96c77 6224/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6225 echo_area_buffer as current buffer.
6226
6227 WHICH zero means use the current message buffer
6228 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6229 from echo_buffer[] and clear it.
6230
6231 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6232 suitable buffer from echo_buffer[] and clear it.
6233
6234 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6235 that the current message becomes the last displayed one, make
6236 choose a suitable buffer for echo_area_buffer[0], and clear it.
6237
4b41cebb 6238 Value is what FN returns. */
c6e89d6c
GM
6239
6240static int
23a96c77 6241with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6242 struct window *w;
6243 int which;
23dd2d97
KR
6244 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6245 EMACS_INT a1;
6246 Lisp_Object a2;
6247 EMACS_INT a3, a4;
c6e89d6c
GM
6248{
6249 Lisp_Object buffer;
15e26c76 6250 int this_one, the_other, clear_buffer_p, rc;
2d27dae2 6251 int count = BINDING_STACK_SIZE ();
c6e89d6c
GM
6252
6253 /* If buffers aren't life, make new ones. */
5bcfeb49 6254 ensure_echo_area_buffers ();
c6e89d6c
GM
6255
6256 clear_buffer_p = 0;
6257
6258 if (which == 0)
6259 this_one = 0, the_other = 1;
6260 else if (which > 0)
6261 this_one = 1, the_other = 0;
5f5c8ee5 6262 else
c6e89d6c
GM
6263 {
6264 this_one = 0, the_other = 1;
6265 clear_buffer_p = 1;
6266
6267 /* We need a fresh one in case the current echo buffer equals
6268 the one containing the last displayed echo area message. */
6269 if (!NILP (echo_area_buffer[this_one])
6270 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6271 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6272 }
6273
6274 /* Choose a suitable buffer from echo_buffer[] is we don't
6275 have one. */
6276 if (NILP (echo_area_buffer[this_one]))
6277 {
6278 echo_area_buffer[this_one]
6279 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6280 ? echo_buffer[the_other]
6281 : echo_buffer[this_one]);
6282 clear_buffer_p = 1;
6283 }
6284
6285 buffer = echo_area_buffer[this_one];
6286
1013f4e3
GM
6287 /* Don't get confused by reusing the buffer used for echoing
6288 for a different purpose. */
032906b1 6289 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6290 cancel_echoing ();
6291
c6e89d6c
GM
6292 record_unwind_protect (unwind_with_echo_area_buffer,
6293 with_echo_area_buffer_unwind_data (w));
6294
6295 /* Make the echo area buffer current. Note that for display
6296 purposes, it is not necessary that the displayed window's buffer
6297 == current_buffer, except for text property lookup. So, let's
6298 only set that buffer temporarily here without doing a full
6299 Fset_window_buffer. We must also change w->pointm, though,
6300 because otherwise an assertions in unshow_buffer fails, and Emacs
6301 aborts. */
9142dd5b 6302 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6303 if (w)
6304 {
6305 w->buffer = buffer;
6306 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6307 }
ad4f174e 6308
c6e89d6c
GM
6309 current_buffer->undo_list = Qt;
6310 current_buffer->read_only = Qnil;
bbbf6d06 6311 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6312 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6313
6314 if (clear_buffer_p && Z > BEG)
6315 del_range (BEG, Z);
6316
6317 xassert (BEGV >= BEG);
6318 xassert (ZV <= Z && ZV >= BEGV);
6319
23a96c77 6320 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6321
6322 xassert (BEGV >= BEG);
6323 xassert (ZV <= Z && ZV >= BEGV);
6324
6325 unbind_to (count, Qnil);
6326 return rc;
5f5c8ee5
GM
6327}
6328
6329
c6e89d6c
GM
6330/* Save state that should be preserved around the call to the function
6331 FN called in with_echo_area_buffer. */
5f5c8ee5 6332
c6e89d6c
GM
6333static Lisp_Object
6334with_echo_area_buffer_unwind_data (w)
6335 struct window *w;
5f5c8ee5 6336{
c6e89d6c
GM
6337 int i = 0;
6338 Lisp_Object vector;
5f5c8ee5 6339
c6e89d6c
GM
6340 /* Reduce consing by keeping one vector in
6341 Vwith_echo_area_save_vector. */
6342 vector = Vwith_echo_area_save_vector;
6343 Vwith_echo_area_save_vector = Qnil;
6344
6345 if (NILP (vector))
9142dd5b 6346 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c 6347
a61b7058
GM
6348 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6349 AREF (vector, i) = Vdeactivate_mark, ++i;
6350 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
c6e89d6c
GM
6351
6352 if (w)
6353 {
a61b7058
GM
6354 XSETWINDOW (AREF (vector, i), w); ++i;
6355 AREF (vector, i) = w->buffer; ++i;
6356 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6357 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6358 }
6359 else
6360 {
6361 int end = i + 4;
a61b7058
GM
6362 for (; i < end; ++i)
6363 AREF (vector, i) = Qnil;
c6e89d6c 6364 }
5f5c8ee5 6365
a61b7058 6366 xassert (i == ASIZE (vector));
c6e89d6c
GM
6367 return vector;
6368}
5f5c8ee5 6369
5f5c8ee5 6370
c6e89d6c
GM
6371/* Restore global state from VECTOR which was created by
6372 with_echo_area_buffer_unwind_data. */
6373
6374static Lisp_Object
6375unwind_with_echo_area_buffer (vector)
6376 Lisp_Object vector;
6377{
bbbf6d06
GM
6378 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6379 Vdeactivate_mark = AREF (vector, 1);
6380 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6381
bbbf6d06 6382 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6383 {
6384 struct window *w;
6385 Lisp_Object buffer, charpos, bytepos;
6386
bbbf6d06
GM
6387 w = XWINDOW (AREF (vector, 3));
6388 buffer = AREF (vector, 4);
6389 charpos = AREF (vector, 5);
6390 bytepos = AREF (vector, 6);
c6e89d6c
GM
6391
6392 w->buffer = buffer;
6393 set_marker_both (w->pointm, buffer,
6394 XFASTINT (charpos), XFASTINT (bytepos));
6395 }
6396
6397 Vwith_echo_area_save_vector = vector;
6398 return Qnil;
6399}
6400
6401
6402/* Set up the echo area for use by print functions. MULTIBYTE_P
6403 non-zero means we will print multibyte. */
6404
6405void
6406setup_echo_area_for_printing (multibyte_p)
6407 int multibyte_p;
6408{
5bcfeb49
GM
6409 ensure_echo_area_buffers ();
6410
c6e89d6c
GM
6411 if (!message_buf_print)
6412 {
6413 /* A message has been output since the last time we printed.
6414 Choose a fresh echo area buffer. */
6415 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6416 echo_area_buffer[0] = echo_buffer[1];
6417 else
6418 echo_area_buffer[0] = echo_buffer[0];
6419
6420 /* Switch to that buffer and clear it. */
6421 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6422 current_buffer->truncate_lines = Qnil;
bbbf6d06 6423
c6e89d6c 6424 if (Z > BEG)
bbbf6d06
GM
6425 {
6426 int count = BINDING_STACK_SIZE ();
6427 specbind (Qinhibit_read_only, Qt);
6428 del_range (BEG, Z);
6429 unbind_to (count, Qnil);
6430 }
c6e89d6c
GM
6431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6432
6433 /* Set up the buffer for the multibyteness we need. */
6434 if (multibyte_p
6435 != !NILP (current_buffer->enable_multibyte_characters))
6436 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6437
6438 /* Raise the frame containing the echo area. */
6439 if (minibuffer_auto_raise)
6440 {
886bd6f2 6441 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6442 Lisp_Object mini_window;
886bd6f2 6443 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6445 }
6446
8a4e3c0c 6447 message_log_maybe_newline ();
c6e89d6c
GM
6448 message_buf_print = 1;
6449 }
fa77249f
GM
6450 else
6451 {
6452 if (NILP (echo_area_buffer[0]))
6453 {
6454 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6455 echo_area_buffer[0] = echo_buffer[1];
6456 else
6457 echo_area_buffer[0] = echo_buffer[0];
6458 }
6459
6460 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
6461 {
6462 /* Someone switched buffers between print requests. */
6463 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6464 current_buffer->truncate_lines = Qnil;
6465 }
fa77249f 6466 }
c6e89d6c
GM
6467}
6468
6469
dd2eb166
GM
6470/* Display an echo area message in window W. Value is non-zero if W's
6471 height is changed. If display_last_displayed_message_p is
6472 non-zero, display the message that was last displayed, otherwise
6473 display the current message. */
c6e89d6c
GM
6474
6475static int
6476display_echo_area (w)
6477 struct window *w;
6478{
25edb08f
GM
6479 int i, no_message_p, window_height_changed_p, count;
6480
6481 /* Temporarily disable garbage collections while displaying the echo
6482 area. This is done because a GC can print a message itself.
6483 That message would modify the echo area buffer's contents while a
6484 redisplay of the buffer is going on, and seriously confuse
6485 redisplay. */
6486 count = inhibit_garbage_collection ();
dd2eb166
GM
6487
6488 /* If there is no message, we must call display_echo_area_1
6489 nevertheless because it resizes the window. But we will have to
6490 reset the echo_area_buffer in question to nil at the end because
6491 with_echo_area_buffer will sets it to an empty buffer. */
6492 i = display_last_displayed_message_p ? 1 : 0;
6493 no_message_p = NILP (echo_area_buffer[i]);
6494
6495 window_height_changed_p
6496 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6497 display_echo_area_1,
23dd2d97 6498 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6499
6500 if (no_message_p)
6501 echo_area_buffer[i] = Qnil;
25edb08f
GM
6502
6503 unbind_to (count, Qnil);
dd2eb166 6504 return window_height_changed_p;
c6e89d6c
GM
6505}
6506
6507
6508/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6509 contains the current echo area message in window W, a mini-window,
6510 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6511 Change the height of W so that all of the message is displayed.
6512 Value is non-zero if height of W was changed. */
6513
6514static int
23a96c77 6515display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6516 EMACS_INT a1;
6517 Lisp_Object a2;
6518 EMACS_INT a3, a4;
c6e89d6c 6519{
23a96c77 6520 struct window *w = (struct window *) a1;
c6e89d6c 6521 Lisp_Object window;
c6e89d6c
GM
6522 struct text_pos start;
6523 int window_height_changed_p = 0;
6524
6525 /* Do this before displaying, so that we have a large enough glyph
6526 matrix for the display. */
92a90e89 6527 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6528
6529 /* Display. */
6530 clear_glyph_matrix (w->desired_matrix);
6531 XSETWINDOW (window, w);
6532 SET_TEXT_POS (start, BEG, BEG_BYTE);
6533 try_window (window, start);
6534
c6e89d6c
GM
6535 return window_height_changed_p;
6536}
6537
6538
92a90e89 6539/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
6540 currently displayed message, if there is one. If a mini-buffer
6541 is active, don't shrink it. */
92a90e89
GM
6542
6543void
308a74d8 6544resize_echo_area_exactly ()
92a90e89
GM
6545{
6546 if (BUFFERP (echo_area_buffer[0])
6547 && WINDOWP (echo_area_window))
6548 {
6549 struct window *w = XWINDOW (echo_area_window);
6550 int resized_p;
6d004fea
GM
6551 Lisp_Object resize_exactly;
6552
6553 if (minibuf_level == 0)
6554 resize_exactly = Qt;
6555 else
6556 resize_exactly = Qnil;
92a90e89 6557
23a96c77 6558 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 6559 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
6560 if (resized_p)
6561 {
6562 ++windows_or_buffers_changed;
6563 ++update_mode_lines;
6564 redisplay_internal (0);
6565 }
6566 }
6567}
6568
6569
23a96c77 6570/* Callback function for with_echo_area_buffer, when used from
308a74d8 6571 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
6572 resize, EXACTLY non-nil means resize the mini-window exactly to the
6573 size of the text displayed. A3 and A4 are not used. Value is what
6574 resize_mini_window returns. */
23a96c77
GM
6575
6576static int
6d004fea 6577resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 6578 EMACS_INT a1;
6d004fea 6579 Lisp_Object exactly;
23dd2d97 6580 EMACS_INT a3, a4;
23a96c77 6581{
6d004fea 6582 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
6583}
6584
6585
92a90e89
GM
6586/* Resize mini-window W to fit the size of its contents. EXACT:P
6587 means size the window exactly to the size needed. Otherwise, it's
6588 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 6589 the window height has been changed. */
c6e89d6c 6590
9472f927 6591int
92a90e89 6592resize_mini_window (w, exact_p)
c6e89d6c 6593 struct window *w;
92a90e89 6594 int exact_p;
c6e89d6c
GM
6595{
6596 struct frame *f = XFRAME (w->frame);
6597 int window_height_changed_p = 0;
6598
6599 xassert (MINI_WINDOW_P (w));
97cafc0f 6600
2913a9c0
GM
6601 /* Don't resize windows while redisplaying a window; it would
6602 confuse redisplay functions when the size of the window they are
6603 displaying changes from under them. Such a resizing can happen,
6604 for instance, when which-func prints a long message while
6605 we are running fontification-functions. We're running these
6606 functions with safe_call which binds inhibit-redisplay to t. */
6607 if (!NILP (Vinhibit_redisplay))
64c5be50
GM
6608 return 0;
6609
97cafc0f 6610 /* Nil means don't try to resize. */
6422c1d7 6611 if (NILP (Vresize_mini_windows)
00f6d59e 6612 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6613 return 0;
c6e89d6c
GM
6614
6615 if (!FRAME_MINIBUF_ONLY_P (f))
6616 {
6617 struct it it;
dd2eb166
GM
6618 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6619 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6620 int height, max_height;
6621 int unit = CANON_Y_UNIT (f);
6622 struct text_pos start;
1bfdbe43
GM
6623 struct buffer *old_current_buffer = NULL;
6624
6625 if (current_buffer != XBUFFER (w->buffer))
6626 {
6627 old_current_buffer = current_buffer;
6628 set_buffer_internal (XBUFFER (w->buffer));
6629 }
9142dd5b 6630
c6e89d6c 6631 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6632
dd2eb166
GM
6633 /* Compute the max. number of lines specified by the user. */
6634 if (FLOATP (Vmax_mini_window_height))
18f9986a 6635 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
dd2eb166
GM
6636 else if (INTEGERP (Vmax_mini_window_height))
6637 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6638 else
6639 max_height = total_height / 4;
dd2eb166 6640
4b41cebb 6641 /* Correct that max. height if it's bogus. */
dd2eb166
GM
6642 max_height = max (1, max_height);
6643 max_height = min (total_height, max_height);
6644
6645 /* Find out the height of the text in the window. */
ad4f174e
GM
6646 if (it.truncate_lines_p)
6647 height = 1;
55b064bd 6648 else
ad4f174e
GM
6649 {
6650 last_height = 0;
6651 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6652 if (it.max_ascent == 0 && it.max_descent == 0)
6653 height = it.current_y + last_height;
6654 else
6655 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6656 height -= it.extra_line_spacing;
ad4f174e
GM
6657 height = (height + unit - 1) / unit;
6658 }
dd2eb166
GM
6659
6660 /* Compute a suitable window start. */
6661 if (height > max_height)
6662 {
6663 height = max_height;
6664 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6665 move_it_vertically_backward (&it, (height - 1) * unit);
6666 start = it.current.pos;
6667 }
6668 else
6669 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6670 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6671
6422c1d7 6672 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6673 {
6422c1d7
GM
6674 /* Let it grow only, until we display an empty message, in which
6675 case the window shrinks again. */
6676 if (height > XFASTINT (w->height))
6677 {
6678 int old_height = XFASTINT (w->height);
6679 freeze_window_starts (f, 1);
6680 grow_mini_window (w, height - XFASTINT (w->height));
6681 window_height_changed_p = XFASTINT (w->height) != old_height;
6682 }
6683 else if (height < XFASTINT (w->height)
6684 && (exact_p || BEGV == ZV))
6685 {
6686 int old_height = XFASTINT (w->height);
6687 freeze_window_starts (f, 0);
6688 shrink_mini_window (w);
6689 window_height_changed_p = XFASTINT (w->height) != old_height;
6690 }
da448723 6691 }
6422c1d7 6692 else
da448723 6693 {
6422c1d7
GM
6694 /* Always resize to exact size needed. */
6695 if (height > XFASTINT (w->height))
6696 {
6697 int old_height = XFASTINT (w->height);
6698 freeze_window_starts (f, 1);
6699 grow_mini_window (w, height - XFASTINT (w->height));
6700 window_height_changed_p = XFASTINT (w->height) != old_height;
6701 }
6702 else if (height < XFASTINT (w->height))
6703 {
6704 int old_height = XFASTINT (w->height);
6705 freeze_window_starts (f, 0);
6706 shrink_mini_window (w);
6707
6708 if (height)
6709 {
6710 freeze_window_starts (f, 1);
6711 grow_mini_window (w, height - XFASTINT (w->height));
6712 }
6713
6714 window_height_changed_p = XFASTINT (w->height) != old_height;
6715 }
9142dd5b 6716 }
1bfdbe43
GM
6717
6718 if (old_current_buffer)
6719 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6720 }
6721
6722 return window_height_changed_p;
6723}
6724
6725
6726/* Value is the current message, a string, or nil if there is no
6727 current message. */
6728
6729Lisp_Object
6730current_message ()
6731{
6732 Lisp_Object msg;
6733
6734 if (NILP (echo_area_buffer[0]))
6735 msg = Qnil;
6736 else
6737 {
23a96c77 6738 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6739 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6740 if (NILP (msg))
6741 echo_area_buffer[0] = Qnil;
6742 }
6743
6744 return msg;
6745}
6746
6747
6748static int
23a96c77 6749current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6750 EMACS_INT a1;
6751 Lisp_Object a2;
6752 EMACS_INT a3, a4;
c6e89d6c 6753{
23a96c77
GM
6754 Lisp_Object *msg = (Lisp_Object *) a1;
6755
c6e89d6c
GM
6756 if (Z > BEG)
6757 *msg = make_buffer_string (BEG, Z, 1);
6758 else
6759 *msg = Qnil;
6760 return 0;
6761}
6762
6763
6764/* Push the current message on Vmessage_stack for later restauration
6765 by restore_message. Value is non-zero if the current message isn't
6766 empty. This is a relatively infrequent operation, so it's not
6767 worth optimizing. */
6768
6769int
6770push_message ()
6771{
6772 Lisp_Object msg;
6773 msg = current_message ();
6774 Vmessage_stack = Fcons (msg, Vmessage_stack);
6775 return STRINGP (msg);
6776}
6777
6778
098ec84a
GM
6779/* Handler for record_unwind_protect calling pop_message. */
6780
6781Lisp_Object
6782push_message_unwind (dummy)
6783 Lisp_Object dummy;
6784{
6785 pop_message ();
6786 return Qnil;
6787}
6788
6789
c6e89d6c
GM
6790/* Restore message display from the top of Vmessage_stack. */
6791
6792void
6793restore_message ()
6794{
6795 Lisp_Object msg;
6796
6797 xassert (CONSP (Vmessage_stack));
6798 msg = XCAR (Vmessage_stack);
6799 if (STRINGP (msg))
6800 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6801 else
6802 message3_nolog (msg, 0, 0);
6803}
6804
6805
6806/* Pop the top-most entry off Vmessage_stack. */
6807
6808void
6809pop_message ()
6810{
6811 xassert (CONSP (Vmessage_stack));
6812 Vmessage_stack = XCDR (Vmessage_stack);
6813}
6814
6815
6816/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6817 exits. If the stack is not empty, we have a missing pop_message
6818 somewhere. */
6819
6820void
6821check_message_stack ()
6822{
6823 if (!NILP (Vmessage_stack))
6824 abort ();
6825}
6826
6827
6828/* Truncate to NCHARS what will be displayed in the echo area the next
6829 time we display it---but don't redisplay it now. */
6830
6831void
6832truncate_echo_area (nchars)
6833 int nchars;
6834{
6835 if (nchars == 0)
6836 echo_area_buffer[0] = Qnil;
6837 /* A null message buffer means that the frame hasn't really been
6838 initialized yet. Error messages get reported properly by
6839 cmd_error, so this must be just an informative message; toss it. */
6840 else if (!noninteractive
6841 && INTERACTIVE
c6e89d6c 6842 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6843 {
6844 struct frame *sf = SELECTED_FRAME ();
6845 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6846 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6847 }
c6e89d6c
GM
6848}
6849
6850
6851/* Helper function for truncate_echo_area. Truncate the current
6852 message to at most NCHARS characters. */
6853
6854static int
23a96c77 6855truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6856 EMACS_INT nchars;
6857 Lisp_Object a2;
6858 EMACS_INT a3, a4;
c6e89d6c
GM
6859{
6860 if (BEG + nchars < Z)
6861 del_range (BEG + nchars, Z);
6862 if (Z == BEG)
6863 echo_area_buffer[0] = Qnil;
6864 return 0;
6865}
6866
6867
6868/* Set the current message to a substring of S or STRING.
6869
6870 If STRING is a Lisp string, set the message to the first NBYTES
6871 bytes from STRING. NBYTES zero means use the whole string. If
6872 STRING is multibyte, the message will be displayed multibyte.
6873
6874 If S is not null, set the message to the first LEN bytes of S. LEN
6875 zero means use the whole string. MULTIBYTE_P non-zero means S is
6876 multibyte. Display the message multibyte in that case. */
6877
6878void
6879set_message (s, string, nbytes, multibyte_p)
6880 char *s;
6881 Lisp_Object string;
6882 int nbytes;
6883{
6884 message_enable_multibyte
6885 = ((s && multibyte_p)
6886 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6887
23a96c77
GM
6888 with_echo_area_buffer (0, -1, set_message_1,
6889 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6890 message_buf_print = 0;
21fdfb65 6891 help_echo_showing_p = 0;
c6e89d6c
GM
6892}
6893
6894
6895/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6896 as there, with A1 corresponding to S and A2 corresponding to STRING
6897 This function is called with the echo area buffer being
c6e89d6c
GM
6898 current. */
6899
6900static int
23a96c77 6901set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6902 EMACS_INT a1;
6903 Lisp_Object a2;
6904 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6905{
23a96c77 6906 char *s = (char *) a1;
23dd2d97 6907 Lisp_Object string = a2;
23a96c77 6908
c6e89d6c
GM
6909 xassert (BEG == Z);
6910
6911 /* Change multibyteness of the echo buffer appropriately. */
6912 if (message_enable_multibyte
6913 != !NILP (current_buffer->enable_multibyte_characters))
6914 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6915
ad4f174e
GM
6916 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6917
c6e89d6c
GM
6918 /* Insert new message at BEG. */
6919 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6920
6921 if (STRINGP (string))
6922 {
6923 int nchars;
6924
6925 if (nbytes == 0)
6926 nbytes = XSTRING (string)->size_byte;
6927 nchars = string_byte_to_char (string, nbytes);
6928
6929 /* This function takes care of single/multibyte conversion. We
6930 just have to ensure that the echo area buffer has the right
6931 setting of enable_multibyte_characters. */
6932 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6933 }
6934 else if (s)
6935 {
6936 if (nbytes == 0)
6937 nbytes = strlen (s);
6938
6939 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6940 {
6941 /* Convert from multi-byte to single-byte. */
6942 int i, c, n;
6943 unsigned char work[1];
6944
6945 /* Convert a multibyte string to single-byte. */
6946 for (i = 0; i < nbytes; i += n)
6947 {
6948 c = string_char_and_length (s + i, nbytes - i, &n);
6949 work[0] = (SINGLE_BYTE_CHAR_P (c)
6950 ? c
6951 : multibyte_char_to_unibyte (c, Qnil));
6952 insert_1_both (work, 1, 1, 1, 0, 0);
6953 }
6954 }
6955 else if (!multibyte_p
6956 && !NILP (current_buffer->enable_multibyte_characters))
6957 {
6958 /* Convert from single-byte to multi-byte. */
6959 int i, c, n;
6960 unsigned char *msg = (unsigned char *) s;
260a86a0 6961 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6962
6963 /* Convert a single-byte string to multibyte. */
6964 for (i = 0; i < nbytes; i++)
6965 {
6966 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6967 n = CHAR_STRING (c, str);
6968 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6969 }
6970 }
6971 else
6972 insert_1 (s, nbytes, 1, 0, 0);
6973 }
6974
6975 return 0;
6976}
6977
6978
6979/* Clear messages. CURRENT_P non-zero means clear the current
6980 message. LAST_DISPLAYED_P non-zero means clear the message
6981 last displayed. */
6982
6983void
6984clear_message (current_p, last_displayed_p)
6985 int current_p, last_displayed_p;
6986{
6987 if (current_p)
6e019995
GM
6988 {
6989 echo_area_buffer[0] = Qnil;
6990 message_cleared_p = 1;
6991 }
c6e89d6c
GM
6992
6993 if (last_displayed_p)
6994 echo_area_buffer[1] = Qnil;
6995
6996 message_buf_print = 0;
6997}
6998
6999/* Clear garbaged frames.
7000
7001 This function is used where the old redisplay called
7002 redraw_garbaged_frames which in turn called redraw_frame which in
7003 turn called clear_frame. The call to clear_frame was a source of
7004 flickering. I believe a clear_frame is not necessary. It should
7005 suffice in the new redisplay to invalidate all current matrices,
7006 and ensure a complete redisplay of all windows. */
7007
7008static void
7009clear_garbaged_frames ()
7010{
5f5c8ee5
GM
7011 if (frame_garbaged)
7012 {
5f5c8ee5
GM
7013 Lisp_Object tail, frame;
7014
7015 FOR_EACH_FRAME (tail, frame)
7016 {
7017 struct frame *f = XFRAME (frame);
7018
7019 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7020 {
6bb95882 7021 if (f->resized_p)
573e57f1 7022 Fredraw_frame (frame);
5f5c8ee5
GM
7023 clear_current_matrices (f);
7024 f->garbaged = 0;
6bb95882 7025 f->resized_p = 0;
5f5c8ee5
GM
7026 }
7027 }
7028
7029 frame_garbaged = 0;
7030 ++windows_or_buffers_changed;
7031 }
c6e89d6c 7032}
5f5c8ee5 7033
5f5c8ee5 7034
886bd6f2
GM
7035/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7036 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7037 mini-windows height has been changed. */
5f5c8ee5 7038
c6e89d6c
GM
7039static int
7040echo_area_display (update_frame_p)
7041 int update_frame_p;
7042{
7043 Lisp_Object mini_window;
7044 struct window *w;
7045 struct frame *f;
7046 int window_height_changed_p = 0;
886bd6f2 7047 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7048
886bd6f2 7049 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7050 w = XWINDOW (mini_window);
7051 f = XFRAME (WINDOW_FRAME (w));
7052
7053 /* Don't display if frame is invisible or not yet initialized. */
7054 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7055 return 0;
5f5c8ee5 7056
1a578e9b
AC
7057/* The terminal frame is used as the first Emacs frame on the Mac OS. */
7058#ifndef macintosh
1ab3e082 7059#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7060 /* When Emacs starts, selected_frame may be a visible terminal
7061 frame, even if we run under a window system. If we let this
7062 through, a message would be displayed on the terminal. */
622e3754
GM
7063 if (EQ (selected_frame, Vterminal_frame)
7064 && !NILP (Vwindow_system))
c6e89d6c 7065 return 0;
1ab3e082 7066#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7067#endif
c6e89d6c
GM
7068
7069 /* Redraw garbaged frames. */
7070 if (frame_garbaged)
7071 clear_garbaged_frames ();
7072
7073 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7074 {
7075 echo_area_window = mini_window;
7076 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7077 w->must_be_updated_p = 1;
c59c668a 7078
d4358b37
GM
7079 /* Update the display, unless called from redisplay_internal.
7080 Also don't update the screen during redisplay itself. The
7081 update will happen at the end of redisplay, and an update
7082 here could cause confusion. */
7083 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7084 {
715e84c9 7085 int n = 0;
edc68111 7086
715e84c9
GM
7087 /* If the display update has been interrupted by pending
7088 input, update mode lines in the frame. Due to the
7089 pending input, it might have been that redisplay hasn't
7090 been called, so that mode lines above the echo area are
7091 garbaged. This looks odd, so we prevent it here. */
7092 if (!display_completed)
7093 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7094
8af1308e
GM
7095 if (window_height_changed_p
7096 /* Don't do this if Emacs is shutting down. Redisplay
7097 needs to run hooks. */
7098 && !NILP (Vrun_hooks))
c59c668a 7099 {
c06017fb
GM
7100 /* Must update other windows. Likewise as in other
7101 cases, don't let this update be interrupted by
7102 pending input. */
7103 int count = BINDING_STACK_SIZE ();
7104 specbind (Qredisplay_dont_pause, Qt);
edc68111 7105 windows_or_buffers_changed = 1;
c59c668a 7106 redisplay_internal (0);
c06017fb 7107 unbind_to (count, Qnil);
c59c668a 7108 }
715e84c9 7109 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7110 {
edc68111 7111 /* Window configuration is the same as before.
715e84c9
GM
7112 Can do with a display update of the echo area,
7113 unless we displayed some mode lines. */
5f5c8ee5
GM
7114 update_single_window (w, 1);
7115 rif->flush_display (f);
7116 }
7117 else
7118 update_frame (f, 1, 1);
31b6671b
GM
7119
7120 /* If cursor is in the echo area, make sure that the next
7121 redisplay displays the minibuffer, so that the cursor will
7122 be replaced with what the minibuffer wants. */
7123 if (cursor_in_echo_area)
7124 ++windows_or_buffers_changed;
5f5c8ee5
GM
7125 }
7126 }
7127 else if (!EQ (mini_window, selected_window))
7128 windows_or_buffers_changed++;
c59c668a
GM
7129
7130 /* Last displayed message is now the current message. */
dd2eb166
GM
7131 echo_area_buffer[1] = echo_area_buffer[0];
7132
5f5c8ee5
GM
7133 /* Prevent redisplay optimization in redisplay_internal by resetting
7134 this_line_start_pos. This is done because the mini-buffer now
7135 displays the message instead of its buffer text. */
7136 if (EQ (mini_window, selected_window))
7137 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7138
7139 return window_height_changed_p;
5f5c8ee5
GM
7140}
7141
7142
7143\f
7144/***********************************************************************
7145 Frame Titles
7146 ***********************************************************************/
7147
7148
7149#ifdef HAVE_WINDOW_SYSTEM
7150
7151/* A buffer for constructing frame titles in it; allocated from the
7152 heap in init_xdisp and resized as needed in store_frame_title_char. */
7153
7154static char *frame_title_buf;
7155
7156/* The buffer's end, and a current output position in it. */
7157
7158static char *frame_title_buf_end;
7159static char *frame_title_ptr;
7160
7161
7162/* Store a single character C for the frame title in frame_title_buf.
7163 Re-allocate frame_title_buf if necessary. */
7164
7165static void
7166store_frame_title_char (c)
7167 char c;
7168{
7169 /* If output position has reached the end of the allocated buffer,
7170 double the buffer's size. */
7171 if (frame_title_ptr == frame_title_buf_end)
7172 {
7173 int len = frame_title_ptr - frame_title_buf;
7174 int new_size = 2 * len * sizeof *frame_title_buf;
7175 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7176 frame_title_buf_end = frame_title_buf + new_size;
7177 frame_title_ptr = frame_title_buf + len;
7178 }
7179
7180 *frame_title_ptr++ = c;
7181}
7182
7183
7184/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7185 frame_title_ptr. STR is the string to store. Do not copy
7186 characters that yield more columns than PRECISION; PRECISION <= 0
7187 means copy the whole string. Pad with spaces until FIELD_WIDTH
7188 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7189 pad. Called from display_mode_element when it is used to build a
7190 frame title. */
5f5c8ee5
GM
7191
7192static int
7193store_frame_title (str, field_width, precision)
7194 unsigned char *str;
7195 int field_width, precision;
7196{
7197 int n = 0;
3b552d56 7198 int dummy, nbytes;
5f5c8ee5
GM
7199
7200 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7201 nbytes = strlen (str);
7202 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7203 while (nbytes--)
7204 store_frame_title_char (*str++);
5f5c8ee5
GM
7205
7206 /* Fill up with spaces until FIELD_WIDTH reached. */
7207 while (field_width > 0
7208 && n < field_width)
7209 {
7210 store_frame_title_char (' ');
7211 ++n;
7212 }
7213
7214 return n;
7215}
7216
7217
7218/* Set the title of FRAME, if it has changed. The title format is
7219 Vicon_title_format if FRAME is iconified, otherwise it is
7220 frame_title_format. */
7221
7222static void
7223x_consider_frame_title (frame)
7224 Lisp_Object frame;
7225{
7226 struct frame *f = XFRAME (frame);
7227
7228 if (FRAME_WINDOW_P (f)
7229 || FRAME_MINIBUF_ONLY_P (f)
7230 || f->explicit_name)
7231 {
7232 /* Do we have more than one visible frame on this X display? */
7233 Lisp_Object tail;
7234 Lisp_Object fmt;
7235 struct buffer *obuf;
7236 int len;
7237 struct it it;
7238
9472f927 7239 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7240 {
74779f52
JR
7241 Lisp_Object other_frame = XCAR (tail);
7242 struct frame *tf = XFRAME (other_frame);
5f5c8ee5
GM
7243
7244 if (tf != f
7245 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7246 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7247 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7248 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7249 break;
7250 }
7251
7252 /* Set global variable indicating that multiple frames exist. */
7253 multiple_frames = CONSP (tail);
7254
7255 /* Switch to the buffer of selected window of the frame. Set up
7256 frame_title_ptr so that display_mode_element will output into it;
7257 then display the title. */
7258 obuf = current_buffer;
bb336f8d 7259 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7260 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7261 frame_title_ptr = frame_title_buf;
7262 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7263 NULL, DEFAULT_FACE_ID);
0fcf414f 7264 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
d26b89b8 7265 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7266 frame_title_ptr = NULL;
bb336f8d 7267 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7268
7269 /* Set the title only if it's changed. This avoids consing in
7270 the common case where it hasn't. (If it turns out that we've
7271 already wasted too much time by walking through the list with
7272 display_mode_element, then we might need to optimize at a
7273 higher level than this.) */
7274 if (! STRINGP (f->name)
7275 || STRING_BYTES (XSTRING (f->name)) != len
7276 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7277 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7278 }
7279}
7280
7281#else /* not HAVE_WINDOW_SYSTEM */
7282
7283#define frame_title_ptr ((char *)0)
7284#define store_frame_title(str, mincol, maxcol) 0
7285
7286#endif /* not HAVE_WINDOW_SYSTEM */
7287
7288
7289
7290\f
7291/***********************************************************************
7292 Menu Bars
7293 ***********************************************************************/
7294
7295
7296/* Prepare for redisplay by updating menu-bar item lists when
7297 appropriate. This can call eval. */
7298
7299void
7300prepare_menu_bars ()
7301{
7302 int all_windows;
7303 struct gcpro gcpro1, gcpro2;
7304 struct frame *f;
6b5c4794 7305 Lisp_Object tooltip_frame;
5f5c8ee5 7306
86ffe5cd 7307#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7308 tooltip_frame = tip_frame;
7309#else
6b5c4794 7310 tooltip_frame = Qnil;
5f5c8ee5
GM
7311#endif
7312
7313 /* Update all frame titles based on their buffer names, etc. We do
7314 this before the menu bars so that the buffer-menu will show the
7315 up-to-date frame titles. */
7316#ifdef HAVE_WINDOW_SYSTEM
7317 if (windows_or_buffers_changed || update_mode_lines)
7318 {
7319 Lisp_Object tail, frame;
7320
7321 FOR_EACH_FRAME (tail, frame)
7322 {
7323 f = XFRAME (frame);
6b5c4794 7324 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7325 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7326 x_consider_frame_title (frame);
7327 }
7328 }
7329#endif /* HAVE_WINDOW_SYSTEM */
7330
7331 /* Update the menu bar item lists, if appropriate. This has to be
7332 done before any actual redisplay or generation of display lines. */
7333 all_windows = (update_mode_lines
7334 || buffer_shared > 1
7335 || windows_or_buffers_changed);
7336 if (all_windows)
7337 {
7338 Lisp_Object tail, frame;
2d27dae2 7339 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
7340
7341 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7342
7343 FOR_EACH_FRAME (tail, frame)
7344 {
7345 f = XFRAME (frame);
7346
7347 /* Ignore tooltip frame. */
6b5c4794 7348 if (EQ (frame, tooltip_frame))
5f5c8ee5
GM
7349 continue;
7350
7351 /* If a window on this frame changed size, report that to
7352 the user and clear the size-change flag. */
7353 if (FRAME_WINDOW_SIZES_CHANGED (f))
7354 {
7355 Lisp_Object functions;
7356
7357 /* Clear flag first in case we get an error below. */
7358 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7359 functions = Vwindow_size_change_functions;
7360 GCPRO2 (tail, functions);
7361
7362 while (CONSP (functions))
7363 {
7364 call1 (XCAR (functions), frame);
7365 functions = XCDR (functions);
7366 }
7367 UNGCPRO;
7368 }
7369
7370 GCPRO1 (tail);
7371 update_menu_bar (f, 0);
7372#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7373 update_tool_bar (f, 0);
5f5c8ee5
GM
7374#endif
7375 UNGCPRO;
7376 }
7377
7378 unbind_to (count, Qnil);
7379 }
7380 else
7381 {
886bd6f2
GM
7382 struct frame *sf = SELECTED_FRAME ();
7383 update_menu_bar (sf, 1);
5f5c8ee5 7384#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7385 update_tool_bar (sf, 1);
5f5c8ee5
GM
7386#endif
7387 }
7388
7389 /* Motif needs this. See comment in xmenu.c. Turn it off when
7390 pending_menu_activation is not defined. */
7391#ifdef USE_X_TOOLKIT
7392 pending_menu_activation = 0;
7393#endif
7394}
7395
7396
7397/* Update the menu bar item list for frame F. This has to be done
7398 before we start to fill in any display lines, because it can call
7399 eval.
7400
7401 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7402
7403static void
7404update_menu_bar (f, save_match_data)
7405 struct frame *f;
7406 int save_match_data;
7407{
7408 Lisp_Object window;
7409 register struct window *w;
7410
e1477f43
GM
7411 /* If called recursively during a menu update, do nothing. This can
7412 happen when, for instance, an activate-menubar-hook causes a
7413 redisplay. */
7414 if (inhibit_menubar_update)
7415 return;
7416
5f5c8ee5
GM
7417 window = FRAME_SELECTED_WINDOW (f);
7418 w = XWINDOW (window);
7419
7420 if (update_mode_lines)
7421 w->update_mode_line = Qt;
7422
7423 if (FRAME_WINDOW_P (f)
7424 ?
1a578e9b 7425#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5
GM
7426 FRAME_EXTERNAL_MENU_BAR (f)
7427#else
7428 FRAME_MENU_BAR_LINES (f) > 0
7429#endif
7430 : FRAME_MENU_BAR_LINES (f) > 0)
7431 {
7432 /* If the user has switched buffers or windows, we need to
7433 recompute to reflect the new bindings. But we'll
7434 recompute when update_mode_lines is set too; that means
7435 that people can use force-mode-line-update to request
7436 that the menu bar be recomputed. The adverse effect on
7437 the rest of the redisplay algorithm is about the same as
7438 windows_or_buffers_changed anyway. */
7439 if (windows_or_buffers_changed
7440 || !NILP (w->update_mode_line)
7441 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7442 < BUF_MODIFF (XBUFFER (w->buffer)))
7443 != !NILP (w->last_had_star))
7444 || ((!NILP (Vtransient_mark_mode)
7445 && !NILP (XBUFFER (w->buffer)->mark_active))
7446 != !NILP (w->region_showing)))
7447 {
7448 struct buffer *prev = current_buffer;
2d27dae2 7449 int count = BINDING_STACK_SIZE ();
5f5c8ee5 7450
e1477f43
GM
7451 specbind (Qinhibit_menubar_update, Qt);
7452
5f5c8ee5
GM
7453 set_buffer_internal_1 (XBUFFER (w->buffer));
7454 if (save_match_data)
7455 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7456 if (NILP (Voverriding_local_map_menu_flag))
7457 {
7458 specbind (Qoverriding_terminal_local_map, Qnil);
7459 specbind (Qoverriding_local_map, Qnil);
7460 }
7461
7462 /* Run the Lucid hook. */
65048e97 7463 safe_run_hooks (Qactivate_menubar_hook);
5f5c8ee5
GM
7464
7465 /* If it has changed current-menubar from previous value,
7466 really recompute the menu-bar from the value. */
7467 if (! NILP (Vlucid_menu_bar_dirty_flag))
7468 call0 (Qrecompute_lucid_menubar);
7469
7470 safe_run_hooks (Qmenu_bar_update_hook);
7471 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7472
7473 /* Redisplay the menu bar in case we changed it. */
1a578e9b
AC
7474#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7475 if (FRAME_WINDOW_P (f)
7476#if defined (macintosh)
7477 /* All frames on Mac OS share the same menubar. So only the
7478 selected frame should be allowed to set it. */
7479 && f == SELECTED_FRAME ()
7480#endif
7481 )
5f5c8ee5
GM
7482 set_frame_menubar (f, 0, 0);
7483 else
7484 /* On a terminal screen, the menu bar is an ordinary screen
7485 line, and this makes it get updated. */
7486 w->update_mode_line = Qt;
7487#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7488 /* In the non-toolkit version, the menu bar is an ordinary screen
7489 line, and this makes it get updated. */
7490 w->update_mode_line = Qt;
7491#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7492
7493 unbind_to (count, Qnil);
7494 set_buffer_internal_1 (prev);
7495 }
7496 }
7497}
7498
7499
7500\f
7501/***********************************************************************
e037b9ec 7502 Tool-bars
5f5c8ee5
GM
7503 ***********************************************************************/
7504
7505#ifdef HAVE_WINDOW_SYSTEM
7506
e037b9ec 7507/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7508 before we start to fill in any display lines. Called from
7509 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7510 and restore it here. */
7511
7512static void
e037b9ec 7513update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7514 struct frame *f;
7515 int save_match_data;
7516{
e037b9ec
GM
7517 if (WINDOWP (f->tool_bar_window)
7518 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
7519 {
7520 Lisp_Object window;
7521 struct window *w;
7522
7523 window = FRAME_SELECTED_WINDOW (f);
7524 w = XWINDOW (window);
7525
7526 /* If the user has switched buffers or windows, we need to
7527 recompute to reflect the new bindings. But we'll
7528 recompute when update_mode_lines is set too; that means
7529 that people can use force-mode-line-update to request
7530 that the menu bar be recomputed. The adverse effect on
7531 the rest of the redisplay algorithm is about the same as
7532 windows_or_buffers_changed anyway. */
7533 if (windows_or_buffers_changed
7534 || !NILP (w->update_mode_line)
7535 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7536 < BUF_MODIFF (XBUFFER (w->buffer)))
7537 != !NILP (w->last_had_star))
7538 || ((!NILP (Vtransient_mark_mode)
7539 && !NILP (XBUFFER (w->buffer)->mark_active))
7540 != !NILP (w->region_showing)))
7541 {
7542 struct buffer *prev = current_buffer;
2d27dae2 7543 int count = BINDING_STACK_SIZE ();
a2889657 7544
5f5c8ee5
GM
7545 /* Set current_buffer to the buffer of the selected
7546 window of the frame, so that we get the right local
7547 keymaps. */
7548 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7549
5f5c8ee5
GM
7550 /* Save match data, if we must. */
7551 if (save_match_data)
7552 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7553
7554 /* Make sure that we don't accidentally use bogus keymaps. */
7555 if (NILP (Voverriding_local_map_menu_flag))
7556 {
7557 specbind (Qoverriding_terminal_local_map, Qnil);
7558 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7559 }
1f40cad2 7560
e037b9ec 7561 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7562 f->tool_bar_items
7563 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
5f5c8ee5 7564
e037b9ec 7565 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7566 w->update_mode_line = Qt;
7567
7568 unbind_to (count, Qnil);
7569 set_buffer_internal_1 (prev);
81d478f3 7570 }
a2889657
JB
7571 }
7572}
7573
6c4429a5 7574
e037b9ec 7575/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7576 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7577 been set up previously by calling prepare_menu_bars. */
7578
a2889657 7579static void
e037b9ec 7580build_desired_tool_bar_string (f)
5f5c8ee5 7581 struct frame *f;
a2889657 7582{
a23887b9 7583 int i, size, size_needed;
5f5c8ee5
GM
7584 struct gcpro gcpro1, gcpro2, gcpro3;
7585 Lisp_Object image, plist, props;
a2889657 7586
5f5c8ee5
GM
7587 image = plist = props = Qnil;
7588 GCPRO3 (image, plist, props);
a2889657 7589
e037b9ec 7590 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
7591 Otherwise, make a new string. */
7592
7593 /* The size of the string we might be able to reuse. */
e037b9ec
GM
7594 size = (STRINGP (f->desired_tool_bar_string)
7595 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
7596 : 0);
7597
b94c0d9c 7598 /* We need one space in the string for each image. */
a23887b9 7599 size_needed = f->n_tool_bar_items;
b94c0d9c
GM
7600
7601 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7602 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7603 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7604 make_number (' '));
5f5c8ee5
GM
7605 else
7606 {
7607 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7608 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7609 props, f->desired_tool_bar_string);
5f5c8ee5 7610 }
a2889657 7611
5f5c8ee5 7612 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7613 put a `menu_item' property on tool-bar items with a value that
7614 is the index of the item in F's tool-bar item vector. */
a23887b9 7615 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7616 {
7464726e 7617#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7618
e037b9ec
GM
7619 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7620 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7621 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7622 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7623
7624 /* If image is a vector, choose the image according to the
7625 button state. */
e037b9ec 7626 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7627 if (VECTORP (image))
7628 {
5f5c8ee5
GM
7629 if (enabled_p)
7630 idx = (selected_p
e037b9ec
GM
7631 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7632 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7633 else
7634 idx = (selected_p
e037b9ec
GM
7635 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7636 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5 7637
37e4e482
GM
7638 xassert (ASIZE (image) >= idx);
7639 image = AREF (image, idx);
5f5c8ee5 7640 }
37e4e482
GM
7641 else
7642 idx = -1;
5f5c8ee5
GM
7643
7644 /* Ignore invalid image specifications. */
7645 if (!valid_image_p (image))
7646 continue;
7647
e037b9ec 7648 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7649 plist = Fcopy_sequence (XCDR (image));
7650
7651 /* Compute margin and relief to draw. */
9f864bf9 7652 relief = (tool_bar_button_relief >= 0
c3d76173
GM
7653 ? tool_bar_button_relief
7654 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7655 hmargin = vmargin = relief;
7656
7657 if (INTEGERP (Vtool_bar_button_margin)
7658 && XINT (Vtool_bar_button_margin) > 0)
7659 {
7660 hmargin += XFASTINT (Vtool_bar_button_margin);
7661 vmargin += XFASTINT (Vtool_bar_button_margin);
7662 }
7663 else if (CONSP (Vtool_bar_button_margin))
7664 {
7665 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7666 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7667 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7668
7669 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7670 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7671 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7672 }
5f5c8ee5 7673
e037b9ec 7674 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7675 {
7676 /* Add a `:relief' property to the image spec if the item is
7677 selected. */
7678 if (selected_p)
7679 {
7680 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7681 hmargin -= relief;
7682 vmargin -= relief;
5f5c8ee5
GM
7683 }
7684 }
7685 else
7686 {
7687 /* If image is selected, display it pressed, i.e. with a
7688 negative relief. If it's not selected, display it with a
7689 raised relief. */
7690 plist = Fplist_put (plist, QCrelief,
7691 (selected_p
7692 ? make_number (-relief)
7693 : make_number (relief)));
35a41507
GM
7694 hmargin -= relief;
7695 vmargin -= relief;
5f5c8ee5
GM
7696 }
7697
7698 /* Put a margin around the image. */
35a41507
GM
7699 if (hmargin || vmargin)
7700 {
7701 if (hmargin == vmargin)
7702 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7703 else
7704 plist = Fplist_put (plist, QCmargin,
7705 Fcons (make_number (hmargin),
7706 make_number (vmargin)));
7707 }
5f5c8ee5 7708
37e4e482
GM
7709 /* If button is not enabled, and we don't have special images
7710 for the disabled state, make the image appear disabled by
5f5c8ee5 7711 applying an appropriate algorithm to it. */
37e4e482 7712 if (!enabled_p && idx < 0)
ecd01a0e 7713 plist = Fplist_put (plist, QCconversion, Qdisabled);
5f5c8ee5
GM
7714
7715 /* Put a `display' text property on the string for the image to
7716 display. Put a `menu-item' property on the string that gives
e037b9ec 7717 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7718 vector. */
7719 image = Fcons (Qimage, plist);
7720 props = list4 (Qdisplay, image,
a23887b9
GM
7721 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7722
7723 /* Let the last image hide all remaining spaces in the tool bar
7724 string. The string can be longer than needed when we reuse a
7725 previous string. */
7726 if (i + 1 == f->n_tool_bar_items)
7727 end = XSTRING (f->desired_tool_bar_string)->size;
7728 else
7729 end = i + 1;
7730 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7731 props, f->desired_tool_bar_string);
5f5c8ee5 7732#undef PROP
a2889657
JB
7733 }
7734
5f5c8ee5
GM
7735 UNGCPRO;
7736}
7737
7738
e037b9ec 7739/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7740
7741static void
e037b9ec 7742display_tool_bar_line (it)
5f5c8ee5
GM
7743 struct it *it;
7744{
7745 struct glyph_row *row = it->glyph_row;
7746 int max_x = it->last_visible_x;
7747 struct glyph *last;
7748
7749 prepare_desired_row (row);
7750 row->y = it->current_y;
90aa2856
GM
7751
7752 /* Note that this isn't made use of if the face hasn't a box,
7753 so there's no need to check the face here. */
7754 it->start_of_box_run_p = 1;
5f5c8ee5
GM
7755
7756 while (it->current_x < max_x)
a2889657 7757 {
5f5c8ee5 7758 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7759
5f5c8ee5
GM
7760 /* Get the next display element. */
7761 if (!get_next_display_element (it))
7762 break;
73af359d 7763
5f5c8ee5
GM
7764 /* Produce glyphs. */
7765 x_before = it->current_x;
7766 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7767 PRODUCE_GLYPHS (it);
daa37602 7768
5f5c8ee5
GM
7769 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7770 i = 0;
7771 x = x_before;
7772 while (i < nglyphs)
7773 {
7774 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7775
7776 if (x + glyph->pixel_width > max_x)
7777 {
7778 /* Glyph doesn't fit on line. */
7779 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7780 it->current_x = x;
7781 goto out;
7782 }
daa37602 7783
5f5c8ee5
GM
7784 ++it->hpos;
7785 x += glyph->pixel_width;
7786 ++i;
7787 }
7788
7789 /* Stop at line ends. */
7790 if (ITERATOR_AT_END_OF_LINE_P (it))
7791 break;
7792
cafafe0b 7793 set_iterator_to_next (it, 1);
a2889657 7794 }
a2889657 7795
5f5c8ee5 7796 out:;
a2889657 7797
5f5c8ee5
GM
7798 row->displays_text_p = row->used[TEXT_AREA] != 0;
7799 extend_face_to_end_of_line (it);
7800 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7801 last->right_box_line_p = 1;
90aa2856
GM
7802 if (last == row->glyphs[TEXT_AREA])
7803 last->left_box_line_p = 1;
5f5c8ee5
GM
7804 compute_line_metrics (it);
7805
e037b9ec 7806 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7807 if (!row->displays_text_p)
7808 {
312246d1
GM
7809 row->height = row->phys_height = it->last_visible_y - row->y;
7810 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
7811 }
7812
7813 row->full_width_p = 1;
7814 row->continued_p = 0;
7815 row->truncated_on_left_p = 0;
7816 row->truncated_on_right_p = 0;
7817
7818 it->current_x = it->hpos = 0;
7819 it->current_y += row->height;
7820 ++it->vpos;
7821 ++it->glyph_row;
a2889657 7822}
96a410bc 7823
5f5c8ee5 7824
e037b9ec 7825/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7826 items of frame F visible. */
96a410bc 7827
d39b6696 7828static int
e037b9ec 7829tool_bar_lines_needed (f)
5f5c8ee5 7830 struct frame *f;
d39b6696 7831{
e037b9ec 7832 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
7833 struct it it;
7834
e037b9ec
GM
7835 /* Initialize an iterator for iteration over
7836 F->desired_tool_bar_string in the tool-bar window of frame F. */
7837 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7838 it.first_visible_x = 0;
7839 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7840 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7841
7842 while (!ITERATOR_AT_END_P (&it))
7843 {
7844 it.glyph_row = w->desired_matrix->rows;
7845 clear_glyph_row (it.glyph_row);
e037b9ec 7846 display_tool_bar_line (&it);
5f5c8ee5
GM
7847 }
7848
7849 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7850}
96a410bc 7851
5f5c8ee5 7852
57c28064
GM
7853DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7854 0, 1, 0,
7ee72033
MB
7855 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7856 (frame)
57c28064
GM
7857 Lisp_Object frame;
7858{
7859 struct frame *f;
7860 struct window *w;
7861 int nlines = 0;
7862
7863 if (NILP (frame))
7864 frame = selected_frame;
7865 else
b7826503 7866 CHECK_FRAME (frame);
57c28064
GM
7867 f = XFRAME (frame);
7868
7869 if (WINDOWP (f->tool_bar_window)
7870 || (w = XWINDOW (f->tool_bar_window),
7871 XFASTINT (w->height) > 0))
7872 {
7873 update_tool_bar (f, 1);
7874 if (f->n_tool_bar_items)
7875 {
7876 build_desired_tool_bar_string (f);
7877 nlines = tool_bar_lines_needed (f);
7878 }
7879 }
7880
7881 return make_number (nlines);
7882}
7883
7884
e037b9ec 7885/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7886 height should be changed. */
7887
7888static int
e037b9ec 7889redisplay_tool_bar (f)
5f5c8ee5 7890 struct frame *f;
96a410bc 7891{
5f5c8ee5
GM
7892 struct window *w;
7893 struct it it;
7894 struct glyph_row *row;
7895 int change_height_p = 0;
7896
e037b9ec
GM
7897 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7898 do anything. This means you must start with tool-bar-lines
5f5c8ee5 7899 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
7900 can turn off tool-bars by specifying tool-bar-lines zero. */
7901 if (!WINDOWP (f->tool_bar_window)
7902 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
7903 XFASTINT (w->height) == 0))
7904 return 0;
96a410bc 7905
e037b9ec
GM
7906 /* Set up an iterator for the tool-bar window. */
7907 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7908 it.first_visible_x = 0;
7909 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7910 row = it.glyph_row;
3450d04c 7911
e037b9ec
GM
7912 /* Build a string that represents the contents of the tool-bar. */
7913 build_desired_tool_bar_string (f);
7914 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 7915
e037b9ec 7916 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 7917 while (it.current_y < it.last_visible_y)
e037b9ec 7918 display_tool_bar_line (&it);
3450d04c 7919
e037b9ec 7920 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
7921 window, so don't do it. */
7922 w->desired_matrix->no_scrolling_p = 1;
7923 w->must_be_updated_p = 1;
3450d04c 7924
e037b9ec 7925 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
7926 {
7927 int nlines;
6e33e6f0
GM
7928
7929 /* If we couldn't display everything, change the tool-bar's
7930 height. */
7931 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7932 change_height_p = 1;
5f5c8ee5
GM
7933
7934 /* If there are blank lines at the end, except for a partially
7935 visible blank line at the end that is smaller than
e037b9ec 7936 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
7937 row = it.glyph_row - 1;
7938 if (!row->displays_text_p
7939 && row->height >= CANON_Y_UNIT (f))
7940 change_height_p = 1;
7941
e037b9ec
GM
7942 /* If row displays tool-bar items, but is partially visible,
7943 change the tool-bar's height. */
5f5c8ee5
GM
7944 if (row->displays_text_p
7945 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7946 change_height_p = 1;
7947
e037b9ec 7948 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
7949 frame parameter. */
7950 if (change_height_p
e037b9ec 7951 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
7952 nlines != XFASTINT (w->height)))
7953 {
e037b9ec 7954 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 7955 Lisp_Object frame;
e85ee976 7956 int old_height = XFASTINT (w->height);
5f5c8ee5
GM
7957
7958 XSETFRAME (frame, f);
7959 clear_glyph_matrix (w->desired_matrix);
7960 Fmodify_frame_parameters (frame,
e037b9ec 7961 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
7962 make_number (nlines)),
7963 Qnil));
e85ee976
GM
7964 if (XFASTINT (w->height) != old_height)
7965 fonts_changed_p = 1;
5f5c8ee5
GM
7966 }
7967 }
3450d04c 7968
5f5c8ee5 7969 return change_height_p;
96a410bc 7970}
90adcf20 7971
5f5c8ee5 7972
e037b9ec
GM
7973/* Get information about the tool-bar item which is displayed in GLYPH
7974 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 7975 properties start in F->tool_bar_items. Value is zero if
e037b9ec 7976 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
7977
7978int
e037b9ec 7979tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
7980 struct frame *f;
7981 struct glyph *glyph;
7982 int *prop_idx;
90adcf20 7983{
5f5c8ee5
GM
7984 Lisp_Object prop;
7985 int success_p;
be676094
GM
7986 int charpos;
7987
7988 /* This function can be called asynchronously, which means we must
7989 exclude any possibility that Fget_text_property signals an
7990 error. */
7991 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7992 charpos = max (0, charpos);
5f5c8ee5
GM
7993
7994 /* Get the text property `menu-item' at pos. The value of that
7995 property is the start index of this item's properties in
7464726e 7996 F->tool_bar_items. */
be676094 7997 prop = Fget_text_property (make_number (charpos),
e037b9ec 7998 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
7999 if (INTEGERP (prop))
8000 {
8001 *prop_idx = XINT (prop);
8002 success_p = 1;
8003 }
8004 else
8005 success_p = 0;
90adcf20 8006
5f5c8ee5
GM
8007 return success_p;
8008}
8009
8010#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 8011
feb0c42f 8012
5f5c8ee5
GM
8013\f
8014/************************************************************************
8015 Horizontal scrolling
8016 ************************************************************************/
feb0c42f 8017
5f5c8ee5
GM
8018static int hscroll_window_tree P_ ((Lisp_Object));
8019static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 8020
5f5c8ee5
GM
8021/* For all leaf windows in the window tree rooted at WINDOW, set their
8022 hscroll value so that PT is (i) visible in the window, and (ii) so
8023 that it is not within a certain margin at the window's left and
8024 right border. Value is non-zero if any window's hscroll has been
8025 changed. */
8026
8027static int
8028hscroll_window_tree (window)
8029 Lisp_Object window;
8030{
8031 int hscrolled_p = 0;
e76d28d5 8032 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
8033 int hscroll_step_abs = 0;
8034 double hscroll_step_rel = 0;
8035
8036 if (hscroll_relative_p)
8037 {
e76d28d5 8038 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
8039 if (hscroll_step_rel < 0)
8040 {
8041 hscroll_relative_p = 0;
8042 hscroll_step_abs = 0;
8043 }
8044 }
e76d28d5 8045 else if (INTEGERP (Vhscroll_step))
1df7e8f0 8046 {
e76d28d5 8047 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
8048 if (hscroll_step_abs < 0)
8049 hscroll_step_abs = 0;
8050 }
8051 else
8052 hscroll_step_abs = 0;
8053
5f5c8ee5 8054 while (WINDOWP (window))
90adcf20 8055 {
5f5c8ee5 8056 struct window *w = XWINDOW (window);
1df7e8f0 8057
5f5c8ee5
GM
8058 if (WINDOWP (w->hchild))
8059 hscrolled_p |= hscroll_window_tree (w->hchild);
8060 else if (WINDOWP (w->vchild))
8061 hscrolled_p |= hscroll_window_tree (w->vchild);
8062 else if (w->cursor.vpos >= 0)
8063 {
e76d28d5 8064 int h_margin, text_area_x, text_area_y;
5f5c8ee5 8065 int text_area_width, text_area_height;
92a90e89
GM
8066 struct glyph_row *current_cursor_row
8067 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8068 struct glyph_row *desired_cursor_row
8069 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8070 struct glyph_row *cursor_row
8071 = (desired_cursor_row->enabled_p
8072 ? desired_cursor_row
8073 : current_cursor_row);
a2725ab2 8074
5f5c8ee5
GM
8075 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8076 &text_area_width, &text_area_height);
90adcf20 8077
5f5c8ee5 8078 /* Scroll when cursor is inside this scroll margin. */
e76d28d5 8079 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
1df7e8f0 8080
5f5c8ee5 8081 if ((XFASTINT (w->hscroll)
e76d28d5 8082 && w->cursor.x <= h_margin)
92a90e89
GM
8083 || (cursor_row->enabled_p
8084 && cursor_row->truncated_on_right_p
e76d28d5 8085 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 8086 {
5f5c8ee5
GM
8087 struct it it;
8088 int hscroll;
8089 struct buffer *saved_current_buffer;
8090 int pt;
1df7e8f0 8091 int wanted_x;
5f5c8ee5
GM
8092
8093 /* Find point in a display of infinite width. */
8094 saved_current_buffer = current_buffer;
8095 current_buffer = XBUFFER (w->buffer);
1df7e8f0 8096
5f5c8ee5
GM
8097 if (w == XWINDOW (selected_window))
8098 pt = BUF_PT (current_buffer);
8099 else
08b610e4 8100 {
5f5c8ee5
GM
8101 pt = marker_position (w->pointm);
8102 pt = max (BEGV, pt);
8103 pt = min (ZV, pt);
8104 }
8105
8106 /* Move iterator to pt starting at cursor_row->start in
8107 a line with infinite width. */
8108 init_to_row_start (&it, w, cursor_row);
8109 it.last_visible_x = INFINITY;
8110 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8111 current_buffer = saved_current_buffer;
8112
1df7e8f0
EZ
8113 /* Position cursor in window. */
8114 if (!hscroll_relative_p && hscroll_step_abs == 0)
8115 hscroll = max (0, it.current_x - text_area_width / 2)
8116 / CANON_X_UNIT (it.f);
e76d28d5 8117 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
8118 {
8119 if (hscroll_relative_p)
8120 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 8121 - h_margin;
1df7e8f0
EZ
8122 else
8123 wanted_x = text_area_width
8124 - hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8125 - h_margin;
1df7e8f0
EZ
8126 hscroll
8127 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8128 }
8129 else
8130 {
8131 if (hscroll_relative_p)
8132 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 8133 + h_margin;
1df7e8f0
EZ
8134 else
8135 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8136 + h_margin;
1df7e8f0
EZ
8137 hscroll
8138 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8139 }
3172f70b 8140 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
8141
8142 /* Don't call Fset_window_hscroll if value hasn't
8143 changed because it will prevent redisplay
8144 optimizations. */
8145 if (XFASTINT (w->hscroll) != hscroll)
8146 {
3172f70b
GM
8147 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8148 w->hscroll = make_number (hscroll);
5f5c8ee5 8149 hscrolled_p = 1;
08b610e4 8150 }
08b610e4 8151 }
08b610e4 8152 }
a2725ab2 8153
5f5c8ee5 8154 window = w->next;
90adcf20 8155 }
cd6dfed6 8156
5f5c8ee5
GM
8157 /* Value is non-zero if hscroll of any leaf window has been changed. */
8158 return hscrolled_p;
8159}
8160
8161
8162/* Set hscroll so that cursor is visible and not inside horizontal
8163 scroll margins for all windows in the tree rooted at WINDOW. See
8164 also hscroll_window_tree above. Value is non-zero if any window's
8165 hscroll has been changed. If it has, desired matrices on the frame
8166 of WINDOW are cleared. */
8167
8168static int
8169hscroll_windows (window)
8170 Lisp_Object window;
8171{
d475bcb8
GM
8172 int hscrolled_p;
8173
8174 if (automatic_hscrolling_p)
8175 {
8176 hscrolled_p = hscroll_window_tree (window);
8177 if (hscrolled_p)
8178 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8179 }
8180 else
8181 hscrolled_p = 0;
5f5c8ee5 8182 return hscrolled_p;
90adcf20 8183}
5f5c8ee5
GM
8184
8185
90adcf20 8186\f
5f5c8ee5
GM
8187/************************************************************************
8188 Redisplay
8189 ************************************************************************/
8190
8191/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8192 to a non-zero value. This is sometimes handy to have in a debugger
8193 session. */
8194
8195#if GLYPH_DEBUG
a2889657 8196
5f5c8ee5
GM
8197/* First and last unchanged row for try_window_id. */
8198
8199int debug_first_unchanged_at_end_vpos;
8200int debug_last_unchanged_at_beg_vpos;
8201
8202/* Delta vpos and y. */
8203
8204int debug_dvpos, debug_dy;
8205
8206/* Delta in characters and bytes for try_window_id. */
8207
8208int debug_delta, debug_delta_bytes;
8209
8210/* Values of window_end_pos and window_end_vpos at the end of
8211 try_window_id. */
8212
31ade731 8213EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
8214
8215/* Append a string to W->desired_matrix->method. FMT is a printf
8216 format string. A1...A9 are a supplement for a variable-length
8217 argument list. If trace_redisplay_p is non-zero also printf the
8218 resulting string to stderr. */
8219
8220static void
8221debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8222 struct window *w;
8223 char *fmt;
8224 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8225{
8226 char buffer[512];
8227 char *method = w->desired_matrix->method;
8228 int len = strlen (method);
8229 int size = sizeof w->desired_matrix->method;
8230 int remaining = size - len - 1;
8231
8232 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8233 if (len && remaining)
8234 {
8235 method[len] = '|';
8236 --remaining, ++len;
8237 }
8238
8239 strncpy (method + len, buffer, remaining);
8240
8241 if (trace_redisplay_p)
8242 fprintf (stderr, "%p (%s): %s\n",
8243 w,
8244 ((BUFFERP (w->buffer)
8245 && STRINGP (XBUFFER (w->buffer)->name))
8246 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8247 : "no buffer"),
8248 buffer);
8249}
a2889657 8250
5f5c8ee5 8251#endif /* GLYPH_DEBUG */
90adcf20 8252
a2889657 8253
5f5c8ee5
GM
8254/* This counter is used to clear the face cache every once in a while
8255 in redisplay_internal. It is incremented for each redisplay.
8256 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8257 cleared. */
0d231165 8258
bb336f8d 8259#define CLEAR_FACE_CACHE_COUNT 500
463f6b91
RS
8260static int clear_face_cache_count;
8261
20de20dc 8262/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
8263
8264static struct frame *previous_terminal_frame;
8265
8266/* Non-zero while redisplay_internal is in progress. */
8267
8268int redisplaying_p;
8269
8270
8271/* Value is non-zero if all changes in window W, which displays
8272 current_buffer, are in the text between START and END. START is a
8273 buffer position, END is given as a distance from Z. Used in
8274 redisplay_internal for display optimization. */
8275
8276static INLINE int
8277text_outside_line_unchanged_p (w, start, end)
8278 struct window *w;
8279 int start, end;
8280{
8281 int unchanged_p = 1;
8282
8283 /* If text or overlays have changed, see where. */
8284 if (XFASTINT (w->last_modified) < MODIFF
8285 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8286 {
8287 /* Gap in the line? */
8288 if (GPT < start || Z - GPT < end)
8289 unchanged_p = 0;
8290
8291 /* Changes start in front of the line, or end after it? */
8292 if (unchanged_p
9142dd5b
GM
8293 && (BEG_UNCHANGED < start - 1
8294 || END_UNCHANGED < end))
5f5c8ee5
GM
8295 unchanged_p = 0;
8296
8297 /* If selective display, can't optimize if changes start at the
8298 beginning of the line. */
8299 if (unchanged_p
8300 && INTEGERP (current_buffer->selective_display)
8301 && XINT (current_buffer->selective_display) > 0
9142dd5b 8302 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 8303 unchanged_p = 0;
47d57b22
GM
8304
8305 /* If there are overlays at the start or end of the line, these
8306 may have overlay strings with newlines in them. A change at
8307 START, for instance, may actually concern the display of such
8308 overlay strings as well, and they are displayed on different
8309 lines. So, quickly rule out this case. (For the future, it
8310 might be desirable to implement something more telling than
8311 just BEG/END_UNCHANGED.) */
8312 if (unchanged_p)
8313 {
8314 if (BEG + BEG_UNCHANGED == start
8315 && overlay_touches_p (start))
8316 unchanged_p = 0;
8317 if (END_UNCHANGED == end
8318 && overlay_touches_p (Z - end))
8319 unchanged_p = 0;
8320 }
5f5c8ee5
GM
8321 }
8322
8323 return unchanged_p;
8324}
8325
8326
8327/* Do a frame update, taking possible shortcuts into account. This is
8328 the main external entry point for redisplay.
8329
8330 If the last redisplay displayed an echo area message and that message
8331 is no longer requested, we clear the echo area or bring back the
8332 mini-buffer if that is in use. */
20de20dc 8333
a2889657
JB
8334void
8335redisplay ()
e9874cee
RS
8336{
8337 redisplay_internal (0);
8338}
8339
47d57b22 8340
260a86a0
KH
8341/* Return 1 if point moved out of or into a composition. Otherwise
8342 return 0. PREV_BUF and PREV_PT are the last point buffer and
8343 position. BUF and PT are the current point buffer and position. */
8344
8345int
8346check_point_in_composition (prev_buf, prev_pt, buf, pt)
8347 struct buffer *prev_buf, *buf;
8348 int prev_pt, pt;
8349{
8350 int start, end;
8351 Lisp_Object prop;
8352 Lisp_Object buffer;
8353
8354 XSETBUFFER (buffer, buf);
8355 /* Check a composition at the last point if point moved within the
8356 same buffer. */
8357 if (prev_buf == buf)
8358 {
8359 if (prev_pt == pt)
8360 /* Point didn't move. */
8361 return 0;
8362
8363 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8364 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8365 && COMPOSITION_VALID_P (start, end, prop)
8366 && start < prev_pt && end > prev_pt)
8367 /* The last point was within the composition. Return 1 iff
8368 point moved out of the composition. */
8369 return (pt <= start || pt >= end);
8370 }
8371
8372 /* Check a composition at the current point. */
8373 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8374 && find_composition (pt, -1, &start, &end, &prop, buffer)
8375 && COMPOSITION_VALID_P (start, end, prop)
8376 && start < pt && end > pt);
8377}
5f5c8ee5 8378
47d57b22 8379
9142dd5b
GM
8380/* Reconsider the setting of B->clip_changed which is displayed
8381 in window W. */
8382
8383static INLINE void
8384reconsider_clip_changes (w, b)
8385 struct window *w;
8386 struct buffer *b;
8387{
8388 if (b->prevent_redisplay_optimizations_p)
8389 b->clip_changed = 1;
8390 else if (b->clip_changed
8391 && !NILP (w->window_end_valid)
8392 && w->current_matrix->buffer == b
8393 && w->current_matrix->zv == BUF_ZV (b)
8394 && w->current_matrix->begv == BUF_BEGV (b))
8395 b->clip_changed = 0;
260a86a0
KH
8396
8397 /* If display wasn't paused, and W is not a tool bar window, see if
8398 point has been moved into or out of a composition. In that case,
8399 we set b->clip_changed to 1 to force updating the screen. If
8400 b->clip_changed has already been set to 1, we can skip this
8401 check. */
8402 if (!b->clip_changed
8403 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8404 {
8405 int pt;
8406
8407 if (w == XWINDOW (selected_window))
8408 pt = BUF_PT (current_buffer);
8409 else
8410 pt = marker_position (w->pointm);
8411
8412 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 8413 || pt != XINT (w->last_point))
260a86a0 8414 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 8415 XINT (w->last_point),
260a86a0
KH
8416 XBUFFER (w->buffer), pt))
8417 b->clip_changed = 1;
8418 }
9142dd5b
GM
8419}
8420
8421
5f5c8ee5
GM
8422/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8423 response to any user action; therefore, we should preserve the echo
8424 area. (Actually, our caller does that job.) Perhaps in the future
8425 avoid recentering windows if it is not necessary; currently that
8426 causes some problems. */
e9874cee
RS
8427
8428static void
8429redisplay_internal (preserve_echo_area)
8430 int preserve_echo_area;
a2889657 8431{
5f5c8ee5
GM
8432 struct window *w = XWINDOW (selected_window);
8433 struct frame *f = XFRAME (w->frame);
8434 int pause;
a2889657 8435 int must_finish = 0;
5f5c8ee5 8436 struct text_pos tlbufpos, tlendpos;
89819bdd 8437 int number_of_visible_frames;
28514cd9 8438 int count;
886bd6f2 8439 struct frame *sf = SELECTED_FRAME ();
a2889657 8440
5f5c8ee5
GM
8441 /* Non-zero means redisplay has to consider all windows on all
8442 frames. Zero means, only selected_window is considered. */
8443 int consider_all_windows_p;
8444
8445 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8446
8447 /* No redisplay if running in batch mode or frame is not yet fully
8448 initialized, or redisplay is explicitly turned off by setting
8449 Vinhibit_redisplay. */
8450 if (noninteractive
8451 || !NILP (Vinhibit_redisplay)
8452 || !f->glyphs_initialized_p)
a2889657
JB
8453 return;
8454
5f5c8ee5
GM
8455 /* The flag redisplay_performed_directly_p is set by
8456 direct_output_for_insert when it already did the whole screen
8457 update necessary. */
8458 if (redisplay_performed_directly_p)
8459 {
8460 redisplay_performed_directly_p = 0;
8461 if (!hscroll_windows (selected_window))
8462 return;
8463 }
8464
15f0cf78
RS
8465#ifdef USE_X_TOOLKIT
8466 if (popup_activated ())
8467 return;
8468#endif
8469
28514cd9 8470 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 8471 if (redisplaying_p)
735c094c
KH
8472 return;
8473
28514cd9
GM
8474 /* Record a function that resets redisplaying_p to its old value
8475 when we leave this function. */
2d27dae2 8476 count = BINDING_STACK_SIZE ();
28514cd9
GM
8477 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8478 ++redisplaying_p;
8479
8b32d885 8480 retry:
bd9d0f3f 8481 pause = 0;
9142dd5b
GM
8482 reconsider_clip_changes (w, current_buffer);
8483
5f5c8ee5
GM
8484 /* If new fonts have been loaded that make a glyph matrix adjustment
8485 necessary, do it. */
8486 if (fonts_changed_p)
8487 {
8488 adjust_glyphs (NULL);
8489 ++windows_or_buffers_changed;
8490 fonts_changed_p = 0;
8491 }
8492
6961e0c1
GM
8493 /* If face_change_count is non-zero, init_iterator will free all
8494 realized faces, which includes the faces referenced from current
8495 matrices. So, we can't reuse current matrices in this case. */
8496 if (face_change_count)
8497 ++windows_or_buffers_changed;
8498
886bd6f2
GM
8499 if (! FRAME_WINDOW_P (sf)
8500 && previous_terminal_frame != sf)
20de20dc 8501 {
5f5c8ee5
GM
8502 /* Since frames on an ASCII terminal share the same display
8503 area, displaying a different frame means redisplay the whole
8504 thing. */
20de20dc 8505 windows_or_buffers_changed++;
886bd6f2
GM
8506 SET_FRAME_GARBAGED (sf);
8507 XSETFRAME (Vterminal_frame, sf);
20de20dc 8508 }
886bd6f2 8509 previous_terminal_frame = sf;
20de20dc 8510
5f5c8ee5
GM
8511 /* Set the visible flags for all frames. Do this before checking
8512 for resized or garbaged frames; they want to know if their frames
8513 are visible. See the comment in frame.h for
8514 FRAME_SAMPLE_VISIBILITY. */
d724d989 8515 {
35f56f96 8516 Lisp_Object tail, frame;
d724d989 8517
89819bdd
RS
8518 number_of_visible_frames = 0;
8519
35f56f96 8520 FOR_EACH_FRAME (tail, frame)
f82aff7c 8521 {
5f5c8ee5
GM
8522 struct frame *f = XFRAME (frame);
8523
8524 FRAME_SAMPLE_VISIBILITY (f);
8525 if (FRAME_VISIBLE_P (f))
8526 ++number_of_visible_frames;
8527 clear_desired_matrices (f);
f82aff7c 8528 }
d724d989
JB
8529 }
8530
44fa5b1e 8531 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8532 do_pending_window_change (1);
a2889657 8533
5f5c8ee5 8534 /* Clear frames marked as garbaged. */
44fa5b1e 8535 if (frame_garbaged)
c6e89d6c 8536 clear_garbaged_frames ();
a2889657 8537
e037b9ec 8538 /* Build menubar and tool-bar items. */
f82aff7c
RS
8539 prepare_menu_bars ();
8540
28995e67 8541 if (windows_or_buffers_changed)
a2889657
JB
8542 update_mode_lines++;
8543
538f13d4
RS
8544 /* Detect case that we need to write or remove a star in the mode line. */
8545 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8546 {
8547 w->update_mode_line = Qt;
8548 if (buffer_shared > 1)
8549 update_mode_lines++;
8550 }
8551
5f5c8ee5 8552 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8553 if (!NILP (w->column_number_displayed)
8554 /* This alternative quickly identifies a common case
8555 where no change is needed. */
8556 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8557 && XFASTINT (w->last_modified) >= MODIFF
8558 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8559 && XFASTINT (w->column_number_displayed) != current_column ())
8560 w->update_mode_line = Qt;
8561
44fa5b1e 8562 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8563
5f5c8ee5
GM
8564 /* The variable buffer_shared is set in redisplay_window and
8565 indicates that we redisplay a buffer in different windows. See
8566 there. */
8567 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
8568
8569 /* If specs for an arrow have changed, do thorough redisplay
8570 to ensure we remove any arrow that should no longer exist. */
d45de95b 8571 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8572 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8573 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8574
90adcf20
RS
8575 /* Normally the message* functions will have already displayed and
8576 updated the echo area, but the frame may have been trashed, or
8577 the update may have been preempted, so display the echo area
6e019995 8578 again here. Checking message_cleared_p captures the case that
c6e89d6c 8579 the echo area should be cleared. */
96d5e9a0
GM
8580 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8581 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
8582 || (message_cleared_p
8583 && minibuf_level == 0
8584 /* If the mini-window is currently selected, this means the
8585 echo-area doesn't show through. */
8586 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 8587 {
c6e89d6c 8588 int window_height_changed_p = echo_area_display (0);
90adcf20 8589 must_finish = 1;
6e019995
GM
8590
8591 /* If we don't display the current message, don't clear the
8592 message_cleared_p flag, because, if we did, we wouldn't clear
8593 the echo area in the next redisplay which doesn't preserve
8594 the echo area. */
8595 if (!display_last_displayed_message_p)
8596 message_cleared_p = 0;
dd2eb166 8597
c6e89d6c
GM
8598 if (fonts_changed_p)
8599 goto retry;
8600 else if (window_height_changed_p)
8601 {
8602 consider_all_windows_p = 1;
8603 ++update_mode_lines;
8604 ++windows_or_buffers_changed;
9142dd5b
GM
8605
8606 /* If window configuration was changed, frames may have been
8607 marked garbaged. Clear them or we will experience
8608 surprises wrt scrolling. */
8609 if (frame_garbaged)
8610 clear_garbaged_frames ();
c6e89d6c 8611 }
90adcf20 8612 }
97a36635 8613 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
8614 && (current_buffer->clip_changed
8615 || XFASTINT (w->last_modified) < MODIFF
8616 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 8617 && resize_mini_window (w, 0))
c6e89d6c
GM
8618 {
8619 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
8620 showing if its contents might have changed. */
8621 must_finish = 1;
8622 consider_all_windows_p = 1;
c6e89d6c 8623 ++windows_or_buffers_changed;
dd2eb166 8624 ++update_mode_lines;
9142dd5b
GM
8625
8626 /* If window configuration was changed, frames may have been
8627 marked garbaged. Clear them or we will experience
8628 surprises wrt scrolling. */
8629 if (frame_garbaged)
8630 clear_garbaged_frames ();
c6e89d6c
GM
8631 }
8632
90adcf20 8633
5f5c8ee5
GM
8634 /* If showing the region, and mark has changed, we must redisplay
8635 the whole window. The assignment to this_line_start_pos prevents
8636 the optimization directly below this if-statement. */
bd66d1ba
RS
8637 if (((!NILP (Vtransient_mark_mode)
8638 && !NILP (XBUFFER (w->buffer)->mark_active))
8639 != !NILP (w->region_showing))
82d04750
JB
8640 || (!NILP (w->region_showing)
8641 && !EQ (w->region_showing,
8642 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
8643 CHARPOS (this_line_start_pos) = 0;
8644
8645 /* Optimize the case that only the line containing the cursor in the
8646 selected window has changed. Variables starting with this_ are
8647 set in display_line and record information about the line
8648 containing the cursor. */
8649 tlbufpos = this_line_start_pos;
8650 tlendpos = this_line_end_pos;
8651 if (!consider_all_windows_p
8652 && CHARPOS (tlbufpos) > 0
8653 && NILP (w->update_mode_line)
73af359d 8654 && !current_buffer->clip_changed
44fa5b1e 8655 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 8656 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 8657 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
8658 && this_line_buffer == current_buffer
8659 && current_buffer == XBUFFER (w->buffer)
265a9e55 8660 && NILP (w->force_start)
5f5c8ee5
GM
8661 /* Point must be on the line that we have info recorded about. */
8662 && PT >= CHARPOS (tlbufpos)
8663 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
8664 /* All text outside that line, including its final newline,
8665 must be unchanged */
5f5c8ee5
GM
8666 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8667 CHARPOS (tlendpos)))
8668 {
8669 if (CHARPOS (tlbufpos) > BEGV
8670 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8671 && (CHARPOS (tlbufpos) == ZV
8672 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
8673 /* Former continuation line has disappeared by becoming empty */
8674 goto cancel;
8675 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 8676 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
8677 || MINI_WINDOW_P (w))
8678 {
1c9241f5
KH
8679 /* We have to handle the case of continuation around a
8680 wide-column character (See the comment in indent.c around
8681 line 885).
8682
8683 For instance, in the following case:
8684
8685 -------- Insert --------
8686 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8687 J_I_ ==> J_I_ `^^' are cursors.
8688 ^^ ^^
8689 -------- --------
8690
8691 As we have to redraw the line above, we should goto cancel. */
8692
5f5c8ee5
GM
8693 struct it it;
8694 int line_height_before = this_line_pixel_height;
8695
8696 /* Note that start_display will handle the case that the
8697 line starting at tlbufpos is a continuation lines. */
8698 start_display (&it, w, tlbufpos);
8699
8700 /* Implementation note: It this still necessary? */
8701 if (it.current_x != this_line_start_x)
1c9241f5
KH
8702 goto cancel;
8703
5f5c8ee5
GM
8704 TRACE ((stderr, "trying display optimization 1\n"));
8705 w->cursor.vpos = -1;
a2889657 8706 overlay_arrow_seen = 0;
5f5c8ee5
GM
8707 it.vpos = this_line_vpos;
8708 it.current_y = this_line_y;
8709 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8710 display_line (&it);
8711
a2889657 8712 /* If line contains point, is not continued,
5f5c8ee5
GM
8713 and ends at same distance from eob as before, we win */
8714 if (w->cursor.vpos >= 0
8715 /* Line is not continued, otherwise this_line_start_pos
8716 would have been set to 0 in display_line. */
8717 && CHARPOS (this_line_start_pos)
8718 /* Line ends as before. */
8719 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8720 /* Line has same height as before. Otherwise other lines
8721 would have to be shifted up or down. */
8722 && this_line_pixel_height == line_height_before)
a2889657 8723 {
5f5c8ee5
GM
8724 /* If this is not the window's last line, we must adjust
8725 the charstarts of the lines below. */
8726 if (it.current_y < it.last_visible_y)
8727 {
8728 struct glyph_row *row
8729 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8730 int delta, delta_bytes;
8731
8732 if (Z - CHARPOS (tlendpos) == ZV)
8733 {
8734 /* This line ends at end of (accessible part of)
8735 buffer. There is no newline to count. */
8736 delta = (Z
8737 - CHARPOS (tlendpos)
8738 - MATRIX_ROW_START_CHARPOS (row));
8739 delta_bytes = (Z_BYTE
8740 - BYTEPOS (tlendpos)
8741 - MATRIX_ROW_START_BYTEPOS (row));
8742 }
8743 else
8744 {
8745 /* This line ends in a newline. Must take
8746 account of the newline and the rest of the
8747 text that follows. */
8748 delta = (Z
8749 - CHARPOS (tlendpos)
8750 - MATRIX_ROW_START_CHARPOS (row));
8751 delta_bytes = (Z_BYTE
8752 - BYTEPOS (tlendpos)
8753 - MATRIX_ROW_START_BYTEPOS (row));
8754 }
8755
f2d86d7a
GM
8756 increment_matrix_positions (w->current_matrix,
8757 this_line_vpos + 1,
8758 w->current_matrix->nrows,
8759 delta, delta_bytes);
85bcef6c 8760 }
46db8486 8761
5f5c8ee5
GM
8762 /* If this row displays text now but previously didn't,
8763 or vice versa, w->window_end_vpos may have to be
8764 adjusted. */
8765 if ((it.glyph_row - 1)->displays_text_p)
8766 {
8767 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8768 XSETINT (w->window_end_vpos, this_line_vpos);
8769 }
8770 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8771 && this_line_vpos > 0)
8772 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8773 w->window_end_valid = Qnil;
8774
8775 /* Update hint: No need to try to scroll in update_window. */
8776 w->desired_matrix->no_scrolling_p = 1;
8777
8778#if GLYPH_DEBUG
8779 *w->desired_matrix->method = 0;
8780 debug_method_add (w, "optimization 1");
8781#endif
a2889657
JB
8782 goto update;
8783 }
8784 else
8785 goto cancel;
8786 }
5f5c8ee5
GM
8787 else if (/* Cursor position hasn't changed. */
8788 PT == XFASTINT (w->last_point)
b6f0fe04
RS
8789 /* Make sure the cursor was last displayed
8790 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
8791 && 0 <= w->cursor.vpos
8792 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
8793 {
8794 if (!must_finish)
8795 {
c6e89d6c 8796 do_pending_window_change (1);
5f5c8ee5
GM
8797
8798 /* We used to always goto end_of_redisplay here, but this
8799 isn't enough if we have a blinking cursor. */
8800 if (w->cursor_off_p == w->last_cursor_off_p)
8801 goto end_of_redisplay;
a2889657
JB
8802 }
8803 goto update;
8804 }
8b51f1e3
KH
8805 /* If highlighting the region, or if the cursor is in the echo area,
8806 then we can't just move the cursor. */
bd66d1ba
RS
8807 else if (! (!NILP (Vtransient_mark_mode)
8808 && !NILP (current_buffer->mark_active))
97a36635 8809 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 8810 || highlight_nonselected_windows)
8b51f1e3 8811 && NILP (w->region_showing)
8f897821 8812 && NILP (Vshow_trailing_whitespace)
8b51f1e3 8813 && !cursor_in_echo_area)
a2889657 8814 {
5f5c8ee5
GM
8815 struct it it;
8816 struct glyph_row *row;
8817
8818 /* Skip from tlbufpos to PT and see where it is. Note that
8819 PT may be in invisible text. If so, we will end at the
8820 next visible position. */
8821 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8822 NULL, DEFAULT_FACE_ID);
8823 it.current_x = this_line_start_x;
8824 it.current_y = this_line_y;
8825 it.vpos = this_line_vpos;
8826
8827 /* The call to move_it_to stops in front of PT, but
8828 moves over before-strings. */
8829 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8830
8831 if (it.vpos == this_line_vpos
8832 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8833 row->enabled_p))
a2889657 8834 {
5f5c8ee5
GM
8835 xassert (this_line_vpos == it.vpos);
8836 xassert (this_line_y == it.current_y);
8837 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
8838#if GLYPH_DEBUG
8839 *w->desired_matrix->method = 0;
8840 debug_method_add (w, "optimization 3");
8841#endif
a2889657
JB
8842 goto update;
8843 }
8844 else
8845 goto cancel;
8846 }
5f5c8ee5 8847
a2889657 8848 cancel:
5f5c8ee5
GM
8849 /* Text changed drastically or point moved off of line. */
8850 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
8851 }
8852
5f5c8ee5
GM
8853 CHARPOS (this_line_start_pos) = 0;
8854 consider_all_windows_p |= buffer_shared > 1;
8855 ++clear_face_cache_count;
a2889657 8856
5f5c8ee5 8857
bd9d0f3f
GM
8858 /* Build desired matrices, and update the display. If
8859 consider_all_windows_p is non-zero, do it for all windows on all
8860 frames. Otherwise do it for selected_window, only. */
463f6b91 8861
5f5c8ee5 8862 if (consider_all_windows_p)
a2889657 8863 {
35f56f96 8864 Lisp_Object tail, frame;
0528abe1
GM
8865 int i, n = 0, size = 50;
8866 struct frame **updated
8867 = (struct frame **) alloca (size * sizeof *updated);
a2889657 8868
5f5c8ee5
GM
8869 /* Clear the face cache eventually. */
8870 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 8871 {
5f5c8ee5 8872 clear_face_cache (0);
463f6b91
RS
8873 clear_face_cache_count = 0;
8874 }
31b24551 8875
5f5c8ee5
GM
8876 /* Recompute # windows showing selected buffer. This will be
8877 incremented each time such a window is displayed. */
a2889657
JB
8878 buffer_shared = 0;
8879
35f56f96 8880 FOR_EACH_FRAME (tail, frame)
30c566e4 8881 {
5f5c8ee5 8882 struct frame *f = XFRAME (frame);
bd9d0f3f 8883
886bd6f2 8884 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 8885 {
ae02e06a 8886#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
8887 if (clear_face_cache_count % 50 == 0
8888 && FRAME_WINDOW_P (f))
8889 clear_image_cache (f, 0);
ae02e06a 8890#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 8891
5f5c8ee5
GM
8892 /* Mark all the scroll bars to be removed; we'll redeem
8893 the ones we want when we redisplay their windows. */
9769686d 8894 if (condemn_scroll_bars_hook)
504454e8 8895 condemn_scroll_bars_hook (f);
30c566e4 8896
f21ef775 8897 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 8898 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 8899
5f5c8ee5
GM
8900 /* Any scroll bars which redisplay_windows should have
8901 nuked should now go away. */
9769686d 8902 if (judge_scroll_bars_hook)
504454e8 8903 judge_scroll_bars_hook (f);
bd9d0f3f
GM
8904
8905 /* If fonts changed, display again. */
8906 if (fonts_changed_p)
8907 goto retry;
8908
8909 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8910 {
8911 /* See if we have to hscroll. */
8912 if (hscroll_windows (f->root_window))
8913 goto retry;
8914
8915 /* Prevent various kinds of signals during display
8916 update. stdio is not robust about handling
8917 signals, which can cause an apparent I/O
8918 error. */
8919 if (interrupt_input)
8920 unrequest_sigio ();
8921 stop_polling ();
8922
8923 /* Update the display. */
8924 set_window_update_flags (XWINDOW (f->root_window), 1);
8925 pause |= update_frame (f, 0, 0);
8926 if (pause)
8927 break;
8928
0528abe1
GM
8929 if (n == size)
8930 {
8931 int nbytes = size * sizeof *updated;
8932 struct frame **p = (struct frame **) alloca (2 * nbytes);
8933 bcopy (updated, p, nbytes);
8934 size *= 2;
8935 }
8936
8937 updated[n++] = f;
bd9d0f3f 8938 }
9769686d 8939 }
30c566e4 8940 }
0528abe1
GM
8941
8942 /* Do the mark_window_display_accurate after all windows have
8943 been redisplayed because this call resets flags in buffers
8944 which are needed for proper redisplay. */
8945 for (i = 0; i < n; ++i)
8946 {
8947 struct frame *f = updated[i];
8948 mark_window_display_accurate (f->root_window, 1);
8949 if (frame_up_to_date_hook)
8950 frame_up_to_date_hook (f);
8951 }
a2889657 8952 }
bd9d0f3f
GM
8953 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8954 {
8955 Lisp_Object mini_window;
8956 struct frame *mini_frame;
5f5c8ee5 8957
82a7ab23
RS
8958 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8959 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8960 redisplay_window_error);
5f5c8ee5 8961
bd9d0f3f
GM
8962 /* Compare desired and current matrices, perform output. */
8963 update:
5f5c8ee5 8964
bd9d0f3f
GM
8965 /* If fonts changed, display again. */
8966 if (fonts_changed_p)
92a90e89 8967 goto retry;
a2889657 8968
bd9d0f3f
GM
8969 /* Prevent various kinds of signals during display update.
8970 stdio is not robust about handling signals,
8971 which can cause an apparent I/O error. */
8972 if (interrupt_input)
8973 unrequest_sigio ();
8974 stop_polling ();
1af9f229 8975
bd9d0f3f 8976 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 8977 {
92a90e89
GM
8978 if (hscroll_windows (selected_window))
8979 goto retry;
8980
5f5c8ee5 8981 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 8982 pause = update_frame (sf, 0, 0);
5f5c8ee5 8983 }
d724d989 8984
8de2d90b 8985 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
8986 function. If the echo area is on another frame, that may
8987 have put text on a frame other than the selected one, so the
8988 above call to update_frame would not have caught it. Catch
8de2d90b 8989 it here. */
bd9d0f3f
GM
8990 mini_window = FRAME_MINIBUF_WINDOW (sf);
8991 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 8992
bd9d0f3f
GM
8993 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8994 {
8995 XWINDOW (mini_window)->must_be_updated_p = 1;
8996 pause |= update_frame (mini_frame, 0, 0);
8997 if (!pause && hscroll_windows (mini_window))
8998 goto retry;
8999 }
6e8290aa 9000 }
a2889657 9001
5f5c8ee5
GM
9002 /* If display was paused because of pending input, make sure we do a
9003 thorough update the next time. */
a2889657
JB
9004 if (pause)
9005 {
5f5c8ee5
GM
9006 /* Prevent the optimization at the beginning of
9007 redisplay_internal that tries a single-line update of the
9008 line containing the cursor in the selected window. */
9009 CHARPOS (this_line_start_pos) = 0;
9010
9011 /* Let the overlay arrow be updated the next time. */
265a9e55 9012 if (!NILP (last_arrow_position))
a2889657
JB
9013 {
9014 last_arrow_position = Qt;
9015 last_arrow_string = Qt;
9016 }
5f5c8ee5
GM
9017
9018 /* If we pause after scrolling, some rows in the current
9019 matrices of some windows are not valid. */
9020 if (!WINDOW_FULL_WIDTH_P (w)
9021 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
9022 update_mode_lines = 1;
9023 }
43c09969 9024 else
a2889657 9025 {
43c09969 9026 if (!consider_all_windows_p)
a2889657 9027 {
43c09969
GM
9028 /* This has already been done above if
9029 consider_all_windows_p is set. */
9030 mark_window_display_accurate_1 (w, 1);
927c5b3b
GM
9031
9032 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9033 last_arrow_string = Voverlay_arrow_string;
9034
efc63ef0 9035 if (frame_up_to_date_hook != 0)
43c09969 9036 frame_up_to_date_hook (sf);
a2889657 9037 }
15e26c76 9038
a2889657
JB
9039 update_mode_lines = 0;
9040 windows_or_buffers_changed = 0;
9041 }
9042
5f5c8ee5
GM
9043 /* Start SIGIO interrupts coming again. Having them off during the
9044 code above makes it less likely one will discard output, but not
9045 impossible, since there might be stuff in the system buffer here.
a2889657 9046 But it is much hairier to try to do anything about that. */
a2889657
JB
9047 if (interrupt_input)
9048 request_sigio ();
9049 start_polling ();
9050
5f5c8ee5
GM
9051 /* If a frame has become visible which was not before, redisplay
9052 again, so that we display it. Expose events for such a frame
9053 (which it gets when becoming visible) don't call the parts of
9054 redisplay constructing glyphs, so simply exposing a frame won't
9055 display anything in this case. So, we have to display these
9056 frames here explicitly. */
11c52c4f
RS
9057 if (!pause)
9058 {
9059 Lisp_Object tail, frame;
9060 int new_count = 0;
9061
9062 FOR_EACH_FRAME (tail, frame)
9063 {
9064 int this_is_visible = 0;
8e83f802
RS
9065
9066 if (XFRAME (frame)->visible)
9067 this_is_visible = 1;
9068 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9069 if (XFRAME (frame)->visible)
9070 this_is_visible = 1;
11c52c4f
RS
9071
9072 if (this_is_visible)
9073 new_count++;
9074 }
9075
89819bdd 9076 if (new_count != number_of_visible_frames)
11c52c4f
RS
9077 windows_or_buffers_changed++;
9078 }
9079
44fa5b1e 9080 /* Change frame size now if a change is pending. */
c6e89d6c 9081 do_pending_window_change (1);
d8e242fd 9082
8b32d885
RS
9083 /* If we just did a pending size change, or have additional
9084 visible frames, redisplay again. */
3c8c72e0 9085 if (windows_or_buffers_changed && !pause)
8b32d885 9086 goto retry;
5f5c8ee5
GM
9087
9088 end_of_redisplay:;
c6e89d6c 9089
28514cd9 9090 unbind_to (count, Qnil);
a2889657
JB
9091}
9092
5f5c8ee5
GM
9093
9094/* Redisplay, but leave alone any recent echo area message unless
9095 another message has been requested in its place.
a2889657
JB
9096
9097 This is useful in situations where you need to redisplay but no
9098 user action has occurred, making it inappropriate for the message
9099 area to be cleared. See tracking_off and
9bf76936
GM
9100 wait_reading_process_input for examples of these situations.
9101
9102 FROM_WHERE is an integer saying from where this function was
9103 called. This is useful for debugging. */
a2889657 9104
8991bb31 9105void
9bf76936
GM
9106redisplay_preserve_echo_area (from_where)
9107 int from_where;
a2889657 9108{
9bf76936
GM
9109 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9110
c6e89d6c 9111 if (!NILP (echo_area_buffer[1]))
a2889657 9112 {
c6e89d6c
GM
9113 /* We have a previously displayed message, but no current
9114 message. Redisplay the previous message. */
9115 display_last_displayed_message_p = 1;
e9874cee 9116 redisplay_internal (1);
c6e89d6c 9117 display_last_displayed_message_p = 0;
a2889657
JB
9118 }
9119 else
e9874cee 9120 redisplay_internal (1);
a2889657
JB
9121}
9122
5f5c8ee5 9123
28514cd9
GM
9124/* Function registered with record_unwind_protect in
9125 redisplay_internal. Clears the flag indicating that a redisplay is
9126 in progress. */
9127
9128static Lisp_Object
9129unwind_redisplay (old_redisplaying_p)
9130 Lisp_Object old_redisplaying_p;
9131{
9132 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 9133 return Qnil;
28514cd9
GM
9134}
9135
9136
43c09969
GM
9137/* Mark the display of window W as accurate or inaccurate. If
9138 ACCURATE_P is non-zero mark display of W as accurate. If
9139 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9140 redisplay_internal is called. */
5f5c8ee5 9141
43c09969
GM
9142static void
9143mark_window_display_accurate_1 (w, accurate_p)
9144 struct window *w;
5f5c8ee5 9145 int accurate_p;
a2889657 9146{
43c09969 9147 if (BUFFERP (w->buffer))
a2889657 9148 {
43c09969
GM
9149 struct buffer *b = XBUFFER (w->buffer);
9150
9151 w->last_modified
9152 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9153 w->last_overlay_modified
9154 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9155 w->last_had_star
9156 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 9157
43c09969 9158 if (accurate_p)
bd66d1ba 9159 {
43c09969
GM
9160 b->clip_changed = 0;
9161 b->prevent_redisplay_optimizations_p = 0;
9162
9163 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9164 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9165 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9166 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
5f5c8ee5 9167
43c09969
GM
9168 w->current_matrix->buffer = b;
9169 w->current_matrix->begv = BUF_BEGV (b);
9170 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5 9171
43c09969
GM
9172 w->last_cursor = w->cursor;
9173 w->last_cursor_off_p = w->cursor_off_p;
9174
9175 if (w == XWINDOW (selected_window))
9176 w->last_point = make_number (BUF_PT (b));
9177 else
9178 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 9179 }
43c09969 9180 }
bd66d1ba 9181
43c09969
GM
9182 if (accurate_p)
9183 {
d2f84654 9184 w->window_end_valid = w->buffer;
99332eb6 9185#if 0 /* This is incorrect with variable-height lines. */
2913a9c0
GM
9186 xassert (XINT (w->window_end_vpos)
9187 < (XINT (w->height)
9188 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 9189#endif
a2889657 9190 w->update_mode_line = Qnil;
43c09969
GM
9191 }
9192}
9193
9194
9195/* Mark the display of windows in the window tree rooted at WINDOW as
9196 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9197 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9198 be redisplayed the next time redisplay_internal is called. */
9199
9200void
9201mark_window_display_accurate (window, accurate_p)
9202 Lisp_Object window;
9203 int accurate_p;
9204{
9205 struct window *w;
9206
9207 for (; !NILP (window); window = w->next)
9208 {
9209 w = XWINDOW (window);
9210 mark_window_display_accurate_1 (w, accurate_p);
a2889657 9211
265a9e55 9212 if (!NILP (w->vchild))
5f5c8ee5 9213 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 9214 if (!NILP (w->hchild))
5f5c8ee5 9215 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
9216 }
9217
5f5c8ee5 9218 if (accurate_p)
a2889657 9219 {
d45de95b 9220 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
9221 last_arrow_string = Voverlay_arrow_string;
9222 }
9223 else
9224 {
5f5c8ee5
GM
9225 /* Force a thorough redisplay the next time by setting
9226 last_arrow_position and last_arrow_string to t, which is
4b41cebb 9227 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
9228 last_arrow_position = Qt;
9229 last_arrow_string = Qt;
9230 }
9231}
5f5c8ee5
GM
9232
9233
9234/* Return value in display table DP (Lisp_Char_Table *) for character
9235 C. Since a display table doesn't have any parent, we don't have to
9236 follow parent. Do not call this function directly but use the
9237 macro DISP_CHAR_VECTOR. */
9238
9239Lisp_Object
9240disp_char_vector (dp, c)
9241 struct Lisp_Char_Table *dp;
9242 int c;
9243{
9244 int code[4], i;
9245 Lisp_Object val;
9246
9247 if (SINGLE_BYTE_CHAR_P (c))
9248 return (dp->contents[c]);
9249
c5924f47 9250 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
9251 if (code[1] < 32)
9252 code[1] = -1;
9253 else if (code[2] < 32)
9254 code[2] = -1;
5f5c8ee5
GM
9255
9256 /* Here, the possible range of code[0] (== charset ID) is
9257 128..max_charset. Since the top level char table contains data
9258 for multibyte characters after 256th element, we must increment
9259 code[0] by 128 to get a correct index. */
9260 code[0] += 128;
9261 code[3] = -1; /* anchor */
9262
9263 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9264 {
9265 val = dp->contents[code[i]];
9266 if (!SUB_CHAR_TABLE_P (val))
9267 return (NILP (val) ? dp->defalt : val);
9268 }
9269
9270 /* Here, val is a sub char table. We return the default value of
9271 it. */
9272 return (dp->defalt);
9273}
9274
9275
a2889657 9276\f
5f5c8ee5
GM
9277/***********************************************************************
9278 Window Redisplay
9279 ***********************************************************************/
a2725ab2 9280
5f5c8ee5 9281/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
9282
9283static void
5f5c8ee5
GM
9284redisplay_windows (window)
9285 Lisp_Object window;
90adcf20 9286{
5f5c8ee5
GM
9287 while (!NILP (window))
9288 {
9289 struct window *w = XWINDOW (window);
9290
9291 if (!NILP (w->hchild))
9292 redisplay_windows (w->hchild);
9293 else if (!NILP (w->vchild))
9294 redisplay_windows (w->vchild);
9295 else
82a7ab23
RS
9296 {
9297 displayed_buffer = XBUFFER (w->buffer);
9298 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9299 redisplay_window_error);
9300 }
a2725ab2 9301
5f5c8ee5
GM
9302 window = w->next;
9303 }
9304}
9305
82a7ab23
RS
9306static Lisp_Object
9307redisplay_window_error ()
9308{
9309 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9310 return Qnil;
9311}
9312
9313static Lisp_Object
9314redisplay_window_0 (window)
9315 Lisp_Object window;
9316{
9317 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9318 redisplay_window (window, 0);
9319 return Qnil;
9320}
5f5c8ee5 9321
82a7ab23
RS
9322static Lisp_Object
9323redisplay_window_1 (window)
9324 Lisp_Object window;
9325{
9326 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9327 redisplay_window (window, 1);
9328 return Qnil;
9329}
9330\f
5f5c8ee5
GM
9331/* Set cursor position of W. PT is assumed to be displayed in ROW.
9332 DELTA is the number of bytes by which positions recorded in ROW
9333 differ from current buffer positions. */
9334
9335void
9336set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9337 struct window *w;
9338 struct glyph_row *row;
9339 struct glyph_matrix *matrix;
9340 int delta, delta_bytes, dy, dvpos;
9341{
9342 struct glyph *glyph = row->glyphs[TEXT_AREA];
9343 struct glyph *end = glyph + row->used[TEXT_AREA];
9344 int x = row->x;
9345 int pt_old = PT - delta;
9346
9347 /* Skip over glyphs not having an object at the start of the row.
9348 These are special glyphs like truncation marks on terminal
9349 frames. */
9350 if (row->displays_text_p)
9351 while (glyph < end
6fc556fd 9352 && INTEGERP (glyph->object)
5f5c8ee5
GM
9353 && glyph->charpos < 0)
9354 {
9355 x += glyph->pixel_width;
9356 ++glyph;
9357 }
9358
9359 while (glyph < end
6fc556fd 9360 && !INTEGERP (glyph->object)
5f5c8ee5
GM
9361 && (!BUFFERP (glyph->object)
9362 || glyph->charpos < pt_old))
9363 {
9364 x += glyph->pixel_width;
9365 ++glyph;
9366 }
9367
9368 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9369 w->cursor.x = x;
9370 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9371 w->cursor.y = row->y + dy;
9372
9373 if (w == XWINDOW (selected_window))
9374 {
9375 if (!row->continued_p
9376 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9377 && row->x == 0)
9378 {
9379 this_line_buffer = XBUFFER (w->buffer);
9380
9381 CHARPOS (this_line_start_pos)
9382 = MATRIX_ROW_START_CHARPOS (row) + delta;
9383 BYTEPOS (this_line_start_pos)
9384 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9385
9386 CHARPOS (this_line_end_pos)
9387 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9388 BYTEPOS (this_line_end_pos)
9389 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9390
9391 this_line_y = w->cursor.y;
9392 this_line_pixel_height = row->height;
9393 this_line_vpos = w->cursor.vpos;
9394 this_line_start_x = row->x;
9395 }
9396 else
9397 CHARPOS (this_line_start_pos) = 0;
9398 }
9399}
9400
9401
9402/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
9403 start STARTP. Sets the window start of WINDOW to that position.
9404
9405 We assume that the window's buffer is really current. */
5f5c8ee5
GM
9406
9407static INLINE struct text_pos
9408run_window_scroll_functions (window, startp)
9409 Lisp_Object window;
9410 struct text_pos startp;
9411{
9412 struct window *w = XWINDOW (window);
9413 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
9414
9415 if (current_buffer != XBUFFER (w->buffer))
9416 abort ();
9417
5f5c8ee5
GM
9418 if (!NILP (Vwindow_scroll_functions))
9419 {
9420 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9421 make_number (CHARPOS (startp)));
9422 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
9423 /* In case the hook functions switch buffers. */
9424 if (current_buffer != XBUFFER (w->buffer))
9425 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 9426 }
90adcf20 9427
5f5c8ee5
GM
9428 return startp;
9429}
9430
9431
9432/* Modify the desired matrix of window W and W->vscroll so that the
cb617e7c
GM
9433 line containing the cursor is fully visible. If this requires
9434 larger matrices than are allocated, set fonts_changed_p and return
9435 0. */
5f5c8ee5 9436
cb617e7c 9437static int
5f5c8ee5
GM
9438make_cursor_line_fully_visible (w)
9439 struct window *w;
9440{
9441 struct glyph_matrix *matrix;
9442 struct glyph_row *row;
478d746b 9443 int window_height;
5f5c8ee5
GM
9444
9445 /* It's not always possible to find the cursor, e.g, when a window
9446 is full of overlay strings. Don't do anything in that case. */
9447 if (w->cursor.vpos < 0)
cb617e7c 9448 return 1;
5f5c8ee5
GM
9449
9450 matrix = w->desired_matrix;
9451 row = MATRIX_ROW (matrix, w->cursor.vpos);
9452
b28cb6ed
GM
9453 /* If the cursor row is not partially visible, there's nothing
9454 to do. */
9455 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 9456 return 1;
b28cb6ed
GM
9457
9458 /* If the row the cursor is in is taller than the window's height,
9459 it's not clear what to do, so do nothing. */
9460 window_height = window_box_height (w);
9461 if (row->height >= window_height)
cb617e7c 9462 return 1;
b28cb6ed
GM
9463
9464 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
9465 {
9466 int dy = row->height - row->visible_height;
9467 w->vscroll = 0;
9468 w->cursor.y += dy;
9469 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9470 }
b28cb6ed 9471 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
9472 {
9473 int dy = - (row->height - row->visible_height);
9474 w->vscroll = dy;
9475 w->cursor.y += dy;
9476 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9477 }
b28cb6ed 9478
5f5c8ee5
GM
9479 /* When we change the cursor y-position of the selected window,
9480 change this_line_y as well so that the display optimization for
9481 the cursor line of the selected window in redisplay_internal uses
9482 the correct y-position. */
9483 if (w == XWINDOW (selected_window))
9484 this_line_y = w->cursor.y;
cb617e7c
GM
9485
9486 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9487 redisplay with larger matrices. */
9488 if (matrix->nrows < required_matrix_height (w))
9489 {
9490 fonts_changed_p = 1;
9491 return 0;
9492 }
9493
9494 return 1;
5f5c8ee5
GM
9495}
9496
9497
9498/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9499 non-zero means only WINDOW is redisplayed in redisplay_internal.
9500 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9501 in redisplay_window to bring a partially visible line into view in
9502 the case that only the cursor has moved.
9503
9504 Value is
9505
9506 1 if scrolling succeeded
9507
9508 0 if scrolling didn't find point.
9509
9510 -1 if new fonts have been loaded so that we must interrupt
9511 redisplay, adjust glyph matrices, and try again. */
9512
cb617e7c
GM
9513enum
9514{
9515 SCROLLING_SUCCESS,
9516 SCROLLING_FAILED,
9517 SCROLLING_NEED_LARGER_MATRICES
9518};
9519
5f5c8ee5
GM
9520static int
9521try_scrolling (window, just_this_one_p, scroll_conservatively,
9522 scroll_step, temp_scroll_step)
9523 Lisp_Object window;
9524 int just_this_one_p;
31ade731 9525 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5
GM
9526 int temp_scroll_step;
9527{
9528 struct window *w = XWINDOW (window);
9529 struct frame *f = XFRAME (w->frame);
9530 struct text_pos scroll_margin_pos;
9531 struct text_pos pos;
9532 struct text_pos startp;
9533 struct it it;
9534 Lisp_Object window_end;
9535 int this_scroll_margin;
9536 int dy = 0;
9537 int scroll_max;
b8a63ccb 9538 int rc;
5f5c8ee5
GM
9539 int amount_to_scroll = 0;
9540 Lisp_Object aggressive;
9541 int height;
9542
9543#if GLYPH_DEBUG
9544 debug_method_add (w, "try_scrolling");
78614721 9545#endif
5f5c8ee5
GM
9546
9547 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9548
9549 /* Compute scroll margin height in pixels. We scroll when point is
9550 within this distance from the top or bottom of the window. */
9551 if (scroll_margin > 0)
90adcf20 9552 {
5f5c8ee5
GM
9553 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9554 this_scroll_margin *= CANON_Y_UNIT (f);
9555 }
9556 else
9557 this_scroll_margin = 0;
9558
9559 /* Compute how much we should try to scroll maximally to bring point
9560 into view. */
0894e696
SM
9561 if (scroll_step || scroll_conservatively || temp_scroll_step)
9562 scroll_max = max (scroll_step,
9563 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
9564 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9565 || NUMBERP (current_buffer->scroll_up_aggressively))
9566 /* We're trying to scroll because of aggressive scrolling
9567 but no scroll_step is set. Choose an arbitrary one. Maybe
9568 there should be a variable for this. */
9569 scroll_max = 10;
9570 else
9571 scroll_max = 0;
9572 scroll_max *= CANON_Y_UNIT (f);
9573
9574 /* Decide whether we have to scroll down. Start at the window end
9575 and move this_scroll_margin up to find the position of the scroll
9576 margin. */
9577 window_end = Fwindow_end (window, Qt);
9578 CHARPOS (scroll_margin_pos) = XINT (window_end);
9579 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9580 if (this_scroll_margin)
9581 {
9582 start_display (&it, w, scroll_margin_pos);
9583 move_it_vertically (&it, - this_scroll_margin);
9584 scroll_margin_pos = it.current.pos;
9585 }
9586
9587 if (PT >= CHARPOS (scroll_margin_pos))
9588 {
9589 int y0;
9590
9591 /* Point is in the scroll margin at the bottom of the window, or
9592 below. Compute a new window start that makes point visible. */
47589c8c 9593
5f5c8ee5
GM
9594 /* Compute the distance from the scroll margin to PT.
9595 Give up if the distance is greater than scroll_max. */
9596 start_display (&it, w, scroll_margin_pos);
9597 y0 = it.current_y;
9598 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9599 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
9600
9601 /* To make point visible, we have to move the window start
9602 down so that the line the cursor is in is visible, which
9603 means we have to add in the height of the cursor line. */
9604 dy = line_bottom_y (&it) - y0;
47589c8c 9605
5f5c8ee5 9606 if (dy > scroll_max)
cb617e7c 9607 return SCROLLING_FAILED;
5f5c8ee5
GM
9608
9609 /* Move the window start down. If scrolling conservatively,
9610 move it just enough down to make point visible. If
9611 scroll_step is set, move it down by scroll_step. */
9612 start_display (&it, w, startp);
9613
9614 if (scroll_conservatively)
e89aaabd
GM
9615 amount_to_scroll
9616 = max (max (dy, CANON_Y_UNIT (f)),
9617 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9618 else if (scroll_step || temp_scroll_step)
9619 amount_to_scroll = scroll_max;
9620 else
90adcf20 9621 {
46226c1d 9622 aggressive = current_buffer->scroll_up_aggressively;
5f5c8ee5 9623 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9624 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9625 if (NUMBERP (aggressive))
9626 amount_to_scroll = XFLOATINT (aggressive) * height;
9627 }
a2725ab2 9628
5f5c8ee5 9629 if (amount_to_scroll <= 0)
cb617e7c 9630 return SCROLLING_FAILED;
a2725ab2 9631
5f5c8ee5
GM
9632 move_it_vertically (&it, amount_to_scroll);
9633 startp = it.current.pos;
9634 }
9635 else
9636 {
9637 /* See if point is inside the scroll margin at the top of the
9638 window. */
9639 scroll_margin_pos = startp;
9640 if (this_scroll_margin)
9641 {
9642 start_display (&it, w, startp);
9643 move_it_vertically (&it, this_scroll_margin);
9644 scroll_margin_pos = it.current.pos;
9645 }
9646
9647 if (PT < CHARPOS (scroll_margin_pos))
9648 {
9649 /* Point is in the scroll margin at the top of the window or
9650 above what is displayed in the window. */
9651 int y0;
9652
9653 /* Compute the vertical distance from PT to the scroll
9654 margin position. Give up if distance is greater than
9655 scroll_max. */
9656 SET_TEXT_POS (pos, PT, PT_BYTE);
9657 start_display (&it, w, pos);
9658 y0 = it.current_y;
9659 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9660 it.last_visible_y, -1,
9661 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9662 dy = it.current_y - y0;
9663 if (dy > scroll_max)
cb617e7c 9664 return SCROLLING_FAILED;
5f5c8ee5
GM
9665
9666 /* Compute new window start. */
9667 start_display (&it, w, startp);
9668
9669 if (scroll_conservatively)
0894e696
SM
9670 amount_to_scroll =
9671 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9672 else if (scroll_step || temp_scroll_step)
9673 amount_to_scroll = scroll_max;
538f13d4 9674 else
5f5c8ee5 9675 {
46226c1d 9676 aggressive = current_buffer->scroll_down_aggressively;
5f5c8ee5 9677 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9678 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9679 if (NUMBERP (aggressive))
9680 amount_to_scroll = XFLOATINT (aggressive) * height;
9681 }
a2725ab2 9682
5f5c8ee5 9683 if (amount_to_scroll <= 0)
cb617e7c 9684 return SCROLLING_FAILED;
5f5c8ee5
GM
9685
9686 move_it_vertically (&it, - amount_to_scroll);
9687 startp = it.current.pos;
90adcf20
RS
9688 }
9689 }
a2889657 9690
5f5c8ee5
GM
9691 /* Run window scroll functions. */
9692 startp = run_window_scroll_functions (window, startp);
90adcf20 9693
5f5c8ee5
GM
9694 /* Display the window. Give up if new fonts are loaded, or if point
9695 doesn't appear. */
9696 if (!try_window (window, startp))
cb617e7c 9697 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
9698 else if (w->cursor.vpos < 0)
9699 {
9700 clear_glyph_matrix (w->desired_matrix);
cb617e7c 9701 rc = SCROLLING_FAILED;
5f5c8ee5
GM
9702 }
9703 else
9704 {
9705 /* Maybe forget recorded base line for line number display. */
9706 if (!just_this_one_p
9707 || current_buffer->clip_changed
9142dd5b 9708 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
9709 w->base_line_number = Qnil;
9710
9711 /* If cursor ends up on a partially visible line, shift display
cb617e7c
GM
9712 lines up or down. If that fails because we need larger
9713 matrices, give up. */
9714 if (!make_cursor_line_fully_visible (w))
9715 rc = SCROLLING_NEED_LARGER_MATRICES;
9716 else
9717 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
9718 }
9719
9720 return rc;
a2889657
JB
9721}
9722
5f5c8ee5
GM
9723
9724/* Compute a suitable window start for window W if display of W starts
9725 on a continuation line. Value is non-zero if a new window start
9726 was computed.
9727
9728 The new window start will be computed, based on W's width, starting
9729 from the start of the continued line. It is the start of the
9730 screen line with the minimum distance from the old start W->start. */
9731
9732static int
9733compute_window_start_on_continuation_line (w)
9734 struct window *w;
1f1ff51d 9735{
5f5c8ee5
GM
9736 struct text_pos pos, start_pos;
9737 int window_start_changed_p = 0;
1f1ff51d 9738
5f5c8ee5 9739 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 9740
5f5c8ee5
GM
9741 /* If window start is on a continuation line... Window start may be
9742 < BEGV in case there's invisible text at the start of the
9743 buffer (M-x rmail, for example). */
9744 if (CHARPOS (start_pos) > BEGV
9745 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 9746 {
5f5c8ee5
GM
9747 struct it it;
9748 struct glyph_row *row;
f3751a65
GM
9749
9750 /* Handle the case that the window start is out of range. */
9751 if (CHARPOS (start_pos) < BEGV)
9752 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9753 else if (CHARPOS (start_pos) > ZV)
9754 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
9755
9756 /* Find the start of the continued line. This should be fast
9757 because scan_buffer is fast (newline cache). */
045dee35 9758 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
9759 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9760 row, DEFAULT_FACE_ID);
9761 reseat_at_previous_visible_line_start (&it);
9762
9763 /* If the line start is "too far" away from the window start,
9764 say it takes too much time to compute a new window start. */
9765 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9766 < XFASTINT (w->height) * XFASTINT (w->width))
9767 {
9768 int min_distance, distance;
9769
9770 /* Move forward by display lines to find the new window
9771 start. If window width was enlarged, the new start can
9772 be expected to be > the old start. If window width was
9773 decreased, the new window start will be < the old start.
9774 So, we're looking for the display line start with the
9775 minimum distance from the old window start. */
9776 pos = it.current.pos;
9777 min_distance = INFINITY;
9778 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9779 distance < min_distance)
9780 {
9781 min_distance = distance;
9782 pos = it.current.pos;
9783 move_it_by_lines (&it, 1, 0);
9784 }
9785
9786 /* Set the window start there. */
9787 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9788 window_start_changed_p = 1;
9789 }
1f1ff51d 9790 }
5f5c8ee5
GM
9791
9792 return window_start_changed_p;
1f1ff51d
KH
9793}
9794
5f5c8ee5 9795
47589c8c
GM
9796/* Try cursor movement in case text has not changes in window WINDOW,
9797 with window start STARTP. Value is
9798
cb617e7c 9799 CURSOR_MOVEMENT_SUCCESS if successful
47589c8c 9800
cb617e7c
GM
9801 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9802
9803 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9804 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9805 we want to scroll as if scroll-step were set to 1. See the code.
9806
9807 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9808 which case we have to abort this redisplay, and adjust matrices
9809 first. */
9810
9811enum
9812{
9813 CURSOR_MOVEMENT_SUCCESS,
9814 CURSOR_MOVEMENT_CANNOT_BE_USED,
9815 CURSOR_MOVEMENT_MUST_SCROLL,
9816 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9817};
47589c8c
GM
9818
9819static int
9820try_cursor_movement (window, startp, scroll_step)
9821 Lisp_Object window;
9822 struct text_pos startp;
9823 int *scroll_step;
9824{
9825 struct window *w = XWINDOW (window);
9826 struct frame *f = XFRAME (w->frame);
cb617e7c 9827 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
47589c8c 9828
69d1f7c9 9829#if GLYPH_DEBUG
76cb5e06
GM
9830 if (inhibit_try_cursor_movement)
9831 return rc;
9832#endif
9833
47589c8c
GM
9834 /* Handle case where text has not changed, only point, and it has
9835 not moved off the frame. */
9836 if (/* Point may be in this window. */
9837 PT >= CHARPOS (startp)
47589c8c
GM
9838 /* Selective display hasn't changed. */
9839 && !current_buffer->clip_changed
4db87380
GM
9840 /* Function force-mode-line-update is used to force a thorough
9841 redisplay. It sets either windows_or_buffers_changed or
9842 update_mode_lines. So don't take a shortcut here for these
9843 cases. */
9844 && !update_mode_lines
9845 && !windows_or_buffers_changed
47589c8c
GM
9846 /* Can't use this case if highlighting a region. When a
9847 region exists, cursor movement has to do more than just
9848 set the cursor. */
9849 && !(!NILP (Vtransient_mark_mode)
9850 && !NILP (current_buffer->mark_active))
9851 && NILP (w->region_showing)
9852 && NILP (Vshow_trailing_whitespace)
9853 /* Right after splitting windows, last_point may be nil. */
9854 && INTEGERP (w->last_point)
9855 /* This code is not used for mini-buffer for the sake of the case
9856 of redisplaying to replace an echo area message; since in
9857 that case the mini-buffer contents per se are usually
9858 unchanged. This code is of no real use in the mini-buffer
9859 since the handling of this_line_start_pos, etc., in redisplay
9860 handles the same cases. */
9861 && !EQ (window, minibuf_window)
9862 /* When splitting windows or for new windows, it happens that
9863 redisplay is called with a nil window_end_vpos or one being
9864 larger than the window. This should really be fixed in
9865 window.c. I don't have this on my list, now, so we do
9866 approximately the same as the old redisplay code. --gerd. */
9867 && INTEGERP (w->window_end_vpos)
9868 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9869 && (FRAME_WINDOW_P (f)
9870 || !MARKERP (Voverlay_arrow_position)
9871 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9872 {
9873 int this_scroll_margin;
8ee5b6a3 9874 struct glyph_row *row = NULL;
47589c8c
GM
9875
9876#if GLYPH_DEBUG
9877 debug_method_add (w, "cursor movement");
9878#endif
9879
9880 /* Scroll if point within this distance from the top or bottom
9881 of the window. This is a pixel value. */
9882 this_scroll_margin = max (0, scroll_margin);
9883 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9884 this_scroll_margin *= CANON_Y_UNIT (f);
9885
9886 /* Start with the row the cursor was displayed during the last
9887 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
9888 if (w->last_cursor.vpos < 0
9889 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 9890 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
9891 else
9892 {
9893 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9894 if (row->mode_line_p)
9895 ++row;
9896 if (!row->enabled_p)
cb617e7c 9897 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
9898 }
9899
cb617e7c 9900 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
9901 {
9902 int scroll_p = 0;
68c5d1db
GM
9903 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9904
47589c8c
GM
9905 if (PT > XFASTINT (w->last_point))
9906 {
9907 /* Point has moved forward. */
47589c8c
GM
9908 while (MATRIX_ROW_END_CHARPOS (row) < PT
9909 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9910 {
9911 xassert (row->enabled_p);
9912 ++row;
9913 }
9914
9915 /* The end position of a row equals the start position
9916 of the next row. If PT is there, we would rather
cafafe0b
GM
9917 display it in the next line. */
9918 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9919 && MATRIX_ROW_END_CHARPOS (row) == PT
9920 && !cursor_row_p (w, row))
9921 ++row;
47589c8c
GM
9922
9923 /* If within the scroll margin, scroll. Note that
9924 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9925 the next line would be drawn, and that
9926 this_scroll_margin can be zero. */
9927 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9928 || PT > MATRIX_ROW_END_CHARPOS (row)
9929 /* Line is completely visible last line in window
9930 and PT is to be set in the next line. */
9931 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9932 && PT == MATRIX_ROW_END_CHARPOS (row)
9933 && !row->ends_at_zv_p
9934 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9935 scroll_p = 1;
9936 }
9937 else if (PT < XFASTINT (w->last_point))
9938 {
9939 /* Cursor has to be moved backward. Note that PT >=
9940 CHARPOS (startp) because of the outer
9941 if-statement. */
9942 while (!row->mode_line_p
9943 && (MATRIX_ROW_START_CHARPOS (row) > PT
9944 || (MATRIX_ROW_START_CHARPOS (row) == PT
9945 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9946 && (row->y > this_scroll_margin
9947 || CHARPOS (startp) == BEGV))
9948 {
9949 xassert (row->enabled_p);
9950 --row;
9951 }
9952
9953 /* Consider the following case: Window starts at BEGV,
9954 there is invisible, intangible text at BEGV, so that
9955 display starts at some point START > BEGV. It can
9956 happen that we are called with PT somewhere between
9957 BEGV and START. Try to handle that case. */
9958 if (row < w->current_matrix->rows
9959 || row->mode_line_p)
9960 {
9961 row = w->current_matrix->rows;
9962 if (row->mode_line_p)
9963 ++row;
9964 }
9965
9966 /* Due to newlines in overlay strings, we may have to
9967 skip forward over overlay strings. */
68c5d1db
GM
9968 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9969 && MATRIX_ROW_END_CHARPOS (row) == PT
9970 && !cursor_row_p (w, row))
47589c8c
GM
9971 ++row;
9972
9973 /* If within the scroll margin, scroll. */
9974 if (row->y < this_scroll_margin
9975 && CHARPOS (startp) != BEGV)
9976 scroll_p = 1;
9977 }
9978
9979 if (PT < MATRIX_ROW_START_CHARPOS (row)
9980 || PT > MATRIX_ROW_END_CHARPOS (row))
9981 {
9982 /* if PT is not in the glyph row, give up. */
cb617e7c 9983 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 9984 }
440fc135 9985 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 9986 {
8de4aaf8
GM
9987 if (PT == MATRIX_ROW_END_CHARPOS (row)
9988 && !row->ends_at_zv_p
9989 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 9990 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 9991 else if (row->height > window_box_height (w))
440fc135 9992 {
8de4aaf8
GM
9993 /* If we end up in a partially visible line, let's
9994 make it fully visible, except when it's taller
9995 than the window, in which case we can't do much
9996 about it. */
440fc135 9997 *scroll_step = 1;
cb617e7c 9998 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
9999 }
10000 else
10001 {
10002 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10003 try_window (window, startp);
cb617e7c
GM
10004 if (!make_cursor_line_fully_visible (w))
10005 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10006 else
10007 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 10008 }
47589c8c
GM
10009 }
10010 else if (scroll_p)
cb617e7c 10011 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10012 else
10013 {
10014 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 10015 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
10016 }
10017 }
10018 }
10019
10020 return rc;
10021}
10022
10023
5f5c8ee5
GM
10024/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10025 selected_window is redisplayed. */
90adcf20 10026
a2889657 10027static void
5f5c8ee5 10028redisplay_window (window, just_this_one_p)
a2889657 10029 Lisp_Object window;
5f5c8ee5 10030 int just_this_one_p;
a2889657 10031{
5f5c8ee5
GM
10032 struct window *w = XWINDOW (window);
10033 struct frame *f = XFRAME (w->frame);
10034 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 10035 struct buffer *old = current_buffer;
5f5c8ee5 10036 struct text_pos lpoint, opoint, startp;
e481f960 10037 int update_mode_line;
5f5c8ee5
GM
10038 int tem;
10039 struct it it;
10040 /* Record it now because it's overwritten. */
10041 int current_matrix_up_to_date_p = 0;
5f5c8ee5 10042 int temp_scroll_step = 0;
2d27dae2 10043 int count = BINDING_STACK_SIZE ();
47589c8c 10044 int rc;
a2889657 10045
5f5c8ee5
GM
10046 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10047 opoint = lpoint;
a2889657 10048
5f5c8ee5
GM
10049 /* W must be a leaf window here. */
10050 xassert (!NILP (w->buffer));
10051#if GLYPH_DEBUG
10052 *w->desired_matrix->method = 0;
10053#endif
2e54982e
RS
10054
10055 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
10056
10057 reconsider_clip_changes (w, buffer);
10058
5f5c8ee5
GM
10059 /* Has the mode line to be updated? */
10060 update_mode_line = (!NILP (w->update_mode_line)
10061 || update_mode_lines
10062 || buffer->clip_changed);
8de2d90b
JB
10063
10064 if (MINI_WINDOW_P (w))
10065 {
5f5c8ee5 10066 if (w == XWINDOW (echo_area_window)
c6e89d6c 10067 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
10068 {
10069 if (update_mode_line)
10070 /* We may have to update a tty frame's menu bar or a
e037b9ec 10071 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
10072 goto finish_menu_bars;
10073 else
10074 /* We've already displayed the echo area glyphs in this window. */
10075 goto finish_scroll_bars;
10076 }
73af359d 10077 else if (w != XWINDOW (minibuf_window))
8de2d90b 10078 {
5f5c8ee5
GM
10079 /* W is a mini-buffer window, but it's not the currently
10080 active one, so clear it. */
10081 int yb = window_text_bottom_y (w);
10082 struct glyph_row *row;
10083 int y;
10084
10085 for (y = 0, row = w->desired_matrix->rows;
10086 y < yb;
10087 y += row->height, ++row)
10088 blank_row (w, row, y);
88f22aff 10089 goto finish_scroll_bars;
8de2d90b 10090 }
c095a1dd
GM
10091
10092 clear_glyph_matrix (w->desired_matrix);
8de2d90b 10093 }
a2889657 10094
5f5c8ee5
GM
10095 /* Otherwise set up data on this window; select its buffer and point
10096 value. */
6a93695f
GM
10097 /* Really select the buffer, for the sake of buffer-local
10098 variables. */
10099 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
10100 SET_TEXT_POS (opoint, PT, PT_BYTE);
10101
10102 current_matrix_up_to_date_p
10103 = (!NILP (w->window_end_valid)
10104 && !current_buffer->clip_changed
10105 && XFASTINT (w->last_modified) >= MODIFF
10106 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 10107
5f5c8ee5
GM
10108 /* When windows_or_buffers_changed is non-zero, we can't rely on
10109 the window end being valid, so set it to nil there. */
10110 if (windows_or_buffers_changed)
10111 {
10112 /* If window starts on a continuation line, maybe adjust the
10113 window start in case the window's width changed. */
10114 if (XMARKER (w->start)->buffer == current_buffer)
10115 compute_window_start_on_continuation_line (w);
10116
10117 w->window_end_valid = Qnil;
10118 }
12adba34 10119
5f5c8ee5
GM
10120 /* Some sanity checks. */
10121 CHECK_WINDOW_END (w);
10122 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 10123 abort ();
5f5c8ee5 10124 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 10125 abort ();
a2889657 10126
28995e67
RS
10127 /* If %c is in mode line, update it if needed. */
10128 if (!NILP (w->column_number_displayed)
10129 /* This alternative quickly identifies a common case
10130 where no change is needed. */
10131 && !(PT == XFASTINT (w->last_point)
8850a573
RS
10132 && XFASTINT (w->last_modified) >= MODIFF
10133 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
10134 && XFASTINT (w->column_number_displayed) != current_column ())
10135 update_mode_line = 1;
10136
5f5c8ee5
GM
10137 /* Count number of windows showing the selected buffer. An indirect
10138 buffer counts as its base buffer. */
10139 if (!just_this_one_p)
42640f83
RS
10140 {
10141 struct buffer *current_base, *window_base;
10142 current_base = current_buffer;
10143 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10144 if (current_base->base_buffer)
10145 current_base = current_base->base_buffer;
10146 if (window_base->base_buffer)
10147 window_base = window_base->base_buffer;
10148 if (current_base == window_base)
10149 buffer_shared++;
10150 }
a2889657 10151
5f5c8ee5
GM
10152 /* Point refers normally to the selected window. For any other
10153 window, set up appropriate value. */
a2889657
JB
10154 if (!EQ (window, selected_window))
10155 {
12adba34
RS
10156 int new_pt = XMARKER (w->pointm)->charpos;
10157 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 10158 if (new_pt < BEGV)
a2889657 10159 {
f67a0f51 10160 new_pt = BEGV;
12adba34
RS
10161 new_pt_byte = BEGV_BYTE;
10162 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 10163 }
f67a0f51 10164 else if (new_pt > (ZV - 1))
a2889657 10165 {
f67a0f51 10166 new_pt = ZV;
12adba34
RS
10167 new_pt_byte = ZV_BYTE;
10168 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 10169 }
5f5c8ee5 10170
f67a0f51 10171 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 10172 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
10173 }
10174
f4faa47c 10175 /* If any of the character widths specified in the display table
5f5c8ee5
GM
10176 have changed, invalidate the width run cache. It's true that
10177 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
10178 redisplay goes (non-fatally) haywire when the display table is
10179 changed, so why should we worry about doing any better? */
10180 if (current_buffer->width_run_cache)
10181 {
f908610f 10182 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
10183
10184 if (! disptab_matches_widthtab (disptab,
10185 XVECTOR (current_buffer->width_table)))
10186 {
10187 invalidate_region_cache (current_buffer,
10188 current_buffer->width_run_cache,
10189 BEG, Z);
10190 recompute_width_table (current_buffer, disptab);
10191 }
10192 }
10193
a2889657 10194 /* If window-start is screwed up, choose a new one. */
a2889657
JB
10195 if (XMARKER (w->start)->buffer != current_buffer)
10196 goto recenter;
10197
5f5c8ee5 10198 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 10199
cf0df6ab
RS
10200 /* If someone specified a new starting point but did not insist,
10201 check whether it can be used. */
cfad01b4
GM
10202 if (!NILP (w->optional_new_start)
10203 && CHARPOS (startp) >= BEGV
10204 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
10205 {
10206 w->optional_new_start = Qnil;
5f5c8ee5
GM
10207 start_display (&it, w, startp);
10208 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10209 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10210 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
10211 w->force_start = Qt;
10212 }
10213
8de2d90b 10214 /* Handle case where place to start displaying has been specified,
aa6d10fa 10215 unless the specified location is outside the accessible range. */
9472f927
GM
10216 if (!NILP (w->force_start)
10217 || w->frozen_window_start_p)
a2889657 10218 {
e63574d7 10219 w->force_start = Qnil;
5f5c8ee5 10220 w->vscroll = 0;
b5174a51 10221 w->window_end_valid = Qnil;
5f5c8ee5
GM
10222
10223 /* Forget any recorded base line for line number display. */
10224 if (!current_matrix_up_to_date_p
10225 || current_buffer->clip_changed)
10226 w->base_line_number = Qnil;
10227
75c43375
RS
10228 /* Redisplay the mode line. Select the buffer properly for that.
10229 Also, run the hook window-scroll-functions
10230 because we have scrolled. */
e63574d7
RS
10231 /* Note, we do this after clearing force_start because
10232 if there's an error, it is better to forget about force_start
10233 than to get into an infinite loop calling the hook functions
10234 and having them get more errors. */
75c43375
RS
10235 if (!update_mode_line
10236 || ! NILP (Vwindow_scroll_functions))
e481f960 10237 {
e481f960
RS
10238 update_mode_line = 1;
10239 w->update_mode_line = Qt;
5f5c8ee5 10240 startp = run_window_scroll_functions (window, startp);
e481f960 10241 }
5f5c8ee5 10242
ac90c44f
GM
10243 w->last_modified = make_number (0);
10244 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
10245 if (CHARPOS (startp) < BEGV)
10246 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10247 else if (CHARPOS (startp) > ZV)
10248 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10249
10250 /* Redisplay, then check if cursor has been set during the
10251 redisplay. Give up if new fonts were loaded. */
10252 if (!try_window (window, startp))
10253 {
10254 w->force_start = Qt;
10255 clear_glyph_matrix (w->desired_matrix);
504454e8 10256 goto finish_scroll_bars;
5f5c8ee5
GM
10257 }
10258
9472f927 10259 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 10260 {
b28cb6ed
GM
10261 /* If point does not appear, try to move point so it does
10262 appear. The desired matrix has been built above, so we
10263 can use it here. */
10264 int window_height;
10265 struct glyph_row *row;
10266
10267 window_height = window_box_height (w) / 2;
10268 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10269 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
5f5c8ee5
GM
10270 ++row;
10271
10272 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10273 MATRIX_ROW_START_BYTEPOS (row));
10274
90adcf20 10275 if (w != XWINDOW (selected_window))
12adba34 10276 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
10277 else if (current_buffer == old)
10278 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10279
10280 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10281
10282 /* If we are highlighting the region, then we just changed
10283 the region, so redisplay to show it. */
df0b5ea1
RS
10284 if (!NILP (Vtransient_mark_mode)
10285 && !NILP (current_buffer->mark_active))
6f27fa9b 10286 {
5f5c8ee5
GM
10287 clear_glyph_matrix (w->desired_matrix);
10288 if (!try_window (window, startp))
cb617e7c 10289 goto need_larger_matrices;
6f27fa9b 10290 }
a2889657 10291 }
5f5c8ee5 10292
cb617e7c
GM
10293 if (!make_cursor_line_fully_visible (w))
10294 goto need_larger_matrices;
5f5c8ee5
GM
10295#if GLYPH_DEBUG
10296 debug_method_add (w, "forced window start");
10297#endif
a2889657
JB
10298 goto done;
10299 }
10300
5f5c8ee5
GM
10301 /* Handle case where text has not changed, only point, and it has
10302 not moved off the frame. */
10303 if (current_matrix_up_to_date_p
47589c8c 10304 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 10305 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 10306 {
cb617e7c
GM
10307 switch (rc)
10308 {
10309 case CURSOR_MOVEMENT_SUCCESS:
10310 goto done;
10311
10312 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10313 goto need_larger_matrices;
10314
10315 case CURSOR_MOVEMENT_MUST_SCROLL:
10316 goto try_to_scroll;
10317
10318 default:
10319 abort ();
10320 }
a2889657
JB
10321 }
10322 /* If current starting point was originally the beginning of a line
10323 but no longer is, find a new starting point. */
265a9e55 10324 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
10325 && !(CHARPOS (startp) <= BEGV
10326 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 10327 {
5f5c8ee5
GM
10328#if GLYPH_DEBUG
10329 debug_method_add (w, "recenter 1");
10330#endif
a2889657
JB
10331 goto recenter;
10332 }
5f5c8ee5 10333
27d16f05
GM
10334 /* Try scrolling with try_window_id. Value is > 0 if update has
10335 been done, it is -1 if we know that the same window start will
10336 not work. It is 0 if unsuccessful for some other reason. */
10337 else if ((tem = try_window_id (w)) != 0)
a2889657 10338 {
5f5c8ee5 10339#if GLYPH_DEBUG
ef121659 10340 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
10341#endif
10342
10343 if (fonts_changed_p)
cb617e7c 10344 goto need_larger_matrices;
a2889657
JB
10345 if (tem > 0)
10346 goto done;
ef121659 10347
5f5c8ee5
GM
10348 /* Otherwise try_window_id has returned -1 which means that we
10349 don't want the alternative below this comment to execute. */
a2889657 10350 }
5f5c8ee5
GM
10351 else if (CHARPOS (startp) >= BEGV
10352 && CHARPOS (startp) <= ZV
10353 && PT >= CHARPOS (startp)
10354 && (CHARPOS (startp) < ZV
e9874cee 10355 /* Avoid starting at end of buffer. */
5f5c8ee5 10356 || CHARPOS (startp) == BEGV
8850a573
RS
10357 || (XFASTINT (w->last_modified) >= MODIFF
10358 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 10359 {
5f5c8ee5
GM
10360#if GLYPH_DEBUG
10361 debug_method_add (w, "same window start");
10362#endif
10363
10364 /* Try to redisplay starting at same place as before.
10365 If point has not moved off frame, accept the results. */
10366 if (!current_matrix_up_to_date_p
10367 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
10368 because a window scroll function can have changed the
10369 buffer. */
5f5c8ee5
GM
10370 || !NILP (Vwindow_scroll_functions)
10371 || MINI_WINDOW_P (w)
10372 || !try_window_reusing_current_matrix (w))
10373 {
10374 IF_DEBUG (debug_method_add (w, "1"));
10375 try_window (window, startp);
10376 }
10377
10378 if (fonts_changed_p)
cb617e7c 10379 goto need_larger_matrices;
5f5c8ee5
GM
10380
10381 if (w->cursor.vpos >= 0)
aa6d10fa 10382 {
5f5c8ee5
GM
10383 if (!just_this_one_p
10384 || current_buffer->clip_changed
9142dd5b 10385 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
10386 /* Forget any recorded base line for line number display. */
10387 w->base_line_number = Qnil;
5f5c8ee5 10388
cb617e7c
GM
10389 if (!make_cursor_line_fully_visible (w))
10390 goto need_larger_matrices;
aa6d10fa
RS
10391 goto done;
10392 }
a2889657 10393 else
5f5c8ee5 10394 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
10395 }
10396
5f5c8ee5
GM
10397 try_to_scroll:
10398
ac90c44f
GM
10399 w->last_modified = make_number (0);
10400 w->last_overlay_modified = make_number (0);
5f5c8ee5 10401
e481f960
RS
10402 /* Redisplay the mode line. Select the buffer properly for that. */
10403 if (!update_mode_line)
10404 {
e481f960
RS
10405 update_mode_line = 1;
10406 w->update_mode_line = Qt;
10407 }
a2889657 10408
5f5c8ee5
GM
10409 /* Try to scroll by specified few lines. */
10410 if ((scroll_conservatively
10411 || scroll_step
10412 || temp_scroll_step
10413 || NUMBERP (current_buffer->scroll_up_aggressively)
10414 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 10415 && !current_buffer->clip_changed
5f5c8ee5
GM
10416 && CHARPOS (startp) >= BEGV
10417 && CHARPOS (startp) <= ZV)
0789adb2 10418 {
5f5c8ee5
GM
10419 /* The function returns -1 if new fonts were loaded, 1 if
10420 successful, 0 if not successful. */
10421 int rc = try_scrolling (window, just_this_one_p,
10422 scroll_conservatively,
10423 scroll_step,
10424 temp_scroll_step);
cb617e7c
GM
10425 switch (rc)
10426 {
10427 case SCROLLING_SUCCESS:
10428 goto done;
10429
10430 case SCROLLING_NEED_LARGER_MATRICES:
10431 goto need_larger_matrices;
10432
10433 case SCROLLING_FAILED:
10434 break;
10435
10436 default:
10437 abort ();
10438 }
5f5c8ee5 10439 }
f9c8af06 10440
5f5c8ee5 10441 /* Finally, just choose place to start which centers point */
5936754e 10442
5f5c8ee5 10443 recenter:
44173109 10444
5f5c8ee5
GM
10445#if GLYPH_DEBUG
10446 debug_method_add (w, "recenter");
10447#endif
0789adb2 10448
5f5c8ee5 10449 /* w->vscroll = 0; */
0789adb2 10450
5f5c8ee5
GM
10451 /* Forget any previously recorded base line for line number display. */
10452 if (!current_matrix_up_to_date_p
10453 || current_buffer->clip_changed)
10454 w->base_line_number = Qnil;
10455
10456 /* Move backward half the height of the window. */
10457 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10458 it.current_y = it.last_visible_y;
3a030013 10459 move_it_vertically_backward (&it, window_box_height (w) / 2);
5f5c8ee5
GM
10460 xassert (IT_CHARPOS (it) >= BEGV);
10461
10462 /* The function move_it_vertically_backward may move over more
10463 than the specified y-distance. If it->w is small, e.g. a
10464 mini-buffer window, we may end up in front of the window's
10465 display area. Start displaying at the start of the line
10466 containing PT in this case. */
10467 if (it.current_y <= 0)
10468 {
10469 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10470 move_it_vertically (&it, 0);
10471 xassert (IT_CHARPOS (it) <= PT);
10472 it.current_y = 0;
0789adb2
RS
10473 }
10474
5f5c8ee5
GM
10475 it.current_x = it.hpos = 0;
10476
10477 /* Set startp here explicitly in case that helps avoid an infinite loop
10478 in case the window-scroll-functions functions get errors. */
10479 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10480
10481 /* Run scroll hooks. */
10482 startp = run_window_scroll_functions (window, it.current.pos);
10483
10484 /* Redisplay the window. */
10485 if (!current_matrix_up_to_date_p
10486 || windows_or_buffers_changed
10487 /* Don't use try_window_reusing_current_matrix in this case
10488 because it can have changed the buffer. */
10489 || !NILP (Vwindow_scroll_functions)
10490 || !just_this_one_p
10491 || MINI_WINDOW_P (w)
10492 || !try_window_reusing_current_matrix (w))
10493 try_window (window, startp);
10494
10495 /* If new fonts have been loaded (due to fontsets), give up. We
10496 have to start a new redisplay since we need to re-adjust glyph
10497 matrices. */
10498 if (fonts_changed_p)
cb617e7c 10499 goto need_larger_matrices;
5f5c8ee5
GM
10500
10501 /* If cursor did not appear assume that the middle of the window is
10502 in the first line of the window. Do it again with the next line.
10503 (Imagine a window of height 100, displaying two lines of height
10504 60. Moving back 50 from it->last_visible_y will end in the first
10505 line.) */
10506 if (w->cursor.vpos < 0)
a2889657 10507 {
5f5c8ee5
GM
10508 if (!NILP (w->window_end_valid)
10509 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 10510 {
5f5c8ee5
GM
10511 clear_glyph_matrix (w->desired_matrix);
10512 move_it_by_lines (&it, 1, 0);
10513 try_window (window, it.current.pos);
a2889657 10514 }
5f5c8ee5 10515 else if (PT < IT_CHARPOS (it))
a2889657 10516 {
5f5c8ee5
GM
10517 clear_glyph_matrix (w->desired_matrix);
10518 move_it_by_lines (&it, -1, 0);
10519 try_window (window, it.current.pos);
10520 }
10521 else
10522 {
10523 /* Not much we can do about it. */
a2889657 10524 }
a2889657 10525 }
010494d0 10526
5f5c8ee5
GM
10527 /* Consider the following case: Window starts at BEGV, there is
10528 invisible, intangible text at BEGV, so that display starts at
10529 some point START > BEGV. It can happen that we are called with
10530 PT somewhere between BEGV and START. Try to handle that case. */
10531 if (w->cursor.vpos < 0)
835766b6 10532 {
5f5c8ee5
GM
10533 struct glyph_row *row = w->current_matrix->rows;
10534 if (row->mode_line_p)
10535 ++row;
10536 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 10537 }
5f5c8ee5 10538
cb617e7c
GM
10539 if (!make_cursor_line_fully_visible (w))
10540 goto need_larger_matrices;
b5174a51 10541
74d481ac
GM
10542 done:
10543
5f5c8ee5
GM
10544 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10545 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10546 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10547 ? Qt : Qnil);
a2889657 10548
5f5c8ee5 10549 /* Display the mode line, if we must. */
e481f960 10550 if ((update_mode_line
aa6d10fa 10551 /* If window not full width, must redo its mode line
5f5c8ee5
GM
10552 if (a) the window to its side is being redone and
10553 (b) we do a frame-based redisplay. This is a consequence
10554 of how inverted lines are drawn in frame-based redisplay. */
10555 || (!just_this_one_p
10556 && !FRAME_WINDOW_P (f)
10557 && !WINDOW_FULL_WIDTH_P (w))
10558 /* Line number to display. */
155ef550 10559 || INTEGERP (w->base_line_pos)
5f5c8ee5 10560 /* Column number is displayed and different from the one displayed. */
155ef550
KH
10561 || (!NILP (w->column_number_displayed)
10562 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
10563 /* This means that the window has a mode line. */
10564 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 10565 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 10566 {
5f5c8ee5
GM
10567 display_mode_lines (w);
10568
10569 /* If mode line height has changed, arrange for a thorough
10570 immediate redisplay using the correct mode line height. */
10571 if (WINDOW_WANTS_MODELINE_P (w)
10572 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 10573 {
5f5c8ee5
GM
10574 fonts_changed_p = 1;
10575 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10576 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 10577 }
5f5c8ee5
GM
10578
10579 /* If top line height has changed, arrange for a thorough
10580 immediate redisplay using the correct mode line height. */
045dee35
GM
10581 if (WINDOW_WANTS_HEADER_LINE_P (w)
10582 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
10583 {
10584 fonts_changed_p = 1;
045dee35
GM
10585 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10586 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10587 }
10588
10589 if (fonts_changed_p)
cb617e7c 10590 goto need_larger_matrices;
5ba50c51 10591 }
5f5c8ee5
GM
10592
10593 if (!line_number_displayed
10594 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
10595 {
10596 w->base_line_pos = Qnil;
10597 w->base_line_number = Qnil;
10598 }
a2889657 10599
5f5c8ee5
GM
10600 finish_menu_bars:
10601
7ce2c095 10602 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 10603 if (update_mode_line
5f5c8ee5
GM
10604 && EQ (FRAME_SELECTED_WINDOW (f), window))
10605 {
10606 int redisplay_menu_p = 0;
10607
10608 if (FRAME_WINDOW_P (f))
10609 {
1a578e9b 10610#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5 10611 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 10612#else
5f5c8ee5 10613 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 10614#endif
5f5c8ee5
GM
10615 }
10616 else
10617 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10618
10619 if (redisplay_menu_p)
10620 display_menu_bar (w);
10621
10622#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
10623 if (WINDOWP (f->tool_bar_window)
10624 && (FRAME_TOOL_BAR_LINES (f) > 0
10625 || auto_resize_tool_bars_p))
10626 redisplay_tool_bar (f);
5f5c8ee5
GM
10627#endif
10628 }
7ce2c095 10629
cb617e7c
GM
10630 need_larger_matrices:
10631 ;
88f22aff 10632 finish_scroll_bars:
5f5c8ee5 10633
88f22aff 10634 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 10635 {
b1d1124b 10636 int start, end, whole;
30c566e4 10637
b1d1124b 10638 /* Calculate the start and end positions for the current window.
3505ea70
JB
10639 At some point, it would be nice to choose between scrollbars
10640 which reflect the whole buffer size, with special markers
10641 indicating narrowing, and scrollbars which reflect only the
10642 visible region.
10643
5f5c8ee5 10644 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 10645 if (!MINI_WINDOW_P (w)
5f5c8ee5 10646 || (w == XWINDOW (minibuf_window)
c6e89d6c 10647 && NILP (echo_area_buffer[0])))
b1d1124b 10648 {
8a9311d7 10649 whole = ZV - BEGV;
4d641a15 10650 start = marker_position (w->start) - BEGV;
b1d1124b
JB
10651 /* I don't think this is guaranteed to be right. For the
10652 moment, we'll pretend it is. */
5f5c8ee5 10653 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 10654
5f5c8ee5
GM
10655 if (end < start)
10656 end = start;
10657 if (whole < (end - start))
10658 whole = end - start;
b1d1124b
JB
10659 }
10660 else
10661 start = end = whole = 0;
30c566e4 10662
88f22aff 10663 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 10664 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 10665
5f5c8ee5
GM
10666 /* Note that we actually used the scroll bar attached to this
10667 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 10668 redeem_scroll_bar_hook (w);
30c566e4 10669 }
b1d1124b 10670
5f5c8ee5
GM
10671 /* Restore current_buffer and value of point in it. */
10672 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 10673 set_buffer_internal_1 (old);
5f5c8ee5 10674 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
10675
10676 unbind_to (count, Qnil);
a2889657 10677}
a2889657 10678
5f5c8ee5
GM
10679
10680/* Build the complete desired matrix of WINDOW with a window start
10681 buffer position POS. Value is non-zero if successful. It is zero
10682 if fonts were loaded during redisplay which makes re-adjusting
10683 glyph matrices necessary. */
10684
10685int
a2889657
JB
10686try_window (window, pos)
10687 Lisp_Object window;
5f5c8ee5
GM
10688 struct text_pos pos;
10689{
10690 struct window *w = XWINDOW (window);
10691 struct it it;
10692 struct glyph_row *last_text_row = NULL;
9cbab4ff 10693
5f5c8ee5
GM
10694 /* Make POS the new window start. */
10695 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 10696
5f5c8ee5
GM
10697 /* Mark cursor position as unknown. No overlay arrow seen. */
10698 w->cursor.vpos = -1;
a2889657 10699 overlay_arrow_seen = 0;
642eefc6 10700
5f5c8ee5
GM
10701 /* Initialize iterator and info to start at POS. */
10702 start_display (&it, w, pos);
a2889657 10703
5f5c8ee5
GM
10704 /* Display all lines of W. */
10705 while (it.current_y < it.last_visible_y)
10706 {
10707 if (display_line (&it))
10708 last_text_row = it.glyph_row - 1;
10709 if (fonts_changed_p)
10710 return 0;
10711 }
a2889657 10712
5f5c8ee5
GM
10713 /* If bottom moved off end of frame, change mode line percentage. */
10714 if (XFASTINT (w->window_end_pos) <= 0
10715 && Z != IT_CHARPOS (it))
a2889657
JB
10716 w->update_mode_line = Qt;
10717
5f5c8ee5
GM
10718 /* Set window_end_pos to the offset of the last character displayed
10719 on the window from the end of current_buffer. Set
10720 window_end_vpos to its row number. */
10721 if (last_text_row)
10722 {
10723 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10724 w->window_end_bytepos
10725 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10726 w->window_end_pos
10727 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10728 w->window_end_vpos
10729 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10730 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10731 ->displays_text_p);
10732 }
10733 else
10734 {
10735 w->window_end_bytepos = 0;
ac90c44f 10736 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10737 }
10738
a2889657
JB
10739 /* But that is not valid info until redisplay finishes. */
10740 w->window_end_valid = Qnil;
5f5c8ee5 10741 return 1;
a2889657 10742}
5f5c8ee5
GM
10743
10744
a2889657 10745\f
5f5c8ee5
GM
10746/************************************************************************
10747 Window redisplay reusing current matrix when buffer has not changed
10748 ************************************************************************/
10749
10750/* Try redisplay of window W showing an unchanged buffer with a
10751 different window start than the last time it was displayed by
10752 reusing its current matrix. Value is non-zero if successful.
10753 W->start is the new window start. */
a2889657
JB
10754
10755static int
5f5c8ee5
GM
10756try_window_reusing_current_matrix (w)
10757 struct window *w;
a2889657 10758{
5f5c8ee5
GM
10759 struct frame *f = XFRAME (w->frame);
10760 struct glyph_row *row, *bottom_row;
10761 struct it it;
10762 struct run run;
10763 struct text_pos start, new_start;
10764 int nrows_scrolled, i;
10765 struct glyph_row *last_text_row;
10766 struct glyph_row *last_reused_text_row;
10767 struct glyph_row *start_row;
10768 int start_vpos, min_y, max_y;
75c5350a 10769
69d1f7c9 10770#if GLYPH_DEBUG
76cb5e06
GM
10771 if (inhibit_try_window_reusing)
10772 return 0;
10773#endif
10774
d18354b6
GM
10775 if (/* This function doesn't handle terminal frames. */
10776 !FRAME_WINDOW_P (f)
10777 /* Don't try to reuse the display if windows have been split
10778 or such. */
10779 || windows_or_buffers_changed)
5f5c8ee5 10780 return 0;
a2889657 10781
5f5c8ee5
GM
10782 /* Can't do this if region may have changed. */
10783 if ((!NILP (Vtransient_mark_mode)
10784 && !NILP (current_buffer->mark_active))
8f897821
GM
10785 || !NILP (w->region_showing)
10786 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 10787 return 0;
a2889657 10788
5f5c8ee5 10789 /* If top-line visibility has changed, give up. */
045dee35
GM
10790 if (WINDOW_WANTS_HEADER_LINE_P (w)
10791 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
10792 return 0;
10793
10794 /* Give up if old or new display is scrolled vertically. We could
10795 make this function handle this, but right now it doesn't. */
10796 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10797 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10798 return 0;
10799
10800 /* The variable new_start now holds the new window start. The old
10801 start `start' can be determined from the current matrix. */
10802 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10803 start = start_row->start.pos;
10804 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 10805
5f5c8ee5
GM
10806 /* Clear the desired matrix for the display below. */
10807 clear_glyph_matrix (w->desired_matrix);
10808
10809 if (CHARPOS (new_start) <= CHARPOS (start))
10810 {
10811 int first_row_y;
10812
607ec83c
GM
10813 /* Don't use this method if the display starts with an ellipsis
10814 displayed for invisible text. It's not easy to handle that case
10815 below, and it's certainly not worth the effort since this is
10816 not a frequent case. */
10817 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10818 return 0;
10819
5f5c8ee5
GM
10820 IF_DEBUG (debug_method_add (w, "twu1"));
10821
10822 /* Display up to a row that can be reused. The variable
10823 last_text_row is set to the last row displayed that displays
b48f74cb
GM
10824 text. Note that it.vpos == 0 if or if not there is a
10825 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
10826 start_display (&it, w, new_start);
10827 first_row_y = it.current_y;
10828 w->cursor.vpos = -1;
10829 last_text_row = last_reused_text_row = NULL;
b48f74cb 10830
5f5c8ee5
GM
10831 while (it.current_y < it.last_visible_y
10832 && IT_CHARPOS (it) < CHARPOS (start)
10833 && !fonts_changed_p)
10834 if (display_line (&it))
10835 last_text_row = it.glyph_row - 1;
10836
10837 /* A value of current_y < last_visible_y means that we stopped
10838 at the previous window start, which in turn means that we
10839 have at least one reusable row. */
10840 if (it.current_y < it.last_visible_y)
a2889657 10841 {
b48f74cb 10842 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5
GM
10843 nrows_scrolled = it.vpos;
10844
10845 /* Find PT if not already found in the lines displayed. */
10846 if (w->cursor.vpos < 0)
a2889657 10847 {
5f5c8ee5
GM
10848 int dy = it.current_y - first_row_y;
10849
10850 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
10851 row = row_containing_pos (w, PT, row, NULL, dy);
10852 if (row)
10853 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10854 dy, nrows_scrolled);
10855 else
5f5c8ee5
GM
10856 {
10857 clear_glyph_matrix (w->desired_matrix);
10858 return 0;
10859 }
a2889657 10860 }
5f5c8ee5
GM
10861
10862 /* Scroll the display. Do it before the current matrix is
10863 changed. The problem here is that update has not yet
10864 run, i.e. part of the current matrix is not up to date.
10865 scroll_run_hook will clear the cursor, and use the
10866 current matrix to get the height of the row the cursor is
10867 in. */
10868 run.current_y = first_row_y;
10869 run.desired_y = it.current_y;
10870 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
10871
10872 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 10873 {
5f5c8ee5
GM
10874 update_begin (f);
10875 rif->update_window_begin_hook (w);
64d1e7d3 10876 rif->clear_mouse_face (w);
5f5c8ee5 10877 rif->scroll_run_hook (w, &run);
64d1e7d3 10878 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 10879 update_end (f);
a2889657 10880 }
5f5c8ee5
GM
10881
10882 /* Shift current matrix down by nrows_scrolled lines. */
10883 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10884 rotate_matrix (w->current_matrix,
10885 start_vpos,
10886 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10887 nrows_scrolled);
10888
9c8b8382 10889 /* Disable lines that must be updated. */
5f5c8ee5 10890 for (i = 0; i < it.vpos; ++i)
b48f74cb 10891 (start_row + i)->enabled_p = 0;
9c8b8382 10892
5f5c8ee5 10893 /* Re-compute Y positions. */
045dee35 10894 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10895 max_y = it.last_visible_y;
b48f74cb
GM
10896 for (row = start_row + nrows_scrolled;
10897 row < bottom_row;
10898 ++row)
d2f84654 10899 {
5f5c8ee5 10900 row->y = it.current_y;
75c5350a 10901 row->visible_height = row->height;
5f5c8ee5
GM
10902
10903 if (row->y < min_y)
75c5350a
GM
10904 row->visible_height -= min_y - row->y;
10905 if (row->y + row->height > max_y)
10906 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
10907
10908 it.current_y += row->height;
5f5c8ee5
GM
10909
10910 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10911 last_reused_text_row = row;
10912 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10913 break;
d2f84654 10914 }
9c8b8382
GM
10915
10916 /* Disable lines in the current matrix which are now
10917 below the window. */
bafb434c 10918 for (++row; row < bottom_row; ++row)
9c8b8382 10919 row->enabled_p = 0;
a2889657 10920 }
5f5c8ee5
GM
10921
10922 /* Update window_end_pos etc.; last_reused_text_row is the last
10923 reused row from the current matrix containing text, if any.
10924 The value of last_text_row is the last displayed line
10925 containing text. */
10926 if (last_reused_text_row)
a2889657 10927 {
5f5c8ee5
GM
10928 w->window_end_bytepos
10929 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
10930 w->window_end_pos
10931 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10932 w->window_end_vpos
10933 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10934 w->current_matrix));
a2889657 10935 }
5f5c8ee5
GM
10936 else if (last_text_row)
10937 {
10938 w->window_end_bytepos
10939 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10940 w->window_end_pos
10941 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10942 w->window_end_vpos
10943 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10944 }
10945 else
10946 {
10947 /* This window must be completely empty. */
10948 w->window_end_bytepos = 0;
ac90c44f 10949 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10950 }
10951 w->window_end_valid = Qnil;
a2889657 10952
5f5c8ee5
GM
10953 /* Update hint: don't try scrolling again in update_window. */
10954 w->desired_matrix->no_scrolling_p = 1;
10955
10956#if GLYPH_DEBUG
10957 debug_method_add (w, "try_window_reusing_current_matrix 1");
10958#endif
10959 return 1;
a2889657 10960 }
5f5c8ee5
GM
10961 else if (CHARPOS (new_start) > CHARPOS (start))
10962 {
10963 struct glyph_row *pt_row, *row;
10964 struct glyph_row *first_reusable_row;
10965 struct glyph_row *first_row_to_display;
10966 int dy;
10967 int yb = window_text_bottom_y (w);
10968
5f5c8ee5
GM
10969 /* Find the row starting at new_start, if there is one. Don't
10970 reuse a partially visible line at the end. */
b48f74cb 10971 first_reusable_row = start_row;
5f5c8ee5
GM
10972 while (first_reusable_row->enabled_p
10973 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10974 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10975 < CHARPOS (new_start)))
10976 ++first_reusable_row;
10977
10978 /* Give up if there is no row to reuse. */
10979 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
10980 || !first_reusable_row->enabled_p
10981 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10982 != CHARPOS (new_start)))
5f5c8ee5
GM
10983 return 0;
10984
5f5c8ee5
GM
10985 /* We can reuse fully visible rows beginning with
10986 first_reusable_row to the end of the window. Set
10987 first_row_to_display to the first row that cannot be reused.
10988 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 10989 pt_row = NULL;
ac90c44f
GM
10990 for (first_row_to_display = first_reusable_row;
10991 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10992 ++first_row_to_display)
5f5c8ee5 10993 {
4bde0ebb
GM
10994 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10995 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 10996 pt_row = first_row_to_display;
5f5c8ee5 10997 }
a2889657 10998
5f5c8ee5
GM
10999 /* Start displaying at the start of first_row_to_display. */
11000 xassert (first_row_to_display->y < yb);
11001 init_to_row_start (&it, w, first_row_to_display);
607ec83c 11002
b48f74cb
GM
11003 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11004 - start_vpos);
5f5c8ee5
GM
11005 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11006 - nrows_scrolled);
b48f74cb
GM
11007 it.current_y = (first_row_to_display->y - first_reusable_row->y
11008 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
11009
11010 /* Display lines beginning with first_row_to_display in the
11011 desired matrix. Set last_text_row to the last row displayed
11012 that displays text. */
11013 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11014 if (pt_row == NULL)
11015 w->cursor.vpos = -1;
11016 last_text_row = NULL;
11017 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11018 if (display_line (&it))
11019 last_text_row = it.glyph_row - 1;
11020
11021 /* Give up If point isn't in a row displayed or reused. */
11022 if (w->cursor.vpos < 0)
11023 {
11024 clear_glyph_matrix (w->desired_matrix);
11025 return 0;
11026 }
12adba34 11027
5f5c8ee5
GM
11028 /* If point is in a reused row, adjust y and vpos of the cursor
11029 position. */
11030 if (pt_row)
11031 {
11032 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11033 w->current_matrix);
11034 w->cursor.y -= first_reusable_row->y;
a2889657
JB
11035 }
11036
5f5c8ee5
GM
11037 /* Scroll the display. */
11038 run.current_y = first_reusable_row->y;
045dee35 11039 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11040 run.height = it.last_visible_y - run.current_y;
4da61803
GM
11041 dy = run.current_y - run.desired_y;
11042
5f5c8ee5
GM
11043 if (run.height)
11044 {
11045 struct frame *f = XFRAME (WINDOW_FRAME (w));
11046 update_begin (f);
11047 rif->update_window_begin_hook (w);
64d1e7d3 11048 rif->clear_mouse_face (w);
5f5c8ee5 11049 rif->scroll_run_hook (w, &run);
64d1e7d3 11050 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11051 update_end (f);
11052 }
a2889657 11053
5f5c8ee5
GM
11054 /* Adjust Y positions of reused rows. */
11055 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 11056 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11057 max_y = it.last_visible_y;
b48f74cb 11058 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
11059 {
11060 row->y -= dy;
75c5350a 11061 row->visible_height = row->height;
5f5c8ee5 11062 if (row->y < min_y)
75c5350a
GM
11063 row->visible_height -= min_y - row->y;
11064 if (row->y + row->height > max_y)
11065 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 11066 }
a2889657 11067
5f5c8ee5
GM
11068 /* Scroll the current matrix. */
11069 xassert (nrows_scrolled > 0);
11070 rotate_matrix (w->current_matrix,
11071 start_vpos,
11072 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11073 -nrows_scrolled);
11074
ac90c44f
GM
11075 /* Disable rows not reused. */
11076 for (row -= nrows_scrolled; row < bottom_row; ++row)
11077 row->enabled_p = 0;
11078
5f5c8ee5
GM
11079 /* Adjust window end. A null value of last_text_row means that
11080 the window end is in reused rows which in turn means that
11081 only its vpos can have changed. */
11082 if (last_text_row)
11083 {
11084 w->window_end_bytepos
11085 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11086 w->window_end_pos
11087 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11088 w->window_end_vpos
11089 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11090 }
11091 else
a2889657 11092 {
ac90c44f
GM
11093 w->window_end_vpos
11094 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 11095 }
5f5c8ee5
GM
11096
11097 w->window_end_valid = Qnil;
11098 w->desired_matrix->no_scrolling_p = 1;
11099
11100#if GLYPH_DEBUG
11101 debug_method_add (w, "try_window_reusing_current_matrix 2");
11102#endif
11103 return 1;
a2889657 11104 }
5f5c8ee5
GM
11105
11106 return 0;
11107}
a2889657 11108
a2889657 11109
5f5c8ee5
GM
11110\f
11111/************************************************************************
11112 Window redisplay reusing current matrix when buffer has changed
11113 ************************************************************************/
11114
1ec185cb
GM
11115static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11116static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
11117 int *, int *));
11118static struct glyph_row *
11119find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11120 struct glyph_row *));
11121
11122
11123/* Return the last row in MATRIX displaying text. If row START is
11124 non-null, start searching with that row. IT gives the dimensions
11125 of the display. Value is null if matrix is empty; otherwise it is
11126 a pointer to the row found. */
11127
11128static struct glyph_row *
11129find_last_row_displaying_text (matrix, it, start)
11130 struct glyph_matrix *matrix;
11131 struct it *it;
11132 struct glyph_row *start;
11133{
11134 struct glyph_row *row, *row_found;
11135
11136 /* Set row_found to the last row in IT->w's current matrix
11137 displaying text. The loop looks funny but think of partially
11138 visible lines. */
11139 row_found = NULL;
11140 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11141 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11142 {
11143 xassert (row->enabled_p);
11144 row_found = row;
11145 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11146 break;
11147 ++row;
a2889657 11148 }
5f5c8ee5
GM
11149
11150 return row_found;
11151}
11152
a2889657 11153
5f5c8ee5 11154/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
11155 by changes at the start of current_buffer that occurred since W's
11156 current matrix was built. Value is null if no such row exists.
a2889657 11157
d43fbe9d
GM
11158 BEG_UNCHANGED us the number of characters unchanged at the start of
11159 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11160 first changed character in current_buffer. Characters at positions <
11161 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11162 when the current matrix was built. */
5f5c8ee5
GM
11163
11164static struct glyph_row *
1ec185cb 11165find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
11166 struct window *w;
11167{
9142dd5b 11168 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
11169 struct glyph_row *row;
11170 struct glyph_row *row_found = NULL;
11171 int yb = window_text_bottom_y (w);
11172
11173 /* Find the last row displaying unchanged text. */
11174 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11175 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11176 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 11177 {
5f5c8ee5
GM
11178 if (/* If row ends before first_changed_pos, it is unchanged,
11179 except in some case. */
11180 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11181 /* When row ends in ZV and we write at ZV it is not
11182 unchanged. */
11183 && !row->ends_at_zv_p
11184 /* When first_changed_pos is the end of a continued line,
11185 row is not unchanged because it may be no longer
11186 continued. */
11187 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11188 && row->continued_p))
11189 row_found = row;
11190
11191 /* Stop if last visible row. */
11192 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11193 break;
11194
11195 ++row;
a2889657
JB
11196 }
11197
5f5c8ee5 11198 return row_found;
a2889657 11199}
5f5c8ee5
GM
11200
11201
11202/* Find the first glyph row in the current matrix of W that is not
d43fbe9d
GM
11203 affected by changes at the end of current_buffer since the
11204 time W's current matrix was built.
11205
11206 Return in *DELTA the number of chars by which buffer positions in
11207 unchanged text at the end of current_buffer must be adjusted.
11208
11209 Return in *DELTA_BYTES the corresponding number of bytes.
11210
11211 Value is null if no such row exists, i.e. all rows are affected by
11212 changes. */
5f5c8ee5
GM
11213
11214static struct glyph_row *
1ec185cb 11215find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
11216 struct window *w;
11217 int *delta, *delta_bytes;
a2889657 11218{
5f5c8ee5
GM
11219 struct glyph_row *row;
11220 struct glyph_row *row_found = NULL;
c581d710 11221
5f5c8ee5 11222 *delta = *delta_bytes = 0;
b2a76982 11223
1ec185cb
GM
11224 /* Display must not have been paused, otherwise the current matrix
11225 is not up to date. */
11226 if (NILP (w->window_end_valid))
11227 abort ();
11228
11229 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
11230 end is in the range of changed text. If so, there is no
11231 unchanged row at the end of W's current matrix. */
9142dd5b 11232 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
11233 return NULL;
11234
11235 /* Set row to the last row in W's current matrix displaying text. */
11236 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11237
5f5c8ee5
GM
11238 /* If matrix is entirely empty, no unchanged row exists. */
11239 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11240 {
11241 /* The value of row is the last glyph row in the matrix having a
11242 meaningful buffer position in it. The end position of row
11243 corresponds to window_end_pos. This allows us to translate
11244 buffer positions in the current matrix to current buffer
11245 positions for characters not in changed text. */
11246 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11247 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11248 int last_unchanged_pos, last_unchanged_pos_old;
11249 struct glyph_row *first_text_row
11250 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11251
11252 *delta = Z - Z_old;
11253 *delta_bytes = Z_BYTE - Z_BYTE_old;
11254
11255 /* Set last_unchanged_pos to the buffer position of the last
11256 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
11257 index + 1 of the last character in current_buffer, i.e. by
11258 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
11259 unchanged character, and we have to add BEG to get its buffer
11260 position. */
9142dd5b 11261 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
11262 last_unchanged_pos_old = last_unchanged_pos - *delta;
11263
11264 /* Search backward from ROW for a row displaying a line that
11265 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 11266 for (; row > first_text_row; --row)
5f5c8ee5 11267 {
1ec185cb
GM
11268 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11269 abort ();
5f5c8ee5
GM
11270
11271 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11272 row_found = row;
5f5c8ee5
GM
11273 }
11274 }
11275
1ec185cb
GM
11276 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11277 abort ();
11278
5f5c8ee5 11279 return row_found;
c581d710
RS
11280}
11281
c581d710 11282
5f5c8ee5
GM
11283/* Make sure that glyph rows in the current matrix of window W
11284 reference the same glyph memory as corresponding rows in the
11285 frame's frame matrix. This function is called after scrolling W's
11286 current matrix on a terminal frame in try_window_id and
11287 try_window_reusing_current_matrix. */
11288
11289static void
11290sync_frame_with_window_matrix_rows (w)
11291 struct window *w;
c581d710 11292{
5f5c8ee5
GM
11293 struct frame *f = XFRAME (w->frame);
11294 struct glyph_row *window_row, *window_row_end, *frame_row;
11295
11296 /* Preconditions: W must be a leaf window and full-width. Its frame
11297 must have a frame matrix. */
11298 xassert (NILP (w->hchild) && NILP (w->vchild));
11299 xassert (WINDOW_FULL_WIDTH_P (w));
11300 xassert (!FRAME_WINDOW_P (f));
11301
11302 /* If W is a full-width window, glyph pointers in W's current matrix
11303 have, by definition, to be the same as glyph pointers in the
11304 corresponding frame matrix. */
11305 window_row = w->current_matrix->rows;
11306 window_row_end = window_row + w->current_matrix->nrows;
11307 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11308 while (window_row < window_row_end)
659a218f 11309 {
5f5c8ee5 11310 int area;
f002db93 11311
5f5c8ee5
GM
11312 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11313 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
11314
11315 /* Disable frame rows whose corresponding window rows have
11316 been disabled in try_window_id. */
11317 if (!window_row->enabled_p)
11318 frame_row->enabled_p = 0;
11319
5f5c8ee5 11320 ++window_row, ++frame_row;
659a218f 11321 }
a2889657 11322}
5f5c8ee5
GM
11323
11324
e037b9ec
GM
11325/* Find the glyph row in window W containing CHARPOS. Consider all
11326 rows between START and END (not inclusive). END null means search
11327 all rows to the end of the display area of W. Value is the row
11328 containing CHARPOS or null. */
11329
0bef35bc 11330struct glyph_row *
0df56f4d 11331row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
11332 struct window *w;
11333 int charpos;
11334 struct glyph_row *start, *end;
0df56f4d 11335 int dy;
e037b9ec
GM
11336{
11337 struct glyph_row *row = start;
11338 int last_y;
11339
11340 /* If we happen to start on a header-line, skip that. */
11341 if (row->mode_line_p)
11342 ++row;
11343
11344 if ((end && row >= end) || !row->enabled_p)
11345 return NULL;
11346
0df56f4d 11347 last_y = window_text_bottom_y (w) - dy;
e037b9ec
GM
11348
11349 while ((end == NULL || row < end)
0df56f4d 11350 && MATRIX_ROW_BOTTOM_Y (row) < last_y
e037b9ec 11351 && (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 11352 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
11353 /* The end position of a row equals the start
11354 position of the next row. If CHARPOS is there, we
11355 would rather display it in the next line, except
11356 when this line ends in ZV. */
11357 && !row->ends_at_zv_p
11358 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
e037b9ec
GM
11359 ++row;
11360
11361 /* Give up if CHARPOS not found. */
11362 if ((end && row >= end)
11363 || charpos < MATRIX_ROW_START_CHARPOS (row)
11364 || charpos > MATRIX_ROW_END_CHARPOS (row))
11365 row = NULL;
11366
11367 return row;
11368}
11369
11370
5f5c8ee5
GM
11371/* Try to redisplay window W by reusing its existing display. W's
11372 current matrix must be up to date when this function is called,
11373 i.e. window_end_valid must not be nil.
11374
11375 Value is
11376
11377 1 if display has been updated
11378 0 if otherwise unsuccessful
11379 -1 if redisplay with same window start is known not to succeed
11380
11381 The following steps are performed:
11382
11383 1. Find the last row in the current matrix of W that is not
11384 affected by changes at the start of current_buffer. If no such row
11385 is found, give up.
11386
11387 2. Find the first row in W's current matrix that is not affected by
11388 changes at the end of current_buffer. Maybe there is no such row.
11389
11390 3. Display lines beginning with the row + 1 found in step 1 to the
11391 row found in step 2 or, if step 2 didn't find a row, to the end of
11392 the window.
11393
11394 4. If cursor is not known to appear on the window, give up.
11395
11396 5. If display stopped at the row found in step 2, scroll the
11397 display and current matrix as needed.
11398
11399 6. Maybe display some lines at the end of W, if we must. This can
11400 happen under various circumstances, like a partially visible line
11401 becoming fully visible, or because newly displayed lines are displayed
11402 in smaller font sizes.
11403
11404 7. Update W's window end information. */
11405
12adba34 11406static int
5f5c8ee5 11407try_window_id (w)
12adba34 11408 struct window *w;
12adba34 11409{
5f5c8ee5
GM
11410 struct frame *f = XFRAME (w->frame);
11411 struct glyph_matrix *current_matrix = w->current_matrix;
11412 struct glyph_matrix *desired_matrix = w->desired_matrix;
11413 struct glyph_row *last_unchanged_at_beg_row;
11414 struct glyph_row *first_unchanged_at_end_row;
11415 struct glyph_row *row;
11416 struct glyph_row *bottom_row;
11417 int bottom_vpos;
11418 struct it it;
11419 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11420 struct text_pos start_pos;
11421 struct run run;
11422 int first_unchanged_at_end_vpos = 0;
11423 struct glyph_row *last_text_row, *last_text_row_at_end;
11424 struct text_pos start;
27d16f05 11425 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 11426
69d1f7c9 11427#if GLYPH_DEBUG
76cb5e06
GM
11428 if (inhibit_try_window_id)
11429 return 0;
11430#endif
11431
27d16f05
GM
11432 /* This is handy for debugging. */
11433#if 0
11434#define GIVE_UP(X) \
11435 do { \
11436 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11437 return 0; \
11438 } while (0)
11439#else
62397849 11440#define GIVE_UP(X) return 0
27d16f05
GM
11441#endif
11442
5f5c8ee5
GM
11443 SET_TEXT_POS_FROM_MARKER (start, w->start);
11444
27d16f05
GM
11445 /* Don't use this for mini-windows because these can show
11446 messages and mini-buffers, and we don't handle that here. */
11447 if (MINI_WINDOW_P (w))
11448 GIVE_UP (1);
11449
11450 /* This flag is used to prevent redisplay optimizations. */
11451 if (windows_or_buffers_changed)
11452 GIVE_UP (2);
11453
f5376658 11454 /* Verify that narrowing has not changed. This flag is also set to prevent
27d16f05
GM
11455 redisplay optimizations. It would be nice to further
11456 reduce the number of cases where this prevents try_window_id. */
11457 if (current_buffer->clip_changed)
11458 GIVE_UP (3);
11459
11460 /* Window must either use window-based redisplay or be full width. */
11461 if (!FRAME_WINDOW_P (f)
11462 && (!line_ins_del_ok
11463 || !WINDOW_FULL_WIDTH_P (w)))
11464 GIVE_UP (4);
5f5c8ee5 11465
f5376658 11466 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
11467 if (PT < CHARPOS (start))
11468 GIVE_UP (5);
11469
11470 /* Another way to prevent redisplay optimizations. */
11471 if (XFASTINT (w->last_modified) == 0)
11472 GIVE_UP (6);
11473
f5376658 11474 /* Verify that window is not hscrolled. */
27d16f05
GM
11475 if (XFASTINT (w->hscroll) != 0)
11476 GIVE_UP (7);
11477
f5376658 11478 /* Verify that display wasn't paused. */
27d16f05
GM
11479 if (NILP (w->window_end_valid))
11480 GIVE_UP (8);
11481
11482 /* Can't use this if highlighting a region because a cursor movement
11483 will do more than just set the cursor. */
11484 if (!NILP (Vtransient_mark_mode)
11485 && !NILP (current_buffer->mark_active))
11486 GIVE_UP (9);
11487
11488 /* Likewise if highlighting trailing whitespace. */
11489 if (!NILP (Vshow_trailing_whitespace))
11490 GIVE_UP (11);
11491
11492 /* Likewise if showing a region. */
11493 if (!NILP (w->region_showing))
11494 GIVE_UP (10);
11495
11496 /* Can use this if overlay arrow position and or string have changed. */
11497 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11498 || !EQ (last_arrow_string, Voverlay_arrow_string))
11499 GIVE_UP (12);
11500
11501
5f5c8ee5
GM
11502 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11503 only if buffer has really changed. The reason is that the gap is
11504 initially at Z for freshly visited files. The code below would
11505 set end_unchanged to 0 in that case. */
28ee91c0
GM
11506 if (MODIFF > SAVE_MODIFF
11507 /* This seems to happen sometimes after saving a buffer. */
11508 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 11509 {
9142dd5b
GM
11510 if (GPT - BEG < BEG_UNCHANGED)
11511 BEG_UNCHANGED = GPT - BEG;
11512 if (Z - GPT < END_UNCHANGED)
11513 END_UNCHANGED = Z - GPT;
5f5c8ee5 11514 }
bf9249e3 11515
27d16f05
GM
11516 /* The position of the first and last character that has been changed. */
11517 first_changed_charpos = BEG + BEG_UNCHANGED;
11518 last_changed_charpos = Z - END_UNCHANGED;
11519
5f5c8ee5
GM
11520 /* If window starts after a line end, and the last change is in
11521 front of that newline, then changes don't affect the display.
f2d86d7a
GM
11522 This case happens with stealth-fontification. Note that although
11523 the display is unchanged, glyph positions in the matrix have to
11524 be adjusted, of course. */
5f5c8ee5 11525 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 11526 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 11527 && ((last_changed_charpos < CHARPOS (start)
27d16f05 11528 && CHARPOS (start) == BEGV)
e60f4527 11529 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
11530 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11531 {
11532 int Z_old, delta, Z_BYTE_old, delta_bytes;
11533 struct glyph_row *r0;
11534
11535 /* Compute how many chars/bytes have been added to or removed
11536 from the buffer. */
11537 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11538 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11539 delta = Z - Z_old;
11540 delta_bytes = Z_BYTE - Z_BYTE_old;
11541
11542 /* Give up if PT is not in the window. Note that it already has
11543 been checked at the start of try_window_id that PT is not in
11544 front of the window start. */
11545 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11546 GIVE_UP (13);
11547
11548 /* If window start is unchanged, we can reuse the whole matrix
11549 as is, after adjusting glyph positions. No need to compute
11550 the window end again, since its offset from Z hasn't changed. */
11551 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11552 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11553 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11554 {
11555 /* Adjust positions in the glyph matrix. */
11556 if (delta || delta_bytes)
11557 {
11558 struct glyph_row *r1
11559 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11560 increment_matrix_positions (w->current_matrix,
11561 MATRIX_ROW_VPOS (r0, current_matrix),
11562 MATRIX_ROW_VPOS (r1, current_matrix),
11563 delta, delta_bytes);
11564 }
e5959a9a 11565
27d16f05 11566 /* Set the cursor. */
0df56f4d 11567 row = row_containing_pos (w, PT, r0, NULL, 0);
27d16f05
GM
11568 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11569 return 1;
11570 }
5f5c8ee5
GM
11571 }
11572
27d16f05 11573 /* Handle the case that changes are all below what is displayed in
33ea6c4d 11574 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
11575 be taken if ZV is visible in the window, and text has been added
11576 there that is visible in the window. */
11577 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
11578 /* ZV is not visible in the window, or there are no
11579 changes at ZV, actually. */
11580 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11581 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 11582 {
27d16f05 11583 struct glyph_row *r0;
ef121659 11584
27d16f05
GM
11585 /* Give up if PT is not in the window. Note that it already has
11586 been checked at the start of try_window_id that PT is not in
11587 front of the window start. */
11588 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11589 GIVE_UP (14);
11590
11591 /* If window start is unchanged, we can reuse the whole matrix
11592 as is, without changing glyph positions since no text has
11593 been added/removed in front of the window end. */
11594 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11595 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11596 {
11597 /* We have to compute the window end anew since text
11598 can have been added/removed after it. */
11599 w->window_end_pos
11600 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11601 w->window_end_bytepos
11602 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11603
11604 /* Set the cursor. */
0df56f4d 11605 row = row_containing_pos (w, PT, r0, NULL, 0);
27d16f05
GM
11606 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11607 return 2;
11608 }
5f5c8ee5
GM
11609 }
11610
2b9c25e0
GM
11611 /* Give up if window start is in the changed area.
11612
11613 The condition used to read
11614
11615 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11616
11617 but why that was tested escapes me at the moment. */
11618 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
11619 && CHARPOS (start) <= last_changed_charpos)
11620 GIVE_UP (15);
11621
f5376658
RS
11622 /* Check that window start agrees with the start of the first glyph
11623 row in its current matrix. Check this after we know the window
11624 start is not in changed text, otherwise positions would not be
11625 comparable. */
27d16f05 11626 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 11627 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 11628 GIVE_UP (16);
5f5c8ee5 11629
23e8bd86
GM
11630 /* Give up if the window ends in strings. Overlay strings
11631 at the end are difficult to handle, so don't try. */
11632 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11633 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11634 GIVE_UP (20);
11635
5f5c8ee5
GM
11636 /* Compute the position at which we have to start displaying new
11637 lines. Some of the lines at the top of the window might be
11638 reusable because they are not displaying changed text. Find the
11639 last row in W's current matrix not affected by changes at the
11640 start of current_buffer. Value is null if changes start in the
11641 first line of window. */
1ec185cb 11642 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
11643 if (last_unchanged_at_beg_row)
11644 {
67f1cf4c
GM
11645 /* Avoid starting to display in the moddle of a character, a TAB
11646 for instance. This is easier than to set up the iterator
11647 exactly, and it's not a frequent case, so the additional
11648 effort wouldn't really pay off. */
e74fb0f7
GM
11649 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11650 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
11651 && last_unchanged_at_beg_row > w->current_matrix->rows)
11652 --last_unchanged_at_beg_row;
11653
11654 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 11655 GIVE_UP (17);
47d57b22
GM
11656
11657 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11658 GIVE_UP (18);
5f5c8ee5
GM
11659 start_pos = it.current.pos;
11660
11661 /* Start displaying new lines in the desired matrix at the same
11662 vpos we would use in the current matrix, i.e. below
11663 last_unchanged_at_beg_row. */
11664 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11665 current_matrix);
11666 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11667 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11668
11669 xassert (it.hpos == 0 && it.current_x == 0);
11670 }
11671 else
11672 {
11673 /* There are no reusable lines at the start of the window.
11674 Start displaying in the first line. */
11675 start_display (&it, w, start);
11676 start_pos = it.current.pos;
11677 }
11678
5f5c8ee5
GM
11679 /* Find the first row that is not affected by changes at the end of
11680 the buffer. Value will be null if there is no unchanged row, in
11681 which case we must redisplay to the end of the window. delta
11682 will be set to the value by which buffer positions beginning with
11683 first_unchanged_at_end_row have to be adjusted due to text
11684 changes. */
11685 first_unchanged_at_end_row
1ec185cb 11686 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
11687 IF_DEBUG (debug_delta = delta);
11688 IF_DEBUG (debug_delta_bytes = delta_bytes);
11689
11690 /* Set stop_pos to the buffer position up to which we will have to
11691 display new lines. If first_unchanged_at_end_row != NULL, this
11692 is the buffer position of the start of the line displayed in that
11693 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11694 that we don't stop at a buffer position. */
11695 stop_pos = 0;
11696 if (first_unchanged_at_end_row)
11697 {
11698 xassert (last_unchanged_at_beg_row == NULL
11699 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11700
11701 /* If this is a continuation line, move forward to the next one
11702 that isn't. Changes in lines above affect this line.
11703 Caution: this may move first_unchanged_at_end_row to a row
11704 not displaying text. */
11705 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11706 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11707 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11708 < it.last_visible_y))
11709 ++first_unchanged_at_end_row;
11710
11711 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11712 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11713 >= it.last_visible_y))
11714 first_unchanged_at_end_row = NULL;
11715 else
11716 {
11717 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11718 + delta);
11719 first_unchanged_at_end_vpos
11720 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 11721 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
11722 }
11723 }
11724 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 11725 GIVE_UP (19);
5f5c8ee5
GM
11726
11727
11728#if GLYPH_DEBUG
11729
11730 /* Either there is no unchanged row at the end, or the one we have
11731 now displays text. This is a necessary condition for the window
11732 end pos calculation at the end of this function. */
11733 xassert (first_unchanged_at_end_row == NULL
11734 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11735
11736 debug_last_unchanged_at_beg_vpos
11737 = (last_unchanged_at_beg_row
11738 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11739 : -1);
11740 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11741
11742#endif /* GLYPH_DEBUG != 0 */
11743
33ea6c4d 11744
5f5c8ee5
GM
11745 /* Display new lines. Set last_text_row to the last new line
11746 displayed which has text on it, i.e. might end up as being the
11747 line where the window_end_vpos is. */
11748 w->cursor.vpos = -1;
11749 last_text_row = NULL;
11750 overlay_arrow_seen = 0;
11751 while (it.current_y < it.last_visible_y
11752 && !fonts_changed_p
11753 && (first_unchanged_at_end_row == NULL
11754 || IT_CHARPOS (it) < stop_pos))
11755 {
11756 if (display_line (&it))
11757 last_text_row = it.glyph_row - 1;
11758 }
11759
11760 if (fonts_changed_p)
11761 return -1;
11762
11763
11764 /* Compute differences in buffer positions, y-positions etc. for
11765 lines reused at the bottom of the window. Compute what we can
11766 scroll. */
ca42b2e8
GM
11767 if (first_unchanged_at_end_row
11768 /* No lines reused because we displayed everything up to the
11769 bottom of the window. */
11770 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
11771 {
11772 dvpos = (it.vpos
11773 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11774 current_matrix));
11775 dy = it.current_y - first_unchanged_at_end_row->y;
11776 run.current_y = first_unchanged_at_end_row->y;
11777 run.desired_y = run.current_y + dy;
11778 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11779 }
11780 else
ca42b2e8
GM
11781 {
11782 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11783 first_unchanged_at_end_row = NULL;
11784 }
5f5c8ee5
GM
11785 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11786
8f8ba186 11787
5f5c8ee5
GM
11788 /* Find the cursor if not already found. We have to decide whether
11789 PT will appear on this window (it sometimes doesn't, but this is
11790 not a very frequent case.) This decision has to be made before
11791 the current matrix is altered. A value of cursor.vpos < 0 means
11792 that PT is either in one of the lines beginning at
11793 first_unchanged_at_end_row or below the window. Don't care for
11794 lines that might be displayed later at the window end; as
11795 mentioned, this is not a frequent case. */
11796 if (w->cursor.vpos < 0)
11797 {
5f5c8ee5
GM
11798 /* Cursor in unchanged rows at the top? */
11799 if (PT < CHARPOS (start_pos)
11800 && last_unchanged_at_beg_row)
11801 {
e037b9ec
GM
11802 row = row_containing_pos (w, PT,
11803 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 11804 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
11805 if (row)
11806 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
11807 }
11808
11809 /* Start from first_unchanged_at_end_row looking for PT. */
11810 else if (first_unchanged_at_end_row)
11811 {
e037b9ec 11812 row = row_containing_pos (w, PT - delta,
0df56f4d 11813 first_unchanged_at_end_row, NULL, 0);
e037b9ec 11814 if (row)
468155d7
GM
11815 set_cursor_from_row (w, row, w->current_matrix, delta,
11816 delta_bytes, dy, dvpos);
5f5c8ee5
GM
11817 }
11818
11819 /* Give up if cursor was not found. */
11820 if (w->cursor.vpos < 0)
11821 {
11822 clear_glyph_matrix (w->desired_matrix);
11823 return -1;
11824 }
11825 }
11826
11827 /* Don't let the cursor end in the scroll margins. */
11828 {
11829 int this_scroll_margin, cursor_height;
11830
11831 this_scroll_margin = max (0, scroll_margin);
11832 this_scroll_margin = min (this_scroll_margin,
11833 XFASTINT (w->height) / 4);
11834 this_scroll_margin *= CANON_Y_UNIT (it.f);
11835 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11836
11837 if ((w->cursor.y < this_scroll_margin
11838 && CHARPOS (start) > BEGV)
11839 /* Don't take scroll margin into account at the bottom because
11840 old redisplay didn't do it either. */
11841 || w->cursor.y + cursor_height > it.last_visible_y)
11842 {
11843 w->cursor.vpos = -1;
11844 clear_glyph_matrix (w->desired_matrix);
11845 return -1;
11846 }
11847 }
11848
11849 /* Scroll the display. Do it before changing the current matrix so
11850 that xterm.c doesn't get confused about where the cursor glyph is
11851 found. */
fa77249f 11852 if (dy && run.height)
5f5c8ee5
GM
11853 {
11854 update_begin (f);
11855
11856 if (FRAME_WINDOW_P (f))
11857 {
11858 rif->update_window_begin_hook (w);
64d1e7d3 11859 rif->clear_mouse_face (w);
5f5c8ee5 11860 rif->scroll_run_hook (w, &run);
64d1e7d3 11861 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11862 }
11863 else
11864 {
11865 /* Terminal frame. In this case, dvpos gives the number of
11866 lines to scroll by; dvpos < 0 means scroll up. */
11867 int first_unchanged_at_end_vpos
11868 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11869 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
522ed7fb
GM
11870 int end = (XFASTINT (w->top)
11871 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11872 + window_internal_height (w));
5f5c8ee5
GM
11873
11874 /* Perform the operation on the screen. */
11875 if (dvpos > 0)
11876 {
11877 /* Scroll last_unchanged_at_beg_row to the end of the
11878 window down dvpos lines. */
11879 set_terminal_window (end);
11880
11881 /* On dumb terminals delete dvpos lines at the end
11882 before inserting dvpos empty lines. */
11883 if (!scroll_region_ok)
11884 ins_del_lines (end - dvpos, -dvpos);
11885
11886 /* Insert dvpos empty lines in front of
11887 last_unchanged_at_beg_row. */
11888 ins_del_lines (from, dvpos);
11889 }
11890 else if (dvpos < 0)
11891 {
11892 /* Scroll up last_unchanged_at_beg_vpos to the end of
11893 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11894 set_terminal_window (end);
11895
11896 /* Delete dvpos lines in front of
11897 last_unchanged_at_beg_vpos. ins_del_lines will set
11898 the cursor to the given vpos and emit |dvpos| delete
11899 line sequences. */
11900 ins_del_lines (from + dvpos, dvpos);
11901
11902 /* On a dumb terminal insert dvpos empty lines at the
11903 end. */
11904 if (!scroll_region_ok)
11905 ins_del_lines (end + dvpos, -dvpos);
11906 }
11907
11908 set_terminal_window (0);
11909 }
11910
11911 update_end (f);
11912 }
11913
ca42b2e8
GM
11914 /* Shift reused rows of the current matrix to the right position.
11915 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11916 text. */
11917 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11918 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
11919 if (dvpos < 0)
11920 {
11921 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11922 bottom_vpos, dvpos);
11923 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11924 bottom_vpos, 0);
11925 }
11926 else if (dvpos > 0)
11927 {
11928 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11929 bottom_vpos, dvpos);
11930 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11931 first_unchanged_at_end_vpos + dvpos, 0);
11932 }
11933
11934 /* For frame-based redisplay, make sure that current frame and window
11935 matrix are in sync with respect to glyph memory. */
11936 if (!FRAME_WINDOW_P (f))
11937 sync_frame_with_window_matrix_rows (w);
11938
11939 /* Adjust buffer positions in reused rows. */
11940 if (delta)
f2d86d7a
GM
11941 increment_matrix_positions (current_matrix,
11942 first_unchanged_at_end_vpos + dvpos,
11943 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
11944
11945 /* Adjust Y positions. */
11946 if (dy)
11947 shift_glyph_matrix (w, current_matrix,
11948 first_unchanged_at_end_vpos + dvpos,
11949 bottom_vpos, dy);
11950
11951 if (first_unchanged_at_end_row)
11952 first_unchanged_at_end_row += dvpos;
11953
11954 /* If scrolling up, there may be some lines to display at the end of
11955 the window. */
11956 last_text_row_at_end = NULL;
11957 if (dy < 0)
11958 {
9c6ba6d1
GM
11959 /* Scrolling up can leave for example a partially visible line
11960 at the end of the window to be redisplayed. */
5f5c8ee5
GM
11961 /* Set last_row to the glyph row in the current matrix where the
11962 window end line is found. It has been moved up or down in
11963 the matrix by dvpos. */
11964 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11965 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11966
11967 /* If last_row is the window end line, it should display text. */
11968 xassert (last_row->displays_text_p);
11969
11970 /* If window end line was partially visible before, begin
11971 displaying at that line. Otherwise begin displaying with the
11972 line following it. */
11973 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11974 {
11975 init_to_row_start (&it, w, last_row);
11976 it.vpos = last_vpos;
11977 it.current_y = last_row->y;
11978 }
11979 else
11980 {
11981 init_to_row_end (&it, w, last_row);
11982 it.vpos = 1 + last_vpos;
11983 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11984 ++last_row;
11985 }
12adba34 11986
23e8bd86
GM
11987 /* We may start in a continuation line. If so, we have to
11988 get the right continuation_lines_width and current_x. */
11989 it.continuation_lines_width = last_row->continuation_lines_width;
11990 it.hpos = it.current_x = 0;
11991
11992 /* Display the rest of the lines at the window end. */
11993 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11994 while (it.current_y < it.last_visible_y
11995 && !fonts_changed_p)
5f5c8ee5 11996 {
23e8bd86
GM
11997 /* Is it always sure that the display agrees with lines in
11998 the current matrix? I don't think so, so we mark rows
11999 displayed invalid in the current matrix by setting their
12000 enabled_p flag to zero. */
12001 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12002 if (display_line (&it))
12003 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
12004 }
12005 }
12adba34 12006
5f5c8ee5
GM
12007 /* Update window_end_pos and window_end_vpos. */
12008 if (first_unchanged_at_end_row
12009 && first_unchanged_at_end_row->y < it.last_visible_y
12010 && !last_text_row_at_end)
12011 {
12012 /* Window end line if one of the preserved rows from the current
12013 matrix. Set row to the last row displaying text in current
12014 matrix starting at first_unchanged_at_end_row, after
12015 scrolling. */
12016 xassert (first_unchanged_at_end_row->displays_text_p);
12017 row = find_last_row_displaying_text (w->current_matrix, &it,
12018 first_unchanged_at_end_row);
12019 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12020
ac90c44f 12021 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 12022 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
12023 w->window_end_vpos
12024 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 12025 xassert (w->window_end_bytepos >= 0);
0416532f 12026 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
12027 }
12028 else if (last_text_row_at_end)
12029 {
ac90c44f
GM
12030 w->window_end_pos
12031 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
12032 w->window_end_bytepos
12033 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
12034 w->window_end_vpos
12035 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 12036 xassert (w->window_end_bytepos >= 0);
0416532f 12037 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
12038 }
12039 else if (last_text_row)
12040 {
12041 /* We have displayed either to the end of the window or at the
12042 end of the window, i.e. the last row with text is to be found
12043 in the desired matrix. */
ac90c44f
GM
12044 w->window_end_pos
12045 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
12046 w->window_end_bytepos
12047 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12048 w->window_end_vpos
12049 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 12050 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
12051 }
12052 else if (first_unchanged_at_end_row == NULL
12053 && last_text_row == NULL
12054 && last_text_row_at_end == NULL)
12055 {
12056 /* Displayed to end of window, but no line containing text was
12057 displayed. Lines were deleted at the end of the window. */
0416532f
GM
12058 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12059 int vpos = XFASTINT (w->window_end_vpos);
12060 struct glyph_row *current_row = current_matrix->rows + vpos;
12061 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12062
12063 for (row = NULL;
12064 row == NULL && vpos >= first_vpos;
12065 --vpos, --current_row, --desired_row)
12066 {
12067 if (desired_row->enabled_p)
12068 {
12069 if (desired_row->displays_text_p)
12070 row = desired_row;
12071 }
12072 else if (current_row->displays_text_p)
12073 row = current_row;
12074 }
12adba34 12075
0416532f 12076 xassert (row != NULL);
d88a79d4 12077 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
12078 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12079 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 12080 xassert (w->window_end_bytepos >= 0);
0416532f 12081 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
12082 }
12083 else
12084 abort ();
ac90c44f 12085
8cdb267e
GM
12086#if 0 /* This leads to problems, for instance when the cursor is
12087 at ZV, and the cursor line displays no text. */
ac90c44f
GM
12088 /* Disable rows below what's displayed in the window. This makes
12089 debugging easier. */
12090 enable_glyph_matrix_rows (current_matrix,
12091 XFASTINT (w->window_end_vpos) + 1,
12092 bottom_vpos, 0);
8cdb267e 12093#endif
23e8bd86 12094
5f5c8ee5
GM
12095 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12096 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 12097
5f5c8ee5
GM
12098 /* Record that display has not been completed. */
12099 w->window_end_valid = Qnil;
12100 w->desired_matrix->no_scrolling_p = 1;
ef121659 12101 return 3;
27d16f05
GM
12102
12103#undef GIVE_UP
12adba34 12104}
0f9c0ff0 12105
a2889657 12106
5f5c8ee5
GM
12107\f
12108/***********************************************************************
12109 More debugging support
12110 ***********************************************************************/
a2889657 12111
5f5c8ee5 12112#if GLYPH_DEBUG
a2889657 12113
eaaa65b0 12114void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 12115void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 12116void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 12117
31b24551 12118
9ab436b9 12119/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 12120
9ab436b9
GM
12121 GLYPHS 0 means don't show glyph contents.
12122 GLYPHS 1 means show glyphs in short form
12123 GLYPHS > 1 means show glyphs in long form. */
12124
12125void
12126dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 12127 struct glyph_matrix *matrix;
9ab436b9 12128 int glyphs;
5f5c8ee5 12129{
efc63ef0 12130 int i;
5f5c8ee5 12131 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 12132 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 12133}
31b24551 12134
68a37fa8 12135
e187cf71
GM
12136/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12137 the glyph row and area where the glyph comes from. */
12138
12139void
12140dump_glyph (row, glyph, area)
12141 struct glyph_row *row;
12142 struct glyph *glyph;
12143 int area;
12144{
12145 if (glyph->type == CHAR_GLYPH)
12146 {
12147 fprintf (stderr,
12148 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12149 glyph - row->glyphs[TEXT_AREA],
12150 'C',
12151 glyph->charpos,
12152 (BUFFERP (glyph->object)
12153 ? 'B'
12154 : (STRINGP (glyph->object)
12155 ? 'S'
12156 : '-')),
12157 glyph->pixel_width,
12158 glyph->u.ch,
12159 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12160 ? glyph->u.ch
12161 : '.'),
12162 glyph->face_id,
12163 glyph->left_box_line_p,
12164 glyph->right_box_line_p);
12165 }
12166 else if (glyph->type == STRETCH_GLYPH)
12167 {
12168 fprintf (stderr,
12169 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12170 glyph - row->glyphs[TEXT_AREA],
12171 'S',
12172 glyph->charpos,
12173 (BUFFERP (glyph->object)
12174 ? 'B'
12175 : (STRINGP (glyph->object)
12176 ? 'S'
12177 : '-')),
12178 glyph->pixel_width,
12179 0,
12180 '.',
12181 glyph->face_id,
12182 glyph->left_box_line_p,
12183 glyph->right_box_line_p);
12184 }
12185 else if (glyph->type == IMAGE_GLYPH)
12186 {
12187 fprintf (stderr,
12188 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12189 glyph - row->glyphs[TEXT_AREA],
12190 'I',
12191 glyph->charpos,
12192 (BUFFERP (glyph->object)
12193 ? 'B'
12194 : (STRINGP (glyph->object)
12195 ? 'S'
12196 : '-')),
12197 glyph->pixel_width,
12198 glyph->u.img_id,
12199 '.',
12200 glyph->face_id,
12201 glyph->left_box_line_p,
12202 glyph->right_box_line_p);
12203 }
12204}
12205
12206
5f5c8ee5 12207/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
12208 GLYPHS 0 means don't show glyph contents.
12209 GLYPHS 1 means show glyphs in short form
12210 GLYPHS > 1 means show glyphs in long form. */
a2889657 12211
5f5c8ee5 12212void
eaaa65b0
GM
12213dump_glyph_row (row, vpos, glyphs)
12214 struct glyph_row *row;
9ab436b9 12215 int vpos, glyphs;
5f5c8ee5 12216{
9ab436b9
GM
12217 if (glyphs != 1)
12218 {
b94c0d9c 12219 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 12220 fprintf (stderr, "=======================================================================\n");
5f5c8ee5 12221
0ad38729 12222 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 12223%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
eaaa65b0 12224 vpos,
9ab436b9
GM
12225 MATRIX_ROW_START_CHARPOS (row),
12226 MATRIX_ROW_END_CHARPOS (row),
12227 row->used[TEXT_AREA],
12228 row->contains_overlapping_glyphs_p,
12229 row->enabled_p,
9ab436b9
GM
12230 row->truncated_on_left_p,
12231 row->truncated_on_right_p,
12232 row->overlay_arrow_p,
12233 row->continued_p,
12234 MATRIX_ROW_CONTINUATION_LINE_P (row),
12235 row->displays_text_p,
12236 row->ends_at_zv_p,
12237 row->fill_line_p,
12238 row->ends_in_middle_of_char_p,
12239 row->starts_in_middle_of_char_p,
b94c0d9c 12240 row->mouse_face_p,
9ab436b9
GM
12241 row->x,
12242 row->y,
12243 row->pixel_width,
12244 row->height,
12245 row->visible_height,
12246 row->ascent,
12247 row->phys_ascent);
33ea6c4d
GM
12248 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12249 row->end.overlay_string_index,
12250 row->continuation_lines_width);
9ab436b9
GM
12251 fprintf (stderr, "%9d %5d\n",
12252 CHARPOS (row->start.string_pos),
12253 CHARPOS (row->end.string_pos));
12254 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12255 row->end.dpvec_index);
12256 }
5f5c8ee5 12257
9ab436b9 12258 if (glyphs > 1)
bd66d1ba 12259 {
e187cf71
GM
12260 int area;
12261
12262 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12263 {
091f8878
GM
12264 struct glyph *glyph = row->glyphs[area];
12265 struct glyph *glyph_end = glyph + row->used[area];
5f5c8ee5 12266
e187cf71 12267 /* Glyph for a line end in text. */
091f8878 12268 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 12269 ++glyph_end;
5f5c8ee5 12270
e187cf71
GM
12271 if (glyph < glyph_end)
12272 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 12273
e187cf71
GM
12274 for (; glyph < glyph_end; ++glyph)
12275 dump_glyph (row, glyph, area);
f7b4b63a 12276 }
f4faa47c 12277 }
9ab436b9
GM
12278 else if (glyphs == 1)
12279 {
e187cf71 12280 int area;
9ab436b9 12281
e187cf71 12282 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 12283 {
e187cf71
GM
12284 char *s = (char *) alloca (row->used[area] + 1);
12285 int i;
12286
12287 for (i = 0; i < row->used[area]; ++i)
12288 {
12289 struct glyph *glyph = row->glyphs[area] + i;
12290 if (glyph->type == CHAR_GLYPH
12291 && glyph->u.ch < 0x80
12292 && glyph->u.ch >= ' ')
12293 s[i] = glyph->u.ch;
12294 else
12295 s[i] = '.';
12296 }
9ab436b9 12297
e187cf71
GM
12298 s[i] = '\0';
12299 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12300 }
9ab436b9 12301 }
5f5c8ee5 12302}
f4faa47c 12303
a2889657 12304
5f5c8ee5
GM
12305DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12306 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 12307 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
12308Shows contents of glyph row structures. With non-nil
12309parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
12310glyphs in short form, otherwise show glyphs in long form. */)
12311 (glyphs)
9ab436b9 12312 Lisp_Object glyphs;
5f5c8ee5
GM
12313{
12314 struct window *w = XWINDOW (selected_window);
12315 struct buffer *buffer = XBUFFER (w->buffer);
12316
12317 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12318 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12319 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12320 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12321 fprintf (stderr, "=============================================\n");
9ab436b9
GM
12322 dump_glyph_matrix (w->current_matrix,
12323 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
12324 return Qnil;
12325}
1c2250c2 12326
1fca3fae 12327
e9abc296 12328DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 12329 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
12330GLYPH 0 means don't dump glyphs.
12331GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12332GLYPH > 1 or omitted means dump glyphs in long form. */)
12333 (row, glyphs)
e9abc296 12334 Lisp_Object row, glyphs;
5f5c8ee5 12335{
eaaa65b0
GM
12336 struct glyph_matrix *matrix;
12337 int vpos;
12338
b7826503 12339 CHECK_NUMBER (row);
eaaa65b0
GM
12340 matrix = XWINDOW (selected_window)->current_matrix;
12341 vpos = XINT (row);
12342 if (vpos >= 0 && vpos < matrix->nrows)
12343 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12344 vpos,
12345 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12346 return Qnil;
12347}
1fca3fae 12348
67481ae5 12349
b94c0d9c 12350DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 12351 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
12352GLYPH 0 means don't dump glyphs.
12353GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12354GLYPH > 1 or omitted means dump glyphs in long form. */)
12355 (row, glyphs)
b94c0d9c 12356 Lisp_Object row, glyphs;
5f5c8ee5 12357{
886bd6f2 12358 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
12359 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12360 int vpos;
12361
b7826503 12362 CHECK_NUMBER (row);
eaaa65b0
GM
12363 vpos = XINT (row);
12364 if (vpos >= 0 && vpos < m->nrows)
12365 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12366 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12367 return Qnil;
12368}
ca26e1c8 12369
0f9c0ff0 12370
62397849 12371DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
12372 doc: /* Toggle tracing of redisplay.
12373With ARG, turn tracing on if and only if ARG is positive. */)
12374 (arg)
62397849 12375 Lisp_Object arg;
5f5c8ee5 12376{
62397849
GM
12377 if (NILP (arg))
12378 trace_redisplay_p = !trace_redisplay_p;
12379 else
12380 {
12381 arg = Fprefix_numeric_value (arg);
12382 trace_redisplay_p = XINT (arg) > 0;
12383 }
12384
5f5c8ee5
GM
12385 return Qnil;
12386}
bf9249e3
GM
12387
12388
f4a374a1 12389DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
7ee72033
MB
12390 doc: /* Like `format', but print result to stderr. */)
12391 (nargs, args)
f4a374a1
GM
12392 int nargs;
12393 Lisp_Object *args;
bf9249e3 12394{
f4a374a1
GM
12395 Lisp_Object s = Fformat (nargs, args);
12396 fprintf (stderr, "%s", XSTRING (s)->data);
bf9249e3
GM
12397 return Qnil;
12398}
5f5c8ee5
GM
12399
12400#endif /* GLYPH_DEBUG */
ca26e1c8 12401
ca26e1c8 12402
5f5c8ee5
GM
12403\f
12404/***********************************************************************
12405 Building Desired Matrix Rows
12406 ***********************************************************************/
a2889657 12407
5f5c8ee5
GM
12408/* Return a temporary glyph row holding the glyphs of an overlay
12409 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 12410
5f5c8ee5
GM
12411static struct glyph_row *
12412get_overlay_arrow_glyph_row (w)
12413 struct window *w;
12414{
12415 struct frame *f = XFRAME (WINDOW_FRAME (w));
12416 struct buffer *buffer = XBUFFER (w->buffer);
12417 struct buffer *old = current_buffer;
12418 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12419 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12420 unsigned char *arrow_end = arrow_string + arrow_len;
12421 unsigned char *p;
12422 struct it it;
12423 int multibyte_p;
12424 int n_glyphs_before;
12425
12426 set_buffer_temp (buffer);
12427 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12428 it.glyph_row->used[TEXT_AREA] = 0;
12429 SET_TEXT_POS (it.position, 0, 0);
12430
12431 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12432 p = arrow_string;
12433 while (p < arrow_end)
12434 {
12435 Lisp_Object face, ilisp;
12436
12437 /* Get the next character. */
12438 if (multibyte_p)
4fdb80f2 12439 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
12440 else
12441 it.c = *p, it.len = 1;
12442 p += it.len;
12443
12444 /* Get its face. */
ac90c44f 12445 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
12446 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12447 it.face_id = compute_char_face (f, it.c, face);
12448
12449 /* Compute its width, get its glyphs. */
12450 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 12451 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
12452 PRODUCE_GLYPHS (&it);
12453
12454 /* If this character doesn't fit any more in the line, we have
12455 to remove some glyphs. */
12456 if (it.current_x > it.last_visible_x)
12457 {
12458 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12459 break;
12460 }
12461 }
12462
12463 set_buffer_temp (old);
12464 return it.glyph_row;
12465}
ca26e1c8 12466
b0a0fbda 12467
5f5c8ee5
GM
12468/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12469 glyphs are only inserted for terminal frames since we can't really
12470 win with truncation glyphs when partially visible glyphs are
12471 involved. Which glyphs to insert is determined by
12472 produce_special_glyphs. */
67481ae5 12473
5f5c8ee5
GM
12474static void
12475insert_left_trunc_glyphs (it)
12476 struct it *it;
12477{
12478 struct it truncate_it;
12479 struct glyph *from, *end, *to, *toend;
12480
12481 xassert (!FRAME_WINDOW_P (it->f));
12482
12483 /* Get the truncation glyphs. */
12484 truncate_it = *it;
5f5c8ee5
GM
12485 truncate_it.current_x = 0;
12486 truncate_it.face_id = DEFAULT_FACE_ID;
12487 truncate_it.glyph_row = &scratch_glyph_row;
12488 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12489 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 12490 truncate_it.object = make_number (0);
5f5c8ee5
GM
12491 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12492
12493 /* Overwrite glyphs from IT with truncation glyphs. */
12494 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12495 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12496 to = it->glyph_row->glyphs[TEXT_AREA];
12497 toend = to + it->glyph_row->used[TEXT_AREA];
12498
12499 while (from < end)
12500 *to++ = *from++;
12501
37be86f2
KH
12502 /* There may be padding glyphs left over. Overwrite them too. */
12503 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12504 {
12505 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12506 while (from < end)
12507 *to++ = *from++;
12508 }
5f5c8ee5 12509
37be86f2
KH
12510 if (to > toend)
12511 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 12512}
e0bfbde6 12513
e0bfbde6 12514
5f5c8ee5 12515/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 12516
5f5c8ee5
GM
12517 Most of the time, ascent and height of a display line will be equal
12518 to the max_ascent and max_height values of the display iterator
12519 structure. This is not the case if
67481ae5 12520
5f5c8ee5
GM
12521 1. We hit ZV without displaying anything. In this case, max_ascent
12522 and max_height will be zero.
1c9241f5 12523
5f5c8ee5
GM
12524 2. We have some glyphs that don't contribute to the line height.
12525 (The glyph row flag contributes_to_line_height_p is for future
12526 pixmap extensions).
f6fd109b 12527
5f5c8ee5
GM
12528 The first case is easily covered by using default values because in
12529 these cases, the line height does not really matter, except that it
12530 must not be zero. */
67481ae5 12531
5f5c8ee5
GM
12532static void
12533compute_line_metrics (it)
12534 struct it *it;
12535{
12536 struct glyph_row *row = it->glyph_row;
12537 int area, i;
1c2250c2 12538
5f5c8ee5
GM
12539 if (FRAME_WINDOW_P (it->f))
12540 {
75c5350a 12541 int i, min_y, max_y;
1c2250c2 12542
5f5c8ee5
GM
12543 /* The line may consist of one space only, that was added to
12544 place the cursor on it. If so, the row's height hasn't been
12545 computed yet. */
12546 if (row->height == 0)
12547 {
12548 if (it->max_ascent + it->max_descent == 0)
312246d1 12549 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
12550 row->ascent = it->max_ascent;
12551 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12552 row->phys_ascent = it->max_phys_ascent;
12553 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12554 }
12555
12556 /* Compute the width of this line. */
12557 row->pixel_width = row->x;
12558 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12559 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12560
12561 xassert (row->pixel_width >= 0);
12562 xassert (row->ascent >= 0 && row->height > 0);
12563
312246d1
GM
12564 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12565 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12566
12567 /* If first line's physical ascent is larger than its logical
12568 ascent, use the physical ascent, and make the row taller.
12569 This makes accented characters fully visible. */
b28cb6ed 12570 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
12571 && row->phys_ascent > row->ascent)
12572 {
12573 row->height += row->phys_ascent - row->ascent;
12574 row->ascent = row->phys_ascent;
12575 }
12576
5f5c8ee5
GM
12577 /* Compute how much of the line is visible. */
12578 row->visible_height = row->height;
12579
75c5350a
GM
12580 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12581 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12582
12583 if (row->y < min_y)
12584 row->visible_height -= min_y - row->y;
12585 if (row->y + row->height > max_y)
12586 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
12587 }
12588 else
12589 {
12590 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
12591 if (row->continued_p)
12592 row->pixel_width -= it->continuation_pixel_width;
12593 else if (row->truncated_on_right_p)
12594 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
12595 row->ascent = row->phys_ascent = 0;
12596 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 12597 }
67481ae5 12598
5f5c8ee5
GM
12599 /* Compute a hash code for this row. */
12600 row->hash = 0;
12601 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12602 for (i = 0; i < row->used[area]; ++i)
12603 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12604 + row->glyphs[area][i].u.val
43d120d8
KH
12605 + row->glyphs[area][i].face_id
12606 + row->glyphs[area][i].padding_p
5f5c8ee5 12607 + (row->glyphs[area][i].type << 2));
a2889657 12608
5f5c8ee5 12609 it->max_ascent = it->max_descent = 0;
312246d1 12610 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 12611}
12adba34 12612
ca26e1c8 12613
5f5c8ee5
GM
12614/* Append one space to the glyph row of iterator IT if doing a
12615 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12616 space have the default face, otherwise let it have the same face as
80c6cb1f 12617 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
12618
12619 This function is called to make sure that there is always one glyph
12620 at the end of a glyph row that the cursor can be set on under
12621 window-systems. (If there weren't such a glyph we would not know
12622 how wide and tall a box cursor should be displayed).
12623
12624 At the same time this space let's a nicely handle clearing to the
12625 end of the line if the row ends in italic text. */
ca26e1c8 12626
80c6cb1f 12627static int
5f5c8ee5
GM
12628append_space (it, default_face_p)
12629 struct it *it;
12630 int default_face_p;
12631{
12632 if (FRAME_WINDOW_P (it->f))
12633 {
12634 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 12635
5f5c8ee5
GM
12636 if (it->glyph_row->glyphs[TEXT_AREA] + n
12637 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 12638 {
cafafe0b
GM
12639 /* Save some values that must not be changed.
12640 Must save IT->c and IT->len because otherwise
12641 ITERATOR_AT_END_P wouldn't work anymore after
12642 append_space has been called. */
478d746b 12643 enum display_element_type saved_what = it->what;
cafafe0b
GM
12644 int saved_c = it->c, saved_len = it->len;
12645 int saved_x = it->current_x;
5f5c8ee5 12646 int saved_face_id = it->face_id;
cafafe0b 12647 struct text_pos saved_pos;
5f5c8ee5 12648 Lisp_Object saved_object;
980806b6 12649 struct face *face;
5f5c8ee5
GM
12650
12651 saved_object = it->object;
12652 saved_pos = it->position;
12653
12654 it->what = IT_CHARACTER;
12655 bzero (&it->position, sizeof it->position);
6fc556fd 12656 it->object = make_number (0);
5f5c8ee5
GM
12657 it->c = ' ';
12658 it->len = 1;
5f5c8ee5
GM
12659
12660 if (default_face_p)
12661 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
12662 else if (it->face_before_selective_p)
12663 it->face_id = it->saved_face_id;
980806b6
KH
12664 face = FACE_FROM_ID (it->f, it->face_id);
12665 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 12666
5f5c8ee5
GM
12667 PRODUCE_GLYPHS (it);
12668
12669 it->current_x = saved_x;
12670 it->object = saved_object;
12671 it->position = saved_pos;
12672 it->what = saved_what;
12673 it->face_id = saved_face_id;
cafafe0b
GM
12674 it->len = saved_len;
12675 it->c = saved_c;
80c6cb1f 12676 return 1;
5f5c8ee5
GM
12677 }
12678 }
80c6cb1f
GM
12679
12680 return 0;
5f5c8ee5 12681}
12adba34 12682
1842fc1a 12683
5f5c8ee5
GM
12684/* Extend the face of the last glyph in the text area of IT->glyph_row
12685 to the end of the display line. Called from display_line.
12686 If the glyph row is empty, add a space glyph to it so that we
12687 know the face to draw. Set the glyph row flag fill_line_p. */
12688
12689static void
12690extend_face_to_end_of_line (it)
12691 struct it *it;
12692{
12693 struct face *face;
12694 struct frame *f = it->f;
1842fc1a 12695
5f5c8ee5
GM
12696 /* If line is already filled, do nothing. */
12697 if (it->current_x >= it->last_visible_x)
12698 return;
12699
12700 /* Face extension extends the background and box of IT->face_id
12701 to the end of the line. If the background equals the background
4aad61f8
GM
12702 of the frame, we don't have to do anything. */
12703 if (it->face_before_selective_p)
12704 face = FACE_FROM_ID (it->f, it->saved_face_id);
12705 else
12706 face = FACE_FROM_ID (f, it->face_id);
12707
5f5c8ee5
GM
12708 if (FRAME_WINDOW_P (f)
12709 && face->box == FACE_NO_BOX
12710 && face->background == FRAME_BACKGROUND_PIXEL (f)
12711 && !face->stipple)
12712 return;
1842fc1a 12713
5f5c8ee5
GM
12714 /* Set the glyph row flag indicating that the face of the last glyph
12715 in the text area has to be drawn to the end of the text area. */
12716 it->glyph_row->fill_line_p = 1;
545e04f6 12717
980806b6
KH
12718 /* If current character of IT is not ASCII, make sure we have the
12719 ASCII face. This will be automatically undone the next time
12720 get_next_display_element returns a multibyte character. Note
12721 that the character will always be single byte in unibyte text. */
12722 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 12723 {
980806b6 12724 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 12725 }
545e04f6 12726
5f5c8ee5
GM
12727 if (FRAME_WINDOW_P (f))
12728 {
12729 /* If the row is empty, add a space with the current face of IT,
12730 so that we know which face to draw. */
12731 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 12732 {
5f5c8ee5 12733 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 12734 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 12735 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 12736 }
5f5c8ee5
GM
12737 }
12738 else
12739 {
12740 /* Save some values that must not be changed. */
12741 int saved_x = it->current_x;
12742 struct text_pos saved_pos;
12743 Lisp_Object saved_object;
478d746b 12744 enum display_element_type saved_what = it->what;
4aad61f8 12745 int saved_face_id = it->face_id;
5f5c8ee5
GM
12746
12747 saved_object = it->object;
12748 saved_pos = it->position;
12749
12750 it->what = IT_CHARACTER;
12751 bzero (&it->position, sizeof it->position);
6fc556fd 12752 it->object = make_number (0);
5f5c8ee5
GM
12753 it->c = ' ';
12754 it->len = 1;
4aad61f8 12755 it->face_id = face->id;
5f5c8ee5
GM
12756
12757 PRODUCE_GLYPHS (it);
12758
12759 while (it->current_x <= it->last_visible_x)
12760 PRODUCE_GLYPHS (it);
12761
12762 /* Don't count these blanks really. It would let us insert a left
12763 truncation glyph below and make us set the cursor on them, maybe. */
12764 it->current_x = saved_x;
12765 it->object = saved_object;
12766 it->position = saved_pos;
12767 it->what = saved_what;
4aad61f8 12768 it->face_id = saved_face_id;
5f5c8ee5
GM
12769 }
12770}
12adba34 12771
545e04f6 12772
5f5c8ee5
GM
12773/* Value is non-zero if text starting at CHARPOS in current_buffer is
12774 trailing whitespace. */
1c9241f5 12775
5f5c8ee5
GM
12776static int
12777trailing_whitespace_p (charpos)
12778 int charpos;
12779{
12780 int bytepos = CHAR_TO_BYTE (charpos);
12781 int c = 0;
7bbe686f 12782
5f5c8ee5
GM
12783 while (bytepos < ZV_BYTE
12784 && (c = FETCH_CHAR (bytepos),
12785 c == ' ' || c == '\t'))
12786 ++bytepos;
0d09d1e6 12787
8f897821
GM
12788 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12789 {
12790 if (bytepos != PT_BYTE)
12791 return 1;
12792 }
12793 return 0;
5f5c8ee5 12794}
31b24551 12795
545e04f6 12796
5f5c8ee5 12797/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 12798
5f5c8ee5
GM
12799void
12800highlight_trailing_whitespace (f, row)
12801 struct frame *f;
12802 struct glyph_row *row;
12803{
12804 int used = row->used[TEXT_AREA];
12805
12806 if (used)
12807 {
12808 struct glyph *start = row->glyphs[TEXT_AREA];
12809 struct glyph *glyph = start + used - 1;
12810
ade0bee1
GM
12811 /* Skip over glyphs inserted to display the cursor at the
12812 end of a line, for extending the face of the last glyph
12813 to the end of the line on terminals, and for truncation
12814 and continuation glyphs. */
65008712
GM
12815 while (glyph >= start
12816 && glyph->type == CHAR_GLYPH
65008712 12817 && INTEGERP (glyph->object))
5f5c8ee5
GM
12818 --glyph;
12819
12820 /* If last glyph is a space or stretch, and it's trailing
12821 whitespace, set the face of all trailing whitespace glyphs in
12822 IT->glyph_row to `trailing-whitespace'. */
12823 if (glyph >= start
12824 && BUFFERP (glyph->object)
12825 && (glyph->type == STRETCH_GLYPH
12826 || (glyph->type == CHAR_GLYPH
43d120d8 12827 && glyph->u.ch == ' '))
5f5c8ee5 12828 && trailing_whitespace_p (glyph->charpos))
545e04f6 12829 {
980806b6 12830 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
12831
12832 while (glyph >= start
12833 && BUFFERP (glyph->object)
12834 && (glyph->type == STRETCH_GLYPH
12835 || (glyph->type == CHAR_GLYPH
43d120d8
KH
12836 && glyph->u.ch == ' ')))
12837 (glyph--)->face_id = face_id;
545e04f6 12838 }
a2889657 12839 }
5f5c8ee5 12840}
a2889657 12841
5fcbb24d 12842
cafafe0b 12843/* Value is non-zero if glyph row ROW in window W should be
0fd37545 12844 used to hold the cursor. */
cafafe0b
GM
12845
12846static int
12847cursor_row_p (w, row)
12848 struct window *w;
12849 struct glyph_row *row;
12850{
9a038881
GM
12851 int cursor_row_p = 1;
12852
cafafe0b
GM
12853 if (PT == MATRIX_ROW_END_CHARPOS (row))
12854 {
9a038881
GM
12855 /* If the row ends with a newline from a string, we don't want
12856 the cursor there (if the row is continued it doesn't end in a
12857 newline). */
12858 if (CHARPOS (row->end.string_pos) >= 0
12859 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12860 cursor_row_p = row->continued_p;
12861
12862 /* If the row ends at ZV, display the cursor at the end of that
12863 row instead of at the start of the row below. */
12864 else if (row->ends_at_zv_p)
12865 cursor_row_p = 1;
12866 else
12867 cursor_row_p = 0;
cafafe0b
GM
12868 }
12869
9a038881 12870 return cursor_row_p;
cafafe0b
GM
12871}
12872
12873
5f5c8ee5
GM
12874/* Construct the glyph row IT->glyph_row in the desired matrix of
12875 IT->w from text at the current position of IT. See dispextern.h
12876 for an overview of struct it. Value is non-zero if
12877 IT->glyph_row displays text, as opposed to a line displaying ZV
12878 only. */
12879
12880static int
12881display_line (it)
12882 struct it *it;
12883{
12884 struct glyph_row *row = it->glyph_row;
12885
12886 /* We always start displaying at hpos zero even if hscrolled. */
12887 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 12888
5f5c8ee5
GM
12889 /* We must not display in a row that's not a text row. */
12890 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12891 < it->w->desired_matrix->nrows);
12adba34 12892
5f5c8ee5
GM
12893 /* Is IT->w showing the region? */
12894 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 12895
5f5c8ee5
GM
12896 /* Clear the result glyph row and enable it. */
12897 prepare_desired_row (row);
12adba34 12898
5f5c8ee5
GM
12899 row->y = it->current_y;
12900 row->start = it->current;
12901 row->continuation_lines_width = it->continuation_lines_width;
12902 row->displays_text_p = 1;
91004049
GM
12903 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12904 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
12905
12906 /* Arrange the overlays nicely for our purposes. Usually, we call
12907 display_line on only one line at a time, in which case this
12908 can't really hurt too much, or we call it on lines which appear
12909 one after another in the buffer, in which case all calls to
12910 recenter_overlay_lists but the first will be pretty cheap. */
12911 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12912
5f5c8ee5
GM
12913 /* Move over display elements that are not visible because we are
12914 hscrolled. This may stop at an x-position < IT->first_visible_x
12915 if the first glyph is partially visible or if we hit a line end. */
12916 if (it->current_x < it->first_visible_x)
12917 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12918 MOVE_TO_POS | MOVE_TO_X);
12919
12920 /* Get the initial row height. This is either the height of the
12921 text hscrolled, if there is any, or zero. */
12922 row->ascent = it->max_ascent;
12923 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12924 row->phys_ascent = it->max_phys_ascent;
12925 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12926
12927 /* Loop generating characters. The loop is left with IT on the next
12928 character to display. */
12929 while (1)
12930 {
12931 int n_glyphs_before, hpos_before, x_before;
12932 int x, i, nglyphs;
ae26e27d 12933 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 12934
5f5c8ee5
GM
12935 /* Retrieve the next thing to display. Value is zero if end of
12936 buffer reached. */
12937 if (!get_next_display_element (it))
12938 {
12939 /* Maybe add a space at the end of this line that is used to
1b335865
GM
12940 display the cursor there under X. Set the charpos of the
12941 first glyph of blank lines not corresponding to any text
12942 to -1. */
12943 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12944 || row->used[TEXT_AREA] == 0)
a2889657 12945 {
5f5c8ee5
GM
12946 row->glyphs[TEXT_AREA]->charpos = -1;
12947 row->displays_text_p = 0;
12948
2ad551af 12949 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
12950 && (!MINI_WINDOW_P (it->w)
12951 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 12952 row->indicate_empty_line_p = 1;
a2889657 12953 }
5f5c8ee5
GM
12954
12955 it->continuation_lines_width = 0;
12956 row->ends_at_zv_p = 1;
12957 break;
a2889657 12958 }
a2889657 12959
5f5c8ee5
GM
12960 /* Now, get the metrics of what we want to display. This also
12961 generates glyphs in `row' (which is IT->glyph_row). */
12962 n_glyphs_before = row->used[TEXT_AREA];
12963 x = it->current_x;
e6819faf
GM
12964
12965 /* Remember the line height so far in case the next element doesn't
12966 fit on the line. */
12967 if (!it->truncate_lines_p)
12968 {
12969 ascent = it->max_ascent;
12970 descent = it->max_descent;
12971 phys_ascent = it->max_phys_ascent;
12972 phys_descent = it->max_phys_descent;
12973 }
12974
5f5c8ee5 12975 PRODUCE_GLYPHS (it);
a2889657 12976
5f5c8ee5
GM
12977 /* If this display element was in marginal areas, continue with
12978 the next one. */
12979 if (it->area != TEXT_AREA)
a2889657 12980 {
5f5c8ee5
GM
12981 row->ascent = max (row->ascent, it->max_ascent);
12982 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12983 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12984 row->phys_height = max (row->phys_height,
12985 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 12986 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12987 continue;
12988 }
5936754e 12989
5f5c8ee5
GM
12990 /* Does the display element fit on the line? If we truncate
12991 lines, we should draw past the right edge of the window. If
12992 we don't truncate, we want to stop so that we can display the
12993 continuation glyph before the right margin. If lines are
12994 continued, there are two possible strategies for characters
12995 resulting in more than 1 glyph (e.g. tabs): Display as many
12996 glyphs as possible in this line and leave the rest for the
12997 continuation line, or display the whole element in the next
12998 line. Original redisplay did the former, so we do it also. */
12999 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13000 hpos_before = it->hpos;
13001 x_before = x;
4dcd74e6 13002
4b41cebb 13003 if (/* Not a newline. */
4dcd74e6 13004 nglyphs > 0
202c1b5b 13005 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
13006 && it->current_x < it->last_visible_x)
13007 {
37be86f2 13008 it->hpos += nglyphs;
5f5c8ee5
GM
13009 row->ascent = max (row->ascent, it->max_ascent);
13010 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13011 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13012 row->phys_height = max (row->phys_height,
13013 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13014 if (it->current_x - it->pixel_width < it->first_visible_x)
13015 row->x = x - it->first_visible_x;
13016 }
13017 else
13018 {
13019 int new_x;
13020 struct glyph *glyph;
13021
13022 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 13023 {
5f5c8ee5
GM
13024 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13025 new_x = x + glyph->pixel_width;
13026
13027 if (/* Lines are continued. */
13028 !it->truncate_lines_p
13029 && (/* Glyph doesn't fit on the line. */
13030 new_x > it->last_visible_x
13031 /* Or it fits exactly on a window system frame. */
13032 || (new_x == it->last_visible_x
13033 && FRAME_WINDOW_P (it->f))))
a2889657 13034 {
5f5c8ee5
GM
13035 /* End of a continued line. */
13036
13037 if (it->hpos == 0
13038 || (new_x == it->last_visible_x
13039 && FRAME_WINDOW_P (it->f)))
13040 {
e6819faf
GM
13041 /* Current glyph is the only one on the line or
13042 fits exactly on the line. We must continue
13043 the line because we can't draw the cursor
13044 after the glyph. */
5f5c8ee5
GM
13045 row->continued_p = 1;
13046 it->current_x = new_x;
13047 it->continuation_lines_width += new_x;
13048 ++it->hpos;
13049 if (i == nglyphs - 1)
cafafe0b 13050 set_iterator_to_next (it, 1);
5f5c8ee5 13051 }
3b3c4bf0
GM
13052 else if (CHAR_GLYPH_PADDING_P (*glyph)
13053 && !FRAME_WINDOW_P (it->f))
13054 {
13055 /* A padding glyph that doesn't fit on this line.
13056 This means the whole character doesn't fit
13057 on the line. */
13058 row->used[TEXT_AREA] = n_glyphs_before;
13059
13060 /* Fill the rest of the row with continuation
13061 glyphs like in 20.x. */
13062 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13063 < row->glyphs[1 + TEXT_AREA])
13064 produce_special_glyphs (it, IT_CONTINUATION);
13065
13066 row->continued_p = 1;
13067 it->current_x = x_before;
13068 it->continuation_lines_width += x_before;
13069
13070 /* Restore the height to what it was before the
13071 element not fitting on the line. */
13072 it->max_ascent = ascent;
13073 it->max_descent = descent;
13074 it->max_phys_ascent = phys_ascent;
13075 it->max_phys_descent = phys_descent;
13076 }
0df56f4d
GM
13077 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13078 {
13079 /* A TAB that extends past the right edge of the
13080 window. This produces a single glyph on
13081 window system frames. We leave the glyph in
13082 this row and let it fill the row, but don't
13083 consume the TAB. */
13084 it->continuation_lines_width += it->last_visible_x;
13085 row->ends_in_middle_of_char_p = 1;
13086 row->continued_p = 1;
13087 glyph->pixel_width = it->last_visible_x - x;
13088 it->starts_in_middle_of_char_p = 1;
13089 }
5f5c8ee5 13090 else
5936754e 13091 {
0df56f4d
GM
13092 /* Something other than a TAB that draws past
13093 the right edge of the window. Restore
13094 positions to values before the element. */
5f5c8ee5
GM
13095 row->used[TEXT_AREA] = n_glyphs_before + i;
13096
13097 /* Display continuation glyphs. */
13098 if (!FRAME_WINDOW_P (it->f))
13099 produce_special_glyphs (it, IT_CONTINUATION);
13100 row->continued_p = 1;
653c329b 13101
0df56f4d 13102 it->continuation_lines_width += x;
5f5c8ee5 13103
91004049
GM
13104 if (nglyphs > 1 && i > 0)
13105 {
13106 row->ends_in_middle_of_char_p = 1;
13107 it->starts_in_middle_of_char_p = 1;
13108 }
e6819faf
GM
13109
13110 /* Restore the height to what it was before the
13111 element not fitting on the line. */
13112 it->max_ascent = ascent;
13113 it->max_descent = descent;
13114 it->max_phys_ascent = phys_ascent;
13115 it->max_phys_descent = phys_descent;
5936754e 13116 }
e6819faf 13117
5f5c8ee5
GM
13118 break;
13119 }
13120 else if (new_x > it->first_visible_x)
13121 {
13122 /* Increment number of glyphs actually displayed. */
13123 ++it->hpos;
13124
13125 if (x < it->first_visible_x)
13126 /* Glyph is partially visible, i.e. row starts at
13127 negative X position. */
13128 row->x = x - it->first_visible_x;
13129 }
13130 else
13131 {
13132 /* Glyph is completely off the left margin of the
13133 window. This should not happen because of the
13134 move_it_in_display_line at the start of
13135 this function. */
13136 abort ();
a2889657 13137 }
a2889657 13138 }
5f5c8ee5
GM
13139
13140 row->ascent = max (row->ascent, it->max_ascent);
13141 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13142 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13143 row->phys_height = max (row->phys_height,
13144 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13145
13146 /* End of this display line if row is continued. */
13147 if (row->continued_p)
13148 break;
a2889657 13149 }
a2889657 13150
5f5c8ee5
GM
13151 /* Is this a line end? If yes, we're also done, after making
13152 sure that a non-default face is extended up to the right
13153 margin of the window. */
13154 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 13155 {
5f5c8ee5
GM
13156 int used_before = row->used[TEXT_AREA];
13157
e74fb0f7
GM
13158 row->ends_in_newline_from_string_p = STRINGP (it->object);
13159
5f5c8ee5
GM
13160 /* Add a space at the end of the line that is used to
13161 display the cursor there. */
13162 append_space (it, 0);
13163
13164 /* Extend the face to the end of the line. */
13165 extend_face_to_end_of_line (it);
13166
13167 /* Make sure we have the position. */
13168 if (used_before == 0)
13169 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13170
13171 /* Consume the line end. This skips over invisible lines. */
cafafe0b 13172 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13173 it->continuation_lines_width = 0;
13174 break;
1c9241f5 13175 }
a2889657 13176
5f5c8ee5
GM
13177 /* Proceed with next display element. Note that this skips
13178 over lines invisible because of selective display. */
cafafe0b 13179 set_iterator_to_next (it, 1);
b1d1124b 13180
5f5c8ee5
GM
13181 /* If we truncate lines, we are done when the last displayed
13182 glyphs reach past the right margin of the window. */
13183 if (it->truncate_lines_p
13184 && (FRAME_WINDOW_P (it->f)
13185 ? (it->current_x >= it->last_visible_x)
13186 : (it->current_x > it->last_visible_x)))
75d13c64 13187 {
5f5c8ee5
GM
13188 /* Maybe add truncation glyphs. */
13189 if (!FRAME_WINDOW_P (it->f))
13190 {
a98b5ed9
GM
13191 int i, n;
13192
13193 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13194 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13195 break;
13196
13197 for (n = row->used[TEXT_AREA]; i < n; ++i)
13198 {
13199 row->used[TEXT_AREA] = i;
13200 produce_special_glyphs (it, IT_TRUNCATION);
13201 }
5f5c8ee5
GM
13202 }
13203
13204 row->truncated_on_right_p = 1;
13205 it->continuation_lines_width = 0;
312246d1 13206 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
13207 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13208 it->hpos = hpos_before;
13209 it->current_x = x_before;
13210 break;
75d13c64 13211 }
a2889657 13212 }
a2889657 13213
5f5c8ee5
GM
13214 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13215 at the left window margin. */
13216 if (it->first_visible_x
13217 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13218 {
13219 if (!FRAME_WINDOW_P (it->f))
13220 insert_left_trunc_glyphs (it);
13221 row->truncated_on_left_p = 1;
13222 }
a2889657 13223
5f5c8ee5
GM
13224 /* If the start of this line is the overlay arrow-position, then
13225 mark this glyph row as the one containing the overlay arrow.
13226 This is clearly a mess with variable size fonts. It would be
13227 better to let it be displayed like cursors under X. */
e24c997d 13228 if (MARKERP (Voverlay_arrow_position)
a2889657 13229 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
13230 && (MATRIX_ROW_START_CHARPOS (row)
13231 == marker_position (Voverlay_arrow_position))
e24c997d 13232 && STRINGP (Voverlay_arrow_string)
a2889657
JB
13233 && ! overlay_arrow_seen)
13234 {
b46952ae 13235 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 13236 if (!FRAME_WINDOW_P (it->f))
c4628384 13237 {
5f5c8ee5
GM
13238 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13239 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13240 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13241 struct glyph *p = row->glyphs[TEXT_AREA];
13242 struct glyph *p2, *end;
13243
13244 /* Copy the arrow glyphs. */
13245 while (glyph < arrow_end)
13246 *p++ = *glyph++;
13247
13248 /* Throw away padding glyphs. */
13249 p2 = p;
13250 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13251 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13252 ++p2;
13253 if (p2 > p)
212e4f87 13254 {
5f5c8ee5
GM
13255 while (p2 < end)
13256 *p++ = *p2++;
13257 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 13258 }
c4628384 13259 }
5f5c8ee5 13260
a2889657 13261 overlay_arrow_seen = 1;
5f5c8ee5 13262 row->overlay_arrow_p = 1;
a2889657
JB
13263 }
13264
5f5c8ee5
GM
13265 /* Compute pixel dimensions of this line. */
13266 compute_line_metrics (it);
13267
13268 /* Remember the position at which this line ends. */
13269 row->end = it->current;
13270
173cbca8 13271 /* Maybe set the cursor. */
5f5c8ee5
GM
13272 if (it->w->cursor.vpos < 0
13273 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
13274 && PT <= MATRIX_ROW_END_CHARPOS (row)
13275 && cursor_row_p (it->w, row))
13276 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13277
13278 /* Highlight trailing whitespace. */
8f897821 13279 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
13280 highlight_trailing_whitespace (it->f, it->glyph_row);
13281
13282 /* Prepare for the next line. This line starts horizontally at (X
13283 HPOS) = (0 0). Vertical positions are incremented. As a
13284 convenience for the caller, IT->glyph_row is set to the next
13285 row to be used. */
13286 it->current_x = it->hpos = 0;
13287 it->current_y += row->height;
13288 ++it->vpos;
13289 ++it->glyph_row;
13290 return row->displays_text_p;
a2889657 13291}
5f5c8ee5
GM
13292
13293
a2889657 13294\f
5f5c8ee5
GM
13295/***********************************************************************
13296 Menu Bar
13297 ***********************************************************************/
13298
13299/* Redisplay the menu bar in the frame for window W.
13300
13301 The menu bar of X frames that don't have X toolkit support is
13302 displayed in a special window W->frame->menu_bar_window.
13303
13304 The menu bar of terminal frames is treated specially as far as
13305 glyph matrices are concerned. Menu bar lines are not part of
13306 windows, so the update is done directly on the frame matrix rows
13307 for the menu bar. */
7ce2c095
RS
13308
13309static void
13310display_menu_bar (w)
13311 struct window *w;
13312{
5f5c8ee5
GM
13313 struct frame *f = XFRAME (WINDOW_FRAME (w));
13314 struct it it;
13315 Lisp_Object items;
8351baf2 13316 int i;
7ce2c095 13317
5f5c8ee5 13318 /* Don't do all this for graphical frames. */
dc937613 13319#ifdef HAVE_NTGUI
d129c4c2
KH
13320 if (!NILP (Vwindow_system))
13321 return;
dc937613 13322#endif
dc937613 13323#ifdef USE_X_TOOLKIT
d3413a53 13324 if (FRAME_X_P (f))
7ce2c095 13325 return;
5f5c8ee5 13326#endif
1a578e9b
AC
13327#ifdef macintosh
13328 if (FRAME_MAC_P (f))
13329 return;
13330#endif
5f5c8ee5
GM
13331
13332#ifdef USE_X_TOOLKIT
13333 xassert (!FRAME_WINDOW_P (f));
52377a47 13334 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
13335 it.first_visible_x = 0;
13336 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13337#else /* not USE_X_TOOLKIT */
13338 if (FRAME_WINDOW_P (f))
13339 {
13340 /* Menu bar lines are displayed in the desired matrix of the
13341 dummy window menu_bar_window. */
13342 struct window *menu_w;
13343 xassert (WINDOWP (f->menu_bar_window));
13344 menu_w = XWINDOW (f->menu_bar_window);
13345 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 13346 MENU_FACE_ID);
5f5c8ee5
GM
13347 it.first_visible_x = 0;
13348 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13349 }
13350 else
13351 {
13352 /* This is a TTY frame, i.e. character hpos/vpos are used as
13353 pixel x/y. */
13354 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 13355 MENU_FACE_ID);
5f5c8ee5
GM
13356 it.first_visible_x = 0;
13357 it.last_visible_x = FRAME_WIDTH (f);
13358 }
13359#endif /* not USE_X_TOOLKIT */
13360
1862a24e
MB
13361 if (! mode_line_inverse_video)
13362 /* Force the menu-bar to be displayed in the default face. */
13363 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13364
5f5c8ee5
GM
13365 /* Clear all rows of the menu bar. */
13366 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13367 {
13368 struct glyph_row *row = it.glyph_row + i;
13369 clear_glyph_row (row);
13370 row->enabled_p = 1;
13371 row->full_width_p = 1;
13372 }
7ce2c095 13373
5f5c8ee5
GM
13374 /* Display all items of the menu bar. */
13375 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 13376 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 13377 {
5f5c8ee5
GM
13378 Lisp_Object string;
13379
13380 /* Stop at nil string. */
a61b7058 13381 string = AREF (items, i + 1);
8351baf2
RS
13382 if (NILP (string))
13383 break;
2d66ad19 13384
5f5c8ee5 13385 /* Remember where item was displayed. */
a61b7058 13386 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 13387
5f5c8ee5
GM
13388 /* Display the item, pad with one space. */
13389 if (it.current_x < it.last_visible_x)
13390 display_string (NULL, string, Qnil, 0, 0, &it,
13391 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
13392 }
13393
2d66ad19 13394 /* Fill out the line with spaces. */
5f5c8ee5
GM
13395 if (it.current_x < it.last_visible_x)
13396 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 13397
5f5c8ee5
GM
13398 /* Compute the total height of the lines. */
13399 compute_line_metrics (&it);
7ce2c095 13400}
5f5c8ee5
GM
13401
13402
7ce2c095 13403\f
5f5c8ee5
GM
13404/***********************************************************************
13405 Mode Line
13406 ***********************************************************************/
13407
715e84c9
GM
13408/* Redisplay mode lines in the window tree whose root is WINDOW. If
13409 FORCE is non-zero, redisplay mode lines unconditionally.
13410 Otherwise, redisplay only mode lines that are garbaged. Value is
13411 the number of windows whose mode lines were redisplayed. */
a2889657 13412
715e84c9
GM
13413static int
13414redisplay_mode_lines (window, force)
13415 Lisp_Object window;
13416 int force;
13417{
13418 int nwindows = 0;
13419
13420 while (!NILP (window))
13421 {
13422 struct window *w = XWINDOW (window);
13423
13424 if (WINDOWP (w->hchild))
13425 nwindows += redisplay_mode_lines (w->hchild, force);
13426 else if (WINDOWP (w->vchild))
13427 nwindows += redisplay_mode_lines (w->vchild, force);
13428 else if (force
13429 || FRAME_GARBAGED_P (XFRAME (w->frame))
13430 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13431 {
715e84c9
GM
13432 struct text_pos lpoint;
13433 struct buffer *old = current_buffer;
13434
13435 /* Set the window's buffer for the mode line display. */
13436 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13437 set_buffer_internal_1 (XBUFFER (w->buffer));
13438
13439 /* Point refers normally to the selected window. For any
13440 other window, set up appropriate value. */
13441 if (!EQ (window, selected_window))
13442 {
13443 struct text_pos pt;
13444
13445 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13446 if (CHARPOS (pt) < BEGV)
13447 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13448 else if (CHARPOS (pt) > (ZV - 1))
13449 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13450 else
13451 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13452 }
13453
715e84c9
GM
13454 /* Display mode lines. */
13455 clear_glyph_matrix (w->desired_matrix);
13456 if (display_mode_lines (w))
13457 {
13458 ++nwindows;
13459 w->must_be_updated_p = 1;
13460 }
13461
13462 /* Restore old settings. */
715e84c9
GM
13463 set_buffer_internal_1 (old);
13464 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13465 }
13466
13467 window = w->next;
13468 }
13469
13470 return nwindows;
13471}
13472
13473
13474/* Display the mode and/or top line of window W. Value is the number
13475 of mode lines displayed. */
13476
13477static int
5f5c8ee5 13478display_mode_lines (w)
a2889657
JB
13479 struct window *w;
13480{
edd1e654 13481 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 13482 int n = 0;
edd1e654
GM
13483
13484 old_selected_frame = selected_frame;
13485 selected_frame = w->frame;
13486 old_selected_window = selected_window;
13487 XSETWINDOW (selected_window, w);
715e84c9 13488
5f5c8ee5 13489 /* These will be set while the mode line specs are processed. */
aa6d10fa 13490 line_number_displayed = 0;
155ef550 13491 w->column_number_displayed = Qnil;
aa6d10fa 13492
5f5c8ee5 13493 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 13494 {
c96e83bf 13495 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 13496
96d2320f 13497 /* Select mode line face based on the real selected window. */
c96e83bf 13498 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
13499 current_buffer->mode_line_format);
13500 ++n;
13501 }
5f5c8ee5 13502
045dee35 13503 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
13504 {
13505 display_mode_line (w, HEADER_LINE_FACE_ID,
13506 current_buffer->header_line_format);
13507 ++n;
13508 }
13509
edd1e654
GM
13510 selected_frame = old_selected_frame;
13511 selected_window = old_selected_window;
715e84c9 13512 return n;
5f5c8ee5 13513}
03b294dc 13514
03b294dc 13515
5f5c8ee5 13516/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 13517 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
13518 FORMAT is the mode line format to display. Value is the pixel
13519 height of the mode line displayed. */
03b294dc 13520
04612a64 13521static int
5f5c8ee5
GM
13522display_mode_line (w, face_id, format)
13523 struct window *w;
13524 enum face_id face_id;
13525 Lisp_Object format;
13526{
13527 struct it it;
13528 struct face *face;
03b294dc 13529
5f5c8ee5
GM
13530 init_iterator (&it, w, -1, -1, NULL, face_id);
13531 prepare_desired_row (it.glyph_row);
13532
1862a24e
MB
13533 if (! mode_line_inverse_video)
13534 /* Force the mode-line to be displayed in the default face. */
13535 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13536
5f5c8ee5
GM
13537 /* Temporarily make frame's keyboard the current kboard so that
13538 kboard-local variables in the mode_line_format will get the right
13539 values. */
13540 push_frame_kboard (it.f);
0fcf414f 13541 display_mode_element (&it, 0, 0, 0, format, Qnil);
5f5c8ee5 13542 pop_frame_kboard ();
a2889657 13543
5f5c8ee5
GM
13544 /* Fill up with spaces. */
13545 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13546
13547 compute_line_metrics (&it);
13548 it.glyph_row->full_width_p = 1;
13549 it.glyph_row->mode_line_p = 1;
5f5c8ee5
GM
13550 it.glyph_row->continued_p = 0;
13551 it.glyph_row->truncated_on_left_p = 0;
13552 it.glyph_row->truncated_on_right_p = 0;
13553
13554 /* Make a 3D mode-line have a shadow at its right end. */
13555 face = FACE_FROM_ID (it.f, face_id);
13556 extend_face_to_end_of_line (&it);
13557 if (face->box != FACE_NO_BOX)
d7eb09a0 13558 {
5f5c8ee5
GM
13559 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13560 + it.glyph_row->used[TEXT_AREA] - 1);
13561 last->right_box_line_p = 1;
d7eb09a0 13562 }
04612a64
GM
13563
13564 return it.glyph_row->height;
a2889657
JB
13565}
13566
0fcf414f
RS
13567/* Alist that caches the results of :propertize.
13568 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13569Lisp_Object mode_line_proptrans_alist;
a2889657 13570
5f5c8ee5
GM
13571/* Contribute ELT to the mode line for window IT->w. How it
13572 translates into text depends on its data type.
a2889657 13573
5f5c8ee5 13574 IT describes the display environment in which we display, as usual.
a2889657
JB
13575
13576 DEPTH is the depth in recursion. It is used to prevent
13577 infinite recursion here.
13578
5f5c8ee5
GM
13579 FIELD_WIDTH is the number of characters the display of ELT should
13580 occupy in the mode line, and PRECISION is the maximum number of
13581 characters to display from ELT's representation. See
b8523839 13582 display_string for details.
a2889657 13583
5f5c8ee5 13584 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
13585
13586static int
0fcf414f 13587display_mode_element (it, depth, field_width, precision, elt, props)
5f5c8ee5 13588 struct it *it;
a2889657 13589 int depth;
5f5c8ee5 13590 int field_width, precision;
0fcf414f 13591 Lisp_Object elt, props;
a2889657 13592{
5f5c8ee5 13593 int n = 0, field, prec;
0fcf414f 13594 int literal = 0;
5f5c8ee5 13595
a2889657
JB
13596 tail_recurse:
13597 if (depth > 10)
13598 goto invalid;
13599
13600 depth++;
13601
0220c518 13602 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
13603 {
13604 case Lisp_String:
13605 {
13606 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 13607 unsigned char c;
ae02e06a 13608 unsigned char *this, *lisp_string;
5f5c8ee5 13609
0fcf414f
RS
13610 if (!NILP (props))
13611 {
13612 Lisp_Object oprops, aelt;
13613 oprops = Ftext_properties_at (make_number (0), elt);
13614 if (NILP (Fequal (props, oprops)))
13615 {
ae02e06a
RS
13616 /* If the starting string has properties,
13617 merge the specified ones onto the existing ones. */
13618 if (! NILP (oprops))
13619 {
13620 Lisp_Object tem;
13621
13622 oprops = Fcopy_sequence (oprops);
13623 tem = props;
13624 while (CONSP (tem))
13625 {
13626 oprops = Fplist_put (oprops, XCAR (tem),
13627 XCAR (XCDR (tem)));
13628 tem = XCDR (XCDR (tem));
13629 }
13630 props = oprops;
13631 }
13632
0fcf414f
RS
13633 aelt = Fassoc (elt, mode_line_proptrans_alist);
13634 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13635 elt = XCAR (aelt);
13636 else
13637 {
13638 elt = Fcopy_sequence (elt);
13639 Fset_text_properties (0, Flength (elt), props, elt);
13640 mode_line_proptrans_alist
13641 = Fcons (Fcons (elt, props),
13642 mode_line_proptrans_alist);
13643 }
13644 }
13645 }
13646
ae02e06a
RS
13647 this = XSTRING (elt)->data;
13648 lisp_string = this;
13649
0fcf414f
RS
13650 if (literal)
13651 {
13652 prec = precision - n;
13653 if (frame_title_ptr)
13654 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13655 else
13656 n += display_string (NULL, elt, Qnil, 0, 0, it,
13657 0, prec, 0, STRING_MULTIBYTE (elt));
13658
13659 break;
13660 }
13661
5f5c8ee5
GM
13662 while ((precision <= 0 || n < precision)
13663 && *this
13664 && (frame_title_ptr
13665 || it->current_x < it->last_visible_x))
a2889657
JB
13666 {
13667 unsigned char *last = this;
5f5c8ee5
GM
13668
13669 /* Advance to end of string or next format specifier. */
a2889657
JB
13670 while ((c = *this++) != '\0' && c != '%')
13671 ;
5f5c8ee5 13672
a2889657
JB
13673 if (this - 1 != last)
13674 {
5f5c8ee5
GM
13675 /* Output to end of string or up to '%'. Field width
13676 is length of string. Don't output more than
13677 PRECISION allows us. */
d26b89b8 13678 --this;
eaaa65b0
GM
13679
13680 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
13681 if (precision > 0 && prec > precision - n)
13682 prec = precision - n;
13683
d39b6696 13684 if (frame_title_ptr)
d26b89b8 13685 n += store_frame_title (last, 0, prec);
d39b6696 13686 else
eaaa65b0
GM
13687 {
13688 int bytepos = last - lisp_string;
13689 int charpos = string_byte_to_char (elt, bytepos);
13690 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
13691 it, 0, prec, 0,
13692 STRING_MULTIBYTE (elt));
eaaa65b0 13693 }
a2889657
JB
13694 }
13695 else /* c == '%' */
13696 {
5f5c8ee5
GM
13697 unsigned char *percent_position = this;
13698
13699 /* Get the specified minimum width. Zero means
13700 don't pad. */
13701 field = 0;
a2889657 13702 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 13703 field = field * 10 + c - '0';
a2889657 13704
5f5c8ee5
GM
13705 /* Don't pad beyond the total padding allowed. */
13706 if (field_width - n > 0 && field > field_width - n)
13707 field = field_width - n;
a2889657 13708
5f5c8ee5
GM
13709 /* Note that either PRECISION <= 0 or N < PRECISION. */
13710 prec = precision - n;
13711
a2889657 13712 if (c == 'M')
5f5c8ee5 13713 n += display_mode_element (it, depth, field, prec,
0fcf414f 13714 Vglobal_mode_string, props);
a2889657 13715 else if (c != 0)
d39b6696 13716 {
72f62cb5 13717 int multibyte;
ae02e06a
RS
13718 int bytepos, charpos;
13719 unsigned char *spec;
13720
13721 bytepos = percent_position - lisp_string;
13722 charpos = (STRING_MULTIBYTE (elt)
13723 ? string_byte_to_char (elt, bytepos)
13724 : bytepos);
13725
13726 spec
72f62cb5
GM
13727 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13728
d39b6696 13729 if (frame_title_ptr)
5f5c8ee5 13730 n += store_frame_title (spec, field, prec);
d39b6696 13731 else
5f5c8ee5 13732 {
ae02e06a 13733 int nglyphs_before, nwritten;
72f62cb5
GM
13734
13735 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
13736 nwritten = display_string (spec, Qnil, elt,
13737 charpos, 0, it,
13738 field, prec, 0,
13739 multibyte);
5f5c8ee5
GM
13740
13741 /* Assign to the glyphs written above the
13742 string where the `%x' came from, position
13743 of the `%'. */
13744 if (nwritten > 0)
13745 {
13746 struct glyph *glyph
13747 = (it->glyph_row->glyphs[TEXT_AREA]
13748 + nglyphs_before);
13749 int i;
13750
13751 for (i = 0; i < nwritten; ++i)
13752 {
13753 glyph[i].object = elt;
13754 glyph[i].charpos = charpos;
13755 }
13756
13757 n += nwritten;
13758 }
13759 }
d39b6696 13760 }
b8523839
AS
13761 else /* c == 0 */
13762 break;
a2889657
JB
13763 }
13764 }
13765 }
13766 break;
13767
13768 case Lisp_Symbol:
13769 /* A symbol: process the value of the symbol recursively
13770 as if it appeared here directly. Avoid error if symbol void.
13771 Special case: if value of symbol is a string, output the string
13772 literally. */
13773 {
13774 register Lisp_Object tem;
13775 tem = Fboundp (elt);
265a9e55 13776 if (!NILP (tem))
a2889657
JB
13777 {
13778 tem = Fsymbol_value (elt);
13779 /* If value is a string, output that string literally:
13780 don't check for % within it. */
e24c997d 13781 if (STRINGP (tem))
0fcf414f
RS
13782 literal = 1;
13783
13784 if (!EQ (tem, elt))
5f5c8ee5
GM
13785 {
13786 /* Give up right away for nil or t. */
13787 elt = tem;
13788 goto tail_recurse;
13789 }
a2889657
JB
13790 }
13791 }
13792 break;
13793
13794 case Lisp_Cons:
13795 {
13796 register Lisp_Object car, tem;
13797
0fcf414f
RS
13798 /* A cons cell: five distinct cases.
13799 If first element is :eval or :propertize, do something special.
a2889657
JB
13800 If first element is a string or a cons, process all the elements
13801 and effectively concatenate them.
13802 If first element is a negative number, truncate displaying cdr to
13803 at most that many characters. If positive, pad (with spaces)
13804 to at least that many characters.
13805 If first element is a symbol, process the cadr or caddr recursively
13806 according to whether the symbol's value is non-nil or nil. */
9472f927 13807 car = XCAR (elt);
0fcf414f 13808 if (EQ (car, QCeval))
5f5c8ee5
GM
13809 {
13810 /* An element of the form (:eval FORM) means evaluate FORM
13811 and use the result as mode line elements. */
0fcf414f
RS
13812
13813 if (CONSP (XCDR (elt)))
13814 {
13815 Lisp_Object spec;
13816 spec = safe_eval (XCAR (XCDR (elt)));
13817 n += display_mode_element (it, depth, field_width - n,
13818 precision - n, spec, props);
13819 }
13820 }
13821 else if (EQ (car, QCpropertize))
13822 {
13823 if (CONSP (XCDR (elt)))
13824 {
13825 /* An element of the form (:propertize ELT PROPS...)
13826 means display ELT but applying properties PROPS. */
13827 n += display_mode_element (it, depth, field_width - n,
13828 precision - n, XCAR (XCDR (elt)),
13829 XCDR (XCDR (elt)));
13830 }
5f5c8ee5
GM
13831 }
13832 else if (SYMBOLP (car))
a2889657
JB
13833 {
13834 tem = Fboundp (car);
9472f927 13835 elt = XCDR (elt);
e24c997d 13836 if (!CONSP (elt))
a2889657
JB
13837 goto invalid;
13838 /* elt is now the cdr, and we know it is a cons cell.
13839 Use its car if CAR has a non-nil value. */
265a9e55 13840 if (!NILP (tem))
a2889657
JB
13841 {
13842 tem = Fsymbol_value (car);
265a9e55 13843 if (!NILP (tem))
9472f927
GM
13844 {
13845 elt = XCAR (elt);
13846 goto tail_recurse;
13847 }
a2889657
JB
13848 }
13849 /* Symbol's value is nil (or symbol is unbound)
13850 Get the cddr of the original list
13851 and if possible find the caddr and use that. */
9472f927 13852 elt = XCDR (elt);
265a9e55 13853 if (NILP (elt))
a2889657 13854 break;
e24c997d 13855 else if (!CONSP (elt))
a2889657 13856 goto invalid;
9472f927 13857 elt = XCAR (elt);
a2889657
JB
13858 goto tail_recurse;
13859 }
e24c997d 13860 else if (INTEGERP (car))
a2889657
JB
13861 {
13862 register int lim = XINT (car);
9472f927 13863 elt = XCDR (elt);
a2889657 13864 if (lim < 0)
5f5c8ee5
GM
13865 {
13866 /* Negative int means reduce maximum width. */
13867 if (precision <= 0)
13868 precision = -lim;
13869 else
13870 precision = min (precision, -lim);
13871 }
a2889657
JB
13872 else if (lim > 0)
13873 {
13874 /* Padding specified. Don't let it be more than
13875 current maximum. */
5f5c8ee5
GM
13876 if (precision > 0)
13877 lim = min (precision, lim);
13878
a2889657
JB
13879 /* If that's more padding than already wanted, queue it.
13880 But don't reduce padding already specified even if
13881 that is beyond the current truncation point. */
5f5c8ee5 13882 field_width = max (lim, field_width);
a2889657
JB
13883 }
13884 goto tail_recurse;
13885 }
e24c997d 13886 else if (STRINGP (car) || CONSP (car))
a2889657
JB
13887 {
13888 register int limit = 50;
5f5c8ee5
GM
13889 /* Limit is to protect against circular lists. */
13890 while (CONSP (elt)
13891 && --limit > 0
13892 && (precision <= 0 || n < precision))
a2889657 13893 {
5f5c8ee5 13894 n += display_mode_element (it, depth, field_width - n,
0fcf414f 13895 precision - n, XCAR (elt), props);
9472f927 13896 elt = XCDR (elt);
a2889657
JB
13897 }
13898 }
13899 }
13900 break;
13901
13902 default:
13903 invalid:
d39b6696 13904 if (frame_title_ptr)
5f5c8ee5 13905 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 13906 else
5f5c8ee5
GM
13907 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13908 precision - n, 0, 0);
13909 return n;
a2889657
JB
13910 }
13911
5f5c8ee5
GM
13912 /* Pad to FIELD_WIDTH. */
13913 if (field_width > 0 && n < field_width)
13914 {
13915 if (frame_title_ptr)
13916 n += store_frame_title ("", field_width - n, 0);
13917 else
13918 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13919 0, 0, 0);
13920 }
13921
13922 return n;
a2889657 13923}
5f5c8ee5
GM
13924
13925
766525bc
RS
13926/* Write a null-terminated, right justified decimal representation of
13927 the positive integer D to BUF using a minimal field width WIDTH. */
13928
13929static void
13930pint2str (buf, width, d)
13931 register char *buf;
13932 register int width;
13933 register int d;
13934{
13935 register char *p = buf;
13936
13937 if (d <= 0)
5f5c8ee5 13938 *p++ = '0';
766525bc 13939 else
5f5c8ee5 13940 {
766525bc 13941 while (d > 0)
5f5c8ee5 13942 {
766525bc
RS
13943 *p++ = d % 10 + '0';
13944 d /= 10;
5f5c8ee5
GM
13945 }
13946 }
13947
13948 for (width -= (int) (p - buf); width > 0; --width)
13949 *p++ = ' ';
766525bc
RS
13950 *p-- = '\0';
13951 while (p > buf)
5f5c8ee5 13952 {
766525bc
RS
13953 d = *buf;
13954 *buf++ = *p;
13955 *p-- = d;
5f5c8ee5 13956 }
766525bc
RS
13957}
13958
5f5c8ee5 13959/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
13960 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13961 type of CODING_SYSTEM. Return updated pointer into BUF. */
13962
6693a99a 13963static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 13964
1c9241f5
KH
13965static char *
13966decode_mode_spec_coding (coding_system, buf, eol_flag)
13967 Lisp_Object coding_system;
13968 register char *buf;
13969 int eol_flag;
13970{
1e1078d6 13971 Lisp_Object val;
916848d8 13972 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
13973 unsigned char *eol_str;
13974 int eol_str_len;
13975 /* The EOL conversion we are using. */
13976 Lisp_Object eoltype;
1e1078d6 13977
4a09dee0 13978 val = Fget (coding_system, Qcoding_system);
085536c2 13979 eoltype = Qnil;
1c9241f5 13980
4a09dee0 13981 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 13982 {
916848d8
RS
13983 if (multibyte)
13984 *buf++ = '-';
21e989e3 13985 if (eol_flag)
302f2b38 13986 eoltype = eol_mnemonic_undecided;
1e1078d6 13987 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
13988 }
13989 else
13990 {
1e1078d6
RS
13991 Lisp_Object eolvalue;
13992
13993 eolvalue = Fget (coding_system, Qeol_type);
13994
916848d8 13995 if (multibyte)
a61b7058 13996 *buf++ = XFASTINT (AREF (val, 1));
916848d8 13997
1c9241f5
KH
13998 if (eol_flag)
13999 {
1e1078d6
RS
14000 /* The EOL conversion that is normal on this system. */
14001
14002 if (NILP (eolvalue)) /* Not yet decided. */
14003 eoltype = eol_mnemonic_undecided;
14004 else if (VECTORP (eolvalue)) /* Not yet decided. */
14005 eoltype = eol_mnemonic_undecided;
14006 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14007 eoltype = (XFASTINT (eolvalue) == 0
14008 ? eol_mnemonic_unix
14009 : (XFASTINT (eolvalue) == 1
14010 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
14011 }
14012 }
5f5c8ee5 14013
302f2b38
EZ
14014 if (eol_flag)
14015 {
14016 /* Mention the EOL conversion if it is not the usual one. */
14017 if (STRINGP (eoltype))
14018 {
14019 eol_str = XSTRING (eoltype)->data;
14020 eol_str_len = XSTRING (eoltype)->size;
14021 }
f30b3499
KH
14022 else if (INTEGERP (eoltype)
14023 && CHAR_VALID_P (XINT (eoltype), 0))
14024 {
4a09dee0 14025 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 14026 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 14027 }
302f2b38
EZ
14028 else
14029 {
14030 eol_str = invalid_eol_type;
14031 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 14032 }
f30b3499 14033 bcopy (eol_str, buf, eol_str_len);
302f2b38 14034 buf += eol_str_len;
1c9241f5 14035 }
302f2b38 14036
1c9241f5
KH
14037 return buf;
14038}
14039
a2889657 14040/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
14041 generated by character C. PRECISION >= 0 means don't return a
14042 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
14043 string returned with spaces to that value. Return 1 in *MULTIBYTE
14044 if the result is multibyte text. */
a2889657 14045
11e82b76
JB
14046static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14047
a2889657 14048static char *
72f62cb5 14049decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 14050 struct window *w;
68c45bf0 14051 register int c;
5f5c8ee5 14052 int field_width, precision;
72f62cb5 14053 int *multibyte;
a2889657 14054{
0b67772d 14055 Lisp_Object obj;
5f5c8ee5
GM
14056 struct frame *f = XFRAME (WINDOW_FRAME (w));
14057 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 14058 struct buffer *b = XBUFFER (w->buffer);
a2889657 14059
0b67772d 14060 obj = Qnil;
72f62cb5 14061 *multibyte = 0;
a2889657
JB
14062
14063 switch (c)
14064 {
1af9f229
RS
14065 case '*':
14066 if (!NILP (b->read_only))
14067 return "%";
14068 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14069 return "*";
14070 return "-";
14071
14072 case '+':
14073 /* This differs from %* only for a modified read-only buffer. */
14074 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14075 return "*";
14076 if (!NILP (b->read_only))
14077 return "%";
14078 return "-";
14079
14080 case '&':
14081 /* This differs from %* in ignoring read-only-ness. */
14082 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14083 return "*";
14084 return "-";
14085
14086 case '%':
14087 return "%";
14088
14089 case '[':
14090 {
14091 int i;
14092 char *p;
14093
14094 if (command_loop_level > 5)
14095 return "[[[... ";
14096 p = decode_mode_spec_buf;
14097 for (i = 0; i < command_loop_level; i++)
14098 *p++ = '[';
14099 *p = 0;
14100 return decode_mode_spec_buf;
14101 }
14102
14103 case ']':
14104 {
14105 int i;
14106 char *p;
14107
14108 if (command_loop_level > 5)
14109 return " ...]]]";
14110 p = decode_mode_spec_buf;
14111 for (i = 0; i < command_loop_level; i++)
14112 *p++ = ']';
14113 *p = 0;
14114 return decode_mode_spec_buf;
14115 }
14116
14117 case '-':
14118 {
1af9f229 14119 register int i;
5f5c8ee5
GM
14120
14121 /* Let lots_of_dashes be a string of infinite length. */
14122 if (field_width <= 0
14123 || field_width > sizeof (lots_of_dashes))
1af9f229 14124 {
5f5c8ee5
GM
14125 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14126 decode_mode_spec_buf[i] = '-';
14127 decode_mode_spec_buf[i] = '\0';
14128 return decode_mode_spec_buf;
1af9f229 14129 }
5f5c8ee5
GM
14130 else
14131 return lots_of_dashes;
1af9f229
RS
14132 }
14133
a2889657 14134 case 'b':
d39b6696 14135 obj = b->name;
a2889657
JB
14136 break;
14137
1af9f229
RS
14138 case 'c':
14139 {
14140 int col = current_column ();
ac90c44f 14141 w->column_number_displayed = make_number (col);
5f5c8ee5 14142 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
14143 return decode_mode_spec_buf;
14144 }
14145
14146 case 'F':
14147 /* %F displays the frame name. */
5f5c8ee5 14148 if (!NILP (f->title))
95184b48 14149 return (char *) XSTRING (f->title)->data;
fd8ff63d 14150 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 14151 return (char *) XSTRING (f->name)->data;
9c6da96f 14152 return "Emacs";
1af9f229 14153
a2889657 14154 case 'f':
d39b6696 14155 obj = b->filename;
a2889657
JB
14156 break;
14157
aa6d10fa
RS
14158 case 'l':
14159 {
12adba34
RS
14160 int startpos = XMARKER (w->start)->charpos;
14161 int startpos_byte = marker_byte_position (w->start);
14162 int line, linepos, linepos_byte, topline;
aa6d10fa 14163 int nlines, junk;
aa6d10fa
RS
14164 int height = XFASTINT (w->height);
14165
14166 /* If we decided that this buffer isn't suitable for line numbers,
14167 don't forget that too fast. */
14168 if (EQ (w->base_line_pos, w->buffer))
766525bc 14169 goto no_value;
5300fd39
RS
14170 /* But do forget it, if the window shows a different buffer now. */
14171 else if (BUFFERP (w->base_line_pos))
14172 w->base_line_pos = Qnil;
aa6d10fa
RS
14173
14174 /* If the buffer is very big, don't waste time. */
37d034d3 14175 if (INTEGERP (Vline_number_display_limit)
090703f4 14176 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
14177 {
14178 w->base_line_pos = Qnil;
14179 w->base_line_number = Qnil;
766525bc 14180 goto no_value;
aa6d10fa
RS
14181 }
14182
14183 if (!NILP (w->base_line_number)
14184 && !NILP (w->base_line_pos)
12adba34 14185 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
14186 {
14187 line = XFASTINT (w->base_line_number);
14188 linepos = XFASTINT (w->base_line_pos);
12adba34 14189 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
14190 }
14191 else
14192 {
14193 line = 1;
d39b6696 14194 linepos = BUF_BEGV (b);
12adba34 14195 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
14196 }
14197
14198 /* Count lines from base line to window start position. */
12adba34
RS
14199 nlines = display_count_lines (linepos, linepos_byte,
14200 startpos_byte,
14201 startpos, &junk);
aa6d10fa
RS
14202
14203 topline = nlines + line;
14204
14205 /* Determine a new base line, if the old one is too close
14206 or too far away, or if we did not have one.
14207 "Too close" means it's plausible a scroll-down would
14208 go back past it. */
d39b6696 14209 if (startpos == BUF_BEGV (b))
aa6d10fa 14210 {
ac90c44f
GM
14211 w->base_line_number = make_number (topline);
14212 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
14213 }
14214 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 14215 || linepos == BUF_BEGV (b))
aa6d10fa 14216 {
d39b6696 14217 int limit = BUF_BEGV (b);
12adba34 14218 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 14219 int position;
5d121aec 14220 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
14221
14222 if (startpos - distance > limit)
12adba34
RS
14223 {
14224 limit = startpos - distance;
14225 limit_byte = CHAR_TO_BYTE (limit);
14226 }
aa6d10fa 14227
12adba34
RS
14228 nlines = display_count_lines (startpos, startpos_byte,
14229 limit_byte,
14230 - (height * 2 + 30),
aa6d10fa
RS
14231 &position);
14232 /* If we couldn't find the lines we wanted within
5d121aec 14233 line_number_display_limit_width chars per line,
aa6d10fa 14234 give up on line numbers for this window. */
12adba34 14235 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
14236 {
14237 w->base_line_pos = w->buffer;
14238 w->base_line_number = Qnil;
766525bc 14239 goto no_value;
aa6d10fa
RS
14240 }
14241
ac90c44f
GM
14242 w->base_line_number = make_number (topline - nlines);
14243 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
14244 }
14245
14246 /* Now count lines from the start pos to point. */
12adba34
RS
14247 nlines = display_count_lines (startpos, startpos_byte,
14248 PT_BYTE, PT, &junk);
aa6d10fa
RS
14249
14250 /* Record that we did display the line number. */
14251 line_number_displayed = 1;
14252
14253 /* Make the string to show. */
5f5c8ee5 14254 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 14255 return decode_mode_spec_buf;
766525bc
RS
14256 no_value:
14257 {
14258 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
14259 int pad = field_width - 2;
14260 while (pad-- > 0)
14261 *p++ = ' ';
14262 *p++ = '?';
b357b9d4
KR
14263 *p++ = '?';
14264 *p = '\0';
766525bc
RS
14265 return decode_mode_spec_buf;
14266 }
aa6d10fa
RS
14267 }
14268 break;
14269
a2889657 14270 case 'm':
d39b6696 14271 obj = b->mode_name;
a2889657
JB
14272 break;
14273
14274 case 'n':
d39b6696 14275 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
14276 return " Narrow";
14277 break;
14278
a2889657
JB
14279 case 'p':
14280 {
14281 int pos = marker_position (w->start);
d39b6696 14282 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 14283
d39b6696 14284 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 14285 {
d39b6696 14286 if (pos <= BUF_BEGV (b))
a2889657
JB
14287 return "All";
14288 else
14289 return "Bottom";
14290 }
d39b6696 14291 else if (pos <= BUF_BEGV (b))
a2889657
JB
14292 return "Top";
14293 else
14294 {
3c7d31b9
RS
14295 if (total > 1000000)
14296 /* Do it differently for a large value, to avoid overflow. */
14297 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14298 else
14299 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
14300 /* We can't normally display a 3-digit number,
14301 so get us a 2-digit number that is close. */
14302 if (total == 100)
14303 total = 99;
14304 sprintf (decode_mode_spec_buf, "%2d%%", total);
14305 return decode_mode_spec_buf;
14306 }
14307 }
14308
8ffcb79f
RS
14309 /* Display percentage of size above the bottom of the screen. */
14310 case 'P':
14311 {
14312 int toppos = marker_position (w->start);
d39b6696
KH
14313 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14314 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 14315
d39b6696 14316 if (botpos >= BUF_ZV (b))
8ffcb79f 14317 {
d39b6696 14318 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
14319 return "All";
14320 else
14321 return "Bottom";
14322 }
14323 else
14324 {
3c7d31b9
RS
14325 if (total > 1000000)
14326 /* Do it differently for a large value, to avoid overflow. */
14327 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14328 else
14329 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
14330 /* We can't normally display a 3-digit number,
14331 so get us a 2-digit number that is close. */
14332 if (total == 100)
14333 total = 99;
d39b6696 14334 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
14335 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14336 else
14337 sprintf (decode_mode_spec_buf, "%2d%%", total);
14338 return decode_mode_spec_buf;
14339 }
14340 }
14341
1af9f229
RS
14342 case 's':
14343 /* status of process */
14344 obj = Fget_buffer_process (w->buffer);
14345 if (NILP (obj))
14346 return "no process";
14347#ifdef subprocesses
14348 obj = Fsymbol_name (Fprocess_status (obj));
14349#endif
14350 break;
d39b6696 14351
1af9f229
RS
14352 case 't': /* indicate TEXT or BINARY */
14353#ifdef MODE_LINE_BINARY_TEXT
14354 return MODE_LINE_BINARY_TEXT (b);
14355#else
14356 return "T";
14357#endif
1c9241f5
KH
14358
14359 case 'z':
14360 /* coding-system (not including end-of-line format) */
14361 case 'Z':
14362 /* coding-system (including end-of-line type) */
14363 {
14364 int eol_flag = (c == 'Z');
539b4d41 14365 char *p = decode_mode_spec_buf;
1c9241f5 14366
d30e754b 14367 if (! FRAME_WINDOW_P (f))
1c9241f5 14368 {
11c52c4f
RS
14369 /* No need to mention EOL here--the terminal never needs
14370 to do EOL conversion. */
14371 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14372 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 14373 }
f13c925f 14374 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 14375 p, eol_flag);
f13c925f 14376
11c52c4f 14377#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
14378#ifdef subprocesses
14379 obj = Fget_buffer_process (Fcurrent_buffer ());
14380 if (PROCESSP (obj))
14381 {
14382 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14383 p, eol_flag);
14384 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14385 p, eol_flag);
14386 }
14387#endif /* subprocesses */
11c52c4f 14388#endif /* 0 */
1c9241f5
KH
14389 *p = 0;
14390 return decode_mode_spec_buf;
14391 }
a2889657 14392 }
d39b6696 14393
e24c997d 14394 if (STRINGP (obj))
72f62cb5
GM
14395 {
14396 *multibyte = STRING_MULTIBYTE (obj);
14397 return (char *) XSTRING (obj)->data;
14398 }
a2889657
JB
14399 else
14400 return "";
14401}
5f5c8ee5
GM
14402
14403
12adba34
RS
14404/* Count up to COUNT lines starting from START / START_BYTE.
14405 But don't go beyond LIMIT_BYTE.
14406 Return the number of lines thus found (always nonnegative).
59b49f63 14407
12adba34 14408 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
14409
14410static int
12adba34
RS
14411display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14412 int start, start_byte, limit_byte, count;
14413 int *byte_pos_ptr;
59b49f63 14414{
59b49f63
RS
14415 register unsigned char *cursor;
14416 unsigned char *base;
14417
14418 register int ceiling;
14419 register unsigned char *ceiling_addr;
12adba34 14420 int orig_count = count;
59b49f63
RS
14421
14422 /* If we are not in selective display mode,
14423 check only for newlines. */
12adba34
RS
14424 int selective_display = (!NILP (current_buffer->selective_display)
14425 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
14426
14427 if (count > 0)
12adba34
RS
14428 {
14429 while (start_byte < limit_byte)
14430 {
14431 ceiling = BUFFER_CEILING_OF (start_byte);
14432 ceiling = min (limit_byte - 1, ceiling);
14433 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14434 base = (cursor = BYTE_POS_ADDR (start_byte));
14435 while (1)
14436 {
14437 if (selective_display)
14438 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14439 ;
14440 else
14441 while (*cursor != '\n' && ++cursor != ceiling_addr)
14442 ;
14443
14444 if (cursor != ceiling_addr)
14445 {
14446 if (--count == 0)
14447 {
14448 start_byte += cursor - base + 1;
14449 *byte_pos_ptr = start_byte;
14450 return orig_count;
14451 }
14452 else
14453 if (++cursor == ceiling_addr)
14454 break;
14455 }
14456 else
14457 break;
14458 }
14459 start_byte += cursor - base;
14460 }
14461 }
59b49f63
RS
14462 else
14463 {
12adba34
RS
14464 while (start_byte > limit_byte)
14465 {
14466 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14467 ceiling = max (limit_byte, ceiling);
14468 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14469 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
14470 while (1)
14471 {
12adba34
RS
14472 if (selective_display)
14473 while (--cursor != ceiling_addr
14474 && *cursor != '\n' && *cursor != 015)
14475 ;
14476 else
14477 while (--cursor != ceiling_addr && *cursor != '\n')
14478 ;
14479
59b49f63
RS
14480 if (cursor != ceiling_addr)
14481 {
14482 if (++count == 0)
14483 {
12adba34
RS
14484 start_byte += cursor - base + 1;
14485 *byte_pos_ptr = start_byte;
14486 /* When scanning backwards, we should
14487 not count the newline posterior to which we stop. */
14488 return - orig_count - 1;
59b49f63
RS
14489 }
14490 }
14491 else
14492 break;
14493 }
12adba34
RS
14494 /* Here we add 1 to compensate for the last decrement
14495 of CURSOR, which took it past the valid range. */
14496 start_byte += cursor - base + 1;
59b49f63
RS
14497 }
14498 }
14499
12adba34 14500 *byte_pos_ptr = limit_byte;
aa6d10fa 14501
12adba34
RS
14502 if (count < 0)
14503 return - orig_count + count;
14504 return orig_count - count;
aa6d10fa 14505
12adba34 14506}
a2889657 14507
a2889657 14508
5f5c8ee5
GM
14509\f
14510/***********************************************************************
14511 Displaying strings
14512 ***********************************************************************/
278feba9 14513
5f5c8ee5 14514/* Display a NUL-terminated string, starting with index START.
a3788d53 14515
5f5c8ee5
GM
14516 If STRING is non-null, display that C string. Otherwise, the Lisp
14517 string LISP_STRING is displayed.
a2889657 14518
5f5c8ee5
GM
14519 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14520 FACE_STRING. Display STRING or LISP_STRING with the face at
14521 FACE_STRING_POS in FACE_STRING:
a2889657 14522
5f5c8ee5
GM
14523 Display the string in the environment given by IT, but use the
14524 standard display table, temporarily.
a3788d53 14525
5f5c8ee5
GM
14526 FIELD_WIDTH is the minimum number of output glyphs to produce.
14527 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14528 with spaces. If STRING has more characters, more than FIELD_WIDTH
14529 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14530
14531 PRECISION is the maximum number of characters to output from
14532 STRING. PRECISION < 0 means don't truncate the string.
a2889657 14533
5f5c8ee5 14534 This is roughly equivalent to printf format specifiers:
a2889657 14535
5f5c8ee5
GM
14536 FIELD_WIDTH PRECISION PRINTF
14537 ----------------------------------------
14538 -1 -1 %s
14539 -1 10 %.10s
14540 10 -1 %10s
14541 20 10 %20.10s
a2889657 14542
5f5c8ee5
GM
14543 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14544 display them, and < 0 means obey the current buffer's value of
14545 enable_multibyte_characters.
278feba9 14546
5f5c8ee5 14547 Value is the number of glyphs produced. */
b1d1124b 14548
5f5c8ee5
GM
14549static int
14550display_string (string, lisp_string, face_string, face_string_pos,
14551 start, it, field_width, precision, max_x, multibyte)
14552 unsigned char *string;
14553 Lisp_Object lisp_string;
68c45bf0
PE
14554 Lisp_Object face_string;
14555 int face_string_pos;
5f5c8ee5
GM
14556 int start;
14557 struct it *it;
14558 int field_width, precision, max_x;
14559 int multibyte;
14560{
14561 int hpos_at_start = it->hpos;
14562 int saved_face_id = it->face_id;
14563 struct glyph_row *row = it->glyph_row;
14564
14565 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 14566 with index START. */
5f5c8ee5
GM
14567 reseat_to_string (it, string, lisp_string, start,
14568 precision, field_width, multibyte);
14569
14570 /* If displaying STRING, set up the face of the iterator
14571 from LISP_STRING, if that's given. */
14572 if (STRINGP (face_string))
14573 {
14574 int endptr;
14575 struct face *face;
14576
14577 it->face_id
14578 = face_at_string_position (it->w, face_string, face_string_pos,
14579 0, it->region_beg_charpos,
14580 it->region_end_charpos,
5de7c6f2 14581 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
14582 face = FACE_FROM_ID (it->f, it->face_id);
14583 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 14584 }
a2889657 14585
5f5c8ee5
GM
14586 /* Set max_x to the maximum allowed X position. Don't let it go
14587 beyond the right edge of the window. */
14588 if (max_x <= 0)
14589 max_x = it->last_visible_x;
14590 else
14591 max_x = min (max_x, it->last_visible_x);
efc63ef0 14592
5f5c8ee5
GM
14593 /* Skip over display elements that are not visible. because IT->w is
14594 hscrolled. */
14595 if (it->current_x < it->first_visible_x)
14596 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14597 MOVE_TO_POS | MOVE_TO_X);
a2889657 14598
5f5c8ee5
GM
14599 row->ascent = it->max_ascent;
14600 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14601 row->phys_ascent = it->max_phys_ascent;
14602 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 14603
5f5c8ee5
GM
14604 /* This condition is for the case that we are called with current_x
14605 past last_visible_x. */
14606 while (it->current_x < max_x)
a2889657 14607 {
5f5c8ee5 14608 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 14609
5f5c8ee5
GM
14610 /* Get the next display element. */
14611 if (!get_next_display_element (it))
90adcf20 14612 break;
1c9241f5 14613
5f5c8ee5
GM
14614 /* Produce glyphs. */
14615 x_before = it->current_x;
14616 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14617 PRODUCE_GLYPHS (it);
90adcf20 14618
5f5c8ee5
GM
14619 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14620 i = 0;
14621 x = x_before;
14622 while (i < nglyphs)
a2889657 14623 {
5f5c8ee5
GM
14624 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14625
14626 if (!it->truncate_lines_p
14627 && x + glyph->pixel_width > max_x)
14628 {
14629 /* End of continued line or max_x reached. */
37be86f2
KH
14630 if (CHAR_GLYPH_PADDING_P (*glyph))
14631 {
14632 /* A wide character is unbreakable. */
14633 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14634 it->current_x = x_before;
14635 }
14636 else
14637 {
14638 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14639 it->current_x = x;
14640 }
5f5c8ee5
GM
14641 break;
14642 }
14643 else if (x + glyph->pixel_width > it->first_visible_x)
14644 {
14645 /* Glyph is at least partially visible. */
14646 ++it->hpos;
14647 if (x < it->first_visible_x)
14648 it->glyph_row->x = x - it->first_visible_x;
14649 }
14650 else
a2889657 14651 {
5f5c8ee5
GM
14652 /* Glyph is off the left margin of the display area.
14653 Should not happen. */
14654 abort ();
a2889657 14655 }
5f5c8ee5
GM
14656
14657 row->ascent = max (row->ascent, it->max_ascent);
14658 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14659 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14660 row->phys_height = max (row->phys_height,
14661 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14662 x += glyph->pixel_width;
14663 ++i;
a2889657 14664 }
5f5c8ee5
GM
14665
14666 /* Stop if max_x reached. */
14667 if (i < nglyphs)
14668 break;
14669
14670 /* Stop at line ends. */
14671 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 14672 {
5f5c8ee5
GM
14673 it->continuation_lines_width = 0;
14674 break;
a2889657 14675 }
1c9241f5 14676
cafafe0b 14677 set_iterator_to_next (it, 1);
a688bb24 14678
5f5c8ee5
GM
14679 /* Stop if truncating at the right edge. */
14680 if (it->truncate_lines_p
14681 && it->current_x >= it->last_visible_x)
14682 {
14683 /* Add truncation mark, but don't do it if the line is
14684 truncated at a padding space. */
14685 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 14686 {
5f5c8ee5 14687 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
14688 {
14689 int i, n;
14690
9299cb15 14691 if (it->current_x > it->last_visible_x)
37be86f2 14692 {
9299cb15
KH
14693 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14694 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14695 break;
14696 for (n = row->used[TEXT_AREA]; i < n; ++i)
14697 {
14698 row->used[TEXT_AREA] = i;
14699 produce_special_glyphs (it, IT_TRUNCATION);
14700 }
37be86f2 14701 }
9299cb15 14702 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 14703 }
5f5c8ee5 14704 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 14705 }
5f5c8ee5 14706 break;
1c9241f5 14707 }
a2889657
JB
14708 }
14709
5f5c8ee5
GM
14710 /* Maybe insert a truncation at the left. */
14711 if (it->first_visible_x
14712 && IT_CHARPOS (*it) > 0)
a2889657 14713 {
5f5c8ee5
GM
14714 if (!FRAME_WINDOW_P (it->f))
14715 insert_left_trunc_glyphs (it);
14716 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
14717 }
14718
5f5c8ee5
GM
14719 it->face_id = saved_face_id;
14720
14721 /* Value is number of columns displayed. */
14722 return it->hpos - hpos_at_start;
14723}
a2889657 14724
a2889657 14725
a2889657 14726\f
3b6b6db7 14727/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
14728 appears as an element of LIST or as the car of an element of LIST.
14729 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
14730 way, and return 1/2 if any element of PROPVAL is found in LIST.
14731 Otherwise return 0. This function cannot quit.
14732 The return value is 2 if the text is invisible but with an ellipsis
14733 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
14734
14735int
14736invisible_p (propval, list)
14737 register Lisp_Object propval;
14738 Lisp_Object list;
14739{
3b6b6db7
SM
14740 register Lisp_Object tail, proptail;
14741
14742 for (tail = list; CONSP (tail); tail = XCDR (tail))
14743 {
14744 register Lisp_Object tem;
14745 tem = XCAR (tail);
14746 if (EQ (propval, tem))
14747 return 1;
14748 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14749 return NILP (XCDR (tem)) ? 1 : 2;
14750 }
14751
14752 if (CONSP (propval))
14753 {
14754 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14755 {
14756 Lisp_Object propelt;
14757 propelt = XCAR (proptail);
14758 for (tail = list; CONSP (tail); tail = XCDR (tail))
14759 {
14760 register Lisp_Object tem;
14761 tem = XCAR (tail);
14762 if (EQ (propelt, tem))
14763 return 1;
14764 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14765 return NILP (XCDR (tem)) ? 1 : 2;
14766 }
14767 }
14768 }
14769
14770 return 0;
642eefc6 14771}
5f5c8ee5 14772
642eefc6 14773\f
5f5c8ee5
GM
14774/***********************************************************************
14775 Initialization
14776 ***********************************************************************/
14777
a2889657
JB
14778void
14779syms_of_xdisp ()
14780{
c6e89d6c
GM
14781 Vwith_echo_area_save_vector = Qnil;
14782 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 14783
c6e89d6c
GM
14784 Vmessage_stack = Qnil;
14785 staticpro (&Vmessage_stack);
14786
735c094c 14787 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 14788 staticpro (&Qinhibit_redisplay);
735c094c 14789
b14bc55e
RS
14790 message_dolog_marker1 = Fmake_marker ();
14791 staticpro (&message_dolog_marker1);
14792 message_dolog_marker2 = Fmake_marker ();
14793 staticpro (&message_dolog_marker2);
14794 message_dolog_marker3 = Fmake_marker ();
14795 staticpro (&message_dolog_marker3);
14796
5f5c8ee5
GM
14797#if GLYPH_DEBUG
14798 defsubr (&Sdump_glyph_matrix);
14799 defsubr (&Sdump_glyph_row);
e037b9ec 14800 defsubr (&Sdump_tool_bar_row);
62397849 14801 defsubr (&Strace_redisplay);
bf9249e3 14802 defsubr (&Strace_to_stderr);
5f5c8ee5 14803#endif
99a5de87 14804#ifdef HAVE_WINDOW_SYSTEM
57c28064 14805 defsubr (&Stool_bar_lines_needed);
99a5de87 14806#endif
5f5c8ee5 14807
cf074754
RS
14808 staticpro (&Qmenu_bar_update_hook);
14809 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14810
d46fb96a 14811 staticpro (&Qoverriding_terminal_local_map);
7079aefa 14812 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 14813
399164b4
KH
14814 staticpro (&Qoverriding_local_map);
14815 Qoverriding_local_map = intern ("overriding-local-map");
14816
75c43375
RS
14817 staticpro (&Qwindow_scroll_functions);
14818 Qwindow_scroll_functions = intern ("window-scroll-functions");
14819
e0bfbde6
RS
14820 staticpro (&Qredisplay_end_trigger_functions);
14821 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 14822
2e54982e
RS
14823 staticpro (&Qinhibit_point_motion_hooks);
14824 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14825
9499d71b
GM
14826 QCdata = intern (":data");
14827 staticpro (&QCdata);
5f5c8ee5 14828 Qdisplay = intern ("display");
f3751a65 14829 staticpro (&Qdisplay);
5f5c8ee5
GM
14830 Qspace_width = intern ("space-width");
14831 staticpro (&Qspace_width);
5f5c8ee5
GM
14832 Qraise = intern ("raise");
14833 staticpro (&Qraise);
14834 Qspace = intern ("space");
14835 staticpro (&Qspace);
f3751a65
GM
14836 Qmargin = intern ("margin");
14837 staticpro (&Qmargin);
5f5c8ee5 14838 Qleft_margin = intern ("left-margin");
f3751a65 14839 staticpro (&Qleft_margin);
5f5c8ee5 14840 Qright_margin = intern ("right-margin");
f3751a65 14841 staticpro (&Qright_margin);
5f5c8ee5
GM
14842 Qalign_to = intern ("align-to");
14843 staticpro (&Qalign_to);
14844 QCalign_to = intern (":align-to");
14845 staticpro (&QCalign_to);
5f5c8ee5
GM
14846 Qrelative_width = intern ("relative-width");
14847 staticpro (&Qrelative_width);
14848 QCrelative_width = intern (":relative-width");
14849 staticpro (&QCrelative_width);
14850 QCrelative_height = intern (":relative-height");
14851 staticpro (&QCrelative_height);
14852 QCeval = intern (":eval");
14853 staticpro (&QCeval);
0fcf414f
RS
14854 QCpropertize = intern (":propertize");
14855 staticpro (&QCpropertize);
d3acf96b 14856 Qwhen = intern ("when");
f3751a65 14857 staticpro (&Qwhen);
886bd6f2
GM
14858 QCfile = intern (":file");
14859 staticpro (&QCfile);
5f5c8ee5
GM
14860 Qfontified = intern ("fontified");
14861 staticpro (&Qfontified);
14862 Qfontification_functions = intern ("fontification-functions");
14863 staticpro (&Qfontification_functions);
5f5c8ee5
GM
14864 Qtrailing_whitespace = intern ("trailing-whitespace");
14865 staticpro (&Qtrailing_whitespace);
14866 Qimage = intern ("image");
14867 staticpro (&Qimage);
ad4f174e
GM
14868 Qmessage_truncate_lines = intern ("message-truncate-lines");
14869 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
14870 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14871 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
14872 Qgrow_only = intern ("grow-only");
14873 staticpro (&Qgrow_only);
e1477f43
GM
14874 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14875 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
14876 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14877 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
14878 Qposition = intern ("position");
14879 staticpro (&Qposition);
14880 Qbuffer_position = intern ("buffer-position");
14881 staticpro (&Qbuffer_position);
14882 Qobject = intern ("object");
14883 staticpro (&Qobject);
5f5c8ee5 14884
a2889657
JB
14885 last_arrow_position = Qnil;
14886 last_arrow_string = Qnil;
f3751a65
GM
14887 staticpro (&last_arrow_position);
14888 staticpro (&last_arrow_string);
c6e89d6c
GM
14889
14890 echo_buffer[0] = echo_buffer[1] = Qnil;
14891 staticpro (&echo_buffer[0]);
14892 staticpro (&echo_buffer[1]);
14893
14894 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14895 staticpro (&echo_area_buffer[0]);
14896 staticpro (&echo_area_buffer[1]);
a2889657 14897
6a94510a
GM
14898 Vmessages_buffer_name = build_string ("*Messages*");
14899 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
14900
14901 mode_line_proptrans_alist = Qnil;
14902 staticpro (&mode_line_proptrans_alist);
6a94510a 14903
7ee72033
MB
14904 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14905 doc: /* Non-nil means highlight trailing whitespace.
228299fa 14906The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
14907 Vshow_trailing_whitespace = Qnil;
14908
7ee72033
MB
14909 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14910 doc: /* Non-nil means don't actually do any redisplay.
228299fa 14911This is used for internal purposes. */);
735c094c
KH
14912 Vinhibit_redisplay = Qnil;
14913
7ee72033
MB
14914 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14915 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
14916 Vglobal_mode_string = Qnil;
14917
7ee72033
MB
14918 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14919 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
14920This must be the beginning of a line in order to work.
14921See also `overlay-arrow-string'. */);
a2889657
JB
14922 Voverlay_arrow_position = Qnil;
14923
7ee72033
MB
14924 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14925 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
a2889657
JB
14926 Voverlay_arrow_string = Qnil;
14927
7ee72033
MB
14928 DEFVAR_INT ("scroll-step", &scroll_step,
14929 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
14930If that fails to bring point back on frame, point is centered instead.
14931If this is zero, point is always centered after it moves off frame.
14932If you want scrolling to always be a line at a time, you should set
14933`scroll-conservatively' to a large value rather than set this to 1. */);
14934
7ee72033
MB
14935 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14936 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
14937A value of zero means to scroll the text to center point vertically
14938in the window. */);
0789adb2
RS
14939 scroll_conservatively = 0;
14940
7ee72033
MB
14941 DEFVAR_INT ("scroll-margin", &scroll_margin,
14942 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
14943Recenter the window whenever point gets within this many lines
14944of the top or bottom of the window. */);
9afd2168
RS
14945 scroll_margin = 0;
14946
5f5c8ee5 14947#if GLYPH_DEBUG
7ee72033 14948 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 14949#endif
a2889657
JB
14950
14951 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
14952 &truncate_partial_width_windows,
14953 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
14954 truncate_partial_width_windows = 1;
14955
7ee72033
MB
14956 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14957 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa
GM
14958Any other value means to use the appropriate face, `mode-line',
14959`header-line', or `menu' respectively.
14960
14961This variable is deprecated; please change the above faces instead. */);
1862a24e 14962 mode_line_inverse_video = 1;
aa6d10fa 14963
7ee72033
MB
14964 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14965 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
14966If the buffer is bigger than this, the line number does not appear
14967in the mode line. A value of nil means no limit. */);
090703f4 14968 Vline_number_display_limit = Qnil;
fba9ce76 14969
090703f4 14970 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
14971 &line_number_display_limit_width,
14972 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
14973If the average length of the lines near point is bigger than this, then the
14974line number may be omitted from the mode line. */);
5d121aec
KH
14975 line_number_display_limit_width = 200;
14976
7ee72033
MB
14977 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14978 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 14979 highlight_nonselected_windows = 0;
d39b6696 14980
7ee72033
MB
14981 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14982 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
14983Minibuffer-only frames don't count, but iconified frames do.
14984This variable is not guaranteed to be accurate except while processing
14985`frame-title-format' and `icon-title-format'. */);
14986
7ee72033
MB
14987 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14988 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
14989\(Assuming the window manager supports this feature.)
14990This variable has the same structure as `mode-line-format' (which see),
14991and is used only on frames for which no explicit name has been set
14992\(see `modify-frame-parameters'). */);
7ee72033
MB
14993 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14994 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
14995\(Assuming the window manager supports this feature.)
14996This variable has the same structure as `mode-line-format' (which see),
14997and is used only on frames for which no explicit name has been set
14998\(see `modify-frame-parameters'). */);
d39b6696
KH
14999 Vicon_title_format
15000 = Vframe_title_format
15001 = Fcons (intern ("multiple-frames"),
15002 Fcons (build_string ("%b"),
3ebf0ea9 15003 Fcons (Fcons (empty_string,
d39b6696
KH
15004 Fcons (intern ("invocation-name"),
15005 Fcons (build_string ("@"),
15006 Fcons (intern ("system-name"),
15007 Qnil)))),
15008 Qnil)));
5992c4f7 15009
7ee72033
MB
15010 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15011 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
15012If nil, disable message logging. If t, log messages but don't truncate
15013the buffer when it becomes large. */);
ac90c44f 15014 Vmessage_log_max = make_number (50);
08b610e4 15015
7ee72033
MB
15016 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15017 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
15018The value should be a list of functions that take one argument.
15019Just before redisplay, for each frame, if any of its windows have changed
15020size since the last redisplay, or have been split or deleted,
15021all the functions in the list are called, with the frame as argument. */);
08b610e4 15022 Vwindow_size_change_functions = Qnil;
75c43375 15023
7ee72033
MB
15024 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15025 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
15026Each function is called with two arguments, the window
15027and its new display-start position. Note that the value of `window-end'
15028is not valid when these functions are called. */);
75c43375 15029 Vwindow_scroll_functions = Qnil;
5f5c8ee5 15030
7ee72033
MB
15031 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15032 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
15033This increases a tool-bar's height if not all tool-bar items are visible.
15034It decreases a tool-bar's height when it would display blank lines
15035otherwise. */);
e037b9ec 15036 auto_resize_tool_bars_p = 1;
5f5c8ee5 15037
7ee72033
MB
15038 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15039 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 15040 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 15041
7ee72033
MB
15042 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15043 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 15044If an integer, use that for both horizontal and vertical margins.
f6c89f27 15045Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
15046HORZ specifying the horizontal margin, and VERT specifying the
15047vertical margin. */);
c3d76173 15048 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 15049
7ee72033 15050 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 15051 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 15052 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 15053
7ee72033
MB
15054 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15055 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
15056Each function is called with one argument POS. Functions must
15057fontify a region starting at POS in the current buffer, and give
15058fontified regions the property `fontified'. */);
5f5c8ee5 15059 Vfontification_functions = Qnil;
6b9f0906 15060 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
15061
15062 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
15063 &unibyte_display_via_language_environment,
15064 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
15065Specifically this means that unibyte non-ASCII characters
15066are displayed by converting them to the equivalent multibyte characters
15067according to the current language environment. As a result, they are
15068displayed according to the current fontset. */);
7bbe686f 15069 unibyte_display_via_language_environment = 0;
c6e89d6c 15070
7ee72033
MB
15071 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15072 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
15073If a float, it specifies a fraction of the mini-window frame's height.
15074If an integer, it specifies a number of lines. */);
c6e89d6c 15075 Vmax_mini_window_height = make_float (0.25);
6422c1d7 15076
7ee72033
MB
15077 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15078 doc: /* *How to resize mini-windows.
228299fa
GM
15079A value of nil means don't automatically resize mini-windows.
15080A value of t means resize them to fit the text displayed in them.
15081A value of `grow-only', the default, means let mini-windows grow
15082only, until their display becomes empty, at which point the windows
15083go back to their normal size. */);
6422c1d7
GM
15084 Vresize_mini_windows = Qgrow_only;
15085
d6d26ed3 15086 DEFVAR_BOOL ("cursor-in-non-selected-windows",
7ee72033
MB
15087 &cursor_in_non_selected_windows,
15088 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
f0529b5b 15089nil means don't display a cursor there. */);
d6d26ed3 15090 cursor_in_non_selected_windows = 1;
d475bcb8 15091
e76d28d5 15092 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 15093 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 15094 automatic_hscrolling_p = 1;
1df7e8f0 15095
e76d28d5 15096 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
15097 doc: /* *How many columns away from the window edge point is allowed to get
15098before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 15099 hscroll_margin = 5;
1df7e8f0 15100
e76d28d5 15101 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
15102 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15103When point is less than `automatic-hscroll-margin' columns from the window
15104edge, automatic hscrolling will scroll the window by the amount of columns
15105determined by this variable. If its value is a positive integer, scroll that
15106many columns. If it's a positive floating-point number, it specifies the
15107fraction of the window's width to scroll. If it's nil or zero, point will be
15108centered horizontally after the scroll. Any other value, including negative
15109numbers, are treated as if the value were zero.
15110
15111Automatic hscrolling always moves point outside the scroll margin, so if
15112point was more than scroll step columns inside the margin, the window will
15113scroll more than the value given by the scroll step.
15114
15115Note that the lower bound for automatic hscrolling specified by `scroll-left'
15116and `scroll-right' overrides this variable's effect. */);
e76d28d5 15117 Vhscroll_step = make_number (0);
e00daaa0 15118
7ee72033
MB
15119 DEFVAR_LISP ("image-types", &Vimage_types,
15120 doc: /* List of supported image types.
228299fa 15121Each element of the list is a symbol for a supported image type. */);
e00daaa0 15122 Vimage_types = Qnil;
ad4f174e 15123
7ee72033
MB
15124 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15125 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 15126Bind this around calls to `message' to let it take effect. */);
ad4f174e 15127 message_truncate_lines = 0;
0bca8940 15128
7ee72033
MB
15129 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15130 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 15131Can be used to update submenus whose contents should vary. */);
6422c1d7 15132 Vmenu_bar_update_hook = Qnil;
e1477f43 15133
7ee72033
MB
15134 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15135 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 15136 inhibit_menubar_update = 0;
30a3f61c 15137
7ee72033
MB
15138 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15139 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 15140 inhibit_eval_during_redisplay = 0;
76cb5e06 15141
69d1f7c9 15142#if GLYPH_DEBUG
76cb5e06
GM
15143 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15144 doc: /* Inhibit try_window_id display optimization. */);
15145 inhibit_try_window_id = 0;
15146
15147 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15148 doc: /* Inhibit try_window_reusing display optimization. */);
15149 inhibit_try_window_reusing = 0;
15150
15151 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15152 doc: /* Inhibit try_cursor_movement display optimization. */);
15153 inhibit_try_cursor_movement = 0;
15154#endif /* GLYPH_DEBUG */
a2889657
JB
15155}
15156
5f5c8ee5
GM
15157
15158/* Initialize this module when Emacs starts. */
15159
dfcf069d 15160void
a2889657
JB
15161init_xdisp ()
15162{
15163 Lisp_Object root_window;
5f5c8ee5 15164 struct window *mini_w;
a2889657 15165
04612a64
GM
15166 current_header_line_height = current_mode_line_height = -1;
15167
5f5c8ee5 15168 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
15169
15170 mini_w = XWINDOW (minibuf_window);
11e82b76 15171 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 15172
a2889657
JB
15173 if (!noninteractive)
15174 {
5f5c8ee5
GM
15175 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15176 int i;
15177
ac90c44f 15178 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
12c226c5 15179 set_window_height (root_window,
5f5c8ee5 15180 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 15181 0);
ac90c44f 15182 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
a2889657
JB
15183 set_window_height (minibuf_window, 1, 0);
15184
ac90c44f
GM
15185 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15186 mini_w->width = make_number (FRAME_WIDTH (f));
5f5c8ee5
GM
15187
15188 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15189 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15190 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15191
15192 /* The default ellipsis glyphs `...'. */
15193 for (i = 0; i < 3; ++i)
ac90c44f 15194 default_invis_vector[i] = make_number ('.');
a2889657 15195 }
5f5c8ee5
GM
15196
15197#ifdef HAVE_WINDOW_SYSTEM
15198 {
15199 /* Allocate the buffer for frame titles. */
15200 int size = 100;
15201 frame_title_buf = (char *) xmalloc (size);
15202 frame_title_buf_end = frame_title_buf + size;
15203 frame_title_ptr = NULL;
15204 }
15205#endif /* HAVE_WINDOW_SYSTEM */
21fdfb65
GM
15206
15207 help_echo_showing_p = 0;
a2889657 15208}
5f5c8ee5
GM
15209
15210