Trailing whitespace deleted.
[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)
2311178e 50 direct_output_for_insert,
5f5c8ee5
GM
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
959bc044 67 What does redisplay do? Obviously, it has to figure out somehow what
5f5c8ee5
GM
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
2311178e 109
5f5c8ee5
GM
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
959bc044 123 interface functions taking an iterator structure (struct it)
5f5c8ee5
GM
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
e0f712ba 197#ifdef MAC_OS
1a578e9b
AC
198#include "macterm.h"
199#endif
a2889657 200
5f5c8ee5
GM
201#define INFINITY 10000000
202
488dd4c4
JD
203#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
204 || defined (USE_GTK)
2e621225 205extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 206extern int pending_menu_activation;
76412d64
RS
207#endif
208
a2889657
JB
209extern int interrupt_input;
210extern int command_loop_level;
211
b6436d4e 212extern int minibuffer_auto_raise;
b2b5455d 213extern Lisp_Object Vminibuffer_list;
b6436d4e 214
c4628384 215extern Lisp_Object Qface;
fec8f23e 216extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
c4628384 217
399164b4
KH
218extern Lisp_Object Voverriding_local_map;
219extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 220extern Lisp_Object Qmenu_item;
399164b4 221
d46fb96a 222Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 223Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 224Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 225Lisp_Object Qinhibit_point_motion_hooks;
0fcf414f 226Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
5f5c8ee5 227Lisp_Object Qfontified;
6422c1d7 228Lisp_Object Qgrow_only;
869fb12c 229Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 230Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5 231
cfe03a41
KS
232/* Cursor shapes */
233Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
234
c53a1624
RS
235Lisp_Object Qrisky_local_variable;
236
7033d6df
RS
237/* Holds the list (error). */
238Lisp_Object list_of_error;
239
5f5c8ee5
GM
240/* Functions called to fontify regions of text. */
241
242Lisp_Object Vfontification_functions;
243Lisp_Object Qfontification_functions;
244
e037b9ec 245/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
246 over them. */
247
e037b9ec 248int auto_raise_tool_bar_buttons_p;
5f5c8ee5 249
e037b9ec 250/* Margin around tool bar buttons in pixels. */
5f5c8ee5 251
35a41507 252Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 253
e037b9ec 254/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 255
31ade731 256EMACS_INT tool_bar_button_relief;
5f5c8ee5 257
e037b9ec 258/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
259 items are visible, and no blank lines remain. */
260
e037b9ec 261int auto_resize_tool_bars_p;
399164b4 262
735c094c
KH
263/* Non-nil means don't actually do any redisplay. */
264
265Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
266
30a3f61c
GM
267/* Non-zero means Lisp evaluation during redisplay is inhibited. */
268
869fb12c 269int inhibit_eval_during_redisplay;
30a3f61c 270
5f5c8ee5
GM
271/* Names of text properties relevant for redisplay. */
272
a7e27ef7 273Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
4b41cebb 274extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
275
276/* Symbols used in text property values. */
277
278Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 279Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 280Lisp_Object Qmargin;
a7e27ef7 281extern Lisp_Object Qheight;
5f5c8ee5 282
8f897821 283/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 284
8f897821 285Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
286
287/* Name of the face used to highlight trailing whitespace. */
288
289Lisp_Object Qtrailing_whitespace;
290
291/* The symbol `image' which is the car of the lists used to represent
292 images in Lisp. */
293
294Lisp_Object Qimage;
295
296/* Non-zero means print newline to stdout before next mini-buffer
297 message. */
a2889657
JB
298
299int noninteractive_need_newline;
300
5f5c8ee5 301/* Non-zero means print newline to message log before next message. */
f88eb0b6 302
3c6595e0 303static int message_log_need_newline;
f88eb0b6 304
b14bc55e
RS
305/* Three markers that message_dolog uses.
306 It could allocate them itself, but that causes trouble
307 in handling memory-full errors. */
308static Lisp_Object message_dolog_marker1;
309static Lisp_Object message_dolog_marker2;
310static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
311\f
312/* The buffer position of the first character appearing entirely or
313 partially on the line of the selected window which contains the
314 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
315 redisplay optimization in redisplay_internal. */
a2889657 316
5f5c8ee5 317static struct text_pos this_line_start_pos;
a2889657 318
5f5c8ee5
GM
319/* Number of characters past the end of the line above, including the
320 terminating newline. */
321
322static struct text_pos this_line_end_pos;
323
324/* The vertical positions and the height of this line. */
a2889657 325
a2889657 326static int this_line_vpos;
5f5c8ee5
GM
327static int this_line_y;
328static int this_line_pixel_height;
329
330/* X position at which this display line starts. Usually zero;
331 negative if first character is partially visible. */
332
333static int this_line_start_x;
a2889657 334
5f5c8ee5 335/* Buffer that this_line_.* variables are referring to. */
a2889657 336
a2889657
JB
337static struct buffer *this_line_buffer;
338
5f5c8ee5
GM
339/* Nonzero means truncate lines in all windows less wide than the
340 frame. */
a2889657 341
a2889657
JB
342int truncate_partial_width_windows;
343
7bbe686f 344/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 345
7bbe686f 346int unibyte_display_via_language_environment;
2311178e 347
5f5c8ee5
GM
348/* Nonzero means we have more than one non-mini-buffer-only frame.
349 Not guaranteed to be accurate except while parsing
350 frame-title-format. */
7bbe686f 351
d39b6696
KH
352int multiple_frames;
353
a2889657
JB
354Lisp_Object Vglobal_mode_string;
355
356/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 357
a2889657
JB
358Lisp_Object Voverlay_arrow_position;
359
5f5c8ee5
GM
360/* String to display for the arrow. Only used on terminal frames. */
361
a2889657
JB
362Lisp_Object Voverlay_arrow_string;
363
5f5c8ee5
GM
364/* Values of those variables at last redisplay. However, if
365 Voverlay_arrow_position is a marker, last_arrow_position is its
366 numerical position. */
367
d45de95b
RS
368static Lisp_Object last_arrow_position, last_arrow_string;
369
5f5c8ee5
GM
370/* Like mode-line-format, but for the title bar on a visible frame. */
371
d39b6696
KH
372Lisp_Object Vframe_title_format;
373
5f5c8ee5
GM
374/* Like mode-line-format, but for the title bar on an iconified frame. */
375
d39b6696
KH
376Lisp_Object Vicon_title_format;
377
08b610e4
RS
378/* List of functions to call when a window's size changes. These
379 functions get one arg, a frame on which one or more windows' sizes
380 have changed. */
5f5c8ee5 381
08b610e4
RS
382static Lisp_Object Vwindow_size_change_functions;
383
0bca8940 384Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 385
a2889657 386/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 387
5f5c8ee5 388static int overlay_arrow_seen;
ca26e1c8 389
fba9ce76 390/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 391
5f5c8ee5
GM
392int highlight_nonselected_windows;
393
394/* If cursor motion alone moves point off frame, try scrolling this
395 many lines up or down if that will bring it back. */
396
31ade731 397static EMACS_INT scroll_step;
a2889657 398
4b41cebb 399/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
400 screen, when appropriate. */
401
31ade731 402static EMACS_INT scroll_conservatively;
0789adb2 403
5f5c8ee5
GM
404/* Recenter the window whenever point gets within this many lines of
405 the top or bottom of the window. This value is translated into a
406 pixel value by multiplying it with CANON_Y_UNIT, which means that
407 there is really a fixed pixel height scroll margin. */
408
31ade731 409EMACS_INT scroll_margin;
9afd2168 410
5f5c8ee5
GM
411/* Number of windows showing the buffer of the selected window (or
412 another buffer with the same base buffer). keyboard.c refers to
413 this. */
a2889657 414
a2889657
JB
415int buffer_shared;
416
5f5c8ee5 417/* Vector containing glyphs for an ellipsis `...'. */
a2889657 418
5f5c8ee5 419static Lisp_Object default_invis_vector[3];
a2889657 420
1862a24e
MB
421/* Zero means display the mode-line/header-line/menu-bar in the default face
422 (this slightly odd definition is for compatibility with previous versions
423 of emacs), non-zero means display them using their respective faces.
424
425 This variable is deprecated. */
a2889657 426
a2889657
JB
427int mode_line_inverse_video;
428
5f5c8ee5
GM
429/* Prompt to display in front of the mini-buffer contents. */
430
8c5b6a0a 431Lisp_Object minibuf_prompt;
a2889657 432
5f5c8ee5
GM
433/* Width of current mini-buffer prompt. Only set after display_line
434 of the line that contains the prompt. */
435
a2889657 436int minibuf_prompt_width;
5f5c8ee5 437
5f5c8ee5
GM
438/* This is the window where the echo area message was displayed. It
439 is always a mini-buffer window, but it may not be the same window
440 currently active as a mini-buffer. */
441
73af359d
RS
442Lisp_Object echo_area_window;
443
c6e89d6c
GM
444/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
445 pushes the current message and the value of
446 message_enable_multibyte on the stack, the function restore_message
447 pops the stack and displays MESSAGE again. */
448
449Lisp_Object Vmessage_stack;
450
a3788d53
RS
451/* Nonzero means multibyte characters were enabled when the echo area
452 message was specified. */
5f5c8ee5 453
a3788d53
RS
454int message_enable_multibyte;
455
4b41cebb 456/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 457
a2889657
JB
458int update_mode_lines;
459
5f5c8ee5 460/* Nonzero if window sizes or contents have changed since last
4b41cebb 461 redisplay that finished. */
5f5c8ee5 462
a2889657
JB
463int windows_or_buffers_changed;
464
5fb96e96
RS
465/* Nonzero means a frame's cursor type has been changed. */
466
467int cursor_type_changed;
468
5f5c8ee5
GM
469/* Nonzero after display_mode_line if %l was used and it displayed a
470 line number. */
471
aa6d10fa
RS
472int line_number_displayed;
473
474/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 475
090703f4 476Lisp_Object Vline_number_display_limit;
5992c4f7 477
4b41cebb 478/* Line width to consider when repositioning for line number display. */
5d121aec 479
31ade731 480static EMACS_INT line_number_display_limit_width;
5d121aec 481
5f5c8ee5
GM
482/* Number of lines to keep in the message log buffer. t means
483 infinite. nil means don't log at all. */
484
5992c4f7 485Lisp_Object Vmessage_log_max;
d45de95b 486
6a94510a
GM
487/* The name of the *Messages* buffer, a string. */
488
489static Lisp_Object Vmessages_buffer_name;
490
c6e89d6c
GM
491/* Current, index 0, and last displayed echo area message. Either
492 buffers from echo_buffers, or nil to indicate no message. */
493
494Lisp_Object echo_area_buffer[2];
495
496/* The buffers referenced from echo_area_buffer. */
497
498static Lisp_Object echo_buffer[2];
499
500/* A vector saved used in with_area_buffer to reduce consing. */
501
502static Lisp_Object Vwith_echo_area_save_vector;
503
504/* Non-zero means display_echo_area should display the last echo area
505 message again. Set by redisplay_preserve_echo_area. */
506
507static int display_last_displayed_message_p;
508
509/* Nonzero if echo area is being used by print; zero if being used by
510 message. */
511
512int message_buf_print;
513
e1477f43
GM
514/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
515
516Lisp_Object Qinhibit_menubar_update;
517int inhibit_menubar_update;
518
9142dd5b
GM
519/* Maximum height for resizing mini-windows. Either a float
520 specifying a fraction of the available height, or an integer
521 specifying a number of lines. */
c6e89d6c 522
ad4f174e
GM
523Lisp_Object Vmax_mini_window_height;
524
525/* Non-zero means messages should be displayed with truncated
526 lines instead of being continued. */
527
528int message_truncate_lines;
529Lisp_Object Qmessage_truncate_lines;
c6e89d6c 530
6e019995
GM
531/* Set to 1 in clear_message to make redisplay_internal aware
532 of an emptied echo area. */
533
534static int message_cleared_p;
535
d6d26ed3
GM
536/* Non-zero means we want a hollow cursor in windows that are not
537 selected. Zero means there's no cursor in such windows. */
538
cfe03a41 539Lisp_Object Vcursor_in_non_selected_windows;
af79bccb 540Lisp_Object Qcursor_in_non_selected_windows;
d6d26ed3 541
cfe03a41
KS
542/* How to blink the default frame cursor off. */
543Lisp_Object Vblink_cursor_alist;
544
5f5c8ee5
GM
545/* A scratch glyph row with contents used for generating truncation
546 glyphs. Also used in direct_output_for_insert. */
12adba34 547
5f5c8ee5
GM
548#define MAX_SCRATCH_GLYPHS 100
549struct glyph_row scratch_glyph_row;
550static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 551
5f5c8ee5
GM
552/* Ascent and height of the last line processed by move_it_to. */
553
554static int last_max_ascent, last_height;
555
21fdfb65
GM
556/* Non-zero if there's a help-echo in the echo area. */
557
558int help_echo_showing_p;
559
04612a64
GM
560/* If >= 0, computed, exact values of mode-line and header-line height
561 to use in the macros CURRENT_MODE_LINE_HEIGHT and
562 CURRENT_HEADER_LINE_HEIGHT. */
563
564int current_mode_line_height, current_header_line_height;
565
5f5c8ee5 566/* The maximum distance to look ahead for text properties. Values
2311178e 567 that are too small let us call compute_char_face and similar
5f5c8ee5
GM
568 functions too often which is expensive. Values that are too large
569 let us call compute_char_face and alike too often because we
570 might not be interested in text properties that far away. */
571
572#define TEXT_PROP_DISTANCE_LIMIT 100
573
47589c8c
GM
574#if GLYPH_DEBUG
575
76cb5e06
GM
576/* Variables to turn off display optimizations from Lisp. */
577
578int inhibit_try_window_id, inhibit_try_window_reusing;
579int inhibit_try_cursor_movement;
580
5f5c8ee5
GM
581/* Non-zero means print traces of redisplay if compiled with
582 GLYPH_DEBUG != 0. */
583
5f5c8ee5 584int trace_redisplay_p;
47589c8c 585
546a4f00 586#endif /* GLYPH_DEBUG */
47589c8c 587
546a4f00
AI
588#ifdef DEBUG_TRACE_MOVE
589/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
590int trace_move;
591
47589c8c
GM
592#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
593#else
91c3f500 594#define TRACE_MOVE(x) (void) 0
5f5c8ee5 595#endif
2311178e 596
d475bcb8
GM
597/* Non-zero means automatically scroll windows horizontally to make
598 point visible. */
599
600int automatic_hscrolling_p;
601
1df7e8f0
EZ
602/* How close to the margin can point get before the window is scrolled
603 horizontally. */
5d335845 604EMACS_INT hscroll_margin;
1df7e8f0
EZ
605
606/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 607Lisp_Object Vhscroll_step;
1df7e8f0 608
e00daaa0
GM
609/* A list of symbols, one for each supported image type. */
610
611Lisp_Object Vimage_types;
612
6422c1d7 613/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 614 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
615 display. If `grow-only', let mini-windows grow only until they
616 become empty. */
617
618Lisp_Object Vresize_mini_windows;
619
82a7ab23
RS
620/* Buffer being redisplayed -- for redisplay_window_error. */
621
622struct buffer *displayed_buffer;
623
5f5c8ee5
GM
624/* Value returned from text property handlers (see below). */
625
626enum prop_handled
3c6595e0 627{
5f5c8ee5
GM
628 HANDLED_NORMALLY,
629 HANDLED_RECOMPUTE_PROPS,
630 HANDLED_OVERLAY_STRING_CONSUMED,
631 HANDLED_RETURN
632};
3c6595e0 633
5f5c8ee5
GM
634/* A description of text properties that redisplay is interested
635 in. */
3c6595e0 636
5f5c8ee5
GM
637struct props
638{
639 /* The name of the property. */
640 Lisp_Object *name;
90adcf20 641
5f5c8ee5
GM
642 /* A unique index for the property. */
643 enum prop_idx idx;
644
645 /* A handler function called to set up iterator IT from the property
646 at IT's current position. Value is used to steer handle_stop. */
647 enum prop_handled (*handler) P_ ((struct it *it));
648};
649
650static enum prop_handled handle_face_prop P_ ((struct it *));
651static enum prop_handled handle_invisible_prop P_ ((struct it *));
652static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 653static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
654static enum prop_handled handle_overlay_change P_ ((struct it *));
655static enum prop_handled handle_fontified_prop P_ ((struct it *));
656
657/* Properties handled by iterators. */
658
659static struct props it_props[] =
5992c4f7 660{
5f5c8ee5
GM
661 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
662 /* Handle `face' before `display' because some sub-properties of
663 `display' need to know the face. */
664 {&Qface, FACE_PROP_IDX, handle_face_prop},
665 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
666 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 667 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
668 {NULL, 0, NULL}
669};
5992c4f7 670
5f5c8ee5
GM
671/* Value is the position described by X. If X is a marker, value is
672 the marker_position of X. Otherwise, value is X. */
12adba34 673
5f5c8ee5 674#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 675
5f5c8ee5 676/* Enumeration returned by some move_it_.* functions internally. */
12adba34 677
5f5c8ee5
GM
678enum move_it_result
679{
680 /* Not used. Undefined value. */
681 MOVE_UNDEFINED,
bab29e15 682
5f5c8ee5
GM
683 /* Move ended at the requested buffer position or ZV. */
684 MOVE_POS_MATCH_OR_ZV,
bab29e15 685
5f5c8ee5
GM
686 /* Move ended at the requested X pixel position. */
687 MOVE_X_REACHED,
12adba34 688
5f5c8ee5
GM
689 /* Move within a line ended at the end of a line that must be
690 continued. */
691 MOVE_LINE_CONTINUED,
2311178e 692
5f5c8ee5
GM
693 /* Move within a line ended at the end of a line that would
694 be displayed truncated. */
695 MOVE_LINE_TRUNCATED,
ff6c30e5 696
5f5c8ee5
GM
697 /* Move within a line ended at a line end. */
698 MOVE_NEWLINE_OR_CR
699};
12adba34 700
1987b083
RS
701/* This counter is used to clear the face cache every once in a while
702 in redisplay_internal. It is incremented for each redisplay.
703 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
704 cleared. */
705
706#define CLEAR_FACE_CACHE_COUNT 500
707static int clear_face_cache_count;
708
709/* Record the previous terminal frame we displayed. */
710
711static struct frame *previous_terminal_frame;
712
713/* Non-zero while redisplay_internal is in progress. */
714
715int redisplaying_p;
716
26683087
RS
717/* Non-zero means don't free realized faces. Bound while freeing
718 realized faces is dangerous because glyph matrices might still
719 reference them. */
1987b083 720
26683087
RS
721int inhibit_free_realized_faces;
722Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 723
5f5c8ee5
GM
724\f
725/* Function prototypes. */
726
5a08cbaf 727static void setup_for_ellipsis P_ ((struct it *));
43c09969 728static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
729static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
730static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 731static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 732static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 733static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
734
735#if 0
2e621225 736static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
737#endif
738
2e621225
GM
739static int next_element_from_ellipsis P_ ((struct it *));
740static void pint2str P_ ((char *, int, int));
741static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
742 struct text_pos));
743static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
744static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
745static void store_frame_title_char P_ ((char));
50f80c2f 746static int store_frame_title P_ ((const unsigned char *, int, int));
2e621225
GM
747static void x_consider_frame_title P_ ((Lisp_Object));
748static void handle_stop P_ ((struct it *));
749static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 750static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 751static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
752static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
753static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 754static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
755 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
756 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 757static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
758static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
759static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
760static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 761static int display_echo_area P_ ((struct window *));
23dd2d97
KR
762static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
763static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 764static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 765static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
766static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
767 struct text_pos));
768static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 769static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
770static void insert_left_trunc_glyphs P_ ((struct it *));
771static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
772static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 773static int append_space P_ ((struct it *, int));
cb617e7c 774static int make_cursor_line_fully_visible P_ ((struct window *));
03b0a4b4 775static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 776static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
777static int trailing_whitespace_p P_ ((int));
778static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
779static void push_it P_ ((struct it *));
780static void pop_it P_ ((struct it *));
781static void sync_frame_with_window_matrix_rows P_ ((struct window *));
782static void redisplay_internal P_ ((int));
c6e89d6c 783static int echo_area_display P_ ((int));
5f5c8ee5
GM
784static void redisplay_windows P_ ((Lisp_Object));
785static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
786static Lisp_Object redisplay_window_error ();
787static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
788static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
789static void update_menu_bar P_ ((struct frame *, int));
790static int try_window_reusing_current_matrix P_ ((struct window *));
791static int try_window_id P_ ((struct window *));
792static int display_line P_ ((struct it *));
715e84c9 793static int display_mode_lines P_ ((struct window *));
04612a64 794static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 795static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 796static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 797static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
798static void display_menu_bar P_ ((struct window *));
799static int display_count_lines P_ ((int, int, int, int, int *));
800static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
801 int, int, struct it *, int, int, int, int));
802static void compute_line_metrics P_ ((struct it *));
803static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 804static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 805static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
806static void reseat P_ ((struct it *, struct text_pos, int));
807static void reseat_1 P_ ((struct it *, struct text_pos, int));
808static void back_to_previous_visible_line_start P_ ((struct it *));
809static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 810static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
811static int next_element_from_display_vector P_ ((struct it *));
812static int next_element_from_string P_ ((struct it *));
813static int next_element_from_c_string P_ ((struct it *));
814static int next_element_from_buffer P_ ((struct it *));
260a86a0 815static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
816static int next_element_from_image P_ ((struct it *));
817static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 818static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
819static int init_from_display_pos P_ ((struct it *, struct window *,
820 struct display_pos *));
5f5c8ee5
GM
821static void reseat_to_string P_ ((struct it *, unsigned char *,
822 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
823static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
824 int, int, int));
825void move_it_vertically_backward P_ ((struct it *, int));
826static void init_to_row_start P_ ((struct it *, struct window *,
827 struct glyph_row *));
47d57b22
GM
828static int init_to_row_end P_ ((struct it *, struct window *,
829 struct glyph_row *));
5f5c8ee5 830static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 831static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
832static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
833 Lisp_Object, int));
834static struct text_pos string_pos P_ ((int, Lisp_Object));
835static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
836static int number_of_chars P_ ((unsigned char *, int));
837static void compute_stop_pos P_ ((struct it *));
838static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
839 Lisp_Object));
840static int face_before_or_after_it_pos P_ ((struct it *, int));
841static int next_overlay_change P_ ((int));
842static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
843 Lisp_Object, struct text_pos *,
844 int));
06a12811 845static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
846static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
847 struct window *));
5f5c8ee5
GM
848
849#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
850#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 851
5f5c8ee5 852#ifdef HAVE_WINDOW_SYSTEM
12adba34 853
e037b9ec
GM
854static void update_tool_bar P_ ((struct frame *, int));
855static void build_desired_tool_bar_string P_ ((struct frame *f));
856static int redisplay_tool_bar P_ ((struct frame *));
857static void display_tool_bar_line P_ ((struct it *));
12adba34 858
5f5c8ee5 859#endif /* HAVE_WINDOW_SYSTEM */
12adba34 860
5f5c8ee5
GM
861\f
862/***********************************************************************
863 Window display dimensions
864 ***********************************************************************/
12adba34 865
40a301b3
RS
866/* Return the bottom boundary y-position for text lines in window W.
867 This is the first y position at which a line cannot start.
868 It is relative to the top of the window.
869
870 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
871
872INLINE int
873window_text_bottom_y (w)
874 struct window *w;
875{
876 struct frame *f = XFRAME (w->frame);
877 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 878
5f5c8ee5
GM
879 if (WINDOW_WANTS_MODELINE_P (w))
880 height -= CURRENT_MODE_LINE_HEIGHT (w);
881 return height;
f88eb0b6
KH
882}
883
f82aff7c 884
5f5c8ee5 885/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 886 means return the total width of W, not including fringes to
5f5c8ee5 887 the left and right of the window. */
ff6c30e5 888
5f5c8ee5
GM
889INLINE int
890window_box_width (w, area)
891 struct window *w;
892 int area;
893{
894 struct frame *f = XFRAME (w->frame);
895 int width = XFASTINT (w->width);
2311178e 896
5f5c8ee5 897 if (!w->pseudo_window_p)
ff6c30e5 898 {
b46952ae 899 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
2311178e 900
5f5c8ee5
GM
901 if (area == TEXT_AREA)
902 {
903 if (INTEGERP (w->left_margin_width))
904 width -= XFASTINT (w->left_margin_width);
905 if (INTEGERP (w->right_margin_width))
906 width -= XFASTINT (w->right_margin_width);
907 }
908 else if (area == LEFT_MARGIN_AREA)
909 width = (INTEGERP (w->left_margin_width)
910 ? XFASTINT (w->left_margin_width) : 0);
911 else if (area == RIGHT_MARGIN_AREA)
912 width = (INTEGERP (w->right_margin_width)
913 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 914 }
5f5c8ee5
GM
915
916 return width * CANON_X_UNIT (f);
ff6c30e5 917}
1adc55de 918
1adc55de 919
5f5c8ee5 920/* Return the pixel height of the display area of window W, not
4b41cebb 921 including mode lines of W, if any. */
f88eb0b6 922
5f5c8ee5
GM
923INLINE int
924window_box_height (w)
925 struct window *w;
f88eb0b6 926{
5f5c8ee5
GM
927 struct frame *f = XFRAME (w->frame);
928 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
929
930 xassert (height >= 0);
2311178e 931
d9c9e99c
MB
932 /* Note: the code below that determines the mode-line/header-line
933 height is essentially the same as that contained in the macro
934 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
935 the appropriate glyph row has its `mode_line_p' flag set,
936 and if it doesn't, uses estimate_mode_line_height instead. */
937
5f5c8ee5 938 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
939 {
940 struct glyph_row *ml_row
941 = (w->current_matrix && w->current_matrix->rows
942 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
943 : 0);
944 if (ml_row && ml_row->mode_line_p)
945 height -= ml_row->height;
946 else
96d2320f 947 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 948 }
5f5c8ee5 949
045dee35 950 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
951 {
952 struct glyph_row *hl_row
953 = (w->current_matrix && w->current_matrix->rows
954 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
955 : 0);
956 if (hl_row && hl_row->mode_line_p)
957 height -= hl_row->height;
958 else
959 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
960 }
5f5c8ee5 961
7c75be36
PJ
962 /* With a very small font and a mode-line that's taller than
963 default, we might end up with a negative height. */
964 return max (0, height);
5992c4f7
KH
965}
966
967
5f5c8ee5
GM
968/* Return the frame-relative coordinate of the left edge of display
969 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 970 whole window, to the right of the left fringe of W. */
5992c4f7 971
5f5c8ee5
GM
972INLINE int
973window_box_left (w, area)
974 struct window *w;
975 int area;
90adcf20 976{
5f5c8ee5
GM
977 struct frame *f = XFRAME (w->frame);
978 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 979
5f5c8ee5 980 if (!w->pseudo_window_p)
90adcf20 981 {
5f5c8ee5 982 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
b46952ae 983 + FRAME_LEFT_FRINGE_WIDTH (f));
2311178e 984
5f5c8ee5
GM
985 if (area == TEXT_AREA)
986 x += window_box_width (w, LEFT_MARGIN_AREA);
987 else if (area == RIGHT_MARGIN_AREA)
988 x += (window_box_width (w, LEFT_MARGIN_AREA)
989 + window_box_width (w, TEXT_AREA));
90adcf20 990 }
73af359d 991
5f5c8ee5 992 return x;
2311178e 993}
90adcf20 994
b6436d4e 995
5f5c8ee5
GM
996/* Return the frame-relative coordinate of the right edge of display
997 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 998 whole window, to the left of the right fringe of W. */
ded34426 999
5f5c8ee5
GM
1000INLINE int
1001window_box_right (w, area)
1002 struct window *w;
1003 int area;
1004{
1005 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1006}
1007
5f5c8ee5
GM
1008
1009/* Get the bounding box of the display area AREA of window W, without
1010 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1011 whole window, not including the left and right fringes of
5f5c8ee5
GM
1012 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1013 coordinates of the upper-left corner of the box. Return in
1014 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1015
1016INLINE void
1017window_box (w, area, box_x, box_y, box_width, box_height)
1018 struct window *w;
1019 int area;
1020 int *box_x, *box_y, *box_width, *box_height;
1021{
1022 struct frame *f = XFRAME (w->frame);
2311178e 1023
5f5c8ee5
GM
1024 *box_width = window_box_width (w, area);
1025 *box_height = window_box_height (w);
1026 *box_x = window_box_left (w, area);
1027 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1028 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
1029 if (WINDOW_WANTS_HEADER_LINE_P (w))
1030 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 1031}
1adc55de 1032
1adc55de 1033
5f5c8ee5 1034/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1035 mode lines. AREA < 0 means the whole window, not including the
1036 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1037 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1038 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1039 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1040 box. */
ded34426 1041
5f5c8ee5
GM
1042INLINE void
1043window_box_edges (w, area, top_left_x, top_left_y,
1044 bottom_right_x, bottom_right_y)
1045 struct window *w;
1046 int area;
1047 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1048{
5f5c8ee5
GM
1049 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1050 bottom_right_y);
1051 *bottom_right_x += *top_left_x;
1052 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1053}
1054
5f5c8ee5
GM
1055
1056\f
1057/***********************************************************************
1058 Utilities
1059 ***********************************************************************/
1060
8b6ea97f
GM
1061/* Return the bottom y-position of the line the iterator IT is in.
1062 This can modify IT's settings. */
1063
1064int
1065line_bottom_y (it)
1066 struct it *it;
1067{
1068 int line_height = it->max_ascent + it->max_descent;
1069 int line_top_y = it->current_y;
2311178e 1070
8b6ea97f
GM
1071 if (line_height == 0)
1072 {
1073 if (last_height)
1074 line_height = last_height;
1075 else if (IT_CHARPOS (*it) < ZV)
1076 {
1077 move_it_by_lines (it, 1, 1);
1078 line_height = (it->max_ascent || it->max_descent
1079 ? it->max_ascent + it->max_descent
1080 : last_height);
1081 }
1082 else
1083 {
1084 struct glyph_row *row = it->glyph_row;
2311178e 1085
8b6ea97f
GM
1086 /* Use the default character height. */
1087 it->glyph_row = NULL;
1088 it->what = IT_CHARACTER;
1089 it->c = ' ';
1090 it->len = 1;
1091 PRODUCE_GLYPHS (it);
1092 line_height = it->ascent + it->descent;
1093 it->glyph_row = row;
1094 }
1095 }
1096
1097 return line_top_y + line_height;
1098}
1099
1100
0db95684
GM
1101/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1102 1 if POS is visible and the line containing POS is fully visible.
1103 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1104 and header-lines heights. */
3a641a69
GM
1105
1106int
04612a64 1107pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1108 struct window *w;
04612a64 1109 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1110{
1111 struct it it;
1112 struct text_pos top;
fcab1954
GM
1113 int visible_p;
1114 struct buffer *old_buffer = NULL;
1115
1116 if (XBUFFER (w->buffer) != current_buffer)
1117 {
1118 old_buffer = current_buffer;
1119 set_buffer_internal_1 (XBUFFER (w->buffer));
1120 }
3a641a69
GM
1121
1122 *fully = visible_p = 0;
1123 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1124
04612a64
GM
1125 /* Compute exact mode line heights, if requested. */
1126 if (exact_mode_line_heights_p)
1127 {
1128 if (WINDOW_WANTS_MODELINE_P (w))
1129 current_mode_line_height
96d2320f 1130 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1131 current_buffer->mode_line_format);
2311178e 1132
04612a64
GM
1133 if (WINDOW_WANTS_HEADER_LINE_P (w))
1134 current_header_line_height
1135 = display_mode_line (w, HEADER_LINE_FACE_ID,
1136 current_buffer->header_line_format);
1137 }
3a641a69 1138
04612a64 1139 start_display (&it, w, top);
3a641a69
GM
1140 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1141 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1142
1143 /* Note that we may overshoot because of invisible text. */
1144 if (IT_CHARPOS (it) >= charpos)
3a641a69 1145 {
8b6ea97f
GM
1146 int top_y = it.current_y;
1147 int bottom_y = line_bottom_y (&it);
fcab1954 1148 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
2311178e 1149
8b6ea97f
GM
1150 if (top_y < window_top_y)
1151 visible_p = bottom_y > window_top_y;
1152 else if (top_y < it.last_visible_y)
fcab1954
GM
1153 {
1154 visible_p = 1;
8b6ea97f 1155 *fully = bottom_y <= it.last_visible_y;
fcab1954 1156 }
3a641a69
GM
1157 }
1158 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1159 {
1160 move_it_by_lines (&it, 1, 0);
1161 if (charpos < IT_CHARPOS (it))
1162 {
1163 visible_p = 1;
1164 *fully = 0;
1165 }
1166 }
fcab1954
GM
1167
1168 if (old_buffer)
1169 set_buffer_internal_1 (old_buffer);
04612a64
GM
1170
1171 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1172 return visible_p;
1173}
1174
1175
4fdb80f2
GM
1176/* Return the next character from STR which is MAXLEN bytes long.
1177 Return in *LEN the length of the character. This is like
1178 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1179 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1180 character. */
1181
1182static INLINE int
7a5b8a93 1183string_char_and_length (str, maxlen, len)
50f80c2f 1184 const unsigned char *str;
7a5b8a93 1185 int maxlen, *len;
4fdb80f2
GM
1186{
1187 int c;
1188
1189 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1190 if (!CHAR_VALID_P (c, 1))
1191 /* We may not change the length here because other places in Emacs
9ab8560d 1192 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1193 characters. */
1194 c = '?';
1195
1196 return c;
1197}
1198
1199
1200
5f5c8ee5
GM
1201/* Given a position POS containing a valid character and byte position
1202 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1203
1204static struct text_pos
1205string_pos_nchars_ahead (pos, string, nchars)
1206 struct text_pos pos;
1207 Lisp_Object string;
1208 int nchars;
0b1005ef 1209{
5f5c8ee5
GM
1210 xassert (STRINGP (string) && nchars >= 0);
1211
1212 if (STRING_MULTIBYTE (string))
1213 {
2051c264 1214 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1215 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1216 int len;
1217
1218 while (nchars--)
1219 {
4fdb80f2 1220 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1221 p += len, rest -= len;
1222 xassert (rest >= 0);
1223 CHARPOS (pos) += 1;
1224 BYTEPOS (pos) += len;
1225 }
1226 }
1227 else
1228 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1229
1230 return pos;
0a9dc68b
RS
1231}
1232
0a9dc68b 1233
5f5c8ee5
GM
1234/* Value is the text position, i.e. character and byte position,
1235 for character position CHARPOS in STRING. */
1236
1237static INLINE struct text_pos
1238string_pos (charpos, string)
1239 int charpos;
0a9dc68b 1240 Lisp_Object string;
0a9dc68b 1241{
5f5c8ee5
GM
1242 struct text_pos pos;
1243 xassert (STRINGP (string));
1244 xassert (charpos >= 0);
1245 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1246 return pos;
1247}
1248
1249
1250/* Value is a text position, i.e. character and byte position, for
1251 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1252 means recognize multibyte characters. */
1253
1254static struct text_pos
1255c_string_pos (charpos, s, multibyte_p)
1256 int charpos;
1257 unsigned char *s;
1258 int multibyte_p;
1259{
1260 struct text_pos pos;
1261
1262 xassert (s != NULL);
1263 xassert (charpos >= 0);
1264
1265 if (multibyte_p)
0a9dc68b 1266 {
5f5c8ee5
GM
1267 int rest = strlen (s), len;
1268
1269 SET_TEXT_POS (pos, 0, 0);
1270 while (charpos--)
0a9dc68b 1271 {
4fdb80f2 1272 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1273 s += len, rest -= len;
1274 xassert (rest >= 0);
1275 CHARPOS (pos) += 1;
1276 BYTEPOS (pos) += len;
0a9dc68b
RS
1277 }
1278 }
5f5c8ee5
GM
1279 else
1280 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1281
5f5c8ee5
GM
1282 return pos;
1283}
0a9dc68b 1284
0a9dc68b 1285
5f5c8ee5
GM
1286/* Value is the number of characters in C string S. MULTIBYTE_P
1287 non-zero means recognize multibyte characters. */
0a9dc68b 1288
5f5c8ee5
GM
1289static int
1290number_of_chars (s, multibyte_p)
1291 unsigned char *s;
1292 int multibyte_p;
1293{
1294 int nchars;
2311178e 1295
5f5c8ee5
GM
1296 if (multibyte_p)
1297 {
1298 int rest = strlen (s), len;
1299 unsigned char *p = (unsigned char *) s;
0a9dc68b 1300
5f5c8ee5
GM
1301 for (nchars = 0; rest > 0; ++nchars)
1302 {
4fdb80f2 1303 string_char_and_length (p, rest, &len);
5f5c8ee5 1304 rest -= len, p += len;
0a9dc68b
RS
1305 }
1306 }
5f5c8ee5
GM
1307 else
1308 nchars = strlen (s);
1309
1310 return nchars;
0b1005ef
KH
1311}
1312
2311178e 1313
5f5c8ee5
GM
1314/* Compute byte position NEWPOS->bytepos corresponding to
1315 NEWPOS->charpos. POS is a known position in string STRING.
1316 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1317
5f5c8ee5
GM
1318static void
1319compute_string_pos (newpos, pos, string)
1320 struct text_pos *newpos, pos;
1321 Lisp_Object string;
76412d64 1322{
5f5c8ee5
GM
1323 xassert (STRINGP (string));
1324 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1325
5f5c8ee5 1326 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1327 *newpos = string_pos_nchars_ahead (pos, string,
1328 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1329 else
1330 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1331}
1332
9c74a0dd 1333
5f5c8ee5
GM
1334\f
1335/***********************************************************************
1336 Lisp form evaluation
1337 ***********************************************************************/
1338
116d6f5c 1339/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1340
1341static Lisp_Object
116d6f5c 1342safe_eval_handler (arg)
5f5c8ee5
GM
1343 Lisp_Object arg;
1344{
0dbf9fd2 1345 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1346 return Qnil;
1347}
1348
1349
1350/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1351 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1352
71e5b1b8 1353Lisp_Object
116d6f5c 1354safe_eval (sexpr)
5f5c8ee5
GM
1355 Lisp_Object sexpr;
1356{
5f5c8ee5 1357 Lisp_Object val;
2311178e 1358
30a3f61c
GM
1359 if (inhibit_eval_during_redisplay)
1360 val = Qnil;
1361 else
1362 {
331379bf 1363 int count = SPECPDL_INDEX ();
30a3f61c 1364 struct gcpro gcpro1;
0d8b31c0 1365
30a3f61c
GM
1366 GCPRO1 (sexpr);
1367 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1368 /* Use Qt to ensure debugger does not run,
1369 so there is no possibility of wanting to redisplay. */
1370 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1371 safe_eval_handler);
1372 UNGCPRO;
1373 val = unbind_to (count, val);
1374 }
2311178e 1375
30a3f61c 1376 return val;
0d8b31c0
GM
1377}
1378
1379
1380/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1381 Return the result, or nil if something went wrong. Prevent
1382 redisplay during the evaluation. */
0d8b31c0
GM
1383
1384Lisp_Object
116d6f5c 1385safe_call (nargs, args)
0d8b31c0
GM
1386 int nargs;
1387 Lisp_Object *args;
1388{
0d8b31c0 1389 Lisp_Object val;
2311178e 1390
30a3f61c
GM
1391 if (inhibit_eval_during_redisplay)
1392 val = Qnil;
1393 else
1394 {
331379bf 1395 int count = SPECPDL_INDEX ();
30a3f61c 1396 struct gcpro gcpro1;
0d8b31c0 1397
30a3f61c
GM
1398 GCPRO1 (args[0]);
1399 gcpro1.nvars = nargs;
1400 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1401 /* Use Qt to ensure debugger does not run,
1402 so there is no possibility of wanting to redisplay. */
1403 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1404 safe_eval_handler);
1405 UNGCPRO;
1406 val = unbind_to (count, val);
1407 }
1408
1409 return val;
5f5c8ee5
GM
1410}
1411
1412
116d6f5c
GM
1413/* Call function FN with one argument ARG.
1414 Return the result, or nil if something went wrong. */
1415
1416Lisp_Object
1417safe_call1 (fn, arg)
1418 Lisp_Object fn, arg;
1419{
1420 Lisp_Object args[2];
1421 args[0] = fn;
1422 args[1] = arg;
1423 return safe_call (2, args);
1424}
1425
1426
5f5c8ee5
GM
1427\f
1428/***********************************************************************
1429 Debugging
1430 ***********************************************************************/
1431
1432#if 0
1433
1434/* Define CHECK_IT to perform sanity checks on iterators.
1435 This is for debugging. It is too slow to do unconditionally. */
1436
1437static void
1438check_it (it)
1439 struct it *it;
1440{
1441 if (it->method == next_element_from_string)
a2889657 1442 {
5f5c8ee5
GM
1443 xassert (STRINGP (it->string));
1444 xassert (IT_STRING_CHARPOS (*it) >= 0);
1445 }
1446 else if (it->method == next_element_from_buffer)
1447 {
1448 /* Check that character and byte positions agree. */
1449 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1450 }
73af359d 1451
5f5c8ee5
GM
1452 if (it->dpvec)
1453 xassert (it->current.dpvec_index >= 0);
1454 else
1455 xassert (it->current.dpvec_index < 0);
1456}
1f40cad2 1457
5f5c8ee5
GM
1458#define CHECK_IT(IT) check_it ((IT))
1459
1460#else /* not 0 */
1461
1462#define CHECK_IT(IT) (void) 0
1463
1464#endif /* not 0 */
1465
1466
1467#if GLYPH_DEBUG
1468
1469/* Check that the window end of window W is what we expect it
1470 to be---the last row in the current matrix displaying text. */
1471
1472static void
1473check_window_end (w)
1474 struct window *w;
1475{
1476 if (!MINI_WINDOW_P (w)
1477 && !NILP (w->window_end_valid))
1478 {
1479 struct glyph_row *row;
1480 xassert ((row = MATRIX_ROW (w->current_matrix,
1481 XFASTINT (w->window_end_vpos)),
1482 !row->enabled_p
1483 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1484 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1485 }
1486}
1487
1488#define CHECK_WINDOW_END(W) check_window_end ((W))
1489
1490#else /* not GLYPH_DEBUG */
1491
1492#define CHECK_WINDOW_END(W) (void) 0
1493
1494#endif /* not GLYPH_DEBUG */
1495
1496
1497\f
1498/***********************************************************************
1499 Iterator initialization
1500 ***********************************************************************/
1501
1502/* Initialize IT for displaying current_buffer in window W, starting
1503 at character position CHARPOS. CHARPOS < 0 means that no buffer
1504 position is specified which is useful when the iterator is assigned
1505 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1506 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1507
1508 If ROW is not null, calls to produce_glyphs with IT as parameter
1509 will produce glyphs in that row.
1510
1511 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1512 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1513 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1514 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 1515
96d2320f
KS
1516 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1517 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1518 will be initialized to use the corresponding mode line glyph row of
1519 the desired matrix of W. */
5f5c8ee5
GM
1520
1521void
1522init_iterator (it, w, charpos, bytepos, row, base_face_id)
1523 struct it *it;
1524 struct window *w;
1525 int charpos, bytepos;
1526 struct glyph_row *row;
1527 enum face_id base_face_id;
1528{
1529 int highlight_region_p;
5f5c8ee5
GM
1530
1531 /* Some precondition checks. */
1532 xassert (w != NULL && it != NULL);
3b6b6db7
SM
1533 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1534 && charpos <= ZV));
5f5c8ee5
GM
1535
1536 /* If face attributes have been changed since the last redisplay,
1537 free realized faces now because they depend on face definitions
7d0393cf 1538 that might have changed. Don't free faces while there might be
1987b083 1539 desired matrices pending which reference these faces. */
26683087 1540 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
1541 {
1542 face_change_count = 0;
1543 free_all_realized_faces (Qnil);
1544 }
1545
1546 /* Use one of the mode line rows of W's desired matrix if
1547 appropriate. */
1548 if (row == NULL)
1549 {
96d2320f
KS
1550 if (base_face_id == MODE_LINE_FACE_ID
1551 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 1552 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1553 else if (base_face_id == HEADER_LINE_FACE_ID)
1554 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 1555 }
2311178e 1556
5f5c8ee5
GM
1557 /* Clear IT. */
1558 bzero (it, sizeof *it);
1559 it->current.overlay_string_index = -1;
1560 it->current.dpvec_index = -1;
5f5c8ee5
GM
1561 it->base_face_id = base_face_id;
1562
1563 /* The window in which we iterate over current_buffer: */
1564 XSETWINDOW (it->window, w);
1565 it->w = w;
1566 it->f = XFRAME (w->frame);
1567
d475bcb8
GM
1568 /* Extra space between lines (on window systems only). */
1569 if (base_face_id == DEFAULT_FACE_ID
1570 && FRAME_WINDOW_P (it->f))
1571 {
1572 if (NATNUMP (current_buffer->extra_line_spacing))
1573 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1574 else if (it->f->extra_line_spacing > 0)
1575 it->extra_line_spacing = it->f->extra_line_spacing;
1576 }
1577
5f5c8ee5 1578 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1579 attribute changes of named faces, recompute them. When running
1580 in batch mode, the face cache of Vterminal_frame is null. If
1581 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1582 if (
1583#ifndef WINDOWSNT
1584 noninteractive &&
1585#endif
1586 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1587 init_frame_faces (it->f);
5f5c8ee5
GM
1588 if (FRAME_FACE_CACHE (it->f)->used == 0)
1589 recompute_basic_faces (it->f);
1590
5f5c8ee5
GM
1591 /* Current value of the `space-width', and 'height' properties. */
1592 it->space_width = Qnil;
1593 it->font_height = Qnil;
2311178e 1594
5f5c8ee5
GM
1595 /* Are control characters displayed as `^C'? */
1596 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1597
1598 /* -1 means everything between a CR and the following line end
1599 is invisible. >0 means lines indented more than this value are
1600 invisible. */
1601 it->selective = (INTEGERP (current_buffer->selective_display)
1602 ? XFASTINT (current_buffer->selective_display)
2311178e 1603 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
1604 ? -1 : 0));
1605 it->selective_display_ellipsis_p
1606 = !NILP (current_buffer->selective_display_ellipses);
1607
1608 /* Display table to use. */
1609 it->dp = window_display_table (w);
1610
1611 /* Are multibyte characters enabled in current_buffer? */
1612 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1613
1614 /* Non-zero if we should highlight the region. */
1615 highlight_region_p
3ebf0ea9 1616 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
1617 && !NILP (current_buffer->mark_active)
1618 && XMARKER (current_buffer->mark)->buffer != 0);
1619
1620 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1621 start and end of a visible region in window IT->w. Set both to
1622 -1 to indicate no region. */
1623 if (highlight_region_p
1624 /* Maybe highlight only in selected window. */
2311178e 1625 && (/* Either show region everywhere. */
5f5c8ee5
GM
1626 highlight_nonselected_windows
1627 /* Or show region in the selected window. */
1628 || w == XWINDOW (selected_window)
1629 /* Or show the region if we are in the mini-buffer and W is
1630 the window the mini-buffer refers to. */
1631 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
1632 && WINDOWP (minibuf_selected_window)
1633 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
1634 {
1635 int charpos = marker_position (current_buffer->mark);
1636 it->region_beg_charpos = min (PT, charpos);
1637 it->region_end_charpos = max (PT, charpos);
1638 }
1639 else
1640 it->region_beg_charpos = it->region_end_charpos = -1;
1641
1642 /* Get the position at which the redisplay_end_trigger hook should
1643 be run, if it is to be run at all. */
1644 if (MARKERP (w->redisplay_end_trigger)
1645 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1646 it->redisplay_end_trigger_charpos
1647 = marker_position (w->redisplay_end_trigger);
1648 else if (INTEGERP (w->redisplay_end_trigger))
1649 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1650
1651 /* Correct bogus values of tab_width. */
1652 it->tab_width = XINT (current_buffer->tab_width);
1653 if (it->tab_width <= 0 || it->tab_width > 1000)
1654 it->tab_width = 8;
1655
1656 /* Are lines in the display truncated? */
1657 it->truncate_lines_p
1658 = (base_face_id != DEFAULT_FACE_ID
1659 || XINT (it->w->hscroll)
1660 || (truncate_partial_width_windows
1661 && !WINDOW_FULL_WIDTH_P (it->w))
1662 || !NILP (current_buffer->truncate_lines));
1663
1664 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 1665 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
1666 frames. */
1667 if (!FRAME_WINDOW_P (it->f))
1668 {
1669 if (it->truncate_lines_p)
1670 {
1671 /* We will need the truncation glyph. */
1672 xassert (it->glyph_row == NULL);
1673 produce_special_glyphs (it, IT_TRUNCATION);
1674 it->truncation_pixel_width = it->pixel_width;
1675 }
1676 else
1677 {
1678 /* We will need the continuation glyph. */
1679 xassert (it->glyph_row == NULL);
1680 produce_special_glyphs (it, IT_CONTINUATION);
1681 it->continuation_pixel_width = it->pixel_width;
1682 }
1683
153c2160 1684 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
1685 above has changed them. */
1686 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1687 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1688 }
1689
1690 /* Set this after getting the dimensions of truncation and
1691 continuation glyphs, so that we don't produce glyphs when calling
1692 produce_special_glyphs, above. */
1693 it->glyph_row = row;
1694 it->area = TEXT_AREA;
1695
1696 /* Get the dimensions of the display area. The display area
1697 consists of the visible window area plus a horizontally scrolled
1698 part to the left of the window. All x-values are relative to the
1699 start of this total display area. */
1700 if (base_face_id != DEFAULT_FACE_ID)
1701 {
1702 /* Mode lines, menu bar in terminal frames. */
1703 it->first_visible_x = 0;
1704 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1705 }
1706 else
1707 {
1708 it->first_visible_x
1709 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1710 it->last_visible_x = (it->first_visible_x
1711 + window_box_width (w, TEXT_AREA));
1712
1713 /* If we truncate lines, leave room for the truncator glyph(s) at
1714 the right margin. Otherwise, leave room for the continuation
1715 glyph(s). Truncation and continuation glyphs are not inserted
1716 for window-based redisplay. */
1717 if (!FRAME_WINDOW_P (it->f))
1718 {
1719 if (it->truncate_lines_p)
1720 it->last_visible_x -= it->truncation_pixel_width;
1721 else
1722 it->last_visible_x -= it->continuation_pixel_width;
1723 }
1724
045dee35
GM
1725 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1726 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1727 }
1728
1729 /* Leave room for a border glyph. */
1730 if (!FRAME_WINDOW_P (it->f)
1731 && !WINDOW_RIGHTMOST_P (it->w))
1732 it->last_visible_x -= 1;
1733
1734 it->last_visible_y = window_text_bottom_y (w);
1735
1736 /* For mode lines and alike, arrange for the first glyph having a
1737 left box line if the face specifies a box. */
1738 if (base_face_id != DEFAULT_FACE_ID)
1739 {
1740 struct face *face;
2311178e 1741
5f5c8ee5
GM
1742 it->face_id = base_face_id;
1743
1744 /* If we have a boxed mode line, make the first character appear
1745 with a left box line. */
1746 face = FACE_FROM_ID (it->f, base_face_id);
1747 if (face->box != FACE_NO_BOX)
1748 it->start_of_box_run_p = 1;
1749 }
1750
1751 /* If a buffer position was specified, set the iterator there,
1752 getting overlays and face properties from that position. */
3ebf0ea9 1753 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
1754 {
1755 it->end_charpos = ZV;
1756 it->face_id = -1;
1757 IT_CHARPOS (*it) = charpos;
2311178e 1758
5f5c8ee5 1759 /* Compute byte position if not specified. */
3ebf0ea9 1760 if (bytepos < charpos)
5f5c8ee5
GM
1761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1762 else
1763 IT_BYTEPOS (*it) = bytepos;
1764
1765 /* Compute faces etc. */
1766 reseat (it, it->current.pos, 1);
1767 }
2311178e 1768
5f5c8ee5
GM
1769 CHECK_IT (it);
1770}
1771
1772
1773/* Initialize IT for the display of window W with window start POS. */
1774
1775void
1776start_display (it, w, pos)
1777 struct it *it;
1778 struct window *w;
1779 struct text_pos pos;
1780{
5f5c8ee5 1781 struct glyph_row *row;
045dee35 1782 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1783
1784 row = w->desired_matrix->rows + first_vpos;
1785 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
17fdcfc8
GM
1786
1787 if (!it->truncate_lines_p)
5f5c8ee5 1788 {
17fdcfc8
GM
1789 int start_at_line_beg_p;
1790 int first_y = it->current_y;
2311178e 1791
17fdcfc8
GM
1792 /* If window start is not at a line start, skip forward to POS to
1793 get the correct continuation lines width. */
1794 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1795 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1796 if (!start_at_line_beg_p)
5f5c8ee5 1797 {
0e47bbf7
RS
1798 int new_x;
1799
17fdcfc8
GM
1800 reseat_at_previous_visible_line_start (it);
1801 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1802
0e47bbf7
RS
1803 new_x = it->current_x + it->pixel_width;
1804
17fdcfc8
GM
1805 /* If lines are continued, this line may end in the middle
1806 of a multi-glyph character (e.g. a control character
1807 displayed as \003, or in the middle of an overlay
1808 string). In this case move_it_to above will not have
1809 taken us to the start of the continuation line but to the
1810 end of the continued line. */
0e47bbf7
RS
1811 if (it->current_x > 0
1812 && !it->truncate_lines_p /* Lines are continued. */
1813 && (/* And glyph doesn't fit on the line. */
1814 new_x > it->last_visible_x
1815 /* Or it fits exactly and we're on a window
1816 system frame. */
1817 || (new_x == it->last_visible_x
1818 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 1819 {
b28cb6ed
GM
1820 if (it->current.dpvec_index >= 0
1821 || it->current.overlay_string_index >= 0)
1822 {
cafafe0b 1823 set_iterator_to_next (it, 1);
b28cb6ed
GM
1824 move_it_in_display_line_to (it, -1, -1, 0);
1825 }
2311178e 1826
b28cb6ed 1827 it->continuation_lines_width += it->current_x;
5f5c8ee5 1828 }
b28cb6ed
GM
1829
1830 /* We're starting a new display line, not affected by the
1831 height of the continued line, so clear the appropriate
1832 fields in the iterator structure. */
1833 it->max_ascent = it->max_descent = 0;
1834 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 1835
17fdcfc8
GM
1836 it->current_y = first_y;
1837 it->vpos = 0;
1838 it->current_x = it->hpos = 0;
1839 }
5f5c8ee5
GM
1840 }
1841
1842#if 0 /* Don't assert the following because start_display is sometimes
1843 called intentionally with a window start that is not at a
1844 line start. Please leave this code in as a comment. */
2311178e 1845
5f5c8ee5
GM
1846 /* Window start should be on a line start, now. */
1847 xassert (it->continuation_lines_width
1848 || IT_CHARPOS (it) == BEGV
1849 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1850#endif /* 0 */
1851}
1852
1853
4bde0ebb
GM
1854/* Return 1 if POS is a position in ellipses displayed for invisible
1855 text. W is the window we display, for text property lookup. */
5f5c8ee5 1856
4bde0ebb
GM
1857static int
1858in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 1859 struct display_pos *pos;
4bde0ebb 1860 struct window *w;
5f5c8ee5 1861{
ac90c44f 1862 Lisp_Object prop, window;
4bde0ebb
GM
1863 int ellipses_p = 0;
1864 int charpos = CHARPOS (pos->pos);
2311178e 1865
ac90c44f
GM
1866 /* If POS specifies a position in a display vector, this might
1867 be for an ellipsis displayed for invisible text. We won't
1868 get the iterator set up for delivering that ellipsis unless
1869 we make sure that it gets aware of the invisible text. */
1870 if (pos->dpvec_index >= 0
1871 && pos->overlay_string_index < 0
1872 && CHARPOS (pos->string_pos) < 0
1873 && charpos > BEGV
1874 && (XSETWINDOW (window, w),
1875 prop = Fget_char_property (make_number (charpos),
1876 Qinvisible, window),
20fbd925 1877 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
1878 {
1879 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1880 window);
8580a4e3 1881 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
1882 }
1883
1884 return ellipses_p;
1885}
1886
1887
1888/* Initialize IT for stepping through current_buffer in window W,
1889 starting at position POS that includes overlay string and display
47d57b22
GM
1890 vector/ control character translation position information. Value
1891 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 1892
47d57b22 1893static int
4bde0ebb
GM
1894init_from_display_pos (it, w, pos)
1895 struct it *it;
1896 struct window *w;
1897 struct display_pos *pos;
1898{
1899 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 1900 int i, overlay_strings_with_newlines = 0;
2311178e 1901
4bde0ebb
GM
1902 /* If POS specifies a position in a display vector, this might
1903 be for an ellipsis displayed for invisible text. We won't
1904 get the iterator set up for delivering that ellipsis unless
1905 we make sure that it gets aware of the invisible text. */
1906 if (in_ellipses_for_invisible_text_p (pos, w))
1907 {
1908 --charpos;
1909 bytepos = 0;
ac90c44f 1910 }
2311178e 1911
5f5c8ee5
GM
1912 /* Keep in mind: the call to reseat in init_iterator skips invisible
1913 text, so we might end up at a position different from POS. This
1914 is only a problem when POS is a row start after a newline and an
1915 overlay starts there with an after-string, and the overlay has an
1916 invisible property. Since we don't skip invisible text in
1917 display_line and elsewhere immediately after consuming the
1918 newline before the row start, such a POS will not be in a string,
1919 but the call to init_iterator below will move us to the
1920 after-string. */
ac90c44f 1921 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 1922
47d57b22 1923 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 1924 {
50f80c2f
KR
1925 const char *s = SDATA (it->overlay_strings[i]);
1926 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 1927
1e1e5daf
GM
1928 while (s < e && *s != '\n')
1929 ++s;
1930
1931 if (s < e)
1932 {
1933 overlay_strings_with_newlines = 1;
1934 break;
1935 }
1936 }
47d57b22 1937
75c5350a
GM
1938 /* If position is within an overlay string, set up IT to the right
1939 overlay string. */
5f5c8ee5
GM
1940 if (pos->overlay_string_index >= 0)
1941 {
1942 int relative_index;
75c5350a
GM
1943
1944 /* If the first overlay string happens to have a `display'
1945 property for an image, the iterator will be set up for that
1946 image, and we have to undo that setup first before we can
1947 correct the overlay string index. */
1948 if (it->method == next_element_from_image)
1949 pop_it (it);
2311178e 1950
5f5c8ee5
GM
1951 /* We already have the first chunk of overlay strings in
1952 IT->overlay_strings. Load more until the one for
1953 pos->overlay_string_index is in IT->overlay_strings. */
1954 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1955 {
1956 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1957 it->current.overlay_string_index = 0;
1958 while (n--)
1959 {
5a08cbaf 1960 load_overlay_strings (it, 0);
5f5c8ee5
GM
1961 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1962 }
1963 }
2311178e 1964
5f5c8ee5
GM
1965 it->current.overlay_string_index = pos->overlay_string_index;
1966 relative_index = (it->current.overlay_string_index
1967 % OVERLAY_STRING_CHUNK_SIZE);
1968 it->string = it->overlay_strings[relative_index];
cafafe0b 1969 xassert (STRINGP (it->string));
5f5c8ee5
GM
1970 it->current.string_pos = pos->string_pos;
1971 it->method = next_element_from_string;
1972 }
2311178e 1973
1e1e5daf
GM
1974#if 0 /* This is bogus because POS not having an overlay string
1975 position does not mean it's after the string. Example: A
1976 line starting with a before-string and initialization of IT
1977 to the previous row's end position. */
2be8f184
GM
1978 else if (it->current.overlay_string_index >= 0)
1979 {
1980 /* If POS says we're already after an overlay string ending at
1981 POS, make sure to pop the iterator because it will be in
1982 front of that overlay string. When POS is ZV, we've thereby
1983 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1984 while (it->sp)
1985 pop_it (it);
2be8f184
GM
1986 it->current.overlay_string_index = -1;
1987 it->method = next_element_from_buffer;
1988 if (CHARPOS (pos->pos) == ZV)
1989 it->overlay_strings_at_end_processed_p = 1;
1990 }
1e1e5daf 1991#endif /* 0 */
2311178e 1992
2be8f184 1993 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1994 {
1995 /* Recorded position is not in an overlay string, but in another
1996 string. This can only be a string from a `display' property.
1997 IT should already be filled with that string. */
1998 it->current.string_pos = pos->string_pos;
1999 xassert (STRINGP (it->string));
2000 }
2001
ac90c44f
GM
2002 /* Restore position in display vector translations, control
2003 character translations or ellipses. */
5f5c8ee5
GM
2004 if (pos->dpvec_index >= 0)
2005 {
ac90c44f
GM
2006 if (it->dpvec == NULL)
2007 get_next_display_element (it);
5f5c8ee5
GM
2008 xassert (it->dpvec && it->current.dpvec_index == 0);
2009 it->current.dpvec_index = pos->dpvec_index;
2010 }
2311178e 2011
5f5c8ee5 2012 CHECK_IT (it);
47d57b22 2013 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2014}
2015
2016
2017/* Initialize IT for stepping through current_buffer in window W
2018 starting at ROW->start. */
2019
2020static void
2021init_to_row_start (it, w, row)
2022 struct it *it;
2023 struct window *w;
2024 struct glyph_row *row;
2025{
2026 init_from_display_pos (it, w, &row->start);
2027 it->continuation_lines_width = row->continuation_lines_width;
2028 CHECK_IT (it);
2029}
2030
2311178e 2031
5f5c8ee5 2032/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2033 starting in the line following ROW, i.e. starting at ROW->end.
2034 Value is zero if there are overlay strings with newlines at ROW's
2035 end position. */
5f5c8ee5 2036
47d57b22 2037static int
5f5c8ee5
GM
2038init_to_row_end (it, w, row)
2039 struct it *it;
2040 struct window *w;
2041 struct glyph_row *row;
2042{
47d57b22 2043 int success = 0;
2311178e 2044
47d57b22
GM
2045 if (init_from_display_pos (it, w, &row->end))
2046 {
2047 if (row->continued_p)
2048 it->continuation_lines_width
2049 = row->continuation_lines_width + row->pixel_width;
2050 CHECK_IT (it);
2051 success = 1;
2052 }
2311178e 2053
47d57b22 2054 return success;
5f5c8ee5
GM
2055}
2056
2057
2058
2059\f
2060/***********************************************************************
2061 Text properties
2062 ***********************************************************************/
2063
2064/* Called when IT reaches IT->stop_charpos. Handle text property and
2065 overlay changes. Set IT->stop_charpos to the next position where
2066 to stop. */
2067
2068static void
2069handle_stop (it)
2070 struct it *it;
2071{
2072 enum prop_handled handled;
2073 int handle_overlay_change_p = 1;
2074 struct props *p;
2075
2076 it->dpvec = NULL;
2077 it->current.dpvec_index = -1;
2078
2079 do
2080 {
2081 handled = HANDLED_NORMALLY;
2311178e 2082
5f5c8ee5
GM
2083 /* Call text property handlers. */
2084 for (p = it_props; p->handler; ++p)
2085 {
2086 handled = p->handler (it);
2970b9be 2087
5f5c8ee5
GM
2088 if (handled == HANDLED_RECOMPUTE_PROPS)
2089 break;
2090 else if (handled == HANDLED_RETURN)
2091 return;
2092 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2093 handle_overlay_change_p = 0;
2094 }
2095
2096 if (handled != HANDLED_RECOMPUTE_PROPS)
2097 {
2098 /* Don't check for overlay strings below when set to deliver
2099 characters from a display vector. */
2100 if (it->method == next_element_from_display_vector)
2101 handle_overlay_change_p = 0;
2102
2103 /* Handle overlay changes. */
2104 if (handle_overlay_change_p)
2105 handled = handle_overlay_change (it);
2311178e 2106
5f5c8ee5
GM
2107 /* Determine where to stop next. */
2108 if (handled == HANDLED_NORMALLY)
2109 compute_stop_pos (it);
2110 }
2111 }
2112 while (handled == HANDLED_RECOMPUTE_PROPS);
2113}
2114
2115
2116/* Compute IT->stop_charpos from text property and overlay change
2117 information for IT's current position. */
2118
2119static void
2120compute_stop_pos (it)
2121 struct it *it;
2122{
2123 register INTERVAL iv, next_iv;
2124 Lisp_Object object, limit, position;
2125
2126 /* If nowhere else, stop at the end. */
2127 it->stop_charpos = it->end_charpos;
2311178e 2128
5f5c8ee5
GM
2129 if (STRINGP (it->string))
2130 {
2131 /* Strings are usually short, so don't limit the search for
2132 properties. */
2133 object = it->string;
2134 limit = Qnil;
ac90c44f 2135 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2136 }
2137 else
2138 {
2139 int charpos;
2140
2141 /* If next overlay change is in front of the current stop pos
2142 (which is IT->end_charpos), stop there. Note: value of
2143 next_overlay_change is point-max if no overlay change
2144 follows. */
2145 charpos = next_overlay_change (IT_CHARPOS (*it));
2146 if (charpos < it->stop_charpos)
2147 it->stop_charpos = charpos;
2148
2149 /* If showing the region, we have to stop at the region
2150 start or end because the face might change there. */
2151 if (it->region_beg_charpos > 0)
2152 {
2153 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2154 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2155 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2156 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2157 }
2311178e 2158
5f5c8ee5
GM
2159 /* Set up variables for computing the stop position from text
2160 property changes. */
2161 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2162 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2163 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2164
2165 }
2166
2167 /* Get the interval containing IT's position. Value is a null
2168 interval if there isn't such an interval. */
2169 iv = validate_interval_range (object, &position, &position, 0);
2170 if (!NULL_INTERVAL_P (iv))
2171 {
2172 Lisp_Object values_here[LAST_PROP_IDX];
2173 struct props *p;
2174
2175 /* Get properties here. */
2176 for (p = it_props; p->handler; ++p)
2177 values_here[p->idx] = textget (iv->plist, *p->name);
2178
2179 /* Look for an interval following iv that has different
2180 properties. */
2181 for (next_iv = next_interval (iv);
2182 (!NULL_INTERVAL_P (next_iv)
2183 && (NILP (limit)
2184 || XFASTINT (limit) > next_iv->position));
2185 next_iv = next_interval (next_iv))
2186 {
2187 for (p = it_props; p->handler; ++p)
2188 {
2189 Lisp_Object new_value;
2190
2191 new_value = textget (next_iv->plist, *p->name);
2192 if (!EQ (values_here[p->idx], new_value))
2193 break;
2194 }
2311178e 2195
5f5c8ee5
GM
2196 if (p->handler)
2197 break;
2198 }
2199
2200 if (!NULL_INTERVAL_P (next_iv))
2201 {
2202 if (INTEGERP (limit)
2203 && next_iv->position >= XFASTINT (limit))
2204 /* No text property change up to limit. */
2205 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2206 else
2207 /* Text properties change in next_iv. */
2208 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2209 }
2210 }
2211
2212 xassert (STRINGP (it->string)
2213 || (it->stop_charpos >= BEGV
2214 && it->stop_charpos >= IT_CHARPOS (*it)));
2215}
2216
2217
2218/* Return the position of the next overlay change after POS in
2219 current_buffer. Value is point-max if no overlay change
2220 follows. This is like `next-overlay-change' but doesn't use
2221 xmalloc. */
2222
2223static int
2224next_overlay_change (pos)
2225 int pos;
2226{
2227 int noverlays;
2228 int endpos;
2229 Lisp_Object *overlays;
2230 int len;
2231 int i;
2232
2233 /* Get all overlays at the given position. */
2234 len = 10;
2235 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2236 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2237 if (noverlays > len)
2238 {
2239 len = noverlays;
2240 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2241 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2242 }
2243
2244 /* If any of these overlays ends before endpos,
2245 use its ending point instead. */
2246 for (i = 0; i < noverlays; ++i)
2247 {
2248 Lisp_Object oend;
2249 int oendpos;
2250
2251 oend = OVERLAY_END (overlays[i]);
2252 oendpos = OVERLAY_POSITION (oend);
2253 endpos = min (endpos, oendpos);
2254 }
2255
2256 return endpos;
2257}
2258
2259
2260\f
2261/***********************************************************************
2262 Fontification
2263 ***********************************************************************/
2264
2265/* Handle changes in the `fontified' property of the current buffer by
2266 calling hook functions from Qfontification_functions to fontify
2267 regions of text. */
2268
2269static enum prop_handled
2270handle_fontified_prop (it)
2271 struct it *it;
2272{
2273 Lisp_Object prop, pos;
2274 enum prop_handled handled = HANDLED_NORMALLY;
2275
2276 /* Get the value of the `fontified' property at IT's current buffer
2277 position. (The `fontified' property doesn't have a special
2278 meaning in strings.) If the value is nil, call functions from
2279 Qfontification_functions. */
2280 if (!STRINGP (it->string)
2281 && it->s == NULL
2282 && !NILP (Vfontification_functions)
085536c2 2283 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2284 && (pos = make_number (IT_CHARPOS (*it)),
2285 prop = Fget_char_property (pos, Qfontified, Qnil),
2286 NILP (prop)))
2287 {
331379bf 2288 int count = SPECPDL_INDEX ();
085536c2
GM
2289 Lisp_Object val;
2290
2291 val = Vfontification_functions;
2292 specbind (Qfontification_functions, Qnil);
2311178e 2293
085536c2 2294 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2295 safe_call1 (val, pos);
085536c2
GM
2296 else
2297 {
2298 Lisp_Object globals, fn;
2299 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2300
085536c2
GM
2301 globals = Qnil;
2302 GCPRO2 (val, globals);
2311178e 2303
085536c2
GM
2304 for (; CONSP (val); val = XCDR (val))
2305 {
2306 fn = XCAR (val);
2311178e 2307
085536c2
GM
2308 if (EQ (fn, Qt))
2309 {
2310 /* A value of t indicates this hook has a local
2311 binding; it means to run the global binding too.
2312 In a global value, t should not occur. If it
2313 does, we must ignore it to avoid an endless
2314 loop. */
2315 for (globals = Fdefault_value (Qfontification_functions);
2316 CONSP (globals);
2317 globals = XCDR (globals))
2318 {
2319 fn = XCAR (globals);
2320 if (!EQ (fn, Qt))
116d6f5c 2321 safe_call1 (fn, pos);
085536c2
GM
2322 }
2323 }
2324 else
116d6f5c 2325 safe_call1 (fn, pos);
085536c2
GM
2326 }
2327
2328 UNGCPRO;
2329 }
2330
2331 unbind_to (count, Qnil);
5f5c8ee5
GM
2332
2333 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2334 something. This avoids an endless loop if they failed to
2335 fontify the text for which reason ever. */
2336 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2337 handled = HANDLED_RECOMPUTE_PROPS;
2338 }
2339
2340 return handled;
2341}
2342
2343
2344\f
2345/***********************************************************************
2346 Faces
2347 ***********************************************************************/
2348
2349/* Set up iterator IT from face properties at its current position.
2350 Called from handle_stop. */
2351
2352static enum prop_handled
2353handle_face_prop (it)
2354 struct it *it;
2355{
2356 int new_face_id, next_stop;
2311178e 2357
5f5c8ee5
GM
2358 if (!STRINGP (it->string))
2359 {
2360 new_face_id
2361 = face_at_buffer_position (it->w,
2362 IT_CHARPOS (*it),
2363 it->region_beg_charpos,
2364 it->region_end_charpos,
2365 &next_stop,
2366 (IT_CHARPOS (*it)
2367 + TEXT_PROP_DISTANCE_LIMIT),
2368 0);
2311178e 2369
5f5c8ee5
GM
2370 /* Is this a start of a run of characters with box face?
2371 Caveat: this can be called for a freshly initialized
4b41cebb 2372 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2373 face will not change until limit, i.e. if the new face has a
2374 box, all characters up to limit will have one. But, as
2375 usual, we don't know whether limit 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);
2311178e 2379
5f5c8ee5
GM
2380 /* If new face has a box but old face has not, this is
2381 the start of a run of characters with box, i.e. it has
2382 a shadow on the left side. The value of face_id of the
2383 iterator will be -1 if this is the initial call that gets
2384 the face. In this case, we have to look in front of IT's
2385 position and see whether there is a face != new_face_id. */
2386 it->start_of_box_run_p
2387 = (new_face->box != FACE_NO_BOX
2388 && (it->face_id >= 0
2389 || IT_CHARPOS (*it) == BEG
2390 || new_face_id != face_before_it_pos (it)));
2391 it->face_box_p = new_face->box != FACE_NO_BOX;
2392 }
2393 }
2394 else
2395 {
06a12811
GM
2396 int base_face_id, bufpos;
2397
2398 if (it->current.overlay_string_index >= 0)
2399 bufpos = IT_CHARPOS (*it);
2400 else
2401 bufpos = 0;
2311178e 2402
06a12811
GM
2403 /* For strings from a buffer, i.e. overlay strings or strings
2404 from a `display' property, use the face at IT's current
2405 buffer position as the base face to merge with, so that
2406 overlay strings appear in the same face as surrounding
2407 text, unless they specify their own faces. */
2408 base_face_id = underlying_face_id (it);
2311178e 2409
06a12811
GM
2410 new_face_id = face_at_string_position (it->w,
2411 it->string,
2412 IT_STRING_CHARPOS (*it),
2413 bufpos,
2414 it->region_beg_charpos,
2415 it->region_end_charpos,
2416 &next_stop,
5de7c6f2 2417 base_face_id, 0);
2311178e 2418
5f5c8ee5
GM
2419#if 0 /* This shouldn't be neccessary. Let's check it. */
2420 /* If IT is used to display a mode line we would really like to
2421 use the mode line face instead of the frame's default face. */
2422 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2423 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2424 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 2425#endif
2311178e 2426
5f5c8ee5
GM
2427 /* Is this a start of a run of characters with box? Caveat:
2428 this can be called for a freshly allocated iterator; face_id
2429 is -1 is this case. We know that the new face will not
2430 change until the next check pos, i.e. if the new face has a
2431 box, all characters up to that position will have a
2432 box. But, as usual, we don't know whether that position
2433 is really the end. */
2434 if (new_face_id != it->face_id)
2435 {
2436 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2437 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 2438
5f5c8ee5
GM
2439 /* If new face has a box but old face hasn't, this is the
2440 start of a run of characters with box, i.e. it has a
2441 shadow on the left side. */
2442 it->start_of_box_run_p
2443 = new_face->box && (old_face == NULL || !old_face->box);
2444 it->face_box_p = new_face->box != FACE_NO_BOX;
2445 }
2446 }
2311178e 2447
5f5c8ee5 2448 it->face_id = new_face_id;
5f5c8ee5
GM
2449 return HANDLED_NORMALLY;
2450}
2451
2452
06a12811
GM
2453/* Return the ID of the face ``underlying'' IT's current position,
2454 which is in a string. If the iterator is associated with a
2455 buffer, return the face at IT's current buffer position.
2456 Otherwise, use the iterator's base_face_id. */
2457
2458static int
2459underlying_face_id (it)
2460 struct it *it;
2461{
2462 int face_id = it->base_face_id, i;
2463
2464 xassert (STRINGP (it->string));
2465
2466 for (i = it->sp - 1; i >= 0; --i)
2467 if (NILP (it->stack[i].string))
2468 face_id = it->stack[i].face_id;
2469
2470 return face_id;
2471}
2472
2473
5f5c8ee5
GM
2474/* Compute the face one character before or after the current position
2475 of IT. BEFORE_P non-zero means get the face in front of IT's
2476 position. Value is the id of the face. */
2477
2478static int
2479face_before_or_after_it_pos (it, before_p)
2480 struct it *it;
2481 int before_p;
2482{
2483 int face_id, limit;
2484 int next_check_charpos;
2485 struct text_pos pos;
2486
2487 xassert (it->s == NULL);
2311178e 2488
5f5c8ee5
GM
2489 if (STRINGP (it->string))
2490 {
06a12811 2491 int bufpos, base_face_id;
2311178e 2492
5f5c8ee5
GM
2493 /* No face change past the end of the string (for the case
2494 we are padding with spaces). No face change before the
2495 string start. */
2051c264 2496 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2497 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2498 return it->face_id;
2499
2500 /* Set pos to the position before or after IT's current position. */
2501 if (before_p)
2502 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2503 else
260a86a0
KH
2504 /* For composition, we must check the character after the
2505 composition. */
2506 pos = (it->what == IT_COMPOSITION
2507 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2508 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2509
06a12811
GM
2510 if (it->current.overlay_string_index >= 0)
2511 bufpos = IT_CHARPOS (*it);
2512 else
2513 bufpos = 0;
2514
2515 base_face_id = underlying_face_id (it);
2516
5f5c8ee5 2517 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2518 face_id = face_at_string_position (it->w,
2519 it->string,
2520 CHARPOS (pos),
2521 bufpos,
2522 it->region_beg_charpos,
2523 it->region_end_charpos,
2524 &next_check_charpos,
5de7c6f2 2525 base_face_id, 0);
5f5c8ee5
GM
2526
2527 /* Correct the face for charsets different from ASCII. Do it
2528 for the multibyte case only. The face returned above is
2529 suitable for unibyte text if IT->string is unibyte. */
2530 if (STRING_MULTIBYTE (it->string))
2531 {
50f80c2f 2532 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 2533 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
2534 int c, len;
2535 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 2536
4fdb80f2 2537 c = string_char_and_length (p, rest, &len);
980806b6 2538 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2539 }
2540 }
2541 else
2542 {
70851746
GM
2543 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2544 || (IT_CHARPOS (*it) <= BEGV && before_p))
2545 return it->face_id;
2311178e 2546
5f5c8ee5
GM
2547 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2548 pos = it->current.pos;
2311178e 2549
5f5c8ee5 2550 if (before_p)
c1005d06 2551 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2552 else
260a86a0
KH
2553 {
2554 if (it->what == IT_COMPOSITION)
2555 /* For composition, we must check the position after the
2556 composition. */
2557 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2558 else
c1005d06 2559 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2560 }
2311178e 2561
5f5c8ee5
GM
2562 /* Determine face for CHARSET_ASCII, or unibyte. */
2563 face_id = face_at_buffer_position (it->w,
2564 CHARPOS (pos),
2565 it->region_beg_charpos,
2566 it->region_end_charpos,
2567 &next_check_charpos,
2568 limit, 0);
2569
2570 /* Correct the face for charsets different from ASCII. Do it
2571 for the multibyte case only. The face returned above is
2572 suitable for unibyte text if current_buffer is unibyte. */
2573 if (it->multibyte_p)
2574 {
1d1b6e6a 2575 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
2576 struct face *face = FACE_FROM_ID (it->f, face_id);
2577 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2578 }
2579 }
2311178e 2580
5f5c8ee5
GM
2581 return face_id;
2582}
2583
2584
2585\f
2586/***********************************************************************
2587 Invisible text
2588 ***********************************************************************/
2589
2590/* Set up iterator IT from invisible properties at its current
2591 position. Called from handle_stop. */
2592
2593static enum prop_handled
2594handle_invisible_prop (it)
2595 struct it *it;
2596{
2597 enum prop_handled handled = HANDLED_NORMALLY;
2598
2599 if (STRINGP (it->string))
2600 {
2601 extern Lisp_Object Qinvisible;
2602 Lisp_Object prop, end_charpos, limit, charpos;
2603
2604 /* Get the value of the invisible text property at the
2605 current position. Value will be nil if there is no such
2606 property. */
ac90c44f 2607 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2608 prop = Fget_text_property (charpos, Qinvisible, it->string);
2609
eadc0bf8
GM
2610 if (!NILP (prop)
2611 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2612 {
2613 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 2614
5f5c8ee5
GM
2615 /* Get the position at which the next change of the
2616 invisible text property can be found in IT->string.
2617 Value will be nil if the property value is the same for
2618 all the rest of IT->string. */
2051c264 2619 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 2620 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 2621 it->string, limit);
2311178e 2622
5f5c8ee5
GM
2623 /* Text at current position is invisible. The next
2624 change in the property is at position end_charpos.
2625 Move IT's current position to that position. */
2626 if (INTEGERP (end_charpos)
2627 && XFASTINT (end_charpos) < XFASTINT (limit))
2628 {
2629 struct text_pos old;
2630 old = it->current.string_pos;
2631 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2632 compute_string_pos (&it->current.string_pos, old, it->string);
2633 }
2634 else
2635 {
2636 /* The rest of the string is invisible. If this is an
2637 overlay string, proceed with the next overlay string
2638 or whatever comes and return a character from there. */
2639 if (it->current.overlay_string_index >= 0)
2640 {
2641 next_overlay_string (it);
2642 /* Don't check for overlay strings when we just
2643 finished processing them. */
2644 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2645 }
2646 else
2647 {
2051c264
GM
2648 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2649 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
2650 }
2651 }
2652 }
2653 }
2654 else
2655 {
8580a4e3 2656 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 2657 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
2658
2659 /* First of all, is there invisible text at this position? */
5a08cbaf 2660 start_charpos = IT_CHARPOS (*it);
ac90c44f 2661 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
2662 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2663 &overlay);
8580a4e3
SM
2664 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2665
5f5c8ee5 2666 /* If we are on invisible text, skip over it. */
8580a4e3 2667 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2668 {
2669 /* Record whether we have to display an ellipsis for the
2670 invisible text. */
8580a4e3 2671 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
2672
2673 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 2674
5f5c8ee5
GM
2675 /* Loop skipping over invisible text. The loop is left at
2676 ZV or with IT on the first char being visible again. */
2677 do
2678 {
2679 /* Try to skip some invisible text. Return value is the
2680 position reached which can be equal to IT's position
2681 if there is nothing invisible here. This skips both
2682 over invisible text properties and overlays with
2683 invisible property. */
2684 newpos = skip_invisible (IT_CHARPOS (*it),
2685 &next_stop, ZV, it->window);
2686
2687 /* If we skipped nothing at all we weren't at invisible
2688 text in the first place. If everything to the end of
2689 the buffer was skipped, end the loop. */
2690 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 2691 invis_p = 0;
5f5c8ee5
GM
2692 else
2693 {
2694 /* We skipped some characters but not necessarily
2695 all there are. Check if we ended up on visible
2696 text. Fget_char_property returns the property of
2697 the char before the given position, i.e. if we
8580a4e3 2698 get invis_p = 0, this means that the char at
5f5c8ee5 2699 newpos is visible. */
ac90c44f 2700 pos = make_number (newpos);
5f5c8ee5 2701 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 2702 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 2703 }
2311178e 2704
5f5c8ee5
GM
2705 /* If we ended up on invisible text, proceed to
2706 skip starting with next_stop. */
8580a4e3 2707 if (invis_p)
5f5c8ee5
GM
2708 IT_CHARPOS (*it) = next_stop;
2709 }
8580a4e3 2710 while (invis_p);
5a08cbaf 2711
5f5c8ee5
GM
2712 /* The position newpos is now either ZV or on visible text. */
2713 IT_CHARPOS (*it) = newpos;
2714 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 2715
5a08cbaf
GM
2716 /* If there are before-strings at the start of invisible
2717 text, and the text is invisible because of a text
2718 property, arrange to show before-strings because 20.x did
2719 it that way. (If the text is invisible because of an
2720 overlay property instead of a text property, this is
2721 already handled in the overlay code.) */
2722 if (NILP (overlay)
2723 && get_overlay_strings (it, start_charpos))
5f5c8ee5 2724 {
5a08cbaf
GM
2725 handled = HANDLED_RECOMPUTE_PROPS;
2726 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 2727 }
5a08cbaf
GM
2728 else if (display_ellipsis_p)
2729 setup_for_ellipsis (it);
5f5c8ee5
GM
2730 }
2731 }
2732
2733 return handled;
2734}
2735
2736
5a08cbaf
GM
2737/* Make iterator IT return `...' next. */
2738
2739static void
2740setup_for_ellipsis (it)
2741 struct it *it;
2742{
2311178e 2743 if (it->dp
5a08cbaf
GM
2744 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2745 {
2746 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2747 it->dpvec = v->contents;
2748 it->dpend = v->contents + v->size;
2749 }
2311178e 2750 else
5a08cbaf
GM
2751 {
2752 /* Default `...'. */
2753 it->dpvec = default_invis_vector;
2754 it->dpend = default_invis_vector + 3;
2755 }
2311178e 2756
5a08cbaf
GM
2757 /* The ellipsis display does not replace the display of the
2758 character at the new position. Indicate this by setting
2759 IT->dpvec_char_len to zero. */
2760 it->dpvec_char_len = 0;
2311178e 2761
5a08cbaf
GM
2762 it->current.dpvec_index = 0;
2763 it->method = next_element_from_display_vector;
2764}
2765
2766
5f5c8ee5
GM
2767\f
2768/***********************************************************************
2769 'display' property
2770 ***********************************************************************/
2771
2772/* Set up iterator IT from `display' property at its current position.
2773 Called from handle_stop. */
2774
2775static enum prop_handled
2776handle_display_prop (it)
2777 struct it *it;
2778{
2779 Lisp_Object prop, object;
2780 struct text_pos *position;
a61b7058 2781 int display_replaced_p = 0;
5f5c8ee5
GM
2782
2783 if (STRINGP (it->string))
2784 {
2785 object = it->string;
2786 position = &it->current.string_pos;
2787 }
2788 else
2789 {
221dd3e7 2790 object = it->w->buffer;
5f5c8ee5
GM
2791 position = &it->current.pos;
2792 }
2793
2794 /* Reset those iterator values set from display property values. */
2795 it->font_height = Qnil;
2796 it->space_width = Qnil;
2797 it->voffset = 0;
2798
2799 /* We don't support recursive `display' properties, i.e. string
2800 values that have a string `display' property, that have a string
2801 `display' property etc. */
2802 if (!it->string_from_display_prop_p)
2803 it->area = TEXT_AREA;
2804
2805 prop = Fget_char_property (make_number (position->charpos),
2806 Qdisplay, object);
2807 if (NILP (prop))
2808 return HANDLED_NORMALLY;
2809
f3751a65 2810 if (CONSP (prop)
12700f40
GM
2811 /* Simple properties. */
2812 && !EQ (XCAR (prop), Qimage)
2813 && !EQ (XCAR (prop), Qspace)
2814 && !EQ (XCAR (prop), Qwhen)
2815 && !EQ (XCAR (prop), Qspace_width)
2816 && !EQ (XCAR (prop), Qheight)
2817 && !EQ (XCAR (prop), Qraise)
2818 /* Marginal area specifications. */
2819 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2820 && !NILP (XCAR (prop)))
5f5c8ee5 2821 {
a61b7058 2822 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 2823 {
a61b7058
GM
2824 if (handle_single_display_prop (it, XCAR (prop), object,
2825 position, display_replaced_p))
2826 display_replaced_p = 1;
5f5c8ee5
GM
2827 }
2828 }
2829 else if (VECTORP (prop))
2830 {
2831 int i;
a61b7058
GM
2832 for (i = 0; i < ASIZE (prop); ++i)
2833 if (handle_single_display_prop (it, AREF (prop, i), object,
2834 position, display_replaced_p))
2835 display_replaced_p = 1;
5f5c8ee5
GM
2836 }
2837 else
2838 {
a61b7058
GM
2839 if (handle_single_display_prop (it, prop, object, position, 0))
2840 display_replaced_p = 1;
5f5c8ee5
GM
2841 }
2842
a61b7058 2843 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
2844}
2845
2846
6c577098 2847/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2848 at START_POS in OBJECT. */
2849
2850static struct text_pos
2851display_prop_end (it, object, start_pos)
2852 struct it *it;
2853 Lisp_Object object;
2854 struct text_pos start_pos;
2855{
2856 Lisp_Object end;
2857 struct text_pos end_pos;
5f5c8ee5 2858
016b5642
MB
2859 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2860 Qdisplay, object, Qnil);
6c577098
GM
2861 CHARPOS (end_pos) = XFASTINT (end);
2862 if (STRINGP (object))
5f5c8ee5
GM
2863 compute_string_pos (&end_pos, start_pos, it->string);
2864 else
6c577098 2865 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2866
2867 return end_pos;
2868}
2869
2870
2871/* Set up IT from a single `display' sub-property value PROP. OBJECT
2872 is the object in which the `display' property was found. *POSITION
a61b7058
GM
2873 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2874 means that we previously saw a display sub-property which already
2875 replaced text display with something else, for example an image;
2876 ignore such properties after the first one has been processed.
5f5c8ee5
GM
2877
2878 If PROP is a `space' or `image' sub-property, set *POSITION to the
2879 end position of the `display' property.
2880
4b41cebb 2881 Value is non-zero if something was found which replaces the display
a61b7058 2882 of buffer or string text. */
5f5c8ee5
GM
2883
2884static int
a61b7058
GM
2885handle_single_display_prop (it, prop, object, position,
2886 display_replaced_before_p)
5f5c8ee5
GM
2887 struct it *it;
2888 Lisp_Object prop;
2889 Lisp_Object object;
2890 struct text_pos *position;
a61b7058 2891 int display_replaced_before_p;
5f5c8ee5
GM
2892{
2893 Lisp_Object value;
a61b7058 2894 int replaces_text_display_p = 0;
5f5c8ee5
GM
2895 Lisp_Object form;
2896
d3acf96b 2897 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 2898 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2899 form = Qt;
d3acf96b 2900 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2901 {
2902 prop = XCDR (prop);
2903 if (!CONSP (prop))
2904 return 0;
2905 form = XCAR (prop);
2906 prop = XCDR (prop);
5f5c8ee5
GM
2907 }
2908
2909 if (!NILP (form) && !EQ (form, Qt))
2910 {
331379bf 2911 int count = SPECPDL_INDEX ();
5f5c8ee5 2912 struct gcpro gcpro1;
5f5c8ee5 2913
b384d6f8
GM
2914 /* Bind `object' to the object having the `display' property, a
2915 buffer or string. Bind `position' to the position in the
2916 object where the property was found, and `buffer-position'
2917 to the current position in the buffer. */
2918 specbind (Qobject, object);
31ac723b
SM
2919 specbind (Qposition, make_number (CHARPOS (*position)));
2920 specbind (Qbuffer_position,
2921 make_number (STRINGP (object)
2922 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 2923 GCPRO1 (form);
116d6f5c 2924 form = safe_eval (form);
b384d6f8
GM
2925 UNGCPRO;
2926 unbind_to (count, Qnil);
5f5c8ee5 2927 }
2311178e 2928
5f5c8ee5
GM
2929 if (NILP (form))
2930 return 0;
2931
2932 if (CONSP (prop)
2933 && EQ (XCAR (prop), Qheight)
2934 && CONSP (XCDR (prop)))
2935 {
e4093f38 2936 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 2937 return 0;
2311178e 2938
5f5c8ee5
GM
2939 /* `(height HEIGHT)'. */
2940 it->font_height = XCAR (XCDR (prop));
2941 if (!NILP (it->font_height))
2942 {
2943 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2944 int new_height = -1;
2945
2946 if (CONSP (it->font_height)
2947 && (EQ (XCAR (it->font_height), Qplus)
2948 || EQ (XCAR (it->font_height), Qminus))
2949 && CONSP (XCDR (it->font_height))
2950 && INTEGERP (XCAR (XCDR (it->font_height))))
2951 {
2952 /* `(+ N)' or `(- N)' where N is an integer. */
2953 int steps = XINT (XCAR (XCDR (it->font_height)));
2954 if (EQ (XCAR (it->font_height), Qplus))
2955 steps = - steps;
2956 it->face_id = smaller_face (it->f, it->face_id, steps);
2957 }
0d8b31c0 2958 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2959 {
2960 /* Call function with current height as argument.
2961 Value is the new height. */
116d6f5c
GM
2962 Lisp_Object height;
2963 height = safe_call1 (it->font_height,
2964 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2965 if (NUMBERP (height))
2966 new_height = XFLOATINT (height);
5f5c8ee5
GM
2967 }
2968 else if (NUMBERP (it->font_height))
2969 {
2970 /* Value is a multiple of the canonical char height. */
2971 struct face *face;
2311178e 2972
5f5c8ee5
GM
2973 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2974 new_height = (XFLOATINT (it->font_height)
2975 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2976 }
2977 else
2978 {
2979 /* Evaluate IT->font_height with `height' bound to the
2980 current specified height to get the new height. */
2981 Lisp_Object value;
331379bf 2982 int count = SPECPDL_INDEX ();
2311178e 2983
5f5c8ee5 2984 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2985 value = safe_eval (it->font_height);
5f5c8ee5 2986 unbind_to (count, Qnil);
2311178e 2987
5f5c8ee5
GM
2988 if (NUMBERP (value))
2989 new_height = XFLOATINT (value);
2990 }
2311178e 2991
5f5c8ee5
GM
2992 if (new_height > 0)
2993 it->face_id = face_with_height (it->f, it->face_id, new_height);
2994 }
2995 }
2996 else if (CONSP (prop)
2997 && EQ (XCAR (prop), Qspace_width)
2998 && CONSP (XCDR (prop)))
2999 {
3000 /* `(space_width WIDTH)'. */
e4093f38 3001 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3002 return 0;
2311178e 3003
5f5c8ee5
GM
3004 value = XCAR (XCDR (prop));
3005 if (NUMBERP (value) && XFLOATINT (value) > 0)
3006 it->space_width = value;
3007 }
3008 else if (CONSP (prop)
3009 && EQ (XCAR (prop), Qraise)
3010 && CONSP (XCDR (prop)))
3011 {
5f5c8ee5 3012 /* `(raise FACTOR)'. */
e4093f38 3013 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3014 return 0;
2311178e 3015
fb3842a8 3016#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
3017 value = XCAR (XCDR (prop));
3018 if (NUMBERP (value))
3019 {
3020 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3021 it->voffset = - (XFLOATINT (value)
1ab3e082 3022 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3023 }
3024#endif /* HAVE_WINDOW_SYSTEM */
3025 }
3026 else if (!it->string_from_display_prop_p)
3027 {
f3751a65 3028 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 3029 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
3030 Lisp_Object location, value;
3031 struct text_pos start_pos;
3032 int valid_p;
3033
3034 /* Characters having this form of property are not displayed, so
3035 we have to find the end of the property. */
5f5c8ee5
GM
3036 start_pos = *position;
3037 *position = display_prop_end (it, object, start_pos);
15e26c76 3038 value = Qnil;
5f5c8ee5
GM
3039
3040 /* Let's stop at the new position and assume that all
3041 text properties change there. */
3042 it->stop_charpos = position->charpos;
3043
f3751a65
GM
3044 location = Qunbound;
3045 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3046 {
f3751a65 3047 Lisp_Object tem;
2311178e 3048
5f5c8ee5 3049 value = XCDR (prop);
f3751a65
GM
3050 if (CONSP (value))
3051 value = XCAR (value);
3052
3053 tem = XCAR (prop);
3054 if (EQ (XCAR (tem), Qmargin)
3055 && (tem = XCDR (tem),
3056 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3057 (NILP (tem)
3058 || EQ (tem, Qleft_margin)
3059 || EQ (tem, Qright_margin))))
3060 location = tem;
5f5c8ee5 3061 }
f3751a65
GM
3062
3063 if (EQ (location, Qunbound))
5f5c8ee5
GM
3064 {
3065 location = Qnil;
3066 value = prop;
3067 }
3068
3069#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 3070 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
3071 valid_p = STRINGP (value);
3072 else
3073 valid_p = (STRINGP (value)
3074 || (CONSP (value) && EQ (XCAR (value), Qspace))
3075 || valid_image_p (value));
3076#else /* not HAVE_WINDOW_SYSTEM */
3077 valid_p = STRINGP (value);
3078#endif /* not HAVE_WINDOW_SYSTEM */
2311178e 3079
5f5c8ee5
GM
3080 if ((EQ (location, Qleft_margin)
3081 || EQ (location, Qright_margin)
3082 || NILP (location))
a61b7058
GM
3083 && valid_p
3084 && !display_replaced_before_p)
5f5c8ee5 3085 {
a61b7058 3086 replaces_text_display_p = 1;
2311178e 3087
5f5c8ee5
GM
3088 /* Save current settings of IT so that we can restore them
3089 when we are finished with the glyph property value. */
3090 push_it (it);
2311178e 3091
5f5c8ee5
GM
3092 if (NILP (location))
3093 it->area = TEXT_AREA;
3094 else if (EQ (location, Qleft_margin))
3095 it->area = LEFT_MARGIN_AREA;
3096 else
3097 it->area = RIGHT_MARGIN_AREA;
2311178e 3098
5f5c8ee5
GM
3099 if (STRINGP (value))
3100 {
3101 it->string = value;
3102 it->multibyte_p = STRING_MULTIBYTE (it->string);
3103 it->current.overlay_string_index = -1;
3104 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3105 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3106 it->method = next_element_from_string;
3107 it->stop_charpos = 0;
3108 it->string_from_display_prop_p = 1;
e187cf71
GM
3109 /* Say that we haven't consumed the characters with
3110 `display' property yet. The call to pop_it in
3111 set_iterator_to_next will clean this up. */
3112 *position = start_pos;
5f5c8ee5
GM
3113 }
3114 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3115 {
3116 it->method = next_element_from_stretch;
3117 it->object = value;
3118 it->current.pos = it->position = start_pos;
3119 }
3120#ifdef HAVE_WINDOW_SYSTEM
3121 else
3122 {
3123 it->what = IT_IMAGE;
3124 it->image_id = lookup_image (it->f, value);
3125 it->position = start_pos;
3126 it->object = NILP (object) ? it->w->buffer : object;
3127 it->method = next_element_from_image;
2311178e 3128
02513cdd 3129 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3130 `display' property yet. The call to pop_it in
3131 set_iterator_to_next will clean this up. */
3132 *position = start_pos;
3133 }
3134#endif /* HAVE_WINDOW_SYSTEM */
3135 }
a21a3943
GM
3136 else
3137 /* Invalid property or property not supported. Restore
3138 the position to what it was before. */
3139 *position = start_pos;
5f5c8ee5
GM
3140 }
3141
a61b7058 3142 return replaces_text_display_p;
5f5c8ee5
GM
3143}
3144
3145
06568bbf
GM
3146/* Check if PROP is a display sub-property value whose text should be
3147 treated as intangible. */
3148
3149static int
3150single_display_prop_intangible_p (prop)
3151 Lisp_Object prop;
3152{
3153 /* Skip over `when FORM'. */
3154 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3155 {
3156 prop = XCDR (prop);
3157 if (!CONSP (prop))
3158 return 0;
3159 prop = XCDR (prop);
3160 }
3161
959bc044
SM
3162 if (STRINGP (prop))
3163 return 1;
3164
06568bbf
GM
3165 if (!CONSP (prop))
3166 return 0;
3167
3168 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3169 we don't need to treat text as intangible. */
3170 if (EQ (XCAR (prop), Qmargin))
3171 {
3172 prop = XCDR (prop);
3173 if (!CONSP (prop))
3174 return 0;
3175
3176 prop = XCDR (prop);
3177 if (!CONSP (prop)
3178 || EQ (XCAR (prop), Qleft_margin)
3179 || EQ (XCAR (prop), Qright_margin))
3180 return 0;
3181 }
2311178e 3182
06568bbf
GM
3183 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3184}
3185
3186
3187/* Check if PROP is a display property value whose text should be
3188 treated as intangible. */
3189
3190int
3191display_prop_intangible_p (prop)
3192 Lisp_Object prop;
3193{
3194 if (CONSP (prop)
3195 && CONSP (XCAR (prop))
3196 && !EQ (Qmargin, XCAR (XCAR (prop))))
3197 {
3198 /* A list of sub-properties. */
3199 while (CONSP (prop))
3200 {
3201 if (single_display_prop_intangible_p (XCAR (prop)))
3202 return 1;
3203 prop = XCDR (prop);
3204 }
3205 }
3206 else if (VECTORP (prop))
3207 {
3208 /* A vector of sub-properties. */
3209 int i;
a61b7058
GM
3210 for (i = 0; i < ASIZE (prop); ++i)
3211 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3212 return 1;
3213 }
3214 else
3215 return single_display_prop_intangible_p (prop);
3216
3217 return 0;
3218}
3219
74bd6d65
GM
3220
3221/* Return 1 if PROP is a display sub-property value containing STRING. */
3222
3223static int
3224single_display_prop_string_p (prop, string)
3225 Lisp_Object prop, string;
3226{
74bd6d65
GM
3227 if (EQ (string, prop))
3228 return 1;
2311178e 3229
74bd6d65
GM
3230 /* Skip over `when FORM'. */
3231 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3232 {
3233 prop = XCDR (prop);
3234 if (!CONSP (prop))
3235 return 0;
3236 prop = XCDR (prop);
3237 }
3238
3239 if (CONSP (prop))
3240 /* Skip over `margin LOCATION'. */
3241 if (EQ (XCAR (prop), Qmargin))
3242 {
3243 prop = XCDR (prop);
3244 if (!CONSP (prop))
3245 return 0;
3246
3247 prop = XCDR (prop);
3248 if (!CONSP (prop))
3249 return 0;
3250 }
2311178e 3251
74bd6d65
GM
3252 return CONSP (prop) && EQ (XCAR (prop), string);
3253}
3254
3255
3256/* Return 1 if STRING appears in the `display' property PROP. */
3257
3258static int
3259display_prop_string_p (prop, string)
3260 Lisp_Object prop, string;
3261{
74bd6d65
GM
3262 if (CONSP (prop)
3263 && CONSP (XCAR (prop))
3264 && !EQ (Qmargin, XCAR (XCAR (prop))))
3265 {
3266 /* A list of sub-properties. */
3267 while (CONSP (prop))
3268 {
3269 if (single_display_prop_string_p (XCAR (prop), string))
3270 return 1;
3271 prop = XCDR (prop);
3272 }
3273 }
3274 else if (VECTORP (prop))
3275 {
3276 /* A vector of sub-properties. */
3277 int i;
3278 for (i = 0; i < ASIZE (prop); ++i)
3279 if (single_display_prop_string_p (AREF (prop, i), string))
3280 return 1;
3281 }
3282 else
3283 return single_display_prop_string_p (prop, string);
3284
3285 return 0;
3286}
3287
3288
3289/* Determine from which buffer position in W's buffer STRING comes
3290 from. AROUND_CHARPOS is an approximate position where it could
3291 be from. Value is the buffer position or 0 if it couldn't be
3292 determined.
3293
3294 W's buffer must be current.
3295
3296 This function is necessary because we don't record buffer positions
3297 in glyphs generated from strings (to keep struct glyph small).
3298 This function may only use code that doesn't eval because it is
3299 called asynchronously from note_mouse_highlight. */
3300
3301int
3302string_buffer_position (w, string, around_charpos)
3303 struct window *w;
3304 Lisp_Object string;
3305 int around_charpos;
3306{
74bd6d65
GM
3307 Lisp_Object limit, prop, pos;
3308 const int MAX_DISTANCE = 1000;
3309 int found = 0;
3310
d8731202 3311 pos = make_number (around_charpos);
74bd6d65
GM
3312 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3313 while (!found && !EQ (pos, limit))
3314 {
3315 prop = Fget_char_property (pos, Qdisplay, Qnil);
3316 if (!NILP (prop) && display_prop_string_p (prop, string))
3317 found = 1;
3318 else
134d9283 3319 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3320 }
3321
3322 if (!found)
3323 {
d8731202 3324 pos = make_number (around_charpos);
74bd6d65
GM
3325 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3326 while (!found && !EQ (pos, limit))
3327 {
3328 prop = Fget_char_property (pos, Qdisplay, Qnil);
3329 if (!NILP (prop) && display_prop_string_p (prop, string))
3330 found = 1;
3331 else
134d9283
GM
3332 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3333 limit);
74bd6d65
GM
3334 }
3335 }
3336
3337 return found ? XINT (pos) : 0;
3338}
3339
3340
5f5c8ee5 3341\f
260a86a0
KH
3342/***********************************************************************
3343 `composition' property
3344 ***********************************************************************/
3345
3346/* Set up iterator IT from `composition' property at its current
3347 position. Called from handle_stop. */
3348
3349static enum prop_handled
3350handle_composition_prop (it)
3351 struct it *it;
3352{
3353 Lisp_Object prop, string;
3354 int pos, pos_byte, end;
3355 enum prop_handled handled = HANDLED_NORMALLY;
3356
3357 if (STRINGP (it->string))
3358 {
3359 pos = IT_STRING_CHARPOS (*it);
3360 pos_byte = IT_STRING_BYTEPOS (*it);
3361 string = it->string;
3362 }
3363 else
3364 {
3365 pos = IT_CHARPOS (*it);
3366 pos_byte = IT_BYTEPOS (*it);
3367 string = Qnil;
3368 }
3369
3370 /* If there's a valid composition and point is not inside of the
3371 composition (in the case that the composition is from the current
3372 buffer), draw a glyph composed from the composition components. */
3373 if (find_composition (pos, -1, &pos, &end, &prop, string)
3374 && COMPOSITION_VALID_P (pos, end, prop)
3375 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3376 {
3377 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3378
3379 if (id >= 0)
3380 {
3381 it->method = next_element_from_composition;
3382 it->cmp_id = id;
3383 it->cmp_len = COMPOSITION_LENGTH (prop);
3384 /* For a terminal, draw only the first character of the
3385 components. */
3386 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3387 it->len = (STRINGP (it->string)
3388 ? string_char_to_byte (it->string, end)
3389 : CHAR_TO_BYTE (end)) - pos_byte;
3390 it->stop_charpos = end;
3391 handled = HANDLED_RETURN;
3392 }
3393 }
3394
3395 return handled;
3396}
3397
3398
3399\f
5f5c8ee5
GM
3400/***********************************************************************
3401 Overlay strings
3402 ***********************************************************************/
3403
3404/* The following structure is used to record overlay strings for
3405 later sorting in load_overlay_strings. */
3406
3407struct overlay_entry
3408{
2970b9be 3409 Lisp_Object overlay;
5f5c8ee5
GM
3410 Lisp_Object string;
3411 int priority;
3412 int after_string_p;
3413};
3414
3415
3416/* Set up iterator IT from overlay strings at its current position.
3417 Called from handle_stop. */
3418
3419static enum prop_handled
3420handle_overlay_change (it)
3421 struct it *it;
3422{
5a08cbaf 3423 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3424 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3425 else
2970b9be 3426 return HANDLED_NORMALLY;
5f5c8ee5
GM
3427}
3428
3429
3430/* Set up the next overlay string for delivery by IT, if there is an
3431 overlay string to deliver. Called by set_iterator_to_next when the
3432 end of the current overlay string is reached. If there are more
3433 overlay strings to display, IT->string and
3434 IT->current.overlay_string_index are set appropriately here.
3435 Otherwise IT->string is set to nil. */
2311178e 3436
5f5c8ee5
GM
3437static void
3438next_overlay_string (it)
3439 struct it *it;
3440{
3441 ++it->current.overlay_string_index;
3442 if (it->current.overlay_string_index == it->n_overlay_strings)
3443 {
3444 /* No more overlay strings. Restore IT's settings to what
3445 they were before overlay strings were processed, and
3446 continue to deliver from current_buffer. */
5a08cbaf 3447 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 3448
5f5c8ee5
GM
3449 pop_it (it);
3450 xassert (it->stop_charpos >= BEGV
3451 && it->stop_charpos <= it->end_charpos);
3452 it->string = Qnil;
3453 it->current.overlay_string_index = -1;
3454 SET_TEXT_POS (it->current.string_pos, -1, -1);
3455 it->n_overlay_strings = 0;
3456 it->method = next_element_from_buffer;
2970b9be
GM
3457
3458 /* If we're at the end of the buffer, record that we have
3459 processed the overlay strings there already, so that
3460 next_element_from_buffer doesn't try it again. */
3461 if (IT_CHARPOS (*it) >= it->end_charpos)
3462 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
3463
3464 /* If we have to display `...' for invisible text, set
3465 the iterator up for that. */
3466 if (display_ellipsis_p)
3467 setup_for_ellipsis (it);
5f5c8ee5
GM
3468 }
3469 else
3470 {
3471 /* There are more overlay strings to process. If
3472 IT->current.overlay_string_index has advanced to a position
3473 where we must load IT->overlay_strings with more strings, do
3474 it. */
3475 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 3476
5f5c8ee5 3477 if (it->current.overlay_string_index && i == 0)
5a08cbaf 3478 load_overlay_strings (it, 0);
5f5c8ee5
GM
3479
3480 /* Initialize IT to deliver display elements from the overlay
3481 string. */
3482 it->string = it->overlay_strings[i];
3483 it->multibyte_p = STRING_MULTIBYTE (it->string);
3484 SET_TEXT_POS (it->current.string_pos, 0, 0);
3485 it->method = next_element_from_string;
3486 it->stop_charpos = 0;
3487 }
2311178e 3488
5f5c8ee5
GM
3489 CHECK_IT (it);
3490}
3491
3492
3493/* Compare two overlay_entry structures E1 and E2. Used as a
3494 comparison function for qsort in load_overlay_strings. Overlay
3495 strings for the same position are sorted so that
3496
2970b9be
GM
3497 1. All after-strings come in front of before-strings, except
3498 when they come from the same overlay.
2311178e 3499
5f5c8ee5
GM
3500 2. Within after-strings, strings are sorted so that overlay strings
3501 from overlays with higher priorities come first.
3502
3503 2. Within before-strings, strings are sorted so that overlay
3504 strings from overlays with higher priorities come last.
3505
3506 Value is analogous to strcmp. */
3507
2311178e 3508
5f5c8ee5
GM
3509static int
3510compare_overlay_entries (e1, e2)
3511 void *e1, *e2;
3512{
3513 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3514 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3515 int result;
3516
3517 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3518 {
3519 /* Let after-strings appear in front of before-strings if
3520 they come from different overlays. */
3521 if (EQ (entry1->overlay, entry2->overlay))
3522 result = entry1->after_string_p ? 1 : -1;
3523 else
3524 result = entry1->after_string_p ? -1 : 1;
3525 }
5f5c8ee5
GM
3526 else if (entry1->after_string_p)
3527 /* After-strings sorted in order of decreasing priority. */
3528 result = entry2->priority - entry1->priority;
3529 else
3530 /* Before-strings sorted in order of increasing priority. */
3531 result = entry1->priority - entry2->priority;
3532
3533 return result;
3534}
3535
3536
3537/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
3538 current buffer position, or from CHARPOS if that is > 0. Set
3539 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
3540
3541 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3542 a time. On entry into load_overlay_strings,
3543 IT->current.overlay_string_index gives the number of overlay
3544 strings that have already been loaded by previous calls to this
3545 function.
3546
2970b9be
GM
3547 IT->add_overlay_start contains an additional overlay start
3548 position to consider for taking overlay strings from, if non-zero.
3549 This position comes into play when the overlay has an `invisible'
3550 property, and both before and after-strings. When we've skipped to
3551 the end of the overlay, because of its `invisible' property, we
3552 nevertheless want its before-string to appear.
3553 IT->add_overlay_start will contain the overlay start position
3554 in this case.
3555
5f5c8ee5
GM
3556 Overlay strings are sorted so that after-string strings come in
3557 front of before-string strings. Within before and after-strings,
3558 strings are sorted by overlay priority. See also function
3559 compare_overlay_entries. */
2311178e 3560
5f5c8ee5 3561static void
5a08cbaf 3562load_overlay_strings (it, charpos)
5f5c8ee5 3563 struct it *it;
5a08cbaf 3564 int charpos;
5f5c8ee5
GM
3565{
3566 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3567 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3568 int start, end;
3569 int size = 20;
cafafe0b 3570 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3571 struct overlay_entry *entries
3572 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
3573
3574 if (charpos <= 0)
3575 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3576
3577 /* Append the overlay string STRING of overlay OVERLAY to vector
3578 `entries' which has size `size' and currently contains `n'
3579 elements. AFTER_P non-zero means STRING is an after-string of
3580 OVERLAY. */
3581#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3582 do \
3583 { \
3584 Lisp_Object priority; \
3585 \
3586 if (n == size) \
3587 { \
3588 int new_size = 2 * size; \
3589 struct overlay_entry *old = entries; \
3590 entries = \
3591 (struct overlay_entry *) alloca (new_size \
3592 * sizeof *entries); \
3593 bcopy (old, entries, size * sizeof *entries); \
3594 size = new_size; \
3595 } \
3596 \
3597 entries[n].string = (STRING); \
2970b9be 3598 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3599 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3600 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3601 entries[n].after_string_p = (AFTER_P); \
3602 ++n; \
3603 } \
3604 while (0)
3605
3606 /* Process overlay before the overlay center. */
2970b9be 3607 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3608 {
9472f927 3609 overlay = XCAR (ov);
5f5c8ee5
GM
3610 xassert (OVERLAYP (overlay));
3611 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3612 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 3613
cafafe0b 3614 if (end < charpos)
5f5c8ee5
GM
3615 break;
3616
3617 /* Skip this overlay if it doesn't start or end at IT's current
3618 position. */
cafafe0b 3619 if (end != charpos && start != charpos)
5f5c8ee5 3620 continue;
2311178e 3621
5f5c8ee5
GM
3622 /* Skip this overlay if it doesn't apply to IT->w. */
3623 window = Foverlay_get (overlay, Qwindow);
3624 if (WINDOWP (window) && XWINDOW (window) != it->w)
3625 continue;
3626
cafafe0b
GM
3627 /* If the text ``under'' the overlay is invisible, both before-
3628 and after-strings from this overlay are visible; start and
3629 end position are indistinguishable. */
3630 invisible = Foverlay_get (overlay, Qinvisible);
3631 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3632
5f5c8ee5 3633 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3634 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 3635 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 3636 && SCHARS (str))
5f5c8ee5 3637 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 3638
5f5c8ee5 3639 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3640 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 3641 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 3642 && SCHARS (str))
5f5c8ee5
GM
3643 RECORD_OVERLAY_STRING (overlay, str, 1);
3644 }
2311178e 3645
5f5c8ee5 3646 /* Process overlays after the overlay center. */
2970b9be 3647 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3648 {
9472f927 3649 overlay = XCAR (ov);
5f5c8ee5
GM
3650 xassert (OVERLAYP (overlay));
3651 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3652 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3653
cafafe0b 3654 if (start > charpos)
5f5c8ee5 3655 break;
2311178e 3656
5f5c8ee5
GM
3657 /* Skip this overlay if it doesn't start or end at IT's current
3658 position. */
cafafe0b 3659 if (end != charpos && start != charpos)
5f5c8ee5
GM
3660 continue;
3661
3662 /* Skip this overlay if it doesn't apply to IT->w. */
3663 window = Foverlay_get (overlay, Qwindow);
3664 if (WINDOWP (window) && XWINDOW (window) != it->w)
3665 continue;
2311178e 3666
cafafe0b
GM
3667 /* If the text ``under'' the overlay is invisible, it has a zero
3668 dimension, and both before- and after-strings apply. */
3669 invisible = Foverlay_get (overlay, Qinvisible);
3670 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3671
5f5c8ee5 3672 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3673 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 3674 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 3675 && SCHARS (str))
5f5c8ee5 3676 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 3677
5f5c8ee5 3678 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3679 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 3680 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 3681 && SCHARS (str))
5f5c8ee5
GM
3682 RECORD_OVERLAY_STRING (overlay, str, 1);
3683 }
3684
3685#undef RECORD_OVERLAY_STRING
2311178e 3686
5f5c8ee5 3687 /* Sort entries. */
cafafe0b 3688 if (n > 1)
2970b9be 3689 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3690
3691 /* Record the total number of strings to process. */
3692 it->n_overlay_strings = n;
3693
3694 /* IT->current.overlay_string_index is the number of overlay strings
3695 that have already been consumed by IT. Copy some of the
3696 remaining overlay strings to IT->overlay_strings. */
3697 i = 0;
3698 j = it->current.overlay_string_index;
3699 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3700 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3701
5f5c8ee5
GM
3702 CHECK_IT (it);
3703}
3704
3705
3706/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
3707 position, or at CHARPOS if that is > 0. Value is non-zero if at
3708 least one overlay string was found. */
5f5c8ee5
GM
3709
3710static int
5a08cbaf 3711get_overlay_strings (it, charpos)
5f5c8ee5 3712 struct it *it;
5a08cbaf 3713 int charpos;
5f5c8ee5
GM
3714{
3715 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3716 process. This fills IT->overlay_strings with strings, and sets
3717 IT->n_overlay_strings to the total number of strings to process.
3718 IT->pos.overlay_string_index has to be set temporarily to zero
3719 because load_overlay_strings needs this; it must be set to -1
3720 when no overlay strings are found because a zero value would
3721 indicate a position in the first overlay string. */
3722 it->current.overlay_string_index = 0;
5a08cbaf 3723 load_overlay_strings (it, charpos);
5f5c8ee5
GM
3724
3725 /* If we found overlay strings, set up IT to deliver display
3726 elements from the first one. Otherwise set up IT to deliver
3727 from current_buffer. */
3728 if (it->n_overlay_strings)
3729 {
3730 /* Make sure we know settings in current_buffer, so that we can
3731 restore meaningful values when we're done with the overlay
3732 strings. */
3733 compute_stop_pos (it);
3734 xassert (it->face_id >= 0);
2311178e 3735
5f5c8ee5
GM
3736 /* Save IT's settings. They are restored after all overlay
3737 strings have been processed. */
3738 xassert (it->sp == 0);
3739 push_it (it);
3740
3741 /* Set up IT to deliver display elements from the first overlay
3742 string. */
3743 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 3744 it->string = it->overlay_strings[0];
b2046df8 3745 it->stop_charpos = 0;
5f5c8ee5 3746 xassert (STRINGP (it->string));
2051c264 3747 it->end_charpos = SCHARS (it->string);
d5db4077 3748 it->multibyte_p = STRING_MULTIBYTE (it->string);
5f5c8ee5
GM
3749 it->method = next_element_from_string;
3750 }
3751 else
3752 {
3753 it->string = Qnil;
3754 it->current.overlay_string_index = -1;
3755 it->method = next_element_from_buffer;
3756 }
3757
3758 CHECK_IT (it);
3759
3760 /* Value is non-zero if we found at least one overlay string. */
3761 return STRINGP (it->string);
3762}
3763
3764
3765\f
3766/***********************************************************************
3767 Saving and restoring state
3768 ***********************************************************************/
3769
3770/* Save current settings of IT on IT->stack. Called, for example,
3771 before setting up IT for an overlay string, to be able to restore
3772 IT's settings to what they were after the overlay string has been
3773 processed. */
3774
3775static void
3776push_it (it)
3777 struct it *it;
3778{
3779 struct iterator_stack_entry *p;
2311178e 3780
5f5c8ee5
GM
3781 xassert (it->sp < 2);
3782 p = it->stack + it->sp;
3783
3784 p->stop_charpos = it->stop_charpos;
3785 xassert (it->face_id >= 0);
3786 p->face_id = it->face_id;
3787 p->string = it->string;
3788 p->pos = it->current;
3789 p->end_charpos = it->end_charpos;
3790 p->string_nchars = it->string_nchars;
3791 p->area = it->area;
3792 p->multibyte_p = it->multibyte_p;
3793 p->space_width = it->space_width;
3794 p->font_height = it->font_height;
3795 p->voffset = it->voffset;
3796 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 3797 p->display_ellipsis_p = 0;
5f5c8ee5
GM
3798 ++it->sp;
3799}
3800
3801
3802/* Restore IT's settings from IT->stack. Called, for example, when no
3803 more overlay strings must be processed, and we return to delivering
3804 display elements from a buffer, or when the end of a string from a
3805 `display' property is reached and we return to delivering display
3806 elements from an overlay string, or from a buffer. */
3807
3808static void
3809pop_it (it)
3810 struct it *it;
3811{
3812 struct iterator_stack_entry *p;
2311178e 3813
5f5c8ee5
GM
3814 xassert (it->sp > 0);
3815 --it->sp;
3816 p = it->stack + it->sp;
3817 it->stop_charpos = p->stop_charpos;
3818 it->face_id = p->face_id;
3819 it->string = p->string;
3820 it->current = p->pos;
3821 it->end_charpos = p->end_charpos;
3822 it->string_nchars = p->string_nchars;
3823 it->area = p->area;
3824 it->multibyte_p = p->multibyte_p;
3825 it->space_width = p->space_width;
3826 it->font_height = p->font_height;
3827 it->voffset = p->voffset;
3828 it->string_from_display_prop_p = p->string_from_display_prop_p;
3829}
3830
3831
3832\f
3833/***********************************************************************
3834 Moving over lines
3835 ***********************************************************************/
3836
3837/* Set IT's current position to the previous line start. */
3838
3839static void
3840back_to_previous_line_start (it)
3841 struct it *it;
3842{
3843 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3844 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3845}
3846
3847
cafafe0b 3848/* Move IT to the next line start.
2311178e 3849
cafafe0b
GM
3850 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3851 we skipped over part of the text (as opposed to moving the iterator
3852 continuously over the text). Otherwise, don't change the value
3853 of *SKIPPED_P.
2311178e 3854
cafafe0b
GM
3855 Newlines may come from buffer text, overlay strings, or strings
3856 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3857 simply use find_next_newline_no_quit.
3858
3859 Note that this function may not skip over invisible text that is so
3860 because of text properties and immediately follows a newline. If
3861 it would, function reseat_at_next_visible_line_start, when called
3862 from set_iterator_to_next, would effectively make invisible
3863 characters following a newline part of the wrong glyph row, which
3864 leads to wrong cursor motion. */
5f5c8ee5 3865
cafafe0b
GM
3866static int
3867forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3868 struct it *it;
cafafe0b 3869 int *skipped_p;
5f5c8ee5 3870{
54918e2b 3871 int old_selective, newline_found_p, n;
cafafe0b
GM
3872 const int MAX_NEWLINE_DISTANCE = 500;
3873
0fd37545
GM
3874 /* If already on a newline, just consume it to avoid unintended
3875 skipping over invisible text below. */
51695746
GM
3876 if (it->what == IT_CHARACTER
3877 && it->c == '\n'
3878 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
3879 {
3880 set_iterator_to_next (it, 0);
a77dc1ec 3881 it->c = 0;
0fd37545
GM
3882 return 1;
3883 }
3884
54918e2b 3885 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3886 unnecessary because it's done by the caller, and (b) leads to an
3887 infinite recursion because next_element_from_ellipsis indirectly
3888 calls this function. */
54918e2b
GM
3889 old_selective = it->selective;
3890 it->selective = 0;
3891
cafafe0b
GM
3892 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3893 from buffer text. */
3aec8722
GM
3894 for (n = newline_found_p = 0;
3895 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3896 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3897 {
8692ca92 3898 if (!get_next_display_element (it))
d43be70c 3899 return 0;
d02f1cb8 3900 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3901 set_iterator_to_next (it, 0);
cafafe0b
GM
3902 }
3903
3904 /* If we didn't find a newline near enough, see if we can use a
3905 short-cut. */
db0bb807 3906 if (!newline_found_p)
cafafe0b
GM
3907 {
3908 int start = IT_CHARPOS (*it);
3909 int limit = find_next_newline_no_quit (start, 1);
3910 Lisp_Object pos;
3911
3912 xassert (!STRINGP (it->string));
3913
3914 /* If there isn't any `display' property in sight, and no
3915 overlays, we can just use the position of the newline in
3916 buffer text. */
3917 if (it->stop_charpos >= limit
3918 || ((pos = Fnext_single_property_change (make_number (start),
3919 Qdisplay,
3920 Qnil, make_number (limit)),
3921 NILP (pos))
3922 && next_overlay_change (start) == ZV))
3923 {
3924 IT_CHARPOS (*it) = limit;
3925 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3926 *skipped_p = newline_found_p = 1;
3927 }
3928 else
3929 {
3930 while (get_next_display_element (it)
3931 && !newline_found_p)
3932 {
3933 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3934 set_iterator_to_next (it, 0);
3935 }
3936 }
3937 }
3938
54918e2b 3939 it->selective = old_selective;
cafafe0b 3940 return newline_found_p;
5f5c8ee5
GM
3941}
3942
3943
3944/* Set IT's current position to the previous visible line start. Skip
3945 invisible text that is so either due to text properties or due to
3946 selective display. Caution: this does not change IT->current_x and
3947 IT->hpos. */
3948
3949static void
3950back_to_previous_visible_line_start (it)
3951 struct it *it;
3952{
3953 int visible_p = 0;
3954
3955 /* Go back one newline if not on BEGV already. */
3956 if (IT_CHARPOS (*it) > BEGV)
3957 back_to_previous_line_start (it);
3958
3959 /* Move over lines that are invisible because of selective display
3960 or text properties. */
3961 while (IT_CHARPOS (*it) > BEGV
3962 && !visible_p)
3963 {
3964 visible_p = 1;
3965
3966 /* If selective > 0, then lines indented more than that values
3967 are invisible. */
3968 if (it->selective > 0
3969 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 3970 (double) it->selective)) /* iftc */
5f5c8ee5 3971 visible_p = 0;
2311178e 3972 else
5f5c8ee5
GM
3973 {
3974 Lisp_Object prop;
3975
6fc556fd
KR
3976 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3977 Qinvisible, it->window);
5f5c8ee5
GM
3978 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3979 visible_p = 0;
3980 }
5f5c8ee5
GM
3981
3982 /* Back one more newline if the current one is invisible. */
3983 if (!visible_p)
3984 back_to_previous_line_start (it);
3985 }
3986
3987 xassert (IT_CHARPOS (*it) >= BEGV);
3988 xassert (IT_CHARPOS (*it) == BEGV
3989 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3990 CHECK_IT (it);
3991}
3992
3993
3994/* Reseat iterator IT at the previous visible line start. Skip
3995 invisible text that is so either due to text properties or due to
3996 selective display. At the end, update IT's overlay information,
3997 face information etc. */
3998
3999static void
4000reseat_at_previous_visible_line_start (it)
4001 struct it *it;
4002{
4003 back_to_previous_visible_line_start (it);
4004 reseat (it, it->current.pos, 1);
4005 CHECK_IT (it);
4006}
4007
4008
4009/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
4010 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4011 preceding the line start. Skip over invisible text that is so
4012 because of selective display. Compute faces, overlays etc at the
4013 new position. Note that this function does not skip over text that
4014 is invisible because of text properties. */
5f5c8ee5
GM
4015
4016static void
312246d1 4017reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 4018 struct it *it;
312246d1 4019 int on_newline_p;
5f5c8ee5 4020{
cafafe0b
GM
4021 int newline_found_p, skipped_p = 0;
4022
4023 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4024
4025 /* Skip over lines that are invisible because they are indented
4026 more than the value of IT->selective. */
4027 if (it->selective > 0)
4028 while (IT_CHARPOS (*it) < ZV
a77dc1ec 4029 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4030 (double) it->selective)) /* iftc */
a77dc1ec
GM
4031 {
4032 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4033 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4034 }
cafafe0b
GM
4035
4036 /* Position on the newline if that's what's requested. */
4037 if (on_newline_p && newline_found_p)
5f5c8ee5 4038 {
cafafe0b 4039 if (STRINGP (it->string))
5f5c8ee5 4040 {
cafafe0b
GM
4041 if (IT_STRING_CHARPOS (*it) > 0)
4042 {
4043 --IT_STRING_CHARPOS (*it);
4044 --IT_STRING_BYTEPOS (*it);
4045 }
5f5c8ee5 4046 }
cafafe0b 4047 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
4048 {
4049 --IT_CHARPOS (*it);
cafafe0b
GM
4050 --IT_BYTEPOS (*it);
4051 reseat (it, it->current.pos, 0);
312246d1 4052 }
5f5c8ee5 4053 }
cafafe0b
GM
4054 else if (skipped_p)
4055 reseat (it, it->current.pos, 0);
2311178e 4056
5f5c8ee5
GM
4057 CHECK_IT (it);
4058}
4059
4060
4061\f
4062/***********************************************************************
4063 Changing an iterator's position
4064***********************************************************************/
4065
4066/* Change IT's current position to POS in current_buffer. If FORCE_P
4067 is non-zero, always check for text properties at the new position.
4068 Otherwise, text properties are only looked up if POS >=
4069 IT->check_charpos of a property. */
4070
4071static void
4072reseat (it, pos, force_p)
4073 struct it *it;
4074 struct text_pos pos;
4075 int force_p;
4076{
4077 int original_pos = IT_CHARPOS (*it);
4078
4079 reseat_1 (it, pos, 0);
4080
4081 /* Determine where to check text properties. Avoid doing it
4082 where possible because text property lookup is very expensive. */
4083 if (force_p
4084 || CHARPOS (pos) > it->stop_charpos
4085 || CHARPOS (pos) < original_pos)
4086 handle_stop (it);
4087
4088 CHECK_IT (it);
4089}
4090
4091
4092/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4093 IT->stop_pos to POS, also. */
4094
4095static void
4096reseat_1 (it, pos, set_stop_p)
4097 struct it *it;
4098 struct text_pos pos;
4099 int set_stop_p;
4100{
4101 /* Don't call this function when scanning a C string. */
4102 xassert (it->s == NULL);
4103
4104 /* POS must be a reasonable value. */
4105 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4106
4107 it->current.pos = it->position = pos;
4108 XSETBUFFER (it->object, current_buffer);
78c663d8 4109 it->end_charpos = ZV;
5f5c8ee5
GM
4110 it->dpvec = NULL;
4111 it->current.dpvec_index = -1;
4112 it->current.overlay_string_index = -1;
4113 IT_STRING_CHARPOS (*it) = -1;
4114 IT_STRING_BYTEPOS (*it) = -1;
4115 it->string = Qnil;
4116 it->method = next_element_from_buffer;
06fd3792 4117 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4118 it->sp = 0;
4aad61f8 4119 it->face_before_selective_p = 0;
5f5c8ee5
GM
4120
4121 if (set_stop_p)
4122 it->stop_charpos = CHARPOS (pos);
4123}
4124
4125
4126/* Set up IT for displaying a string, starting at CHARPOS in window W.
4127 If S is non-null, it is a C string to iterate over. Otherwise,
4128 STRING gives a Lisp string to iterate over.
2311178e 4129
5f5c8ee5
GM
4130 If PRECISION > 0, don't return more then PRECISION number of
4131 characters from the string.
4132
4133 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4134 characters have been returned. FIELD_WIDTH < 0 means an infinite
4135 field width.
4136
4137 MULTIBYTE = 0 means disable processing of multibyte characters,
4138 MULTIBYTE > 0 means enable it,
4139 MULTIBYTE < 0 means use IT->multibyte_p.
4140
4141 IT must be initialized via a prior call to init_iterator before
4142 calling this function. */
4143
4144static void
4145reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4146 struct it *it;
4147 unsigned char *s;
4148 Lisp_Object string;
4149 int charpos;
4150 int precision, field_width, multibyte;
4151{
4152 /* No region in strings. */
4153 it->region_beg_charpos = it->region_end_charpos = -1;
4154
4155 /* No text property checks performed by default, but see below. */
4156 it->stop_charpos = -1;
4157
4158 /* Set iterator position and end position. */
4159 bzero (&it->current, sizeof it->current);
4160 it->current.overlay_string_index = -1;
4161 it->current.dpvec_index = -1;
5f5c8ee5 4162 xassert (charpos >= 0);
2311178e 4163
e719f5ae
GM
4164 /* If STRING is specified, use its multibyteness, otherwise use the
4165 setting of MULTIBYTE, if specified. */
cabf45da 4166 if (multibyte >= 0)
5f5c8ee5 4167 it->multibyte_p = multibyte > 0;
2311178e 4168
5f5c8ee5
GM
4169 if (s == NULL)
4170 {
4171 xassert (STRINGP (string));
4172 it->string = string;
4173 it->s = NULL;
2051c264 4174 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4175 it->method = next_element_from_string;
4176 it->current.string_pos = string_pos (charpos, string);
4177 }
4178 else
4179 {
4180 it->s = s;
4181 it->string = Qnil;
4182
4183 /* Note that we use IT->current.pos, not it->current.string_pos,
4184 for displaying C strings. */
4185 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4186 if (it->multibyte_p)
4187 {
4188 it->current.pos = c_string_pos (charpos, s, 1);
4189 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4190 }
4191 else
4192 {
4193 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4194 it->end_charpos = it->string_nchars = strlen (s);
4195 }
2311178e 4196
5f5c8ee5
GM
4197 it->method = next_element_from_c_string;
4198 }
4199
4200 /* PRECISION > 0 means don't return more than PRECISION characters
4201 from the string. */
4202 if (precision > 0 && it->end_charpos - charpos > precision)
4203 it->end_charpos = it->string_nchars = charpos + precision;
4204
4205 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4206 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4207 FIELD_WIDTH < 0 means infinite field width. This is useful for
4208 padding with `-' at the end of a mode line. */
4209 if (field_width < 0)
4210 field_width = INFINITY;
4211 if (field_width > it->end_charpos - charpos)
4212 it->end_charpos = charpos + field_width;
4213
4214 /* Use the standard display table for displaying strings. */
4215 if (DISP_TABLE_P (Vstandard_display_table))
4216 it->dp = XCHAR_TABLE (Vstandard_display_table);
4217
4218 it->stop_charpos = charpos;
4219 CHECK_IT (it);
4220}
4221
4222
4223\f
4224/***********************************************************************
4225 Iteration
4226 ***********************************************************************/
4227
4228/* Load IT's display element fields with information about the next
4229 display element from the current position of IT. Value is zero if
4230 end of buffer (or C string) is reached. */
4231
4232int
4233get_next_display_element (it)
4234 struct it *it;
4235{
7d0393cf 4236 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
4237 we hit the end of what we iterate over. Performance note: the
4238 function pointer `method' used here turns out to be faster than
4239 using a sequence of if-statements. */
4240 int success_p = (*it->method) (it);
5f5c8ee5
GM
4241
4242 if (it->what == IT_CHARACTER)
4243 {
4244 /* Map via display table or translate control characters.
4245 IT->c, IT->len etc. have been set to the next character by
4246 the function call above. If we have a display table, and it
4247 contains an entry for IT->c, translate it. Don't do this if
4248 IT->c itself comes from a display table, otherwise we could
4249 end up in an infinite recursion. (An alternative could be to
4250 count the recursion depth of this function and signal an
4251 error when a certain maximum depth is reached.) Is it worth
4252 it? */
4253 if (success_p && it->dpvec == NULL)
4254 {
4255 Lisp_Object dv;
4256
4257 if (it->dp
4258 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4259 VECTORP (dv)))
4260 {
4261 struct Lisp_Vector *v = XVECTOR (dv);
4262
4263 /* Return the first character from the display table
4264 entry, if not empty. If empty, don't display the
4265 current character. */
4266 if (v->size)
4267 {
4268 it->dpvec_char_len = it->len;
4269 it->dpvec = v->contents;
4270 it->dpend = v->contents + v->size;
4271 it->current.dpvec_index = 0;
4272 it->method = next_element_from_display_vector;
7eee47cc
GM
4273 success_p = get_next_display_element (it);
4274 }
4275 else
4276 {
4277 set_iterator_to_next (it, 0);
4278 success_p = get_next_display_element (it);
5f5c8ee5 4279 }
5f5c8ee5
GM
4280 }
4281
4282 /* Translate control characters into `\003' or `^C' form.
4283 Control characters coming from a display table entry are
4284 currently not translated because we use IT->dpvec to hold
4285 the translation. This could easily be changed but I
197516c2
KH
4286 don't believe that it is worth doing.
4287
fa831cf8
KH
4288 If it->multibyte_p is nonzero, eight-bit characters and
4289 non-printable multibyte characters are also translated to
4290 octal form.
4291
4292 If it->multibyte_p is zero, eight-bit characters that
4293 don't have corresponding multibyte char code are also
4294 translated to octal form. */
ba197fe6 4295 else if ((it->c < ' '
5f5c8ee5 4296 && (it->area != TEXT_AREA
c6e89d6c 4297 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
4298 || (it->multibyte_p
4299 ? ((it->c >= 127
4300 && it->len == 1)
4301 || !CHAR_PRINTABLE_P (it->c))
ba197fe6 4302 : (it->c >= 127
fa831cf8 4303 && it->c == unibyte_char_to_multibyte (it->c))))
5f5c8ee5
GM
4304 {
4305 /* IT->c is a control character which must be displayed
4306 either as '\003' or as `^C' where the '\\' and '^'
4307 can be defined in the display table. Fill
4308 IT->ctl_chars with glyphs for what we have to
4309 display. Then, set IT->dpvec to these glyphs. */
4310 GLYPH g;
4311
54c85a23 4312 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4313 {
4314 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4315 if (it->dp
4316 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4317 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4318 g = XINT (DISP_CTRL_GLYPH (it->dp));
4319 else
4320 g = FAST_MAKE_GLYPH ('^', 0);
4321 XSETINT (it->ctl_chars[0], g);
4322
4323 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4324 XSETINT (it->ctl_chars[1], g);
4325
4326 /* Set up IT->dpvec and return first character from it. */
4327 it->dpvec_char_len = it->len;
4328 it->dpvec = it->ctl_chars;
4329 it->dpend = it->dpvec + 2;
4330 it->current.dpvec_index = 0;
4331 it->method = next_element_from_display_vector;
4332 get_next_display_element (it);
4333 }
4334 else
4335 {
260a86a0 4336 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4337 int len;
197516c2
KH
4338 int i;
4339 GLYPH escape_glyph;
4340
5f5c8ee5
GM
4341 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4342 if (it->dp
4343 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4344 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4345 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4346 else
197516c2
KH
4347 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4348
c5924f47
KH
4349 if (SINGLE_BYTE_CHAR_P (it->c))
4350 str[0] = it->c, len = 1;
4351 else
ea9dd091
GM
4352 {
4353 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4354 if (len < 0)
4355 {
4356 /* It's an invalid character, which
4357 shouldn't happen actually, but due to
4358 bugs it may happen. Let's print the char
4359 as is, there's not much meaningful we can
4360 do with it. */
4361 str[0] = it->c;
4362 str[1] = it->c >> 8;
4363 str[2] = it->c >> 16;
4364 str[3] = it->c >> 24;
4365 len = 4;
4366 }
4367 }
c5924f47 4368
197516c2
KH
4369 for (i = 0; i < len; i++)
4370 {
4371 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4372 /* Insert three more glyphs into IT->ctl_chars for
4373 the octal display of the character. */
2311178e 4374 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
197516c2 4375 XSETINT (it->ctl_chars[i * 4 + 1], g);
2311178e 4376 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
197516c2 4377 XSETINT (it->ctl_chars[i * 4 + 2], g);
2311178e 4378 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
197516c2
KH
4379 XSETINT (it->ctl_chars[i * 4 + 3], g);
4380 }
5f5c8ee5
GM
4381
4382 /* Set up IT->dpvec and return the first character
4383 from it. */
4384 it->dpvec_char_len = it->len;
4385 it->dpvec = it->ctl_chars;
197516c2 4386 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4387 it->current.dpvec_index = 0;
4388 it->method = next_element_from_display_vector;
4389 get_next_display_element (it);
4390 }
4391 }
4392 }
4393
980806b6
KH
4394 /* Adjust face id for a multibyte character. There are no
4395 multibyte character in unibyte text. */
5f5c8ee5
GM
4396 if (it->multibyte_p
4397 && success_p
980806b6 4398 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4399 {
980806b6
KH
4400 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4401 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4402 }
4403 }
4404
4405 /* Is this character the last one of a run of characters with
4406 box? If yes, set IT->end_of_box_run_p to 1. */
4407 if (it->face_box_p
4408 && it->s == NULL)
4409 {
4410 int face_id;
4411 struct face *face;
4412
4413 it->end_of_box_run_p
4414 = ((face_id = face_after_it_pos (it),
4415 face_id != it->face_id)
4416 && (face = FACE_FROM_ID (it->f, face_id),
4417 face->box == FACE_NO_BOX));
4418 }
4419
4420 /* Value is 0 if end of buffer or string reached. */
4421 return success_p;
4422}
4423
4424
4425/* Move IT to the next display element.
4426
cafafe0b
GM
4427 RESEAT_P non-zero means if called on a newline in buffer text,
4428 skip to the next visible line start.
4429
5f5c8ee5
GM
4430 Functions get_next_display_element and set_iterator_to_next are
4431 separate because I find this arrangement easier to handle than a
4432 get_next_display_element function that also increments IT's
4433 position. The way it is we can first look at an iterator's current
4434 display element, decide whether it fits on a line, and if it does,
4435 increment the iterator position. The other way around we probably
4436 would either need a flag indicating whether the iterator has to be
4437 incremented the next time, or we would have to implement a
4438 decrement position function which would not be easy to write. */
4439
4440void
cafafe0b 4441set_iterator_to_next (it, reseat_p)
5f5c8ee5 4442 struct it *it;
cafafe0b 4443 int reseat_p;
5f5c8ee5 4444{
483de32b
GM
4445 /* Reset flags indicating start and end of a sequence of characters
4446 with box. Reset them at the start of this function because
4447 moving the iterator to a new position might set them. */
4448 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 4449
5f5c8ee5
GM
4450 if (it->method == next_element_from_buffer)
4451 {
4452 /* The current display element of IT is a character from
4453 current_buffer. Advance in the buffer, and maybe skip over
4454 invisible lines that are so because of selective display. */
cafafe0b 4455 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4456 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4457 else
4458 {
4459 xassert (it->len != 0);
4460 IT_BYTEPOS (*it) += it->len;
4461 IT_CHARPOS (*it) += 1;
4462 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4463 }
4464 }
260a86a0
KH
4465 else if (it->method == next_element_from_composition)
4466 {
4467 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4468 if (STRINGP (it->string))
4469 {
4470 IT_STRING_BYTEPOS (*it) += it->len;
4471 IT_STRING_CHARPOS (*it) += it->cmp_len;
4472 it->method = next_element_from_string;
4473 goto consider_string_end;
4474 }
4475 else
4476 {
4477 IT_BYTEPOS (*it) += it->len;
4478 IT_CHARPOS (*it) += it->cmp_len;
4479 it->method = next_element_from_buffer;
4480 }
4481 }
5f5c8ee5
GM
4482 else if (it->method == next_element_from_c_string)
4483 {
4484 /* Current display element of IT is from a C string. */
4485 IT_BYTEPOS (*it) += it->len;
4486 IT_CHARPOS (*it) += 1;
4487 }
4488 else if (it->method == next_element_from_display_vector)
4489 {
4490 /* Current display element of IT is from a display table entry.
4491 Advance in the display table definition. Reset it to null if
4492 end reached, and continue with characters from buffers/
4493 strings. */
4494 ++it->current.dpvec_index;
286bcbc9 4495
980806b6
KH
4496 /* Restore face of the iterator to what they were before the
4497 display vector entry (these entries may contain faces). */
5f5c8ee5 4498 it->face_id = it->saved_face_id;
2311178e 4499
5f5c8ee5
GM
4500 if (it->dpvec + it->current.dpvec_index == it->dpend)
4501 {
4502 if (it->s)
4503 it->method = next_element_from_c_string;
4504 else if (STRINGP (it->string))
4505 it->method = next_element_from_string;
4506 else
4507 it->method = next_element_from_buffer;
4508
4509 it->dpvec = NULL;
4510 it->current.dpvec_index = -1;
4511
312246d1
GM
4512 /* Skip over characters which were displayed via IT->dpvec. */
4513 if (it->dpvec_char_len < 0)
4514 reseat_at_next_visible_line_start (it, 1);
4515 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4516 {
4517 it->len = it->dpvec_char_len;
cafafe0b 4518 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4519 }
4520 }
4521 }
4522 else if (it->method == next_element_from_string)
4523 {
4524 /* Current display element is a character from a Lisp string. */
4525 xassert (it->s == NULL && STRINGP (it->string));
4526 IT_STRING_BYTEPOS (*it) += it->len;
4527 IT_STRING_CHARPOS (*it) += 1;
2311178e 4528
5f5c8ee5
GM
4529 consider_string_end:
4530
4531 if (it->current.overlay_string_index >= 0)
4532 {
4533 /* IT->string is an overlay string. Advance to the
4534 next, if there is one. */
2051c264 4535 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4536 next_overlay_string (it);
4537 }
4538 else
4539 {
4540 /* IT->string is not an overlay string. If we reached
4541 its end, and there is something on IT->stack, proceed
4542 with what is on the stack. This can be either another
4543 string, this time an overlay string, or a buffer. */
2051c264 4544 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
4545 && it->sp > 0)
4546 {
4547 pop_it (it);
4548 if (!STRINGP (it->string))
4549 it->method = next_element_from_buffer;
b2046df8
GM
4550 else
4551 goto consider_string_end;
5f5c8ee5
GM
4552 }
4553 }
4554 }
4555 else if (it->method == next_element_from_image
4556 || it->method == next_element_from_stretch)
4557 {
4558 /* The position etc with which we have to proceed are on
4559 the stack. The position may be at the end of a string,
4560 if the `display' property takes up the whole string. */
4561 pop_it (it);
4562 it->image_id = 0;
4563 if (STRINGP (it->string))
4564 {
4565 it->method = next_element_from_string;
4566 goto consider_string_end;
4567 }
4568 else
4569 it->method = next_element_from_buffer;
4570 }
4571 else
4572 /* There are no other methods defined, so this should be a bug. */
4573 abort ();
4574
5f5c8ee5
GM
4575 xassert (it->method != next_element_from_string
4576 || (STRINGP (it->string)
4577 && IT_STRING_CHARPOS (*it) >= 0));
4578}
4579
4580
4581/* Load IT's display element fields with information about the next
4582 display element which comes from a display table entry or from the
4583 result of translating a control character to one of the forms `^C'
4584 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4585
4586static int
4587next_element_from_display_vector (it)
4588 struct it *it;
4589{
4590 /* Precondition. */
4591 xassert (it->dpvec && it->current.dpvec_index >= 0);
4592
4593 /* Remember the current face id in case glyphs specify faces.
4594 IT's face is restored in set_iterator_to_next. */
4595 it->saved_face_id = it->face_id;
2311178e 4596
5f5c8ee5
GM
4597 if (INTEGERP (*it->dpvec)
4598 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4599 {
4600 int lface_id;
4601 GLYPH g;
4602
4603 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4604 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4605 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4606
4607 /* The entry may contain a face id to use. Such a face id is
4608 the id of a Lisp face, not a realized face. A face id of
969065c3 4609 zero means no face is specified. */
5f5c8ee5
GM
4610 lface_id = FAST_GLYPH_FACE (g);
4611 if (lface_id)
4612 {
969065c3 4613 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4614 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4615 if (face_id >= 0)
969065c3 4616 it->face_id = face_id;
5f5c8ee5
GM
4617 }
4618 }
4619 else
4620 /* Display table entry is invalid. Return a space. */
4621 it->c = ' ', it->len = 1;
4622
4623 /* Don't change position and object of the iterator here. They are
4624 still the values of the character that had this display table
4625 entry or was translated, and that's what we want. */
4626 it->what = IT_CHARACTER;
4627 return 1;
4628}
4629
4630
4631/* Load IT with the next display element from Lisp string IT->string.
4632 IT->current.string_pos is the current position within the string.
4633 If IT->current.overlay_string_index >= 0, the Lisp string is an
4634 overlay string. */
4635
4636static int
4637next_element_from_string (it)
4638 struct it *it;
4639{
4640 struct text_pos position;
4641
4642 xassert (STRINGP (it->string));
4643 xassert (IT_STRING_CHARPOS (*it) >= 0);
4644 position = it->current.string_pos;
4645
4646 /* Time to check for invisible text? */
4647 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4648 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4649 {
4650 handle_stop (it);
4651
4652 /* Since a handler may have changed IT->method, we must
4653 recurse here. */
4654 return get_next_display_element (it);
4655 }
4656
4657 if (it->current.overlay_string_index >= 0)
4658 {
4659 /* Get the next character from an overlay string. In overlay
4660 strings, There is no field width or padding with spaces to
4661 do. */
2051c264 4662 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4663 {
4664 it->what = IT_EOB;
4665 return 0;
4666 }
4667 else if (STRING_MULTIBYTE (it->string))
4668 {
2051c264 4669 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
4670 const unsigned char *s = (SDATA (it->string)
4671 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4672 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4673 }
4674 else
4675 {
2051c264 4676 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
4677 it->len = 1;
4678 }
4679 }
4680 else
4681 {
4682 /* Get the next character from a Lisp string that is not an
4683 overlay string. Such strings come from the mode line, for
4684 example. We may have to pad with spaces, or truncate the
4685 string. See also next_element_from_c_string. */
4686 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4687 {
4688 it->what = IT_EOB;
4689 return 0;
4690 }
4691 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4692 {
4693 /* Pad with spaces. */
4694 it->c = ' ', it->len = 1;
4695 CHARPOS (position) = BYTEPOS (position) = -1;
4696 }
4697 else if (STRING_MULTIBYTE (it->string))
4698 {
2051c264 4699 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
4700 const unsigned char *s = (SDATA (it->string)
4701 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4702 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4703 }
4704 else
4705 {
2051c264 4706 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
4707 it->len = 1;
4708 }
4709 }
4710
4711 /* Record what we have and where it came from. Note that we store a
4712 buffer position in IT->position although it could arguably be a
4713 string position. */
4714 it->what = IT_CHARACTER;
4715 it->object = it->string;
4716 it->position = position;
4717 return 1;
4718}
4719
4720
4721/* Load IT with next display element from C string IT->s.
4722 IT->string_nchars is the maximum number of characters to return
4723 from the string. IT->end_charpos may be greater than
4724 IT->string_nchars when this function is called, in which case we
4725 may have to return padding spaces. Value is zero if end of string
4726 reached, including padding spaces. */
4727
4728static int
4729next_element_from_c_string (it)
4730 struct it *it;
4731{
4732 int success_p = 1;
2311178e 4733
5f5c8ee5
GM
4734 xassert (it->s);
4735 it->what = IT_CHARACTER;
4736 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4737 it->object = Qnil;
2311178e 4738
5f5c8ee5
GM
4739 /* IT's position can be greater IT->string_nchars in case a field
4740 width or precision has been specified when the iterator was
4741 initialized. */
4742 if (IT_CHARPOS (*it) >= it->end_charpos)
4743 {
4744 /* End of the game. */
4745 it->what = IT_EOB;
4746 success_p = 0;
4747 }
4748 else if (IT_CHARPOS (*it) >= it->string_nchars)
4749 {
4750 /* Pad with spaces. */
4751 it->c = ' ', it->len = 1;
4752 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4753 }
4754 else if (it->multibyte_p)
4755 {
4756 /* Implementation note: The calls to strlen apparently aren't a
4757 performance problem because there is no noticeable performance
4758 difference between Emacs running in unibyte or multibyte mode. */
4759 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4760 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4761 maxlen, &it->len);
5f5c8ee5
GM
4762 }
4763 else
4764 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 4765
5f5c8ee5
GM
4766 return success_p;
4767}
4768
4769
4770/* Set up IT to return characters from an ellipsis, if appropriate.
4771 The definition of the ellipsis glyphs may come from a display table
4772 entry. This function Fills IT with the first glyph from the
4773 ellipsis if an ellipsis is to be displayed. */
4774
13f19968 4775static int
5f5c8ee5
GM
4776next_element_from_ellipsis (it)
4777 struct it *it;
4778{
13f19968 4779 if (it->selective_display_ellipsis_p)
5f5c8ee5 4780 {
13f19968
GM
4781 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4782 {
4783 /* Use the display table definition for `...'. Invalid glyphs
4784 will be handled by the method returning elements from dpvec. */
4785 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4786 it->dpvec_char_len = it->len;
4787 it->dpvec = v->contents;
4788 it->dpend = v->contents + v->size;
4789 it->current.dpvec_index = 0;
4790 it->method = next_element_from_display_vector;
4791 }
4792 else
4793 {
4794 /* Use default `...' which is stored in default_invis_vector. */
4795 it->dpvec_char_len = it->len;
4796 it->dpvec = default_invis_vector;
4797 it->dpend = default_invis_vector + 3;
4798 it->current.dpvec_index = 0;
4799 it->method = next_element_from_display_vector;
4800 }
5f5c8ee5 4801 }
13f19968 4802 else
54918e2b 4803 {
4aad61f8
GM
4804 /* The face at the current position may be different from the
4805 face we find after the invisible text. Remember what it
4806 was in IT->saved_face_id, and signal that it's there by
4807 setting face_before_selective_p. */
4808 it->saved_face_id = it->face_id;
54918e2b
GM
4809 it->method = next_element_from_buffer;
4810 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4811 it->face_before_selective_p = 1;
54918e2b 4812 }
2311178e 4813
13f19968 4814 return get_next_display_element (it);
5f5c8ee5
GM
4815}
4816
4817
4818/* Deliver an image display element. The iterator IT is already
4819 filled with image information (done in handle_display_prop). Value
4820 is always 1. */
2311178e 4821
5f5c8ee5
GM
4822
4823static int
4824next_element_from_image (it)
4825 struct it *it;
4826{
4827 it->what = IT_IMAGE;
4828 return 1;
4829}
4830
4831
4832/* Fill iterator IT with next display element from a stretch glyph
4833 property. IT->object is the value of the text property. Value is
4834 always 1. */
4835
4836static int
4837next_element_from_stretch (it)
4838 struct it *it;
4839{
4840 it->what = IT_STRETCH;
4841 return 1;
4842}
4843
4844
4845/* Load IT with the next display element from current_buffer. Value
4846 is zero if end of buffer reached. IT->stop_charpos is the next
4847 position at which to stop and check for text properties or buffer
4848 end. */
4849
4850static int
4851next_element_from_buffer (it)
4852 struct it *it;
4853{
4854 int success_p = 1;
4855
4856 /* Check this assumption, otherwise, we would never enter the
4857 if-statement, below. */
4858 xassert (IT_CHARPOS (*it) >= BEGV
4859 && IT_CHARPOS (*it) <= it->stop_charpos);
4860
4861 if (IT_CHARPOS (*it) >= it->stop_charpos)
4862 {
4863 if (IT_CHARPOS (*it) >= it->end_charpos)
4864 {
4865 int overlay_strings_follow_p;
2311178e 4866
5f5c8ee5
GM
4867 /* End of the game, except when overlay strings follow that
4868 haven't been returned yet. */
4869 if (it->overlay_strings_at_end_processed_p)
4870 overlay_strings_follow_p = 0;
4871 else
4872 {
4873 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 4874 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
4875 }
4876
4877 if (overlay_strings_follow_p)
4878 success_p = get_next_display_element (it);
4879 else
4880 {
4881 it->what = IT_EOB;
4882 it->position = it->current.pos;
4883 success_p = 0;
4884 }
4885 }
4886 else
4887 {
4888 handle_stop (it);
4889 return get_next_display_element (it);
4890 }
4891 }
4892 else
4893 {
4894 /* No face changes, overlays etc. in sight, so just return a
4895 character from current_buffer. */
4896 unsigned char *p;
4897
4898 /* Maybe run the redisplay end trigger hook. Performance note:
4899 This doesn't seem to cost measurable time. */
4900 if (it->redisplay_end_trigger_charpos
4901 && it->glyph_row
4902 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4903 run_redisplay_end_trigger_hook (it);
4904
4905 /* Get the next character, maybe multibyte. */
4906 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4907 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4908 {
4909 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4910 - IT_BYTEPOS (*it));
4fdb80f2 4911 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4912 }
4913 else
4914 it->c = *p, it->len = 1;
4915
4916 /* Record what we have and where it came from. */
4917 it->what = IT_CHARACTER;;
4918 it->object = it->w->buffer;
4919 it->position = it->current.pos;
4920
4921 /* Normally we return the character found above, except when we
4922 really want to return an ellipsis for selective display. */
4923 if (it->selective)
4924 {
4925 if (it->c == '\n')
4926 {
4927 /* A value of selective > 0 means hide lines indented more
4928 than that number of columns. */
4929 if (it->selective > 0
4930 && IT_CHARPOS (*it) + 1 < ZV
4931 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4932 IT_BYTEPOS (*it) + 1,
a67e162b 4933 (double) it->selective)) /* iftc */
312246d1 4934 {
13f19968 4935 success_p = next_element_from_ellipsis (it);
312246d1
GM
4936 it->dpvec_char_len = -1;
4937 }
5f5c8ee5
GM
4938 }
4939 else if (it->c == '\r' && it->selective == -1)
4940 {
4941 /* A value of selective == -1 means that everything from the
4942 CR to the end of the line is invisible, with maybe an
4943 ellipsis displayed for it. */
13f19968 4944 success_p = next_element_from_ellipsis (it);
312246d1 4945 it->dpvec_char_len = -1;
5f5c8ee5
GM
4946 }
4947 }
4948 }
4949
4950 /* Value is zero if end of buffer reached. */
c880678e 4951 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4952 return success_p;
4953}
4954
2311178e 4955
5f5c8ee5
GM
4956/* Run the redisplay end trigger hook for IT. */
4957
4958static void
4959run_redisplay_end_trigger_hook (it)
4960 struct it *it;
4961{
4962 Lisp_Object args[3];
4963
4964 /* IT->glyph_row should be non-null, i.e. we should be actually
4965 displaying something, or otherwise we should not run the hook. */
4966 xassert (it->glyph_row);
4967
4968 /* Set up hook arguments. */
4969 args[0] = Qredisplay_end_trigger_functions;
4970 args[1] = it->window;
4971 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4972 it->redisplay_end_trigger_charpos = 0;
4973
4974 /* Since we are *trying* to run these functions, don't try to run
4975 them again, even if they get an error. */
4976 it->w->redisplay_end_trigger = Qnil;
4977 Frun_hook_with_args (3, args);
2311178e 4978
5f5c8ee5
GM
4979 /* Notice if it changed the face of the character we are on. */
4980 handle_face_prop (it);
4981}
4982
4983
260a86a0
KH
4984/* Deliver a composition display element. The iterator IT is already
4985 filled with composition information (done in
4986 handle_composition_prop). Value is always 1. */
4987
4988static int
4989next_element_from_composition (it)
4990 struct it *it;
4991{
4992 it->what = IT_COMPOSITION;
4993 it->position = (STRINGP (it->string)
4994 ? it->current.string_pos
4995 : it->current.pos);
4996 return 1;
4997}
4998
4999
5f5c8ee5
GM
5000\f
5001/***********************************************************************
5002 Moving an iterator without producing glyphs
5003 ***********************************************************************/
5004
5005/* Move iterator IT to a specified buffer or X position within one
5006 line on the display without producing glyphs.
5007
c53a1624
RS
5008 OP should be a bit mask including some or all of these bits:
5009 MOVE_TO_X: Stop on reaching x-position TO_X.
5010 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5011 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 5012
c53a1624
RS
5013 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5014 This means, in particular, that TO_X includes window's horizontal
5015 scroll amount.
5f5c8ee5 5016
c53a1624
RS
5017 The return value has several possible values that
5018 say what condition caused the scan to stop:
5f5c8ee5
GM
5019
5020 MOVE_POS_MATCH_OR_ZV
5021 - when TO_POS or ZV was reached.
2311178e 5022
5f5c8ee5
GM
5023 MOVE_X_REACHED
5024 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 5025
5f5c8ee5
GM
5026 MOVE_LINE_CONTINUED
5027 - when we reached the end of the display area and the line must
5028 be continued.
2311178e 5029
5f5c8ee5
GM
5030 MOVE_LINE_TRUNCATED
5031 - when we reached the end of the display area and the line is
5032 truncated.
5033
5034 MOVE_NEWLINE_OR_CR
5035 - when we stopped at a line end, i.e. a newline or a CR and selective
5036 display is on. */
5037
701552dd 5038static enum move_it_result
5f5c8ee5
GM
5039move_it_in_display_line_to (it, to_charpos, to_x, op)
5040 struct it *it;
5041 int to_charpos, to_x, op;
5042{
5043 enum move_it_result result = MOVE_UNDEFINED;
5044 struct glyph_row *saved_glyph_row;
5045
5046 /* Don't produce glyphs in produce_glyphs. */
5047 saved_glyph_row = it->glyph_row;
5048 it->glyph_row = NULL;
5049
5f5c8ee5
GM
5050 while (1)
5051 {
ae26e27d 5052 int x, i, ascent = 0, descent = 0;
2311178e 5053
5f5c8ee5
GM
5054 /* Stop when ZV or TO_CHARPOS reached. */
5055 if (!get_next_display_element (it)
5056 || ((op & MOVE_TO_POS) != 0
5057 && BUFFERP (it->object)
5058 && IT_CHARPOS (*it) >= to_charpos))
5059 {
5060 result = MOVE_POS_MATCH_OR_ZV;
5061 break;
5062 }
2311178e 5063
5f5c8ee5
GM
5064 /* The call to produce_glyphs will get the metrics of the
5065 display element IT is loaded with. We record in x the
5066 x-position before this display element in case it does not
5067 fit on the line. */
5068 x = it->current_x;
2311178e 5069
47589c8c
GM
5070 /* Remember the line height so far in case the next element doesn't
5071 fit on the line. */
5072 if (!it->truncate_lines_p)
5073 {
5074 ascent = it->max_ascent;
5075 descent = it->max_descent;
5076 }
2311178e 5077
5f5c8ee5
GM
5078 PRODUCE_GLYPHS (it);
5079
5080 if (it->area != TEXT_AREA)
5081 {
cafafe0b 5082 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5083 continue;
5084 }
5085
5086 /* The number of glyphs we get back in IT->nglyphs will normally
5087 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5088 character on a terminal frame, or (iii) a line end. For the
5089 second case, IT->nglyphs - 1 padding glyphs will be present
5090 (on X frames, there is only one glyph produced for a
5091 composite character.
5092
5093 The behavior implemented below means, for continuation lines,
5094 that as many spaces of a TAB as fit on the current line are
5095 displayed there. For terminal frames, as many glyphs of a
5096 multi-glyph character are displayed in the current line, too.
5097 This is what the old redisplay code did, and we keep it that
5098 way. Under X, the whole shape of a complex character must
5099 fit on the line or it will be completely displayed in the
5100 next line.
5101
5102 Note that both for tabs and padding glyphs, all glyphs have
5103 the same width. */
5104 if (it->nglyphs)
5105 {
5106 /* More than one glyph or glyph doesn't fit on line. All
5107 glyphs have the same width. */
5108 int single_glyph_width = it->pixel_width / it->nglyphs;
5109 int new_x;
2311178e 5110
5f5c8ee5
GM
5111 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5112 {
5113 new_x = x + single_glyph_width;
5114
5115 /* We want to leave anything reaching TO_X to the caller. */
5116 if ((op & MOVE_TO_X) && new_x > to_x)
5117 {
5118 it->current_x = x;
5119 result = MOVE_X_REACHED;
5120 break;
5121 }
5122 else if (/* Lines are continued. */
5123 !it->truncate_lines_p
5124 && (/* And glyph doesn't fit on the line. */
5125 new_x > it->last_visible_x
5126 /* Or it fits exactly and we're on a window
5127 system frame. */
5128 || (new_x == it->last_visible_x
5129 && FRAME_WINDOW_P (it->f))))
5130 {
5131 if (/* IT->hpos == 0 means the very first glyph
5132 doesn't fit on the line, e.g. a wide image. */
5133 it->hpos == 0
5134 || (new_x == it->last_visible_x
5135 && FRAME_WINDOW_P (it->f)))
5136 {
5137 ++it->hpos;
5138 it->current_x = new_x;
5139 if (i == it->nglyphs - 1)
cafafe0b 5140 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5141 }
5142 else
47589c8c
GM
5143 {
5144 it->current_x = x;
5145 it->max_ascent = ascent;
5146 it->max_descent = descent;
5147 }
2311178e 5148
47589c8c
GM
5149 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5150 IT_CHARPOS (*it)));
5f5c8ee5
GM
5151 result = MOVE_LINE_CONTINUED;
5152 break;
5153 }
5154 else if (new_x > it->first_visible_x)
5155 {
5156 /* Glyph is visible. Increment number of glyphs that
5157 would be displayed. */
5158 ++it->hpos;
5159 }
5160 else
5161 {
2311178e 5162 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
5163 area. Nothing to do. */
5164 }
5165 }
5166
5167 if (result != MOVE_UNDEFINED)
5168 break;
5169 }
5170 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5171 {
5172 /* Stop when TO_X specified and reached. This check is
5173 necessary here because of lines consisting of a line end,
5174 only. The line end will not produce any glyphs and we
5175 would never get MOVE_X_REACHED. */
5176 xassert (it->nglyphs == 0);
5177 result = MOVE_X_REACHED;
5178 break;
5179 }
2311178e 5180
5f5c8ee5
GM
5181 /* Is this a line end? If yes, we're done. */
5182 if (ITERATOR_AT_END_OF_LINE_P (it))
5183 {
5184 result = MOVE_NEWLINE_OR_CR;
5185 break;
5186 }
2311178e 5187
5f5c8ee5
GM
5188 /* The current display element has been consumed. Advance
5189 to the next. */
cafafe0b 5190 set_iterator_to_next (it, 1);
2311178e 5191
5f5c8ee5
GM
5192 /* Stop if lines are truncated and IT's current x-position is
5193 past the right edge of the window now. */
5194 if (it->truncate_lines_p
5195 && it->current_x >= it->last_visible_x)
5196 {
5197 result = MOVE_LINE_TRUNCATED;
5198 break;
5199 }
5200 }
5201
5202 /* Restore the iterator settings altered at the beginning of this
5203 function. */
5204 it->glyph_row = saved_glyph_row;
5205 return result;
5206}
5207
5208
9b2bba76
RS
5209/* Move IT forward until it satisfies one or more of the criteria in
5210 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5211
5212 OP is a bit-mask that specifies where to stop, and in particular,
5213 which of those four position arguments makes a difference. See the
5214 description of enum move_operation_enum.
2311178e 5215
5f5c8ee5
GM
5216 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5217 screen line, this function will set IT to the next position >
5218 TO_CHARPOS. */
5219
5220void
5221move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5222 struct it *it;
5223 int to_charpos, to_x, to_y, to_vpos;
5224 int op;
5225{
5226 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5227 int line_height;
47589c8c 5228 int reached = 0;
5f5c8ee5 5229
47589c8c 5230 for (;;)
5f5c8ee5
GM
5231 {
5232 if (op & MOVE_TO_VPOS)
5233 {
5234 /* If no TO_CHARPOS and no TO_X specified, stop at the
5235 start of the line TO_VPOS. */
5236 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5237 {
5238 if (it->vpos == to_vpos)
47589c8c
GM
5239 {
5240 reached = 1;
5241 break;
5242 }
5243 else
5244 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5245 }
5246 else
5247 {
5248 /* TO_VPOS >= 0 means stop at TO_X in the line at
5249 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5250 if (it->vpos == to_vpos)
5251 {
5252 reached = 2;
5253 break;
5254 }
2311178e 5255
5f5c8ee5
GM
5256 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5257
5258 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5259 {
5260 reached = 3;
5261 break;
5262 }
5f5c8ee5
GM
5263 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5264 {
5265 /* We have reached TO_X but not in the line we want. */
5266 skip = move_it_in_display_line_to (it, to_charpos,
5267 -1, MOVE_TO_POS);
5268 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5269 {
5270 reached = 4;
5271 break;
5272 }
5f5c8ee5
GM
5273 }
5274 }
5275 }
5276 else if (op & MOVE_TO_Y)
5277 {
5278 struct it it_backup;
2311178e 5279
5f5c8ee5
GM
5280 /* TO_Y specified means stop at TO_X in the line containing
5281 TO_Y---or at TO_CHARPOS if this is reached first. The
5282 problem is that we can't really tell whether the line
5283 contains TO_Y before we have completely scanned it, and
5284 this may skip past TO_X. What we do is to first scan to
5285 TO_X.
5286
5287 If TO_X is not specified, use a TO_X of zero. The reason
5288 is to make the outcome of this function more predictable.
5289 If we didn't use TO_X == 0, we would stop at the end of
5290 the line which is probably not what a caller would expect
5291 to happen. */
5292 skip = move_it_in_display_line_to (it, to_charpos,
5293 ((op & MOVE_TO_X)
5294 ? to_x : 0),
5295 (MOVE_TO_X
5296 | (op & MOVE_TO_POS)));
5297
5298 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5299 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5300 {
5301 reached = 5;
5302 break;
5303 }
2311178e 5304
5f5c8ee5
GM
5305 /* If TO_X was reached, we would like to know whether TO_Y
5306 is in the line. This can only be said if we know the
5307 total line height which requires us to scan the rest of
5308 the line. */
5f5c8ee5
GM
5309 if (skip == MOVE_X_REACHED)
5310 {
5311 it_backup = *it;
47589c8c 5312 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5313 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5314 op & MOVE_TO_POS);
47589c8c 5315 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5316 }
5317
5318 /* Now, decide whether TO_Y is in this line. */
5319 line_height = it->max_ascent + it->max_descent;
47589c8c 5320 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 5321
5f5c8ee5
GM
5322 if (to_y >= it->current_y
5323 && to_y < it->current_y + line_height)
5324 {
5325 if (skip == MOVE_X_REACHED)
5326 /* If TO_Y is in this line and TO_X was reached above,
5327 we scanned too far. We have to restore IT's settings
5328 to the ones before skipping. */
5329 *it = it_backup;
47589c8c 5330 reached = 6;
5f5c8ee5
GM
5331 }
5332 else if (skip == MOVE_X_REACHED)
5333 {
5334 skip = skip2;
5335 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5336 reached = 7;
5f5c8ee5
GM
5337 }
5338
47589c8c 5339 if (reached)
5f5c8ee5
GM
5340 break;
5341 }
5342 else
5343 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5344
5345 switch (skip)
5346 {
5347 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5348 reached = 8;
5349 goto out;
5f5c8ee5
GM
5350
5351 case MOVE_NEWLINE_OR_CR:
cafafe0b 5352 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5353 it->continuation_lines_width = 0;
5354 break;
5355
5356 case MOVE_LINE_TRUNCATED:
5357 it->continuation_lines_width = 0;
312246d1 5358 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5359 if ((op & MOVE_TO_POS) != 0
5360 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5361 {
5362 reached = 9;
5363 goto out;
5364 }
5f5c8ee5
GM
5365 break;
5366
5367 case MOVE_LINE_CONTINUED:
5368 it->continuation_lines_width += it->current_x;
5369 break;
5370
5371 default:
5372 abort ();
5373 }
5374
5375 /* Reset/increment for the next run. */
5376 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5377 it->current_x = it->hpos = 0;
5378 it->current_y += it->max_ascent + it->max_descent;
5379 ++it->vpos;
5380 last_height = it->max_ascent + it->max_descent;
5381 last_max_ascent = it->max_ascent;
5382 it->max_ascent = it->max_descent = 0;
5383 }
2311178e 5384
47589c8c
GM
5385 out:
5386
5387 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5388}
5389
5390
5391/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5392
5393 If DY > 0, move IT backward at least that many pixels. DY = 0
5394 means move IT backward to the preceding line start or BEGV. This
5395 function may move over more than DY pixels if IT->current_y - DY
5396 ends up in the middle of a line; in this case IT->current_y will be
5397 set to the top of the line moved to. */
5398
5399void
5400move_it_vertically_backward (it, dy)
5401 struct it *it;
5402 int dy;
5403{
79ddf6f7
GM
5404 int nlines, h;
5405 struct it it2, it3;
5f5c8ee5 5406 int start_pos = IT_CHARPOS (*it);
2311178e 5407
5f5c8ee5
GM
5408 xassert (dy >= 0);
5409
5410 /* Estimate how many newlines we must move back. */
5411 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5412
5413 /* Set the iterator's position that many lines back. */
5414 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5415 back_to_previous_visible_line_start (it);
5416
5417 /* Reseat the iterator here. When moving backward, we don't want
5418 reseat to skip forward over invisible text, set up the iterator
5419 to deliver from overlay strings at the new position etc. So,
5420 use reseat_1 here. */
5421 reseat_1 (it, it->current.pos, 1);
5422
5423 /* We are now surely at a line start. */
5424 it->current_x = it->hpos = 0;
0e47bbf7 5425 it->continuation_lines_width = 0;
5f5c8ee5
GM
5426
5427 /* Move forward and see what y-distance we moved. First move to the
5428 start of the next line so that we get its height. We need this
5429 height to be able to tell whether we reached the specified
5430 y-distance. */
5431 it2 = *it;
5432 it2.max_ascent = it2.max_descent = 0;
5433 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5434 MOVE_TO_POS | MOVE_TO_VPOS);
5435 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 5436 it3 = it2;
2311178e 5437
5f5c8ee5
GM
5438 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5439 xassert (IT_CHARPOS (*it) >= BEGV);
5440 h = it2.current_y - it->current_y;
5441 nlines = it2.vpos - it->vpos;
5442
5443 /* Correct IT's y and vpos position. */
5444 it->vpos -= nlines;
5445 it->current_y -= h;
2311178e 5446
5f5c8ee5
GM
5447 if (dy == 0)
5448 {
5449 /* DY == 0 means move to the start of the screen line. The
5450 value of nlines is > 0 if continuation lines were involved. */
5451 if (nlines > 0)
5452 move_it_by_lines (it, nlines, 1);
5453 xassert (IT_CHARPOS (*it) <= start_pos);
5454 }
5455 else if (nlines)
5456 {
5457 /* The y-position we try to reach. Note that h has been
5458 subtracted in front of the if-statement. */
5459 int target_y = it->current_y + h - dy;
79ddf6f7
GM
5460 int y0 = it3.current_y;
5461 int y1 = line_bottom_y (&it3);
5462 int line_height = y1 - y0;
f7ccfc8c 5463
5f5c8ee5
GM
5464 /* If we did not reach target_y, try to move further backward if
5465 we can. If we moved too far backward, try to move forward. */
5466 if (target_y < it->current_y
79ddf6f7
GM
5467 /* This is heuristic. In a window that's 3 lines high, with
5468 a line height of 13 pixels each, recentering with point
5469 on the bottom line will try to move -39/2 = 19 pixels
5470 backward. Try to avoid moving into the first line. */
798dbe1f 5471 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
5472 && IT_CHARPOS (*it) > BEGV)
5473 {
f7ccfc8c
GM
5474 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5475 target_y - it->current_y));
5f5c8ee5
GM
5476 move_it_vertically (it, target_y - it->current_y);
5477 xassert (IT_CHARPOS (*it) >= BEGV);
5478 }
5479 else if (target_y >= it->current_y + line_height
5480 && IT_CHARPOS (*it) < ZV)
5481 {
55591976 5482 /* Should move forward by at least one line, maybe more.
2311178e 5483
55591976
GM
5484 Note: Calling move_it_by_lines can be expensive on
5485 terminal frames, where compute_motion is used (via
5486 vmotion) to do the job, when there are very long lines
5487 and truncate-lines is nil. That's the reason for
5488 treating terminal frames specially here. */
2311178e 5489
55591976
GM
5490 if (!FRAME_WINDOW_P (it->f))
5491 move_it_vertically (it, target_y - (it->current_y + line_height));
5492 else
f7ccfc8c 5493 {
55591976
GM
5494 do
5495 {
5496 move_it_by_lines (it, 1, 1);
5497 }
5498 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 5499 }
f7ccfc8c 5500
5f5c8ee5
GM
5501 xassert (IT_CHARPOS (*it) >= BEGV);
5502 }
5503 }
5504}
5505
5506
5507/* Move IT by a specified amount of pixel lines DY. DY negative means
5508 move backwards. DY = 0 means move to start of screen line. At the
5509 end, IT will be on the start of a screen line. */
5510
2311178e 5511void
5f5c8ee5
GM
5512move_it_vertically (it, dy)
5513 struct it *it;
5514 int dy;
5515{
5516 if (dy <= 0)
5517 move_it_vertically_backward (it, -dy);
5518 else if (dy > 0)
5519 {
47589c8c 5520 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5521 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5522 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5523 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5524
5525 /* If buffer ends in ZV without a newline, move to the start of
5526 the line to satisfy the post-condition. */
5527 if (IT_CHARPOS (*it) == ZV
5528 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5529 move_it_by_lines (it, 0, 0);
5530 }
5531}
5532
5533
47fc2c10
GM
5534/* Move iterator IT past the end of the text line it is in. */
5535
5536void
5537move_it_past_eol (it)
5538 struct it *it;
5539{
5540 enum move_it_result rc;
2311178e 5541
47fc2c10
GM
5542 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5543 if (rc == MOVE_NEWLINE_OR_CR)
5544 set_iterator_to_next (it, 0);
5545}
5546
5547
2c79b732
GM
5548#if 0 /* Currently not used. */
5549
5f5c8ee5
GM
5550/* Return non-zero if some text between buffer positions START_CHARPOS
5551 and END_CHARPOS is invisible. IT->window is the window for text
5552 property lookup. */
5553
5554static int
5555invisible_text_between_p (it, start_charpos, end_charpos)
5556 struct it *it;
5557 int start_charpos, end_charpos;
5558{
5f5c8ee5
GM
5559 Lisp_Object prop, limit;
5560 int invisible_found_p;
2311178e 5561
5f5c8ee5
GM
5562 xassert (it != NULL && start_charpos <= end_charpos);
5563
5564 /* Is text at START invisible? */
5565 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5566 it->window);
5567 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5568 invisible_found_p = 1;
5569 else
5570 {
016b5642
MB
5571 limit = Fnext_single_char_property_change (make_number (start_charpos),
5572 Qinvisible, Qnil,
5573 make_number (end_charpos));
5f5c8ee5
GM
5574 invisible_found_p = XFASTINT (limit) < end_charpos;
5575 }
5576
5577 return invisible_found_p;
5f5c8ee5
GM
5578}
5579
2c79b732
GM
5580#endif /* 0 */
5581
5f5c8ee5
GM
5582
5583/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5584 negative means move up. DVPOS == 0 means move to the start of the
5585 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5586 NEED_Y_P is zero, IT->current_y will be left unchanged.
5587
5588 Further optimization ideas: If we would know that IT->f doesn't use
5589 a face with proportional font, we could be faster for
5590 truncate-lines nil. */
5591
5592void
5593move_it_by_lines (it, dvpos, need_y_p)
5594 struct it *it;
5595 int dvpos, need_y_p;
5596{
5597 struct position pos;
2311178e 5598
5f5c8ee5
GM
5599 if (!FRAME_WINDOW_P (it->f))
5600 {
5601 struct text_pos textpos;
2311178e 5602
5f5c8ee5
GM
5603 /* We can use vmotion on frames without proportional fonts. */
5604 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5605 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5606 reseat (it, textpos, 1);
5607 it->vpos += pos.vpos;
5608 it->current_y += pos.vpos;
5609 }
5610 else if (dvpos == 0)
5611 {
5612 /* DVPOS == 0 means move to the start of the screen line. */
5613 move_it_vertically_backward (it, 0);
5614 xassert (it->current_x == 0 && it->hpos == 0);
5615 }
5616 else if (dvpos > 0)
2c79b732 5617 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5618 else
5619 {
5620 struct it it2;
5621 int start_charpos, i;
2311178e 5622
e8660d73
GM
5623 /* Start at the beginning of the screen line containing IT's
5624 position. */
5625 move_it_vertically_backward (it, 0);
2311178e 5626
5f5c8ee5
GM
5627 /* Go back -DVPOS visible lines and reseat the iterator there. */
5628 start_charpos = IT_CHARPOS (*it);
5629 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5630 back_to_previous_visible_line_start (it);
5631 reseat (it, it->current.pos, 1);
5632 it->current_x = it->hpos = 0;
5633
5634 /* Above call may have moved too far if continuation lines
5635 are involved. Scan forward and see if it did. */
5636 it2 = *it;
5637 it2.vpos = it2.current_y = 0;
5638 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5639 it->vpos -= it2.vpos;
5640 it->current_y -= it2.current_y;
5641 it->current_x = it->hpos = 0;
5642
5643 /* If we moved too far, move IT some lines forward. */
5644 if (it2.vpos > -dvpos)
5645 {
5646 int delta = it2.vpos + dvpos;
5647 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5648 }
5649 }
5650}
5651
5652
5653\f
5654/***********************************************************************
5655 Messages
5656 ***********************************************************************/
5657
5658
937248bc
GM
5659/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5660 to *Messages*. */
5661
5662void
5663add_to_log (format, arg1, arg2)
5664 char *format;
5665 Lisp_Object arg1, arg2;
5666{
5667 Lisp_Object args[3];
5668 Lisp_Object msg, fmt;
5669 char *buffer;
5670 int len;
5671 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5672
ae794295
GM
5673 /* Do nothing if called asynchronously. Inserting text into
5674 a buffer may call after-change-functions and alike and
5675 that would means running Lisp asynchronously. */
5676 if (handling_signal)
5677 return;
5678
937248bc
GM
5679 fmt = msg = Qnil;
5680 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 5681
937248bc
GM
5682 args[0] = fmt = build_string (format);
5683 args[1] = arg1;
5684 args[2] = arg2;
6fc556fd 5685 msg = Fformat (3, args);
937248bc 5686
2051c264 5687 len = SBYTES (msg) + 1;
937248bc 5688 buffer = (char *) alloca (len);
2051c264 5689 bcopy (SDATA (msg), buffer, len);
2311178e 5690
796184bc 5691 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5692 UNGCPRO;
5693}
5694
5695
5f5c8ee5
GM
5696/* Output a newline in the *Messages* buffer if "needs" one. */
5697
5698void
5699message_log_maybe_newline ()
5700{
5701 if (message_log_need_newline)
5702 message_dolog ("", 0, 1, 0);
5703}
5704
5705
1e313f28 5706/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5707 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5708 nonzero, means interpret the contents of M as multibyte. This
5709 function calls low-level routines in order to bypass text property
5710 hooks, etc. which might not be safe to run. */
5711
5712void
1e313f28 5713message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 5714 const char *m;
1e313f28 5715 int nbytes, nlflag, multibyte;
5f5c8ee5 5716{
a67e162b
RS
5717 if (!NILP (Vmemory_full))
5718 return;
5719
5f5c8ee5
GM
5720 if (!NILP (Vmessage_log_max))
5721 {
5722 struct buffer *oldbuf;
5723 Lisp_Object oldpoint, oldbegv, oldzv;
5724 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5725 int point_at_end = 0;
5726 int zv_at_end = 0;
5727 Lisp_Object old_deactivate_mark, tem;
6052529b 5728 struct gcpro gcpro1;
5f5c8ee5
GM
5729
5730 old_deactivate_mark = Vdeactivate_mark;
5731 oldbuf = current_buffer;
6a94510a 5732 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5733 current_buffer->undo_list = Qt;
5734
b14bc55e
RS
5735 oldpoint = message_dolog_marker1;
5736 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5737 oldbegv = message_dolog_marker2;
5738 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5739 oldzv = message_dolog_marker3;
5740 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5741 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
5742
5743 if (PT == Z)
5744 point_at_end = 1;
5745 if (ZV == Z)
5746 zv_at_end = 1;
5747
5748 BEGV = BEG;
5749 BEGV_BYTE = BEG_BYTE;
5750 ZV = Z;
5751 ZV_BYTE = Z_BYTE;
5752 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5753
5754 /* Insert the string--maybe converting multibyte to single byte
5755 or vice versa, so that all the text fits the buffer. */
5756 if (multibyte
5757 && NILP (current_buffer->enable_multibyte_characters))
5758 {
1e313f28 5759 int i, c, char_bytes;
5f5c8ee5 5760 unsigned char work[1];
2311178e 5761
5f5c8ee5
GM
5762 /* Convert a multibyte string to single-byte
5763 for the *Message* buffer. */
6e57ec5e 5764 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 5765 {
1e313f28 5766 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5767 work[0] = (SINGLE_BYTE_CHAR_P (c)
5768 ? c
5769 : multibyte_char_to_unibyte (c, Qnil));
5770 insert_1_both (work, 1, 1, 1, 0, 0);
5771 }
5772 }
5773 else if (! multibyte
5774 && ! NILP (current_buffer->enable_multibyte_characters))
5775 {
1e313f28 5776 int i, c, char_bytes;
5f5c8ee5 5777 unsigned char *msg = (unsigned char *) m;
260a86a0 5778 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5779 /* Convert a single-byte string to multibyte
5780 for the *Message* buffer. */
1e313f28 5781 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5782 {
5783 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5784 char_bytes = CHAR_STRING (c, str);
5785 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5786 }
5787 }
1e313f28
GM
5788 else if (nbytes)
5789 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5790
5791 if (nlflag)
5792 {
5793 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5794 insert_1 ("\n", 1, 1, 0, 0);
5795
5796 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5797 this_bol = PT;
5798 this_bol_byte = PT_BYTE;
5799
b14bc55e
RS
5800 /* See if this line duplicates the previous one.
5801 If so, combine duplicates. */
5f5c8ee5
GM
5802 if (this_bol > BEG)
5803 {
5804 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5805 prev_bol = PT;
5806 prev_bol_byte = PT_BYTE;
5807
5808 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5809 this_bol, this_bol_byte);
5810 if (dup)
5811 {
5812 del_range_both (prev_bol, prev_bol_byte,
5813 this_bol, this_bol_byte, 0);
5814 if (dup > 1)
5815 {
5816 char dupstr[40];
5817 int duplen;
5818
5819 /* If you change this format, don't forget to also
5820 change message_log_check_duplicate. */
5821 sprintf (dupstr, " [%d times]", dup);
5822 duplen = strlen (dupstr);
5823 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5824 insert_1 (dupstr, duplen, 1, 0, 1);
5825 }
5826 }
5827 }
5828
b14bc55e
RS
5829 /* If we have more than the desired maximum number of lines
5830 in the *Messages* buffer now, delete the oldest ones.
5831 This is safe because we don't have undo in this buffer. */
5832
5f5c8ee5
GM
5833 if (NATNUMP (Vmessage_log_max))
5834 {
5835 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5836 -XFASTINT (Vmessage_log_max) - 1, 0);
5837 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5838 }
5839 }
5840 BEGV = XMARKER (oldbegv)->charpos;
5841 BEGV_BYTE = marker_byte_position (oldbegv);
5842
5843 if (zv_at_end)
5844 {
5845 ZV = Z;
5846 ZV_BYTE = Z_BYTE;
5847 }
5848 else
5849 {
5850 ZV = XMARKER (oldzv)->charpos;
5851 ZV_BYTE = marker_byte_position (oldzv);
5852 }
5853
5854 if (point_at_end)
5855 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5856 else
5857 /* We can't do Fgoto_char (oldpoint) because it will run some
5858 Lisp code. */
5859 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5860 XMARKER (oldpoint)->bytepos);
5861
5862 UNGCPRO;
b14bc55e
RS
5863 unchain_marker (oldpoint);
5864 unchain_marker (oldbegv);
5865 unchain_marker (oldzv);
5f5c8ee5
GM
5866
5867 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5868 set_buffer_internal (oldbuf);
5869 if (NILP (tem))
5870 windows_or_buffers_changed = old_windows_or_buffers_changed;
5871 message_log_need_newline = !nlflag;
5872 Vdeactivate_mark = old_deactivate_mark;
5873 }
5874}
5875
5876
5877/* We are at the end of the buffer after just having inserted a newline.
5878 (Note: We depend on the fact we won't be crossing the gap.)
5879 Check to see if the most recent message looks a lot like the previous one.
5880 Return 0 if different, 1 if the new one should just replace it, or a
5881 value N > 1 if we should also append " [N times]". */
5882
5883static int
5884message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5885 int prev_bol, this_bol;
5886 int prev_bol_byte, this_bol_byte;
5887{
5888 int i;
5889 int len = Z_BYTE - 1 - this_bol_byte;
5890 int seen_dots = 0;
5891 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5892 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5893
5894 for (i = 0; i < len; i++)
5895 {
509633e3 5896 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5897 seen_dots = 1;
5898 if (p1[i] != p2[i])
5899 return seen_dots;
5900 }
5901 p1 += len;
5902 if (*p1 == '\n')
5903 return 2;
5904 if (*p1++ == ' ' && *p1++ == '[')
5905 {
5906 int n = 0;
5907 while (*p1 >= '0' && *p1 <= '9')
5908 n = n * 10 + *p1++ - '0';
5909 if (strncmp (p1, " times]\n", 8) == 0)
5910 return n+1;
5911 }
5912 return 0;
5913}
5914
5915
1e313f28
GM
5916/* Display an echo area message M with a specified length of NBYTES
5917 bytes. The string may include null characters. If M is 0, clear
5918 out any existing message, and let the mini-buffer text show
5919 through.
5f5c8ee5
GM
5920
5921 The buffer M must continue to exist until after the echo area gets
5922 cleared or some other message gets displayed there. This means do
5923 not pass text that is stored in a Lisp string; do not pass text in
5924 a buffer that was alloca'd. */
5925
5926void
1e313f28 5927message2 (m, nbytes, multibyte)
50f80c2f 5928 const char *m;
1e313f28 5929 int nbytes;
5f5c8ee5
GM
5930 int multibyte;
5931{
5932 /* First flush out any partial line written with print. */
5933 message_log_maybe_newline ();
5934 if (m)
1e313f28
GM
5935 message_dolog (m, nbytes, 1, multibyte);
5936 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5937}
5938
5939
5940/* The non-logging counterpart of message2. */
5941
5942void
1e313f28 5943message2_nolog (m, nbytes, multibyte)
50f80c2f 5944 const char *m;
e3383b6f 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 (m)
1e313f28 5956 fwrite (m, 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. */
2311178e 5964 else if (INTERACTIVE
886bd6f2
GM
5965 && sf->glyphs_initialized_p
5966 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5967 {
5968 Lisp_Object mini_window;
5969 struct frame *f;
5970
5971 /* Get the frame containing the mini-buffer
5972 that the selected frame is using. */
886bd6f2 5973 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5974 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5975
5976 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5977 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5978 && ! FRAME_VISIBLE_P (f))
5979 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5980
5981 if (m)
5982 {
1e313f28 5983 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5984 if (minibuffer_auto_raise)
5985 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5986 }
5987 else
c6e89d6c 5988 clear_message (1, 1);
5f5c8ee5 5989
c6e89d6c 5990 do_pending_window_change (0);
5f5c8ee5 5991 echo_area_display (1);
c6e89d6c 5992 do_pending_window_change (0);
5f5c8ee5
GM
5993 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5994 (*frame_up_to_date_hook) (f);
5995 }
5996}
5997
5998
c6e89d6c
GM
5999/* Display an echo area message M with a specified length of NBYTES
6000 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
6001 string, clear out any existing message, and let the mini-buffer
6002 text show through. */
6003
6004void
c6e89d6c 6005message3 (m, nbytes, multibyte)
5f5c8ee5 6006 Lisp_Object m;
c6e89d6c 6007 int nbytes;
5f5c8ee5
GM
6008 int multibyte;
6009{
6010 struct gcpro gcpro1;
6011
6012 GCPRO1 (m);
2311178e 6013
5f5c8ee5
GM
6014 /* First flush out any partial line written with print. */
6015 message_log_maybe_newline ();
6016 if (STRINGP (m))
2051c264 6017 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 6018 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6019
6020 UNGCPRO;
6021}
6022
6023
6024/* The non-logging version of message3. */
6025
6026void
c6e89d6c 6027message3_nolog (m, nbytes, multibyte)
5f5c8ee5 6028 Lisp_Object m;
c6e89d6c 6029 int nbytes, multibyte;
5f5c8ee5 6030{
886bd6f2 6031 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6032 message_enable_multibyte = multibyte;
6033
6034 if (noninteractive)
6035 {
6036 if (noninteractive_need_newline)
6037 putc ('\n', stderr);
6038 noninteractive_need_newline = 0;
6039 if (STRINGP (m))
2051c264 6040 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
6041 if (cursor_in_echo_area == 0)
6042 fprintf (stderr, "\n");
6043 fflush (stderr);
6044 }
6045 /* A null message buffer means that the frame hasn't really been
6046 initialized yet. Error messages get reported properly by
6047 cmd_error, so this must be just an informative message; toss it. */
2311178e 6048 else if (INTERACTIVE
886bd6f2
GM
6049 && sf->glyphs_initialized_p
6050 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6051 {
6052 Lisp_Object mini_window;
c6e89d6c 6053 Lisp_Object frame;
5f5c8ee5
GM
6054 struct frame *f;
6055
6056 /* Get the frame containing the mini-buffer
6057 that the selected frame is using. */
886bd6f2 6058 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6059 frame = XWINDOW (mini_window)->frame;
6060 f = XFRAME (frame);
5f5c8ee5
GM
6061
6062 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6063 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6064 && !FRAME_VISIBLE_P (f))
6065 Fmake_frame_visible (frame);
5f5c8ee5 6066
2051c264 6067 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6068 {
c6e89d6c 6069 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6070 if (minibuffer_auto_raise)
6071 Fraise_frame (frame);
5f5c8ee5
GM
6072 }
6073 else
c6e89d6c 6074 clear_message (1, 1);
5f5c8ee5 6075
c6e89d6c 6076 do_pending_window_change (0);
5f5c8ee5 6077 echo_area_display (1);
c6e89d6c 6078 do_pending_window_change (0);
5f5c8ee5
GM
6079 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6080 (*frame_up_to_date_hook) (f);
6081 }
6082}
6083
6084
6085/* Display a null-terminated echo area message M. If M is 0, clear
6086 out any existing message, and let the mini-buffer text show through.
6087
6088 The buffer M must continue to exist until after the echo area gets
6089 cleared or some other message gets displayed there. Do not pass
6090 text that is stored in a Lisp string. Do not pass text in a buffer
6091 that was alloca'd. */
6092
6093void
6094message1 (m)
6095 char *m;
6096{
6097 message2 (m, (m ? strlen (m) : 0), 0);
6098}
6099
6100
6101/* The non-logging counterpart of message1. */
6102
6103void
6104message1_nolog (m)
6105 char *m;
6106{
6107 message2_nolog (m, (m ? strlen (m) : 0), 0);
6108}
6109
6110/* Display a message M which contains a single %s
6111 which gets replaced with STRING. */
6112
6113void
6114message_with_string (m, string, log)
6115 char *m;
6116 Lisp_Object string;
6117 int log;
6118{
5b6d51b6
RS
6119 CHECK_STRING (string);
6120
5f5c8ee5
GM
6121 if (noninteractive)
6122 {
6123 if (m)
6124 {
6125 if (noninteractive_need_newline)
6126 putc ('\n', stderr);
6127 noninteractive_need_newline = 0;
2051c264 6128 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6129 if (cursor_in_echo_area == 0)
6130 fprintf (stderr, "\n");
6131 fflush (stderr);
6132 }
6133 }
6134 else if (INTERACTIVE)
6135 {
6136 /* The frame whose minibuffer we're going to display the message on.
6137 It may be larger than the selected frame, so we need
6138 to use its buffer, not the selected frame's buffer. */
6139 Lisp_Object mini_window;
886bd6f2 6140 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6141
6142 /* Get the frame containing the minibuffer
6143 that the selected frame is using. */
886bd6f2 6144 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6145 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6146
6147 /* A null message buffer means that the frame hasn't really been
6148 initialized yet. Error messages get reported properly by
6149 cmd_error, so this must be just an informative message; toss it. */
6150 if (FRAME_MESSAGE_BUF (f))
6151 {
eb484132
GM
6152 Lisp_Object args[2], message;
6153 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6154
eb484132
GM
6155 args[0] = build_string (m);
6156 args[1] = message = string;
78e17433 6157 GCPRO2 (args[0], message);
eb484132 6158 gcpro1.nvars = 2;
2311178e 6159
eb484132 6160 message = Fformat (2, args);
5f5c8ee5
GM
6161
6162 if (log)
d5db4077 6163 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 6164 else
d5db4077 6165 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
6166
6167 UNGCPRO;
5f5c8ee5
GM
6168
6169 /* Print should start at the beginning of the message
6170 buffer next time. */
6171 message_buf_print = 0;
6172 }
6173 }
6174}
6175
6176
5f5c8ee5
GM
6177/* Dump an informative message to the minibuf. If M is 0, clear out
6178 any existing message, and let the mini-buffer text show through. */
6179
6180/* VARARGS 1 */
6181void
6182message (m, a1, a2, a3)
6183 char *m;
6184 EMACS_INT a1, a2, a3;
6185{
6186 if (noninteractive)
6187 {
6188 if (m)
6189 {
6190 if (noninteractive_need_newline)
6191 putc ('\n', stderr);
6192 noninteractive_need_newline = 0;
6193 fprintf (stderr, m, a1, a2, a3);
6194 if (cursor_in_echo_area == 0)
6195 fprintf (stderr, "\n");
6196 fflush (stderr);
6197 }
6198 }
6199 else if (INTERACTIVE)
6200 {
6201 /* The frame whose mini-buffer we're going to display the message
6202 on. It may be larger than the selected frame, so we need to
6203 use its buffer, not the selected frame's buffer. */
6204 Lisp_Object mini_window;
886bd6f2 6205 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6206
6207 /* Get the frame containing the mini-buffer
6208 that the selected frame is using. */
886bd6f2 6209 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6210 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6211
6212 /* A null message buffer means that the frame hasn't really been
6213 initialized yet. Error messages get reported properly by
6214 cmd_error, so this must be just an informative message; toss
6215 it. */
6216 if (FRAME_MESSAGE_BUF (f))
6217 {
6218 if (m)
6219 {
6220 int len;
6221#ifdef NO_ARG_ARRAY
6222 char *a[3];
6223 a[0] = (char *) a1;
6224 a[1] = (char *) a2;
6225 a[2] = (char *) a3;
6226
6227 len = doprnt (FRAME_MESSAGE_BUF (f),
6228 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6229#else
6230 len = doprnt (FRAME_MESSAGE_BUF (f),
6231 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6232 (char **) &a1);
6233#endif /* NO_ARG_ARRAY */
6234
6235 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6236 }
6237 else
6238 message1 (0);
6239
6240 /* Print should start at the beginning of the message
6241 buffer next time. */
6242 message_buf_print = 0;
6243 }
6244 }
6245}
6246
6247
6248/* The non-logging version of message. */
6249
6250void
6251message_nolog (m, a1, a2, a3)
6252 char *m;
6253 EMACS_INT a1, a2, a3;
6254{
6255 Lisp_Object old_log_max;
6256 old_log_max = Vmessage_log_max;
6257 Vmessage_log_max = Qnil;
6258 message (m, a1, a2, a3);
6259 Vmessage_log_max = old_log_max;
6260}
6261
6262
c6e89d6c
GM
6263/* Display the current message in the current mini-buffer. This is
6264 only called from error handlers in process.c, and is not time
6265 critical. */
5f5c8ee5
GM
6266
6267void
6268update_echo_area ()
6269{
c6e89d6c
GM
6270 if (!NILP (echo_area_buffer[0]))
6271 {
6272 Lisp_Object string;
6273 string = Fcurrent_message ();
2311178e 6274 message3 (string, SBYTES (string),
c6e89d6c
GM
6275 !NILP (current_buffer->enable_multibyte_characters));
6276 }
6277}
6278
6279
a67e162b
RS
6280/* Make sure echo area buffers in `echo_buffers' are live.
6281 If they aren't, make new ones. */
5bcfeb49
GM
6282
6283static void
6284ensure_echo_area_buffers ()
6285{
6286 int i;
6287
6288 for (i = 0; i < 2; ++i)
6289 if (!BUFFERP (echo_buffer[i])
6290 || NILP (XBUFFER (echo_buffer[i])->name))
6291 {
6292 char name[30];
ff3d9573
GM
6293 Lisp_Object old_buffer;
6294 int j;
6295
6296 old_buffer = echo_buffer[i];
5bcfeb49
GM
6297 sprintf (name, " *Echo Area %d*", i);
6298 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6299 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6300
6301 for (j = 0; j < 2; ++j)
6302 if (EQ (old_buffer, echo_area_buffer[j]))
6303 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6304 }
6305}
6306
6307
23a96c77 6308/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6309 echo_area_buffer as current buffer.
6310
6311 WHICH zero means use the current message buffer
6312 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6313 from echo_buffer[] and clear it.
6314
6315 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6316 suitable buffer from echo_buffer[] and clear it.
6317
6318 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6319 that the current message becomes the last displayed one, make
6320 choose a suitable buffer for echo_area_buffer[0], and clear it.
6321
4b41cebb 6322 Value is what FN returns. */
c6e89d6c
GM
6323
6324static int
23a96c77 6325with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6326 struct window *w;
6327 int which;
23dd2d97
KR
6328 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6329 EMACS_INT a1;
6330 Lisp_Object a2;
6331 EMACS_INT a3, a4;
c6e89d6c
GM
6332{
6333 Lisp_Object buffer;
15e26c76 6334 int this_one, the_other, clear_buffer_p, rc;
331379bf 6335 int count = SPECPDL_INDEX ();
c6e89d6c 6336
9583b2bb 6337 /* If buffers aren't live, make new ones. */
5bcfeb49 6338 ensure_echo_area_buffers ();
c6e89d6c
GM
6339
6340 clear_buffer_p = 0;
2311178e 6341
c6e89d6c
GM
6342 if (which == 0)
6343 this_one = 0, the_other = 1;
6344 else if (which > 0)
6345 this_one = 1, the_other = 0;
5f5c8ee5 6346 else
c6e89d6c
GM
6347 {
6348 this_one = 0, the_other = 1;
6349 clear_buffer_p = 1;
2311178e 6350
c6e89d6c
GM
6351 /* We need a fresh one in case the current echo buffer equals
6352 the one containing the last displayed echo area message. */
6353 if (!NILP (echo_area_buffer[this_one])
6354 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6355 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6356 }
6357
6358 /* Choose a suitable buffer from echo_buffer[] is we don't
6359 have one. */
6360 if (NILP (echo_area_buffer[this_one]))
6361 {
6362 echo_area_buffer[this_one]
6363 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6364 ? echo_buffer[the_other]
6365 : echo_buffer[this_one]);
6366 clear_buffer_p = 1;
6367 }
6368
6369 buffer = echo_area_buffer[this_one];
6370
1013f4e3
GM
6371 /* Don't get confused by reusing the buffer used for echoing
6372 for a different purpose. */
032906b1 6373 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6374 cancel_echoing ();
6375
c6e89d6c
GM
6376 record_unwind_protect (unwind_with_echo_area_buffer,
6377 with_echo_area_buffer_unwind_data (w));
6378
6379 /* Make the echo area buffer current. Note that for display
6380 purposes, it is not necessary that the displayed window's buffer
6381 == current_buffer, except for text property lookup. So, let's
6382 only set that buffer temporarily here without doing a full
6383 Fset_window_buffer. We must also change w->pointm, though,
6384 because otherwise an assertions in unshow_buffer fails, and Emacs
6385 aborts. */
9142dd5b 6386 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6387 if (w)
6388 {
6389 w->buffer = buffer;
6390 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6391 }
ad4f174e 6392
c6e89d6c
GM
6393 current_buffer->undo_list = Qt;
6394 current_buffer->read_only = Qnil;
bbbf6d06 6395 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6396 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6397
6398 if (clear_buffer_p && Z > BEG)
6399 del_range (BEG, Z);
6400
6401 xassert (BEGV >= BEG);
6402 xassert (ZV <= Z && ZV >= BEGV);
6403
23a96c77 6404 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6405
6406 xassert (BEGV >= BEG);
6407 xassert (ZV <= Z && ZV >= BEGV);
6408
6409 unbind_to (count, Qnil);
6410 return rc;
5f5c8ee5
GM
6411}
6412
6413
c6e89d6c
GM
6414/* Save state that should be preserved around the call to the function
6415 FN called in with_echo_area_buffer. */
5f5c8ee5 6416
c6e89d6c
GM
6417static Lisp_Object
6418with_echo_area_buffer_unwind_data (w)
6419 struct window *w;
5f5c8ee5 6420{
c6e89d6c
GM
6421 int i = 0;
6422 Lisp_Object vector;
5f5c8ee5 6423
c6e89d6c
GM
6424 /* Reduce consing by keeping one vector in
6425 Vwith_echo_area_save_vector. */
6426 vector = Vwith_echo_area_save_vector;
6427 Vwith_echo_area_save_vector = Qnil;
2311178e 6428
c6e89d6c 6429 if (NILP (vector))
9142dd5b 6430 vector = Fmake_vector (make_number (7), Qnil);
2311178e 6431
a61b7058
GM
6432 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6433 AREF (vector, i) = Vdeactivate_mark, ++i;
6434 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 6435
c6e89d6c
GM
6436 if (w)
6437 {
a61b7058
GM
6438 XSETWINDOW (AREF (vector, i), w); ++i;
6439 AREF (vector, i) = w->buffer; ++i;
6440 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6441 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6442 }
6443 else
6444 {
6445 int end = i + 4;
a61b7058
GM
6446 for (; i < end; ++i)
6447 AREF (vector, i) = Qnil;
c6e89d6c 6448 }
5f5c8ee5 6449
a61b7058 6450 xassert (i == ASIZE (vector));
c6e89d6c
GM
6451 return vector;
6452}
5f5c8ee5 6453
5f5c8ee5 6454
c6e89d6c
GM
6455/* Restore global state from VECTOR which was created by
6456 with_echo_area_buffer_unwind_data. */
6457
6458static Lisp_Object
6459unwind_with_echo_area_buffer (vector)
6460 Lisp_Object vector;
6461{
bbbf6d06
GM
6462 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6463 Vdeactivate_mark = AREF (vector, 1);
6464 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6465
bbbf6d06 6466 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6467 {
6468 struct window *w;
6469 Lisp_Object buffer, charpos, bytepos;
2311178e 6470
bbbf6d06
GM
6471 w = XWINDOW (AREF (vector, 3));
6472 buffer = AREF (vector, 4);
6473 charpos = AREF (vector, 5);
6474 bytepos = AREF (vector, 6);
2311178e 6475
c6e89d6c
GM
6476 w->buffer = buffer;
6477 set_marker_both (w->pointm, buffer,
6478 XFASTINT (charpos), XFASTINT (bytepos));
6479 }
6480
6481 Vwith_echo_area_save_vector = vector;
6482 return Qnil;
6483}
6484
6485
6486/* Set up the echo area for use by print functions. MULTIBYTE_P
6487 non-zero means we will print multibyte. */
6488
6489void
6490setup_echo_area_for_printing (multibyte_p)
6491 int multibyte_p;
6492{
03b0a4b4
RS
6493 /* If we can't find an echo area any more, exit. */
6494 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6495 Fkill_emacs (Qnil);
6496
5bcfeb49
GM
6497 ensure_echo_area_buffers ();
6498
c6e89d6c
GM
6499 if (!message_buf_print)
6500 {
6501 /* A message has been output since the last time we printed.
6502 Choose a fresh echo area buffer. */
6503 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6504 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
6505 else
6506 echo_area_buffer[0] = echo_buffer[0];
6507
6508 /* Switch to that buffer and clear it. */
6509 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6510 current_buffer->truncate_lines = Qnil;
2311178e 6511
c6e89d6c 6512 if (Z > BEG)
bbbf6d06 6513 {
331379bf 6514 int count = SPECPDL_INDEX ();
bbbf6d06 6515 specbind (Qinhibit_read_only, Qt);
a67e162b 6516 /* Note that undo recording is always disabled. */
bbbf6d06
GM
6517 del_range (BEG, Z);
6518 unbind_to (count, Qnil);
6519 }
c6e89d6c
GM
6520 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6521
6522 /* Set up the buffer for the multibyteness we need. */
6523 if (multibyte_p
6524 != !NILP (current_buffer->enable_multibyte_characters))
6525 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6526
6527 /* Raise the frame containing the echo area. */
6528 if (minibuffer_auto_raise)
6529 {
886bd6f2 6530 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6531 Lisp_Object mini_window;
886bd6f2 6532 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6533 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6534 }
6535
8a4e3c0c 6536 message_log_maybe_newline ();
c6e89d6c
GM
6537 message_buf_print = 1;
6538 }
fa77249f
GM
6539 else
6540 {
6541 if (NILP (echo_area_buffer[0]))
6542 {
6543 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6544 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
6545 else
6546 echo_area_buffer[0] = echo_buffer[0];
6547 }
2311178e 6548
fa77249f 6549 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
6550 {
6551 /* Someone switched buffers between print requests. */
6552 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6553 current_buffer->truncate_lines = Qnil;
6554 }
fa77249f 6555 }
c6e89d6c
GM
6556}
6557
6558
dd2eb166
GM
6559/* Display an echo area message in window W. Value is non-zero if W's
6560 height is changed. If display_last_displayed_message_p is
6561 non-zero, display the message that was last displayed, otherwise
6562 display the current message. */
c6e89d6c
GM
6563
6564static int
6565display_echo_area (w)
6566 struct window *w;
6567{
25edb08f
GM
6568 int i, no_message_p, window_height_changed_p, count;
6569
6570 /* Temporarily disable garbage collections while displaying the echo
6571 area. This is done because a GC can print a message itself.
6572 That message would modify the echo area buffer's contents while a
6573 redisplay of the buffer is going on, and seriously confuse
6574 redisplay. */
6575 count = inhibit_garbage_collection ();
dd2eb166
GM
6576
6577 /* If there is no message, we must call display_echo_area_1
6578 nevertheless because it resizes the window. But we will have to
6579 reset the echo_area_buffer in question to nil at the end because
6580 with_echo_area_buffer will sets it to an empty buffer. */
6581 i = display_last_displayed_message_p ? 1 : 0;
6582 no_message_p = NILP (echo_area_buffer[i]);
2311178e 6583
dd2eb166
GM
6584 window_height_changed_p
6585 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6586 display_echo_area_1,
23dd2d97 6587 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6588
6589 if (no_message_p)
6590 echo_area_buffer[i] = Qnil;
25edb08f
GM
6591
6592 unbind_to (count, Qnil);
dd2eb166 6593 return window_height_changed_p;
c6e89d6c
GM
6594}
6595
6596
6597/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6598 contains the current echo area message in window W, a mini-window,
6599 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6600 Change the height of W so that all of the message is displayed.
6601 Value is non-zero if height of W was changed. */
6602
6603static int
23a96c77 6604display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6605 EMACS_INT a1;
6606 Lisp_Object a2;
6607 EMACS_INT a3, a4;
c6e89d6c 6608{
23a96c77 6609 struct window *w = (struct window *) a1;
c6e89d6c 6610 Lisp_Object window;
c6e89d6c
GM
6611 struct text_pos start;
6612 int window_height_changed_p = 0;
6613
6614 /* Do this before displaying, so that we have a large enough glyph
6615 matrix for the display. */
92a90e89 6616 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6617
6618 /* Display. */
6619 clear_glyph_matrix (w->desired_matrix);
6620 XSETWINDOW (window, w);
6621 SET_TEXT_POS (start, BEG, BEG_BYTE);
6622 try_window (window, start);
6623
c6e89d6c
GM
6624 return window_height_changed_p;
6625}
6626
6627
92a90e89 6628/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
6629 currently displayed message, if there is one. If a mini-buffer
6630 is active, don't shrink it. */
92a90e89
GM
6631
6632void
308a74d8 6633resize_echo_area_exactly ()
92a90e89
GM
6634{
6635 if (BUFFERP (echo_area_buffer[0])
6636 && WINDOWP (echo_area_window))
6637 {
6638 struct window *w = XWINDOW (echo_area_window);
6639 int resized_p;
6d004fea
GM
6640 Lisp_Object resize_exactly;
6641
6642 if (minibuf_level == 0)
6643 resize_exactly = Qt;
6644 else
6645 resize_exactly = Qnil;
2311178e 6646
23a96c77 6647 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 6648 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
6649 if (resized_p)
6650 {
6651 ++windows_or_buffers_changed;
6652 ++update_mode_lines;
6653 redisplay_internal (0);
6654 }
6655 }
6656}
6657
6658
23a96c77 6659/* Callback function for with_echo_area_buffer, when used from
308a74d8 6660 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
6661 resize, EXACTLY non-nil means resize the mini-window exactly to the
6662 size of the text displayed. A3 and A4 are not used. Value is what
6663 resize_mini_window returns. */
23a96c77
GM
6664
6665static int
6d004fea 6666resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 6667 EMACS_INT a1;
6d004fea 6668 Lisp_Object exactly;
23dd2d97 6669 EMACS_INT a3, a4;
23a96c77 6670{
6d004fea 6671 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
6672}
6673
6674
92a90e89
GM
6675/* Resize mini-window W to fit the size of its contents. EXACT:P
6676 means size the window exactly to the size needed. Otherwise, it's
6677 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 6678 the window height has been changed. */
c6e89d6c 6679
9472f927 6680int
92a90e89 6681resize_mini_window (w, exact_p)
c6e89d6c 6682 struct window *w;
92a90e89 6683 int exact_p;
c6e89d6c
GM
6684{
6685 struct frame *f = XFRAME (w->frame);
6686 int window_height_changed_p = 0;
6687
6688 xassert (MINI_WINDOW_P (w));
97cafc0f 6689
2913a9c0
GM
6690 /* Don't resize windows while redisplaying a window; it would
6691 confuse redisplay functions when the size of the window they are
6692 displaying changes from under them. Such a resizing can happen,
6693 for instance, when which-func prints a long message while
6694 we are running fontification-functions. We're running these
6695 functions with safe_call which binds inhibit-redisplay to t. */
6696 if (!NILP (Vinhibit_redisplay))
64c5be50 6697 return 0;
2311178e 6698
97cafc0f 6699 /* Nil means don't try to resize. */
6422c1d7 6700 if (NILP (Vresize_mini_windows)
00f6d59e 6701 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6702 return 0;
2311178e 6703
c6e89d6c
GM
6704 if (!FRAME_MINIBUF_ONLY_P (f))
6705 {
6706 struct it it;
dd2eb166
GM
6707 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6708 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6709 int height, max_height;
6710 int unit = CANON_Y_UNIT (f);
6711 struct text_pos start;
1bfdbe43
GM
6712 struct buffer *old_current_buffer = NULL;
6713
6714 if (current_buffer != XBUFFER (w->buffer))
6715 {
6716 old_current_buffer = current_buffer;
6717 set_buffer_internal (XBUFFER (w->buffer));
6718 }
9142dd5b 6719
c6e89d6c 6720 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6721
dd2eb166
GM
6722 /* Compute the max. number of lines specified by the user. */
6723 if (FLOATP (Vmax_mini_window_height))
18f9986a 6724 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
dd2eb166
GM
6725 else if (INTEGERP (Vmax_mini_window_height))
6726 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6727 else
6728 max_height = total_height / 4;
2311178e 6729
4b41cebb 6730 /* Correct that max. height if it's bogus. */
dd2eb166
GM
6731 max_height = max (1, max_height);
6732 max_height = min (total_height, max_height);
2311178e 6733
dd2eb166 6734 /* Find out the height of the text in the window. */
ad4f174e
GM
6735 if (it.truncate_lines_p)
6736 height = 1;
55b064bd 6737 else
ad4f174e
GM
6738 {
6739 last_height = 0;
6740 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6741 if (it.max_ascent == 0 && it.max_descent == 0)
6742 height = it.current_y + last_height;
6743 else
6744 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6745 height -= it.extra_line_spacing;
ad4f174e
GM
6746 height = (height + unit - 1) / unit;
6747 }
2311178e 6748
dd2eb166
GM
6749 /* Compute a suitable window start. */
6750 if (height > max_height)
6751 {
6752 height = max_height;
6753 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6754 move_it_vertically_backward (&it, (height - 1) * unit);
6755 start = it.current.pos;
6756 }
6757 else
6758 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6759 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6760
6422c1d7 6761 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6762 {
6422c1d7
GM
6763 /* Let it grow only, until we display an empty message, in which
6764 case the window shrinks again. */
6765 if (height > XFASTINT (w->height))
6766 {
6767 int old_height = XFASTINT (w->height);
6768 freeze_window_starts (f, 1);
6769 grow_mini_window (w, height - XFASTINT (w->height));
6770 window_height_changed_p = XFASTINT (w->height) != old_height;
6771 }
6772 else if (height < XFASTINT (w->height)
6773 && (exact_p || BEGV == ZV))
6774 {
6775 int old_height = XFASTINT (w->height);
6776 freeze_window_starts (f, 0);
6777 shrink_mini_window (w);
6778 window_height_changed_p = XFASTINT (w->height) != old_height;
6779 }
da448723 6780 }
2311178e 6781 else
da448723 6782 {
6422c1d7
GM
6783 /* Always resize to exact size needed. */
6784 if (height > XFASTINT (w->height))
6785 {
6786 int old_height = XFASTINT (w->height);
6787 freeze_window_starts (f, 1);
6788 grow_mini_window (w, height - XFASTINT (w->height));
6789 window_height_changed_p = XFASTINT (w->height) != old_height;
6790 }
6791 else if (height < XFASTINT (w->height))
6792 {
6793 int old_height = XFASTINT (w->height);
6794 freeze_window_starts (f, 0);
6795 shrink_mini_window (w);
6796
6797 if (height)
6798 {
6799 freeze_window_starts (f, 1);
6800 grow_mini_window (w, height - XFASTINT (w->height));
6801 }
2311178e 6802
6422c1d7
GM
6803 window_height_changed_p = XFASTINT (w->height) != old_height;
6804 }
9142dd5b 6805 }
1bfdbe43
GM
6806
6807 if (old_current_buffer)
6808 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6809 }
6810
6811 return window_height_changed_p;
6812}
6813
6814
6815/* Value is the current message, a string, or nil if there is no
6816 current message. */
6817
6818Lisp_Object
6819current_message ()
6820{
6821 Lisp_Object msg;
6822
6823 if (NILP (echo_area_buffer[0]))
6824 msg = Qnil;
6825 else
6826 {
23a96c77 6827 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6828 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6829 if (NILP (msg))
6830 echo_area_buffer[0] = Qnil;
6831 }
2311178e 6832
c6e89d6c
GM
6833 return msg;
6834}
6835
6836
6837static int
23a96c77 6838current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6839 EMACS_INT a1;
6840 Lisp_Object a2;
6841 EMACS_INT a3, a4;
c6e89d6c 6842{
23a96c77 6843 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 6844
c6e89d6c
GM
6845 if (Z > BEG)
6846 *msg = make_buffer_string (BEG, Z, 1);
6847 else
6848 *msg = Qnil;
6849 return 0;
6850}
6851
6852
6853/* Push the current message on Vmessage_stack for later restauration
6854 by restore_message. Value is non-zero if the current message isn't
6855 empty. This is a relatively infrequent operation, so it's not
6856 worth optimizing. */
6857
6858int
6859push_message ()
6860{
6861 Lisp_Object msg;
6862 msg = current_message ();
6863 Vmessage_stack = Fcons (msg, Vmessage_stack);
6864 return STRINGP (msg);
6865}
6866
6867
6868/* Restore message display from the top of Vmessage_stack. */
6869
6870void
6871restore_message ()
6872{
6873 Lisp_Object msg;
2311178e 6874
c6e89d6c
GM
6875 xassert (CONSP (Vmessage_stack));
6876 msg = XCAR (Vmessage_stack);
6877 if (STRINGP (msg))
d5db4077 6878 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
6879 else
6880 message3_nolog (msg, 0, 0);
6881}
6882
6883
37d66095
RS
6884/* Handler for record_unwind_protect calling pop_message. */
6885
6886Lisp_Object
6887pop_message_unwind (dummy)
6888 Lisp_Object dummy;
6889{
6890 pop_message ();
6891 return Qnil;
6892}
6893
c6e89d6c
GM
6894/* Pop the top-most entry off Vmessage_stack. */
6895
6896void
6897pop_message ()
6898{
6899 xassert (CONSP (Vmessage_stack));
6900 Vmessage_stack = XCDR (Vmessage_stack);
6901}
6902
6903
6904/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6905 exits. If the stack is not empty, we have a missing pop_message
6906 somewhere. */
6907
6908void
6909check_message_stack ()
6910{
6911 if (!NILP (Vmessage_stack))
6912 abort ();
6913}
6914
6915
6916/* Truncate to NCHARS what will be displayed in the echo area the next
6917 time we display it---but don't redisplay it now. */
6918
6919void
6920truncate_echo_area (nchars)
6921 int nchars;
6922{
6923 if (nchars == 0)
6924 echo_area_buffer[0] = Qnil;
6925 /* A null message buffer means that the frame hasn't really been
6926 initialized yet. Error messages get reported properly by
6927 cmd_error, so this must be just an informative message; toss it. */
6928 else if (!noninteractive
6929 && INTERACTIVE
c6e89d6c 6930 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6931 {
6932 struct frame *sf = SELECTED_FRAME ();
6933 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6934 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6935 }
c6e89d6c
GM
6936}
6937
6938
6939/* Helper function for truncate_echo_area. Truncate the current
6940 message to at most NCHARS characters. */
6941
6942static int
23a96c77 6943truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6944 EMACS_INT nchars;
6945 Lisp_Object a2;
6946 EMACS_INT a3, a4;
c6e89d6c
GM
6947{
6948 if (BEG + nchars < Z)
6949 del_range (BEG + nchars, Z);
6950 if (Z == BEG)
6951 echo_area_buffer[0] = Qnil;
6952 return 0;
6953}
6954
6955
6956/* Set the current message to a substring of S or STRING.
6957
6958 If STRING is a Lisp string, set the message to the first NBYTES
6959 bytes from STRING. NBYTES zero means use the whole string. If
6960 STRING is multibyte, the message will be displayed multibyte.
6961
6962 If S is not null, set the message to the first LEN bytes of S. LEN
6963 zero means use the whole string. MULTIBYTE_P non-zero means S is
6964 multibyte. Display the message multibyte in that case. */
6965
6966void
6967set_message (s, string, nbytes, multibyte_p)
50f80c2f 6968 const char *s;
c6e89d6c 6969 Lisp_Object string;
e3383b6f 6970 int nbytes, multibyte_p;
c6e89d6c
GM
6971{
6972 message_enable_multibyte
6973 = ((s && multibyte_p)
6974 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 6975
23a96c77
GM
6976 with_echo_area_buffer (0, -1, set_message_1,
6977 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6978 message_buf_print = 0;
21fdfb65 6979 help_echo_showing_p = 0;
c6e89d6c
GM
6980}
6981
6982
6983/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6984 as there, with A1 corresponding to S and A2 corresponding to STRING
6985 This function is called with the echo area buffer being
c6e89d6c
GM
6986 current. */
6987
6988static int
23a96c77 6989set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6990 EMACS_INT a1;
6991 Lisp_Object a2;
6992 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6993{
50f80c2f 6994 const char *s = (const char *) a1;
23dd2d97 6995 Lisp_Object string = a2;
2311178e 6996
c6e89d6c 6997 xassert (BEG == Z);
2311178e 6998
c6e89d6c
GM
6999 /* Change multibyteness of the echo buffer appropriately. */
7000 if (message_enable_multibyte
7001 != !NILP (current_buffer->enable_multibyte_characters))
7002 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7003
ad4f174e 7004 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 7005
c6e89d6c
GM
7006 /* Insert new message at BEG. */
7007 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7008
7009 if (STRINGP (string))
7010 {
7011 int nchars;
2311178e 7012
c6e89d6c 7013 if (nbytes == 0)
2051c264 7014 nbytes = SBYTES (string);
c6e89d6c 7015 nchars = string_byte_to_char (string, nbytes);
2311178e 7016
c6e89d6c
GM
7017 /* This function takes care of single/multibyte conversion. We
7018 just have to ensure that the echo area buffer has the right
7019 setting of enable_multibyte_characters. */
7020 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7021 }
7022 else if (s)
7023 {
7024 if (nbytes == 0)
7025 nbytes = strlen (s);
2311178e 7026
c6e89d6c
GM
7027 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7028 {
7029 /* Convert from multi-byte to single-byte. */
7030 int i, c, n;
7031 unsigned char work[1];
2311178e 7032
c6e89d6c
GM
7033 /* Convert a multibyte string to single-byte. */
7034 for (i = 0; i < nbytes; i += n)
7035 {
7036 c = string_char_and_length (s + i, nbytes - i, &n);
7037 work[0] = (SINGLE_BYTE_CHAR_P (c)
7038 ? c
7039 : multibyte_char_to_unibyte (c, Qnil));
7040 insert_1_both (work, 1, 1, 1, 0, 0);
7041 }
7042 }
7043 else if (!multibyte_p
7044 && !NILP (current_buffer->enable_multibyte_characters))
7045 {
7046 /* Convert from single-byte to multi-byte. */
7047 int i, c, n;
50f80c2f 7048 const unsigned char *msg = (const unsigned char *) s;
260a86a0 7049 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 7050
c6e89d6c
GM
7051 /* Convert a single-byte string to multibyte. */
7052 for (i = 0; i < nbytes; i++)
7053 {
7054 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
7055 n = CHAR_STRING (c, str);
7056 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
7057 }
7058 }
7059 else
7060 insert_1 (s, nbytes, 1, 0, 0);
7061 }
7062
7063 return 0;
7064}
7065
7066
7067/* Clear messages. CURRENT_P non-zero means clear the current
7068 message. LAST_DISPLAYED_P non-zero means clear the message
7069 last displayed. */
7070
7071void
7072clear_message (current_p, last_displayed_p)
7073 int current_p, last_displayed_p;
7074{
7075 if (current_p)
6e019995
GM
7076 {
7077 echo_area_buffer[0] = Qnil;
7078 message_cleared_p = 1;
7079 }
2311178e 7080
c6e89d6c
GM
7081 if (last_displayed_p)
7082 echo_area_buffer[1] = Qnil;
2311178e 7083
c6e89d6c
GM
7084 message_buf_print = 0;
7085}
7086
7087/* Clear garbaged frames.
7088
7089 This function is used where the old redisplay called
7090 redraw_garbaged_frames which in turn called redraw_frame which in
7091 turn called clear_frame. The call to clear_frame was a source of
7092 flickering. I believe a clear_frame is not necessary. It should
7093 suffice in the new redisplay to invalidate all current matrices,
7094 and ensure a complete redisplay of all windows. */
7095
7096static void
7097clear_garbaged_frames ()
7098{
5f5c8ee5
GM
7099 if (frame_garbaged)
7100 {
5f5c8ee5 7101 Lisp_Object tail, frame;
5fb96e96 7102 int changed_count = 0;
2311178e 7103
5f5c8ee5
GM
7104 FOR_EACH_FRAME (tail, frame)
7105 {
7106 struct frame *f = XFRAME (frame);
2311178e 7107
5f5c8ee5
GM
7108 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7109 {
6bb95882 7110 if (f->resized_p)
573e57f1 7111 Fredraw_frame (frame);
5f5c8ee5 7112 clear_current_matrices (f);
5fb96e96 7113 changed_count++;
5f5c8ee5 7114 f->garbaged = 0;
6bb95882 7115 f->resized_p = 0;
5f5c8ee5
GM
7116 }
7117 }
7118
7119 frame_garbaged = 0;
5fb96e96
RS
7120 if (changed_count)
7121 ++windows_or_buffers_changed;
5f5c8ee5 7122 }
c6e89d6c 7123}
5f5c8ee5 7124
5f5c8ee5 7125
886bd6f2
GM
7126/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7127 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7128 mini-windows height has been changed. */
5f5c8ee5 7129
c6e89d6c
GM
7130static int
7131echo_area_display (update_frame_p)
7132 int update_frame_p;
7133{
7134 Lisp_Object mini_window;
7135 struct window *w;
7136 struct frame *f;
7137 int window_height_changed_p = 0;
886bd6f2 7138 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7139
886bd6f2 7140 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7141 w = XWINDOW (mini_window);
7142 f = XFRAME (WINDOW_FRAME (w));
7143
7144 /* Don't display if frame is invisible or not yet initialized. */
7145 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7146 return 0;
5f5c8ee5 7147
1a578e9b 7148/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7149#ifndef MAC_OS8
1ab3e082 7150#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7151 /* When Emacs starts, selected_frame may be a visible terminal
7152 frame, even if we run under a window system. If we let this
7153 through, a message would be displayed on the terminal. */
2311178e 7154 if (EQ (selected_frame, Vterminal_frame)
622e3754 7155 && !NILP (Vwindow_system))
c6e89d6c 7156 return 0;
1ab3e082 7157#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7158#endif
c6e89d6c
GM
7159
7160 /* Redraw garbaged frames. */
7161 if (frame_garbaged)
7162 clear_garbaged_frames ();
7163
7164 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7165 {
7166 echo_area_window = mini_window;
7167 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7168 w->must_be_updated_p = 1;
c59c668a 7169
d4358b37
GM
7170 /* Update the display, unless called from redisplay_internal.
7171 Also don't update the screen during redisplay itself. The
7172 update will happen at the end of redisplay, and an update
7173 here could cause confusion. */
7174 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7175 {
715e84c9 7176 int n = 0;
edc68111 7177
715e84c9
GM
7178 /* If the display update has been interrupted by pending
7179 input, update mode lines in the frame. Due to the
7180 pending input, it might have been that redisplay hasn't
7181 been called, so that mode lines above the echo area are
7182 garbaged. This looks odd, so we prevent it here. */
7183 if (!display_completed)
7184 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 7185
8af1308e
GM
7186 if (window_height_changed_p
7187 /* Don't do this if Emacs is shutting down. Redisplay
7188 needs to run hooks. */
7189 && !NILP (Vrun_hooks))
c59c668a 7190 {
c06017fb
GM
7191 /* Must update other windows. Likewise as in other
7192 cases, don't let this update be interrupted by
7193 pending input. */
331379bf 7194 int count = SPECPDL_INDEX ();
c06017fb 7195 specbind (Qredisplay_dont_pause, Qt);
edc68111 7196 windows_or_buffers_changed = 1;
c59c668a 7197 redisplay_internal (0);
c06017fb 7198 unbind_to (count, Qnil);
c59c668a 7199 }
715e84c9 7200 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7201 {
edc68111 7202 /* Window configuration is the same as before.
715e84c9
GM
7203 Can do with a display update of the echo area,
7204 unless we displayed some mode lines. */
5f5c8ee5
GM
7205 update_single_window (w, 1);
7206 rif->flush_display (f);
7207 }
7208 else
7209 update_frame (f, 1, 1);
31b6671b
GM
7210
7211 /* If cursor is in the echo area, make sure that the next
7212 redisplay displays the minibuffer, so that the cursor will
7213 be replaced with what the minibuffer wants. */
7214 if (cursor_in_echo_area)
7215 ++windows_or_buffers_changed;
5f5c8ee5
GM
7216 }
7217 }
7218 else if (!EQ (mini_window, selected_window))
7219 windows_or_buffers_changed++;
c59c668a
GM
7220
7221 /* Last displayed message is now the current message. */
dd2eb166 7222 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 7223
5f5c8ee5
GM
7224 /* Prevent redisplay optimization in redisplay_internal by resetting
7225 this_line_start_pos. This is done because the mini-buffer now
7226 displays the message instead of its buffer text. */
7227 if (EQ (mini_window, selected_window))
7228 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7229
7230 return window_height_changed_p;
5f5c8ee5
GM
7231}
7232
7233
7234\f
7235/***********************************************************************
7236 Frame Titles
7237 ***********************************************************************/
7238
7239
8143e6ab
KS
7240/* The frame title buffering code is also used by Fformat_mode_line.
7241 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
5f5c8ee5
GM
7242
7243/* A buffer for constructing frame titles in it; allocated from the
7244 heap in init_xdisp and resized as needed in store_frame_title_char. */
7245
7246static char *frame_title_buf;
7247
7248/* The buffer's end, and a current output position in it. */
7249
7250static char *frame_title_buf_end;
7251static char *frame_title_ptr;
7252
7253
7254/* Store a single character C for the frame title in frame_title_buf.
7255 Re-allocate frame_title_buf if necessary. */
7256
7257static void
5d15cc8f
DL
7258#ifdef PROTOTYPES
7259store_frame_title_char (char c)
7260#else
5f5c8ee5
GM
7261store_frame_title_char (c)
7262 char c;
5d15cc8f 7263#endif
5f5c8ee5
GM
7264{
7265 /* If output position has reached the end of the allocated buffer,
7266 double the buffer's size. */
7267 if (frame_title_ptr == frame_title_buf_end)
7268 {
7269 int len = frame_title_ptr - frame_title_buf;
7270 int new_size = 2 * len * sizeof *frame_title_buf;
7271 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7272 frame_title_buf_end = frame_title_buf + new_size;
7273 frame_title_ptr = frame_title_buf + len;
7274 }
7275
7276 *frame_title_ptr++ = c;
7277}
7278
7279
7280/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7281 frame_title_ptr. STR is the string to store. Do not copy
7282 characters that yield more columns than PRECISION; PRECISION <= 0
7283 means copy the whole string. Pad with spaces until FIELD_WIDTH
7284 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7285 pad. Called from display_mode_element when it is used to build a
7286 frame title. */
5f5c8ee5
GM
7287
7288static int
7289store_frame_title (str, field_width, precision)
50f80c2f 7290 const unsigned char *str;
5f5c8ee5
GM
7291 int field_width, precision;
7292{
7293 int n = 0;
3b552d56 7294 int dummy, nbytes;
5f5c8ee5
GM
7295
7296 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7297 nbytes = strlen (str);
7298 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7299 while (nbytes--)
7300 store_frame_title_char (*str++);
5f5c8ee5
GM
7301
7302 /* Fill up with spaces until FIELD_WIDTH reached. */
7303 while (field_width > 0
7304 && n < field_width)
7305 {
7306 store_frame_title_char (' ');
7307 ++n;
7308 }
7309
7310 return n;
7311}
7312
8143e6ab 7313#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7314
7315/* Set the title of FRAME, if it has changed. The title format is
7316 Vicon_title_format if FRAME is iconified, otherwise it is
7317 frame_title_format. */
7318
7319static void
7320x_consider_frame_title (frame)
7321 Lisp_Object frame;
7322{
7323 struct frame *f = XFRAME (frame);
7324
7325 if (FRAME_WINDOW_P (f)
7326 || FRAME_MINIBUF_ONLY_P (f)
7327 || f->explicit_name)
7328 {
7329 /* Do we have more than one visible frame on this X display? */
7330 Lisp_Object tail;
7331 Lisp_Object fmt;
7332 struct buffer *obuf;
7333 int len;
7334 struct it it;
7335
9472f927 7336 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7337 {
74779f52
JR
7338 Lisp_Object other_frame = XCAR (tail);
7339 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 7340
2311178e 7341 if (tf != f
5f5c8ee5
GM
7342 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7343 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7344 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7345 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7346 break;
7347 }
7348
7349 /* Set global variable indicating that multiple frames exist. */
7350 multiple_frames = CONSP (tail);
7351
7352 /* Switch to the buffer of selected window of the frame. Set up
7353 frame_title_ptr so that display_mode_element will output into it;
7354 then display the title. */
7355 obuf = current_buffer;
bb336f8d 7356 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7357 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7358 frame_title_ptr = frame_title_buf;
7359 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7360 NULL, DEFAULT_FACE_ID);
c53a1624 7361 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 7362 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7363 frame_title_ptr = NULL;
bb336f8d 7364 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7365
7366 /* Set the title only if it's changed. This avoids consing in
7367 the common case where it hasn't. (If it turns out that we've
7368 already wasted too much time by walking through the list with
7369 display_mode_element, then we might need to optimize at a
7370 higher level than this.) */
2311178e 7371 if (! STRINGP (f->name)
2051c264
GM
7372 || SBYTES (f->name) != len
7373 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
7374 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7375 }
7376}
7377
5f5c8ee5
GM
7378#endif /* not HAVE_WINDOW_SYSTEM */
7379
7380
7381
7382\f
7383/***********************************************************************
7384 Menu Bars
7385 ***********************************************************************/
7386
7387
7388/* Prepare for redisplay by updating menu-bar item lists when
7389 appropriate. This can call eval. */
7390
7391void
7392prepare_menu_bars ()
7393{
7394 int all_windows;
7395 struct gcpro gcpro1, gcpro2;
7396 struct frame *f;
6b5c4794 7397 Lisp_Object tooltip_frame;
5f5c8ee5 7398
86ffe5cd 7399#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7400 tooltip_frame = tip_frame;
7401#else
6b5c4794 7402 tooltip_frame = Qnil;
5f5c8ee5
GM
7403#endif
7404
7405 /* Update all frame titles based on their buffer names, etc. We do
7406 this before the menu bars so that the buffer-menu will show the
7407 up-to-date frame titles. */
7408#ifdef HAVE_WINDOW_SYSTEM
7409 if (windows_or_buffers_changed || update_mode_lines)
7410 {
7411 Lisp_Object tail, frame;
7412
7413 FOR_EACH_FRAME (tail, frame)
7414 {
7415 f = XFRAME (frame);
6b5c4794 7416 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7417 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7418 x_consider_frame_title (frame);
7419 }
7420 }
7421#endif /* HAVE_WINDOW_SYSTEM */
7422
7423 /* Update the menu bar item lists, if appropriate. This has to be
7424 done before any actual redisplay or generation of display lines. */
2311178e 7425 all_windows = (update_mode_lines
5f5c8ee5
GM
7426 || buffer_shared > 1
7427 || windows_or_buffers_changed);
7428 if (all_windows)
7429 {
7430 Lisp_Object tail, frame;
331379bf 7431 int count = SPECPDL_INDEX ();
5f5c8ee5
GM
7432
7433 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7434
7435 FOR_EACH_FRAME (tail, frame)
7436 {
7437 f = XFRAME (frame);
7438
7439 /* Ignore tooltip frame. */
6b5c4794 7440 if (EQ (frame, tooltip_frame))
5f5c8ee5 7441 continue;
2311178e 7442
5f5c8ee5
GM
7443 /* If a window on this frame changed size, report that to
7444 the user and clear the size-change flag. */
7445 if (FRAME_WINDOW_SIZES_CHANGED (f))
7446 {
7447 Lisp_Object functions;
2311178e 7448
5f5c8ee5
GM
7449 /* Clear flag first in case we get an error below. */
7450 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7451 functions = Vwindow_size_change_functions;
7452 GCPRO2 (tail, functions);
2311178e 7453
5f5c8ee5
GM
7454 while (CONSP (functions))
7455 {
7456 call1 (XCAR (functions), frame);
7457 functions = XCDR (functions);
7458 }
7459 UNGCPRO;
7460 }
2311178e 7461
5f5c8ee5
GM
7462 GCPRO1 (tail);
7463 update_menu_bar (f, 0);
7464#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7465 update_tool_bar (f, 0);
5f5c8ee5
GM
7466#endif
7467 UNGCPRO;
7468 }
7469
7470 unbind_to (count, Qnil);
7471 }
7472 else
7473 {
886bd6f2
GM
7474 struct frame *sf = SELECTED_FRAME ();
7475 update_menu_bar (sf, 1);
5f5c8ee5 7476#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7477 update_tool_bar (sf, 1);
5f5c8ee5
GM
7478#endif
7479 }
7480
7481 /* Motif needs this. See comment in xmenu.c. Turn it off when
7482 pending_menu_activation is not defined. */
7483#ifdef USE_X_TOOLKIT
7484 pending_menu_activation = 0;
7485#endif
7486}
7487
7488
7489/* Update the menu bar item list for frame F. This has to be done
7490 before we start to fill in any display lines, because it can call
7491 eval.
7492
7493 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7494
7495static void
7496update_menu_bar (f, save_match_data)
7497 struct frame *f;
7498 int save_match_data;
7499{
7500 Lisp_Object window;
7501 register struct window *w;
7502
e1477f43
GM
7503 /* If called recursively during a menu update, do nothing. This can
7504 happen when, for instance, an activate-menubar-hook causes a
7505 redisplay. */
7506 if (inhibit_menubar_update)
7507 return;
7508
5f5c8ee5
GM
7509 window = FRAME_SELECTED_WINDOW (f);
7510 w = XWINDOW (window);
2311178e 7511
40ae145b 7512#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
7513 condition !NILP (w->update_mode_line), rather than using
7514 update_mode_lines directly, and this if statement may have
7515 been added to make that condition work. Now the if
2311178e 7516 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
7517 if (update_mode_lines)
7518 w->update_mode_line = Qt;
a5f08374 7519#endif
5f5c8ee5
GM
7520
7521 if (FRAME_WINDOW_P (f)
7522 ?
488dd4c4
JD
7523#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7524 || defined (USE_GTK)
2311178e 7525 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
7526#else
7527 FRAME_MENU_BAR_LINES (f) > 0
7528#endif
7529 : FRAME_MENU_BAR_LINES (f) > 0)
7530 {
7531 /* If the user has switched buffers or windows, we need to
7532 recompute to reflect the new bindings. But we'll
7533 recompute when update_mode_lines is set too; that means
7534 that people can use force-mode-line-update to request
7535 that the menu bar be recomputed. The adverse effect on
7536 the rest of the redisplay algorithm is about the same as
7537 windows_or_buffers_changed anyway. */
7538 if (windows_or_buffers_changed
a5f08374
RS
7539 /* This used to test w->update_mode_line, but we believe
7540 there is no need to recompute the menu in that case. */
7541 || update_mode_lines
5f5c8ee5
GM
7542 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7543 < BUF_MODIFF (XBUFFER (w->buffer)))
7544 != !NILP (w->last_had_star))
7545 || ((!NILP (Vtransient_mark_mode)
7546 && !NILP (XBUFFER (w->buffer)->mark_active))
7547 != !NILP (w->region_showing)))
7548 {
7549 struct buffer *prev = current_buffer;
331379bf 7550 int count = SPECPDL_INDEX ();
5f5c8ee5 7551
e1477f43
GM
7552 specbind (Qinhibit_menubar_update, Qt);
7553
5f5c8ee5
GM
7554 set_buffer_internal_1 (XBUFFER (w->buffer));
7555 if (save_match_data)
7556 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7557 if (NILP (Voverriding_local_map_menu_flag))
7558 {
7559 specbind (Qoverriding_terminal_local_map, Qnil);
7560 specbind (Qoverriding_local_map, Qnil);
7561 }
7562
7563 /* Run the Lucid hook. */
65048e97 7564 safe_run_hooks (Qactivate_menubar_hook);
2311178e 7565
5f5c8ee5
GM
7566 /* If it has changed current-menubar from previous value,
7567 really recompute the menu-bar from the value. */
7568 if (! NILP (Vlucid_menu_bar_dirty_flag))
7569 call0 (Qrecompute_lucid_menubar);
2311178e 7570
5f5c8ee5
GM
7571 safe_run_hooks (Qmenu_bar_update_hook);
7572 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 7573
5f5c8ee5 7574 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
7575#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7576 || defined (USE_GTK)
1a578e9b 7577 if (FRAME_WINDOW_P (f)
e0f712ba 7578#if defined (MAC_OS)
1a578e9b
AC
7579 /* All frames on Mac OS share the same menubar. So only the
7580 selected frame should be allowed to set it. */
7581 && f == SELECTED_FRAME ()
7582#endif
7583 )
5f5c8ee5
GM
7584 set_frame_menubar (f, 0, 0);
7585 else
7586 /* On a terminal screen, the menu bar is an ordinary screen
7587 line, and this makes it get updated. */
7588 w->update_mode_line = Qt;
488dd4c4 7589#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
7590 /* In the non-toolkit version, the menu bar is an ordinary screen
7591 line, and this makes it get updated. */
7592 w->update_mode_line = Qt;
488dd4c4 7593#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
7594
7595 unbind_to (count, Qnil);
7596 set_buffer_internal_1 (prev);
7597 }
7598 }
7599}
7600
7601
7602\f
7603/***********************************************************************
e037b9ec 7604 Tool-bars
5f5c8ee5
GM
7605 ***********************************************************************/
7606
7607#ifdef HAVE_WINDOW_SYSTEM
7608
e037b9ec 7609/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7610 before we start to fill in any display lines. Called from
7611 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7612 and restore it here. */
7613
7614static void
e037b9ec 7615update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7616 struct frame *f;
7617 int save_match_data;
7618{
488dd4c4
JD
7619#ifdef USE_GTK
7620 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
7621#else
7622 int do_update = WINDOWP (f->tool_bar_window)
7623 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
7624#endif
7625
7626 if (do_update)
5f5c8ee5
GM
7627 {
7628 Lisp_Object window;
7629 struct window *w;
7630
7631 window = FRAME_SELECTED_WINDOW (f);
7632 w = XWINDOW (window);
2311178e 7633
5f5c8ee5
GM
7634 /* If the user has switched buffers or windows, we need to
7635 recompute to reflect the new bindings. But we'll
7636 recompute when update_mode_lines is set too; that means
7637 that people can use force-mode-line-update to request
7638 that the menu bar be recomputed. The adverse effect on
7639 the rest of the redisplay algorithm is about the same as
7640 windows_or_buffers_changed anyway. */
7641 if (windows_or_buffers_changed
7642 || !NILP (w->update_mode_line)
7643 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7644 < BUF_MODIFF (XBUFFER (w->buffer)))
7645 != !NILP (w->last_had_star))
7646 || ((!NILP (Vtransient_mark_mode)
7647 && !NILP (XBUFFER (w->buffer)->mark_active))
7648 != !NILP (w->region_showing)))
7649 {
7650 struct buffer *prev = current_buffer;
331379bf 7651 int count = SPECPDL_INDEX ();
a2889657 7652
5f5c8ee5
GM
7653 /* Set current_buffer to the buffer of the selected
7654 window of the frame, so that we get the right local
7655 keymaps. */
7656 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7657
5f5c8ee5
GM
7658 /* Save match data, if we must. */
7659 if (save_match_data)
7660 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7661
7662 /* Make sure that we don't accidentally use bogus keymaps. */
7663 if (NILP (Voverriding_local_map_menu_flag))
7664 {
7665 specbind (Qoverriding_terminal_local_map, Qnil);
7666 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7667 }
1f40cad2 7668
e037b9ec 7669 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7670 f->tool_bar_items
7671 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
2311178e 7672
e037b9ec 7673 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7674 w->update_mode_line = Qt;
7675
7676 unbind_to (count, Qnil);
7677 set_buffer_internal_1 (prev);
81d478f3 7678 }
a2889657
JB
7679 }
7680}
7681
6c4429a5 7682
e037b9ec 7683/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7684 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7685 been set up previously by calling prepare_menu_bars. */
7686
a2889657 7687static void
e037b9ec 7688build_desired_tool_bar_string (f)
5f5c8ee5 7689 struct frame *f;
a2889657 7690{
a23887b9 7691 int i, size, size_needed;
5f5c8ee5
GM
7692 struct gcpro gcpro1, gcpro2, gcpro3;
7693 Lisp_Object image, plist, props;
a2889657 7694
5f5c8ee5
GM
7695 image = plist = props = Qnil;
7696 GCPRO3 (image, plist, props);
a2889657 7697
e037b9ec 7698 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 7699 Otherwise, make a new string. */
2311178e 7700
5f5c8ee5 7701 /* The size of the string we might be able to reuse. */
e037b9ec 7702 size = (STRINGP (f->desired_tool_bar_string)
2051c264 7703 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
7704 : 0);
7705
b94c0d9c 7706 /* We need one space in the string for each image. */
a23887b9 7707 size_needed = f->n_tool_bar_items;
2311178e 7708
b94c0d9c 7709 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7710 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7711 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7712 make_number (' '));
5f5c8ee5
GM
7713 else
7714 {
7715 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7716 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7717 props, f->desired_tool_bar_string);
5f5c8ee5 7718 }
a2889657 7719
5f5c8ee5 7720 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7721 put a `menu_item' property on tool-bar items with a value that
7722 is the index of the item in F's tool-bar item vector. */
a23887b9 7723 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7724 {
7464726e 7725#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7726
e037b9ec
GM
7727 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7728 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7729 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7730 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7731
7732 /* If image is a vector, choose the image according to the
7733 button state. */
e037b9ec 7734 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7735 if (VECTORP (image))
7736 {
5f5c8ee5
GM
7737 if (enabled_p)
7738 idx = (selected_p
e037b9ec
GM
7739 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7740 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7741 else
7742 idx = (selected_p
e037b9ec
GM
7743 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7744 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 7745
37e4e482
GM
7746 xassert (ASIZE (image) >= idx);
7747 image = AREF (image, idx);
5f5c8ee5 7748 }
37e4e482
GM
7749 else
7750 idx = -1;
5f5c8ee5
GM
7751
7752 /* Ignore invalid image specifications. */
7753 if (!valid_image_p (image))
7754 continue;
7755
e037b9ec 7756 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7757 plist = Fcopy_sequence (XCDR (image));
7758
7759 /* Compute margin and relief to draw. */
9f864bf9 7760 relief = (tool_bar_button_relief >= 0
c3d76173
GM
7761 ? tool_bar_button_relief
7762 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7763 hmargin = vmargin = relief;
7764
7765 if (INTEGERP (Vtool_bar_button_margin)
7766 && XINT (Vtool_bar_button_margin) > 0)
7767 {
7768 hmargin += XFASTINT (Vtool_bar_button_margin);
7769 vmargin += XFASTINT (Vtool_bar_button_margin);
7770 }
7771 else if (CONSP (Vtool_bar_button_margin))
7772 {
7773 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7774 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7775 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 7776
35a41507
GM
7777 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7778 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7779 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7780 }
2311178e 7781
e037b9ec 7782 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7783 {
7784 /* Add a `:relief' property to the image spec if the item is
7785 selected. */
7786 if (selected_p)
7787 {
7788 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7789 hmargin -= relief;
7790 vmargin -= relief;
5f5c8ee5
GM
7791 }
7792 }
7793 else
7794 {
7795 /* If image is selected, display it pressed, i.e. with a
7796 negative relief. If it's not selected, display it with a
7797 raised relief. */
7798 plist = Fplist_put (plist, QCrelief,
7799 (selected_p
7800 ? make_number (-relief)
7801 : make_number (relief)));
35a41507
GM
7802 hmargin -= relief;
7803 vmargin -= relief;
5f5c8ee5
GM
7804 }
7805
7806 /* Put a margin around the image. */
35a41507
GM
7807 if (hmargin || vmargin)
7808 {
7809 if (hmargin == vmargin)
7810 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7811 else
7812 plist = Fplist_put (plist, QCmargin,
7813 Fcons (make_number (hmargin),
7814 make_number (vmargin)));
7815 }
2311178e 7816
37e4e482
GM
7817 /* If button is not enabled, and we don't have special images
7818 for the disabled state, make the image appear disabled by
5f5c8ee5 7819 applying an appropriate algorithm to it. */
37e4e482 7820 if (!enabled_p && idx < 0)
ecd01a0e 7821 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 7822
5f5c8ee5
GM
7823 /* Put a `display' text property on the string for the image to
7824 display. Put a `menu-item' property on the string that gives
e037b9ec 7825 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7826 vector. */
7827 image = Fcons (Qimage, plist);
7828 props = list4 (Qdisplay, image,
a23887b9
GM
7829 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7830
7831 /* Let the last image hide all remaining spaces in the tool bar
7832 string. The string can be longer than needed when we reuse a
7833 previous string. */
7834 if (i + 1 == f->n_tool_bar_items)
2051c264 7835 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
7836 else
7837 end = i + 1;
7838 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7839 props, f->desired_tool_bar_string);
5f5c8ee5 7840#undef PROP
a2889657
JB
7841 }
7842
5f5c8ee5
GM
7843 UNGCPRO;
7844}
7845
7846
e037b9ec 7847/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7848
7849static void
e037b9ec 7850display_tool_bar_line (it)
5f5c8ee5
GM
7851 struct it *it;
7852{
7853 struct glyph_row *row = it->glyph_row;
7854 int max_x = it->last_visible_x;
7855 struct glyph *last;
2311178e 7856
5f5c8ee5
GM
7857 prepare_desired_row (row);
7858 row->y = it->current_y;
90aa2856
GM
7859
7860 /* Note that this isn't made use of if the face hasn't a box,
7861 so there's no need to check the face here. */
7862 it->start_of_box_run_p = 1;
2311178e 7863
5f5c8ee5 7864 while (it->current_x < max_x)
a2889657 7865 {
5f5c8ee5 7866 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7867
5f5c8ee5
GM
7868 /* Get the next display element. */
7869 if (!get_next_display_element (it))
7870 break;
73af359d 7871
5f5c8ee5
GM
7872 /* Produce glyphs. */
7873 x_before = it->current_x;
7874 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7875 PRODUCE_GLYPHS (it);
daa37602 7876
5f5c8ee5
GM
7877 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7878 i = 0;
7879 x = x_before;
7880 while (i < nglyphs)
7881 {
7882 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 7883
5f5c8ee5
GM
7884 if (x + glyph->pixel_width > max_x)
7885 {
7886 /* Glyph doesn't fit on line. */
7887 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7888 it->current_x = x;
7889 goto out;
7890 }
daa37602 7891
5f5c8ee5
GM
7892 ++it->hpos;
7893 x += glyph->pixel_width;
7894 ++i;
7895 }
7896
7897 /* Stop at line ends. */
7898 if (ITERATOR_AT_END_OF_LINE_P (it))
7899 break;
7900
cafafe0b 7901 set_iterator_to_next (it, 1);
a2889657 7902 }
a2889657 7903
5f5c8ee5 7904 out:;
a2889657 7905
5f5c8ee5
GM
7906 row->displays_text_p = row->used[TEXT_AREA] != 0;
7907 extend_face_to_end_of_line (it);
7908 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7909 last->right_box_line_p = 1;
90aa2856
GM
7910 if (last == row->glyphs[TEXT_AREA])
7911 last->left_box_line_p = 1;
5f5c8ee5 7912 compute_line_metrics (it);
2311178e 7913
e037b9ec 7914 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7915 if (!row->displays_text_p)
7916 {
312246d1
GM
7917 row->height = row->phys_height = it->last_visible_y - row->y;
7918 row->ascent = row->phys_ascent = 0;
5f5c8ee5 7919 }
2311178e 7920
5f5c8ee5
GM
7921 row->full_width_p = 1;
7922 row->continued_p = 0;
7923 row->truncated_on_left_p = 0;
7924 row->truncated_on_right_p = 0;
7925
7926 it->current_x = it->hpos = 0;
7927 it->current_y += row->height;
7928 ++it->vpos;
7929 ++it->glyph_row;
a2889657 7930}
96a410bc 7931
5f5c8ee5 7932
e037b9ec 7933/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7934 items of frame F visible. */
96a410bc 7935
d39b6696 7936static int
e037b9ec 7937tool_bar_lines_needed (f)
5f5c8ee5 7938 struct frame *f;
d39b6696 7939{
e037b9ec 7940 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 7941 struct it it;
2311178e 7942
e037b9ec
GM
7943 /* Initialize an iterator for iteration over
7944 F->desired_tool_bar_string in the tool-bar window of frame F. */
7945 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7946 it.first_visible_x = 0;
7947 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7948 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7949
7950 while (!ITERATOR_AT_END_P (&it))
7951 {
7952 it.glyph_row = w->desired_matrix->rows;
7953 clear_glyph_row (it.glyph_row);
e037b9ec 7954 display_tool_bar_line (&it);
5f5c8ee5
GM
7955 }
7956
7957 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7958}
96a410bc 7959
5f5c8ee5 7960
57c28064
GM
7961DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7962 0, 1, 0,
7ee72033
MB
7963 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7964 (frame)
57c28064
GM
7965 Lisp_Object frame;
7966{
7967 struct frame *f;
7968 struct window *w;
7969 int nlines = 0;
7970
7971 if (NILP (frame))
7972 frame = selected_frame;
7973 else
b7826503 7974 CHECK_FRAME (frame);
57c28064 7975 f = XFRAME (frame);
2311178e 7976
57c28064
GM
7977 if (WINDOWP (f->tool_bar_window)
7978 || (w = XWINDOW (f->tool_bar_window),
7979 XFASTINT (w->height) > 0))
7980 {
7981 update_tool_bar (f, 1);
7982 if (f->n_tool_bar_items)
7983 {
7984 build_desired_tool_bar_string (f);
7985 nlines = tool_bar_lines_needed (f);
7986 }
7987 }
7988
7989 return make_number (nlines);
7990}
7991
7992
e037b9ec 7993/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7994 height should be changed. */
7995
7996static int
e037b9ec 7997redisplay_tool_bar (f)
5f5c8ee5 7998 struct frame *f;
96a410bc 7999{
5f5c8ee5
GM
8000 struct window *w;
8001 struct it it;
8002 struct glyph_row *row;
8003 int change_height_p = 0;
177c0ea7 8004
488dd4c4
JD
8005#ifdef USE_GTK
8006 if (FRAME_EXTERNAL_TOOL_BAR(f))
8007 update_frame_tool_bar (f);
8008 return 0;
8009#endif
2311178e 8010
e037b9ec
GM
8011 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8012 do anything. This means you must start with tool-bar-lines
5f5c8ee5 8013 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
8014 can turn off tool-bars by specifying tool-bar-lines zero. */
8015 if (!WINDOWP (f->tool_bar_window)
8016 || (w = XWINDOW (f->tool_bar_window),
488dd4c4 8017 XFASTINT (w->height) == 0))
5f5c8ee5 8018 return 0;
96a410bc 8019
e037b9ec
GM
8020 /* Set up an iterator for the tool-bar window. */
8021 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
8022 it.first_visible_x = 0;
8023 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8024 row = it.glyph_row;
3450d04c 8025
e037b9ec
GM
8026 /* Build a string that represents the contents of the tool-bar. */
8027 build_desired_tool_bar_string (f);
8028 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 8029
e037b9ec 8030 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 8031 while (it.current_y < it.last_visible_y)
e037b9ec 8032 display_tool_bar_line (&it);
3450d04c 8033
e037b9ec 8034 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
8035 window, so don't do it. */
8036 w->desired_matrix->no_scrolling_p = 1;
8037 w->must_be_updated_p = 1;
3450d04c 8038
e037b9ec 8039 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
8040 {
8041 int nlines;
6e33e6f0
GM
8042
8043 /* If we couldn't display everything, change the tool-bar's
8044 height. */
8045 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8046 change_height_p = 1;
2311178e 8047
5f5c8ee5
GM
8048 /* If there are blank lines at the end, except for a partially
8049 visible blank line at the end that is smaller than
e037b9ec 8050 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
8051 row = it.glyph_row - 1;
8052 if (!row->displays_text_p
8053 && row->height >= CANON_Y_UNIT (f))
8054 change_height_p = 1;
8055
e037b9ec
GM
8056 /* If row displays tool-bar items, but is partially visible,
8057 change the tool-bar's height. */
5f5c8ee5
GM
8058 if (row->displays_text_p
8059 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8060 change_height_p = 1;
8061
e037b9ec 8062 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
8063 frame parameter. */
8064 if (change_height_p
e037b9ec 8065 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
8066 nlines != XFASTINT (w->height)))
8067 {
e037b9ec 8068 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 8069 Lisp_Object frame;
e85ee976 8070 int old_height = XFASTINT (w->height);
2311178e 8071
5f5c8ee5
GM
8072 XSETFRAME (frame, f);
8073 clear_glyph_matrix (w->desired_matrix);
8074 Fmodify_frame_parameters (frame,
e037b9ec 8075 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
8076 make_number (nlines)),
8077 Qnil));
e85ee976
GM
8078 if (XFASTINT (w->height) != old_height)
8079 fonts_changed_p = 1;
5f5c8ee5
GM
8080 }
8081 }
3450d04c 8082
5f5c8ee5 8083 return change_height_p;
96a410bc 8084}
90adcf20 8085
5f5c8ee5 8086
e037b9ec
GM
8087/* Get information about the tool-bar item which is displayed in GLYPH
8088 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8089 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8090 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
8091
8092int
e037b9ec 8093tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8094 struct frame *f;
8095 struct glyph *glyph;
8096 int *prop_idx;
90adcf20 8097{
5f5c8ee5
GM
8098 Lisp_Object prop;
8099 int success_p;
be676094
GM
8100 int charpos;
8101
8102 /* This function can be called asynchronously, which means we must
8103 exclude any possibility that Fget_text_property signals an
8104 error. */
2051c264 8105 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8106 charpos = max (0, charpos);
2311178e 8107
5f5c8ee5
GM
8108 /* Get the text property `menu-item' at pos. The value of that
8109 property is the start index of this item's properties in
7464726e 8110 F->tool_bar_items. */
be676094 8111 prop = Fget_text_property (make_number (charpos),
e037b9ec 8112 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8113 if (INTEGERP (prop))
8114 {
8115 *prop_idx = XINT (prop);
8116 success_p = 1;
8117 }
8118 else
8119 success_p = 0;
90adcf20 8120
5f5c8ee5
GM
8121 return success_p;
8122}
2311178e 8123
5f5c8ee5 8124#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 8125
feb0c42f 8126
5f5c8ee5
GM
8127\f
8128/************************************************************************
8129 Horizontal scrolling
8130 ************************************************************************/
feb0c42f 8131
5f5c8ee5
GM
8132static int hscroll_window_tree P_ ((Lisp_Object));
8133static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 8134
5f5c8ee5
GM
8135/* For all leaf windows in the window tree rooted at WINDOW, set their
8136 hscroll value so that PT is (i) visible in the window, and (ii) so
8137 that it is not within a certain margin at the window's left and
8138 right border. Value is non-zero if any window's hscroll has been
8139 changed. */
8140
8141static int
8142hscroll_window_tree (window)
8143 Lisp_Object window;
8144{
8145 int hscrolled_p = 0;
e76d28d5 8146 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
8147 int hscroll_step_abs = 0;
8148 double hscroll_step_rel = 0;
8149
8150 if (hscroll_relative_p)
8151 {
e76d28d5 8152 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
8153 if (hscroll_step_rel < 0)
8154 {
8155 hscroll_relative_p = 0;
8156 hscroll_step_abs = 0;
8157 }
8158 }
e76d28d5 8159 else if (INTEGERP (Vhscroll_step))
1df7e8f0 8160 {
e76d28d5 8161 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
8162 if (hscroll_step_abs < 0)
8163 hscroll_step_abs = 0;
8164 }
8165 else
8166 hscroll_step_abs = 0;
8167
5f5c8ee5 8168 while (WINDOWP (window))
90adcf20 8169 {
5f5c8ee5 8170 struct window *w = XWINDOW (window);
1df7e8f0 8171
5f5c8ee5
GM
8172 if (WINDOWP (w->hchild))
8173 hscrolled_p |= hscroll_window_tree (w->hchild);
8174 else if (WINDOWP (w->vchild))
8175 hscrolled_p |= hscroll_window_tree (w->vchild);
8176 else if (w->cursor.vpos >= 0)
8177 {
e76d28d5 8178 int h_margin, text_area_x, text_area_y;
5f5c8ee5 8179 int text_area_width, text_area_height;
92a90e89
GM
8180 struct glyph_row *current_cursor_row
8181 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8182 struct glyph_row *desired_cursor_row
8183 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8184 struct glyph_row *cursor_row
8185 = (desired_cursor_row->enabled_p
8186 ? desired_cursor_row
8187 : current_cursor_row);
a2725ab2 8188
5f5c8ee5
GM
8189 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8190 &text_area_width, &text_area_height);
90adcf20 8191
5f5c8ee5 8192 /* Scroll when cursor is inside this scroll margin. */
e76d28d5 8193 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
1df7e8f0 8194
5f5c8ee5 8195 if ((XFASTINT (w->hscroll)
e76d28d5 8196 && w->cursor.x <= h_margin)
92a90e89
GM
8197 || (cursor_row->enabled_p
8198 && cursor_row->truncated_on_right_p
e76d28d5 8199 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 8200 {
5f5c8ee5
GM
8201 struct it it;
8202 int hscroll;
8203 struct buffer *saved_current_buffer;
8204 int pt;
1df7e8f0 8205 int wanted_x;
5f5c8ee5
GM
8206
8207 /* Find point in a display of infinite width. */
8208 saved_current_buffer = current_buffer;
8209 current_buffer = XBUFFER (w->buffer);
1df7e8f0 8210
5f5c8ee5
GM
8211 if (w == XWINDOW (selected_window))
8212 pt = BUF_PT (current_buffer);
8213 else
08b610e4 8214 {
5f5c8ee5
GM
8215 pt = marker_position (w->pointm);
8216 pt = max (BEGV, pt);
8217 pt = min (ZV, pt);
8218 }
8219
8220 /* Move iterator to pt starting at cursor_row->start in
8221 a line with infinite width. */
8222 init_to_row_start (&it, w, cursor_row);
8223 it.last_visible_x = INFINITY;
8224 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8225 current_buffer = saved_current_buffer;
8226
1df7e8f0
EZ
8227 /* Position cursor in window. */
8228 if (!hscroll_relative_p && hscroll_step_abs == 0)
8229 hscroll = max (0, it.current_x - text_area_width / 2)
8230 / CANON_X_UNIT (it.f);
e76d28d5 8231 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
8232 {
8233 if (hscroll_relative_p)
8234 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 8235 - h_margin;
1df7e8f0
EZ
8236 else
8237 wanted_x = text_area_width
8238 - hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8239 - h_margin;
1df7e8f0
EZ
8240 hscroll
8241 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8242 }
8243 else
8244 {
8245 if (hscroll_relative_p)
8246 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 8247 + h_margin;
1df7e8f0
EZ
8248 else
8249 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8250 + h_margin;
1df7e8f0
EZ
8251 hscroll
8252 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8253 }
3172f70b 8254 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
8255
8256 /* Don't call Fset_window_hscroll if value hasn't
8257 changed because it will prevent redisplay
8258 optimizations. */
8259 if (XFASTINT (w->hscroll) != hscroll)
8260 {
3172f70b
GM
8261 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8262 w->hscroll = make_number (hscroll);
5f5c8ee5 8263 hscrolled_p = 1;
08b610e4 8264 }
08b610e4 8265 }
08b610e4 8266 }
a2725ab2 8267
5f5c8ee5 8268 window = w->next;
90adcf20 8269 }
cd6dfed6 8270
5f5c8ee5
GM
8271 /* Value is non-zero if hscroll of any leaf window has been changed. */
8272 return hscrolled_p;
8273}
8274
8275
8276/* Set hscroll so that cursor is visible and not inside horizontal
8277 scroll margins for all windows in the tree rooted at WINDOW. See
8278 also hscroll_window_tree above. Value is non-zero if any window's
8279 hscroll has been changed. If it has, desired matrices on the frame
8280 of WINDOW are cleared. */
8281
8282static int
8283hscroll_windows (window)
8284 Lisp_Object window;
8285{
d475bcb8 8286 int hscrolled_p;
2311178e 8287
d475bcb8
GM
8288 if (automatic_hscrolling_p)
8289 {
8290 hscrolled_p = hscroll_window_tree (window);
8291 if (hscrolled_p)
8292 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8293 }
8294 else
8295 hscrolled_p = 0;
5f5c8ee5 8296 return hscrolled_p;
90adcf20 8297}
5f5c8ee5
GM
8298
8299
90adcf20 8300\f
5f5c8ee5
GM
8301/************************************************************************
8302 Redisplay
8303 ************************************************************************/
8304
8305/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8306 to a non-zero value. This is sometimes handy to have in a debugger
8307 session. */
8308
8309#if GLYPH_DEBUG
a2889657 8310
5f5c8ee5
GM
8311/* First and last unchanged row for try_window_id. */
8312
8313int debug_first_unchanged_at_end_vpos;
8314int debug_last_unchanged_at_beg_vpos;
8315
8316/* Delta vpos and y. */
8317
8318int debug_dvpos, debug_dy;
8319
8320/* Delta in characters and bytes for try_window_id. */
8321
8322int debug_delta, debug_delta_bytes;
8323
8324/* Values of window_end_pos and window_end_vpos at the end of
8325 try_window_id. */
8326
31ade731 8327EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
8328
8329/* Append a string to W->desired_matrix->method. FMT is a printf
8330 format string. A1...A9 are a supplement for a variable-length
8331 argument list. If trace_redisplay_p is non-zero also printf the
8332 resulting string to stderr. */
8333
8334static void
8335debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8336 struct window *w;
8337 char *fmt;
8338 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8339{
8340 char buffer[512];
8341 char *method = w->desired_matrix->method;
8342 int len = strlen (method);
8343 int size = sizeof w->desired_matrix->method;
8344 int remaining = size - len - 1;
8345
8346 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8347 if (len && remaining)
8348 {
8349 method[len] = '|';
8350 --remaining, ++len;
8351 }
2311178e 8352
5f5c8ee5
GM
8353 strncpy (method + len, buffer, remaining);
8354
8355 if (trace_redisplay_p)
8356 fprintf (stderr, "%p (%s): %s\n",
8357 w,
8358 ((BUFFERP (w->buffer)
8359 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 8360 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
8361 : "no buffer"),
8362 buffer);
8363}
a2889657 8364
5f5c8ee5 8365#endif /* GLYPH_DEBUG */
90adcf20 8366
a2889657 8367
5f5c8ee5
GM
8368/* Value is non-zero if all changes in window W, which displays
8369 current_buffer, are in the text between START and END. START is a
8370 buffer position, END is given as a distance from Z. Used in
8371 redisplay_internal for display optimization. */
8372
8373static INLINE int
8374text_outside_line_unchanged_p (w, start, end)
8375 struct window *w;
8376 int start, end;
8377{
8378 int unchanged_p = 1;
2311178e 8379
5f5c8ee5
GM
8380 /* If text or overlays have changed, see where. */
8381 if (XFASTINT (w->last_modified) < MODIFF
8382 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8383 {
8384 /* Gap in the line? */
8385 if (GPT < start || Z - GPT < end)
8386 unchanged_p = 0;
8387
8388 /* Changes start in front of the line, or end after it? */
8389 if (unchanged_p
9142dd5b
GM
8390 && (BEG_UNCHANGED < start - 1
8391 || END_UNCHANGED < end))
5f5c8ee5 8392 unchanged_p = 0;
2311178e 8393
5f5c8ee5
GM
8394 /* If selective display, can't optimize if changes start at the
8395 beginning of the line. */
8396 if (unchanged_p
8397 && INTEGERP (current_buffer->selective_display)
8398 && XINT (current_buffer->selective_display) > 0
9142dd5b 8399 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 8400 unchanged_p = 0;
47d57b22
GM
8401
8402 /* If there are overlays at the start or end of the line, these
8403 may have overlay strings with newlines in them. A change at
8404 START, for instance, may actually concern the display of such
8405 overlay strings as well, and they are displayed on different
8406 lines. So, quickly rule out this case. (For the future, it
8407 might be desirable to implement something more telling than
8408 just BEG/END_UNCHANGED.) */
8409 if (unchanged_p)
8410 {
8411 if (BEG + BEG_UNCHANGED == start
8412 && overlay_touches_p (start))
8413 unchanged_p = 0;
8414 if (END_UNCHANGED == end
8415 && overlay_touches_p (Z - end))
8416 unchanged_p = 0;
8417 }
5f5c8ee5
GM
8418 }
8419
8420 return unchanged_p;
8421}
8422
8423
8424/* Do a frame update, taking possible shortcuts into account. This is
8425 the main external entry point for redisplay.
8426
8427 If the last redisplay displayed an echo area message and that message
8428 is no longer requested, we clear the echo area or bring back the
8429 mini-buffer if that is in use. */
20de20dc 8430
a2889657
JB
8431void
8432redisplay ()
e9874cee
RS
8433{
8434 redisplay_internal (0);
8435}
8436
47d57b22 8437
260a86a0
KH
8438/* Return 1 if point moved out of or into a composition. Otherwise
8439 return 0. PREV_BUF and PREV_PT are the last point buffer and
8440 position. BUF and PT are the current point buffer and position. */
8441
8442int
8443check_point_in_composition (prev_buf, prev_pt, buf, pt)
8444 struct buffer *prev_buf, *buf;
8445 int prev_pt, pt;
8446{
8447 int start, end;
8448 Lisp_Object prop;
8449 Lisp_Object buffer;
8450
8451 XSETBUFFER (buffer, buf);
8452 /* Check a composition at the last point if point moved within the
8453 same buffer. */
8454 if (prev_buf == buf)
8455 {
8456 if (prev_pt == pt)
8457 /* Point didn't move. */
8458 return 0;
2311178e 8459
260a86a0
KH
8460 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8461 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8462 && COMPOSITION_VALID_P (start, end, prop)
8463 && start < prev_pt && end > prev_pt)
8464 /* The last point was within the composition. Return 1 iff
8465 point moved out of the composition. */
8466 return (pt <= start || pt >= end);
8467 }
8468
8469 /* Check a composition at the current point. */
8470 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8471 && find_composition (pt, -1, &start, &end, &prop, buffer)
8472 && COMPOSITION_VALID_P (start, end, prop)
8473 && start < pt && end > pt);
8474}
5f5c8ee5 8475
47d57b22 8476
9142dd5b
GM
8477/* Reconsider the setting of B->clip_changed which is displayed
8478 in window W. */
8479
8480static INLINE void
8481reconsider_clip_changes (w, b)
8482 struct window *w;
8483 struct buffer *b;
8484{
c62c1bb5 8485 if (b->clip_changed
9142dd5b
GM
8486 && !NILP (w->window_end_valid)
8487 && w->current_matrix->buffer == b
8488 && w->current_matrix->zv == BUF_ZV (b)
8489 && w->current_matrix->begv == BUF_BEGV (b))
8490 b->clip_changed = 0;
260a86a0
KH
8491
8492 /* If display wasn't paused, and W is not a tool bar window, see if
8493 point has been moved into or out of a composition. In that case,
8494 we set b->clip_changed to 1 to force updating the screen. If
8495 b->clip_changed has already been set to 1, we can skip this
8496 check. */
8497 if (!b->clip_changed
8498 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8499 {
8500 int pt;
8501
8502 if (w == XWINDOW (selected_window))
8503 pt = BUF_PT (current_buffer);
8504 else
8505 pt = marker_position (w->pointm);
8506
8507 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 8508 || pt != XINT (w->last_point))
260a86a0 8509 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 8510 XINT (w->last_point),
260a86a0
KH
8511 XBUFFER (w->buffer), pt))
8512 b->clip_changed = 1;
8513 }
9142dd5b 8514}
aac2d8b2
RS
8515\f
8516#define STOP_POLLING \
8517do { if (! polling_stopped_here) stop_polling (); \
8518 polling_stopped_here = 1; } while (0)
8519
8520#define RESUME_POLLING \
8521do { if (polling_stopped_here) start_polling (); \
8522 polling_stopped_here = 0; } while (0)
9142dd5b
GM
8523
8524
5f5c8ee5
GM
8525/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8526 response to any user action; therefore, we should preserve the echo
8527 area. (Actually, our caller does that job.) Perhaps in the future
8528 avoid recentering windows if it is not necessary; currently that
8529 causes some problems. */
e9874cee
RS
8530
8531static void
8532redisplay_internal (preserve_echo_area)
8533 int preserve_echo_area;
a2889657 8534{
5f5c8ee5
GM
8535 struct window *w = XWINDOW (selected_window);
8536 struct frame *f = XFRAME (w->frame);
8537 int pause;
a2889657 8538 int must_finish = 0;
5f5c8ee5 8539 struct text_pos tlbufpos, tlendpos;
89819bdd 8540 int number_of_visible_frames;
28514cd9 8541 int count;
886bd6f2 8542 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 8543 int polling_stopped_here = 0;
a2889657 8544
5f5c8ee5
GM
8545 /* Non-zero means redisplay has to consider all windows on all
8546 frames. Zero means, only selected_window is considered. */
8547 int consider_all_windows_p;
2311178e 8548
5f5c8ee5
GM
8549 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8550
8551 /* No redisplay if running in batch mode or frame is not yet fully
8552 initialized, or redisplay is explicitly turned off by setting
8553 Vinhibit_redisplay. */
8554 if (noninteractive
8555 || !NILP (Vinhibit_redisplay)
8556 || !f->glyphs_initialized_p)
a2889657
JB
8557 return;
8558
5f5c8ee5
GM
8559 /* The flag redisplay_performed_directly_p is set by
8560 direct_output_for_insert when it already did the whole screen
8561 update necessary. */
8562 if (redisplay_performed_directly_p)
8563 {
8564 redisplay_performed_directly_p = 0;
8565 if (!hscroll_windows (selected_window))
8566 return;
8567 }
8568
488dd4c4 8569#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
8570 if (popup_activated ())
8571 return;
8572#endif
8573
28514cd9 8574 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 8575 if (redisplaying_p)
735c094c
KH
8576 return;
8577
28514cd9
GM
8578 /* Record a function that resets redisplaying_p to its old value
8579 when we leave this function. */
331379bf 8580 count = SPECPDL_INDEX ();
28514cd9
GM
8581 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8582 ++redisplaying_p;
26683087 8583 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 8584
8b32d885 8585 retry:
bd9d0f3f 8586 pause = 0;
9142dd5b
GM
8587 reconsider_clip_changes (w, current_buffer);
8588
5f5c8ee5
GM
8589 /* If new fonts have been loaded that make a glyph matrix adjustment
8590 necessary, do it. */
8591 if (fonts_changed_p)
8592 {
8593 adjust_glyphs (NULL);
8594 ++windows_or_buffers_changed;
8595 fonts_changed_p = 0;
8596 }
8597
6961e0c1
GM
8598 /* If face_change_count is non-zero, init_iterator will free all
8599 realized faces, which includes the faces referenced from current
8600 matrices. So, we can't reuse current matrices in this case. */
8601 if (face_change_count)
8602 ++windows_or_buffers_changed;
8603
886bd6f2
GM
8604 if (! FRAME_WINDOW_P (sf)
8605 && previous_terminal_frame != sf)
20de20dc 8606 {
5f5c8ee5
GM
8607 /* Since frames on an ASCII terminal share the same display
8608 area, displaying a different frame means redisplay the whole
8609 thing. */
20de20dc 8610 windows_or_buffers_changed++;
886bd6f2
GM
8611 SET_FRAME_GARBAGED (sf);
8612 XSETFRAME (Vterminal_frame, sf);
20de20dc 8613 }
886bd6f2 8614 previous_terminal_frame = sf;
20de20dc 8615
5f5c8ee5
GM
8616 /* Set the visible flags for all frames. Do this before checking
8617 for resized or garbaged frames; they want to know if their frames
8618 are visible. See the comment in frame.h for
8619 FRAME_SAMPLE_VISIBILITY. */
d724d989 8620 {
35f56f96 8621 Lisp_Object tail, frame;
d724d989 8622
89819bdd
RS
8623 number_of_visible_frames = 0;
8624
35f56f96 8625 FOR_EACH_FRAME (tail, frame)
f82aff7c 8626 {
5f5c8ee5 8627 struct frame *f = XFRAME (frame);
2311178e 8628
5f5c8ee5
GM
8629 FRAME_SAMPLE_VISIBILITY (f);
8630 if (FRAME_VISIBLE_P (f))
8631 ++number_of_visible_frames;
8632 clear_desired_matrices (f);
f82aff7c 8633 }
d724d989
JB
8634 }
8635
44fa5b1e 8636 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8637 do_pending_window_change (1);
a2889657 8638
5f5c8ee5 8639 /* Clear frames marked as garbaged. */
44fa5b1e 8640 if (frame_garbaged)
c6e89d6c 8641 clear_garbaged_frames ();
a2889657 8642
e037b9ec 8643 /* Build menubar and tool-bar items. */
f82aff7c
RS
8644 prepare_menu_bars ();
8645
28995e67 8646 if (windows_or_buffers_changed)
a2889657
JB
8647 update_mode_lines++;
8648
538f13d4
RS
8649 /* Detect case that we need to write or remove a star in the mode line. */
8650 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8651 {
8652 w->update_mode_line = Qt;
8653 if (buffer_shared > 1)
8654 update_mode_lines++;
8655 }
8656
5f5c8ee5 8657 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8658 if (!NILP (w->column_number_displayed)
8659 /* This alternative quickly identifies a common case
8660 where no change is needed. */
8661 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8662 && XFASTINT (w->last_modified) >= MODIFF
8663 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
8664 && (XFASTINT (w->column_number_displayed)
8665 != (int) current_column ())) /* iftc */
8666 w->update_mode_line = Qt;
28995e67 8667
44fa5b1e 8668 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8669
5f5c8ee5
GM
8670 /* The variable buffer_shared is set in redisplay_window and
8671 indicates that we redisplay a buffer in different windows. See
8672 there. */
5fb96e96
RS
8673 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8674 || cursor_type_changed);
a2889657
JB
8675
8676 /* If specs for an arrow have changed, do thorough redisplay
8677 to ensure we remove any arrow that should no longer exist. */
d45de95b 8678 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8679 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8680 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8681
90adcf20
RS
8682 /* Normally the message* functions will have already displayed and
8683 updated the echo area, but the frame may have been trashed, or
8684 the update may have been preempted, so display the echo area
6e019995 8685 again here. Checking message_cleared_p captures the case that
c6e89d6c 8686 the echo area should be cleared. */
96d5e9a0
GM
8687 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8688 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
8689 || (message_cleared_p
8690 && minibuf_level == 0
8691 /* If the mini-window is currently selected, this means the
8692 echo-area doesn't show through. */
8693 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 8694 {
c6e89d6c 8695 int window_height_changed_p = echo_area_display (0);
90adcf20 8696 must_finish = 1;
6e019995
GM
8697
8698 /* If we don't display the current message, don't clear the
8699 message_cleared_p flag, because, if we did, we wouldn't clear
8700 the echo area in the next redisplay which doesn't preserve
8701 the echo area. */
8702 if (!display_last_displayed_message_p)
8703 message_cleared_p = 0;
2311178e 8704
c6e89d6c
GM
8705 if (fonts_changed_p)
8706 goto retry;
8707 else if (window_height_changed_p)
8708 {
8709 consider_all_windows_p = 1;
8710 ++update_mode_lines;
8711 ++windows_or_buffers_changed;
2311178e 8712
9142dd5b
GM
8713 /* If window configuration was changed, frames may have been
8714 marked garbaged. Clear them or we will experience
8715 surprises wrt scrolling. */
8716 if (frame_garbaged)
8717 clear_garbaged_frames ();
c6e89d6c 8718 }
90adcf20 8719 }
97a36635 8720 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
8721 && (current_buffer->clip_changed
8722 || XFASTINT (w->last_modified) < MODIFF
8723 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 8724 && resize_mini_window (w, 0))
c6e89d6c
GM
8725 {
8726 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
8727 showing if its contents might have changed. */
8728 must_finish = 1;
8729 consider_all_windows_p = 1;
c6e89d6c 8730 ++windows_or_buffers_changed;
dd2eb166 8731 ++update_mode_lines;
2311178e 8732
9142dd5b
GM
8733 /* If window configuration was changed, frames may have been
8734 marked garbaged. Clear them or we will experience
8735 surprises wrt scrolling. */
8736 if (frame_garbaged)
8737 clear_garbaged_frames ();
c6e89d6c 8738 }
2311178e 8739
90adcf20 8740
5f5c8ee5
GM
8741 /* If showing the region, and mark has changed, we must redisplay
8742 the whole window. The assignment to this_line_start_pos prevents
8743 the optimization directly below this if-statement. */
bd66d1ba
RS
8744 if (((!NILP (Vtransient_mark_mode)
8745 && !NILP (XBUFFER (w->buffer)->mark_active))
8746 != !NILP (w->region_showing))
82d04750
JB
8747 || (!NILP (w->region_showing)
8748 && !EQ (w->region_showing,
8749 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
8750 CHARPOS (this_line_start_pos) = 0;
8751
8752 /* Optimize the case that only the line containing the cursor in the
8753 selected window has changed. Variables starting with this_ are
8754 set in display_line and record information about the line
8755 containing the cursor. */
8756 tlbufpos = this_line_start_pos;
8757 tlendpos = this_line_end_pos;
8758 if (!consider_all_windows_p
8759 && CHARPOS (tlbufpos) > 0
8760 && NILP (w->update_mode_line)
73af359d 8761 && !current_buffer->clip_changed
c62c1bb5 8762 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 8763 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 8764 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 8765 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
8766 && this_line_buffer == current_buffer
8767 && current_buffer == XBUFFER (w->buffer)
265a9e55 8768 && NILP (w->force_start)
acda20e1 8769 && NILP (w->optional_new_start)
5f5c8ee5
GM
8770 /* Point must be on the line that we have info recorded about. */
8771 && PT >= CHARPOS (tlbufpos)
8772 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
8773 /* All text outside that line, including its final newline,
8774 must be unchanged */
5f5c8ee5
GM
8775 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8776 CHARPOS (tlendpos)))
8777 {
8778 if (CHARPOS (tlbufpos) > BEGV
8779 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8780 && (CHARPOS (tlbufpos) == ZV
8781 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
8782 /* Former continuation line has disappeared by becoming empty */
8783 goto cancel;
8784 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 8785 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
8786 || MINI_WINDOW_P (w))
8787 {
1c9241f5
KH
8788 /* We have to handle the case of continuation around a
8789 wide-column character (See the comment in indent.c around
8790 line 885).
8791
8792 For instance, in the following case:
8793
8794 -------- Insert --------
8795 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8796 J_I_ ==> J_I_ `^^' are cursors.
8797 ^^ ^^
8798 -------- --------
8799
8800 As we have to redraw the line above, we should goto cancel. */
8801
5f5c8ee5
GM
8802 struct it it;
8803 int line_height_before = this_line_pixel_height;
8804
8805 /* Note that start_display will handle the case that the
8806 line starting at tlbufpos is a continuation lines. */
8807 start_display (&it, w, tlbufpos);
8808
8809 /* Implementation note: It this still necessary? */
8810 if (it.current_x != this_line_start_x)
1c9241f5
KH
8811 goto cancel;
8812
5f5c8ee5
GM
8813 TRACE ((stderr, "trying display optimization 1\n"));
8814 w->cursor.vpos = -1;
a2889657 8815 overlay_arrow_seen = 0;
5f5c8ee5
GM
8816 it.vpos = this_line_vpos;
8817 it.current_y = this_line_y;
8818 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8819 display_line (&it);
8820
a2889657 8821 /* If line contains point, is not continued,
5f5c8ee5 8822 and ends at same distance from eob as before, we win */
2311178e 8823 if (w->cursor.vpos >= 0
5f5c8ee5
GM
8824 /* Line is not continued, otherwise this_line_start_pos
8825 would have been set to 0 in display_line. */
8826 && CHARPOS (this_line_start_pos)
8827 /* Line ends as before. */
8828 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8829 /* Line has same height as before. Otherwise other lines
8830 would have to be shifted up or down. */
8831 && this_line_pixel_height == line_height_before)
a2889657 8832 {
5f5c8ee5
GM
8833 /* If this is not the window's last line, we must adjust
8834 the charstarts of the lines below. */
8835 if (it.current_y < it.last_visible_y)
8836 {
8837 struct glyph_row *row
8838 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8839 int delta, delta_bytes;
2311178e 8840
5f5c8ee5
GM
8841 if (Z - CHARPOS (tlendpos) == ZV)
8842 {
8843 /* This line ends at end of (accessible part of)
8844 buffer. There is no newline to count. */
8845 delta = (Z
8846 - CHARPOS (tlendpos)
8847 - MATRIX_ROW_START_CHARPOS (row));
8848 delta_bytes = (Z_BYTE
8849 - BYTEPOS (tlendpos)
8850 - MATRIX_ROW_START_BYTEPOS (row));
8851 }
8852 else
8853 {
8854 /* This line ends in a newline. Must take
8855 account of the newline and the rest of the
8856 text that follows. */
8857 delta = (Z
8858 - CHARPOS (tlendpos)
8859 - MATRIX_ROW_START_CHARPOS (row));
8860 delta_bytes = (Z_BYTE
8861 - BYTEPOS (tlendpos)
8862 - MATRIX_ROW_START_BYTEPOS (row));
8863 }
2311178e 8864
f2d86d7a
GM
8865 increment_matrix_positions (w->current_matrix,
8866 this_line_vpos + 1,
8867 w->current_matrix->nrows,
8868 delta, delta_bytes);
85bcef6c 8869 }
46db8486 8870
5f5c8ee5
GM
8871 /* If this row displays text now but previously didn't,
8872 or vice versa, w->window_end_vpos may have to be
8873 adjusted. */
8874 if ((it.glyph_row - 1)->displays_text_p)
8875 {
8876 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8877 XSETINT (w->window_end_vpos, this_line_vpos);
8878 }
8879 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8880 && this_line_vpos > 0)
8881 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8882 w->window_end_valid = Qnil;
2311178e 8883
5f5c8ee5
GM
8884 /* Update hint: No need to try to scroll in update_window. */
8885 w->desired_matrix->no_scrolling_p = 1;
8886
8887#if GLYPH_DEBUG
8888 *w->desired_matrix->method = 0;
8889 debug_method_add (w, "optimization 1");
8890#endif
a2889657
JB
8891 goto update;
8892 }
8893 else
8894 goto cancel;
8895 }
5f5c8ee5
GM
8896 else if (/* Cursor position hasn't changed. */
8897 PT == XFASTINT (w->last_point)
b6f0fe04
RS
8898 /* Make sure the cursor was last displayed
8899 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
8900 && 0 <= w->cursor.vpos
8901 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
8902 {
8903 if (!must_finish)
8904 {
c6e89d6c 8905 do_pending_window_change (1);
5f5c8ee5 8906
2311178e 8907 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
8908 isn't enough if we have a blinking cursor. */
8909 if (w->cursor_off_p == w->last_cursor_off_p)
8910 goto end_of_redisplay;
a2889657
JB
8911 }
8912 goto update;
8913 }
8b51f1e3
KH
8914 /* If highlighting the region, or if the cursor is in the echo area,
8915 then we can't just move the cursor. */
bd66d1ba
RS
8916 else if (! (!NILP (Vtransient_mark_mode)
8917 && !NILP (current_buffer->mark_active))
97a36635 8918 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 8919 || highlight_nonselected_windows)
8b51f1e3 8920 && NILP (w->region_showing)
8f897821 8921 && NILP (Vshow_trailing_whitespace)
8b51f1e3 8922 && !cursor_in_echo_area)
a2889657 8923 {
5f5c8ee5
GM
8924 struct it it;
8925 struct glyph_row *row;
8926
8927 /* Skip from tlbufpos to PT and see where it is. Note that
8928 PT may be in invisible text. If so, we will end at the
8929 next visible position. */
8930 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8931 NULL, DEFAULT_FACE_ID);
8932 it.current_x = this_line_start_x;
8933 it.current_y = this_line_y;
8934 it.vpos = this_line_vpos;
2311178e 8935
5f5c8ee5
GM
8936 /* The call to move_it_to stops in front of PT, but
8937 moves over before-strings. */
8938 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8939
8940 if (it.vpos == this_line_vpos
8941 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8942 row->enabled_p))
a2889657 8943 {
5f5c8ee5
GM
8944 xassert (this_line_vpos == it.vpos);
8945 xassert (this_line_y == it.current_y);
8946 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
8947#if GLYPH_DEBUG
8948 *w->desired_matrix->method = 0;
8949 debug_method_add (w, "optimization 3");
8950#endif
a2889657
JB
8951 goto update;
8952 }
8953 else
8954 goto cancel;
8955 }
5f5c8ee5 8956
a2889657 8957 cancel:
5f5c8ee5
GM
8958 /* Text changed drastically or point moved off of line. */
8959 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
8960 }
8961
5f5c8ee5
GM
8962 CHARPOS (this_line_start_pos) = 0;
8963 consider_all_windows_p |= buffer_shared > 1;
8964 ++clear_face_cache_count;
a2889657 8965
2311178e 8966
bd9d0f3f
GM
8967 /* Build desired matrices, and update the display. If
8968 consider_all_windows_p is non-zero, do it for all windows on all
8969 frames. Otherwise do it for selected_window, only. */
463f6b91 8970
5f5c8ee5 8971 if (consider_all_windows_p)
a2889657 8972 {
35f56f96 8973 Lisp_Object tail, frame;
0528abe1
GM
8974 int i, n = 0, size = 50;
8975 struct frame **updated
8976 = (struct frame **) alloca (size * sizeof *updated);
a2889657 8977
5f5c8ee5
GM
8978 /* Clear the face cache eventually. */
8979 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 8980 {
5f5c8ee5 8981 clear_face_cache (0);
463f6b91
RS
8982 clear_face_cache_count = 0;
8983 }
31b24551 8984
5f5c8ee5
GM
8985 /* Recompute # windows showing selected buffer. This will be
8986 incremented each time such a window is displayed. */
a2889657
JB
8987 buffer_shared = 0;
8988
35f56f96 8989 FOR_EACH_FRAME (tail, frame)
30c566e4 8990 {
5f5c8ee5 8991 struct frame *f = XFRAME (frame);
2311178e 8992
886bd6f2 8993 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 8994 {
ae02e06a 8995#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
8996 if (clear_face_cache_count % 50 == 0
8997 && FRAME_WINDOW_P (f))
8998 clear_image_cache (f, 0);
ae02e06a 8999#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 9000
5f5c8ee5
GM
9001 /* Mark all the scroll bars to be removed; we'll redeem
9002 the ones we want when we redisplay their windows. */
9769686d 9003 if (condemn_scroll_bars_hook)
504454e8 9004 condemn_scroll_bars_hook (f);
30c566e4 9005
f21ef775 9006 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 9007 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 9008
5f5c8ee5
GM
9009 /* Any scroll bars which redisplay_windows should have
9010 nuked should now go away. */
9769686d 9011 if (judge_scroll_bars_hook)
504454e8 9012 judge_scroll_bars_hook (f);
bd9d0f3f
GM
9013
9014 /* If fonts changed, display again. */
b60c9653
RS
9015 /* ??? rms: I suspect it is a mistake to jump all the way
9016 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
9017 if (fonts_changed_p)
9018 goto retry;
2311178e 9019
bd9d0f3f
GM
9020 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9021 {
9022 /* See if we have to hscroll. */
9023 if (hscroll_windows (f->root_window))
9024 goto retry;
9025
9026 /* Prevent various kinds of signals during display
9027 update. stdio is not robust about handling
9028 signals, which can cause an apparent I/O
9029 error. */
9030 if (interrupt_input)
9031 unrequest_sigio ();
aac2d8b2 9032 STOP_POLLING;
bd9d0f3f
GM
9033
9034 /* Update the display. */
9035 set_window_update_flags (XWINDOW (f->root_window), 1);
9036 pause |= update_frame (f, 0, 0);
9037 if (pause)
9038 break;
9039
0528abe1
GM
9040 if (n == size)
9041 {
9042 int nbytes = size * sizeof *updated;
9043 struct frame **p = (struct frame **) alloca (2 * nbytes);
9044 bcopy (updated, p, nbytes);
9045 size *= 2;
9046 }
2311178e 9047
0528abe1 9048 updated[n++] = f;
bd9d0f3f 9049 }
9769686d 9050 }
30c566e4 9051 }
0528abe1
GM
9052
9053 /* Do the mark_window_display_accurate after all windows have
9054 been redisplayed because this call resets flags in buffers
9055 which are needed for proper redisplay. */
9056 for (i = 0; i < n; ++i)
9057 {
9058 struct frame *f = updated[i];
9059 mark_window_display_accurate (f->root_window, 1);
9060 if (frame_up_to_date_hook)
9061 frame_up_to_date_hook (f);
9062 }
a2889657 9063 }
bd9d0f3f
GM
9064 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9065 {
9066 Lisp_Object mini_window;
9067 struct frame *mini_frame;
5f5c8ee5 9068
82a7ab23 9069 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
9070 /* Use list_of_error, not Qerror, so that
9071 we catch only errors and don't run the debugger. */
9072 internal_condition_case_1 (redisplay_window_1, selected_window,
9073 list_of_error,
82a7ab23 9074 redisplay_window_error);
2311178e 9075
bd9d0f3f 9076 /* Compare desired and current matrices, perform output. */
7d0393cf 9077
26683087 9078 update:
bd9d0f3f
GM
9079 /* If fonts changed, display again. */
9080 if (fonts_changed_p)
92a90e89 9081 goto retry;
a2889657 9082
bd9d0f3f
GM
9083 /* Prevent various kinds of signals during display update.
9084 stdio is not robust about handling signals,
9085 which can cause an apparent I/O error. */
9086 if (interrupt_input)
9087 unrequest_sigio ();
aac2d8b2 9088 STOP_POLLING;
1af9f229 9089
bd9d0f3f 9090 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 9091 {
92a90e89
GM
9092 if (hscroll_windows (selected_window))
9093 goto retry;
2311178e 9094
5f5c8ee5 9095 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 9096 pause = update_frame (sf, 0, 0);
5f5c8ee5 9097 }
d724d989 9098
8de2d90b 9099 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
9100 function. If the echo area is on another frame, that may
9101 have put text on a frame other than the selected one, so the
9102 above call to update_frame would not have caught it. Catch
8de2d90b 9103 it here. */
bd9d0f3f
GM
9104 mini_window = FRAME_MINIBUF_WINDOW (sf);
9105 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 9106
bd9d0f3f
GM
9107 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9108 {
9109 XWINDOW (mini_window)->must_be_updated_p = 1;
9110 pause |= update_frame (mini_frame, 0, 0);
9111 if (!pause && hscroll_windows (mini_window))
9112 goto retry;
9113 }
6e8290aa 9114 }
a2889657 9115
5f5c8ee5
GM
9116 /* If display was paused because of pending input, make sure we do a
9117 thorough update the next time. */
a2889657
JB
9118 if (pause)
9119 {
5f5c8ee5
GM
9120 /* Prevent the optimization at the beginning of
9121 redisplay_internal that tries a single-line update of the
9122 line containing the cursor in the selected window. */
9123 CHARPOS (this_line_start_pos) = 0;
9124
9125 /* Let the overlay arrow be updated the next time. */
265a9e55 9126 if (!NILP (last_arrow_position))
a2889657
JB
9127 {
9128 last_arrow_position = Qt;
9129 last_arrow_string = Qt;
9130 }
2311178e 9131
5f5c8ee5
GM
9132 /* If we pause after scrolling, some rows in the current
9133 matrices of some windows are not valid. */
9134 if (!WINDOW_FULL_WIDTH_P (w)
9135 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
9136 update_mode_lines = 1;
9137 }
43c09969 9138 else
a2889657 9139 {
43c09969 9140 if (!consider_all_windows_p)
a2889657 9141 {
43c09969
GM
9142 /* This has already been done above if
9143 consider_all_windows_p is set. */
9144 mark_window_display_accurate_1 (w, 1);
2311178e 9145
927c5b3b
GM
9146 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9147 last_arrow_string = Voverlay_arrow_string;
2311178e 9148
efc63ef0 9149 if (frame_up_to_date_hook != 0)
43c09969 9150 frame_up_to_date_hook (sf);
a2889657 9151 }
15e26c76 9152
a2889657
JB
9153 update_mode_lines = 0;
9154 windows_or_buffers_changed = 0;
5fb96e96 9155 cursor_type_changed = 0;
a2889657
JB
9156 }
9157
5f5c8ee5
GM
9158 /* Start SIGIO interrupts coming again. Having them off during the
9159 code above makes it less likely one will discard output, but not
9160 impossible, since there might be stuff in the system buffer here.
a2889657 9161 But it is much hairier to try to do anything about that. */
a2889657
JB
9162 if (interrupt_input)
9163 request_sigio ();
aac2d8b2 9164 RESUME_POLLING;
a2889657 9165
5f5c8ee5
GM
9166 /* If a frame has become visible which was not before, redisplay
9167 again, so that we display it. Expose events for such a frame
9168 (which it gets when becoming visible) don't call the parts of
9169 redisplay constructing glyphs, so simply exposing a frame won't
9170 display anything in this case. So, we have to display these
9171 frames here explicitly. */
11c52c4f
RS
9172 if (!pause)
9173 {
9174 Lisp_Object tail, frame;
9175 int new_count = 0;
9176
9177 FOR_EACH_FRAME (tail, frame)
9178 {
9179 int this_is_visible = 0;
8e83f802
RS
9180
9181 if (XFRAME (frame)->visible)
9182 this_is_visible = 1;
9183 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9184 if (XFRAME (frame)->visible)
9185 this_is_visible = 1;
11c52c4f
RS
9186
9187 if (this_is_visible)
9188 new_count++;
9189 }
9190
89819bdd 9191 if (new_count != number_of_visible_frames)
11c52c4f
RS
9192 windows_or_buffers_changed++;
9193 }
9194
44fa5b1e 9195 /* Change frame size now if a change is pending. */
c6e89d6c 9196 do_pending_window_change (1);
d8e242fd 9197
8b32d885
RS
9198 /* If we just did a pending size change, or have additional
9199 visible frames, redisplay again. */
3c8c72e0 9200 if (windows_or_buffers_changed && !pause)
8b32d885 9201 goto retry;
5f5c8ee5 9202
1987b083 9203 end_of_redisplay:
28514cd9 9204 unbind_to (count, Qnil);
aac2d8b2 9205 RESUME_POLLING;
a2889657
JB
9206}
9207
5f5c8ee5
GM
9208
9209/* Redisplay, but leave alone any recent echo area message unless
9210 another message has been requested in its place.
a2889657
JB
9211
9212 This is useful in situations where you need to redisplay but no
9213 user action has occurred, making it inappropriate for the message
9214 area to be cleared. See tracking_off and
9bf76936
GM
9215 wait_reading_process_input for examples of these situations.
9216
9217 FROM_WHERE is an integer saying from where this function was
9218 called. This is useful for debugging. */
a2889657 9219
8991bb31 9220void
9bf76936
GM
9221redisplay_preserve_echo_area (from_where)
9222 int from_where;
a2889657 9223{
9bf76936
GM
9224 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9225
c6e89d6c 9226 if (!NILP (echo_area_buffer[1]))
a2889657 9227 {
c6e89d6c
GM
9228 /* We have a previously displayed message, but no current
9229 message. Redisplay the previous message. */
9230 display_last_displayed_message_p = 1;
e9874cee 9231 redisplay_internal (1);
c6e89d6c 9232 display_last_displayed_message_p = 0;
a2889657
JB
9233 }
9234 else
e9874cee 9235 redisplay_internal (1);
a2889657
JB
9236}
9237
5f5c8ee5 9238
28514cd9 9239/* Function registered with record_unwind_protect in
acfca545
RS
9240 redisplay_internal. Reset redisplaying_p to the value it had
9241 before redisplay_internal was called, and clear
26683087 9242 prevent_freeing_realized_faces_p. */
28514cd9
GM
9243
9244static Lisp_Object
9245unwind_redisplay (old_redisplaying_p)
9246 Lisp_Object old_redisplaying_p;
9247{
9248 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 9249 return Qnil;
28514cd9
GM
9250}
9251
9252
43c09969
GM
9253/* Mark the display of window W as accurate or inaccurate. If
9254 ACCURATE_P is non-zero mark display of W as accurate. If
9255 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9256 redisplay_internal is called. */
5f5c8ee5 9257
43c09969
GM
9258static void
9259mark_window_display_accurate_1 (w, accurate_p)
9260 struct window *w;
5f5c8ee5 9261 int accurate_p;
a2889657 9262{
43c09969 9263 if (BUFFERP (w->buffer))
a2889657 9264 {
43c09969 9265 struct buffer *b = XBUFFER (w->buffer);
2311178e 9266
43c09969
GM
9267 w->last_modified
9268 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9269 w->last_overlay_modified
9270 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9271 w->last_had_star
9272 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 9273
43c09969 9274 if (accurate_p)
bd66d1ba 9275 {
43c09969
GM
9276 b->clip_changed = 0;
9277 b->prevent_redisplay_optimizations_p = 0;
9278
9279 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9280 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9281 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9282 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 9283
43c09969
GM
9284 w->current_matrix->buffer = b;
9285 w->current_matrix->begv = BUF_BEGV (b);
9286 w->current_matrix->zv = BUF_ZV (b);
2311178e 9287
43c09969
GM
9288 w->last_cursor = w->cursor;
9289 w->last_cursor_off_p = w->cursor_off_p;
2311178e 9290
43c09969
GM
9291 if (w == XWINDOW (selected_window))
9292 w->last_point = make_number (BUF_PT (b));
9293 else
9294 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 9295 }
43c09969 9296 }
bd66d1ba 9297
43c09969
GM
9298 if (accurate_p)
9299 {
d2f84654 9300 w->window_end_valid = w->buffer;
99332eb6 9301#if 0 /* This is incorrect with variable-height lines. */
2913a9c0
GM
9302 xassert (XINT (w->window_end_vpos)
9303 < (XINT (w->height)
9304 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 9305#endif
a2889657 9306 w->update_mode_line = Qnil;
43c09969
GM
9307 }
9308}
9309
9310
9311/* Mark the display of windows in the window tree rooted at WINDOW as
9312 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9313 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9314 be redisplayed the next time redisplay_internal is called. */
9315
9316void
9317mark_window_display_accurate (window, accurate_p)
9318 Lisp_Object window;
9319 int accurate_p;
9320{
9321 struct window *w;
2311178e 9322
43c09969
GM
9323 for (; !NILP (window); window = w->next)
9324 {
9325 w = XWINDOW (window);
9326 mark_window_display_accurate_1 (w, accurate_p);
a2889657 9327
265a9e55 9328 if (!NILP (w->vchild))
5f5c8ee5 9329 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 9330 if (!NILP (w->hchild))
5f5c8ee5 9331 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
9332 }
9333
5f5c8ee5 9334 if (accurate_p)
a2889657 9335 {
d45de95b 9336 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
9337 last_arrow_string = Voverlay_arrow_string;
9338 }
9339 else
9340 {
5f5c8ee5
GM
9341 /* Force a thorough redisplay the next time by setting
9342 last_arrow_position and last_arrow_string to t, which is
4b41cebb 9343 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
9344 last_arrow_position = Qt;
9345 last_arrow_string = Qt;
9346 }
9347}
5f5c8ee5
GM
9348
9349
9350/* Return value in display table DP (Lisp_Char_Table *) for character
9351 C. Since a display table doesn't have any parent, we don't have to
9352 follow parent. Do not call this function directly but use the
9353 macro DISP_CHAR_VECTOR. */
9354
9355Lisp_Object
9356disp_char_vector (dp, c)
9357 struct Lisp_Char_Table *dp;
9358 int c;
9359{
9360 int code[4], i;
9361 Lisp_Object val;
9362
9363 if (SINGLE_BYTE_CHAR_P (c))
9364 return (dp->contents[c]);
2311178e 9365
c5924f47 9366 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
9367 if (code[1] < 32)
9368 code[1] = -1;
9369 else if (code[2] < 32)
9370 code[2] = -1;
2311178e 9371
5f5c8ee5
GM
9372 /* Here, the possible range of code[0] (== charset ID) is
9373 128..max_charset. Since the top level char table contains data
9374 for multibyte characters after 256th element, we must increment
9375 code[0] by 128 to get a correct index. */
9376 code[0] += 128;
9377 code[3] = -1; /* anchor */
9378
9379 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9380 {
9381 val = dp->contents[code[i]];
9382 if (!SUB_CHAR_TABLE_P (val))
9383 return (NILP (val) ? dp->defalt : val);
9384 }
2311178e 9385
5f5c8ee5
GM
9386 /* Here, val is a sub char table. We return the default value of
9387 it. */
9388 return (dp->defalt);
9389}
9390
9391
a2889657 9392\f
5f5c8ee5
GM
9393/***********************************************************************
9394 Window Redisplay
9395 ***********************************************************************/
a2725ab2 9396
5f5c8ee5 9397/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
9398
9399static void
5f5c8ee5
GM
9400redisplay_windows (window)
9401 Lisp_Object window;
90adcf20 9402{
5f5c8ee5
GM
9403 while (!NILP (window))
9404 {
9405 struct window *w = XWINDOW (window);
2311178e 9406
5f5c8ee5
GM
9407 if (!NILP (w->hchild))
9408 redisplay_windows (w->hchild);
9409 else if (!NILP (w->vchild))
9410 redisplay_windows (w->vchild);
9411 else
82a7ab23
RS
9412 {
9413 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
9414 /* Use list_of_error, not Qerror, so that
9415 we catch only errors and don't run the debugger. */
2311178e 9416 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 9417 list_of_error,
82a7ab23
RS
9418 redisplay_window_error);
9419 }
a2725ab2 9420
5f5c8ee5
GM
9421 window = w->next;
9422 }
9423}
9424
82a7ab23
RS
9425static Lisp_Object
9426redisplay_window_error ()
9427{
9428 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9429 return Qnil;
9430}
9431
9432static Lisp_Object
9433redisplay_window_0 (window)
9434 Lisp_Object window;
9435{
9436 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9437 redisplay_window (window, 0);
9438 return Qnil;
9439}
5f5c8ee5 9440
82a7ab23
RS
9441static Lisp_Object
9442redisplay_window_1 (window)
9443 Lisp_Object window;
9444{
9445 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9446 redisplay_window (window, 1);
9447 return Qnil;
9448}
9449\f
bc6371a6
KH
9450
9451/* Increment GLYPH until it reaches END or CONDITION fails while
9452 adding (GLYPH)->pixel_width to X. */
9453
9454#define SKIP_GLYPHS(glyph, end, x, condition) \
9455 do \
9456 { \
9457 (x) += (glyph)->pixel_width; \
9458 ++(glyph); \
9459 } \
9460 while ((glyph) < (end) && (condition))
9461
9462
5f5c8ee5
GM
9463/* Set cursor position of W. PT is assumed to be displayed in ROW.
9464 DELTA is the number of bytes by which positions recorded in ROW
9465 differ from current buffer positions. */
9466
9467void
9468set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9469 struct window *w;
9470 struct glyph_row *row;
9471 struct glyph_matrix *matrix;
9472 int delta, delta_bytes, dy, dvpos;
9473{
9474 struct glyph *glyph = row->glyphs[TEXT_AREA];
9475 struct glyph *end = glyph + row->used[TEXT_AREA];
ae73dc1c
KH
9476 /* The first glyph that starts a sequence of glyphs from string. */
9477 struct glyph *string_start;
9478 /* The X coordinate of string_start. */
9479 int string_start_x;
9480 /* The last known character position. */
9481 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
9482 /* The last known character position before string_start. */
9483 int string_before_pos;
5f5c8ee5
GM
9484 int x = row->x;
9485 int pt_old = PT - delta;
9486
9487 /* Skip over glyphs not having an object at the start of the row.
9488 These are special glyphs like truncation marks on terminal
9489 frames. */
9490 if (row->displays_text_p)
9491 while (glyph < end
6fc556fd 9492 && INTEGERP (glyph->object)
5f5c8ee5
GM
9493 && glyph->charpos < 0)
9494 {
9495 x += glyph->pixel_width;
9496 ++glyph;
9497 }
9498
ae73dc1c 9499 string_start = NULL;
5f5c8ee5 9500 while (glyph < end
6fc556fd 9501 && !INTEGERP (glyph->object)
5f5c8ee5 9502 && (!BUFFERP (glyph->object)
ae73dc1c 9503 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 9504 {
ae73dc1c
KH
9505 if (! STRINGP (glyph->object))
9506 {
9507 string_start = NULL;
9508 x += glyph->pixel_width;
9509 ++glyph;
9510 }
9511 else
9512 {
9513 string_before_pos = last_pos;
9514 string_start = glyph;
9515 string_start_x = x;
9516 /* Skip all glyphs from string. */
bc6371a6 9517 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
ae73dc1c
KH
9518 }
9519 }
9520
9521 if (string_start
9522 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
9523 {
9524 /* We may have skipped over point because the previous glyphs
9525 are from string. As there's no easy way to know the
9526 character position of the current glyph, find the correct
9527 glyph on point by scanning from string_start again. */
bc6371a6
KH
9528 Lisp_Object limit;
9529 Lisp_Object string;
9530 int pos;
ae73dc1c 9531
bc6371a6
KH
9532 limit = make_number (pt_old + 1);
9533 end = glyph;
ae73dc1c
KH
9534 glyph = string_start;
9535 x = string_start_x;
bc6371a6
KH
9536 string = glyph->object;
9537 pos = string_buffer_position (w, string, string_before_pos);
9538 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
9539 because we always put cursor after overlay strings. */
9540 while (pos == 0 && glyph < end)
ae73dc1c 9541 {
bc6371a6
KH
9542 string = glyph->object;
9543 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9544 if (glyph < end)
9545 pos = string_buffer_position (w, glyph->object, string_before_pos);
9546 }
9547
9548 while (glyph < end)
9549 {
9550 pos = XINT (Fnext_single_char_property_change
9551 (make_number (pos), Qdisplay, Qnil, limit));
9552 if (pos > pt_old)
9553 break;
ae73dc1c 9554 /* Skip glyphs from the same string. */
bc6371a6
KH
9555 string = glyph->object;
9556 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9557 /* Skip glyphs from an overlay. */
9558 while (glyph < end
9559 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 9560 {
bc6371a6
KH
9561 string = glyph->object;
9562 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 9563 }
ae73dc1c 9564 }
5f5c8ee5
GM
9565 }
9566
9567 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9568 w->cursor.x = x;
9569 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9570 w->cursor.y = row->y + dy;
9571
9572 if (w == XWINDOW (selected_window))
9573 {
9574 if (!row->continued_p
9575 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9576 && row->x == 0)
9577 {
9578 this_line_buffer = XBUFFER (w->buffer);
2311178e 9579
5f5c8ee5
GM
9580 CHARPOS (this_line_start_pos)
9581 = MATRIX_ROW_START_CHARPOS (row) + delta;
9582 BYTEPOS (this_line_start_pos)
9583 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 9584
5f5c8ee5
GM
9585 CHARPOS (this_line_end_pos)
9586 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9587 BYTEPOS (this_line_end_pos)
9588 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 9589
5f5c8ee5
GM
9590 this_line_y = w->cursor.y;
9591 this_line_pixel_height = row->height;
9592 this_line_vpos = w->cursor.vpos;
9593 this_line_start_x = row->x;
9594 }
9595 else
9596 CHARPOS (this_line_start_pos) = 0;
9597 }
9598}
9599
9600
9601/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
9602 start STARTP. Sets the window start of WINDOW to that position.
9603
9604 We assume that the window's buffer is really current. */
5f5c8ee5
GM
9605
9606static INLINE struct text_pos
9607run_window_scroll_functions (window, startp)
9608 Lisp_Object window;
9609 struct text_pos startp;
9610{
9611 struct window *w = XWINDOW (window);
9612 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
9613
9614 if (current_buffer != XBUFFER (w->buffer))
9615 abort ();
9616
5f5c8ee5
GM
9617 if (!NILP (Vwindow_scroll_functions))
9618 {
2311178e 9619 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
9620 make_number (CHARPOS (startp)));
9621 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
9622 /* In case the hook functions switch buffers. */
9623 if (current_buffer != XBUFFER (w->buffer))
9624 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 9625 }
90adcf20 9626
5f5c8ee5
GM
9627 return startp;
9628}
9629
9630
ddf6b9a3
RS
9631/* Make sure the line containing the cursor is fully visible.
9632 A value of 1 means there is nothing to be done.
9633 (Either the line is fully visible, or it cannot be made so,
9634 or we cannot tell.)
9635 A value of 0 means the caller should do scrolling
9636 as if point had gone off the screen. */
5f5c8ee5 9637
cb617e7c 9638static int
5f5c8ee5
GM
9639make_cursor_line_fully_visible (w)
9640 struct window *w;
9641{
9642 struct glyph_matrix *matrix;
9643 struct glyph_row *row;
478d746b 9644 int window_height;
2311178e 9645
5f5c8ee5
GM
9646 /* It's not always possible to find the cursor, e.g, when a window
9647 is full of overlay strings. Don't do anything in that case. */
9648 if (w->cursor.vpos < 0)
cb617e7c 9649 return 1;
2311178e 9650
5f5c8ee5
GM
9651 matrix = w->desired_matrix;
9652 row = MATRIX_ROW (matrix, w->cursor.vpos);
9653
acda20e1 9654 /* If the cursor row is not partially visible, there's nothing to do. */
b28cb6ed 9655 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 9656 return 1;
b28cb6ed
GM
9657
9658 /* If the row the cursor is in is taller than the window's height,
9659 it's not clear what to do, so do nothing. */
9660 window_height = window_box_height (w);
9661 if (row->height >= window_height)
cb617e7c 9662 return 1;
b28cb6ed 9663
ddf6b9a3
RS
9664 return 0;
9665
9666#if 0
9667 /* This code used to try to scroll the window just enough to make
9668 the line visible. It returned 0 to say that the caller should
9669 allocate larger glyph matrices. */
9670
b28cb6ed 9671 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
9672 {
9673 int dy = row->height - row->visible_height;
9674 w->vscroll = 0;
9675 w->cursor.y += dy;
9676 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9677 }
b28cb6ed 9678 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
9679 {
9680 int dy = - (row->height - row->visible_height);
9681 w->vscroll = dy;
9682 w->cursor.y += dy;
9683 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9684 }
2311178e 9685
5f5c8ee5
GM
9686 /* When we change the cursor y-position of the selected window,
9687 change this_line_y as well so that the display optimization for
9688 the cursor line of the selected window in redisplay_internal uses
9689 the correct y-position. */
9690 if (w == XWINDOW (selected_window))
9691 this_line_y = w->cursor.y;
cb617e7c
GM
9692
9693 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9694 redisplay with larger matrices. */
9695 if (matrix->nrows < required_matrix_height (w))
9696 {
9697 fonts_changed_p = 1;
9698 return 0;
9699 }
9700
9701 return 1;
ddf6b9a3 9702#endif /* 0 */
5f5c8ee5
GM
9703}
9704
9705
9706/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9707 non-zero means only WINDOW is redisplayed in redisplay_internal.
9708 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9709 in redisplay_window to bring a partially visible line into view in
9710 the case that only the cursor has moved.
9711
03b0a4b4
RS
9712 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
9713 last screen line's vertical height extends past the end of the screen.
9714
5f5c8ee5
GM
9715 Value is
9716
9717 1 if scrolling succeeded
2311178e 9718
5f5c8ee5 9719 0 if scrolling didn't find point.
2311178e 9720
5f5c8ee5
GM
9721 -1 if new fonts have been loaded so that we must interrupt
9722 redisplay, adjust glyph matrices, and try again. */
9723
cb617e7c
GM
9724enum
9725{
9726 SCROLLING_SUCCESS,
9727 SCROLLING_FAILED,
9728 SCROLLING_NEED_LARGER_MATRICES
9729};
9730
5f5c8ee5
GM
9731static int
9732try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 9733 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
9734 Lisp_Object window;
9735 int just_this_one_p;
31ade731 9736 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 9737 int temp_scroll_step;
03b0a4b4 9738 int last_line_misfit;
5f5c8ee5
GM
9739{
9740 struct window *w = XWINDOW (window);
9741 struct frame *f = XFRAME (w->frame);
9742 struct text_pos scroll_margin_pos;
9743 struct text_pos pos;
9744 struct text_pos startp;
9745 struct it it;
9746 Lisp_Object window_end;
9747 int this_scroll_margin;
9748 int dy = 0;
9749 int scroll_max;
b8a63ccb 9750 int rc;
5f5c8ee5
GM
9751 int amount_to_scroll = 0;
9752 Lisp_Object aggressive;
9753 int height;
03b0a4b4 9754 int end_scroll_margin;
5f5c8ee5
GM
9755
9756#if GLYPH_DEBUG
9757 debug_method_add (w, "try_scrolling");
78614721 9758#endif
5f5c8ee5
GM
9759
9760 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 9761
5f5c8ee5
GM
9762 /* Compute scroll margin height in pixels. We scroll when point is
9763 within this distance from the top or bottom of the window. */
9764 if (scroll_margin > 0)
90adcf20 9765 {
5f5c8ee5
GM
9766 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9767 this_scroll_margin *= CANON_Y_UNIT (f);
9768 }
9769 else
9770 this_scroll_margin = 0;
9771
9772 /* Compute how much we should try to scroll maximally to bring point
9773 into view. */
0894e696
SM
9774 if (scroll_step || scroll_conservatively || temp_scroll_step)
9775 scroll_max = max (scroll_step,
9776 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
9777 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9778 || NUMBERP (current_buffer->scroll_up_aggressively))
9779 /* We're trying to scroll because of aggressive scrolling
9780 but no scroll_step is set. Choose an arbitrary one. Maybe
9781 there should be a variable for this. */
9782 scroll_max = 10;
9783 else
9784 scroll_max = 0;
9785 scroll_max *= CANON_Y_UNIT (f);
9786
9787 /* Decide whether we have to scroll down. Start at the window end
9788 and move this_scroll_margin up to find the position of the scroll
9789 margin. */
9790 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
9791
9792 too_near_end:
9793
5f5c8ee5
GM
9794 CHARPOS (scroll_margin_pos) = XINT (window_end);
9795 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4
RS
9796
9797 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
9798 if (end_scroll_margin)
5f5c8ee5
GM
9799 {
9800 start_display (&it, w, scroll_margin_pos);
03b0a4b4 9801 move_it_vertically (&it, - end_scroll_margin);
5f5c8ee5
GM
9802 scroll_margin_pos = it.current.pos;
9803 }
9804
9805 if (PT >= CHARPOS (scroll_margin_pos))
9806 {
9807 int y0;
2311178e 9808
5f5c8ee5
GM
9809 /* Point is in the scroll margin at the bottom of the window, or
9810 below. Compute a new window start that makes point visible. */
47589c8c 9811
5f5c8ee5
GM
9812 /* Compute the distance from the scroll margin to PT.
9813 Give up if the distance is greater than scroll_max. */
9814 start_display (&it, w, scroll_margin_pos);
9815 y0 = it.current_y;
9816 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9817 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
9818
9819 /* To make point visible, we have to move the window start
9820 down so that the line the cursor is in is visible, which
9821 means we have to add in the height of the cursor line. */
9822 dy = line_bottom_y (&it) - y0;
2311178e 9823
5f5c8ee5 9824 if (dy > scroll_max)
cb617e7c 9825 return SCROLLING_FAILED;
2311178e 9826
5f5c8ee5
GM
9827 /* Move the window start down. If scrolling conservatively,
9828 move it just enough down to make point visible. If
9829 scroll_step is set, move it down by scroll_step. */
9830 start_display (&it, w, startp);
9831
9832 if (scroll_conservatively)
03b0a4b4
RS
9833 /* Set AMOUNT_TO_SCROLL to at least one line,
9834 and at most scroll_conservatively lines. */
e89aaabd 9835 amount_to_scroll
03b0a4b4
RS
9836 = min (max (dy, CANON_Y_UNIT (f)),
9837 CANON_Y_UNIT (f) * scroll_conservatively);
5f5c8ee5
GM
9838 else if (scroll_step || temp_scroll_step)
9839 amount_to_scroll = scroll_max;
9840 else
90adcf20 9841 {
46226c1d 9842 aggressive = current_buffer->scroll_up_aggressively;
5f5c8ee5 9843 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9844 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9845 if (NUMBERP (aggressive))
9846 amount_to_scroll = XFLOATINT (aggressive) * height;
9847 }
a2725ab2 9848
5f5c8ee5 9849 if (amount_to_scroll <= 0)
cb617e7c 9850 return SCROLLING_FAILED;
a2725ab2 9851
03b0a4b4
RS
9852 /* If moving by amount_to_scroll leaves STARTP unchanged,
9853 move it down one screen line. */
9854
5f5c8ee5 9855 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
9856 if (CHARPOS (it.current.pos) == CHARPOS (startp))
9857 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
9858 startp = it.current.pos;
9859 }
9860 else
9861 {
9862 /* See if point is inside the scroll margin at the top of the
9863 window. */
9864 scroll_margin_pos = startp;
9865 if (this_scroll_margin)
9866 {
9867 start_display (&it, w, startp);
9868 move_it_vertically (&it, this_scroll_margin);
9869 scroll_margin_pos = it.current.pos;
9870 }
9871
9872 if (PT < CHARPOS (scroll_margin_pos))
9873 {
9874 /* Point is in the scroll margin at the top of the window or
9875 above what is displayed in the window. */
9876 int y0;
2311178e 9877
5f5c8ee5
GM
9878 /* Compute the vertical distance from PT to the scroll
9879 margin position. Give up if distance is greater than
9880 scroll_max. */
9881 SET_TEXT_POS (pos, PT, PT_BYTE);
9882 start_display (&it, w, pos);
9883 y0 = it.current_y;
9884 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9885 it.last_visible_y, -1,
9886 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9887 dy = it.current_y - y0;
9888 if (dy > scroll_max)
cb617e7c 9889 return SCROLLING_FAILED;
2311178e 9890
5f5c8ee5
GM
9891 /* Compute new window start. */
9892 start_display (&it, w, startp);
2311178e 9893
5f5c8ee5 9894 if (scroll_conservatively)
0894e696
SM
9895 amount_to_scroll =
9896 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9897 else if (scroll_step || temp_scroll_step)
9898 amount_to_scroll = scroll_max;
538f13d4 9899 else
5f5c8ee5 9900 {
46226c1d 9901 aggressive = current_buffer->scroll_down_aggressively;
5f5c8ee5 9902 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9903 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9904 if (NUMBERP (aggressive))
9905 amount_to_scroll = XFLOATINT (aggressive) * height;
9906 }
a2725ab2 9907
5f5c8ee5 9908 if (amount_to_scroll <= 0)
cb617e7c 9909 return SCROLLING_FAILED;
2311178e 9910
5f5c8ee5
GM
9911 move_it_vertically (&it, - amount_to_scroll);
9912 startp = it.current.pos;
90adcf20
RS
9913 }
9914 }
a2889657 9915
5f5c8ee5
GM
9916 /* Run window scroll functions. */
9917 startp = run_window_scroll_functions (window, startp);
90adcf20 9918
5f5c8ee5
GM
9919 /* Display the window. Give up if new fonts are loaded, or if point
9920 doesn't appear. */
9921 if (!try_window (window, startp))
cb617e7c 9922 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
9923 else if (w->cursor.vpos < 0)
9924 {
9925 clear_glyph_matrix (w->desired_matrix);
cb617e7c 9926 rc = SCROLLING_FAILED;
5f5c8ee5
GM
9927 }
9928 else
9929 {
9930 /* Maybe forget recorded base line for line number display. */
2311178e 9931 if (!just_this_one_p
5f5c8ee5 9932 || current_buffer->clip_changed
9142dd5b 9933 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 9934 w->base_line_number = Qnil;
2311178e 9935
ddf6b9a3
RS
9936 /* If cursor ends up on a partially visible line,
9937 treat that as being off the bottom of the screen. */
9938 if (! make_cursor_line_fully_visible (w))
ef3c2c73
RS
9939 {
9940 clear_glyph_matrix (w->desired_matrix);
03b0a4b4 9941 last_line_misfit = 1;
ef3c2c73
RS
9942 goto too_near_end;
9943 }
ddf6b9a3 9944 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
9945 }
9946
9947 return rc;
a2889657
JB
9948}
9949
5f5c8ee5
GM
9950
9951/* Compute a suitable window start for window W if display of W starts
9952 on a continuation line. Value is non-zero if a new window start
9953 was computed.
9954
9955 The new window start will be computed, based on W's width, starting
9956 from the start of the continued line. It is the start of the
9957 screen line with the minimum distance from the old start W->start. */
9958
9959static int
9960compute_window_start_on_continuation_line (w)
9961 struct window *w;
1f1ff51d 9962{
5f5c8ee5
GM
9963 struct text_pos pos, start_pos;
9964 int window_start_changed_p = 0;
1f1ff51d 9965
5f5c8ee5 9966 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 9967
5f5c8ee5 9968 /* If window start is on a continuation line... Window start may be
2311178e 9969 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
9970 buffer (M-x rmail, for example). */
9971 if (CHARPOS (start_pos) > BEGV
9972 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 9973 {
5f5c8ee5
GM
9974 struct it it;
9975 struct glyph_row *row;
f3751a65
GM
9976
9977 /* Handle the case that the window start is out of range. */
9978 if (CHARPOS (start_pos) < BEGV)
9979 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9980 else if (CHARPOS (start_pos) > ZV)
9981 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 9982
5f5c8ee5
GM
9983 /* Find the start of the continued line. This should be fast
9984 because scan_buffer is fast (newline cache). */
045dee35 9985 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
9986 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9987 row, DEFAULT_FACE_ID);
9988 reseat_at_previous_visible_line_start (&it);
9989
9990 /* If the line start is "too far" away from the window start,
9991 say it takes too much time to compute a new window start. */
9992 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9993 < XFASTINT (w->height) * XFASTINT (w->width))
9994 {
9995 int min_distance, distance;
2311178e 9996
5f5c8ee5
GM
9997 /* Move forward by display lines to find the new window
9998 start. If window width was enlarged, the new start can
9999 be expected to be > the old start. If window width was
10000 decreased, the new window start will be < the old start.
10001 So, we're looking for the display line start with the
10002 minimum distance from the old window start. */
10003 pos = it.current.pos;
10004 min_distance = INFINITY;
10005 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10006 distance < min_distance)
10007 {
10008 min_distance = distance;
10009 pos = it.current.pos;
10010 move_it_by_lines (&it, 1, 0);
10011 }
2311178e 10012
5f5c8ee5
GM
10013 /* Set the window start there. */
10014 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10015 window_start_changed_p = 1;
10016 }
1f1ff51d 10017 }
2311178e 10018
5f5c8ee5 10019 return window_start_changed_p;
1f1ff51d
KH
10020}
10021
5f5c8ee5 10022
1dd5768b 10023/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
10024 with window start STARTP. Value is
10025
cb617e7c 10026 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 10027
cb617e7c
GM
10028 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
10029
10030 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
10031 display. *SCROLL_STEP is set to 1, under certain circumstances, if
10032 we want to scroll as if scroll-step were set to 1. See the code.
10033
10034 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
10035 which case we have to abort this redisplay, and adjust matrices
10036 first. */
10037
2311178e 10038enum
cb617e7c
GM
10039{
10040 CURSOR_MOVEMENT_SUCCESS,
10041 CURSOR_MOVEMENT_CANNOT_BE_USED,
10042 CURSOR_MOVEMENT_MUST_SCROLL,
10043 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
10044};
47589c8c
GM
10045
10046static int
10047try_cursor_movement (window, startp, scroll_step)
10048 Lisp_Object window;
10049 struct text_pos startp;
10050 int *scroll_step;
10051{
10052 struct window *w = XWINDOW (window);
10053 struct frame *f = XFRAME (w->frame);
cb617e7c 10054 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 10055
69d1f7c9 10056#if GLYPH_DEBUG
76cb5e06
GM
10057 if (inhibit_try_cursor_movement)
10058 return rc;
10059#endif
10060
47589c8c
GM
10061 /* Handle case where text has not changed, only point, and it has
10062 not moved off the frame. */
10063 if (/* Point may be in this window. */
10064 PT >= CHARPOS (startp)
47589c8c
GM
10065 /* Selective display hasn't changed. */
10066 && !current_buffer->clip_changed
4db87380
GM
10067 /* Function force-mode-line-update is used to force a thorough
10068 redisplay. It sets either windows_or_buffers_changed or
10069 update_mode_lines. So don't take a shortcut here for these
10070 cases. */
10071 && !update_mode_lines
10072 && !windows_or_buffers_changed
5fb96e96 10073 && !cursor_type_changed
2311178e 10074 /* Can't use this case if highlighting a region. When a
47589c8c
GM
10075 region exists, cursor movement has to do more than just
10076 set the cursor. */
10077 && !(!NILP (Vtransient_mark_mode)
10078 && !NILP (current_buffer->mark_active))
10079 && NILP (w->region_showing)
10080 && NILP (Vshow_trailing_whitespace)
10081 /* Right after splitting windows, last_point may be nil. */
10082 && INTEGERP (w->last_point)
10083 /* This code is not used for mini-buffer for the sake of the case
10084 of redisplaying to replace an echo area message; since in
10085 that case the mini-buffer contents per se are usually
10086 unchanged. This code is of no real use in the mini-buffer
10087 since the handling of this_line_start_pos, etc., in redisplay
10088 handles the same cases. */
10089 && !EQ (window, minibuf_window)
10090 /* When splitting windows or for new windows, it happens that
10091 redisplay is called with a nil window_end_vpos or one being
10092 larger than the window. This should really be fixed in
10093 window.c. I don't have this on my list, now, so we do
10094 approximately the same as the old redisplay code. --gerd. */
10095 && INTEGERP (w->window_end_vpos)
10096 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
10097 && (FRAME_WINDOW_P (f)
10098 || !MARKERP (Voverlay_arrow_position)
10099 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
10100 {
10101 int this_scroll_margin;
8ee5b6a3 10102 struct glyph_row *row = NULL;
47589c8c
GM
10103
10104#if GLYPH_DEBUG
10105 debug_method_add (w, "cursor movement");
10106#endif
10107
10108 /* Scroll if point within this distance from the top or bottom
10109 of the window. This is a pixel value. */
10110 this_scroll_margin = max (0, scroll_margin);
10111 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
10112 this_scroll_margin *= CANON_Y_UNIT (f);
10113
10114 /* Start with the row the cursor was displayed during the last
10115 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
10116 if (w->last_cursor.vpos < 0
10117 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 10118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10119 else
10120 {
10121 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10122 if (row->mode_line_p)
10123 ++row;
10124 if (!row->enabled_p)
cb617e7c 10125 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10126 }
10127
cb617e7c 10128 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
10129 {
10130 int scroll_p = 0;
68c5d1db 10131 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 10132
47589c8c
GM
10133 if (PT > XFASTINT (w->last_point))
10134 {
10135 /* Point has moved forward. */
47589c8c
GM
10136 while (MATRIX_ROW_END_CHARPOS (row) < PT
10137 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10138 {
10139 xassert (row->enabled_p);
10140 ++row;
10141 }
10142
10143 /* The end position of a row equals the start position
10144 of the next row. If PT is there, we would rather
cafafe0b
GM
10145 display it in the next line. */
10146 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10147 && MATRIX_ROW_END_CHARPOS (row) == PT
10148 && !cursor_row_p (w, row))
10149 ++row;
47589c8c
GM
10150
10151 /* If within the scroll margin, scroll. Note that
10152 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10153 the next line would be drawn, and that
10154 this_scroll_margin can be zero. */
10155 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10156 || PT > MATRIX_ROW_END_CHARPOS (row)
10157 /* Line is completely visible last line in window
10158 and PT is to be set in the next line. */
10159 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10160 && PT == MATRIX_ROW_END_CHARPOS (row)
10161 && !row->ends_at_zv_p
10162 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10163 scroll_p = 1;
10164 }
10165 else if (PT < XFASTINT (w->last_point))
10166 {
10167 /* Cursor has to be moved backward. Note that PT >=
10168 CHARPOS (startp) because of the outer
10169 if-statement. */
10170 while (!row->mode_line_p
10171 && (MATRIX_ROW_START_CHARPOS (row) > PT
10172 || (MATRIX_ROW_START_CHARPOS (row) == PT
10173 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10174 && (row->y > this_scroll_margin
10175 || CHARPOS (startp) == BEGV))
10176 {
10177 xassert (row->enabled_p);
10178 --row;
10179 }
10180
10181 /* Consider the following case: Window starts at BEGV,
10182 there is invisible, intangible text at BEGV, so that
10183 display starts at some point START > BEGV. It can
10184 happen that we are called with PT somewhere between
10185 BEGV and START. Try to handle that case. */
10186 if (row < w->current_matrix->rows
10187 || row->mode_line_p)
10188 {
10189 row = w->current_matrix->rows;
10190 if (row->mode_line_p)
10191 ++row;
10192 }
10193
10194 /* Due to newlines in overlay strings, we may have to
10195 skip forward over overlay strings. */
68c5d1db
GM
10196 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10197 && MATRIX_ROW_END_CHARPOS (row) == PT
10198 && !cursor_row_p (w, row))
47589c8c 10199 ++row;
2311178e 10200
47589c8c
GM
10201 /* If within the scroll margin, scroll. */
10202 if (row->y < this_scroll_margin
10203 && CHARPOS (startp) != BEGV)
10204 scroll_p = 1;
10205 }
10206
10207 if (PT < MATRIX_ROW_START_CHARPOS (row)
10208 || PT > MATRIX_ROW_END_CHARPOS (row))
10209 {
10210 /* if PT is not in the glyph row, give up. */
cb617e7c 10211 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 10212 }
440fc135 10213 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 10214 {
8de4aaf8
GM
10215 if (PT == MATRIX_ROW_END_CHARPOS (row)
10216 && !row->ends_at_zv_p
10217 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 10218 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 10219 else if (row->height > window_box_height (w))
440fc135 10220 {
8de4aaf8
GM
10221 /* If we end up in a partially visible line, let's
10222 make it fully visible, except when it's taller
10223 than the window, in which case we can't do much
10224 about it. */
440fc135 10225 *scroll_step = 1;
cb617e7c 10226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
10227 }
10228 else
10229 {
10230 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 10231 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 10232 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
10233 else
10234 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 10235 }
47589c8c
GM
10236 }
10237 else if (scroll_p)
cb617e7c 10238 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10239 else
10240 {
10241 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 10242 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
10243 }
10244 }
10245 }
10246
10247 return rc;
10248}
10249
10250
5f5c8ee5 10251/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
10252 selected_window is redisplayed.
10253
10254 We can return without actually redisplaying the window if
10255 fonts_changed_p is nonzero. In that case, redisplay_internal will
10256 retry. */
90adcf20 10257
a2889657 10258static void
5f5c8ee5 10259redisplay_window (window, just_this_one_p)
a2889657 10260 Lisp_Object window;
5f5c8ee5 10261 int just_this_one_p;
a2889657 10262{
5f5c8ee5
GM
10263 struct window *w = XWINDOW (window);
10264 struct frame *f = XFRAME (w->frame);
10265 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 10266 struct buffer *old = current_buffer;
5f5c8ee5 10267 struct text_pos lpoint, opoint, startp;
e481f960 10268 int update_mode_line;
5f5c8ee5
GM
10269 int tem;
10270 struct it it;
10271 /* Record it now because it's overwritten. */
10272 int current_matrix_up_to_date_p = 0;
c62c1bb5
RS
10273 /* This is less strict than current_matrix_up_to_date_p.
10274 It indictes that the buffer contents and narrowing are unchanged. */
10275 int buffer_unchanged_p = 0;
5f5c8ee5 10276 int temp_scroll_step = 0;
331379bf 10277 int count = SPECPDL_INDEX ();
47589c8c 10278 int rc;
ddf6b9a3 10279 int centering_position;
03b0a4b4 10280 int last_line_misfit = 0;
a2889657 10281
5f5c8ee5
GM
10282 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10283 opoint = lpoint;
a2889657 10284
5f5c8ee5
GM
10285 /* W must be a leaf window here. */
10286 xassert (!NILP (w->buffer));
10287#if GLYPH_DEBUG
10288 *w->desired_matrix->method = 0;
10289#endif
2e54982e
RS
10290
10291 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
10292
10293 reconsider_clip_changes (w, buffer);
2311178e
TTN
10294
10295 /* Has the mode line to be updated? */
5f5c8ee5
GM
10296 update_mode_line = (!NILP (w->update_mode_line)
10297 || update_mode_lines
c62c1bb5
RS
10298 || buffer->clip_changed
10299 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
10300
10301 if (MINI_WINDOW_P (w))
10302 {
5f5c8ee5 10303 if (w == XWINDOW (echo_area_window)
c6e89d6c 10304 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
10305 {
10306 if (update_mode_line)
10307 /* We may have to update a tty frame's menu bar or a
e037b9ec 10308 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
10309 goto finish_menu_bars;
10310 else
10311 /* We've already displayed the echo area glyphs in this window. */
10312 goto finish_scroll_bars;
10313 }
b2b5455d
RS
10314 else if ((w != XWINDOW (minibuf_window)
10315 || minibuf_level == 0)
3354fdcf
RS
10316 /* Quail displays non-mini buffers in minibuffer window.
10317 In that case, redisplay the window normally. */
b2b5455d 10318 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 10319 {
3354fdcf
RS
10320 /* W is a mini-buffer window, but it's not active, so clear
10321 it. */
5f5c8ee5
GM
10322 int yb = window_text_bottom_y (w);
10323 struct glyph_row *row;
10324 int y;
10325
10326 for (y = 0, row = w->desired_matrix->rows;
10327 y < yb;
10328 y += row->height, ++row)
10329 blank_row (w, row, y);
88f22aff 10330 goto finish_scroll_bars;
8de2d90b 10331 }
c095a1dd
GM
10332
10333 clear_glyph_matrix (w->desired_matrix);
8de2d90b 10334 }
a2889657 10335
5f5c8ee5
GM
10336 /* Otherwise set up data on this window; select its buffer and point
10337 value. */
6a93695f
GM
10338 /* Really select the buffer, for the sake of buffer-local
10339 variables. */
10340 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
10341 SET_TEXT_POS (opoint, PT, PT_BYTE);
10342
10343 current_matrix_up_to_date_p
10344 = (!NILP (w->window_end_valid)
10345 && !current_buffer->clip_changed
c62c1bb5 10346 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
10347 && XFASTINT (w->last_modified) >= MODIFF
10348 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 10349
c62c1bb5
RS
10350 buffer_unchanged_p
10351 = (!NILP (w->window_end_valid)
10352 && !current_buffer->clip_changed
3f1258d0 10353 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
10354 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10355
5f5c8ee5
GM
10356 /* When windows_or_buffers_changed is non-zero, we can't rely on
10357 the window end being valid, so set it to nil there. */
10358 if (windows_or_buffers_changed)
10359 {
10360 /* If window starts on a continuation line, maybe adjust the
10361 window start in case the window's width changed. */
10362 if (XMARKER (w->start)->buffer == current_buffer)
10363 compute_window_start_on_continuation_line (w);
2311178e 10364
5f5c8ee5
GM
10365 w->window_end_valid = Qnil;
10366 }
12adba34 10367
5f5c8ee5
GM
10368 /* Some sanity checks. */
10369 CHECK_WINDOW_END (w);
10370 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 10371 abort ();
5f5c8ee5 10372 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 10373 abort ();
a2889657 10374
28995e67
RS
10375 /* If %c is in mode line, update it if needed. */
10376 if (!NILP (w->column_number_displayed)
10377 /* This alternative quickly identifies a common case
10378 where no change is needed. */
10379 && !(PT == XFASTINT (w->last_point)
8850a573
RS
10380 && XFASTINT (w->last_modified) >= MODIFF
10381 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
10382 && (XFASTINT (w->column_number_displayed)
10383 != (int) current_column ())) /* iftc */
10384 update_mode_line = 1;
28995e67 10385
5f5c8ee5
GM
10386 /* Count number of windows showing the selected buffer. An indirect
10387 buffer counts as its base buffer. */
10388 if (!just_this_one_p)
42640f83
RS
10389 {
10390 struct buffer *current_base, *window_base;
10391 current_base = current_buffer;
10392 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10393 if (current_base->base_buffer)
10394 current_base = current_base->base_buffer;
10395 if (window_base->base_buffer)
10396 window_base = window_base->base_buffer;
10397 if (current_base == window_base)
10398 buffer_shared++;
10399 }
a2889657 10400
5f5c8ee5
GM
10401 /* Point refers normally to the selected window. For any other
10402 window, set up appropriate value. */
a2889657
JB
10403 if (!EQ (window, selected_window))
10404 {
12adba34
RS
10405 int new_pt = XMARKER (w->pointm)->charpos;
10406 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 10407 if (new_pt < BEGV)
a2889657 10408 {
f67a0f51 10409 new_pt = BEGV;
12adba34
RS
10410 new_pt_byte = BEGV_BYTE;
10411 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 10412 }
f67a0f51 10413 else if (new_pt > (ZV - 1))
a2889657 10414 {
f67a0f51 10415 new_pt = ZV;
12adba34
RS
10416 new_pt_byte = ZV_BYTE;
10417 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 10418 }
2311178e 10419
f67a0f51 10420 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 10421 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
10422 }
10423
f4faa47c 10424 /* If any of the character widths specified in the display table
5f5c8ee5
GM
10425 have changed, invalidate the width run cache. It's true that
10426 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
10427 redisplay goes (non-fatally) haywire when the display table is
10428 changed, so why should we worry about doing any better? */
10429 if (current_buffer->width_run_cache)
10430 {
f908610f 10431 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
10432
10433 if (! disptab_matches_widthtab (disptab,
10434 XVECTOR (current_buffer->width_table)))
10435 {
10436 invalidate_region_cache (current_buffer,
10437 current_buffer->width_run_cache,
10438 BEG, Z);
10439 recompute_width_table (current_buffer, disptab);
10440 }
10441 }
10442
a2889657 10443 /* If window-start is screwed up, choose a new one. */
a2889657
JB
10444 if (XMARKER (w->start)->buffer != current_buffer)
10445 goto recenter;
10446
5f5c8ee5 10447 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 10448
cf0df6ab
RS
10449 /* If someone specified a new starting point but did not insist,
10450 check whether it can be used. */
cfad01b4
GM
10451 if (!NILP (w->optional_new_start)
10452 && CHARPOS (startp) >= BEGV
10453 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
10454 {
10455 w->optional_new_start = Qnil;
5f5c8ee5
GM
10456 start_display (&it, w, startp);
10457 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10458 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10459 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
10460 w->force_start = Qt;
10461 }
10462
8de2d90b 10463 /* Handle case where place to start displaying has been specified,
aa6d10fa 10464 unless the specified location is outside the accessible range. */
9472f927
GM
10465 if (!NILP (w->force_start)
10466 || w->frozen_window_start_p)
a2889657 10467 {
b60c9653
RS
10468 /* We set this later on if we have to adjust point. */
10469 int new_vpos = -1;
10470
e63574d7 10471 w->force_start = Qnil;
5f5c8ee5 10472 w->vscroll = 0;
b5174a51 10473 w->window_end_valid = Qnil;
5f5c8ee5
GM
10474
10475 /* Forget any recorded base line for line number display. */
c62c1bb5 10476 if (!buffer_unchanged_p)
5f5c8ee5
GM
10477 w->base_line_number = Qnil;
10478
75c43375
RS
10479 /* Redisplay the mode line. Select the buffer properly for that.
10480 Also, run the hook window-scroll-functions
10481 because we have scrolled. */
e63574d7
RS
10482 /* Note, we do this after clearing force_start because
10483 if there's an error, it is better to forget about force_start
10484 than to get into an infinite loop calling the hook functions
10485 and having them get more errors. */
75c43375
RS
10486 if (!update_mode_line
10487 || ! NILP (Vwindow_scroll_functions))
e481f960 10488 {
e481f960
RS
10489 update_mode_line = 1;
10490 w->update_mode_line = Qt;
5f5c8ee5 10491 startp = run_window_scroll_functions (window, startp);
e481f960 10492 }
2311178e 10493
ac90c44f
GM
10494 w->last_modified = make_number (0);
10495 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
10496 if (CHARPOS (startp) < BEGV)
10497 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10498 else if (CHARPOS (startp) > ZV)
10499 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
10500
10501 /* Redisplay, then check if cursor has been set during the
5f5c8ee5
GM
10502 redisplay. Give up if new fonts were loaded. */
10503 if (!try_window (window, startp))
10504 {
10505 w->force_start = Qt;
10506 clear_glyph_matrix (w->desired_matrix);
b60c9653 10507 goto need_larger_matrices;
5f5c8ee5
GM
10508 }
10509
9472f927 10510 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 10511 {
b28cb6ed
GM
10512 /* If point does not appear, try to move point so it does
10513 appear. The desired matrix has been built above, so we
10514 can use it here. */
b60c9653
RS
10515 new_vpos = window_box_height (w) / 2;
10516 }
10517
10518 if (!make_cursor_line_fully_visible (w))
10519 {
10520 /* Point does appear, but on a line partly visible at end of window.
10521 Move it back to a fully-visible line. */
10522 new_vpos = window_box_height (w);
10523 }
10524
10525 /* If we need to move point for either of the above reasons,
10526 now actually do it. */
10527 if (new_vpos >= 0)
10528 {
b28cb6ed
GM
10529 struct glyph_row *row;
10530
b28cb6ed 10531 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 10532 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
10533 ++row;
10534
10535 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10536 MATRIX_ROW_START_BYTEPOS (row));
10537
90adcf20 10538 if (w != XWINDOW (selected_window))
12adba34 10539 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
10540 else if (current_buffer == old)
10541 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10542
10543 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 10544
5f5c8ee5
GM
10545 /* If we are highlighting the region, then we just changed
10546 the region, so redisplay to show it. */
df0b5ea1
RS
10547 if (!NILP (Vtransient_mark_mode)
10548 && !NILP (current_buffer->mark_active))
6f27fa9b 10549 {
5f5c8ee5
GM
10550 clear_glyph_matrix (w->desired_matrix);
10551 if (!try_window (window, startp))
cb617e7c 10552 goto need_larger_matrices;
6f27fa9b 10553 }
a2889657 10554 }
5f5c8ee5 10555
5f5c8ee5
GM
10556#if GLYPH_DEBUG
10557 debug_method_add (w, "forced window start");
10558#endif
a2889657
JB
10559 goto done;
10560 }
10561
5f5c8ee5 10562 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
10563 not moved off the frame, and we are not retrying after hscroll.
10564 (current_matrix_up_to_date_p is nonzero when retrying.) */
10565 if (current_matrix_up_to_date_p
47589c8c 10566 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 10567 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 10568 {
cb617e7c
GM
10569 switch (rc)
10570 {
10571 case CURSOR_MOVEMENT_SUCCESS:
10572 goto done;
2311178e 10573
b60c9653 10574#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
10575 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10576 goto need_larger_matrices;
b60c9653 10577#endif
2311178e 10578
cb617e7c
GM
10579 case CURSOR_MOVEMENT_MUST_SCROLL:
10580 goto try_to_scroll;
2311178e 10581
cb617e7c
GM
10582 default:
10583 abort ();
10584 }
a2889657
JB
10585 }
10586 /* If current starting point was originally the beginning of a line
10587 but no longer is, find a new starting point. */
265a9e55 10588 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
10589 && !(CHARPOS (startp) <= BEGV
10590 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 10591 {
5f5c8ee5
GM
10592#if GLYPH_DEBUG
10593 debug_method_add (w, "recenter 1");
10594#endif
a2889657
JB
10595 goto recenter;
10596 }
2311178e 10597
27d16f05
GM
10598 /* Try scrolling with try_window_id. Value is > 0 if update has
10599 been done, it is -1 if we know that the same window start will
10600 not work. It is 0 if unsuccessful for some other reason. */
10601 else if ((tem = try_window_id (w)) != 0)
a2889657 10602 {
5f5c8ee5 10603#if GLYPH_DEBUG
ef121659 10604 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
10605#endif
10606
10607 if (fonts_changed_p)
cb617e7c 10608 goto need_larger_matrices;
a2889657
JB
10609 if (tem > 0)
10610 goto done;
ef121659 10611
5f5c8ee5
GM
10612 /* Otherwise try_window_id has returned -1 which means that we
10613 don't want the alternative below this comment to execute. */
a2889657 10614 }
5f5c8ee5
GM
10615 else if (CHARPOS (startp) >= BEGV
10616 && CHARPOS (startp) <= ZV
10617 && PT >= CHARPOS (startp)
10618 && (CHARPOS (startp) < ZV
e9874cee 10619 /* Avoid starting at end of buffer. */
5f5c8ee5 10620 || CHARPOS (startp) == BEGV
8850a573
RS
10621 || (XFASTINT (w->last_modified) >= MODIFF
10622 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 10623 {
5f5c8ee5
GM
10624#if GLYPH_DEBUG
10625 debug_method_add (w, "same window start");
10626#endif
2311178e 10627
5f5c8ee5
GM
10628 /* Try to redisplay starting at same place as before.
10629 If point has not moved off frame, accept the results. */
10630 if (!current_matrix_up_to_date_p
10631 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
10632 because a window scroll function can have changed the
10633 buffer. */
5f5c8ee5
GM
10634 || !NILP (Vwindow_scroll_functions)
10635 || MINI_WINDOW_P (w)
10636 || !try_window_reusing_current_matrix (w))
10637 {
10638 IF_DEBUG (debug_method_add (w, "1"));
10639 try_window (window, startp);
10640 }
10641
10642 if (fonts_changed_p)
cb617e7c 10643 goto need_larger_matrices;
2311178e 10644
5f5c8ee5 10645 if (w->cursor.vpos >= 0)
aa6d10fa 10646 {
2311178e 10647 if (!just_this_one_p
5f5c8ee5 10648 || current_buffer->clip_changed
9142dd5b 10649 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
10650 /* Forget any recorded base line for line number display. */
10651 w->base_line_number = Qnil;
2311178e 10652
cb617e7c 10653 if (!make_cursor_line_fully_visible (w))
03b0a4b4
RS
10654 {
10655 clear_glyph_matrix (w->desired_matrix);
10656 last_line_misfit = 1;
10657 }
ddf6b9a3 10658 /* Drop through and scroll. */
ef3c2c73
RS
10659 else
10660 goto done;
aa6d10fa 10661 }
a2889657 10662 else
5f5c8ee5 10663 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
10664 }
10665
5f5c8ee5
GM
10666 try_to_scroll:
10667
ac90c44f
GM
10668 w->last_modified = make_number (0);
10669 w->last_overlay_modified = make_number (0);
5f5c8ee5 10670
e481f960
RS
10671 /* Redisplay the mode line. Select the buffer properly for that. */
10672 if (!update_mode_line)
10673 {
e481f960
RS
10674 update_mode_line = 1;
10675 w->update_mode_line = Qt;
10676 }
a2889657 10677
5f5c8ee5
GM
10678 /* Try to scroll by specified few lines. */
10679 if ((scroll_conservatively
10680 || scroll_step
10681 || temp_scroll_step
10682 || NUMBERP (current_buffer->scroll_up_aggressively)
10683 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 10684 && !current_buffer->clip_changed
2311178e 10685 && CHARPOS (startp) >= BEGV
5f5c8ee5 10686 && CHARPOS (startp) <= ZV)
0789adb2 10687 {
5f5c8ee5
GM
10688 /* The function returns -1 if new fonts were loaded, 1 if
10689 successful, 0 if not successful. */
10690 int rc = try_scrolling (window, just_this_one_p,
10691 scroll_conservatively,
10692 scroll_step,
03b0a4b4 10693 temp_scroll_step, last_line_misfit);
cb617e7c
GM
10694 switch (rc)
10695 {
10696 case SCROLLING_SUCCESS:
10697 goto done;
2311178e 10698
cb617e7c
GM
10699 case SCROLLING_NEED_LARGER_MATRICES:
10700 goto need_larger_matrices;
2311178e 10701
cb617e7c
GM
10702 case SCROLLING_FAILED:
10703 break;
2311178e 10704
cb617e7c
GM
10705 default:
10706 abort ();
10707 }
5f5c8ee5 10708 }
f9c8af06 10709
5f5c8ee5 10710 /* Finally, just choose place to start which centers point */
5936754e 10711
5f5c8ee5 10712 recenter:
ddf6b9a3
RS
10713 centering_position = window_box_height (w) / 2;
10714
10715 point_at_top:
10716 /* Jump here with centering_position already set to 0. */
44173109 10717
5f5c8ee5
GM
10718#if GLYPH_DEBUG
10719 debug_method_add (w, "recenter");
10720#endif
0789adb2 10721
5f5c8ee5 10722 /* w->vscroll = 0; */
0789adb2 10723
5f5c8ee5 10724 /* Forget any previously recorded base line for line number display. */
c62c1bb5 10725 if (!buffer_unchanged_p)
5f5c8ee5
GM
10726 w->base_line_number = Qnil;
10727
10728 /* Move backward half the height of the window. */
10729 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10730 it.current_y = it.last_visible_y;
ddf6b9a3 10731 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
10732 xassert (IT_CHARPOS (it) >= BEGV);
10733
10734 /* The function move_it_vertically_backward may move over more
10735 than the specified y-distance. If it->w is small, e.g. a
10736 mini-buffer window, we may end up in front of the window's
10737 display area. Start displaying at the start of the line
10738 containing PT in this case. */
10739 if (it.current_y <= 0)
10740 {
10741 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10742 move_it_vertically (&it, 0);
10743 xassert (IT_CHARPOS (it) <= PT);
10744 it.current_y = 0;
0789adb2
RS
10745 }
10746
5f5c8ee5 10747 it.current_x = it.hpos = 0;
2311178e 10748
5f5c8ee5
GM
10749 /* Set startp here explicitly in case that helps avoid an infinite loop
10750 in case the window-scroll-functions functions get errors. */
10751 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10752
10753 /* Run scroll hooks. */
10754 startp = run_window_scroll_functions (window, it.current.pos);
10755
10756 /* Redisplay the window. */
10757 if (!current_matrix_up_to_date_p
10758 || windows_or_buffers_changed
5fb96e96 10759 || cursor_type_changed
5f5c8ee5
GM
10760 /* Don't use try_window_reusing_current_matrix in this case
10761 because it can have changed the buffer. */
10762 || !NILP (Vwindow_scroll_functions)
10763 || !just_this_one_p
10764 || MINI_WINDOW_P (w)
10765 || !try_window_reusing_current_matrix (w))
10766 try_window (window, startp);
10767
10768 /* If new fonts have been loaded (due to fontsets), give up. We
10769 have to start a new redisplay since we need to re-adjust glyph
10770 matrices. */
10771 if (fonts_changed_p)
cb617e7c 10772 goto need_larger_matrices;
5f5c8ee5
GM
10773
10774 /* If cursor did not appear assume that the middle of the window is
10775 in the first line of the window. Do it again with the next line.
10776 (Imagine a window of height 100, displaying two lines of height
10777 60. Moving back 50 from it->last_visible_y will end in the first
10778 line.) */
10779 if (w->cursor.vpos < 0)
a2889657 10780 {
5f5c8ee5
GM
10781 if (!NILP (w->window_end_valid)
10782 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 10783 {
5f5c8ee5
GM
10784 clear_glyph_matrix (w->desired_matrix);
10785 move_it_by_lines (&it, 1, 0);
10786 try_window (window, it.current.pos);
a2889657 10787 }
5f5c8ee5 10788 else if (PT < IT_CHARPOS (it))
a2889657 10789 {
5f5c8ee5
GM
10790 clear_glyph_matrix (w->desired_matrix);
10791 move_it_by_lines (&it, -1, 0);
10792 try_window (window, it.current.pos);
10793 }
10794 else
10795 {
10796 /* Not much we can do about it. */
a2889657 10797 }
a2889657 10798 }
010494d0 10799
5f5c8ee5
GM
10800 /* Consider the following case: Window starts at BEGV, there is
10801 invisible, intangible text at BEGV, so that display starts at
10802 some point START > BEGV. It can happen that we are called with
10803 PT somewhere between BEGV and START. Try to handle that case. */
10804 if (w->cursor.vpos < 0)
835766b6 10805 {
5f5c8ee5
GM
10806 struct glyph_row *row = w->current_matrix->rows;
10807 if (row->mode_line_p)
10808 ++row;
10809 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 10810 }
2311178e 10811
cb617e7c 10812 if (!make_cursor_line_fully_visible (w))
ddf6b9a3
RS
10813 {
10814 /* If centering point failed to make the whole line visible,
10815 put point at the top instead. That has to make the whole line
10816 visible, if it can be done. */
10817 centering_position = 0;
10818 goto point_at_top;
10819 }
b5174a51 10820
74d481ac
GM
10821 done:
10822
5f5c8ee5
GM
10823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10824 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10825 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10826 ? Qt : Qnil);
a2889657 10827
5f5c8ee5 10828 /* Display the mode line, if we must. */
e481f960 10829 if ((update_mode_line
aa6d10fa 10830 /* If window not full width, must redo its mode line
2311178e 10831 if (a) the window to its side is being redone and
5f5c8ee5
GM
10832 (b) we do a frame-based redisplay. This is a consequence
10833 of how inverted lines are drawn in frame-based redisplay. */
2311178e 10834 || (!just_this_one_p
5f5c8ee5
GM
10835 && !FRAME_WINDOW_P (f)
10836 && !WINDOW_FULL_WIDTH_P (w))
10837 /* Line number to display. */
155ef550 10838 || INTEGERP (w->base_line_pos)
5f5c8ee5 10839 /* Column number is displayed and different from the one displayed. */
155ef550 10840 || (!NILP (w->column_number_displayed)
2311178e
TTN
10841 && (XFASTINT (w->column_number_displayed)
10842 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
10843 /* This means that the window has a mode line. */
10844 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 10845 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 10846 {
5f5c8ee5
GM
10847 display_mode_lines (w);
10848
10849 /* If mode line height has changed, arrange for a thorough
10850 immediate redisplay using the correct mode line height. */
10851 if (WINDOW_WANTS_MODELINE_P (w)
10852 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 10853 {
5f5c8ee5
GM
10854 fonts_changed_p = 1;
10855 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10856 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 10857 }
2311178e 10858
5f5c8ee5
GM
10859 /* If top line height has changed, arrange for a thorough
10860 immediate redisplay using the correct mode line height. */
045dee35
GM
10861 if (WINDOW_WANTS_HEADER_LINE_P (w)
10862 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
10863 {
10864 fonts_changed_p = 1;
045dee35
GM
10865 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10866 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10867 }
10868
10869 if (fonts_changed_p)
cb617e7c 10870 goto need_larger_matrices;
5ba50c51 10871 }
5f5c8ee5
GM
10872
10873 if (!line_number_displayed
10874 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
10875 {
10876 w->base_line_pos = Qnil;
10877 w->base_line_number = Qnil;
10878 }
a2889657 10879
5f5c8ee5 10880 finish_menu_bars:
2311178e 10881
7ce2c095 10882 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 10883 if (update_mode_line
5f5c8ee5
GM
10884 && EQ (FRAME_SELECTED_WINDOW (f), window))
10885 {
10886 int redisplay_menu_p = 0;
488dd4c4 10887 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
10888
10889 if (FRAME_WINDOW_P (f))
10890 {
488dd4c4
JD
10891#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
10892 || defined (USE_GTK)
5f5c8ee5 10893 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 10894#else
5f5c8ee5 10895 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 10896#endif
5f5c8ee5
GM
10897 }
10898 else
10899 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10900
10901 if (redisplay_menu_p)
10902 display_menu_bar (w);
10903
10904#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
10905#ifdef USE_GTK
10906 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
10907#else
10908 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
10909 && (FRAME_TOOL_BAR_LINES (f) > 0
10910 || auto_resize_tool_bars_p);
177c0ea7 10911
488dd4c4
JD
10912#endif
10913
10914 if (redisplay_tool_bar_p)
10915 redisplay_tool_bar (f);
5f5c8ee5
GM
10916#endif
10917 }
7ce2c095 10918
b60c9653
RS
10919 /* We go to this label, with fonts_changed_p nonzero,
10920 if it is necessary to try again using larger glyph matrices.
10921 We have to redeem the scroll bar even in this case,
10922 because the loop in redisplay_internal expects that. */
cb617e7c
GM
10923 need_larger_matrices:
10924 ;
88f22aff 10925 finish_scroll_bars:
5f5c8ee5 10926
88f22aff 10927 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 10928 {
b1d1124b 10929 int start, end, whole;
30c566e4 10930
b1d1124b 10931 /* Calculate the start and end positions for the current window.
3505ea70
JB
10932 At some point, it would be nice to choose between scrollbars
10933 which reflect the whole buffer size, with special markers
10934 indicating narrowing, and scrollbars which reflect only the
10935 visible region.
10936
5f5c8ee5 10937 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 10938 if (!MINI_WINDOW_P (w)
5f5c8ee5 10939 || (w == XWINDOW (minibuf_window)
c6e89d6c 10940 && NILP (echo_area_buffer[0])))
b1d1124b 10941 {
8a9311d7 10942 whole = ZV - BEGV;
4d641a15 10943 start = marker_position (w->start) - BEGV;
b1d1124b
JB
10944 /* I don't think this is guaranteed to be right. For the
10945 moment, we'll pretend it is. */
5f5c8ee5 10946 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 10947
2311178e 10948 if (end < start)
5f5c8ee5 10949 end = start;
2311178e 10950 if (whole < (end - start))
5f5c8ee5 10951 whole = end - start;
b1d1124b
JB
10952 }
10953 else
10954 start = end = whole = 0;
30c566e4 10955
88f22aff 10956 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 10957 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 10958
5f5c8ee5
GM
10959 /* Note that we actually used the scroll bar attached to this
10960 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 10961 redeem_scroll_bar_hook (w);
30c566e4 10962 }
b1d1124b 10963
5f5c8ee5
GM
10964 /* Restore current_buffer and value of point in it. */
10965 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 10966 set_buffer_internal_1 (old);
5f5c8ee5 10967 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
10968
10969 unbind_to (count, Qnil);
a2889657 10970}
a2889657 10971
5f5c8ee5
GM
10972
10973/* Build the complete desired matrix of WINDOW with a window start
10974 buffer position POS. Value is non-zero if successful. It is zero
10975 if fonts were loaded during redisplay which makes re-adjusting
10976 glyph matrices necessary. */
10977
10978int
a2889657
JB
10979try_window (window, pos)
10980 Lisp_Object window;
5f5c8ee5
GM
10981 struct text_pos pos;
10982{
10983 struct window *w = XWINDOW (window);
10984 struct it it;
10985 struct glyph_row *last_text_row = NULL;
9cbab4ff 10986
5f5c8ee5
GM
10987 /* Make POS the new window start. */
10988 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 10989
5f5c8ee5
GM
10990 /* Mark cursor position as unknown. No overlay arrow seen. */
10991 w->cursor.vpos = -1;
a2889657 10992 overlay_arrow_seen = 0;
642eefc6 10993
5f5c8ee5
GM
10994 /* Initialize iterator and info to start at POS. */
10995 start_display (&it, w, pos);
a2889657 10996
5f5c8ee5
GM
10997 /* Display all lines of W. */
10998 while (it.current_y < it.last_visible_y)
10999 {
11000 if (display_line (&it))
11001 last_text_row = it.glyph_row - 1;
11002 if (fonts_changed_p)
11003 return 0;
11004 }
a2889657 11005
5f5c8ee5
GM
11006 /* If bottom moved off end of frame, change mode line percentage. */
11007 if (XFASTINT (w->window_end_pos) <= 0
11008 && Z != IT_CHARPOS (it))
a2889657
JB
11009 w->update_mode_line = Qt;
11010
5f5c8ee5
GM
11011 /* Set window_end_pos to the offset of the last character displayed
11012 on the window from the end of current_buffer. Set
11013 window_end_vpos to its row number. */
11014 if (last_text_row)
11015 {
11016 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
11017 w->window_end_bytepos
11018 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11019 w->window_end_pos
11020 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11021 w->window_end_vpos
11022 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11023 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
11024 ->displays_text_p);
11025 }
11026 else
11027 {
11028 w->window_end_bytepos = 0;
ac90c44f 11029 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5 11030 }
2311178e 11031
a2889657
JB
11032 /* But that is not valid info until redisplay finishes. */
11033 w->window_end_valid = Qnil;
5f5c8ee5 11034 return 1;
a2889657 11035}
5f5c8ee5
GM
11036
11037
a2889657 11038\f
5f5c8ee5
GM
11039/************************************************************************
11040 Window redisplay reusing current matrix when buffer has not changed
11041 ************************************************************************/
11042
11043/* Try redisplay of window W showing an unchanged buffer with a
11044 different window start than the last time it was displayed by
11045 reusing its current matrix. Value is non-zero if successful.
11046 W->start is the new window start. */
a2889657
JB
11047
11048static int
5f5c8ee5
GM
11049try_window_reusing_current_matrix (w)
11050 struct window *w;
a2889657 11051{
5f5c8ee5
GM
11052 struct frame *f = XFRAME (w->frame);
11053 struct glyph_row *row, *bottom_row;
11054 struct it it;
11055 struct run run;
11056 struct text_pos start, new_start;
11057 int nrows_scrolled, i;
11058 struct glyph_row *last_text_row;
11059 struct glyph_row *last_reused_text_row;
11060 struct glyph_row *start_row;
11061 int start_vpos, min_y, max_y;
75c5350a 11062
69d1f7c9 11063#if GLYPH_DEBUG
76cb5e06
GM
11064 if (inhibit_try_window_reusing)
11065 return 0;
11066#endif
11067
d18354b6
GM
11068 if (/* This function doesn't handle terminal frames. */
11069 !FRAME_WINDOW_P (f)
11070 /* Don't try to reuse the display if windows have been split
11071 or such. */
5fb96e96
RS
11072 || windows_or_buffers_changed
11073 || cursor_type_changed)
5f5c8ee5 11074 return 0;
a2889657 11075
5f5c8ee5
GM
11076 /* Can't do this if region may have changed. */
11077 if ((!NILP (Vtransient_mark_mode)
11078 && !NILP (current_buffer->mark_active))
8f897821
GM
11079 || !NILP (w->region_showing)
11080 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 11081 return 0;
a2889657 11082
5f5c8ee5 11083 /* If top-line visibility has changed, give up. */
045dee35
GM
11084 if (WINDOW_WANTS_HEADER_LINE_P (w)
11085 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
11086 return 0;
11087
11088 /* Give up if old or new display is scrolled vertically. We could
11089 make this function handle this, but right now it doesn't. */
11090 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11091 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
11092 return 0;
11093
11094 /* The variable new_start now holds the new window start. The old
11095 start `start' can be determined from the current matrix. */
11096 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
11097 start = start_row->start.pos;
11098 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 11099
5f5c8ee5
GM
11100 /* Clear the desired matrix for the display below. */
11101 clear_glyph_matrix (w->desired_matrix);
2311178e 11102
5f5c8ee5
GM
11103 if (CHARPOS (new_start) <= CHARPOS (start))
11104 {
11105 int first_row_y;
2311178e 11106
607ec83c
GM
11107 /* Don't use this method if the display starts with an ellipsis
11108 displayed for invisible text. It's not easy to handle that case
11109 below, and it's certainly not worth the effort since this is
11110 not a frequent case. */
11111 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
11112 return 0;
11113
5f5c8ee5 11114 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 11115
5f5c8ee5
GM
11116 /* Display up to a row that can be reused. The variable
11117 last_text_row is set to the last row displayed that displays
b48f74cb
GM
11118 text. Note that it.vpos == 0 if or if not there is a
11119 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
11120 start_display (&it, w, new_start);
11121 first_row_y = it.current_y;
11122 w->cursor.vpos = -1;
11123 last_text_row = last_reused_text_row = NULL;
2311178e 11124
5f5c8ee5
GM
11125 while (it.current_y < it.last_visible_y
11126 && IT_CHARPOS (it) < CHARPOS (start)
11127 && !fonts_changed_p)
11128 if (display_line (&it))
11129 last_text_row = it.glyph_row - 1;
11130
11131 /* A value of current_y < last_visible_y means that we stopped
11132 at the previous window start, which in turn means that we
11133 have at least one reusable row. */
11134 if (it.current_y < it.last_visible_y)
a2889657 11135 {
b48f74cb 11136 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5 11137 nrows_scrolled = it.vpos;
2311178e 11138
5f5c8ee5
GM
11139 /* Find PT if not already found in the lines displayed. */
11140 if (w->cursor.vpos < 0)
a2889657 11141 {
5f5c8ee5 11142 int dy = it.current_y - first_row_y;
2311178e 11143
5f5c8ee5 11144 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
11145 row = row_containing_pos (w, PT, row, NULL, dy);
11146 if (row)
11147 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11148 dy, nrows_scrolled);
11149 else
5f5c8ee5
GM
11150 {
11151 clear_glyph_matrix (w->desired_matrix);
11152 return 0;
11153 }
a2889657 11154 }
5f5c8ee5
GM
11155
11156 /* Scroll the display. Do it before the current matrix is
11157 changed. The problem here is that update has not yet
11158 run, i.e. part of the current matrix is not up to date.
11159 scroll_run_hook will clear the cursor, and use the
11160 current matrix to get the height of the row the cursor is
11161 in. */
11162 run.current_y = first_row_y;
11163 run.desired_y = it.current_y;
11164 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
11165
11166 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 11167 {
5f5c8ee5
GM
11168 update_begin (f);
11169 rif->update_window_begin_hook (w);
64d1e7d3 11170 rif->clear_mouse_face (w);
5f5c8ee5 11171 rif->scroll_run_hook (w, &run);
64d1e7d3 11172 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 11173 update_end (f);
a2889657 11174 }
5f5c8ee5
GM
11175
11176 /* Shift current matrix down by nrows_scrolled lines. */
11177 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11178 rotate_matrix (w->current_matrix,
11179 start_vpos,
11180 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11181 nrows_scrolled);
2311178e 11182
9c8b8382 11183 /* Disable lines that must be updated. */
5f5c8ee5 11184 for (i = 0; i < it.vpos; ++i)
b48f74cb 11185 (start_row + i)->enabled_p = 0;
9c8b8382 11186
5f5c8ee5 11187 /* Re-compute Y positions. */
045dee35 11188 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11189 max_y = it.last_visible_y;
b48f74cb
GM
11190 for (row = start_row + nrows_scrolled;
11191 row < bottom_row;
11192 ++row)
d2f84654 11193 {
5f5c8ee5 11194 row->y = it.current_y;
75c5350a 11195 row->visible_height = row->height;
5f5c8ee5
GM
11196
11197 if (row->y < min_y)
75c5350a
GM
11198 row->visible_height -= min_y - row->y;
11199 if (row->y + row->height > max_y)
11200 row->visible_height -= row->y + row->height - max_y;
2311178e 11201
5f5c8ee5 11202 it.current_y += row->height;
5f5c8ee5
GM
11203
11204 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11205 last_reused_text_row = row;
11206 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11207 break;
d2f84654 11208 }
2311178e 11209
9c8b8382
GM
11210 /* Disable lines in the current matrix which are now
11211 below the window. */
bafb434c 11212 for (++row; row < bottom_row; ++row)
9c8b8382 11213 row->enabled_p = 0;
a2889657 11214 }
5f5c8ee5
GM
11215
11216 /* Update window_end_pos etc.; last_reused_text_row is the last
11217 reused row from the current matrix containing text, if any.
11218 The value of last_text_row is the last displayed line
11219 containing text. */
11220 if (last_reused_text_row)
a2889657 11221 {
5f5c8ee5
GM
11222 w->window_end_bytepos
11223 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
11224 w->window_end_pos
11225 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11226 w->window_end_vpos
11227 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11228 w->current_matrix));
a2889657 11229 }
5f5c8ee5
GM
11230 else if (last_text_row)
11231 {
11232 w->window_end_bytepos
11233 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11234 w->window_end_pos
11235 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11236 w->window_end_vpos
11237 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11238 }
11239 else
11240 {
11241 /* This window must be completely empty. */
11242 w->window_end_bytepos = 0;
ac90c44f 11243 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
11244 }
11245 w->window_end_valid = Qnil;
a2889657 11246
5f5c8ee5
GM
11247 /* Update hint: don't try scrolling again in update_window. */
11248 w->desired_matrix->no_scrolling_p = 1;
2311178e 11249
5f5c8ee5
GM
11250#if GLYPH_DEBUG
11251 debug_method_add (w, "try_window_reusing_current_matrix 1");
11252#endif
11253 return 1;
a2889657 11254 }
5f5c8ee5
GM
11255 else if (CHARPOS (new_start) > CHARPOS (start))
11256 {
11257 struct glyph_row *pt_row, *row;
11258 struct glyph_row *first_reusable_row;
11259 struct glyph_row *first_row_to_display;
11260 int dy;
11261 int yb = window_text_bottom_y (w);
11262
5f5c8ee5
GM
11263 /* Find the row starting at new_start, if there is one. Don't
11264 reuse a partially visible line at the end. */
b48f74cb 11265 first_reusable_row = start_row;
5f5c8ee5
GM
11266 while (first_reusable_row->enabled_p
11267 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11268 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11269 < CHARPOS (new_start)))
11270 ++first_reusable_row;
11271
11272 /* Give up if there is no row to reuse. */
11273 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
11274 || !first_reusable_row->enabled_p
11275 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11276 != CHARPOS (new_start)))
5f5c8ee5
GM
11277 return 0;
11278
5f5c8ee5
GM
11279 /* We can reuse fully visible rows beginning with
11280 first_reusable_row to the end of the window. Set
11281 first_row_to_display to the first row that cannot be reused.
11282 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 11283 pt_row = NULL;
ac90c44f
GM
11284 for (first_row_to_display = first_reusable_row;
11285 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11286 ++first_row_to_display)
5f5c8ee5 11287 {
4bde0ebb
GM
11288 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11289 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 11290 pt_row = first_row_to_display;
5f5c8ee5 11291 }
a2889657 11292
5f5c8ee5
GM
11293 /* Start displaying at the start of first_row_to_display. */
11294 xassert (first_row_to_display->y < yb);
11295 init_to_row_start (&it, w, first_row_to_display);
607ec83c 11296
b48f74cb
GM
11297 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11298 - start_vpos);
5f5c8ee5
GM
11299 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11300 - nrows_scrolled);
b48f74cb
GM
11301 it.current_y = (first_row_to_display->y - first_reusable_row->y
11302 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
11303
11304 /* Display lines beginning with first_row_to_display in the
11305 desired matrix. Set last_text_row to the last row displayed
11306 that displays text. */
11307 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11308 if (pt_row == NULL)
11309 w->cursor.vpos = -1;
11310 last_text_row = NULL;
11311 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11312 if (display_line (&it))
11313 last_text_row = it.glyph_row - 1;
11314
11315 /* Give up If point isn't in a row displayed or reused. */
11316 if (w->cursor.vpos < 0)
11317 {
11318 clear_glyph_matrix (w->desired_matrix);
11319 return 0;
11320 }
12adba34 11321
5f5c8ee5
GM
11322 /* If point is in a reused row, adjust y and vpos of the cursor
11323 position. */
11324 if (pt_row)
11325 {
11326 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11327 w->current_matrix);
11328 w->cursor.y -= first_reusable_row->y;
a2889657
JB
11329 }
11330
5f5c8ee5
GM
11331 /* Scroll the display. */
11332 run.current_y = first_reusable_row->y;
045dee35 11333 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11334 run.height = it.last_visible_y - run.current_y;
4da61803 11335 dy = run.current_y - run.desired_y;
2311178e 11336
5f5c8ee5
GM
11337 if (run.height)
11338 {
11339 struct frame *f = XFRAME (WINDOW_FRAME (w));
11340 update_begin (f);
11341 rif->update_window_begin_hook (w);
64d1e7d3 11342 rif->clear_mouse_face (w);
5f5c8ee5 11343 rif->scroll_run_hook (w, &run);
64d1e7d3 11344 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11345 update_end (f);
11346 }
a2889657 11347
5f5c8ee5
GM
11348 /* Adjust Y positions of reused rows. */
11349 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 11350 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11351 max_y = it.last_visible_y;
b48f74cb 11352 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
11353 {
11354 row->y -= dy;
75c5350a 11355 row->visible_height = row->height;
5f5c8ee5 11356 if (row->y < min_y)
75c5350a
GM
11357 row->visible_height -= min_y - row->y;
11358 if (row->y + row->height > max_y)
11359 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 11360 }
a2889657 11361
5f5c8ee5
GM
11362 /* Scroll the current matrix. */
11363 xassert (nrows_scrolled > 0);
11364 rotate_matrix (w->current_matrix,
11365 start_vpos,
11366 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11367 -nrows_scrolled);
11368
ac90c44f
GM
11369 /* Disable rows not reused. */
11370 for (row -= nrows_scrolled; row < bottom_row; ++row)
11371 row->enabled_p = 0;
11372
5f5c8ee5
GM
11373 /* Adjust window end. A null value of last_text_row means that
11374 the window end is in reused rows which in turn means that
11375 only its vpos can have changed. */
11376 if (last_text_row)
11377 {
11378 w->window_end_bytepos
11379 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11380 w->window_end_pos
11381 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11382 w->window_end_vpos
11383 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11384 }
11385 else
a2889657 11386 {
ac90c44f
GM
11387 w->window_end_vpos
11388 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 11389 }
2311178e 11390
5f5c8ee5
GM
11391 w->window_end_valid = Qnil;
11392 w->desired_matrix->no_scrolling_p = 1;
11393
11394#if GLYPH_DEBUG
11395 debug_method_add (w, "try_window_reusing_current_matrix 2");
11396#endif
11397 return 1;
a2889657 11398 }
2311178e 11399
5f5c8ee5
GM
11400 return 0;
11401}
a2889657 11402
a2889657 11403
5f5c8ee5
GM
11404\f
11405/************************************************************************
11406 Window redisplay reusing current matrix when buffer has changed
11407 ************************************************************************/
11408
1ec185cb
GM
11409static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11410static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
11411 int *, int *));
11412static struct glyph_row *
11413find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11414 struct glyph_row *));
11415
11416
11417/* Return the last row in MATRIX displaying text. If row START is
11418 non-null, start searching with that row. IT gives the dimensions
11419 of the display. Value is null if matrix is empty; otherwise it is
11420 a pointer to the row found. */
11421
11422static struct glyph_row *
11423find_last_row_displaying_text (matrix, it, start)
11424 struct glyph_matrix *matrix;
11425 struct it *it;
11426 struct glyph_row *start;
11427{
11428 struct glyph_row *row, *row_found;
11429
11430 /* Set row_found to the last row in IT->w's current matrix
11431 displaying text. The loop looks funny but think of partially
11432 visible lines. */
11433 row_found = NULL;
11434 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11435 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11436 {
11437 xassert (row->enabled_p);
11438 row_found = row;
11439 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11440 break;
11441 ++row;
a2889657 11442 }
2311178e 11443
5f5c8ee5
GM
11444 return row_found;
11445}
11446
a2889657 11447
5f5c8ee5 11448/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
11449 by changes at the start of current_buffer that occurred since W's
11450 current matrix was built. Value is null if no such row exists.
a2889657 11451
d43fbe9d
GM
11452 BEG_UNCHANGED us the number of characters unchanged at the start of
11453 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11454 first changed character in current_buffer. Characters at positions <
11455 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11456 when the current matrix was built. */
5f5c8ee5
GM
11457
11458static struct glyph_row *
1ec185cb 11459find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
11460 struct window *w;
11461{
9142dd5b 11462 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
11463 struct glyph_row *row;
11464 struct glyph_row *row_found = NULL;
11465 int yb = window_text_bottom_y (w);
11466
11467 /* Find the last row displaying unchanged text. */
11468 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11469 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11470 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 11471 {
5f5c8ee5
GM
11472 if (/* If row ends before first_changed_pos, it is unchanged,
11473 except in some case. */
11474 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11475 /* When row ends in ZV and we write at ZV it is not
11476 unchanged. */
11477 && !row->ends_at_zv_p
11478 /* When first_changed_pos is the end of a continued line,
11479 row is not unchanged because it may be no longer
11480 continued. */
11481 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11482 && row->continued_p))
11483 row_found = row;
11484
11485 /* Stop if last visible row. */
11486 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11487 break;
2311178e 11488
5f5c8ee5 11489 ++row;
a2889657
JB
11490 }
11491
5f5c8ee5 11492 return row_found;
a2889657 11493}
5f5c8ee5
GM
11494
11495
11496/* Find the first glyph row in the current matrix of W that is not
2311178e 11497 affected by changes at the end of current_buffer since the
d43fbe9d
GM
11498 time W's current matrix was built.
11499
11500 Return in *DELTA the number of chars by which buffer positions in
11501 unchanged text at the end of current_buffer must be adjusted.
2311178e 11502
d43fbe9d
GM
11503 Return in *DELTA_BYTES the corresponding number of bytes.
11504
11505 Value is null if no such row exists, i.e. all rows are affected by
11506 changes. */
2311178e 11507
5f5c8ee5 11508static struct glyph_row *
1ec185cb 11509find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
11510 struct window *w;
11511 int *delta, *delta_bytes;
a2889657 11512{
5f5c8ee5
GM
11513 struct glyph_row *row;
11514 struct glyph_row *row_found = NULL;
c581d710 11515
5f5c8ee5 11516 *delta = *delta_bytes = 0;
b2a76982 11517
1ec185cb
GM
11518 /* Display must not have been paused, otherwise the current matrix
11519 is not up to date. */
11520 if (NILP (w->window_end_valid))
11521 abort ();
2311178e 11522
1ec185cb 11523 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
11524 end is in the range of changed text. If so, there is no
11525 unchanged row at the end of W's current matrix. */
9142dd5b 11526 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
11527 return NULL;
11528
11529 /* Set row to the last row in W's current matrix displaying text. */
11530 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
11531
11532 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
11533 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11534 {
11535 /* The value of row is the last glyph row in the matrix having a
11536 meaningful buffer position in it. The end position of row
11537 corresponds to window_end_pos. This allows us to translate
11538 buffer positions in the current matrix to current buffer
11539 positions for characters not in changed text. */
11540 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11541 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11542 int last_unchanged_pos, last_unchanged_pos_old;
11543 struct glyph_row *first_text_row
11544 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11545
11546 *delta = Z - Z_old;
11547 *delta_bytes = Z_BYTE - Z_BYTE_old;
11548
11549 /* Set last_unchanged_pos to the buffer position of the last
11550 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
11551 index + 1 of the last character in current_buffer, i.e. by
11552 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
11553 unchanged character, and we have to add BEG to get its buffer
11554 position. */
9142dd5b 11555 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 11556 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 11557
5f5c8ee5
GM
11558 /* Search backward from ROW for a row displaying a line that
11559 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 11560 for (; row > first_text_row; --row)
5f5c8ee5 11561 {
1ec185cb
GM
11562 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11563 abort ();
2311178e 11564
5f5c8ee5
GM
11565 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11566 row_found = row;
5f5c8ee5
GM
11567 }
11568 }
11569
1ec185cb
GM
11570 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11571 abort ();
2311178e 11572
5f5c8ee5 11573 return row_found;
c581d710
RS
11574}
11575
c581d710 11576
5f5c8ee5
GM
11577/* Make sure that glyph rows in the current matrix of window W
11578 reference the same glyph memory as corresponding rows in the
11579 frame's frame matrix. This function is called after scrolling W's
11580 current matrix on a terminal frame in try_window_id and
11581 try_window_reusing_current_matrix. */
11582
11583static void
11584sync_frame_with_window_matrix_rows (w)
11585 struct window *w;
c581d710 11586{
5f5c8ee5
GM
11587 struct frame *f = XFRAME (w->frame);
11588 struct glyph_row *window_row, *window_row_end, *frame_row;
11589
11590 /* Preconditions: W must be a leaf window and full-width. Its frame
11591 must have a frame matrix. */
11592 xassert (NILP (w->hchild) && NILP (w->vchild));
11593 xassert (WINDOW_FULL_WIDTH_P (w));
11594 xassert (!FRAME_WINDOW_P (f));
11595
11596 /* If W is a full-width window, glyph pointers in W's current matrix
11597 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
11598 corresponding frame matrix. Note that frame matrices have no
11599 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
11600 window_row = w->current_matrix->rows;
11601 window_row_end = window_row + w->current_matrix->nrows;
11602 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11603 while (window_row < window_row_end)
659a218f 11604 {
7d4cc828
GM
11605 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11606 struct glyph *end = window_row->glyphs[LAST_AREA];
11607
11608 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11609 frame_row->glyphs[TEXT_AREA] = start;
11610 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11611 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
11612
11613 /* Disable frame rows whose corresponding window rows have
11614 been disabled in try_window_id. */
11615 if (!window_row->enabled_p)
11616 frame_row->enabled_p = 0;
2311178e 11617
5f5c8ee5 11618 ++window_row, ++frame_row;
659a218f 11619 }
a2889657 11620}
5f5c8ee5
GM
11621
11622
e037b9ec
GM
11623/* Find the glyph row in window W containing CHARPOS. Consider all
11624 rows between START and END (not inclusive). END null means search
11625 all rows to the end of the display area of W. Value is the row
11626 containing CHARPOS or null. */
11627
0bef35bc 11628struct glyph_row *
0df56f4d 11629row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
11630 struct window *w;
11631 int charpos;
11632 struct glyph_row *start, *end;
0df56f4d 11633 int dy;
e037b9ec
GM
11634{
11635 struct glyph_row *row = start;
11636 int last_y;
11637
11638 /* If we happen to start on a header-line, skip that. */
11639 if (row->mode_line_p)
11640 ++row;
2311178e 11641
e037b9ec
GM
11642 if ((end && row >= end) || !row->enabled_p)
11643 return NULL;
2311178e 11644
0df56f4d 11645 last_y = window_text_bottom_y (w) - dy;
2311178e 11646
5cce13dd
RS
11647 while (1)
11648 {
11649 /* Give up if we have gone too far. */
11650 if (end && row >= end)
11651 return NULL;
40a301b3
RS
11652 /* This formerly returned if they were equal.
11653 I think that both quantities are of a "last plus one" type;
11654 if so, when they are equal, the row is within the screen. -- rms. */
11655 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
11656 return NULL;
11657
11658 /* If it is in this row, return this row. */
11659 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 11660 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
11661 /* The end position of a row equals the start
11662 position of the next row. If CHARPOS is there, we
11663 would rather display it in the next line, except
11664 when this line ends in ZV. */
11665 && !row->ends_at_zv_p
5cce13dd
RS
11666 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11667 && charpos >= MATRIX_ROW_START_CHARPOS (row))
11668 return row;
11669 ++row;
11670 }
e037b9ec
GM
11671}
11672
11673
5f5c8ee5
GM
11674/* Try to redisplay window W by reusing its existing display. W's
11675 current matrix must be up to date when this function is called,
11676 i.e. window_end_valid must not be nil.
11677
11678 Value is
11679
11680 1 if display has been updated
11681 0 if otherwise unsuccessful
11682 -1 if redisplay with same window start is known not to succeed
11683
11684 The following steps are performed:
11685
11686 1. Find the last row in the current matrix of W that is not
11687 affected by changes at the start of current_buffer. If no such row
11688 is found, give up.
11689
11690 2. Find the first row in W's current matrix that is not affected by
11691 changes at the end of current_buffer. Maybe there is no such row.
11692
11693 3. Display lines beginning with the row + 1 found in step 1 to the
11694 row found in step 2 or, if step 2 didn't find a row, to the end of
11695 the window.
11696
11697 4. If cursor is not known to appear on the window, give up.
11698
11699 5. If display stopped at the row found in step 2, scroll the
11700 display and current matrix as needed.
11701
11702 6. Maybe display some lines at the end of W, if we must. This can
11703 happen under various circumstances, like a partially visible line
11704 becoming fully visible, or because newly displayed lines are displayed
11705 in smaller font sizes.
11706
11707 7. Update W's window end information. */
11708
12adba34 11709static int
5f5c8ee5 11710try_window_id (w)
12adba34 11711 struct window *w;
12adba34 11712{
5f5c8ee5
GM
11713 struct frame *f = XFRAME (w->frame);
11714 struct glyph_matrix *current_matrix = w->current_matrix;
11715 struct glyph_matrix *desired_matrix = w->desired_matrix;
11716 struct glyph_row *last_unchanged_at_beg_row;
11717 struct glyph_row *first_unchanged_at_end_row;
11718 struct glyph_row *row;
11719 struct glyph_row *bottom_row;
11720 int bottom_vpos;
11721 struct it it;
11722 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11723 struct text_pos start_pos;
11724 struct run run;
11725 int first_unchanged_at_end_vpos = 0;
11726 struct glyph_row *last_text_row, *last_text_row_at_end;
11727 struct text_pos start;
27d16f05 11728 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 11729
69d1f7c9 11730#if GLYPH_DEBUG
76cb5e06
GM
11731 if (inhibit_try_window_id)
11732 return 0;
11733#endif
11734
27d16f05
GM
11735 /* This is handy for debugging. */
11736#if 0
11737#define GIVE_UP(X) \
11738 do { \
11739 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11740 return 0; \
11741 } while (0)
11742#else
62397849 11743#define GIVE_UP(X) return 0
27d16f05 11744#endif
2311178e 11745
5f5c8ee5
GM
11746 SET_TEXT_POS_FROM_MARKER (start, w->start);
11747
27d16f05
GM
11748 /* Don't use this for mini-windows because these can show
11749 messages and mini-buffers, and we don't handle that here. */
11750 if (MINI_WINDOW_P (w))
11751 GIVE_UP (1);
2311178e 11752
27d16f05 11753 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 11754 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 11755 GIVE_UP (2);
2311178e 11756
c62c1bb5
RS
11757 /* Verify that narrowing has not changed.
11758 Also verify that we were not told to prevent redisplay optimizations.
11759 It would be nice to further
27d16f05 11760 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
11761 if (current_buffer->clip_changed
11762 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
11763 GIVE_UP (3);
11764
11765 /* Window must either use window-based redisplay or be full width. */
11766 if (!FRAME_WINDOW_P (f)
11767 && (!line_ins_del_ok
11768 || !WINDOW_FULL_WIDTH_P (w)))
11769 GIVE_UP (4);
5f5c8ee5 11770
f5376658 11771 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
11772 if (PT < CHARPOS (start))
11773 GIVE_UP (5);
11774
11775 /* Another way to prevent redisplay optimizations. */
11776 if (XFASTINT (w->last_modified) == 0)
11777 GIVE_UP (6);
2311178e 11778
f5376658 11779 /* Verify that window is not hscrolled. */
27d16f05
GM
11780 if (XFASTINT (w->hscroll) != 0)
11781 GIVE_UP (7);
2311178e 11782
f5376658 11783 /* Verify that display wasn't paused. */
27d16f05
GM
11784 if (NILP (w->window_end_valid))
11785 GIVE_UP (8);
2311178e 11786
27d16f05
GM
11787 /* Can't use this if highlighting a region because a cursor movement
11788 will do more than just set the cursor. */
11789 if (!NILP (Vtransient_mark_mode)
11790 && !NILP (current_buffer->mark_active))
11791 GIVE_UP (9);
11792
11793 /* Likewise if highlighting trailing whitespace. */
11794 if (!NILP (Vshow_trailing_whitespace))
11795 GIVE_UP (11);
2311178e 11796
27d16f05
GM
11797 /* Likewise if showing a region. */
11798 if (!NILP (w->region_showing))
11799 GIVE_UP (10);
2311178e 11800
27d16f05
GM
11801 /* Can use this if overlay arrow position and or string have changed. */
11802 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11803 || !EQ (last_arrow_string, Voverlay_arrow_string))
11804 GIVE_UP (12);
11805
2311178e 11806
5f5c8ee5
GM
11807 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11808 only if buffer has really changed. The reason is that the gap is
11809 initially at Z for freshly visited files. The code below would
11810 set end_unchanged to 0 in that case. */
28ee91c0
GM
11811 if (MODIFF > SAVE_MODIFF
11812 /* This seems to happen sometimes after saving a buffer. */
11813 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 11814 {
9142dd5b
GM
11815 if (GPT - BEG < BEG_UNCHANGED)
11816 BEG_UNCHANGED = GPT - BEG;
11817 if (Z - GPT < END_UNCHANGED)
11818 END_UNCHANGED = Z - GPT;
5f5c8ee5 11819 }
bf9249e3 11820
27d16f05
GM
11821 /* The position of the first and last character that has been changed. */
11822 first_changed_charpos = BEG + BEG_UNCHANGED;
11823 last_changed_charpos = Z - END_UNCHANGED;
11824
5f5c8ee5
GM
11825 /* If window starts after a line end, and the last change is in
11826 front of that newline, then changes don't affect the display.
f2d86d7a
GM
11827 This case happens with stealth-fontification. Note that although
11828 the display is unchanged, glyph positions in the matrix have to
11829 be adjusted, of course. */
5f5c8ee5 11830 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 11831 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 11832 && ((last_changed_charpos < CHARPOS (start)
27d16f05 11833 && CHARPOS (start) == BEGV)
e60f4527 11834 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
11835 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11836 {
11837 int Z_old, delta, Z_BYTE_old, delta_bytes;
11838 struct glyph_row *r0;
11839
11840 /* Compute how many chars/bytes have been added to or removed
11841 from the buffer. */
11842 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11843 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11844 delta = Z - Z_old;
11845 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 11846
27d16f05
GM
11847 /* Give up if PT is not in the window. Note that it already has
11848 been checked at the start of try_window_id that PT is not in
11849 front of the window start. */
11850 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11851 GIVE_UP (13);
11852
11853 /* If window start is unchanged, we can reuse the whole matrix
11854 as is, after adjusting glyph positions. No need to compute
11855 the window end again, since its offset from Z hasn't changed. */
11856 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11857 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11858 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11859 {
11860 /* Adjust positions in the glyph matrix. */
11861 if (delta || delta_bytes)
11862 {
11863 struct glyph_row *r1
11864 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11865 increment_matrix_positions (w->current_matrix,
11866 MATRIX_ROW_VPOS (r0, current_matrix),
11867 MATRIX_ROW_VPOS (r1, current_matrix),
11868 delta, delta_bytes);
11869 }
2311178e 11870
27d16f05 11871 /* Set the cursor. */
0df56f4d 11872 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
11873 if (row)
11874 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
11875 else
11876 abort ();
27d16f05
GM
11877 return 1;
11878 }
5f5c8ee5
GM
11879 }
11880
27d16f05 11881 /* Handle the case that changes are all below what is displayed in
33ea6c4d 11882 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
11883 be taken if ZV is visible in the window, and text has been added
11884 there that is visible in the window. */
11885 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
11886 /* ZV is not visible in the window, or there are no
11887 changes at ZV, actually. */
11888 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11889 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 11890 {
27d16f05 11891 struct glyph_row *r0;
ef121659 11892
27d16f05
GM
11893 /* Give up if PT is not in the window. Note that it already has
11894 been checked at the start of try_window_id that PT is not in
11895 front of the window start. */
11896 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11897 GIVE_UP (14);
11898
11899 /* If window start is unchanged, we can reuse the whole matrix
11900 as is, without changing glyph positions since no text has
11901 been added/removed in front of the window end. */
11902 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11903 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11904 {
11905 /* We have to compute the window end anew since text
11906 can have been added/removed after it. */
11907 w->window_end_pos
11908 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11909 w->window_end_bytepos
11910 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11911
11912 /* Set the cursor. */
0df56f4d 11913 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
11914 if (row)
11915 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
11916 else
11917 abort ();
27d16f05
GM
11918 return 2;
11919 }
5f5c8ee5
GM
11920 }
11921
2b9c25e0 11922 /* Give up if window start is in the changed area.
2311178e 11923
2b9c25e0
GM
11924 The condition used to read
11925
11926 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11927
11928 but why that was tested escapes me at the moment. */
11929 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
11930 && CHARPOS (start) <= last_changed_charpos)
11931 GIVE_UP (15);
2311178e 11932
f5376658
RS
11933 /* Check that window start agrees with the start of the first glyph
11934 row in its current matrix. Check this after we know the window
11935 start is not in changed text, otherwise positions would not be
11936 comparable. */
27d16f05 11937 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 11938 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 11939 GIVE_UP (16);
5f5c8ee5 11940
23e8bd86
GM
11941 /* Give up if the window ends in strings. Overlay strings
11942 at the end are difficult to handle, so don't try. */
11943 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11944 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11945 GIVE_UP (20);
11946
5f5c8ee5
GM
11947 /* Compute the position at which we have to start displaying new
11948 lines. Some of the lines at the top of the window might be
11949 reusable because they are not displaying changed text. Find the
11950 last row in W's current matrix not affected by changes at the
11951 start of current_buffer. Value is null if changes start in the
11952 first line of window. */
1ec185cb 11953 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
11954 if (last_unchanged_at_beg_row)
11955 {
67f1cf4c
GM
11956 /* Avoid starting to display in the moddle of a character, a TAB
11957 for instance. This is easier than to set up the iterator
11958 exactly, and it's not a frequent case, so the additional
11959 effort wouldn't really pay off. */
e74fb0f7
GM
11960 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11961 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
11962 && last_unchanged_at_beg_row > w->current_matrix->rows)
11963 --last_unchanged_at_beg_row;
11964
11965 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 11966 GIVE_UP (17);
47d57b22
GM
11967
11968 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11969 GIVE_UP (18);
5f5c8ee5 11970 start_pos = it.current.pos;
2311178e 11971
5f5c8ee5
GM
11972 /* Start displaying new lines in the desired matrix at the same
11973 vpos we would use in the current matrix, i.e. below
11974 last_unchanged_at_beg_row. */
11975 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11976 current_matrix);
11977 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11978 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11979
11980 xassert (it.hpos == 0 && it.current_x == 0);
11981 }
11982 else
11983 {
11984 /* There are no reusable lines at the start of the window.
11985 Start displaying in the first line. */
11986 start_display (&it, w, start);
11987 start_pos = it.current.pos;
11988 }
11989
5f5c8ee5
GM
11990 /* Find the first row that is not affected by changes at the end of
11991 the buffer. Value will be null if there is no unchanged row, in
11992 which case we must redisplay to the end of the window. delta
11993 will be set to the value by which buffer positions beginning with
11994 first_unchanged_at_end_row have to be adjusted due to text
11995 changes. */
11996 first_unchanged_at_end_row
1ec185cb 11997 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
11998 IF_DEBUG (debug_delta = delta);
11999 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 12000
5f5c8ee5
GM
12001 /* Set stop_pos to the buffer position up to which we will have to
12002 display new lines. If first_unchanged_at_end_row != NULL, this
12003 is the buffer position of the start of the line displayed in that
12004 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12005 that we don't stop at a buffer position. */
12006 stop_pos = 0;
12007 if (first_unchanged_at_end_row)
12008 {
12009 xassert (last_unchanged_at_beg_row == NULL
12010 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 12011
5f5c8ee5
GM
12012 /* If this is a continuation line, move forward to the next one
12013 that isn't. Changes in lines above affect this line.
12014 Caution: this may move first_unchanged_at_end_row to a row
12015 not displaying text. */
12016 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
12017 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12018 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12019 < it.last_visible_y))
12020 ++first_unchanged_at_end_row;
12021
12022 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12023 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12024 >= it.last_visible_y))
12025 first_unchanged_at_end_row = NULL;
12026 else
12027 {
12028 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
12029 + delta);
12030 first_unchanged_at_end_vpos
12031 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 12032 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
12033 }
12034 }
12035 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 12036 GIVE_UP (19);
5f5c8ee5
GM
12037
12038
12039#if GLYPH_DEBUG
2311178e 12040
5f5c8ee5
GM
12041 /* Either there is no unchanged row at the end, or the one we have
12042 now displays text. This is a necessary condition for the window
12043 end pos calculation at the end of this function. */
12044 xassert (first_unchanged_at_end_row == NULL
12045 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 12046
5f5c8ee5
GM
12047 debug_last_unchanged_at_beg_vpos
12048 = (last_unchanged_at_beg_row
12049 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
12050 : -1);
12051 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 12052
5f5c8ee5
GM
12053#endif /* GLYPH_DEBUG != 0 */
12054
2311178e 12055
5f5c8ee5
GM
12056 /* Display new lines. Set last_text_row to the last new line
12057 displayed which has text on it, i.e. might end up as being the
12058 line where the window_end_vpos is. */
12059 w->cursor.vpos = -1;
12060 last_text_row = NULL;
12061 overlay_arrow_seen = 0;
12062 while (it.current_y < it.last_visible_y
12063 && !fonts_changed_p
12064 && (first_unchanged_at_end_row == NULL
12065 || IT_CHARPOS (it) < stop_pos))
12066 {
12067 if (display_line (&it))
12068 last_text_row = it.glyph_row - 1;
12069 }
12070
12071 if (fonts_changed_p)
12072 return -1;
12073
12074
12075 /* Compute differences in buffer positions, y-positions etc. for
12076 lines reused at the bottom of the window. Compute what we can
12077 scroll. */
ca42b2e8
GM
12078 if (first_unchanged_at_end_row
12079 /* No lines reused because we displayed everything up to the
12080 bottom of the window. */
12081 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
12082 {
12083 dvpos = (it.vpos
12084 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
12085 current_matrix));
12086 dy = it.current_y - first_unchanged_at_end_row->y;
12087 run.current_y = first_unchanged_at_end_row->y;
12088 run.desired_y = run.current_y + dy;
12089 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
12090 }
12091 else
ca42b2e8
GM
12092 {
12093 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
12094 first_unchanged_at_end_row = NULL;
12095 }
5f5c8ee5
GM
12096 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
12097
8f8ba186 12098
5f5c8ee5
GM
12099 /* Find the cursor if not already found. We have to decide whether
12100 PT will appear on this window (it sometimes doesn't, but this is
12101 not a very frequent case.) This decision has to be made before
12102 the current matrix is altered. A value of cursor.vpos < 0 means
12103 that PT is either in one of the lines beginning at
12104 first_unchanged_at_end_row or below the window. Don't care for
12105 lines that might be displayed later at the window end; as
12106 mentioned, this is not a frequent case. */
12107 if (w->cursor.vpos < 0)
12108 {
5f5c8ee5
GM
12109 /* Cursor in unchanged rows at the top? */
12110 if (PT < CHARPOS (start_pos)
12111 && last_unchanged_at_beg_row)
12112 {
e037b9ec
GM
12113 row = row_containing_pos (w, PT,
12114 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 12115 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
12116 if (row)
12117 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
12118 }
12119
12120 /* Start from first_unchanged_at_end_row looking for PT. */
12121 else if (first_unchanged_at_end_row)
12122 {
e037b9ec 12123 row = row_containing_pos (w, PT - delta,
0df56f4d 12124 first_unchanged_at_end_row, NULL, 0);
e037b9ec 12125 if (row)
468155d7
GM
12126 set_cursor_from_row (w, row, w->current_matrix, delta,
12127 delta_bytes, dy, dvpos);
5f5c8ee5
GM
12128 }
12129
12130 /* Give up if cursor was not found. */
12131 if (w->cursor.vpos < 0)
12132 {
12133 clear_glyph_matrix (w->desired_matrix);
12134 return -1;
12135 }
12136 }
2311178e 12137
5f5c8ee5
GM
12138 /* Don't let the cursor end in the scroll margins. */
12139 {
12140 int this_scroll_margin, cursor_height;
2311178e 12141
5f5c8ee5
GM
12142 this_scroll_margin = max (0, scroll_margin);
12143 this_scroll_margin = min (this_scroll_margin,
12144 XFASTINT (w->height) / 4);
12145 this_scroll_margin *= CANON_Y_UNIT (it.f);
12146 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 12147
5f5c8ee5
GM
12148 if ((w->cursor.y < this_scroll_margin
12149 && CHARPOS (start) > BEGV)
12150 /* Don't take scroll margin into account at the bottom because
12151 old redisplay didn't do it either. */
12152 || w->cursor.y + cursor_height > it.last_visible_y)
12153 {
12154 w->cursor.vpos = -1;
12155 clear_glyph_matrix (w->desired_matrix);
12156 return -1;
12157 }
12158 }
12159
12160 /* Scroll the display. Do it before changing the current matrix so
12161 that xterm.c doesn't get confused about where the cursor glyph is
12162 found. */
fa77249f 12163 if (dy && run.height)
5f5c8ee5
GM
12164 {
12165 update_begin (f);
2311178e 12166
5f5c8ee5
GM
12167 if (FRAME_WINDOW_P (f))
12168 {
12169 rif->update_window_begin_hook (w);
64d1e7d3 12170 rif->clear_mouse_face (w);
5f5c8ee5 12171 rif->scroll_run_hook (w, &run);
64d1e7d3 12172 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
12173 }
12174 else
12175 {
12176 /* Terminal frame. In this case, dvpos gives the number of
12177 lines to scroll by; dvpos < 0 means scroll up. */
12178 int first_unchanged_at_end_vpos
12179 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12180 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
522ed7fb
GM
12181 int end = (XFASTINT (w->top)
12182 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12183 + window_internal_height (w));
2311178e 12184
5f5c8ee5
GM
12185 /* Perform the operation on the screen. */
12186 if (dvpos > 0)
12187 {
12188 /* Scroll last_unchanged_at_beg_row to the end of the
12189 window down dvpos lines. */
12190 set_terminal_window (end);
12191
12192 /* On dumb terminals delete dvpos lines at the end
12193 before inserting dvpos empty lines. */
12194 if (!scroll_region_ok)
12195 ins_del_lines (end - dvpos, -dvpos);
12196
12197 /* Insert dvpos empty lines in front of
12198 last_unchanged_at_beg_row. */
12199 ins_del_lines (from, dvpos);
12200 }
12201 else if (dvpos < 0)
12202 {
12203 /* Scroll up last_unchanged_at_beg_vpos to the end of
12204 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12205 set_terminal_window (end);
12206
12207 /* Delete dvpos lines in front of
12208 last_unchanged_at_beg_vpos. ins_del_lines will set
12209 the cursor to the given vpos and emit |dvpos| delete
12210 line sequences. */
12211 ins_del_lines (from + dvpos, dvpos);
12212
12213 /* On a dumb terminal insert dvpos empty lines at the
12214 end. */
12215 if (!scroll_region_ok)
12216 ins_del_lines (end + dvpos, -dvpos);
12217 }
2311178e 12218
5f5c8ee5
GM
12219 set_terminal_window (0);
12220 }
2311178e 12221
5f5c8ee5
GM
12222 update_end (f);
12223 }
12224
ca42b2e8
GM
12225 /* Shift reused rows of the current matrix to the right position.
12226 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12227 text. */
12228 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12229 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
12230 if (dvpos < 0)
12231 {
12232 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12233 bottom_vpos, dvpos);
12234 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12235 bottom_vpos, 0);
12236 }
12237 else if (dvpos > 0)
12238 {
12239 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12240 bottom_vpos, dvpos);
12241 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12242 first_unchanged_at_end_vpos + dvpos, 0);
12243 }
12244
12245 /* For frame-based redisplay, make sure that current frame and window
12246 matrix are in sync with respect to glyph memory. */
12247 if (!FRAME_WINDOW_P (f))
12248 sync_frame_with_window_matrix_rows (w);
12249
12250 /* Adjust buffer positions in reused rows. */
12251 if (delta)
f2d86d7a
GM
12252 increment_matrix_positions (current_matrix,
12253 first_unchanged_at_end_vpos + dvpos,
12254 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
12255
12256 /* Adjust Y positions. */
12257 if (dy)
12258 shift_glyph_matrix (w, current_matrix,
12259 first_unchanged_at_end_vpos + dvpos,
12260 bottom_vpos, dy);
12261
12262 if (first_unchanged_at_end_row)
12263 first_unchanged_at_end_row += dvpos;
12264
12265 /* If scrolling up, there may be some lines to display at the end of
12266 the window. */
12267 last_text_row_at_end = NULL;
12268 if (dy < 0)
12269 {
9c6ba6d1
GM
12270 /* Scrolling up can leave for example a partially visible line
12271 at the end of the window to be redisplayed. */
5f5c8ee5
GM
12272 /* Set last_row to the glyph row in the current matrix where the
12273 window end line is found. It has been moved up or down in
12274 the matrix by dvpos. */
12275 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12276 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12277
12278 /* If last_row is the window end line, it should display text. */
12279 xassert (last_row->displays_text_p);
12280
12281 /* If window end line was partially visible before, begin
12282 displaying at that line. Otherwise begin displaying with the
12283 line following it. */
12284 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12285 {
12286 init_to_row_start (&it, w, last_row);
12287 it.vpos = last_vpos;
12288 it.current_y = last_row->y;
12289 }
12290 else
12291 {
12292 init_to_row_end (&it, w, last_row);
12293 it.vpos = 1 + last_vpos;
12294 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12295 ++last_row;
12296 }
12adba34 12297
23e8bd86
GM
12298 /* We may start in a continuation line. If so, we have to
12299 get the right continuation_lines_width and current_x. */
12300 it.continuation_lines_width = last_row->continuation_lines_width;
12301 it.hpos = it.current_x = 0;
2311178e 12302
23e8bd86
GM
12303 /* Display the rest of the lines at the window end. */
12304 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12305 while (it.current_y < it.last_visible_y
12306 && !fonts_changed_p)
5f5c8ee5 12307 {
23e8bd86
GM
12308 /* Is it always sure that the display agrees with lines in
12309 the current matrix? I don't think so, so we mark rows
12310 displayed invalid in the current matrix by setting their
12311 enabled_p flag to zero. */
12312 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12313 if (display_line (&it))
12314 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
12315 }
12316 }
12adba34 12317
5f5c8ee5
GM
12318 /* Update window_end_pos and window_end_vpos. */
12319 if (first_unchanged_at_end_row
12320 && first_unchanged_at_end_row->y < it.last_visible_y
12321 && !last_text_row_at_end)
12322 {
12323 /* Window end line if one of the preserved rows from the current
12324 matrix. Set row to the last row displaying text in current
12325 matrix starting at first_unchanged_at_end_row, after
12326 scrolling. */
12327 xassert (first_unchanged_at_end_row->displays_text_p);
12328 row = find_last_row_displaying_text (w->current_matrix, &it,
12329 first_unchanged_at_end_row);
12330 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 12331
ac90c44f 12332 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 12333 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
12334 w->window_end_vpos
12335 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 12336 xassert (w->window_end_bytepos >= 0);
0416532f 12337 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
12338 }
12339 else if (last_text_row_at_end)
12340 {
ac90c44f
GM
12341 w->window_end_pos
12342 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
12343 w->window_end_bytepos
12344 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
12345 w->window_end_vpos
12346 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 12347 xassert (w->window_end_bytepos >= 0);
0416532f 12348 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
12349 }
12350 else if (last_text_row)
12351 {
12352 /* We have displayed either to the end of the window or at the
12353 end of the window, i.e. the last row with text is to be found
12354 in the desired matrix. */
ac90c44f
GM
12355 w->window_end_pos
12356 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
12357 w->window_end_bytepos
12358 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12359 w->window_end_vpos
12360 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 12361 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
12362 }
12363 else if (first_unchanged_at_end_row == NULL
12364 && last_text_row == NULL
12365 && last_text_row_at_end == NULL)
12366 {
12367 /* Displayed to end of window, but no line containing text was
12368 displayed. Lines were deleted at the end of the window. */
0416532f
GM
12369 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12370 int vpos = XFASTINT (w->window_end_vpos);
12371 struct glyph_row *current_row = current_matrix->rows + vpos;
12372 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12373
12374 for (row = NULL;
12375 row == NULL && vpos >= first_vpos;
12376 --vpos, --current_row, --desired_row)
12377 {
12378 if (desired_row->enabled_p)
12379 {
12380 if (desired_row->displays_text_p)
12381 row = desired_row;
12382 }
12383 else if (current_row->displays_text_p)
12384 row = current_row;
12385 }
12adba34 12386
0416532f 12387 xassert (row != NULL);
d88a79d4 12388 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
12389 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12390 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 12391 xassert (w->window_end_bytepos >= 0);
0416532f 12392 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
12393 }
12394 else
12395 abort ();
ac90c44f 12396
8cdb267e
GM
12397#if 0 /* This leads to problems, for instance when the cursor is
12398 at ZV, and the cursor line displays no text. */
ac90c44f
GM
12399 /* Disable rows below what's displayed in the window. This makes
12400 debugging easier. */
12401 enable_glyph_matrix_rows (current_matrix,
12402 XFASTINT (w->window_end_vpos) + 1,
12403 bottom_vpos, 0);
8cdb267e 12404#endif
23e8bd86 12405
5f5c8ee5
GM
12406 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12407 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 12408
5f5c8ee5
GM
12409 /* Record that display has not been completed. */
12410 w->window_end_valid = Qnil;
12411 w->desired_matrix->no_scrolling_p = 1;
ef121659 12412 return 3;
27d16f05
GM
12413
12414#undef GIVE_UP
12adba34 12415}
0f9c0ff0 12416
a2889657 12417
5f5c8ee5
GM
12418\f
12419/***********************************************************************
12420 More debugging support
12421 ***********************************************************************/
a2889657 12422
5f5c8ee5 12423#if GLYPH_DEBUG
a2889657 12424
eaaa65b0 12425void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 12426void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 12427void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 12428
31b24551 12429
9ab436b9 12430/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 12431
9ab436b9
GM
12432 GLYPHS 0 means don't show glyph contents.
12433 GLYPHS 1 means show glyphs in short form
12434 GLYPHS > 1 means show glyphs in long form. */
12435
12436void
12437dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 12438 struct glyph_matrix *matrix;
9ab436b9 12439 int glyphs;
5f5c8ee5 12440{
efc63ef0 12441 int i;
5f5c8ee5 12442 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 12443 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 12444}
31b24551 12445
68a37fa8 12446
e187cf71
GM
12447/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12448 the glyph row and area where the glyph comes from. */
12449
12450void
12451dump_glyph (row, glyph, area)
12452 struct glyph_row *row;
12453 struct glyph *glyph;
12454 int area;
12455{
12456 if (glyph->type == CHAR_GLYPH)
12457 {
12458 fprintf (stderr,
12459 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12460 glyph - row->glyphs[TEXT_AREA],
12461 'C',
12462 glyph->charpos,
12463 (BUFFERP (glyph->object)
12464 ? 'B'
12465 : (STRINGP (glyph->object)
12466 ? 'S'
12467 : '-')),
12468 glyph->pixel_width,
12469 glyph->u.ch,
12470 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12471 ? glyph->u.ch
12472 : '.'),
12473 glyph->face_id,
12474 glyph->left_box_line_p,
12475 glyph->right_box_line_p);
12476 }
12477 else if (glyph->type == STRETCH_GLYPH)
12478 {
12479 fprintf (stderr,
12480 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12481 glyph - row->glyphs[TEXT_AREA],
12482 'S',
12483 glyph->charpos,
12484 (BUFFERP (glyph->object)
12485 ? 'B'
12486 : (STRINGP (glyph->object)
12487 ? 'S'
12488 : '-')),
12489 glyph->pixel_width,
12490 0,
12491 '.',
12492 glyph->face_id,
12493 glyph->left_box_line_p,
12494 glyph->right_box_line_p);
12495 }
12496 else if (glyph->type == IMAGE_GLYPH)
12497 {
12498 fprintf (stderr,
12499 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12500 glyph - row->glyphs[TEXT_AREA],
12501 'I',
12502 glyph->charpos,
12503 (BUFFERP (glyph->object)
12504 ? 'B'
12505 : (STRINGP (glyph->object)
12506 ? 'S'
12507 : '-')),
12508 glyph->pixel_width,
12509 glyph->u.img_id,
12510 '.',
12511 glyph->face_id,
12512 glyph->left_box_line_p,
12513 glyph->right_box_line_p);
12514 }
12515}
12516
12517
5f5c8ee5 12518/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
12519 GLYPHS 0 means don't show glyph contents.
12520 GLYPHS 1 means show glyphs in short form
12521 GLYPHS > 1 means show glyphs in long form. */
a2889657 12522
5f5c8ee5 12523void
eaaa65b0
GM
12524dump_glyph_row (row, vpos, glyphs)
12525 struct glyph_row *row;
9ab436b9 12526 int vpos, glyphs;
5f5c8ee5 12527{
9ab436b9
GM
12528 if (glyphs != 1)
12529 {
b94c0d9c 12530 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 12531 fprintf (stderr, "=======================================================================\n");
2311178e 12532
0ad38729 12533 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 12534%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 12535 vpos,
9ab436b9
GM
12536 MATRIX_ROW_START_CHARPOS (row),
12537 MATRIX_ROW_END_CHARPOS (row),
12538 row->used[TEXT_AREA],
12539 row->contains_overlapping_glyphs_p,
12540 row->enabled_p,
9ab436b9
GM
12541 row->truncated_on_left_p,
12542 row->truncated_on_right_p,
12543 row->overlay_arrow_p,
12544 row->continued_p,
12545 MATRIX_ROW_CONTINUATION_LINE_P (row),
12546 row->displays_text_p,
12547 row->ends_at_zv_p,
12548 row->fill_line_p,
12549 row->ends_in_middle_of_char_p,
12550 row->starts_in_middle_of_char_p,
b94c0d9c 12551 row->mouse_face_p,
9ab436b9
GM
12552 row->x,
12553 row->y,
12554 row->pixel_width,
12555 row->height,
12556 row->visible_height,
12557 row->ascent,
12558 row->phys_ascent);
33ea6c4d
GM
12559 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12560 row->end.overlay_string_index,
12561 row->continuation_lines_width);
9ab436b9
GM
12562 fprintf (stderr, "%9d %5d\n",
12563 CHARPOS (row->start.string_pos),
12564 CHARPOS (row->end.string_pos));
12565 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12566 row->end.dpvec_index);
12567 }
2311178e 12568
9ab436b9 12569 if (glyphs > 1)
bd66d1ba 12570 {
e187cf71
GM
12571 int area;
12572
12573 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12574 {
091f8878
GM
12575 struct glyph *glyph = row->glyphs[area];
12576 struct glyph *glyph_end = glyph + row->used[area];
2311178e 12577
e187cf71 12578 /* Glyph for a line end in text. */
091f8878 12579 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 12580 ++glyph_end;
2311178e 12581
e187cf71
GM
12582 if (glyph < glyph_end)
12583 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 12584
e187cf71
GM
12585 for (; glyph < glyph_end; ++glyph)
12586 dump_glyph (row, glyph, area);
f7b4b63a 12587 }
f4faa47c 12588 }
9ab436b9
GM
12589 else if (glyphs == 1)
12590 {
e187cf71 12591 int area;
9ab436b9 12592
e187cf71 12593 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 12594 {
e187cf71
GM
12595 char *s = (char *) alloca (row->used[area] + 1);
12596 int i;
12597
12598 for (i = 0; i < row->used[area]; ++i)
12599 {
12600 struct glyph *glyph = row->glyphs[area] + i;
12601 if (glyph->type == CHAR_GLYPH
12602 && glyph->u.ch < 0x80
12603 && glyph->u.ch >= ' ')
12604 s[i] = glyph->u.ch;
12605 else
12606 s[i] = '.';
12607 }
2311178e 12608
e187cf71
GM
12609 s[i] = '\0';
12610 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12611 }
9ab436b9 12612 }
5f5c8ee5 12613}
f4faa47c 12614
a2889657 12615
5f5c8ee5
GM
12616DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12617 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 12618 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
12619Shows contents of glyph row structures. With non-nil
12620parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
12621glyphs in short form, otherwise show glyphs in long form. */)
12622 (glyphs)
9ab436b9 12623 Lisp_Object glyphs;
5f5c8ee5
GM
12624{
12625 struct window *w = XWINDOW (selected_window);
12626 struct buffer *buffer = XBUFFER (w->buffer);
12627
12628 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12629 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12630 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12631 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12632 fprintf (stderr, "=============================================\n");
9ab436b9
GM
12633 dump_glyph_matrix (w->current_matrix,
12634 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
12635 return Qnil;
12636}
1c2250c2 12637
1fca3fae 12638
7d4cc828
GM
12639DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12640 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12641 ()
12642{
12643 struct frame *f = XFRAME (selected_frame);
12644 dump_glyph_matrix (f->current_matrix, 1);
12645 return Qnil;
12646}
12647
12648
e9abc296 12649DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 12650 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
12651GLYPH 0 means don't dump glyphs.
12652GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12653GLYPH > 1 or omitted means dump glyphs in long form. */)
12654 (row, glyphs)
e9abc296 12655 Lisp_Object row, glyphs;
5f5c8ee5 12656{
eaaa65b0
GM
12657 struct glyph_matrix *matrix;
12658 int vpos;
2311178e 12659
b7826503 12660 CHECK_NUMBER (row);
eaaa65b0
GM
12661 matrix = XWINDOW (selected_window)->current_matrix;
12662 vpos = XINT (row);
12663 if (vpos >= 0 && vpos < matrix->nrows)
12664 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12665 vpos,
12666 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12667 return Qnil;
12668}
1fca3fae 12669
67481ae5 12670
b94c0d9c 12671DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 12672 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
12673GLYPH 0 means don't dump glyphs.
12674GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12675GLYPH > 1 or omitted means dump glyphs in long form. */)
12676 (row, glyphs)
b94c0d9c 12677 Lisp_Object row, glyphs;
5f5c8ee5 12678{
886bd6f2 12679 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
12680 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12681 int vpos;
2311178e 12682
b7826503 12683 CHECK_NUMBER (row);
eaaa65b0
GM
12684 vpos = XINT (row);
12685 if (vpos >= 0 && vpos < m->nrows)
12686 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12687 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12688 return Qnil;
12689}
ca26e1c8 12690
0f9c0ff0 12691
62397849 12692DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
12693 doc: /* Toggle tracing of redisplay.
12694With ARG, turn tracing on if and only if ARG is positive. */)
12695 (arg)
62397849 12696 Lisp_Object arg;
5f5c8ee5 12697{
62397849
GM
12698 if (NILP (arg))
12699 trace_redisplay_p = !trace_redisplay_p;
12700 else
12701 {
12702 arg = Fprefix_numeric_value (arg);
12703 trace_redisplay_p = XINT (arg) > 0;
12704 }
2311178e 12705
5f5c8ee5
GM
12706 return Qnil;
12707}
bf9249e3
GM
12708
12709
f4a374a1 12710DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
12711 doc: /* Like `format', but print result to stderr.
12712usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 12713 (nargs, args)
f4a374a1
GM
12714 int nargs;
12715 Lisp_Object *args;
bf9249e3 12716{
f4a374a1 12717 Lisp_Object s = Fformat (nargs, args);
2051c264 12718 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
12719 return Qnil;
12720}
2311178e 12721
5f5c8ee5 12722#endif /* GLYPH_DEBUG */
ca26e1c8 12723
ca26e1c8 12724
5f5c8ee5
GM
12725\f
12726/***********************************************************************
12727 Building Desired Matrix Rows
12728 ***********************************************************************/
a2889657 12729
5f5c8ee5
GM
12730/* Return a temporary glyph row holding the glyphs of an overlay
12731 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 12732
5f5c8ee5
GM
12733static struct glyph_row *
12734get_overlay_arrow_glyph_row (w)
12735 struct window *w;
12736{
12737 struct frame *f = XFRAME (WINDOW_FRAME (w));
12738 struct buffer *buffer = XBUFFER (w->buffer);
12739 struct buffer *old = current_buffer;
50f80c2f 12740 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
2051c264 12741 int arrow_len = SCHARS (Voverlay_arrow_string);
50f80c2f
KR
12742 const unsigned char *arrow_end = arrow_string + arrow_len;
12743 const unsigned char *p;
5f5c8ee5
GM
12744 struct it it;
12745 int multibyte_p;
12746 int n_glyphs_before;
12747
12748 set_buffer_temp (buffer);
12749 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12750 it.glyph_row->used[TEXT_AREA] = 0;
12751 SET_TEXT_POS (it.position, 0, 0);
12752
12753 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12754 p = arrow_string;
12755 while (p < arrow_end)
12756 {
12757 Lisp_Object face, ilisp;
2311178e 12758
5f5c8ee5
GM
12759 /* Get the next character. */
12760 if (multibyte_p)
4fdb80f2 12761 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
12762 else
12763 it.c = *p, it.len = 1;
12764 p += it.len;
2311178e 12765
5f5c8ee5 12766 /* Get its face. */
ac90c44f 12767 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
12768 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12769 it.face_id = compute_char_face (f, it.c, face);
12770
12771 /* Compute its width, get its glyphs. */
12772 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 12773 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
12774 PRODUCE_GLYPHS (&it);
12775
12776 /* If this character doesn't fit any more in the line, we have
12777 to remove some glyphs. */
12778 if (it.current_x > it.last_visible_x)
12779 {
12780 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12781 break;
12782 }
12783 }
2311178e 12784
5f5c8ee5
GM
12785 set_buffer_temp (old);
12786 return it.glyph_row;
12787}
ca26e1c8 12788
b0a0fbda 12789
5f5c8ee5
GM
12790/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12791 glyphs are only inserted for terminal frames since we can't really
12792 win with truncation glyphs when partially visible glyphs are
12793 involved. Which glyphs to insert is determined by
12794 produce_special_glyphs. */
67481ae5 12795
5f5c8ee5
GM
12796static void
12797insert_left_trunc_glyphs (it)
12798 struct it *it;
12799{
12800 struct it truncate_it;
12801 struct glyph *from, *end, *to, *toend;
12802
12803 xassert (!FRAME_WINDOW_P (it->f));
12804
12805 /* Get the truncation glyphs. */
12806 truncate_it = *it;
5f5c8ee5
GM
12807 truncate_it.current_x = 0;
12808 truncate_it.face_id = DEFAULT_FACE_ID;
12809 truncate_it.glyph_row = &scratch_glyph_row;
12810 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12811 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 12812 truncate_it.object = make_number (0);
5f5c8ee5 12813 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 12814
5f5c8ee5
GM
12815 /* Overwrite glyphs from IT with truncation glyphs. */
12816 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12817 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12818 to = it->glyph_row->glyphs[TEXT_AREA];
12819 toend = to + it->glyph_row->used[TEXT_AREA];
12820
12821 while (from < end)
12822 *to++ = *from++;
12823
37be86f2
KH
12824 /* There may be padding glyphs left over. Overwrite them too. */
12825 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12826 {
12827 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12828 while (from < end)
12829 *to++ = *from++;
12830 }
5f5c8ee5 12831
37be86f2
KH
12832 if (to > toend)
12833 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 12834}
e0bfbde6 12835
e0bfbde6 12836
5f5c8ee5 12837/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 12838
5f5c8ee5
GM
12839 Most of the time, ascent and height of a display line will be equal
12840 to the max_ascent and max_height values of the display iterator
12841 structure. This is not the case if
67481ae5 12842
5f5c8ee5
GM
12843 1. We hit ZV without displaying anything. In this case, max_ascent
12844 and max_height will be zero.
1c9241f5 12845
5f5c8ee5
GM
12846 2. We have some glyphs that don't contribute to the line height.
12847 (The glyph row flag contributes_to_line_height_p is for future
12848 pixmap extensions).
f6fd109b 12849
5f5c8ee5
GM
12850 The first case is easily covered by using default values because in
12851 these cases, the line height does not really matter, except that it
12852 must not be zero. */
67481ae5 12853
5f5c8ee5
GM
12854static void
12855compute_line_metrics (it)
12856 struct it *it;
12857{
12858 struct glyph_row *row = it->glyph_row;
12859 int area, i;
1c2250c2 12860
5f5c8ee5
GM
12861 if (FRAME_WINDOW_P (it->f))
12862 {
75c5350a 12863 int i, min_y, max_y;
1c2250c2 12864
5f5c8ee5
GM
12865 /* The line may consist of one space only, that was added to
12866 place the cursor on it. If so, the row's height hasn't been
12867 computed yet. */
12868 if (row->height == 0)
12869 {
12870 if (it->max_ascent + it->max_descent == 0)
312246d1 12871 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
12872 row->ascent = it->max_ascent;
12873 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12874 row->phys_ascent = it->max_phys_ascent;
12875 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5 12876 }
2311178e 12877
5f5c8ee5
GM
12878 /* Compute the width of this line. */
12879 row->pixel_width = row->x;
12880 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12881 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12882
12883 xassert (row->pixel_width >= 0);
12884 xassert (row->ascent >= 0 && row->height > 0);
12885
312246d1
GM
12886 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12887 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12888
12889 /* If first line's physical ascent is larger than its logical
12890 ascent, use the physical ascent, and make the row taller.
12891 This makes accented characters fully visible. */
b28cb6ed 12892 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
12893 && row->phys_ascent > row->ascent)
12894 {
12895 row->height += row->phys_ascent - row->ascent;
12896 row->ascent = row->phys_ascent;
12897 }
12898
5f5c8ee5
GM
12899 /* Compute how much of the line is visible. */
12900 row->visible_height = row->height;
2311178e 12901
75c5350a
GM
12902 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12903 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12904
12905 if (row->y < min_y)
12906 row->visible_height -= min_y - row->y;
12907 if (row->y + row->height > max_y)
12908 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
12909 }
12910 else
12911 {
12912 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
12913 if (row->continued_p)
12914 row->pixel_width -= it->continuation_pixel_width;
12915 else if (row->truncated_on_right_p)
12916 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
12917 row->ascent = row->phys_ascent = 0;
12918 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 12919 }
67481ae5 12920
5f5c8ee5
GM
12921 /* Compute a hash code for this row. */
12922 row->hash = 0;
12923 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12924 for (i = 0; i < row->used[area]; ++i)
12925 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12926 + row->glyphs[area][i].u.val
43d120d8
KH
12927 + row->glyphs[area][i].face_id
12928 + row->glyphs[area][i].padding_p
5f5c8ee5 12929 + (row->glyphs[area][i].type << 2));
a2889657 12930
5f5c8ee5 12931 it->max_ascent = it->max_descent = 0;
312246d1 12932 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 12933}
12adba34 12934
ca26e1c8 12935
5f5c8ee5
GM
12936/* Append one space to the glyph row of iterator IT if doing a
12937 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12938 space have the default face, otherwise let it have the same face as
80c6cb1f 12939 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
12940
12941 This function is called to make sure that there is always one glyph
12942 at the end of a glyph row that the cursor can be set on under
12943 window-systems. (If there weren't such a glyph we would not know
12944 how wide and tall a box cursor should be displayed).
12945
12946 At the same time this space let's a nicely handle clearing to the
12947 end of the line if the row ends in italic text. */
ca26e1c8 12948
80c6cb1f 12949static int
5f5c8ee5
GM
12950append_space (it, default_face_p)
12951 struct it *it;
12952 int default_face_p;
12953{
12954 if (FRAME_WINDOW_P (it->f))
12955 {
12956 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 12957
5f5c8ee5
GM
12958 if (it->glyph_row->glyphs[TEXT_AREA] + n
12959 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 12960 {
cafafe0b
GM
12961 /* Save some values that must not be changed.
12962 Must save IT->c and IT->len because otherwise
12963 ITERATOR_AT_END_P wouldn't work anymore after
12964 append_space has been called. */
478d746b 12965 enum display_element_type saved_what = it->what;
cafafe0b
GM
12966 int saved_c = it->c, saved_len = it->len;
12967 int saved_x = it->current_x;
5f5c8ee5 12968 int saved_face_id = it->face_id;
cafafe0b 12969 struct text_pos saved_pos;
5f5c8ee5 12970 Lisp_Object saved_object;
980806b6 12971 struct face *face;
5f5c8ee5
GM
12972
12973 saved_object = it->object;
12974 saved_pos = it->position;
2311178e 12975
5f5c8ee5
GM
12976 it->what = IT_CHARACTER;
12977 bzero (&it->position, sizeof it->position);
6fc556fd 12978 it->object = make_number (0);
5f5c8ee5
GM
12979 it->c = ' ';
12980 it->len = 1;
5f5c8ee5
GM
12981
12982 if (default_face_p)
12983 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
12984 else if (it->face_before_selective_p)
12985 it->face_id = it->saved_face_id;
980806b6
KH
12986 face = FACE_FROM_ID (it->f, it->face_id);
12987 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 12988
5f5c8ee5 12989 PRODUCE_GLYPHS (it);
2311178e 12990
5f5c8ee5
GM
12991 it->current_x = saved_x;
12992 it->object = saved_object;
12993 it->position = saved_pos;
12994 it->what = saved_what;
12995 it->face_id = saved_face_id;
cafafe0b
GM
12996 it->len = saved_len;
12997 it->c = saved_c;
80c6cb1f 12998 return 1;
5f5c8ee5
GM
12999 }
13000 }
80c6cb1f
GM
13001
13002 return 0;
5f5c8ee5 13003}
12adba34 13004
1842fc1a 13005
5f5c8ee5
GM
13006/* Extend the face of the last glyph in the text area of IT->glyph_row
13007 to the end of the display line. Called from display_line.
13008 If the glyph row is empty, add a space glyph to it so that we
13009 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 13010
5f5c8ee5
GM
13011static void
13012extend_face_to_end_of_line (it)
13013 struct it *it;
13014{
13015 struct face *face;
13016 struct frame *f = it->f;
1842fc1a 13017
5f5c8ee5
GM
13018 /* If line is already filled, do nothing. */
13019 if (it->current_x >= it->last_visible_x)
13020 return;
2311178e 13021
5f5c8ee5
GM
13022 /* Face extension extends the background and box of IT->face_id
13023 to the end of the line. If the background equals the background
4aad61f8
GM
13024 of the frame, we don't have to do anything. */
13025 if (it->face_before_selective_p)
13026 face = FACE_FROM_ID (it->f, it->saved_face_id);
13027 else
13028 face = FACE_FROM_ID (f, it->face_id);
2311178e 13029
5f5c8ee5
GM
13030 if (FRAME_WINDOW_P (f)
13031 && face->box == FACE_NO_BOX
13032 && face->background == FRAME_BACKGROUND_PIXEL (f)
13033 && !face->stipple)
13034 return;
1842fc1a 13035
5f5c8ee5
GM
13036 /* Set the glyph row flag indicating that the face of the last glyph
13037 in the text area has to be drawn to the end of the text area. */
13038 it->glyph_row->fill_line_p = 1;
545e04f6 13039
980806b6
KH
13040 /* If current character of IT is not ASCII, make sure we have the
13041 ASCII face. This will be automatically undone the next time
13042 get_next_display_element returns a multibyte character. Note
13043 that the character will always be single byte in unibyte text. */
13044 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 13045 {
980806b6 13046 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 13047 }
545e04f6 13048
5f5c8ee5
GM
13049 if (FRAME_WINDOW_P (f))
13050 {
13051 /* If the row is empty, add a space with the current face of IT,
13052 so that we know which face to draw. */
13053 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 13054 {
5f5c8ee5 13055 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 13056 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 13057 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 13058 }
5f5c8ee5
GM
13059 }
13060 else
13061 {
13062 /* Save some values that must not be changed. */
13063 int saved_x = it->current_x;
13064 struct text_pos saved_pos;
13065 Lisp_Object saved_object;
478d746b 13066 enum display_element_type saved_what = it->what;
4aad61f8 13067 int saved_face_id = it->face_id;
5f5c8ee5
GM
13068
13069 saved_object = it->object;
13070 saved_pos = it->position;
2311178e 13071
5f5c8ee5
GM
13072 it->what = IT_CHARACTER;
13073 bzero (&it->position, sizeof it->position);
6fc556fd 13074 it->object = make_number (0);
5f5c8ee5
GM
13075 it->c = ' ';
13076 it->len = 1;
4aad61f8 13077 it->face_id = face->id;
2311178e 13078
5f5c8ee5 13079 PRODUCE_GLYPHS (it);
2311178e 13080
5f5c8ee5
GM
13081 while (it->current_x <= it->last_visible_x)
13082 PRODUCE_GLYPHS (it);
2311178e 13083
5f5c8ee5
GM
13084 /* Don't count these blanks really. It would let us insert a left
13085 truncation glyph below and make us set the cursor on them, maybe. */
13086 it->current_x = saved_x;
13087 it->object = saved_object;
13088 it->position = saved_pos;
13089 it->what = saved_what;
4aad61f8 13090 it->face_id = saved_face_id;
5f5c8ee5
GM
13091 }
13092}
12adba34 13093
545e04f6 13094
5f5c8ee5
GM
13095/* Value is non-zero if text starting at CHARPOS in current_buffer is
13096 trailing whitespace. */
1c9241f5 13097
5f5c8ee5
GM
13098static int
13099trailing_whitespace_p (charpos)
13100 int charpos;
13101{
13102 int bytepos = CHAR_TO_BYTE (charpos);
13103 int c = 0;
7bbe686f 13104
5f5c8ee5
GM
13105 while (bytepos < ZV_BYTE
13106 && (c = FETCH_CHAR (bytepos),
13107 c == ' ' || c == '\t'))
13108 ++bytepos;
0d09d1e6 13109
8f897821
GM
13110 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
13111 {
13112 if (bytepos != PT_BYTE)
13113 return 1;
13114 }
13115 return 0;
5f5c8ee5 13116}
31b24551 13117
545e04f6 13118
5f5c8ee5 13119/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 13120
5f5c8ee5
GM
13121void
13122highlight_trailing_whitespace (f, row)
13123 struct frame *f;
13124 struct glyph_row *row;
13125{
13126 int used = row->used[TEXT_AREA];
2311178e 13127
5f5c8ee5
GM
13128 if (used)
13129 {
13130 struct glyph *start = row->glyphs[TEXT_AREA];
13131 struct glyph *glyph = start + used - 1;
13132
ade0bee1
GM
13133 /* Skip over glyphs inserted to display the cursor at the
13134 end of a line, for extending the face of the last glyph
13135 to the end of the line on terminals, and for truncation
13136 and continuation glyphs. */
65008712
GM
13137 while (glyph >= start
13138 && glyph->type == CHAR_GLYPH
65008712 13139 && INTEGERP (glyph->object))
5f5c8ee5
GM
13140 --glyph;
13141
13142 /* If last glyph is a space or stretch, and it's trailing
13143 whitespace, set the face of all trailing whitespace glyphs in
13144 IT->glyph_row to `trailing-whitespace'. */
13145 if (glyph >= start
13146 && BUFFERP (glyph->object)
13147 && (glyph->type == STRETCH_GLYPH
13148 || (glyph->type == CHAR_GLYPH
43d120d8 13149 && glyph->u.ch == ' '))
5f5c8ee5 13150 && trailing_whitespace_p (glyph->charpos))
545e04f6 13151 {
980806b6 13152 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
2311178e 13153
5f5c8ee5
GM
13154 while (glyph >= start
13155 && BUFFERP (glyph->object)
13156 && (glyph->type == STRETCH_GLYPH
13157 || (glyph->type == CHAR_GLYPH
43d120d8
KH
13158 && glyph->u.ch == ' ')))
13159 (glyph--)->face_id = face_id;
545e04f6 13160 }
a2889657 13161 }
5f5c8ee5 13162}
a2889657 13163
5fcbb24d 13164
cafafe0b 13165/* Value is non-zero if glyph row ROW in window W should be
0fd37545 13166 used to hold the cursor. */
cafafe0b
GM
13167
13168static int
13169cursor_row_p (w, row)
13170 struct window *w;
13171 struct glyph_row *row;
13172{
9a038881 13173 int cursor_row_p = 1;
2311178e 13174
cafafe0b
GM
13175 if (PT == MATRIX_ROW_END_CHARPOS (row))
13176 {
9a038881
GM
13177 /* If the row ends with a newline from a string, we don't want
13178 the cursor there (if the row is continued it doesn't end in a
13179 newline). */
13180 if (CHARPOS (row->end.string_pos) >= 0
13181 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13182 cursor_row_p = row->continued_p;
13183
13184 /* If the row ends at ZV, display the cursor at the end of that
13185 row instead of at the start of the row below. */
13186 else if (row->ends_at_zv_p)
13187 cursor_row_p = 1;
13188 else
13189 cursor_row_p = 0;
cafafe0b
GM
13190 }
13191
9a038881 13192 return cursor_row_p;
cafafe0b
GM
13193}
13194
13195
5f5c8ee5
GM
13196/* Construct the glyph row IT->glyph_row in the desired matrix of
13197 IT->w from text at the current position of IT. See dispextern.h
13198 for an overview of struct it. Value is non-zero if
13199 IT->glyph_row displays text, as opposed to a line displaying ZV
13200 only. */
2311178e 13201
5f5c8ee5
GM
13202static int
13203display_line (it)
13204 struct it *it;
13205{
13206 struct glyph_row *row = it->glyph_row;
13207
13208 /* We always start displaying at hpos zero even if hscrolled. */
13209 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 13210
5f5c8ee5
GM
13211 /* We must not display in a row that's not a text row. */
13212 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13213 < it->w->desired_matrix->nrows);
12adba34 13214
5f5c8ee5
GM
13215 /* Is IT->w showing the region? */
13216 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 13217
5f5c8ee5
GM
13218 /* Clear the result glyph row and enable it. */
13219 prepare_desired_row (row);
12adba34 13220
5f5c8ee5
GM
13221 row->y = it->current_y;
13222 row->start = it->current;
13223 row->continuation_lines_width = it->continuation_lines_width;
13224 row->displays_text_p = 1;
91004049
GM
13225 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13226 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
13227
13228 /* Arrange the overlays nicely for our purposes. Usually, we call
13229 display_line on only one line at a time, in which case this
13230 can't really hurt too much, or we call it on lines which appear
13231 one after another in the buffer, in which case all calls to
13232 recenter_overlay_lists but the first will be pretty cheap. */
13233 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13234
5f5c8ee5
GM
13235 /* Move over display elements that are not visible because we are
13236 hscrolled. This may stop at an x-position < IT->first_visible_x
13237 if the first glyph is partially visible or if we hit a line end. */
13238 if (it->current_x < it->first_visible_x)
13239 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13240 MOVE_TO_POS | MOVE_TO_X);
13241
13242 /* Get the initial row height. This is either the height of the
13243 text hscrolled, if there is any, or zero. */
13244 row->ascent = it->max_ascent;
13245 row->height = it->max_ascent + it->max_descent;
312246d1
GM
13246 row->phys_ascent = it->max_phys_ascent;
13247 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
13248
13249 /* Loop generating characters. The loop is left with IT on the next
13250 character to display. */
13251 while (1)
13252 {
13253 int n_glyphs_before, hpos_before, x_before;
13254 int x, i, nglyphs;
ae26e27d 13255 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 13256
5f5c8ee5
GM
13257 /* Retrieve the next thing to display. Value is zero if end of
13258 buffer reached. */
13259 if (!get_next_display_element (it))
13260 {
13261 /* Maybe add a space at the end of this line that is used to
1b335865
GM
13262 display the cursor there under X. Set the charpos of the
13263 first glyph of blank lines not corresponding to any text
13264 to -1. */
13265 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13266 || row->used[TEXT_AREA] == 0)
a2889657 13267 {
5f5c8ee5
GM
13268 row->glyphs[TEXT_AREA]->charpos = -1;
13269 row->displays_text_p = 0;
13270
2ad551af 13271 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
13272 && (!MINI_WINDOW_P (it->w)
13273 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 13274 row->indicate_empty_line_p = 1;
a2889657 13275 }
2311178e 13276
5f5c8ee5
GM
13277 it->continuation_lines_width = 0;
13278 row->ends_at_zv_p = 1;
13279 break;
a2889657 13280 }
a2889657 13281
5f5c8ee5
GM
13282 /* Now, get the metrics of what we want to display. This also
13283 generates glyphs in `row' (which is IT->glyph_row). */
13284 n_glyphs_before = row->used[TEXT_AREA];
13285 x = it->current_x;
e6819faf
GM
13286
13287 /* Remember the line height so far in case the next element doesn't
13288 fit on the line. */
13289 if (!it->truncate_lines_p)
13290 {
13291 ascent = it->max_ascent;
13292 descent = it->max_descent;
13293 phys_ascent = it->max_phys_ascent;
13294 phys_descent = it->max_phys_descent;
13295 }
2311178e 13296
5f5c8ee5 13297 PRODUCE_GLYPHS (it);
a2889657 13298
5f5c8ee5
GM
13299 /* If this display element was in marginal areas, continue with
13300 the next one. */
13301 if (it->area != TEXT_AREA)
a2889657 13302 {
5f5c8ee5
GM
13303 row->ascent = max (row->ascent, it->max_ascent);
13304 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13305 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13306 row->phys_height = max (row->phys_height,
13307 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 13308 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13309 continue;
13310 }
5936754e 13311
5f5c8ee5
GM
13312 /* Does the display element fit on the line? If we truncate
13313 lines, we should draw past the right edge of the window. If
13314 we don't truncate, we want to stop so that we can display the
13315 continuation glyph before the right margin. If lines are
13316 continued, there are two possible strategies for characters
13317 resulting in more than 1 glyph (e.g. tabs): Display as many
13318 glyphs as possible in this line and leave the rest for the
13319 continuation line, or display the whole element in the next
13320 line. Original redisplay did the former, so we do it also. */
13321 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13322 hpos_before = it->hpos;
13323 x_before = x;
4dcd74e6 13324
4b41cebb 13325 if (/* Not a newline. */
4dcd74e6 13326 nglyphs > 0
202c1b5b 13327 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
13328 && it->current_x < it->last_visible_x)
13329 {
37be86f2 13330 it->hpos += nglyphs;
5f5c8ee5
GM
13331 row->ascent = max (row->ascent, it->max_ascent);
13332 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13333 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13334 row->phys_height = max (row->phys_height,
13335 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13336 if (it->current_x - it->pixel_width < it->first_visible_x)
13337 row->x = x - it->first_visible_x;
13338 }
13339 else
13340 {
13341 int new_x;
13342 struct glyph *glyph;
2311178e 13343
5f5c8ee5 13344 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 13345 {
5f5c8ee5
GM
13346 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13347 new_x = x + glyph->pixel_width;
13348
13349 if (/* Lines are continued. */
13350 !it->truncate_lines_p
13351 && (/* Glyph doesn't fit on the line. */
13352 new_x > it->last_visible_x
13353 /* Or it fits exactly on a window system frame. */
13354 || (new_x == it->last_visible_x
13355 && FRAME_WINDOW_P (it->f))))
a2889657 13356 {
5f5c8ee5 13357 /* End of a continued line. */
2311178e 13358
5f5c8ee5
GM
13359 if (it->hpos == 0
13360 || (new_x == it->last_visible_x
13361 && FRAME_WINDOW_P (it->f)))
13362 {
e6819faf
GM
13363 /* Current glyph is the only one on the line or
13364 fits exactly on the line. We must continue
13365 the line because we can't draw the cursor
13366 after the glyph. */
5f5c8ee5
GM
13367 row->continued_p = 1;
13368 it->current_x = new_x;
13369 it->continuation_lines_width += new_x;
13370 ++it->hpos;
13371 if (i == nglyphs - 1)
cafafe0b 13372 set_iterator_to_next (it, 1);
5f5c8ee5 13373 }
3b3c4bf0
GM
13374 else if (CHAR_GLYPH_PADDING_P (*glyph)
13375 && !FRAME_WINDOW_P (it->f))
13376 {
13377 /* A padding glyph that doesn't fit on this line.
13378 This means the whole character doesn't fit
13379 on the line. */
13380 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 13381
3b3c4bf0
GM
13382 /* Fill the rest of the row with continuation
13383 glyphs like in 20.x. */
13384 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13385 < row->glyphs[1 + TEXT_AREA])
13386 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 13387
3b3c4bf0
GM
13388 row->continued_p = 1;
13389 it->current_x = x_before;
13390 it->continuation_lines_width += x_before;
2311178e 13391
3b3c4bf0
GM
13392 /* Restore the height to what it was before the
13393 element not fitting on the line. */
13394 it->max_ascent = ascent;
13395 it->max_descent = descent;
13396 it->max_phys_ascent = phys_ascent;
13397 it->max_phys_descent = phys_descent;
13398 }
0df56f4d
GM
13399 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13400 {
13401 /* A TAB that extends past the right edge of the
13402 window. This produces a single glyph on
13403 window system frames. We leave the glyph in
13404 this row and let it fill the row, but don't
13405 consume the TAB. */
13406 it->continuation_lines_width += it->last_visible_x;
13407 row->ends_in_middle_of_char_p = 1;
13408 row->continued_p = 1;
13409 glyph->pixel_width = it->last_visible_x - x;
13410 it->starts_in_middle_of_char_p = 1;
13411 }
5f5c8ee5 13412 else
5936754e 13413 {
0df56f4d
GM
13414 /* Something other than a TAB that draws past
13415 the right edge of the window. Restore
13416 positions to values before the element. */
5f5c8ee5 13417 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 13418
5f5c8ee5
GM
13419 /* Display continuation glyphs. */
13420 if (!FRAME_WINDOW_P (it->f))
13421 produce_special_glyphs (it, IT_CONTINUATION);
13422 row->continued_p = 1;
653c329b 13423
0df56f4d 13424 it->continuation_lines_width += x;
2311178e 13425
91004049
GM
13426 if (nglyphs > 1 && i > 0)
13427 {
13428 row->ends_in_middle_of_char_p = 1;
13429 it->starts_in_middle_of_char_p = 1;
13430 }
2311178e 13431
e6819faf
GM
13432 /* Restore the height to what it was before the
13433 element not fitting on the line. */
13434 it->max_ascent = ascent;
13435 it->max_descent = descent;
13436 it->max_phys_ascent = phys_ascent;
13437 it->max_phys_descent = phys_descent;
5936754e 13438 }
e6819faf 13439
5f5c8ee5
GM
13440 break;
13441 }
13442 else if (new_x > it->first_visible_x)
13443 {
13444 /* Increment number of glyphs actually displayed. */
13445 ++it->hpos;
2311178e 13446
5f5c8ee5
GM
13447 if (x < it->first_visible_x)
13448 /* Glyph is partially visible, i.e. row starts at
13449 negative X position. */
13450 row->x = x - it->first_visible_x;
13451 }
13452 else
13453 {
13454 /* Glyph is completely off the left margin of the
13455 window. This should not happen because of the
72b8c434
RS
13456 move_it_in_display_line at the start of this
13457 function, unless the text display area of the
13458 window is empty. */
13459 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 13460 }
a2889657 13461 }
2311178e 13462
5f5c8ee5
GM
13463 row->ascent = max (row->ascent, it->max_ascent);
13464 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13465 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13466 row->phys_height = max (row->phys_height,
13467 it->max_phys_ascent + it->max_phys_descent);
2311178e 13468
5f5c8ee5
GM
13469 /* End of this display line if row is continued. */
13470 if (row->continued_p)
13471 break;
a2889657 13472 }
a2889657 13473
5f5c8ee5
GM
13474 /* Is this a line end? If yes, we're also done, after making
13475 sure that a non-default face is extended up to the right
13476 margin of the window. */
13477 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 13478 {
5f5c8ee5
GM
13479 int used_before = row->used[TEXT_AREA];
13480
e74fb0f7
GM
13481 row->ends_in_newline_from_string_p = STRINGP (it->object);
13482
5f5c8ee5
GM
13483 /* Add a space at the end of the line that is used to
13484 display the cursor there. */
13485 append_space (it, 0);
2311178e 13486
5f5c8ee5
GM
13487 /* Extend the face to the end of the line. */
13488 extend_face_to_end_of_line (it);
13489
13490 /* Make sure we have the position. */
13491 if (used_before == 0)
13492 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 13493
5f5c8ee5 13494 /* Consume the line end. This skips over invisible lines. */
cafafe0b 13495 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13496 it->continuation_lines_width = 0;
13497 break;
1c9241f5 13498 }
a2889657 13499
2311178e 13500 /* Proceed with next display element. Note that this skips
5f5c8ee5 13501 over lines invisible because of selective display. */
cafafe0b 13502 set_iterator_to_next (it, 1);
b1d1124b 13503
5f5c8ee5
GM
13504 /* If we truncate lines, we are done when the last displayed
13505 glyphs reach past the right margin of the window. */
13506 if (it->truncate_lines_p
13507 && (FRAME_WINDOW_P (it->f)
13508 ? (it->current_x >= it->last_visible_x)
13509 : (it->current_x > it->last_visible_x)))
75d13c64 13510 {
5f5c8ee5
GM
13511 /* Maybe add truncation glyphs. */
13512 if (!FRAME_WINDOW_P (it->f))
13513 {
a98b5ed9 13514 int i, n;
2311178e 13515
a98b5ed9
GM
13516 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13517 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13518 break;
13519
13520 for (n = row->used[TEXT_AREA]; i < n; ++i)
13521 {
13522 row->used[TEXT_AREA] = i;
13523 produce_special_glyphs (it, IT_TRUNCATION);
13524 }
5f5c8ee5 13525 }
2311178e 13526
5f5c8ee5
GM
13527 row->truncated_on_right_p = 1;
13528 it->continuation_lines_width = 0;
312246d1 13529 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
13530 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13531 it->hpos = hpos_before;
13532 it->current_x = x_before;
13533 break;
75d13c64 13534 }
a2889657 13535 }
a2889657 13536
5f5c8ee5
GM
13537 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13538 at the left window margin. */
13539 if (it->first_visible_x
13540 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13541 {
13542 if (!FRAME_WINDOW_P (it->f))
13543 insert_left_trunc_glyphs (it);
13544 row->truncated_on_left_p = 1;
13545 }
a2889657 13546
5f5c8ee5
GM
13547 /* If the start of this line is the overlay arrow-position, then
13548 mark this glyph row as the one containing the overlay arrow.
13549 This is clearly a mess with variable size fonts. It would be
13550 better to let it be displayed like cursors under X. */
e24c997d 13551 if (MARKERP (Voverlay_arrow_position)
a2889657 13552 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
13553 && (MATRIX_ROW_START_CHARPOS (row)
13554 == marker_position (Voverlay_arrow_position))
e24c997d 13555 && STRINGP (Voverlay_arrow_string)
a2889657
JB
13556 && ! overlay_arrow_seen)
13557 {
b46952ae 13558 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 13559 if (!FRAME_WINDOW_P (it->f))
c4628384 13560 {
5f5c8ee5
GM
13561 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13562 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13563 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13564 struct glyph *p = row->glyphs[TEXT_AREA];
13565 struct glyph *p2, *end;
13566
13567 /* Copy the arrow glyphs. */
13568 while (glyph < arrow_end)
13569 *p++ = *glyph++;
13570
13571 /* Throw away padding glyphs. */
13572 p2 = p;
13573 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13574 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13575 ++p2;
13576 if (p2 > p)
212e4f87 13577 {
5f5c8ee5
GM
13578 while (p2 < end)
13579 *p++ = *p2++;
13580 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 13581 }
c4628384 13582 }
2311178e 13583
a2889657 13584 overlay_arrow_seen = 1;
5f5c8ee5 13585 row->overlay_arrow_p = 1;
a2889657
JB
13586 }
13587
5f5c8ee5
GM
13588 /* Compute pixel dimensions of this line. */
13589 compute_line_metrics (it);
13590
13591 /* Remember the position at which this line ends. */
13592 row->end = it->current;
13593
173cbca8 13594 /* Maybe set the cursor. */
5f5c8ee5
GM
13595 if (it->w->cursor.vpos < 0
13596 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
13597 && PT <= MATRIX_ROW_END_CHARPOS (row)
13598 && cursor_row_p (it->w, row))
13599 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13600
13601 /* Highlight trailing whitespace. */
8f897821 13602 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
13603 highlight_trailing_whitespace (it->f, it->glyph_row);
13604
13605 /* Prepare for the next line. This line starts horizontally at (X
13606 HPOS) = (0 0). Vertical positions are incremented. As a
13607 convenience for the caller, IT->glyph_row is set to the next
13608 row to be used. */
13609 it->current_x = it->hpos = 0;
13610 it->current_y += row->height;
13611 ++it->vpos;
13612 ++it->glyph_row;
13613 return row->displays_text_p;
a2889657 13614}
5f5c8ee5
GM
13615
13616
a2889657 13617\f
5f5c8ee5
GM
13618/***********************************************************************
13619 Menu Bar
13620 ***********************************************************************/
13621
13622/* Redisplay the menu bar in the frame for window W.
13623
13624 The menu bar of X frames that don't have X toolkit support is
13625 displayed in a special window W->frame->menu_bar_window.
2311178e 13626
5f5c8ee5
GM
13627 The menu bar of terminal frames is treated specially as far as
13628 glyph matrices are concerned. Menu bar lines are not part of
13629 windows, so the update is done directly on the frame matrix rows
13630 for the menu bar. */
7ce2c095
RS
13631
13632static void
13633display_menu_bar (w)
13634 struct window *w;
13635{
5f5c8ee5
GM
13636 struct frame *f = XFRAME (WINDOW_FRAME (w));
13637 struct it it;
13638 Lisp_Object items;
8351baf2 13639 int i;
7ce2c095 13640
5f5c8ee5 13641 /* Don't do all this for graphical frames. */
dc937613 13642#ifdef HAVE_NTGUI
d129c4c2
KH
13643 if (!NILP (Vwindow_system))
13644 return;
dc937613 13645#endif
488dd4c4 13646#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 13647 if (FRAME_X_P (f))
7ce2c095 13648 return;
5f5c8ee5 13649#endif
e0f712ba 13650#ifdef MAC_OS
1a578e9b
AC
13651 if (FRAME_MAC_P (f))
13652 return;
13653#endif
5f5c8ee5
GM
13654
13655#ifdef USE_X_TOOLKIT
13656 xassert (!FRAME_WINDOW_P (f));
52377a47 13657 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
13658 it.first_visible_x = 0;
13659 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13660#else /* not USE_X_TOOLKIT */
13661 if (FRAME_WINDOW_P (f))
13662 {
13663 /* Menu bar lines are displayed in the desired matrix of the
13664 dummy window menu_bar_window. */
13665 struct window *menu_w;
13666 xassert (WINDOWP (f->menu_bar_window));
13667 menu_w = XWINDOW (f->menu_bar_window);
13668 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 13669 MENU_FACE_ID);
5f5c8ee5
GM
13670 it.first_visible_x = 0;
13671 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13672 }
13673 else
13674 {
13675 /* This is a TTY frame, i.e. character hpos/vpos are used as
13676 pixel x/y. */
13677 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 13678 MENU_FACE_ID);
5f5c8ee5
GM
13679 it.first_visible_x = 0;
13680 it.last_visible_x = FRAME_WIDTH (f);
13681 }
13682#endif /* not USE_X_TOOLKIT */
13683
1862a24e
MB
13684 if (! mode_line_inverse_video)
13685 /* Force the menu-bar to be displayed in the default face. */
13686 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13687
5f5c8ee5
GM
13688 /* Clear all rows of the menu bar. */
13689 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13690 {
13691 struct glyph_row *row = it.glyph_row + i;
13692 clear_glyph_row (row);
13693 row->enabled_p = 1;
13694 row->full_width_p = 1;
13695 }
7ce2c095 13696
5f5c8ee5
GM
13697 /* Display all items of the menu bar. */
13698 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 13699 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 13700 {
5f5c8ee5
GM
13701 Lisp_Object string;
13702
13703 /* Stop at nil string. */
a61b7058 13704 string = AREF (items, i + 1);
8351baf2
RS
13705 if (NILP (string))
13706 break;
2d66ad19 13707
5f5c8ee5 13708 /* Remember where item was displayed. */
a61b7058 13709 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 13710
5f5c8ee5
GM
13711 /* Display the item, pad with one space. */
13712 if (it.current_x < it.last_visible_x)
13713 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 13714 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
13715 }
13716
2d66ad19 13717 /* Fill out the line with spaces. */
5f5c8ee5
GM
13718 if (it.current_x < it.last_visible_x)
13719 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 13720
5f5c8ee5
GM
13721 /* Compute the total height of the lines. */
13722 compute_line_metrics (&it);
7ce2c095 13723}
5f5c8ee5
GM
13724
13725
7ce2c095 13726\f
5f5c8ee5
GM
13727/***********************************************************************
13728 Mode Line
13729 ***********************************************************************/
13730
715e84c9
GM
13731/* Redisplay mode lines in the window tree whose root is WINDOW. If
13732 FORCE is non-zero, redisplay mode lines unconditionally.
13733 Otherwise, redisplay only mode lines that are garbaged. Value is
13734 the number of windows whose mode lines were redisplayed. */
a2889657 13735
715e84c9
GM
13736static int
13737redisplay_mode_lines (window, force)
13738 Lisp_Object window;
13739 int force;
13740{
13741 int nwindows = 0;
2311178e 13742
715e84c9
GM
13743 while (!NILP (window))
13744 {
13745 struct window *w = XWINDOW (window);
2311178e 13746
715e84c9
GM
13747 if (WINDOWP (w->hchild))
13748 nwindows += redisplay_mode_lines (w->hchild, force);
13749 else if (WINDOWP (w->vchild))
13750 nwindows += redisplay_mode_lines (w->vchild, force);
13751 else if (force
13752 || FRAME_GARBAGED_P (XFRAME (w->frame))
13753 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13754 {
715e84c9
GM
13755 struct text_pos lpoint;
13756 struct buffer *old = current_buffer;
13757
13758 /* Set the window's buffer for the mode line display. */
13759 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13760 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 13761
715e84c9
GM
13762 /* Point refers normally to the selected window. For any
13763 other window, set up appropriate value. */
13764 if (!EQ (window, selected_window))
13765 {
13766 struct text_pos pt;
2311178e 13767
715e84c9
GM
13768 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13769 if (CHARPOS (pt) < BEGV)
13770 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13771 else if (CHARPOS (pt) > (ZV - 1))
13772 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13773 else
13774 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13775 }
13776
715e84c9
GM
13777 /* Display mode lines. */
13778 clear_glyph_matrix (w->desired_matrix);
13779 if (display_mode_lines (w))
13780 {
13781 ++nwindows;
13782 w->must_be_updated_p = 1;
13783 }
13784
13785 /* Restore old settings. */
715e84c9
GM
13786 set_buffer_internal_1 (old);
13787 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13788 }
13789
13790 window = w->next;
13791 }
13792
13793 return nwindows;
13794}
13795
13796
13797/* Display the mode and/or top line of window W. Value is the number
13798 of mode lines displayed. */
13799
13800static int
5f5c8ee5 13801display_mode_lines (w)
a2889657
JB
13802 struct window *w;
13803{
edd1e654 13804 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 13805 int n = 0;
edd1e654
GM
13806
13807 old_selected_frame = selected_frame;
13808 selected_frame = w->frame;
13809 old_selected_window = selected_window;
13810 XSETWINDOW (selected_window, w);
2311178e 13811
5f5c8ee5 13812 /* These will be set while the mode line specs are processed. */
aa6d10fa 13813 line_number_displayed = 0;
155ef550 13814 w->column_number_displayed = Qnil;
aa6d10fa 13815
5f5c8ee5 13816 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 13817 {
c96e83bf 13818 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 13819
96d2320f 13820 /* Select mode line face based on the real selected window. */
c96e83bf 13821 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
13822 current_buffer->mode_line_format);
13823 ++n;
13824 }
2311178e 13825
045dee35 13826 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
13827 {
13828 display_mode_line (w, HEADER_LINE_FACE_ID,
13829 current_buffer->header_line_format);
13830 ++n;
13831 }
13832
edd1e654
GM
13833 selected_frame = old_selected_frame;
13834 selected_window = old_selected_window;
715e84c9 13835 return n;
5f5c8ee5 13836}
03b294dc 13837
03b294dc 13838
5f5c8ee5 13839/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 13840 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
13841 FORMAT is the mode line format to display. Value is the pixel
13842 height of the mode line displayed. */
03b294dc 13843
04612a64 13844static int
5f5c8ee5
GM
13845display_mode_line (w, face_id, format)
13846 struct window *w;
13847 enum face_id face_id;
13848 Lisp_Object format;
13849{
13850 struct it it;
13851 struct face *face;
03b294dc 13852
5f5c8ee5
GM
13853 init_iterator (&it, w, -1, -1, NULL, face_id);
13854 prepare_desired_row (it.glyph_row);
13855
1862a24e
MB
13856 if (! mode_line_inverse_video)
13857 /* Force the mode-line to be displayed in the default face. */
13858 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13859
5f5c8ee5
GM
13860 /* Temporarily make frame's keyboard the current kboard so that
13861 kboard-local variables in the mode_line_format will get the right
13862 values. */
13863 push_frame_kboard (it.f);
c53a1624 13864 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 13865 pop_frame_kboard ();
a2889657 13866
5f5c8ee5
GM
13867 /* Fill up with spaces. */
13868 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 13869
5f5c8ee5
GM
13870 compute_line_metrics (&it);
13871 it.glyph_row->full_width_p = 1;
13872 it.glyph_row->mode_line_p = 1;
5f5c8ee5
GM
13873 it.glyph_row->continued_p = 0;
13874 it.glyph_row->truncated_on_left_p = 0;
13875 it.glyph_row->truncated_on_right_p = 0;
13876
13877 /* Make a 3D mode-line have a shadow at its right end. */
13878 face = FACE_FROM_ID (it.f, face_id);
13879 extend_face_to_end_of_line (&it);
13880 if (face->box != FACE_NO_BOX)
d7eb09a0 13881 {
5f5c8ee5
GM
13882 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13883 + it.glyph_row->used[TEXT_AREA] - 1);
13884 last->right_box_line_p = 1;
d7eb09a0 13885 }
04612a64
GM
13886
13887 return it.glyph_row->height;
a2889657
JB
13888}
13889
0fcf414f
RS
13890/* Alist that caches the results of :propertize.
13891 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13892Lisp_Object mode_line_proptrans_alist;
a2889657 13893
fec8f23e
KS
13894/* List of strings making up the mode-line. */
13895Lisp_Object mode_line_string_list;
13896
13897/* Base face property when building propertized mode line string. */
13898static Lisp_Object mode_line_string_face;
13899static Lisp_Object mode_line_string_face_prop;
13900
13901
5f5c8ee5
GM
13902/* Contribute ELT to the mode line for window IT->w. How it
13903 translates into text depends on its data type.
a2889657 13904
5f5c8ee5 13905 IT describes the display environment in which we display, as usual.
a2889657
JB
13906
13907 DEPTH is the depth in recursion. It is used to prevent
13908 infinite recursion here.
13909
5f5c8ee5
GM
13910 FIELD_WIDTH is the number of characters the display of ELT should
13911 occupy in the mode line, and PRECISION is the maximum number of
13912 characters to display from ELT's representation. See
b8523839 13913 display_string for details.
a2889657 13914
c53a1624
RS
13915 Returns the hpos of the end of the text generated by ELT.
13916
13917 PROPS is a property list to add to any string we encounter.
13918
7d0393cf 13919 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
13920 we encounter, and ignore :eval and :propertize.
13921
13922 If the global variable `frame_title_ptr' is non-NULL, then the output
13923 is passed to `store_frame_title' instead of `display_string'. */
a2889657
JB
13924
13925static int
c53a1624 13926display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 13927 struct it *it;
a2889657 13928 int depth;
5f5c8ee5 13929 int field_width, precision;
0fcf414f 13930 Lisp_Object elt, props;
c53a1624 13931 int risky;
a2889657 13932{
5f5c8ee5 13933 int n = 0, field, prec;
0fcf414f 13934 int literal = 0;
5f5c8ee5 13935
a2889657
JB
13936 tail_recurse:
13937 if (depth > 10)
13938 goto invalid;
13939
13940 depth++;
13941
0220c518 13942 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
13943 {
13944 case Lisp_String:
13945 {
13946 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 13947 unsigned char c;
50f80c2f 13948 const unsigned char *this, *lisp_string;
5f5c8ee5 13949
c53a1624 13950 if (!NILP (props) || risky)
0fcf414f
RS
13951 {
13952 Lisp_Object oprops, aelt;
13953 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
13954
13955 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 13956 {
ae02e06a
RS
13957 /* If the starting string has properties,
13958 merge the specified ones onto the existing ones. */
c53a1624 13959 if (! NILP (oprops) && !risky)
ae02e06a
RS
13960 {
13961 Lisp_Object tem;
13962
13963 oprops = Fcopy_sequence (oprops);
13964 tem = props;
13965 while (CONSP (tem))
13966 {
13967 oprops = Fplist_put (oprops, XCAR (tem),
13968 XCAR (XCDR (tem)));
13969 tem = XCDR (XCDR (tem));
13970 }
13971 props = oprops;
13972 }
13973
0fcf414f
RS
13974 aelt = Fassoc (elt, mode_line_proptrans_alist);
13975 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
13976 {
13977 mode_line_proptrans_alist
13978 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13979 elt = XCAR (aelt);
13980 }
0fcf414f
RS
13981 else
13982 {
adb63af1
RS
13983 Lisp_Object tem;
13984
0fcf414f 13985 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
13986 Fset_text_properties (make_number (0), Flength (elt),
13987 props, elt);
adb63af1 13988 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
13989 mode_line_proptrans_alist
13990 = Fcons (Fcons (elt, props),
13991 mode_line_proptrans_alist);
adb63af1
RS
13992 /* Truncate mode_line_proptrans_alist
13993 to at most 50 elements. */
13994 tem = Fnthcdr (make_number (50),
13995 mode_line_proptrans_alist);
13996 if (! NILP (tem))
13997 XSETCDR (tem, Qnil);
0fcf414f
RS
13998 }
13999 }
14000 }
14001
2051c264 14002 this = SDATA (elt);
ae02e06a
RS
14003 lisp_string = this;
14004
0fcf414f
RS
14005 if (literal)
14006 {
14007 prec = precision - n;
14008 if (frame_title_ptr)
2051c264 14009 n += store_frame_title (SDATA (elt), -1, prec);
fec8f23e
KS
14010 else if (!NILP (mode_line_string_list))
14011 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
0fcf414f
RS
14012 else
14013 n += display_string (NULL, elt, Qnil, 0, 0, it,
14014 0, prec, 0, STRING_MULTIBYTE (elt));
14015
14016 break;
14017 }
14018
5f5c8ee5
GM
14019 while ((precision <= 0 || n < precision)
14020 && *this
14021 && (frame_title_ptr
fec8f23e 14022 || !NILP (mode_line_string_list)
5f5c8ee5 14023 || it->current_x < it->last_visible_x))
a2889657 14024 {
50f80c2f 14025 const unsigned char *last = this;
5f5c8ee5
GM
14026
14027 /* Advance to end of string or next format specifier. */
a2889657
JB
14028 while ((c = *this++) != '\0' && c != '%')
14029 ;
2311178e 14030
a2889657
JB
14031 if (this - 1 != last)
14032 {
5f5c8ee5
GM
14033 /* Output to end of string or up to '%'. Field width
14034 is length of string. Don't output more than
14035 PRECISION allows us. */
d26b89b8 14036 --this;
eaaa65b0
GM
14037
14038 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
14039 if (precision > 0 && prec > precision - n)
14040 prec = precision - n;
2311178e 14041
d39b6696 14042 if (frame_title_ptr)
d26b89b8 14043 n += store_frame_title (last, 0, prec);
fec8f23e
KS
14044 else if (!NILP (mode_line_string_list))
14045 {
14046 int bytepos = last - lisp_string;
14047 int charpos = string_byte_to_char (elt, bytepos);
14048 n += store_mode_line_string (NULL,
14049 Fsubstring (elt, make_number (charpos),
14050 make_number (charpos + prec)),
14051 0, 0, 0, Qnil);
14052 }
d39b6696 14053 else
eaaa65b0
GM
14054 {
14055 int bytepos = last - lisp_string;
14056 int charpos = string_byte_to_char (elt, bytepos);
14057 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
14058 it, 0, prec, 0,
14059 STRING_MULTIBYTE (elt));
eaaa65b0 14060 }
a2889657
JB
14061 }
14062 else /* c == '%' */
14063 {
50f80c2f 14064 const unsigned char *percent_position = this;
2311178e 14065
5f5c8ee5
GM
14066 /* Get the specified minimum width. Zero means
14067 don't pad. */
14068 field = 0;
a2889657 14069 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 14070 field = field * 10 + c - '0';
a2889657 14071
5f5c8ee5
GM
14072 /* Don't pad beyond the total padding allowed. */
14073 if (field_width - n > 0 && field > field_width - n)
14074 field = field_width - n;
a2889657 14075
5f5c8ee5
GM
14076 /* Note that either PRECISION <= 0 or N < PRECISION. */
14077 prec = precision - n;
2311178e 14078
a2889657 14079 if (c == 'M')
5f5c8ee5 14080 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
14081 Vglobal_mode_string, props,
14082 risky);
a2889657 14083 else if (c != 0)
d39b6696 14084 {
72f62cb5 14085 int multibyte;
ae02e06a
RS
14086 int bytepos, charpos;
14087 unsigned char *spec;
2311178e 14088
ae02e06a
RS
14089 bytepos = percent_position - lisp_string;
14090 charpos = (STRING_MULTIBYTE (elt)
14091 ? string_byte_to_char (elt, bytepos)
14092 : bytepos);
14093
14094 spec
72f62cb5
GM
14095 = decode_mode_spec (it->w, c, field, prec, &multibyte);
14096
d39b6696 14097 if (frame_title_ptr)
5f5c8ee5 14098 n += store_frame_title (spec, field, prec);
fec8f23e
KS
14099 else if (!NILP (mode_line_string_list))
14100 {
14101 int len = strlen (spec);
14102 Lisp_Object tem = make_string (spec, len);
14103 props = Ftext_properties_at (make_number (charpos), elt);
14104 /* Should only keep face property in props */
14105 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
14106 }
d39b6696 14107 else
5f5c8ee5 14108 {
ae02e06a 14109 int nglyphs_before, nwritten;
2311178e 14110
72f62cb5 14111 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
14112 nwritten = display_string (spec, Qnil, elt,
14113 charpos, 0, it,
14114 field, prec, 0,
14115 multibyte);
5f5c8ee5
GM
14116
14117 /* Assign to the glyphs written above the
14118 string where the `%x' came from, position
14119 of the `%'. */
14120 if (nwritten > 0)
14121 {
14122 struct glyph *glyph
14123 = (it->glyph_row->glyphs[TEXT_AREA]
14124 + nglyphs_before);
14125 int i;
14126
14127 for (i = 0; i < nwritten; ++i)
14128 {
14129 glyph[i].object = elt;
14130 glyph[i].charpos = charpos;
14131 }
2311178e 14132
5f5c8ee5
GM
14133 n += nwritten;
14134 }
14135 }
d39b6696 14136 }
b8523839
AS
14137 else /* c == 0 */
14138 break;
a2889657
JB
14139 }
14140 }
14141 }
14142 break;
14143
14144 case Lisp_Symbol:
14145 /* A symbol: process the value of the symbol recursively
14146 as if it appeared here directly. Avoid error if symbol void.
14147 Special case: if value of symbol is a string, output the string
14148 literally. */
14149 {
14150 register Lisp_Object tem;
c53a1624
RS
14151
14152 /* If the variable is not marked as risky to set
14153 then its contents are risky to use. */
14154 if (NILP (Fget (elt, Qrisky_local_variable)))
14155 risky = 1;
14156
a2889657 14157 tem = Fboundp (elt);
265a9e55 14158 if (!NILP (tem))
a2889657
JB
14159 {
14160 tem = Fsymbol_value (elt);
14161 /* If value is a string, output that string literally:
14162 don't check for % within it. */
e24c997d 14163 if (STRINGP (tem))
0fcf414f
RS
14164 literal = 1;
14165
14166 if (!EQ (tem, elt))
5f5c8ee5
GM
14167 {
14168 /* Give up right away for nil or t. */
14169 elt = tem;
14170 goto tail_recurse;
14171 }
a2889657
JB
14172 }
14173 }
14174 break;
14175
14176 case Lisp_Cons:
14177 {
14178 register Lisp_Object car, tem;
14179
0fcf414f
RS
14180 /* A cons cell: five distinct cases.
14181 If first element is :eval or :propertize, do something special.
a2889657
JB
14182 If first element is a string or a cons, process all the elements
14183 and effectively concatenate them.
14184 If first element is a negative number, truncate displaying cdr to
14185 at most that many characters. If positive, pad (with spaces)
14186 to at least that many characters.
14187 If first element is a symbol, process the cadr or caddr recursively
14188 according to whether the symbol's value is non-nil or nil. */
9472f927 14189 car = XCAR (elt);
0fcf414f 14190 if (EQ (car, QCeval))
5f5c8ee5
GM
14191 {
14192 /* An element of the form (:eval FORM) means evaluate FORM
14193 and use the result as mode line elements. */
0fcf414f 14194
c53a1624
RS
14195 if (risky)
14196 break;
14197
0fcf414f
RS
14198 if (CONSP (XCDR (elt)))
14199 {
14200 Lisp_Object spec;
14201 spec = safe_eval (XCAR (XCDR (elt)));
14202 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
14203 precision - n, spec, props,
14204 risky);
0fcf414f
RS
14205 }
14206 }
14207 else if (EQ (car, QCpropertize))
14208 {
c53a1624
RS
14209 /* An element of the form (:propertize ELT PROPS...)
14210 means display ELT but applying properties PROPS. */
14211
14212 if (risky)
14213 break;
14214
0fcf414f 14215 if (CONSP (XCDR (elt)))
c53a1624
RS
14216 n += display_mode_element (it, depth, field_width - n,
14217 precision - n, XCAR (XCDR (elt)),
14218 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
14219 }
14220 else if (SYMBOLP (car))
a2889657
JB
14221 {
14222 tem = Fboundp (car);
9472f927 14223 elt = XCDR (elt);
e24c997d 14224 if (!CONSP (elt))
a2889657
JB
14225 goto invalid;
14226 /* elt is now the cdr, and we know it is a cons cell.
14227 Use its car if CAR has a non-nil value. */
265a9e55 14228 if (!NILP (tem))
a2889657
JB
14229 {
14230 tem = Fsymbol_value (car);
265a9e55 14231 if (!NILP (tem))
9472f927
GM
14232 {
14233 elt = XCAR (elt);
14234 goto tail_recurse;
14235 }
a2889657
JB
14236 }
14237 /* Symbol's value is nil (or symbol is unbound)
14238 Get the cddr of the original list
14239 and if possible find the caddr and use that. */
9472f927 14240 elt = XCDR (elt);
265a9e55 14241 if (NILP (elt))
a2889657 14242 break;
e24c997d 14243 else if (!CONSP (elt))
a2889657 14244 goto invalid;
9472f927 14245 elt = XCAR (elt);
a2889657
JB
14246 goto tail_recurse;
14247 }
e24c997d 14248 else if (INTEGERP (car))
a2889657
JB
14249 {
14250 register int lim = XINT (car);
9472f927 14251 elt = XCDR (elt);
a2889657 14252 if (lim < 0)
5f5c8ee5
GM
14253 {
14254 /* Negative int means reduce maximum width. */
14255 if (precision <= 0)
14256 precision = -lim;
14257 else
14258 precision = min (precision, -lim);
14259 }
a2889657
JB
14260 else if (lim > 0)
14261 {
14262 /* Padding specified. Don't let it be more than
14263 current maximum. */
5f5c8ee5
GM
14264 if (precision > 0)
14265 lim = min (precision, lim);
14266
a2889657
JB
14267 /* If that's more padding than already wanted, queue it.
14268 But don't reduce padding already specified even if
14269 that is beyond the current truncation point. */
5f5c8ee5 14270 field_width = max (lim, field_width);
a2889657
JB
14271 }
14272 goto tail_recurse;
14273 }
e24c997d 14274 else if (STRINGP (car) || CONSP (car))
a2889657
JB
14275 {
14276 register int limit = 50;
5f5c8ee5
GM
14277 /* Limit is to protect against circular lists. */
14278 while (CONSP (elt)
14279 && --limit > 0
14280 && (precision <= 0 || n < precision))
a2889657 14281 {
5f5c8ee5 14282 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
14283 precision - n, XCAR (elt),
14284 props, risky);
9472f927 14285 elt = XCDR (elt);
a2889657
JB
14286 }
14287 }
14288 }
14289 break;
14290
14291 default:
14292 invalid:
d39b6696 14293 if (frame_title_ptr)
5f5c8ee5 14294 n += store_frame_title ("*invalid*", 0, precision - n);
fec8f23e
KS
14295 else if (!NILP (mode_line_string_list))
14296 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
d39b6696 14297 else
5f5c8ee5
GM
14298 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14299 precision - n, 0, 0);
14300 return n;
a2889657
JB
14301 }
14302
5f5c8ee5
GM
14303 /* Pad to FIELD_WIDTH. */
14304 if (field_width > 0 && n < field_width)
14305 {
14306 if (frame_title_ptr)
14307 n += store_frame_title ("", field_width - n, 0);
fec8f23e
KS
14308 else if (!NILP (mode_line_string_list))
14309 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
5f5c8ee5
GM
14310 else
14311 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14312 0, 0, 0);
14313 }
2311178e 14314
5f5c8ee5 14315 return n;
a2889657 14316}
5f5c8ee5 14317
fec8f23e
KS
14318/* Store a mode-line string element in mode_line_string_list.
14319
14320 If STRING is non-null, display that C string. Otherwise, the Lisp
14321 string LISP_STRING is displayed.
14322
14323 FIELD_WIDTH is the minimum number of output glyphs to produce.
14324 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14325 with spaces. FIELD_WIDTH <= 0 means don't pad.
14326
14327 PRECISION is the maximum number of characters to output from
14328 STRING. PRECISION <= 0 means don't truncate the string.
14329
14330 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14331 properties to the string.
14332
7d0393cf 14333 PROPS are the properties to add to the string.
fec8f23e
KS
14334 The mode_line_string_face face property is always added to the string.
14335 */
14336
14337static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14338 char *string;
14339 Lisp_Object lisp_string;
14340 int copy_string;
14341 int field_width;
14342 int precision;
14343 Lisp_Object props;
14344{
14345 int len;
14346 int n = 0;
14347
14348 if (string != NULL)
14349 {
14350 len = strlen (string);
14351 if (precision > 0 && len > precision)
14352 len = precision;
14353 lisp_string = make_string (string, len);
14354 if (NILP (props))
14355 props = mode_line_string_face_prop;
14356 else if (!NILP (mode_line_string_face))
14357 {
14358 Lisp_Object face = Fplist_get (props, Qface);
14359 props = Fcopy_sequence (props);
14360 if (NILP (face))
14361 face = mode_line_string_face;
14362 else
14363 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14364 props = Fplist_put (props, Qface, face);
14365 }
14366 Fadd_text_properties (make_number (0), make_number (len),
14367 props, lisp_string);
14368 }
7d0393cf 14369 else
fec8f23e 14370 {
c8224325 14371 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
14372 if (precision > 0 && len > precision)
14373 {
14374 len = precision;
14375 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14376 precision = -1;
14377 }
14378 if (!NILP (mode_line_string_face))
14379 {
14380 Lisp_Object face;
14381 if (NILP (props))
14382 props = Ftext_properties_at (make_number (0), lisp_string);
14383 face = Fplist_get (props, Qface);
14384 if (NILP (face))
14385 face = mode_line_string_face;
14386 else
14387 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14388 props = Fcons (Qface, Fcons (face, Qnil));
14389 if (copy_string)
14390 lisp_string = Fcopy_sequence (lisp_string);
14391 }
14392 if (!NILP (props))
14393 Fadd_text_properties (make_number (0), make_number (len),
14394 props, lisp_string);
14395 }
14396
14397 if (len > 0)
14398 {
14399 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 14400 n += len;
fec8f23e
KS
14401 }
14402
14403 if (field_width > len)
14404 {
14405 field_width -= len;
14406 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14407 if (!NILP (props))
14408 Fadd_text_properties (make_number (0), make_number (field_width),
14409 props, lisp_string);
14410 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 14411 n += field_width;
fec8f23e
KS
14412 }
14413
14414 return n;
14415}
14416
5f5c8ee5 14417
8143e6ab 14418DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
fec8f23e 14419 0, 3, 0,
8143e6ab
KS
14420 doc: /* Return the mode-line of selected window as a string.
14421First optional arg FORMAT specifies a different format string (see
8b22c65a 14422`mode-line-format' for details) to use. If FORMAT is t, return
8143e6ab 14423the buffer's header-line. Second optional arg WINDOW specifies a
fec8f23e
KS
14424different window to use as the context for the formatting.
14425If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14426 (format, window, no_props)
14427 Lisp_Object format, window, no_props;
8143e6ab
KS
14428{
14429 struct it it;
8143e6ab
KS
14430 int len;
14431 struct window *w;
14432 struct buffer *old_buffer = NULL;
fec8f23e 14433 enum face_id face_id = DEFAULT_FACE_ID;
8143e6ab
KS
14434
14435 if (NILP (window))
14436 window = selected_window;
14437 CHECK_WINDOW (window);
14438 w = XWINDOW (window);
14439 CHECK_BUFFER (w->buffer);
14440
14441 if (XBUFFER (w->buffer) != current_buffer)
14442 {
14443 old_buffer = current_buffer;
14444 set_buffer_internal_1 (XBUFFER (w->buffer));
14445 }
14446
14447 if (NILP (format) || EQ (format, Qt))
fec8f23e
KS
14448 {
14449 face_id = NILP (format)
14450 ? CURRENT_MODE_LINE_FACE_ID (w) :
14451 HEADER_LINE_FACE_ID;
14452 format = NILP (format)
14453 ? current_buffer->mode_line_format
14454 : current_buffer->header_line_format;
14455 }
14456
14457 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 14458
fec8f23e
KS
14459 if (NILP (no_props))
14460 {
14461 mode_line_string_face =
14462 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14463 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14464 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
8143e6ab 14465
fec8f23e
KS
14466 mode_line_string_face_prop =
14467 NILP (mode_line_string_face) ? Qnil :
14468 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14469
14470 /* We need a dummy last element in mode_line_string_list to
14471 indicate we are building the propertized mode-line string.
14472 Using mode_line_string_face_prop here GC protects it. */
7d0393cf 14473 mode_line_string_list =
fec8f23e
KS
14474 Fcons (mode_line_string_face_prop, Qnil);
14475 frame_title_ptr = NULL;
14476 }
14477 else
14478 {
14479 mode_line_string_face_prop = Qnil;
14480 mode_line_string_list = Qnil;
14481 frame_title_ptr = frame_title_buf;
14482 }
8143e6ab
KS
14483
14484 push_frame_kboard (it.f);
14485 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14486 pop_frame_kboard ();
14487
14488 if (old_buffer)
14489 set_buffer_internal_1 (old_buffer);
14490
fec8f23e
KS
14491 if (NILP (no_props))
14492 {
14493 Lisp_Object str;
14494 mode_line_string_list = Fnreverse (mode_line_string_list);
14495 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14496 make_string ("", 0));
14497 mode_line_string_face_prop = Qnil;
14498 mode_line_string_list = Qnil;
14499 return str;
14500 }
14501
8143e6ab
KS
14502 len = frame_title_ptr - frame_title_buf;
14503 if (len > 0 && frame_title_ptr[-1] == '-')
14504 {
14505 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14506 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14507 ;
14508 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14509 if (len > frame_title_ptr - frame_title_buf)
14510 len = frame_title_ptr - frame_title_buf;
14511 }
14512
14513 frame_title_ptr = NULL;
14514 return make_string (frame_title_buf, len);
14515}
14516
766525bc
RS
14517/* Write a null-terminated, right justified decimal representation of
14518 the positive integer D to BUF using a minimal field width WIDTH. */
14519
14520static void
14521pint2str (buf, width, d)
14522 register char *buf;
14523 register int width;
14524 register int d;
14525{
14526 register char *p = buf;
2311178e 14527
766525bc 14528 if (d <= 0)
5f5c8ee5 14529 *p++ = '0';
766525bc 14530 else
5f5c8ee5 14531 {
766525bc 14532 while (d > 0)
5f5c8ee5 14533 {
766525bc
RS
14534 *p++ = d % 10 + '0';
14535 d /= 10;
5f5c8ee5
GM
14536 }
14537 }
2311178e 14538
5f5c8ee5
GM
14539 for (width -= (int) (p - buf); width > 0; --width)
14540 *p++ = ' ';
766525bc
RS
14541 *p-- = '\0';
14542 while (p > buf)
5f5c8ee5 14543 {
766525bc
RS
14544 d = *buf;
14545 *buf++ = *p;
14546 *p-- = d;
5f5c8ee5 14547 }
766525bc
RS
14548}
14549
5f5c8ee5 14550/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
14551 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14552 type of CODING_SYSTEM. Return updated pointer into BUF. */
14553
6693a99a 14554static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 14555
1c9241f5
KH
14556static char *
14557decode_mode_spec_coding (coding_system, buf, eol_flag)
14558 Lisp_Object coding_system;
14559 register char *buf;
14560 int eol_flag;
14561{
1e1078d6 14562 Lisp_Object val;
916848d8 14563 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 14564 const unsigned char *eol_str;
302f2b38
EZ
14565 int eol_str_len;
14566 /* The EOL conversion we are using. */
14567 Lisp_Object eoltype;
1e1078d6 14568
4a09dee0 14569 val = Fget (coding_system, Qcoding_system);
085536c2 14570 eoltype = Qnil;
1c9241f5 14571
4a09dee0 14572 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 14573 {
916848d8
RS
14574 if (multibyte)
14575 *buf++ = '-';
21e989e3 14576 if (eol_flag)
302f2b38 14577 eoltype = eol_mnemonic_undecided;
1e1078d6 14578 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
14579 }
14580 else
14581 {
1e1078d6
RS
14582 Lisp_Object eolvalue;
14583
14584 eolvalue = Fget (coding_system, Qeol_type);
14585
916848d8 14586 if (multibyte)
a61b7058 14587 *buf++ = XFASTINT (AREF (val, 1));
916848d8 14588
1c9241f5
KH
14589 if (eol_flag)
14590 {
1e1078d6
RS
14591 /* The EOL conversion that is normal on this system. */
14592
14593 if (NILP (eolvalue)) /* Not yet decided. */
14594 eoltype = eol_mnemonic_undecided;
14595 else if (VECTORP (eolvalue)) /* Not yet decided. */
14596 eoltype = eol_mnemonic_undecided;
14597 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14598 eoltype = (XFASTINT (eolvalue) == 0
14599 ? eol_mnemonic_unix
14600 : (XFASTINT (eolvalue) == 1
14601 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
14602 }
14603 }
2311178e 14604
302f2b38
EZ
14605 if (eol_flag)
14606 {
14607 /* Mention the EOL conversion if it is not the usual one. */
14608 if (STRINGP (eoltype))
14609 {
2051c264
GM
14610 eol_str = SDATA (eoltype);
14611 eol_str_len = SBYTES (eoltype);
302f2b38 14612 }
f30b3499
KH
14613 else if (INTEGERP (eoltype)
14614 && CHAR_VALID_P (XINT (eoltype), 0))
14615 {
50f80c2f
KR
14616 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14617 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14618 eol_str = tmp;
f30b3499 14619 }
302f2b38
EZ
14620 else
14621 {
14622 eol_str = invalid_eol_type;
14623 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 14624 }
f30b3499 14625 bcopy (eol_str, buf, eol_str_len);
302f2b38 14626 buf += eol_str_len;
1c9241f5 14627 }
302f2b38 14628
1c9241f5
KH
14629 return buf;
14630}
14631
a2889657 14632/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
14633 generated by character C. PRECISION >= 0 means don't return a
14634 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
14635 string returned with spaces to that value. Return 1 in *MULTIBYTE
14636 if the result is multibyte text. */
a2889657 14637
11e82b76
JB
14638static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14639
a2889657 14640static char *
72f62cb5 14641decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 14642 struct window *w;
68c45bf0 14643 register int c;
5f5c8ee5 14644 int field_width, precision;
72f62cb5 14645 int *multibyte;
a2889657 14646{
0b67772d 14647 Lisp_Object obj;
5f5c8ee5
GM
14648 struct frame *f = XFRAME (WINDOW_FRAME (w));
14649 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 14650 struct buffer *b = XBUFFER (w->buffer);
a2889657 14651
0b67772d 14652 obj = Qnil;
72f62cb5 14653 *multibyte = 0;
a2889657
JB
14654
14655 switch (c)
14656 {
1af9f229
RS
14657 case '*':
14658 if (!NILP (b->read_only))
14659 return "%";
14660 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14661 return "*";
14662 return "-";
14663
14664 case '+':
14665 /* This differs from %* only for a modified read-only buffer. */
14666 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14667 return "*";
14668 if (!NILP (b->read_only))
14669 return "%";
14670 return "-";
14671
14672 case '&':
14673 /* This differs from %* in ignoring read-only-ness. */
14674 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14675 return "*";
14676 return "-";
14677
14678 case '%':
14679 return "%";
14680
2311178e 14681 case '[':
1af9f229
RS
14682 {
14683 int i;
14684 char *p;
14685
14686 if (command_loop_level > 5)
14687 return "[[[... ";
14688 p = decode_mode_spec_buf;
14689 for (i = 0; i < command_loop_level; i++)
14690 *p++ = '[';
14691 *p = 0;
14692 return decode_mode_spec_buf;
14693 }
14694
2311178e 14695 case ']':
1af9f229
RS
14696 {
14697 int i;
14698 char *p;
14699
14700 if (command_loop_level > 5)
14701 return " ...]]]";
14702 p = decode_mode_spec_buf;
14703 for (i = 0; i < command_loop_level; i++)
14704 *p++ = ']';
14705 *p = 0;
14706 return decode_mode_spec_buf;
14707 }
14708
14709 case '-':
14710 {
1af9f229 14711 register int i;
5f5c8ee5
GM
14712
14713 /* Let lots_of_dashes be a string of infinite length. */
fec8f23e
KS
14714 if (!NILP (mode_line_string_list))
14715 return "--";
5f5c8ee5
GM
14716 if (field_width <= 0
14717 || field_width > sizeof (lots_of_dashes))
1af9f229 14718 {
5f5c8ee5
GM
14719 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14720 decode_mode_spec_buf[i] = '-';
14721 decode_mode_spec_buf[i] = '\0';
14722 return decode_mode_spec_buf;
1af9f229 14723 }
5f5c8ee5
GM
14724 else
14725 return lots_of_dashes;
1af9f229
RS
14726 }
14727
2311178e 14728 case 'b':
d39b6696 14729 obj = b->name;
a2889657
JB
14730 break;
14731
1af9f229
RS
14732 case 'c':
14733 {
2311178e 14734 int col = (int) current_column (); /* iftc */
ac90c44f 14735 w->column_number_displayed = make_number (col);
5f5c8ee5 14736 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
14737 return decode_mode_spec_buf;
14738 }
14739
14740 case 'F':
14741 /* %F displays the frame name. */
5f5c8ee5 14742 if (!NILP (f->title))
2051c264 14743 return (char *) SDATA (f->title);
fd8ff63d 14744 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 14745 return (char *) SDATA (f->name);
9c6da96f 14746 return "Emacs";
1af9f229 14747
2311178e 14748 case 'f':
d39b6696 14749 obj = b->filename;
a2889657
JB
14750 break;
14751
aa6d10fa
RS
14752 case 'l':
14753 {
12adba34
RS
14754 int startpos = XMARKER (w->start)->charpos;
14755 int startpos_byte = marker_byte_position (w->start);
14756 int line, linepos, linepos_byte, topline;
aa6d10fa 14757 int nlines, junk;
aa6d10fa
RS
14758 int height = XFASTINT (w->height);
14759
2311178e 14760 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
14761 don't forget that too fast. */
14762 if (EQ (w->base_line_pos, w->buffer))
766525bc 14763 goto no_value;
5300fd39
RS
14764 /* But do forget it, if the window shows a different buffer now. */
14765 else if (BUFFERP (w->base_line_pos))
14766 w->base_line_pos = Qnil;
aa6d10fa
RS
14767
14768 /* If the buffer is very big, don't waste time. */
37d034d3 14769 if (INTEGERP (Vline_number_display_limit)
090703f4 14770 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
14771 {
14772 w->base_line_pos = Qnil;
14773 w->base_line_number = Qnil;
766525bc 14774 goto no_value;
aa6d10fa
RS
14775 }
14776
14777 if (!NILP (w->base_line_number)
14778 && !NILP (w->base_line_pos)
12adba34 14779 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
14780 {
14781 line = XFASTINT (w->base_line_number);
14782 linepos = XFASTINT (w->base_line_pos);
12adba34 14783 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
14784 }
14785 else
14786 {
14787 line = 1;
d39b6696 14788 linepos = BUF_BEGV (b);
12adba34 14789 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
14790 }
14791
14792 /* Count lines from base line to window start position. */
12adba34
RS
14793 nlines = display_count_lines (linepos, linepos_byte,
14794 startpos_byte,
14795 startpos, &junk);
aa6d10fa
RS
14796
14797 topline = nlines + line;
14798
14799 /* Determine a new base line, if the old one is too close
14800 or too far away, or if we did not have one.
14801 "Too close" means it's plausible a scroll-down would
14802 go back past it. */
d39b6696 14803 if (startpos == BUF_BEGV (b))
aa6d10fa 14804 {
ac90c44f
GM
14805 w->base_line_number = make_number (topline);
14806 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
14807 }
14808 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 14809 || linepos == BUF_BEGV (b))
aa6d10fa 14810 {
d39b6696 14811 int limit = BUF_BEGV (b);
12adba34 14812 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 14813 int position;
5d121aec 14814 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
14815
14816 if (startpos - distance > limit)
12adba34
RS
14817 {
14818 limit = startpos - distance;
14819 limit_byte = CHAR_TO_BYTE (limit);
14820 }
aa6d10fa 14821
12adba34
RS
14822 nlines = display_count_lines (startpos, startpos_byte,
14823 limit_byte,
14824 - (height * 2 + 30),
aa6d10fa 14825 &position);
2311178e 14826 /* If we couldn't find the lines we wanted within
5d121aec 14827 line_number_display_limit_width chars per line,
aa6d10fa 14828 give up on line numbers for this window. */
12adba34 14829 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
14830 {
14831 w->base_line_pos = w->buffer;
14832 w->base_line_number = Qnil;
766525bc 14833 goto no_value;
aa6d10fa
RS
14834 }
14835
ac90c44f
GM
14836 w->base_line_number = make_number (topline - nlines);
14837 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
14838 }
14839
14840 /* Now count lines from the start pos to point. */
12adba34
RS
14841 nlines = display_count_lines (startpos, startpos_byte,
14842 PT_BYTE, PT, &junk);
aa6d10fa
RS
14843
14844 /* Record that we did display the line number. */
14845 line_number_displayed = 1;
14846
14847 /* Make the string to show. */
5f5c8ee5 14848 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 14849 return decode_mode_spec_buf;
766525bc
RS
14850 no_value:
14851 {
14852 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
14853 int pad = field_width - 2;
14854 while (pad-- > 0)
14855 *p++ = ' ';
14856 *p++ = '?';
b357b9d4
KR
14857 *p++ = '?';
14858 *p = '\0';
766525bc
RS
14859 return decode_mode_spec_buf;
14860 }
aa6d10fa
RS
14861 }
14862 break;
14863
2311178e 14864 case 'm':
d39b6696 14865 obj = b->mode_name;
a2889657
JB
14866 break;
14867
14868 case 'n':
d39b6696 14869 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
14870 return " Narrow";
14871 break;
14872
a2889657
JB
14873 case 'p':
14874 {
14875 int pos = marker_position (w->start);
d39b6696 14876 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 14877
d39b6696 14878 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 14879 {
d39b6696 14880 if (pos <= BUF_BEGV (b))
a2889657
JB
14881 return "All";
14882 else
14883 return "Bottom";
14884 }
d39b6696 14885 else if (pos <= BUF_BEGV (b))
a2889657
JB
14886 return "Top";
14887 else
14888 {
3c7d31b9
RS
14889 if (total > 1000000)
14890 /* Do it differently for a large value, to avoid overflow. */
14891 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14892 else
14893 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
14894 /* We can't normally display a 3-digit number,
14895 so get us a 2-digit number that is close. */
14896 if (total == 100)
14897 total = 99;
14898 sprintf (decode_mode_spec_buf, "%2d%%", total);
14899 return decode_mode_spec_buf;
14900 }
14901 }
14902
8ffcb79f
RS
14903 /* Display percentage of size above the bottom of the screen. */
14904 case 'P':
14905 {
14906 int toppos = marker_position (w->start);
d39b6696
KH
14907 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14908 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 14909
d39b6696 14910 if (botpos >= BUF_ZV (b))
8ffcb79f 14911 {
d39b6696 14912 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
14913 return "All";
14914 else
14915 return "Bottom";
14916 }
14917 else
14918 {
3c7d31b9
RS
14919 if (total > 1000000)
14920 /* Do it differently for a large value, to avoid overflow. */
14921 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14922 else
14923 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
14924 /* We can't normally display a 3-digit number,
14925 so get us a 2-digit number that is close. */
14926 if (total == 100)
14927 total = 99;
d39b6696 14928 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
14929 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14930 else
14931 sprintf (decode_mode_spec_buf, "%2d%%", total);
14932 return decode_mode_spec_buf;
14933 }
14934 }
14935
1af9f229
RS
14936 case 's':
14937 /* status of process */
14938 obj = Fget_buffer_process (w->buffer);
14939 if (NILP (obj))
14940 return "no process";
14941#ifdef subprocesses
14942 obj = Fsymbol_name (Fprocess_status (obj));
14943#endif
14944 break;
d39b6696 14945
1af9f229
RS
14946 case 't': /* indicate TEXT or BINARY */
14947#ifdef MODE_LINE_BINARY_TEXT
14948 return MODE_LINE_BINARY_TEXT (b);
14949#else
14950 return "T";
14951#endif
1c9241f5
KH
14952
14953 case 'z':
14954 /* coding-system (not including end-of-line format) */
14955 case 'Z':
14956 /* coding-system (including end-of-line type) */
14957 {
14958 int eol_flag = (c == 'Z');
539b4d41 14959 char *p = decode_mode_spec_buf;
1c9241f5 14960
d30e754b 14961 if (! FRAME_WINDOW_P (f))
1c9241f5 14962 {
11c52c4f
RS
14963 /* No need to mention EOL here--the terminal never needs
14964 to do EOL conversion. */
14965 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14966 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 14967 }
f13c925f 14968 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 14969 p, eol_flag);
f13c925f 14970
11c52c4f 14971#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
14972#ifdef subprocesses
14973 obj = Fget_buffer_process (Fcurrent_buffer ());
14974 if (PROCESSP (obj))
14975 {
14976 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14977 p, eol_flag);
14978 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14979 p, eol_flag);
14980 }
14981#endif /* subprocesses */
11c52c4f 14982#endif /* 0 */
1c9241f5
KH
14983 *p = 0;
14984 return decode_mode_spec_buf;
14985 }
a2889657 14986 }
d39b6696 14987
e24c997d 14988 if (STRINGP (obj))
72f62cb5
GM
14989 {
14990 *multibyte = STRING_MULTIBYTE (obj);
2051c264 14991 return (char *) SDATA (obj);
72f62cb5 14992 }
a2889657
JB
14993 else
14994 return "";
14995}
5f5c8ee5
GM
14996
14997
12adba34
RS
14998/* Count up to COUNT lines starting from START / START_BYTE.
14999 But don't go beyond LIMIT_BYTE.
15000 Return the number of lines thus found (always nonnegative).
59b49f63 15001
12adba34 15002 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
15003
15004static int
12adba34
RS
15005display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15006 int start, start_byte, limit_byte, count;
15007 int *byte_pos_ptr;
59b49f63 15008{
59b49f63
RS
15009 register unsigned char *cursor;
15010 unsigned char *base;
15011
15012 register int ceiling;
15013 register unsigned char *ceiling_addr;
12adba34 15014 int orig_count = count;
59b49f63
RS
15015
15016 /* If we are not in selective display mode,
15017 check only for newlines. */
12adba34
RS
15018 int selective_display = (!NILP (current_buffer->selective_display)
15019 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
15020
15021 if (count > 0)
12adba34
RS
15022 {
15023 while (start_byte < limit_byte)
15024 {
15025 ceiling = BUFFER_CEILING_OF (start_byte);
15026 ceiling = min (limit_byte - 1, ceiling);
15027 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
15028 base = (cursor = BYTE_POS_ADDR (start_byte));
15029 while (1)
15030 {
15031 if (selective_display)
15032 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
15033 ;
15034 else
15035 while (*cursor != '\n' && ++cursor != ceiling_addr)
15036 ;
15037
15038 if (cursor != ceiling_addr)
15039 {
15040 if (--count == 0)
15041 {
15042 start_byte += cursor - base + 1;
15043 *byte_pos_ptr = start_byte;
15044 return orig_count;
15045 }
15046 else
15047 if (++cursor == ceiling_addr)
15048 break;
15049 }
15050 else
15051 break;
15052 }
15053 start_byte += cursor - base;
15054 }
15055 }
59b49f63
RS
15056 else
15057 {
12adba34
RS
15058 while (start_byte > limit_byte)
15059 {
15060 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
15061 ceiling = max (limit_byte, ceiling);
15062 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
15063 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
15064 while (1)
15065 {
12adba34
RS
15066 if (selective_display)
15067 while (--cursor != ceiling_addr
15068 && *cursor != '\n' && *cursor != 015)
15069 ;
15070 else
15071 while (--cursor != ceiling_addr && *cursor != '\n')
15072 ;
15073
59b49f63
RS
15074 if (cursor != ceiling_addr)
15075 {
15076 if (++count == 0)
15077 {
12adba34
RS
15078 start_byte += cursor - base + 1;
15079 *byte_pos_ptr = start_byte;
15080 /* When scanning backwards, we should
15081 not count the newline posterior to which we stop. */
15082 return - orig_count - 1;
59b49f63
RS
15083 }
15084 }
15085 else
15086 break;
15087 }
12adba34
RS
15088 /* Here we add 1 to compensate for the last decrement
15089 of CURSOR, which took it past the valid range. */
15090 start_byte += cursor - base + 1;
59b49f63
RS
15091 }
15092 }
15093
12adba34 15094 *byte_pos_ptr = limit_byte;
aa6d10fa 15095
12adba34
RS
15096 if (count < 0)
15097 return - orig_count + count;
15098 return orig_count - count;
aa6d10fa 15099
12adba34 15100}
a2889657 15101
a2889657 15102
5f5c8ee5
GM
15103\f
15104/***********************************************************************
15105 Displaying strings
15106 ***********************************************************************/
278feba9 15107
5f5c8ee5 15108/* Display a NUL-terminated string, starting with index START.
a3788d53 15109
5f5c8ee5
GM
15110 If STRING is non-null, display that C string. Otherwise, the Lisp
15111 string LISP_STRING is displayed.
a2889657 15112
5f5c8ee5
GM
15113 If FACE_STRING is not nil, FACE_STRING_POS is a position in
15114 FACE_STRING. Display STRING or LISP_STRING with the face at
15115 FACE_STRING_POS in FACE_STRING:
a2889657 15116
5f5c8ee5
GM
15117 Display the string in the environment given by IT, but use the
15118 standard display table, temporarily.
a3788d53 15119
5f5c8ee5
GM
15120 FIELD_WIDTH is the minimum number of output glyphs to produce.
15121 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15122 with spaces. If STRING has more characters, more than FIELD_WIDTH
15123 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 15124
5f5c8ee5
GM
15125 PRECISION is the maximum number of characters to output from
15126 STRING. PRECISION < 0 means don't truncate the string.
a2889657 15127
5f5c8ee5 15128 This is roughly equivalent to printf format specifiers:
a2889657 15129
5f5c8ee5
GM
15130 FIELD_WIDTH PRECISION PRINTF
15131 ----------------------------------------
15132 -1 -1 %s
15133 -1 10 %.10s
15134 10 -1 %10s
15135 20 10 %20.10s
a2889657 15136
5f5c8ee5
GM
15137 MULTIBYTE zero means do not display multibyte chars, > 0 means do
15138 display them, and < 0 means obey the current buffer's value of
15139 enable_multibyte_characters.
278feba9 15140
5f5c8ee5 15141 Value is the number of glyphs produced. */
b1d1124b 15142
5f5c8ee5
GM
15143static int
15144display_string (string, lisp_string, face_string, face_string_pos,
15145 start, it, field_width, precision, max_x, multibyte)
15146 unsigned char *string;
15147 Lisp_Object lisp_string;
68c45bf0
PE
15148 Lisp_Object face_string;
15149 int face_string_pos;
5f5c8ee5
GM
15150 int start;
15151 struct it *it;
15152 int field_width, precision, max_x;
15153 int multibyte;
15154{
15155 int hpos_at_start = it->hpos;
15156 int saved_face_id = it->face_id;
15157 struct glyph_row *row = it->glyph_row;
15158
15159 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 15160 with index START. */
5f5c8ee5
GM
15161 reseat_to_string (it, string, lisp_string, start,
15162 precision, field_width, multibyte);
15163
15164 /* If displaying STRING, set up the face of the iterator
15165 from LISP_STRING, if that's given. */
15166 if (STRINGP (face_string))
15167 {
15168 int endptr;
15169 struct face *face;
2311178e 15170
5f5c8ee5
GM
15171 it->face_id
15172 = face_at_string_position (it->w, face_string, face_string_pos,
15173 0, it->region_beg_charpos,
15174 it->region_end_charpos,
5de7c6f2 15175 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
15176 face = FACE_FROM_ID (it->f, it->face_id);
15177 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 15178 }
a2889657 15179
5f5c8ee5
GM
15180 /* Set max_x to the maximum allowed X position. Don't let it go
15181 beyond the right edge of the window. */
15182 if (max_x <= 0)
15183 max_x = it->last_visible_x;
15184 else
15185 max_x = min (max_x, it->last_visible_x);
efc63ef0 15186
5f5c8ee5
GM
15187 /* Skip over display elements that are not visible. because IT->w is
15188 hscrolled. */
15189 if (it->current_x < it->first_visible_x)
15190 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15191 MOVE_TO_POS | MOVE_TO_X);
a2889657 15192
5f5c8ee5
GM
15193 row->ascent = it->max_ascent;
15194 row->height = it->max_ascent + it->max_descent;
312246d1
GM
15195 row->phys_ascent = it->max_phys_ascent;
15196 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 15197
5f5c8ee5
GM
15198 /* This condition is for the case that we are called with current_x
15199 past last_visible_x. */
15200 while (it->current_x < max_x)
a2889657 15201 {
5f5c8ee5 15202 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 15203
5f5c8ee5
GM
15204 /* Get the next display element. */
15205 if (!get_next_display_element (it))
90adcf20 15206 break;
1c9241f5 15207
5f5c8ee5
GM
15208 /* Produce glyphs. */
15209 x_before = it->current_x;
15210 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15211 PRODUCE_GLYPHS (it);
90adcf20 15212
5f5c8ee5
GM
15213 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15214 i = 0;
15215 x = x_before;
15216 while (i < nglyphs)
a2889657 15217 {
5f5c8ee5 15218 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 15219
5f5c8ee5
GM
15220 if (!it->truncate_lines_p
15221 && x + glyph->pixel_width > max_x)
15222 {
15223 /* End of continued line or max_x reached. */
37be86f2
KH
15224 if (CHAR_GLYPH_PADDING_P (*glyph))
15225 {
15226 /* A wide character is unbreakable. */
15227 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15228 it->current_x = x_before;
15229 }
15230 else
15231 {
15232 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15233 it->current_x = x;
15234 }
5f5c8ee5
GM
15235 break;
15236 }
15237 else if (x + glyph->pixel_width > it->first_visible_x)
15238 {
15239 /* Glyph is at least partially visible. */
15240 ++it->hpos;
15241 if (x < it->first_visible_x)
15242 it->glyph_row->x = x - it->first_visible_x;
15243 }
15244 else
a2889657 15245 {
5f5c8ee5
GM
15246 /* Glyph is off the left margin of the display area.
15247 Should not happen. */
15248 abort ();
a2889657 15249 }
5f5c8ee5
GM
15250
15251 row->ascent = max (row->ascent, it->max_ascent);
15252 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
15253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15254 row->phys_height = max (row->phys_height,
15255 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
15256 x += glyph->pixel_width;
15257 ++i;
a2889657 15258 }
5f5c8ee5
GM
15259
15260 /* Stop if max_x reached. */
15261 if (i < nglyphs)
15262 break;
15263
15264 /* Stop at line ends. */
15265 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 15266 {
5f5c8ee5
GM
15267 it->continuation_lines_width = 0;
15268 break;
a2889657 15269 }
1c9241f5 15270
cafafe0b 15271 set_iterator_to_next (it, 1);
a688bb24 15272
5f5c8ee5
GM
15273 /* Stop if truncating at the right edge. */
15274 if (it->truncate_lines_p
15275 && it->current_x >= it->last_visible_x)
15276 {
15277 /* Add truncation mark, but don't do it if the line is
15278 truncated at a padding space. */
15279 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 15280 {
5f5c8ee5 15281 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
15282 {
15283 int i, n;
15284
9299cb15 15285 if (it->current_x > it->last_visible_x)
37be86f2 15286 {
9299cb15
KH
15287 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15288 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15289 break;
15290 for (n = row->used[TEXT_AREA]; i < n; ++i)
15291 {
15292 row->used[TEXT_AREA] = i;
15293 produce_special_glyphs (it, IT_TRUNCATION);
15294 }
37be86f2 15295 }
9299cb15 15296 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 15297 }
5f5c8ee5 15298 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 15299 }
5f5c8ee5 15300 break;
1c9241f5 15301 }
a2889657
JB
15302 }
15303
5f5c8ee5
GM
15304 /* Maybe insert a truncation at the left. */
15305 if (it->first_visible_x
15306 && IT_CHARPOS (*it) > 0)
a2889657 15307 {
5f5c8ee5
GM
15308 if (!FRAME_WINDOW_P (it->f))
15309 insert_left_trunc_glyphs (it);
15310 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
15311 }
15312
5f5c8ee5 15313 it->face_id = saved_face_id;
2311178e 15314
5f5c8ee5
GM
15315 /* Value is number of columns displayed. */
15316 return it->hpos - hpos_at_start;
15317}
a2889657 15318
a2889657 15319
a2889657 15320\f
3b6b6db7 15321/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
15322 appears as an element of LIST or as the car of an element of LIST.
15323 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
15324 way, and return 1/2 if any element of PROPVAL is found in LIST.
15325 Otherwise return 0. This function cannot quit.
15326 The return value is 2 if the text is invisible but with an ellipsis
15327 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
15328
15329int
15330invisible_p (propval, list)
15331 register Lisp_Object propval;
15332 Lisp_Object list;
15333{
3b6b6db7 15334 register Lisp_Object tail, proptail;
2311178e 15335
3b6b6db7
SM
15336 for (tail = list; CONSP (tail); tail = XCDR (tail))
15337 {
15338 register Lisp_Object tem;
15339 tem = XCAR (tail);
15340 if (EQ (propval, tem))
15341 return 1;
15342 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15343 return NILP (XCDR (tem)) ? 1 : 2;
15344 }
2311178e 15345
3b6b6db7
SM
15346 if (CONSP (propval))
15347 {
15348 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15349 {
15350 Lisp_Object propelt;
15351 propelt = XCAR (proptail);
15352 for (tail = list; CONSP (tail); tail = XCDR (tail))
15353 {
15354 register Lisp_Object tem;
15355 tem = XCAR (tail);
15356 if (EQ (propelt, tem))
15357 return 1;
15358 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15359 return NILP (XCDR (tem)) ? 1 : 2;
15360 }
15361 }
15362 }
2311178e 15363
3b6b6db7 15364 return 0;
642eefc6 15365}
5f5c8ee5 15366
642eefc6 15367\f
cfe03a41
KS
15368/***********************************************************************
15369 Cursor types
15370 ***********************************************************************/
15371
15372/* Value is the internal representation of the specified cursor type
15373 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15374 of the bar cursor. */
15375
15376enum text_cursor_kinds
15377get_specified_cursor_type (arg, width)
15378 Lisp_Object arg;
15379 int *width;
15380{
15381 enum text_cursor_kinds type;
7d0393cf 15382
cfe03a41
KS
15383 if (NILP (arg))
15384 return NO_CURSOR;
15385
15386 if (EQ (arg, Qbox))
15387 return FILLED_BOX_CURSOR;
15388
15389 if (EQ (arg, Qhollow))
15390 return HOLLOW_BOX_CURSOR;
15391
15392 if (EQ (arg, Qbar))
15393 {
15394 *width = 2;
15395 return BAR_CURSOR;
15396 }
15397
15398 if (CONSP (arg)
15399 && EQ (XCAR (arg), Qbar)
15400 && INTEGERP (XCDR (arg))
15401 && XINT (XCDR (arg)) >= 0)
15402 {
15403 *width = XINT (XCDR (arg));
15404 return BAR_CURSOR;
15405 }
15406
15407 if (EQ (arg, Qhbar))
15408 {
15409 *width = 2;
15410 return HBAR_CURSOR;
15411 }
15412
15413 if (CONSP (arg)
15414 && EQ (XCAR (arg), Qhbar)
15415 && INTEGERP (XCDR (arg))
15416 && XINT (XCDR (arg)) >= 0)
15417 {
15418 *width = XINT (XCDR (arg));
15419 return HBAR_CURSOR;
15420 }
15421
15422 /* Treat anything unknown as "hollow box cursor".
15423 It was bad to signal an error; people have trouble fixing
15424 .Xdefaults with Emacs, when it has something bad in it. */
15425 type = HOLLOW_BOX_CURSOR;
15426
15427 return type;
15428}
15429
15430/* Set the default cursor types for specified frame. */
15431void
15432set_frame_cursor_types (f, arg)
15433 struct frame *f;
15434 Lisp_Object arg;
15435{
15436 int width;
15437 Lisp_Object tem;
15438
15439 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15440 FRAME_CURSOR_WIDTH (f) = width;
15441
15442 /* By default, set up the blink-off state depending on the on-state. */
15443
15444 tem = Fassoc (arg, Vblink_cursor_alist);
15445 if (!NILP (tem))
15446 {
15447 FRAME_BLINK_OFF_CURSOR (f)
15448 = get_specified_cursor_type (XCDR (tem), &width);
15449 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15450 }
15451 else
15452 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15453}
15454
15455
e9c99027
KS
15456/* Return the cursor we want to be displayed in window W. Return
15457 width of bar/hbar cursor through WIDTH arg. Return with
15458 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
15459 (i.e. if the `system caret' should track this cursor).
15460
15461 In a mini-buffer window, we want the cursor only to appear if we
15462 are reading input from this window. For the selected window, we
15463 want the cursor type given by the frame parameter or buffer local
15464 setting of cursor-type. If explicitly marked off, draw no cursor.
15465 In all other cases, we want a hollow box cursor. */
cfe03a41
KS
15466
15467enum text_cursor_kinds
e9c99027 15468get_window_cursor_type (w, width, active_cursor)
cfe03a41
KS
15469 struct window *w;
15470 int *width;
e9c99027 15471 int *active_cursor;
cfe03a41
KS
15472{
15473 struct frame *f = XFRAME (w->frame);
15474 struct buffer *b = XBUFFER (w->buffer);
15475 int cursor_type = DEFAULT_CURSOR;
15476 Lisp_Object alt_cursor;
15477 int non_selected = 0;
15478
e9c99027
KS
15479 *active_cursor = 1;
15480
cfe03a41
KS
15481 /* Echo area */
15482 if (cursor_in_echo_area
15483 && FRAME_HAS_MINIBUF_P (f)
15484 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15485 {
15486 if (w == XWINDOW (echo_area_window))
15487 {
15488 *width = FRAME_CURSOR_WIDTH (f);
15489 return FRAME_DESIRED_CURSOR (f);
15490 }
15491
e9c99027 15492 *active_cursor = 0;
cfe03a41
KS
15493 non_selected = 1;
15494 }
15495
15496 /* Nonselected window or nonselected frame. */
97acc803
EZ
15497 else if (w != XWINDOW (f->selected_window)
15498#ifdef HAVE_WINDOW_SYSTEM
15499 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15500#endif
15501 )
cfe03a41 15502 {
e9c99027
KS
15503 *active_cursor = 0;
15504
cfe03a41
KS
15505 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15506 return NO_CURSOR;
15507
15508 non_selected = 1;
15509 }
15510
15511 /* Never display a cursor in a window in which cursor-type is nil. */
15512 if (NILP (b->cursor_type))
15513 return NO_CURSOR;
15514
15515 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15516 if (non_selected)
15517 {
15518 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15519 return get_specified_cursor_type (alt_cursor, width);
15520 }
15521
15522 /* Get the normal cursor type for this window. */
15523 if (EQ (b->cursor_type, Qt))
15524 {
15525 cursor_type = FRAME_DESIRED_CURSOR (f);
15526 *width = FRAME_CURSOR_WIDTH (f);
15527 }
15528 else
15529 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15530
15531 /* Use normal cursor if not blinked off. */
15532 if (!w->cursor_off_p)
15533 return cursor_type;
15534
15535 /* Cursor is blinked off, so determine how to "toggle" it. */
15536
8420a5dd
KS
15537 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
15538 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
cfe03a41
KS
15539 return get_specified_cursor_type (XCDR (alt_cursor), width);
15540
15541 /* Then see if frame has specified a specific blink off cursor type. */
15542 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15543 {
15544 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15545 return FRAME_BLINK_OFF_CURSOR (f);
15546 }
7d0393cf
JB
15547
15548 /* Finally perform built-in cursor blinking:
cfe03a41
KS
15549 filled box <-> hollow box
15550 wide [h]bar <-> narrow [h]bar
15551 narrow [h]bar <-> no cursor
15552 other type <-> no cursor */
15553
15554 if (cursor_type == FILLED_BOX_CURSOR)
15555 return HOLLOW_BOX_CURSOR;
15556
15557 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15558 {
15559 *width = 1;
15560 return cursor_type;
15561 }
15562
15563 return NO_CURSOR;
15564}
15565
15566\f
5f5c8ee5
GM
15567/***********************************************************************
15568 Initialization
15569 ***********************************************************************/
15570
a2889657
JB
15571void
15572syms_of_xdisp ()
15573{
c6e89d6c
GM
15574 Vwith_echo_area_save_vector = Qnil;
15575 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 15576
c6e89d6c
GM
15577 Vmessage_stack = Qnil;
15578 staticpro (&Vmessage_stack);
2311178e 15579
735c094c 15580 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 15581 staticpro (&Qinhibit_redisplay);
735c094c 15582
b14bc55e
RS
15583 message_dolog_marker1 = Fmake_marker ();
15584 staticpro (&message_dolog_marker1);
15585 message_dolog_marker2 = Fmake_marker ();
15586 staticpro (&message_dolog_marker2);
15587 message_dolog_marker3 = Fmake_marker ();
15588 staticpro (&message_dolog_marker3);
15589
5f5c8ee5 15590#if GLYPH_DEBUG
7d4cc828 15591 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
15592 defsubr (&Sdump_glyph_matrix);
15593 defsubr (&Sdump_glyph_row);
e037b9ec 15594 defsubr (&Sdump_tool_bar_row);
62397849 15595 defsubr (&Strace_redisplay);
bf9249e3 15596 defsubr (&Strace_to_stderr);
5f5c8ee5 15597#endif
99a5de87 15598#ifdef HAVE_WINDOW_SYSTEM
57c28064 15599 defsubr (&Stool_bar_lines_needed);
99a5de87 15600#endif
8143e6ab 15601 defsubr (&Sformat_mode_line);
5f5c8ee5 15602
cf074754
RS
15603 staticpro (&Qmenu_bar_update_hook);
15604 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15605
d46fb96a 15606 staticpro (&Qoverriding_terminal_local_map);
7079aefa 15607 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 15608
399164b4
KH
15609 staticpro (&Qoverriding_local_map);
15610 Qoverriding_local_map = intern ("overriding-local-map");
15611
75c43375
RS
15612 staticpro (&Qwindow_scroll_functions);
15613 Qwindow_scroll_functions = intern ("window-scroll-functions");
15614
e0bfbde6
RS
15615 staticpro (&Qredisplay_end_trigger_functions);
15616 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 15617
2e54982e
RS
15618 staticpro (&Qinhibit_point_motion_hooks);
15619 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15620
9499d71b
GM
15621 QCdata = intern (":data");
15622 staticpro (&QCdata);
5f5c8ee5 15623 Qdisplay = intern ("display");
f3751a65 15624 staticpro (&Qdisplay);
5f5c8ee5
GM
15625 Qspace_width = intern ("space-width");
15626 staticpro (&Qspace_width);
5f5c8ee5
GM
15627 Qraise = intern ("raise");
15628 staticpro (&Qraise);
15629 Qspace = intern ("space");
15630 staticpro (&Qspace);
f3751a65
GM
15631 Qmargin = intern ("margin");
15632 staticpro (&Qmargin);
5f5c8ee5 15633 Qleft_margin = intern ("left-margin");
f3751a65 15634 staticpro (&Qleft_margin);
5f5c8ee5 15635 Qright_margin = intern ("right-margin");
f3751a65 15636 staticpro (&Qright_margin);
5f5c8ee5
GM
15637 Qalign_to = intern ("align-to");
15638 staticpro (&Qalign_to);
15639 QCalign_to = intern (":align-to");
15640 staticpro (&QCalign_to);
5f5c8ee5
GM
15641 Qrelative_width = intern ("relative-width");
15642 staticpro (&Qrelative_width);
15643 QCrelative_width = intern (":relative-width");
15644 staticpro (&QCrelative_width);
15645 QCrelative_height = intern (":relative-height");
15646 staticpro (&QCrelative_height);
15647 QCeval = intern (":eval");
15648 staticpro (&QCeval);
0fcf414f
RS
15649 QCpropertize = intern (":propertize");
15650 staticpro (&QCpropertize);
d3acf96b 15651 Qwhen = intern ("when");
f3751a65 15652 staticpro (&Qwhen);
886bd6f2
GM
15653 QCfile = intern (":file");
15654 staticpro (&QCfile);
5f5c8ee5
GM
15655 Qfontified = intern ("fontified");
15656 staticpro (&Qfontified);
15657 Qfontification_functions = intern ("fontification-functions");
15658 staticpro (&Qfontification_functions);
5f5c8ee5
GM
15659 Qtrailing_whitespace = intern ("trailing-whitespace");
15660 staticpro (&Qtrailing_whitespace);
15661 Qimage = intern ("image");
15662 staticpro (&Qimage);
ad4f174e
GM
15663 Qmessage_truncate_lines = intern ("message-truncate-lines");
15664 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
15665 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15666 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
15667 Qgrow_only = intern ("grow-only");
15668 staticpro (&Qgrow_only);
e1477f43
GM
15669 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15670 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
15671 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15672 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
15673 Qposition = intern ("position");
15674 staticpro (&Qposition);
15675 Qbuffer_position = intern ("buffer-position");
15676 staticpro (&Qbuffer_position);
15677 Qobject = intern ("object");
15678 staticpro (&Qobject);
cfe03a41
KS
15679 Qbar = intern ("bar");
15680 staticpro (&Qbar);
15681 Qhbar = intern ("hbar");
15682 staticpro (&Qhbar);
15683 Qbox = intern ("box");
15684 staticpro (&Qbox);
15685 Qhollow = intern ("hollow");
15686 staticpro (&Qhollow);
c53a1624
RS
15687 Qrisky_local_variable = intern ("risky-local-variable");
15688 staticpro (&Qrisky_local_variable);
26683087
RS
15689 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15690 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 15691
7033d6df
RS
15692 list_of_error = Fcons (intern ("error"), Qnil);
15693 staticpro (&list_of_error);
15694
a2889657
JB
15695 last_arrow_position = Qnil;
15696 last_arrow_string = Qnil;
f3751a65
GM
15697 staticpro (&last_arrow_position);
15698 staticpro (&last_arrow_string);
2311178e 15699
c6e89d6c
GM
15700 echo_buffer[0] = echo_buffer[1] = Qnil;
15701 staticpro (&echo_buffer[0]);
15702 staticpro (&echo_buffer[1]);
15703
15704 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15705 staticpro (&echo_area_buffer[0]);
15706 staticpro (&echo_area_buffer[1]);
a2889657 15707
6a94510a
GM
15708 Vmessages_buffer_name = build_string ("*Messages*");
15709 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
15710
15711 mode_line_proptrans_alist = Qnil;
15712 staticpro (&mode_line_proptrans_alist);
2311178e 15713
fec8f23e
KS
15714 mode_line_string_list = Qnil;
15715 staticpro (&mode_line_string_list);
15716
7ee72033
MB
15717 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15718 doc: /* Non-nil means highlight trailing whitespace.
228299fa 15719The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
15720 Vshow_trailing_whitespace = Qnil;
15721
7ee72033
MB
15722 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15723 doc: /* Non-nil means don't actually do any redisplay.
228299fa 15724This is used for internal purposes. */);
735c094c
KH
15725 Vinhibit_redisplay = Qnil;
15726
7ee72033
MB
15727 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15728 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
15729 Vglobal_mode_string = Qnil;
15730
7ee72033
MB
15731 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15732 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
15733This must be the beginning of a line in order to work.
15734See also `overlay-arrow-string'. */);
a2889657
JB
15735 Voverlay_arrow_position = Qnil;
15736
7ee72033
MB
15737 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15738 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
a2889657
JB
15739 Voverlay_arrow_string = Qnil;
15740
7ee72033
MB
15741 DEFVAR_INT ("scroll-step", &scroll_step,
15742 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
15743If that fails to bring point back on frame, point is centered instead.
15744If this is zero, point is always centered after it moves off frame.
15745If you want scrolling to always be a line at a time, you should set
15746`scroll-conservatively' to a large value rather than set this to 1. */);
15747
7ee72033
MB
15748 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15749 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
15750A value of zero means to scroll the text to center point vertically
15751in the window. */);
0789adb2
RS
15752 scroll_conservatively = 0;
15753
7ee72033
MB
15754 DEFVAR_INT ("scroll-margin", &scroll_margin,
15755 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
15756Recenter the window whenever point gets within this many lines
15757of the top or bottom of the window. */);
9afd2168
RS
15758 scroll_margin = 0;
15759
5f5c8ee5 15760#if GLYPH_DEBUG
7ee72033 15761 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 15762#endif
a2889657
JB
15763
15764 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
15765 &truncate_partial_width_windows,
15766 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
15767 truncate_partial_width_windows = 1;
15768
7ee72033
MB
15769 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15770 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 15771Any other value means to use the appropriate face, `mode-line',
ccfe8f57 15772`header-line', or `menu' respectively. */);
1862a24e 15773 mode_line_inverse_video = 1;
aa6d10fa 15774
7ee72033
MB
15775 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15776 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
15777If the buffer is bigger than this, the line number does not appear
15778in the mode line. A value of nil means no limit. */);
090703f4 15779 Vline_number_display_limit = Qnil;
fba9ce76 15780
090703f4 15781 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
15782 &line_number_display_limit_width,
15783 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
15784If the average length of the lines near point is bigger than this, then the
15785line number may be omitted from the mode line. */);
5d121aec
KH
15786 line_number_display_limit_width = 200;
15787
7ee72033
MB
15788 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15789 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 15790 highlight_nonselected_windows = 0;
d39b6696 15791
7ee72033
MB
15792 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15793 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
15794Minibuffer-only frames don't count, but iconified frames do.
15795This variable is not guaranteed to be accurate except while processing
15796`frame-title-format' and `icon-title-format'. */);
15797
7ee72033
MB
15798 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15799 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
15800\(Assuming the window manager supports this feature.)
15801This variable has the same structure as `mode-line-format' (which see),
15802and is used only on frames for which no explicit name has been set
15803\(see `modify-frame-parameters'). */);
7ee72033
MB
15804 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15805 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
15806\(Assuming the window manager supports this feature.)
15807This variable has the same structure as `mode-line-format' (which see),
15808and is used only on frames for which no explicit name has been set
15809\(see `modify-frame-parameters'). */);
d39b6696
KH
15810 Vicon_title_format
15811 = Vframe_title_format
15812 = Fcons (intern ("multiple-frames"),
15813 Fcons (build_string ("%b"),
3ebf0ea9 15814 Fcons (Fcons (empty_string,
d39b6696
KH
15815 Fcons (intern ("invocation-name"),
15816 Fcons (build_string ("@"),
15817 Fcons (intern ("system-name"),
15818 Qnil)))),
15819 Qnil)));
5992c4f7 15820
7ee72033
MB
15821 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15822 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
15823If nil, disable message logging. If t, log messages but don't truncate
15824the buffer when it becomes large. */);
ac90c44f 15825 Vmessage_log_max = make_number (50);
08b610e4 15826
7ee72033
MB
15827 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15828 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
15829The value should be a list of functions that take one argument.
15830Just before redisplay, for each frame, if any of its windows have changed
15831size since the last redisplay, or have been split or deleted,
15832all the functions in the list are called, with the frame as argument. */);
08b610e4 15833 Vwindow_size_change_functions = Qnil;
75c43375 15834
7ee72033
MB
15835 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15836 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
15837Each function is called with two arguments, the window
15838and its new display-start position. Note that the value of `window-end'
15839is not valid when these functions are called. */);
75c43375 15840 Vwindow_scroll_functions = Qnil;
2311178e 15841
7ee72033
MB
15842 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15843 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
15844This increases a tool-bar's height if not all tool-bar items are visible.
15845It decreases a tool-bar's height when it would display blank lines
15846otherwise. */);
e037b9ec 15847 auto_resize_tool_bars_p = 1;
2311178e 15848
7ee72033
MB
15849 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15850 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 15851 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 15852
7ee72033
MB
15853 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15854 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 15855If an integer, use that for both horizontal and vertical margins.
f6c89f27 15856Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
15857HORZ specifying the horizontal margin, and VERT specifying the
15858vertical margin. */);
c3d76173 15859 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 15860
7ee72033 15861 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 15862 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 15863 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 15864
7ee72033
MB
15865 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15866 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
15867Each function is called with one argument POS. Functions must
15868fontify a region starting at POS in the current buffer, and give
15869fontified regions the property `fontified'. */);
5f5c8ee5 15870 Vfontification_functions = Qnil;
6b9f0906 15871 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
15872
15873 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
15874 &unibyte_display_via_language_environment,
15875 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
15876Specifically this means that unibyte non-ASCII characters
15877are displayed by converting them to the equivalent multibyte characters
15878according to the current language environment. As a result, they are
15879displayed according to the current fontset. */);
7bbe686f 15880 unibyte_display_via_language_environment = 0;
c6e89d6c 15881
7ee72033
MB
15882 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15883 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
15884If a float, it specifies a fraction of the mini-window frame's height.
15885If an integer, it specifies a number of lines. */);
c6e89d6c 15886 Vmax_mini_window_height = make_float (0.25);
6422c1d7 15887
7ee72033
MB
15888 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15889 doc: /* *How to resize mini-windows.
228299fa
GM
15890A value of nil means don't automatically resize mini-windows.
15891A value of t means resize them to fit the text displayed in them.
15892A value of `grow-only', the default, means let mini-windows grow
15893only, until their display becomes empty, at which point the windows
15894go back to their normal size. */);
6422c1d7
GM
15895 Vresize_mini_windows = Qgrow_only;
15896
cfe03a41
KS
15897 DEFVAR_LISP ("cursor-in-non-selected-windows",
15898 &Vcursor_in_non_selected_windows,
15899 doc: /* *Cursor type to display in non-selected windows.
15900t means to use hollow box cursor. See `cursor-type' for other values. */);
15901 Vcursor_in_non_selected_windows = Qt;
15902
cfe03a41
KS
15903 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
15904 doc: /* Alist specifying how to blink the cursor off.
15905Each element has the form (ON-STATE . OFF-STATE). Whenever the
15906`cursor-type' frame-parameter or variable equals ON-STATE,
15907comparing using `equal', Emacs uses OFF-STATE to specify
15908how to blink it off. */);
15909 Vblink_cursor_alist = Qnil;
2311178e 15910
e76d28d5 15911 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 15912 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 15913 automatic_hscrolling_p = 1;
1df7e8f0 15914
e76d28d5 15915 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
15916 doc: /* *How many columns away from the window edge point is allowed to get
15917before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 15918 hscroll_margin = 5;
1df7e8f0 15919
e76d28d5 15920 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
15921 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15922When point is less than `automatic-hscroll-margin' columns from the window
15923edge, automatic hscrolling will scroll the window by the amount of columns
15924determined by this variable. If its value is a positive integer, scroll that
15925many columns. If it's a positive floating-point number, it specifies the
15926fraction of the window's width to scroll. If it's nil or zero, point will be
15927centered horizontally after the scroll. Any other value, including negative
15928numbers, are treated as if the value were zero.
15929
15930Automatic hscrolling always moves point outside the scroll margin, so if
15931point was more than scroll step columns inside the margin, the window will
15932scroll more than the value given by the scroll step.
15933
15934Note that the lower bound for automatic hscrolling specified by `scroll-left'
15935and `scroll-right' overrides this variable's effect. */);
e76d28d5 15936 Vhscroll_step = make_number (0);
2311178e 15937
7ee72033
MB
15938 DEFVAR_LISP ("image-types", &Vimage_types,
15939 doc: /* List of supported image types.
228299fa 15940Each element of the list is a symbol for a supported image type. */);
e00daaa0 15941 Vimage_types = Qnil;
2311178e 15942
7ee72033
MB
15943 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15944 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 15945Bind this around calls to `message' to let it take effect. */);
ad4f174e 15946 message_truncate_lines = 0;
0bca8940 15947
7ee72033
MB
15948 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15949 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 15950Can be used to update submenus whose contents should vary. */);
6422c1d7 15951 Vmenu_bar_update_hook = Qnil;
2311178e 15952
7ee72033
MB
15953 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15954 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 15955 inhibit_menubar_update = 0;
30a3f61c 15956
7ee72033
MB
15957 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15958 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 15959 inhibit_eval_during_redisplay = 0;
76cb5e06 15960
26683087
RS
15961 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
15962 doc: /* Non-nil means don't free realized faces. Internal use only. */);
15963 inhibit_free_realized_faces = 0;
15964
69d1f7c9 15965#if GLYPH_DEBUG
76cb5e06
GM
15966 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15967 doc: /* Inhibit try_window_id display optimization. */);
15968 inhibit_try_window_id = 0;
15969
15970 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15971 doc: /* Inhibit try_window_reusing display optimization. */);
15972 inhibit_try_window_reusing = 0;
15973
15974 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15975 doc: /* Inhibit try_cursor_movement display optimization. */);
15976 inhibit_try_cursor_movement = 0;
15977#endif /* GLYPH_DEBUG */
a2889657
JB
15978}
15979
5f5c8ee5
GM
15980
15981/* Initialize this module when Emacs starts. */
15982
dfcf069d 15983void
a2889657
JB
15984init_xdisp ()
15985{
15986 Lisp_Object root_window;
5f5c8ee5 15987 struct window *mini_w;
a2889657 15988
04612a64
GM
15989 current_header_line_height = current_mode_line_height = -1;
15990
5f5c8ee5 15991 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
15992
15993 mini_w = XWINDOW (minibuf_window);
11e82b76 15994 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 15995
a2889657
JB
15996 if (!noninteractive)
15997 {
5f5c8ee5
GM
15998 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15999 int i;
16000
ac90c44f 16001 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
12c226c5 16002 set_window_height (root_window,
5f5c8ee5 16003 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 16004 0);
ac90c44f 16005 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
a2889657
JB
16006 set_window_height (minibuf_window, 1, 0);
16007
ac90c44f
GM
16008 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
16009 mini_w->width = make_number (FRAME_WIDTH (f));
5f5c8ee5
GM
16010
16011 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
16012 scratch_glyph_row.glyphs[TEXT_AREA + 1]
16013 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
16014
2311178e 16015 /* The default ellipsis glyphs `...'. */
5f5c8ee5 16016 for (i = 0; i < 3; ++i)
ac90c44f 16017 default_invis_vector[i] = make_number ('.');
a2889657 16018 }
5f5c8ee5 16019
5f5c8ee5 16020 {
93da8435
SM
16021 /* Allocate the buffer for frame titles.
16022 Also used for `format-mode-line'. */
5f5c8ee5
GM
16023 int size = 100;
16024 frame_title_buf = (char *) xmalloc (size);
16025 frame_title_buf_end = frame_title_buf + size;
16026 frame_title_ptr = NULL;
16027 }
2311178e 16028
21fdfb65 16029 help_echo_showing_p = 0;
a2889657 16030}
5f5c8ee5
GM
16031
16032