* gtkutil.c (xg_frame_cleared): Call gtk_widget_queue_draw for
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
3824b1a6 2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99, 2000, 2001, 2002, 2003
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"
f9b3d256 190#include "blockinput.h"
dfcf069d 191
6d55d620 192#ifdef HAVE_X_WINDOWS
dfcf069d
AS
193#include "xterm.h"
194#endif
a75dfea0
AI
195#ifdef WINDOWSNT
196#include "w32term.h"
197#endif
e0f712ba 198#ifdef MAC_OS
1a578e9b
AC
199#include "macterm.h"
200#endif
a2889657 201
5f5c8ee5
GM
202#define INFINITY 10000000
203
488dd4c4
JD
204#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
205 || defined (USE_GTK)
2e621225 206extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 207extern int pending_menu_activation;
76412d64
RS
208#endif
209
a2889657
JB
210extern int interrupt_input;
211extern int command_loop_level;
212
b6436d4e 213extern int minibuffer_auto_raise;
b2b5455d 214extern Lisp_Object Vminibuffer_list;
b6436d4e 215
c4628384 216extern Lisp_Object Qface;
fec8f23e 217extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
c4628384 218
399164b4
KH
219extern Lisp_Object Voverriding_local_map;
220extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 221extern Lisp_Object Qmenu_item;
0b063c27 222extern Lisp_Object Qwhen;
399164b4 223
d46fb96a 224Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 225Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 226Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 227Lisp_Object Qinhibit_point_motion_hooks;
0b063c27 228Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
5f5c8ee5 229Lisp_Object Qfontified;
6422c1d7 230Lisp_Object Qgrow_only;
869fb12c 231Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 232Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5 233
cfe03a41
KS
234/* Cursor shapes */
235Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
236
c53a1624
RS
237Lisp_Object Qrisky_local_variable;
238
7033d6df
RS
239/* Holds the list (error). */
240Lisp_Object list_of_error;
241
5f5c8ee5
GM
242/* Functions called to fontify regions of text. */
243
244Lisp_Object Vfontification_functions;
245Lisp_Object Qfontification_functions;
246
e037b9ec 247/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
248 over them. */
249
e037b9ec 250int auto_raise_tool_bar_buttons_p;
5f5c8ee5 251
e037b9ec 252/* Margin around tool bar buttons in pixels. */
5f5c8ee5 253
35a41507 254Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 255
e037b9ec 256/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 257
31ade731 258EMACS_INT tool_bar_button_relief;
5f5c8ee5 259
e037b9ec 260/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
261 items are visible, and no blank lines remain. */
262
e037b9ec 263int auto_resize_tool_bars_p;
399164b4 264
735c094c
KH
265/* Non-nil means don't actually do any redisplay. */
266
267Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
268
30a3f61c
GM
269/* Non-zero means Lisp evaluation during redisplay is inhibited. */
270
869fb12c 271int inhibit_eval_during_redisplay;
30a3f61c 272
5f5c8ee5
GM
273/* Names of text properties relevant for redisplay. */
274
a7e27ef7 275Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
4b41cebb 276extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
277
278/* Symbols used in text property values. */
279
280Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 281Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 282Lisp_Object Qmargin;
a7e27ef7 283extern Lisp_Object Qheight;
133c764e 284extern Lisp_Object QCwidth, QCheight, QCascent;
5f5c8ee5 285
8f897821 286/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 287
8f897821 288Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
289
290/* Name of the face used to highlight trailing whitespace. */
291
292Lisp_Object Qtrailing_whitespace;
293
294/* The symbol `image' which is the car of the lists used to represent
295 images in Lisp. */
296
297Lisp_Object Qimage;
298
299/* Non-zero means print newline to stdout before next mini-buffer
300 message. */
a2889657
JB
301
302int noninteractive_need_newline;
303
5f5c8ee5 304/* Non-zero means print newline to message log before next message. */
f88eb0b6 305
3c6595e0 306static int message_log_need_newline;
f88eb0b6 307
b14bc55e
RS
308/* Three markers that message_dolog uses.
309 It could allocate them itself, but that causes trouble
310 in handling memory-full errors. */
311static Lisp_Object message_dolog_marker1;
312static Lisp_Object message_dolog_marker2;
313static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
314\f
315/* The buffer position of the first character appearing entirely or
316 partially on the line of the selected window which contains the
317 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
318 redisplay optimization in redisplay_internal. */
a2889657 319
5f5c8ee5 320static struct text_pos this_line_start_pos;
a2889657 321
5f5c8ee5
GM
322/* Number of characters past the end of the line above, including the
323 terminating newline. */
324
325static struct text_pos this_line_end_pos;
326
327/* The vertical positions and the height of this line. */
a2889657 328
a2889657 329static int this_line_vpos;
5f5c8ee5
GM
330static int this_line_y;
331static int this_line_pixel_height;
332
333/* X position at which this display line starts. Usually zero;
334 negative if first character is partially visible. */
335
336static int this_line_start_x;
a2889657 337
5f5c8ee5 338/* Buffer that this_line_.* variables are referring to. */
a2889657 339
a2889657
JB
340static struct buffer *this_line_buffer;
341
5f5c8ee5
GM
342/* Nonzero means truncate lines in all windows less wide than the
343 frame. */
a2889657 344
a2889657
JB
345int truncate_partial_width_windows;
346
7bbe686f 347/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 348
7bbe686f 349int unibyte_display_via_language_environment;
2311178e 350
5f5c8ee5
GM
351/* Nonzero means we have more than one non-mini-buffer-only frame.
352 Not guaranteed to be accurate except while parsing
353 frame-title-format. */
7bbe686f 354
d39b6696
KH
355int multiple_frames;
356
a2889657
JB
357Lisp_Object Vglobal_mode_string;
358
359/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 360
a2889657
JB
361Lisp_Object Voverlay_arrow_position;
362
5f5c8ee5
GM
363/* String to display for the arrow. Only used on terminal frames. */
364
a2889657
JB
365Lisp_Object Voverlay_arrow_string;
366
5f5c8ee5
GM
367/* Values of those variables at last redisplay. However, if
368 Voverlay_arrow_position is a marker, last_arrow_position is its
369 numerical position. */
370
d45de95b
RS
371static Lisp_Object last_arrow_position, last_arrow_string;
372
5f5c8ee5
GM
373/* Like mode-line-format, but for the title bar on a visible frame. */
374
d39b6696
KH
375Lisp_Object Vframe_title_format;
376
5f5c8ee5
GM
377/* Like mode-line-format, but for the title bar on an iconified frame. */
378
d39b6696
KH
379Lisp_Object Vicon_title_format;
380
08b610e4
RS
381/* List of functions to call when a window's size changes. These
382 functions get one arg, a frame on which one or more windows' sizes
383 have changed. */
5f5c8ee5 384
08b610e4
RS
385static Lisp_Object Vwindow_size_change_functions;
386
0bca8940 387Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 388
a2889657 389/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 390
5f5c8ee5 391static int overlay_arrow_seen;
ca26e1c8 392
fba9ce76 393/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 394
5f5c8ee5
GM
395int highlight_nonselected_windows;
396
397/* If cursor motion alone moves point off frame, try scrolling this
398 many lines up or down if that will bring it back. */
399
31ade731 400static EMACS_INT scroll_step;
a2889657 401
4b41cebb 402/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
403 screen, when appropriate. */
404
31ade731 405static EMACS_INT scroll_conservatively;
0789adb2 406
5f5c8ee5
GM
407/* Recenter the window whenever point gets within this many lines of
408 the top or bottom of the window. This value is translated into a
409 pixel value by multiplying it with CANON_Y_UNIT, which means that
410 there is really a fixed pixel height scroll margin. */
411
31ade731 412EMACS_INT scroll_margin;
9afd2168 413
5f5c8ee5
GM
414/* Number of windows showing the buffer of the selected window (or
415 another buffer with the same base buffer). keyboard.c refers to
416 this. */
a2889657 417
a2889657
JB
418int buffer_shared;
419
5f5c8ee5 420/* Vector containing glyphs for an ellipsis `...'. */
a2889657 421
5f5c8ee5 422static Lisp_Object default_invis_vector[3];
a2889657 423
1862a24e
MB
424/* Zero means display the mode-line/header-line/menu-bar in the default face
425 (this slightly odd definition is for compatibility with previous versions
426 of emacs), non-zero means display them using their respective faces.
427
428 This variable is deprecated. */
a2889657 429
a2889657
JB
430int mode_line_inverse_video;
431
5f5c8ee5
GM
432/* Prompt to display in front of the mini-buffer contents. */
433
8c5b6a0a 434Lisp_Object minibuf_prompt;
a2889657 435
5f5c8ee5
GM
436/* Width of current mini-buffer prompt. Only set after display_line
437 of the line that contains the prompt. */
438
a2889657 439int minibuf_prompt_width;
5f5c8ee5 440
5f5c8ee5
GM
441/* This is the window where the echo area message was displayed. It
442 is always a mini-buffer window, but it may not be the same window
443 currently active as a mini-buffer. */
444
73af359d
RS
445Lisp_Object echo_area_window;
446
c6e89d6c
GM
447/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
448 pushes the current message and the value of
449 message_enable_multibyte on the stack, the function restore_message
450 pops the stack and displays MESSAGE again. */
451
452Lisp_Object Vmessage_stack;
453
a3788d53
RS
454/* Nonzero means multibyte characters were enabled when the echo area
455 message was specified. */
5f5c8ee5 456
a3788d53
RS
457int message_enable_multibyte;
458
4b41cebb 459/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 460
a2889657
JB
461int update_mode_lines;
462
5f5c8ee5 463/* Nonzero if window sizes or contents have changed since last
4b41cebb 464 redisplay that finished. */
5f5c8ee5 465
a2889657
JB
466int windows_or_buffers_changed;
467
5fb96e96
RS
468/* Nonzero means a frame's cursor type has been changed. */
469
470int cursor_type_changed;
471
5f5c8ee5
GM
472/* Nonzero after display_mode_line if %l was used and it displayed a
473 line number. */
474
aa6d10fa
RS
475int line_number_displayed;
476
477/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 478
090703f4 479Lisp_Object Vline_number_display_limit;
5992c4f7 480
4b41cebb 481/* Line width to consider when repositioning for line number display. */
5d121aec 482
31ade731 483static EMACS_INT line_number_display_limit_width;
5d121aec 484
5f5c8ee5
GM
485/* Number of lines to keep in the message log buffer. t means
486 infinite. nil means don't log at all. */
487
5992c4f7 488Lisp_Object Vmessage_log_max;
d45de95b 489
6a94510a
GM
490/* The name of the *Messages* buffer, a string. */
491
492static Lisp_Object Vmessages_buffer_name;
493
c6e89d6c
GM
494/* Current, index 0, and last displayed echo area message. Either
495 buffers from echo_buffers, or nil to indicate no message. */
496
497Lisp_Object echo_area_buffer[2];
498
499/* The buffers referenced from echo_area_buffer. */
500
501static Lisp_Object echo_buffer[2];
502
503/* A vector saved used in with_area_buffer to reduce consing. */
504
505static Lisp_Object Vwith_echo_area_save_vector;
506
507/* Non-zero means display_echo_area should display the last echo area
508 message again. Set by redisplay_preserve_echo_area. */
509
510static int display_last_displayed_message_p;
511
512/* Nonzero if echo area is being used by print; zero if being used by
513 message. */
514
515int message_buf_print;
516
e1477f43
GM
517/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
518
519Lisp_Object Qinhibit_menubar_update;
520int inhibit_menubar_update;
521
9142dd5b
GM
522/* Maximum height for resizing mini-windows. Either a float
523 specifying a fraction of the available height, or an integer
524 specifying a number of lines. */
c6e89d6c 525
ad4f174e
GM
526Lisp_Object Vmax_mini_window_height;
527
528/* Non-zero means messages should be displayed with truncated
529 lines instead of being continued. */
530
531int message_truncate_lines;
532Lisp_Object Qmessage_truncate_lines;
c6e89d6c 533
6e019995
GM
534/* Set to 1 in clear_message to make redisplay_internal aware
535 of an emptied echo area. */
536
537static int message_cleared_p;
538
d6d26ed3
GM
539/* Non-zero means we want a hollow cursor in windows that are not
540 selected. Zero means there's no cursor in such windows. */
541
cfe03a41 542Lisp_Object Vcursor_in_non_selected_windows;
af79bccb 543Lisp_Object Qcursor_in_non_selected_windows;
d6d26ed3 544
cfe03a41
KS
545/* How to blink the default frame cursor off. */
546Lisp_Object Vblink_cursor_alist;
547
5f5c8ee5
GM
548/* A scratch glyph row with contents used for generating truncation
549 glyphs. Also used in direct_output_for_insert. */
12adba34 550
5f5c8ee5
GM
551#define MAX_SCRATCH_GLYPHS 100
552struct glyph_row scratch_glyph_row;
553static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 554
5f5c8ee5
GM
555/* Ascent and height of the last line processed by move_it_to. */
556
557static int last_max_ascent, last_height;
558
21fdfb65
GM
559/* Non-zero if there's a help-echo in the echo area. */
560
561int help_echo_showing_p;
562
04612a64
GM
563/* If >= 0, computed, exact values of mode-line and header-line height
564 to use in the macros CURRENT_MODE_LINE_HEIGHT and
565 CURRENT_HEADER_LINE_HEIGHT. */
566
567int current_mode_line_height, current_header_line_height;
568
5f5c8ee5 569/* The maximum distance to look ahead for text properties. Values
2311178e 570 that are too small let us call compute_char_face and similar
5f5c8ee5
GM
571 functions too often which is expensive. Values that are too large
572 let us call compute_char_face and alike too often because we
573 might not be interested in text properties that far away. */
574
575#define TEXT_PROP_DISTANCE_LIMIT 100
576
47589c8c
GM
577#if GLYPH_DEBUG
578
76cb5e06
GM
579/* Variables to turn off display optimizations from Lisp. */
580
581int inhibit_try_window_id, inhibit_try_window_reusing;
582int inhibit_try_cursor_movement;
583
5f5c8ee5
GM
584/* Non-zero means print traces of redisplay if compiled with
585 GLYPH_DEBUG != 0. */
586
5f5c8ee5 587int trace_redisplay_p;
47589c8c 588
546a4f00 589#endif /* GLYPH_DEBUG */
47589c8c 590
546a4f00
AI
591#ifdef DEBUG_TRACE_MOVE
592/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
593int trace_move;
594
47589c8c
GM
595#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
596#else
91c3f500 597#define TRACE_MOVE(x) (void) 0
5f5c8ee5 598#endif
2311178e 599
d475bcb8
GM
600/* Non-zero means automatically scroll windows horizontally to make
601 point visible. */
602
603int automatic_hscrolling_p;
604
1df7e8f0
EZ
605/* How close to the margin can point get before the window is scrolled
606 horizontally. */
5d335845 607EMACS_INT hscroll_margin;
1df7e8f0
EZ
608
609/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 610Lisp_Object Vhscroll_step;
1df7e8f0 611
e00daaa0
GM
612/* A list of symbols, one for each supported image type. */
613
614Lisp_Object Vimage_types;
615
6422c1d7 616/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 617 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
618 display. If `grow-only', let mini-windows grow only until they
619 become empty. */
620
621Lisp_Object Vresize_mini_windows;
622
82a7ab23
RS
623/* Buffer being redisplayed -- for redisplay_window_error. */
624
625struct buffer *displayed_buffer;
626
5f5c8ee5
GM
627/* Value returned from text property handlers (see below). */
628
629enum prop_handled
3c6595e0 630{
5f5c8ee5
GM
631 HANDLED_NORMALLY,
632 HANDLED_RECOMPUTE_PROPS,
633 HANDLED_OVERLAY_STRING_CONSUMED,
634 HANDLED_RETURN
635};
3c6595e0 636
5f5c8ee5
GM
637/* A description of text properties that redisplay is interested
638 in. */
3c6595e0 639
5f5c8ee5
GM
640struct props
641{
642 /* The name of the property. */
643 Lisp_Object *name;
90adcf20 644
5f5c8ee5
GM
645 /* A unique index for the property. */
646 enum prop_idx idx;
647
648 /* A handler function called to set up iterator IT from the property
649 at IT's current position. Value is used to steer handle_stop. */
650 enum prop_handled (*handler) P_ ((struct it *it));
651};
652
653static enum prop_handled handle_face_prop P_ ((struct it *));
654static enum prop_handled handle_invisible_prop P_ ((struct it *));
655static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 656static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
657static enum prop_handled handle_overlay_change P_ ((struct it *));
658static enum prop_handled handle_fontified_prop P_ ((struct it *));
659
660/* Properties handled by iterators. */
661
662static struct props it_props[] =
5992c4f7 663{
5f5c8ee5
GM
664 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
665 /* Handle `face' before `display' because some sub-properties of
666 `display' need to know the face. */
667 {&Qface, FACE_PROP_IDX, handle_face_prop},
668 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
669 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 670 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
671 {NULL, 0, NULL}
672};
5992c4f7 673
5f5c8ee5
GM
674/* Value is the position described by X. If X is a marker, value is
675 the marker_position of X. Otherwise, value is X. */
12adba34 676
5f5c8ee5 677#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 678
5f5c8ee5 679/* Enumeration returned by some move_it_.* functions internally. */
12adba34 680
5f5c8ee5
GM
681enum move_it_result
682{
683 /* Not used. Undefined value. */
684 MOVE_UNDEFINED,
bab29e15 685
5f5c8ee5
GM
686 /* Move ended at the requested buffer position or ZV. */
687 MOVE_POS_MATCH_OR_ZV,
bab29e15 688
5f5c8ee5
GM
689 /* Move ended at the requested X pixel position. */
690 MOVE_X_REACHED,
12adba34 691
5f5c8ee5
GM
692 /* Move within a line ended at the end of a line that must be
693 continued. */
694 MOVE_LINE_CONTINUED,
2311178e 695
5f5c8ee5
GM
696 /* Move within a line ended at the end of a line that would
697 be displayed truncated. */
698 MOVE_LINE_TRUNCATED,
ff6c30e5 699
5f5c8ee5
GM
700 /* Move within a line ended at a line end. */
701 MOVE_NEWLINE_OR_CR
702};
12adba34 703
1987b083
RS
704/* This counter is used to clear the face cache every once in a while
705 in redisplay_internal. It is incremented for each redisplay.
706 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
707 cleared. */
708
709#define CLEAR_FACE_CACHE_COUNT 500
710static int clear_face_cache_count;
711
712/* Record the previous terminal frame we displayed. */
713
714static struct frame *previous_terminal_frame;
715
716/* Non-zero while redisplay_internal is in progress. */
717
718int redisplaying_p;
719
26683087
RS
720/* Non-zero means don't free realized faces. Bound while freeing
721 realized faces is dangerous because glyph matrices might still
722 reference them. */
1987b083 723
26683087
RS
724int inhibit_free_realized_faces;
725Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 726
5f5c8ee5
GM
727\f
728/* Function prototypes. */
729
5a08cbaf 730static void setup_for_ellipsis P_ ((struct it *));
43c09969 731static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
732static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
733static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 734static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 735static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 736static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
737
738#if 0
2e621225 739static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
740#endif
741
2e621225
GM
742static int next_element_from_ellipsis P_ ((struct it *));
743static void pint2str P_ ((char *, int, int));
744static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
745 struct text_pos));
746static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
747static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
748static void store_frame_title_char P_ ((char));
50f80c2f 749static int store_frame_title P_ ((const unsigned char *, int, int));
2e621225
GM
750static void x_consider_frame_title P_ ((Lisp_Object));
751static void handle_stop P_ ((struct it *));
752static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 753static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 754static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
755static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
756static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 757static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
758 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
759 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 760static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
761static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
763static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 764static int display_echo_area P_ ((struct window *));
23dd2d97
KR
765static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
766static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 767static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 768static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
769static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
770 struct text_pos));
771static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 772static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
773static void insert_left_trunc_glyphs P_ ((struct it *));
774static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
775static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 776static int append_space P_ ((struct it *, int));
cb617e7c 777static int make_cursor_line_fully_visible P_ ((struct window *));
03b0a4b4 778static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 779static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
780static int trailing_whitespace_p P_ ((int));
781static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
782static void push_it P_ ((struct it *));
783static void pop_it P_ ((struct it *));
784static void sync_frame_with_window_matrix_rows P_ ((struct window *));
785static void redisplay_internal P_ ((int));
c6e89d6c 786static int echo_area_display P_ ((int));
5f5c8ee5
GM
787static void redisplay_windows P_ ((Lisp_Object));
788static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
789static Lisp_Object redisplay_window_error ();
790static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
791static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
792static void update_menu_bar P_ ((struct frame *, int));
793static int try_window_reusing_current_matrix P_ ((struct window *));
794static int try_window_id P_ ((struct window *));
795static int display_line P_ ((struct it *));
715e84c9 796static int display_mode_lines P_ ((struct window *));
04612a64 797static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 798static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 799static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 800static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
801static void display_menu_bar P_ ((struct window *));
802static int display_count_lines P_ ((int, int, int, int, int *));
803static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
804 int, int, struct it *, int, int, int, int));
805static void compute_line_metrics P_ ((struct it *));
806static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 807static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 808static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
809static void reseat P_ ((struct it *, struct text_pos, int));
810static void reseat_1 P_ ((struct it *, struct text_pos, int));
811static void back_to_previous_visible_line_start P_ ((struct it *));
812static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 813static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
814static int next_element_from_display_vector P_ ((struct it *));
815static int next_element_from_string P_ ((struct it *));
816static int next_element_from_c_string P_ ((struct it *));
817static int next_element_from_buffer P_ ((struct it *));
260a86a0 818static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
819static int next_element_from_image P_ ((struct it *));
820static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 821static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
822static int init_from_display_pos P_ ((struct it *, struct window *,
823 struct display_pos *));
5f5c8ee5
GM
824static void reseat_to_string P_ ((struct it *, unsigned char *,
825 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
826static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
827 int, int, int));
828void move_it_vertically_backward P_ ((struct it *, int));
829static void init_to_row_start P_ ((struct it *, struct window *,
830 struct glyph_row *));
47d57b22
GM
831static int init_to_row_end P_ ((struct it *, struct window *,
832 struct glyph_row *));
5f5c8ee5 833static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 834static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
835static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
836 Lisp_Object, int));
837static struct text_pos string_pos P_ ((int, Lisp_Object));
838static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
839static int number_of_chars P_ ((unsigned char *, int));
840static void compute_stop_pos P_ ((struct it *));
841static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
842 Lisp_Object));
843static int face_before_or_after_it_pos P_ ((struct it *, int));
844static int next_overlay_change P_ ((int));
845static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
846 Lisp_Object, struct text_pos *,
847 int));
06a12811 848static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
849static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
850 struct window *));
5f5c8ee5
GM
851
852#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
853#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 854
5f5c8ee5 855#ifdef HAVE_WINDOW_SYSTEM
12adba34 856
e037b9ec
GM
857static void update_tool_bar P_ ((struct frame *, int));
858static void build_desired_tool_bar_string P_ ((struct frame *f));
859static int redisplay_tool_bar P_ ((struct frame *));
860static void display_tool_bar_line P_ ((struct it *));
12adba34 861
5f5c8ee5 862#endif /* HAVE_WINDOW_SYSTEM */
12adba34 863
5f5c8ee5
GM
864\f
865/***********************************************************************
866 Window display dimensions
867 ***********************************************************************/
12adba34 868
40a301b3
RS
869/* Return the bottom boundary y-position for text lines in window W.
870 This is the first y position at which a line cannot start.
871 It is relative to the top of the window.
872
873 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
874
875INLINE int
876window_text_bottom_y (w)
877 struct window *w;
878{
879 struct frame *f = XFRAME (w->frame);
880 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 881
5f5c8ee5
GM
882 if (WINDOW_WANTS_MODELINE_P (w))
883 height -= CURRENT_MODE_LINE_HEIGHT (w);
884 return height;
f88eb0b6
KH
885}
886
f82aff7c 887
5f5c8ee5 888/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 889 means return the total width of W, not including fringes to
5f5c8ee5 890 the left and right of the window. */
ff6c30e5 891
5f5c8ee5
GM
892INLINE int
893window_box_width (w, area)
894 struct window *w;
895 int area;
896{
897 struct frame *f = XFRAME (w->frame);
898 int width = XFASTINT (w->width);
2311178e 899
5f5c8ee5 900 if (!w->pseudo_window_p)
ff6c30e5 901 {
b46952ae 902 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
2311178e 903
5f5c8ee5
GM
904 if (area == TEXT_AREA)
905 {
906 if (INTEGERP (w->left_margin_width))
907 width -= XFASTINT (w->left_margin_width);
908 if (INTEGERP (w->right_margin_width))
909 width -= XFASTINT (w->right_margin_width);
910 }
911 else if (area == LEFT_MARGIN_AREA)
912 width = (INTEGERP (w->left_margin_width)
913 ? XFASTINT (w->left_margin_width) : 0);
914 else if (area == RIGHT_MARGIN_AREA)
915 width = (INTEGERP (w->right_margin_width)
916 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 917 }
5f5c8ee5
GM
918
919 return width * CANON_X_UNIT (f);
ff6c30e5 920}
1adc55de 921
1adc55de 922
5f5c8ee5 923/* Return the pixel height of the display area of window W, not
4b41cebb 924 including mode lines of W, if any. */
f88eb0b6 925
5f5c8ee5
GM
926INLINE int
927window_box_height (w)
928 struct window *w;
f88eb0b6 929{
5f5c8ee5
GM
930 struct frame *f = XFRAME (w->frame);
931 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
932
933 xassert (height >= 0);
2311178e 934
d9c9e99c
MB
935 /* Note: the code below that determines the mode-line/header-line
936 height is essentially the same as that contained in the macro
937 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
938 the appropriate glyph row has its `mode_line_p' flag set,
939 and if it doesn't, uses estimate_mode_line_height instead. */
940
5f5c8ee5 941 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
942 {
943 struct glyph_row *ml_row
944 = (w->current_matrix && w->current_matrix->rows
945 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
946 : 0);
947 if (ml_row && ml_row->mode_line_p)
948 height -= ml_row->height;
949 else
96d2320f 950 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 951 }
5f5c8ee5 952
045dee35 953 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
954 {
955 struct glyph_row *hl_row
956 = (w->current_matrix && w->current_matrix->rows
957 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
958 : 0);
959 if (hl_row && hl_row->mode_line_p)
960 height -= hl_row->height;
961 else
962 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
963 }
5f5c8ee5 964
7c75be36
PJ
965 /* With a very small font and a mode-line that's taller than
966 default, we might end up with a negative height. */
967 return max (0, height);
5992c4f7
KH
968}
969
970
5f5c8ee5
GM
971/* Return the frame-relative coordinate of the left edge of display
972 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 973 whole window, to the right of the left fringe of W. */
5992c4f7 974
5f5c8ee5
GM
975INLINE int
976window_box_left (w, area)
977 struct window *w;
978 int area;
90adcf20 979{
5f5c8ee5
GM
980 struct frame *f = XFRAME (w->frame);
981 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 982
5f5c8ee5 983 if (!w->pseudo_window_p)
90adcf20 984 {
5f5c8ee5 985 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
b46952ae 986 + FRAME_LEFT_FRINGE_WIDTH (f));
2311178e 987
5f5c8ee5
GM
988 if (area == TEXT_AREA)
989 x += window_box_width (w, LEFT_MARGIN_AREA);
990 else if (area == RIGHT_MARGIN_AREA)
991 x += (window_box_width (w, LEFT_MARGIN_AREA)
992 + window_box_width (w, TEXT_AREA));
90adcf20 993 }
73af359d 994
5f5c8ee5 995 return x;
2311178e 996}
90adcf20 997
b6436d4e 998
5f5c8ee5
GM
999/* Return the frame-relative coordinate of the right edge of display
1000 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1001 whole window, to the left of the right fringe of W. */
ded34426 1002
5f5c8ee5
GM
1003INLINE int
1004window_box_right (w, area)
1005 struct window *w;
1006 int area;
1007{
1008 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1009}
1010
5f5c8ee5
GM
1011
1012/* Get the bounding box of the display area AREA of window W, without
1013 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1014 whole window, not including the left and right fringes of
5f5c8ee5
GM
1015 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1016 coordinates of the upper-left corner of the box. Return in
1017 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1018
1019INLINE void
1020window_box (w, area, box_x, box_y, box_width, box_height)
1021 struct window *w;
1022 int area;
1023 int *box_x, *box_y, *box_width, *box_height;
1024{
1025 struct frame *f = XFRAME (w->frame);
2311178e 1026
5f5c8ee5
GM
1027 *box_width = window_box_width (w, area);
1028 *box_height = window_box_height (w);
1029 *box_x = window_box_left (w, area);
1030 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1031 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
1032 if (WINDOW_WANTS_HEADER_LINE_P (w))
1033 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 1034}
1adc55de 1035
1adc55de 1036
5f5c8ee5 1037/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1038 mode lines. AREA < 0 means the whole window, not including the
1039 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1040 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1041 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1042 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1043 box. */
ded34426 1044
5f5c8ee5
GM
1045INLINE void
1046window_box_edges (w, area, top_left_x, top_left_y,
1047 bottom_right_x, bottom_right_y)
1048 struct window *w;
1049 int area;
1050 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1051{
5f5c8ee5
GM
1052 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1053 bottom_right_y);
1054 *bottom_right_x += *top_left_x;
1055 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1056}
1057
5f5c8ee5
GM
1058
1059\f
1060/***********************************************************************
1061 Utilities
1062 ***********************************************************************/
1063
8b6ea97f
GM
1064/* Return the bottom y-position of the line the iterator IT is in.
1065 This can modify IT's settings. */
1066
1067int
1068line_bottom_y (it)
1069 struct it *it;
1070{
1071 int line_height = it->max_ascent + it->max_descent;
1072 int line_top_y = it->current_y;
2311178e 1073
8b6ea97f
GM
1074 if (line_height == 0)
1075 {
1076 if (last_height)
1077 line_height = last_height;
1078 else if (IT_CHARPOS (*it) < ZV)
1079 {
1080 move_it_by_lines (it, 1, 1);
1081 line_height = (it->max_ascent || it->max_descent
1082 ? it->max_ascent + it->max_descent
1083 : last_height);
1084 }
1085 else
1086 {
1087 struct glyph_row *row = it->glyph_row;
2311178e 1088
8b6ea97f
GM
1089 /* Use the default character height. */
1090 it->glyph_row = NULL;
1091 it->what = IT_CHARACTER;
1092 it->c = ' ';
1093 it->len = 1;
1094 PRODUCE_GLYPHS (it);
1095 line_height = it->ascent + it->descent;
1096 it->glyph_row = row;
1097 }
1098 }
1099
1100 return line_top_y + line_height;
1101}
1102
1103
0db95684
GM
1104/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1105 1 if POS is visible and the line containing POS is fully visible.
1106 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1107 and header-lines heights. */
3a641a69
GM
1108
1109int
04612a64 1110pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1111 struct window *w;
04612a64 1112 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1113{
1114 struct it it;
1115 struct text_pos top;
fcab1954
GM
1116 int visible_p;
1117 struct buffer *old_buffer = NULL;
1118
1119 if (XBUFFER (w->buffer) != current_buffer)
1120 {
1121 old_buffer = current_buffer;
1122 set_buffer_internal_1 (XBUFFER (w->buffer));
1123 }
3a641a69
GM
1124
1125 *fully = visible_p = 0;
1126 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1127
04612a64
GM
1128 /* Compute exact mode line heights, if requested. */
1129 if (exact_mode_line_heights_p)
1130 {
1131 if (WINDOW_WANTS_MODELINE_P (w))
1132 current_mode_line_height
96d2320f 1133 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1134 current_buffer->mode_line_format);
2311178e 1135
04612a64
GM
1136 if (WINDOW_WANTS_HEADER_LINE_P (w))
1137 current_header_line_height
1138 = display_mode_line (w, HEADER_LINE_FACE_ID,
1139 current_buffer->header_line_format);
1140 }
3a641a69 1141
04612a64 1142 start_display (&it, w, top);
3a641a69
GM
1143 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1144 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1145
1146 /* Note that we may overshoot because of invisible text. */
1147 if (IT_CHARPOS (it) >= charpos)
3a641a69 1148 {
8b6ea97f
GM
1149 int top_y = it.current_y;
1150 int bottom_y = line_bottom_y (&it);
fcab1954 1151 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
2311178e 1152
8b6ea97f
GM
1153 if (top_y < window_top_y)
1154 visible_p = bottom_y > window_top_y;
1155 else if (top_y < it.last_visible_y)
fcab1954
GM
1156 {
1157 visible_p = 1;
8b6ea97f 1158 *fully = bottom_y <= it.last_visible_y;
fcab1954 1159 }
3a641a69
GM
1160 }
1161 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1162 {
1163 move_it_by_lines (&it, 1, 0);
1164 if (charpos < IT_CHARPOS (it))
1165 {
1166 visible_p = 1;
1167 *fully = 0;
1168 }
1169 }
fcab1954
GM
1170
1171 if (old_buffer)
1172 set_buffer_internal_1 (old_buffer);
04612a64
GM
1173
1174 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1175 return visible_p;
1176}
1177
1178
4fdb80f2
GM
1179/* Return the next character from STR which is MAXLEN bytes long.
1180 Return in *LEN the length of the character. This is like
1181 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1182 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1183 character. */
1184
1185static INLINE int
7a5b8a93 1186string_char_and_length (str, maxlen, len)
50f80c2f 1187 const unsigned char *str;
7a5b8a93 1188 int maxlen, *len;
4fdb80f2
GM
1189{
1190 int c;
1191
1192 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1193 if (!CHAR_VALID_P (c, 1))
1194 /* We may not change the length here because other places in Emacs
9ab8560d 1195 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1196 characters. */
1197 c = '?';
1198
1199 return c;
1200}
1201
1202
1203
5f5c8ee5
GM
1204/* Given a position POS containing a valid character and byte position
1205 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1206
1207static struct text_pos
1208string_pos_nchars_ahead (pos, string, nchars)
1209 struct text_pos pos;
1210 Lisp_Object string;
1211 int nchars;
0b1005ef 1212{
5f5c8ee5
GM
1213 xassert (STRINGP (string) && nchars >= 0);
1214
1215 if (STRING_MULTIBYTE (string))
1216 {
2051c264 1217 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1218 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1219 int len;
1220
1221 while (nchars--)
1222 {
4fdb80f2 1223 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1224 p += len, rest -= len;
1225 xassert (rest >= 0);
1226 CHARPOS (pos) += 1;
1227 BYTEPOS (pos) += len;
1228 }
1229 }
1230 else
1231 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1232
1233 return pos;
0a9dc68b
RS
1234}
1235
0a9dc68b 1236
5f5c8ee5
GM
1237/* Value is the text position, i.e. character and byte position,
1238 for character position CHARPOS in STRING. */
1239
1240static INLINE struct text_pos
1241string_pos (charpos, string)
1242 int charpos;
0a9dc68b 1243 Lisp_Object string;
0a9dc68b 1244{
5f5c8ee5
GM
1245 struct text_pos pos;
1246 xassert (STRINGP (string));
1247 xassert (charpos >= 0);
1248 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1249 return pos;
1250}
1251
1252
1253/* Value is a text position, i.e. character and byte position, for
1254 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1255 means recognize multibyte characters. */
1256
1257static struct text_pos
1258c_string_pos (charpos, s, multibyte_p)
1259 int charpos;
1260 unsigned char *s;
1261 int multibyte_p;
1262{
1263 struct text_pos pos;
1264
1265 xassert (s != NULL);
1266 xassert (charpos >= 0);
1267
1268 if (multibyte_p)
0a9dc68b 1269 {
5f5c8ee5
GM
1270 int rest = strlen (s), len;
1271
1272 SET_TEXT_POS (pos, 0, 0);
1273 while (charpos--)
0a9dc68b 1274 {
4fdb80f2 1275 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1276 s += len, rest -= len;
1277 xassert (rest >= 0);
1278 CHARPOS (pos) += 1;
1279 BYTEPOS (pos) += len;
0a9dc68b
RS
1280 }
1281 }
5f5c8ee5
GM
1282 else
1283 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1284
5f5c8ee5
GM
1285 return pos;
1286}
0a9dc68b 1287
0a9dc68b 1288
5f5c8ee5
GM
1289/* Value is the number of characters in C string S. MULTIBYTE_P
1290 non-zero means recognize multibyte characters. */
0a9dc68b 1291
5f5c8ee5
GM
1292static int
1293number_of_chars (s, multibyte_p)
1294 unsigned char *s;
1295 int multibyte_p;
1296{
1297 int nchars;
2311178e 1298
5f5c8ee5
GM
1299 if (multibyte_p)
1300 {
1301 int rest = strlen (s), len;
1302 unsigned char *p = (unsigned char *) s;
0a9dc68b 1303
5f5c8ee5
GM
1304 for (nchars = 0; rest > 0; ++nchars)
1305 {
4fdb80f2 1306 string_char_and_length (p, rest, &len);
5f5c8ee5 1307 rest -= len, p += len;
0a9dc68b
RS
1308 }
1309 }
5f5c8ee5
GM
1310 else
1311 nchars = strlen (s);
1312
1313 return nchars;
0b1005ef
KH
1314}
1315
2311178e 1316
5f5c8ee5
GM
1317/* Compute byte position NEWPOS->bytepos corresponding to
1318 NEWPOS->charpos. POS is a known position in string STRING.
1319 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1320
5f5c8ee5
GM
1321static void
1322compute_string_pos (newpos, pos, string)
1323 struct text_pos *newpos, pos;
1324 Lisp_Object string;
76412d64 1325{
5f5c8ee5
GM
1326 xassert (STRINGP (string));
1327 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1328
5f5c8ee5 1329 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1330 *newpos = string_pos_nchars_ahead (pos, string,
1331 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1332 else
1333 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1334}
1335
9c74a0dd 1336
5f5c8ee5
GM
1337\f
1338/***********************************************************************
1339 Lisp form evaluation
1340 ***********************************************************************/
1341
116d6f5c 1342/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1343
1344static Lisp_Object
116d6f5c 1345safe_eval_handler (arg)
5f5c8ee5
GM
1346 Lisp_Object arg;
1347{
0dbf9fd2 1348 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1349 return Qnil;
1350}
1351
1352
1353/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1354 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1355
71e5b1b8 1356Lisp_Object
116d6f5c 1357safe_eval (sexpr)
5f5c8ee5
GM
1358 Lisp_Object sexpr;
1359{
5f5c8ee5 1360 Lisp_Object val;
2311178e 1361
30a3f61c
GM
1362 if (inhibit_eval_during_redisplay)
1363 val = Qnil;
1364 else
1365 {
331379bf 1366 int count = SPECPDL_INDEX ();
30a3f61c 1367 struct gcpro gcpro1;
0d8b31c0 1368
30a3f61c
GM
1369 GCPRO1 (sexpr);
1370 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1371 /* Use Qt to ensure debugger does not run,
1372 so there is no possibility of wanting to redisplay. */
1373 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1374 safe_eval_handler);
1375 UNGCPRO;
1376 val = unbind_to (count, val);
1377 }
2311178e 1378
30a3f61c 1379 return val;
0d8b31c0
GM
1380}
1381
1382
1383/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1384 Return the result, or nil if something went wrong. Prevent
1385 redisplay during the evaluation. */
0d8b31c0
GM
1386
1387Lisp_Object
116d6f5c 1388safe_call (nargs, args)
0d8b31c0
GM
1389 int nargs;
1390 Lisp_Object *args;
1391{
0d8b31c0 1392 Lisp_Object val;
2311178e 1393
30a3f61c
GM
1394 if (inhibit_eval_during_redisplay)
1395 val = Qnil;
1396 else
1397 {
331379bf 1398 int count = SPECPDL_INDEX ();
30a3f61c 1399 struct gcpro gcpro1;
0d8b31c0 1400
30a3f61c
GM
1401 GCPRO1 (args[0]);
1402 gcpro1.nvars = nargs;
1403 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1404 /* Use Qt to ensure debugger does not run,
1405 so there is no possibility of wanting to redisplay. */
1406 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1407 safe_eval_handler);
1408 UNGCPRO;
1409 val = unbind_to (count, val);
1410 }
1411
1412 return val;
5f5c8ee5
GM
1413}
1414
1415
116d6f5c
GM
1416/* Call function FN with one argument ARG.
1417 Return the result, or nil if something went wrong. */
1418
1419Lisp_Object
1420safe_call1 (fn, arg)
1421 Lisp_Object fn, arg;
1422{
1423 Lisp_Object args[2];
1424 args[0] = fn;
1425 args[1] = arg;
1426 return safe_call (2, args);
1427}
1428
1429
5f5c8ee5
GM
1430\f
1431/***********************************************************************
1432 Debugging
1433 ***********************************************************************/
1434
1435#if 0
1436
1437/* Define CHECK_IT to perform sanity checks on iterators.
1438 This is for debugging. It is too slow to do unconditionally. */
1439
1440static void
1441check_it (it)
1442 struct it *it;
1443{
1444 if (it->method == next_element_from_string)
a2889657 1445 {
5f5c8ee5
GM
1446 xassert (STRINGP (it->string));
1447 xassert (IT_STRING_CHARPOS (*it) >= 0);
1448 }
1449 else if (it->method == next_element_from_buffer)
1450 {
1451 /* Check that character and byte positions agree. */
1452 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1453 }
73af359d 1454
5f5c8ee5
GM
1455 if (it->dpvec)
1456 xassert (it->current.dpvec_index >= 0);
1457 else
1458 xassert (it->current.dpvec_index < 0);
1459}
1f40cad2 1460
5f5c8ee5
GM
1461#define CHECK_IT(IT) check_it ((IT))
1462
1463#else /* not 0 */
1464
1465#define CHECK_IT(IT) (void) 0
1466
1467#endif /* not 0 */
1468
1469
1470#if GLYPH_DEBUG
1471
1472/* Check that the window end of window W is what we expect it
1473 to be---the last row in the current matrix displaying text. */
1474
1475static void
1476check_window_end (w)
1477 struct window *w;
1478{
1479 if (!MINI_WINDOW_P (w)
1480 && !NILP (w->window_end_valid))
1481 {
1482 struct glyph_row *row;
1483 xassert ((row = MATRIX_ROW (w->current_matrix,
1484 XFASTINT (w->window_end_vpos)),
1485 !row->enabled_p
1486 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1487 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1488 }
1489}
1490
1491#define CHECK_WINDOW_END(W) check_window_end ((W))
1492
1493#else /* not GLYPH_DEBUG */
1494
1495#define CHECK_WINDOW_END(W) (void) 0
1496
1497#endif /* not GLYPH_DEBUG */
1498
1499
1500\f
1501/***********************************************************************
1502 Iterator initialization
1503 ***********************************************************************/
1504
1505/* Initialize IT for displaying current_buffer in window W, starting
1506 at character position CHARPOS. CHARPOS < 0 means that no buffer
1507 position is specified which is useful when the iterator is assigned
1508 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1509 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1510
1511 If ROW is not null, calls to produce_glyphs with IT as parameter
1512 will produce glyphs in that row.
1513
1514 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1515 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1516 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1517 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 1518
96d2320f
KS
1519 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1520 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1521 will be initialized to use the corresponding mode line glyph row of
1522 the desired matrix of W. */
5f5c8ee5
GM
1523
1524void
1525init_iterator (it, w, charpos, bytepos, row, base_face_id)
1526 struct it *it;
1527 struct window *w;
1528 int charpos, bytepos;
1529 struct glyph_row *row;
1530 enum face_id base_face_id;
1531{
1532 int highlight_region_p;
5f5c8ee5
GM
1533
1534 /* Some precondition checks. */
1535 xassert (w != NULL && it != NULL);
3b6b6db7
SM
1536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1537 && charpos <= ZV));
5f5c8ee5
GM
1538
1539 /* If face attributes have been changed since the last redisplay,
1540 free realized faces now because they depend on face definitions
7d0393cf 1541 that might have changed. Don't free faces while there might be
1987b083 1542 desired matrices pending which reference these faces. */
26683087 1543 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
1544 {
1545 face_change_count = 0;
1546 free_all_realized_faces (Qnil);
1547 }
1548
1549 /* Use one of the mode line rows of W's desired matrix if
1550 appropriate. */
1551 if (row == NULL)
1552 {
96d2320f
KS
1553 if (base_face_id == MODE_LINE_FACE_ID
1554 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 1555 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1556 else if (base_face_id == HEADER_LINE_FACE_ID)
1557 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 1558 }
2311178e 1559
5f5c8ee5
GM
1560 /* Clear IT. */
1561 bzero (it, sizeof *it);
1562 it->current.overlay_string_index = -1;
1563 it->current.dpvec_index = -1;
5f5c8ee5
GM
1564 it->base_face_id = base_face_id;
1565
1566 /* The window in which we iterate over current_buffer: */
1567 XSETWINDOW (it->window, w);
1568 it->w = w;
1569 it->f = XFRAME (w->frame);
1570
d475bcb8
GM
1571 /* Extra space between lines (on window systems only). */
1572 if (base_face_id == DEFAULT_FACE_ID
1573 && FRAME_WINDOW_P (it->f))
1574 {
1575 if (NATNUMP (current_buffer->extra_line_spacing))
1576 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1577 else if (it->f->extra_line_spacing > 0)
1578 it->extra_line_spacing = it->f->extra_line_spacing;
1579 }
1580
5f5c8ee5 1581 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1582 attribute changes of named faces, recompute them. When running
1583 in batch mode, the face cache of Vterminal_frame is null. If
1584 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1585 if (
1586#ifndef WINDOWSNT
1587 noninteractive &&
1588#endif
1589 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1590 init_frame_faces (it->f);
5f5c8ee5
GM
1591 if (FRAME_FACE_CACHE (it->f)->used == 0)
1592 recompute_basic_faces (it->f);
1593
5f5c8ee5
GM
1594 /* Current value of the `space-width', and 'height' properties. */
1595 it->space_width = Qnil;
1596 it->font_height = Qnil;
2311178e 1597
5f5c8ee5
GM
1598 /* Are control characters displayed as `^C'? */
1599 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1600
1601 /* -1 means everything between a CR and the following line end
1602 is invisible. >0 means lines indented more than this value are
1603 invisible. */
1604 it->selective = (INTEGERP (current_buffer->selective_display)
1605 ? XFASTINT (current_buffer->selective_display)
2311178e 1606 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
1607 ? -1 : 0));
1608 it->selective_display_ellipsis_p
1609 = !NILP (current_buffer->selective_display_ellipses);
1610
1611 /* Display table to use. */
1612 it->dp = window_display_table (w);
1613
1614 /* Are multibyte characters enabled in current_buffer? */
1615 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1616
1617 /* Non-zero if we should highlight the region. */
1618 highlight_region_p
3ebf0ea9 1619 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
1620 && !NILP (current_buffer->mark_active)
1621 && XMARKER (current_buffer->mark)->buffer != 0);
1622
1623 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1624 start and end of a visible region in window IT->w. Set both to
1625 -1 to indicate no region. */
1626 if (highlight_region_p
1627 /* Maybe highlight only in selected window. */
2311178e 1628 && (/* Either show region everywhere. */
5f5c8ee5
GM
1629 highlight_nonselected_windows
1630 /* Or show region in the selected window. */
1631 || w == XWINDOW (selected_window)
1632 /* Or show the region if we are in the mini-buffer and W is
1633 the window the mini-buffer refers to. */
1634 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
1635 && WINDOWP (minibuf_selected_window)
1636 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
1637 {
1638 int charpos = marker_position (current_buffer->mark);
1639 it->region_beg_charpos = min (PT, charpos);
1640 it->region_end_charpos = max (PT, charpos);
1641 }
1642 else
1643 it->region_beg_charpos = it->region_end_charpos = -1;
1644
1645 /* Get the position at which the redisplay_end_trigger hook should
1646 be run, if it is to be run at all. */
1647 if (MARKERP (w->redisplay_end_trigger)
1648 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1649 it->redisplay_end_trigger_charpos
1650 = marker_position (w->redisplay_end_trigger);
1651 else if (INTEGERP (w->redisplay_end_trigger))
1652 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1653
1654 /* Correct bogus values of tab_width. */
1655 it->tab_width = XINT (current_buffer->tab_width);
1656 if (it->tab_width <= 0 || it->tab_width > 1000)
1657 it->tab_width = 8;
1658
1659 /* Are lines in the display truncated? */
1660 it->truncate_lines_p
1661 = (base_face_id != DEFAULT_FACE_ID
1662 || XINT (it->w->hscroll)
1663 || (truncate_partial_width_windows
1664 && !WINDOW_FULL_WIDTH_P (it->w))
1665 || !NILP (current_buffer->truncate_lines));
1666
1667 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 1668 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
1669 frames. */
1670 if (!FRAME_WINDOW_P (it->f))
1671 {
1672 if (it->truncate_lines_p)
1673 {
1674 /* We will need the truncation glyph. */
1675 xassert (it->glyph_row == NULL);
1676 produce_special_glyphs (it, IT_TRUNCATION);
1677 it->truncation_pixel_width = it->pixel_width;
1678 }
1679 else
1680 {
1681 /* We will need the continuation glyph. */
1682 xassert (it->glyph_row == NULL);
1683 produce_special_glyphs (it, IT_CONTINUATION);
1684 it->continuation_pixel_width = it->pixel_width;
1685 }
1686
153c2160 1687 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
1688 above has changed them. */
1689 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1690 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1691 }
1692
1693 /* Set this after getting the dimensions of truncation and
1694 continuation glyphs, so that we don't produce glyphs when calling
1695 produce_special_glyphs, above. */
1696 it->glyph_row = row;
1697 it->area = TEXT_AREA;
1698
1699 /* Get the dimensions of the display area. The display area
1700 consists of the visible window area plus a horizontally scrolled
1701 part to the left of the window. All x-values are relative to the
1702 start of this total display area. */
1703 if (base_face_id != DEFAULT_FACE_ID)
1704 {
1705 /* Mode lines, menu bar in terminal frames. */
1706 it->first_visible_x = 0;
1707 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1708 }
1709 else
1710 {
1711 it->first_visible_x
1712 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1713 it->last_visible_x = (it->first_visible_x
1714 + window_box_width (w, TEXT_AREA));
1715
1716 /* If we truncate lines, leave room for the truncator glyph(s) at
1717 the right margin. Otherwise, leave room for the continuation
1718 glyph(s). Truncation and continuation glyphs are not inserted
1719 for window-based redisplay. */
1720 if (!FRAME_WINDOW_P (it->f))
1721 {
1722 if (it->truncate_lines_p)
1723 it->last_visible_x -= it->truncation_pixel_width;
1724 else
1725 it->last_visible_x -= it->continuation_pixel_width;
1726 }
1727
045dee35
GM
1728 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1729 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1730 }
1731
1732 /* Leave room for a border glyph. */
1733 if (!FRAME_WINDOW_P (it->f)
1734 && !WINDOW_RIGHTMOST_P (it->w))
1735 it->last_visible_x -= 1;
1736
1737 it->last_visible_y = window_text_bottom_y (w);
1738
1739 /* For mode lines and alike, arrange for the first glyph having a
1740 left box line if the face specifies a box. */
1741 if (base_face_id != DEFAULT_FACE_ID)
1742 {
1743 struct face *face;
2311178e 1744
5f5c8ee5
GM
1745 it->face_id = base_face_id;
1746
1747 /* If we have a boxed mode line, make the first character appear
1748 with a left box line. */
1749 face = FACE_FROM_ID (it->f, base_face_id);
1750 if (face->box != FACE_NO_BOX)
1751 it->start_of_box_run_p = 1;
1752 }
1753
1754 /* If a buffer position was specified, set the iterator there,
1755 getting overlays and face properties from that position. */
3ebf0ea9 1756 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
1757 {
1758 it->end_charpos = ZV;
1759 it->face_id = -1;
1760 IT_CHARPOS (*it) = charpos;
2311178e 1761
5f5c8ee5 1762 /* Compute byte position if not specified. */
3ebf0ea9 1763 if (bytepos < charpos)
5f5c8ee5
GM
1764 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1765 else
1766 IT_BYTEPOS (*it) = bytepos;
1767
1768 /* Compute faces etc. */
1769 reseat (it, it->current.pos, 1);
1770 }
2311178e 1771
5f5c8ee5
GM
1772 CHECK_IT (it);
1773}
1774
1775
1776/* Initialize IT for the display of window W with window start POS. */
1777
1778void
1779start_display (it, w, pos)
1780 struct it *it;
1781 struct window *w;
1782 struct text_pos pos;
1783{
5f5c8ee5 1784 struct glyph_row *row;
045dee35 1785 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1786
1787 row = w->desired_matrix->rows + first_vpos;
1788 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
17fdcfc8
GM
1789
1790 if (!it->truncate_lines_p)
5f5c8ee5 1791 {
17fdcfc8
GM
1792 int start_at_line_beg_p;
1793 int first_y = it->current_y;
2311178e 1794
17fdcfc8
GM
1795 /* If window start is not at a line start, skip forward to POS to
1796 get the correct continuation lines width. */
1797 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1798 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1799 if (!start_at_line_beg_p)
5f5c8ee5 1800 {
0e47bbf7
RS
1801 int new_x;
1802
17fdcfc8
GM
1803 reseat_at_previous_visible_line_start (it);
1804 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1805
0e47bbf7
RS
1806 new_x = it->current_x + it->pixel_width;
1807
17fdcfc8
GM
1808 /* If lines are continued, this line may end in the middle
1809 of a multi-glyph character (e.g. a control character
1810 displayed as \003, or in the middle of an overlay
1811 string). In this case move_it_to above will not have
1812 taken us to the start of the continuation line but to the
1813 end of the continued line. */
0e47bbf7
RS
1814 if (it->current_x > 0
1815 && !it->truncate_lines_p /* Lines are continued. */
1816 && (/* And glyph doesn't fit on the line. */
1817 new_x > it->last_visible_x
1818 /* Or it fits exactly and we're on a window
1819 system frame. */
1820 || (new_x == it->last_visible_x
1821 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 1822 {
b28cb6ed
GM
1823 if (it->current.dpvec_index >= 0
1824 || it->current.overlay_string_index >= 0)
1825 {
cafafe0b 1826 set_iterator_to_next (it, 1);
b28cb6ed
GM
1827 move_it_in_display_line_to (it, -1, -1, 0);
1828 }
2311178e 1829
b28cb6ed 1830 it->continuation_lines_width += it->current_x;
5f5c8ee5 1831 }
b28cb6ed
GM
1832
1833 /* We're starting a new display line, not affected by the
1834 height of the continued line, so clear the appropriate
1835 fields in the iterator structure. */
1836 it->max_ascent = it->max_descent = 0;
1837 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 1838
17fdcfc8
GM
1839 it->current_y = first_y;
1840 it->vpos = 0;
1841 it->current_x = it->hpos = 0;
1842 }
5f5c8ee5
GM
1843 }
1844
1845#if 0 /* Don't assert the following because start_display is sometimes
1846 called intentionally with a window start that is not at a
1847 line start. Please leave this code in as a comment. */
2311178e 1848
5f5c8ee5
GM
1849 /* Window start should be on a line start, now. */
1850 xassert (it->continuation_lines_width
1851 || IT_CHARPOS (it) == BEGV
1852 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1853#endif /* 0 */
1854}
1855
1856
4bde0ebb
GM
1857/* Return 1 if POS is a position in ellipses displayed for invisible
1858 text. W is the window we display, for text property lookup. */
5f5c8ee5 1859
4bde0ebb
GM
1860static int
1861in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 1862 struct display_pos *pos;
4bde0ebb 1863 struct window *w;
5f5c8ee5 1864{
ac90c44f 1865 Lisp_Object prop, window;
4bde0ebb
GM
1866 int ellipses_p = 0;
1867 int charpos = CHARPOS (pos->pos);
2311178e 1868
ac90c44f
GM
1869 /* If POS specifies a position in a display vector, this might
1870 be for an ellipsis displayed for invisible text. We won't
1871 get the iterator set up for delivering that ellipsis unless
1872 we make sure that it gets aware of the invisible text. */
1873 if (pos->dpvec_index >= 0
1874 && pos->overlay_string_index < 0
1875 && CHARPOS (pos->string_pos) < 0
1876 && charpos > BEGV
1877 && (XSETWINDOW (window, w),
1878 prop = Fget_char_property (make_number (charpos),
1879 Qinvisible, window),
20fbd925 1880 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
1881 {
1882 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1883 window);
8580a4e3 1884 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
1885 }
1886
1887 return ellipses_p;
1888}
1889
1890
1891/* Initialize IT for stepping through current_buffer in window W,
1892 starting at position POS that includes overlay string and display
47d57b22
GM
1893 vector/ control character translation position information. Value
1894 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 1895
47d57b22 1896static int
4bde0ebb
GM
1897init_from_display_pos (it, w, pos)
1898 struct it *it;
1899 struct window *w;
1900 struct display_pos *pos;
1901{
1902 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 1903 int i, overlay_strings_with_newlines = 0;
2311178e 1904
4bde0ebb
GM
1905 /* If POS specifies a position in a display vector, this might
1906 be for an ellipsis displayed for invisible text. We won't
1907 get the iterator set up for delivering that ellipsis unless
1908 we make sure that it gets aware of the invisible text. */
1909 if (in_ellipses_for_invisible_text_p (pos, w))
1910 {
1911 --charpos;
1912 bytepos = 0;
ac90c44f 1913 }
2311178e 1914
5f5c8ee5
GM
1915 /* Keep in mind: the call to reseat in init_iterator skips invisible
1916 text, so we might end up at a position different from POS. This
1917 is only a problem when POS is a row start after a newline and an
1918 overlay starts there with an after-string, and the overlay has an
1919 invisible property. Since we don't skip invisible text in
1920 display_line and elsewhere immediately after consuming the
1921 newline before the row start, such a POS will not be in a string,
1922 but the call to init_iterator below will move us to the
1923 after-string. */
ac90c44f 1924 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 1925
47d57b22 1926 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 1927 {
50f80c2f
KR
1928 const char *s = SDATA (it->overlay_strings[i]);
1929 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 1930
1e1e5daf
GM
1931 while (s < e && *s != '\n')
1932 ++s;
1933
1934 if (s < e)
1935 {
1936 overlay_strings_with_newlines = 1;
1937 break;
1938 }
1939 }
47d57b22 1940
75c5350a
GM
1941 /* If position is within an overlay string, set up IT to the right
1942 overlay string. */
5f5c8ee5
GM
1943 if (pos->overlay_string_index >= 0)
1944 {
1945 int relative_index;
75c5350a
GM
1946
1947 /* If the first overlay string happens to have a `display'
1948 property for an image, the iterator will be set up for that
1949 image, and we have to undo that setup first before we can
1950 correct the overlay string index. */
1951 if (it->method == next_element_from_image)
1952 pop_it (it);
2311178e 1953
5f5c8ee5
GM
1954 /* We already have the first chunk of overlay strings in
1955 IT->overlay_strings. Load more until the one for
1956 pos->overlay_string_index is in IT->overlay_strings. */
1957 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1958 {
1959 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1960 it->current.overlay_string_index = 0;
1961 while (n--)
1962 {
5a08cbaf 1963 load_overlay_strings (it, 0);
5f5c8ee5
GM
1964 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1965 }
1966 }
2311178e 1967
5f5c8ee5
GM
1968 it->current.overlay_string_index = pos->overlay_string_index;
1969 relative_index = (it->current.overlay_string_index
1970 % OVERLAY_STRING_CHUNK_SIZE);
1971 it->string = it->overlay_strings[relative_index];
cafafe0b 1972 xassert (STRINGP (it->string));
5f5c8ee5
GM
1973 it->current.string_pos = pos->string_pos;
1974 it->method = next_element_from_string;
1975 }
2311178e 1976
1e1e5daf
GM
1977#if 0 /* This is bogus because POS not having an overlay string
1978 position does not mean it's after the string. Example: A
1979 line starting with a before-string and initialization of IT
1980 to the previous row's end position. */
2be8f184
GM
1981 else if (it->current.overlay_string_index >= 0)
1982 {
1983 /* If POS says we're already after an overlay string ending at
1984 POS, make sure to pop the iterator because it will be in
1985 front of that overlay string. When POS is ZV, we've thereby
1986 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1987 while (it->sp)
1988 pop_it (it);
2be8f184
GM
1989 it->current.overlay_string_index = -1;
1990 it->method = next_element_from_buffer;
1991 if (CHARPOS (pos->pos) == ZV)
1992 it->overlay_strings_at_end_processed_p = 1;
1993 }
1e1e5daf 1994#endif /* 0 */
2311178e 1995
2be8f184 1996 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1997 {
1998 /* Recorded position is not in an overlay string, but in another
1999 string. This can only be a string from a `display' property.
2000 IT should already be filled with that string. */
2001 it->current.string_pos = pos->string_pos;
2002 xassert (STRINGP (it->string));
2003 }
2004
ac90c44f
GM
2005 /* Restore position in display vector translations, control
2006 character translations or ellipses. */
5f5c8ee5
GM
2007 if (pos->dpvec_index >= 0)
2008 {
ac90c44f
GM
2009 if (it->dpvec == NULL)
2010 get_next_display_element (it);
5f5c8ee5
GM
2011 xassert (it->dpvec && it->current.dpvec_index == 0);
2012 it->current.dpvec_index = pos->dpvec_index;
2013 }
2311178e 2014
5f5c8ee5 2015 CHECK_IT (it);
47d57b22 2016 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2017}
2018
2019
2020/* Initialize IT for stepping through current_buffer in window W
2021 starting at ROW->start. */
2022
2023static void
2024init_to_row_start (it, w, row)
2025 struct it *it;
2026 struct window *w;
2027 struct glyph_row *row;
2028{
2029 init_from_display_pos (it, w, &row->start);
2030 it->continuation_lines_width = row->continuation_lines_width;
2031 CHECK_IT (it);
2032}
2033
2311178e 2034
5f5c8ee5 2035/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2036 starting in the line following ROW, i.e. starting at ROW->end.
2037 Value is zero if there are overlay strings with newlines at ROW's
2038 end position. */
5f5c8ee5 2039
47d57b22 2040static int
5f5c8ee5
GM
2041init_to_row_end (it, w, row)
2042 struct it *it;
2043 struct window *w;
2044 struct glyph_row *row;
2045{
47d57b22 2046 int success = 0;
2311178e 2047
47d57b22
GM
2048 if (init_from_display_pos (it, w, &row->end))
2049 {
2050 if (row->continued_p)
2051 it->continuation_lines_width
2052 = row->continuation_lines_width + row->pixel_width;
2053 CHECK_IT (it);
2054 success = 1;
2055 }
2311178e 2056
47d57b22 2057 return success;
5f5c8ee5
GM
2058}
2059
2060
2061
2062\f
2063/***********************************************************************
2064 Text properties
2065 ***********************************************************************/
2066
2067/* Called when IT reaches IT->stop_charpos. Handle text property and
2068 overlay changes. Set IT->stop_charpos to the next position where
2069 to stop. */
2070
2071static void
2072handle_stop (it)
2073 struct it *it;
2074{
2075 enum prop_handled handled;
2076 int handle_overlay_change_p = 1;
2077 struct props *p;
2078
2079 it->dpvec = NULL;
2080 it->current.dpvec_index = -1;
2081
2082 do
2083 {
2084 handled = HANDLED_NORMALLY;
2311178e 2085
5f5c8ee5
GM
2086 /* Call text property handlers. */
2087 for (p = it_props; p->handler; ++p)
2088 {
2089 handled = p->handler (it);
2970b9be 2090
5f5c8ee5
GM
2091 if (handled == HANDLED_RECOMPUTE_PROPS)
2092 break;
2093 else if (handled == HANDLED_RETURN)
2094 return;
2095 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2096 handle_overlay_change_p = 0;
2097 }
2098
2099 if (handled != HANDLED_RECOMPUTE_PROPS)
2100 {
2101 /* Don't check for overlay strings below when set to deliver
2102 characters from a display vector. */
2103 if (it->method == next_element_from_display_vector)
2104 handle_overlay_change_p = 0;
2105
2106 /* Handle overlay changes. */
2107 if (handle_overlay_change_p)
2108 handled = handle_overlay_change (it);
2311178e 2109
5f5c8ee5
GM
2110 /* Determine where to stop next. */
2111 if (handled == HANDLED_NORMALLY)
2112 compute_stop_pos (it);
2113 }
2114 }
2115 while (handled == HANDLED_RECOMPUTE_PROPS);
2116}
2117
2118
2119/* Compute IT->stop_charpos from text property and overlay change
2120 information for IT's current position. */
2121
2122static void
2123compute_stop_pos (it)
2124 struct it *it;
2125{
2126 register INTERVAL iv, next_iv;
2127 Lisp_Object object, limit, position;
2128
2129 /* If nowhere else, stop at the end. */
2130 it->stop_charpos = it->end_charpos;
2311178e 2131
5f5c8ee5
GM
2132 if (STRINGP (it->string))
2133 {
2134 /* Strings are usually short, so don't limit the search for
2135 properties. */
2136 object = it->string;
2137 limit = Qnil;
ac90c44f 2138 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2139 }
2140 else
2141 {
2142 int charpos;
2143
2144 /* If next overlay change is in front of the current stop pos
2145 (which is IT->end_charpos), stop there. Note: value of
2146 next_overlay_change is point-max if no overlay change
2147 follows. */
2148 charpos = next_overlay_change (IT_CHARPOS (*it));
2149 if (charpos < it->stop_charpos)
2150 it->stop_charpos = charpos;
2151
2152 /* If showing the region, we have to stop at the region
2153 start or end because the face might change there. */
2154 if (it->region_beg_charpos > 0)
2155 {
2156 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2157 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2158 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2159 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2160 }
2311178e 2161
5f5c8ee5
GM
2162 /* Set up variables for computing the stop position from text
2163 property changes. */
2164 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2165 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2166 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2167
2168 }
2169
2170 /* Get the interval containing IT's position. Value is a null
2171 interval if there isn't such an interval. */
2172 iv = validate_interval_range (object, &position, &position, 0);
2173 if (!NULL_INTERVAL_P (iv))
2174 {
2175 Lisp_Object values_here[LAST_PROP_IDX];
2176 struct props *p;
2177
2178 /* Get properties here. */
2179 for (p = it_props; p->handler; ++p)
2180 values_here[p->idx] = textget (iv->plist, *p->name);
2181
2182 /* Look for an interval following iv that has different
2183 properties. */
2184 for (next_iv = next_interval (iv);
2185 (!NULL_INTERVAL_P (next_iv)
2186 && (NILP (limit)
2187 || XFASTINT (limit) > next_iv->position));
2188 next_iv = next_interval (next_iv))
2189 {
2190 for (p = it_props; p->handler; ++p)
2191 {
2192 Lisp_Object new_value;
2193
2194 new_value = textget (next_iv->plist, *p->name);
2195 if (!EQ (values_here[p->idx], new_value))
2196 break;
2197 }
2311178e 2198
5f5c8ee5
GM
2199 if (p->handler)
2200 break;
2201 }
2202
2203 if (!NULL_INTERVAL_P (next_iv))
2204 {
2205 if (INTEGERP (limit)
2206 && next_iv->position >= XFASTINT (limit))
2207 /* No text property change up to limit. */
2208 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2209 else
2210 /* Text properties change in next_iv. */
2211 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2212 }
2213 }
2214
2215 xassert (STRINGP (it->string)
2216 || (it->stop_charpos >= BEGV
2217 && it->stop_charpos >= IT_CHARPOS (*it)));
2218}
2219
2220
2221/* Return the position of the next overlay change after POS in
2222 current_buffer. Value is point-max if no overlay change
2223 follows. This is like `next-overlay-change' but doesn't use
2224 xmalloc. */
2225
2226static int
2227next_overlay_change (pos)
2228 int pos;
2229{
2230 int noverlays;
2231 int endpos;
2232 Lisp_Object *overlays;
2233 int len;
2234 int i;
2235
2236 /* Get all overlays at the given position. */
2237 len = 10;
2238 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2239 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2240 if (noverlays > len)
2241 {
2242 len = noverlays;
2243 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2244 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2245 }
2246
2247 /* If any of these overlays ends before endpos,
2248 use its ending point instead. */
2249 for (i = 0; i < noverlays; ++i)
2250 {
2251 Lisp_Object oend;
2252 int oendpos;
2253
2254 oend = OVERLAY_END (overlays[i]);
2255 oendpos = OVERLAY_POSITION (oend);
2256 endpos = min (endpos, oendpos);
2257 }
2258
2259 return endpos;
2260}
2261
2262
2263\f
2264/***********************************************************************
2265 Fontification
2266 ***********************************************************************/
2267
2268/* Handle changes in the `fontified' property of the current buffer by
2269 calling hook functions from Qfontification_functions to fontify
2270 regions of text. */
2271
2272static enum prop_handled
2273handle_fontified_prop (it)
2274 struct it *it;
2275{
2276 Lisp_Object prop, pos;
2277 enum prop_handled handled = HANDLED_NORMALLY;
2278
2279 /* Get the value of the `fontified' property at IT's current buffer
2280 position. (The `fontified' property doesn't have a special
2281 meaning in strings.) If the value is nil, call functions from
2282 Qfontification_functions. */
2283 if (!STRINGP (it->string)
2284 && it->s == NULL
2285 && !NILP (Vfontification_functions)
085536c2 2286 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2287 && (pos = make_number (IT_CHARPOS (*it)),
2288 prop = Fget_char_property (pos, Qfontified, Qnil),
2289 NILP (prop)))
2290 {
331379bf 2291 int count = SPECPDL_INDEX ();
085536c2
GM
2292 Lisp_Object val;
2293
2294 val = Vfontification_functions;
2295 specbind (Qfontification_functions, Qnil);
2311178e 2296
085536c2 2297 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2298 safe_call1 (val, pos);
085536c2
GM
2299 else
2300 {
2301 Lisp_Object globals, fn;
2302 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2303
085536c2
GM
2304 globals = Qnil;
2305 GCPRO2 (val, globals);
2311178e 2306
085536c2
GM
2307 for (; CONSP (val); val = XCDR (val))
2308 {
2309 fn = XCAR (val);
2311178e 2310
085536c2
GM
2311 if (EQ (fn, Qt))
2312 {
2313 /* A value of t indicates this hook has a local
2314 binding; it means to run the global binding too.
2315 In a global value, t should not occur. If it
2316 does, we must ignore it to avoid an endless
2317 loop. */
2318 for (globals = Fdefault_value (Qfontification_functions);
2319 CONSP (globals);
2320 globals = XCDR (globals))
2321 {
2322 fn = XCAR (globals);
2323 if (!EQ (fn, Qt))
116d6f5c 2324 safe_call1 (fn, pos);
085536c2
GM
2325 }
2326 }
2327 else
116d6f5c 2328 safe_call1 (fn, pos);
085536c2
GM
2329 }
2330
2331 UNGCPRO;
2332 }
2333
2334 unbind_to (count, Qnil);
5f5c8ee5
GM
2335
2336 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2337 something. This avoids an endless loop if they failed to
2338 fontify the text for which reason ever. */
2339 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2340 handled = HANDLED_RECOMPUTE_PROPS;
2341 }
2342
2343 return handled;
2344}
2345
2346
2347\f
2348/***********************************************************************
2349 Faces
2350 ***********************************************************************/
2351
2352/* Set up iterator IT from face properties at its current position.
2353 Called from handle_stop. */
2354
2355static enum prop_handled
2356handle_face_prop (it)
2357 struct it *it;
2358{
2359 int new_face_id, next_stop;
2311178e 2360
5f5c8ee5
GM
2361 if (!STRINGP (it->string))
2362 {
2363 new_face_id
2364 = face_at_buffer_position (it->w,
2365 IT_CHARPOS (*it),
2366 it->region_beg_charpos,
2367 it->region_end_charpos,
2368 &next_stop,
2369 (IT_CHARPOS (*it)
2370 + TEXT_PROP_DISTANCE_LIMIT),
2371 0);
2311178e 2372
5f5c8ee5
GM
2373 /* Is this a start of a run of characters with box face?
2374 Caveat: this can be called for a freshly initialized
4b41cebb 2375 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2376 face will not change until limit, i.e. if the new face has a
2377 box, all characters up to limit will have one. But, as
2378 usual, we don't know whether limit is really the end. */
2379 if (new_face_id != it->face_id)
2380 {
2381 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2311178e 2382
5f5c8ee5
GM
2383 /* If new face has a box but old face has not, this is
2384 the start of a run of characters with box, i.e. it has
2385 a shadow on the left side. The value of face_id of the
2386 iterator will be -1 if this is the initial call that gets
2387 the face. In this case, we have to look in front of IT's
2388 position and see whether there is a face != new_face_id. */
2389 it->start_of_box_run_p
2390 = (new_face->box != FACE_NO_BOX
2391 && (it->face_id >= 0
2392 || IT_CHARPOS (*it) == BEG
2393 || new_face_id != face_before_it_pos (it)));
2394 it->face_box_p = new_face->box != FACE_NO_BOX;
2395 }
2396 }
2397 else
2398 {
06a12811
GM
2399 int base_face_id, bufpos;
2400
2401 if (it->current.overlay_string_index >= 0)
2402 bufpos = IT_CHARPOS (*it);
2403 else
2404 bufpos = 0;
2311178e 2405
06a12811
GM
2406 /* For strings from a buffer, i.e. overlay strings or strings
2407 from a `display' property, use the face at IT's current
2408 buffer position as the base face to merge with, so that
2409 overlay strings appear in the same face as surrounding
2410 text, unless they specify their own faces. */
2411 base_face_id = underlying_face_id (it);
2311178e 2412
06a12811
GM
2413 new_face_id = face_at_string_position (it->w,
2414 it->string,
2415 IT_STRING_CHARPOS (*it),
2416 bufpos,
2417 it->region_beg_charpos,
2418 it->region_end_charpos,
2419 &next_stop,
5de7c6f2 2420 base_face_id, 0);
2311178e 2421
5f5c8ee5
GM
2422#if 0 /* This shouldn't be neccessary. Let's check it. */
2423 /* If IT is used to display a mode line we would really like to
2424 use the mode line face instead of the frame's default face. */
2425 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2426 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2427 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 2428#endif
2311178e 2429
5f5c8ee5
GM
2430 /* Is this a start of a run of characters with box? Caveat:
2431 this can be called for a freshly allocated iterator; face_id
2432 is -1 is this case. We know that the new face will not
2433 change until the next check pos, i.e. if the new face has a
2434 box, all characters up to that position will have a
2435 box. But, as usual, we don't know whether that position
2436 is really the end. */
2437 if (new_face_id != it->face_id)
2438 {
2439 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2440 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 2441
5f5c8ee5
GM
2442 /* If new face has a box but old face hasn't, this is the
2443 start of a run of characters with box, i.e. it has a
2444 shadow on the left side. */
2445 it->start_of_box_run_p
2446 = new_face->box && (old_face == NULL || !old_face->box);
2447 it->face_box_p = new_face->box != FACE_NO_BOX;
2448 }
2449 }
2311178e 2450
5f5c8ee5 2451 it->face_id = new_face_id;
5f5c8ee5
GM
2452 return HANDLED_NORMALLY;
2453}
2454
2455
06a12811
GM
2456/* Return the ID of the face ``underlying'' IT's current position,
2457 which is in a string. If the iterator is associated with a
2458 buffer, return the face at IT's current buffer position.
2459 Otherwise, use the iterator's base_face_id. */
2460
2461static int
2462underlying_face_id (it)
2463 struct it *it;
2464{
2465 int face_id = it->base_face_id, i;
2466
2467 xassert (STRINGP (it->string));
2468
2469 for (i = it->sp - 1; i >= 0; --i)
2470 if (NILP (it->stack[i].string))
2471 face_id = it->stack[i].face_id;
2472
2473 return face_id;
2474}
2475
2476
5f5c8ee5
GM
2477/* Compute the face one character before or after the current position
2478 of IT. BEFORE_P non-zero means get the face in front of IT's
2479 position. Value is the id of the face. */
2480
2481static int
2482face_before_or_after_it_pos (it, before_p)
2483 struct it *it;
2484 int before_p;
2485{
2486 int face_id, limit;
2487 int next_check_charpos;
2488 struct text_pos pos;
2489
2490 xassert (it->s == NULL);
2311178e 2491
5f5c8ee5
GM
2492 if (STRINGP (it->string))
2493 {
06a12811 2494 int bufpos, base_face_id;
2311178e 2495
5f5c8ee5
GM
2496 /* No face change past the end of the string (for the case
2497 we are padding with spaces). No face change before the
2498 string start. */
2051c264 2499 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2500 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2501 return it->face_id;
2502
2503 /* Set pos to the position before or after IT's current position. */
2504 if (before_p)
2505 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2506 else
260a86a0
KH
2507 /* For composition, we must check the character after the
2508 composition. */
2509 pos = (it->what == IT_COMPOSITION
2510 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2511 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2512
06a12811
GM
2513 if (it->current.overlay_string_index >= 0)
2514 bufpos = IT_CHARPOS (*it);
2515 else
2516 bufpos = 0;
2517
2518 base_face_id = underlying_face_id (it);
2519
5f5c8ee5 2520 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2521 face_id = face_at_string_position (it->w,
2522 it->string,
2523 CHARPOS (pos),
2524 bufpos,
2525 it->region_beg_charpos,
2526 it->region_end_charpos,
2527 &next_check_charpos,
5de7c6f2 2528 base_face_id, 0);
5f5c8ee5
GM
2529
2530 /* Correct the face for charsets different from ASCII. Do it
2531 for the multibyte case only. The face returned above is
2532 suitable for unibyte text if IT->string is unibyte. */
2533 if (STRING_MULTIBYTE (it->string))
2534 {
50f80c2f 2535 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 2536 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
2537 int c, len;
2538 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 2539
4fdb80f2 2540 c = string_char_and_length (p, rest, &len);
980806b6 2541 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2542 }
2543 }
2544 else
2545 {
70851746
GM
2546 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2547 || (IT_CHARPOS (*it) <= BEGV && before_p))
2548 return it->face_id;
2311178e 2549
5f5c8ee5
GM
2550 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2551 pos = it->current.pos;
2311178e 2552
5f5c8ee5 2553 if (before_p)
c1005d06 2554 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2555 else
260a86a0
KH
2556 {
2557 if (it->what == IT_COMPOSITION)
2558 /* For composition, we must check the position after the
2559 composition. */
2560 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2561 else
c1005d06 2562 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2563 }
2311178e 2564
5f5c8ee5
GM
2565 /* Determine face for CHARSET_ASCII, or unibyte. */
2566 face_id = face_at_buffer_position (it->w,
2567 CHARPOS (pos),
2568 it->region_beg_charpos,
2569 it->region_end_charpos,
2570 &next_check_charpos,
2571 limit, 0);
2572
2573 /* Correct the face for charsets different from ASCII. Do it
2574 for the multibyte case only. The face returned above is
2575 suitable for unibyte text if current_buffer is unibyte. */
2576 if (it->multibyte_p)
2577 {
1d1b6e6a 2578 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
2579 struct face *face = FACE_FROM_ID (it->f, face_id);
2580 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2581 }
2582 }
2311178e 2583
5f5c8ee5
GM
2584 return face_id;
2585}
2586
2587
2588\f
2589/***********************************************************************
2590 Invisible text
2591 ***********************************************************************/
2592
2593/* Set up iterator IT from invisible properties at its current
2594 position. Called from handle_stop. */
2595
2596static enum prop_handled
2597handle_invisible_prop (it)
2598 struct it *it;
2599{
2600 enum prop_handled handled = HANDLED_NORMALLY;
2601
2602 if (STRINGP (it->string))
2603 {
2604 extern Lisp_Object Qinvisible;
2605 Lisp_Object prop, end_charpos, limit, charpos;
2606
2607 /* Get the value of the invisible text property at the
2608 current position. Value will be nil if there is no such
2609 property. */
ac90c44f 2610 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2611 prop = Fget_text_property (charpos, Qinvisible, it->string);
2612
eadc0bf8
GM
2613 if (!NILP (prop)
2614 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2615 {
2616 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 2617
5f5c8ee5
GM
2618 /* Get the position at which the next change of the
2619 invisible text property can be found in IT->string.
2620 Value will be nil if the property value is the same for
2621 all the rest of IT->string. */
2051c264 2622 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 2623 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 2624 it->string, limit);
2311178e 2625
5f5c8ee5
GM
2626 /* Text at current position is invisible. The next
2627 change in the property is at position end_charpos.
2628 Move IT's current position to that position. */
2629 if (INTEGERP (end_charpos)
2630 && XFASTINT (end_charpos) < XFASTINT (limit))
2631 {
2632 struct text_pos old;
2633 old = it->current.string_pos;
2634 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2635 compute_string_pos (&it->current.string_pos, old, it->string);
2636 }
2637 else
2638 {
2639 /* The rest of the string is invisible. If this is an
2640 overlay string, proceed with the next overlay string
2641 or whatever comes and return a character from there. */
2642 if (it->current.overlay_string_index >= 0)
2643 {
2644 next_overlay_string (it);
2645 /* Don't check for overlay strings when we just
2646 finished processing them. */
2647 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2648 }
2649 else
2650 {
2051c264
GM
2651 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2652 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
2653 }
2654 }
2655 }
2656 }
2657 else
2658 {
8580a4e3 2659 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 2660 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
2661
2662 /* First of all, is there invisible text at this position? */
5a08cbaf 2663 start_charpos = IT_CHARPOS (*it);
ac90c44f 2664 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
2665 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2666 &overlay);
8580a4e3
SM
2667 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2668
5f5c8ee5 2669 /* If we are on invisible text, skip over it. */
8580a4e3 2670 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2671 {
2672 /* Record whether we have to display an ellipsis for the
2673 invisible text. */
8580a4e3 2674 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
2675
2676 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 2677
5f5c8ee5
GM
2678 /* Loop skipping over invisible text. The loop is left at
2679 ZV or with IT on the first char being visible again. */
2680 do
2681 {
2682 /* Try to skip some invisible text. Return value is the
2683 position reached which can be equal to IT's position
2684 if there is nothing invisible here. This skips both
2685 over invisible text properties and overlays with
2686 invisible property. */
2687 newpos = skip_invisible (IT_CHARPOS (*it),
2688 &next_stop, ZV, it->window);
2689
2690 /* If we skipped nothing at all we weren't at invisible
2691 text in the first place. If everything to the end of
2692 the buffer was skipped, end the loop. */
2693 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 2694 invis_p = 0;
5f5c8ee5
GM
2695 else
2696 {
2697 /* We skipped some characters but not necessarily
2698 all there are. Check if we ended up on visible
2699 text. Fget_char_property returns the property of
2700 the char before the given position, i.e. if we
8580a4e3 2701 get invis_p = 0, this means that the char at
5f5c8ee5 2702 newpos is visible. */
ac90c44f 2703 pos = make_number (newpos);
5f5c8ee5 2704 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 2705 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 2706 }
2311178e 2707
5f5c8ee5
GM
2708 /* If we ended up on invisible text, proceed to
2709 skip starting with next_stop. */
8580a4e3 2710 if (invis_p)
5f5c8ee5
GM
2711 IT_CHARPOS (*it) = next_stop;
2712 }
8580a4e3 2713 while (invis_p);
5a08cbaf 2714
5f5c8ee5
GM
2715 /* The position newpos is now either ZV or on visible text. */
2716 IT_CHARPOS (*it) = newpos;
2717 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 2718
5a08cbaf
GM
2719 /* If there are before-strings at the start of invisible
2720 text, and the text is invisible because of a text
2721 property, arrange to show before-strings because 20.x did
2722 it that way. (If the text is invisible because of an
2723 overlay property instead of a text property, this is
2724 already handled in the overlay code.) */
2725 if (NILP (overlay)
2726 && get_overlay_strings (it, start_charpos))
5f5c8ee5 2727 {
5a08cbaf
GM
2728 handled = HANDLED_RECOMPUTE_PROPS;
2729 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 2730 }
5a08cbaf
GM
2731 else if (display_ellipsis_p)
2732 setup_for_ellipsis (it);
5f5c8ee5
GM
2733 }
2734 }
2735
2736 return handled;
2737}
2738
2739
5a08cbaf
GM
2740/* Make iterator IT return `...' next. */
2741
2742static void
2743setup_for_ellipsis (it)
2744 struct it *it;
2745{
2311178e 2746 if (it->dp
5a08cbaf
GM
2747 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2748 {
2749 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2750 it->dpvec = v->contents;
2751 it->dpend = v->contents + v->size;
2752 }
2311178e 2753 else
5a08cbaf
GM
2754 {
2755 /* Default `...'. */
2756 it->dpvec = default_invis_vector;
2757 it->dpend = default_invis_vector + 3;
2758 }
2311178e 2759
5a08cbaf
GM
2760 /* The ellipsis display does not replace the display of the
2761 character at the new position. Indicate this by setting
2762 IT->dpvec_char_len to zero. */
2763 it->dpvec_char_len = 0;
2311178e 2764
5a08cbaf
GM
2765 it->current.dpvec_index = 0;
2766 it->method = next_element_from_display_vector;
2767}
2768
2769
5f5c8ee5
GM
2770\f
2771/***********************************************************************
2772 'display' property
2773 ***********************************************************************/
2774
2775/* Set up iterator IT from `display' property at its current position.
2776 Called from handle_stop. */
2777
2778static enum prop_handled
2779handle_display_prop (it)
2780 struct it *it;
2781{
2782 Lisp_Object prop, object;
2783 struct text_pos *position;
a61b7058 2784 int display_replaced_p = 0;
5f5c8ee5
GM
2785
2786 if (STRINGP (it->string))
2787 {
2788 object = it->string;
2789 position = &it->current.string_pos;
2790 }
2791 else
2792 {
221dd3e7 2793 object = it->w->buffer;
5f5c8ee5
GM
2794 position = &it->current.pos;
2795 }
2796
2797 /* Reset those iterator values set from display property values. */
2798 it->font_height = Qnil;
2799 it->space_width = Qnil;
2800 it->voffset = 0;
2801
2802 /* We don't support recursive `display' properties, i.e. string
2803 values that have a string `display' property, that have a string
2804 `display' property etc. */
2805 if (!it->string_from_display_prop_p)
2806 it->area = TEXT_AREA;
2807
2808 prop = Fget_char_property (make_number (position->charpos),
2809 Qdisplay, object);
2810 if (NILP (prop))
2811 return HANDLED_NORMALLY;
2812
f3751a65 2813 if (CONSP (prop)
12700f40
GM
2814 /* Simple properties. */
2815 && !EQ (XCAR (prop), Qimage)
2816 && !EQ (XCAR (prop), Qspace)
2817 && !EQ (XCAR (prop), Qwhen)
2818 && !EQ (XCAR (prop), Qspace_width)
2819 && !EQ (XCAR (prop), Qheight)
2820 && !EQ (XCAR (prop), Qraise)
2821 /* Marginal area specifications. */
2822 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2823 && !NILP (XCAR (prop)))
5f5c8ee5 2824 {
a61b7058 2825 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 2826 {
a61b7058
GM
2827 if (handle_single_display_prop (it, XCAR (prop), object,
2828 position, display_replaced_p))
2829 display_replaced_p = 1;
5f5c8ee5
GM
2830 }
2831 }
2832 else if (VECTORP (prop))
2833 {
2834 int i;
a61b7058
GM
2835 for (i = 0; i < ASIZE (prop); ++i)
2836 if (handle_single_display_prop (it, AREF (prop, i), object,
2837 position, display_replaced_p))
2838 display_replaced_p = 1;
5f5c8ee5
GM
2839 }
2840 else
2841 {
a61b7058
GM
2842 if (handle_single_display_prop (it, prop, object, position, 0))
2843 display_replaced_p = 1;
5f5c8ee5
GM
2844 }
2845
a61b7058 2846 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
2847}
2848
2849
6c577098 2850/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2851 at START_POS in OBJECT. */
2852
2853static struct text_pos
2854display_prop_end (it, object, start_pos)
2855 struct it *it;
2856 Lisp_Object object;
2857 struct text_pos start_pos;
2858{
2859 Lisp_Object end;
2860 struct text_pos end_pos;
5f5c8ee5 2861
016b5642
MB
2862 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2863 Qdisplay, object, Qnil);
6c577098
GM
2864 CHARPOS (end_pos) = XFASTINT (end);
2865 if (STRINGP (object))
5f5c8ee5
GM
2866 compute_string_pos (&end_pos, start_pos, it->string);
2867 else
6c577098 2868 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2869
2870 return end_pos;
2871}
2872
2873
2874/* Set up IT from a single `display' sub-property value PROP. OBJECT
2875 is the object in which the `display' property was found. *POSITION
a61b7058
GM
2876 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2877 means that we previously saw a display sub-property which already
2878 replaced text display with something else, for example an image;
2879 ignore such properties after the first one has been processed.
5f5c8ee5
GM
2880
2881 If PROP is a `space' or `image' sub-property, set *POSITION to the
2882 end position of the `display' property.
2883
4b41cebb 2884 Value is non-zero if something was found which replaces the display
a61b7058 2885 of buffer or string text. */
5f5c8ee5
GM
2886
2887static int
a61b7058
GM
2888handle_single_display_prop (it, prop, object, position,
2889 display_replaced_before_p)
5f5c8ee5
GM
2890 struct it *it;
2891 Lisp_Object prop;
2892 Lisp_Object object;
2893 struct text_pos *position;
a61b7058 2894 int display_replaced_before_p;
5f5c8ee5
GM
2895{
2896 Lisp_Object value;
a61b7058 2897 int replaces_text_display_p = 0;
5f5c8ee5
GM
2898 Lisp_Object form;
2899
d3acf96b 2900 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 2901 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2902 form = Qt;
d3acf96b 2903 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2904 {
2905 prop = XCDR (prop);
2906 if (!CONSP (prop))
2907 return 0;
2908 form = XCAR (prop);
2909 prop = XCDR (prop);
5f5c8ee5
GM
2910 }
2911
2912 if (!NILP (form) && !EQ (form, Qt))
2913 {
331379bf 2914 int count = SPECPDL_INDEX ();
5f5c8ee5 2915 struct gcpro gcpro1;
5f5c8ee5 2916
b384d6f8
GM
2917 /* Bind `object' to the object having the `display' property, a
2918 buffer or string. Bind `position' to the position in the
2919 object where the property was found, and `buffer-position'
2920 to the current position in the buffer. */
2921 specbind (Qobject, object);
31ac723b
SM
2922 specbind (Qposition, make_number (CHARPOS (*position)));
2923 specbind (Qbuffer_position,
2924 make_number (STRINGP (object)
2925 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 2926 GCPRO1 (form);
116d6f5c 2927 form = safe_eval (form);
b384d6f8
GM
2928 UNGCPRO;
2929 unbind_to (count, Qnil);
5f5c8ee5 2930 }
2311178e 2931
5f5c8ee5
GM
2932 if (NILP (form))
2933 return 0;
2934
2935 if (CONSP (prop)
2936 && EQ (XCAR (prop), Qheight)
2937 && CONSP (XCDR (prop)))
2938 {
e4093f38 2939 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 2940 return 0;
2311178e 2941
5f5c8ee5
GM
2942 /* `(height HEIGHT)'. */
2943 it->font_height = XCAR (XCDR (prop));
2944 if (!NILP (it->font_height))
2945 {
2946 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2947 int new_height = -1;
2948
2949 if (CONSP (it->font_height)
2950 && (EQ (XCAR (it->font_height), Qplus)
2951 || EQ (XCAR (it->font_height), Qminus))
2952 && CONSP (XCDR (it->font_height))
2953 && INTEGERP (XCAR (XCDR (it->font_height))))
2954 {
2955 /* `(+ N)' or `(- N)' where N is an integer. */
2956 int steps = XINT (XCAR (XCDR (it->font_height)));
2957 if (EQ (XCAR (it->font_height), Qplus))
2958 steps = - steps;
2959 it->face_id = smaller_face (it->f, it->face_id, steps);
2960 }
0d8b31c0 2961 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2962 {
2963 /* Call function with current height as argument.
2964 Value is the new height. */
116d6f5c
GM
2965 Lisp_Object height;
2966 height = safe_call1 (it->font_height,
2967 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2968 if (NUMBERP (height))
2969 new_height = XFLOATINT (height);
5f5c8ee5
GM
2970 }
2971 else if (NUMBERP (it->font_height))
2972 {
2973 /* Value is a multiple of the canonical char height. */
2974 struct face *face;
2311178e 2975
5f5c8ee5
GM
2976 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2977 new_height = (XFLOATINT (it->font_height)
2978 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2979 }
2980 else
2981 {
2982 /* Evaluate IT->font_height with `height' bound to the
2983 current specified height to get the new height. */
2984 Lisp_Object value;
331379bf 2985 int count = SPECPDL_INDEX ();
2311178e 2986
5f5c8ee5 2987 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2988 value = safe_eval (it->font_height);
5f5c8ee5 2989 unbind_to (count, Qnil);
2311178e 2990
5f5c8ee5
GM
2991 if (NUMBERP (value))
2992 new_height = XFLOATINT (value);
2993 }
2311178e 2994
5f5c8ee5
GM
2995 if (new_height > 0)
2996 it->face_id = face_with_height (it->f, it->face_id, new_height);
2997 }
2998 }
2999 else if (CONSP (prop)
3000 && EQ (XCAR (prop), Qspace_width)
3001 && CONSP (XCDR (prop)))
3002 {
3003 /* `(space_width WIDTH)'. */
e4093f38 3004 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3005 return 0;
2311178e 3006
5f5c8ee5
GM
3007 value = XCAR (XCDR (prop));
3008 if (NUMBERP (value) && XFLOATINT (value) > 0)
3009 it->space_width = value;
3010 }
3011 else if (CONSP (prop)
3012 && EQ (XCAR (prop), Qraise)
3013 && CONSP (XCDR (prop)))
3014 {
5f5c8ee5 3015 /* `(raise FACTOR)'. */
e4093f38 3016 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3017 return 0;
2311178e 3018
fb3842a8 3019#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
3020 value = XCAR (XCDR (prop));
3021 if (NUMBERP (value))
3022 {
3023 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3024 it->voffset = - (XFLOATINT (value)
1ab3e082 3025 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3026 }
3027#endif /* HAVE_WINDOW_SYSTEM */
3028 }
3029 else if (!it->string_from_display_prop_p)
3030 {
f3751a65 3031 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 3032 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
3033 Lisp_Object location, value;
3034 struct text_pos start_pos;
3035 int valid_p;
3036
3037 /* Characters having this form of property are not displayed, so
3038 we have to find the end of the property. */
5f5c8ee5
GM
3039 start_pos = *position;
3040 *position = display_prop_end (it, object, start_pos);
15e26c76 3041 value = Qnil;
5f5c8ee5
GM
3042
3043 /* Let's stop at the new position and assume that all
3044 text properties change there. */
3045 it->stop_charpos = position->charpos;
3046
f3751a65
GM
3047 location = Qunbound;
3048 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3049 {
f3751a65 3050 Lisp_Object tem;
2311178e 3051
5f5c8ee5 3052 value = XCDR (prop);
f3751a65
GM
3053 if (CONSP (value))
3054 value = XCAR (value);
3055
3056 tem = XCAR (prop);
3057 if (EQ (XCAR (tem), Qmargin)
3058 && (tem = XCDR (tem),
3059 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3060 (NILP (tem)
3061 || EQ (tem, Qleft_margin)
3062 || EQ (tem, Qright_margin))))
3063 location = tem;
5f5c8ee5 3064 }
f3751a65
GM
3065
3066 if (EQ (location, Qunbound))
5f5c8ee5
GM
3067 {
3068 location = Qnil;
3069 value = prop;
3070 }
3071
3072#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 3073 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
3074 valid_p = STRINGP (value);
3075 else
3076 valid_p = (STRINGP (value)
3077 || (CONSP (value) && EQ (XCAR (value), Qspace))
3078 || valid_image_p (value));
3079#else /* not HAVE_WINDOW_SYSTEM */
3080 valid_p = STRINGP (value);
3081#endif /* not HAVE_WINDOW_SYSTEM */
2311178e 3082
5f5c8ee5
GM
3083 if ((EQ (location, Qleft_margin)
3084 || EQ (location, Qright_margin)
3085 || NILP (location))
a61b7058
GM
3086 && valid_p
3087 && !display_replaced_before_p)
5f5c8ee5 3088 {
a61b7058 3089 replaces_text_display_p = 1;
2311178e 3090
5f5c8ee5
GM
3091 /* Save current settings of IT so that we can restore them
3092 when we are finished with the glyph property value. */
3093 push_it (it);
2311178e 3094
5f5c8ee5
GM
3095 if (NILP (location))
3096 it->area = TEXT_AREA;
3097 else if (EQ (location, Qleft_margin))
3098 it->area = LEFT_MARGIN_AREA;
3099 else
3100 it->area = RIGHT_MARGIN_AREA;
2311178e 3101
5f5c8ee5
GM
3102 if (STRINGP (value))
3103 {
3104 it->string = value;
3105 it->multibyte_p = STRING_MULTIBYTE (it->string);
3106 it->current.overlay_string_index = -1;
3107 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3108 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3109 it->method = next_element_from_string;
3110 it->stop_charpos = 0;
3111 it->string_from_display_prop_p = 1;
e187cf71
GM
3112 /* Say that we haven't consumed the characters with
3113 `display' property yet. The call to pop_it in
3114 set_iterator_to_next will clean this up. */
3115 *position = start_pos;
5f5c8ee5
GM
3116 }
3117 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3118 {
3119 it->method = next_element_from_stretch;
3120 it->object = value;
3121 it->current.pos = it->position = start_pos;
3122 }
3123#ifdef HAVE_WINDOW_SYSTEM
3124 else
3125 {
3126 it->what = IT_IMAGE;
3127 it->image_id = lookup_image (it->f, value);
3128 it->position = start_pos;
3129 it->object = NILP (object) ? it->w->buffer : object;
3130 it->method = next_element_from_image;
2311178e 3131
02513cdd 3132 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3133 `display' property yet. The call to pop_it in
3134 set_iterator_to_next will clean this up. */
3135 *position = start_pos;
3136 }
3137#endif /* HAVE_WINDOW_SYSTEM */
3138 }
a21a3943
GM
3139 else
3140 /* Invalid property or property not supported. Restore
3141 the position to what it was before. */
3142 *position = start_pos;
5f5c8ee5
GM
3143 }
3144
a61b7058 3145 return replaces_text_display_p;
5f5c8ee5
GM
3146}
3147
3148
06568bbf
GM
3149/* Check if PROP is a display sub-property value whose text should be
3150 treated as intangible. */
3151
3152static int
3153single_display_prop_intangible_p (prop)
3154 Lisp_Object prop;
3155{
3156 /* Skip over `when FORM'. */
3157 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3158 {
3159 prop = XCDR (prop);
3160 if (!CONSP (prop))
3161 return 0;
3162 prop = XCDR (prop);
3163 }
3164
959bc044
SM
3165 if (STRINGP (prop))
3166 return 1;
3167
06568bbf
GM
3168 if (!CONSP (prop))
3169 return 0;
3170
3171 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3172 we don't need to treat text as intangible. */
3173 if (EQ (XCAR (prop), Qmargin))
3174 {
3175 prop = XCDR (prop);
3176 if (!CONSP (prop))
3177 return 0;
3178
3179 prop = XCDR (prop);
3180 if (!CONSP (prop)
3181 || EQ (XCAR (prop), Qleft_margin)
3182 || EQ (XCAR (prop), Qright_margin))
3183 return 0;
3184 }
2311178e 3185
06568bbf
GM
3186 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3187}
3188
3189
3190/* Check if PROP is a display property value whose text should be
3191 treated as intangible. */
3192
3193int
3194display_prop_intangible_p (prop)
3195 Lisp_Object prop;
3196{
3197 if (CONSP (prop)
3198 && CONSP (XCAR (prop))
3199 && !EQ (Qmargin, XCAR (XCAR (prop))))
3200 {
3201 /* A list of sub-properties. */
3202 while (CONSP (prop))
3203 {
3204 if (single_display_prop_intangible_p (XCAR (prop)))
3205 return 1;
3206 prop = XCDR (prop);
3207 }
3208 }
3209 else if (VECTORP (prop))
3210 {
3211 /* A vector of sub-properties. */
3212 int i;
a61b7058
GM
3213 for (i = 0; i < ASIZE (prop); ++i)
3214 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3215 return 1;
3216 }
3217 else
3218 return single_display_prop_intangible_p (prop);
3219
3220 return 0;
3221}
3222
74bd6d65
GM
3223
3224/* Return 1 if PROP is a display sub-property value containing STRING. */
3225
3226static int
3227single_display_prop_string_p (prop, string)
3228 Lisp_Object prop, string;
3229{
74bd6d65
GM
3230 if (EQ (string, prop))
3231 return 1;
2311178e 3232
74bd6d65
GM
3233 /* Skip over `when FORM'. */
3234 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3235 {
3236 prop = XCDR (prop);
3237 if (!CONSP (prop))
3238 return 0;
3239 prop = XCDR (prop);
3240 }
3241
3242 if (CONSP (prop))
3243 /* Skip over `margin LOCATION'. */
3244 if (EQ (XCAR (prop), Qmargin))
3245 {
3246 prop = XCDR (prop);
3247 if (!CONSP (prop))
3248 return 0;
3249
3250 prop = XCDR (prop);
3251 if (!CONSP (prop))
3252 return 0;
3253 }
2311178e 3254
74bd6d65
GM
3255 return CONSP (prop) && EQ (XCAR (prop), string);
3256}
3257
3258
3259/* Return 1 if STRING appears in the `display' property PROP. */
3260
3261static int
3262display_prop_string_p (prop, string)
3263 Lisp_Object prop, string;
3264{
74bd6d65
GM
3265 if (CONSP (prop)
3266 && CONSP (XCAR (prop))
3267 && !EQ (Qmargin, XCAR (XCAR (prop))))
3268 {
3269 /* A list of sub-properties. */
3270 while (CONSP (prop))
3271 {
3272 if (single_display_prop_string_p (XCAR (prop), string))
3273 return 1;
3274 prop = XCDR (prop);
3275 }
3276 }
3277 else if (VECTORP (prop))
3278 {
3279 /* A vector of sub-properties. */
3280 int i;
3281 for (i = 0; i < ASIZE (prop); ++i)
3282 if (single_display_prop_string_p (AREF (prop, i), string))
3283 return 1;
3284 }
3285 else
3286 return single_display_prop_string_p (prop, string);
3287
3288 return 0;
3289}
3290
3291
3292/* Determine from which buffer position in W's buffer STRING comes
3293 from. AROUND_CHARPOS is an approximate position where it could
3294 be from. Value is the buffer position or 0 if it couldn't be
3295 determined.
3296
3297 W's buffer must be current.
3298
3299 This function is necessary because we don't record buffer positions
3300 in glyphs generated from strings (to keep struct glyph small).
3301 This function may only use code that doesn't eval because it is
3302 called asynchronously from note_mouse_highlight. */
3303
3304int
3305string_buffer_position (w, string, around_charpos)
3306 struct window *w;
3307 Lisp_Object string;
3308 int around_charpos;
3309{
74bd6d65
GM
3310 Lisp_Object limit, prop, pos;
3311 const int MAX_DISTANCE = 1000;
3312 int found = 0;
3313
d8731202 3314 pos = make_number (around_charpos);
74bd6d65
GM
3315 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3316 while (!found && !EQ (pos, limit))
3317 {
3318 prop = Fget_char_property (pos, Qdisplay, Qnil);
3319 if (!NILP (prop) && display_prop_string_p (prop, string))
3320 found = 1;
3321 else
134d9283 3322 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3323 }
3324
3325 if (!found)
3326 {
d8731202 3327 pos = make_number (around_charpos);
74bd6d65
GM
3328 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3329 while (!found && !EQ (pos, limit))
3330 {
3331 prop = Fget_char_property (pos, Qdisplay, Qnil);
3332 if (!NILP (prop) && display_prop_string_p (prop, string))
3333 found = 1;
3334 else
134d9283
GM
3335 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3336 limit);
74bd6d65
GM
3337 }
3338 }
3339
3340 return found ? XINT (pos) : 0;
3341}
3342
3343
5f5c8ee5 3344\f
260a86a0
KH
3345/***********************************************************************
3346 `composition' property
3347 ***********************************************************************/
3348
3349/* Set up iterator IT from `composition' property at its current
3350 position. Called from handle_stop. */
3351
3352static enum prop_handled
3353handle_composition_prop (it)
3354 struct it *it;
3355{
3356 Lisp_Object prop, string;
3357 int pos, pos_byte, end;
3358 enum prop_handled handled = HANDLED_NORMALLY;
3359
3360 if (STRINGP (it->string))
3361 {
3362 pos = IT_STRING_CHARPOS (*it);
3363 pos_byte = IT_STRING_BYTEPOS (*it);
3364 string = it->string;
3365 }
3366 else
3367 {
3368 pos = IT_CHARPOS (*it);
3369 pos_byte = IT_BYTEPOS (*it);
3370 string = Qnil;
3371 }
3372
3373 /* If there's a valid composition and point is not inside of the
3374 composition (in the case that the composition is from the current
3375 buffer), draw a glyph composed from the composition components. */
3376 if (find_composition (pos, -1, &pos, &end, &prop, string)
3377 && COMPOSITION_VALID_P (pos, end, prop)
3378 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3379 {
3380 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3381
3382 if (id >= 0)
3383 {
3384 it->method = next_element_from_composition;
3385 it->cmp_id = id;
3386 it->cmp_len = COMPOSITION_LENGTH (prop);
3387 /* For a terminal, draw only the first character of the
3388 components. */
3389 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3390 it->len = (STRINGP (it->string)
3391 ? string_char_to_byte (it->string, end)
3392 : CHAR_TO_BYTE (end)) - pos_byte;
3393 it->stop_charpos = end;
3394 handled = HANDLED_RETURN;
3395 }
3396 }
3397
3398 return handled;
3399}
3400
3401
3402\f
5f5c8ee5
GM
3403/***********************************************************************
3404 Overlay strings
3405 ***********************************************************************/
3406
3407/* The following structure is used to record overlay strings for
3408 later sorting in load_overlay_strings. */
3409
3410struct overlay_entry
3411{
2970b9be 3412 Lisp_Object overlay;
5f5c8ee5
GM
3413 Lisp_Object string;
3414 int priority;
3415 int after_string_p;
3416};
3417
3418
3419/* Set up iterator IT from overlay strings at its current position.
3420 Called from handle_stop. */
3421
3422static enum prop_handled
3423handle_overlay_change (it)
3424 struct it *it;
3425{
5a08cbaf 3426 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3427 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3428 else
2970b9be 3429 return HANDLED_NORMALLY;
5f5c8ee5
GM
3430}
3431
3432
3433/* Set up the next overlay string for delivery by IT, if there is an
3434 overlay string to deliver. Called by set_iterator_to_next when the
3435 end of the current overlay string is reached. If there are more
3436 overlay strings to display, IT->string and
3437 IT->current.overlay_string_index are set appropriately here.
3438 Otherwise IT->string is set to nil. */
2311178e 3439
5f5c8ee5
GM
3440static void
3441next_overlay_string (it)
3442 struct it *it;
3443{
3444 ++it->current.overlay_string_index;
3445 if (it->current.overlay_string_index == it->n_overlay_strings)
3446 {
3447 /* No more overlay strings. Restore IT's settings to what
3448 they were before overlay strings were processed, and
3449 continue to deliver from current_buffer. */
5a08cbaf 3450 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 3451
5f5c8ee5
GM
3452 pop_it (it);
3453 xassert (it->stop_charpos >= BEGV
3454 && it->stop_charpos <= it->end_charpos);
3455 it->string = Qnil;
3456 it->current.overlay_string_index = -1;
3457 SET_TEXT_POS (it->current.string_pos, -1, -1);
3458 it->n_overlay_strings = 0;
3459 it->method = next_element_from_buffer;
2970b9be
GM
3460
3461 /* If we're at the end of the buffer, record that we have
3462 processed the overlay strings there already, so that
3463 next_element_from_buffer doesn't try it again. */
3464 if (IT_CHARPOS (*it) >= it->end_charpos)
3465 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
3466
3467 /* If we have to display `...' for invisible text, set
3468 the iterator up for that. */
3469 if (display_ellipsis_p)
3470 setup_for_ellipsis (it);
5f5c8ee5
GM
3471 }
3472 else
3473 {
3474 /* There are more overlay strings to process. If
3475 IT->current.overlay_string_index has advanced to a position
3476 where we must load IT->overlay_strings with more strings, do
3477 it. */
3478 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 3479
5f5c8ee5 3480 if (it->current.overlay_string_index && i == 0)
5a08cbaf 3481 load_overlay_strings (it, 0);
5f5c8ee5
GM
3482
3483 /* Initialize IT to deliver display elements from the overlay
3484 string. */
3485 it->string = it->overlay_strings[i];
3486 it->multibyte_p = STRING_MULTIBYTE (it->string);
3487 SET_TEXT_POS (it->current.string_pos, 0, 0);
3488 it->method = next_element_from_string;
3489 it->stop_charpos = 0;
3490 }
2311178e 3491
5f5c8ee5
GM
3492 CHECK_IT (it);
3493}
3494
3495
3496/* Compare two overlay_entry structures E1 and E2. Used as a
3497 comparison function for qsort in load_overlay_strings. Overlay
3498 strings for the same position are sorted so that
3499
2970b9be
GM
3500 1. All after-strings come in front of before-strings, except
3501 when they come from the same overlay.
2311178e 3502
5f5c8ee5
GM
3503 2. Within after-strings, strings are sorted so that overlay strings
3504 from overlays with higher priorities come first.
3505
3506 2. Within before-strings, strings are sorted so that overlay
3507 strings from overlays with higher priorities come last.
3508
3509 Value is analogous to strcmp. */
3510
2311178e 3511
5f5c8ee5
GM
3512static int
3513compare_overlay_entries (e1, e2)
3514 void *e1, *e2;
3515{
3516 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3517 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3518 int result;
3519
3520 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3521 {
3522 /* Let after-strings appear in front of before-strings if
3523 they come from different overlays. */
3524 if (EQ (entry1->overlay, entry2->overlay))
3525 result = entry1->after_string_p ? 1 : -1;
3526 else
3527 result = entry1->after_string_p ? -1 : 1;
3528 }
5f5c8ee5
GM
3529 else if (entry1->after_string_p)
3530 /* After-strings sorted in order of decreasing priority. */
3531 result = entry2->priority - entry1->priority;
3532 else
3533 /* Before-strings sorted in order of increasing priority. */
3534 result = entry1->priority - entry2->priority;
3535
3536 return result;
3537}
3538
3539
3540/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
3541 current buffer position, or from CHARPOS if that is > 0. Set
3542 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
3543
3544 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3545 a time. On entry into load_overlay_strings,
3546 IT->current.overlay_string_index gives the number of overlay
3547 strings that have already been loaded by previous calls to this
3548 function.
3549
2970b9be
GM
3550 IT->add_overlay_start contains an additional overlay start
3551 position to consider for taking overlay strings from, if non-zero.
3552 This position comes into play when the overlay has an `invisible'
3553 property, and both before and after-strings. When we've skipped to
3554 the end of the overlay, because of its `invisible' property, we
3555 nevertheless want its before-string to appear.
3556 IT->add_overlay_start will contain the overlay start position
3557 in this case.
3558
5f5c8ee5
GM
3559 Overlay strings are sorted so that after-string strings come in
3560 front of before-string strings. Within before and after-strings,
3561 strings are sorted by overlay priority. See also function
3562 compare_overlay_entries. */
2311178e 3563
5f5c8ee5 3564static void
5a08cbaf 3565load_overlay_strings (it, charpos)
5f5c8ee5 3566 struct it *it;
5a08cbaf 3567 int charpos;
5f5c8ee5
GM
3568{
3569 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3570 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3571 int start, end;
3572 int size = 20;
cafafe0b 3573 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3574 struct overlay_entry *entries
3575 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
3576
3577 if (charpos <= 0)
3578 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3579
3580 /* Append the overlay string STRING of overlay OVERLAY to vector
3581 `entries' which has size `size' and currently contains `n'
3582 elements. AFTER_P non-zero means STRING is an after-string of
3583 OVERLAY. */
3584#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3585 do \
3586 { \
3587 Lisp_Object priority; \
3588 \
3589 if (n == size) \
3590 { \
3591 int new_size = 2 * size; \
3592 struct overlay_entry *old = entries; \
3593 entries = \
3594 (struct overlay_entry *) alloca (new_size \
3595 * sizeof *entries); \
3596 bcopy (old, entries, size * sizeof *entries); \
3597 size = new_size; \
3598 } \
3599 \
3600 entries[n].string = (STRING); \
2970b9be 3601 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3602 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3603 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3604 entries[n].after_string_p = (AFTER_P); \
3605 ++n; \
3606 } \
3607 while (0)
3608
3609 /* Process overlay before the overlay center. */
2970b9be 3610 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3611 {
9472f927 3612 overlay = XCAR (ov);
5f5c8ee5
GM
3613 xassert (OVERLAYP (overlay));
3614 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3615 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 3616
cafafe0b 3617 if (end < charpos)
5f5c8ee5
GM
3618 break;
3619
3620 /* Skip this overlay if it doesn't start or end at IT's current
3621 position. */
cafafe0b 3622 if (end != charpos && start != charpos)
5f5c8ee5 3623 continue;
2311178e 3624
5f5c8ee5
GM
3625 /* Skip this overlay if it doesn't apply to IT->w. */
3626 window = Foverlay_get (overlay, Qwindow);
3627 if (WINDOWP (window) && XWINDOW (window) != it->w)
3628 continue;
3629
cafafe0b
GM
3630 /* If the text ``under'' the overlay is invisible, both before-
3631 and after-strings from this overlay are visible; start and
3632 end position are indistinguishable. */
3633 invisible = Foverlay_get (overlay, Qinvisible);
3634 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3635
5f5c8ee5 3636 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3637 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 3638 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 3639 && SCHARS (str))
5f5c8ee5 3640 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 3641
5f5c8ee5 3642 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3643 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 3644 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 3645 && SCHARS (str))
5f5c8ee5
GM
3646 RECORD_OVERLAY_STRING (overlay, str, 1);
3647 }
2311178e 3648
5f5c8ee5 3649 /* Process overlays after the overlay center. */
2970b9be 3650 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3651 {
9472f927 3652 overlay = XCAR (ov);
5f5c8ee5
GM
3653 xassert (OVERLAYP (overlay));
3654 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3655 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3656
cafafe0b 3657 if (start > charpos)
5f5c8ee5 3658 break;
2311178e 3659
5f5c8ee5
GM
3660 /* Skip this overlay if it doesn't start or end at IT's current
3661 position. */
cafafe0b 3662 if (end != charpos && start != charpos)
5f5c8ee5
GM
3663 continue;
3664
3665 /* Skip this overlay if it doesn't apply to IT->w. */
3666 window = Foverlay_get (overlay, Qwindow);
3667 if (WINDOWP (window) && XWINDOW (window) != it->w)
3668 continue;
2311178e 3669
cafafe0b
GM
3670 /* If the text ``under'' the overlay is invisible, it has a zero
3671 dimension, and both before- and after-strings apply. */
3672 invisible = Foverlay_get (overlay, Qinvisible);
3673 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3674
5f5c8ee5 3675 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3676 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 3677 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 3678 && SCHARS (str))
5f5c8ee5 3679 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 3680
5f5c8ee5 3681 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3682 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 3683 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 3684 && SCHARS (str))
5f5c8ee5
GM
3685 RECORD_OVERLAY_STRING (overlay, str, 1);
3686 }
3687
3688#undef RECORD_OVERLAY_STRING
2311178e 3689
5f5c8ee5 3690 /* Sort entries. */
cafafe0b 3691 if (n > 1)
2970b9be 3692 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3693
3694 /* Record the total number of strings to process. */
3695 it->n_overlay_strings = n;
3696
3697 /* IT->current.overlay_string_index is the number of overlay strings
3698 that have already been consumed by IT. Copy some of the
3699 remaining overlay strings to IT->overlay_strings. */
3700 i = 0;
3701 j = it->current.overlay_string_index;
3702 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3703 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3704
5f5c8ee5
GM
3705 CHECK_IT (it);
3706}
3707
3708
3709/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
3710 position, or at CHARPOS if that is > 0. Value is non-zero if at
3711 least one overlay string was found. */
5f5c8ee5
GM
3712
3713static int
5a08cbaf 3714get_overlay_strings (it, charpos)
5f5c8ee5 3715 struct it *it;
5a08cbaf 3716 int charpos;
5f5c8ee5
GM
3717{
3718 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3719 process. This fills IT->overlay_strings with strings, and sets
3720 IT->n_overlay_strings to the total number of strings to process.
3721 IT->pos.overlay_string_index has to be set temporarily to zero
3722 because load_overlay_strings needs this; it must be set to -1
3723 when no overlay strings are found because a zero value would
3724 indicate a position in the first overlay string. */
3725 it->current.overlay_string_index = 0;
5a08cbaf 3726 load_overlay_strings (it, charpos);
5f5c8ee5
GM
3727
3728 /* If we found overlay strings, set up IT to deliver display
3729 elements from the first one. Otherwise set up IT to deliver
3730 from current_buffer. */
3731 if (it->n_overlay_strings)
3732 {
3733 /* Make sure we know settings in current_buffer, so that we can
3734 restore meaningful values when we're done with the overlay
3735 strings. */
3736 compute_stop_pos (it);
3737 xassert (it->face_id >= 0);
2311178e 3738
5f5c8ee5
GM
3739 /* Save IT's settings. They are restored after all overlay
3740 strings have been processed. */
3741 xassert (it->sp == 0);
3742 push_it (it);
3743
3744 /* Set up IT to deliver display elements from the first overlay
3745 string. */
3746 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 3747 it->string = it->overlay_strings[0];
b2046df8 3748 it->stop_charpos = 0;
5f5c8ee5 3749 xassert (STRINGP (it->string));
2051c264 3750 it->end_charpos = SCHARS (it->string);
d5db4077 3751 it->multibyte_p = STRING_MULTIBYTE (it->string);
5f5c8ee5
GM
3752 it->method = next_element_from_string;
3753 }
3754 else
3755 {
3756 it->string = Qnil;
3757 it->current.overlay_string_index = -1;
3758 it->method = next_element_from_buffer;
3759 }
3760
3761 CHECK_IT (it);
3762
3763 /* Value is non-zero if we found at least one overlay string. */
3764 return STRINGP (it->string);
3765}
3766
3767
3768\f
3769/***********************************************************************
3770 Saving and restoring state
3771 ***********************************************************************/
3772
3773/* Save current settings of IT on IT->stack. Called, for example,
3774 before setting up IT for an overlay string, to be able to restore
3775 IT's settings to what they were after the overlay string has been
3776 processed. */
3777
3778static void
3779push_it (it)
3780 struct it *it;
3781{
3782 struct iterator_stack_entry *p;
2311178e 3783
5f5c8ee5
GM
3784 xassert (it->sp < 2);
3785 p = it->stack + it->sp;
3786
3787 p->stop_charpos = it->stop_charpos;
3788 xassert (it->face_id >= 0);
3789 p->face_id = it->face_id;
3790 p->string = it->string;
3791 p->pos = it->current;
3792 p->end_charpos = it->end_charpos;
3793 p->string_nchars = it->string_nchars;
3794 p->area = it->area;
3795 p->multibyte_p = it->multibyte_p;
3796 p->space_width = it->space_width;
3797 p->font_height = it->font_height;
3798 p->voffset = it->voffset;
3799 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 3800 p->display_ellipsis_p = 0;
5f5c8ee5
GM
3801 ++it->sp;
3802}
3803
3804
3805/* Restore IT's settings from IT->stack. Called, for example, when no
3806 more overlay strings must be processed, and we return to delivering
3807 display elements from a buffer, or when the end of a string from a
3808 `display' property is reached and we return to delivering display
3809 elements from an overlay string, or from a buffer. */
3810
3811static void
3812pop_it (it)
3813 struct it *it;
3814{
3815 struct iterator_stack_entry *p;
2311178e 3816
5f5c8ee5
GM
3817 xassert (it->sp > 0);
3818 --it->sp;
3819 p = it->stack + it->sp;
3820 it->stop_charpos = p->stop_charpos;
3821 it->face_id = p->face_id;
3822 it->string = p->string;
3823 it->current = p->pos;
3824 it->end_charpos = p->end_charpos;
3825 it->string_nchars = p->string_nchars;
3826 it->area = p->area;
3827 it->multibyte_p = p->multibyte_p;
3828 it->space_width = p->space_width;
3829 it->font_height = p->font_height;
3830 it->voffset = p->voffset;
3831 it->string_from_display_prop_p = p->string_from_display_prop_p;
3832}
3833
3834
3835\f
3836/***********************************************************************
3837 Moving over lines
3838 ***********************************************************************/
3839
3840/* Set IT's current position to the previous line start. */
3841
3842static void
3843back_to_previous_line_start (it)
3844 struct it *it;
3845{
3846 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3847 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3848}
3849
3850
cafafe0b 3851/* Move IT to the next line start.
2311178e 3852
cafafe0b
GM
3853 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3854 we skipped over part of the text (as opposed to moving the iterator
3855 continuously over the text). Otherwise, don't change the value
3856 of *SKIPPED_P.
2311178e 3857
cafafe0b
GM
3858 Newlines may come from buffer text, overlay strings, or strings
3859 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3860 simply use find_next_newline_no_quit.
3861
3862 Note that this function may not skip over invisible text that is so
3863 because of text properties and immediately follows a newline. If
3864 it would, function reseat_at_next_visible_line_start, when called
3865 from set_iterator_to_next, would effectively make invisible
3866 characters following a newline part of the wrong glyph row, which
3867 leads to wrong cursor motion. */
5f5c8ee5 3868
cafafe0b
GM
3869static int
3870forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3871 struct it *it;
cafafe0b 3872 int *skipped_p;
5f5c8ee5 3873{
54918e2b 3874 int old_selective, newline_found_p, n;
cafafe0b
GM
3875 const int MAX_NEWLINE_DISTANCE = 500;
3876
0fd37545
GM
3877 /* If already on a newline, just consume it to avoid unintended
3878 skipping over invisible text below. */
51695746
GM
3879 if (it->what == IT_CHARACTER
3880 && it->c == '\n'
3881 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
3882 {
3883 set_iterator_to_next (it, 0);
a77dc1ec 3884 it->c = 0;
0fd37545
GM
3885 return 1;
3886 }
3887
54918e2b 3888 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3889 unnecessary because it's done by the caller, and (b) leads to an
3890 infinite recursion because next_element_from_ellipsis indirectly
3891 calls this function. */
54918e2b
GM
3892 old_selective = it->selective;
3893 it->selective = 0;
3894
cafafe0b
GM
3895 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3896 from buffer text. */
3aec8722
GM
3897 for (n = newline_found_p = 0;
3898 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3899 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3900 {
8692ca92 3901 if (!get_next_display_element (it))
d43be70c 3902 return 0;
d02f1cb8 3903 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3904 set_iterator_to_next (it, 0);
cafafe0b
GM
3905 }
3906
3907 /* If we didn't find a newline near enough, see if we can use a
3908 short-cut. */
db0bb807 3909 if (!newline_found_p)
cafafe0b
GM
3910 {
3911 int start = IT_CHARPOS (*it);
3912 int limit = find_next_newline_no_quit (start, 1);
3913 Lisp_Object pos;
3914
3915 xassert (!STRINGP (it->string));
3916
3917 /* If there isn't any `display' property in sight, and no
3918 overlays, we can just use the position of the newline in
3919 buffer text. */
3920 if (it->stop_charpos >= limit
3921 || ((pos = Fnext_single_property_change (make_number (start),
3922 Qdisplay,
3923 Qnil, make_number (limit)),
3924 NILP (pos))
3925 && next_overlay_change (start) == ZV))
3926 {
3927 IT_CHARPOS (*it) = limit;
3928 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3929 *skipped_p = newline_found_p = 1;
3930 }
3931 else
3932 {
3933 while (get_next_display_element (it)
3934 && !newline_found_p)
3935 {
3936 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3937 set_iterator_to_next (it, 0);
3938 }
3939 }
3940 }
3941
54918e2b 3942 it->selective = old_selective;
cafafe0b 3943 return newline_found_p;
5f5c8ee5
GM
3944}
3945
3946
3947/* Set IT's current position to the previous visible line start. Skip
3948 invisible text that is so either due to text properties or due to
3949 selective display. Caution: this does not change IT->current_x and
3950 IT->hpos. */
3951
3952static void
3953back_to_previous_visible_line_start (it)
3954 struct it *it;
3955{
3956 int visible_p = 0;
3957
3958 /* Go back one newline if not on BEGV already. */
3959 if (IT_CHARPOS (*it) > BEGV)
3960 back_to_previous_line_start (it);
3961
3962 /* Move over lines that are invisible because of selective display
3963 or text properties. */
3964 while (IT_CHARPOS (*it) > BEGV
3965 && !visible_p)
3966 {
3967 visible_p = 1;
3968
3969 /* If selective > 0, then lines indented more than that values
3970 are invisible. */
3971 if (it->selective > 0
3972 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 3973 (double) it->selective)) /* iftc */
5f5c8ee5 3974 visible_p = 0;
2311178e 3975 else
5f5c8ee5
GM
3976 {
3977 Lisp_Object prop;
3978
6fc556fd
KR
3979 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3980 Qinvisible, it->window);
5f5c8ee5
GM
3981 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3982 visible_p = 0;
3983 }
5f5c8ee5
GM
3984
3985 /* Back one more newline if the current one is invisible. */
3986 if (!visible_p)
3987 back_to_previous_line_start (it);
3988 }
3989
3990 xassert (IT_CHARPOS (*it) >= BEGV);
3991 xassert (IT_CHARPOS (*it) == BEGV
3992 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3993 CHECK_IT (it);
3994}
3995
3996
3997/* Reseat iterator IT at the previous visible line start. Skip
3998 invisible text that is so either due to text properties or due to
3999 selective display. At the end, update IT's overlay information,
4000 face information etc. */
4001
4002static void
4003reseat_at_previous_visible_line_start (it)
4004 struct it *it;
4005{
4006 back_to_previous_visible_line_start (it);
4007 reseat (it, it->current.pos, 1);
4008 CHECK_IT (it);
4009}
4010
4011
4012/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
4013 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4014 preceding the line start. Skip over invisible text that is so
4015 because of selective display. Compute faces, overlays etc at the
4016 new position. Note that this function does not skip over text that
4017 is invisible because of text properties. */
5f5c8ee5
GM
4018
4019static void
312246d1 4020reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 4021 struct it *it;
312246d1 4022 int on_newline_p;
5f5c8ee5 4023{
cafafe0b
GM
4024 int newline_found_p, skipped_p = 0;
4025
4026 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4027
4028 /* Skip over lines that are invisible because they are indented
4029 more than the value of IT->selective. */
4030 if (it->selective > 0)
4031 while (IT_CHARPOS (*it) < ZV
a77dc1ec 4032 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4033 (double) it->selective)) /* iftc */
a77dc1ec
GM
4034 {
4035 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4036 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4037 }
cafafe0b
GM
4038
4039 /* Position on the newline if that's what's requested. */
4040 if (on_newline_p && newline_found_p)
5f5c8ee5 4041 {
cafafe0b 4042 if (STRINGP (it->string))
5f5c8ee5 4043 {
cafafe0b
GM
4044 if (IT_STRING_CHARPOS (*it) > 0)
4045 {
4046 --IT_STRING_CHARPOS (*it);
4047 --IT_STRING_BYTEPOS (*it);
4048 }
5f5c8ee5 4049 }
cafafe0b 4050 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
4051 {
4052 --IT_CHARPOS (*it);
cafafe0b
GM
4053 --IT_BYTEPOS (*it);
4054 reseat (it, it->current.pos, 0);
312246d1 4055 }
5f5c8ee5 4056 }
cafafe0b
GM
4057 else if (skipped_p)
4058 reseat (it, it->current.pos, 0);
2311178e 4059
5f5c8ee5
GM
4060 CHECK_IT (it);
4061}
4062
4063
4064\f
4065/***********************************************************************
4066 Changing an iterator's position
4067***********************************************************************/
4068
4069/* Change IT's current position to POS in current_buffer. If FORCE_P
4070 is non-zero, always check for text properties at the new position.
4071 Otherwise, text properties are only looked up if POS >=
4072 IT->check_charpos of a property. */
4073
4074static void
4075reseat (it, pos, force_p)
4076 struct it *it;
4077 struct text_pos pos;
4078 int force_p;
4079{
4080 int original_pos = IT_CHARPOS (*it);
4081
4082 reseat_1 (it, pos, 0);
4083
4084 /* Determine where to check text properties. Avoid doing it
4085 where possible because text property lookup is very expensive. */
4086 if (force_p
4087 || CHARPOS (pos) > it->stop_charpos
4088 || CHARPOS (pos) < original_pos)
4089 handle_stop (it);
4090
4091 CHECK_IT (it);
4092}
4093
4094
4095/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4096 IT->stop_pos to POS, also. */
4097
4098static void
4099reseat_1 (it, pos, set_stop_p)
4100 struct it *it;
4101 struct text_pos pos;
4102 int set_stop_p;
4103{
4104 /* Don't call this function when scanning a C string. */
4105 xassert (it->s == NULL);
4106
4107 /* POS must be a reasonable value. */
4108 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4109
4110 it->current.pos = it->position = pos;
4111 XSETBUFFER (it->object, current_buffer);
78c663d8 4112 it->end_charpos = ZV;
5f5c8ee5
GM
4113 it->dpvec = NULL;
4114 it->current.dpvec_index = -1;
4115 it->current.overlay_string_index = -1;
4116 IT_STRING_CHARPOS (*it) = -1;
4117 IT_STRING_BYTEPOS (*it) = -1;
4118 it->string = Qnil;
4119 it->method = next_element_from_buffer;
06fd3792 4120 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4121 it->sp = 0;
4aad61f8 4122 it->face_before_selective_p = 0;
5f5c8ee5
GM
4123
4124 if (set_stop_p)
4125 it->stop_charpos = CHARPOS (pos);
4126}
4127
4128
4129/* Set up IT for displaying a string, starting at CHARPOS in window W.
4130 If S is non-null, it is a C string to iterate over. Otherwise,
4131 STRING gives a Lisp string to iterate over.
2311178e 4132
5f5c8ee5
GM
4133 If PRECISION > 0, don't return more then PRECISION number of
4134 characters from the string.
4135
4136 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4137 characters have been returned. FIELD_WIDTH < 0 means an infinite
4138 field width.
4139
4140 MULTIBYTE = 0 means disable processing of multibyte characters,
4141 MULTIBYTE > 0 means enable it,
4142 MULTIBYTE < 0 means use IT->multibyte_p.
4143
4144 IT must be initialized via a prior call to init_iterator before
4145 calling this function. */
4146
4147static void
4148reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4149 struct it *it;
4150 unsigned char *s;
4151 Lisp_Object string;
4152 int charpos;
4153 int precision, field_width, multibyte;
4154{
4155 /* No region in strings. */
4156 it->region_beg_charpos = it->region_end_charpos = -1;
4157
4158 /* No text property checks performed by default, but see below. */
4159 it->stop_charpos = -1;
4160
4161 /* Set iterator position and end position. */
4162 bzero (&it->current, sizeof it->current);
4163 it->current.overlay_string_index = -1;
4164 it->current.dpvec_index = -1;
5f5c8ee5 4165 xassert (charpos >= 0);
2311178e 4166
e719f5ae
GM
4167 /* If STRING is specified, use its multibyteness, otherwise use the
4168 setting of MULTIBYTE, if specified. */
cabf45da 4169 if (multibyte >= 0)
5f5c8ee5 4170 it->multibyte_p = multibyte > 0;
2311178e 4171
5f5c8ee5
GM
4172 if (s == NULL)
4173 {
4174 xassert (STRINGP (string));
4175 it->string = string;
4176 it->s = NULL;
2051c264 4177 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4178 it->method = next_element_from_string;
4179 it->current.string_pos = string_pos (charpos, string);
4180 }
4181 else
4182 {
4183 it->s = s;
4184 it->string = Qnil;
4185
4186 /* Note that we use IT->current.pos, not it->current.string_pos,
4187 for displaying C strings. */
4188 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4189 if (it->multibyte_p)
4190 {
4191 it->current.pos = c_string_pos (charpos, s, 1);
4192 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4193 }
4194 else
4195 {
4196 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4197 it->end_charpos = it->string_nchars = strlen (s);
4198 }
2311178e 4199
5f5c8ee5
GM
4200 it->method = next_element_from_c_string;
4201 }
4202
4203 /* PRECISION > 0 means don't return more than PRECISION characters
4204 from the string. */
4205 if (precision > 0 && it->end_charpos - charpos > precision)
4206 it->end_charpos = it->string_nchars = charpos + precision;
4207
4208 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4209 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4210 FIELD_WIDTH < 0 means infinite field width. This is useful for
4211 padding with `-' at the end of a mode line. */
4212 if (field_width < 0)
4213 field_width = INFINITY;
4214 if (field_width > it->end_charpos - charpos)
4215 it->end_charpos = charpos + field_width;
4216
4217 /* Use the standard display table for displaying strings. */
4218 if (DISP_TABLE_P (Vstandard_display_table))
4219 it->dp = XCHAR_TABLE (Vstandard_display_table);
4220
4221 it->stop_charpos = charpos;
4222 CHECK_IT (it);
4223}
4224
4225
4226\f
4227/***********************************************************************
4228 Iteration
4229 ***********************************************************************/
4230
4231/* Load IT's display element fields with information about the next
4232 display element from the current position of IT. Value is zero if
4233 end of buffer (or C string) is reached. */
4234
4235int
4236get_next_display_element (it)
4237 struct it *it;
4238{
7d0393cf 4239 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
4240 we hit the end of what we iterate over. Performance note: the
4241 function pointer `method' used here turns out to be faster than
4242 using a sequence of if-statements. */
4243 int success_p = (*it->method) (it);
5f5c8ee5
GM
4244
4245 if (it->what == IT_CHARACTER)
4246 {
4247 /* Map via display table or translate control characters.
4248 IT->c, IT->len etc. have been set to the next character by
4249 the function call above. If we have a display table, and it
4250 contains an entry for IT->c, translate it. Don't do this if
4251 IT->c itself comes from a display table, otherwise we could
4252 end up in an infinite recursion. (An alternative could be to
4253 count the recursion depth of this function and signal an
4254 error when a certain maximum depth is reached.) Is it worth
4255 it? */
4256 if (success_p && it->dpvec == NULL)
4257 {
4258 Lisp_Object dv;
4259
4260 if (it->dp
4261 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4262 VECTORP (dv)))
4263 {
4264 struct Lisp_Vector *v = XVECTOR (dv);
4265
4266 /* Return the first character from the display table
4267 entry, if not empty. If empty, don't display the
4268 current character. */
4269 if (v->size)
4270 {
4271 it->dpvec_char_len = it->len;
4272 it->dpvec = v->contents;
4273 it->dpend = v->contents + v->size;
4274 it->current.dpvec_index = 0;
4275 it->method = next_element_from_display_vector;
7eee47cc
GM
4276 success_p = get_next_display_element (it);
4277 }
4278 else
4279 {
4280 set_iterator_to_next (it, 0);
4281 success_p = get_next_display_element (it);
5f5c8ee5 4282 }
5f5c8ee5
GM
4283 }
4284
4285 /* Translate control characters into `\003' or `^C' form.
4286 Control characters coming from a display table entry are
4287 currently not translated because we use IT->dpvec to hold
4288 the translation. This could easily be changed but I
197516c2
KH
4289 don't believe that it is worth doing.
4290
fa831cf8
KH
4291 If it->multibyte_p is nonzero, eight-bit characters and
4292 non-printable multibyte characters are also translated to
4293 octal form.
4294
4295 If it->multibyte_p is zero, eight-bit characters that
4296 don't have corresponding multibyte char code are also
4297 translated to octal form. */
ba197fe6 4298 else if ((it->c < ' '
5f5c8ee5 4299 && (it->area != TEXT_AREA
c6e89d6c 4300 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
4301 || (it->multibyte_p
4302 ? ((it->c >= 127
4303 && it->len == 1)
4304 || !CHAR_PRINTABLE_P (it->c))
ba197fe6 4305 : (it->c >= 127
fa831cf8 4306 && it->c == unibyte_char_to_multibyte (it->c))))
5f5c8ee5
GM
4307 {
4308 /* IT->c is a control character which must be displayed
4309 either as '\003' or as `^C' where the '\\' and '^'
4310 can be defined in the display table. Fill
4311 IT->ctl_chars with glyphs for what we have to
4312 display. Then, set IT->dpvec to these glyphs. */
4313 GLYPH g;
4314
54c85a23 4315 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4316 {
4317 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4318 if (it->dp
4319 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4320 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4321 g = XINT (DISP_CTRL_GLYPH (it->dp));
4322 else
4323 g = FAST_MAKE_GLYPH ('^', 0);
4324 XSETINT (it->ctl_chars[0], g);
4325
4326 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4327 XSETINT (it->ctl_chars[1], g);
4328
4329 /* Set up IT->dpvec and return first character from it. */
4330 it->dpvec_char_len = it->len;
4331 it->dpvec = it->ctl_chars;
4332 it->dpend = it->dpvec + 2;
4333 it->current.dpvec_index = 0;
4334 it->method = next_element_from_display_vector;
4335 get_next_display_element (it);
4336 }
4337 else
4338 {
260a86a0 4339 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4340 int len;
197516c2
KH
4341 int i;
4342 GLYPH escape_glyph;
4343
5f5c8ee5
GM
4344 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4345 if (it->dp
4346 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4347 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4348 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4349 else
197516c2
KH
4350 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4351
c5924f47
KH
4352 if (SINGLE_BYTE_CHAR_P (it->c))
4353 str[0] = it->c, len = 1;
4354 else
ea9dd091
GM
4355 {
4356 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4357 if (len < 0)
4358 {
4359 /* It's an invalid character, which
4360 shouldn't happen actually, but due to
4361 bugs it may happen. Let's print the char
4362 as is, there's not much meaningful we can
4363 do with it. */
4364 str[0] = it->c;
4365 str[1] = it->c >> 8;
4366 str[2] = it->c >> 16;
4367 str[3] = it->c >> 24;
4368 len = 4;
4369 }
4370 }
c5924f47 4371
197516c2
KH
4372 for (i = 0; i < len; i++)
4373 {
4374 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4375 /* Insert three more glyphs into IT->ctl_chars for
4376 the octal display of the character. */
2311178e 4377 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
197516c2 4378 XSETINT (it->ctl_chars[i * 4 + 1], g);
2311178e 4379 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
197516c2 4380 XSETINT (it->ctl_chars[i * 4 + 2], g);
2311178e 4381 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
197516c2
KH
4382 XSETINT (it->ctl_chars[i * 4 + 3], g);
4383 }
5f5c8ee5
GM
4384
4385 /* Set up IT->dpvec and return the first character
4386 from it. */
4387 it->dpvec_char_len = it->len;
4388 it->dpvec = it->ctl_chars;
197516c2 4389 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4390 it->current.dpvec_index = 0;
4391 it->method = next_element_from_display_vector;
4392 get_next_display_element (it);
4393 }
4394 }
4395 }
4396
980806b6
KH
4397 /* Adjust face id for a multibyte character. There are no
4398 multibyte character in unibyte text. */
5f5c8ee5
GM
4399 if (it->multibyte_p
4400 && success_p
980806b6 4401 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4402 {
980806b6
KH
4403 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4404 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4405 }
4406 }
4407
4408 /* Is this character the last one of a run of characters with
4409 box? If yes, set IT->end_of_box_run_p to 1. */
4410 if (it->face_box_p
4411 && it->s == NULL)
4412 {
4413 int face_id;
4414 struct face *face;
4415
4416 it->end_of_box_run_p
4417 = ((face_id = face_after_it_pos (it),
4418 face_id != it->face_id)
4419 && (face = FACE_FROM_ID (it->f, face_id),
4420 face->box == FACE_NO_BOX));
4421 }
4422
4423 /* Value is 0 if end of buffer or string reached. */
4424 return success_p;
4425}
4426
4427
4428/* Move IT to the next display element.
4429
cafafe0b
GM
4430 RESEAT_P non-zero means if called on a newline in buffer text,
4431 skip to the next visible line start.
4432
5f5c8ee5
GM
4433 Functions get_next_display_element and set_iterator_to_next are
4434 separate because I find this arrangement easier to handle than a
4435 get_next_display_element function that also increments IT's
4436 position. The way it is we can first look at an iterator's current
4437 display element, decide whether it fits on a line, and if it does,
4438 increment the iterator position. The other way around we probably
4439 would either need a flag indicating whether the iterator has to be
4440 incremented the next time, or we would have to implement a
4441 decrement position function which would not be easy to write. */
4442
4443void
cafafe0b 4444set_iterator_to_next (it, reseat_p)
5f5c8ee5 4445 struct it *it;
cafafe0b 4446 int reseat_p;
5f5c8ee5 4447{
483de32b
GM
4448 /* Reset flags indicating start and end of a sequence of characters
4449 with box. Reset them at the start of this function because
4450 moving the iterator to a new position might set them. */
4451 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 4452
5f5c8ee5
GM
4453 if (it->method == next_element_from_buffer)
4454 {
4455 /* The current display element of IT is a character from
4456 current_buffer. Advance in the buffer, and maybe skip over
4457 invisible lines that are so because of selective display. */
cafafe0b 4458 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4459 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4460 else
4461 {
4462 xassert (it->len != 0);
4463 IT_BYTEPOS (*it) += it->len;
4464 IT_CHARPOS (*it) += 1;
4465 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4466 }
4467 }
260a86a0
KH
4468 else if (it->method == next_element_from_composition)
4469 {
4470 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4471 if (STRINGP (it->string))
4472 {
4473 IT_STRING_BYTEPOS (*it) += it->len;
4474 IT_STRING_CHARPOS (*it) += it->cmp_len;
4475 it->method = next_element_from_string;
4476 goto consider_string_end;
4477 }
4478 else
4479 {
4480 IT_BYTEPOS (*it) += it->len;
4481 IT_CHARPOS (*it) += it->cmp_len;
4482 it->method = next_element_from_buffer;
4483 }
4484 }
5f5c8ee5
GM
4485 else if (it->method == next_element_from_c_string)
4486 {
4487 /* Current display element of IT is from a C string. */
4488 IT_BYTEPOS (*it) += it->len;
4489 IT_CHARPOS (*it) += 1;
4490 }
4491 else if (it->method == next_element_from_display_vector)
4492 {
4493 /* Current display element of IT is from a display table entry.
4494 Advance in the display table definition. Reset it to null if
4495 end reached, and continue with characters from buffers/
4496 strings. */
4497 ++it->current.dpvec_index;
286bcbc9 4498
980806b6
KH
4499 /* Restore face of the iterator to what they were before the
4500 display vector entry (these entries may contain faces). */
5f5c8ee5 4501 it->face_id = it->saved_face_id;
2311178e 4502
5f5c8ee5
GM
4503 if (it->dpvec + it->current.dpvec_index == it->dpend)
4504 {
4505 if (it->s)
4506 it->method = next_element_from_c_string;
4507 else if (STRINGP (it->string))
4508 it->method = next_element_from_string;
4509 else
4510 it->method = next_element_from_buffer;
4511
4512 it->dpvec = NULL;
4513 it->current.dpvec_index = -1;
4514
312246d1
GM
4515 /* Skip over characters which were displayed via IT->dpvec. */
4516 if (it->dpvec_char_len < 0)
4517 reseat_at_next_visible_line_start (it, 1);
4518 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4519 {
4520 it->len = it->dpvec_char_len;
cafafe0b 4521 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4522 }
4523 }
4524 }
4525 else if (it->method == next_element_from_string)
4526 {
4527 /* Current display element is a character from a Lisp string. */
4528 xassert (it->s == NULL && STRINGP (it->string));
4529 IT_STRING_BYTEPOS (*it) += it->len;
4530 IT_STRING_CHARPOS (*it) += 1;
2311178e 4531
5f5c8ee5
GM
4532 consider_string_end:
4533
4534 if (it->current.overlay_string_index >= 0)
4535 {
4536 /* IT->string is an overlay string. Advance to the
4537 next, if there is one. */
2051c264 4538 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4539 next_overlay_string (it);
4540 }
4541 else
4542 {
4543 /* IT->string is not an overlay string. If we reached
4544 its end, and there is something on IT->stack, proceed
4545 with what is on the stack. This can be either another
4546 string, this time an overlay string, or a buffer. */
2051c264 4547 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
4548 && it->sp > 0)
4549 {
4550 pop_it (it);
4551 if (!STRINGP (it->string))
4552 it->method = next_element_from_buffer;
b2046df8
GM
4553 else
4554 goto consider_string_end;
5f5c8ee5
GM
4555 }
4556 }
4557 }
4558 else if (it->method == next_element_from_image
4559 || it->method == next_element_from_stretch)
4560 {
4561 /* The position etc with which we have to proceed are on
4562 the stack. The position may be at the end of a string,
4563 if the `display' property takes up the whole string. */
4564 pop_it (it);
4565 it->image_id = 0;
4566 if (STRINGP (it->string))
4567 {
4568 it->method = next_element_from_string;
4569 goto consider_string_end;
4570 }
4571 else
4572 it->method = next_element_from_buffer;
4573 }
4574 else
4575 /* There are no other methods defined, so this should be a bug. */
4576 abort ();
4577
5f5c8ee5
GM
4578 xassert (it->method != next_element_from_string
4579 || (STRINGP (it->string)
4580 && IT_STRING_CHARPOS (*it) >= 0));
4581}
4582
4583
4584/* Load IT's display element fields with information about the next
4585 display element which comes from a display table entry or from the
4586 result of translating a control character to one of the forms `^C'
4587 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4588
4589static int
4590next_element_from_display_vector (it)
4591 struct it *it;
4592{
4593 /* Precondition. */
4594 xassert (it->dpvec && it->current.dpvec_index >= 0);
4595
4596 /* Remember the current face id in case glyphs specify faces.
4597 IT's face is restored in set_iterator_to_next. */
4598 it->saved_face_id = it->face_id;
2311178e 4599
5f5c8ee5
GM
4600 if (INTEGERP (*it->dpvec)
4601 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4602 {
4603 int lface_id;
4604 GLYPH g;
4605
4606 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4607 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4608 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4609
4610 /* The entry may contain a face id to use. Such a face id is
4611 the id of a Lisp face, not a realized face. A face id of
969065c3 4612 zero means no face is specified. */
5f5c8ee5
GM
4613 lface_id = FAST_GLYPH_FACE (g);
4614 if (lface_id)
4615 {
969065c3 4616 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4617 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4618 if (face_id >= 0)
969065c3 4619 it->face_id = face_id;
5f5c8ee5
GM
4620 }
4621 }
4622 else
4623 /* Display table entry is invalid. Return a space. */
4624 it->c = ' ', it->len = 1;
4625
4626 /* Don't change position and object of the iterator here. They are
4627 still the values of the character that had this display table
4628 entry or was translated, and that's what we want. */
4629 it->what = IT_CHARACTER;
4630 return 1;
4631}
4632
4633
4634/* Load IT with the next display element from Lisp string IT->string.
4635 IT->current.string_pos is the current position within the string.
4636 If IT->current.overlay_string_index >= 0, the Lisp string is an
4637 overlay string. */
4638
4639static int
4640next_element_from_string (it)
4641 struct it *it;
4642{
4643 struct text_pos position;
4644
4645 xassert (STRINGP (it->string));
4646 xassert (IT_STRING_CHARPOS (*it) >= 0);
4647 position = it->current.string_pos;
4648
4649 /* Time to check for invisible text? */
4650 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4651 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4652 {
4653 handle_stop (it);
4654
4655 /* Since a handler may have changed IT->method, we must
4656 recurse here. */
4657 return get_next_display_element (it);
4658 }
4659
4660 if (it->current.overlay_string_index >= 0)
4661 {
4662 /* Get the next character from an overlay string. In overlay
4663 strings, There is no field width or padding with spaces to
4664 do. */
2051c264 4665 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4666 {
4667 it->what = IT_EOB;
4668 return 0;
4669 }
4670 else if (STRING_MULTIBYTE (it->string))
4671 {
2051c264 4672 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
4673 const unsigned char *s = (SDATA (it->string)
4674 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4675 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4676 }
4677 else
4678 {
2051c264 4679 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
4680 it->len = 1;
4681 }
4682 }
4683 else
4684 {
4685 /* Get the next character from a Lisp string that is not an
4686 overlay string. Such strings come from the mode line, for
4687 example. We may have to pad with spaces, or truncate the
4688 string. See also next_element_from_c_string. */
4689 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4690 {
4691 it->what = IT_EOB;
4692 return 0;
4693 }
4694 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4695 {
4696 /* Pad with spaces. */
4697 it->c = ' ', it->len = 1;
4698 CHARPOS (position) = BYTEPOS (position) = -1;
4699 }
4700 else if (STRING_MULTIBYTE (it->string))
4701 {
2051c264 4702 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
4703 const unsigned char *s = (SDATA (it->string)
4704 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4705 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4706 }
4707 else
4708 {
2051c264 4709 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
4710 it->len = 1;
4711 }
4712 }
4713
4714 /* Record what we have and where it came from. Note that we store a
4715 buffer position in IT->position although it could arguably be a
4716 string position. */
4717 it->what = IT_CHARACTER;
4718 it->object = it->string;
4719 it->position = position;
4720 return 1;
4721}
4722
4723
4724/* Load IT with next display element from C string IT->s.
4725 IT->string_nchars is the maximum number of characters to return
4726 from the string. IT->end_charpos may be greater than
4727 IT->string_nchars when this function is called, in which case we
4728 may have to return padding spaces. Value is zero if end of string
4729 reached, including padding spaces. */
4730
4731static int
4732next_element_from_c_string (it)
4733 struct it *it;
4734{
4735 int success_p = 1;
2311178e 4736
5f5c8ee5
GM
4737 xassert (it->s);
4738 it->what = IT_CHARACTER;
4739 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4740 it->object = Qnil;
2311178e 4741
5f5c8ee5
GM
4742 /* IT's position can be greater IT->string_nchars in case a field
4743 width or precision has been specified when the iterator was
4744 initialized. */
4745 if (IT_CHARPOS (*it) >= it->end_charpos)
4746 {
4747 /* End of the game. */
4748 it->what = IT_EOB;
4749 success_p = 0;
4750 }
4751 else if (IT_CHARPOS (*it) >= it->string_nchars)
4752 {
4753 /* Pad with spaces. */
4754 it->c = ' ', it->len = 1;
4755 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4756 }
4757 else if (it->multibyte_p)
4758 {
4759 /* Implementation note: The calls to strlen apparently aren't a
4760 performance problem because there is no noticeable performance
4761 difference between Emacs running in unibyte or multibyte mode. */
4762 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4763 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4764 maxlen, &it->len);
5f5c8ee5
GM
4765 }
4766 else
4767 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 4768
5f5c8ee5
GM
4769 return success_p;
4770}
4771
4772
4773/* Set up IT to return characters from an ellipsis, if appropriate.
4774 The definition of the ellipsis glyphs may come from a display table
4775 entry. This function Fills IT with the first glyph from the
4776 ellipsis if an ellipsis is to be displayed. */
4777
13f19968 4778static int
5f5c8ee5
GM
4779next_element_from_ellipsis (it)
4780 struct it *it;
4781{
13f19968 4782 if (it->selective_display_ellipsis_p)
5f5c8ee5 4783 {
13f19968
GM
4784 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4785 {
4786 /* Use the display table definition for `...'. Invalid glyphs
4787 will be handled by the method returning elements from dpvec. */
4788 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4789 it->dpvec_char_len = it->len;
4790 it->dpvec = v->contents;
4791 it->dpend = v->contents + v->size;
4792 it->current.dpvec_index = 0;
4793 it->method = next_element_from_display_vector;
4794 }
4795 else
4796 {
4797 /* Use default `...' which is stored in default_invis_vector. */
4798 it->dpvec_char_len = it->len;
4799 it->dpvec = default_invis_vector;
4800 it->dpend = default_invis_vector + 3;
4801 it->current.dpvec_index = 0;
4802 it->method = next_element_from_display_vector;
4803 }
5f5c8ee5 4804 }
13f19968 4805 else
54918e2b 4806 {
4aad61f8
GM
4807 /* The face at the current position may be different from the
4808 face we find after the invisible text. Remember what it
4809 was in IT->saved_face_id, and signal that it's there by
4810 setting face_before_selective_p. */
4811 it->saved_face_id = it->face_id;
54918e2b
GM
4812 it->method = next_element_from_buffer;
4813 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4814 it->face_before_selective_p = 1;
54918e2b 4815 }
2311178e 4816
13f19968 4817 return get_next_display_element (it);
5f5c8ee5
GM
4818}
4819
4820
4821/* Deliver an image display element. The iterator IT is already
4822 filled with image information (done in handle_display_prop). Value
4823 is always 1. */
2311178e 4824
5f5c8ee5
GM
4825
4826static int
4827next_element_from_image (it)
4828 struct it *it;
4829{
4830 it->what = IT_IMAGE;
4831 return 1;
4832}
4833
4834
4835/* Fill iterator IT with next display element from a stretch glyph
4836 property. IT->object is the value of the text property. Value is
4837 always 1. */
4838
4839static int
4840next_element_from_stretch (it)
4841 struct it *it;
4842{
4843 it->what = IT_STRETCH;
4844 return 1;
4845}
4846
4847
4848/* Load IT with the next display element from current_buffer. Value
4849 is zero if end of buffer reached. IT->stop_charpos is the next
4850 position at which to stop and check for text properties or buffer
4851 end. */
4852
4853static int
4854next_element_from_buffer (it)
4855 struct it *it;
4856{
4857 int success_p = 1;
4858
4859 /* Check this assumption, otherwise, we would never enter the
4860 if-statement, below. */
4861 xassert (IT_CHARPOS (*it) >= BEGV
4862 && IT_CHARPOS (*it) <= it->stop_charpos);
4863
4864 if (IT_CHARPOS (*it) >= it->stop_charpos)
4865 {
4866 if (IT_CHARPOS (*it) >= it->end_charpos)
4867 {
4868 int overlay_strings_follow_p;
2311178e 4869
5f5c8ee5
GM
4870 /* End of the game, except when overlay strings follow that
4871 haven't been returned yet. */
4872 if (it->overlay_strings_at_end_processed_p)
4873 overlay_strings_follow_p = 0;
4874 else
4875 {
4876 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 4877 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
4878 }
4879
4880 if (overlay_strings_follow_p)
4881 success_p = get_next_display_element (it);
4882 else
4883 {
4884 it->what = IT_EOB;
4885 it->position = it->current.pos;
4886 success_p = 0;
4887 }
4888 }
4889 else
4890 {
4891 handle_stop (it);
4892 return get_next_display_element (it);
4893 }
4894 }
4895 else
4896 {
4897 /* No face changes, overlays etc. in sight, so just return a
4898 character from current_buffer. */
4899 unsigned char *p;
4900
4901 /* Maybe run the redisplay end trigger hook. Performance note:
4902 This doesn't seem to cost measurable time. */
4903 if (it->redisplay_end_trigger_charpos
4904 && it->glyph_row
4905 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4906 run_redisplay_end_trigger_hook (it);
4907
4908 /* Get the next character, maybe multibyte. */
4909 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4910 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4911 {
4912 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4913 - IT_BYTEPOS (*it));
4fdb80f2 4914 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4915 }
4916 else
4917 it->c = *p, it->len = 1;
4918
4919 /* Record what we have and where it came from. */
4920 it->what = IT_CHARACTER;;
4921 it->object = it->w->buffer;
4922 it->position = it->current.pos;
4923
4924 /* Normally we return the character found above, except when we
4925 really want to return an ellipsis for selective display. */
4926 if (it->selective)
4927 {
4928 if (it->c == '\n')
4929 {
4930 /* A value of selective > 0 means hide lines indented more
4931 than that number of columns. */
4932 if (it->selective > 0
4933 && IT_CHARPOS (*it) + 1 < ZV
4934 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4935 IT_BYTEPOS (*it) + 1,
a67e162b 4936 (double) it->selective)) /* iftc */
312246d1 4937 {
13f19968 4938 success_p = next_element_from_ellipsis (it);
312246d1
GM
4939 it->dpvec_char_len = -1;
4940 }
5f5c8ee5
GM
4941 }
4942 else if (it->c == '\r' && it->selective == -1)
4943 {
4944 /* A value of selective == -1 means that everything from the
4945 CR to the end of the line is invisible, with maybe an
4946 ellipsis displayed for it. */
13f19968 4947 success_p = next_element_from_ellipsis (it);
312246d1 4948 it->dpvec_char_len = -1;
5f5c8ee5
GM
4949 }
4950 }
4951 }
4952
4953 /* Value is zero if end of buffer reached. */
c880678e 4954 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4955 return success_p;
4956}
4957
2311178e 4958
5f5c8ee5
GM
4959/* Run the redisplay end trigger hook for IT. */
4960
4961static void
4962run_redisplay_end_trigger_hook (it)
4963 struct it *it;
4964{
4965 Lisp_Object args[3];
4966
4967 /* IT->glyph_row should be non-null, i.e. we should be actually
4968 displaying something, or otherwise we should not run the hook. */
4969 xassert (it->glyph_row);
4970
4971 /* Set up hook arguments. */
4972 args[0] = Qredisplay_end_trigger_functions;
4973 args[1] = it->window;
4974 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4975 it->redisplay_end_trigger_charpos = 0;
4976
4977 /* Since we are *trying* to run these functions, don't try to run
4978 them again, even if they get an error. */
4979 it->w->redisplay_end_trigger = Qnil;
4980 Frun_hook_with_args (3, args);
2311178e 4981
5f5c8ee5
GM
4982 /* Notice if it changed the face of the character we are on. */
4983 handle_face_prop (it);
4984}
4985
4986
260a86a0
KH
4987/* Deliver a composition display element. The iterator IT is already
4988 filled with composition information (done in
4989 handle_composition_prop). Value is always 1. */
4990
4991static int
4992next_element_from_composition (it)
4993 struct it *it;
4994{
4995 it->what = IT_COMPOSITION;
4996 it->position = (STRINGP (it->string)
4997 ? it->current.string_pos
4998 : it->current.pos);
4999 return 1;
5000}
5001
5002
5f5c8ee5
GM
5003\f
5004/***********************************************************************
5005 Moving an iterator without producing glyphs
5006 ***********************************************************************/
5007
5008/* Move iterator IT to a specified buffer or X position within one
5009 line on the display without producing glyphs.
5010
c53a1624
RS
5011 OP should be a bit mask including some or all of these bits:
5012 MOVE_TO_X: Stop on reaching x-position TO_X.
5013 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5014 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 5015
c53a1624
RS
5016 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5017 This means, in particular, that TO_X includes window's horizontal
5018 scroll amount.
5f5c8ee5 5019
c53a1624
RS
5020 The return value has several possible values that
5021 say what condition caused the scan to stop:
5f5c8ee5
GM
5022
5023 MOVE_POS_MATCH_OR_ZV
5024 - when TO_POS or ZV was reached.
2311178e 5025
5f5c8ee5
GM
5026 MOVE_X_REACHED
5027 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 5028
5f5c8ee5
GM
5029 MOVE_LINE_CONTINUED
5030 - when we reached the end of the display area and the line must
5031 be continued.
2311178e 5032
5f5c8ee5
GM
5033 MOVE_LINE_TRUNCATED
5034 - when we reached the end of the display area and the line is
5035 truncated.
5036
5037 MOVE_NEWLINE_OR_CR
5038 - when we stopped at a line end, i.e. a newline or a CR and selective
5039 display is on. */
5040
701552dd 5041static enum move_it_result
5f5c8ee5
GM
5042move_it_in_display_line_to (it, to_charpos, to_x, op)
5043 struct it *it;
5044 int to_charpos, to_x, op;
5045{
5046 enum move_it_result result = MOVE_UNDEFINED;
5047 struct glyph_row *saved_glyph_row;
5048
5049 /* Don't produce glyphs in produce_glyphs. */
5050 saved_glyph_row = it->glyph_row;
5051 it->glyph_row = NULL;
5052
5f5c8ee5
GM
5053 while (1)
5054 {
ae26e27d 5055 int x, i, ascent = 0, descent = 0;
2311178e 5056
5f5c8ee5
GM
5057 /* Stop when ZV or TO_CHARPOS reached. */
5058 if (!get_next_display_element (it)
5059 || ((op & MOVE_TO_POS) != 0
5060 && BUFFERP (it->object)
5061 && IT_CHARPOS (*it) >= to_charpos))
5062 {
5063 result = MOVE_POS_MATCH_OR_ZV;
5064 break;
5065 }
2311178e 5066
5f5c8ee5
GM
5067 /* The call to produce_glyphs will get the metrics of the
5068 display element IT is loaded with. We record in x the
5069 x-position before this display element in case it does not
5070 fit on the line. */
5071 x = it->current_x;
2311178e 5072
47589c8c
GM
5073 /* Remember the line height so far in case the next element doesn't
5074 fit on the line. */
5075 if (!it->truncate_lines_p)
5076 {
5077 ascent = it->max_ascent;
5078 descent = it->max_descent;
5079 }
2311178e 5080
5f5c8ee5
GM
5081 PRODUCE_GLYPHS (it);
5082
5083 if (it->area != TEXT_AREA)
5084 {
cafafe0b 5085 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5086 continue;
5087 }
5088
5089 /* The number of glyphs we get back in IT->nglyphs will normally
5090 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5091 character on a terminal frame, or (iii) a line end. For the
5092 second case, IT->nglyphs - 1 padding glyphs will be present
5093 (on X frames, there is only one glyph produced for a
5094 composite character.
5095
5096 The behavior implemented below means, for continuation lines,
5097 that as many spaces of a TAB as fit on the current line are
5098 displayed there. For terminal frames, as many glyphs of a
5099 multi-glyph character are displayed in the current line, too.
5100 This is what the old redisplay code did, and we keep it that
5101 way. Under X, the whole shape of a complex character must
5102 fit on the line or it will be completely displayed in the
5103 next line.
5104
5105 Note that both for tabs and padding glyphs, all glyphs have
5106 the same width. */
5107 if (it->nglyphs)
5108 {
5109 /* More than one glyph or glyph doesn't fit on line. All
5110 glyphs have the same width. */
5111 int single_glyph_width = it->pixel_width / it->nglyphs;
5112 int new_x;
2311178e 5113
5f5c8ee5
GM
5114 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5115 {
5116 new_x = x + single_glyph_width;
5117
5118 /* We want to leave anything reaching TO_X to the caller. */
5119 if ((op & MOVE_TO_X) && new_x > to_x)
5120 {
5121 it->current_x = x;
5122 result = MOVE_X_REACHED;
5123 break;
5124 }
5125 else if (/* Lines are continued. */
5126 !it->truncate_lines_p
5127 && (/* And glyph doesn't fit on the line. */
5128 new_x > it->last_visible_x
5129 /* Or it fits exactly and we're on a window
5130 system frame. */
5131 || (new_x == it->last_visible_x
5132 && FRAME_WINDOW_P (it->f))))
5133 {
5134 if (/* IT->hpos == 0 means the very first glyph
5135 doesn't fit on the line, e.g. a wide image. */
5136 it->hpos == 0
5137 || (new_x == it->last_visible_x
5138 && FRAME_WINDOW_P (it->f)))
5139 {
5140 ++it->hpos;
5141 it->current_x = new_x;
5142 if (i == it->nglyphs - 1)
cafafe0b 5143 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5144 }
5145 else
47589c8c
GM
5146 {
5147 it->current_x = x;
5148 it->max_ascent = ascent;
5149 it->max_descent = descent;
5150 }
2311178e 5151
47589c8c
GM
5152 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5153 IT_CHARPOS (*it)));
5f5c8ee5
GM
5154 result = MOVE_LINE_CONTINUED;
5155 break;
5156 }
5157 else if (new_x > it->first_visible_x)
5158 {
5159 /* Glyph is visible. Increment number of glyphs that
5160 would be displayed. */
5161 ++it->hpos;
5162 }
5163 else
5164 {
2311178e 5165 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
5166 area. Nothing to do. */
5167 }
5168 }
5169
5170 if (result != MOVE_UNDEFINED)
5171 break;
5172 }
5173 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5174 {
5175 /* Stop when TO_X specified and reached. This check is
5176 necessary here because of lines consisting of a line end,
5177 only. The line end will not produce any glyphs and we
5178 would never get MOVE_X_REACHED. */
5179 xassert (it->nglyphs == 0);
5180 result = MOVE_X_REACHED;
5181 break;
5182 }
2311178e 5183
5f5c8ee5
GM
5184 /* Is this a line end? If yes, we're done. */
5185 if (ITERATOR_AT_END_OF_LINE_P (it))
5186 {
5187 result = MOVE_NEWLINE_OR_CR;
5188 break;
5189 }
2311178e 5190
5f5c8ee5
GM
5191 /* The current display element has been consumed. Advance
5192 to the next. */
cafafe0b 5193 set_iterator_to_next (it, 1);
2311178e 5194
5f5c8ee5
GM
5195 /* Stop if lines are truncated and IT's current x-position is
5196 past the right edge of the window now. */
5197 if (it->truncate_lines_p
5198 && it->current_x >= it->last_visible_x)
5199 {
5200 result = MOVE_LINE_TRUNCATED;
5201 break;
5202 }
5203 }
5204
5205 /* Restore the iterator settings altered at the beginning of this
5206 function. */
5207 it->glyph_row = saved_glyph_row;
5208 return result;
5209}
5210
5211
9b2bba76
RS
5212/* Move IT forward until it satisfies one or more of the criteria in
5213 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5214
5215 OP is a bit-mask that specifies where to stop, and in particular,
5216 which of those four position arguments makes a difference. See the
5217 description of enum move_operation_enum.
2311178e 5218
5f5c8ee5
GM
5219 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5220 screen line, this function will set IT to the next position >
5221 TO_CHARPOS. */
5222
5223void
5224move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5225 struct it *it;
5226 int to_charpos, to_x, to_y, to_vpos;
5227 int op;
5228{
5229 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5230 int line_height;
47589c8c 5231 int reached = 0;
5f5c8ee5 5232
47589c8c 5233 for (;;)
5f5c8ee5
GM
5234 {
5235 if (op & MOVE_TO_VPOS)
5236 {
5237 /* If no TO_CHARPOS and no TO_X specified, stop at the
5238 start of the line TO_VPOS. */
5239 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5240 {
5241 if (it->vpos == to_vpos)
47589c8c
GM
5242 {
5243 reached = 1;
5244 break;
5245 }
5246 else
5247 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5248 }
5249 else
5250 {
5251 /* TO_VPOS >= 0 means stop at TO_X in the line at
5252 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5253 if (it->vpos == to_vpos)
5254 {
5255 reached = 2;
5256 break;
5257 }
2311178e 5258
5f5c8ee5
GM
5259 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5260
5261 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5262 {
5263 reached = 3;
5264 break;
5265 }
5f5c8ee5
GM
5266 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5267 {
5268 /* We have reached TO_X but not in the line we want. */
5269 skip = move_it_in_display_line_to (it, to_charpos,
5270 -1, MOVE_TO_POS);
5271 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5272 {
5273 reached = 4;
5274 break;
5275 }
5f5c8ee5
GM
5276 }
5277 }
5278 }
5279 else if (op & MOVE_TO_Y)
5280 {
5281 struct it it_backup;
2311178e 5282
5f5c8ee5
GM
5283 /* TO_Y specified means stop at TO_X in the line containing
5284 TO_Y---or at TO_CHARPOS if this is reached first. The
5285 problem is that we can't really tell whether the line
5286 contains TO_Y before we have completely scanned it, and
5287 this may skip past TO_X. What we do is to first scan to
5288 TO_X.
5289
5290 If TO_X is not specified, use a TO_X of zero. The reason
5291 is to make the outcome of this function more predictable.
5292 If we didn't use TO_X == 0, we would stop at the end of
5293 the line which is probably not what a caller would expect
5294 to happen. */
5295 skip = move_it_in_display_line_to (it, to_charpos,
5296 ((op & MOVE_TO_X)
5297 ? to_x : 0),
5298 (MOVE_TO_X
5299 | (op & MOVE_TO_POS)));
5300
5301 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5302 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5303 {
5304 reached = 5;
5305 break;
5306 }
2311178e 5307
5f5c8ee5
GM
5308 /* If TO_X was reached, we would like to know whether TO_Y
5309 is in the line. This can only be said if we know the
5310 total line height which requires us to scan the rest of
5311 the line. */
5f5c8ee5
GM
5312 if (skip == MOVE_X_REACHED)
5313 {
5314 it_backup = *it;
47589c8c 5315 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5316 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5317 op & MOVE_TO_POS);
47589c8c 5318 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5319 }
5320
5321 /* Now, decide whether TO_Y is in this line. */
5322 line_height = it->max_ascent + it->max_descent;
47589c8c 5323 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 5324
5f5c8ee5
GM
5325 if (to_y >= it->current_y
5326 && to_y < it->current_y + line_height)
5327 {
5328 if (skip == MOVE_X_REACHED)
5329 /* If TO_Y is in this line and TO_X was reached above,
5330 we scanned too far. We have to restore IT's settings
5331 to the ones before skipping. */
5332 *it = it_backup;
47589c8c 5333 reached = 6;
5f5c8ee5
GM
5334 }
5335 else if (skip == MOVE_X_REACHED)
5336 {
5337 skip = skip2;
5338 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5339 reached = 7;
5f5c8ee5
GM
5340 }
5341
47589c8c 5342 if (reached)
5f5c8ee5
GM
5343 break;
5344 }
5345 else
5346 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5347
5348 switch (skip)
5349 {
5350 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5351 reached = 8;
5352 goto out;
5f5c8ee5
GM
5353
5354 case MOVE_NEWLINE_OR_CR:
cafafe0b 5355 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5356 it->continuation_lines_width = 0;
5357 break;
5358
5359 case MOVE_LINE_TRUNCATED:
5360 it->continuation_lines_width = 0;
312246d1 5361 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5362 if ((op & MOVE_TO_POS) != 0
5363 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5364 {
5365 reached = 9;
5366 goto out;
5367 }
5f5c8ee5
GM
5368 break;
5369
5370 case MOVE_LINE_CONTINUED:
5371 it->continuation_lines_width += it->current_x;
5372 break;
5373
5374 default:
5375 abort ();
5376 }
5377
5378 /* Reset/increment for the next run. */
5379 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5380 it->current_x = it->hpos = 0;
5381 it->current_y += it->max_ascent + it->max_descent;
5382 ++it->vpos;
5383 last_height = it->max_ascent + it->max_descent;
5384 last_max_ascent = it->max_ascent;
5385 it->max_ascent = it->max_descent = 0;
5386 }
2311178e 5387
47589c8c
GM
5388 out:
5389
5390 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5391}
5392
5393
5394/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5395
5396 If DY > 0, move IT backward at least that many pixels. DY = 0
5397 means move IT backward to the preceding line start or BEGV. This
5398 function may move over more than DY pixels if IT->current_y - DY
5399 ends up in the middle of a line; in this case IT->current_y will be
5400 set to the top of the line moved to. */
5401
5402void
5403move_it_vertically_backward (it, dy)
5404 struct it *it;
5405 int dy;
5406{
79ddf6f7
GM
5407 int nlines, h;
5408 struct it it2, it3;
5f5c8ee5 5409 int start_pos = IT_CHARPOS (*it);
2311178e 5410
5f5c8ee5
GM
5411 xassert (dy >= 0);
5412
5413 /* Estimate how many newlines we must move back. */
5414 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5415
5416 /* Set the iterator's position that many lines back. */
5417 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5418 back_to_previous_visible_line_start (it);
5419
5420 /* Reseat the iterator here. When moving backward, we don't want
5421 reseat to skip forward over invisible text, set up the iterator
5422 to deliver from overlay strings at the new position etc. So,
5423 use reseat_1 here. */
5424 reseat_1 (it, it->current.pos, 1);
5425
5426 /* We are now surely at a line start. */
5427 it->current_x = it->hpos = 0;
0e47bbf7 5428 it->continuation_lines_width = 0;
5f5c8ee5
GM
5429
5430 /* Move forward and see what y-distance we moved. First move to the
5431 start of the next line so that we get its height. We need this
5432 height to be able to tell whether we reached the specified
5433 y-distance. */
5434 it2 = *it;
5435 it2.max_ascent = it2.max_descent = 0;
5436 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5437 MOVE_TO_POS | MOVE_TO_VPOS);
5438 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 5439 it3 = it2;
2311178e 5440
5f5c8ee5
GM
5441 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5442 xassert (IT_CHARPOS (*it) >= BEGV);
5443 h = it2.current_y - it->current_y;
5444 nlines = it2.vpos - it->vpos;
5445
5446 /* Correct IT's y and vpos position. */
5447 it->vpos -= nlines;
5448 it->current_y -= h;
2311178e 5449
5f5c8ee5
GM
5450 if (dy == 0)
5451 {
5452 /* DY == 0 means move to the start of the screen line. The
5453 value of nlines is > 0 if continuation lines were involved. */
5454 if (nlines > 0)
5455 move_it_by_lines (it, nlines, 1);
5456 xassert (IT_CHARPOS (*it) <= start_pos);
5457 }
5458 else if (nlines)
5459 {
5460 /* The y-position we try to reach. Note that h has been
5461 subtracted in front of the if-statement. */
5462 int target_y = it->current_y + h - dy;
79ddf6f7
GM
5463 int y0 = it3.current_y;
5464 int y1 = line_bottom_y (&it3);
5465 int line_height = y1 - y0;
f7ccfc8c 5466
5f5c8ee5
GM
5467 /* If we did not reach target_y, try to move further backward if
5468 we can. If we moved too far backward, try to move forward. */
5469 if (target_y < it->current_y
79ddf6f7
GM
5470 /* This is heuristic. In a window that's 3 lines high, with
5471 a line height of 13 pixels each, recentering with point
5472 on the bottom line will try to move -39/2 = 19 pixels
5473 backward. Try to avoid moving into the first line. */
798dbe1f 5474 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
5475 && IT_CHARPOS (*it) > BEGV)
5476 {
f7ccfc8c
GM
5477 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5478 target_y - it->current_y));
5f5c8ee5
GM
5479 move_it_vertically (it, target_y - it->current_y);
5480 xassert (IT_CHARPOS (*it) >= BEGV);
5481 }
5482 else if (target_y >= it->current_y + line_height
5483 && IT_CHARPOS (*it) < ZV)
5484 {
55591976 5485 /* Should move forward by at least one line, maybe more.
2311178e 5486
55591976
GM
5487 Note: Calling move_it_by_lines can be expensive on
5488 terminal frames, where compute_motion is used (via
5489 vmotion) to do the job, when there are very long lines
5490 and truncate-lines is nil. That's the reason for
5491 treating terminal frames specially here. */
2311178e 5492
55591976
GM
5493 if (!FRAME_WINDOW_P (it->f))
5494 move_it_vertically (it, target_y - (it->current_y + line_height));
5495 else
f7ccfc8c 5496 {
55591976
GM
5497 do
5498 {
5499 move_it_by_lines (it, 1, 1);
5500 }
5501 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 5502 }
f7ccfc8c 5503
5f5c8ee5
GM
5504 xassert (IT_CHARPOS (*it) >= BEGV);
5505 }
5506 }
5507}
5508
5509
5510/* Move IT by a specified amount of pixel lines DY. DY negative means
5511 move backwards. DY = 0 means move to start of screen line. At the
5512 end, IT will be on the start of a screen line. */
5513
2311178e 5514void
5f5c8ee5
GM
5515move_it_vertically (it, dy)
5516 struct it *it;
5517 int dy;
5518{
5519 if (dy <= 0)
5520 move_it_vertically_backward (it, -dy);
5521 else if (dy > 0)
5522 {
47589c8c 5523 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5524 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5525 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5526 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5527
5528 /* If buffer ends in ZV without a newline, move to the start of
5529 the line to satisfy the post-condition. */
5530 if (IT_CHARPOS (*it) == ZV
5531 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5532 move_it_by_lines (it, 0, 0);
5533 }
5534}
5535
5536
47fc2c10
GM
5537/* Move iterator IT past the end of the text line it is in. */
5538
5539void
5540move_it_past_eol (it)
5541 struct it *it;
5542{
5543 enum move_it_result rc;
2311178e 5544
47fc2c10
GM
5545 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5546 if (rc == MOVE_NEWLINE_OR_CR)
5547 set_iterator_to_next (it, 0);
5548}
5549
5550
2c79b732
GM
5551#if 0 /* Currently not used. */
5552
5f5c8ee5
GM
5553/* Return non-zero if some text between buffer positions START_CHARPOS
5554 and END_CHARPOS is invisible. IT->window is the window for text
5555 property lookup. */
5556
5557static int
5558invisible_text_between_p (it, start_charpos, end_charpos)
5559 struct it *it;
5560 int start_charpos, end_charpos;
5561{
5f5c8ee5
GM
5562 Lisp_Object prop, limit;
5563 int invisible_found_p;
2311178e 5564
5f5c8ee5
GM
5565 xassert (it != NULL && start_charpos <= end_charpos);
5566
5567 /* Is text at START invisible? */
5568 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5569 it->window);
5570 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5571 invisible_found_p = 1;
5572 else
5573 {
016b5642
MB
5574 limit = Fnext_single_char_property_change (make_number (start_charpos),
5575 Qinvisible, Qnil,
5576 make_number (end_charpos));
5f5c8ee5
GM
5577 invisible_found_p = XFASTINT (limit) < end_charpos;
5578 }
5579
5580 return invisible_found_p;
5f5c8ee5
GM
5581}
5582
2c79b732
GM
5583#endif /* 0 */
5584
5f5c8ee5
GM
5585
5586/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5587 negative means move up. DVPOS == 0 means move to the start of the
5588 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5589 NEED_Y_P is zero, IT->current_y will be left unchanged.
5590
5591 Further optimization ideas: If we would know that IT->f doesn't use
5592 a face with proportional font, we could be faster for
5593 truncate-lines nil. */
5594
5595void
5596move_it_by_lines (it, dvpos, need_y_p)
5597 struct it *it;
5598 int dvpos, need_y_p;
5599{
5600 struct position pos;
2311178e 5601
5f5c8ee5
GM
5602 if (!FRAME_WINDOW_P (it->f))
5603 {
5604 struct text_pos textpos;
2311178e 5605
5f5c8ee5
GM
5606 /* We can use vmotion on frames without proportional fonts. */
5607 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5608 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5609 reseat (it, textpos, 1);
5610 it->vpos += pos.vpos;
5611 it->current_y += pos.vpos;
5612 }
5613 else if (dvpos == 0)
5614 {
5615 /* DVPOS == 0 means move to the start of the screen line. */
5616 move_it_vertically_backward (it, 0);
5617 xassert (it->current_x == 0 && it->hpos == 0);
5618 }
5619 else if (dvpos > 0)
2c79b732 5620 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5621 else
5622 {
5623 struct it it2;
5624 int start_charpos, i;
2311178e 5625
e8660d73
GM
5626 /* Start at the beginning of the screen line containing IT's
5627 position. */
5628 move_it_vertically_backward (it, 0);
2311178e 5629
5f5c8ee5
GM
5630 /* Go back -DVPOS visible lines and reseat the iterator there. */
5631 start_charpos = IT_CHARPOS (*it);
5632 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5633 back_to_previous_visible_line_start (it);
5634 reseat (it, it->current.pos, 1);
5635 it->current_x = it->hpos = 0;
5636
5637 /* Above call may have moved too far if continuation lines
5638 are involved. Scan forward and see if it did. */
5639 it2 = *it;
5640 it2.vpos = it2.current_y = 0;
5641 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5642 it->vpos -= it2.vpos;
5643 it->current_y -= it2.current_y;
5644 it->current_x = it->hpos = 0;
5645
5646 /* If we moved too far, move IT some lines forward. */
5647 if (it2.vpos > -dvpos)
5648 {
5649 int delta = it2.vpos + dvpos;
5650 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5651 }
5652 }
5653}
5654
3824b1a6
AS
5655/* Return 1 if IT points into the middle of a display vector. */
5656
5657int
5658in_display_vector_p (it)
5659 struct it *it;
5660{
5661 return (it->method == next_element_from_display_vector
5662 && it->current.dpvec_index > 0
5663 && it->dpvec + it->current.dpvec_index != it->dpend);
5664}
5f5c8ee5
GM
5665
5666\f
5667/***********************************************************************
5668 Messages
5669 ***********************************************************************/
5670
5671
937248bc
GM
5672/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5673 to *Messages*. */
5674
5675void
5676add_to_log (format, arg1, arg2)
5677 char *format;
5678 Lisp_Object arg1, arg2;
5679{
5680 Lisp_Object args[3];
5681 Lisp_Object msg, fmt;
5682 char *buffer;
5683 int len;
5684 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5685
ae794295
GM
5686 /* Do nothing if called asynchronously. Inserting text into
5687 a buffer may call after-change-functions and alike and
5688 that would means running Lisp asynchronously. */
5689 if (handling_signal)
5690 return;
5691
937248bc
GM
5692 fmt = msg = Qnil;
5693 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 5694
937248bc
GM
5695 args[0] = fmt = build_string (format);
5696 args[1] = arg1;
5697 args[2] = arg2;
6fc556fd 5698 msg = Fformat (3, args);
937248bc 5699
2051c264 5700 len = SBYTES (msg) + 1;
937248bc 5701 buffer = (char *) alloca (len);
2051c264 5702 bcopy (SDATA (msg), buffer, len);
2311178e 5703
796184bc 5704 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5705 UNGCPRO;
5706}
5707
5708
5f5c8ee5
GM
5709/* Output a newline in the *Messages* buffer if "needs" one. */
5710
5711void
5712message_log_maybe_newline ()
5713{
5714 if (message_log_need_newline)
5715 message_dolog ("", 0, 1, 0);
5716}
5717
5718
1e313f28 5719/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5720 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5721 nonzero, means interpret the contents of M as multibyte. This
5722 function calls low-level routines in order to bypass text property
5723 hooks, etc. which might not be safe to run. */
5724
5725void
1e313f28 5726message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 5727 const char *m;
1e313f28 5728 int nbytes, nlflag, multibyte;
5f5c8ee5 5729{
a67e162b
RS
5730 if (!NILP (Vmemory_full))
5731 return;
5732
5f5c8ee5
GM
5733 if (!NILP (Vmessage_log_max))
5734 {
5735 struct buffer *oldbuf;
5736 Lisp_Object oldpoint, oldbegv, oldzv;
5737 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5738 int point_at_end = 0;
5739 int zv_at_end = 0;
5740 Lisp_Object old_deactivate_mark, tem;
6052529b 5741 struct gcpro gcpro1;
5f5c8ee5
GM
5742
5743 old_deactivate_mark = Vdeactivate_mark;
5744 oldbuf = current_buffer;
6a94510a 5745 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5746 current_buffer->undo_list = Qt;
5747
b14bc55e
RS
5748 oldpoint = message_dolog_marker1;
5749 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5750 oldbegv = message_dolog_marker2;
5751 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5752 oldzv = message_dolog_marker3;
5753 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5754 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
5755
5756 if (PT == Z)
5757 point_at_end = 1;
5758 if (ZV == Z)
5759 zv_at_end = 1;
5760
5761 BEGV = BEG;
5762 BEGV_BYTE = BEG_BYTE;
5763 ZV = Z;
5764 ZV_BYTE = Z_BYTE;
5765 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5766
5767 /* Insert the string--maybe converting multibyte to single byte
5768 or vice versa, so that all the text fits the buffer. */
5769 if (multibyte
5770 && NILP (current_buffer->enable_multibyte_characters))
5771 {
1e313f28 5772 int i, c, char_bytes;
5f5c8ee5 5773 unsigned char work[1];
2311178e 5774
5f5c8ee5
GM
5775 /* Convert a multibyte string to single-byte
5776 for the *Message* buffer. */
6e57ec5e 5777 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 5778 {
1e313f28 5779 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5780 work[0] = (SINGLE_BYTE_CHAR_P (c)
5781 ? c
5782 : multibyte_char_to_unibyte (c, Qnil));
5783 insert_1_both (work, 1, 1, 1, 0, 0);
5784 }
5785 }
5786 else if (! multibyte
5787 && ! NILP (current_buffer->enable_multibyte_characters))
5788 {
1e313f28 5789 int i, c, char_bytes;
5f5c8ee5 5790 unsigned char *msg = (unsigned char *) m;
260a86a0 5791 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5792 /* Convert a single-byte string to multibyte
5793 for the *Message* buffer. */
1e313f28 5794 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5795 {
5796 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5797 char_bytes = CHAR_STRING (c, str);
5798 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5799 }
5800 }
1e313f28
GM
5801 else if (nbytes)
5802 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5803
5804 if (nlflag)
5805 {
5806 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5807 insert_1 ("\n", 1, 1, 0, 0);
5808
5809 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5810 this_bol = PT;
5811 this_bol_byte = PT_BYTE;
5812
b14bc55e
RS
5813 /* See if this line duplicates the previous one.
5814 If so, combine duplicates. */
5f5c8ee5
GM
5815 if (this_bol > BEG)
5816 {
5817 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5818 prev_bol = PT;
5819 prev_bol_byte = PT_BYTE;
5820
5821 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5822 this_bol, this_bol_byte);
5823 if (dup)
5824 {
5825 del_range_both (prev_bol, prev_bol_byte,
5826 this_bol, this_bol_byte, 0);
5827 if (dup > 1)
5828 {
5829 char dupstr[40];
5830 int duplen;
5831
5832 /* If you change this format, don't forget to also
5833 change message_log_check_duplicate. */
5834 sprintf (dupstr, " [%d times]", dup);
5835 duplen = strlen (dupstr);
5836 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5837 insert_1 (dupstr, duplen, 1, 0, 1);
5838 }
5839 }
5840 }
5841
b14bc55e
RS
5842 /* If we have more than the desired maximum number of lines
5843 in the *Messages* buffer now, delete the oldest ones.
5844 This is safe because we don't have undo in this buffer. */
5845
5f5c8ee5
GM
5846 if (NATNUMP (Vmessage_log_max))
5847 {
5848 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5849 -XFASTINT (Vmessage_log_max) - 1, 0);
5850 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5851 }
5852 }
5853 BEGV = XMARKER (oldbegv)->charpos;
5854 BEGV_BYTE = marker_byte_position (oldbegv);
5855
5856 if (zv_at_end)
5857 {
5858 ZV = Z;
5859 ZV_BYTE = Z_BYTE;
5860 }
5861 else
5862 {
5863 ZV = XMARKER (oldzv)->charpos;
5864 ZV_BYTE = marker_byte_position (oldzv);
5865 }
5866
5867 if (point_at_end)
5868 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5869 else
5870 /* We can't do Fgoto_char (oldpoint) because it will run some
5871 Lisp code. */
5872 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5873 XMARKER (oldpoint)->bytepos);
5874
5875 UNGCPRO;
b14bc55e
RS
5876 unchain_marker (oldpoint);
5877 unchain_marker (oldbegv);
5878 unchain_marker (oldzv);
5f5c8ee5
GM
5879
5880 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5881 set_buffer_internal (oldbuf);
5882 if (NILP (tem))
5883 windows_or_buffers_changed = old_windows_or_buffers_changed;
5884 message_log_need_newline = !nlflag;
5885 Vdeactivate_mark = old_deactivate_mark;
5886 }
5887}
5888
5889
5890/* We are at the end of the buffer after just having inserted a newline.
5891 (Note: We depend on the fact we won't be crossing the gap.)
5892 Check to see if the most recent message looks a lot like the previous one.
5893 Return 0 if different, 1 if the new one should just replace it, or a
5894 value N > 1 if we should also append " [N times]". */
5895
5896static int
5897message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5898 int prev_bol, this_bol;
5899 int prev_bol_byte, this_bol_byte;
5900{
5901 int i;
5902 int len = Z_BYTE - 1 - this_bol_byte;
5903 int seen_dots = 0;
5904 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5905 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5906
5907 for (i = 0; i < len; i++)
5908 {
509633e3 5909 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5910 seen_dots = 1;
5911 if (p1[i] != p2[i])
5912 return seen_dots;
5913 }
5914 p1 += len;
5915 if (*p1 == '\n')
5916 return 2;
5917 if (*p1++ == ' ' && *p1++ == '[')
5918 {
5919 int n = 0;
5920 while (*p1 >= '0' && *p1 <= '9')
5921 n = n * 10 + *p1++ - '0';
5922 if (strncmp (p1, " times]\n", 8) == 0)
5923 return n+1;
5924 }
5925 return 0;
5926}
5927
5928
1e313f28
GM
5929/* Display an echo area message M with a specified length of NBYTES
5930 bytes. The string may include null characters. If M is 0, clear
5931 out any existing message, and let the mini-buffer text show
5932 through.
5f5c8ee5
GM
5933
5934 The buffer M must continue to exist until after the echo area gets
5935 cleared or some other message gets displayed there. This means do
5936 not pass text that is stored in a Lisp string; do not pass text in
5937 a buffer that was alloca'd. */
5938
5939void
1e313f28 5940message2 (m, nbytes, multibyte)
50f80c2f 5941 const char *m;
1e313f28 5942 int nbytes;
5f5c8ee5
GM
5943 int multibyte;
5944{
5945 /* First flush out any partial line written with print. */
5946 message_log_maybe_newline ();
5947 if (m)
1e313f28
GM
5948 message_dolog (m, nbytes, 1, multibyte);
5949 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5950}
5951
5952
5953/* The non-logging counterpart of message2. */
5954
5955void
1e313f28 5956message2_nolog (m, nbytes, multibyte)
50f80c2f 5957 const char *m;
e3383b6f 5958 int nbytes, multibyte;
5f5c8ee5 5959{
886bd6f2 5960 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5961 message_enable_multibyte = multibyte;
5962
5963 if (noninteractive)
5964 {
5965 if (noninteractive_need_newline)
5966 putc ('\n', stderr);
5967 noninteractive_need_newline = 0;
5968 if (m)
1e313f28 5969 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
5970 if (cursor_in_echo_area == 0)
5971 fprintf (stderr, "\n");
5972 fflush (stderr);
5973 }
5974 /* A null message buffer means that the frame hasn't really been
5975 initialized yet. Error messages get reported properly by
5976 cmd_error, so this must be just an informative message; toss it. */
2311178e 5977 else if (INTERACTIVE
886bd6f2
GM
5978 && sf->glyphs_initialized_p
5979 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5980 {
5981 Lisp_Object mini_window;
5982 struct frame *f;
5983
5984 /* Get the frame containing the mini-buffer
5985 that the selected frame is using. */
886bd6f2 5986 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5987 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5988
5989 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5990 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5991 && ! FRAME_VISIBLE_P (f))
5992 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5993
5994 if (m)
5995 {
1e313f28 5996 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5997 if (minibuffer_auto_raise)
5998 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5999 }
6000 else
c6e89d6c 6001 clear_message (1, 1);
5f5c8ee5 6002
c6e89d6c 6003 do_pending_window_change (0);
5f5c8ee5 6004 echo_area_display (1);
c6e89d6c 6005 do_pending_window_change (0);
5f5c8ee5
GM
6006 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6007 (*frame_up_to_date_hook) (f);
6008 }
6009}
6010
6011
c6e89d6c
GM
6012/* Display an echo area message M with a specified length of NBYTES
6013 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
6014 string, clear out any existing message, and let the mini-buffer
6015 text show through. */
6016
6017void
c6e89d6c 6018message3 (m, nbytes, multibyte)
5f5c8ee5 6019 Lisp_Object m;
c6e89d6c 6020 int nbytes;
5f5c8ee5
GM
6021 int multibyte;
6022{
6023 struct gcpro gcpro1;
6024
6025 GCPRO1 (m);
2311178e 6026
5f5c8ee5
GM
6027 /* First flush out any partial line written with print. */
6028 message_log_maybe_newline ();
6029 if (STRINGP (m))
2051c264 6030 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 6031 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6032
6033 UNGCPRO;
6034}
6035
6036
6037/* The non-logging version of message3. */
6038
6039void
c6e89d6c 6040message3_nolog (m, nbytes, multibyte)
5f5c8ee5 6041 Lisp_Object m;
c6e89d6c 6042 int nbytes, multibyte;
5f5c8ee5 6043{
886bd6f2 6044 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6045 message_enable_multibyte = multibyte;
6046
6047 if (noninteractive)
6048 {
6049 if (noninteractive_need_newline)
6050 putc ('\n', stderr);
6051 noninteractive_need_newline = 0;
6052 if (STRINGP (m))
2051c264 6053 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
6054 if (cursor_in_echo_area == 0)
6055 fprintf (stderr, "\n");
6056 fflush (stderr);
6057 }
6058 /* A null message buffer means that the frame hasn't really been
6059 initialized yet. Error messages get reported properly by
6060 cmd_error, so this must be just an informative message; toss it. */
2311178e 6061 else if (INTERACTIVE
886bd6f2
GM
6062 && sf->glyphs_initialized_p
6063 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6064 {
6065 Lisp_Object mini_window;
c6e89d6c 6066 Lisp_Object frame;
5f5c8ee5
GM
6067 struct frame *f;
6068
6069 /* Get the frame containing the mini-buffer
6070 that the selected frame is using. */
886bd6f2 6071 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6072 frame = XWINDOW (mini_window)->frame;
6073 f = XFRAME (frame);
5f5c8ee5
GM
6074
6075 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6076 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6077 && !FRAME_VISIBLE_P (f))
6078 Fmake_frame_visible (frame);
5f5c8ee5 6079
2051c264 6080 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6081 {
c6e89d6c 6082 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6083 if (minibuffer_auto_raise)
6084 Fraise_frame (frame);
5f5c8ee5
GM
6085 }
6086 else
c6e89d6c 6087 clear_message (1, 1);
5f5c8ee5 6088
c6e89d6c 6089 do_pending_window_change (0);
5f5c8ee5 6090 echo_area_display (1);
c6e89d6c 6091 do_pending_window_change (0);
5f5c8ee5
GM
6092 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6093 (*frame_up_to_date_hook) (f);
6094 }
6095}
6096
6097
6098/* Display a null-terminated echo area message M. If M is 0, clear
6099 out any existing message, and let the mini-buffer text show through.
6100
6101 The buffer M must continue to exist until after the echo area gets
6102 cleared or some other message gets displayed there. Do not pass
6103 text that is stored in a Lisp string. Do not pass text in a buffer
6104 that was alloca'd. */
6105
6106void
6107message1 (m)
6108 char *m;
6109{
6110 message2 (m, (m ? strlen (m) : 0), 0);
6111}
6112
6113
6114/* The non-logging counterpart of message1. */
6115
6116void
6117message1_nolog (m)
6118 char *m;
6119{
6120 message2_nolog (m, (m ? strlen (m) : 0), 0);
6121}
6122
6123/* Display a message M which contains a single %s
6124 which gets replaced with STRING. */
6125
6126void
6127message_with_string (m, string, log)
6128 char *m;
6129 Lisp_Object string;
6130 int log;
6131{
5b6d51b6
RS
6132 CHECK_STRING (string);
6133
5f5c8ee5
GM
6134 if (noninteractive)
6135 {
6136 if (m)
6137 {
6138 if (noninteractive_need_newline)
6139 putc ('\n', stderr);
6140 noninteractive_need_newline = 0;
2051c264 6141 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6142 if (cursor_in_echo_area == 0)
6143 fprintf (stderr, "\n");
6144 fflush (stderr);
6145 }
6146 }
6147 else if (INTERACTIVE)
6148 {
6149 /* The frame whose minibuffer we're going to display the message on.
6150 It may be larger than the selected frame, so we need
6151 to use its buffer, not the selected frame's buffer. */
6152 Lisp_Object mini_window;
886bd6f2 6153 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6154
6155 /* Get the frame containing the minibuffer
6156 that the selected frame is using. */
886bd6f2 6157 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6158 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6159
6160 /* A null message buffer means that the frame hasn't really been
6161 initialized yet. Error messages get reported properly by
6162 cmd_error, so this must be just an informative message; toss it. */
6163 if (FRAME_MESSAGE_BUF (f))
6164 {
eb484132
GM
6165 Lisp_Object args[2], message;
6166 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6167
eb484132
GM
6168 args[0] = build_string (m);
6169 args[1] = message = string;
78e17433 6170 GCPRO2 (args[0], message);
eb484132 6171 gcpro1.nvars = 2;
2311178e 6172
eb484132 6173 message = Fformat (2, args);
5f5c8ee5
GM
6174
6175 if (log)
d5db4077 6176 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 6177 else
d5db4077 6178 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
6179
6180 UNGCPRO;
5f5c8ee5
GM
6181
6182 /* Print should start at the beginning of the message
6183 buffer next time. */
6184 message_buf_print = 0;
6185 }
6186 }
6187}
6188
6189
5f5c8ee5
GM
6190/* Dump an informative message to the minibuf. If M is 0, clear out
6191 any existing message, and let the mini-buffer text show through. */
6192
6193/* VARARGS 1 */
6194void
6195message (m, a1, a2, a3)
6196 char *m;
6197 EMACS_INT a1, a2, a3;
6198{
6199 if (noninteractive)
6200 {
6201 if (m)
6202 {
6203 if (noninteractive_need_newline)
6204 putc ('\n', stderr);
6205 noninteractive_need_newline = 0;
6206 fprintf (stderr, m, a1, a2, a3);
6207 if (cursor_in_echo_area == 0)
6208 fprintf (stderr, "\n");
6209 fflush (stderr);
6210 }
6211 }
6212 else if (INTERACTIVE)
6213 {
6214 /* The frame whose mini-buffer we're going to display the message
6215 on. It may be larger than the selected frame, so we need to
6216 use its buffer, not the selected frame's buffer. */
6217 Lisp_Object mini_window;
886bd6f2 6218 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6219
6220 /* Get the frame containing the mini-buffer
6221 that the selected frame is using. */
886bd6f2 6222 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6223 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6224
6225 /* A null message buffer means that the frame hasn't really been
6226 initialized yet. Error messages get reported properly by
6227 cmd_error, so this must be just an informative message; toss
6228 it. */
6229 if (FRAME_MESSAGE_BUF (f))
6230 {
6231 if (m)
6232 {
6233 int len;
6234#ifdef NO_ARG_ARRAY
6235 char *a[3];
6236 a[0] = (char *) a1;
6237 a[1] = (char *) a2;
6238 a[2] = (char *) a3;
6239
6240 len = doprnt (FRAME_MESSAGE_BUF (f),
6241 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6242#else
6243 len = doprnt (FRAME_MESSAGE_BUF (f),
6244 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6245 (char **) &a1);
6246#endif /* NO_ARG_ARRAY */
6247
6248 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6249 }
6250 else
6251 message1 (0);
6252
6253 /* Print should start at the beginning of the message
6254 buffer next time. */
6255 message_buf_print = 0;
6256 }
6257 }
6258}
6259
6260
6261/* The non-logging version of message. */
6262
6263void
6264message_nolog (m, a1, a2, a3)
6265 char *m;
6266 EMACS_INT a1, a2, a3;
6267{
6268 Lisp_Object old_log_max;
6269 old_log_max = Vmessage_log_max;
6270 Vmessage_log_max = Qnil;
6271 message (m, a1, a2, a3);
6272 Vmessage_log_max = old_log_max;
6273}
6274
6275
c6e89d6c
GM
6276/* Display the current message in the current mini-buffer. This is
6277 only called from error handlers in process.c, and is not time
6278 critical. */
5f5c8ee5
GM
6279
6280void
6281update_echo_area ()
6282{
c6e89d6c
GM
6283 if (!NILP (echo_area_buffer[0]))
6284 {
6285 Lisp_Object string;
6286 string = Fcurrent_message ();
2311178e 6287 message3 (string, SBYTES (string),
c6e89d6c
GM
6288 !NILP (current_buffer->enable_multibyte_characters));
6289 }
6290}
6291
6292
a67e162b
RS
6293/* Make sure echo area buffers in `echo_buffers' are live.
6294 If they aren't, make new ones. */
5bcfeb49
GM
6295
6296static void
6297ensure_echo_area_buffers ()
6298{
6299 int i;
6300
6301 for (i = 0; i < 2; ++i)
6302 if (!BUFFERP (echo_buffer[i])
6303 || NILP (XBUFFER (echo_buffer[i])->name))
6304 {
6305 char name[30];
ff3d9573
GM
6306 Lisp_Object old_buffer;
6307 int j;
6308
6309 old_buffer = echo_buffer[i];
5bcfeb49
GM
6310 sprintf (name, " *Echo Area %d*", i);
6311 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6312 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6313
6314 for (j = 0; j < 2; ++j)
6315 if (EQ (old_buffer, echo_area_buffer[j]))
6316 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6317 }
6318}
6319
6320
23a96c77 6321/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6322 echo_area_buffer as current buffer.
6323
6324 WHICH zero means use the current message buffer
6325 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6326 from echo_buffer[] and clear it.
6327
6328 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6329 suitable buffer from echo_buffer[] and clear it.
6330
6331 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6332 that the current message becomes the last displayed one, make
6333 choose a suitable buffer for echo_area_buffer[0], and clear it.
6334
4b41cebb 6335 Value is what FN returns. */
c6e89d6c
GM
6336
6337static int
23a96c77 6338with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6339 struct window *w;
6340 int which;
23dd2d97
KR
6341 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6342 EMACS_INT a1;
6343 Lisp_Object a2;
6344 EMACS_INT a3, a4;
c6e89d6c
GM
6345{
6346 Lisp_Object buffer;
15e26c76 6347 int this_one, the_other, clear_buffer_p, rc;
331379bf 6348 int count = SPECPDL_INDEX ();
c6e89d6c 6349
9583b2bb 6350 /* If buffers aren't live, make new ones. */
5bcfeb49 6351 ensure_echo_area_buffers ();
c6e89d6c
GM
6352
6353 clear_buffer_p = 0;
2311178e 6354
c6e89d6c
GM
6355 if (which == 0)
6356 this_one = 0, the_other = 1;
6357 else if (which > 0)
6358 this_one = 1, the_other = 0;
5f5c8ee5 6359 else
c6e89d6c
GM
6360 {
6361 this_one = 0, the_other = 1;
6362 clear_buffer_p = 1;
2311178e 6363
c6e89d6c
GM
6364 /* We need a fresh one in case the current echo buffer equals
6365 the one containing the last displayed echo area message. */
6366 if (!NILP (echo_area_buffer[this_one])
6367 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6368 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6369 }
6370
6371 /* Choose a suitable buffer from echo_buffer[] is we don't
6372 have one. */
6373 if (NILP (echo_area_buffer[this_one]))
6374 {
6375 echo_area_buffer[this_one]
6376 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6377 ? echo_buffer[the_other]
6378 : echo_buffer[this_one]);
6379 clear_buffer_p = 1;
6380 }
6381
6382 buffer = echo_area_buffer[this_one];
6383
1013f4e3
GM
6384 /* Don't get confused by reusing the buffer used for echoing
6385 for a different purpose. */
032906b1 6386 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6387 cancel_echoing ();
6388
c6e89d6c
GM
6389 record_unwind_protect (unwind_with_echo_area_buffer,
6390 with_echo_area_buffer_unwind_data (w));
6391
6392 /* Make the echo area buffer current. Note that for display
6393 purposes, it is not necessary that the displayed window's buffer
6394 == current_buffer, except for text property lookup. So, let's
6395 only set that buffer temporarily here without doing a full
6396 Fset_window_buffer. We must also change w->pointm, though,
6397 because otherwise an assertions in unshow_buffer fails, and Emacs
6398 aborts. */
9142dd5b 6399 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6400 if (w)
6401 {
6402 w->buffer = buffer;
6403 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6404 }
ad4f174e 6405
c6e89d6c
GM
6406 current_buffer->undo_list = Qt;
6407 current_buffer->read_only = Qnil;
bbbf6d06 6408 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6409 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6410
6411 if (clear_buffer_p && Z > BEG)
6412 del_range (BEG, Z);
6413
6414 xassert (BEGV >= BEG);
6415 xassert (ZV <= Z && ZV >= BEGV);
6416
23a96c77 6417 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6418
6419 xassert (BEGV >= BEG);
6420 xassert (ZV <= Z && ZV >= BEGV);
6421
6422 unbind_to (count, Qnil);
6423 return rc;
5f5c8ee5
GM
6424}
6425
6426
c6e89d6c
GM
6427/* Save state that should be preserved around the call to the function
6428 FN called in with_echo_area_buffer. */
5f5c8ee5 6429
c6e89d6c
GM
6430static Lisp_Object
6431with_echo_area_buffer_unwind_data (w)
6432 struct window *w;
5f5c8ee5 6433{
c6e89d6c
GM
6434 int i = 0;
6435 Lisp_Object vector;
5f5c8ee5 6436
c6e89d6c
GM
6437 /* Reduce consing by keeping one vector in
6438 Vwith_echo_area_save_vector. */
6439 vector = Vwith_echo_area_save_vector;
6440 Vwith_echo_area_save_vector = Qnil;
2311178e 6441
c6e89d6c 6442 if (NILP (vector))
9142dd5b 6443 vector = Fmake_vector (make_number (7), Qnil);
2311178e 6444
a61b7058
GM
6445 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6446 AREF (vector, i) = Vdeactivate_mark, ++i;
6447 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 6448
c6e89d6c
GM
6449 if (w)
6450 {
a61b7058
GM
6451 XSETWINDOW (AREF (vector, i), w); ++i;
6452 AREF (vector, i) = w->buffer; ++i;
6453 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6454 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6455 }
6456 else
6457 {
6458 int end = i + 4;
a61b7058
GM
6459 for (; i < end; ++i)
6460 AREF (vector, i) = Qnil;
c6e89d6c 6461 }
5f5c8ee5 6462
a61b7058 6463 xassert (i == ASIZE (vector));
c6e89d6c
GM
6464 return vector;
6465}
5f5c8ee5 6466
5f5c8ee5 6467
c6e89d6c
GM
6468/* Restore global state from VECTOR which was created by
6469 with_echo_area_buffer_unwind_data. */
6470
6471static Lisp_Object
6472unwind_with_echo_area_buffer (vector)
6473 Lisp_Object vector;
6474{
bbbf6d06
GM
6475 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6476 Vdeactivate_mark = AREF (vector, 1);
6477 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6478
bbbf6d06 6479 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6480 {
6481 struct window *w;
6482 Lisp_Object buffer, charpos, bytepos;
2311178e 6483
bbbf6d06
GM
6484 w = XWINDOW (AREF (vector, 3));
6485 buffer = AREF (vector, 4);
6486 charpos = AREF (vector, 5);
6487 bytepos = AREF (vector, 6);
2311178e 6488
c6e89d6c
GM
6489 w->buffer = buffer;
6490 set_marker_both (w->pointm, buffer,
6491 XFASTINT (charpos), XFASTINT (bytepos));
6492 }
6493
6494 Vwith_echo_area_save_vector = vector;
6495 return Qnil;
6496}
6497
6498
6499/* Set up the echo area for use by print functions. MULTIBYTE_P
6500 non-zero means we will print multibyte. */
6501
6502void
6503setup_echo_area_for_printing (multibyte_p)
6504 int multibyte_p;
6505{
03b0a4b4
RS
6506 /* If we can't find an echo area any more, exit. */
6507 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6508 Fkill_emacs (Qnil);
6509
5bcfeb49
GM
6510 ensure_echo_area_buffers ();
6511
c6e89d6c
GM
6512 if (!message_buf_print)
6513 {
6514 /* A message has been output since the last time we printed.
6515 Choose a fresh echo area buffer. */
6516 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6517 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
6518 else
6519 echo_area_buffer[0] = echo_buffer[0];
6520
6521 /* Switch to that buffer and clear it. */
6522 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6523 current_buffer->truncate_lines = Qnil;
2311178e 6524
c6e89d6c 6525 if (Z > BEG)
bbbf6d06 6526 {
331379bf 6527 int count = SPECPDL_INDEX ();
bbbf6d06 6528 specbind (Qinhibit_read_only, Qt);
a67e162b 6529 /* Note that undo recording is always disabled. */
bbbf6d06
GM
6530 del_range (BEG, Z);
6531 unbind_to (count, Qnil);
6532 }
c6e89d6c
GM
6533 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6534
6535 /* Set up the buffer for the multibyteness we need. */
6536 if (multibyte_p
6537 != !NILP (current_buffer->enable_multibyte_characters))
6538 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6539
6540 /* Raise the frame containing the echo area. */
6541 if (minibuffer_auto_raise)
6542 {
886bd6f2 6543 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6544 Lisp_Object mini_window;
886bd6f2 6545 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6546 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6547 }
6548
8a4e3c0c 6549 message_log_maybe_newline ();
c6e89d6c
GM
6550 message_buf_print = 1;
6551 }
fa77249f
GM
6552 else
6553 {
6554 if (NILP (echo_area_buffer[0]))
6555 {
6556 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6557 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
6558 else
6559 echo_area_buffer[0] = echo_buffer[0];
6560 }
2311178e 6561
fa77249f 6562 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
6563 {
6564 /* Someone switched buffers between print requests. */
6565 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6566 current_buffer->truncate_lines = Qnil;
6567 }
fa77249f 6568 }
c6e89d6c
GM
6569}
6570
6571
dd2eb166
GM
6572/* Display an echo area message in window W. Value is non-zero if W's
6573 height is changed. If display_last_displayed_message_p is
6574 non-zero, display the message that was last displayed, otherwise
6575 display the current message. */
c6e89d6c
GM
6576
6577static int
6578display_echo_area (w)
6579 struct window *w;
6580{
25edb08f
GM
6581 int i, no_message_p, window_height_changed_p, count;
6582
6583 /* Temporarily disable garbage collections while displaying the echo
6584 area. This is done because a GC can print a message itself.
6585 That message would modify the echo area buffer's contents while a
6586 redisplay of the buffer is going on, and seriously confuse
6587 redisplay. */
6588 count = inhibit_garbage_collection ();
dd2eb166
GM
6589
6590 /* If there is no message, we must call display_echo_area_1
6591 nevertheless because it resizes the window. But we will have to
6592 reset the echo_area_buffer in question to nil at the end because
6593 with_echo_area_buffer will sets it to an empty buffer. */
6594 i = display_last_displayed_message_p ? 1 : 0;
6595 no_message_p = NILP (echo_area_buffer[i]);
2311178e 6596
dd2eb166
GM
6597 window_height_changed_p
6598 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6599 display_echo_area_1,
23dd2d97 6600 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6601
6602 if (no_message_p)
6603 echo_area_buffer[i] = Qnil;
25edb08f
GM
6604
6605 unbind_to (count, Qnil);
dd2eb166 6606 return window_height_changed_p;
c6e89d6c
GM
6607}
6608
6609
6610/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6611 contains the current echo area message in window W, a mini-window,
6612 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6613 Change the height of W so that all of the message is displayed.
6614 Value is non-zero if height of W was changed. */
6615
6616static int
23a96c77 6617display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6618 EMACS_INT a1;
6619 Lisp_Object a2;
6620 EMACS_INT a3, a4;
c6e89d6c 6621{
23a96c77 6622 struct window *w = (struct window *) a1;
c6e89d6c 6623 Lisp_Object window;
c6e89d6c
GM
6624 struct text_pos start;
6625 int window_height_changed_p = 0;
6626
6627 /* Do this before displaying, so that we have a large enough glyph
6628 matrix for the display. */
92a90e89 6629 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6630
6631 /* Display. */
6632 clear_glyph_matrix (w->desired_matrix);
6633 XSETWINDOW (window, w);
6634 SET_TEXT_POS (start, BEG, BEG_BYTE);
6635 try_window (window, start);
6636
c6e89d6c
GM
6637 return window_height_changed_p;
6638}
6639
6640
92a90e89 6641/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
6642 currently displayed message, if there is one. If a mini-buffer
6643 is active, don't shrink it. */
92a90e89
GM
6644
6645void
308a74d8 6646resize_echo_area_exactly ()
92a90e89
GM
6647{
6648 if (BUFFERP (echo_area_buffer[0])
6649 && WINDOWP (echo_area_window))
6650 {
6651 struct window *w = XWINDOW (echo_area_window);
6652 int resized_p;
6d004fea
GM
6653 Lisp_Object resize_exactly;
6654
6655 if (minibuf_level == 0)
6656 resize_exactly = Qt;
6657 else
6658 resize_exactly = Qnil;
2311178e 6659
23a96c77 6660 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 6661 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
6662 if (resized_p)
6663 {
6664 ++windows_or_buffers_changed;
6665 ++update_mode_lines;
6666 redisplay_internal (0);
6667 }
6668 }
6669}
6670
6671
23a96c77 6672/* Callback function for with_echo_area_buffer, when used from
308a74d8 6673 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
6674 resize, EXACTLY non-nil means resize the mini-window exactly to the
6675 size of the text displayed. A3 and A4 are not used. Value is what
6676 resize_mini_window returns. */
23a96c77
GM
6677
6678static int
6d004fea 6679resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 6680 EMACS_INT a1;
6d004fea 6681 Lisp_Object exactly;
23dd2d97 6682 EMACS_INT a3, a4;
23a96c77 6683{
6d004fea 6684 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
6685}
6686
6687
92a90e89
GM
6688/* Resize mini-window W to fit the size of its contents. EXACT:P
6689 means size the window exactly to the size needed. Otherwise, it's
6690 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 6691 the window height has been changed. */
c6e89d6c 6692
9472f927 6693int
92a90e89 6694resize_mini_window (w, exact_p)
c6e89d6c 6695 struct window *w;
92a90e89 6696 int exact_p;
c6e89d6c
GM
6697{
6698 struct frame *f = XFRAME (w->frame);
6699 int window_height_changed_p = 0;
6700
6701 xassert (MINI_WINDOW_P (w));
97cafc0f 6702
2913a9c0
GM
6703 /* Don't resize windows while redisplaying a window; it would
6704 confuse redisplay functions when the size of the window they are
6705 displaying changes from under them. Such a resizing can happen,
6706 for instance, when which-func prints a long message while
6707 we are running fontification-functions. We're running these
6708 functions with safe_call which binds inhibit-redisplay to t. */
6709 if (!NILP (Vinhibit_redisplay))
64c5be50 6710 return 0;
2311178e 6711
97cafc0f 6712 /* Nil means don't try to resize. */
6422c1d7 6713 if (NILP (Vresize_mini_windows)
00f6d59e 6714 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6715 return 0;
2311178e 6716
c6e89d6c
GM
6717 if (!FRAME_MINIBUF_ONLY_P (f))
6718 {
6719 struct it it;
dd2eb166
GM
6720 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6721 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6722 int height, max_height;
6723 int unit = CANON_Y_UNIT (f);
6724 struct text_pos start;
1bfdbe43
GM
6725 struct buffer *old_current_buffer = NULL;
6726
6727 if (current_buffer != XBUFFER (w->buffer))
6728 {
6729 old_current_buffer = current_buffer;
6730 set_buffer_internal (XBUFFER (w->buffer));
6731 }
9142dd5b 6732
c6e89d6c 6733 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6734
dd2eb166
GM
6735 /* Compute the max. number of lines specified by the user. */
6736 if (FLOATP (Vmax_mini_window_height))
18f9986a 6737 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
dd2eb166
GM
6738 else if (INTEGERP (Vmax_mini_window_height))
6739 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6740 else
6741 max_height = total_height / 4;
2311178e 6742
4b41cebb 6743 /* Correct that max. height if it's bogus. */
dd2eb166
GM
6744 max_height = max (1, max_height);
6745 max_height = min (total_height, max_height);
2311178e 6746
dd2eb166 6747 /* Find out the height of the text in the window. */
ad4f174e
GM
6748 if (it.truncate_lines_p)
6749 height = 1;
55b064bd 6750 else
ad4f174e
GM
6751 {
6752 last_height = 0;
6753 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6754 if (it.max_ascent == 0 && it.max_descent == 0)
6755 height = it.current_y + last_height;
6756 else
6757 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6758 height -= it.extra_line_spacing;
ad4f174e
GM
6759 height = (height + unit - 1) / unit;
6760 }
2311178e 6761
dd2eb166
GM
6762 /* Compute a suitable window start. */
6763 if (height > max_height)
6764 {
6765 height = max_height;
6766 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6767 move_it_vertically_backward (&it, (height - 1) * unit);
6768 start = it.current.pos;
6769 }
6770 else
6771 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6772 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6773
6422c1d7 6774 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6775 {
6422c1d7
GM
6776 /* Let it grow only, until we display an empty message, in which
6777 case the window shrinks again. */
6778 if (height > XFASTINT (w->height))
6779 {
6780 int old_height = XFASTINT (w->height);
6781 freeze_window_starts (f, 1);
6782 grow_mini_window (w, height - XFASTINT (w->height));
6783 window_height_changed_p = XFASTINT (w->height) != old_height;
6784 }
6785 else if (height < XFASTINT (w->height)
6786 && (exact_p || BEGV == ZV))
6787 {
6788 int old_height = XFASTINT (w->height);
6789 freeze_window_starts (f, 0);
6790 shrink_mini_window (w);
6791 window_height_changed_p = XFASTINT (w->height) != old_height;
6792 }
da448723 6793 }
2311178e 6794 else
da448723 6795 {
6422c1d7
GM
6796 /* Always resize to exact size needed. */
6797 if (height > XFASTINT (w->height))
6798 {
6799 int old_height = XFASTINT (w->height);
6800 freeze_window_starts (f, 1);
6801 grow_mini_window (w, height - XFASTINT (w->height));
6802 window_height_changed_p = XFASTINT (w->height) != old_height;
6803 }
6804 else if (height < XFASTINT (w->height))
6805 {
6806 int old_height = XFASTINT (w->height);
6807 freeze_window_starts (f, 0);
6808 shrink_mini_window (w);
6809
6810 if (height)
6811 {
6812 freeze_window_starts (f, 1);
6813 grow_mini_window (w, height - XFASTINT (w->height));
6814 }
2311178e 6815
6422c1d7
GM
6816 window_height_changed_p = XFASTINT (w->height) != old_height;
6817 }
9142dd5b 6818 }
1bfdbe43
GM
6819
6820 if (old_current_buffer)
6821 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6822 }
6823
6824 return window_height_changed_p;
6825}
6826
6827
6828/* Value is the current message, a string, or nil if there is no
6829 current message. */
6830
6831Lisp_Object
6832current_message ()
6833{
6834 Lisp_Object msg;
6835
6836 if (NILP (echo_area_buffer[0]))
6837 msg = Qnil;
6838 else
6839 {
23a96c77 6840 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6841 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6842 if (NILP (msg))
6843 echo_area_buffer[0] = Qnil;
6844 }
2311178e 6845
c6e89d6c
GM
6846 return msg;
6847}
6848
6849
6850static int
23a96c77 6851current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6852 EMACS_INT a1;
6853 Lisp_Object a2;
6854 EMACS_INT a3, a4;
c6e89d6c 6855{
23a96c77 6856 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 6857
c6e89d6c
GM
6858 if (Z > BEG)
6859 *msg = make_buffer_string (BEG, Z, 1);
6860 else
6861 *msg = Qnil;
6862 return 0;
6863}
6864
6865
6866/* Push the current message on Vmessage_stack for later restauration
6867 by restore_message. Value is non-zero if the current message isn't
6868 empty. This is a relatively infrequent operation, so it's not
6869 worth optimizing. */
6870
6871int
6872push_message ()
6873{
6874 Lisp_Object msg;
6875 msg = current_message ();
6876 Vmessage_stack = Fcons (msg, Vmessage_stack);
6877 return STRINGP (msg);
6878}
6879
6880
6881/* Restore message display from the top of Vmessage_stack. */
6882
6883void
6884restore_message ()
6885{
6886 Lisp_Object msg;
2311178e 6887
c6e89d6c
GM
6888 xassert (CONSP (Vmessage_stack));
6889 msg = XCAR (Vmessage_stack);
6890 if (STRINGP (msg))
d5db4077 6891 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
6892 else
6893 message3_nolog (msg, 0, 0);
6894}
6895
6896
37d66095
RS
6897/* Handler for record_unwind_protect calling pop_message. */
6898
6899Lisp_Object
6900pop_message_unwind (dummy)
6901 Lisp_Object dummy;
6902{
6903 pop_message ();
6904 return Qnil;
6905}
6906
c6e89d6c
GM
6907/* Pop the top-most entry off Vmessage_stack. */
6908
6909void
6910pop_message ()
6911{
6912 xassert (CONSP (Vmessage_stack));
6913 Vmessage_stack = XCDR (Vmessage_stack);
6914}
6915
6916
6917/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6918 exits. If the stack is not empty, we have a missing pop_message
6919 somewhere. */
6920
6921void
6922check_message_stack ()
6923{
6924 if (!NILP (Vmessage_stack))
6925 abort ();
6926}
6927
6928
6929/* Truncate to NCHARS what will be displayed in the echo area the next
6930 time we display it---but don't redisplay it now. */
6931
6932void
6933truncate_echo_area (nchars)
6934 int nchars;
6935{
6936 if (nchars == 0)
6937 echo_area_buffer[0] = Qnil;
6938 /* A null message buffer means that the frame hasn't really been
6939 initialized yet. Error messages get reported properly by
6940 cmd_error, so this must be just an informative message; toss it. */
6941 else if (!noninteractive
6942 && INTERACTIVE
c6e89d6c 6943 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6944 {
6945 struct frame *sf = SELECTED_FRAME ();
6946 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6947 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6948 }
c6e89d6c
GM
6949}
6950
6951
6952/* Helper function for truncate_echo_area. Truncate the current
6953 message to at most NCHARS characters. */
6954
6955static int
23a96c77 6956truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6957 EMACS_INT nchars;
6958 Lisp_Object a2;
6959 EMACS_INT a3, a4;
c6e89d6c
GM
6960{
6961 if (BEG + nchars < Z)
6962 del_range (BEG + nchars, Z);
6963 if (Z == BEG)
6964 echo_area_buffer[0] = Qnil;
6965 return 0;
6966}
6967
6968
6969/* Set the current message to a substring of S or STRING.
6970
6971 If STRING is a Lisp string, set the message to the first NBYTES
6972 bytes from STRING. NBYTES zero means use the whole string. If
6973 STRING is multibyte, the message will be displayed multibyte.
6974
6975 If S is not null, set the message to the first LEN bytes of S. LEN
6976 zero means use the whole string. MULTIBYTE_P non-zero means S is
6977 multibyte. Display the message multibyte in that case. */
6978
6979void
6980set_message (s, string, nbytes, multibyte_p)
50f80c2f 6981 const char *s;
c6e89d6c 6982 Lisp_Object string;
e3383b6f 6983 int nbytes, multibyte_p;
c6e89d6c
GM
6984{
6985 message_enable_multibyte
6986 = ((s && multibyte_p)
6987 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 6988
23a96c77
GM
6989 with_echo_area_buffer (0, -1, set_message_1,
6990 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6991 message_buf_print = 0;
21fdfb65 6992 help_echo_showing_p = 0;
c6e89d6c
GM
6993}
6994
6995
6996/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6997 as there, with A1 corresponding to S and A2 corresponding to STRING
6998 This function is called with the echo area buffer being
c6e89d6c
GM
6999 current. */
7000
7001static int
23a96c77 7002set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
7003 EMACS_INT a1;
7004 Lisp_Object a2;
7005 EMACS_INT nbytes, multibyte_p;
c6e89d6c 7006{
50f80c2f 7007 const char *s = (const char *) a1;
23dd2d97 7008 Lisp_Object string = a2;
2311178e 7009
c6e89d6c 7010 xassert (BEG == Z);
2311178e 7011
c6e89d6c
GM
7012 /* Change multibyteness of the echo buffer appropriately. */
7013 if (message_enable_multibyte
7014 != !NILP (current_buffer->enable_multibyte_characters))
7015 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7016
ad4f174e 7017 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 7018
c6e89d6c
GM
7019 /* Insert new message at BEG. */
7020 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7021
7022 if (STRINGP (string))
7023 {
7024 int nchars;
2311178e 7025
c6e89d6c 7026 if (nbytes == 0)
2051c264 7027 nbytes = SBYTES (string);
c6e89d6c 7028 nchars = string_byte_to_char (string, nbytes);
2311178e 7029
c6e89d6c
GM
7030 /* This function takes care of single/multibyte conversion. We
7031 just have to ensure that the echo area buffer has the right
7032 setting of enable_multibyte_characters. */
7033 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7034 }
7035 else if (s)
7036 {
7037 if (nbytes == 0)
7038 nbytes = strlen (s);
2311178e 7039
c6e89d6c
GM
7040 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7041 {
7042 /* Convert from multi-byte to single-byte. */
7043 int i, c, n;
7044 unsigned char work[1];
2311178e 7045
c6e89d6c
GM
7046 /* Convert a multibyte string to single-byte. */
7047 for (i = 0; i < nbytes; i += n)
7048 {
7049 c = string_char_and_length (s + i, nbytes - i, &n);
7050 work[0] = (SINGLE_BYTE_CHAR_P (c)
7051 ? c
7052 : multibyte_char_to_unibyte (c, Qnil));
7053 insert_1_both (work, 1, 1, 1, 0, 0);
7054 }
7055 }
7056 else if (!multibyte_p
7057 && !NILP (current_buffer->enable_multibyte_characters))
7058 {
7059 /* Convert from single-byte to multi-byte. */
7060 int i, c, n;
50f80c2f 7061 const unsigned char *msg = (const unsigned char *) s;
260a86a0 7062 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 7063
c6e89d6c
GM
7064 /* Convert a single-byte string to multibyte. */
7065 for (i = 0; i < nbytes; i++)
7066 {
7067 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
7068 n = CHAR_STRING (c, str);
7069 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
7070 }
7071 }
7072 else
7073 insert_1 (s, nbytes, 1, 0, 0);
7074 }
7075
7076 return 0;
7077}
7078
7079
7080/* Clear messages. CURRENT_P non-zero means clear the current
7081 message. LAST_DISPLAYED_P non-zero means clear the message
7082 last displayed. */
7083
7084void
7085clear_message (current_p, last_displayed_p)
7086 int current_p, last_displayed_p;
7087{
7088 if (current_p)
6e019995
GM
7089 {
7090 echo_area_buffer[0] = Qnil;
7091 message_cleared_p = 1;
7092 }
2311178e 7093
c6e89d6c
GM
7094 if (last_displayed_p)
7095 echo_area_buffer[1] = Qnil;
2311178e 7096
c6e89d6c
GM
7097 message_buf_print = 0;
7098}
7099
7100/* Clear garbaged frames.
7101
7102 This function is used where the old redisplay called
7103 redraw_garbaged_frames which in turn called redraw_frame which in
7104 turn called clear_frame. The call to clear_frame was a source of
7105 flickering. I believe a clear_frame is not necessary. It should
7106 suffice in the new redisplay to invalidate all current matrices,
7107 and ensure a complete redisplay of all windows. */
7108
7109static void
7110clear_garbaged_frames ()
7111{
5f5c8ee5
GM
7112 if (frame_garbaged)
7113 {
5f5c8ee5 7114 Lisp_Object tail, frame;
5fb96e96 7115 int changed_count = 0;
2311178e 7116
5f5c8ee5
GM
7117 FOR_EACH_FRAME (tail, frame)
7118 {
7119 struct frame *f = XFRAME (frame);
2311178e 7120
5f5c8ee5
GM
7121 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7122 {
6bb95882 7123 if (f->resized_p)
573e57f1 7124 Fredraw_frame (frame);
5f5c8ee5 7125 clear_current_matrices (f);
5fb96e96 7126 changed_count++;
5f5c8ee5 7127 f->garbaged = 0;
6bb95882 7128 f->resized_p = 0;
5f5c8ee5
GM
7129 }
7130 }
7131
7132 frame_garbaged = 0;
5fb96e96
RS
7133 if (changed_count)
7134 ++windows_or_buffers_changed;
5f5c8ee5 7135 }
c6e89d6c 7136}
5f5c8ee5 7137
5f5c8ee5 7138
886bd6f2
GM
7139/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7140 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7141 mini-windows height has been changed. */
5f5c8ee5 7142
c6e89d6c
GM
7143static int
7144echo_area_display (update_frame_p)
7145 int update_frame_p;
7146{
7147 Lisp_Object mini_window;
7148 struct window *w;
7149 struct frame *f;
7150 int window_height_changed_p = 0;
886bd6f2 7151 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7152
886bd6f2 7153 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7154 w = XWINDOW (mini_window);
7155 f = XFRAME (WINDOW_FRAME (w));
7156
7157 /* Don't display if frame is invisible or not yet initialized. */
7158 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7159 return 0;
5f5c8ee5 7160
1a578e9b 7161/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7162#ifndef MAC_OS8
1ab3e082 7163#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7164 /* When Emacs starts, selected_frame may be a visible terminal
7165 frame, even if we run under a window system. If we let this
7166 through, a message would be displayed on the terminal. */
2311178e 7167 if (EQ (selected_frame, Vterminal_frame)
622e3754 7168 && !NILP (Vwindow_system))
c6e89d6c 7169 return 0;
1ab3e082 7170#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7171#endif
c6e89d6c
GM
7172
7173 /* Redraw garbaged frames. */
7174 if (frame_garbaged)
7175 clear_garbaged_frames ();
7176
7177 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7178 {
7179 echo_area_window = mini_window;
7180 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7181 w->must_be_updated_p = 1;
c59c668a 7182
d4358b37
GM
7183 /* Update the display, unless called from redisplay_internal.
7184 Also don't update the screen during redisplay itself. The
7185 update will happen at the end of redisplay, and an update
7186 here could cause confusion. */
7187 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7188 {
715e84c9 7189 int n = 0;
edc68111 7190
715e84c9
GM
7191 /* If the display update has been interrupted by pending
7192 input, update mode lines in the frame. Due to the
7193 pending input, it might have been that redisplay hasn't
7194 been called, so that mode lines above the echo area are
7195 garbaged. This looks odd, so we prevent it here. */
7196 if (!display_completed)
7197 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 7198
8af1308e
GM
7199 if (window_height_changed_p
7200 /* Don't do this if Emacs is shutting down. Redisplay
7201 needs to run hooks. */
7202 && !NILP (Vrun_hooks))
c59c668a 7203 {
c06017fb
GM
7204 /* Must update other windows. Likewise as in other
7205 cases, don't let this update be interrupted by
7206 pending input. */
331379bf 7207 int count = SPECPDL_INDEX ();
c06017fb 7208 specbind (Qredisplay_dont_pause, Qt);
edc68111 7209 windows_or_buffers_changed = 1;
c59c668a 7210 redisplay_internal (0);
c06017fb 7211 unbind_to (count, Qnil);
c59c668a 7212 }
715e84c9 7213 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7214 {
edc68111 7215 /* Window configuration is the same as before.
715e84c9
GM
7216 Can do with a display update of the echo area,
7217 unless we displayed some mode lines. */
5f5c8ee5
GM
7218 update_single_window (w, 1);
7219 rif->flush_display (f);
7220 }
7221 else
7222 update_frame (f, 1, 1);
31b6671b
GM
7223
7224 /* If cursor is in the echo area, make sure that the next
7225 redisplay displays the minibuffer, so that the cursor will
7226 be replaced with what the minibuffer wants. */
7227 if (cursor_in_echo_area)
7228 ++windows_or_buffers_changed;
5f5c8ee5
GM
7229 }
7230 }
7231 else if (!EQ (mini_window, selected_window))
7232 windows_or_buffers_changed++;
c59c668a
GM
7233
7234 /* Last displayed message is now the current message. */
dd2eb166 7235 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 7236
5f5c8ee5
GM
7237 /* Prevent redisplay optimization in redisplay_internal by resetting
7238 this_line_start_pos. This is done because the mini-buffer now
7239 displays the message instead of its buffer text. */
7240 if (EQ (mini_window, selected_window))
7241 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7242
7243 return window_height_changed_p;
5f5c8ee5
GM
7244}
7245
7246
7247\f
7248/***********************************************************************
7249 Frame Titles
7250 ***********************************************************************/
7251
7252
8143e6ab
KS
7253/* The frame title buffering code is also used by Fformat_mode_line.
7254 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
5f5c8ee5
GM
7255
7256/* A buffer for constructing frame titles in it; allocated from the
7257 heap in init_xdisp and resized as needed in store_frame_title_char. */
7258
7259static char *frame_title_buf;
7260
7261/* The buffer's end, and a current output position in it. */
7262
7263static char *frame_title_buf_end;
7264static char *frame_title_ptr;
7265
7266
7267/* Store a single character C for the frame title in frame_title_buf.
7268 Re-allocate frame_title_buf if necessary. */
7269
7270static void
5d15cc8f
DL
7271#ifdef PROTOTYPES
7272store_frame_title_char (char c)
7273#else
5f5c8ee5
GM
7274store_frame_title_char (c)
7275 char c;
5d15cc8f 7276#endif
5f5c8ee5
GM
7277{
7278 /* If output position has reached the end of the allocated buffer,
7279 double the buffer's size. */
7280 if (frame_title_ptr == frame_title_buf_end)
7281 {
7282 int len = frame_title_ptr - frame_title_buf;
7283 int new_size = 2 * len * sizeof *frame_title_buf;
7284 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7285 frame_title_buf_end = frame_title_buf + new_size;
7286 frame_title_ptr = frame_title_buf + len;
7287 }
7288
7289 *frame_title_ptr++ = c;
7290}
7291
7292
7293/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7294 frame_title_ptr. STR is the string to store. Do not copy
7295 characters that yield more columns than PRECISION; PRECISION <= 0
7296 means copy the whole string. Pad with spaces until FIELD_WIDTH
7297 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7298 pad. Called from display_mode_element when it is used to build a
7299 frame title. */
5f5c8ee5
GM
7300
7301static int
7302store_frame_title (str, field_width, precision)
50f80c2f 7303 const unsigned char *str;
5f5c8ee5
GM
7304 int field_width, precision;
7305{
7306 int n = 0;
3b552d56 7307 int dummy, nbytes;
5f5c8ee5
GM
7308
7309 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7310 nbytes = strlen (str);
7311 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7312 while (nbytes--)
7313 store_frame_title_char (*str++);
5f5c8ee5
GM
7314
7315 /* Fill up with spaces until FIELD_WIDTH reached. */
7316 while (field_width > 0
7317 && n < field_width)
7318 {
7319 store_frame_title_char (' ');
7320 ++n;
7321 }
7322
7323 return n;
7324}
7325
8143e6ab 7326#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7327
7328/* Set the title of FRAME, if it has changed. The title format is
7329 Vicon_title_format if FRAME is iconified, otherwise it is
7330 frame_title_format. */
7331
7332static void
7333x_consider_frame_title (frame)
7334 Lisp_Object frame;
7335{
7336 struct frame *f = XFRAME (frame);
7337
7338 if (FRAME_WINDOW_P (f)
7339 || FRAME_MINIBUF_ONLY_P (f)
7340 || f->explicit_name)
7341 {
7342 /* Do we have more than one visible frame on this X display? */
7343 Lisp_Object tail;
7344 Lisp_Object fmt;
7345 struct buffer *obuf;
7346 int len;
7347 struct it it;
7348
9472f927 7349 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7350 {
74779f52
JR
7351 Lisp_Object other_frame = XCAR (tail);
7352 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 7353
2311178e 7354 if (tf != f
5f5c8ee5
GM
7355 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7356 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7357 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7358 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7359 break;
7360 }
7361
7362 /* Set global variable indicating that multiple frames exist. */
7363 multiple_frames = CONSP (tail);
7364
7365 /* Switch to the buffer of selected window of the frame. Set up
7366 frame_title_ptr so that display_mode_element will output into it;
7367 then display the title. */
7368 obuf = current_buffer;
bb336f8d 7369 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7370 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7371 frame_title_ptr = frame_title_buf;
7372 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7373 NULL, DEFAULT_FACE_ID);
c53a1624 7374 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 7375 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7376 frame_title_ptr = NULL;
bb336f8d 7377 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7378
7379 /* Set the title only if it's changed. This avoids consing in
7380 the common case where it hasn't. (If it turns out that we've
7381 already wasted too much time by walking through the list with
7382 display_mode_element, then we might need to optimize at a
7383 higher level than this.) */
2311178e 7384 if (! STRINGP (f->name)
2051c264
GM
7385 || SBYTES (f->name) != len
7386 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
7387 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7388 }
7389}
7390
5f5c8ee5
GM
7391#endif /* not HAVE_WINDOW_SYSTEM */
7392
7393
7394
7395\f
7396/***********************************************************************
7397 Menu Bars
7398 ***********************************************************************/
7399
7400
7401/* Prepare for redisplay by updating menu-bar item lists when
7402 appropriate. This can call eval. */
7403
7404void
7405prepare_menu_bars ()
7406{
7407 int all_windows;
7408 struct gcpro gcpro1, gcpro2;
7409 struct frame *f;
6b5c4794 7410 Lisp_Object tooltip_frame;
5f5c8ee5 7411
86ffe5cd 7412#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7413 tooltip_frame = tip_frame;
7414#else
6b5c4794 7415 tooltip_frame = Qnil;
5f5c8ee5
GM
7416#endif
7417
7418 /* Update all frame titles based on their buffer names, etc. We do
7419 this before the menu bars so that the buffer-menu will show the
7420 up-to-date frame titles. */
7421#ifdef HAVE_WINDOW_SYSTEM
7422 if (windows_or_buffers_changed || update_mode_lines)
7423 {
7424 Lisp_Object tail, frame;
7425
7426 FOR_EACH_FRAME (tail, frame)
7427 {
7428 f = XFRAME (frame);
6b5c4794 7429 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7430 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7431 x_consider_frame_title (frame);
7432 }
7433 }
7434#endif /* HAVE_WINDOW_SYSTEM */
7435
7436 /* Update the menu bar item lists, if appropriate. This has to be
7437 done before any actual redisplay or generation of display lines. */
2311178e 7438 all_windows = (update_mode_lines
5f5c8ee5
GM
7439 || buffer_shared > 1
7440 || windows_or_buffers_changed);
7441 if (all_windows)
7442 {
7443 Lisp_Object tail, frame;
331379bf 7444 int count = SPECPDL_INDEX ();
5f5c8ee5
GM
7445
7446 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7447
7448 FOR_EACH_FRAME (tail, frame)
7449 {
7450 f = XFRAME (frame);
7451
7452 /* Ignore tooltip frame. */
6b5c4794 7453 if (EQ (frame, tooltip_frame))
5f5c8ee5 7454 continue;
2311178e 7455
5f5c8ee5
GM
7456 /* If a window on this frame changed size, report that to
7457 the user and clear the size-change flag. */
7458 if (FRAME_WINDOW_SIZES_CHANGED (f))
7459 {
7460 Lisp_Object functions;
2311178e 7461
5f5c8ee5
GM
7462 /* Clear flag first in case we get an error below. */
7463 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7464 functions = Vwindow_size_change_functions;
7465 GCPRO2 (tail, functions);
2311178e 7466
5f5c8ee5
GM
7467 while (CONSP (functions))
7468 {
7469 call1 (XCAR (functions), frame);
7470 functions = XCDR (functions);
7471 }
7472 UNGCPRO;
7473 }
2311178e 7474
5f5c8ee5
GM
7475 GCPRO1 (tail);
7476 update_menu_bar (f, 0);
7477#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7478 update_tool_bar (f, 0);
5f5c8ee5
GM
7479#endif
7480 UNGCPRO;
7481 }
7482
7483 unbind_to (count, Qnil);
7484 }
7485 else
7486 {
886bd6f2
GM
7487 struct frame *sf = SELECTED_FRAME ();
7488 update_menu_bar (sf, 1);
5f5c8ee5 7489#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7490 update_tool_bar (sf, 1);
5f5c8ee5
GM
7491#endif
7492 }
7493
7494 /* Motif needs this. See comment in xmenu.c. Turn it off when
7495 pending_menu_activation is not defined. */
7496#ifdef USE_X_TOOLKIT
7497 pending_menu_activation = 0;
7498#endif
7499}
7500
7501
7502/* Update the menu bar item list for frame F. This has to be done
7503 before we start to fill in any display lines, because it can call
7504 eval.
7505
7506 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7507
7508static void
7509update_menu_bar (f, save_match_data)
7510 struct frame *f;
7511 int save_match_data;
7512{
7513 Lisp_Object window;
7514 register struct window *w;
7515
e1477f43
GM
7516 /* If called recursively during a menu update, do nothing. This can
7517 happen when, for instance, an activate-menubar-hook causes a
7518 redisplay. */
7519 if (inhibit_menubar_update)
7520 return;
7521
5f5c8ee5
GM
7522 window = FRAME_SELECTED_WINDOW (f);
7523 w = XWINDOW (window);
2311178e 7524
40ae145b 7525#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
7526 condition !NILP (w->update_mode_line), rather than using
7527 update_mode_lines directly, and this if statement may have
7528 been added to make that condition work. Now the if
2311178e 7529 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
7530 if (update_mode_lines)
7531 w->update_mode_line = Qt;
a5f08374 7532#endif
5f5c8ee5
GM
7533
7534 if (FRAME_WINDOW_P (f)
7535 ?
488dd4c4
JD
7536#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7537 || defined (USE_GTK)
2311178e 7538 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
7539#else
7540 FRAME_MENU_BAR_LINES (f) > 0
7541#endif
7542 : FRAME_MENU_BAR_LINES (f) > 0)
7543 {
7544 /* If the user has switched buffers or windows, we need to
7545 recompute to reflect the new bindings. But we'll
7546 recompute when update_mode_lines is set too; that means
7547 that people can use force-mode-line-update to request
7548 that the menu bar be recomputed. The adverse effect on
7549 the rest of the redisplay algorithm is about the same as
7550 windows_or_buffers_changed anyway. */
7551 if (windows_or_buffers_changed
a5f08374
RS
7552 /* This used to test w->update_mode_line, but we believe
7553 there is no need to recompute the menu in that case. */
7554 || update_mode_lines
5f5c8ee5
GM
7555 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7556 < BUF_MODIFF (XBUFFER (w->buffer)))
7557 != !NILP (w->last_had_star))
7558 || ((!NILP (Vtransient_mark_mode)
7559 && !NILP (XBUFFER (w->buffer)->mark_active))
7560 != !NILP (w->region_showing)))
7561 {
7562 struct buffer *prev = current_buffer;
331379bf 7563 int count = SPECPDL_INDEX ();
5f5c8ee5 7564
e1477f43
GM
7565 specbind (Qinhibit_menubar_update, Qt);
7566
5f5c8ee5
GM
7567 set_buffer_internal_1 (XBUFFER (w->buffer));
7568 if (save_match_data)
7569 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7570 if (NILP (Voverriding_local_map_menu_flag))
7571 {
7572 specbind (Qoverriding_terminal_local_map, Qnil);
7573 specbind (Qoverriding_local_map, Qnil);
7574 }
7575
7576 /* Run the Lucid hook. */
65048e97 7577 safe_run_hooks (Qactivate_menubar_hook);
2311178e 7578
5f5c8ee5
GM
7579 /* If it has changed current-menubar from previous value,
7580 really recompute the menu-bar from the value. */
7581 if (! NILP (Vlucid_menu_bar_dirty_flag))
7582 call0 (Qrecompute_lucid_menubar);
2311178e 7583
5f5c8ee5
GM
7584 safe_run_hooks (Qmenu_bar_update_hook);
7585 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 7586
5f5c8ee5 7587 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
7588#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7589 || defined (USE_GTK)
1a578e9b 7590 if (FRAME_WINDOW_P (f)
e0f712ba 7591#if defined (MAC_OS)
1a578e9b
AC
7592 /* All frames on Mac OS share the same menubar. So only the
7593 selected frame should be allowed to set it. */
7594 && f == SELECTED_FRAME ()
7595#endif
7596 )
5f5c8ee5
GM
7597 set_frame_menubar (f, 0, 0);
7598 else
7599 /* On a terminal screen, the menu bar is an ordinary screen
7600 line, and this makes it get updated. */
7601 w->update_mode_line = Qt;
488dd4c4 7602#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
7603 /* In the non-toolkit version, the menu bar is an ordinary screen
7604 line, and this makes it get updated. */
7605 w->update_mode_line = Qt;
488dd4c4 7606#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
7607
7608 unbind_to (count, Qnil);
7609 set_buffer_internal_1 (prev);
7610 }
7611 }
7612}
7613
7614
7615\f
7616/***********************************************************************
e037b9ec 7617 Tool-bars
5f5c8ee5
GM
7618 ***********************************************************************/
7619
7620#ifdef HAVE_WINDOW_SYSTEM
7621
e037b9ec 7622/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7623 before we start to fill in any display lines. Called from
7624 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7625 and restore it here. */
7626
7627static void
e037b9ec 7628update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7629 struct frame *f;
7630 int save_match_data;
7631{
488dd4c4
JD
7632#ifdef USE_GTK
7633 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
7634#else
7635 int do_update = WINDOWP (f->tool_bar_window)
7636 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
7637#endif
7638
7639 if (do_update)
5f5c8ee5
GM
7640 {
7641 Lisp_Object window;
7642 struct window *w;
7643
7644 window = FRAME_SELECTED_WINDOW (f);
7645 w = XWINDOW (window);
2311178e 7646
5f5c8ee5
GM
7647 /* If the user has switched buffers or windows, we need to
7648 recompute to reflect the new bindings. But we'll
7649 recompute when update_mode_lines is set too; that means
7650 that people can use force-mode-line-update to request
7651 that the menu bar be recomputed. The adverse effect on
7652 the rest of the redisplay algorithm is about the same as
7653 windows_or_buffers_changed anyway. */
7654 if (windows_or_buffers_changed
7655 || !NILP (w->update_mode_line)
7656 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7657 < BUF_MODIFF (XBUFFER (w->buffer)))
7658 != !NILP (w->last_had_star))
7659 || ((!NILP (Vtransient_mark_mode)
7660 && !NILP (XBUFFER (w->buffer)->mark_active))
7661 != !NILP (w->region_showing)))
7662 {
7663 struct buffer *prev = current_buffer;
331379bf 7664 int count = SPECPDL_INDEX ();
a2889657 7665
5f5c8ee5
GM
7666 /* Set current_buffer to the buffer of the selected
7667 window of the frame, so that we get the right local
7668 keymaps. */
7669 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7670
5f5c8ee5
GM
7671 /* Save match data, if we must. */
7672 if (save_match_data)
7673 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7674
7675 /* Make sure that we don't accidentally use bogus keymaps. */
7676 if (NILP (Voverriding_local_map_menu_flag))
7677 {
7678 specbind (Qoverriding_terminal_local_map, Qnil);
7679 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7680 }
1f40cad2 7681
e037b9ec 7682 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7683 f->tool_bar_items
7684 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
2311178e 7685
e037b9ec 7686 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7687 w->update_mode_line = Qt;
7688
7689 unbind_to (count, Qnil);
7690 set_buffer_internal_1 (prev);
81d478f3 7691 }
a2889657
JB
7692 }
7693}
7694
6c4429a5 7695
e037b9ec 7696/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7697 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7698 been set up previously by calling prepare_menu_bars. */
7699
a2889657 7700static void
e037b9ec 7701build_desired_tool_bar_string (f)
5f5c8ee5 7702 struct frame *f;
a2889657 7703{
a23887b9 7704 int i, size, size_needed;
5f5c8ee5
GM
7705 struct gcpro gcpro1, gcpro2, gcpro3;
7706 Lisp_Object image, plist, props;
a2889657 7707
5f5c8ee5
GM
7708 image = plist = props = Qnil;
7709 GCPRO3 (image, plist, props);
a2889657 7710
e037b9ec 7711 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 7712 Otherwise, make a new string. */
2311178e 7713
5f5c8ee5 7714 /* The size of the string we might be able to reuse. */
e037b9ec 7715 size = (STRINGP (f->desired_tool_bar_string)
2051c264 7716 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
7717 : 0);
7718
b94c0d9c 7719 /* We need one space in the string for each image. */
a23887b9 7720 size_needed = f->n_tool_bar_items;
2311178e 7721
b94c0d9c 7722 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7723 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7724 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7725 make_number (' '));
5f5c8ee5
GM
7726 else
7727 {
7728 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7729 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7730 props, f->desired_tool_bar_string);
5f5c8ee5 7731 }
a2889657 7732
5f5c8ee5 7733 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7734 put a `menu_item' property on tool-bar items with a value that
7735 is the index of the item in F's tool-bar item vector. */
a23887b9 7736 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7737 {
7464726e 7738#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7739
e037b9ec
GM
7740 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7741 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7742 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7743 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7744
7745 /* If image is a vector, choose the image according to the
7746 button state. */
e037b9ec 7747 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7748 if (VECTORP (image))
7749 {
5f5c8ee5
GM
7750 if (enabled_p)
7751 idx = (selected_p
e037b9ec
GM
7752 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7753 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7754 else
7755 idx = (selected_p
e037b9ec
GM
7756 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7757 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 7758
37e4e482
GM
7759 xassert (ASIZE (image) >= idx);
7760 image = AREF (image, idx);
5f5c8ee5 7761 }
37e4e482
GM
7762 else
7763 idx = -1;
5f5c8ee5
GM
7764
7765 /* Ignore invalid image specifications. */
7766 if (!valid_image_p (image))
7767 continue;
7768
e037b9ec 7769 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7770 plist = Fcopy_sequence (XCDR (image));
7771
7772 /* Compute margin and relief to draw. */
9f864bf9 7773 relief = (tool_bar_button_relief >= 0
c3d76173
GM
7774 ? tool_bar_button_relief
7775 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7776 hmargin = vmargin = relief;
7777
7778 if (INTEGERP (Vtool_bar_button_margin)
7779 && XINT (Vtool_bar_button_margin) > 0)
7780 {
7781 hmargin += XFASTINT (Vtool_bar_button_margin);
7782 vmargin += XFASTINT (Vtool_bar_button_margin);
7783 }
7784 else if (CONSP (Vtool_bar_button_margin))
7785 {
7786 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7787 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7788 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 7789
35a41507
GM
7790 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7791 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7792 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7793 }
2311178e 7794
e037b9ec 7795 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7796 {
7797 /* Add a `:relief' property to the image spec if the item is
7798 selected. */
7799 if (selected_p)
7800 {
7801 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7802 hmargin -= relief;
7803 vmargin -= relief;
5f5c8ee5
GM
7804 }
7805 }
7806 else
7807 {
7808 /* If image is selected, display it pressed, i.e. with a
7809 negative relief. If it's not selected, display it with a
7810 raised relief. */
7811 plist = Fplist_put (plist, QCrelief,
7812 (selected_p
7813 ? make_number (-relief)
7814 : make_number (relief)));
35a41507
GM
7815 hmargin -= relief;
7816 vmargin -= relief;
5f5c8ee5
GM
7817 }
7818
7819 /* Put a margin around the image. */
35a41507
GM
7820 if (hmargin || vmargin)
7821 {
7822 if (hmargin == vmargin)
7823 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7824 else
7825 plist = Fplist_put (plist, QCmargin,
7826 Fcons (make_number (hmargin),
7827 make_number (vmargin)));
7828 }
2311178e 7829
37e4e482
GM
7830 /* If button is not enabled, and we don't have special images
7831 for the disabled state, make the image appear disabled by
5f5c8ee5 7832 applying an appropriate algorithm to it. */
37e4e482 7833 if (!enabled_p && idx < 0)
ecd01a0e 7834 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 7835
5f5c8ee5
GM
7836 /* Put a `display' text property on the string for the image to
7837 display. Put a `menu-item' property on the string that gives
e037b9ec 7838 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7839 vector. */
7840 image = Fcons (Qimage, plist);
7841 props = list4 (Qdisplay, image,
a23887b9
GM
7842 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7843
7844 /* Let the last image hide all remaining spaces in the tool bar
7845 string. The string can be longer than needed when we reuse a
7846 previous string. */
7847 if (i + 1 == f->n_tool_bar_items)
2051c264 7848 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
7849 else
7850 end = i + 1;
7851 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7852 props, f->desired_tool_bar_string);
5f5c8ee5 7853#undef PROP
a2889657
JB
7854 }
7855
5f5c8ee5
GM
7856 UNGCPRO;
7857}
7858
7859
e037b9ec 7860/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7861
7862static void
e037b9ec 7863display_tool_bar_line (it)
5f5c8ee5
GM
7864 struct it *it;
7865{
7866 struct glyph_row *row = it->glyph_row;
7867 int max_x = it->last_visible_x;
7868 struct glyph *last;
2311178e 7869
5f5c8ee5
GM
7870 prepare_desired_row (row);
7871 row->y = it->current_y;
90aa2856
GM
7872
7873 /* Note that this isn't made use of if the face hasn't a box,
7874 so there's no need to check the face here. */
7875 it->start_of_box_run_p = 1;
2311178e 7876
5f5c8ee5 7877 while (it->current_x < max_x)
a2889657 7878 {
5f5c8ee5 7879 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7880
5f5c8ee5
GM
7881 /* Get the next display element. */
7882 if (!get_next_display_element (it))
7883 break;
73af359d 7884
5f5c8ee5
GM
7885 /* Produce glyphs. */
7886 x_before = it->current_x;
7887 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7888 PRODUCE_GLYPHS (it);
daa37602 7889
5f5c8ee5
GM
7890 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7891 i = 0;
7892 x = x_before;
7893 while (i < nglyphs)
7894 {
7895 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 7896
5f5c8ee5
GM
7897 if (x + glyph->pixel_width > max_x)
7898 {
7899 /* Glyph doesn't fit on line. */
7900 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7901 it->current_x = x;
7902 goto out;
7903 }
daa37602 7904
5f5c8ee5
GM
7905 ++it->hpos;
7906 x += glyph->pixel_width;
7907 ++i;
7908 }
7909
7910 /* Stop at line ends. */
7911 if (ITERATOR_AT_END_OF_LINE_P (it))
7912 break;
7913
cafafe0b 7914 set_iterator_to_next (it, 1);
a2889657 7915 }
a2889657 7916
5f5c8ee5 7917 out:;
a2889657 7918
5f5c8ee5
GM
7919 row->displays_text_p = row->used[TEXT_AREA] != 0;
7920 extend_face_to_end_of_line (it);
7921 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7922 last->right_box_line_p = 1;
90aa2856
GM
7923 if (last == row->glyphs[TEXT_AREA])
7924 last->left_box_line_p = 1;
5f5c8ee5 7925 compute_line_metrics (it);
2311178e 7926
e037b9ec 7927 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7928 if (!row->displays_text_p)
7929 {
312246d1
GM
7930 row->height = row->phys_height = it->last_visible_y - row->y;
7931 row->ascent = row->phys_ascent = 0;
5f5c8ee5 7932 }
2311178e 7933
5f5c8ee5
GM
7934 row->full_width_p = 1;
7935 row->continued_p = 0;
7936 row->truncated_on_left_p = 0;
7937 row->truncated_on_right_p = 0;
7938
7939 it->current_x = it->hpos = 0;
7940 it->current_y += row->height;
7941 ++it->vpos;
7942 ++it->glyph_row;
a2889657 7943}
96a410bc 7944
5f5c8ee5 7945
e037b9ec 7946/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7947 items of frame F visible. */
96a410bc 7948
d39b6696 7949static int
e037b9ec 7950tool_bar_lines_needed (f)
5f5c8ee5 7951 struct frame *f;
d39b6696 7952{
e037b9ec 7953 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 7954 struct it it;
2311178e 7955
e037b9ec
GM
7956 /* Initialize an iterator for iteration over
7957 F->desired_tool_bar_string in the tool-bar window of frame F. */
7958 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7959 it.first_visible_x = 0;
7960 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7961 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7962
7963 while (!ITERATOR_AT_END_P (&it))
7964 {
7965 it.glyph_row = w->desired_matrix->rows;
7966 clear_glyph_row (it.glyph_row);
e037b9ec 7967 display_tool_bar_line (&it);
5f5c8ee5
GM
7968 }
7969
7970 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7971}
96a410bc 7972
5f5c8ee5 7973
57c28064
GM
7974DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7975 0, 1, 0,
7ee72033
MB
7976 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7977 (frame)
57c28064
GM
7978 Lisp_Object frame;
7979{
7980 struct frame *f;
7981 struct window *w;
7982 int nlines = 0;
7983
7984 if (NILP (frame))
7985 frame = selected_frame;
7986 else
b7826503 7987 CHECK_FRAME (frame);
57c28064 7988 f = XFRAME (frame);
2311178e 7989
57c28064
GM
7990 if (WINDOWP (f->tool_bar_window)
7991 || (w = XWINDOW (f->tool_bar_window),
7992 XFASTINT (w->height) > 0))
7993 {
7994 update_tool_bar (f, 1);
7995 if (f->n_tool_bar_items)
7996 {
7997 build_desired_tool_bar_string (f);
7998 nlines = tool_bar_lines_needed (f);
7999 }
8000 }
8001
8002 return make_number (nlines);
8003}
8004
8005
e037b9ec 8006/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
8007 height should be changed. */
8008
8009static int
e037b9ec 8010redisplay_tool_bar (f)
5f5c8ee5 8011 struct frame *f;
96a410bc 8012{
5f5c8ee5
GM
8013 struct window *w;
8014 struct it it;
8015 struct glyph_row *row;
8016 int change_height_p = 0;
177c0ea7 8017
488dd4c4
JD
8018#ifdef USE_GTK
8019 if (FRAME_EXTERNAL_TOOL_BAR(f))
8020 update_frame_tool_bar (f);
8021 return 0;
8022#endif
2311178e 8023
e037b9ec
GM
8024 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8025 do anything. This means you must start with tool-bar-lines
5f5c8ee5 8026 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
8027 can turn off tool-bars by specifying tool-bar-lines zero. */
8028 if (!WINDOWP (f->tool_bar_window)
8029 || (w = XWINDOW (f->tool_bar_window),
488dd4c4 8030 XFASTINT (w->height) == 0))
5f5c8ee5 8031 return 0;
96a410bc 8032
e037b9ec
GM
8033 /* Set up an iterator for the tool-bar window. */
8034 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
8035 it.first_visible_x = 0;
8036 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8037 row = it.glyph_row;
3450d04c 8038
e037b9ec
GM
8039 /* Build a string that represents the contents of the tool-bar. */
8040 build_desired_tool_bar_string (f);
8041 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 8042
e037b9ec 8043 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 8044 while (it.current_y < it.last_visible_y)
e037b9ec 8045 display_tool_bar_line (&it);
3450d04c 8046
e037b9ec 8047 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
8048 window, so don't do it. */
8049 w->desired_matrix->no_scrolling_p = 1;
8050 w->must_be_updated_p = 1;
3450d04c 8051
e037b9ec 8052 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
8053 {
8054 int nlines;
6e33e6f0
GM
8055
8056 /* If we couldn't display everything, change the tool-bar's
8057 height. */
8058 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8059 change_height_p = 1;
2311178e 8060
5f5c8ee5
GM
8061 /* If there are blank lines at the end, except for a partially
8062 visible blank line at the end that is smaller than
e037b9ec 8063 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
8064 row = it.glyph_row - 1;
8065 if (!row->displays_text_p
8066 && row->height >= CANON_Y_UNIT (f))
8067 change_height_p = 1;
8068
e037b9ec
GM
8069 /* If row displays tool-bar items, but is partially visible,
8070 change the tool-bar's height. */
5f5c8ee5
GM
8071 if (row->displays_text_p
8072 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8073 change_height_p = 1;
8074
e037b9ec 8075 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
8076 frame parameter. */
8077 if (change_height_p
e037b9ec 8078 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
8079 nlines != XFASTINT (w->height)))
8080 {
e037b9ec 8081 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 8082 Lisp_Object frame;
e85ee976 8083 int old_height = XFASTINT (w->height);
2311178e 8084
5f5c8ee5
GM
8085 XSETFRAME (frame, f);
8086 clear_glyph_matrix (w->desired_matrix);
8087 Fmodify_frame_parameters (frame,
e037b9ec 8088 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
8089 make_number (nlines)),
8090 Qnil));
e85ee976
GM
8091 if (XFASTINT (w->height) != old_height)
8092 fonts_changed_p = 1;
5f5c8ee5
GM
8093 }
8094 }
3450d04c 8095
5f5c8ee5 8096 return change_height_p;
96a410bc 8097}
90adcf20 8098
5f5c8ee5 8099
e037b9ec
GM
8100/* Get information about the tool-bar item which is displayed in GLYPH
8101 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8102 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8103 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
8104
8105int
e037b9ec 8106tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8107 struct frame *f;
8108 struct glyph *glyph;
8109 int *prop_idx;
90adcf20 8110{
5f5c8ee5
GM
8111 Lisp_Object prop;
8112 int success_p;
be676094
GM
8113 int charpos;
8114
8115 /* This function can be called asynchronously, which means we must
8116 exclude any possibility that Fget_text_property signals an
8117 error. */
2051c264 8118 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8119 charpos = max (0, charpos);
2311178e 8120
5f5c8ee5
GM
8121 /* Get the text property `menu-item' at pos. The value of that
8122 property is the start index of this item's properties in
7464726e 8123 F->tool_bar_items. */
be676094 8124 prop = Fget_text_property (make_number (charpos),
e037b9ec 8125 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8126 if (INTEGERP (prop))
8127 {
8128 *prop_idx = XINT (prop);
8129 success_p = 1;
8130 }
8131 else
8132 success_p = 0;
90adcf20 8133
5f5c8ee5
GM
8134 return success_p;
8135}
2311178e 8136
5f5c8ee5 8137#endif /* HAVE_WINDOW_SYSTEM */
ffbbc941
KS
8138
8139
8140\f
8141/***********************************************************************
8142 Fringes
8143 ***********************************************************************/
8144
8145#ifdef HAVE_WINDOW_SYSTEM
8146
8147/* An arrow like this: `<-'. */
8148static unsigned char left_bits[] = {
8149 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8150
8151/* Right truncation arrow bitmap `->'. */
8152static unsigned char right_bits[] = {
8153 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8154
8155/* Marker for continued lines. */
8156static unsigned char continued_bits[] = {
8157 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8158
8159/* Marker for continuation lines. */
8160static unsigned char continuation_bits[] = {
8161 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8162
8163/* Overlay arrow bitmap. A triangular arrow. */
8164static unsigned char ov_bits[] = {
8165 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8166
8167/* Bitmap drawn to indicate lines not displaying text if
8168 `indicate-empty-lines' is non-nil. */
8169static unsigned char zv_bits[] = {
8170 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8171 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8172 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8173 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8174 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8175 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8176 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8177 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8178
8179struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8180{
8181 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8182 { 8, sizeof (left_bits), 0, left_bits },
8183 { 8, sizeof (right_bits), 0, right_bits },
8184 { 8, sizeof (continued_bits), 0, continued_bits },
8185 { 8, sizeof (continuation_bits), 0, continuation_bits },
8186 { 8, sizeof (ov_bits), 0, ov_bits },
8187 { 8, sizeof (zv_bits), 3, zv_bits }
8188};
8189
8190
8191/* Draw the bitmap WHICH in one of the left or right fringes of
8192 window W. ROW is the glyph row for which to display the bitmap; it
8193 determines the vertical position at which the bitmap has to be
8194 drawn. */
8195
8196static void
8197draw_fringe_bitmap (w, row, which, left_p)
8198 struct window *w;
8199 struct glyph_row *row;
8200 enum fringe_bitmap_type which;
8201 int left_p;
8202{
8203 struct frame *f = XFRAME (WINDOW_FRAME (w));
8204 struct draw_fringe_bitmap_params p;
8205
8206 /* Convert row to frame coordinates. */
8207 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8208
8209 p.which = which;
8210 p.wd = fringe_bitmaps[which].width;
8211
8212 p.h = fringe_bitmaps[which].height;
8213 p.dh = (fringe_bitmaps[which].period
8214 ? (p.y % fringe_bitmaps[which].period)
8215 : 0);
8216 p.h -= p.dh;
8217 /* Clip bitmap if too high. */
8218 if (p.h > row->height)
8219 p.h = row->height;
8220
8221 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8222 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8223
8224 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8225 the fringe. */
8226 p.bx = -1;
8227 if (left_p)
8228 {
8229 if (p.wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
8230 p.wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
8231 p.x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
8232 - p.wd
8233 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - p.wd) / 2);
8234 if (p.wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > p.h)
8235 {
8236 /* If W has a vertical border to its left, don't draw over it. */
8237 int border = ((XFASTINT (w->left) > 0
8238 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8239 ? 1 : 0);
8240 p.bx = (window_box_left (w, -1)
8241 - FRAME_X_LEFT_FRINGE_WIDTH (f)
8242 + border);
8243 p.nx = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
8244 }
8245 }
8246 else
8247 {
8248 if (p.wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
8249 p.wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8250 p.x = (window_box_right (w, -1)
8251 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - p.wd) / 2);
8252 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8253 the fringe. */
8254 if (p.wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > p.h)
8255 {
8256 p.bx = window_box_right (w, -1);
8257 p.nx = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8258 }
8259 }
8260
8261 if (p.bx >= 0)
8262 {
8263 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8264
8265 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8266 p.ny = row->visible_height;
8267 }
8268
8269 /* Adjust y to the offset in the row to start drawing the bitmap. */
8270 p.y += (row->height - p.h) / 2;
8271
8272 rif->draw_fringe_bitmap (w, row, &p);
8273}
8274
8275/* Draw fringe bitmaps for glyph row ROW on window W. Call this
8276 function with input blocked. */
8277
8278void
8279draw_row_fringe_bitmaps (w, row)
8280 struct window *w;
8281 struct glyph_row *row;
8282{
8283 struct frame *f = XFRAME (w->frame);
8284 enum fringe_bitmap_type bitmap;
8285
8286 xassert (interrupt_input_blocked);
8287
8288 /* If row is completely invisible, because of vscrolling, we
8289 don't have to draw anything. */
8290 if (row->visible_height <= 0)
8291 return;
8292
8293 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
8294 {
8295 /* Decide which bitmap to draw in the left fringe. */
8296 if (row->overlay_arrow_p)
8297 bitmap = OVERLAY_ARROW_BITMAP;
8298 else if (row->truncated_on_left_p)
8299 bitmap = LEFT_TRUNCATION_BITMAP;
8300 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
8301 bitmap = CONTINUATION_LINE_BITMAP;
8302 else if (row->indicate_empty_line_p)
8303 bitmap = ZV_LINE_BITMAP;
8304 else
8305 bitmap = NO_FRINGE_BITMAP;
8306
8307 draw_fringe_bitmap (w, row, bitmap, 1);
8308 }
8309
8310 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
8311 {
8312 /* Decide which bitmap to draw in the right fringe. */
8313 if (row->truncated_on_right_p)
8314 bitmap = RIGHT_TRUNCATION_BITMAP;
8315 else if (row->continued_p)
8316 bitmap = CONTINUED_LINE_BITMAP;
8317 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
8318 bitmap = ZV_LINE_BITMAP;
8319 else
8320 bitmap = NO_FRINGE_BITMAP;
8321
8322 draw_fringe_bitmap (w, row, bitmap, 0);
8323 }
8324}
8325
8326
8327/* Compute actual fringe widths */
8328
8329void
8330compute_fringe_widths (f, redraw)
8331 struct frame *f;
8332 int redraw;
8333{
8334 int o_left = FRAME_X_LEFT_FRINGE_WIDTH (f);
8335 int o_right = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8336 int o_cols = FRAME_X_FRINGE_COLS (f);
8337
8338 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
8339 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
8340 int left_fringe_width, right_fringe_width;
8341
8342 if (!NILP (left_fringe))
8343 left_fringe = Fcdr (left_fringe);
8344 if (!NILP (right_fringe))
8345 right_fringe = Fcdr (right_fringe);
8346
8347 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
8348 XINT (left_fringe));
8349 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
8350 XINT (right_fringe));
8351
8352 if (left_fringe_width || right_fringe_width)
8353 {
8354 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
8355 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
8356 int conf_wid = left_wid + right_wid;
3ecd814e 8357 int font_wid = FONT_WIDTH (FRAME_FONT (f));
ffbbc941
KS
8358 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
8359 int real_wid = cols * font_wid;
8360 if (left_wid && right_wid)
8361 {
8362 if (left_fringe_width < 0)
8363 {
8364 /* Left fringe width is fixed, adjust right fringe if necessary */
8365 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid;
8366 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
8367 }
8368 else if (right_fringe_width < 0)
8369 {
8370 /* Right fringe width is fixed, adjust left fringe if necessary */
8371 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
8372 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid;
8373 }
8374 else
8375 {
8376 /* Adjust both fringes with an equal amount.
8377 Note that we are doing integer arithmetic here, so don't
8378 lose a pixel if the total width is an odd number. */
8379 int fill = real_wid - conf_wid;
8380 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
8381 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
8382 }
8383 }
8384 else if (left_fringe_width)
8385 {
8386 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid;
8387 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
8388 }
8389 else
8390 {
8391 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
8392 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid;
8393 }
8394 FRAME_X_FRINGE_COLS (f) = cols;
8395 FRAME_X_FRINGE_WIDTH (f) = real_wid;
8396 }
8397 else
8398 {
8399 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
8400 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
8401 FRAME_X_FRINGE_COLS (f) = 0;
8402 FRAME_X_FRINGE_WIDTH (f) = 0;
8403 }
8404
8405 if (redraw && FRAME_VISIBLE_P (f))
8406 if (o_left != FRAME_X_LEFT_FRINGE_WIDTH (f) ||
8407 o_right != FRAME_X_RIGHT_FRINGE_WIDTH (f) ||
8408 o_cols != FRAME_X_FRINGE_COLS (f))
8409 redraw_frame (f);
8410}
8411
8412#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 8413
feb0c42f 8414
5f5c8ee5
GM
8415\f
8416/************************************************************************
8417 Horizontal scrolling
8418 ************************************************************************/
feb0c42f 8419
5f5c8ee5
GM
8420static int hscroll_window_tree P_ ((Lisp_Object));
8421static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 8422
5f5c8ee5
GM
8423/* For all leaf windows in the window tree rooted at WINDOW, set their
8424 hscroll value so that PT is (i) visible in the window, and (ii) so
8425 that it is not within a certain margin at the window's left and
8426 right border. Value is non-zero if any window's hscroll has been
8427 changed. */
8428
8429static int
8430hscroll_window_tree (window)
8431 Lisp_Object window;
8432{
8433 int hscrolled_p = 0;
e76d28d5 8434 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
8435 int hscroll_step_abs = 0;
8436 double hscroll_step_rel = 0;
8437
8438 if (hscroll_relative_p)
8439 {
e76d28d5 8440 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
8441 if (hscroll_step_rel < 0)
8442 {
8443 hscroll_relative_p = 0;
8444 hscroll_step_abs = 0;
8445 }
8446 }
e76d28d5 8447 else if (INTEGERP (Vhscroll_step))
1df7e8f0 8448 {
e76d28d5 8449 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
8450 if (hscroll_step_abs < 0)
8451 hscroll_step_abs = 0;
8452 }
8453 else
8454 hscroll_step_abs = 0;
8455
5f5c8ee5 8456 while (WINDOWP (window))
90adcf20 8457 {
5f5c8ee5 8458 struct window *w = XWINDOW (window);
1df7e8f0 8459
5f5c8ee5
GM
8460 if (WINDOWP (w->hchild))
8461 hscrolled_p |= hscroll_window_tree (w->hchild);
8462 else if (WINDOWP (w->vchild))
8463 hscrolled_p |= hscroll_window_tree (w->vchild);
8464 else if (w->cursor.vpos >= 0)
8465 {
e76d28d5 8466 int h_margin, text_area_x, text_area_y;
5f5c8ee5 8467 int text_area_width, text_area_height;
92a90e89
GM
8468 struct glyph_row *current_cursor_row
8469 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8470 struct glyph_row *desired_cursor_row
8471 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8472 struct glyph_row *cursor_row
8473 = (desired_cursor_row->enabled_p
8474 ? desired_cursor_row
8475 : current_cursor_row);
a2725ab2 8476
5f5c8ee5
GM
8477 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8478 &text_area_width, &text_area_height);
90adcf20 8479
5f5c8ee5 8480 /* Scroll when cursor is inside this scroll margin. */
e76d28d5 8481 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
1df7e8f0 8482
5f5c8ee5 8483 if ((XFASTINT (w->hscroll)
e76d28d5 8484 && w->cursor.x <= h_margin)
92a90e89
GM
8485 || (cursor_row->enabled_p
8486 && cursor_row->truncated_on_right_p
e76d28d5 8487 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 8488 {
5f5c8ee5
GM
8489 struct it it;
8490 int hscroll;
8491 struct buffer *saved_current_buffer;
8492 int pt;
1df7e8f0 8493 int wanted_x;
5f5c8ee5
GM
8494
8495 /* Find point in a display of infinite width. */
8496 saved_current_buffer = current_buffer;
8497 current_buffer = XBUFFER (w->buffer);
1df7e8f0 8498
5f5c8ee5
GM
8499 if (w == XWINDOW (selected_window))
8500 pt = BUF_PT (current_buffer);
8501 else
08b610e4 8502 {
5f5c8ee5
GM
8503 pt = marker_position (w->pointm);
8504 pt = max (BEGV, pt);
8505 pt = min (ZV, pt);
8506 }
8507
8508 /* Move iterator to pt starting at cursor_row->start in
8509 a line with infinite width. */
8510 init_to_row_start (&it, w, cursor_row);
8511 it.last_visible_x = INFINITY;
8512 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8513 current_buffer = saved_current_buffer;
8514
1df7e8f0
EZ
8515 /* Position cursor in window. */
8516 if (!hscroll_relative_p && hscroll_step_abs == 0)
8517 hscroll = max (0, it.current_x - text_area_width / 2)
8518 / CANON_X_UNIT (it.f);
e76d28d5 8519 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
8520 {
8521 if (hscroll_relative_p)
8522 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 8523 - h_margin;
1df7e8f0
EZ
8524 else
8525 wanted_x = text_area_width
8526 - hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8527 - h_margin;
1df7e8f0
EZ
8528 hscroll
8529 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8530 }
8531 else
8532 {
8533 if (hscroll_relative_p)
8534 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 8535 + h_margin;
1df7e8f0
EZ
8536 else
8537 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8538 + h_margin;
1df7e8f0
EZ
8539 hscroll
8540 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8541 }
3172f70b 8542 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
8543
8544 /* Don't call Fset_window_hscroll if value hasn't
8545 changed because it will prevent redisplay
8546 optimizations. */
8547 if (XFASTINT (w->hscroll) != hscroll)
8548 {
3172f70b
GM
8549 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8550 w->hscroll = make_number (hscroll);
5f5c8ee5 8551 hscrolled_p = 1;
08b610e4 8552 }
08b610e4 8553 }
08b610e4 8554 }
a2725ab2 8555
5f5c8ee5 8556 window = w->next;
90adcf20 8557 }
cd6dfed6 8558
5f5c8ee5
GM
8559 /* Value is non-zero if hscroll of any leaf window has been changed. */
8560 return hscrolled_p;
8561}
8562
8563
8564/* Set hscroll so that cursor is visible and not inside horizontal
8565 scroll margins for all windows in the tree rooted at WINDOW. See
8566 also hscroll_window_tree above. Value is non-zero if any window's
8567 hscroll has been changed. If it has, desired matrices on the frame
8568 of WINDOW are cleared. */
8569
8570static int
8571hscroll_windows (window)
8572 Lisp_Object window;
8573{
d475bcb8 8574 int hscrolled_p;
2311178e 8575
d475bcb8
GM
8576 if (automatic_hscrolling_p)
8577 {
8578 hscrolled_p = hscroll_window_tree (window);
8579 if (hscrolled_p)
8580 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8581 }
8582 else
8583 hscrolled_p = 0;
5f5c8ee5 8584 return hscrolled_p;
90adcf20 8585}
5f5c8ee5
GM
8586
8587
90adcf20 8588\f
5f5c8ee5
GM
8589/************************************************************************
8590 Redisplay
8591 ************************************************************************/
8592
8593/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8594 to a non-zero value. This is sometimes handy to have in a debugger
8595 session. */
8596
8597#if GLYPH_DEBUG
a2889657 8598
5f5c8ee5
GM
8599/* First and last unchanged row for try_window_id. */
8600
8601int debug_first_unchanged_at_end_vpos;
8602int debug_last_unchanged_at_beg_vpos;
8603
8604/* Delta vpos and y. */
8605
8606int debug_dvpos, debug_dy;
8607
8608/* Delta in characters and bytes for try_window_id. */
8609
8610int debug_delta, debug_delta_bytes;
8611
8612/* Values of window_end_pos and window_end_vpos at the end of
8613 try_window_id. */
8614
31ade731 8615EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
8616
8617/* Append a string to W->desired_matrix->method. FMT is a printf
8618 format string. A1...A9 are a supplement for a variable-length
8619 argument list. If trace_redisplay_p is non-zero also printf the
8620 resulting string to stderr. */
8621
8622static void
8623debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8624 struct window *w;
8625 char *fmt;
8626 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8627{
8628 char buffer[512];
8629 char *method = w->desired_matrix->method;
8630 int len = strlen (method);
8631 int size = sizeof w->desired_matrix->method;
8632 int remaining = size - len - 1;
8633
8634 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8635 if (len && remaining)
8636 {
8637 method[len] = '|';
8638 --remaining, ++len;
8639 }
2311178e 8640
5f5c8ee5
GM
8641 strncpy (method + len, buffer, remaining);
8642
8643 if (trace_redisplay_p)
8644 fprintf (stderr, "%p (%s): %s\n",
8645 w,
8646 ((BUFFERP (w->buffer)
8647 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 8648 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
8649 : "no buffer"),
8650 buffer);
8651}
a2889657 8652
5f5c8ee5 8653#endif /* GLYPH_DEBUG */
90adcf20 8654
a2889657 8655
5f5c8ee5
GM
8656/* Value is non-zero if all changes in window W, which displays
8657 current_buffer, are in the text between START and END. START is a
8658 buffer position, END is given as a distance from Z. Used in
8659 redisplay_internal for display optimization. */
8660
8661static INLINE int
8662text_outside_line_unchanged_p (w, start, end)
8663 struct window *w;
8664 int start, end;
8665{
8666 int unchanged_p = 1;
2311178e 8667
5f5c8ee5
GM
8668 /* If text or overlays have changed, see where. */
8669 if (XFASTINT (w->last_modified) < MODIFF
8670 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8671 {
8672 /* Gap in the line? */
8673 if (GPT < start || Z - GPT < end)
8674 unchanged_p = 0;
8675
8676 /* Changes start in front of the line, or end after it? */
8677 if (unchanged_p
9142dd5b
GM
8678 && (BEG_UNCHANGED < start - 1
8679 || END_UNCHANGED < end))
5f5c8ee5 8680 unchanged_p = 0;
2311178e 8681
5f5c8ee5
GM
8682 /* If selective display, can't optimize if changes start at the
8683 beginning of the line. */
8684 if (unchanged_p
8685 && INTEGERP (current_buffer->selective_display)
8686 && XINT (current_buffer->selective_display) > 0
9142dd5b 8687 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 8688 unchanged_p = 0;
47d57b22
GM
8689
8690 /* If there are overlays at the start or end of the line, these
8691 may have overlay strings with newlines in them. A change at
8692 START, for instance, may actually concern the display of such
8693 overlay strings as well, and they are displayed on different
8694 lines. So, quickly rule out this case. (For the future, it
8695 might be desirable to implement something more telling than
8696 just BEG/END_UNCHANGED.) */
8697 if (unchanged_p)
8698 {
8699 if (BEG + BEG_UNCHANGED == start
8700 && overlay_touches_p (start))
8701 unchanged_p = 0;
8702 if (END_UNCHANGED == end
8703 && overlay_touches_p (Z - end))
8704 unchanged_p = 0;
8705 }
5f5c8ee5
GM
8706 }
8707
8708 return unchanged_p;
8709}
8710
8711
8712/* Do a frame update, taking possible shortcuts into account. This is
8713 the main external entry point for redisplay.
8714
8715 If the last redisplay displayed an echo area message and that message
8716 is no longer requested, we clear the echo area or bring back the
8717 mini-buffer if that is in use. */
20de20dc 8718
a2889657
JB
8719void
8720redisplay ()
e9874cee
RS
8721{
8722 redisplay_internal (0);
8723}
8724
47d57b22 8725
260a86a0
KH
8726/* Return 1 if point moved out of or into a composition. Otherwise
8727 return 0. PREV_BUF and PREV_PT are the last point buffer and
8728 position. BUF and PT are the current point buffer and position. */
8729
8730int
8731check_point_in_composition (prev_buf, prev_pt, buf, pt)
8732 struct buffer *prev_buf, *buf;
8733 int prev_pt, pt;
8734{
8735 int start, end;
8736 Lisp_Object prop;
8737 Lisp_Object buffer;
8738
8739 XSETBUFFER (buffer, buf);
8740 /* Check a composition at the last point if point moved within the
8741 same buffer. */
8742 if (prev_buf == buf)
8743 {
8744 if (prev_pt == pt)
8745 /* Point didn't move. */
8746 return 0;
2311178e 8747
260a86a0
KH
8748 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8749 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8750 && COMPOSITION_VALID_P (start, end, prop)
8751 && start < prev_pt && end > prev_pt)
8752 /* The last point was within the composition. Return 1 iff
8753 point moved out of the composition. */
8754 return (pt <= start || pt >= end);
8755 }
8756
8757 /* Check a composition at the current point. */
8758 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8759 && find_composition (pt, -1, &start, &end, &prop, buffer)
8760 && COMPOSITION_VALID_P (start, end, prop)
8761 && start < pt && end > pt);
8762}
5f5c8ee5 8763
47d57b22 8764
9142dd5b
GM
8765/* Reconsider the setting of B->clip_changed which is displayed
8766 in window W. */
8767
8768static INLINE void
8769reconsider_clip_changes (w, b)
8770 struct window *w;
8771 struct buffer *b;
8772{
c62c1bb5 8773 if (b->clip_changed
9142dd5b
GM
8774 && !NILP (w->window_end_valid)
8775 && w->current_matrix->buffer == b
8776 && w->current_matrix->zv == BUF_ZV (b)
8777 && w->current_matrix->begv == BUF_BEGV (b))
8778 b->clip_changed = 0;
260a86a0
KH
8779
8780 /* If display wasn't paused, and W is not a tool bar window, see if
8781 point has been moved into or out of a composition. In that case,
8782 we set b->clip_changed to 1 to force updating the screen. If
8783 b->clip_changed has already been set to 1, we can skip this
8784 check. */
8785 if (!b->clip_changed
8786 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8787 {
8788 int pt;
8789
8790 if (w == XWINDOW (selected_window))
8791 pt = BUF_PT (current_buffer);
8792 else
8793 pt = marker_position (w->pointm);
8794
8795 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 8796 || pt != XINT (w->last_point))
260a86a0 8797 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 8798 XINT (w->last_point),
260a86a0
KH
8799 XBUFFER (w->buffer), pt))
8800 b->clip_changed = 1;
8801 }
9142dd5b 8802}
aac2d8b2
RS
8803\f
8804#define STOP_POLLING \
8805do { if (! polling_stopped_here) stop_polling (); \
8806 polling_stopped_here = 1; } while (0)
8807
8808#define RESUME_POLLING \
8809do { if (polling_stopped_here) start_polling (); \
8810 polling_stopped_here = 0; } while (0)
9142dd5b
GM
8811
8812
5f5c8ee5
GM
8813/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8814 response to any user action; therefore, we should preserve the echo
8815 area. (Actually, our caller does that job.) Perhaps in the future
8816 avoid recentering windows if it is not necessary; currently that
8817 causes some problems. */
e9874cee
RS
8818
8819static void
8820redisplay_internal (preserve_echo_area)
8821 int preserve_echo_area;
a2889657 8822{
5f5c8ee5
GM
8823 struct window *w = XWINDOW (selected_window);
8824 struct frame *f = XFRAME (w->frame);
8825 int pause;
a2889657 8826 int must_finish = 0;
5f5c8ee5 8827 struct text_pos tlbufpos, tlendpos;
89819bdd 8828 int number_of_visible_frames;
28514cd9 8829 int count;
886bd6f2 8830 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 8831 int polling_stopped_here = 0;
a2889657 8832
5f5c8ee5
GM
8833 /* Non-zero means redisplay has to consider all windows on all
8834 frames. Zero means, only selected_window is considered. */
8835 int consider_all_windows_p;
2311178e 8836
5f5c8ee5
GM
8837 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8838
8839 /* No redisplay if running in batch mode or frame is not yet fully
8840 initialized, or redisplay is explicitly turned off by setting
8841 Vinhibit_redisplay. */
8842 if (noninteractive
8843 || !NILP (Vinhibit_redisplay)
8844 || !f->glyphs_initialized_p)
a2889657
JB
8845 return;
8846
5f5c8ee5
GM
8847 /* The flag redisplay_performed_directly_p is set by
8848 direct_output_for_insert when it already did the whole screen
8849 update necessary. */
8850 if (redisplay_performed_directly_p)
8851 {
8852 redisplay_performed_directly_p = 0;
8853 if (!hscroll_windows (selected_window))
8854 return;
8855 }
8856
488dd4c4 8857#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
8858 if (popup_activated ())
8859 return;
8860#endif
8861
28514cd9 8862 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 8863 if (redisplaying_p)
735c094c
KH
8864 return;
8865
28514cd9
GM
8866 /* Record a function that resets redisplaying_p to its old value
8867 when we leave this function. */
331379bf 8868 count = SPECPDL_INDEX ();
28514cd9
GM
8869 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8870 ++redisplaying_p;
26683087 8871 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 8872
8b32d885 8873 retry:
bd9d0f3f 8874 pause = 0;
9142dd5b
GM
8875 reconsider_clip_changes (w, current_buffer);
8876
5f5c8ee5
GM
8877 /* If new fonts have been loaded that make a glyph matrix adjustment
8878 necessary, do it. */
8879 if (fonts_changed_p)
8880 {
8881 adjust_glyphs (NULL);
8882 ++windows_or_buffers_changed;
8883 fonts_changed_p = 0;
8884 }
8885
6961e0c1
GM
8886 /* If face_change_count is non-zero, init_iterator will free all
8887 realized faces, which includes the faces referenced from current
8888 matrices. So, we can't reuse current matrices in this case. */
8889 if (face_change_count)
8890 ++windows_or_buffers_changed;
8891
886bd6f2
GM
8892 if (! FRAME_WINDOW_P (sf)
8893 && previous_terminal_frame != sf)
20de20dc 8894 {
5f5c8ee5
GM
8895 /* Since frames on an ASCII terminal share the same display
8896 area, displaying a different frame means redisplay the whole
8897 thing. */
20de20dc 8898 windows_or_buffers_changed++;
886bd6f2
GM
8899 SET_FRAME_GARBAGED (sf);
8900 XSETFRAME (Vterminal_frame, sf);
20de20dc 8901 }
886bd6f2 8902 previous_terminal_frame = sf;
20de20dc 8903
5f5c8ee5
GM
8904 /* Set the visible flags for all frames. Do this before checking
8905 for resized or garbaged frames; they want to know if their frames
8906 are visible. See the comment in frame.h for
8907 FRAME_SAMPLE_VISIBILITY. */
d724d989 8908 {
35f56f96 8909 Lisp_Object tail, frame;
d724d989 8910
89819bdd
RS
8911 number_of_visible_frames = 0;
8912
35f56f96 8913 FOR_EACH_FRAME (tail, frame)
f82aff7c 8914 {
5f5c8ee5 8915 struct frame *f = XFRAME (frame);
2311178e 8916
5f5c8ee5
GM
8917 FRAME_SAMPLE_VISIBILITY (f);
8918 if (FRAME_VISIBLE_P (f))
8919 ++number_of_visible_frames;
8920 clear_desired_matrices (f);
f82aff7c 8921 }
d724d989
JB
8922 }
8923
44fa5b1e 8924 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8925 do_pending_window_change (1);
a2889657 8926
5f5c8ee5 8927 /* Clear frames marked as garbaged. */
44fa5b1e 8928 if (frame_garbaged)
c6e89d6c 8929 clear_garbaged_frames ();
a2889657 8930
e037b9ec 8931 /* Build menubar and tool-bar items. */
f82aff7c
RS
8932 prepare_menu_bars ();
8933
28995e67 8934 if (windows_or_buffers_changed)
a2889657
JB
8935 update_mode_lines++;
8936
538f13d4
RS
8937 /* Detect case that we need to write or remove a star in the mode line. */
8938 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8939 {
8940 w->update_mode_line = Qt;
8941 if (buffer_shared > 1)
8942 update_mode_lines++;
8943 }
8944
5f5c8ee5 8945 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8946 if (!NILP (w->column_number_displayed)
8947 /* This alternative quickly identifies a common case
8948 where no change is needed. */
8949 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8950 && XFASTINT (w->last_modified) >= MODIFF
8951 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
8952 && (XFASTINT (w->column_number_displayed)
8953 != (int) current_column ())) /* iftc */
8954 w->update_mode_line = Qt;
28995e67 8955
44fa5b1e 8956 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8957
5f5c8ee5
GM
8958 /* The variable buffer_shared is set in redisplay_window and
8959 indicates that we redisplay a buffer in different windows. See
8960 there. */
5fb96e96
RS
8961 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8962 || cursor_type_changed);
a2889657
JB
8963
8964 /* If specs for an arrow have changed, do thorough redisplay
8965 to ensure we remove any arrow that should no longer exist. */
d45de95b 8966 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8967 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8968 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8969
90adcf20
RS
8970 /* Normally the message* functions will have already displayed and
8971 updated the echo area, but the frame may have been trashed, or
8972 the update may have been preempted, so display the echo area
6e019995 8973 again here. Checking message_cleared_p captures the case that
c6e89d6c 8974 the echo area should be cleared. */
96d5e9a0
GM
8975 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8976 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
8977 || (message_cleared_p
8978 && minibuf_level == 0
8979 /* If the mini-window is currently selected, this means the
8980 echo-area doesn't show through. */
8981 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 8982 {
c6e89d6c 8983 int window_height_changed_p = echo_area_display (0);
90adcf20 8984 must_finish = 1;
6e019995
GM
8985
8986 /* If we don't display the current message, don't clear the
8987 message_cleared_p flag, because, if we did, we wouldn't clear
8988 the echo area in the next redisplay which doesn't preserve
8989 the echo area. */
8990 if (!display_last_displayed_message_p)
8991 message_cleared_p = 0;
2311178e 8992
c6e89d6c
GM
8993 if (fonts_changed_p)
8994 goto retry;
8995 else if (window_height_changed_p)
8996 {
8997 consider_all_windows_p = 1;
8998 ++update_mode_lines;
8999 ++windows_or_buffers_changed;
2311178e 9000
9142dd5b
GM
9001 /* If window configuration was changed, frames may have been
9002 marked garbaged. Clear them or we will experience
9003 surprises wrt scrolling. */
9004 if (frame_garbaged)
9005 clear_garbaged_frames ();
c6e89d6c 9006 }
90adcf20 9007 }
97a36635 9008 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
9009 && (current_buffer->clip_changed
9010 || XFASTINT (w->last_modified) < MODIFF
9011 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 9012 && resize_mini_window (w, 0))
c6e89d6c
GM
9013 {
9014 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
9015 showing if its contents might have changed. */
9016 must_finish = 1;
9017 consider_all_windows_p = 1;
c6e89d6c 9018 ++windows_or_buffers_changed;
dd2eb166 9019 ++update_mode_lines;
2311178e 9020
9142dd5b
GM
9021 /* If window configuration was changed, frames may have been
9022 marked garbaged. Clear them or we will experience
9023 surprises wrt scrolling. */
9024 if (frame_garbaged)
9025 clear_garbaged_frames ();
c6e89d6c 9026 }
2311178e 9027
90adcf20 9028
5f5c8ee5
GM
9029 /* If showing the region, and mark has changed, we must redisplay
9030 the whole window. The assignment to this_line_start_pos prevents
9031 the optimization directly below this if-statement. */
bd66d1ba
RS
9032 if (((!NILP (Vtransient_mark_mode)
9033 && !NILP (XBUFFER (w->buffer)->mark_active))
9034 != !NILP (w->region_showing))
82d04750
JB
9035 || (!NILP (w->region_showing)
9036 && !EQ (w->region_showing,
9037 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
9038 CHARPOS (this_line_start_pos) = 0;
9039
9040 /* Optimize the case that only the line containing the cursor in the
9041 selected window has changed. Variables starting with this_ are
9042 set in display_line and record information about the line
9043 containing the cursor. */
9044 tlbufpos = this_line_start_pos;
9045 tlendpos = this_line_end_pos;
9046 if (!consider_all_windows_p
9047 && CHARPOS (tlbufpos) > 0
9048 && NILP (w->update_mode_line)
73af359d 9049 && !current_buffer->clip_changed
c62c1bb5 9050 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 9051 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 9052 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 9053 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
9054 && this_line_buffer == current_buffer
9055 && current_buffer == XBUFFER (w->buffer)
265a9e55 9056 && NILP (w->force_start)
acda20e1 9057 && NILP (w->optional_new_start)
5f5c8ee5
GM
9058 /* Point must be on the line that we have info recorded about. */
9059 && PT >= CHARPOS (tlbufpos)
9060 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
9061 /* All text outside that line, including its final newline,
9062 must be unchanged */
5f5c8ee5
GM
9063 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9064 CHARPOS (tlendpos)))
9065 {
9066 if (CHARPOS (tlbufpos) > BEGV
9067 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9068 && (CHARPOS (tlbufpos) == ZV
9069 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
9070 /* Former continuation line has disappeared by becoming empty */
9071 goto cancel;
9072 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 9073 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
9074 || MINI_WINDOW_P (w))
9075 {
1c9241f5
KH
9076 /* We have to handle the case of continuation around a
9077 wide-column character (See the comment in indent.c around
9078 line 885).
9079
9080 For instance, in the following case:
9081
9082 -------- Insert --------
9083 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9084 J_I_ ==> J_I_ `^^' are cursors.
9085 ^^ ^^
9086 -------- --------
9087
9088 As we have to redraw the line above, we should goto cancel. */
9089
5f5c8ee5
GM
9090 struct it it;
9091 int line_height_before = this_line_pixel_height;
9092
9093 /* Note that start_display will handle the case that the
9094 line starting at tlbufpos is a continuation lines. */
9095 start_display (&it, w, tlbufpos);
9096
9097 /* Implementation note: It this still necessary? */
9098 if (it.current_x != this_line_start_x)
1c9241f5
KH
9099 goto cancel;
9100
5f5c8ee5
GM
9101 TRACE ((stderr, "trying display optimization 1\n"));
9102 w->cursor.vpos = -1;
a2889657 9103 overlay_arrow_seen = 0;
5f5c8ee5
GM
9104 it.vpos = this_line_vpos;
9105 it.current_y = this_line_y;
9106 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9107 display_line (&it);
9108
a2889657 9109 /* If line contains point, is not continued,
5f5c8ee5 9110 and ends at same distance from eob as before, we win */
2311178e 9111 if (w->cursor.vpos >= 0
5f5c8ee5
GM
9112 /* Line is not continued, otherwise this_line_start_pos
9113 would have been set to 0 in display_line. */
9114 && CHARPOS (this_line_start_pos)
9115 /* Line ends as before. */
9116 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9117 /* Line has same height as before. Otherwise other lines
9118 would have to be shifted up or down. */
9119 && this_line_pixel_height == line_height_before)
a2889657 9120 {
5f5c8ee5
GM
9121 /* If this is not the window's last line, we must adjust
9122 the charstarts of the lines below. */
9123 if (it.current_y < it.last_visible_y)
9124 {
9125 struct glyph_row *row
9126 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9127 int delta, delta_bytes;
2311178e 9128
5f5c8ee5
GM
9129 if (Z - CHARPOS (tlendpos) == ZV)
9130 {
9131 /* This line ends at end of (accessible part of)
9132 buffer. There is no newline to count. */
9133 delta = (Z
9134 - CHARPOS (tlendpos)
9135 - MATRIX_ROW_START_CHARPOS (row));
9136 delta_bytes = (Z_BYTE
9137 - BYTEPOS (tlendpos)
9138 - MATRIX_ROW_START_BYTEPOS (row));
9139 }
9140 else
9141 {
9142 /* This line ends in a newline. Must take
9143 account of the newline and the rest of the
9144 text that follows. */
9145 delta = (Z
9146 - CHARPOS (tlendpos)
9147 - MATRIX_ROW_START_CHARPOS (row));
9148 delta_bytes = (Z_BYTE
9149 - BYTEPOS (tlendpos)
9150 - MATRIX_ROW_START_BYTEPOS (row));
9151 }
2311178e 9152
f2d86d7a
GM
9153 increment_matrix_positions (w->current_matrix,
9154 this_line_vpos + 1,
9155 w->current_matrix->nrows,
9156 delta, delta_bytes);
85bcef6c 9157 }
46db8486 9158
5f5c8ee5
GM
9159 /* If this row displays text now but previously didn't,
9160 or vice versa, w->window_end_vpos may have to be
9161 adjusted. */
9162 if ((it.glyph_row - 1)->displays_text_p)
9163 {
9164 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9165 XSETINT (w->window_end_vpos, this_line_vpos);
9166 }
9167 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9168 && this_line_vpos > 0)
9169 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9170 w->window_end_valid = Qnil;
2311178e 9171
5f5c8ee5
GM
9172 /* Update hint: No need to try to scroll in update_window. */
9173 w->desired_matrix->no_scrolling_p = 1;
9174
9175#if GLYPH_DEBUG
9176 *w->desired_matrix->method = 0;
9177 debug_method_add (w, "optimization 1");
9178#endif
a2889657
JB
9179 goto update;
9180 }
9181 else
9182 goto cancel;
9183 }
5f5c8ee5
GM
9184 else if (/* Cursor position hasn't changed. */
9185 PT == XFASTINT (w->last_point)
b6f0fe04
RS
9186 /* Make sure the cursor was last displayed
9187 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
9188 && 0 <= w->cursor.vpos
9189 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
9190 {
9191 if (!must_finish)
9192 {
c6e89d6c 9193 do_pending_window_change (1);
5f5c8ee5 9194
2311178e 9195 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
9196 isn't enough if we have a blinking cursor. */
9197 if (w->cursor_off_p == w->last_cursor_off_p)
9198 goto end_of_redisplay;
a2889657
JB
9199 }
9200 goto update;
9201 }
8b51f1e3
KH
9202 /* If highlighting the region, or if the cursor is in the echo area,
9203 then we can't just move the cursor. */
bd66d1ba
RS
9204 else if (! (!NILP (Vtransient_mark_mode)
9205 && !NILP (current_buffer->mark_active))
97a36635 9206 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 9207 || highlight_nonselected_windows)
8b51f1e3 9208 && NILP (w->region_showing)
8f897821 9209 && NILP (Vshow_trailing_whitespace)
8b51f1e3 9210 && !cursor_in_echo_area)
a2889657 9211 {
5f5c8ee5
GM
9212 struct it it;
9213 struct glyph_row *row;
9214
9215 /* Skip from tlbufpos to PT and see where it is. Note that
9216 PT may be in invisible text. If so, we will end at the
9217 next visible position. */
9218 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9219 NULL, DEFAULT_FACE_ID);
9220 it.current_x = this_line_start_x;
9221 it.current_y = this_line_y;
9222 it.vpos = this_line_vpos;
2311178e 9223
5f5c8ee5
GM
9224 /* The call to move_it_to stops in front of PT, but
9225 moves over before-strings. */
9226 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9227
9228 if (it.vpos == this_line_vpos
9229 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9230 row->enabled_p))
a2889657 9231 {
5f5c8ee5
GM
9232 xassert (this_line_vpos == it.vpos);
9233 xassert (this_line_y == it.current_y);
9234 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
9235#if GLYPH_DEBUG
9236 *w->desired_matrix->method = 0;
9237 debug_method_add (w, "optimization 3");
9238#endif
a2889657
JB
9239 goto update;
9240 }
9241 else
9242 goto cancel;
9243 }
5f5c8ee5 9244
a2889657 9245 cancel:
5f5c8ee5
GM
9246 /* Text changed drastically or point moved off of line. */
9247 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
9248 }
9249
5f5c8ee5
GM
9250 CHARPOS (this_line_start_pos) = 0;
9251 consider_all_windows_p |= buffer_shared > 1;
9252 ++clear_face_cache_count;
a2889657 9253
2311178e 9254
bd9d0f3f
GM
9255 /* Build desired matrices, and update the display. If
9256 consider_all_windows_p is non-zero, do it for all windows on all
9257 frames. Otherwise do it for selected_window, only. */
463f6b91 9258
5f5c8ee5 9259 if (consider_all_windows_p)
a2889657 9260 {
35f56f96 9261 Lisp_Object tail, frame;
0528abe1
GM
9262 int i, n = 0, size = 50;
9263 struct frame **updated
9264 = (struct frame **) alloca (size * sizeof *updated);
a2889657 9265
5f5c8ee5
GM
9266 /* Clear the face cache eventually. */
9267 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 9268 {
5f5c8ee5 9269 clear_face_cache (0);
463f6b91
RS
9270 clear_face_cache_count = 0;
9271 }
31b24551 9272
5f5c8ee5
GM
9273 /* Recompute # windows showing selected buffer. This will be
9274 incremented each time such a window is displayed. */
a2889657
JB
9275 buffer_shared = 0;
9276
35f56f96 9277 FOR_EACH_FRAME (tail, frame)
30c566e4 9278 {
5f5c8ee5 9279 struct frame *f = XFRAME (frame);
2311178e 9280
886bd6f2 9281 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 9282 {
ae02e06a 9283#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
9284 if (clear_face_cache_count % 50 == 0
9285 && FRAME_WINDOW_P (f))
9286 clear_image_cache (f, 0);
ae02e06a 9287#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 9288
5f5c8ee5
GM
9289 /* Mark all the scroll bars to be removed; we'll redeem
9290 the ones we want when we redisplay their windows. */
9769686d 9291 if (condemn_scroll_bars_hook)
504454e8 9292 condemn_scroll_bars_hook (f);
30c566e4 9293
f21ef775 9294 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 9295 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 9296
5f5c8ee5
GM
9297 /* Any scroll bars which redisplay_windows should have
9298 nuked should now go away. */
9769686d 9299 if (judge_scroll_bars_hook)
504454e8 9300 judge_scroll_bars_hook (f);
bd9d0f3f
GM
9301
9302 /* If fonts changed, display again. */
b60c9653
RS
9303 /* ??? rms: I suspect it is a mistake to jump all the way
9304 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
9305 if (fonts_changed_p)
9306 goto retry;
2311178e 9307
bd9d0f3f
GM
9308 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9309 {
9310 /* See if we have to hscroll. */
9311 if (hscroll_windows (f->root_window))
9312 goto retry;
9313
9314 /* Prevent various kinds of signals during display
9315 update. stdio is not robust about handling
9316 signals, which can cause an apparent I/O
9317 error. */
9318 if (interrupt_input)
9319 unrequest_sigio ();
aac2d8b2 9320 STOP_POLLING;
bd9d0f3f
GM
9321
9322 /* Update the display. */
9323 set_window_update_flags (XWINDOW (f->root_window), 1);
9324 pause |= update_frame (f, 0, 0);
9325 if (pause)
9326 break;
9327
0528abe1
GM
9328 if (n == size)
9329 {
9330 int nbytes = size * sizeof *updated;
9331 struct frame **p = (struct frame **) alloca (2 * nbytes);
9332 bcopy (updated, p, nbytes);
9333 size *= 2;
9334 }
2311178e 9335
0528abe1 9336 updated[n++] = f;
bd9d0f3f 9337 }
9769686d 9338 }
30c566e4 9339 }
0528abe1
GM
9340
9341 /* Do the mark_window_display_accurate after all windows have
9342 been redisplayed because this call resets flags in buffers
9343 which are needed for proper redisplay. */
9344 for (i = 0; i < n; ++i)
9345 {
9346 struct frame *f = updated[i];
9347 mark_window_display_accurate (f->root_window, 1);
9348 if (frame_up_to_date_hook)
9349 frame_up_to_date_hook (f);
9350 }
a2889657 9351 }
bd9d0f3f
GM
9352 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9353 {
9354 Lisp_Object mini_window;
9355 struct frame *mini_frame;
5f5c8ee5 9356
82a7ab23 9357 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
9358 /* Use list_of_error, not Qerror, so that
9359 we catch only errors and don't run the debugger. */
9360 internal_condition_case_1 (redisplay_window_1, selected_window,
9361 list_of_error,
82a7ab23 9362 redisplay_window_error);
2311178e 9363
bd9d0f3f 9364 /* Compare desired and current matrices, perform output. */
7d0393cf 9365
26683087 9366 update:
bd9d0f3f
GM
9367 /* If fonts changed, display again. */
9368 if (fonts_changed_p)
92a90e89 9369 goto retry;
a2889657 9370
bd9d0f3f
GM
9371 /* Prevent various kinds of signals during display update.
9372 stdio is not robust about handling signals,
9373 which can cause an apparent I/O error. */
9374 if (interrupt_input)
9375 unrequest_sigio ();
aac2d8b2 9376 STOP_POLLING;
1af9f229 9377
bd9d0f3f 9378 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 9379 {
92a90e89
GM
9380 if (hscroll_windows (selected_window))
9381 goto retry;
2311178e 9382
5f5c8ee5 9383 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 9384 pause = update_frame (sf, 0, 0);
5f5c8ee5 9385 }
d724d989 9386
8de2d90b 9387 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
9388 function. If the echo area is on another frame, that may
9389 have put text on a frame other than the selected one, so the
9390 above call to update_frame would not have caught it. Catch
8de2d90b 9391 it here. */
bd9d0f3f
GM
9392 mini_window = FRAME_MINIBUF_WINDOW (sf);
9393 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 9394
bd9d0f3f
GM
9395 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9396 {
9397 XWINDOW (mini_window)->must_be_updated_p = 1;
9398 pause |= update_frame (mini_frame, 0, 0);
9399 if (!pause && hscroll_windows (mini_window))
9400 goto retry;
9401 }
6e8290aa 9402 }
a2889657 9403
5f5c8ee5
GM
9404 /* If display was paused because of pending input, make sure we do a
9405 thorough update the next time. */
a2889657
JB
9406 if (pause)
9407 {
5f5c8ee5
GM
9408 /* Prevent the optimization at the beginning of
9409 redisplay_internal that tries a single-line update of the
9410 line containing the cursor in the selected window. */
9411 CHARPOS (this_line_start_pos) = 0;
9412
9413 /* Let the overlay arrow be updated the next time. */
265a9e55 9414 if (!NILP (last_arrow_position))
a2889657
JB
9415 {
9416 last_arrow_position = Qt;
9417 last_arrow_string = Qt;
9418 }
2311178e 9419
5f5c8ee5
GM
9420 /* If we pause after scrolling, some rows in the current
9421 matrices of some windows are not valid. */
9422 if (!WINDOW_FULL_WIDTH_P (w)
9423 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
9424 update_mode_lines = 1;
9425 }
43c09969 9426 else
a2889657 9427 {
43c09969 9428 if (!consider_all_windows_p)
a2889657 9429 {
43c09969
GM
9430 /* This has already been done above if
9431 consider_all_windows_p is set. */
9432 mark_window_display_accurate_1 (w, 1);
2311178e 9433
927c5b3b
GM
9434 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9435 last_arrow_string = Voverlay_arrow_string;
2311178e 9436
efc63ef0 9437 if (frame_up_to_date_hook != 0)
43c09969 9438 frame_up_to_date_hook (sf);
a2889657 9439 }
15e26c76 9440
a2889657
JB
9441 update_mode_lines = 0;
9442 windows_or_buffers_changed = 0;
5fb96e96 9443 cursor_type_changed = 0;
a2889657
JB
9444 }
9445
5f5c8ee5
GM
9446 /* Start SIGIO interrupts coming again. Having them off during the
9447 code above makes it less likely one will discard output, but not
9448 impossible, since there might be stuff in the system buffer here.
a2889657 9449 But it is much hairier to try to do anything about that. */
a2889657
JB
9450 if (interrupt_input)
9451 request_sigio ();
aac2d8b2 9452 RESUME_POLLING;
a2889657 9453
5f5c8ee5
GM
9454 /* If a frame has become visible which was not before, redisplay
9455 again, so that we display it. Expose events for such a frame
9456 (which it gets when becoming visible) don't call the parts of
9457 redisplay constructing glyphs, so simply exposing a frame won't
9458 display anything in this case. So, we have to display these
9459 frames here explicitly. */
11c52c4f
RS
9460 if (!pause)
9461 {
9462 Lisp_Object tail, frame;
9463 int new_count = 0;
9464
9465 FOR_EACH_FRAME (tail, frame)
9466 {
9467 int this_is_visible = 0;
8e83f802
RS
9468
9469 if (XFRAME (frame)->visible)
9470 this_is_visible = 1;
9471 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9472 if (XFRAME (frame)->visible)
9473 this_is_visible = 1;
11c52c4f
RS
9474
9475 if (this_is_visible)
9476 new_count++;
9477 }
9478
89819bdd 9479 if (new_count != number_of_visible_frames)
11c52c4f
RS
9480 windows_or_buffers_changed++;
9481 }
9482
44fa5b1e 9483 /* Change frame size now if a change is pending. */
c6e89d6c 9484 do_pending_window_change (1);
d8e242fd 9485
8b32d885
RS
9486 /* If we just did a pending size change, or have additional
9487 visible frames, redisplay again. */
3c8c72e0 9488 if (windows_or_buffers_changed && !pause)
8b32d885 9489 goto retry;
5f5c8ee5 9490
1987b083 9491 end_of_redisplay:
28514cd9 9492 unbind_to (count, Qnil);
aac2d8b2 9493 RESUME_POLLING;
a2889657
JB
9494}
9495
5f5c8ee5
GM
9496
9497/* Redisplay, but leave alone any recent echo area message unless
9498 another message has been requested in its place.
a2889657
JB
9499
9500 This is useful in situations where you need to redisplay but no
9501 user action has occurred, making it inappropriate for the message
9502 area to be cleared. See tracking_off and
9bf76936
GM
9503 wait_reading_process_input for examples of these situations.
9504
9505 FROM_WHERE is an integer saying from where this function was
9506 called. This is useful for debugging. */
a2889657 9507
8991bb31 9508void
9bf76936
GM
9509redisplay_preserve_echo_area (from_where)
9510 int from_where;
a2889657 9511{
9bf76936
GM
9512 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9513
c6e89d6c 9514 if (!NILP (echo_area_buffer[1]))
a2889657 9515 {
c6e89d6c
GM
9516 /* We have a previously displayed message, but no current
9517 message. Redisplay the previous message. */
9518 display_last_displayed_message_p = 1;
e9874cee 9519 redisplay_internal (1);
c6e89d6c 9520 display_last_displayed_message_p = 0;
a2889657
JB
9521 }
9522 else
e9874cee 9523 redisplay_internal (1);
a2889657
JB
9524}
9525
5f5c8ee5 9526
28514cd9 9527/* Function registered with record_unwind_protect in
acfca545
RS
9528 redisplay_internal. Reset redisplaying_p to the value it had
9529 before redisplay_internal was called, and clear
26683087 9530 prevent_freeing_realized_faces_p. */
28514cd9
GM
9531
9532static Lisp_Object
9533unwind_redisplay (old_redisplaying_p)
9534 Lisp_Object old_redisplaying_p;
9535{
9536 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 9537 return Qnil;
28514cd9
GM
9538}
9539
9540
43c09969
GM
9541/* Mark the display of window W as accurate or inaccurate. If
9542 ACCURATE_P is non-zero mark display of W as accurate. If
9543 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9544 redisplay_internal is called. */
5f5c8ee5 9545
43c09969
GM
9546static void
9547mark_window_display_accurate_1 (w, accurate_p)
9548 struct window *w;
5f5c8ee5 9549 int accurate_p;
a2889657 9550{
43c09969 9551 if (BUFFERP (w->buffer))
a2889657 9552 {
43c09969 9553 struct buffer *b = XBUFFER (w->buffer);
2311178e 9554
43c09969
GM
9555 w->last_modified
9556 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9557 w->last_overlay_modified
9558 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9559 w->last_had_star
9560 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 9561
43c09969 9562 if (accurate_p)
bd66d1ba 9563 {
43c09969
GM
9564 b->clip_changed = 0;
9565 b->prevent_redisplay_optimizations_p = 0;
9566
9567 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9568 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9569 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9570 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 9571
43c09969
GM
9572 w->current_matrix->buffer = b;
9573 w->current_matrix->begv = BUF_BEGV (b);
9574 w->current_matrix->zv = BUF_ZV (b);
2311178e 9575
43c09969
GM
9576 w->last_cursor = w->cursor;
9577 w->last_cursor_off_p = w->cursor_off_p;
2311178e 9578
43c09969
GM
9579 if (w == XWINDOW (selected_window))
9580 w->last_point = make_number (BUF_PT (b));
9581 else
9582 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 9583 }
43c09969 9584 }
bd66d1ba 9585
43c09969
GM
9586 if (accurate_p)
9587 {
d2f84654 9588 w->window_end_valid = w->buffer;
99332eb6 9589#if 0 /* This is incorrect with variable-height lines. */
2913a9c0
GM
9590 xassert (XINT (w->window_end_vpos)
9591 < (XINT (w->height)
9592 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 9593#endif
a2889657 9594 w->update_mode_line = Qnil;
43c09969
GM
9595 }
9596}
9597
9598
9599/* Mark the display of windows in the window tree rooted at WINDOW as
9600 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9601 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9602 be redisplayed the next time redisplay_internal is called. */
9603
9604void
9605mark_window_display_accurate (window, accurate_p)
9606 Lisp_Object window;
9607 int accurate_p;
9608{
9609 struct window *w;
2311178e 9610
43c09969
GM
9611 for (; !NILP (window); window = w->next)
9612 {
9613 w = XWINDOW (window);
9614 mark_window_display_accurate_1 (w, accurate_p);
a2889657 9615
265a9e55 9616 if (!NILP (w->vchild))
5f5c8ee5 9617 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 9618 if (!NILP (w->hchild))
5f5c8ee5 9619 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
9620 }
9621
5f5c8ee5 9622 if (accurate_p)
a2889657 9623 {
d45de95b 9624 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
9625 last_arrow_string = Voverlay_arrow_string;
9626 }
9627 else
9628 {
5f5c8ee5
GM
9629 /* Force a thorough redisplay the next time by setting
9630 last_arrow_position and last_arrow_string to t, which is
4b41cebb 9631 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
9632 last_arrow_position = Qt;
9633 last_arrow_string = Qt;
9634 }
9635}
5f5c8ee5
GM
9636
9637
9638/* Return value in display table DP (Lisp_Char_Table *) for character
9639 C. Since a display table doesn't have any parent, we don't have to
9640 follow parent. Do not call this function directly but use the
9641 macro DISP_CHAR_VECTOR. */
9642
9643Lisp_Object
9644disp_char_vector (dp, c)
9645 struct Lisp_Char_Table *dp;
9646 int c;
9647{
9648 int code[4], i;
9649 Lisp_Object val;
9650
9651 if (SINGLE_BYTE_CHAR_P (c))
9652 return (dp->contents[c]);
2311178e 9653
c5924f47 9654 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
9655 if (code[1] < 32)
9656 code[1] = -1;
9657 else if (code[2] < 32)
9658 code[2] = -1;
2311178e 9659
5f5c8ee5
GM
9660 /* Here, the possible range of code[0] (== charset ID) is
9661 128..max_charset. Since the top level char table contains data
9662 for multibyte characters after 256th element, we must increment
9663 code[0] by 128 to get a correct index. */
9664 code[0] += 128;
9665 code[3] = -1; /* anchor */
9666
9667 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9668 {
9669 val = dp->contents[code[i]];
9670 if (!SUB_CHAR_TABLE_P (val))
9671 return (NILP (val) ? dp->defalt : val);
9672 }
2311178e 9673
5f5c8ee5
GM
9674 /* Here, val is a sub char table. We return the default value of
9675 it. */
9676 return (dp->defalt);
9677}
9678
9679
a2889657 9680\f
5f5c8ee5
GM
9681/***********************************************************************
9682 Window Redisplay
9683 ***********************************************************************/
a2725ab2 9684
5f5c8ee5 9685/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
9686
9687static void
5f5c8ee5
GM
9688redisplay_windows (window)
9689 Lisp_Object window;
90adcf20 9690{
5f5c8ee5
GM
9691 while (!NILP (window))
9692 {
9693 struct window *w = XWINDOW (window);
2311178e 9694
5f5c8ee5
GM
9695 if (!NILP (w->hchild))
9696 redisplay_windows (w->hchild);
9697 else if (!NILP (w->vchild))
9698 redisplay_windows (w->vchild);
9699 else
82a7ab23
RS
9700 {
9701 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
9702 /* Use list_of_error, not Qerror, so that
9703 we catch only errors and don't run the debugger. */
2311178e 9704 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 9705 list_of_error,
82a7ab23
RS
9706 redisplay_window_error);
9707 }
a2725ab2 9708
5f5c8ee5
GM
9709 window = w->next;
9710 }
9711}
9712
82a7ab23
RS
9713static Lisp_Object
9714redisplay_window_error ()
9715{
9716 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9717 return Qnil;
9718}
9719
9720static Lisp_Object
9721redisplay_window_0 (window)
9722 Lisp_Object window;
9723{
9724 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9725 redisplay_window (window, 0);
9726 return Qnil;
9727}
5f5c8ee5 9728
82a7ab23
RS
9729static Lisp_Object
9730redisplay_window_1 (window)
9731 Lisp_Object window;
9732{
9733 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9734 redisplay_window (window, 1);
9735 return Qnil;
9736}
9737\f
bc6371a6
KH
9738
9739/* Increment GLYPH until it reaches END or CONDITION fails while
9740 adding (GLYPH)->pixel_width to X. */
9741
9742#define SKIP_GLYPHS(glyph, end, x, condition) \
9743 do \
9744 { \
9745 (x) += (glyph)->pixel_width; \
9746 ++(glyph); \
9747 } \
9748 while ((glyph) < (end) && (condition))
9749
9750
5f5c8ee5
GM
9751/* Set cursor position of W. PT is assumed to be displayed in ROW.
9752 DELTA is the number of bytes by which positions recorded in ROW
9753 differ from current buffer positions. */
9754
9755void
9756set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9757 struct window *w;
9758 struct glyph_row *row;
9759 struct glyph_matrix *matrix;
9760 int delta, delta_bytes, dy, dvpos;
9761{
9762 struct glyph *glyph = row->glyphs[TEXT_AREA];
9763 struct glyph *end = glyph + row->used[TEXT_AREA];
ae73dc1c
KH
9764 /* The first glyph that starts a sequence of glyphs from string. */
9765 struct glyph *string_start;
9766 /* The X coordinate of string_start. */
9767 int string_start_x;
9768 /* The last known character position. */
9769 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
9770 /* The last known character position before string_start. */
9771 int string_before_pos;
5f5c8ee5
GM
9772 int x = row->x;
9773 int pt_old = PT - delta;
9774
9775 /* Skip over glyphs not having an object at the start of the row.
9776 These are special glyphs like truncation marks on terminal
9777 frames. */
9778 if (row->displays_text_p)
9779 while (glyph < end
6fc556fd 9780 && INTEGERP (glyph->object)
5f5c8ee5
GM
9781 && glyph->charpos < 0)
9782 {
9783 x += glyph->pixel_width;
9784 ++glyph;
9785 }
9786
ae73dc1c 9787 string_start = NULL;
5f5c8ee5 9788 while (glyph < end
6fc556fd 9789 && !INTEGERP (glyph->object)
5f5c8ee5 9790 && (!BUFFERP (glyph->object)
ae73dc1c 9791 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 9792 {
ae73dc1c
KH
9793 if (! STRINGP (glyph->object))
9794 {
9795 string_start = NULL;
9796 x += glyph->pixel_width;
9797 ++glyph;
9798 }
9799 else
9800 {
9801 string_before_pos = last_pos;
9802 string_start = glyph;
9803 string_start_x = x;
9804 /* Skip all glyphs from string. */
bc6371a6 9805 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
ae73dc1c
KH
9806 }
9807 }
9808
9809 if (string_start
9810 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
9811 {
9812 /* We may have skipped over point because the previous glyphs
9813 are from string. As there's no easy way to know the
9814 character position of the current glyph, find the correct
9815 glyph on point by scanning from string_start again. */
bc6371a6
KH
9816 Lisp_Object limit;
9817 Lisp_Object string;
9818 int pos;
ae73dc1c 9819
bc6371a6
KH
9820 limit = make_number (pt_old + 1);
9821 end = glyph;
ae73dc1c
KH
9822 glyph = string_start;
9823 x = string_start_x;
bc6371a6
KH
9824 string = glyph->object;
9825 pos = string_buffer_position (w, string, string_before_pos);
9826 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
9827 because we always put cursor after overlay strings. */
9828 while (pos == 0 && glyph < end)
ae73dc1c 9829 {
bc6371a6
KH
9830 string = glyph->object;
9831 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9832 if (glyph < end)
9833 pos = string_buffer_position (w, glyph->object, string_before_pos);
9834 }
9835
9836 while (glyph < end)
9837 {
9838 pos = XINT (Fnext_single_char_property_change
9839 (make_number (pos), Qdisplay, Qnil, limit));
9840 if (pos > pt_old)
9841 break;
ae73dc1c 9842 /* Skip glyphs from the same string. */
bc6371a6
KH
9843 string = glyph->object;
9844 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9845 /* Skip glyphs from an overlay. */
9846 while (glyph < end
9847 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 9848 {
bc6371a6
KH
9849 string = glyph->object;
9850 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 9851 }
ae73dc1c 9852 }
5f5c8ee5
GM
9853 }
9854
9855 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9856 w->cursor.x = x;
9857 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9858 w->cursor.y = row->y + dy;
9859
9860 if (w == XWINDOW (selected_window))
9861 {
9862 if (!row->continued_p
9863 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9864 && row->x == 0)
9865 {
9866 this_line_buffer = XBUFFER (w->buffer);
2311178e 9867
5f5c8ee5
GM
9868 CHARPOS (this_line_start_pos)
9869 = MATRIX_ROW_START_CHARPOS (row) + delta;
9870 BYTEPOS (this_line_start_pos)
9871 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 9872
5f5c8ee5
GM
9873 CHARPOS (this_line_end_pos)
9874 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9875 BYTEPOS (this_line_end_pos)
9876 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 9877
5f5c8ee5
GM
9878 this_line_y = w->cursor.y;
9879 this_line_pixel_height = row->height;
9880 this_line_vpos = w->cursor.vpos;
9881 this_line_start_x = row->x;
9882 }
9883 else
9884 CHARPOS (this_line_start_pos) = 0;
9885 }
9886}
9887
9888
9889/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
9890 start STARTP. Sets the window start of WINDOW to that position.
9891
9892 We assume that the window's buffer is really current. */
5f5c8ee5
GM
9893
9894static INLINE struct text_pos
9895run_window_scroll_functions (window, startp)
9896 Lisp_Object window;
9897 struct text_pos startp;
9898{
9899 struct window *w = XWINDOW (window);
9900 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
9901
9902 if (current_buffer != XBUFFER (w->buffer))
9903 abort ();
9904
5f5c8ee5
GM
9905 if (!NILP (Vwindow_scroll_functions))
9906 {
2311178e 9907 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
9908 make_number (CHARPOS (startp)));
9909 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
9910 /* In case the hook functions switch buffers. */
9911 if (current_buffer != XBUFFER (w->buffer))
9912 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 9913 }
90adcf20 9914
5f5c8ee5
GM
9915 return startp;
9916}
9917
9918
ddf6b9a3
RS
9919/* Make sure the line containing the cursor is fully visible.
9920 A value of 1 means there is nothing to be done.
9921 (Either the line is fully visible, or it cannot be made so,
9922 or we cannot tell.)
9923 A value of 0 means the caller should do scrolling
9924 as if point had gone off the screen. */
5f5c8ee5 9925
cb617e7c 9926static int
5f5c8ee5
GM
9927make_cursor_line_fully_visible (w)
9928 struct window *w;
9929{
9930 struct glyph_matrix *matrix;
9931 struct glyph_row *row;
478d746b 9932 int window_height;
2311178e 9933
5f5c8ee5
GM
9934 /* It's not always possible to find the cursor, e.g, when a window
9935 is full of overlay strings. Don't do anything in that case. */
9936 if (w->cursor.vpos < 0)
cb617e7c 9937 return 1;
2311178e 9938
5f5c8ee5
GM
9939 matrix = w->desired_matrix;
9940 row = MATRIX_ROW (matrix, w->cursor.vpos);
9941
acda20e1 9942 /* If the cursor row is not partially visible, there's nothing to do. */
b28cb6ed 9943 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 9944 return 1;
b28cb6ed
GM
9945
9946 /* If the row the cursor is in is taller than the window's height,
9947 it's not clear what to do, so do nothing. */
9948 window_height = window_box_height (w);
9949 if (row->height >= window_height)
cb617e7c 9950 return 1;
b28cb6ed 9951
ddf6b9a3
RS
9952 return 0;
9953
9954#if 0
9955 /* This code used to try to scroll the window just enough to make
9956 the line visible. It returned 0 to say that the caller should
9957 allocate larger glyph matrices. */
9958
b28cb6ed 9959 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
9960 {
9961 int dy = row->height - row->visible_height;
9962 w->vscroll = 0;
9963 w->cursor.y += dy;
9964 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9965 }
b28cb6ed 9966 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
9967 {
9968 int dy = - (row->height - row->visible_height);
9969 w->vscroll = dy;
9970 w->cursor.y += dy;
9971 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9972 }
2311178e 9973
5f5c8ee5
GM
9974 /* When we change the cursor y-position of the selected window,
9975 change this_line_y as well so that the display optimization for
9976 the cursor line of the selected window in redisplay_internal uses
9977 the correct y-position. */
9978 if (w == XWINDOW (selected_window))
9979 this_line_y = w->cursor.y;
cb617e7c
GM
9980
9981 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9982 redisplay with larger matrices. */
9983 if (matrix->nrows < required_matrix_height (w))
9984 {
9985 fonts_changed_p = 1;
9986 return 0;
9987 }
9988
9989 return 1;
ddf6b9a3 9990#endif /* 0 */
5f5c8ee5
GM
9991}
9992
9993
9994/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9995 non-zero means only WINDOW is redisplayed in redisplay_internal.
9996 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9997 in redisplay_window to bring a partially visible line into view in
9998 the case that only the cursor has moved.
9999
03b0a4b4
RS
10000 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10001 last screen line's vertical height extends past the end of the screen.
10002
5f5c8ee5
GM
10003 Value is
10004
10005 1 if scrolling succeeded
2311178e 10006
5f5c8ee5 10007 0 if scrolling didn't find point.
2311178e 10008
5f5c8ee5
GM
10009 -1 if new fonts have been loaded so that we must interrupt
10010 redisplay, adjust glyph matrices, and try again. */
10011
cb617e7c
GM
10012enum
10013{
10014 SCROLLING_SUCCESS,
10015 SCROLLING_FAILED,
10016 SCROLLING_NEED_LARGER_MATRICES
10017};
10018
5f5c8ee5
GM
10019static int
10020try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 10021 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
10022 Lisp_Object window;
10023 int just_this_one_p;
31ade731 10024 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 10025 int temp_scroll_step;
03b0a4b4 10026 int last_line_misfit;
5f5c8ee5
GM
10027{
10028 struct window *w = XWINDOW (window);
10029 struct frame *f = XFRAME (w->frame);
10030 struct text_pos scroll_margin_pos;
10031 struct text_pos pos;
10032 struct text_pos startp;
10033 struct it it;
10034 Lisp_Object window_end;
10035 int this_scroll_margin;
10036 int dy = 0;
10037 int scroll_max;
b8a63ccb 10038 int rc;
5f5c8ee5
GM
10039 int amount_to_scroll = 0;
10040 Lisp_Object aggressive;
10041 int height;
03b0a4b4 10042 int end_scroll_margin;
5f5c8ee5
GM
10043
10044#if GLYPH_DEBUG
10045 debug_method_add (w, "try_scrolling");
78614721 10046#endif
5f5c8ee5
GM
10047
10048 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 10049
5f5c8ee5
GM
10050 /* Compute scroll margin height in pixels. We scroll when point is
10051 within this distance from the top or bottom of the window. */
10052 if (scroll_margin > 0)
90adcf20 10053 {
5f5c8ee5
GM
10054 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
10055 this_scroll_margin *= CANON_Y_UNIT (f);
10056 }
10057 else
10058 this_scroll_margin = 0;
10059
10060 /* Compute how much we should try to scroll maximally to bring point
10061 into view. */
0894e696
SM
10062 if (scroll_step || scroll_conservatively || temp_scroll_step)
10063 scroll_max = max (scroll_step,
10064 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
10065 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10066 || NUMBERP (current_buffer->scroll_up_aggressively))
10067 /* We're trying to scroll because of aggressive scrolling
10068 but no scroll_step is set. Choose an arbitrary one. Maybe
10069 there should be a variable for this. */
10070 scroll_max = 10;
10071 else
10072 scroll_max = 0;
10073 scroll_max *= CANON_Y_UNIT (f);
10074
10075 /* Decide whether we have to scroll down. Start at the window end
10076 and move this_scroll_margin up to find the position of the scroll
10077 margin. */
10078 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
10079
10080 too_near_end:
10081
5f5c8ee5
GM
10082 CHARPOS (scroll_margin_pos) = XINT (window_end);
10083 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4
RS
10084
10085 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10086 if (end_scroll_margin)
5f5c8ee5
GM
10087 {
10088 start_display (&it, w, scroll_margin_pos);
03b0a4b4 10089 move_it_vertically (&it, - end_scroll_margin);
5f5c8ee5
GM
10090 scroll_margin_pos = it.current.pos;
10091 }
10092
10093 if (PT >= CHARPOS (scroll_margin_pos))
10094 {
10095 int y0;
2311178e 10096
5f5c8ee5
GM
10097 /* Point is in the scroll margin at the bottom of the window, or
10098 below. Compute a new window start that makes point visible. */
47589c8c 10099
5f5c8ee5
GM
10100 /* Compute the distance from the scroll margin to PT.
10101 Give up if the distance is greater than scroll_max. */
10102 start_display (&it, w, scroll_margin_pos);
10103 y0 = it.current_y;
10104 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10105 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
10106
10107 /* To make point visible, we have to move the window start
10108 down so that the line the cursor is in is visible, which
10109 means we have to add in the height of the cursor line. */
10110 dy = line_bottom_y (&it) - y0;
2311178e 10111
5f5c8ee5 10112 if (dy > scroll_max)
cb617e7c 10113 return SCROLLING_FAILED;
2311178e 10114
5f5c8ee5
GM
10115 /* Move the window start down. If scrolling conservatively,
10116 move it just enough down to make point visible. If
10117 scroll_step is set, move it down by scroll_step. */
10118 start_display (&it, w, startp);
10119
10120 if (scroll_conservatively)
03b0a4b4
RS
10121 /* Set AMOUNT_TO_SCROLL to at least one line,
10122 and at most scroll_conservatively lines. */
e89aaabd 10123 amount_to_scroll
03b0a4b4
RS
10124 = min (max (dy, CANON_Y_UNIT (f)),
10125 CANON_Y_UNIT (f) * scroll_conservatively);
5f5c8ee5
GM
10126 else if (scroll_step || temp_scroll_step)
10127 amount_to_scroll = scroll_max;
10128 else
90adcf20 10129 {
46226c1d 10130 aggressive = current_buffer->scroll_up_aggressively;
5f5c8ee5 10131 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 10132 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
10133 if (NUMBERP (aggressive))
10134 amount_to_scroll = XFLOATINT (aggressive) * height;
10135 }
a2725ab2 10136
5f5c8ee5 10137 if (amount_to_scroll <= 0)
cb617e7c 10138 return SCROLLING_FAILED;
a2725ab2 10139
03b0a4b4
RS
10140 /* If moving by amount_to_scroll leaves STARTP unchanged,
10141 move it down one screen line. */
10142
5f5c8ee5 10143 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
10144 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10145 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
10146 startp = it.current.pos;
10147 }
10148 else
10149 {
10150 /* See if point is inside the scroll margin at the top of the
10151 window. */
10152 scroll_margin_pos = startp;
10153 if (this_scroll_margin)
10154 {
10155 start_display (&it, w, startp);
10156 move_it_vertically (&it, this_scroll_margin);
10157 scroll_margin_pos = it.current.pos;
10158 }
10159
10160 if (PT < CHARPOS (scroll_margin_pos))
10161 {
10162 /* Point is in the scroll margin at the top of the window or
10163 above what is displayed in the window. */
10164 int y0;
2311178e 10165
5f5c8ee5
GM
10166 /* Compute the vertical distance from PT to the scroll
10167 margin position. Give up if distance is greater than
10168 scroll_max. */
10169 SET_TEXT_POS (pos, PT, PT_BYTE);
10170 start_display (&it, w, pos);
10171 y0 = it.current_y;
10172 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10173 it.last_visible_y, -1,
10174 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10175 dy = it.current_y - y0;
10176 if (dy > scroll_max)
cb617e7c 10177 return SCROLLING_FAILED;
2311178e 10178
5f5c8ee5
GM
10179 /* Compute new window start. */
10180 start_display (&it, w, startp);
2311178e 10181
5f5c8ee5 10182 if (scroll_conservatively)
0894e696
SM
10183 amount_to_scroll =
10184 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
10185 else if (scroll_step || temp_scroll_step)
10186 amount_to_scroll = scroll_max;
538f13d4 10187 else
5f5c8ee5 10188 {
46226c1d 10189 aggressive = current_buffer->scroll_down_aggressively;
5f5c8ee5 10190 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 10191 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
10192 if (NUMBERP (aggressive))
10193 amount_to_scroll = XFLOATINT (aggressive) * height;
10194 }
a2725ab2 10195
5f5c8ee5 10196 if (amount_to_scroll <= 0)
cb617e7c 10197 return SCROLLING_FAILED;
2311178e 10198
5f5c8ee5
GM
10199 move_it_vertically (&it, - amount_to_scroll);
10200 startp = it.current.pos;
90adcf20
RS
10201 }
10202 }
a2889657 10203
5f5c8ee5
GM
10204 /* Run window scroll functions. */
10205 startp = run_window_scroll_functions (window, startp);
90adcf20 10206
5f5c8ee5
GM
10207 /* Display the window. Give up if new fonts are loaded, or if point
10208 doesn't appear. */
10209 if (!try_window (window, startp))
cb617e7c 10210 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
10211 else if (w->cursor.vpos < 0)
10212 {
10213 clear_glyph_matrix (w->desired_matrix);
cb617e7c 10214 rc = SCROLLING_FAILED;
5f5c8ee5
GM
10215 }
10216 else
10217 {
10218 /* Maybe forget recorded base line for line number display. */
2311178e 10219 if (!just_this_one_p
5f5c8ee5 10220 || current_buffer->clip_changed
9142dd5b 10221 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 10222 w->base_line_number = Qnil;
2311178e 10223
ddf6b9a3
RS
10224 /* If cursor ends up on a partially visible line,
10225 treat that as being off the bottom of the screen. */
10226 if (! make_cursor_line_fully_visible (w))
ef3c2c73
RS
10227 {
10228 clear_glyph_matrix (w->desired_matrix);
03b0a4b4 10229 last_line_misfit = 1;
ef3c2c73
RS
10230 goto too_near_end;
10231 }
ddf6b9a3 10232 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
10233 }
10234
10235 return rc;
a2889657
JB
10236}
10237
5f5c8ee5
GM
10238
10239/* Compute a suitable window start for window W if display of W starts
10240 on a continuation line. Value is non-zero if a new window start
10241 was computed.
10242
10243 The new window start will be computed, based on W's width, starting
10244 from the start of the continued line. It is the start of the
10245 screen line with the minimum distance from the old start W->start. */
10246
10247static int
10248compute_window_start_on_continuation_line (w)
10249 struct window *w;
1f1ff51d 10250{
5f5c8ee5
GM
10251 struct text_pos pos, start_pos;
10252 int window_start_changed_p = 0;
1f1ff51d 10253
5f5c8ee5 10254 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 10255
5f5c8ee5 10256 /* If window start is on a continuation line... Window start may be
2311178e 10257 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
10258 buffer (M-x rmail, for example). */
10259 if (CHARPOS (start_pos) > BEGV
10260 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 10261 {
5f5c8ee5
GM
10262 struct it it;
10263 struct glyph_row *row;
f3751a65
GM
10264
10265 /* Handle the case that the window start is out of range. */
10266 if (CHARPOS (start_pos) < BEGV)
10267 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10268 else if (CHARPOS (start_pos) > ZV)
10269 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 10270
5f5c8ee5
GM
10271 /* Find the start of the continued line. This should be fast
10272 because scan_buffer is fast (newline cache). */
045dee35 10273 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
10274 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10275 row, DEFAULT_FACE_ID);
10276 reseat_at_previous_visible_line_start (&it);
10277
10278 /* If the line start is "too far" away from the window start,
10279 say it takes too much time to compute a new window start. */
10280 if (CHARPOS (start_pos) - IT_CHARPOS (it)
10281 < XFASTINT (w->height) * XFASTINT (w->width))
10282 {
10283 int min_distance, distance;
2311178e 10284
5f5c8ee5
GM
10285 /* Move forward by display lines to find the new window
10286 start. If window width was enlarged, the new start can
10287 be expected to be > the old start. If window width was
10288 decreased, the new window start will be < the old start.
10289 So, we're looking for the display line start with the
10290 minimum distance from the old window start. */
10291 pos = it.current.pos;
10292 min_distance = INFINITY;
10293 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10294 distance < min_distance)
10295 {
10296 min_distance = distance;
10297 pos = it.current.pos;
10298 move_it_by_lines (&it, 1, 0);
10299 }
2311178e 10300
5f5c8ee5
GM
10301 /* Set the window start there. */
10302 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10303 window_start_changed_p = 1;
10304 }
1f1ff51d 10305 }
2311178e 10306
5f5c8ee5 10307 return window_start_changed_p;
1f1ff51d
KH
10308}
10309
5f5c8ee5 10310
1dd5768b 10311/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
10312 with window start STARTP. Value is
10313
cb617e7c 10314 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 10315
cb617e7c
GM
10316 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
10317
10318 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
10319 display. *SCROLL_STEP is set to 1, under certain circumstances, if
10320 we want to scroll as if scroll-step were set to 1. See the code.
10321
10322 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
10323 which case we have to abort this redisplay, and adjust matrices
10324 first. */
10325
2311178e 10326enum
cb617e7c
GM
10327{
10328 CURSOR_MOVEMENT_SUCCESS,
10329 CURSOR_MOVEMENT_CANNOT_BE_USED,
10330 CURSOR_MOVEMENT_MUST_SCROLL,
10331 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
10332};
47589c8c
GM
10333
10334static int
10335try_cursor_movement (window, startp, scroll_step)
10336 Lisp_Object window;
10337 struct text_pos startp;
10338 int *scroll_step;
10339{
10340 struct window *w = XWINDOW (window);
10341 struct frame *f = XFRAME (w->frame);
cb617e7c 10342 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 10343
69d1f7c9 10344#if GLYPH_DEBUG
76cb5e06
GM
10345 if (inhibit_try_cursor_movement)
10346 return rc;
10347#endif
10348
47589c8c
GM
10349 /* Handle case where text has not changed, only point, and it has
10350 not moved off the frame. */
10351 if (/* Point may be in this window. */
10352 PT >= CHARPOS (startp)
47589c8c
GM
10353 /* Selective display hasn't changed. */
10354 && !current_buffer->clip_changed
4db87380
GM
10355 /* Function force-mode-line-update is used to force a thorough
10356 redisplay. It sets either windows_or_buffers_changed or
10357 update_mode_lines. So don't take a shortcut here for these
10358 cases. */
10359 && !update_mode_lines
10360 && !windows_or_buffers_changed
5fb96e96 10361 && !cursor_type_changed
2311178e 10362 /* Can't use this case if highlighting a region. When a
47589c8c
GM
10363 region exists, cursor movement has to do more than just
10364 set the cursor. */
10365 && !(!NILP (Vtransient_mark_mode)
10366 && !NILP (current_buffer->mark_active))
10367 && NILP (w->region_showing)
10368 && NILP (Vshow_trailing_whitespace)
10369 /* Right after splitting windows, last_point may be nil. */
10370 && INTEGERP (w->last_point)
10371 /* This code is not used for mini-buffer for the sake of the case
10372 of redisplaying to replace an echo area message; since in
10373 that case the mini-buffer contents per se are usually
10374 unchanged. This code is of no real use in the mini-buffer
10375 since the handling of this_line_start_pos, etc., in redisplay
10376 handles the same cases. */
10377 && !EQ (window, minibuf_window)
10378 /* When splitting windows or for new windows, it happens that
10379 redisplay is called with a nil window_end_vpos or one being
10380 larger than the window. This should really be fixed in
10381 window.c. I don't have this on my list, now, so we do
10382 approximately the same as the old redisplay code. --gerd. */
10383 && INTEGERP (w->window_end_vpos)
10384 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
10385 && (FRAME_WINDOW_P (f)
10386 || !MARKERP (Voverlay_arrow_position)
10387 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
10388 {
10389 int this_scroll_margin;
8ee5b6a3 10390 struct glyph_row *row = NULL;
47589c8c
GM
10391
10392#if GLYPH_DEBUG
10393 debug_method_add (w, "cursor movement");
10394#endif
10395
10396 /* Scroll if point within this distance from the top or bottom
10397 of the window. This is a pixel value. */
10398 this_scroll_margin = max (0, scroll_margin);
10399 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
10400 this_scroll_margin *= CANON_Y_UNIT (f);
10401
10402 /* Start with the row the cursor was displayed during the last
10403 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
10404 if (w->last_cursor.vpos < 0
10405 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 10406 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10407 else
10408 {
10409 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10410 if (row->mode_line_p)
10411 ++row;
10412 if (!row->enabled_p)
cb617e7c 10413 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10414 }
10415
cb617e7c 10416 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
10417 {
10418 int scroll_p = 0;
68c5d1db 10419 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 10420
47589c8c
GM
10421 if (PT > XFASTINT (w->last_point))
10422 {
10423 /* Point has moved forward. */
47589c8c
GM
10424 while (MATRIX_ROW_END_CHARPOS (row) < PT
10425 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10426 {
10427 xassert (row->enabled_p);
10428 ++row;
10429 }
10430
10431 /* The end position of a row equals the start position
10432 of the next row. If PT is there, we would rather
cafafe0b
GM
10433 display it in the next line. */
10434 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10435 && MATRIX_ROW_END_CHARPOS (row) == PT
10436 && !cursor_row_p (w, row))
10437 ++row;
47589c8c
GM
10438
10439 /* If within the scroll margin, scroll. Note that
10440 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10441 the next line would be drawn, and that
10442 this_scroll_margin can be zero. */
10443 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10444 || PT > MATRIX_ROW_END_CHARPOS (row)
10445 /* Line is completely visible last line in window
10446 and PT is to be set in the next line. */
10447 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10448 && PT == MATRIX_ROW_END_CHARPOS (row)
10449 && !row->ends_at_zv_p
10450 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10451 scroll_p = 1;
10452 }
10453 else if (PT < XFASTINT (w->last_point))
10454 {
10455 /* Cursor has to be moved backward. Note that PT >=
10456 CHARPOS (startp) because of the outer
10457 if-statement. */
10458 while (!row->mode_line_p
10459 && (MATRIX_ROW_START_CHARPOS (row) > PT
10460 || (MATRIX_ROW_START_CHARPOS (row) == PT
10461 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10462 && (row->y > this_scroll_margin
10463 || CHARPOS (startp) == BEGV))
10464 {
10465 xassert (row->enabled_p);
10466 --row;
10467 }
10468
10469 /* Consider the following case: Window starts at BEGV,
10470 there is invisible, intangible text at BEGV, so that
10471 display starts at some point START > BEGV. It can
10472 happen that we are called with PT somewhere between
10473 BEGV and START. Try to handle that case. */
10474 if (row < w->current_matrix->rows
10475 || row->mode_line_p)
10476 {
10477 row = w->current_matrix->rows;
10478 if (row->mode_line_p)
10479 ++row;
10480 }
10481
10482 /* Due to newlines in overlay strings, we may have to
10483 skip forward over overlay strings. */
68c5d1db
GM
10484 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10485 && MATRIX_ROW_END_CHARPOS (row) == PT
10486 && !cursor_row_p (w, row))
47589c8c 10487 ++row;
2311178e 10488
47589c8c
GM
10489 /* If within the scroll margin, scroll. */
10490 if (row->y < this_scroll_margin
10491 && CHARPOS (startp) != BEGV)
10492 scroll_p = 1;
10493 }
10494
10495 if (PT < MATRIX_ROW_START_CHARPOS (row)
10496 || PT > MATRIX_ROW_END_CHARPOS (row))
10497 {
10498 /* if PT is not in the glyph row, give up. */
cb617e7c 10499 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 10500 }
440fc135 10501 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 10502 {
8de4aaf8
GM
10503 if (PT == MATRIX_ROW_END_CHARPOS (row)
10504 && !row->ends_at_zv_p
10505 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 10506 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 10507 else if (row->height > window_box_height (w))
440fc135 10508 {
8de4aaf8
GM
10509 /* If we end up in a partially visible line, let's
10510 make it fully visible, except when it's taller
10511 than the window, in which case we can't do much
10512 about it. */
440fc135 10513 *scroll_step = 1;
cb617e7c 10514 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
10515 }
10516 else
10517 {
10518 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 10519 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 10520 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
10521 else
10522 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 10523 }
47589c8c
GM
10524 }
10525 else if (scroll_p)
cb617e7c 10526 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10527 else
10528 {
10529 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 10530 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
10531 }
10532 }
10533 }
10534
10535 return rc;
10536}
10537
10538
5f5c8ee5 10539/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
10540 selected_window is redisplayed.
10541
10542 We can return without actually redisplaying the window if
10543 fonts_changed_p is nonzero. In that case, redisplay_internal will
10544 retry. */
90adcf20 10545
a2889657 10546static void
5f5c8ee5 10547redisplay_window (window, just_this_one_p)
a2889657 10548 Lisp_Object window;
5f5c8ee5 10549 int just_this_one_p;
a2889657 10550{
5f5c8ee5
GM
10551 struct window *w = XWINDOW (window);
10552 struct frame *f = XFRAME (w->frame);
10553 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 10554 struct buffer *old = current_buffer;
5f5c8ee5 10555 struct text_pos lpoint, opoint, startp;
e481f960 10556 int update_mode_line;
5f5c8ee5
GM
10557 int tem;
10558 struct it it;
10559 /* Record it now because it's overwritten. */
10560 int current_matrix_up_to_date_p = 0;
c62c1bb5
RS
10561 /* This is less strict than current_matrix_up_to_date_p.
10562 It indictes that the buffer contents and narrowing are unchanged. */
10563 int buffer_unchanged_p = 0;
5f5c8ee5 10564 int temp_scroll_step = 0;
331379bf 10565 int count = SPECPDL_INDEX ();
47589c8c 10566 int rc;
ddf6b9a3 10567 int centering_position;
03b0a4b4 10568 int last_line_misfit = 0;
a2889657 10569
5f5c8ee5
GM
10570 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10571 opoint = lpoint;
a2889657 10572
5f5c8ee5
GM
10573 /* W must be a leaf window here. */
10574 xassert (!NILP (w->buffer));
10575#if GLYPH_DEBUG
10576 *w->desired_matrix->method = 0;
10577#endif
2e54982e
RS
10578
10579 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
10580
10581 reconsider_clip_changes (w, buffer);
2311178e
TTN
10582
10583 /* Has the mode line to be updated? */
5f5c8ee5
GM
10584 update_mode_line = (!NILP (w->update_mode_line)
10585 || update_mode_lines
c62c1bb5
RS
10586 || buffer->clip_changed
10587 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
10588
10589 if (MINI_WINDOW_P (w))
10590 {
5f5c8ee5 10591 if (w == XWINDOW (echo_area_window)
c6e89d6c 10592 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
10593 {
10594 if (update_mode_line)
10595 /* We may have to update a tty frame's menu bar or a
e037b9ec 10596 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
10597 goto finish_menu_bars;
10598 else
10599 /* We've already displayed the echo area glyphs in this window. */
10600 goto finish_scroll_bars;
10601 }
b2b5455d
RS
10602 else if ((w != XWINDOW (minibuf_window)
10603 || minibuf_level == 0)
3354fdcf
RS
10604 /* Quail displays non-mini buffers in minibuffer window.
10605 In that case, redisplay the window normally. */
b2b5455d 10606 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 10607 {
3354fdcf
RS
10608 /* W is a mini-buffer window, but it's not active, so clear
10609 it. */
5f5c8ee5
GM
10610 int yb = window_text_bottom_y (w);
10611 struct glyph_row *row;
10612 int y;
10613
10614 for (y = 0, row = w->desired_matrix->rows;
10615 y < yb;
10616 y += row->height, ++row)
10617 blank_row (w, row, y);
88f22aff 10618 goto finish_scroll_bars;
8de2d90b 10619 }
c095a1dd
GM
10620
10621 clear_glyph_matrix (w->desired_matrix);
8de2d90b 10622 }
a2889657 10623
5f5c8ee5
GM
10624 /* Otherwise set up data on this window; select its buffer and point
10625 value. */
6a93695f
GM
10626 /* Really select the buffer, for the sake of buffer-local
10627 variables. */
10628 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
10629 SET_TEXT_POS (opoint, PT, PT_BYTE);
10630
10631 current_matrix_up_to_date_p
10632 = (!NILP (w->window_end_valid)
10633 && !current_buffer->clip_changed
c62c1bb5 10634 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
10635 && XFASTINT (w->last_modified) >= MODIFF
10636 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 10637
c62c1bb5
RS
10638 buffer_unchanged_p
10639 = (!NILP (w->window_end_valid)
10640 && !current_buffer->clip_changed
3f1258d0 10641 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
10642 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10643
5f5c8ee5
GM
10644 /* When windows_or_buffers_changed is non-zero, we can't rely on
10645 the window end being valid, so set it to nil there. */
10646 if (windows_or_buffers_changed)
10647 {
10648 /* If window starts on a continuation line, maybe adjust the
10649 window start in case the window's width changed. */
10650 if (XMARKER (w->start)->buffer == current_buffer)
10651 compute_window_start_on_continuation_line (w);
2311178e 10652
5f5c8ee5
GM
10653 w->window_end_valid = Qnil;
10654 }
12adba34 10655
5f5c8ee5
GM
10656 /* Some sanity checks. */
10657 CHECK_WINDOW_END (w);
10658 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 10659 abort ();
5f5c8ee5 10660 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 10661 abort ();
a2889657 10662
28995e67
RS
10663 /* If %c is in mode line, update it if needed. */
10664 if (!NILP (w->column_number_displayed)
10665 /* This alternative quickly identifies a common case
10666 where no change is needed. */
10667 && !(PT == XFASTINT (w->last_point)
8850a573
RS
10668 && XFASTINT (w->last_modified) >= MODIFF
10669 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
10670 && (XFASTINT (w->column_number_displayed)
10671 != (int) current_column ())) /* iftc */
10672 update_mode_line = 1;
28995e67 10673
5f5c8ee5
GM
10674 /* Count number of windows showing the selected buffer. An indirect
10675 buffer counts as its base buffer. */
10676 if (!just_this_one_p)
42640f83
RS
10677 {
10678 struct buffer *current_base, *window_base;
10679 current_base = current_buffer;
10680 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10681 if (current_base->base_buffer)
10682 current_base = current_base->base_buffer;
10683 if (window_base->base_buffer)
10684 window_base = window_base->base_buffer;
10685 if (current_base == window_base)
10686 buffer_shared++;
10687 }
a2889657 10688
5f5c8ee5
GM
10689 /* Point refers normally to the selected window. For any other
10690 window, set up appropriate value. */
a2889657
JB
10691 if (!EQ (window, selected_window))
10692 {
12adba34
RS
10693 int new_pt = XMARKER (w->pointm)->charpos;
10694 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 10695 if (new_pt < BEGV)
a2889657 10696 {
f67a0f51 10697 new_pt = BEGV;
12adba34
RS
10698 new_pt_byte = BEGV_BYTE;
10699 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 10700 }
f67a0f51 10701 else if (new_pt > (ZV - 1))
a2889657 10702 {
f67a0f51 10703 new_pt = ZV;
12adba34
RS
10704 new_pt_byte = ZV_BYTE;
10705 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 10706 }
2311178e 10707
f67a0f51 10708 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 10709 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
10710 }
10711
f4faa47c 10712 /* If any of the character widths specified in the display table
5f5c8ee5
GM
10713 have changed, invalidate the width run cache. It's true that
10714 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
10715 redisplay goes (non-fatally) haywire when the display table is
10716 changed, so why should we worry about doing any better? */
10717 if (current_buffer->width_run_cache)
10718 {
f908610f 10719 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
10720
10721 if (! disptab_matches_widthtab (disptab,
10722 XVECTOR (current_buffer->width_table)))
10723 {
10724 invalidate_region_cache (current_buffer,
10725 current_buffer->width_run_cache,
10726 BEG, Z);
10727 recompute_width_table (current_buffer, disptab);
10728 }
10729 }
10730
a2889657 10731 /* If window-start is screwed up, choose a new one. */
a2889657
JB
10732 if (XMARKER (w->start)->buffer != current_buffer)
10733 goto recenter;
10734
5f5c8ee5 10735 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 10736
cf0df6ab
RS
10737 /* If someone specified a new starting point but did not insist,
10738 check whether it can be used. */
cfad01b4
GM
10739 if (!NILP (w->optional_new_start)
10740 && CHARPOS (startp) >= BEGV
10741 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
10742 {
10743 w->optional_new_start = Qnil;
5f5c8ee5
GM
10744 start_display (&it, w, startp);
10745 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10746 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10747 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
10748 w->force_start = Qt;
10749 }
10750
8de2d90b 10751 /* Handle case where place to start displaying has been specified,
aa6d10fa 10752 unless the specified location is outside the accessible range. */
9472f927
GM
10753 if (!NILP (w->force_start)
10754 || w->frozen_window_start_p)
a2889657 10755 {
b60c9653
RS
10756 /* We set this later on if we have to adjust point. */
10757 int new_vpos = -1;
10758
e63574d7 10759 w->force_start = Qnil;
5f5c8ee5 10760 w->vscroll = 0;
b5174a51 10761 w->window_end_valid = Qnil;
5f5c8ee5
GM
10762
10763 /* Forget any recorded base line for line number display. */
c62c1bb5 10764 if (!buffer_unchanged_p)
5f5c8ee5
GM
10765 w->base_line_number = Qnil;
10766
75c43375
RS
10767 /* Redisplay the mode line. Select the buffer properly for that.
10768 Also, run the hook window-scroll-functions
10769 because we have scrolled. */
e63574d7
RS
10770 /* Note, we do this after clearing force_start because
10771 if there's an error, it is better to forget about force_start
10772 than to get into an infinite loop calling the hook functions
10773 and having them get more errors. */
75c43375
RS
10774 if (!update_mode_line
10775 || ! NILP (Vwindow_scroll_functions))
e481f960 10776 {
e481f960
RS
10777 update_mode_line = 1;
10778 w->update_mode_line = Qt;
5f5c8ee5 10779 startp = run_window_scroll_functions (window, startp);
e481f960 10780 }
2311178e 10781
ac90c44f
GM
10782 w->last_modified = make_number (0);
10783 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
10784 if (CHARPOS (startp) < BEGV)
10785 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10786 else if (CHARPOS (startp) > ZV)
10787 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
10788
10789 /* Redisplay, then check if cursor has been set during the
5f5c8ee5
GM
10790 redisplay. Give up if new fonts were loaded. */
10791 if (!try_window (window, startp))
10792 {
10793 w->force_start = Qt;
10794 clear_glyph_matrix (w->desired_matrix);
b60c9653 10795 goto need_larger_matrices;
5f5c8ee5
GM
10796 }
10797
9472f927 10798 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 10799 {
b28cb6ed
GM
10800 /* If point does not appear, try to move point so it does
10801 appear. The desired matrix has been built above, so we
10802 can use it here. */
b60c9653
RS
10803 new_vpos = window_box_height (w) / 2;
10804 }
10805
10806 if (!make_cursor_line_fully_visible (w))
10807 {
10808 /* Point does appear, but on a line partly visible at end of window.
10809 Move it back to a fully-visible line. */
10810 new_vpos = window_box_height (w);
10811 }
10812
10813 /* If we need to move point for either of the above reasons,
10814 now actually do it. */
10815 if (new_vpos >= 0)
10816 {
b28cb6ed
GM
10817 struct glyph_row *row;
10818
b28cb6ed 10819 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 10820 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
10821 ++row;
10822
10823 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10824 MATRIX_ROW_START_BYTEPOS (row));
10825
90adcf20 10826 if (w != XWINDOW (selected_window))
12adba34 10827 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
10828 else if (current_buffer == old)
10829 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10830
10831 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 10832
5f5c8ee5
GM
10833 /* If we are highlighting the region, then we just changed
10834 the region, so redisplay to show it. */
df0b5ea1
RS
10835 if (!NILP (Vtransient_mark_mode)
10836 && !NILP (current_buffer->mark_active))
6f27fa9b 10837 {
5f5c8ee5
GM
10838 clear_glyph_matrix (w->desired_matrix);
10839 if (!try_window (window, startp))
cb617e7c 10840 goto need_larger_matrices;
6f27fa9b 10841 }
a2889657 10842 }
5f5c8ee5 10843
5f5c8ee5
GM
10844#if GLYPH_DEBUG
10845 debug_method_add (w, "forced window start");
10846#endif
a2889657
JB
10847 goto done;
10848 }
10849
5f5c8ee5 10850 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
10851 not moved off the frame, and we are not retrying after hscroll.
10852 (current_matrix_up_to_date_p is nonzero when retrying.) */
10853 if (current_matrix_up_to_date_p
47589c8c 10854 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 10855 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 10856 {
cb617e7c
GM
10857 switch (rc)
10858 {
10859 case CURSOR_MOVEMENT_SUCCESS:
10860 goto done;
2311178e 10861
b60c9653 10862#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
10863 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10864 goto need_larger_matrices;
b60c9653 10865#endif
2311178e 10866
cb617e7c
GM
10867 case CURSOR_MOVEMENT_MUST_SCROLL:
10868 goto try_to_scroll;
2311178e 10869
cb617e7c
GM
10870 default:
10871 abort ();
10872 }
a2889657
JB
10873 }
10874 /* If current starting point was originally the beginning of a line
10875 but no longer is, find a new starting point. */
265a9e55 10876 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
10877 && !(CHARPOS (startp) <= BEGV
10878 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 10879 {
5f5c8ee5
GM
10880#if GLYPH_DEBUG
10881 debug_method_add (w, "recenter 1");
10882#endif
a2889657
JB
10883 goto recenter;
10884 }
2311178e 10885
27d16f05
GM
10886 /* Try scrolling with try_window_id. Value is > 0 if update has
10887 been done, it is -1 if we know that the same window start will
10888 not work. It is 0 if unsuccessful for some other reason. */
10889 else if ((tem = try_window_id (w)) != 0)
a2889657 10890 {
5f5c8ee5 10891#if GLYPH_DEBUG
ef121659 10892 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
10893#endif
10894
10895 if (fonts_changed_p)
cb617e7c 10896 goto need_larger_matrices;
a2889657
JB
10897 if (tem > 0)
10898 goto done;
ef121659 10899
5f5c8ee5
GM
10900 /* Otherwise try_window_id has returned -1 which means that we
10901 don't want the alternative below this comment to execute. */
a2889657 10902 }
5f5c8ee5
GM
10903 else if (CHARPOS (startp) >= BEGV
10904 && CHARPOS (startp) <= ZV
10905 && PT >= CHARPOS (startp)
10906 && (CHARPOS (startp) < ZV
e9874cee 10907 /* Avoid starting at end of buffer. */
5f5c8ee5 10908 || CHARPOS (startp) == BEGV
8850a573
RS
10909 || (XFASTINT (w->last_modified) >= MODIFF
10910 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 10911 {
5f5c8ee5
GM
10912#if GLYPH_DEBUG
10913 debug_method_add (w, "same window start");
10914#endif
2311178e 10915
5f5c8ee5
GM
10916 /* Try to redisplay starting at same place as before.
10917 If point has not moved off frame, accept the results. */
10918 if (!current_matrix_up_to_date_p
10919 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
10920 because a window scroll function can have changed the
10921 buffer. */
5f5c8ee5
GM
10922 || !NILP (Vwindow_scroll_functions)
10923 || MINI_WINDOW_P (w)
10924 || !try_window_reusing_current_matrix (w))
10925 {
10926 IF_DEBUG (debug_method_add (w, "1"));
10927 try_window (window, startp);
10928 }
10929
10930 if (fonts_changed_p)
cb617e7c 10931 goto need_larger_matrices;
2311178e 10932
5f5c8ee5 10933 if (w->cursor.vpos >= 0)
aa6d10fa 10934 {
2311178e 10935 if (!just_this_one_p
5f5c8ee5 10936 || current_buffer->clip_changed
9142dd5b 10937 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
10938 /* Forget any recorded base line for line number display. */
10939 w->base_line_number = Qnil;
2311178e 10940
cb617e7c 10941 if (!make_cursor_line_fully_visible (w))
03b0a4b4
RS
10942 {
10943 clear_glyph_matrix (w->desired_matrix);
10944 last_line_misfit = 1;
10945 }
ddf6b9a3 10946 /* Drop through and scroll. */
ef3c2c73
RS
10947 else
10948 goto done;
aa6d10fa 10949 }
a2889657 10950 else
5f5c8ee5 10951 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
10952 }
10953
5f5c8ee5
GM
10954 try_to_scroll:
10955
ac90c44f
GM
10956 w->last_modified = make_number (0);
10957 w->last_overlay_modified = make_number (0);
5f5c8ee5 10958
e481f960
RS
10959 /* Redisplay the mode line. Select the buffer properly for that. */
10960 if (!update_mode_line)
10961 {
e481f960
RS
10962 update_mode_line = 1;
10963 w->update_mode_line = Qt;
10964 }
a2889657 10965
5f5c8ee5
GM
10966 /* Try to scroll by specified few lines. */
10967 if ((scroll_conservatively
10968 || scroll_step
10969 || temp_scroll_step
10970 || NUMBERP (current_buffer->scroll_up_aggressively)
10971 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 10972 && !current_buffer->clip_changed
2311178e 10973 && CHARPOS (startp) >= BEGV
5f5c8ee5 10974 && CHARPOS (startp) <= ZV)
0789adb2 10975 {
5f5c8ee5
GM
10976 /* The function returns -1 if new fonts were loaded, 1 if
10977 successful, 0 if not successful. */
10978 int rc = try_scrolling (window, just_this_one_p,
10979 scroll_conservatively,
10980 scroll_step,
03b0a4b4 10981 temp_scroll_step, last_line_misfit);
cb617e7c
GM
10982 switch (rc)
10983 {
10984 case SCROLLING_SUCCESS:
10985 goto done;
2311178e 10986
cb617e7c
GM
10987 case SCROLLING_NEED_LARGER_MATRICES:
10988 goto need_larger_matrices;
2311178e 10989
cb617e7c
GM
10990 case SCROLLING_FAILED:
10991 break;
2311178e 10992
cb617e7c
GM
10993 default:
10994 abort ();
10995 }
5f5c8ee5 10996 }
f9c8af06 10997
5f5c8ee5 10998 /* Finally, just choose place to start which centers point */
5936754e 10999
5f5c8ee5 11000 recenter:
ddf6b9a3
RS
11001 centering_position = window_box_height (w) / 2;
11002
11003 point_at_top:
11004 /* Jump here with centering_position already set to 0. */
44173109 11005
5f5c8ee5
GM
11006#if GLYPH_DEBUG
11007 debug_method_add (w, "recenter");
11008#endif
0789adb2 11009
5f5c8ee5 11010 /* w->vscroll = 0; */
0789adb2 11011
5f5c8ee5 11012 /* Forget any previously recorded base line for line number display. */
c62c1bb5 11013 if (!buffer_unchanged_p)
5f5c8ee5
GM
11014 w->base_line_number = Qnil;
11015
11016 /* Move backward half the height of the window. */
11017 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11018 it.current_y = it.last_visible_y;
ddf6b9a3 11019 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
11020 xassert (IT_CHARPOS (it) >= BEGV);
11021
11022 /* The function move_it_vertically_backward may move over more
11023 than the specified y-distance. If it->w is small, e.g. a
11024 mini-buffer window, we may end up in front of the window's
11025 display area. Start displaying at the start of the line
11026 containing PT in this case. */
11027 if (it.current_y <= 0)
11028 {
11029 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11030 move_it_vertically (&it, 0);
11031 xassert (IT_CHARPOS (it) <= PT);
11032 it.current_y = 0;
0789adb2
RS
11033 }
11034
5f5c8ee5 11035 it.current_x = it.hpos = 0;
2311178e 11036
5f5c8ee5
GM
11037 /* Set startp here explicitly in case that helps avoid an infinite loop
11038 in case the window-scroll-functions functions get errors. */
11039 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11040
11041 /* Run scroll hooks. */
11042 startp = run_window_scroll_functions (window, it.current.pos);
11043
11044 /* Redisplay the window. */
11045 if (!current_matrix_up_to_date_p
11046 || windows_or_buffers_changed
5fb96e96 11047 || cursor_type_changed
5f5c8ee5
GM
11048 /* Don't use try_window_reusing_current_matrix in this case
11049 because it can have changed the buffer. */
11050 || !NILP (Vwindow_scroll_functions)
11051 || !just_this_one_p
11052 || MINI_WINDOW_P (w)
11053 || !try_window_reusing_current_matrix (w))
11054 try_window (window, startp);
11055
11056 /* If new fonts have been loaded (due to fontsets), give up. We
11057 have to start a new redisplay since we need to re-adjust glyph
11058 matrices. */
11059 if (fonts_changed_p)
cb617e7c 11060 goto need_larger_matrices;
5f5c8ee5
GM
11061
11062 /* If cursor did not appear assume that the middle of the window is
11063 in the first line of the window. Do it again with the next line.
11064 (Imagine a window of height 100, displaying two lines of height
11065 60. Moving back 50 from it->last_visible_y will end in the first
11066 line.) */
11067 if (w->cursor.vpos < 0)
a2889657 11068 {
5f5c8ee5
GM
11069 if (!NILP (w->window_end_valid)
11070 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 11071 {
5f5c8ee5
GM
11072 clear_glyph_matrix (w->desired_matrix);
11073 move_it_by_lines (&it, 1, 0);
11074 try_window (window, it.current.pos);
a2889657 11075 }
5f5c8ee5 11076 else if (PT < IT_CHARPOS (it))
a2889657 11077 {
5f5c8ee5
GM
11078 clear_glyph_matrix (w->desired_matrix);
11079 move_it_by_lines (&it, -1, 0);
11080 try_window (window, it.current.pos);
11081 }
11082 else
11083 {
11084 /* Not much we can do about it. */
a2889657 11085 }
a2889657 11086 }
010494d0 11087
5f5c8ee5
GM
11088 /* Consider the following case: Window starts at BEGV, there is
11089 invisible, intangible text at BEGV, so that display starts at
11090 some point START > BEGV. It can happen that we are called with
11091 PT somewhere between BEGV and START. Try to handle that case. */
11092 if (w->cursor.vpos < 0)
835766b6 11093 {
5f5c8ee5
GM
11094 struct glyph_row *row = w->current_matrix->rows;
11095 if (row->mode_line_p)
11096 ++row;
11097 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 11098 }
2311178e 11099
cb617e7c 11100 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 11101 {
932357b4
KS
11102 /* If vscroll is enabled, disable it and try again. */
11103 if (w->vscroll)
11104 {
11105 w->vscroll = 0;
11106 clear_glyph_matrix (w->desired_matrix);
11107 goto recenter;
11108 }
2a6d0874 11109
ddf6b9a3
RS
11110 /* If centering point failed to make the whole line visible,
11111 put point at the top instead. That has to make the whole line
11112 visible, if it can be done. */
11113 centering_position = 0;
11114 goto point_at_top;
11115 }
b5174a51 11116
74d481ac
GM
11117 done:
11118
5f5c8ee5
GM
11119 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11120 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11121 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11122 ? Qt : Qnil);
a2889657 11123
5f5c8ee5 11124 /* Display the mode line, if we must. */
e481f960 11125 if ((update_mode_line
aa6d10fa 11126 /* If window not full width, must redo its mode line
2311178e 11127 if (a) the window to its side is being redone and
5f5c8ee5
GM
11128 (b) we do a frame-based redisplay. This is a consequence
11129 of how inverted lines are drawn in frame-based redisplay. */
2311178e 11130 || (!just_this_one_p
5f5c8ee5
GM
11131 && !FRAME_WINDOW_P (f)
11132 && !WINDOW_FULL_WIDTH_P (w))
11133 /* Line number to display. */
155ef550 11134 || INTEGERP (w->base_line_pos)
5f5c8ee5 11135 /* Column number is displayed and different from the one displayed. */
155ef550 11136 || (!NILP (w->column_number_displayed)
2311178e
TTN
11137 && (XFASTINT (w->column_number_displayed)
11138 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
11139 /* This means that the window has a mode line. */
11140 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 11141 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 11142 {
5f5c8ee5
GM
11143 display_mode_lines (w);
11144
11145 /* If mode line height has changed, arrange for a thorough
11146 immediate redisplay using the correct mode line height. */
11147 if (WINDOW_WANTS_MODELINE_P (w)
11148 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 11149 {
5f5c8ee5
GM
11150 fonts_changed_p = 1;
11151 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11152 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 11153 }
2311178e 11154
5f5c8ee5
GM
11155 /* If top line height has changed, arrange for a thorough
11156 immediate redisplay using the correct mode line height. */
045dee35
GM
11157 if (WINDOW_WANTS_HEADER_LINE_P (w)
11158 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
11159 {
11160 fonts_changed_p = 1;
045dee35
GM
11161 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11162 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
11163 }
11164
11165 if (fonts_changed_p)
cb617e7c 11166 goto need_larger_matrices;
5ba50c51 11167 }
5f5c8ee5
GM
11168
11169 if (!line_number_displayed
11170 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
11171 {
11172 w->base_line_pos = Qnil;
11173 w->base_line_number = Qnil;
11174 }
a2889657 11175
5f5c8ee5 11176 finish_menu_bars:
2311178e 11177
7ce2c095 11178 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 11179 if (update_mode_line
5f5c8ee5
GM
11180 && EQ (FRAME_SELECTED_WINDOW (f), window))
11181 {
11182 int redisplay_menu_p = 0;
488dd4c4 11183 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
11184
11185 if (FRAME_WINDOW_P (f))
11186 {
488dd4c4
JD
11187#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11188 || defined (USE_GTK)
5f5c8ee5 11189 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 11190#else
5f5c8ee5 11191 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 11192#endif
5f5c8ee5
GM
11193 }
11194 else
11195 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11196
11197 if (redisplay_menu_p)
11198 display_menu_bar (w);
11199
11200#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
11201#ifdef USE_GTK
11202 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11203#else
11204 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11205 && (FRAME_TOOL_BAR_LINES (f) > 0
11206 || auto_resize_tool_bars_p);
177c0ea7 11207
488dd4c4
JD
11208#endif
11209
11210 if (redisplay_tool_bar_p)
11211 redisplay_tool_bar (f);
5f5c8ee5
GM
11212#endif
11213 }
7ce2c095 11214
b60c9653
RS
11215 /* We go to this label, with fonts_changed_p nonzero,
11216 if it is necessary to try again using larger glyph matrices.
11217 We have to redeem the scroll bar even in this case,
11218 because the loop in redisplay_internal expects that. */
cb617e7c
GM
11219 need_larger_matrices:
11220 ;
88f22aff 11221 finish_scroll_bars:
5f5c8ee5 11222
88f22aff 11223 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 11224 {
b1d1124b 11225 int start, end, whole;
30c566e4 11226
b1d1124b 11227 /* Calculate the start and end positions for the current window.
3505ea70
JB
11228 At some point, it would be nice to choose between scrollbars
11229 which reflect the whole buffer size, with special markers
11230 indicating narrowing, and scrollbars which reflect only the
11231 visible region.
11232
5f5c8ee5 11233 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 11234 if (!MINI_WINDOW_P (w)
5f5c8ee5 11235 || (w == XWINDOW (minibuf_window)
c6e89d6c 11236 && NILP (echo_area_buffer[0])))
b1d1124b 11237 {
8a9311d7 11238 whole = ZV - BEGV;
4d641a15 11239 start = marker_position (w->start) - BEGV;
b1d1124b
JB
11240 /* I don't think this is guaranteed to be right. For the
11241 moment, we'll pretend it is. */
5f5c8ee5 11242 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 11243
2311178e 11244 if (end < start)
5f5c8ee5 11245 end = start;
2311178e 11246 if (whole < (end - start))
5f5c8ee5 11247 whole = end - start;
b1d1124b
JB
11248 }
11249 else
11250 start = end = whole = 0;
30c566e4 11251
88f22aff 11252 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 11253 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 11254
5f5c8ee5
GM
11255 /* Note that we actually used the scroll bar attached to this
11256 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 11257 redeem_scroll_bar_hook (w);
30c566e4 11258 }
b1d1124b 11259
5f5c8ee5
GM
11260 /* Restore current_buffer and value of point in it. */
11261 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 11262 set_buffer_internal_1 (old);
5f5c8ee5 11263 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
11264
11265 unbind_to (count, Qnil);
a2889657 11266}
a2889657 11267
5f5c8ee5
GM
11268
11269/* Build the complete desired matrix of WINDOW with a window start
11270 buffer position POS. Value is non-zero if successful. It is zero
11271 if fonts were loaded during redisplay which makes re-adjusting
11272 glyph matrices necessary. */
11273
11274int
a2889657
JB
11275try_window (window, pos)
11276 Lisp_Object window;
5f5c8ee5
GM
11277 struct text_pos pos;
11278{
11279 struct window *w = XWINDOW (window);
11280 struct it it;
11281 struct glyph_row *last_text_row = NULL;
9cbab4ff 11282
5f5c8ee5
GM
11283 /* Make POS the new window start. */
11284 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 11285
5f5c8ee5
GM
11286 /* Mark cursor position as unknown. No overlay arrow seen. */
11287 w->cursor.vpos = -1;
a2889657 11288 overlay_arrow_seen = 0;
642eefc6 11289
5f5c8ee5
GM
11290 /* Initialize iterator and info to start at POS. */
11291 start_display (&it, w, pos);
a2889657 11292
5f5c8ee5
GM
11293 /* Display all lines of W. */
11294 while (it.current_y < it.last_visible_y)
11295 {
11296 if (display_line (&it))
11297 last_text_row = it.glyph_row - 1;
11298 if (fonts_changed_p)
11299 return 0;
11300 }
a2889657 11301
5f5c8ee5
GM
11302 /* If bottom moved off end of frame, change mode line percentage. */
11303 if (XFASTINT (w->window_end_pos) <= 0
11304 && Z != IT_CHARPOS (it))
a2889657
JB
11305 w->update_mode_line = Qt;
11306
5f5c8ee5
GM
11307 /* Set window_end_pos to the offset of the last character displayed
11308 on the window from the end of current_buffer. Set
11309 window_end_vpos to its row number. */
11310 if (last_text_row)
11311 {
11312 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
11313 w->window_end_bytepos
11314 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11315 w->window_end_pos
11316 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11317 w->window_end_vpos
11318 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11319 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
11320 ->displays_text_p);
11321 }
11322 else
11323 {
11324 w->window_end_bytepos = 0;
ac90c44f 11325 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5 11326 }
2311178e 11327
a2889657
JB
11328 /* But that is not valid info until redisplay finishes. */
11329 w->window_end_valid = Qnil;
5f5c8ee5 11330 return 1;
a2889657 11331}
5f5c8ee5
GM
11332
11333
a2889657 11334\f
5f5c8ee5
GM
11335/************************************************************************
11336 Window redisplay reusing current matrix when buffer has not changed
11337 ************************************************************************/
11338
11339/* Try redisplay of window W showing an unchanged buffer with a
11340 different window start than the last time it was displayed by
11341 reusing its current matrix. Value is non-zero if successful.
11342 W->start is the new window start. */
a2889657
JB
11343
11344static int
5f5c8ee5
GM
11345try_window_reusing_current_matrix (w)
11346 struct window *w;
a2889657 11347{
5f5c8ee5
GM
11348 struct frame *f = XFRAME (w->frame);
11349 struct glyph_row *row, *bottom_row;
11350 struct it it;
11351 struct run run;
11352 struct text_pos start, new_start;
11353 int nrows_scrolled, i;
11354 struct glyph_row *last_text_row;
11355 struct glyph_row *last_reused_text_row;
11356 struct glyph_row *start_row;
11357 int start_vpos, min_y, max_y;
75c5350a 11358
69d1f7c9 11359#if GLYPH_DEBUG
76cb5e06
GM
11360 if (inhibit_try_window_reusing)
11361 return 0;
11362#endif
11363
d18354b6
GM
11364 if (/* This function doesn't handle terminal frames. */
11365 !FRAME_WINDOW_P (f)
11366 /* Don't try to reuse the display if windows have been split
11367 or such. */
5fb96e96
RS
11368 || windows_or_buffers_changed
11369 || cursor_type_changed)
5f5c8ee5 11370 return 0;
a2889657 11371
5f5c8ee5
GM
11372 /* Can't do this if region may have changed. */
11373 if ((!NILP (Vtransient_mark_mode)
11374 && !NILP (current_buffer->mark_active))
8f897821
GM
11375 || !NILP (w->region_showing)
11376 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 11377 return 0;
a2889657 11378
5f5c8ee5 11379 /* If top-line visibility has changed, give up. */
045dee35
GM
11380 if (WINDOW_WANTS_HEADER_LINE_P (w)
11381 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
11382 return 0;
11383
11384 /* Give up if old or new display is scrolled vertically. We could
11385 make this function handle this, but right now it doesn't. */
11386 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11387 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
11388 return 0;
11389
11390 /* The variable new_start now holds the new window start. The old
11391 start `start' can be determined from the current matrix. */
11392 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
11393 start = start_row->start.pos;
11394 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 11395
5f5c8ee5
GM
11396 /* Clear the desired matrix for the display below. */
11397 clear_glyph_matrix (w->desired_matrix);
2311178e 11398
5f5c8ee5
GM
11399 if (CHARPOS (new_start) <= CHARPOS (start))
11400 {
11401 int first_row_y;
2311178e 11402
607ec83c
GM
11403 /* Don't use this method if the display starts with an ellipsis
11404 displayed for invisible text. It's not easy to handle that case
11405 below, and it's certainly not worth the effort since this is
11406 not a frequent case. */
11407 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
11408 return 0;
11409
5f5c8ee5 11410 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 11411
5f5c8ee5
GM
11412 /* Display up to a row that can be reused. The variable
11413 last_text_row is set to the last row displayed that displays
b48f74cb
GM
11414 text. Note that it.vpos == 0 if or if not there is a
11415 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
11416 start_display (&it, w, new_start);
11417 first_row_y = it.current_y;
11418 w->cursor.vpos = -1;
11419 last_text_row = last_reused_text_row = NULL;
2311178e 11420
5f5c8ee5
GM
11421 while (it.current_y < it.last_visible_y
11422 && IT_CHARPOS (it) < CHARPOS (start)
11423 && !fonts_changed_p)
11424 if (display_line (&it))
11425 last_text_row = it.glyph_row - 1;
11426
11427 /* A value of current_y < last_visible_y means that we stopped
11428 at the previous window start, which in turn means that we
11429 have at least one reusable row. */
11430 if (it.current_y < it.last_visible_y)
a2889657 11431 {
b48f74cb 11432 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5 11433 nrows_scrolled = it.vpos;
2311178e 11434
5f5c8ee5
GM
11435 /* Find PT if not already found in the lines displayed. */
11436 if (w->cursor.vpos < 0)
a2889657 11437 {
5f5c8ee5 11438 int dy = it.current_y - first_row_y;
2311178e 11439
5f5c8ee5 11440 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
11441 row = row_containing_pos (w, PT, row, NULL, dy);
11442 if (row)
11443 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11444 dy, nrows_scrolled);
11445 else
5f5c8ee5
GM
11446 {
11447 clear_glyph_matrix (w->desired_matrix);
11448 return 0;
11449 }
a2889657 11450 }
5f5c8ee5
GM
11451
11452 /* Scroll the display. Do it before the current matrix is
11453 changed. The problem here is that update has not yet
11454 run, i.e. part of the current matrix is not up to date.
11455 scroll_run_hook will clear the cursor, and use the
11456 current matrix to get the height of the row the cursor is
11457 in. */
11458 run.current_y = first_row_y;
11459 run.desired_y = it.current_y;
11460 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
11461
11462 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 11463 {
5f5c8ee5
GM
11464 update_begin (f);
11465 rif->update_window_begin_hook (w);
64d1e7d3 11466 rif->clear_mouse_face (w);
5f5c8ee5 11467 rif->scroll_run_hook (w, &run);
64d1e7d3 11468 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 11469 update_end (f);
a2889657 11470 }
5f5c8ee5
GM
11471
11472 /* Shift current matrix down by nrows_scrolled lines. */
11473 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11474 rotate_matrix (w->current_matrix,
11475 start_vpos,
11476 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11477 nrows_scrolled);
2311178e 11478
9c8b8382 11479 /* Disable lines that must be updated. */
5f5c8ee5 11480 for (i = 0; i < it.vpos; ++i)
b48f74cb 11481 (start_row + i)->enabled_p = 0;
9c8b8382 11482
5f5c8ee5 11483 /* Re-compute Y positions. */
045dee35 11484 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11485 max_y = it.last_visible_y;
b48f74cb
GM
11486 for (row = start_row + nrows_scrolled;
11487 row < bottom_row;
11488 ++row)
d2f84654 11489 {
5f5c8ee5 11490 row->y = it.current_y;
75c5350a 11491 row->visible_height = row->height;
5f5c8ee5
GM
11492
11493 if (row->y < min_y)
75c5350a
GM
11494 row->visible_height -= min_y - row->y;
11495 if (row->y + row->height > max_y)
11496 row->visible_height -= row->y + row->height - max_y;
2311178e 11497
5f5c8ee5 11498 it.current_y += row->height;
5f5c8ee5
GM
11499
11500 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11501 last_reused_text_row = row;
11502 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11503 break;
d2f84654 11504 }
2311178e 11505
9c8b8382
GM
11506 /* Disable lines in the current matrix which are now
11507 below the window. */
bafb434c 11508 for (++row; row < bottom_row; ++row)
9c8b8382 11509 row->enabled_p = 0;
a2889657 11510 }
5f5c8ee5
GM
11511
11512 /* Update window_end_pos etc.; last_reused_text_row is the last
11513 reused row from the current matrix containing text, if any.
11514 The value of last_text_row is the last displayed line
11515 containing text. */
11516 if (last_reused_text_row)
a2889657 11517 {
5f5c8ee5
GM
11518 w->window_end_bytepos
11519 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
11520 w->window_end_pos
11521 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11522 w->window_end_vpos
11523 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11524 w->current_matrix));
a2889657 11525 }
5f5c8ee5
GM
11526 else if (last_text_row)
11527 {
11528 w->window_end_bytepos
11529 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11530 w->window_end_pos
11531 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11532 w->window_end_vpos
11533 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11534 }
11535 else
11536 {
11537 /* This window must be completely empty. */
11538 w->window_end_bytepos = 0;
ac90c44f 11539 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
11540 }
11541 w->window_end_valid = Qnil;
a2889657 11542
5f5c8ee5
GM
11543 /* Update hint: don't try scrolling again in update_window. */
11544 w->desired_matrix->no_scrolling_p = 1;
2311178e 11545
5f5c8ee5
GM
11546#if GLYPH_DEBUG
11547 debug_method_add (w, "try_window_reusing_current_matrix 1");
11548#endif
11549 return 1;
a2889657 11550 }
5f5c8ee5
GM
11551 else if (CHARPOS (new_start) > CHARPOS (start))
11552 {
11553 struct glyph_row *pt_row, *row;
11554 struct glyph_row *first_reusable_row;
11555 struct glyph_row *first_row_to_display;
11556 int dy;
11557 int yb = window_text_bottom_y (w);
11558
5f5c8ee5
GM
11559 /* Find the row starting at new_start, if there is one. Don't
11560 reuse a partially visible line at the end. */
b48f74cb 11561 first_reusable_row = start_row;
5f5c8ee5
GM
11562 while (first_reusable_row->enabled_p
11563 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11564 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11565 < CHARPOS (new_start)))
11566 ++first_reusable_row;
11567
11568 /* Give up if there is no row to reuse. */
11569 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
11570 || !first_reusable_row->enabled_p
11571 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11572 != CHARPOS (new_start)))
5f5c8ee5
GM
11573 return 0;
11574
5f5c8ee5
GM
11575 /* We can reuse fully visible rows beginning with
11576 first_reusable_row to the end of the window. Set
11577 first_row_to_display to the first row that cannot be reused.
11578 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 11579 pt_row = NULL;
ac90c44f
GM
11580 for (first_row_to_display = first_reusable_row;
11581 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11582 ++first_row_to_display)
5f5c8ee5 11583 {
4bde0ebb
GM
11584 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11585 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 11586 pt_row = first_row_to_display;
5f5c8ee5 11587 }
a2889657 11588
5f5c8ee5
GM
11589 /* Start displaying at the start of first_row_to_display. */
11590 xassert (first_row_to_display->y < yb);
11591 init_to_row_start (&it, w, first_row_to_display);
607ec83c 11592
b48f74cb
GM
11593 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11594 - start_vpos);
5f5c8ee5
GM
11595 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11596 - nrows_scrolled);
b48f74cb
GM
11597 it.current_y = (first_row_to_display->y - first_reusable_row->y
11598 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
11599
11600 /* Display lines beginning with first_row_to_display in the
11601 desired matrix. Set last_text_row to the last row displayed
11602 that displays text. */
11603 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11604 if (pt_row == NULL)
11605 w->cursor.vpos = -1;
11606 last_text_row = NULL;
11607 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11608 if (display_line (&it))
11609 last_text_row = it.glyph_row - 1;
11610
11611 /* Give up If point isn't in a row displayed or reused. */
11612 if (w->cursor.vpos < 0)
11613 {
11614 clear_glyph_matrix (w->desired_matrix);
11615 return 0;
11616 }
12adba34 11617
5f5c8ee5
GM
11618 /* If point is in a reused row, adjust y and vpos of the cursor
11619 position. */
11620 if (pt_row)
11621 {
11622 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11623 w->current_matrix);
11624 w->cursor.y -= first_reusable_row->y;
a2889657
JB
11625 }
11626
5f5c8ee5
GM
11627 /* Scroll the display. */
11628 run.current_y = first_reusable_row->y;
045dee35 11629 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11630 run.height = it.last_visible_y - run.current_y;
4da61803 11631 dy = run.current_y - run.desired_y;
2311178e 11632
5f5c8ee5
GM
11633 if (run.height)
11634 {
11635 struct frame *f = XFRAME (WINDOW_FRAME (w));
11636 update_begin (f);
11637 rif->update_window_begin_hook (w);
64d1e7d3 11638 rif->clear_mouse_face (w);
5f5c8ee5 11639 rif->scroll_run_hook (w, &run);
64d1e7d3 11640 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11641 update_end (f);
11642 }
a2889657 11643
5f5c8ee5
GM
11644 /* Adjust Y positions of reused rows. */
11645 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 11646 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11647 max_y = it.last_visible_y;
b48f74cb 11648 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
11649 {
11650 row->y -= dy;
75c5350a 11651 row->visible_height = row->height;
5f5c8ee5 11652 if (row->y < min_y)
75c5350a
GM
11653 row->visible_height -= min_y - row->y;
11654 if (row->y + row->height > max_y)
11655 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 11656 }
a2889657 11657
5f5c8ee5
GM
11658 /* Scroll the current matrix. */
11659 xassert (nrows_scrolled > 0);
11660 rotate_matrix (w->current_matrix,
11661 start_vpos,
11662 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11663 -nrows_scrolled);
11664
ac90c44f
GM
11665 /* Disable rows not reused. */
11666 for (row -= nrows_scrolled; row < bottom_row; ++row)
11667 row->enabled_p = 0;
11668
5f5c8ee5
GM
11669 /* Adjust window end. A null value of last_text_row means that
11670 the window end is in reused rows which in turn means that
11671 only its vpos can have changed. */
11672 if (last_text_row)
11673 {
11674 w->window_end_bytepos
11675 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11676 w->window_end_pos
11677 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11678 w->window_end_vpos
11679 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11680 }
11681 else
a2889657 11682 {
ac90c44f
GM
11683 w->window_end_vpos
11684 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 11685 }
2311178e 11686
5f5c8ee5
GM
11687 w->window_end_valid = Qnil;
11688 w->desired_matrix->no_scrolling_p = 1;
11689
11690#if GLYPH_DEBUG
11691 debug_method_add (w, "try_window_reusing_current_matrix 2");
11692#endif
11693 return 1;
a2889657 11694 }
2311178e 11695
5f5c8ee5
GM
11696 return 0;
11697}
a2889657 11698
a2889657 11699
5f5c8ee5
GM
11700\f
11701/************************************************************************
11702 Window redisplay reusing current matrix when buffer has changed
11703 ************************************************************************/
11704
1ec185cb
GM
11705static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11706static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
11707 int *, int *));
11708static struct glyph_row *
11709find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11710 struct glyph_row *));
11711
11712
11713/* Return the last row in MATRIX displaying text. If row START is
11714 non-null, start searching with that row. IT gives the dimensions
11715 of the display. Value is null if matrix is empty; otherwise it is
11716 a pointer to the row found. */
11717
11718static struct glyph_row *
11719find_last_row_displaying_text (matrix, it, start)
11720 struct glyph_matrix *matrix;
11721 struct it *it;
11722 struct glyph_row *start;
11723{
11724 struct glyph_row *row, *row_found;
11725
11726 /* Set row_found to the last row in IT->w's current matrix
11727 displaying text. The loop looks funny but think of partially
11728 visible lines. */
11729 row_found = NULL;
11730 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11731 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11732 {
11733 xassert (row->enabled_p);
11734 row_found = row;
11735 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11736 break;
11737 ++row;
a2889657 11738 }
2311178e 11739
5f5c8ee5
GM
11740 return row_found;
11741}
11742
a2889657 11743
5f5c8ee5 11744/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
11745 by changes at the start of current_buffer that occurred since W's
11746 current matrix was built. Value is null if no such row exists.
a2889657 11747
d43fbe9d
GM
11748 BEG_UNCHANGED us the number of characters unchanged at the start of
11749 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11750 first changed character in current_buffer. Characters at positions <
11751 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11752 when the current matrix was built. */
5f5c8ee5
GM
11753
11754static struct glyph_row *
1ec185cb 11755find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
11756 struct window *w;
11757{
9142dd5b 11758 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
11759 struct glyph_row *row;
11760 struct glyph_row *row_found = NULL;
11761 int yb = window_text_bottom_y (w);
11762
11763 /* Find the last row displaying unchanged text. */
11764 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11765 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11766 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 11767 {
5f5c8ee5
GM
11768 if (/* If row ends before first_changed_pos, it is unchanged,
11769 except in some case. */
11770 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11771 /* When row ends in ZV and we write at ZV it is not
11772 unchanged. */
11773 && !row->ends_at_zv_p
11774 /* When first_changed_pos is the end of a continued line,
11775 row is not unchanged because it may be no longer
11776 continued. */
11777 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11778 && row->continued_p))
11779 row_found = row;
11780
11781 /* Stop if last visible row. */
11782 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11783 break;
2311178e 11784
5f5c8ee5 11785 ++row;
a2889657
JB
11786 }
11787
5f5c8ee5 11788 return row_found;
a2889657 11789}
5f5c8ee5
GM
11790
11791
11792/* Find the first glyph row in the current matrix of W that is not
2311178e 11793 affected by changes at the end of current_buffer since the
d43fbe9d
GM
11794 time W's current matrix was built.
11795
11796 Return in *DELTA the number of chars by which buffer positions in
11797 unchanged text at the end of current_buffer must be adjusted.
2311178e 11798
d43fbe9d
GM
11799 Return in *DELTA_BYTES the corresponding number of bytes.
11800
11801 Value is null if no such row exists, i.e. all rows are affected by
11802 changes. */
2311178e 11803
5f5c8ee5 11804static struct glyph_row *
1ec185cb 11805find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
11806 struct window *w;
11807 int *delta, *delta_bytes;
a2889657 11808{
5f5c8ee5
GM
11809 struct glyph_row *row;
11810 struct glyph_row *row_found = NULL;
c581d710 11811
5f5c8ee5 11812 *delta = *delta_bytes = 0;
b2a76982 11813
1ec185cb
GM
11814 /* Display must not have been paused, otherwise the current matrix
11815 is not up to date. */
11816 if (NILP (w->window_end_valid))
11817 abort ();
2311178e 11818
1ec185cb 11819 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
11820 end is in the range of changed text. If so, there is no
11821 unchanged row at the end of W's current matrix. */
9142dd5b 11822 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
11823 return NULL;
11824
11825 /* Set row to the last row in W's current matrix displaying text. */
11826 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
11827
11828 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
11829 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11830 {
11831 /* The value of row is the last glyph row in the matrix having a
11832 meaningful buffer position in it. The end position of row
11833 corresponds to window_end_pos. This allows us to translate
11834 buffer positions in the current matrix to current buffer
11835 positions for characters not in changed text. */
11836 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11837 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11838 int last_unchanged_pos, last_unchanged_pos_old;
11839 struct glyph_row *first_text_row
11840 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11841
11842 *delta = Z - Z_old;
11843 *delta_bytes = Z_BYTE - Z_BYTE_old;
11844
11845 /* Set last_unchanged_pos to the buffer position of the last
11846 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
11847 index + 1 of the last character in current_buffer, i.e. by
11848 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
11849 unchanged character, and we have to add BEG to get its buffer
11850 position. */
9142dd5b 11851 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 11852 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 11853
5f5c8ee5
GM
11854 /* Search backward from ROW for a row displaying a line that
11855 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 11856 for (; row > first_text_row; --row)
5f5c8ee5 11857 {
1ec185cb
GM
11858 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11859 abort ();
2311178e 11860
5f5c8ee5
GM
11861 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11862 row_found = row;
5f5c8ee5
GM
11863 }
11864 }
11865
1ec185cb
GM
11866 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11867 abort ();
2311178e 11868
5f5c8ee5 11869 return row_found;
c581d710
RS
11870}
11871
c581d710 11872
5f5c8ee5
GM
11873/* Make sure that glyph rows in the current matrix of window W
11874 reference the same glyph memory as corresponding rows in the
11875 frame's frame matrix. This function is called after scrolling W's
11876 current matrix on a terminal frame in try_window_id and
11877 try_window_reusing_current_matrix. */
11878
11879static void
11880sync_frame_with_window_matrix_rows (w)
11881 struct window *w;
c581d710 11882{
5f5c8ee5
GM
11883 struct frame *f = XFRAME (w->frame);
11884 struct glyph_row *window_row, *window_row_end, *frame_row;
11885
11886 /* Preconditions: W must be a leaf window and full-width. Its frame
11887 must have a frame matrix. */
11888 xassert (NILP (w->hchild) && NILP (w->vchild));
11889 xassert (WINDOW_FULL_WIDTH_P (w));
11890 xassert (!FRAME_WINDOW_P (f));
11891
11892 /* If W is a full-width window, glyph pointers in W's current matrix
11893 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
11894 corresponding frame matrix. Note that frame matrices have no
11895 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
11896 window_row = w->current_matrix->rows;
11897 window_row_end = window_row + w->current_matrix->nrows;
11898 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11899 while (window_row < window_row_end)
659a218f 11900 {
7d4cc828
GM
11901 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11902 struct glyph *end = window_row->glyphs[LAST_AREA];
11903
11904 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11905 frame_row->glyphs[TEXT_AREA] = start;
11906 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11907 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
11908
11909 /* Disable frame rows whose corresponding window rows have
11910 been disabled in try_window_id. */
11911 if (!window_row->enabled_p)
11912 frame_row->enabled_p = 0;
2311178e 11913
5f5c8ee5 11914 ++window_row, ++frame_row;
659a218f 11915 }
a2889657 11916}
5f5c8ee5
GM
11917
11918
e037b9ec
GM
11919/* Find the glyph row in window W containing CHARPOS. Consider all
11920 rows between START and END (not inclusive). END null means search
11921 all rows to the end of the display area of W. Value is the row
11922 containing CHARPOS or null. */
11923
0bef35bc 11924struct glyph_row *
0df56f4d 11925row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
11926 struct window *w;
11927 int charpos;
11928 struct glyph_row *start, *end;
0df56f4d 11929 int dy;
e037b9ec
GM
11930{
11931 struct glyph_row *row = start;
11932 int last_y;
11933
11934 /* If we happen to start on a header-line, skip that. */
11935 if (row->mode_line_p)
11936 ++row;
2311178e 11937
e037b9ec
GM
11938 if ((end && row >= end) || !row->enabled_p)
11939 return NULL;
2311178e 11940
0df56f4d 11941 last_y = window_text_bottom_y (w) - dy;
2311178e 11942
5cce13dd
RS
11943 while (1)
11944 {
11945 /* Give up if we have gone too far. */
11946 if (end && row >= end)
11947 return NULL;
40a301b3
RS
11948 /* This formerly returned if they were equal.
11949 I think that both quantities are of a "last plus one" type;
11950 if so, when they are equal, the row is within the screen. -- rms. */
11951 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
11952 return NULL;
11953
11954 /* If it is in this row, return this row. */
11955 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 11956 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
11957 /* The end position of a row equals the start
11958 position of the next row. If CHARPOS is there, we
11959 would rather display it in the next line, except
11960 when this line ends in ZV. */
11961 && !row->ends_at_zv_p
5cce13dd
RS
11962 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11963 && charpos >= MATRIX_ROW_START_CHARPOS (row))
11964 return row;
11965 ++row;
11966 }
e037b9ec
GM
11967}
11968
11969
5f5c8ee5
GM
11970/* Try to redisplay window W by reusing its existing display. W's
11971 current matrix must be up to date when this function is called,
11972 i.e. window_end_valid must not be nil.
11973
11974 Value is
11975
11976 1 if display has been updated
11977 0 if otherwise unsuccessful
11978 -1 if redisplay with same window start is known not to succeed
11979
11980 The following steps are performed:
11981
11982 1. Find the last row in the current matrix of W that is not
11983 affected by changes at the start of current_buffer. If no such row
11984 is found, give up.
11985
11986 2. Find the first row in W's current matrix that is not affected by
11987 changes at the end of current_buffer. Maybe there is no such row.
11988
11989 3. Display lines beginning with the row + 1 found in step 1 to the
11990 row found in step 2 or, if step 2 didn't find a row, to the end of
11991 the window.
11992
11993 4. If cursor is not known to appear on the window, give up.
11994
11995 5. If display stopped at the row found in step 2, scroll the
11996 display and current matrix as needed.
11997
11998 6. Maybe display some lines at the end of W, if we must. This can
11999 happen under various circumstances, like a partially visible line
12000 becoming fully visible, or because newly displayed lines are displayed
12001 in smaller font sizes.
12002
12003 7. Update W's window end information. */
12004
12adba34 12005static int
5f5c8ee5 12006try_window_id (w)
12adba34 12007 struct window *w;
12adba34 12008{
5f5c8ee5
GM
12009 struct frame *f = XFRAME (w->frame);
12010 struct glyph_matrix *current_matrix = w->current_matrix;
12011 struct glyph_matrix *desired_matrix = w->desired_matrix;
12012 struct glyph_row *last_unchanged_at_beg_row;
12013 struct glyph_row *first_unchanged_at_end_row;
12014 struct glyph_row *row;
12015 struct glyph_row *bottom_row;
12016 int bottom_vpos;
12017 struct it it;
12018 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12019 struct text_pos start_pos;
12020 struct run run;
12021 int first_unchanged_at_end_vpos = 0;
12022 struct glyph_row *last_text_row, *last_text_row_at_end;
12023 struct text_pos start;
27d16f05 12024 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 12025
69d1f7c9 12026#if GLYPH_DEBUG
76cb5e06
GM
12027 if (inhibit_try_window_id)
12028 return 0;
12029#endif
12030
27d16f05
GM
12031 /* This is handy for debugging. */
12032#if 0
12033#define GIVE_UP(X) \
12034 do { \
12035 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12036 return 0; \
12037 } while (0)
12038#else
62397849 12039#define GIVE_UP(X) return 0
27d16f05 12040#endif
2311178e 12041
5f5c8ee5
GM
12042 SET_TEXT_POS_FROM_MARKER (start, w->start);
12043
27d16f05
GM
12044 /* Don't use this for mini-windows because these can show
12045 messages and mini-buffers, and we don't handle that here. */
12046 if (MINI_WINDOW_P (w))
12047 GIVE_UP (1);
2311178e 12048
27d16f05 12049 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 12050 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 12051 GIVE_UP (2);
2311178e 12052
c62c1bb5
RS
12053 /* Verify that narrowing has not changed.
12054 Also verify that we were not told to prevent redisplay optimizations.
12055 It would be nice to further
27d16f05 12056 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
12057 if (current_buffer->clip_changed
12058 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
12059 GIVE_UP (3);
12060
12061 /* Window must either use window-based redisplay or be full width. */
12062 if (!FRAME_WINDOW_P (f)
12063 && (!line_ins_del_ok
12064 || !WINDOW_FULL_WIDTH_P (w)))
12065 GIVE_UP (4);
5f5c8ee5 12066
f5376658 12067 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
12068 if (PT < CHARPOS (start))
12069 GIVE_UP (5);
12070
12071 /* Another way to prevent redisplay optimizations. */
12072 if (XFASTINT (w->last_modified) == 0)
12073 GIVE_UP (6);
2311178e 12074
f5376658 12075 /* Verify that window is not hscrolled. */
27d16f05
GM
12076 if (XFASTINT (w->hscroll) != 0)
12077 GIVE_UP (7);
2311178e 12078
f5376658 12079 /* Verify that display wasn't paused. */
27d16f05
GM
12080 if (NILP (w->window_end_valid))
12081 GIVE_UP (8);
2311178e 12082
27d16f05
GM
12083 /* Can't use this if highlighting a region because a cursor movement
12084 will do more than just set the cursor. */
12085 if (!NILP (Vtransient_mark_mode)
12086 && !NILP (current_buffer->mark_active))
12087 GIVE_UP (9);
12088
12089 /* Likewise if highlighting trailing whitespace. */
12090 if (!NILP (Vshow_trailing_whitespace))
12091 GIVE_UP (11);
2311178e 12092
27d16f05
GM
12093 /* Likewise if showing a region. */
12094 if (!NILP (w->region_showing))
12095 GIVE_UP (10);
2311178e 12096
27d16f05
GM
12097 /* Can use this if overlay arrow position and or string have changed. */
12098 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12099 || !EQ (last_arrow_string, Voverlay_arrow_string))
12100 GIVE_UP (12);
12101
2311178e 12102
5f5c8ee5
GM
12103 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12104 only if buffer has really changed. The reason is that the gap is
12105 initially at Z for freshly visited files. The code below would
12106 set end_unchanged to 0 in that case. */
28ee91c0
GM
12107 if (MODIFF > SAVE_MODIFF
12108 /* This seems to happen sometimes after saving a buffer. */
12109 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 12110 {
9142dd5b
GM
12111 if (GPT - BEG < BEG_UNCHANGED)
12112 BEG_UNCHANGED = GPT - BEG;
12113 if (Z - GPT < END_UNCHANGED)
12114 END_UNCHANGED = Z - GPT;
5f5c8ee5 12115 }
bf9249e3 12116
27d16f05
GM
12117 /* The position of the first and last character that has been changed. */
12118 first_changed_charpos = BEG + BEG_UNCHANGED;
12119 last_changed_charpos = Z - END_UNCHANGED;
12120
5f5c8ee5
GM
12121 /* If window starts after a line end, and the last change is in
12122 front of that newline, then changes don't affect the display.
f2d86d7a
GM
12123 This case happens with stealth-fontification. Note that although
12124 the display is unchanged, glyph positions in the matrix have to
12125 be adjusted, of course. */
5f5c8ee5 12126 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 12127 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 12128 && ((last_changed_charpos < CHARPOS (start)
27d16f05 12129 && CHARPOS (start) == BEGV)
e60f4527 12130 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
12131 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12132 {
12133 int Z_old, delta, Z_BYTE_old, delta_bytes;
12134 struct glyph_row *r0;
12135
12136 /* Compute how many chars/bytes have been added to or removed
12137 from the buffer. */
12138 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12139 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12140 delta = Z - Z_old;
12141 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 12142
27d16f05
GM
12143 /* Give up if PT is not in the window. Note that it already has
12144 been checked at the start of try_window_id that PT is not in
12145 front of the window start. */
12146 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12147 GIVE_UP (13);
12148
12149 /* If window start is unchanged, we can reuse the whole matrix
12150 as is, after adjusting glyph positions. No need to compute
12151 the window end again, since its offset from Z hasn't changed. */
12152 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12153 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12154 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
12155 {
12156 /* Adjust positions in the glyph matrix. */
12157 if (delta || delta_bytes)
12158 {
12159 struct glyph_row *r1
12160 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12161 increment_matrix_positions (w->current_matrix,
12162 MATRIX_ROW_VPOS (r0, current_matrix),
12163 MATRIX_ROW_VPOS (r1, current_matrix),
12164 delta, delta_bytes);
12165 }
2311178e 12166
27d16f05 12167 /* Set the cursor. */
0df56f4d 12168 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
12169 if (row)
12170 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
12171 else
12172 abort ();
27d16f05
GM
12173 return 1;
12174 }
5f5c8ee5
GM
12175 }
12176
27d16f05 12177 /* Handle the case that changes are all below what is displayed in
33ea6c4d 12178 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
12179 be taken if ZV is visible in the window, and text has been added
12180 there that is visible in the window. */
12181 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
12182 /* ZV is not visible in the window, or there are no
12183 changes at ZV, actually. */
12184 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12185 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 12186 {
27d16f05 12187 struct glyph_row *r0;
ef121659 12188
27d16f05
GM
12189 /* Give up if PT is not in the window. Note that it already has
12190 been checked at the start of try_window_id that PT is not in
12191 front of the window start. */
12192 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12193 GIVE_UP (14);
12194
12195 /* If window start is unchanged, we can reuse the whole matrix
12196 as is, without changing glyph positions since no text has
12197 been added/removed in front of the window end. */
12198 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12199 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
12200 {
12201 /* We have to compute the window end anew since text
12202 can have been added/removed after it. */
12203 w->window_end_pos
12204 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12205 w->window_end_bytepos
12206 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12207
12208 /* Set the cursor. */
0df56f4d 12209 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
12210 if (row)
12211 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
12212 else
12213 abort ();
27d16f05
GM
12214 return 2;
12215 }
5f5c8ee5
GM
12216 }
12217
2b9c25e0 12218 /* Give up if window start is in the changed area.
2311178e 12219
2b9c25e0
GM
12220 The condition used to read
12221
12222 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12223
12224 but why that was tested escapes me at the moment. */
12225 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
12226 && CHARPOS (start) <= last_changed_charpos)
12227 GIVE_UP (15);
2311178e 12228
f5376658
RS
12229 /* Check that window start agrees with the start of the first glyph
12230 row in its current matrix. Check this after we know the window
12231 start is not in changed text, otherwise positions would not be
12232 comparable. */
27d16f05 12233 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 12234 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 12235 GIVE_UP (16);
5f5c8ee5 12236
23e8bd86
GM
12237 /* Give up if the window ends in strings. Overlay strings
12238 at the end are difficult to handle, so don't try. */
12239 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12240 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12241 GIVE_UP (20);
12242
5f5c8ee5
GM
12243 /* Compute the position at which we have to start displaying new
12244 lines. Some of the lines at the top of the window might be
12245 reusable because they are not displaying changed text. Find the
12246 last row in W's current matrix not affected by changes at the
12247 start of current_buffer. Value is null if changes start in the
12248 first line of window. */
1ec185cb 12249 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
12250 if (last_unchanged_at_beg_row)
12251 {
67f1cf4c
GM
12252 /* Avoid starting to display in the moddle of a character, a TAB
12253 for instance. This is easier than to set up the iterator
12254 exactly, and it's not a frequent case, so the additional
12255 effort wouldn't really pay off. */
e74fb0f7
GM
12256 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12257 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
12258 && last_unchanged_at_beg_row > w->current_matrix->rows)
12259 --last_unchanged_at_beg_row;
12260
12261 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 12262 GIVE_UP (17);
47d57b22
GM
12263
12264 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
12265 GIVE_UP (18);
5f5c8ee5 12266 start_pos = it.current.pos;
2311178e 12267
5f5c8ee5
GM
12268 /* Start displaying new lines in the desired matrix at the same
12269 vpos we would use in the current matrix, i.e. below
12270 last_unchanged_at_beg_row. */
12271 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
12272 current_matrix);
12273 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12274 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
12275
12276 xassert (it.hpos == 0 && it.current_x == 0);
12277 }
12278 else
12279 {
12280 /* There are no reusable lines at the start of the window.
12281 Start displaying in the first line. */
12282 start_display (&it, w, start);
12283 start_pos = it.current.pos;
12284 }
12285
5f5c8ee5
GM
12286 /* Find the first row that is not affected by changes at the end of
12287 the buffer. Value will be null if there is no unchanged row, in
12288 which case we must redisplay to the end of the window. delta
12289 will be set to the value by which buffer positions beginning with
12290 first_unchanged_at_end_row have to be adjusted due to text
12291 changes. */
12292 first_unchanged_at_end_row
1ec185cb 12293 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
12294 IF_DEBUG (debug_delta = delta);
12295 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 12296
5f5c8ee5
GM
12297 /* Set stop_pos to the buffer position up to which we will have to
12298 display new lines. If first_unchanged_at_end_row != NULL, this
12299 is the buffer position of the start of the line displayed in that
12300 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12301 that we don't stop at a buffer position. */
12302 stop_pos = 0;
12303 if (first_unchanged_at_end_row)
12304 {
12305 xassert (last_unchanged_at_beg_row == NULL
12306 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 12307
5f5c8ee5
GM
12308 /* If this is a continuation line, move forward to the next one
12309 that isn't. Changes in lines above affect this line.
12310 Caution: this may move first_unchanged_at_end_row to a row
12311 not displaying text. */
12312 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
12313 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12314 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12315 < it.last_visible_y))
12316 ++first_unchanged_at_end_row;
12317
12318 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12319 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12320 >= it.last_visible_y))
12321 first_unchanged_at_end_row = NULL;
12322 else
12323 {
12324 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
12325 + delta);
12326 first_unchanged_at_end_vpos
12327 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 12328 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
12329 }
12330 }
12331 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 12332 GIVE_UP (19);
5f5c8ee5
GM
12333
12334
12335#if GLYPH_DEBUG
2311178e 12336
5f5c8ee5
GM
12337 /* Either there is no unchanged row at the end, or the one we have
12338 now displays text. This is a necessary condition for the window
12339 end pos calculation at the end of this function. */
12340 xassert (first_unchanged_at_end_row == NULL
12341 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 12342
5f5c8ee5
GM
12343 debug_last_unchanged_at_beg_vpos
12344 = (last_unchanged_at_beg_row
12345 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
12346 : -1);
12347 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 12348
5f5c8ee5
GM
12349#endif /* GLYPH_DEBUG != 0 */
12350
2311178e 12351
5f5c8ee5
GM
12352 /* Display new lines. Set last_text_row to the last new line
12353 displayed which has text on it, i.e. might end up as being the
12354 line where the window_end_vpos is. */
12355 w->cursor.vpos = -1;
12356 last_text_row = NULL;
12357 overlay_arrow_seen = 0;
12358 while (it.current_y < it.last_visible_y
12359 && !fonts_changed_p
12360 && (first_unchanged_at_end_row == NULL
12361 || IT_CHARPOS (it) < stop_pos))
12362 {
12363 if (display_line (&it))
12364 last_text_row = it.glyph_row - 1;
12365 }
12366
12367 if (fonts_changed_p)
12368 return -1;
12369
12370
12371 /* Compute differences in buffer positions, y-positions etc. for
12372 lines reused at the bottom of the window. Compute what we can
12373 scroll. */
ca42b2e8
GM
12374 if (first_unchanged_at_end_row
12375 /* No lines reused because we displayed everything up to the
12376 bottom of the window. */
12377 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
12378 {
12379 dvpos = (it.vpos
12380 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
12381 current_matrix));
12382 dy = it.current_y - first_unchanged_at_end_row->y;
12383 run.current_y = first_unchanged_at_end_row->y;
12384 run.desired_y = run.current_y + dy;
12385 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
12386 }
12387 else
ca42b2e8
GM
12388 {
12389 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
12390 first_unchanged_at_end_row = NULL;
12391 }
5f5c8ee5
GM
12392 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
12393
8f8ba186 12394
5f5c8ee5
GM
12395 /* Find the cursor if not already found. We have to decide whether
12396 PT will appear on this window (it sometimes doesn't, but this is
12397 not a very frequent case.) This decision has to be made before
12398 the current matrix is altered. A value of cursor.vpos < 0 means
12399 that PT is either in one of the lines beginning at
12400 first_unchanged_at_end_row or below the window. Don't care for
12401 lines that might be displayed later at the window end; as
12402 mentioned, this is not a frequent case. */
12403 if (w->cursor.vpos < 0)
12404 {
5f5c8ee5
GM
12405 /* Cursor in unchanged rows at the top? */
12406 if (PT < CHARPOS (start_pos)
12407 && last_unchanged_at_beg_row)
12408 {
e037b9ec
GM
12409 row = row_containing_pos (w, PT,
12410 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 12411 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
12412 if (row)
12413 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
12414 }
12415
12416 /* Start from first_unchanged_at_end_row looking for PT. */
12417 else if (first_unchanged_at_end_row)
12418 {
e037b9ec 12419 row = row_containing_pos (w, PT - delta,
0df56f4d 12420 first_unchanged_at_end_row, NULL, 0);
e037b9ec 12421 if (row)
468155d7
GM
12422 set_cursor_from_row (w, row, w->current_matrix, delta,
12423 delta_bytes, dy, dvpos);
5f5c8ee5
GM
12424 }
12425
12426 /* Give up if cursor was not found. */
12427 if (w->cursor.vpos < 0)
12428 {
12429 clear_glyph_matrix (w->desired_matrix);
12430 return -1;
12431 }
12432 }
2311178e 12433
5f5c8ee5
GM
12434 /* Don't let the cursor end in the scroll margins. */
12435 {
12436 int this_scroll_margin, cursor_height;
2311178e 12437
5f5c8ee5
GM
12438 this_scroll_margin = max (0, scroll_margin);
12439 this_scroll_margin = min (this_scroll_margin,
12440 XFASTINT (w->height) / 4);
12441 this_scroll_margin *= CANON_Y_UNIT (it.f);
12442 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 12443
5f5c8ee5
GM
12444 if ((w->cursor.y < this_scroll_margin
12445 && CHARPOS (start) > BEGV)
12446 /* Don't take scroll margin into account at the bottom because
12447 old redisplay didn't do it either. */
12448 || w->cursor.y + cursor_height > it.last_visible_y)
12449 {
12450 w->cursor.vpos = -1;
12451 clear_glyph_matrix (w->desired_matrix);
12452 return -1;
12453 }
12454 }
12455
12456 /* Scroll the display. Do it before changing the current matrix so
12457 that xterm.c doesn't get confused about where the cursor glyph is
12458 found. */
fa77249f 12459 if (dy && run.height)
5f5c8ee5
GM
12460 {
12461 update_begin (f);
2311178e 12462
5f5c8ee5
GM
12463 if (FRAME_WINDOW_P (f))
12464 {
12465 rif->update_window_begin_hook (w);
64d1e7d3 12466 rif->clear_mouse_face (w);
5f5c8ee5 12467 rif->scroll_run_hook (w, &run);
64d1e7d3 12468 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
12469 }
12470 else
12471 {
12472 /* Terminal frame. In this case, dvpos gives the number of
12473 lines to scroll by; dvpos < 0 means scroll up. */
12474 int first_unchanged_at_end_vpos
12475 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12476 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
522ed7fb
GM
12477 int end = (XFASTINT (w->top)
12478 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12479 + window_internal_height (w));
2311178e 12480
5f5c8ee5
GM
12481 /* Perform the operation on the screen. */
12482 if (dvpos > 0)
12483 {
12484 /* Scroll last_unchanged_at_beg_row to the end of the
12485 window down dvpos lines. */
12486 set_terminal_window (end);
12487
12488 /* On dumb terminals delete dvpos lines at the end
12489 before inserting dvpos empty lines. */
12490 if (!scroll_region_ok)
12491 ins_del_lines (end - dvpos, -dvpos);
12492
12493 /* Insert dvpos empty lines in front of
12494 last_unchanged_at_beg_row. */
12495 ins_del_lines (from, dvpos);
12496 }
12497 else if (dvpos < 0)
12498 {
12499 /* Scroll up last_unchanged_at_beg_vpos to the end of
12500 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12501 set_terminal_window (end);
12502
12503 /* Delete dvpos lines in front of
12504 last_unchanged_at_beg_vpos. ins_del_lines will set
12505 the cursor to the given vpos and emit |dvpos| delete
12506 line sequences. */
12507 ins_del_lines (from + dvpos, dvpos);
12508
12509 /* On a dumb terminal insert dvpos empty lines at the
12510 end. */
12511 if (!scroll_region_ok)
12512 ins_del_lines (end + dvpos, -dvpos);
12513 }
2311178e 12514
5f5c8ee5
GM
12515 set_terminal_window (0);
12516 }
2311178e 12517
5f5c8ee5
GM
12518 update_end (f);
12519 }
12520
ca42b2e8
GM
12521 /* Shift reused rows of the current matrix to the right position.
12522 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12523 text. */
12524 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12525 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
12526 if (dvpos < 0)
12527 {
12528 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12529 bottom_vpos, dvpos);
12530 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12531 bottom_vpos, 0);
12532 }
12533 else if (dvpos > 0)
12534 {
12535 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12536 bottom_vpos, dvpos);
12537 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12538 first_unchanged_at_end_vpos + dvpos, 0);
12539 }
12540
12541 /* For frame-based redisplay, make sure that current frame and window
12542 matrix are in sync with respect to glyph memory. */
12543 if (!FRAME_WINDOW_P (f))
12544 sync_frame_with_window_matrix_rows (w);
12545
12546 /* Adjust buffer positions in reused rows. */
12547 if (delta)
f2d86d7a
GM
12548 increment_matrix_positions (current_matrix,
12549 first_unchanged_at_end_vpos + dvpos,
12550 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
12551
12552 /* Adjust Y positions. */
12553 if (dy)
12554 shift_glyph_matrix (w, current_matrix,
12555 first_unchanged_at_end_vpos + dvpos,
12556 bottom_vpos, dy);
12557
12558 if (first_unchanged_at_end_row)
12559 first_unchanged_at_end_row += dvpos;
12560
12561 /* If scrolling up, there may be some lines to display at the end of
12562 the window. */
12563 last_text_row_at_end = NULL;
12564 if (dy < 0)
12565 {
9c6ba6d1
GM
12566 /* Scrolling up can leave for example a partially visible line
12567 at the end of the window to be redisplayed. */
5f5c8ee5
GM
12568 /* Set last_row to the glyph row in the current matrix where the
12569 window end line is found. It has been moved up or down in
12570 the matrix by dvpos. */
12571 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12572 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12573
12574 /* If last_row is the window end line, it should display text. */
12575 xassert (last_row->displays_text_p);
12576
12577 /* If window end line was partially visible before, begin
12578 displaying at that line. Otherwise begin displaying with the
12579 line following it. */
12580 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12581 {
12582 init_to_row_start (&it, w, last_row);
12583 it.vpos = last_vpos;
12584 it.current_y = last_row->y;
12585 }
12586 else
12587 {
12588 init_to_row_end (&it, w, last_row);
12589 it.vpos = 1 + last_vpos;
12590 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12591 ++last_row;
12592 }
12adba34 12593
23e8bd86
GM
12594 /* We may start in a continuation line. If so, we have to
12595 get the right continuation_lines_width and current_x. */
12596 it.continuation_lines_width = last_row->continuation_lines_width;
12597 it.hpos = it.current_x = 0;
2311178e 12598
23e8bd86
GM
12599 /* Display the rest of the lines at the window end. */
12600 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12601 while (it.current_y < it.last_visible_y
12602 && !fonts_changed_p)
5f5c8ee5 12603 {
23e8bd86
GM
12604 /* Is it always sure that the display agrees with lines in
12605 the current matrix? I don't think so, so we mark rows
12606 displayed invalid in the current matrix by setting their
12607 enabled_p flag to zero. */
12608 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12609 if (display_line (&it))
12610 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
12611 }
12612 }
12adba34 12613
5f5c8ee5
GM
12614 /* Update window_end_pos and window_end_vpos. */
12615 if (first_unchanged_at_end_row
12616 && first_unchanged_at_end_row->y < it.last_visible_y
12617 && !last_text_row_at_end)
12618 {
12619 /* Window end line if one of the preserved rows from the current
12620 matrix. Set row to the last row displaying text in current
12621 matrix starting at first_unchanged_at_end_row, after
12622 scrolling. */
12623 xassert (first_unchanged_at_end_row->displays_text_p);
12624 row = find_last_row_displaying_text (w->current_matrix, &it,
12625 first_unchanged_at_end_row);
12626 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 12627
ac90c44f 12628 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 12629 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
12630 w->window_end_vpos
12631 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 12632 xassert (w->window_end_bytepos >= 0);
0416532f 12633 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
12634 }
12635 else if (last_text_row_at_end)
12636 {
ac90c44f
GM
12637 w->window_end_pos
12638 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
12639 w->window_end_bytepos
12640 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
12641 w->window_end_vpos
12642 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 12643 xassert (w->window_end_bytepos >= 0);
0416532f 12644 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
12645 }
12646 else if (last_text_row)
12647 {
12648 /* We have displayed either to the end of the window or at the
12649 end of the window, i.e. the last row with text is to be found
12650 in the desired matrix. */
ac90c44f
GM
12651 w->window_end_pos
12652 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
12653 w->window_end_bytepos
12654 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12655 w->window_end_vpos
12656 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 12657 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
12658 }
12659 else if (first_unchanged_at_end_row == NULL
12660 && last_text_row == NULL
12661 && last_text_row_at_end == NULL)
12662 {
12663 /* Displayed to end of window, but no line containing text was
12664 displayed. Lines were deleted at the end of the window. */
0416532f
GM
12665 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12666 int vpos = XFASTINT (w->window_end_vpos);
12667 struct glyph_row *current_row = current_matrix->rows + vpos;
12668 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12669
12670 for (row = NULL;
12671 row == NULL && vpos >= first_vpos;
12672 --vpos, --current_row, --desired_row)
12673 {
12674 if (desired_row->enabled_p)
12675 {
12676 if (desired_row->displays_text_p)
12677 row = desired_row;
12678 }
12679 else if (current_row->displays_text_p)
12680 row = current_row;
12681 }
12adba34 12682
0416532f 12683 xassert (row != NULL);
d88a79d4 12684 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
12685 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12686 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 12687 xassert (w->window_end_bytepos >= 0);
0416532f 12688 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
12689 }
12690 else
12691 abort ();
ac90c44f 12692
8cdb267e
GM
12693#if 0 /* This leads to problems, for instance when the cursor is
12694 at ZV, and the cursor line displays no text. */
ac90c44f
GM
12695 /* Disable rows below what's displayed in the window. This makes
12696 debugging easier. */
12697 enable_glyph_matrix_rows (current_matrix,
12698 XFASTINT (w->window_end_vpos) + 1,
12699 bottom_vpos, 0);
8cdb267e 12700#endif
23e8bd86 12701
5f5c8ee5
GM
12702 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12703 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 12704
5f5c8ee5
GM
12705 /* Record that display has not been completed. */
12706 w->window_end_valid = Qnil;
12707 w->desired_matrix->no_scrolling_p = 1;
ef121659 12708 return 3;
27d16f05
GM
12709
12710#undef GIVE_UP
12adba34 12711}
0f9c0ff0 12712
a2889657 12713
5f5c8ee5
GM
12714\f
12715/***********************************************************************
12716 More debugging support
12717 ***********************************************************************/
a2889657 12718
5f5c8ee5 12719#if GLYPH_DEBUG
a2889657 12720
eaaa65b0 12721void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 12722void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 12723void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 12724
31b24551 12725
9ab436b9 12726/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 12727
9ab436b9
GM
12728 GLYPHS 0 means don't show glyph contents.
12729 GLYPHS 1 means show glyphs in short form
12730 GLYPHS > 1 means show glyphs in long form. */
12731
12732void
12733dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 12734 struct glyph_matrix *matrix;
9ab436b9 12735 int glyphs;
5f5c8ee5 12736{
efc63ef0 12737 int i;
5f5c8ee5 12738 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 12739 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 12740}
31b24551 12741
68a37fa8 12742
e187cf71
GM
12743/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12744 the glyph row and area where the glyph comes from. */
12745
12746void
12747dump_glyph (row, glyph, area)
12748 struct glyph_row *row;
12749 struct glyph *glyph;
12750 int area;
12751{
12752 if (glyph->type == CHAR_GLYPH)
12753 {
12754 fprintf (stderr,
12755 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12756 glyph - row->glyphs[TEXT_AREA],
12757 'C',
12758 glyph->charpos,
12759 (BUFFERP (glyph->object)
12760 ? 'B'
12761 : (STRINGP (glyph->object)
12762 ? 'S'
12763 : '-')),
12764 glyph->pixel_width,
12765 glyph->u.ch,
12766 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12767 ? glyph->u.ch
12768 : '.'),
12769 glyph->face_id,
12770 glyph->left_box_line_p,
12771 glyph->right_box_line_p);
12772 }
12773 else if (glyph->type == STRETCH_GLYPH)
12774 {
12775 fprintf (stderr,
12776 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12777 glyph - row->glyphs[TEXT_AREA],
12778 'S',
12779 glyph->charpos,
12780 (BUFFERP (glyph->object)
12781 ? 'B'
12782 : (STRINGP (glyph->object)
12783 ? 'S'
12784 : '-')),
12785 glyph->pixel_width,
12786 0,
12787 '.',
12788 glyph->face_id,
12789 glyph->left_box_line_p,
12790 glyph->right_box_line_p);
12791 }
12792 else if (glyph->type == IMAGE_GLYPH)
12793 {
12794 fprintf (stderr,
12795 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12796 glyph - row->glyphs[TEXT_AREA],
12797 'I',
12798 glyph->charpos,
12799 (BUFFERP (glyph->object)
12800 ? 'B'
12801 : (STRINGP (glyph->object)
12802 ? 'S'
12803 : '-')),
12804 glyph->pixel_width,
12805 glyph->u.img_id,
12806 '.',
12807 glyph->face_id,
12808 glyph->left_box_line_p,
12809 glyph->right_box_line_p);
12810 }
12811}
12812
12813
5f5c8ee5 12814/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
12815 GLYPHS 0 means don't show glyph contents.
12816 GLYPHS 1 means show glyphs in short form
12817 GLYPHS > 1 means show glyphs in long form. */
a2889657 12818
5f5c8ee5 12819void
eaaa65b0
GM
12820dump_glyph_row (row, vpos, glyphs)
12821 struct glyph_row *row;
9ab436b9 12822 int vpos, glyphs;
5f5c8ee5 12823{
9ab436b9
GM
12824 if (glyphs != 1)
12825 {
b94c0d9c 12826 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 12827 fprintf (stderr, "=======================================================================\n");
2311178e 12828
0ad38729 12829 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 12830%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 12831 vpos,
9ab436b9
GM
12832 MATRIX_ROW_START_CHARPOS (row),
12833 MATRIX_ROW_END_CHARPOS (row),
12834 row->used[TEXT_AREA],
12835 row->contains_overlapping_glyphs_p,
12836 row->enabled_p,
9ab436b9
GM
12837 row->truncated_on_left_p,
12838 row->truncated_on_right_p,
12839 row->overlay_arrow_p,
12840 row->continued_p,
12841 MATRIX_ROW_CONTINUATION_LINE_P (row),
12842 row->displays_text_p,
12843 row->ends_at_zv_p,
12844 row->fill_line_p,
12845 row->ends_in_middle_of_char_p,
12846 row->starts_in_middle_of_char_p,
b94c0d9c 12847 row->mouse_face_p,
9ab436b9
GM
12848 row->x,
12849 row->y,
12850 row->pixel_width,
12851 row->height,
12852 row->visible_height,
12853 row->ascent,
12854 row->phys_ascent);
33ea6c4d
GM
12855 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12856 row->end.overlay_string_index,
12857 row->continuation_lines_width);
9ab436b9
GM
12858 fprintf (stderr, "%9d %5d\n",
12859 CHARPOS (row->start.string_pos),
12860 CHARPOS (row->end.string_pos));
12861 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12862 row->end.dpvec_index);
12863 }
2311178e 12864
9ab436b9 12865 if (glyphs > 1)
bd66d1ba 12866 {
e187cf71
GM
12867 int area;
12868
12869 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12870 {
091f8878
GM
12871 struct glyph *glyph = row->glyphs[area];
12872 struct glyph *glyph_end = glyph + row->used[area];
2311178e 12873
e187cf71 12874 /* Glyph for a line end in text. */
091f8878 12875 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 12876 ++glyph_end;
2311178e 12877
e187cf71
GM
12878 if (glyph < glyph_end)
12879 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 12880
e187cf71
GM
12881 for (; glyph < glyph_end; ++glyph)
12882 dump_glyph (row, glyph, area);
f7b4b63a 12883 }
f4faa47c 12884 }
9ab436b9
GM
12885 else if (glyphs == 1)
12886 {
e187cf71 12887 int area;
9ab436b9 12888
e187cf71 12889 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 12890 {
e187cf71
GM
12891 char *s = (char *) alloca (row->used[area] + 1);
12892 int i;
12893
12894 for (i = 0; i < row->used[area]; ++i)
12895 {
12896 struct glyph *glyph = row->glyphs[area] + i;
12897 if (glyph->type == CHAR_GLYPH
12898 && glyph->u.ch < 0x80
12899 && glyph->u.ch >= ' ')
12900 s[i] = glyph->u.ch;
12901 else
12902 s[i] = '.';
12903 }
2311178e 12904
e187cf71
GM
12905 s[i] = '\0';
12906 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12907 }
9ab436b9 12908 }
5f5c8ee5 12909}
f4faa47c 12910
a2889657 12911
5f5c8ee5
GM
12912DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12913 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 12914 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
12915Shows contents of glyph row structures. With non-nil
12916parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
12917glyphs in short form, otherwise show glyphs in long form. */)
12918 (glyphs)
9ab436b9 12919 Lisp_Object glyphs;
5f5c8ee5
GM
12920{
12921 struct window *w = XWINDOW (selected_window);
12922 struct buffer *buffer = XBUFFER (w->buffer);
12923
12924 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12925 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12926 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12927 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12928 fprintf (stderr, "=============================================\n");
9ab436b9
GM
12929 dump_glyph_matrix (w->current_matrix,
12930 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
12931 return Qnil;
12932}
1c2250c2 12933
1fca3fae 12934
7d4cc828
GM
12935DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12936 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12937 ()
12938{
12939 struct frame *f = XFRAME (selected_frame);
12940 dump_glyph_matrix (f->current_matrix, 1);
12941 return Qnil;
12942}
12943
12944
e9abc296 12945DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 12946 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
12947GLYPH 0 means don't dump glyphs.
12948GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12949GLYPH > 1 or omitted means dump glyphs in long form. */)
12950 (row, glyphs)
e9abc296 12951 Lisp_Object row, glyphs;
5f5c8ee5 12952{
eaaa65b0
GM
12953 struct glyph_matrix *matrix;
12954 int vpos;
2311178e 12955
b7826503 12956 CHECK_NUMBER (row);
eaaa65b0
GM
12957 matrix = XWINDOW (selected_window)->current_matrix;
12958 vpos = XINT (row);
12959 if (vpos >= 0 && vpos < matrix->nrows)
12960 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12961 vpos,
12962 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12963 return Qnil;
12964}
1fca3fae 12965
67481ae5 12966
b94c0d9c 12967DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 12968 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
12969GLYPH 0 means don't dump glyphs.
12970GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12971GLYPH > 1 or omitted means dump glyphs in long form. */)
12972 (row, glyphs)
b94c0d9c 12973 Lisp_Object row, glyphs;
5f5c8ee5 12974{
886bd6f2 12975 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
12976 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12977 int vpos;
2311178e 12978
b7826503 12979 CHECK_NUMBER (row);
eaaa65b0
GM
12980 vpos = XINT (row);
12981 if (vpos >= 0 && vpos < m->nrows)
12982 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12983 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12984 return Qnil;
12985}
ca26e1c8 12986
0f9c0ff0 12987
62397849 12988DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
12989 doc: /* Toggle tracing of redisplay.
12990With ARG, turn tracing on if and only if ARG is positive. */)
12991 (arg)
62397849 12992 Lisp_Object arg;
5f5c8ee5 12993{
62397849
GM
12994 if (NILP (arg))
12995 trace_redisplay_p = !trace_redisplay_p;
12996 else
12997 {
12998 arg = Fprefix_numeric_value (arg);
12999 trace_redisplay_p = XINT (arg) > 0;
13000 }
2311178e 13001
5f5c8ee5
GM
13002 return Qnil;
13003}
bf9249e3
GM
13004
13005
f4a374a1 13006DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
13007 doc: /* Like `format', but print result to stderr.
13008usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 13009 (nargs, args)
f4a374a1
GM
13010 int nargs;
13011 Lisp_Object *args;
bf9249e3 13012{
f4a374a1 13013 Lisp_Object s = Fformat (nargs, args);
2051c264 13014 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
13015 return Qnil;
13016}
2311178e 13017
5f5c8ee5 13018#endif /* GLYPH_DEBUG */
ca26e1c8 13019
ca26e1c8 13020
5f5c8ee5
GM
13021\f
13022/***********************************************************************
13023 Building Desired Matrix Rows
13024 ***********************************************************************/
a2889657 13025
5f5c8ee5
GM
13026/* Return a temporary glyph row holding the glyphs of an overlay
13027 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 13028
5f5c8ee5
GM
13029static struct glyph_row *
13030get_overlay_arrow_glyph_row (w)
13031 struct window *w;
13032{
13033 struct frame *f = XFRAME (WINDOW_FRAME (w));
13034 struct buffer *buffer = XBUFFER (w->buffer);
13035 struct buffer *old = current_buffer;
50f80c2f 13036 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
2051c264 13037 int arrow_len = SCHARS (Voverlay_arrow_string);
50f80c2f
KR
13038 const unsigned char *arrow_end = arrow_string + arrow_len;
13039 const unsigned char *p;
5f5c8ee5
GM
13040 struct it it;
13041 int multibyte_p;
13042 int n_glyphs_before;
13043
13044 set_buffer_temp (buffer);
13045 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13046 it.glyph_row->used[TEXT_AREA] = 0;
13047 SET_TEXT_POS (it.position, 0, 0);
13048
13049 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13050 p = arrow_string;
13051 while (p < arrow_end)
13052 {
13053 Lisp_Object face, ilisp;
2311178e 13054
5f5c8ee5
GM
13055 /* Get the next character. */
13056 if (multibyte_p)
4fdb80f2 13057 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
13058 else
13059 it.c = *p, it.len = 1;
13060 p += it.len;
2311178e 13061
5f5c8ee5 13062 /* Get its face. */
ac90c44f 13063 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
13064 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13065 it.face_id = compute_char_face (f, it.c, face);
13066
13067 /* Compute its width, get its glyphs. */
13068 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 13069 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
13070 PRODUCE_GLYPHS (&it);
13071
13072 /* If this character doesn't fit any more in the line, we have
13073 to remove some glyphs. */
13074 if (it.current_x > it.last_visible_x)
13075 {
13076 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13077 break;
13078 }
13079 }
2311178e 13080
5f5c8ee5
GM
13081 set_buffer_temp (old);
13082 return it.glyph_row;
13083}
ca26e1c8 13084
b0a0fbda 13085
5f5c8ee5
GM
13086/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13087 glyphs are only inserted for terminal frames since we can't really
13088 win with truncation glyphs when partially visible glyphs are
13089 involved. Which glyphs to insert is determined by
13090 produce_special_glyphs. */
67481ae5 13091
5f5c8ee5
GM
13092static void
13093insert_left_trunc_glyphs (it)
13094 struct it *it;
13095{
13096 struct it truncate_it;
13097 struct glyph *from, *end, *to, *toend;
13098
13099 xassert (!FRAME_WINDOW_P (it->f));
13100
13101 /* Get the truncation glyphs. */
13102 truncate_it = *it;
5f5c8ee5
GM
13103 truncate_it.current_x = 0;
13104 truncate_it.face_id = DEFAULT_FACE_ID;
13105 truncate_it.glyph_row = &scratch_glyph_row;
13106 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13107 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 13108 truncate_it.object = make_number (0);
5f5c8ee5 13109 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 13110
5f5c8ee5
GM
13111 /* Overwrite glyphs from IT with truncation glyphs. */
13112 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13113 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13114 to = it->glyph_row->glyphs[TEXT_AREA];
13115 toend = to + it->glyph_row->used[TEXT_AREA];
13116
13117 while (from < end)
13118 *to++ = *from++;
13119
37be86f2
KH
13120 /* There may be padding glyphs left over. Overwrite them too. */
13121 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13122 {
13123 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13124 while (from < end)
13125 *to++ = *from++;
13126 }
5f5c8ee5 13127
37be86f2
KH
13128 if (to > toend)
13129 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 13130}
e0bfbde6 13131
e0bfbde6 13132
5f5c8ee5 13133/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 13134
5f5c8ee5
GM
13135 Most of the time, ascent and height of a display line will be equal
13136 to the max_ascent and max_height values of the display iterator
13137 structure. This is not the case if
67481ae5 13138
5f5c8ee5
GM
13139 1. We hit ZV without displaying anything. In this case, max_ascent
13140 and max_height will be zero.
1c9241f5 13141
5f5c8ee5
GM
13142 2. We have some glyphs that don't contribute to the line height.
13143 (The glyph row flag contributes_to_line_height_p is for future
13144 pixmap extensions).
f6fd109b 13145
5f5c8ee5
GM
13146 The first case is easily covered by using default values because in
13147 these cases, the line height does not really matter, except that it
13148 must not be zero. */
67481ae5 13149
5f5c8ee5
GM
13150static void
13151compute_line_metrics (it)
13152 struct it *it;
13153{
13154 struct glyph_row *row = it->glyph_row;
13155 int area, i;
1c2250c2 13156
5f5c8ee5
GM
13157 if (FRAME_WINDOW_P (it->f))
13158 {
75c5350a 13159 int i, min_y, max_y;
1c2250c2 13160
5f5c8ee5
GM
13161 /* The line may consist of one space only, that was added to
13162 place the cursor on it. If so, the row's height hasn't been
13163 computed yet. */
13164 if (row->height == 0)
13165 {
13166 if (it->max_ascent + it->max_descent == 0)
312246d1 13167 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
13168 row->ascent = it->max_ascent;
13169 row->height = it->max_ascent + it->max_descent;
312246d1
GM
13170 row->phys_ascent = it->max_phys_ascent;
13171 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5 13172 }
2311178e 13173
5f5c8ee5
GM
13174 /* Compute the width of this line. */
13175 row->pixel_width = row->x;
13176 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13177 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13178
13179 xassert (row->pixel_width >= 0);
13180 xassert (row->ascent >= 0 && row->height > 0);
13181
312246d1
GM
13182 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13183 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13184
13185 /* If first line's physical ascent is larger than its logical
13186 ascent, use the physical ascent, and make the row taller.
13187 This makes accented characters fully visible. */
b28cb6ed 13188 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
13189 && row->phys_ascent > row->ascent)
13190 {
13191 row->height += row->phys_ascent - row->ascent;
13192 row->ascent = row->phys_ascent;
13193 }
13194
5f5c8ee5
GM
13195 /* Compute how much of the line is visible. */
13196 row->visible_height = row->height;
2311178e 13197
75c5350a
GM
13198 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
13199 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
13200
13201 if (row->y < min_y)
13202 row->visible_height -= min_y - row->y;
13203 if (row->y + row->height > max_y)
13204 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
13205 }
13206 else
13207 {
13208 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
13209 if (row->continued_p)
13210 row->pixel_width -= it->continuation_pixel_width;
13211 else if (row->truncated_on_right_p)
13212 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
13213 row->ascent = row->phys_ascent = 0;
13214 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 13215 }
67481ae5 13216
5f5c8ee5
GM
13217 /* Compute a hash code for this row. */
13218 row->hash = 0;
13219 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13220 for (i = 0; i < row->used[area]; ++i)
13221 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13222 + row->glyphs[area][i].u.val
43d120d8
KH
13223 + row->glyphs[area][i].face_id
13224 + row->glyphs[area][i].padding_p
5f5c8ee5 13225 + (row->glyphs[area][i].type << 2));
a2889657 13226
5f5c8ee5 13227 it->max_ascent = it->max_descent = 0;
312246d1 13228 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 13229}
12adba34 13230
ca26e1c8 13231
5f5c8ee5
GM
13232/* Append one space to the glyph row of iterator IT if doing a
13233 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13234 space have the default face, otherwise let it have the same face as
80c6cb1f 13235 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
13236
13237 This function is called to make sure that there is always one glyph
13238 at the end of a glyph row that the cursor can be set on under
13239 window-systems. (If there weren't such a glyph we would not know
13240 how wide and tall a box cursor should be displayed).
13241
13242 At the same time this space let's a nicely handle clearing to the
13243 end of the line if the row ends in italic text. */
ca26e1c8 13244
80c6cb1f 13245static int
5f5c8ee5
GM
13246append_space (it, default_face_p)
13247 struct it *it;
13248 int default_face_p;
13249{
13250 if (FRAME_WINDOW_P (it->f))
13251 {
13252 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 13253
5f5c8ee5
GM
13254 if (it->glyph_row->glyphs[TEXT_AREA] + n
13255 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 13256 {
cafafe0b
GM
13257 /* Save some values that must not be changed.
13258 Must save IT->c and IT->len because otherwise
13259 ITERATOR_AT_END_P wouldn't work anymore after
13260 append_space has been called. */
478d746b 13261 enum display_element_type saved_what = it->what;
cafafe0b
GM
13262 int saved_c = it->c, saved_len = it->len;
13263 int saved_x = it->current_x;
5f5c8ee5 13264 int saved_face_id = it->face_id;
cafafe0b 13265 struct text_pos saved_pos;
5f5c8ee5 13266 Lisp_Object saved_object;
980806b6 13267 struct face *face;
5f5c8ee5
GM
13268
13269 saved_object = it->object;
13270 saved_pos = it->position;
2311178e 13271
5f5c8ee5
GM
13272 it->what = IT_CHARACTER;
13273 bzero (&it->position, sizeof it->position);
6fc556fd 13274 it->object = make_number (0);
5f5c8ee5
GM
13275 it->c = ' ';
13276 it->len = 1;
5f5c8ee5
GM
13277
13278 if (default_face_p)
13279 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
13280 else if (it->face_before_selective_p)
13281 it->face_id = it->saved_face_id;
980806b6
KH
13282 face = FACE_FROM_ID (it->f, it->face_id);
13283 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 13284
5f5c8ee5 13285 PRODUCE_GLYPHS (it);
2311178e 13286
5f5c8ee5
GM
13287 it->current_x = saved_x;
13288 it->object = saved_object;
13289 it->position = saved_pos;
13290 it->what = saved_what;
13291 it->face_id = saved_face_id;
cafafe0b
GM
13292 it->len = saved_len;
13293 it->c = saved_c;
80c6cb1f 13294 return 1;
5f5c8ee5
GM
13295 }
13296 }
80c6cb1f
GM
13297
13298 return 0;
5f5c8ee5 13299}
12adba34 13300
1842fc1a 13301
5f5c8ee5
GM
13302/* Extend the face of the last glyph in the text area of IT->glyph_row
13303 to the end of the display line. Called from display_line.
13304 If the glyph row is empty, add a space glyph to it so that we
13305 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 13306
5f5c8ee5
GM
13307static void
13308extend_face_to_end_of_line (it)
13309 struct it *it;
13310{
13311 struct face *face;
13312 struct frame *f = it->f;
1842fc1a 13313
5f5c8ee5
GM
13314 /* If line is already filled, do nothing. */
13315 if (it->current_x >= it->last_visible_x)
13316 return;
2311178e 13317
5f5c8ee5
GM
13318 /* Face extension extends the background and box of IT->face_id
13319 to the end of the line. If the background equals the background
4aad61f8
GM
13320 of the frame, we don't have to do anything. */
13321 if (it->face_before_selective_p)
13322 face = FACE_FROM_ID (it->f, it->saved_face_id);
13323 else
13324 face = FACE_FROM_ID (f, it->face_id);
2311178e 13325
5f5c8ee5
GM
13326 if (FRAME_WINDOW_P (f)
13327 && face->box == FACE_NO_BOX
13328 && face->background == FRAME_BACKGROUND_PIXEL (f)
13329 && !face->stipple)
13330 return;
1842fc1a 13331
5f5c8ee5
GM
13332 /* Set the glyph row flag indicating that the face of the last glyph
13333 in the text area has to be drawn to the end of the text area. */
13334 it->glyph_row->fill_line_p = 1;
545e04f6 13335
980806b6
KH
13336 /* If current character of IT is not ASCII, make sure we have the
13337 ASCII face. This will be automatically undone the next time
13338 get_next_display_element returns a multibyte character. Note
13339 that the character will always be single byte in unibyte text. */
13340 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 13341 {
980806b6 13342 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 13343 }
545e04f6 13344
5f5c8ee5
GM
13345 if (FRAME_WINDOW_P (f))
13346 {
13347 /* If the row is empty, add a space with the current face of IT,
13348 so that we know which face to draw. */
13349 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 13350 {
5f5c8ee5 13351 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 13352 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 13353 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 13354 }
5f5c8ee5
GM
13355 }
13356 else
13357 {
13358 /* Save some values that must not be changed. */
13359 int saved_x = it->current_x;
13360 struct text_pos saved_pos;
13361 Lisp_Object saved_object;
478d746b 13362 enum display_element_type saved_what = it->what;
4aad61f8 13363 int saved_face_id = it->face_id;
5f5c8ee5
GM
13364
13365 saved_object = it->object;
13366 saved_pos = it->position;
2311178e 13367
5f5c8ee5
GM
13368 it->what = IT_CHARACTER;
13369 bzero (&it->position, sizeof it->position);
6fc556fd 13370 it->object = make_number (0);
5f5c8ee5
GM
13371 it->c = ' ';
13372 it->len = 1;
4aad61f8 13373 it->face_id = face->id;
2311178e 13374
5f5c8ee5 13375 PRODUCE_GLYPHS (it);
2311178e 13376
5f5c8ee5
GM
13377 while (it->current_x <= it->last_visible_x)
13378 PRODUCE_GLYPHS (it);
2311178e 13379
5f5c8ee5
GM
13380 /* Don't count these blanks really. It would let us insert a left
13381 truncation glyph below and make us set the cursor on them, maybe. */
13382 it->current_x = saved_x;
13383 it->object = saved_object;
13384 it->position = saved_pos;
13385 it->what = saved_what;
4aad61f8 13386 it->face_id = saved_face_id;
5f5c8ee5
GM
13387 }
13388}
12adba34 13389
545e04f6 13390
5f5c8ee5
GM
13391/* Value is non-zero if text starting at CHARPOS in current_buffer is
13392 trailing whitespace. */
1c9241f5 13393
5f5c8ee5
GM
13394static int
13395trailing_whitespace_p (charpos)
13396 int charpos;
13397{
13398 int bytepos = CHAR_TO_BYTE (charpos);
13399 int c = 0;
7bbe686f 13400
5f5c8ee5
GM
13401 while (bytepos < ZV_BYTE
13402 && (c = FETCH_CHAR (bytepos),
13403 c == ' ' || c == '\t'))
13404 ++bytepos;
0d09d1e6 13405
8f897821
GM
13406 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
13407 {
13408 if (bytepos != PT_BYTE)
13409 return 1;
13410 }
13411 return 0;
5f5c8ee5 13412}
31b24551 13413
545e04f6 13414
5f5c8ee5 13415/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 13416
5f5c8ee5
GM
13417void
13418highlight_trailing_whitespace (f, row)
13419 struct frame *f;
13420 struct glyph_row *row;
13421{
13422 int used = row->used[TEXT_AREA];
2311178e 13423
5f5c8ee5
GM
13424 if (used)
13425 {
13426 struct glyph *start = row->glyphs[TEXT_AREA];
13427 struct glyph *glyph = start + used - 1;
13428
ade0bee1
GM
13429 /* Skip over glyphs inserted to display the cursor at the
13430 end of a line, for extending the face of the last glyph
13431 to the end of the line on terminals, and for truncation
13432 and continuation glyphs. */
65008712
GM
13433 while (glyph >= start
13434 && glyph->type == CHAR_GLYPH
65008712 13435 && INTEGERP (glyph->object))
5f5c8ee5
GM
13436 --glyph;
13437
13438 /* If last glyph is a space or stretch, and it's trailing
13439 whitespace, set the face of all trailing whitespace glyphs in
13440 IT->glyph_row to `trailing-whitespace'. */
13441 if (glyph >= start
13442 && BUFFERP (glyph->object)
13443 && (glyph->type == STRETCH_GLYPH
13444 || (glyph->type == CHAR_GLYPH
43d120d8 13445 && glyph->u.ch == ' '))
5f5c8ee5 13446 && trailing_whitespace_p (glyph->charpos))
545e04f6 13447 {
980806b6 13448 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
2311178e 13449
5f5c8ee5
GM
13450 while (glyph >= start
13451 && BUFFERP (glyph->object)
13452 && (glyph->type == STRETCH_GLYPH
13453 || (glyph->type == CHAR_GLYPH
43d120d8
KH
13454 && glyph->u.ch == ' ')))
13455 (glyph--)->face_id = face_id;
545e04f6 13456 }
a2889657 13457 }
5f5c8ee5 13458}
a2889657 13459
5fcbb24d 13460
cafafe0b 13461/* Value is non-zero if glyph row ROW in window W should be
0fd37545 13462 used to hold the cursor. */
cafafe0b
GM
13463
13464static int
13465cursor_row_p (w, row)
13466 struct window *w;
13467 struct glyph_row *row;
13468{
9a038881 13469 int cursor_row_p = 1;
2311178e 13470
cafafe0b
GM
13471 if (PT == MATRIX_ROW_END_CHARPOS (row))
13472 {
9a038881
GM
13473 /* If the row ends with a newline from a string, we don't want
13474 the cursor there (if the row is continued it doesn't end in a
13475 newline). */
13476 if (CHARPOS (row->end.string_pos) >= 0
13477 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13478 cursor_row_p = row->continued_p;
13479
13480 /* If the row ends at ZV, display the cursor at the end of that
13481 row instead of at the start of the row below. */
13482 else if (row->ends_at_zv_p)
13483 cursor_row_p = 1;
13484 else
13485 cursor_row_p = 0;
cafafe0b
GM
13486 }
13487
9a038881 13488 return cursor_row_p;
cafafe0b
GM
13489}
13490
13491
5f5c8ee5
GM
13492/* Construct the glyph row IT->glyph_row in the desired matrix of
13493 IT->w from text at the current position of IT. See dispextern.h
13494 for an overview of struct it. Value is non-zero if
13495 IT->glyph_row displays text, as opposed to a line displaying ZV
13496 only. */
2311178e 13497
5f5c8ee5
GM
13498static int
13499display_line (it)
13500 struct it *it;
13501{
13502 struct glyph_row *row = it->glyph_row;
13503
13504 /* We always start displaying at hpos zero even if hscrolled. */
13505 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 13506
5f5c8ee5
GM
13507 /* We must not display in a row that's not a text row. */
13508 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13509 < it->w->desired_matrix->nrows);
12adba34 13510
5f5c8ee5
GM
13511 /* Is IT->w showing the region? */
13512 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 13513
5f5c8ee5
GM
13514 /* Clear the result glyph row and enable it. */
13515 prepare_desired_row (row);
12adba34 13516
5f5c8ee5
GM
13517 row->y = it->current_y;
13518 row->start = it->current;
13519 row->continuation_lines_width = it->continuation_lines_width;
13520 row->displays_text_p = 1;
91004049
GM
13521 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13522 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
13523
13524 /* Arrange the overlays nicely for our purposes. Usually, we call
13525 display_line on only one line at a time, in which case this
13526 can't really hurt too much, or we call it on lines which appear
13527 one after another in the buffer, in which case all calls to
13528 recenter_overlay_lists but the first will be pretty cheap. */
13529 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13530
5f5c8ee5
GM
13531 /* Move over display elements that are not visible because we are
13532 hscrolled. This may stop at an x-position < IT->first_visible_x
13533 if the first glyph is partially visible or if we hit a line end. */
13534 if (it->current_x < it->first_visible_x)
13535 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13536 MOVE_TO_POS | MOVE_TO_X);
13537
13538 /* Get the initial row height. This is either the height of the
13539 text hscrolled, if there is any, or zero. */
13540 row->ascent = it->max_ascent;
13541 row->height = it->max_ascent + it->max_descent;
312246d1
GM
13542 row->phys_ascent = it->max_phys_ascent;
13543 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
13544
13545 /* Loop generating characters. The loop is left with IT on the next
13546 character to display. */
13547 while (1)
13548 {
13549 int n_glyphs_before, hpos_before, x_before;
13550 int x, i, nglyphs;
ae26e27d 13551 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 13552
5f5c8ee5
GM
13553 /* Retrieve the next thing to display. Value is zero if end of
13554 buffer reached. */
13555 if (!get_next_display_element (it))
13556 {
13557 /* Maybe add a space at the end of this line that is used to
1b335865
GM
13558 display the cursor there under X. Set the charpos of the
13559 first glyph of blank lines not corresponding to any text
13560 to -1. */
13561 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13562 || row->used[TEXT_AREA] == 0)
a2889657 13563 {
5f5c8ee5
GM
13564 row->glyphs[TEXT_AREA]->charpos = -1;
13565 row->displays_text_p = 0;
13566
2ad551af 13567 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
13568 && (!MINI_WINDOW_P (it->w)
13569 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 13570 row->indicate_empty_line_p = 1;
a2889657 13571 }
2311178e 13572
5f5c8ee5
GM
13573 it->continuation_lines_width = 0;
13574 row->ends_at_zv_p = 1;
13575 break;
a2889657 13576 }
a2889657 13577
5f5c8ee5
GM
13578 /* Now, get the metrics of what we want to display. This also
13579 generates glyphs in `row' (which is IT->glyph_row). */
13580 n_glyphs_before = row->used[TEXT_AREA];
13581 x = it->current_x;
e6819faf
GM
13582
13583 /* Remember the line height so far in case the next element doesn't
13584 fit on the line. */
13585 if (!it->truncate_lines_p)
13586 {
13587 ascent = it->max_ascent;
13588 descent = it->max_descent;
13589 phys_ascent = it->max_phys_ascent;
13590 phys_descent = it->max_phys_descent;
13591 }
2311178e 13592
5f5c8ee5 13593 PRODUCE_GLYPHS (it);
a2889657 13594
5f5c8ee5
GM
13595 /* If this display element was in marginal areas, continue with
13596 the next one. */
13597 if (it->area != TEXT_AREA)
a2889657 13598 {
5f5c8ee5
GM
13599 row->ascent = max (row->ascent, it->max_ascent);
13600 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13601 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13602 row->phys_height = max (row->phys_height,
13603 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 13604 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13605 continue;
13606 }
5936754e 13607
5f5c8ee5
GM
13608 /* Does the display element fit on the line? If we truncate
13609 lines, we should draw past the right edge of the window. If
13610 we don't truncate, we want to stop so that we can display the
13611 continuation glyph before the right margin. If lines are
13612 continued, there are two possible strategies for characters
13613 resulting in more than 1 glyph (e.g. tabs): Display as many
13614 glyphs as possible in this line and leave the rest for the
13615 continuation line, or display the whole element in the next
13616 line. Original redisplay did the former, so we do it also. */
13617 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13618 hpos_before = it->hpos;
13619 x_before = x;
4dcd74e6 13620
4b41cebb 13621 if (/* Not a newline. */
4dcd74e6 13622 nglyphs > 0
202c1b5b 13623 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
13624 && it->current_x < it->last_visible_x)
13625 {
37be86f2 13626 it->hpos += nglyphs;
5f5c8ee5
GM
13627 row->ascent = max (row->ascent, it->max_ascent);
13628 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13629 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13630 row->phys_height = max (row->phys_height,
13631 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13632 if (it->current_x - it->pixel_width < it->first_visible_x)
13633 row->x = x - it->first_visible_x;
13634 }
13635 else
13636 {
13637 int new_x;
13638 struct glyph *glyph;
2311178e 13639
5f5c8ee5 13640 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 13641 {
5f5c8ee5
GM
13642 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13643 new_x = x + glyph->pixel_width;
13644
13645 if (/* Lines are continued. */
13646 !it->truncate_lines_p
13647 && (/* Glyph doesn't fit on the line. */
13648 new_x > it->last_visible_x
13649 /* Or it fits exactly on a window system frame. */
13650 || (new_x == it->last_visible_x
13651 && FRAME_WINDOW_P (it->f))))
a2889657 13652 {
5f5c8ee5 13653 /* End of a continued line. */
2311178e 13654
5f5c8ee5
GM
13655 if (it->hpos == 0
13656 || (new_x == it->last_visible_x
13657 && FRAME_WINDOW_P (it->f)))
13658 {
e6819faf
GM
13659 /* Current glyph is the only one on the line or
13660 fits exactly on the line. We must continue
13661 the line because we can't draw the cursor
13662 after the glyph. */
5f5c8ee5
GM
13663 row->continued_p = 1;
13664 it->current_x = new_x;
13665 it->continuation_lines_width += new_x;
13666 ++it->hpos;
13667 if (i == nglyphs - 1)
cafafe0b 13668 set_iterator_to_next (it, 1);
5f5c8ee5 13669 }
3b3c4bf0
GM
13670 else if (CHAR_GLYPH_PADDING_P (*glyph)
13671 && !FRAME_WINDOW_P (it->f))
13672 {
13673 /* A padding glyph that doesn't fit on this line.
13674 This means the whole character doesn't fit
13675 on the line. */
13676 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 13677
3b3c4bf0
GM
13678 /* Fill the rest of the row with continuation
13679 glyphs like in 20.x. */
13680 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13681 < row->glyphs[1 + TEXT_AREA])
13682 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 13683
3b3c4bf0
GM
13684 row->continued_p = 1;
13685 it->current_x = x_before;
13686 it->continuation_lines_width += x_before;
2311178e 13687
3b3c4bf0
GM
13688 /* Restore the height to what it was before the
13689 element not fitting on the line. */
13690 it->max_ascent = ascent;
13691 it->max_descent = descent;
13692 it->max_phys_ascent = phys_ascent;
13693 it->max_phys_descent = phys_descent;
13694 }
0df56f4d
GM
13695 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13696 {
13697 /* A TAB that extends past the right edge of the
13698 window. This produces a single glyph on
13699 window system frames. We leave the glyph in
13700 this row and let it fill the row, but don't
13701 consume the TAB. */
13702 it->continuation_lines_width += it->last_visible_x;
13703 row->ends_in_middle_of_char_p = 1;
13704 row->continued_p = 1;
13705 glyph->pixel_width = it->last_visible_x - x;
13706 it->starts_in_middle_of_char_p = 1;
13707 }
5f5c8ee5 13708 else
5936754e 13709 {
0df56f4d
GM
13710 /* Something other than a TAB that draws past
13711 the right edge of the window. Restore
13712 positions to values before the element. */
5f5c8ee5 13713 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 13714
5f5c8ee5
GM
13715 /* Display continuation glyphs. */
13716 if (!FRAME_WINDOW_P (it->f))
13717 produce_special_glyphs (it, IT_CONTINUATION);
13718 row->continued_p = 1;
653c329b 13719
0df56f4d 13720 it->continuation_lines_width += x;
2311178e 13721
91004049
GM
13722 if (nglyphs > 1 && i > 0)
13723 {
13724 row->ends_in_middle_of_char_p = 1;
13725 it->starts_in_middle_of_char_p = 1;
13726 }
2311178e 13727
e6819faf
GM
13728 /* Restore the height to what it was before the
13729 element not fitting on the line. */
13730 it->max_ascent = ascent;
13731 it->max_descent = descent;
13732 it->max_phys_ascent = phys_ascent;
13733 it->max_phys_descent = phys_descent;
5936754e 13734 }
e6819faf 13735
5f5c8ee5
GM
13736 break;
13737 }
13738 else if (new_x > it->first_visible_x)
13739 {
13740 /* Increment number of glyphs actually displayed. */
13741 ++it->hpos;
2311178e 13742
5f5c8ee5
GM
13743 if (x < it->first_visible_x)
13744 /* Glyph is partially visible, i.e. row starts at
13745 negative X position. */
13746 row->x = x - it->first_visible_x;
13747 }
13748 else
13749 {
13750 /* Glyph is completely off the left margin of the
13751 window. This should not happen because of the
72b8c434
RS
13752 move_it_in_display_line at the start of this
13753 function, unless the text display area of the
13754 window is empty. */
13755 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 13756 }
a2889657 13757 }
2311178e 13758
5f5c8ee5
GM
13759 row->ascent = max (row->ascent, it->max_ascent);
13760 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13761 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13762 row->phys_height = max (row->phys_height,
13763 it->max_phys_ascent + it->max_phys_descent);
2311178e 13764
5f5c8ee5
GM
13765 /* End of this display line if row is continued. */
13766 if (row->continued_p)
13767 break;
a2889657 13768 }
a2889657 13769
5f5c8ee5
GM
13770 /* Is this a line end? If yes, we're also done, after making
13771 sure that a non-default face is extended up to the right
13772 margin of the window. */
13773 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 13774 {
5f5c8ee5
GM
13775 int used_before = row->used[TEXT_AREA];
13776
e74fb0f7
GM
13777 row->ends_in_newline_from_string_p = STRINGP (it->object);
13778
5f5c8ee5
GM
13779 /* Add a space at the end of the line that is used to
13780 display the cursor there. */
13781 append_space (it, 0);
2311178e 13782
5f5c8ee5
GM
13783 /* Extend the face to the end of the line. */
13784 extend_face_to_end_of_line (it);
13785
13786 /* Make sure we have the position. */
13787 if (used_before == 0)
13788 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 13789
5f5c8ee5 13790 /* Consume the line end. This skips over invisible lines. */
cafafe0b 13791 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13792 it->continuation_lines_width = 0;
13793 break;
1c9241f5 13794 }
a2889657 13795
2311178e 13796 /* Proceed with next display element. Note that this skips
5f5c8ee5 13797 over lines invisible because of selective display. */
cafafe0b 13798 set_iterator_to_next (it, 1);
b1d1124b 13799
5f5c8ee5
GM
13800 /* If we truncate lines, we are done when the last displayed
13801 glyphs reach past the right margin of the window. */
13802 if (it->truncate_lines_p
13803 && (FRAME_WINDOW_P (it->f)
13804 ? (it->current_x >= it->last_visible_x)
13805 : (it->current_x > it->last_visible_x)))
75d13c64 13806 {
5f5c8ee5
GM
13807 /* Maybe add truncation glyphs. */
13808 if (!FRAME_WINDOW_P (it->f))
13809 {
a98b5ed9 13810 int i, n;
2311178e 13811
a98b5ed9
GM
13812 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13813 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13814 break;
13815
13816 for (n = row->used[TEXT_AREA]; i < n; ++i)
13817 {
13818 row->used[TEXT_AREA] = i;
13819 produce_special_glyphs (it, IT_TRUNCATION);
13820 }
5f5c8ee5 13821 }
2311178e 13822
5f5c8ee5
GM
13823 row->truncated_on_right_p = 1;
13824 it->continuation_lines_width = 0;
312246d1 13825 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
13826 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13827 it->hpos = hpos_before;
13828 it->current_x = x_before;
13829 break;
75d13c64 13830 }
a2889657 13831 }
a2889657 13832
5f5c8ee5
GM
13833 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13834 at the left window margin. */
13835 if (it->first_visible_x
13836 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13837 {
13838 if (!FRAME_WINDOW_P (it->f))
13839 insert_left_trunc_glyphs (it);
13840 row->truncated_on_left_p = 1;
13841 }
a2889657 13842
5f5c8ee5
GM
13843 /* If the start of this line is the overlay arrow-position, then
13844 mark this glyph row as the one containing the overlay arrow.
13845 This is clearly a mess with variable size fonts. It would be
13846 better to let it be displayed like cursors under X. */
e24c997d 13847 if (MARKERP (Voverlay_arrow_position)
a2889657 13848 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
13849 && (MATRIX_ROW_START_CHARPOS (row)
13850 == marker_position (Voverlay_arrow_position))
e24c997d 13851 && STRINGP (Voverlay_arrow_string)
a2889657
JB
13852 && ! overlay_arrow_seen)
13853 {
b46952ae 13854 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 13855 if (!FRAME_WINDOW_P (it->f))
c4628384 13856 {
5f5c8ee5
GM
13857 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13858 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13859 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13860 struct glyph *p = row->glyphs[TEXT_AREA];
13861 struct glyph *p2, *end;
13862
13863 /* Copy the arrow glyphs. */
13864 while (glyph < arrow_end)
13865 *p++ = *glyph++;
13866
13867 /* Throw away padding glyphs. */
13868 p2 = p;
13869 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13870 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13871 ++p2;
13872 if (p2 > p)
212e4f87 13873 {
5f5c8ee5
GM
13874 while (p2 < end)
13875 *p++ = *p2++;
13876 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 13877 }
c4628384 13878 }
2311178e 13879
a2889657 13880 overlay_arrow_seen = 1;
5f5c8ee5 13881 row->overlay_arrow_p = 1;
a2889657
JB
13882 }
13883
5f5c8ee5
GM
13884 /* Compute pixel dimensions of this line. */
13885 compute_line_metrics (it);
13886
13887 /* Remember the position at which this line ends. */
13888 row->end = it->current;
13889
173cbca8 13890 /* Maybe set the cursor. */
5f5c8ee5
GM
13891 if (it->w->cursor.vpos < 0
13892 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
13893 && PT <= MATRIX_ROW_END_CHARPOS (row)
13894 && cursor_row_p (it->w, row))
13895 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13896
13897 /* Highlight trailing whitespace. */
8f897821 13898 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
13899 highlight_trailing_whitespace (it->f, it->glyph_row);
13900
13901 /* Prepare for the next line. This line starts horizontally at (X
13902 HPOS) = (0 0). Vertical positions are incremented. As a
13903 convenience for the caller, IT->glyph_row is set to the next
13904 row to be used. */
13905 it->current_x = it->hpos = 0;
13906 it->current_y += row->height;
13907 ++it->vpos;
13908 ++it->glyph_row;
13909 return row->displays_text_p;
a2889657 13910}
5f5c8ee5
GM
13911
13912
a2889657 13913\f
5f5c8ee5
GM
13914/***********************************************************************
13915 Menu Bar
13916 ***********************************************************************/
13917
13918/* Redisplay the menu bar in the frame for window W.
13919
13920 The menu bar of X frames that don't have X toolkit support is
13921 displayed in a special window W->frame->menu_bar_window.
2311178e 13922
5f5c8ee5
GM
13923 The menu bar of terminal frames is treated specially as far as
13924 glyph matrices are concerned. Menu bar lines are not part of
13925 windows, so the update is done directly on the frame matrix rows
13926 for the menu bar. */
7ce2c095
RS
13927
13928static void
13929display_menu_bar (w)
13930 struct window *w;
13931{
5f5c8ee5
GM
13932 struct frame *f = XFRAME (WINDOW_FRAME (w));
13933 struct it it;
13934 Lisp_Object items;
8351baf2 13935 int i;
7ce2c095 13936
5f5c8ee5 13937 /* Don't do all this for graphical frames. */
dc937613 13938#ifdef HAVE_NTGUI
d129c4c2
KH
13939 if (!NILP (Vwindow_system))
13940 return;
dc937613 13941#endif
488dd4c4 13942#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 13943 if (FRAME_X_P (f))
7ce2c095 13944 return;
5f5c8ee5 13945#endif
e0f712ba 13946#ifdef MAC_OS
1a578e9b
AC
13947 if (FRAME_MAC_P (f))
13948 return;
13949#endif
5f5c8ee5
GM
13950
13951#ifdef USE_X_TOOLKIT
13952 xassert (!FRAME_WINDOW_P (f));
52377a47 13953 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
13954 it.first_visible_x = 0;
13955 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13956#else /* not USE_X_TOOLKIT */
13957 if (FRAME_WINDOW_P (f))
13958 {
13959 /* Menu bar lines are displayed in the desired matrix of the
13960 dummy window menu_bar_window. */
13961 struct window *menu_w;
13962 xassert (WINDOWP (f->menu_bar_window));
13963 menu_w = XWINDOW (f->menu_bar_window);
13964 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 13965 MENU_FACE_ID);
5f5c8ee5
GM
13966 it.first_visible_x = 0;
13967 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13968 }
13969 else
13970 {
13971 /* This is a TTY frame, i.e. character hpos/vpos are used as
13972 pixel x/y. */
13973 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 13974 MENU_FACE_ID);
5f5c8ee5
GM
13975 it.first_visible_x = 0;
13976 it.last_visible_x = FRAME_WIDTH (f);
13977 }
13978#endif /* not USE_X_TOOLKIT */
13979
1862a24e
MB
13980 if (! mode_line_inverse_video)
13981 /* Force the menu-bar to be displayed in the default face. */
13982 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13983
5f5c8ee5
GM
13984 /* Clear all rows of the menu bar. */
13985 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13986 {
13987 struct glyph_row *row = it.glyph_row + i;
13988 clear_glyph_row (row);
13989 row->enabled_p = 1;
13990 row->full_width_p = 1;
13991 }
7ce2c095 13992
5f5c8ee5
GM
13993 /* Display all items of the menu bar. */
13994 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 13995 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 13996 {
5f5c8ee5
GM
13997 Lisp_Object string;
13998
13999 /* Stop at nil string. */
a61b7058 14000 string = AREF (items, i + 1);
8351baf2
RS
14001 if (NILP (string))
14002 break;
2d66ad19 14003
5f5c8ee5 14004 /* Remember where item was displayed. */
a61b7058 14005 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 14006
5f5c8ee5
GM
14007 /* Display the item, pad with one space. */
14008 if (it.current_x < it.last_visible_x)
14009 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 14010 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
14011 }
14012
2d66ad19 14013 /* Fill out the line with spaces. */
5f5c8ee5
GM
14014 if (it.current_x < it.last_visible_x)
14015 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 14016
5f5c8ee5
GM
14017 /* Compute the total height of the lines. */
14018 compute_line_metrics (&it);
7ce2c095 14019}
5f5c8ee5
GM
14020
14021
7ce2c095 14022\f
5f5c8ee5
GM
14023/***********************************************************************
14024 Mode Line
14025 ***********************************************************************/
14026
715e84c9
GM
14027/* Redisplay mode lines in the window tree whose root is WINDOW. If
14028 FORCE is non-zero, redisplay mode lines unconditionally.
14029 Otherwise, redisplay only mode lines that are garbaged. Value is
14030 the number of windows whose mode lines were redisplayed. */
a2889657 14031
715e84c9
GM
14032static int
14033redisplay_mode_lines (window, force)
14034 Lisp_Object window;
14035 int force;
14036{
14037 int nwindows = 0;
2311178e 14038
715e84c9
GM
14039 while (!NILP (window))
14040 {
14041 struct window *w = XWINDOW (window);
2311178e 14042
715e84c9
GM
14043 if (WINDOWP (w->hchild))
14044 nwindows += redisplay_mode_lines (w->hchild, force);
14045 else if (WINDOWP (w->vchild))
14046 nwindows += redisplay_mode_lines (w->vchild, force);
14047 else if (force
14048 || FRAME_GARBAGED_P (XFRAME (w->frame))
14049 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14050 {
715e84c9
GM
14051 struct text_pos lpoint;
14052 struct buffer *old = current_buffer;
14053
14054 /* Set the window's buffer for the mode line display. */
14055 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14056 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 14057
715e84c9
GM
14058 /* Point refers normally to the selected window. For any
14059 other window, set up appropriate value. */
14060 if (!EQ (window, selected_window))
14061 {
14062 struct text_pos pt;
2311178e 14063
715e84c9
GM
14064 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14065 if (CHARPOS (pt) < BEGV)
14066 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14067 else if (CHARPOS (pt) > (ZV - 1))
14068 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14069 else
14070 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14071 }
14072
715e84c9
GM
14073 /* Display mode lines. */
14074 clear_glyph_matrix (w->desired_matrix);
14075 if (display_mode_lines (w))
14076 {
14077 ++nwindows;
14078 w->must_be_updated_p = 1;
14079 }
14080
14081 /* Restore old settings. */
715e84c9
GM
14082 set_buffer_internal_1 (old);
14083 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14084 }
14085
14086 window = w->next;
14087 }
14088
14089 return nwindows;
14090}
14091
14092
14093/* Display the mode and/or top line of window W. Value is the number
14094 of mode lines displayed. */
14095
14096static int
5f5c8ee5 14097display_mode_lines (w)
a2889657
JB
14098 struct window *w;
14099{
edd1e654 14100 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 14101 int n = 0;
edd1e654
GM
14102
14103 old_selected_frame = selected_frame;
14104 selected_frame = w->frame;
14105 old_selected_window = selected_window;
14106 XSETWINDOW (selected_window, w);
2311178e 14107
5f5c8ee5 14108 /* These will be set while the mode line specs are processed. */
aa6d10fa 14109 line_number_displayed = 0;
155ef550 14110 w->column_number_displayed = Qnil;
aa6d10fa 14111
5f5c8ee5 14112 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 14113 {
c96e83bf 14114 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 14115
96d2320f 14116 /* Select mode line face based on the real selected window. */
c96e83bf 14117 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
14118 current_buffer->mode_line_format);
14119 ++n;
14120 }
2311178e 14121
045dee35 14122 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
14123 {
14124 display_mode_line (w, HEADER_LINE_FACE_ID,
14125 current_buffer->header_line_format);
14126 ++n;
14127 }
14128
edd1e654
GM
14129 selected_frame = old_selected_frame;
14130 selected_window = old_selected_window;
715e84c9 14131 return n;
5f5c8ee5 14132}
03b294dc 14133
03b294dc 14134
5f5c8ee5 14135/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 14136 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
14137 FORMAT is the mode line format to display. Value is the pixel
14138 height of the mode line displayed. */
03b294dc 14139
04612a64 14140static int
5f5c8ee5
GM
14141display_mode_line (w, face_id, format)
14142 struct window *w;
14143 enum face_id face_id;
14144 Lisp_Object format;
14145{
14146 struct it it;
14147 struct face *face;
03b294dc 14148
5f5c8ee5
GM
14149 init_iterator (&it, w, -1, -1, NULL, face_id);
14150 prepare_desired_row (it.glyph_row);
14151
1862a24e
MB
14152 if (! mode_line_inverse_video)
14153 /* Force the mode-line to be displayed in the default face. */
14154 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14155
5f5c8ee5
GM
14156 /* Temporarily make frame's keyboard the current kboard so that
14157 kboard-local variables in the mode_line_format will get the right
14158 values. */
14159 push_frame_kboard (it.f);
c53a1624 14160 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 14161 pop_frame_kboard ();
a2889657 14162
5f5c8ee5
GM
14163 /* Fill up with spaces. */
14164 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 14165
5f5c8ee5
GM
14166 compute_line_metrics (&it);
14167 it.glyph_row->full_width_p = 1;
14168 it.glyph_row->mode_line_p = 1;
5f5c8ee5
GM
14169 it.glyph_row->continued_p = 0;
14170 it.glyph_row->truncated_on_left_p = 0;
14171 it.glyph_row->truncated_on_right_p = 0;
14172
14173 /* Make a 3D mode-line have a shadow at its right end. */
14174 face = FACE_FROM_ID (it.f, face_id);
14175 extend_face_to_end_of_line (&it);
14176 if (face->box != FACE_NO_BOX)
d7eb09a0 14177 {
5f5c8ee5
GM
14178 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14179 + it.glyph_row->used[TEXT_AREA] - 1);
14180 last->right_box_line_p = 1;
d7eb09a0 14181 }
04612a64
GM
14182
14183 return it.glyph_row->height;
a2889657
JB
14184}
14185
0fcf414f
RS
14186/* Alist that caches the results of :propertize.
14187 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14188Lisp_Object mode_line_proptrans_alist;
a2889657 14189
fec8f23e
KS
14190/* List of strings making up the mode-line. */
14191Lisp_Object mode_line_string_list;
14192
14193/* Base face property when building propertized mode line string. */
14194static Lisp_Object mode_line_string_face;
14195static Lisp_Object mode_line_string_face_prop;
14196
14197
5f5c8ee5
GM
14198/* Contribute ELT to the mode line for window IT->w. How it
14199 translates into text depends on its data type.
a2889657 14200
5f5c8ee5 14201 IT describes the display environment in which we display, as usual.
a2889657
JB
14202
14203 DEPTH is the depth in recursion. It is used to prevent
14204 infinite recursion here.
14205
5f5c8ee5
GM
14206 FIELD_WIDTH is the number of characters the display of ELT should
14207 occupy in the mode line, and PRECISION is the maximum number of
14208 characters to display from ELT's representation. See
b8523839 14209 display_string for details.
a2889657 14210
c53a1624
RS
14211 Returns the hpos of the end of the text generated by ELT.
14212
14213 PROPS is a property list to add to any string we encounter.
14214
7d0393cf 14215 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
14216 we encounter, and ignore :eval and :propertize.
14217
14218 If the global variable `frame_title_ptr' is non-NULL, then the output
14219 is passed to `store_frame_title' instead of `display_string'. */
a2889657
JB
14220
14221static int
c53a1624 14222display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 14223 struct it *it;
a2889657 14224 int depth;
5f5c8ee5 14225 int field_width, precision;
0fcf414f 14226 Lisp_Object elt, props;
c53a1624 14227 int risky;
a2889657 14228{
5f5c8ee5 14229 int n = 0, field, prec;
0fcf414f 14230 int literal = 0;
5f5c8ee5 14231
a2889657
JB
14232 tail_recurse:
14233 if (depth > 10)
14234 goto invalid;
14235
14236 depth++;
14237
0220c518 14238 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
14239 {
14240 case Lisp_String:
14241 {
14242 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 14243 unsigned char c;
50f80c2f 14244 const unsigned char *this, *lisp_string;
5f5c8ee5 14245
c53a1624 14246 if (!NILP (props) || risky)
0fcf414f
RS
14247 {
14248 Lisp_Object oprops, aelt;
14249 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
14250
14251 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 14252 {
ae02e06a
RS
14253 /* If the starting string has properties,
14254 merge the specified ones onto the existing ones. */
c53a1624 14255 if (! NILP (oprops) && !risky)
ae02e06a
RS
14256 {
14257 Lisp_Object tem;
14258
14259 oprops = Fcopy_sequence (oprops);
14260 tem = props;
14261 while (CONSP (tem))
14262 {
14263 oprops = Fplist_put (oprops, XCAR (tem),
14264 XCAR (XCDR (tem)));
14265 tem = XCDR (XCDR (tem));
14266 }
14267 props = oprops;
14268 }
14269
0fcf414f
RS
14270 aelt = Fassoc (elt, mode_line_proptrans_alist);
14271 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
14272 {
14273 mode_line_proptrans_alist
14274 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
14275 elt = XCAR (aelt);
14276 }
0fcf414f
RS
14277 else
14278 {
adb63af1
RS
14279 Lisp_Object tem;
14280
0fcf414f 14281 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
14282 Fset_text_properties (make_number (0), Flength (elt),
14283 props, elt);
adb63af1 14284 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
14285 mode_line_proptrans_alist
14286 = Fcons (Fcons (elt, props),
14287 mode_line_proptrans_alist);
adb63af1
RS
14288 /* Truncate mode_line_proptrans_alist
14289 to at most 50 elements. */
14290 tem = Fnthcdr (make_number (50),
14291 mode_line_proptrans_alist);
14292 if (! NILP (tem))
14293 XSETCDR (tem, Qnil);
0fcf414f
RS
14294 }
14295 }
14296 }
14297
2051c264 14298 this = SDATA (elt);
ae02e06a
RS
14299 lisp_string = this;
14300
0fcf414f
RS
14301 if (literal)
14302 {
14303 prec = precision - n;
14304 if (frame_title_ptr)
2051c264 14305 n += store_frame_title (SDATA (elt), -1, prec);
fec8f23e
KS
14306 else if (!NILP (mode_line_string_list))
14307 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
0fcf414f
RS
14308 else
14309 n += display_string (NULL, elt, Qnil, 0, 0, it,
14310 0, prec, 0, STRING_MULTIBYTE (elt));
14311
14312 break;
14313 }
14314
5f5c8ee5
GM
14315 while ((precision <= 0 || n < precision)
14316 && *this
14317 && (frame_title_ptr
fec8f23e 14318 || !NILP (mode_line_string_list)
5f5c8ee5 14319 || it->current_x < it->last_visible_x))
a2889657 14320 {
50f80c2f 14321 const unsigned char *last = this;
5f5c8ee5
GM
14322
14323 /* Advance to end of string or next format specifier. */
a2889657
JB
14324 while ((c = *this++) != '\0' && c != '%')
14325 ;
2311178e 14326
a2889657
JB
14327 if (this - 1 != last)
14328 {
5f5c8ee5
GM
14329 /* Output to end of string or up to '%'. Field width
14330 is length of string. Don't output more than
14331 PRECISION allows us. */
d26b89b8 14332 --this;
eaaa65b0
GM
14333
14334 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
14335 if (precision > 0 && prec > precision - n)
14336 prec = precision - n;
2311178e 14337
d39b6696 14338 if (frame_title_ptr)
d26b89b8 14339 n += store_frame_title (last, 0, prec);
fec8f23e
KS
14340 else if (!NILP (mode_line_string_list))
14341 {
14342 int bytepos = last - lisp_string;
14343 int charpos = string_byte_to_char (elt, bytepos);
14344 n += store_mode_line_string (NULL,
14345 Fsubstring (elt, make_number (charpos),
14346 make_number (charpos + prec)),
14347 0, 0, 0, Qnil);
14348 }
d39b6696 14349 else
eaaa65b0
GM
14350 {
14351 int bytepos = last - lisp_string;
14352 int charpos = string_byte_to_char (elt, bytepos);
14353 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
14354 it, 0, prec, 0,
14355 STRING_MULTIBYTE (elt));
eaaa65b0 14356 }
a2889657
JB
14357 }
14358 else /* c == '%' */
14359 {
50f80c2f 14360 const unsigned char *percent_position = this;
2311178e 14361
5f5c8ee5
GM
14362 /* Get the specified minimum width. Zero means
14363 don't pad. */
14364 field = 0;
a2889657 14365 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 14366 field = field * 10 + c - '0';
a2889657 14367
5f5c8ee5
GM
14368 /* Don't pad beyond the total padding allowed. */
14369 if (field_width - n > 0 && field > field_width - n)
14370 field = field_width - n;
a2889657 14371
5f5c8ee5
GM
14372 /* Note that either PRECISION <= 0 or N < PRECISION. */
14373 prec = precision - n;
2311178e 14374
a2889657 14375 if (c == 'M')
5f5c8ee5 14376 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
14377 Vglobal_mode_string, props,
14378 risky);
a2889657 14379 else if (c != 0)
d39b6696 14380 {
72f62cb5 14381 int multibyte;
ae02e06a
RS
14382 int bytepos, charpos;
14383 unsigned char *spec;
2311178e 14384
ae02e06a
RS
14385 bytepos = percent_position - lisp_string;
14386 charpos = (STRING_MULTIBYTE (elt)
14387 ? string_byte_to_char (elt, bytepos)
14388 : bytepos);
14389
14390 spec
72f62cb5
GM
14391 = decode_mode_spec (it->w, c, field, prec, &multibyte);
14392
d39b6696 14393 if (frame_title_ptr)
5f5c8ee5 14394 n += store_frame_title (spec, field, prec);
fec8f23e
KS
14395 else if (!NILP (mode_line_string_list))
14396 {
14397 int len = strlen (spec);
14398 Lisp_Object tem = make_string (spec, len);
14399 props = Ftext_properties_at (make_number (charpos), elt);
14400 /* Should only keep face property in props */
14401 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
14402 }
d39b6696 14403 else
5f5c8ee5 14404 {
ae02e06a 14405 int nglyphs_before, nwritten;
2311178e 14406
72f62cb5 14407 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
14408 nwritten = display_string (spec, Qnil, elt,
14409 charpos, 0, it,
14410 field, prec, 0,
14411 multibyte);
5f5c8ee5
GM
14412
14413 /* Assign to the glyphs written above the
14414 string where the `%x' came from, position
14415 of the `%'. */
14416 if (nwritten > 0)
14417 {
14418 struct glyph *glyph
14419 = (it->glyph_row->glyphs[TEXT_AREA]
14420 + nglyphs_before);
14421 int i;
14422
14423 for (i = 0; i < nwritten; ++i)
14424 {
14425 glyph[i].object = elt;
14426 glyph[i].charpos = charpos;
14427 }
2311178e 14428
5f5c8ee5
GM
14429 n += nwritten;
14430 }
14431 }
d39b6696 14432 }
b8523839
AS
14433 else /* c == 0 */
14434 break;
a2889657
JB
14435 }
14436 }
14437 }
14438 break;
14439
14440 case Lisp_Symbol:
14441 /* A symbol: process the value of the symbol recursively
14442 as if it appeared here directly. Avoid error if symbol void.
14443 Special case: if value of symbol is a string, output the string
14444 literally. */
14445 {
14446 register Lisp_Object tem;
c53a1624
RS
14447
14448 /* If the variable is not marked as risky to set
14449 then its contents are risky to use. */
14450 if (NILP (Fget (elt, Qrisky_local_variable)))
14451 risky = 1;
14452
a2889657 14453 tem = Fboundp (elt);
265a9e55 14454 if (!NILP (tem))
a2889657
JB
14455 {
14456 tem = Fsymbol_value (elt);
14457 /* If value is a string, output that string literally:
14458 don't check for % within it. */
e24c997d 14459 if (STRINGP (tem))
0fcf414f
RS
14460 literal = 1;
14461
14462 if (!EQ (tem, elt))
5f5c8ee5
GM
14463 {
14464 /* Give up right away for nil or t. */
14465 elt = tem;
14466 goto tail_recurse;
14467 }
a2889657
JB
14468 }
14469 }
14470 break;
14471
14472 case Lisp_Cons:
14473 {
14474 register Lisp_Object car, tem;
14475
0fcf414f
RS
14476 /* A cons cell: five distinct cases.
14477 If first element is :eval or :propertize, do something special.
a2889657
JB
14478 If first element is a string or a cons, process all the elements
14479 and effectively concatenate them.
14480 If first element is a negative number, truncate displaying cdr to
14481 at most that many characters. If positive, pad (with spaces)
14482 to at least that many characters.
14483 If first element is a symbol, process the cadr or caddr recursively
14484 according to whether the symbol's value is non-nil or nil. */
9472f927 14485 car = XCAR (elt);
0fcf414f 14486 if (EQ (car, QCeval))
5f5c8ee5
GM
14487 {
14488 /* An element of the form (:eval FORM) means evaluate FORM
14489 and use the result as mode line elements. */
0fcf414f 14490
c53a1624
RS
14491 if (risky)
14492 break;
14493
0fcf414f
RS
14494 if (CONSP (XCDR (elt)))
14495 {
14496 Lisp_Object spec;
14497 spec = safe_eval (XCAR (XCDR (elt)));
14498 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
14499 precision - n, spec, props,
14500 risky);
0fcf414f
RS
14501 }
14502 }
14503 else if (EQ (car, QCpropertize))
14504 {
c53a1624
RS
14505 /* An element of the form (:propertize ELT PROPS...)
14506 means display ELT but applying properties PROPS. */
14507
14508 if (risky)
14509 break;
14510
0fcf414f 14511 if (CONSP (XCDR (elt)))
c53a1624
RS
14512 n += display_mode_element (it, depth, field_width - n,
14513 precision - n, XCAR (XCDR (elt)),
14514 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
14515 }
14516 else if (SYMBOLP (car))
a2889657
JB
14517 {
14518 tem = Fboundp (car);
9472f927 14519 elt = XCDR (elt);
e24c997d 14520 if (!CONSP (elt))
a2889657
JB
14521 goto invalid;
14522 /* elt is now the cdr, and we know it is a cons cell.
14523 Use its car if CAR has a non-nil value. */
265a9e55 14524 if (!NILP (tem))
a2889657
JB
14525 {
14526 tem = Fsymbol_value (car);
265a9e55 14527 if (!NILP (tem))
9472f927
GM
14528 {
14529 elt = XCAR (elt);
14530 goto tail_recurse;
14531 }
a2889657
JB
14532 }
14533 /* Symbol's value is nil (or symbol is unbound)
14534 Get the cddr of the original list
14535 and if possible find the caddr and use that. */
9472f927 14536 elt = XCDR (elt);
265a9e55 14537 if (NILP (elt))
a2889657 14538 break;
e24c997d 14539 else if (!CONSP (elt))
a2889657 14540 goto invalid;
9472f927 14541 elt = XCAR (elt);
a2889657
JB
14542 goto tail_recurse;
14543 }
e24c997d 14544 else if (INTEGERP (car))
a2889657
JB
14545 {
14546 register int lim = XINT (car);
9472f927 14547 elt = XCDR (elt);
a2889657 14548 if (lim < 0)
5f5c8ee5
GM
14549 {
14550 /* Negative int means reduce maximum width. */
14551 if (precision <= 0)
14552 precision = -lim;
14553 else
14554 precision = min (precision, -lim);
14555 }
a2889657
JB
14556 else if (lim > 0)
14557 {
14558 /* Padding specified. Don't let it be more than
14559 current maximum. */
5f5c8ee5
GM
14560 if (precision > 0)
14561 lim = min (precision, lim);
14562
a2889657
JB
14563 /* If that's more padding than already wanted, queue it.
14564 But don't reduce padding already specified even if
14565 that is beyond the current truncation point. */
5f5c8ee5 14566 field_width = max (lim, field_width);
a2889657
JB
14567 }
14568 goto tail_recurse;
14569 }
e24c997d 14570 else if (STRINGP (car) || CONSP (car))
a2889657
JB
14571 {
14572 register int limit = 50;
5f5c8ee5
GM
14573 /* Limit is to protect against circular lists. */
14574 while (CONSP (elt)
14575 && --limit > 0
14576 && (precision <= 0 || n < precision))
a2889657 14577 {
5f5c8ee5 14578 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
14579 precision - n, XCAR (elt),
14580 props, risky);
9472f927 14581 elt = XCDR (elt);
a2889657
JB
14582 }
14583 }
14584 }
14585 break;
14586
14587 default:
14588 invalid:
d39b6696 14589 if (frame_title_ptr)
5f5c8ee5 14590 n += store_frame_title ("*invalid*", 0, precision - n);
fec8f23e
KS
14591 else if (!NILP (mode_line_string_list))
14592 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
d39b6696 14593 else
5f5c8ee5
GM
14594 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14595 precision - n, 0, 0);
14596 return n;
a2889657
JB
14597 }
14598
5f5c8ee5
GM
14599 /* Pad to FIELD_WIDTH. */
14600 if (field_width > 0 && n < field_width)
14601 {
14602 if (frame_title_ptr)
14603 n += store_frame_title ("", field_width - n, 0);
fec8f23e
KS
14604 else if (!NILP (mode_line_string_list))
14605 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
5f5c8ee5
GM
14606 else
14607 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14608 0, 0, 0);
14609 }
2311178e 14610
5f5c8ee5 14611 return n;
a2889657 14612}
5f5c8ee5 14613
fec8f23e
KS
14614/* Store a mode-line string element in mode_line_string_list.
14615
14616 If STRING is non-null, display that C string. Otherwise, the Lisp
14617 string LISP_STRING is displayed.
14618
14619 FIELD_WIDTH is the minimum number of output glyphs to produce.
14620 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14621 with spaces. FIELD_WIDTH <= 0 means don't pad.
14622
14623 PRECISION is the maximum number of characters to output from
14624 STRING. PRECISION <= 0 means don't truncate the string.
14625
14626 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14627 properties to the string.
14628
7d0393cf 14629 PROPS are the properties to add to the string.
fec8f23e
KS
14630 The mode_line_string_face face property is always added to the string.
14631 */
14632
14633static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14634 char *string;
14635 Lisp_Object lisp_string;
14636 int copy_string;
14637 int field_width;
14638 int precision;
14639 Lisp_Object props;
14640{
14641 int len;
14642 int n = 0;
14643
14644 if (string != NULL)
14645 {
14646 len = strlen (string);
14647 if (precision > 0 && len > precision)
14648 len = precision;
14649 lisp_string = make_string (string, len);
14650 if (NILP (props))
14651 props = mode_line_string_face_prop;
14652 else if (!NILP (mode_line_string_face))
14653 {
14654 Lisp_Object face = Fplist_get (props, Qface);
14655 props = Fcopy_sequence (props);
14656 if (NILP (face))
14657 face = mode_line_string_face;
14658 else
14659 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14660 props = Fplist_put (props, Qface, face);
14661 }
14662 Fadd_text_properties (make_number (0), make_number (len),
14663 props, lisp_string);
14664 }
7d0393cf 14665 else
fec8f23e 14666 {
c8224325 14667 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
14668 if (precision > 0 && len > precision)
14669 {
14670 len = precision;
14671 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14672 precision = -1;
14673 }
14674 if (!NILP (mode_line_string_face))
14675 {
14676 Lisp_Object face;
14677 if (NILP (props))
14678 props = Ftext_properties_at (make_number (0), lisp_string);
14679 face = Fplist_get (props, Qface);
14680 if (NILP (face))
14681 face = mode_line_string_face;
14682 else
14683 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14684 props = Fcons (Qface, Fcons (face, Qnil));
14685 if (copy_string)
14686 lisp_string = Fcopy_sequence (lisp_string);
14687 }
14688 if (!NILP (props))
14689 Fadd_text_properties (make_number (0), make_number (len),
14690 props, lisp_string);
14691 }
14692
14693 if (len > 0)
14694 {
14695 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 14696 n += len;
fec8f23e
KS
14697 }
14698
14699 if (field_width > len)
14700 {
14701 field_width -= len;
14702 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14703 if (!NILP (props))
14704 Fadd_text_properties (make_number (0), make_number (field_width),
14705 props, lisp_string);
14706 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 14707 n += field_width;
fec8f23e
KS
14708 }
14709
14710 return n;
14711}
14712
5f5c8ee5 14713
8143e6ab 14714DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
fec8f23e 14715 0, 3, 0,
8143e6ab
KS
14716 doc: /* Return the mode-line of selected window as a string.
14717First optional arg FORMAT specifies a different format string (see
8b22c65a 14718`mode-line-format' for details) to use. If FORMAT is t, return
8143e6ab 14719the buffer's header-line. Second optional arg WINDOW specifies a
fec8f23e
KS
14720different window to use as the context for the formatting.
14721If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14722 (format, window, no_props)
14723 Lisp_Object format, window, no_props;
8143e6ab
KS
14724{
14725 struct it it;
8143e6ab
KS
14726 int len;
14727 struct window *w;
14728 struct buffer *old_buffer = NULL;
fec8f23e 14729 enum face_id face_id = DEFAULT_FACE_ID;
8143e6ab
KS
14730
14731 if (NILP (window))
14732 window = selected_window;
14733 CHECK_WINDOW (window);
14734 w = XWINDOW (window);
14735 CHECK_BUFFER (w->buffer);
14736
14737 if (XBUFFER (w->buffer) != current_buffer)
14738 {
14739 old_buffer = current_buffer;
14740 set_buffer_internal_1 (XBUFFER (w->buffer));
14741 }
14742
14743 if (NILP (format) || EQ (format, Qt))
fec8f23e
KS
14744 {
14745 face_id = NILP (format)
14746 ? CURRENT_MODE_LINE_FACE_ID (w) :
14747 HEADER_LINE_FACE_ID;
14748 format = NILP (format)
14749 ? current_buffer->mode_line_format
14750 : current_buffer->header_line_format;
14751 }
14752
14753 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 14754
fec8f23e
KS
14755 if (NILP (no_props))
14756 {
14757 mode_line_string_face =
14758 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14759 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14760 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
8143e6ab 14761
fec8f23e
KS
14762 mode_line_string_face_prop =
14763 NILP (mode_line_string_face) ? Qnil :
14764 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14765
14766 /* We need a dummy last element in mode_line_string_list to
14767 indicate we are building the propertized mode-line string.
14768 Using mode_line_string_face_prop here GC protects it. */
7d0393cf 14769 mode_line_string_list =
fec8f23e
KS
14770 Fcons (mode_line_string_face_prop, Qnil);
14771 frame_title_ptr = NULL;
14772 }
14773 else
14774 {
14775 mode_line_string_face_prop = Qnil;
14776 mode_line_string_list = Qnil;
14777 frame_title_ptr = frame_title_buf;
14778 }
8143e6ab
KS
14779
14780 push_frame_kboard (it.f);
14781 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14782 pop_frame_kboard ();
14783
14784 if (old_buffer)
14785 set_buffer_internal_1 (old_buffer);
14786
fec8f23e
KS
14787 if (NILP (no_props))
14788 {
14789 Lisp_Object str;
14790 mode_line_string_list = Fnreverse (mode_line_string_list);
14791 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14792 make_string ("", 0));
14793 mode_line_string_face_prop = Qnil;
14794 mode_line_string_list = Qnil;
14795 return str;
14796 }
14797
8143e6ab
KS
14798 len = frame_title_ptr - frame_title_buf;
14799 if (len > 0 && frame_title_ptr[-1] == '-')
14800 {
14801 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14802 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14803 ;
14804 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14805 if (len > frame_title_ptr - frame_title_buf)
14806 len = frame_title_ptr - frame_title_buf;
14807 }
14808
14809 frame_title_ptr = NULL;
14810 return make_string (frame_title_buf, len);
14811}
14812
766525bc
RS
14813/* Write a null-terminated, right justified decimal representation of
14814 the positive integer D to BUF using a minimal field width WIDTH. */
14815
14816static void
14817pint2str (buf, width, d)
14818 register char *buf;
14819 register int width;
14820 register int d;
14821{
14822 register char *p = buf;
2311178e 14823
766525bc 14824 if (d <= 0)
5f5c8ee5 14825 *p++ = '0';
766525bc 14826 else
5f5c8ee5 14827 {
766525bc 14828 while (d > 0)
5f5c8ee5 14829 {
766525bc
RS
14830 *p++ = d % 10 + '0';
14831 d /= 10;
5f5c8ee5
GM
14832 }
14833 }
2311178e 14834
5f5c8ee5
GM
14835 for (width -= (int) (p - buf); width > 0; --width)
14836 *p++ = ' ';
766525bc
RS
14837 *p-- = '\0';
14838 while (p > buf)
5f5c8ee5 14839 {
766525bc
RS
14840 d = *buf;
14841 *buf++ = *p;
14842 *p-- = d;
5f5c8ee5 14843 }
766525bc
RS
14844}
14845
5f5c8ee5 14846/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
14847 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14848 type of CODING_SYSTEM. Return updated pointer into BUF. */
14849
6693a99a 14850static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 14851
1c9241f5
KH
14852static char *
14853decode_mode_spec_coding (coding_system, buf, eol_flag)
14854 Lisp_Object coding_system;
14855 register char *buf;
14856 int eol_flag;
14857{
1e1078d6 14858 Lisp_Object val;
916848d8 14859 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 14860 const unsigned char *eol_str;
302f2b38
EZ
14861 int eol_str_len;
14862 /* The EOL conversion we are using. */
14863 Lisp_Object eoltype;
1e1078d6 14864
4a09dee0 14865 val = Fget (coding_system, Qcoding_system);
085536c2 14866 eoltype = Qnil;
1c9241f5 14867
4a09dee0 14868 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 14869 {
916848d8
RS
14870 if (multibyte)
14871 *buf++ = '-';
21e989e3 14872 if (eol_flag)
302f2b38 14873 eoltype = eol_mnemonic_undecided;
1e1078d6 14874 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
14875 }
14876 else
14877 {
1e1078d6
RS
14878 Lisp_Object eolvalue;
14879
14880 eolvalue = Fget (coding_system, Qeol_type);
14881
916848d8 14882 if (multibyte)
a61b7058 14883 *buf++ = XFASTINT (AREF (val, 1));
916848d8 14884
1c9241f5
KH
14885 if (eol_flag)
14886 {
1e1078d6
RS
14887 /* The EOL conversion that is normal on this system. */
14888
14889 if (NILP (eolvalue)) /* Not yet decided. */
14890 eoltype = eol_mnemonic_undecided;
14891 else if (VECTORP (eolvalue)) /* Not yet decided. */
14892 eoltype = eol_mnemonic_undecided;
14893 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14894 eoltype = (XFASTINT (eolvalue) == 0
14895 ? eol_mnemonic_unix
14896 : (XFASTINT (eolvalue) == 1
14897 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
14898 }
14899 }
2311178e 14900
302f2b38
EZ
14901 if (eol_flag)
14902 {
14903 /* Mention the EOL conversion if it is not the usual one. */
14904 if (STRINGP (eoltype))
14905 {
2051c264
GM
14906 eol_str = SDATA (eoltype);
14907 eol_str_len = SBYTES (eoltype);
302f2b38 14908 }
f30b3499
KH
14909 else if (INTEGERP (eoltype)
14910 && CHAR_VALID_P (XINT (eoltype), 0))
14911 {
50f80c2f
KR
14912 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14913 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14914 eol_str = tmp;
f30b3499 14915 }
302f2b38
EZ
14916 else
14917 {
14918 eol_str = invalid_eol_type;
14919 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 14920 }
f30b3499 14921 bcopy (eol_str, buf, eol_str_len);
302f2b38 14922 buf += eol_str_len;
1c9241f5 14923 }
302f2b38 14924
1c9241f5
KH
14925 return buf;
14926}
14927
a2889657 14928/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
14929 generated by character C. PRECISION >= 0 means don't return a
14930 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
14931 string returned with spaces to that value. Return 1 in *MULTIBYTE
14932 if the result is multibyte text. */
a2889657 14933
11e82b76
JB
14934static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14935
a2889657 14936static char *
72f62cb5 14937decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 14938 struct window *w;
68c45bf0 14939 register int c;
5f5c8ee5 14940 int field_width, precision;
72f62cb5 14941 int *multibyte;
a2889657 14942{
0b67772d 14943 Lisp_Object obj;
5f5c8ee5
GM
14944 struct frame *f = XFRAME (WINDOW_FRAME (w));
14945 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 14946 struct buffer *b = XBUFFER (w->buffer);
a2889657 14947
0b67772d 14948 obj = Qnil;
72f62cb5 14949 *multibyte = 0;
a2889657
JB
14950
14951 switch (c)
14952 {
1af9f229
RS
14953 case '*':
14954 if (!NILP (b->read_only))
14955 return "%";
14956 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14957 return "*";
14958 return "-";
14959
14960 case '+':
14961 /* This differs from %* only for a modified read-only buffer. */
14962 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14963 return "*";
14964 if (!NILP (b->read_only))
14965 return "%";
14966 return "-";
14967
14968 case '&':
14969 /* This differs from %* in ignoring read-only-ness. */
14970 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14971 return "*";
14972 return "-";
14973
14974 case '%':
14975 return "%";
14976
2311178e 14977 case '[':
1af9f229
RS
14978 {
14979 int i;
14980 char *p;
14981
14982 if (command_loop_level > 5)
14983 return "[[[... ";
14984 p = decode_mode_spec_buf;
14985 for (i = 0; i < command_loop_level; i++)
14986 *p++ = '[';
14987 *p = 0;
14988 return decode_mode_spec_buf;
14989 }
14990
2311178e 14991 case ']':
1af9f229
RS
14992 {
14993 int i;
14994 char *p;
14995
14996 if (command_loop_level > 5)
14997 return " ...]]]";
14998 p = decode_mode_spec_buf;
14999 for (i = 0; i < command_loop_level; i++)
15000 *p++ = ']';
15001 *p = 0;
15002 return decode_mode_spec_buf;
15003 }
15004
15005 case '-':
15006 {
1af9f229 15007 register int i;
5f5c8ee5
GM
15008
15009 /* Let lots_of_dashes be a string of infinite length. */
fec8f23e
KS
15010 if (!NILP (mode_line_string_list))
15011 return "--";
5f5c8ee5
GM
15012 if (field_width <= 0
15013 || field_width > sizeof (lots_of_dashes))
1af9f229 15014 {
5f5c8ee5
GM
15015 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15016 decode_mode_spec_buf[i] = '-';
15017 decode_mode_spec_buf[i] = '\0';
15018 return decode_mode_spec_buf;
1af9f229 15019 }
5f5c8ee5
GM
15020 else
15021 return lots_of_dashes;
1af9f229
RS
15022 }
15023
2311178e 15024 case 'b':
d39b6696 15025 obj = b->name;
a2889657
JB
15026 break;
15027
1af9f229
RS
15028 case 'c':
15029 {
2311178e 15030 int col = (int) current_column (); /* iftc */
ac90c44f 15031 w->column_number_displayed = make_number (col);
5f5c8ee5 15032 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
15033 return decode_mode_spec_buf;
15034 }
15035
15036 case 'F':
15037 /* %F displays the frame name. */
5f5c8ee5 15038 if (!NILP (f->title))
2051c264 15039 return (char *) SDATA (f->title);
fd8ff63d 15040 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 15041 return (char *) SDATA (f->name);
9c6da96f 15042 return "Emacs";
1af9f229 15043
2311178e 15044 case 'f':
d39b6696 15045 obj = b->filename;
a2889657
JB
15046 break;
15047
aa6d10fa
RS
15048 case 'l':
15049 {
12adba34
RS
15050 int startpos = XMARKER (w->start)->charpos;
15051 int startpos_byte = marker_byte_position (w->start);
15052 int line, linepos, linepos_byte, topline;
aa6d10fa 15053 int nlines, junk;
aa6d10fa
RS
15054 int height = XFASTINT (w->height);
15055
2311178e 15056 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
15057 don't forget that too fast. */
15058 if (EQ (w->base_line_pos, w->buffer))
766525bc 15059 goto no_value;
5300fd39
RS
15060 /* But do forget it, if the window shows a different buffer now. */
15061 else if (BUFFERP (w->base_line_pos))
15062 w->base_line_pos = Qnil;
aa6d10fa
RS
15063
15064 /* If the buffer is very big, don't waste time. */
37d034d3 15065 if (INTEGERP (Vline_number_display_limit)
090703f4 15066 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
15067 {
15068 w->base_line_pos = Qnil;
15069 w->base_line_number = Qnil;
766525bc 15070 goto no_value;
aa6d10fa
RS
15071 }
15072
15073 if (!NILP (w->base_line_number)
15074 && !NILP (w->base_line_pos)
12adba34 15075 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
15076 {
15077 line = XFASTINT (w->base_line_number);
15078 linepos = XFASTINT (w->base_line_pos);
12adba34 15079 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
15080 }
15081 else
15082 {
15083 line = 1;
d39b6696 15084 linepos = BUF_BEGV (b);
12adba34 15085 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
15086 }
15087
15088 /* Count lines from base line to window start position. */
12adba34
RS
15089 nlines = display_count_lines (linepos, linepos_byte,
15090 startpos_byte,
15091 startpos, &junk);
aa6d10fa
RS
15092
15093 topline = nlines + line;
15094
15095 /* Determine a new base line, if the old one is too close
15096 or too far away, or if we did not have one.
15097 "Too close" means it's plausible a scroll-down would
15098 go back past it. */
d39b6696 15099 if (startpos == BUF_BEGV (b))
aa6d10fa 15100 {
ac90c44f
GM
15101 w->base_line_number = make_number (topline);
15102 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
15103 }
15104 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 15105 || linepos == BUF_BEGV (b))
aa6d10fa 15106 {
d39b6696 15107 int limit = BUF_BEGV (b);
12adba34 15108 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 15109 int position;
5d121aec 15110 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
15111
15112 if (startpos - distance > limit)
12adba34
RS
15113 {
15114 limit = startpos - distance;
15115 limit_byte = CHAR_TO_BYTE (limit);
15116 }
aa6d10fa 15117
12adba34
RS
15118 nlines = display_count_lines (startpos, startpos_byte,
15119 limit_byte,
15120 - (height * 2 + 30),
aa6d10fa 15121 &position);
2311178e 15122 /* If we couldn't find the lines we wanted within
5d121aec 15123 line_number_display_limit_width chars per line,
aa6d10fa 15124 give up on line numbers for this window. */
12adba34 15125 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
15126 {
15127 w->base_line_pos = w->buffer;
15128 w->base_line_number = Qnil;
766525bc 15129 goto no_value;
aa6d10fa
RS
15130 }
15131
ac90c44f
GM
15132 w->base_line_number = make_number (topline - nlines);
15133 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
15134 }
15135
15136 /* Now count lines from the start pos to point. */
12adba34
RS
15137 nlines = display_count_lines (startpos, startpos_byte,
15138 PT_BYTE, PT, &junk);
aa6d10fa
RS
15139
15140 /* Record that we did display the line number. */
15141 line_number_displayed = 1;
15142
15143 /* Make the string to show. */
5f5c8ee5 15144 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 15145 return decode_mode_spec_buf;
766525bc
RS
15146 no_value:
15147 {
15148 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
15149 int pad = field_width - 2;
15150 while (pad-- > 0)
15151 *p++ = ' ';
15152 *p++ = '?';
b357b9d4
KR
15153 *p++ = '?';
15154 *p = '\0';
766525bc
RS
15155 return decode_mode_spec_buf;
15156 }
aa6d10fa
RS
15157 }
15158 break;
15159
2311178e 15160 case 'm':
d39b6696 15161 obj = b->mode_name;
a2889657
JB
15162 break;
15163
15164 case 'n':
d39b6696 15165 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
15166 return " Narrow";
15167 break;
15168
a2889657
JB
15169 case 'p':
15170 {
15171 int pos = marker_position (w->start);
d39b6696 15172 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 15173
d39b6696 15174 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 15175 {
d39b6696 15176 if (pos <= BUF_BEGV (b))
a2889657
JB
15177 return "All";
15178 else
15179 return "Bottom";
15180 }
d39b6696 15181 else if (pos <= BUF_BEGV (b))
a2889657
JB
15182 return "Top";
15183 else
15184 {
3c7d31b9
RS
15185 if (total > 1000000)
15186 /* Do it differently for a large value, to avoid overflow. */
15187 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15188 else
15189 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
15190 /* We can't normally display a 3-digit number,
15191 so get us a 2-digit number that is close. */
15192 if (total == 100)
15193 total = 99;
15194 sprintf (decode_mode_spec_buf, "%2d%%", total);
15195 return decode_mode_spec_buf;
15196 }
15197 }
15198
8ffcb79f
RS
15199 /* Display percentage of size above the bottom of the screen. */
15200 case 'P':
15201 {
15202 int toppos = marker_position (w->start);
d39b6696
KH
15203 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15204 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 15205
d39b6696 15206 if (botpos >= BUF_ZV (b))
8ffcb79f 15207 {
d39b6696 15208 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
15209 return "All";
15210 else
15211 return "Bottom";
15212 }
15213 else
15214 {
3c7d31b9
RS
15215 if (total > 1000000)
15216 /* Do it differently for a large value, to avoid overflow. */
15217 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15218 else
15219 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
15220 /* We can't normally display a 3-digit number,
15221 so get us a 2-digit number that is close. */
15222 if (total == 100)
15223 total = 99;
d39b6696 15224 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
15225 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15226 else
15227 sprintf (decode_mode_spec_buf, "%2d%%", total);
15228 return decode_mode_spec_buf;
15229 }
15230 }
15231
1af9f229
RS
15232 case 's':
15233 /* status of process */
15234 obj = Fget_buffer_process (w->buffer);
15235 if (NILP (obj))
15236 return "no process";
15237#ifdef subprocesses
15238 obj = Fsymbol_name (Fprocess_status (obj));
15239#endif
15240 break;
d39b6696 15241
1af9f229
RS
15242 case 't': /* indicate TEXT or BINARY */
15243#ifdef MODE_LINE_BINARY_TEXT
15244 return MODE_LINE_BINARY_TEXT (b);
15245#else
15246 return "T";
15247#endif
1c9241f5
KH
15248
15249 case 'z':
15250 /* coding-system (not including end-of-line format) */
15251 case 'Z':
15252 /* coding-system (including end-of-line type) */
15253 {
15254 int eol_flag = (c == 'Z');
539b4d41 15255 char *p = decode_mode_spec_buf;
1c9241f5 15256
d30e754b 15257 if (! FRAME_WINDOW_P (f))
1c9241f5 15258 {
11c52c4f
RS
15259 /* No need to mention EOL here--the terminal never needs
15260 to do EOL conversion. */
15261 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15262 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 15263 }
f13c925f 15264 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 15265 p, eol_flag);
f13c925f 15266
11c52c4f 15267#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
15268#ifdef subprocesses
15269 obj = Fget_buffer_process (Fcurrent_buffer ());
15270 if (PROCESSP (obj))
15271 {
15272 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
15273 p, eol_flag);
15274 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
15275 p, eol_flag);
15276 }
15277#endif /* subprocesses */
11c52c4f 15278#endif /* 0 */
1c9241f5
KH
15279 *p = 0;
15280 return decode_mode_spec_buf;
15281 }
a2889657 15282 }
d39b6696 15283
e24c997d 15284 if (STRINGP (obj))
72f62cb5
GM
15285 {
15286 *multibyte = STRING_MULTIBYTE (obj);
2051c264 15287 return (char *) SDATA (obj);
72f62cb5 15288 }
a2889657
JB
15289 else
15290 return "";
15291}
5f5c8ee5
GM
15292
15293
12adba34
RS
15294/* Count up to COUNT lines starting from START / START_BYTE.
15295 But don't go beyond LIMIT_BYTE.
15296 Return the number of lines thus found (always nonnegative).
59b49f63 15297
12adba34 15298 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
15299
15300static int
12adba34
RS
15301display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15302 int start, start_byte, limit_byte, count;
15303 int *byte_pos_ptr;
59b49f63 15304{
59b49f63
RS
15305 register unsigned char *cursor;
15306 unsigned char *base;
15307
15308 register int ceiling;
15309 register unsigned char *ceiling_addr;
12adba34 15310 int orig_count = count;
59b49f63
RS
15311
15312 /* If we are not in selective display mode,
15313 check only for newlines. */
12adba34
RS
15314 int selective_display = (!NILP (current_buffer->selective_display)
15315 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
15316
15317 if (count > 0)
12adba34
RS
15318 {
15319 while (start_byte < limit_byte)
15320 {
15321 ceiling = BUFFER_CEILING_OF (start_byte);
15322 ceiling = min (limit_byte - 1, ceiling);
15323 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
15324 base = (cursor = BYTE_POS_ADDR (start_byte));
15325 while (1)
15326 {
15327 if (selective_display)
15328 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
15329 ;
15330 else
15331 while (*cursor != '\n' && ++cursor != ceiling_addr)
15332 ;
15333
15334 if (cursor != ceiling_addr)
15335 {
15336 if (--count == 0)
15337 {
15338 start_byte += cursor - base + 1;
15339 *byte_pos_ptr = start_byte;
15340 return orig_count;
15341 }
15342 else
15343 if (++cursor == ceiling_addr)
15344 break;
15345 }
15346 else
15347 break;
15348 }
15349 start_byte += cursor - base;
15350 }
15351 }
59b49f63
RS
15352 else
15353 {
12adba34
RS
15354 while (start_byte > limit_byte)
15355 {
15356 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
15357 ceiling = max (limit_byte, ceiling);
15358 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
15359 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
15360 while (1)
15361 {
12adba34
RS
15362 if (selective_display)
15363 while (--cursor != ceiling_addr
15364 && *cursor != '\n' && *cursor != 015)
15365 ;
15366 else
15367 while (--cursor != ceiling_addr && *cursor != '\n')
15368 ;
15369
59b49f63
RS
15370 if (cursor != ceiling_addr)
15371 {
15372 if (++count == 0)
15373 {
12adba34
RS
15374 start_byte += cursor - base + 1;
15375 *byte_pos_ptr = start_byte;
15376 /* When scanning backwards, we should
15377 not count the newline posterior to which we stop. */
15378 return - orig_count - 1;
59b49f63
RS
15379 }
15380 }
15381 else
15382 break;
15383 }
12adba34
RS
15384 /* Here we add 1 to compensate for the last decrement
15385 of CURSOR, which took it past the valid range. */
15386 start_byte += cursor - base + 1;
59b49f63
RS
15387 }
15388 }
15389
12adba34 15390 *byte_pos_ptr = limit_byte;
aa6d10fa 15391
12adba34
RS
15392 if (count < 0)
15393 return - orig_count + count;
15394 return orig_count - count;
aa6d10fa 15395
12adba34 15396}
a2889657 15397
a2889657 15398
5f5c8ee5
GM
15399\f
15400/***********************************************************************
15401 Displaying strings
15402 ***********************************************************************/
278feba9 15403
5f5c8ee5 15404/* Display a NUL-terminated string, starting with index START.
a3788d53 15405
5f5c8ee5
GM
15406 If STRING is non-null, display that C string. Otherwise, the Lisp
15407 string LISP_STRING is displayed.
a2889657 15408
5f5c8ee5
GM
15409 If FACE_STRING is not nil, FACE_STRING_POS is a position in
15410 FACE_STRING. Display STRING or LISP_STRING with the face at
15411 FACE_STRING_POS in FACE_STRING:
a2889657 15412
5f5c8ee5
GM
15413 Display the string in the environment given by IT, but use the
15414 standard display table, temporarily.
a3788d53 15415
5f5c8ee5
GM
15416 FIELD_WIDTH is the minimum number of output glyphs to produce.
15417 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15418 with spaces. If STRING has more characters, more than FIELD_WIDTH
15419 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 15420
5f5c8ee5
GM
15421 PRECISION is the maximum number of characters to output from
15422 STRING. PRECISION < 0 means don't truncate the string.
a2889657 15423
5f5c8ee5 15424 This is roughly equivalent to printf format specifiers:
a2889657 15425
5f5c8ee5
GM
15426 FIELD_WIDTH PRECISION PRINTF
15427 ----------------------------------------
15428 -1 -1 %s
15429 -1 10 %.10s
15430 10 -1 %10s
15431 20 10 %20.10s
a2889657 15432
5f5c8ee5
GM
15433 MULTIBYTE zero means do not display multibyte chars, > 0 means do
15434 display them, and < 0 means obey the current buffer's value of
15435 enable_multibyte_characters.
278feba9 15436
5f5c8ee5 15437 Value is the number of glyphs produced. */
b1d1124b 15438
5f5c8ee5
GM
15439static int
15440display_string (string, lisp_string, face_string, face_string_pos,
15441 start, it, field_width, precision, max_x, multibyte)
15442 unsigned char *string;
15443 Lisp_Object lisp_string;
68c45bf0
PE
15444 Lisp_Object face_string;
15445 int face_string_pos;
5f5c8ee5
GM
15446 int start;
15447 struct it *it;
15448 int field_width, precision, max_x;
15449 int multibyte;
15450{
15451 int hpos_at_start = it->hpos;
15452 int saved_face_id = it->face_id;
15453 struct glyph_row *row = it->glyph_row;
15454
15455 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 15456 with index START. */
5f5c8ee5
GM
15457 reseat_to_string (it, string, lisp_string, start,
15458 precision, field_width, multibyte);
15459
15460 /* If displaying STRING, set up the face of the iterator
15461 from LISP_STRING, if that's given. */
15462 if (STRINGP (face_string))
15463 {
15464 int endptr;
15465 struct face *face;
2311178e 15466
5f5c8ee5
GM
15467 it->face_id
15468 = face_at_string_position (it->w, face_string, face_string_pos,
15469 0, it->region_beg_charpos,
15470 it->region_end_charpos,
5de7c6f2 15471 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
15472 face = FACE_FROM_ID (it->f, it->face_id);
15473 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 15474 }
a2889657 15475
5f5c8ee5
GM
15476 /* Set max_x to the maximum allowed X position. Don't let it go
15477 beyond the right edge of the window. */
15478 if (max_x <= 0)
15479 max_x = it->last_visible_x;
15480 else
15481 max_x = min (max_x, it->last_visible_x);
efc63ef0 15482
5f5c8ee5
GM
15483 /* Skip over display elements that are not visible. because IT->w is
15484 hscrolled. */
15485 if (it->current_x < it->first_visible_x)
15486 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15487 MOVE_TO_POS | MOVE_TO_X);
a2889657 15488
5f5c8ee5
GM
15489 row->ascent = it->max_ascent;
15490 row->height = it->max_ascent + it->max_descent;
312246d1
GM
15491 row->phys_ascent = it->max_phys_ascent;
15492 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 15493
5f5c8ee5
GM
15494 /* This condition is for the case that we are called with current_x
15495 past last_visible_x. */
15496 while (it->current_x < max_x)
a2889657 15497 {
5f5c8ee5 15498 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 15499
5f5c8ee5
GM
15500 /* Get the next display element. */
15501 if (!get_next_display_element (it))
90adcf20 15502 break;
1c9241f5 15503
5f5c8ee5
GM
15504 /* Produce glyphs. */
15505 x_before = it->current_x;
15506 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15507 PRODUCE_GLYPHS (it);
90adcf20 15508
5f5c8ee5
GM
15509 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15510 i = 0;
15511 x = x_before;
15512 while (i < nglyphs)
a2889657 15513 {
5f5c8ee5 15514 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 15515
5f5c8ee5
GM
15516 if (!it->truncate_lines_p
15517 && x + glyph->pixel_width > max_x)
15518 {
15519 /* End of continued line or max_x reached. */
37be86f2
KH
15520 if (CHAR_GLYPH_PADDING_P (*glyph))
15521 {
15522 /* A wide character is unbreakable. */
15523 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15524 it->current_x = x_before;
15525 }
15526 else
15527 {
15528 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15529 it->current_x = x;
15530 }
5f5c8ee5
GM
15531 break;
15532 }
15533 else if (x + glyph->pixel_width > it->first_visible_x)
15534 {
15535 /* Glyph is at least partially visible. */
15536 ++it->hpos;
15537 if (x < it->first_visible_x)
15538 it->glyph_row->x = x - it->first_visible_x;
15539 }
15540 else
a2889657 15541 {
5f5c8ee5
GM
15542 /* Glyph is off the left margin of the display area.
15543 Should not happen. */
15544 abort ();
a2889657 15545 }
5f5c8ee5
GM
15546
15547 row->ascent = max (row->ascent, it->max_ascent);
15548 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
15549 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15550 row->phys_height = max (row->phys_height,
15551 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
15552 x += glyph->pixel_width;
15553 ++i;
a2889657 15554 }
5f5c8ee5
GM
15555
15556 /* Stop if max_x reached. */
15557 if (i < nglyphs)
15558 break;
15559
15560 /* Stop at line ends. */
15561 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 15562 {
5f5c8ee5
GM
15563 it->continuation_lines_width = 0;
15564 break;
a2889657 15565 }
1c9241f5 15566
cafafe0b 15567 set_iterator_to_next (it, 1);
a688bb24 15568
5f5c8ee5
GM
15569 /* Stop if truncating at the right edge. */
15570 if (it->truncate_lines_p
15571 && it->current_x >= it->last_visible_x)
15572 {
15573 /* Add truncation mark, but don't do it if the line is
15574 truncated at a padding space. */
15575 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 15576 {
5f5c8ee5 15577 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
15578 {
15579 int i, n;
15580
9299cb15 15581 if (it->current_x > it->last_visible_x)
37be86f2 15582 {
9299cb15
KH
15583 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15584 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15585 break;
15586 for (n = row->used[TEXT_AREA]; i < n; ++i)
15587 {
15588 row->used[TEXT_AREA] = i;
15589 produce_special_glyphs (it, IT_TRUNCATION);
15590 }
37be86f2 15591 }
9299cb15 15592 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 15593 }
5f5c8ee5 15594 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 15595 }
5f5c8ee5 15596 break;
1c9241f5 15597 }
a2889657
JB
15598 }
15599
5f5c8ee5
GM
15600 /* Maybe insert a truncation at the left. */
15601 if (it->first_visible_x
15602 && IT_CHARPOS (*it) > 0)
a2889657 15603 {
5f5c8ee5
GM
15604 if (!FRAME_WINDOW_P (it->f))
15605 insert_left_trunc_glyphs (it);
15606 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
15607 }
15608
5f5c8ee5 15609 it->face_id = saved_face_id;
2311178e 15610
5f5c8ee5
GM
15611 /* Value is number of columns displayed. */
15612 return it->hpos - hpos_at_start;
15613}
a2889657 15614
a2889657 15615
a2889657 15616\f
3b6b6db7 15617/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
15618 appears as an element of LIST or as the car of an element of LIST.
15619 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
15620 way, and return 1/2 if any element of PROPVAL is found in LIST.
15621 Otherwise return 0. This function cannot quit.
15622 The return value is 2 if the text is invisible but with an ellipsis
15623 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
15624
15625int
15626invisible_p (propval, list)
15627 register Lisp_Object propval;
15628 Lisp_Object list;
15629{
3b6b6db7 15630 register Lisp_Object tail, proptail;
2311178e 15631
3b6b6db7
SM
15632 for (tail = list; CONSP (tail); tail = XCDR (tail))
15633 {
15634 register Lisp_Object tem;
15635 tem = XCAR (tail);
15636 if (EQ (propval, tem))
15637 return 1;
15638 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15639 return NILP (XCDR (tem)) ? 1 : 2;
15640 }
2311178e 15641
3b6b6db7
SM
15642 if (CONSP (propval))
15643 {
15644 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15645 {
15646 Lisp_Object propelt;
15647 propelt = XCAR (proptail);
15648 for (tail = list; CONSP (tail); tail = XCDR (tail))
15649 {
15650 register Lisp_Object tem;
15651 tem = XCAR (tail);
15652 if (EQ (propelt, tem))
15653 return 1;
15654 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15655 return NILP (XCDR (tem)) ? 1 : 2;
15656 }
15657 }
15658 }
2311178e 15659
3b6b6db7 15660 return 0;
642eefc6 15661}
5f5c8ee5 15662
642eefc6 15663\f
133c764e
KS
15664/***********************************************************************
15665 Glyph Display
15666 ***********************************************************************/
15667
15668#if GLYPH_DEBUG
15669
15670void
15671dump_glyph_string (s)
15672 struct glyph_string *s;
15673{
15674 fprintf (stderr, "glyph string\n");
15675 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
15676 s->x, s->y, s->width, s->height);
15677 fprintf (stderr, " ybase = %d\n", s->ybase);
15678 fprintf (stderr, " hl = %d\n", s->hl);
15679 fprintf (stderr, " left overhang = %d, right = %d\n",
15680 s->left_overhang, s->right_overhang);
15681 fprintf (stderr, " nchars = %d\n", s->nchars);
15682 fprintf (stderr, " extends to end of line = %d\n",
15683 s->extends_to_end_of_line_p);
15684 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
15685 fprintf (stderr, " bg width = %d\n", s->background_width);
15686}
15687
15688#endif /* GLYPH_DEBUG */
15689
15690/* Initialize glyph string S. CHAR2B is a suitably allocated vector
15691 of XChar2b structures for S; it can't be allocated in
15692 init_glyph_string because it must be allocated via `alloca'. W
15693 is the window on which S is drawn. ROW and AREA are the glyph row
15694 and area within the row from which S is constructed. START is the
15695 index of the first glyph structure covered by S. HL is a
15696 face-override for drawing S. */
15697
15698#ifdef HAVE_NTGUI
15699#define OPTIONAL_HDC(hdc) hdc,
15700#define DECLARE_HDC(hdc) HDC hdc;
15701#define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
15702#define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
15703#endif
15704
15705#ifndef OPTIONAL_HDC
15706#define OPTIONAL_HDC(hdc)
15707#define DECLARE_HDC(hdc)
15708#define ALLOCATE_HDC(hdc, f)
15709#define RELEASE_HDC(hdc, f)
15710#endif
15711
15712static void
15713init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
15714 struct glyph_string *s;
15715 DECLARE_HDC (hdc)
15716 XChar2b *char2b;
15717 struct window *w;
15718 struct glyph_row *row;
15719 enum glyph_row_area area;
15720 int start;
15721 enum draw_glyphs_face hl;
15722{
15723 bzero (s, sizeof *s);
15724 s->w = w;
15725 s->f = XFRAME (w->frame);
15726#ifdef HAVE_NTGUI
15727 s->hdc = hdc;
15728#endif
15729 s->display = FRAME_X_DISPLAY (s->f);
15730 s->window = FRAME_X_WINDOW (s->f);
15731 s->char2b = char2b;
15732 s->hl = hl;
15733 s->row = row;
15734 s->area = area;
15735 s->first_glyph = row->glyphs[area] + start;
15736 s->height = row->height;
15737 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
15738
15739 /* Display the internal border below the tool-bar window. */
15740 if (s->w == XWINDOW (s->f->tool_bar_window))
15741 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
15742
15743 s->ybase = s->y + row->ascent;
15744}
15745
15746
15747/* Append the list of glyph strings with head H and tail T to the list
15748 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
15749
15750static INLINE void
15751append_glyph_string_lists (head, tail, h, t)
15752 struct glyph_string **head, **tail;
15753 struct glyph_string *h, *t;
15754{
15755 if (h)
15756 {
15757 if (*head)
15758 (*tail)->next = h;
15759 else
15760 *head = h;
15761 h->prev = *tail;
15762 *tail = t;
15763 }
15764}
15765
15766
15767/* Prepend the list of glyph strings with head H and tail T to the
15768 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
15769 result. */
15770
15771static INLINE void
15772prepend_glyph_string_lists (head, tail, h, t)
15773 struct glyph_string **head, **tail;
15774 struct glyph_string *h, *t;
15775{
15776 if (h)
15777 {
15778 if (*head)
15779 (*head)->prev = t;
15780 else
15781 *tail = t;
15782 t->next = *head;
15783 *head = h;
15784 }
15785}
15786
15787
15788/* Append glyph string S to the list with head *HEAD and tail *TAIL.
15789 Set *HEAD and *TAIL to the resulting list. */
15790
15791static INLINE void
15792append_glyph_string (head, tail, s)
15793 struct glyph_string **head, **tail;
15794 struct glyph_string *s;
15795{
15796 s->next = s->prev = NULL;
15797 append_glyph_string_lists (head, tail, s, s);
15798}
15799
15800
15801/* Get face and two-byte form of character glyph GLYPH on frame F.
15802 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
15803 a pointer to a realized face that is ready for display. */
15804
15805static INLINE struct face *
15806get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
15807 struct frame *f;
15808 struct glyph *glyph;
15809 XChar2b *char2b;
15810 int *two_byte_p;
15811{
15812 struct face *face;
15813
15814 xassert (glyph->type == CHAR_GLYPH);
15815 face = FACE_FROM_ID (f, glyph->face_id);
15816
15817 if (two_byte_p)
15818 *two_byte_p = 0;
15819
15820 if (!glyph->multibyte_p)
15821 {
15822 /* Unibyte case. We don't have to encode, but we have to make
15823 sure to use a face suitable for unibyte. */
15824 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
15825 }
15826 else if (glyph->u.ch < 128
15827 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
15828 {
15829 /* Case of ASCII in a face known to fit ASCII. */
15830 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
15831 }
15832 else
15833 {
15834 int c1, c2, charset;
15835
15836 /* Split characters into bytes. If c2 is -1 afterwards, C is
15837 really a one-byte character so that byte1 is zero. */
15838 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
15839 if (c2 > 0)
15840 STORE_XCHAR2B (char2b, c1, c2);
15841 else
15842 STORE_XCHAR2B (char2b, 0, c1);
15843
15844 /* Maybe encode the character in *CHAR2B. */
15845 if (charset != CHARSET_ASCII)
15846 {
15847 struct font_info *font_info
15848 = FONT_INFO_FROM_ID (f, face->font_info_id);
15849 if (font_info)
15850 glyph->font_type
15851 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
15852 }
15853 }
15854
15855 /* Make sure X resources of the face are allocated. */
15856 xassert (face != NULL);
15857 PREPARE_FACE_FOR_DISPLAY (f, face);
15858 return face;
15859}
15860
15861
15862/* Fill glyph string S with composition components specified by S->cmp.
15863
15864 FACES is an array of faces for all components of this composition.
15865 S->gidx is the index of the first component for S.
15866 OVERLAPS_P non-zero means S should draw the foreground only, and
15867 use its physical height for clipping.
15868
15869 Value is the index of a component not in S. */
15870
15871static int
15872fill_composite_glyph_string (s, faces, overlaps_p)
15873 struct glyph_string *s;
15874 struct face **faces;
15875 int overlaps_p;
15876{
15877 int i;
15878
15879 xassert (s);
15880
15881 s->for_overlaps_p = overlaps_p;
15882
15883 s->face = faces[s->gidx];
15884 s->font = s->face->font;
15885 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
15886
15887 /* For all glyphs of this composition, starting at the offset
15888 S->gidx, until we reach the end of the definition or encounter a
15889 glyph that requires the different face, add it to S. */
15890 ++s->nchars;
15891 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
15892 ++s->nchars;
15893
15894 /* All glyph strings for the same composition has the same width,
15895 i.e. the width set for the first component of the composition. */
15896
15897 s->width = s->first_glyph->pixel_width;
15898
15899 /* If the specified font could not be loaded, use the frame's
15900 default font, but record the fact that we couldn't load it in
15901 the glyph string so that we can draw rectangles for the
15902 characters of the glyph string. */
15903 if (s->font == NULL)
15904 {
15905 s->font_not_found_p = 1;
15906 s->font = FRAME_FONT (s->f);
15907 }
15908
15909 /* Adjust base line for subscript/superscript text. */
15910 s->ybase += s->first_glyph->voffset;
15911
15912 xassert (s->face && s->face->gc);
15913
15914 /* This glyph string must always be drawn with 16-bit functions. */
15915 s->two_byte_p = 1;
15916
15917 return s->gidx + s->nchars;
15918}
15919
15920
15921/* Fill glyph string S from a sequence of character glyphs.
15922
15923 FACE_ID is the face id of the string. START is the index of the
15924 first glyph to consider, END is the index of the last + 1.
15925 OVERLAPS_P non-zero means S should draw the foreground only, and
15926 use its physical height for clipping.
15927
15928 Value is the index of the first glyph not in S. */
15929
15930static int
15931fill_glyph_string (s, face_id, start, end, overlaps_p)
15932 struct glyph_string *s;
15933 int face_id;
15934 int start, end, overlaps_p;
15935{
15936 struct glyph *glyph, *last;
15937 int voffset;
15938 int glyph_not_available_p;
15939
15940 xassert (s->f == XFRAME (s->w->frame));
15941 xassert (s->nchars == 0);
15942 xassert (start >= 0 && end > start);
15943
15944 s->for_overlaps_p = overlaps_p,
15945 glyph = s->row->glyphs[s->area] + start;
15946 last = s->row->glyphs[s->area] + end;
15947 voffset = glyph->voffset;
15948
15949 glyph_not_available_p = glyph->glyph_not_available_p;
15950
15951 while (glyph < last
15952 && glyph->type == CHAR_GLYPH
15953 && glyph->voffset == voffset
15954 /* Same face id implies same font, nowadays. */
15955 && glyph->face_id == face_id
15956 && glyph->glyph_not_available_p == glyph_not_available_p)
15957 {
15958 int two_byte_p;
15959
15960 s->face = get_glyph_face_and_encoding (s->f, glyph,
15961 s->char2b + s->nchars,
15962 &two_byte_p);
15963 s->two_byte_p = two_byte_p;
15964 ++s->nchars;
15965 xassert (s->nchars <= end - start);
15966 s->width += glyph->pixel_width;
15967 ++glyph;
15968 }
15969
15970 s->font = s->face->font;
15971 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
15972
15973 /* If the specified font could not be loaded, use the frame's font,
15974 but record the fact that we couldn't load it in
15975 S->font_not_found_p so that we can draw rectangles for the
15976 characters of the glyph string. */
15977 if (s->font == NULL || glyph_not_available_p)
15978 {
15979 s->font_not_found_p = 1;
15980 s->font = FRAME_FONT (s->f);
15981 }
15982
15983 /* Adjust base line for subscript/superscript text. */
15984 s->ybase += voffset;
15985
15986 xassert (s->face && s->face->gc);
15987 return glyph - s->row->glyphs[s->area];
15988}
15989
15990
15991/* Fill glyph string S from image glyph S->first_glyph. */
15992
15993static void
15994fill_image_glyph_string (s)
15995 struct glyph_string *s;
15996{
15997 xassert (s->first_glyph->type == IMAGE_GLYPH);
15998 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
15999 xassert (s->img);
16000 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16001 s->font = s->face->font;
16002 s->width = s->first_glyph->pixel_width;
16003
16004 /* Adjust base line for subscript/superscript text. */
16005 s->ybase += s->first_glyph->voffset;
16006}
16007
16008
16009/* Fill glyph string S from a sequence of stretch glyphs.
16010
16011 ROW is the glyph row in which the glyphs are found, AREA is the
16012 area within the row. START is the index of the first glyph to
16013 consider, END is the index of the last + 1.
16014
16015 Value is the index of the first glyph not in S. */
16016
16017static int
16018fill_stretch_glyph_string (s, row, area, start, end)
16019 struct glyph_string *s;
16020 struct glyph_row *row;
16021 enum glyph_row_area area;
16022 int start, end;
16023{
16024 struct glyph *glyph, *last;
16025 int voffset, face_id;
16026
16027 xassert (s->first_glyph->type == STRETCH_GLYPH);
16028
16029 glyph = s->row->glyphs[s->area] + start;
16030 last = s->row->glyphs[s->area] + end;
16031 face_id = glyph->face_id;
16032 s->face = FACE_FROM_ID (s->f, face_id);
16033 s->font = s->face->font;
16034 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16035 s->width = glyph->pixel_width;
16036 voffset = glyph->voffset;
16037
16038 for (++glyph;
16039 (glyph < last
16040 && glyph->type == STRETCH_GLYPH
16041 && glyph->voffset == voffset
16042 && glyph->face_id == face_id);
16043 ++glyph)
16044 s->width += glyph->pixel_width;
16045
16046 /* Adjust base line for subscript/superscript text. */
16047 s->ybase += voffset;
16048
16049 /* The case that face->gc == 0 is handled when drawing the glyph
16050 string by calling PREPARE_FACE_FOR_DISPLAY. */
16051 xassert (s->face);
16052 return glyph - s->row->glyphs[s->area];
16053}
16054
16055
16056/* EXPORT for RIF:
16057 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16058 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16059 assumed to be zero. */
16060
16061void
16062x_get_glyph_overhangs (glyph, f, left, right)
16063 struct glyph *glyph;
16064 struct frame *f;
16065 int *left, *right;
16066{
16067 *left = *right = 0;
16068
16069 if (glyph->type == CHAR_GLYPH)
16070 {
16071 XFontStruct *font;
16072 struct face *face;
16073 struct font_info *font_info;
16074 XChar2b char2b;
16075 XCharStruct *pcm;
16076
16077 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16078 font = face->font;
16079 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16080 if (font /* ++KFS: Should this be font_info ? */
16081 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16082 {
16083 if (pcm->rbearing > pcm->width)
16084 *right = pcm->rbearing - pcm->width;
16085 if (pcm->lbearing < 0)
16086 *left = -pcm->lbearing;
16087 }
16088 }
16089}
16090
16091
16092/* Return the index of the first glyph preceding glyph string S that
16093 is overwritten by S because of S's left overhang. Value is -1
16094 if no glyphs are overwritten. */
16095
16096static int
16097left_overwritten (s)
16098 struct glyph_string *s;
16099{
16100 int k;
16101
16102 if (s->left_overhang)
16103 {
16104 int x = 0, i;
16105 struct glyph *glyphs = s->row->glyphs[s->area];
16106 int first = s->first_glyph - glyphs;
16107
16108 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16109 x -= glyphs[i].pixel_width;
16110
16111 k = i + 1;
16112 }
16113 else
16114 k = -1;
16115
16116 return k;
16117}
16118
16119
16120/* Return the index of the first glyph preceding glyph string S that
16121 is overwriting S because of its right overhang. Value is -1 if no
16122 glyph in front of S overwrites S. */
16123
16124static int
16125left_overwriting (s)
16126 struct glyph_string *s;
16127{
16128 int i, k, x;
16129 struct glyph *glyphs = s->row->glyphs[s->area];
16130 int first = s->first_glyph - glyphs;
16131
16132 k = -1;
16133 x = 0;
16134 for (i = first - 1; i >= 0; --i)
16135 {
16136 int left, right;
16137 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16138 if (x + right > 0)
16139 k = i;
16140 x -= glyphs[i].pixel_width;
16141 }
16142
16143 return k;
16144}
16145
16146
16147/* Return the index of the last glyph following glyph string S that is
16148 not overwritten by S because of S's right overhang. Value is -1 if
16149 no such glyph is found. */
16150
16151static int
16152right_overwritten (s)
16153 struct glyph_string *s;
16154{
16155 int k = -1;
16156
16157 if (s->right_overhang)
16158 {
16159 int x = 0, i;
16160 struct glyph *glyphs = s->row->glyphs[s->area];
16161 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16162 int end = s->row->used[s->area];
16163
16164 for (i = first; i < end && s->right_overhang > x; ++i)
16165 x += glyphs[i].pixel_width;
16166
16167 k = i;
16168 }
16169
16170 return k;
16171}
16172
16173
16174/* Return the index of the last glyph following glyph string S that
16175 overwrites S because of its left overhang. Value is negative
16176 if no such glyph is found. */
16177
16178static int
16179right_overwriting (s)
16180 struct glyph_string *s;
16181{
16182 int i, k, x;
16183 int end = s->row->used[s->area];
16184 struct glyph *glyphs = s->row->glyphs[s->area];
16185 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16186
16187 k = -1;
16188 x = 0;
16189 for (i = first; i < end; ++i)
16190 {
16191 int left, right;
16192 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16193 if (x - left < 0)
16194 k = i;
16195 x += glyphs[i].pixel_width;
16196 }
16197
16198 return k;
16199}
16200
16201
16202/* Get face and two-byte form of character C in face FACE_ID on frame
16203 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16204 means we want to display multibyte text. DISPLAY_P non-zero means
16205 make sure that X resources for the face returned are allocated.
16206 Value is a pointer to a realized face that is ready for display if
16207 DISPLAY_P is non-zero. */
16208
16209static INLINE struct face *
16210get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16211 struct frame *f;
16212 int c, face_id;
16213 XChar2b *char2b;
16214 int multibyte_p, display_p;
16215{
16216 struct face *face = FACE_FROM_ID (f, face_id);
16217
16218 if (!multibyte_p)
16219 {
16220 /* Unibyte case. We don't have to encode, but we have to make
16221 sure to use a face suitable for unibyte. */
16222 STORE_XCHAR2B (char2b, 0, c);
16223 face_id = FACE_FOR_CHAR (f, face, c);
16224 face = FACE_FROM_ID (f, face_id);
16225 }
16226 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16227 {
16228 /* Case of ASCII in a face known to fit ASCII. */
16229 STORE_XCHAR2B (char2b, 0, c);
16230 }
16231 else
16232 {
16233 int c1, c2, charset;
16234
16235 /* Split characters into bytes. If c2 is -1 afterwards, C is
16236 really a one-byte character so that byte1 is zero. */
16237 SPLIT_CHAR (c, charset, c1, c2);
16238 if (c2 > 0)
16239 STORE_XCHAR2B (char2b, c1, c2);
16240 else
16241 STORE_XCHAR2B (char2b, 0, c1);
16242
16243 /* Maybe encode the character in *CHAR2B. */
16244 if (face->font != NULL)
16245 {
16246 struct font_info *font_info
16247 = FONT_INFO_FROM_ID (f, face->font_info_id);
16248 if (font_info)
16249 rif->encode_char (c, char2b, font_info, 0);
16250 }
16251 }
16252
16253 /* Make sure X resources of the face are allocated. */
16254#ifdef HAVE_X_WINDOWS
16255 if (display_p)
16256#endif
16257 {
16258 xassert (face != NULL);
16259 PREPARE_FACE_FOR_DISPLAY (f, face);
16260 }
16261
16262 return face;
16263}
16264
16265
16266/* Set background width of glyph string S. START is the index of the
16267 first glyph following S. LAST_X is the right-most x-position + 1
16268 in the drawing area. */
16269
16270static INLINE void
16271set_glyph_string_background_width (s, start, last_x)
16272 struct glyph_string *s;
16273 int start;
16274 int last_x;
16275{
16276 /* If the face of this glyph string has to be drawn to the end of
16277 the drawing area, set S->extends_to_end_of_line_p. */
16278 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
16279
16280 if (start == s->row->used[s->area]
16281 && s->area == TEXT_AREA
16282 && ((s->hl == DRAW_NORMAL_TEXT
16283 && (s->row->fill_line_p
16284 || s->face->background != default_face->background
16285 || s->face->stipple != default_face->stipple
16286 || s->row->mouse_face_p))
16287 || s->hl == DRAW_MOUSE_FACE
16288 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
16289 && s->row->fill_line_p)))
16290 s->extends_to_end_of_line_p = 1;
16291
16292 /* If S extends its face to the end of the line, set its
16293 background_width to the distance to the right edge of the drawing
16294 area. */
16295 if (s->extends_to_end_of_line_p)
16296 s->background_width = last_x - s->x + 1;
16297 else
16298 s->background_width = s->width;
16299}
16300
16301
16302/* Compute overhangs and x-positions for glyph string S and its
16303 predecessors, or successors. X is the starting x-position for S.
16304 BACKWARD_P non-zero means process predecessors. */
16305
16306static void
16307compute_overhangs_and_x (s, x, backward_p)
16308 struct glyph_string *s;
16309 int x;
16310 int backward_p;
16311{
16312 if (backward_p)
16313 {
16314 while (s)
16315 {
16316 if (rif->compute_glyph_string_overhangs)
16317 rif->compute_glyph_string_overhangs (s);
16318 x -= s->width;
16319 s->x = x;
16320 s = s->prev;
16321 }
16322 }
16323 else
16324 {
16325 while (s)
16326 {
16327 if (rif->compute_glyph_string_overhangs)
16328 rif->compute_glyph_string_overhangs (s);
16329 s->x = x;
16330 x += s->width;
16331 s = s->next;
16332 }
16333 }
16334}
16335
16336
16337
16338/* The following macros are only called from x_draw_glyphs below.
16339 They reference the following parameters of that function directly:
16340 `w', `row', `area', and `overlap_p'
16341 as well as the following local variables:
16342 `s', `f', and `hdc' (in W32) */
16343
16344#ifdef HAVE_NTGUI
16345/* On W32, silently add local `hdc' variable to argument list of
16346 init_glyph_string. */
16347#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
16348 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
16349#else
16350#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
16351 init_glyph_string (s, char2b, w, row, area, start, hl)
16352#endif
16353
16354/* Add a glyph string for a stretch glyph to the list of strings
16355 between HEAD and TAIL. START is the index of the stretch glyph in
16356 row area AREA of glyph row ROW. END is the index of the last glyph
16357 in that glyph row area. X is the current output position assigned
16358 to the new glyph string constructed. HL overrides that face of the
16359 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
16360 is the right-most x-position of the drawing area. */
16361
16362/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
16363 and below -- keep them on one line. */
16364#define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
16365 do \
16366 { \
16367 s = (struct glyph_string *) alloca (sizeof *s); \
16368 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
16369 START = fill_stretch_glyph_string (s, row, area, START, END); \
16370 append_glyph_string (&HEAD, &TAIL, s); \
16371 s->x = (X); \
16372 } \
16373 while (0)
16374
16375
16376/* Add a glyph string for an image glyph to the list of strings
16377 between HEAD and TAIL. START is the index of the image glyph in
16378 row area AREA of glyph row ROW. END is the index of the last glyph
16379 in that glyph row area. X is the current output position assigned
16380 to the new glyph string constructed. HL overrides that face of the
16381 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
16382 is the right-most x-position of the drawing area. */
16383
16384#define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
16385 do \
16386 { \
16387 s = (struct glyph_string *) alloca (sizeof *s); \
16388 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
16389 fill_image_glyph_string (s); \
16390 append_glyph_string (&HEAD, &TAIL, s); \
16391 ++START; \
16392 s->x = (X); \
16393 } \
16394 while (0)
16395
16396
16397/* Add a glyph string for a sequence of character glyphs to the list
16398 of strings between HEAD and TAIL. START is the index of the first
16399 glyph in row area AREA of glyph row ROW that is part of the new
16400 glyph string. END is the index of the last glyph in that glyph row
16401 area. X is the current output position assigned to the new glyph
16402 string constructed. HL overrides that face of the glyph; e.g. it
16403 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
16404 right-most x-position of the drawing area. */
16405
16406#define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
16407 do \
16408 { \
16409 int c, face_id; \
16410 XChar2b *char2b; \
16411 \
16412 c = (row)->glyphs[area][START].u.ch; \
16413 face_id = (row)->glyphs[area][START].face_id; \
16414 \
16415 s = (struct glyph_string *) alloca (sizeof *s); \
16416 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
16417 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
16418 append_glyph_string (&HEAD, &TAIL, s); \
16419 s->x = (X); \
16420 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
16421 } \
16422 while (0)
16423
16424
16425/* Add a glyph string for a composite sequence to the list of strings
16426 between HEAD and TAIL. START is the index of the first glyph in
16427 row area AREA of glyph row ROW that is part of the new glyph
16428 string. END is the index of the last glyph in that glyph row area.
16429 X is the current output position assigned to the new glyph string
16430 constructed. HL overrides that face of the glyph; e.g. it is
16431 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
16432 x-position of the drawing area. */
16433
16434#define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
16435 do { \
16436 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
16437 int face_id = (row)->glyphs[area][START].face_id; \
16438 struct face *base_face = FACE_FROM_ID (f, face_id); \
16439 struct composition *cmp = composition_table[cmp_id]; \
16440 int glyph_len = cmp->glyph_len; \
16441 XChar2b *char2b; \
16442 struct face **faces; \
16443 struct glyph_string *first_s = NULL; \
16444 int n; \
16445 \
16446 base_face = base_face->ascii_face; \
16447 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
16448 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
16449 /* At first, fill in `char2b' and `faces'. */ \
16450 for (n = 0; n < glyph_len; n++) \
16451 { \
16452 int c = COMPOSITION_GLYPH (cmp, n); \
16453 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
16454 faces[n] = FACE_FROM_ID (f, this_face_id); \
16455 get_char_face_and_encoding (f, c, this_face_id, \
16456 char2b + n, 1, 1); \
16457 } \
16458 \
16459 /* Make glyph_strings for each glyph sequence that is drawable by \
16460 the same face, and append them to HEAD/TAIL. */ \
16461 for (n = 0; n < cmp->glyph_len;) \
16462 { \
16463 s = (struct glyph_string *) alloca (sizeof *s); \
16464 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
16465 append_glyph_string (&(HEAD), &(TAIL), s); \
16466 s->cmp = cmp; \
16467 s->gidx = n; \
16468 s->x = (X); \
16469 \
16470 if (n == 0) \
16471 first_s = s; \
16472 \
16473 n = fill_composite_glyph_string (s, faces, overlaps_p); \
16474 } \
16475 \
16476 ++START; \
16477 s = first_s; \
16478 } while (0)
16479
16480
16481/* Build a list of glyph strings between HEAD and TAIL for the glyphs
16482 of AREA of glyph row ROW on window W between indices START and END.
16483 HL overrides the face for drawing glyph strings, e.g. it is
16484 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
16485 x-positions of the drawing area.
16486
16487 This is an ugly monster macro construct because we must use alloca
16488 to allocate glyph strings (because x_draw_glyphs can be called
16489 asynchronously). */
16490
16491#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
16492 do \
16493 { \
16494 HEAD = TAIL = NULL; \
16495 while (START < END) \
16496 { \
16497 struct glyph *first_glyph = (row)->glyphs[area] + START; \
16498 switch (first_glyph->type) \
16499 { \
16500 case CHAR_GLYPH: \
16501 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
16502 HL, X, LAST_X); \
16503 break; \
16504 \
16505 case COMPOSITE_GLYPH: \
16506 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
16507 HL, X, LAST_X); \
16508 break; \
16509 \
16510 case STRETCH_GLYPH: \
16511 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
16512 HL, X, LAST_X); \
16513 break; \
16514 \
16515 case IMAGE_GLYPH: \
16516 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
16517 HL, X, LAST_X); \
16518 break; \
16519 \
16520 default: \
16521 abort (); \
16522 } \
16523 \
16524 set_glyph_string_background_width (s, START, LAST_X); \
16525 (X) += s->width; \
16526 } \
16527 } \
16528 while (0)
16529
16530
16531/* Draw glyphs between START and END in AREA of ROW on window W,
16532 starting at x-position X. X is relative to AREA in W. HL is a
16533 face-override with the following meaning:
16534
16535 DRAW_NORMAL_TEXT draw normally
16536 DRAW_CURSOR draw in cursor face
16537 DRAW_MOUSE_FACE draw in mouse face.
16538 DRAW_INVERSE_VIDEO draw in mode line face
16539 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
16540 DRAW_IMAGE_RAISED draw an image with a raised relief around it
16541
16542 If OVERLAPS_P is non-zero, draw only the foreground of characters
16543 and clip to the physical height of ROW.
16544
16545 Value is the x-position reached, relative to AREA of W. */
16546
16547int
16548x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
16549 struct window *w;
16550 int x;
16551 struct glyph_row *row;
16552 enum glyph_row_area area;
16553 int start, end;
16554 enum draw_glyphs_face hl;
16555 int overlaps_p;
16556{
16557 struct glyph_string *head, *tail;
16558 struct glyph_string *s;
16559 int last_x, area_width;
16560 int x_reached;
16561 int i, j;
16562 struct frame *f = XFRAME (WINDOW_FRAME (w));
16563 DECLARE_HDC (hdc);
16564
16565 ALLOCATE_HDC (hdc, f);
16566
16567 /* Let's rather be paranoid than getting a SEGV. */
16568 end = min (end, row->used[area]);
16569 start = max (0, start);
16570 start = min (end, start);
16571
16572 /* Translate X to frame coordinates. Set last_x to the right
16573 end of the drawing area. */
16574 if (row->full_width_p)
16575 {
16576 /* X is relative to the left edge of W, without scroll bars
16577 or fringes. */
16578 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
16579
16580 x += window_left_x;
16581 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
16582 last_x = window_left_x + area_width;
16583
16584 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
16585 {
16586 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
16587 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
16588 last_x += width;
16589 else
16590 x -= width;
16591 }
16592
16593 x += FRAME_INTERNAL_BORDER_WIDTH (f);
16594 /* ++KFS: W32 and MAC versions had -= in next line (bug??) */
16595 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
16596 }
16597 else
16598 {
16599 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
16600 area_width = window_box_width (w, area);
16601 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
16602 }
16603
16604 /* Build a doubly-linked list of glyph_string structures between
16605 head and tail from what we have to draw. Note that the macro
16606 BUILD_GLYPH_STRINGS will modify its start parameter. That's
16607 the reason we use a separate variable `i'. */
16608 i = start;
16609 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
16610 if (tail)
16611 x_reached = tail->x + tail->background_width;
16612 else
16613 x_reached = x;
16614
16615 /* If there are any glyphs with lbearing < 0 or rbearing > width in
16616 the row, redraw some glyphs in front or following the glyph
16617 strings built above. */
16618 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
16619 {
16620 int dummy_x = 0;
16621 struct glyph_string *h, *t;
16622
16623 /* Compute overhangs for all glyph strings. */
16624 if (rif->compute_glyph_string_overhangs)
16625 for (s = head; s; s = s->next)
16626 rif->compute_glyph_string_overhangs (s);
16627
16628 /* Prepend glyph strings for glyphs in front of the first glyph
16629 string that are overwritten because of the first glyph
16630 string's left overhang. The background of all strings
16631 prepended must be drawn because the first glyph string
16632 draws over it. */
16633 i = left_overwritten (head);
16634 if (i >= 0)
16635 {
16636 j = i;
16637 BUILD_GLYPH_STRINGS (j, start, h, t,
16638 DRAW_NORMAL_TEXT, dummy_x, last_x);
16639 start = i;
16640 compute_overhangs_and_x (t, head->x, 1);
16641 prepend_glyph_string_lists (&head, &tail, h, t);
16642 }
16643
16644 /* Prepend glyph strings for glyphs in front of the first glyph
16645 string that overwrite that glyph string because of their
16646 right overhang. For these strings, only the foreground must
16647 be drawn, because it draws over the glyph string at `head'.
16648 The background must not be drawn because this would overwrite
16649 right overhangs of preceding glyphs for which no glyph
16650 strings exist. */
16651 i = left_overwriting (head);
16652 if (i >= 0)
16653 {
16654 BUILD_GLYPH_STRINGS (i, start, h, t,
16655 DRAW_NORMAL_TEXT, dummy_x, last_x);
16656 for (s = h; s; s = s->next)
16657 s->background_filled_p = 1;
16658 compute_overhangs_and_x (t, head->x, 1);
16659 prepend_glyph_string_lists (&head, &tail, h, t);
16660 }
16661
16662 /* Append glyphs strings for glyphs following the last glyph
16663 string tail that are overwritten by tail. The background of
16664 these strings has to be drawn because tail's foreground draws
16665 over it. */
16666 i = right_overwritten (tail);
16667 if (i >= 0)
16668 {
16669 BUILD_GLYPH_STRINGS (end, i, h, t,
16670 DRAW_NORMAL_TEXT, x, last_x);
16671 compute_overhangs_and_x (h, tail->x + tail->width, 0);
16672 append_glyph_string_lists (&head, &tail, h, t);
16673 }
16674
16675 /* Append glyph strings for glyphs following the last glyph
16676 string tail that overwrite tail. The foreground of such
16677 glyphs has to be drawn because it writes into the background
16678 of tail. The background must not be drawn because it could
16679 paint over the foreground of following glyphs. */
16680 i = right_overwriting (tail);
16681 if (i >= 0)
16682 {
16683 BUILD_GLYPH_STRINGS (end, i, h, t,
16684 DRAW_NORMAL_TEXT, x, last_x);
16685 for (s = h; s; s = s->next)
16686 s->background_filled_p = 1;
16687 compute_overhangs_and_x (h, tail->x + tail->width, 0);
16688 append_glyph_string_lists (&head, &tail, h, t);
16689 }
16690 }
16691
16692 /* Draw all strings. */
16693 for (s = head; s; s = s->next)
16694 rif->draw_glyph_string (s);
16695
16696 if (area == TEXT_AREA
16697 && !row->full_width_p
16698 /* When drawing overlapping rows, only the glyph strings'
16699 foreground is drawn, which doesn't erase a cursor
16700 completely. */
16701 && !overlaps_p)
16702 {
16703 int x0 = head ? head->x : x;
16704 int x1 = tail ? tail->x + tail->background_width : x;
16705
16706 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
16707 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
16708
16709 /* ++KFS: W32 and MAC versions had following test here:
16710 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
16711 */
16712
16713 if (XFASTINT (w->left_margin_width) != 0)
16714 {
16715 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
16716 x0 -= left_area_width;
16717 x1 -= left_area_width;
16718 }
16719
16720 notice_overwritten_cursor (w, area, x0, x1,
16721 row->y, MATRIX_ROW_BOTTOM_Y (row));
16722 }
16723
16724 /* Value is the x-position up to which drawn, relative to AREA of W.
16725 This doesn't include parts drawn because of overhangs. */
16726 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
16727 if (!row->full_width_p)
16728 {
16729 /* ++KFS: W32 and MAC versions only had this test here:
16730 if (area > LEFT_MARGIN_AREA)
16731 */
16732
16733 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
16734 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
16735 if (area > TEXT_AREA)
16736 x_reached -= window_box_width (w, TEXT_AREA);
16737 }
16738
16739 RELEASE_HDC (hdc, f);
16740
16741 return x_reached;
16742}
16743
16744
16745/* Store one glyph for IT->char_to_display in IT->glyph_row.
16746 Called from x_produce_glyphs when IT->glyph_row is non-null. */
16747
16748static INLINE void
16749append_glyph (it)
16750 struct it *it;
16751{
16752 struct glyph *glyph;
16753 enum glyph_row_area area = it->area;
16754
16755 xassert (it->glyph_row);
16756 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
16757
16758 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16759 if (glyph < it->glyph_row->glyphs[area + 1])
16760 {
16761 glyph->charpos = CHARPOS (it->position);
16762 glyph->object = it->object;
16763 glyph->pixel_width = it->pixel_width;
16764 glyph->voffset = it->voffset;
16765 glyph->type = CHAR_GLYPH;
16766 glyph->multibyte_p = it->multibyte_p;
16767 glyph->left_box_line_p = it->start_of_box_run_p;
16768 glyph->right_box_line_p = it->end_of_box_run_p;
16769 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
16770 || it->phys_descent > it->descent);
16771 glyph->padding_p = 0;
16772 glyph->glyph_not_available_p = it->glyph_not_available_p;
16773 glyph->face_id = it->face_id;
16774 glyph->u.ch = it->char_to_display;
16775 glyph->font_type = FONT_TYPE_UNKNOWN;
16776 ++it->glyph_row->used[area];
16777 }
16778}
16779
16780/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
16781 Called from x_produce_glyphs when IT->glyph_row is non-null. */
16782
16783static INLINE void
16784append_composite_glyph (it)
16785 struct it *it;
16786{
16787 struct glyph *glyph;
16788 enum glyph_row_area area = it->area;
16789
16790 xassert (it->glyph_row);
16791
16792 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16793 if (glyph < it->glyph_row->glyphs[area + 1])
16794 {
16795 glyph->charpos = CHARPOS (it->position);
16796 glyph->object = it->object;
16797 glyph->pixel_width = it->pixel_width;
16798 glyph->voffset = it->voffset;
16799 glyph->type = COMPOSITE_GLYPH;
16800 glyph->multibyte_p = it->multibyte_p;
16801 glyph->left_box_line_p = it->start_of_box_run_p;
16802 glyph->right_box_line_p = it->end_of_box_run_p;
16803 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
16804 || it->phys_descent > it->descent);
16805 glyph->padding_p = 0;
16806 glyph->glyph_not_available_p = 0;
16807 glyph->face_id = it->face_id;
16808 glyph->u.cmp_id = it->cmp_id;
16809 glyph->font_type = FONT_TYPE_UNKNOWN;
16810 ++it->glyph_row->used[area];
16811 }
16812}
16813
16814
16815/* Change IT->ascent and IT->height according to the setting of
16816 IT->voffset. */
16817
16818static INLINE void
16819take_vertical_position_into_account (it)
16820 struct it *it;
16821{
16822 if (it->voffset)
16823 {
16824 if (it->voffset < 0)
16825 /* Increase the ascent so that we can display the text higher
16826 in the line. */
16827 it->ascent += abs (it->voffset);
16828 else
16829 /* Increase the descent so that we can display the text lower
16830 in the line. */
16831 it->descent += it->voffset;
16832 }
16833}
16834
16835
16836/* Produce glyphs/get display metrics for the image IT is loaded with.
16837 See the description of struct display_iterator in dispextern.h for
16838 an overview of struct display_iterator. */
16839
16840static void
16841produce_image_glyph (it)
16842 struct it *it;
16843{
16844 struct image *img;
16845 struct face *face;
16846
16847 xassert (it->what == IT_IMAGE);
16848
16849 face = FACE_FROM_ID (it->f, it->face_id);
16850 img = IMAGE_FROM_ID (it->f, it->image_id);
16851 xassert (img);
16852
16853 /* Make sure X resources of the face and image are loaded. */
16854 PREPARE_FACE_FOR_DISPLAY (it->f, face);
16855 prepare_image_for_display (it->f, img);
16856
16857 it->ascent = it->phys_ascent = image_ascent (img, face);
16858 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
16859 it->pixel_width = img->width + 2 * img->hmargin;
16860
16861 it->nglyphs = 1;
16862
16863 if (face->box != FACE_NO_BOX)
16864 {
16865 if (face->box_line_width > 0)
16866 {
16867 it->ascent += face->box_line_width;
16868 it->descent += face->box_line_width;
16869 }
16870
16871 if (it->start_of_box_run_p)
16872 it->pixel_width += abs (face->box_line_width);
16873 if (it->end_of_box_run_p)
16874 it->pixel_width += abs (face->box_line_width);
16875 }
16876
16877 take_vertical_position_into_account (it);
16878
16879 if (it->glyph_row)
16880 {
16881 struct glyph *glyph;
16882 enum glyph_row_area area = it->area;
16883
16884 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16885 if (glyph < it->glyph_row->glyphs[area + 1])
16886 {
16887 glyph->charpos = CHARPOS (it->position);
16888 glyph->object = it->object;
16889 glyph->pixel_width = it->pixel_width;
16890 glyph->voffset = it->voffset;
16891 glyph->type = IMAGE_GLYPH;
16892 glyph->multibyte_p = it->multibyte_p;
16893 glyph->left_box_line_p = it->start_of_box_run_p;
16894 glyph->right_box_line_p = it->end_of_box_run_p;
16895 glyph->overlaps_vertically_p = 0;
16896 glyph->padding_p = 0;
16897 glyph->glyph_not_available_p = 0;
16898 glyph->face_id = it->face_id;
16899 glyph->u.img_id = img->id;
16900 glyph->font_type = FONT_TYPE_UNKNOWN;
16901 ++it->glyph_row->used[area];
16902 }
16903 }
16904}
16905
16906
16907/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
16908 of the glyph, WIDTH and HEIGHT are the width and height of the
16909 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
16910 ascent of the glyph (0 <= ASCENT <= 1). */
16911
16912static void
16913append_stretch_glyph (it, object, width, height, ascent)
16914 struct it *it;
16915 Lisp_Object object;
16916 int width, height;
16917 double ascent;
16918{
16919 struct glyph *glyph;
16920 enum glyph_row_area area = it->area;
16921
16922 xassert (ascent >= 0 && ascent <= 1);
16923
16924 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16925 if (glyph < it->glyph_row->glyphs[area + 1])
16926 {
16927 glyph->charpos = CHARPOS (it->position);
16928 glyph->object = object;
16929 glyph->pixel_width = width;
16930 glyph->voffset = it->voffset;
16931 glyph->type = STRETCH_GLYPH;
16932 glyph->multibyte_p = it->multibyte_p;
16933 glyph->left_box_line_p = it->start_of_box_run_p;
16934 glyph->right_box_line_p = it->end_of_box_run_p;
16935 glyph->overlaps_vertically_p = 0;
16936 glyph->padding_p = 0;
16937 glyph->glyph_not_available_p = 0;
16938 glyph->face_id = it->face_id;
16939 glyph->u.stretch.ascent = height * ascent;
16940 glyph->u.stretch.height = height;
16941 glyph->font_type = FONT_TYPE_UNKNOWN;
16942 ++it->glyph_row->used[area];
16943 }
16944}
16945
16946
16947/* Produce a stretch glyph for iterator IT. IT->object is the value
16948 of the glyph property displayed. The value must be a list
16949 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
16950 being recognized:
16951
16952 1. `:width WIDTH' specifies that the space should be WIDTH *
16953 canonical char width wide. WIDTH may be an integer or floating
16954 point number.
16955
16956 2. `:relative-width FACTOR' specifies that the width of the stretch
16957 should be computed from the width of the first character having the
16958 `glyph' property, and should be FACTOR times that width.
16959
16960 3. `:align-to HPOS' specifies that the space should be wide enough
16961 to reach HPOS, a value in canonical character units.
16962
16963 Exactly one of the above pairs must be present.
16964
16965 4. `:height HEIGHT' specifies that the height of the stretch produced
16966 should be HEIGHT, measured in canonical character units.
16967
16968 5. `:relative-height FACTOR' specifies that the height of the
16969 stretch should be FACTOR times the height of the characters having
16970 the glyph property.
16971
16972 Either none or exactly one of 4 or 5 must be present.
16973
16974 6. `:ascent ASCENT' specifies that ASCENT percent of the height
16975 of the stretch should be used for the ascent of the stretch.
16976 ASCENT must be in the range 0 <= ASCENT <= 100. */
16977
16978#define NUMVAL(X) \
16979 ((INTEGERP (X) || FLOATP (X)) \
16980 ? XFLOATINT (X) \
16981 : - 1)
16982
16983
16984static void
16985produce_stretch_glyph (it)
16986 struct it *it;
16987{
16988 /* (space :width WIDTH :height HEIGHT. */
16989 Lisp_Object prop, plist;
16990 int width = 0, height = 0;
16991 double ascent = 0;
16992 struct face *face = FACE_FROM_ID (it->f, it->face_id);
16993 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
16994
16995 PREPARE_FACE_FOR_DISPLAY (it->f, face);
16996
16997 /* List should start with `space'. */
16998 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
16999 plist = XCDR (it->object);
17000
17001 /* Compute the width of the stretch. */
17002 if (prop = Fplist_get (plist, QCwidth),
17003 NUMVAL (prop) > 0)
17004 /* Absolute width `:width WIDTH' specified and valid. */
17005 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
17006 else if (prop = Fplist_get (plist, QCrelative_width),
17007 NUMVAL (prop) > 0)
17008 {
17009 /* Relative width `:relative-width FACTOR' specified and valid.
17010 Compute the width of the characters having the `glyph'
17011 property. */
17012 struct it it2;
17013 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17014
17015 it2 = *it;
17016 if (it->multibyte_p)
17017 {
17018 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17019 - IT_BYTEPOS (*it));
17020 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17021 }
17022 else
17023 it2.c = *p, it2.len = 1;
17024
17025 it2.glyph_row = NULL;
17026 it2.what = IT_CHARACTER;
17027 x_produce_glyphs (&it2);
17028 width = NUMVAL (prop) * it2.pixel_width;
17029 }
17030 else if (prop = Fplist_get (plist, QCalign_to),
17031 NUMVAL (prop) > 0)
17032 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
17033 else
17034 /* Nothing specified -> width defaults to canonical char width. */
17035 width = CANON_X_UNIT (it->f);
17036
17037 /* Compute height. */
17038 if (prop = Fplist_get (plist, QCheight),
17039 NUMVAL (prop) > 0)
17040 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
17041 else if (prop = Fplist_get (plist, QCrelative_height),
17042 NUMVAL (prop) > 0)
17043 height = FONT_HEIGHT (font) * NUMVAL (prop);
17044 else
17045 height = FONT_HEIGHT (font);
17046
17047 /* Compute percentage of height used for ascent. If
17048 `:ascent ASCENT' is present and valid, use that. Otherwise,
17049 derive the ascent from the font in use. */
17050 if (prop = Fplist_get (plist, QCascent),
17051 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17052 ascent = NUMVAL (prop) / 100.0;
17053 else
17054 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17055
17056 if (width <= 0)
17057 width = 1;
17058 if (height <= 0)
17059 height = 1;
17060
17061 if (it->glyph_row)
17062 {
17063 Lisp_Object object = it->stack[it->sp - 1].string;
17064 if (!STRINGP (object))
17065 object = it->w->buffer;
17066 append_stretch_glyph (it, object, width, height, ascent);
17067 }
17068
17069 it->pixel_width = width;
17070 it->ascent = it->phys_ascent = height * ascent;
17071 it->descent = it->phys_descent = height - it->ascent;
17072 it->nglyphs = 1;
17073
17074 if (face->box != FACE_NO_BOX)
17075 {
17076 if (face->box_line_width > 0)
17077 {
17078 it->ascent += face->box_line_width;
17079 it->descent += face->box_line_width;
17080 }
17081
17082 if (it->start_of_box_run_p)
17083 it->pixel_width += abs (face->box_line_width);
17084 if (it->end_of_box_run_p)
17085 it->pixel_width += abs (face->box_line_width);
17086 }
17087
17088 take_vertical_position_into_account (it);
17089}
17090
17091/* RIF:
17092 Produce glyphs/get display metrics for the display element IT is
17093 loaded with. See the description of struct display_iterator in
17094 dispextern.h for an overview of struct display_iterator. */
17095
17096void
17097x_produce_glyphs (it)
17098 struct it *it;
17099{
17100 it->glyph_not_available_p = 0;
17101
17102 if (it->what == IT_CHARACTER)
17103 {
17104 XChar2b char2b;
17105 XFontStruct *font;
17106 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17107 XCharStruct *pcm;
17108 int font_not_found_p;
17109 struct font_info *font_info;
17110 int boff; /* baseline offset */
17111 /* We may change it->multibyte_p upon unibyte<->multibyte
17112 conversion. So, save the current value now and restore it
17113 later.
17114
17115 Note: It seems that we don't have to record multibyte_p in
17116 struct glyph because the character code itself tells if or
17117 not the character is multibyte. Thus, in the future, we must
17118 consider eliminating the field `multibyte_p' in the struct
17119 glyph. */
17120 int saved_multibyte_p = it->multibyte_p;
17121
17122 /* Maybe translate single-byte characters to multibyte, or the
17123 other way. */
17124 it->char_to_display = it->c;
17125 if (!ASCII_BYTE_P (it->c))
17126 {
17127 if (unibyte_display_via_language_environment
17128 && SINGLE_BYTE_CHAR_P (it->c)
17129 && (it->c >= 0240
17130 || !NILP (Vnonascii_translation_table)))
17131 {
17132 it->char_to_display = unibyte_char_to_multibyte (it->c);
17133 it->multibyte_p = 1;
17134 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17135 face = FACE_FROM_ID (it->f, it->face_id);
17136 }
17137 else if (!SINGLE_BYTE_CHAR_P (it->c)
17138 && !it->multibyte_p)
17139 {
17140 it->multibyte_p = 1;
17141 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17142 face = FACE_FROM_ID (it->f, it->face_id);
17143 }
17144 }
17145
17146 /* Get font to use. Encode IT->char_to_display. */
17147 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17148 &char2b, it->multibyte_p, 0);
17149 font = face->font;
17150
17151 /* When no suitable font found, use the default font. */
17152 font_not_found_p = font == NULL;
17153 if (font_not_found_p)
17154 {
17155 font = FRAME_FONT (it->f);
17156 boff = FRAME_BASELINE_OFFSET (it->f);
17157 font_info = NULL;
17158 }
17159 else
17160 {
17161 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17162 boff = font_info->baseline_offset;
17163 if (font_info->vertical_centering)
17164 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17165 }
17166
17167 if (it->char_to_display >= ' '
17168 && (!it->multibyte_p || it->char_to_display < 128))
17169 {
17170 /* Either unibyte or ASCII. */
17171 int stretched_p;
17172
17173 it->nglyphs = 1;
17174
17175 pcm = rif->per_char_metric (font, &char2b,
17176 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17177 it->ascent = FONT_BASE (font) + boff;
17178 it->descent = FONT_DESCENT (font) - boff;
17179
17180 if (pcm)
17181 {
17182 it->phys_ascent = pcm->ascent + boff;
17183 it->phys_descent = pcm->descent - boff;
17184 it->pixel_width = pcm->width;
17185 }
17186 else
17187 {
17188 it->glyph_not_available_p = 1;
17189 it->phys_ascent = FONT_BASE (font) + boff;
17190 it->phys_descent = FONT_DESCENT (font) - boff;
17191 it->pixel_width = FONT_WIDTH (font);
17192 }
17193
17194 /* If this is a space inside a region of text with
17195 `space-width' property, change its width. */
17196 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17197 if (stretched_p)
17198 it->pixel_width *= XFLOATINT (it->space_width);
17199
17200 /* If face has a box, add the box thickness to the character
17201 height. If character has a box line to the left and/or
17202 right, add the box line width to the character's width. */
17203 if (face->box != FACE_NO_BOX)
17204 {
17205 int thick = face->box_line_width;
17206
17207 if (thick > 0)
17208 {
17209 it->ascent += thick;
17210 it->descent += thick;
17211 }
17212 else
17213 thick = -thick;
17214
17215 if (it->start_of_box_run_p)
17216 it->pixel_width += thick;
17217 if (it->end_of_box_run_p)
17218 it->pixel_width += thick;
17219 }
17220
17221 /* If face has an overline, add the height of the overline
17222 (1 pixel) and a 1 pixel margin to the character height. */
17223 if (face->overline_p)
17224 it->ascent += 2;
17225
17226 take_vertical_position_into_account (it);
17227
17228 /* If we have to actually produce glyphs, do it. */
17229 if (it->glyph_row)
17230 {
17231 if (stretched_p)
17232 {
17233 /* Translate a space with a `space-width' property
17234 into a stretch glyph. */
17235 double ascent = (double) FONT_BASE (font)
17236 / FONT_HEIGHT (font);
17237 append_stretch_glyph (it, it->object, it->pixel_width,
17238 it->ascent + it->descent, ascent);
17239 }
17240 else
17241 append_glyph (it);
17242
17243 /* If characters with lbearing or rbearing are displayed
17244 in this line, record that fact in a flag of the
17245 glyph row. This is used to optimize X output code. */
17246 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17247 it->glyph_row->contains_overlapping_glyphs_p = 1;
17248 }
17249 }
17250 else if (it->char_to_display == '\n')
17251 {
17252 /* A newline has no width but we need the height of the line. */
17253 it->pixel_width = 0;
17254 it->nglyphs = 0;
17255 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17256 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17257
17258 if (face->box != FACE_NO_BOX
17259 && face->box_line_width > 0)
17260 {
17261 it->ascent += face->box_line_width;
17262 it->descent += face->box_line_width;
17263 }
17264 }
17265 else if (it->char_to_display == '\t')
17266 {
17267 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
17268 int x = it->current_x + it->continuation_lines_width;
17269 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17270
17271 /* If the distance from the current position to the next tab
17272 stop is less than a canonical character width, use the
17273 tab stop after that. */
17274 if (next_tab_x - x < CANON_X_UNIT (it->f))
17275 next_tab_x += tab_width;
17276
17277 it->pixel_width = next_tab_x - x;
17278 it->nglyphs = 1;
17279 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17280 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17281
17282 if (it->glyph_row)
17283 {
17284 double ascent = (double) it->ascent / (it->ascent + it->descent);
17285 append_stretch_glyph (it, it->object, it->pixel_width,
17286 it->ascent + it->descent, ascent);
17287 }
17288 }
17289 else
17290 {
17291 /* A multi-byte character. Assume that the display width of the
17292 character is the width of the character multiplied by the
17293 width of the font. */
17294
17295 /* If we found a font, this font should give us the right
17296 metrics. If we didn't find a font, use the frame's
17297 default font and calculate the width of the character
17298 from the charset width; this is what old redisplay code
17299 did. */
17300
17301 pcm = rif->per_char_metric (font, &char2b,
17302 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
17303
17304 if (font_not_found_p || !pcm)
17305 {
17306 int charset = CHAR_CHARSET (it->char_to_display);
17307
17308 it->glyph_not_available_p = 1;
17309 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
17310 * CHARSET_WIDTH (charset));
17311 it->phys_ascent = FONT_BASE (font) + boff;
17312 it->phys_descent = FONT_DESCENT (font) - boff;
17313 }
17314 else
17315 {
17316 it->pixel_width = pcm->width;
17317 it->phys_ascent = pcm->ascent + boff;
17318 it->phys_descent = pcm->descent - boff;
17319 if (it->glyph_row
17320 && (pcm->lbearing < 0
17321 || pcm->rbearing > pcm->width))
17322 it->glyph_row->contains_overlapping_glyphs_p = 1;
17323 }
17324 it->nglyphs = 1;
17325 it->ascent = FONT_BASE (font) + boff;
17326 it->descent = FONT_DESCENT (font) - boff;
17327 if (face->box != FACE_NO_BOX)
17328 {
17329 int thick = face->box_line_width;
17330
17331 if (thick > 0)
17332 {
17333 it->ascent += thick;
17334 it->descent += thick;
17335 }
17336 else
17337 thick = - thick;
17338
17339 if (it->start_of_box_run_p)
17340 it->pixel_width += thick;
17341 if (it->end_of_box_run_p)
17342 it->pixel_width += thick;
17343 }
17344
17345 /* If face has an overline, add the height of the overline
17346 (1 pixel) and a 1 pixel margin to the character height. */
17347 if (face->overline_p)
17348 it->ascent += 2;
17349
17350 take_vertical_position_into_account (it);
17351
17352 if (it->glyph_row)
17353 append_glyph (it);
17354 }
17355 it->multibyte_p = saved_multibyte_p;
17356 }
17357 else if (it->what == IT_COMPOSITION)
17358 {
17359 /* Note: A composition is represented as one glyph in the
17360 glyph matrix. There are no padding glyphs. */
17361 XChar2b char2b;
17362 XFontStruct *font;
17363 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17364 XCharStruct *pcm;
17365 int font_not_found_p;
17366 struct font_info *font_info;
17367 int boff; /* baseline offset */
17368 struct composition *cmp = composition_table[it->cmp_id];
17369
17370 /* Maybe translate single-byte characters to multibyte. */
17371 it->char_to_display = it->c;
17372 if (unibyte_display_via_language_environment
17373 && SINGLE_BYTE_CHAR_P (it->c)
17374 && (it->c >= 0240
17375 || (it->c >= 0200
17376 && !NILP (Vnonascii_translation_table))))
17377 {
17378 it->char_to_display = unibyte_char_to_multibyte (it->c);
17379 }
17380
17381 /* Get face and font to use. Encode IT->char_to_display. */
17382 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17383 face = FACE_FROM_ID (it->f, it->face_id);
17384 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17385 &char2b, it->multibyte_p, 0);
17386 font = face->font;
17387
17388 /* When no suitable font found, use the default font. */
17389 font_not_found_p = font == NULL;
17390 if (font_not_found_p)
17391 {
17392 font = FRAME_FONT (it->f);
17393 boff = FRAME_BASELINE_OFFSET (it->f);
17394 font_info = NULL;
17395 }
17396 else
17397 {
17398 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17399 boff = font_info->baseline_offset;
17400 if (font_info->vertical_centering)
17401 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17402 }
17403
17404 /* There are no padding glyphs, so there is only one glyph to
17405 produce for the composition. Important is that pixel_width,
17406 ascent and descent are the values of what is drawn by
17407 draw_glyphs (i.e. the values of the overall glyphs composed). */
17408 it->nglyphs = 1;
17409
17410 /* If we have not yet calculated pixel size data of glyphs of
17411 the composition for the current face font, calculate them
17412 now. Theoretically, we have to check all fonts for the
17413 glyphs, but that requires much time and memory space. So,
17414 here we check only the font of the first glyph. This leads
17415 to incorrect display very rarely, and C-l (recenter) can
17416 correct the display anyway. */
17417 if (cmp->font != (void *) font)
17418 {
17419 /* Ascent and descent of the font of the first character of
17420 this composition (adjusted by baseline offset). Ascent
17421 and descent of overall glyphs should not be less than
17422 them respectively. */
17423 int font_ascent = FONT_BASE (font) + boff;
17424 int font_descent = FONT_DESCENT (font) - boff;
17425 /* Bounding box of the overall glyphs. */
17426 int leftmost, rightmost, lowest, highest;
17427 int i, width, ascent, descent;
17428
17429 cmp->font = (void *) font;
17430
17431 /* Initialize the bounding box. */
17432 if (font_info
17433 && (pcm = rif->per_char_metric (font, &char2b,
17434 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
17435 {
17436 width = pcm->width;
17437 ascent = pcm->ascent;
17438 descent = pcm->descent;
17439 }
17440 else
17441 {
17442 width = FONT_WIDTH (font);
17443 ascent = FONT_BASE (font);
17444 descent = FONT_DESCENT (font);
17445 }
17446
17447 rightmost = width;
17448 lowest = - descent + boff;
17449 highest = ascent + boff;
17450 leftmost = 0;
17451
17452 if (font_info
17453 && font_info->default_ascent
17454 && CHAR_TABLE_P (Vuse_default_ascent)
17455 && !NILP (Faref (Vuse_default_ascent,
17456 make_number (it->char_to_display))))
17457 highest = font_info->default_ascent + boff;
17458
17459 /* Draw the first glyph at the normal position. It may be
17460 shifted to right later if some other glyphs are drawn at
17461 the left. */
17462 cmp->offsets[0] = 0;
17463 cmp->offsets[1] = boff;
17464
17465 /* Set cmp->offsets for the remaining glyphs. */
17466 for (i = 1; i < cmp->glyph_len; i++)
17467 {
17468 int left, right, btm, top;
17469 int ch = COMPOSITION_GLYPH (cmp, i);
17470 int face_id = FACE_FOR_CHAR (it->f, face, ch);
17471
17472 face = FACE_FROM_ID (it->f, face_id);
17473 get_char_face_and_encoding (it->f, ch, face->id,
17474 &char2b, it->multibyte_p, 0);
17475 font = face->font;
17476 if (font == NULL)
17477 {
17478 font = FRAME_FONT (it->f);
2a6d0874 17479 boff = FRAME_BASELINE_OFFSET (it->f);
133c764e
KS
17480 font_info = NULL;
17481 }
17482 else
17483 {
17484 font_info
17485 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17486 boff = font_info->baseline_offset;
17487 if (font_info->vertical_centering)
17488 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17489 }
17490
17491 if (font_info
2a6d0874 17492 && (pcm = rif->per_char_metric (font, &char2b,
133c764e
KS
17493 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
17494 {
17495 width = pcm->width;
17496 ascent = pcm->ascent;
17497 descent = pcm->descent;
17498 }
17499 else
17500 {
17501 width = FONT_WIDTH (font);
17502 ascent = 1;
17503 descent = 0;
17504 }
17505
17506 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
17507 {
17508 /* Relative composition with or without
17509 alternate chars. */
17510 left = (leftmost + rightmost - width) / 2;
17511 btm = - descent + boff;
17512 if (font_info && font_info->relative_compose
17513 && (! CHAR_TABLE_P (Vignore_relative_composition)
17514 || NILP (Faref (Vignore_relative_composition,
17515 make_number (ch)))))
17516 {
17517
17518 if (- descent >= font_info->relative_compose)
17519 /* One extra pixel between two glyphs. */
17520 btm = highest + 1;
17521 else if (ascent <= 0)
17522 /* One extra pixel between two glyphs. */
17523 btm = lowest - 1 - ascent - descent;
17524 }
17525 }
17526 else
17527 {
17528 /* A composition rule is specified by an integer
17529 value that encodes global and new reference
17530 points (GREF and NREF). GREF and NREF are
17531 specified by numbers as below:
17532
17533 0---1---2 -- ascent
17534 | |
17535 | |
17536 | |
17537 9--10--11 -- center
17538 | |
17539 ---3---4---5--- baseline
17540 | |
17541 6---7---8 -- descent
17542 */
17543 int rule = COMPOSITION_RULE (cmp, i);
17544 int gref, nref, grefx, grefy, nrefx, nrefy;
17545
17546 COMPOSITION_DECODE_RULE (rule, gref, nref);
17547 grefx = gref % 3, nrefx = nref % 3;
17548 grefy = gref / 3, nrefy = nref / 3;
17549
17550 left = (leftmost
17551 + grefx * (rightmost - leftmost) / 2
17552 - nrefx * width / 2);
17553 btm = ((grefy == 0 ? highest
17554 : grefy == 1 ? 0
17555 : grefy == 2 ? lowest
17556 : (highest + lowest) / 2)
17557 - (nrefy == 0 ? ascent + descent
17558 : nrefy == 1 ? descent - boff
17559 : nrefy == 2 ? 0
17560 : (ascent + descent) / 2));
17561 }
17562
17563 cmp->offsets[i * 2] = left;
17564 cmp->offsets[i * 2 + 1] = btm + descent;
17565
17566 /* Update the bounding box of the overall glyphs. */
17567 right = left + width;
17568 top = btm + descent + ascent;
17569 if (left < leftmost)
17570 leftmost = left;
17571 if (right > rightmost)
17572 rightmost = right;
17573 if (top > highest)
17574 highest = top;
17575 if (btm < lowest)
17576 lowest = btm;
17577 }
17578
17579 /* If there are glyphs whose x-offsets are negative,
17580 shift all glyphs to the right and make all x-offsets
17581 non-negative. */
17582 if (leftmost < 0)
17583 {
17584 for (i = 0; i < cmp->glyph_len; i++)
17585 cmp->offsets[i * 2] -= leftmost;
17586 rightmost -= leftmost;
17587 }
17588
17589 cmp->pixel_width = rightmost;
17590 cmp->ascent = highest;
17591 cmp->descent = - lowest;
17592 if (cmp->ascent < font_ascent)
17593 cmp->ascent = font_ascent;
17594 if (cmp->descent < font_descent)
17595 cmp->descent = font_descent;
17596 }
17597
17598 it->pixel_width = cmp->pixel_width;
17599 it->ascent = it->phys_ascent = cmp->ascent;
17600 it->descent = it->phys_descent = cmp->descent;
17601
17602 if (face->box != FACE_NO_BOX)
17603 {
17604 int thick = face->box_line_width;
17605
17606 if (thick > 0)
17607 {
17608 it->ascent += thick;
17609 it->descent += thick;
17610 }
17611 else
17612 thick = - thick;
17613
17614 if (it->start_of_box_run_p)
17615 it->pixel_width += thick;
17616 if (it->end_of_box_run_p)
17617 it->pixel_width += thick;
17618 }
17619
17620 /* If face has an overline, add the height of the overline
17621 (1 pixel) and a 1 pixel margin to the character height. */
17622 if (face->overline_p)
17623 it->ascent += 2;
17624
17625 take_vertical_position_into_account (it);
17626
17627 if (it->glyph_row)
17628 append_composite_glyph (it);
17629 }
17630 else if (it->what == IT_IMAGE)
17631 produce_image_glyph (it);
17632 else if (it->what == IT_STRETCH)
17633 produce_stretch_glyph (it);
17634
17635 /* Accumulate dimensions. Note: can't assume that it->descent > 0
17636 because this isn't true for images with `:ascent 100'. */
17637 xassert (it->ascent >= 0 && it->descent >= 0);
17638 if (it->area == TEXT_AREA)
17639 it->current_x += it->pixel_width;
17640
17641 it->descent += it->extra_line_spacing;
17642
17643 it->max_ascent = max (it->max_ascent, it->ascent);
17644 it->max_descent = max (it->max_descent, it->descent);
17645 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
17646 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
17647}
17648
17649
17650
17651
17652
17653\f
cfe03a41
KS
17654/***********************************************************************
17655 Cursor types
17656 ***********************************************************************/
17657
17658/* Value is the internal representation of the specified cursor type
17659 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
17660 of the bar cursor. */
17661
17662enum text_cursor_kinds
17663get_specified_cursor_type (arg, width)
17664 Lisp_Object arg;
17665 int *width;
17666{
17667 enum text_cursor_kinds type;
7d0393cf 17668
cfe03a41
KS
17669 if (NILP (arg))
17670 return NO_CURSOR;
17671
17672 if (EQ (arg, Qbox))
17673 return FILLED_BOX_CURSOR;
17674
17675 if (EQ (arg, Qhollow))
17676 return HOLLOW_BOX_CURSOR;
17677
17678 if (EQ (arg, Qbar))
17679 {
17680 *width = 2;
17681 return BAR_CURSOR;
17682 }
17683
17684 if (CONSP (arg)
17685 && EQ (XCAR (arg), Qbar)
17686 && INTEGERP (XCDR (arg))
17687 && XINT (XCDR (arg)) >= 0)
17688 {
17689 *width = XINT (XCDR (arg));
17690 return BAR_CURSOR;
17691 }
17692
17693 if (EQ (arg, Qhbar))
17694 {
17695 *width = 2;
17696 return HBAR_CURSOR;
17697 }
17698
17699 if (CONSP (arg)
17700 && EQ (XCAR (arg), Qhbar)
17701 && INTEGERP (XCDR (arg))
17702 && XINT (XCDR (arg)) >= 0)
17703 {
17704 *width = XINT (XCDR (arg));
17705 return HBAR_CURSOR;
17706 }
17707
17708 /* Treat anything unknown as "hollow box cursor".
17709 It was bad to signal an error; people have trouble fixing
17710 .Xdefaults with Emacs, when it has something bad in it. */
17711 type = HOLLOW_BOX_CURSOR;
17712
17713 return type;
17714}
17715
17716/* Set the default cursor types for specified frame. */
17717void
17718set_frame_cursor_types (f, arg)
17719 struct frame *f;
17720 Lisp_Object arg;
17721{
17722 int width;
17723 Lisp_Object tem;
17724
17725 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
17726 FRAME_CURSOR_WIDTH (f) = width;
17727
17728 /* By default, set up the blink-off state depending on the on-state. */
17729
17730 tem = Fassoc (arg, Vblink_cursor_alist);
17731 if (!NILP (tem))
17732 {
17733 FRAME_BLINK_OFF_CURSOR (f)
17734 = get_specified_cursor_type (XCDR (tem), &width);
17735 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
17736 }
17737 else
17738 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
17739}
17740
17741
e9c99027
KS
17742/* Return the cursor we want to be displayed in window W. Return
17743 width of bar/hbar cursor through WIDTH arg. Return with
17744 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
17745 (i.e. if the `system caret' should track this cursor).
17746
17747 In a mini-buffer window, we want the cursor only to appear if we
17748 are reading input from this window. For the selected window, we
17749 want the cursor type given by the frame parameter or buffer local
17750 setting of cursor-type. If explicitly marked off, draw no cursor.
17751 In all other cases, we want a hollow box cursor. */
cfe03a41
KS
17752
17753enum text_cursor_kinds
e9c99027 17754get_window_cursor_type (w, width, active_cursor)
cfe03a41
KS
17755 struct window *w;
17756 int *width;
e9c99027 17757 int *active_cursor;
cfe03a41
KS
17758{
17759 struct frame *f = XFRAME (w->frame);
17760 struct buffer *b = XBUFFER (w->buffer);
17761 int cursor_type = DEFAULT_CURSOR;
17762 Lisp_Object alt_cursor;
17763 int non_selected = 0;
17764
e9c99027
KS
17765 *active_cursor = 1;
17766
cfe03a41
KS
17767 /* Echo area */
17768 if (cursor_in_echo_area
17769 && FRAME_HAS_MINIBUF_P (f)
17770 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
17771 {
17772 if (w == XWINDOW (echo_area_window))
17773 {
17774 *width = FRAME_CURSOR_WIDTH (f);
17775 return FRAME_DESIRED_CURSOR (f);
17776 }
17777
e9c99027 17778 *active_cursor = 0;
cfe03a41
KS
17779 non_selected = 1;
17780 }
17781
17782 /* Nonselected window or nonselected frame. */
97acc803
EZ
17783 else if (w != XWINDOW (f->selected_window)
17784#ifdef HAVE_WINDOW_SYSTEM
17785 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
17786#endif
17787 )
cfe03a41 17788 {
e9c99027
KS
17789 *active_cursor = 0;
17790
cfe03a41
KS
17791 if (MINI_WINDOW_P (w) && minibuf_level == 0)
17792 return NO_CURSOR;
17793
17794 non_selected = 1;
17795 }
17796
17797 /* Never display a cursor in a window in which cursor-type is nil. */
17798 if (NILP (b->cursor_type))
17799 return NO_CURSOR;
17800
17801 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
17802 if (non_selected)
17803 {
17804 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
17805 return get_specified_cursor_type (alt_cursor, width);
17806 }
17807
17808 /* Get the normal cursor type for this window. */
17809 if (EQ (b->cursor_type, Qt))
17810 {
17811 cursor_type = FRAME_DESIRED_CURSOR (f);
17812 *width = FRAME_CURSOR_WIDTH (f);
17813 }
17814 else
17815 cursor_type = get_specified_cursor_type (b->cursor_type, width);
17816
17817 /* Use normal cursor if not blinked off. */
17818 if (!w->cursor_off_p)
17819 return cursor_type;
17820
17821 /* Cursor is blinked off, so determine how to "toggle" it. */
17822
8420a5dd
KS
17823 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
17824 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
cfe03a41
KS
17825 return get_specified_cursor_type (XCDR (alt_cursor), width);
17826
17827 /* Then see if frame has specified a specific blink off cursor type. */
17828 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
17829 {
17830 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
17831 return FRAME_BLINK_OFF_CURSOR (f);
17832 }
7d0393cf
JB
17833
17834 /* Finally perform built-in cursor blinking:
cfe03a41
KS
17835 filled box <-> hollow box
17836 wide [h]bar <-> narrow [h]bar
17837 narrow [h]bar <-> no cursor
17838 other type <-> no cursor */
17839
17840 if (cursor_type == FILLED_BOX_CURSOR)
17841 return HOLLOW_BOX_CURSOR;
17842
17843 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
17844 {
17845 *width = 1;
17846 return cursor_type;
17847 }
17848
17849 return NO_CURSOR;
17850}
17851
133c764e
KS
17852
17853/* Notice when the text cursor of window W has been completely
17854 overwritten by a drawing operation that outputs glyphs in AREA
17855 starting at X0 and ending at X1 in the line starting at Y0 and
17856 ending at Y1. X coordinates are area-relative. X1 < 0 means all
17857 the rest of the line after X0 has been written. Y coordinates
17858 are window-relative. */
17859
17860void
17861notice_overwritten_cursor (w, area, x0, x1, y0, y1)
17862 struct window *w;
17863 enum glyph_row_area area;
17864 int x0, y0, x1, y1;
17865{
17866#ifdef HAVE_CARBON
17867 /* ++KFS: Why is there a special version of this for the mac ? */
17868 if (area == TEXT_AREA
17869 && w->phys_cursor_on_p
17870 && y0 <= w->phys_cursor.y
17871 && y1 >= w->phys_cursor.y + w->phys_cursor_height
17872 && x0 <= w->phys_cursor.x
17873 && (x1 < 0 || x1 > w->phys_cursor.x))
17874 w->phys_cursor_on_p = 0;
17875#else
17876 if (area == TEXT_AREA && w->phys_cursor_on_p)
17877 {
17878 int cx0 = w->phys_cursor.x;
17879 int cx1 = cx0 + w->phys_cursor_width;
17880 int cy0 = w->phys_cursor.y;
17881 int cy1 = cy0 + w->phys_cursor_height;
17882
17883 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
17884 {
17885 /* The cursor image will be completely removed from the
17886 screen if the output area intersects the cursor area in
17887 y-direction. When we draw in [y0 y1[, and some part of
17888 the cursor is at y < y0, that part must have been drawn
17889 before. When scrolling, the cursor is erased before
17890 actually scrolling, so we don't come here. When not
17891 scrolling, the rows above the old cursor row must have
17892 changed, and in this case these rows must have written
17893 over the cursor image.
17894
17895 Likewise if part of the cursor is below y1, with the
17896 exception of the cursor being in the first blank row at
17897 the buffer and window end because update_text_area
17898 doesn't draw that row. (Except when it does, but
17899 that's handled in update_text_area.) */
17900
17901 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
17902 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
17903 w->phys_cursor_on_p = 0;
17904 }
17905 }
17906#endif
17907}
17908
cfe03a41 17909\f
5f5c8ee5
GM
17910/***********************************************************************
17911 Initialization
17912 ***********************************************************************/
17913
a2889657
JB
17914void
17915syms_of_xdisp ()
17916{
c6e89d6c
GM
17917 Vwith_echo_area_save_vector = Qnil;
17918 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 17919
c6e89d6c
GM
17920 Vmessage_stack = Qnil;
17921 staticpro (&Vmessage_stack);
2311178e 17922
735c094c 17923 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 17924 staticpro (&Qinhibit_redisplay);
735c094c 17925
b14bc55e
RS
17926 message_dolog_marker1 = Fmake_marker ();
17927 staticpro (&message_dolog_marker1);
17928 message_dolog_marker2 = Fmake_marker ();
17929 staticpro (&message_dolog_marker2);
17930 message_dolog_marker3 = Fmake_marker ();
17931 staticpro (&message_dolog_marker3);
17932
5f5c8ee5 17933#if GLYPH_DEBUG
7d4cc828 17934 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
17935 defsubr (&Sdump_glyph_matrix);
17936 defsubr (&Sdump_glyph_row);
e037b9ec 17937 defsubr (&Sdump_tool_bar_row);
62397849 17938 defsubr (&Strace_redisplay);
bf9249e3 17939 defsubr (&Strace_to_stderr);
5f5c8ee5 17940#endif
99a5de87 17941#ifdef HAVE_WINDOW_SYSTEM
57c28064 17942 defsubr (&Stool_bar_lines_needed);
99a5de87 17943#endif
8143e6ab 17944 defsubr (&Sformat_mode_line);
5f5c8ee5 17945
cf074754
RS
17946 staticpro (&Qmenu_bar_update_hook);
17947 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
17948
d46fb96a 17949 staticpro (&Qoverriding_terminal_local_map);
7079aefa 17950 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 17951
399164b4
KH
17952 staticpro (&Qoverriding_local_map);
17953 Qoverriding_local_map = intern ("overriding-local-map");
17954
75c43375
RS
17955 staticpro (&Qwindow_scroll_functions);
17956 Qwindow_scroll_functions = intern ("window-scroll-functions");
17957
e0bfbde6
RS
17958 staticpro (&Qredisplay_end_trigger_functions);
17959 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 17960
2e54982e
RS
17961 staticpro (&Qinhibit_point_motion_hooks);
17962 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
17963
9499d71b
GM
17964 QCdata = intern (":data");
17965 staticpro (&QCdata);
5f5c8ee5 17966 Qdisplay = intern ("display");
f3751a65 17967 staticpro (&Qdisplay);
5f5c8ee5
GM
17968 Qspace_width = intern ("space-width");
17969 staticpro (&Qspace_width);
5f5c8ee5
GM
17970 Qraise = intern ("raise");
17971 staticpro (&Qraise);
17972 Qspace = intern ("space");
17973 staticpro (&Qspace);
f3751a65
GM
17974 Qmargin = intern ("margin");
17975 staticpro (&Qmargin);
5f5c8ee5 17976 Qleft_margin = intern ("left-margin");
f3751a65 17977 staticpro (&Qleft_margin);
5f5c8ee5 17978 Qright_margin = intern ("right-margin");
f3751a65 17979 staticpro (&Qright_margin);
5f5c8ee5
GM
17980 Qalign_to = intern ("align-to");
17981 staticpro (&Qalign_to);
17982 QCalign_to = intern (":align-to");
17983 staticpro (&QCalign_to);
5f5c8ee5
GM
17984 Qrelative_width = intern ("relative-width");
17985 staticpro (&Qrelative_width);
17986 QCrelative_width = intern (":relative-width");
17987 staticpro (&QCrelative_width);
17988 QCrelative_height = intern (":relative-height");
17989 staticpro (&QCrelative_height);
17990 QCeval = intern (":eval");
17991 staticpro (&QCeval);
0fcf414f
RS
17992 QCpropertize = intern (":propertize");
17993 staticpro (&QCpropertize);
886bd6f2
GM
17994 QCfile = intern (":file");
17995 staticpro (&QCfile);
5f5c8ee5
GM
17996 Qfontified = intern ("fontified");
17997 staticpro (&Qfontified);
17998 Qfontification_functions = intern ("fontification-functions");
17999 staticpro (&Qfontification_functions);
5f5c8ee5
GM
18000 Qtrailing_whitespace = intern ("trailing-whitespace");
18001 staticpro (&Qtrailing_whitespace);
18002 Qimage = intern ("image");
18003 staticpro (&Qimage);
ad4f174e
GM
18004 Qmessage_truncate_lines = intern ("message-truncate-lines");
18005 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
18006 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
18007 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
18008 Qgrow_only = intern ("grow-only");
18009 staticpro (&Qgrow_only);
e1477f43
GM
18010 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
18011 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
18012 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
18013 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
18014 Qposition = intern ("position");
18015 staticpro (&Qposition);
18016 Qbuffer_position = intern ("buffer-position");
18017 staticpro (&Qbuffer_position);
18018 Qobject = intern ("object");
18019 staticpro (&Qobject);
cfe03a41
KS
18020 Qbar = intern ("bar");
18021 staticpro (&Qbar);
18022 Qhbar = intern ("hbar");
18023 staticpro (&Qhbar);
18024 Qbox = intern ("box");
18025 staticpro (&Qbox);
18026 Qhollow = intern ("hollow");
18027 staticpro (&Qhollow);
c53a1624
RS
18028 Qrisky_local_variable = intern ("risky-local-variable");
18029 staticpro (&Qrisky_local_variable);
26683087
RS
18030 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
18031 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 18032
7033d6df
RS
18033 list_of_error = Fcons (intern ("error"), Qnil);
18034 staticpro (&list_of_error);
18035
a2889657
JB
18036 last_arrow_position = Qnil;
18037 last_arrow_string = Qnil;
f3751a65
GM
18038 staticpro (&last_arrow_position);
18039 staticpro (&last_arrow_string);
2311178e 18040
c6e89d6c
GM
18041 echo_buffer[0] = echo_buffer[1] = Qnil;
18042 staticpro (&echo_buffer[0]);
18043 staticpro (&echo_buffer[1]);
18044
18045 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
18046 staticpro (&echo_area_buffer[0]);
18047 staticpro (&echo_area_buffer[1]);
a2889657 18048
6a94510a
GM
18049 Vmessages_buffer_name = build_string ("*Messages*");
18050 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
18051
18052 mode_line_proptrans_alist = Qnil;
18053 staticpro (&mode_line_proptrans_alist);
2311178e 18054
fec8f23e
KS
18055 mode_line_string_list = Qnil;
18056 staticpro (&mode_line_string_list);
18057
7ee72033
MB
18058 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
18059 doc: /* Non-nil means highlight trailing whitespace.
228299fa 18060The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
18061 Vshow_trailing_whitespace = Qnil;
18062
7ee72033
MB
18063 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
18064 doc: /* Non-nil means don't actually do any redisplay.
228299fa 18065This is used for internal purposes. */);
735c094c
KH
18066 Vinhibit_redisplay = Qnil;
18067
7ee72033
MB
18068 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
18069 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
18070 Vglobal_mode_string = Qnil;
18071
7ee72033
MB
18072 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
18073 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
18074This must be the beginning of a line in order to work.
18075See also `overlay-arrow-string'. */);
a2889657
JB
18076 Voverlay_arrow_position = Qnil;
18077
7ee72033
MB
18078 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
18079 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
a2889657
JB
18080 Voverlay_arrow_string = Qnil;
18081
7ee72033
MB
18082 DEFVAR_INT ("scroll-step", &scroll_step,
18083 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
18084If that fails to bring point back on frame, point is centered instead.
18085If this is zero, point is always centered after it moves off frame.
18086If you want scrolling to always be a line at a time, you should set
18087`scroll-conservatively' to a large value rather than set this to 1. */);
18088
7ee72033
MB
18089 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
18090 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
18091A value of zero means to scroll the text to center point vertically
18092in the window. */);
0789adb2
RS
18093 scroll_conservatively = 0;
18094
7ee72033
MB
18095 DEFVAR_INT ("scroll-margin", &scroll_margin,
18096 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
18097Recenter the window whenever point gets within this many lines
18098of the top or bottom of the window. */);
9afd2168
RS
18099 scroll_margin = 0;
18100
5f5c8ee5 18101#if GLYPH_DEBUG
7ee72033 18102 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 18103#endif
a2889657
JB
18104
18105 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
18106 &truncate_partial_width_windows,
18107 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
18108 truncate_partial_width_windows = 1;
18109
7ee72033
MB
18110 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
18111 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 18112Any other value means to use the appropriate face, `mode-line',
ccfe8f57 18113`header-line', or `menu' respectively. */);
1862a24e 18114 mode_line_inverse_video = 1;
aa6d10fa 18115
7ee72033
MB
18116 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
18117 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
18118If the buffer is bigger than this, the line number does not appear
18119in the mode line. A value of nil means no limit. */);
090703f4 18120 Vline_number_display_limit = Qnil;
fba9ce76 18121
090703f4 18122 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
18123 &line_number_display_limit_width,
18124 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
18125If the average length of the lines near point is bigger than this, then the
18126line number may be omitted from the mode line. */);
5d121aec
KH
18127 line_number_display_limit_width = 200;
18128
7ee72033
MB
18129 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
18130 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 18131 highlight_nonselected_windows = 0;
d39b6696 18132
7ee72033
MB
18133 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
18134 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
18135Minibuffer-only frames don't count, but iconified frames do.
18136This variable is not guaranteed to be accurate except while processing
18137`frame-title-format' and `icon-title-format'. */);
18138
7ee72033
MB
18139 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
18140 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
18141\(Assuming the window manager supports this feature.)
18142This variable has the same structure as `mode-line-format' (which see),
18143and is used only on frames for which no explicit name has been set
18144\(see `modify-frame-parameters'). */);
7ee72033
MB
18145 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
18146 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
18147\(Assuming the window manager supports this feature.)
18148This variable has the same structure as `mode-line-format' (which see),
18149and is used only on frames for which no explicit name has been set
18150\(see `modify-frame-parameters'). */);
d39b6696
KH
18151 Vicon_title_format
18152 = Vframe_title_format
18153 = Fcons (intern ("multiple-frames"),
18154 Fcons (build_string ("%b"),
3ebf0ea9 18155 Fcons (Fcons (empty_string,
d39b6696
KH
18156 Fcons (intern ("invocation-name"),
18157 Fcons (build_string ("@"),
18158 Fcons (intern ("system-name"),
18159 Qnil)))),
18160 Qnil)));
5992c4f7 18161
7ee72033
MB
18162 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
18163 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
18164If nil, disable message logging. If t, log messages but don't truncate
18165the buffer when it becomes large. */);
ac90c44f 18166 Vmessage_log_max = make_number (50);
08b610e4 18167
7ee72033
MB
18168 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
18169 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
18170The value should be a list of functions that take one argument.
18171Just before redisplay, for each frame, if any of its windows have changed
18172size since the last redisplay, or have been split or deleted,
18173all the functions in the list are called, with the frame as argument. */);
08b610e4 18174 Vwindow_size_change_functions = Qnil;
75c43375 18175
7ee72033
MB
18176 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
18177 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
18178Each function is called with two arguments, the window
18179and its new display-start position. Note that the value of `window-end'
18180is not valid when these functions are called. */);
75c43375 18181 Vwindow_scroll_functions = Qnil;
2311178e 18182
7ee72033
MB
18183 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
18184 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
18185This increases a tool-bar's height if not all tool-bar items are visible.
18186It decreases a tool-bar's height when it would display blank lines
18187otherwise. */);
e037b9ec 18188 auto_resize_tool_bars_p = 1;
2311178e 18189
7ee72033
MB
18190 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
18191 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 18192 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 18193
7ee72033
MB
18194 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
18195 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 18196If an integer, use that for both horizontal and vertical margins.
f6c89f27 18197Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
18198HORZ specifying the horizontal margin, and VERT specifying the
18199vertical margin. */);
c3d76173 18200 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 18201
7ee72033 18202 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 18203 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 18204 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 18205
7ee72033
MB
18206 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
18207 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
18208Each function is called with one argument POS. Functions must
18209fontify a region starting at POS in the current buffer, and give
18210fontified regions the property `fontified'. */);
5f5c8ee5 18211 Vfontification_functions = Qnil;
6b9f0906 18212 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
18213
18214 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
18215 &unibyte_display_via_language_environment,
18216 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
18217Specifically this means that unibyte non-ASCII characters
18218are displayed by converting them to the equivalent multibyte characters
18219according to the current language environment. As a result, they are
18220displayed according to the current fontset. */);
7bbe686f 18221 unibyte_display_via_language_environment = 0;
c6e89d6c 18222
7ee72033
MB
18223 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
18224 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
18225If a float, it specifies a fraction of the mini-window frame's height.
18226If an integer, it specifies a number of lines. */);
c6e89d6c 18227 Vmax_mini_window_height = make_float (0.25);
6422c1d7 18228
7ee72033
MB
18229 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
18230 doc: /* *How to resize mini-windows.
228299fa
GM
18231A value of nil means don't automatically resize mini-windows.
18232A value of t means resize them to fit the text displayed in them.
18233A value of `grow-only', the default, means let mini-windows grow
18234only, until their display becomes empty, at which point the windows
18235go back to their normal size. */);
6422c1d7
GM
18236 Vresize_mini_windows = Qgrow_only;
18237
cfe03a41
KS
18238 DEFVAR_LISP ("cursor-in-non-selected-windows",
18239 &Vcursor_in_non_selected_windows,
18240 doc: /* *Cursor type to display in non-selected windows.
18241t means to use hollow box cursor. See `cursor-type' for other values. */);
18242 Vcursor_in_non_selected_windows = Qt;
18243
cfe03a41
KS
18244 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
18245 doc: /* Alist specifying how to blink the cursor off.
18246Each element has the form (ON-STATE . OFF-STATE). Whenever the
18247`cursor-type' frame-parameter or variable equals ON-STATE,
18248comparing using `equal', Emacs uses OFF-STATE to specify
18249how to blink it off. */);
18250 Vblink_cursor_alist = Qnil;
2311178e 18251
e76d28d5 18252 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 18253 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 18254 automatic_hscrolling_p = 1;
1df7e8f0 18255
e76d28d5 18256 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
18257 doc: /* *How many columns away from the window edge point is allowed to get
18258before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 18259 hscroll_margin = 5;
1df7e8f0 18260
e76d28d5 18261 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
18262 doc: /* *How many columns to scroll the window when point gets too close to the edge.
18263When point is less than `automatic-hscroll-margin' columns from the window
18264edge, automatic hscrolling will scroll the window by the amount of columns
18265determined by this variable. If its value is a positive integer, scroll that
18266many columns. If it's a positive floating-point number, it specifies the
18267fraction of the window's width to scroll. If it's nil or zero, point will be
18268centered horizontally after the scroll. Any other value, including negative
18269numbers, are treated as if the value were zero.
18270
18271Automatic hscrolling always moves point outside the scroll margin, so if
18272point was more than scroll step columns inside the margin, the window will
18273scroll more than the value given by the scroll step.
18274
18275Note that the lower bound for automatic hscrolling specified by `scroll-left'
18276and `scroll-right' overrides this variable's effect. */);
e76d28d5 18277 Vhscroll_step = make_number (0);
2311178e 18278
7ee72033
MB
18279 DEFVAR_LISP ("image-types", &Vimage_types,
18280 doc: /* List of supported image types.
228299fa 18281Each element of the list is a symbol for a supported image type. */);
e00daaa0 18282 Vimage_types = Qnil;
2311178e 18283
7ee72033
MB
18284 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
18285 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 18286Bind this around calls to `message' to let it take effect. */);
ad4f174e 18287 message_truncate_lines = 0;
0bca8940 18288
7ee72033
MB
18289 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
18290 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 18291Can be used to update submenus whose contents should vary. */);
6422c1d7 18292 Vmenu_bar_update_hook = Qnil;
2311178e 18293
7ee72033
MB
18294 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
18295 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 18296 inhibit_menubar_update = 0;
30a3f61c 18297
7ee72033
MB
18298 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
18299 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 18300 inhibit_eval_during_redisplay = 0;
76cb5e06 18301
26683087
RS
18302 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
18303 doc: /* Non-nil means don't free realized faces. Internal use only. */);
18304 inhibit_free_realized_faces = 0;
18305
69d1f7c9 18306#if GLYPH_DEBUG
76cb5e06
GM
18307 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
18308 doc: /* Inhibit try_window_id display optimization. */);
18309 inhibit_try_window_id = 0;
18310
18311 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
18312 doc: /* Inhibit try_window_reusing display optimization. */);
18313 inhibit_try_window_reusing = 0;
18314
18315 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
18316 doc: /* Inhibit try_cursor_movement display optimization. */);
18317 inhibit_try_cursor_movement = 0;
18318#endif /* GLYPH_DEBUG */
a2889657
JB
18319}
18320
5f5c8ee5
GM
18321
18322/* Initialize this module when Emacs starts. */
18323
dfcf069d 18324void
a2889657
JB
18325init_xdisp ()
18326{
18327 Lisp_Object root_window;
5f5c8ee5 18328 struct window *mini_w;
a2889657 18329
04612a64
GM
18330 current_header_line_height = current_mode_line_height = -1;
18331
5f5c8ee5 18332 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
18333
18334 mini_w = XWINDOW (minibuf_window);
11e82b76 18335 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 18336
a2889657
JB
18337 if (!noninteractive)
18338 {
5f5c8ee5
GM
18339 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
18340 int i;
18341
ac90c44f 18342 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
12c226c5 18343 set_window_height (root_window,
5f5c8ee5 18344 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 18345 0);
ac90c44f 18346 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
a2889657
JB
18347 set_window_height (minibuf_window, 1, 0);
18348
ac90c44f
GM
18349 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
18350 mini_w->width = make_number (FRAME_WIDTH (f));
5f5c8ee5
GM
18351
18352 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
18353 scratch_glyph_row.glyphs[TEXT_AREA + 1]
18354 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
18355
2311178e 18356 /* The default ellipsis glyphs `...'. */
5f5c8ee5 18357 for (i = 0; i < 3; ++i)
ac90c44f 18358 default_invis_vector[i] = make_number ('.');
a2889657 18359 }
5f5c8ee5 18360
5f5c8ee5 18361 {
93da8435
SM
18362 /* Allocate the buffer for frame titles.
18363 Also used for `format-mode-line'. */
5f5c8ee5
GM
18364 int size = 100;
18365 frame_title_buf = (char *) xmalloc (size);
18366 frame_title_buf_end = frame_title_buf + size;
18367 frame_title_ptr = NULL;
18368 }
2311178e 18369
21fdfb65 18370 help_echo_showing_p = 0;
a2889657 18371}
5f5c8ee5
GM
18372
18373