Patch for building Emacs on Mac OS X. April 26, 2002. See ChangeLog,
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5d335845 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
5f5c8ee5 3 Free Software Foundation, Inc.
a2889657
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
b1d1124b 9the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
a2889657 21
5f5c8ee5
GM
22/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
48400103 36 let's say almost---see the description of direct update
4b41cebb 37 operations, below.)
5f5c8ee5
GM
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
82a7ab23 55 +--------------+ redisplay +----------------+
5f5c8ee5
GM
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
48400103 91 You will find a lot of redisplay optimizations when you start
5f5c8ee5
GM
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
48400103 126 Iteration over things to be displayed is then simple. It is
1178ddde
GM
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
5f5c8ee5
GM
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
a2889657 168
18160b98 169#include <config.h>
a2889657 170#include <stdio.h>
7ee72033 171
a2889657 172#include "lisp.h"
546a4f00 173#include "keyboard.h"
44fa5b1e 174#include "frame.h"
a2889657
JB
175#include "window.h"
176#include "termchar.h"
177#include "dispextern.h"
178#include "buffer.h"
1c9241f5 179#include "charset.h"
a2889657
JB
180#include "indent.h"
181#include "commands.h"
182#include "macros.h"
183#include "disptab.h"
30c566e4 184#include "termhooks.h"
b0a0fbda 185#include "intervals.h"
1c9241f5
KH
186#include "coding.h"
187#include "process.h"
dfcf069d 188#include "region-cache.h"
0ef75e87 189#include "fontset.h"
dfcf069d 190
6d55d620 191#ifdef HAVE_X_WINDOWS
dfcf069d
AS
192#include "xterm.h"
193#endif
a75dfea0
AI
194#ifdef WINDOWSNT
195#include "w32term.h"
196#endif
e0f712ba 197#ifdef MAC_OS
1a578e9b
AC
198#include "macterm.h"
199#endif
a2889657 200
5f5c8ee5
GM
201#define INFINITY 10000000
202
e0f712ba 203#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
2e621225 204extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 205extern int pending_menu_activation;
76412d64
RS
206#endif
207
a2889657
JB
208extern int interrupt_input;
209extern int command_loop_level;
210
b6436d4e
RS
211extern int minibuffer_auto_raise;
212
c4628384
RS
213extern Lisp_Object Qface;
214
399164b4
KH
215extern Lisp_Object Voverriding_local_map;
216extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 217extern Lisp_Object Qmenu_item;
399164b4 218
d46fb96a 219Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 220Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 221Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 222Lisp_Object Qinhibit_point_motion_hooks;
0fcf414f 223Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
5f5c8ee5 224Lisp_Object Qfontified;
6422c1d7 225Lisp_Object Qgrow_only;
869fb12c 226Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 227Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5 228
c53a1624
RS
229Lisp_Object Qrisky_local_variable;
230
7033d6df
RS
231/* Holds the list (error). */
232Lisp_Object list_of_error;
233
5f5c8ee5
GM
234/* Functions called to fontify regions of text. */
235
236Lisp_Object Vfontification_functions;
237Lisp_Object Qfontification_functions;
238
e037b9ec 239/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
240 over them. */
241
e037b9ec 242int auto_raise_tool_bar_buttons_p;
5f5c8ee5 243
e037b9ec 244/* Margin around tool bar buttons in pixels. */
5f5c8ee5 245
35a41507 246Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 247
e037b9ec 248/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 249
31ade731 250EMACS_INT tool_bar_button_relief;
5f5c8ee5 251
e037b9ec 252/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
253 items are visible, and no blank lines remain. */
254
e037b9ec 255int auto_resize_tool_bars_p;
399164b4 256
735c094c
KH
257/* Non-nil means don't actually do any redisplay. */
258
259Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
260
30a3f61c
GM
261/* Non-zero means Lisp evaluation during redisplay is inhibited. */
262
869fb12c 263int inhibit_eval_during_redisplay;
30a3f61c 264
5f5c8ee5
GM
265/* Names of text properties relevant for redisplay. */
266
a7e27ef7 267Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
4b41cebb 268extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
269
270/* Symbols used in text property values. */
271
272Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 273Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 274Lisp_Object Qmargin;
a7e27ef7 275extern Lisp_Object Qheight;
5f5c8ee5 276
8f897821 277/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 278
8f897821 279Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
280
281/* Name of the face used to highlight trailing whitespace. */
282
283Lisp_Object Qtrailing_whitespace;
284
285/* The symbol `image' which is the car of the lists used to represent
286 images in Lisp. */
287
288Lisp_Object Qimage;
289
290/* Non-zero means print newline to stdout before next mini-buffer
291 message. */
a2889657
JB
292
293int noninteractive_need_newline;
294
5f5c8ee5 295/* Non-zero means print newline to message log before next message. */
f88eb0b6 296
3c6595e0 297static int message_log_need_newline;
f88eb0b6 298
b14bc55e
RS
299/* Three markers that message_dolog uses.
300 It could allocate them itself, but that causes trouble
301 in handling memory-full errors. */
302static Lisp_Object message_dolog_marker1;
303static Lisp_Object message_dolog_marker2;
304static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
305\f
306/* The buffer position of the first character appearing entirely or
307 partially on the line of the selected window which contains the
308 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
309 redisplay optimization in redisplay_internal. */
a2889657 310
5f5c8ee5 311static struct text_pos this_line_start_pos;
a2889657 312
5f5c8ee5
GM
313/* Number of characters past the end of the line above, including the
314 terminating newline. */
315
316static struct text_pos this_line_end_pos;
317
318/* The vertical positions and the height of this line. */
a2889657 319
a2889657 320static int this_line_vpos;
5f5c8ee5
GM
321static int this_line_y;
322static int this_line_pixel_height;
323
324/* X position at which this display line starts. Usually zero;
325 negative if first character is partially visible. */
326
327static int this_line_start_x;
a2889657 328
5f5c8ee5 329/* Buffer that this_line_.* variables are referring to. */
a2889657 330
a2889657
JB
331static struct buffer *this_line_buffer;
332
5f5c8ee5
GM
333/* Nonzero means truncate lines in all windows less wide than the
334 frame. */
a2889657 335
a2889657
JB
336int truncate_partial_width_windows;
337
7bbe686f 338/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 339
7bbe686f 340int unibyte_display_via_language_environment;
5f5c8ee5
GM
341
342/* Nonzero means we have more than one non-mini-buffer-only frame.
343 Not guaranteed to be accurate except while parsing
344 frame-title-format. */
7bbe686f 345
d39b6696
KH
346int multiple_frames;
347
a2889657
JB
348Lisp_Object Vglobal_mode_string;
349
350/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 351
a2889657
JB
352Lisp_Object Voverlay_arrow_position;
353
5f5c8ee5
GM
354/* String to display for the arrow. Only used on terminal frames. */
355
a2889657
JB
356Lisp_Object Voverlay_arrow_string;
357
5f5c8ee5
GM
358/* Values of those variables at last redisplay. However, if
359 Voverlay_arrow_position is a marker, last_arrow_position is its
360 numerical position. */
361
d45de95b
RS
362static Lisp_Object last_arrow_position, last_arrow_string;
363
5f5c8ee5
GM
364/* Like mode-line-format, but for the title bar on a visible frame. */
365
d39b6696
KH
366Lisp_Object Vframe_title_format;
367
5f5c8ee5
GM
368/* Like mode-line-format, but for the title bar on an iconified frame. */
369
d39b6696
KH
370Lisp_Object Vicon_title_format;
371
08b610e4
RS
372/* List of functions to call when a window's size changes. These
373 functions get one arg, a frame on which one or more windows' sizes
374 have changed. */
5f5c8ee5 375
08b610e4
RS
376static Lisp_Object Vwindow_size_change_functions;
377
0bca8940 378Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 379
a2889657 380/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 381
5f5c8ee5 382static int overlay_arrow_seen;
ca26e1c8 383
fba9ce76 384/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 385
5f5c8ee5
GM
386int highlight_nonselected_windows;
387
388/* If cursor motion alone moves point off frame, try scrolling this
389 many lines up or down if that will bring it back. */
390
31ade731 391static EMACS_INT scroll_step;
a2889657 392
4b41cebb 393/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
394 screen, when appropriate. */
395
31ade731 396static EMACS_INT scroll_conservatively;
0789adb2 397
5f5c8ee5
GM
398/* Recenter the window whenever point gets within this many lines of
399 the top or bottom of the window. This value is translated into a
400 pixel value by multiplying it with CANON_Y_UNIT, which means that
401 there is really a fixed pixel height scroll margin. */
402
31ade731 403EMACS_INT scroll_margin;
9afd2168 404
5f5c8ee5
GM
405/* Number of windows showing the buffer of the selected window (or
406 another buffer with the same base buffer). keyboard.c refers to
407 this. */
a2889657 408
a2889657
JB
409int buffer_shared;
410
5f5c8ee5 411/* Vector containing glyphs for an ellipsis `...'. */
a2889657 412
5f5c8ee5 413static Lisp_Object default_invis_vector[3];
a2889657 414
1862a24e
MB
415/* Zero means display the mode-line/header-line/menu-bar in the default face
416 (this slightly odd definition is for compatibility with previous versions
417 of emacs), non-zero means display them using their respective faces.
418
419 This variable is deprecated. */
a2889657 420
a2889657
JB
421int mode_line_inverse_video;
422
5f5c8ee5
GM
423/* Prompt to display in front of the mini-buffer contents. */
424
8c5b6a0a 425Lisp_Object minibuf_prompt;
a2889657 426
5f5c8ee5
GM
427/* Width of current mini-buffer prompt. Only set after display_line
428 of the line that contains the prompt. */
429
a2889657 430int minibuf_prompt_width;
5f5c8ee5 431
5f5c8ee5
GM
432/* This is the window where the echo area message was displayed. It
433 is always a mini-buffer window, but it may not be the same window
434 currently active as a mini-buffer. */
435
73af359d
RS
436Lisp_Object echo_area_window;
437
c6e89d6c
GM
438/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
439 pushes the current message and the value of
440 message_enable_multibyte on the stack, the function restore_message
441 pops the stack and displays MESSAGE again. */
442
443Lisp_Object Vmessage_stack;
444
a3788d53
RS
445/* Nonzero means multibyte characters were enabled when the echo area
446 message was specified. */
5f5c8ee5 447
a3788d53
RS
448int message_enable_multibyte;
449
4b41cebb 450/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 451
a2889657
JB
452int update_mode_lines;
453
5f5c8ee5 454/* Nonzero if window sizes or contents have changed since last
4b41cebb 455 redisplay that finished. */
5f5c8ee5 456
a2889657
JB
457int windows_or_buffers_changed;
458
5fb96e96
RS
459/* Nonzero means a frame's cursor type has been changed. */
460
461int cursor_type_changed;
462
5f5c8ee5
GM
463/* Nonzero after display_mode_line if %l was used and it displayed a
464 line number. */
465
aa6d10fa
RS
466int line_number_displayed;
467
468/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 469
090703f4 470Lisp_Object Vline_number_display_limit;
5992c4f7 471
4b41cebb 472/* Line width to consider when repositioning for line number display. */
5d121aec 473
31ade731 474static EMACS_INT line_number_display_limit_width;
5d121aec 475
5f5c8ee5
GM
476/* Number of lines to keep in the message log buffer. t means
477 infinite. nil means don't log at all. */
478
5992c4f7 479Lisp_Object Vmessage_log_max;
d45de95b 480
6a94510a
GM
481/* The name of the *Messages* buffer, a string. */
482
483static Lisp_Object Vmessages_buffer_name;
484
c6e89d6c
GM
485/* Current, index 0, and last displayed echo area message. Either
486 buffers from echo_buffers, or nil to indicate no message. */
487
488Lisp_Object echo_area_buffer[2];
489
490/* The buffers referenced from echo_area_buffer. */
491
492static Lisp_Object echo_buffer[2];
493
494/* A vector saved used in with_area_buffer to reduce consing. */
495
496static Lisp_Object Vwith_echo_area_save_vector;
497
498/* Non-zero means display_echo_area should display the last echo area
499 message again. Set by redisplay_preserve_echo_area. */
500
501static int display_last_displayed_message_p;
502
503/* Nonzero if echo area is being used by print; zero if being used by
504 message. */
505
506int message_buf_print;
507
e1477f43
GM
508/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
509
510Lisp_Object Qinhibit_menubar_update;
511int inhibit_menubar_update;
512
9142dd5b
GM
513/* Maximum height for resizing mini-windows. Either a float
514 specifying a fraction of the available height, or an integer
515 specifying a number of lines. */
c6e89d6c 516
ad4f174e
GM
517Lisp_Object Vmax_mini_window_height;
518
519/* Non-zero means messages should be displayed with truncated
520 lines instead of being continued. */
521
522int message_truncate_lines;
523Lisp_Object Qmessage_truncate_lines;
c6e89d6c 524
6e019995
GM
525/* Set to 1 in clear_message to make redisplay_internal aware
526 of an emptied echo area. */
527
528static int message_cleared_p;
529
d6d26ed3
GM
530/* Non-zero means we want a hollow cursor in windows that are not
531 selected. Zero means there's no cursor in such windows. */
532
533int cursor_in_non_selected_windows;
af79bccb 534Lisp_Object Qcursor_in_non_selected_windows;
d6d26ed3 535
5f5c8ee5
GM
536/* A scratch glyph row with contents used for generating truncation
537 glyphs. Also used in direct_output_for_insert. */
12adba34 538
5f5c8ee5
GM
539#define MAX_SCRATCH_GLYPHS 100
540struct glyph_row scratch_glyph_row;
541static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 542
5f5c8ee5
GM
543/* Ascent and height of the last line processed by move_it_to. */
544
545static int last_max_ascent, last_height;
546
21fdfb65
GM
547/* Non-zero if there's a help-echo in the echo area. */
548
549int help_echo_showing_p;
550
04612a64
GM
551/* If >= 0, computed, exact values of mode-line and header-line height
552 to use in the macros CURRENT_MODE_LINE_HEIGHT and
553 CURRENT_HEADER_LINE_HEIGHT. */
554
555int current_mode_line_height, current_header_line_height;
556
5f5c8ee5
GM
557/* The maximum distance to look ahead for text properties. Values
558 that are too small let us call compute_char_face and similar
559 functions too often which is expensive. Values that are too large
560 let us call compute_char_face and alike too often because we
561 might not be interested in text properties that far away. */
562
563#define TEXT_PROP_DISTANCE_LIMIT 100
564
47589c8c
GM
565#if GLYPH_DEBUG
566
76cb5e06
GM
567/* Variables to turn off display optimizations from Lisp. */
568
569int inhibit_try_window_id, inhibit_try_window_reusing;
570int inhibit_try_cursor_movement;
571
5f5c8ee5
GM
572/* Non-zero means print traces of redisplay if compiled with
573 GLYPH_DEBUG != 0. */
574
5f5c8ee5 575int trace_redisplay_p;
47589c8c 576
546a4f00 577#endif /* GLYPH_DEBUG */
47589c8c 578
546a4f00
AI
579#ifdef DEBUG_TRACE_MOVE
580/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
581int trace_move;
582
47589c8c
GM
583#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
584#else
91c3f500 585#define TRACE_MOVE(x) (void) 0
5f5c8ee5 586#endif
546a4f00 587
d475bcb8
GM
588/* Non-zero means automatically scroll windows horizontally to make
589 point visible. */
590
591int automatic_hscrolling_p;
592
1df7e8f0
EZ
593/* How close to the margin can point get before the window is scrolled
594 horizontally. */
5d335845 595EMACS_INT hscroll_margin;
1df7e8f0
EZ
596
597/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 598Lisp_Object Vhscroll_step;
1df7e8f0 599
e00daaa0
GM
600/* A list of symbols, one for each supported image type. */
601
602Lisp_Object Vimage_types;
603
6422c1d7 604/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 605 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
606 display. If `grow-only', let mini-windows grow only until they
607 become empty. */
608
609Lisp_Object Vresize_mini_windows;
610
82a7ab23
RS
611/* Buffer being redisplayed -- for redisplay_window_error. */
612
613struct buffer *displayed_buffer;
614
5f5c8ee5
GM
615/* Value returned from text property handlers (see below). */
616
617enum prop_handled
3c6595e0 618{
5f5c8ee5
GM
619 HANDLED_NORMALLY,
620 HANDLED_RECOMPUTE_PROPS,
621 HANDLED_OVERLAY_STRING_CONSUMED,
622 HANDLED_RETURN
623};
3c6595e0 624
5f5c8ee5
GM
625/* A description of text properties that redisplay is interested
626 in. */
3c6595e0 627
5f5c8ee5
GM
628struct props
629{
630 /* The name of the property. */
631 Lisp_Object *name;
90adcf20 632
5f5c8ee5
GM
633 /* A unique index for the property. */
634 enum prop_idx idx;
635
636 /* A handler function called to set up iterator IT from the property
637 at IT's current position. Value is used to steer handle_stop. */
638 enum prop_handled (*handler) P_ ((struct it *it));
639};
640
641static enum prop_handled handle_face_prop P_ ((struct it *));
642static enum prop_handled handle_invisible_prop P_ ((struct it *));
643static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 644static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
645static enum prop_handled handle_overlay_change P_ ((struct it *));
646static enum prop_handled handle_fontified_prop P_ ((struct it *));
647
648/* Properties handled by iterators. */
649
650static struct props it_props[] =
5992c4f7 651{
5f5c8ee5
GM
652 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
653 /* Handle `face' before `display' because some sub-properties of
654 `display' need to know the face. */
655 {&Qface, FACE_PROP_IDX, handle_face_prop},
656 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
657 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 658 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
659 {NULL, 0, NULL}
660};
5992c4f7 661
5f5c8ee5
GM
662/* Value is the position described by X. If X is a marker, value is
663 the marker_position of X. Otherwise, value is X. */
12adba34 664
5f5c8ee5 665#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 666
5f5c8ee5 667/* Enumeration returned by some move_it_.* functions internally. */
12adba34 668
5f5c8ee5
GM
669enum move_it_result
670{
671 /* Not used. Undefined value. */
672 MOVE_UNDEFINED,
bab29e15 673
5f5c8ee5
GM
674 /* Move ended at the requested buffer position or ZV. */
675 MOVE_POS_MATCH_OR_ZV,
bab29e15 676
5f5c8ee5
GM
677 /* Move ended at the requested X pixel position. */
678 MOVE_X_REACHED,
12adba34 679
5f5c8ee5
GM
680 /* Move within a line ended at the end of a line that must be
681 continued. */
682 MOVE_LINE_CONTINUED,
683
684 /* Move within a line ended at the end of a line that would
685 be displayed truncated. */
686 MOVE_LINE_TRUNCATED,
ff6c30e5 687
5f5c8ee5
GM
688 /* Move within a line ended at a line end. */
689 MOVE_NEWLINE_OR_CR
690};
12adba34 691
ff6c30e5 692
5f5c8ee5
GM
693\f
694/* Function prototypes. */
695
5a08cbaf 696static void setup_for_ellipsis P_ ((struct it *));
43c09969 697static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
698static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
699static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 700static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 701static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 702static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
703
704#if 0
2e621225 705static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
706#endif
707
2e621225
GM
708static int next_element_from_ellipsis P_ ((struct it *));
709static void pint2str P_ ((char *, int, int));
710static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
711 struct text_pos));
712static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
713static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
714static void store_frame_title_char P_ ((char));
715static int store_frame_title P_ ((unsigned char *, int, int));
716static void x_consider_frame_title P_ ((Lisp_Object));
717static void handle_stop P_ ((struct it *));
718static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 719static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 720static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
721static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
722static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 723static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
724 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
725 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 726static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
727static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
728static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
729static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 730static int display_echo_area P_ ((struct window *));
23dd2d97
KR
731static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
732static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 733static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 734static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
735static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
736 struct text_pos));
737static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 738static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
739static void insert_left_trunc_glyphs P_ ((struct it *));
740static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
741static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 742static int append_space P_ ((struct it *, int));
cb617e7c 743static int make_cursor_line_fully_visible P_ ((struct window *));
31ade731 744static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
47589c8c 745static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
746static int trailing_whitespace_p P_ ((int));
747static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
748static void push_it P_ ((struct it *));
749static void pop_it P_ ((struct it *));
750static void sync_frame_with_window_matrix_rows P_ ((struct window *));
751static void redisplay_internal P_ ((int));
c6e89d6c 752static int echo_area_display P_ ((int));
5f5c8ee5
GM
753static void redisplay_windows P_ ((Lisp_Object));
754static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
755static Lisp_Object redisplay_window_error ();
756static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
757static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
758static void update_menu_bar P_ ((struct frame *, int));
759static int try_window_reusing_current_matrix P_ ((struct window *));
760static int try_window_id P_ ((struct window *));
761static int display_line P_ ((struct it *));
715e84c9 762static int display_mode_lines P_ ((struct window *));
04612a64 763static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 764static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
72f62cb5 765static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
766static void display_menu_bar P_ ((struct window *));
767static int display_count_lines P_ ((int, int, int, int, int *));
768static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
769 int, int, struct it *, int, int, int, int));
770static void compute_line_metrics P_ ((struct it *));
771static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 772static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 773static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
774static void reseat P_ ((struct it *, struct text_pos, int));
775static void reseat_1 P_ ((struct it *, struct text_pos, int));
776static void back_to_previous_visible_line_start P_ ((struct it *));
777static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 778static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
779static int next_element_from_display_vector P_ ((struct it *));
780static int next_element_from_string P_ ((struct it *));
781static int next_element_from_c_string P_ ((struct it *));
782static int next_element_from_buffer P_ ((struct it *));
260a86a0 783static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
784static int next_element_from_image P_ ((struct it *));
785static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 786static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
787static int init_from_display_pos P_ ((struct it *, struct window *,
788 struct display_pos *));
5f5c8ee5
GM
789static void reseat_to_string P_ ((struct it *, unsigned char *,
790 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
791static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
792 int, int, int));
793void move_it_vertically_backward P_ ((struct it *, int));
794static void init_to_row_start P_ ((struct it *, struct window *,
795 struct glyph_row *));
47d57b22
GM
796static int init_to_row_end P_ ((struct it *, struct window *,
797 struct glyph_row *));
5f5c8ee5 798static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 799static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
800static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
801 Lisp_Object, int));
802static struct text_pos string_pos P_ ((int, Lisp_Object));
803static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
804static int number_of_chars P_ ((unsigned char *, int));
805static void compute_stop_pos P_ ((struct it *));
806static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
807 Lisp_Object));
808static int face_before_or_after_it_pos P_ ((struct it *, int));
809static int next_overlay_change P_ ((int));
810static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
811 Lisp_Object, struct text_pos *,
812 int));
06a12811 813static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
814static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
815 struct window *));
5f5c8ee5
GM
816
817#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
818#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 819
5f5c8ee5 820#ifdef HAVE_WINDOW_SYSTEM
12adba34 821
e037b9ec
GM
822static void update_tool_bar P_ ((struct frame *, int));
823static void build_desired_tool_bar_string P_ ((struct frame *f));
824static int redisplay_tool_bar P_ ((struct frame *));
825static void display_tool_bar_line P_ ((struct it *));
12adba34 826
5f5c8ee5 827#endif /* HAVE_WINDOW_SYSTEM */
12adba34 828
5f5c8ee5
GM
829\f
830/***********************************************************************
831 Window display dimensions
832 ***********************************************************************/
12adba34 833
5f5c8ee5
GM
834/* Return the window-relative maximum y + 1 for glyph rows displaying
835 text in window W. This is the height of W minus the height of a
836 mode line, if any. */
837
838INLINE int
839window_text_bottom_y (w)
840 struct window *w;
841{
842 struct frame *f = XFRAME (w->frame);
843 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 844
5f5c8ee5
GM
845 if (WINDOW_WANTS_MODELINE_P (w))
846 height -= CURRENT_MODE_LINE_HEIGHT (w);
847 return height;
f88eb0b6
KH
848}
849
f82aff7c 850
5f5c8ee5 851/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 852 means return the total width of W, not including fringes to
5f5c8ee5 853 the left and right of the window. */
ff6c30e5 854
5f5c8ee5
GM
855INLINE int
856window_box_width (w, area)
857 struct window *w;
858 int area;
859{
860 struct frame *f = XFRAME (w->frame);
861 int width = XFASTINT (w->width);
862
863 if (!w->pseudo_window_p)
ff6c30e5 864 {
b46952ae 865 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
5f5c8ee5
GM
866
867 if (area == TEXT_AREA)
868 {
869 if (INTEGERP (w->left_margin_width))
870 width -= XFASTINT (w->left_margin_width);
871 if (INTEGERP (w->right_margin_width))
872 width -= XFASTINT (w->right_margin_width);
873 }
874 else if (area == LEFT_MARGIN_AREA)
875 width = (INTEGERP (w->left_margin_width)
876 ? XFASTINT (w->left_margin_width) : 0);
877 else if (area == RIGHT_MARGIN_AREA)
878 width = (INTEGERP (w->right_margin_width)
879 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 880 }
5f5c8ee5
GM
881
882 return width * CANON_X_UNIT (f);
ff6c30e5 883}
1adc55de 884
1adc55de 885
5f5c8ee5 886/* Return the pixel height of the display area of window W, not
4b41cebb 887 including mode lines of W, if any. */
f88eb0b6 888
5f5c8ee5
GM
889INLINE int
890window_box_height (w)
891 struct window *w;
f88eb0b6 892{
5f5c8ee5
GM
893 struct frame *f = XFRAME (w->frame);
894 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
895
896 xassert (height >= 0);
5f5c8ee5 897
d9c9e99c
MB
898 /* Note: the code below that determines the mode-line/header-line
899 height is essentially the same as that contained in the macro
900 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
901 the appropriate glyph row has its `mode_line_p' flag set,
902 and if it doesn't, uses estimate_mode_line_height instead. */
903
5f5c8ee5 904 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
905 {
906 struct glyph_row *ml_row
907 = (w->current_matrix && w->current_matrix->rows
908 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
909 : 0);
910 if (ml_row && ml_row->mode_line_p)
911 height -= ml_row->height;
912 else
96d2320f 913 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 914 }
5f5c8ee5 915
045dee35 916 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
917 {
918 struct glyph_row *hl_row
919 = (w->current_matrix && w->current_matrix->rows
920 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
921 : 0);
922 if (hl_row && hl_row->mode_line_p)
923 height -= hl_row->height;
924 else
925 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
926 }
5f5c8ee5 927
7c75be36
PJ
928 /* With a very small font and a mode-line that's taller than
929 default, we might end up with a negative height. */
930 return max (0, height);
5992c4f7
KH
931}
932
933
5f5c8ee5
GM
934/* Return the frame-relative coordinate of the left edge of display
935 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 936 whole window, to the right of the left fringe of W. */
5992c4f7 937
5f5c8ee5
GM
938INLINE int
939window_box_left (w, area)
940 struct window *w;
941 int area;
90adcf20 942{
5f5c8ee5
GM
943 struct frame *f = XFRAME (w->frame);
944 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 945
5f5c8ee5 946 if (!w->pseudo_window_p)
90adcf20 947 {
5f5c8ee5 948 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
b46952ae 949 + FRAME_LEFT_FRINGE_WIDTH (f));
5f5c8ee5
GM
950
951 if (area == TEXT_AREA)
952 x += window_box_width (w, LEFT_MARGIN_AREA);
953 else if (area == RIGHT_MARGIN_AREA)
954 x += (window_box_width (w, LEFT_MARGIN_AREA)
955 + window_box_width (w, TEXT_AREA));
90adcf20 956 }
73af359d 957
5f5c8ee5
GM
958 return x;
959}
90adcf20 960
b6436d4e 961
5f5c8ee5
GM
962/* Return the frame-relative coordinate of the right edge of display
963 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 964 whole window, to the left of the right fringe of W. */
ded34426 965
5f5c8ee5
GM
966INLINE int
967window_box_right (w, area)
968 struct window *w;
969 int area;
970{
971 return window_box_left (w, area) + window_box_width (w, area);
972}
973
974
975/* Get the bounding box of the display area AREA of window W, without
976 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 977 whole window, not including the left and right fringes of
5f5c8ee5
GM
978 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
979 coordinates of the upper-left corner of the box. Return in
980 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
981
982INLINE void
983window_box (w, area, box_x, box_y, box_width, box_height)
984 struct window *w;
985 int area;
986 int *box_x, *box_y, *box_width, *box_height;
987{
988 struct frame *f = XFRAME (w->frame);
989
990 *box_width = window_box_width (w, area);
991 *box_height = window_box_height (w);
992 *box_x = window_box_left (w, area);
993 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
994 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
995 if (WINDOW_WANTS_HEADER_LINE_P (w))
996 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 997}
1adc55de 998
1adc55de 999
5f5c8ee5 1000/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1001 mode lines. AREA < 0 means the whole window, not including the
1002 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1003 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1004 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1005 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1006 box. */
ded34426 1007
5f5c8ee5
GM
1008INLINE void
1009window_box_edges (w, area, top_left_x, top_left_y,
1010 bottom_right_x, bottom_right_y)
1011 struct window *w;
1012 int area;
1013 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1014{
5f5c8ee5
GM
1015 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1016 bottom_right_y);
1017 *bottom_right_x += *top_left_x;
1018 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1019}
1020
5f5c8ee5
GM
1021
1022\f
1023/***********************************************************************
1024 Utilities
1025 ***********************************************************************/
1026
8b6ea97f
GM
1027/* Return the bottom y-position of the line the iterator IT is in.
1028 This can modify IT's settings. */
1029
1030int
1031line_bottom_y (it)
1032 struct it *it;
1033{
1034 int line_height = it->max_ascent + it->max_descent;
1035 int line_top_y = it->current_y;
1036
1037 if (line_height == 0)
1038 {
1039 if (last_height)
1040 line_height = last_height;
1041 else if (IT_CHARPOS (*it) < ZV)
1042 {
1043 move_it_by_lines (it, 1, 1);
1044 line_height = (it->max_ascent || it->max_descent
1045 ? it->max_ascent + it->max_descent
1046 : last_height);
1047 }
1048 else
1049 {
1050 struct glyph_row *row = it->glyph_row;
1051
1052 /* Use the default character height. */
1053 it->glyph_row = NULL;
1054 it->what = IT_CHARACTER;
1055 it->c = ' ';
1056 it->len = 1;
1057 PRODUCE_GLYPHS (it);
1058 line_height = it->ascent + it->descent;
1059 it->glyph_row = row;
1060 }
1061 }
1062
1063 return line_top_y + line_height;
1064}
1065
1066
0db95684
GM
1067/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1068 1 if POS is visible and the line containing POS is fully visible.
1069 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1070 and header-lines heights. */
3a641a69
GM
1071
1072int
04612a64 1073pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1074 struct window *w;
04612a64 1075 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1076{
1077 struct it it;
1078 struct text_pos top;
fcab1954
GM
1079 int visible_p;
1080 struct buffer *old_buffer = NULL;
1081
1082 if (XBUFFER (w->buffer) != current_buffer)
1083 {
1084 old_buffer = current_buffer;
1085 set_buffer_internal_1 (XBUFFER (w->buffer));
1086 }
3a641a69
GM
1087
1088 *fully = visible_p = 0;
1089 SET_TEXT_POS_FROM_MARKER (top, w->start);
04612a64
GM
1090
1091 /* Compute exact mode line heights, if requested. */
1092 if (exact_mode_line_heights_p)
1093 {
1094 if (WINDOW_WANTS_MODELINE_P (w))
1095 current_mode_line_height
96d2320f 1096 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64
GM
1097 current_buffer->mode_line_format);
1098
1099 if (WINDOW_WANTS_HEADER_LINE_P (w))
1100 current_header_line_height
1101 = display_mode_line (w, HEADER_LINE_FACE_ID,
1102 current_buffer->header_line_format);
1103 }
3a641a69 1104
04612a64 1105 start_display (&it, w, top);
3a641a69
GM
1106 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1107 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1108
1109 /* Note that we may overshoot because of invisible text. */
1110 if (IT_CHARPOS (it) >= charpos)
3a641a69 1111 {
8b6ea97f
GM
1112 int top_y = it.current_y;
1113 int bottom_y = line_bottom_y (&it);
fcab1954 1114 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
3a641a69 1115
8b6ea97f
GM
1116 if (top_y < window_top_y)
1117 visible_p = bottom_y > window_top_y;
1118 else if (top_y < it.last_visible_y)
fcab1954
GM
1119 {
1120 visible_p = 1;
8b6ea97f 1121 *fully = bottom_y <= it.last_visible_y;
fcab1954 1122 }
3a641a69
GM
1123 }
1124 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1125 {
1126 move_it_by_lines (&it, 1, 0);
1127 if (charpos < IT_CHARPOS (it))
1128 {
1129 visible_p = 1;
1130 *fully = 0;
1131 }
1132 }
fcab1954
GM
1133
1134 if (old_buffer)
1135 set_buffer_internal_1 (old_buffer);
04612a64
GM
1136
1137 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1138 return visible_p;
1139}
1140
1141
4fdb80f2
GM
1142/* Return the next character from STR which is MAXLEN bytes long.
1143 Return in *LEN the length of the character. This is like
1144 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1145 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1146 character. */
1147
1148static INLINE int
7a5b8a93 1149string_char_and_length (str, maxlen, len)
4fdb80f2 1150 unsigned char *str;
7a5b8a93 1151 int maxlen, *len;
4fdb80f2
GM
1152{
1153 int c;
1154
1155 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1156 if (!CHAR_VALID_P (c, 1))
1157 /* We may not change the length here because other places in Emacs
9ab8560d 1158 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1159 characters. */
1160 c = '?';
1161
1162 return c;
1163}
1164
1165
1166
5f5c8ee5
GM
1167/* Given a position POS containing a valid character and byte position
1168 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1169
1170static struct text_pos
1171string_pos_nchars_ahead (pos, string, nchars)
1172 struct text_pos pos;
1173 Lisp_Object string;
1174 int nchars;
0b1005ef 1175{
5f5c8ee5
GM
1176 xassert (STRINGP (string) && nchars >= 0);
1177
1178 if (STRING_MULTIBYTE (string))
1179 {
2051c264
GM
1180 int rest = SBYTES (string) - BYTEPOS (pos);
1181 unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1182 int len;
1183
1184 while (nchars--)
1185 {
4fdb80f2 1186 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1187 p += len, rest -= len;
1188 xassert (rest >= 0);
1189 CHARPOS (pos) += 1;
1190 BYTEPOS (pos) += len;
1191 }
1192 }
1193 else
1194 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1195
1196 return pos;
0a9dc68b
RS
1197}
1198
0a9dc68b 1199
5f5c8ee5
GM
1200/* Value is the text position, i.e. character and byte position,
1201 for character position CHARPOS in STRING. */
1202
1203static INLINE struct text_pos
1204string_pos (charpos, string)
1205 int charpos;
0a9dc68b 1206 Lisp_Object string;
0a9dc68b 1207{
5f5c8ee5
GM
1208 struct text_pos pos;
1209 xassert (STRINGP (string));
1210 xassert (charpos >= 0);
1211 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1212 return pos;
1213}
1214
1215
1216/* Value is a text position, i.e. character and byte position, for
1217 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1218 means recognize multibyte characters. */
1219
1220static struct text_pos
1221c_string_pos (charpos, s, multibyte_p)
1222 int charpos;
1223 unsigned char *s;
1224 int multibyte_p;
1225{
1226 struct text_pos pos;
1227
1228 xassert (s != NULL);
1229 xassert (charpos >= 0);
1230
1231 if (multibyte_p)
0a9dc68b 1232 {
5f5c8ee5
GM
1233 int rest = strlen (s), len;
1234
1235 SET_TEXT_POS (pos, 0, 0);
1236 while (charpos--)
0a9dc68b 1237 {
4fdb80f2 1238 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1239 s += len, rest -= len;
1240 xassert (rest >= 0);
1241 CHARPOS (pos) += 1;
1242 BYTEPOS (pos) += len;
0a9dc68b
RS
1243 }
1244 }
5f5c8ee5
GM
1245 else
1246 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1247
5f5c8ee5
GM
1248 return pos;
1249}
0a9dc68b 1250
0a9dc68b 1251
5f5c8ee5
GM
1252/* Value is the number of characters in C string S. MULTIBYTE_P
1253 non-zero means recognize multibyte characters. */
0a9dc68b 1254
5f5c8ee5
GM
1255static int
1256number_of_chars (s, multibyte_p)
1257 unsigned char *s;
1258 int multibyte_p;
1259{
1260 int nchars;
1261
1262 if (multibyte_p)
1263 {
1264 int rest = strlen (s), len;
1265 unsigned char *p = (unsigned char *) s;
0a9dc68b 1266
5f5c8ee5
GM
1267 for (nchars = 0; rest > 0; ++nchars)
1268 {
4fdb80f2 1269 string_char_and_length (p, rest, &len);
5f5c8ee5 1270 rest -= len, p += len;
0a9dc68b
RS
1271 }
1272 }
5f5c8ee5
GM
1273 else
1274 nchars = strlen (s);
1275
1276 return nchars;
0b1005ef
KH
1277}
1278
5f5c8ee5
GM
1279
1280/* Compute byte position NEWPOS->bytepos corresponding to
1281 NEWPOS->charpos. POS is a known position in string STRING.
1282 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1283
5f5c8ee5
GM
1284static void
1285compute_string_pos (newpos, pos, string)
1286 struct text_pos *newpos, pos;
1287 Lisp_Object string;
76412d64 1288{
5f5c8ee5
GM
1289 xassert (STRINGP (string));
1290 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1291
1292 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1293 *newpos = string_pos_nchars_ahead (pos, string,
1294 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1295 else
1296 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1297}
1298
9c74a0dd 1299
5f5c8ee5
GM
1300\f
1301/***********************************************************************
1302 Lisp form evaluation
1303 ***********************************************************************/
1304
116d6f5c 1305/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1306
1307static Lisp_Object
116d6f5c 1308safe_eval_handler (arg)
5f5c8ee5
GM
1309 Lisp_Object arg;
1310{
0dbf9fd2 1311 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1312 return Qnil;
1313}
1314
1315
1316/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1317 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1318
71e5b1b8 1319Lisp_Object
116d6f5c 1320safe_eval (sexpr)
5f5c8ee5
GM
1321 Lisp_Object sexpr;
1322{
5f5c8ee5 1323 Lisp_Object val;
30a3f61c
GM
1324
1325 if (inhibit_eval_during_redisplay)
1326 val = Qnil;
1327 else
1328 {
1329 int count = BINDING_STACK_SIZE ();
1330 struct gcpro gcpro1;
0d8b31c0 1331
30a3f61c
GM
1332 GCPRO1 (sexpr);
1333 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1334 /* Use Qt to ensure debugger does not run,
1335 so there is no possibility of wanting to redisplay. */
1336 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1337 safe_eval_handler);
1338 UNGCPRO;
1339 val = unbind_to (count, val);
1340 }
1341
1342 return val;
0d8b31c0
GM
1343}
1344
1345
1346/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1347 Return the result, or nil if something went wrong. Prevent
1348 redisplay during the evaluation. */
0d8b31c0
GM
1349
1350Lisp_Object
116d6f5c 1351safe_call (nargs, args)
0d8b31c0
GM
1352 int nargs;
1353 Lisp_Object *args;
1354{
0d8b31c0 1355 Lisp_Object val;
30a3f61c
GM
1356
1357 if (inhibit_eval_during_redisplay)
1358 val = Qnil;
1359 else
1360 {
1361 int count = BINDING_STACK_SIZE ();
1362 struct gcpro gcpro1;
0d8b31c0 1363
30a3f61c
GM
1364 GCPRO1 (args[0]);
1365 gcpro1.nvars = nargs;
1366 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1367 /* Use Qt to ensure debugger does not run,
1368 so there is no possibility of wanting to redisplay. */
1369 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1370 safe_eval_handler);
1371 UNGCPRO;
1372 val = unbind_to (count, val);
1373 }
1374
1375 return val;
5f5c8ee5
GM
1376}
1377
1378
116d6f5c
GM
1379/* Call function FN with one argument ARG.
1380 Return the result, or nil if something went wrong. */
1381
1382Lisp_Object
1383safe_call1 (fn, arg)
1384 Lisp_Object fn, arg;
1385{
1386 Lisp_Object args[2];
1387 args[0] = fn;
1388 args[1] = arg;
1389 return safe_call (2, args);
1390}
1391
1392
5f5c8ee5
GM
1393\f
1394/***********************************************************************
1395 Debugging
1396 ***********************************************************************/
1397
1398#if 0
1399
1400/* Define CHECK_IT to perform sanity checks on iterators.
1401 This is for debugging. It is too slow to do unconditionally. */
1402
1403static void
1404check_it (it)
1405 struct it *it;
1406{
1407 if (it->method == next_element_from_string)
a2889657 1408 {
5f5c8ee5
GM
1409 xassert (STRINGP (it->string));
1410 xassert (IT_STRING_CHARPOS (*it) >= 0);
1411 }
1412 else if (it->method == next_element_from_buffer)
1413 {
1414 /* Check that character and byte positions agree. */
1415 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1416 }
73af359d 1417
5f5c8ee5
GM
1418 if (it->dpvec)
1419 xassert (it->current.dpvec_index >= 0);
1420 else
1421 xassert (it->current.dpvec_index < 0);
1422}
1f40cad2 1423
5f5c8ee5
GM
1424#define CHECK_IT(IT) check_it ((IT))
1425
1426#else /* not 0 */
1427
1428#define CHECK_IT(IT) (void) 0
1429
1430#endif /* not 0 */
1431
1432
1433#if GLYPH_DEBUG
1434
1435/* Check that the window end of window W is what we expect it
1436 to be---the last row in the current matrix displaying text. */
1437
1438static void
1439check_window_end (w)
1440 struct window *w;
1441{
1442 if (!MINI_WINDOW_P (w)
1443 && !NILP (w->window_end_valid))
1444 {
1445 struct glyph_row *row;
1446 xassert ((row = MATRIX_ROW (w->current_matrix,
1447 XFASTINT (w->window_end_vpos)),
1448 !row->enabled_p
1449 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1450 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1451 }
1452}
1453
1454#define CHECK_WINDOW_END(W) check_window_end ((W))
1455
1456#else /* not GLYPH_DEBUG */
1457
1458#define CHECK_WINDOW_END(W) (void) 0
1459
1460#endif /* not GLYPH_DEBUG */
1461
1462
1463\f
1464/***********************************************************************
1465 Iterator initialization
1466 ***********************************************************************/
1467
1468/* Initialize IT for displaying current_buffer in window W, starting
1469 at character position CHARPOS. CHARPOS < 0 means that no buffer
1470 position is specified which is useful when the iterator is assigned
1471 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1472 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1473
1474 If ROW is not null, calls to produce_glyphs with IT as parameter
1475 will produce glyphs in that row.
1476
1477 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1478 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1479 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1480 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
5f5c8ee5 1481
96d2320f
KS
1482 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1483 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1484 will be initialized to use the corresponding mode line glyph row of
1485 the desired matrix of W. */
5f5c8ee5
GM
1486
1487void
1488init_iterator (it, w, charpos, bytepos, row, base_face_id)
1489 struct it *it;
1490 struct window *w;
1491 int charpos, bytepos;
1492 struct glyph_row *row;
1493 enum face_id base_face_id;
1494{
1495 int highlight_region_p;
5f5c8ee5
GM
1496
1497 /* Some precondition checks. */
1498 xassert (w != NULL && it != NULL);
3b6b6db7
SM
1499 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1500 && charpos <= ZV));
5f5c8ee5
GM
1501
1502 /* If face attributes have been changed since the last redisplay,
1503 free realized faces now because they depend on face definitions
1504 that might have changed. */
1505 if (face_change_count)
1506 {
1507 face_change_count = 0;
1508 free_all_realized_faces (Qnil);
1509 }
1510
1511 /* Use one of the mode line rows of W's desired matrix if
1512 appropriate. */
1513 if (row == NULL)
1514 {
96d2320f
KS
1515 if (base_face_id == MODE_LINE_FACE_ID
1516 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 1517 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1518 else if (base_face_id == HEADER_LINE_FACE_ID)
1519 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1520 }
1521
1522 /* Clear IT. */
1523 bzero (it, sizeof *it);
1524 it->current.overlay_string_index = -1;
1525 it->current.dpvec_index = -1;
5f5c8ee5
GM
1526 it->base_face_id = base_face_id;
1527
1528 /* The window in which we iterate over current_buffer: */
1529 XSETWINDOW (it->window, w);
1530 it->w = w;
1531 it->f = XFRAME (w->frame);
1532
d475bcb8
GM
1533 /* Extra space between lines (on window systems only). */
1534 if (base_face_id == DEFAULT_FACE_ID
1535 && FRAME_WINDOW_P (it->f))
1536 {
1537 if (NATNUMP (current_buffer->extra_line_spacing))
1538 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1539 else if (it->f->extra_line_spacing > 0)
1540 it->extra_line_spacing = it->f->extra_line_spacing;
1541 }
1542
5f5c8ee5 1543 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1544 attribute changes of named faces, recompute them. When running
1545 in batch mode, the face cache of Vterminal_frame is null. If
1546 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1547 if (
1548#ifndef WINDOWSNT
1549 noninteractive &&
1550#endif
1551 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1552 init_frame_faces (it->f);
5f5c8ee5
GM
1553 if (FRAME_FACE_CACHE (it->f)->used == 0)
1554 recompute_basic_faces (it->f);
1555
5f5c8ee5
GM
1556 /* Current value of the `space-width', and 'height' properties. */
1557 it->space_width = Qnil;
1558 it->font_height = Qnil;
1559
1560 /* Are control characters displayed as `^C'? */
1561 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1562
1563 /* -1 means everything between a CR and the following line end
1564 is invisible. >0 means lines indented more than this value are
1565 invisible. */
1566 it->selective = (INTEGERP (current_buffer->selective_display)
1567 ? XFASTINT (current_buffer->selective_display)
1568 : (!NILP (current_buffer->selective_display)
1569 ? -1 : 0));
1570 it->selective_display_ellipsis_p
1571 = !NILP (current_buffer->selective_display_ellipses);
1572
1573 /* Display table to use. */
1574 it->dp = window_display_table (w);
1575
1576 /* Are multibyte characters enabled in current_buffer? */
1577 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1578
1579 /* Non-zero if we should highlight the region. */
1580 highlight_region_p
3ebf0ea9 1581 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
1582 && !NILP (current_buffer->mark_active)
1583 && XMARKER (current_buffer->mark)->buffer != 0);
1584
1585 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1586 start and end of a visible region in window IT->w. Set both to
1587 -1 to indicate no region. */
1588 if (highlight_region_p
1589 /* Maybe highlight only in selected window. */
1590 && (/* Either show region everywhere. */
1591 highlight_nonselected_windows
1592 /* Or show region in the selected window. */
1593 || w == XWINDOW (selected_window)
1594 /* Or show the region if we are in the mini-buffer and W is
1595 the window the mini-buffer refers to. */
1596 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
1597 && WINDOWP (minibuf_selected_window)
1598 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
1599 {
1600 int charpos = marker_position (current_buffer->mark);
1601 it->region_beg_charpos = min (PT, charpos);
1602 it->region_end_charpos = max (PT, charpos);
1603 }
1604 else
1605 it->region_beg_charpos = it->region_end_charpos = -1;
1606
1607 /* Get the position at which the redisplay_end_trigger hook should
1608 be run, if it is to be run at all. */
1609 if (MARKERP (w->redisplay_end_trigger)
1610 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1611 it->redisplay_end_trigger_charpos
1612 = marker_position (w->redisplay_end_trigger);
1613 else if (INTEGERP (w->redisplay_end_trigger))
1614 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1615
1616 /* Correct bogus values of tab_width. */
1617 it->tab_width = XINT (current_buffer->tab_width);
1618 if (it->tab_width <= 0 || it->tab_width > 1000)
1619 it->tab_width = 8;
1620
1621 /* Are lines in the display truncated? */
1622 it->truncate_lines_p
1623 = (base_face_id != DEFAULT_FACE_ID
1624 || XINT (it->w->hscroll)
1625 || (truncate_partial_width_windows
1626 && !WINDOW_FULL_WIDTH_P (it->w))
1627 || !NILP (current_buffer->truncate_lines));
1628
1629 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 1630 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
1631 frames. */
1632 if (!FRAME_WINDOW_P (it->f))
1633 {
1634 if (it->truncate_lines_p)
1635 {
1636 /* We will need the truncation glyph. */
1637 xassert (it->glyph_row == NULL);
1638 produce_special_glyphs (it, IT_TRUNCATION);
1639 it->truncation_pixel_width = it->pixel_width;
1640 }
1641 else
1642 {
1643 /* We will need the continuation glyph. */
1644 xassert (it->glyph_row == NULL);
1645 produce_special_glyphs (it, IT_CONTINUATION);
1646 it->continuation_pixel_width = it->pixel_width;
1647 }
1648
153c2160 1649 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
1650 above has changed them. */
1651 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1652 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1653 }
1654
1655 /* Set this after getting the dimensions of truncation and
1656 continuation glyphs, so that we don't produce glyphs when calling
1657 produce_special_glyphs, above. */
1658 it->glyph_row = row;
1659 it->area = TEXT_AREA;
1660
1661 /* Get the dimensions of the display area. The display area
1662 consists of the visible window area plus a horizontally scrolled
1663 part to the left of the window. All x-values are relative to the
1664 start of this total display area. */
1665 if (base_face_id != DEFAULT_FACE_ID)
1666 {
1667 /* Mode lines, menu bar in terminal frames. */
1668 it->first_visible_x = 0;
1669 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1670 }
1671 else
1672 {
1673 it->first_visible_x
1674 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1675 it->last_visible_x = (it->first_visible_x
1676 + window_box_width (w, TEXT_AREA));
1677
1678 /* If we truncate lines, leave room for the truncator glyph(s) at
1679 the right margin. Otherwise, leave room for the continuation
1680 glyph(s). Truncation and continuation glyphs are not inserted
1681 for window-based redisplay. */
1682 if (!FRAME_WINDOW_P (it->f))
1683 {
1684 if (it->truncate_lines_p)
1685 it->last_visible_x -= it->truncation_pixel_width;
1686 else
1687 it->last_visible_x -= it->continuation_pixel_width;
1688 }
1689
045dee35
GM
1690 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1691 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1692 }
1693
1694 /* Leave room for a border glyph. */
1695 if (!FRAME_WINDOW_P (it->f)
1696 && !WINDOW_RIGHTMOST_P (it->w))
1697 it->last_visible_x -= 1;
1698
1699 it->last_visible_y = window_text_bottom_y (w);
1700
1701 /* For mode lines and alike, arrange for the first glyph having a
1702 left box line if the face specifies a box. */
1703 if (base_face_id != DEFAULT_FACE_ID)
1704 {
1705 struct face *face;
1706
1707 it->face_id = base_face_id;
1708
1709 /* If we have a boxed mode line, make the first character appear
1710 with a left box line. */
1711 face = FACE_FROM_ID (it->f, base_face_id);
1712 if (face->box != FACE_NO_BOX)
1713 it->start_of_box_run_p = 1;
1714 }
1715
1716 /* If a buffer position was specified, set the iterator there,
1717 getting overlays and face properties from that position. */
3ebf0ea9 1718 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
1719 {
1720 it->end_charpos = ZV;
1721 it->face_id = -1;
1722 IT_CHARPOS (*it) = charpos;
1723
1724 /* Compute byte position if not specified. */
3ebf0ea9 1725 if (bytepos < charpos)
5f5c8ee5
GM
1726 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1727 else
1728 IT_BYTEPOS (*it) = bytepos;
1729
1730 /* Compute faces etc. */
1731 reseat (it, it->current.pos, 1);
1732 }
1733
1734 CHECK_IT (it);
1735}
1736
1737
1738/* Initialize IT for the display of window W with window start POS. */
1739
1740void
1741start_display (it, w, pos)
1742 struct it *it;
1743 struct window *w;
1744 struct text_pos pos;
1745{
5f5c8ee5 1746 struct glyph_row *row;
045dee35 1747 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1748
1749 row = w->desired_matrix->rows + first_vpos;
1750 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
17fdcfc8
GM
1751
1752 if (!it->truncate_lines_p)
5f5c8ee5 1753 {
17fdcfc8
GM
1754 int start_at_line_beg_p;
1755 int first_y = it->current_y;
1756
1757 /* If window start is not at a line start, skip forward to POS to
1758 get the correct continuation lines width. */
1759 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1760 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1761 if (!start_at_line_beg_p)
5f5c8ee5 1762 {
17fdcfc8
GM
1763 reseat_at_previous_visible_line_start (it);
1764 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1765
1766 /* If lines are continued, this line may end in the middle
1767 of a multi-glyph character (e.g. a control character
1768 displayed as \003, or in the middle of an overlay
1769 string). In this case move_it_to above will not have
1770 taken us to the start of the continuation line but to the
1771 end of the continued line. */
b28cb6ed 1772 if (it->current_x > 0)
5f5c8ee5 1773 {
b28cb6ed
GM
1774 if (it->current.dpvec_index >= 0
1775 || it->current.overlay_string_index >= 0)
1776 {
cafafe0b 1777 set_iterator_to_next (it, 1);
b28cb6ed
GM
1778 move_it_in_display_line_to (it, -1, -1, 0);
1779 }
17fdcfc8 1780
b28cb6ed 1781 it->continuation_lines_width += it->current_x;
5f5c8ee5 1782 }
b28cb6ed
GM
1783
1784 /* We're starting a new display line, not affected by the
1785 height of the continued line, so clear the appropriate
1786 fields in the iterator structure. */
1787 it->max_ascent = it->max_descent = 0;
1788 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 1789
17fdcfc8
GM
1790 it->current_y = first_y;
1791 it->vpos = 0;
1792 it->current_x = it->hpos = 0;
1793 }
5f5c8ee5
GM
1794 }
1795
1796#if 0 /* Don't assert the following because start_display is sometimes
1797 called intentionally with a window start that is not at a
1798 line start. Please leave this code in as a comment. */
1799
1800 /* Window start should be on a line start, now. */
1801 xassert (it->continuation_lines_width
1802 || IT_CHARPOS (it) == BEGV
1803 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1804#endif /* 0 */
1805}
1806
1807
4bde0ebb
GM
1808/* Return 1 if POS is a position in ellipses displayed for invisible
1809 text. W is the window we display, for text property lookup. */
5f5c8ee5 1810
4bde0ebb
GM
1811static int
1812in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 1813 struct display_pos *pos;
4bde0ebb 1814 struct window *w;
5f5c8ee5 1815{
ac90c44f 1816 Lisp_Object prop, window;
4bde0ebb
GM
1817 int ellipses_p = 0;
1818 int charpos = CHARPOS (pos->pos);
ac90c44f
GM
1819
1820 /* If POS specifies a position in a display vector, this might
1821 be for an ellipsis displayed for invisible text. We won't
1822 get the iterator set up for delivering that ellipsis unless
1823 we make sure that it gets aware of the invisible text. */
1824 if (pos->dpvec_index >= 0
1825 && pos->overlay_string_index < 0
1826 && CHARPOS (pos->string_pos) < 0
1827 && charpos > BEGV
1828 && (XSETWINDOW (window, w),
1829 prop = Fget_char_property (make_number (charpos),
1830 Qinvisible, window),
20fbd925 1831 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
1832 {
1833 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1834 window);
8580a4e3 1835 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
1836 }
1837
1838 return ellipses_p;
1839}
1840
1841
1842/* Initialize IT for stepping through current_buffer in window W,
1843 starting at position POS that includes overlay string and display
47d57b22
GM
1844 vector/ control character translation position information. Value
1845 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 1846
47d57b22 1847static int
4bde0ebb
GM
1848init_from_display_pos (it, w, pos)
1849 struct it *it;
1850 struct window *w;
1851 struct display_pos *pos;
1852{
1853 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 1854 int i, overlay_strings_with_newlines = 0;
4bde0ebb
GM
1855
1856 /* If POS specifies a position in a display vector, this might
1857 be for an ellipsis displayed for invisible text. We won't
1858 get the iterator set up for delivering that ellipsis unless
1859 we make sure that it gets aware of the invisible text. */
1860 if (in_ellipses_for_invisible_text_p (pos, w))
1861 {
1862 --charpos;
1863 bytepos = 0;
ac90c44f
GM
1864 }
1865
5f5c8ee5
GM
1866 /* Keep in mind: the call to reseat in init_iterator skips invisible
1867 text, so we might end up at a position different from POS. This
1868 is only a problem when POS is a row start after a newline and an
1869 overlay starts there with an after-string, and the overlay has an
1870 invisible property. Since we don't skip invisible text in
1871 display_line and elsewhere immediately after consuming the
1872 newline before the row start, such a POS will not be in a string,
1873 but the call to init_iterator below will move us to the
1874 after-string. */
ac90c44f 1875 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 1876
47d57b22 1877 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 1878 {
2051c264
GM
1879 char *s = SDATA (it->overlay_strings[i]);
1880 char *e = s + SBYTES (it->overlay_strings[i]);
1e1e5daf
GM
1881
1882 while (s < e && *s != '\n')
1883 ++s;
1884
1885 if (s < e)
1886 {
1887 overlay_strings_with_newlines = 1;
1888 break;
1889 }
1890 }
47d57b22 1891
75c5350a
GM
1892 /* If position is within an overlay string, set up IT to the right
1893 overlay string. */
5f5c8ee5
GM
1894 if (pos->overlay_string_index >= 0)
1895 {
1896 int relative_index;
75c5350a
GM
1897
1898 /* If the first overlay string happens to have a `display'
1899 property for an image, the iterator will be set up for that
1900 image, and we have to undo that setup first before we can
1901 correct the overlay string index. */
1902 if (it->method == next_element_from_image)
1903 pop_it (it);
5f5c8ee5
GM
1904
1905 /* We already have the first chunk of overlay strings in
1906 IT->overlay_strings. Load more until the one for
1907 pos->overlay_string_index is in IT->overlay_strings. */
1908 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1909 {
1910 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1911 it->current.overlay_string_index = 0;
1912 while (n--)
1913 {
5a08cbaf 1914 load_overlay_strings (it, 0);
5f5c8ee5
GM
1915 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1916 }
1917 }
1918
1919 it->current.overlay_string_index = pos->overlay_string_index;
1920 relative_index = (it->current.overlay_string_index
1921 % OVERLAY_STRING_CHUNK_SIZE);
1922 it->string = it->overlay_strings[relative_index];
cafafe0b 1923 xassert (STRINGP (it->string));
5f5c8ee5
GM
1924 it->current.string_pos = pos->string_pos;
1925 it->method = next_element_from_string;
1926 }
1e1e5daf
GM
1927
1928#if 0 /* This is bogus because POS not having an overlay string
1929 position does not mean it's after the string. Example: A
1930 line starting with a before-string and initialization of IT
1931 to the previous row's end position. */
2be8f184
GM
1932 else if (it->current.overlay_string_index >= 0)
1933 {
1934 /* If POS says we're already after an overlay string ending at
1935 POS, make sure to pop the iterator because it will be in
1936 front of that overlay string. When POS is ZV, we've thereby
1937 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1938 while (it->sp)
1939 pop_it (it);
2be8f184
GM
1940 it->current.overlay_string_index = -1;
1941 it->method = next_element_from_buffer;
1942 if (CHARPOS (pos->pos) == ZV)
1943 it->overlay_strings_at_end_processed_p = 1;
1944 }
1e1e5daf 1945#endif /* 0 */
2be8f184
GM
1946
1947 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1948 {
1949 /* Recorded position is not in an overlay string, but in another
1950 string. This can only be a string from a `display' property.
1951 IT should already be filled with that string. */
1952 it->current.string_pos = pos->string_pos;
1953 xassert (STRINGP (it->string));
1954 }
1955
ac90c44f
GM
1956 /* Restore position in display vector translations, control
1957 character translations or ellipses. */
5f5c8ee5
GM
1958 if (pos->dpvec_index >= 0)
1959 {
ac90c44f
GM
1960 if (it->dpvec == NULL)
1961 get_next_display_element (it);
5f5c8ee5
GM
1962 xassert (it->dpvec && it->current.dpvec_index == 0);
1963 it->current.dpvec_index = pos->dpvec_index;
1964 }
1965
1966 CHECK_IT (it);
47d57b22 1967 return !overlay_strings_with_newlines;
5f5c8ee5
GM
1968}
1969
1970
1971/* Initialize IT for stepping through current_buffer in window W
1972 starting at ROW->start. */
1973
1974static void
1975init_to_row_start (it, w, row)
1976 struct it *it;
1977 struct window *w;
1978 struct glyph_row *row;
1979{
1980 init_from_display_pos (it, w, &row->start);
1981 it->continuation_lines_width = row->continuation_lines_width;
1982 CHECK_IT (it);
1983}
1984
1985
1986/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
1987 starting in the line following ROW, i.e. starting at ROW->end.
1988 Value is zero if there are overlay strings with newlines at ROW's
1989 end position. */
5f5c8ee5 1990
47d57b22 1991static int
5f5c8ee5
GM
1992init_to_row_end (it, w, row)
1993 struct it *it;
1994 struct window *w;
1995 struct glyph_row *row;
1996{
47d57b22
GM
1997 int success = 0;
1998
1999 if (init_from_display_pos (it, w, &row->end))
2000 {
2001 if (row->continued_p)
2002 it->continuation_lines_width
2003 = row->continuation_lines_width + row->pixel_width;
2004 CHECK_IT (it);
2005 success = 1;
2006 }
2007
2008 return success;
5f5c8ee5
GM
2009}
2010
2011
2012
2013\f
2014/***********************************************************************
2015 Text properties
2016 ***********************************************************************/
2017
2018/* Called when IT reaches IT->stop_charpos. Handle text property and
2019 overlay changes. Set IT->stop_charpos to the next position where
2020 to stop. */
2021
2022static void
2023handle_stop (it)
2024 struct it *it;
2025{
2026 enum prop_handled handled;
2027 int handle_overlay_change_p = 1;
2028 struct props *p;
2029
2030 it->dpvec = NULL;
2031 it->current.dpvec_index = -1;
2032
2033 do
2034 {
2035 handled = HANDLED_NORMALLY;
2036
2037 /* Call text property handlers. */
2038 for (p = it_props; p->handler; ++p)
2039 {
2040 handled = p->handler (it);
2970b9be 2041
5f5c8ee5
GM
2042 if (handled == HANDLED_RECOMPUTE_PROPS)
2043 break;
2044 else if (handled == HANDLED_RETURN)
2045 return;
2046 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2047 handle_overlay_change_p = 0;
2048 }
2049
2050 if (handled != HANDLED_RECOMPUTE_PROPS)
2051 {
2052 /* Don't check for overlay strings below when set to deliver
2053 characters from a display vector. */
2054 if (it->method == next_element_from_display_vector)
2055 handle_overlay_change_p = 0;
2056
2057 /* Handle overlay changes. */
2058 if (handle_overlay_change_p)
2059 handled = handle_overlay_change (it);
2060
2061 /* Determine where to stop next. */
2062 if (handled == HANDLED_NORMALLY)
2063 compute_stop_pos (it);
2064 }
2065 }
2066 while (handled == HANDLED_RECOMPUTE_PROPS);
2067}
2068
2069
2070/* Compute IT->stop_charpos from text property and overlay change
2071 information for IT's current position. */
2072
2073static void
2074compute_stop_pos (it)
2075 struct it *it;
2076{
2077 register INTERVAL iv, next_iv;
2078 Lisp_Object object, limit, position;
2079
2080 /* If nowhere else, stop at the end. */
2081 it->stop_charpos = it->end_charpos;
2082
2083 if (STRINGP (it->string))
2084 {
2085 /* Strings are usually short, so don't limit the search for
2086 properties. */
2087 object = it->string;
2088 limit = Qnil;
ac90c44f 2089 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2090 }
2091 else
2092 {
2093 int charpos;
2094
2095 /* If next overlay change is in front of the current stop pos
2096 (which is IT->end_charpos), stop there. Note: value of
2097 next_overlay_change is point-max if no overlay change
2098 follows. */
2099 charpos = next_overlay_change (IT_CHARPOS (*it));
2100 if (charpos < it->stop_charpos)
2101 it->stop_charpos = charpos;
2102
2103 /* If showing the region, we have to stop at the region
2104 start or end because the face might change there. */
2105 if (it->region_beg_charpos > 0)
2106 {
2107 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2108 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2109 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2110 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2111 }
2112
2113 /* Set up variables for computing the stop position from text
2114 property changes. */
2115 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2116 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2117 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2118
2119 }
2120
2121 /* Get the interval containing IT's position. Value is a null
2122 interval if there isn't such an interval. */
2123 iv = validate_interval_range (object, &position, &position, 0);
2124 if (!NULL_INTERVAL_P (iv))
2125 {
2126 Lisp_Object values_here[LAST_PROP_IDX];
2127 struct props *p;
2128
2129 /* Get properties here. */
2130 for (p = it_props; p->handler; ++p)
2131 values_here[p->idx] = textget (iv->plist, *p->name);
2132
2133 /* Look for an interval following iv that has different
2134 properties. */
2135 for (next_iv = next_interval (iv);
2136 (!NULL_INTERVAL_P (next_iv)
2137 && (NILP (limit)
2138 || XFASTINT (limit) > next_iv->position));
2139 next_iv = next_interval (next_iv))
2140 {
2141 for (p = it_props; p->handler; ++p)
2142 {
2143 Lisp_Object new_value;
2144
2145 new_value = textget (next_iv->plist, *p->name);
2146 if (!EQ (values_here[p->idx], new_value))
2147 break;
2148 }
2149
2150 if (p->handler)
2151 break;
2152 }
2153
2154 if (!NULL_INTERVAL_P (next_iv))
2155 {
2156 if (INTEGERP (limit)
2157 && next_iv->position >= XFASTINT (limit))
2158 /* No text property change up to limit. */
2159 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2160 else
2161 /* Text properties change in next_iv. */
2162 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2163 }
2164 }
2165
2166 xassert (STRINGP (it->string)
2167 || (it->stop_charpos >= BEGV
2168 && it->stop_charpos >= IT_CHARPOS (*it)));
2169}
2170
2171
2172/* Return the position of the next overlay change after POS in
2173 current_buffer. Value is point-max if no overlay change
2174 follows. This is like `next-overlay-change' but doesn't use
2175 xmalloc. */
2176
2177static int
2178next_overlay_change (pos)
2179 int pos;
2180{
2181 int noverlays;
2182 int endpos;
2183 Lisp_Object *overlays;
2184 int len;
2185 int i;
2186
2187 /* Get all overlays at the given position. */
2188 len = 10;
2189 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2190 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2191 if (noverlays > len)
2192 {
2193 len = noverlays;
2194 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2195 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2196 }
2197
2198 /* If any of these overlays ends before endpos,
2199 use its ending point instead. */
2200 for (i = 0; i < noverlays; ++i)
2201 {
2202 Lisp_Object oend;
2203 int oendpos;
2204
2205 oend = OVERLAY_END (overlays[i]);
2206 oendpos = OVERLAY_POSITION (oend);
2207 endpos = min (endpos, oendpos);
2208 }
2209
2210 return endpos;
2211}
2212
2213
2214\f
2215/***********************************************************************
2216 Fontification
2217 ***********************************************************************/
2218
2219/* Handle changes in the `fontified' property of the current buffer by
2220 calling hook functions from Qfontification_functions to fontify
2221 regions of text. */
2222
2223static enum prop_handled
2224handle_fontified_prop (it)
2225 struct it *it;
2226{
2227 Lisp_Object prop, pos;
2228 enum prop_handled handled = HANDLED_NORMALLY;
2229
2230 /* Get the value of the `fontified' property at IT's current buffer
2231 position. (The `fontified' property doesn't have a special
2232 meaning in strings.) If the value is nil, call functions from
2233 Qfontification_functions. */
2234 if (!STRINGP (it->string)
2235 && it->s == NULL
2236 && !NILP (Vfontification_functions)
085536c2 2237 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2238 && (pos = make_number (IT_CHARPOS (*it)),
2239 prop = Fget_char_property (pos, Qfontified, Qnil),
2240 NILP (prop)))
2241 {
2d27dae2 2242 int count = BINDING_STACK_SIZE ();
085536c2
GM
2243 Lisp_Object val;
2244
2245 val = Vfontification_functions;
2246 specbind (Qfontification_functions, Qnil);
085536c2
GM
2247
2248 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2249 safe_call1 (val, pos);
085536c2
GM
2250 else
2251 {
2252 Lisp_Object globals, fn;
2253 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2254
085536c2
GM
2255 globals = Qnil;
2256 GCPRO2 (val, globals);
2257
2258 for (; CONSP (val); val = XCDR (val))
2259 {
2260 fn = XCAR (val);
2261
2262 if (EQ (fn, Qt))
2263 {
2264 /* A value of t indicates this hook has a local
2265 binding; it means to run the global binding too.
2266 In a global value, t should not occur. If it
2267 does, we must ignore it to avoid an endless
2268 loop. */
2269 for (globals = Fdefault_value (Qfontification_functions);
2270 CONSP (globals);
2271 globals = XCDR (globals))
2272 {
2273 fn = XCAR (globals);
2274 if (!EQ (fn, Qt))
116d6f5c 2275 safe_call1 (fn, pos);
085536c2
GM
2276 }
2277 }
2278 else
116d6f5c 2279 safe_call1 (fn, pos);
085536c2
GM
2280 }
2281
2282 UNGCPRO;
2283 }
2284
2285 unbind_to (count, Qnil);
5f5c8ee5
GM
2286
2287 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2288 something. This avoids an endless loop if they failed to
2289 fontify the text for which reason ever. */
2290 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2291 handled = HANDLED_RECOMPUTE_PROPS;
2292 }
2293
2294 return handled;
2295}
2296
2297
2298\f
2299/***********************************************************************
2300 Faces
2301 ***********************************************************************/
2302
2303/* Set up iterator IT from face properties at its current position.
2304 Called from handle_stop. */
2305
2306static enum prop_handled
2307handle_face_prop (it)
2308 struct it *it;
2309{
2310 int new_face_id, next_stop;
2311
2312 if (!STRINGP (it->string))
2313 {
2314 new_face_id
2315 = face_at_buffer_position (it->w,
2316 IT_CHARPOS (*it),
2317 it->region_beg_charpos,
2318 it->region_end_charpos,
2319 &next_stop,
2320 (IT_CHARPOS (*it)
2321 + TEXT_PROP_DISTANCE_LIMIT),
2322 0);
2323
2324 /* Is this a start of a run of characters with box face?
2325 Caveat: this can be called for a freshly initialized
4b41cebb 2326 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2327 face will not change until limit, i.e. if the new face has a
2328 box, all characters up to limit will have one. But, as
2329 usual, we don't know whether limit is really the end. */
2330 if (new_face_id != it->face_id)
2331 {
2332 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2333
2334 /* If new face has a box but old face has not, this is
2335 the start of a run of characters with box, i.e. it has
2336 a shadow on the left side. The value of face_id of the
2337 iterator will be -1 if this is the initial call that gets
2338 the face. In this case, we have to look in front of IT's
2339 position and see whether there is a face != new_face_id. */
2340 it->start_of_box_run_p
2341 = (new_face->box != FACE_NO_BOX
2342 && (it->face_id >= 0
2343 || IT_CHARPOS (*it) == BEG
2344 || new_face_id != face_before_it_pos (it)));
2345 it->face_box_p = new_face->box != FACE_NO_BOX;
2346 }
2347 }
2348 else
2349 {
06a12811
GM
2350 int base_face_id, bufpos;
2351
2352 if (it->current.overlay_string_index >= 0)
2353 bufpos = IT_CHARPOS (*it);
2354 else
2355 bufpos = 0;
2356
2357 /* For strings from a buffer, i.e. overlay strings or strings
2358 from a `display' property, use the face at IT's current
2359 buffer position as the base face to merge with, so that
2360 overlay strings appear in the same face as surrounding
2361 text, unless they specify their own faces. */
2362 base_face_id = underlying_face_id (it);
2363
2364 new_face_id = face_at_string_position (it->w,
2365 it->string,
2366 IT_STRING_CHARPOS (*it),
2367 bufpos,
2368 it->region_beg_charpos,
2369 it->region_end_charpos,
2370 &next_stop,
5de7c6f2 2371 base_face_id, 0);
5f5c8ee5
GM
2372
2373#if 0 /* This shouldn't be neccessary. Let's check it. */
2374 /* If IT is used to display a mode line we would really like to
2375 use the mode line face instead of the frame's default face. */
2376 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2377 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2378 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5
GM
2379#endif
2380
2381 /* Is this a start of a run of characters with box? Caveat:
2382 this can be called for a freshly allocated iterator; face_id
2383 is -1 is this case. We know that the new face will not
2384 change until the next check pos, i.e. if the new face has a
2385 box, all characters up to that position will have a
2386 box. But, as usual, we don't know whether that position
2387 is really the end. */
2388 if (new_face_id != it->face_id)
2389 {
2390 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2391 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2392
2393 /* If new face has a box but old face hasn't, this is the
2394 start of a run of characters with box, i.e. it has a
2395 shadow on the left side. */
2396 it->start_of_box_run_p
2397 = new_face->box && (old_face == NULL || !old_face->box);
2398 it->face_box_p = new_face->box != FACE_NO_BOX;
2399 }
2400 }
2401
2402 it->face_id = new_face_id;
5f5c8ee5
GM
2403 return HANDLED_NORMALLY;
2404}
2405
2406
06a12811
GM
2407/* Return the ID of the face ``underlying'' IT's current position,
2408 which is in a string. If the iterator is associated with a
2409 buffer, return the face at IT's current buffer position.
2410 Otherwise, use the iterator's base_face_id. */
2411
2412static int
2413underlying_face_id (it)
2414 struct it *it;
2415{
2416 int face_id = it->base_face_id, i;
2417
2418 xassert (STRINGP (it->string));
2419
2420 for (i = it->sp - 1; i >= 0; --i)
2421 if (NILP (it->stack[i].string))
2422 face_id = it->stack[i].face_id;
2423
2424 return face_id;
2425}
2426
2427
5f5c8ee5
GM
2428/* Compute the face one character before or after the current position
2429 of IT. BEFORE_P non-zero means get the face in front of IT's
2430 position. Value is the id of the face. */
2431
2432static int
2433face_before_or_after_it_pos (it, before_p)
2434 struct it *it;
2435 int before_p;
2436{
2437 int face_id, limit;
2438 int next_check_charpos;
2439 struct text_pos pos;
2440
2441 xassert (it->s == NULL);
2442
2443 if (STRINGP (it->string))
2444 {
06a12811
GM
2445 int bufpos, base_face_id;
2446
5f5c8ee5
GM
2447 /* No face change past the end of the string (for the case
2448 we are padding with spaces). No face change before the
2449 string start. */
2051c264 2450 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2451 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2452 return it->face_id;
2453
2454 /* Set pos to the position before or after IT's current position. */
2455 if (before_p)
2456 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2457 else
260a86a0
KH
2458 /* For composition, we must check the character after the
2459 composition. */
2460 pos = (it->what == IT_COMPOSITION
2461 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2462 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2463
06a12811
GM
2464 if (it->current.overlay_string_index >= 0)
2465 bufpos = IT_CHARPOS (*it);
2466 else
2467 bufpos = 0;
2468
2469 base_face_id = underlying_face_id (it);
2470
5f5c8ee5 2471 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2472 face_id = face_at_string_position (it->w,
2473 it->string,
2474 CHARPOS (pos),
2475 bufpos,
2476 it->region_beg_charpos,
2477 it->region_end_charpos,
2478 &next_check_charpos,
5de7c6f2 2479 base_face_id, 0);
5f5c8ee5
GM
2480
2481 /* Correct the face for charsets different from ASCII. Do it
2482 for the multibyte case only. The face returned above is
2483 suitable for unibyte text if IT->string is unibyte. */
2484 if (STRING_MULTIBYTE (it->string))
2485 {
2051c264
GM
2486 unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2487 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
2488 int c, len;
2489 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 2490
4fdb80f2 2491 c = string_char_and_length (p, rest, &len);
980806b6 2492 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2493 }
2494 }
2495 else
2496 {
70851746
GM
2497 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2498 || (IT_CHARPOS (*it) <= BEGV && before_p))
2499 return it->face_id;
2500
5f5c8ee5
GM
2501 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2502 pos = it->current.pos;
2503
2504 if (before_p)
c1005d06 2505 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2506 else
260a86a0
KH
2507 {
2508 if (it->what == IT_COMPOSITION)
2509 /* For composition, we must check the position after the
2510 composition. */
2511 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2512 else
c1005d06 2513 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2514 }
06a12811 2515
5f5c8ee5
GM
2516 /* Determine face for CHARSET_ASCII, or unibyte. */
2517 face_id = face_at_buffer_position (it->w,
2518 CHARPOS (pos),
2519 it->region_beg_charpos,
2520 it->region_end_charpos,
2521 &next_check_charpos,
2522 limit, 0);
2523
2524 /* Correct the face for charsets different from ASCII. Do it
2525 for the multibyte case only. The face returned above is
2526 suitable for unibyte text if current_buffer is unibyte. */
2527 if (it->multibyte_p)
2528 {
980806b6
KH
2529 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2530 struct face *face = FACE_FROM_ID (it->f, face_id);
2531 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2532 }
2533 }
2534
2535 return face_id;
2536}
2537
2538
2539\f
2540/***********************************************************************
2541 Invisible text
2542 ***********************************************************************/
2543
2544/* Set up iterator IT from invisible properties at its current
2545 position. Called from handle_stop. */
2546
2547static enum prop_handled
2548handle_invisible_prop (it)
2549 struct it *it;
2550{
2551 enum prop_handled handled = HANDLED_NORMALLY;
2552
2553 if (STRINGP (it->string))
2554 {
2555 extern Lisp_Object Qinvisible;
2556 Lisp_Object prop, end_charpos, limit, charpos;
2557
2558 /* Get the value of the invisible text property at the
2559 current position. Value will be nil if there is no such
2560 property. */
ac90c44f 2561 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2562 prop = Fget_text_property (charpos, Qinvisible, it->string);
2563
eadc0bf8
GM
2564 if (!NILP (prop)
2565 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2566 {
2567 handled = HANDLED_RECOMPUTE_PROPS;
2568
2569 /* Get the position at which the next change of the
2570 invisible text property can be found in IT->string.
2571 Value will be nil if the property value is the same for
2572 all the rest of IT->string. */
2051c264 2573 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 2574 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 2575 it->string, limit);
5f5c8ee5
GM
2576
2577 /* Text at current position is invisible. The next
2578 change in the property is at position end_charpos.
2579 Move IT's current position to that position. */
2580 if (INTEGERP (end_charpos)
2581 && XFASTINT (end_charpos) < XFASTINT (limit))
2582 {
2583 struct text_pos old;
2584 old = it->current.string_pos;
2585 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2586 compute_string_pos (&it->current.string_pos, old, it->string);
2587 }
2588 else
2589 {
2590 /* The rest of the string is invisible. If this is an
2591 overlay string, proceed with the next overlay string
2592 or whatever comes and return a character from there. */
2593 if (it->current.overlay_string_index >= 0)
2594 {
2595 next_overlay_string (it);
2596 /* Don't check for overlay strings when we just
2597 finished processing them. */
2598 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2599 }
2600 else
2601 {
2051c264
GM
2602 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2603 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
2604 }
2605 }
2606 }
2607 }
2608 else
2609 {
8580a4e3 2610 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 2611 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
2612
2613 /* First of all, is there invisible text at this position? */
5a08cbaf 2614 start_charpos = IT_CHARPOS (*it);
ac90c44f 2615 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
2616 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2617 &overlay);
8580a4e3
SM
2618 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2619
5f5c8ee5 2620 /* If we are on invisible text, skip over it. */
8580a4e3 2621 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2622 {
2623 /* Record whether we have to display an ellipsis for the
2624 invisible text. */
8580a4e3 2625 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
2626
2627 handled = HANDLED_RECOMPUTE_PROPS;
2628
2629 /* Loop skipping over invisible text. The loop is left at
2630 ZV or with IT on the first char being visible again. */
2631 do
2632 {
2633 /* Try to skip some invisible text. Return value is the
2634 position reached which can be equal to IT's position
2635 if there is nothing invisible here. This skips both
2636 over invisible text properties and overlays with
2637 invisible property. */
2638 newpos = skip_invisible (IT_CHARPOS (*it),
2639 &next_stop, ZV, it->window);
2640
2641 /* If we skipped nothing at all we weren't at invisible
2642 text in the first place. If everything to the end of
2643 the buffer was skipped, end the loop. */
2644 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 2645 invis_p = 0;
5f5c8ee5
GM
2646 else
2647 {
2648 /* We skipped some characters but not necessarily
2649 all there are. Check if we ended up on visible
2650 text. Fget_char_property returns the property of
2651 the char before the given position, i.e. if we
8580a4e3 2652 get invis_p = 0, this means that the char at
5f5c8ee5 2653 newpos is visible. */
ac90c44f 2654 pos = make_number (newpos);
5f5c8ee5 2655 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 2656 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5
GM
2657 }
2658
2659 /* If we ended up on invisible text, proceed to
2660 skip starting with next_stop. */
8580a4e3 2661 if (invis_p)
5f5c8ee5
GM
2662 IT_CHARPOS (*it) = next_stop;
2663 }
8580a4e3 2664 while (invis_p);
5a08cbaf 2665
5f5c8ee5
GM
2666 /* The position newpos is now either ZV or on visible text. */
2667 IT_CHARPOS (*it) = newpos;
2668 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2669
5a08cbaf
GM
2670 /* If there are before-strings at the start of invisible
2671 text, and the text is invisible because of a text
2672 property, arrange to show before-strings because 20.x did
2673 it that way. (If the text is invisible because of an
2674 overlay property instead of a text property, this is
2675 already handled in the overlay code.) */
2676 if (NILP (overlay)
2677 && get_overlay_strings (it, start_charpos))
5f5c8ee5 2678 {
5a08cbaf
GM
2679 handled = HANDLED_RECOMPUTE_PROPS;
2680 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 2681 }
5a08cbaf
GM
2682 else if (display_ellipsis_p)
2683 setup_for_ellipsis (it);
5f5c8ee5
GM
2684 }
2685 }
2686
2687 return handled;
2688}
2689
2690
5a08cbaf
GM
2691/* Make iterator IT return `...' next. */
2692
2693static void
2694setup_for_ellipsis (it)
2695 struct it *it;
2696{
2697 if (it->dp
2698 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2699 {
2700 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2701 it->dpvec = v->contents;
2702 it->dpend = v->contents + v->size;
2703 }
2704 else
2705 {
2706 /* Default `...'. */
2707 it->dpvec = default_invis_vector;
2708 it->dpend = default_invis_vector + 3;
2709 }
2710
2711 /* The ellipsis display does not replace the display of the
2712 character at the new position. Indicate this by setting
2713 IT->dpvec_char_len to zero. */
2714 it->dpvec_char_len = 0;
2715
2716 it->current.dpvec_index = 0;
2717 it->method = next_element_from_display_vector;
2718}
2719
2720
5f5c8ee5
GM
2721\f
2722/***********************************************************************
2723 'display' property
2724 ***********************************************************************/
2725
2726/* Set up iterator IT from `display' property at its current position.
2727 Called from handle_stop. */
2728
2729static enum prop_handled
2730handle_display_prop (it)
2731 struct it *it;
2732{
2733 Lisp_Object prop, object;
2734 struct text_pos *position;
a61b7058 2735 int display_replaced_p = 0;
5f5c8ee5
GM
2736
2737 if (STRINGP (it->string))
2738 {
2739 object = it->string;
2740 position = &it->current.string_pos;
2741 }
2742 else
2743 {
221dd3e7 2744 object = it->w->buffer;
5f5c8ee5
GM
2745 position = &it->current.pos;
2746 }
2747
2748 /* Reset those iterator values set from display property values. */
2749 it->font_height = Qnil;
2750 it->space_width = Qnil;
2751 it->voffset = 0;
2752
2753 /* We don't support recursive `display' properties, i.e. string
2754 values that have a string `display' property, that have a string
2755 `display' property etc. */
2756 if (!it->string_from_display_prop_p)
2757 it->area = TEXT_AREA;
2758
2759 prop = Fget_char_property (make_number (position->charpos),
2760 Qdisplay, object);
2761 if (NILP (prop))
2762 return HANDLED_NORMALLY;
2763
f3751a65 2764 if (CONSP (prop)
12700f40
GM
2765 /* Simple properties. */
2766 && !EQ (XCAR (prop), Qimage)
2767 && !EQ (XCAR (prop), Qspace)
2768 && !EQ (XCAR (prop), Qwhen)
2769 && !EQ (XCAR (prop), Qspace_width)
2770 && !EQ (XCAR (prop), Qheight)
2771 && !EQ (XCAR (prop), Qraise)
2772 /* Marginal area specifications. */
2773 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2774 && !NILP (XCAR (prop)))
5f5c8ee5 2775 {
a61b7058 2776 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 2777 {
a61b7058
GM
2778 if (handle_single_display_prop (it, XCAR (prop), object,
2779 position, display_replaced_p))
2780 display_replaced_p = 1;
5f5c8ee5
GM
2781 }
2782 }
2783 else if (VECTORP (prop))
2784 {
2785 int i;
a61b7058
GM
2786 for (i = 0; i < ASIZE (prop); ++i)
2787 if (handle_single_display_prop (it, AREF (prop, i), object,
2788 position, display_replaced_p))
2789 display_replaced_p = 1;
5f5c8ee5
GM
2790 }
2791 else
2792 {
a61b7058
GM
2793 if (handle_single_display_prop (it, prop, object, position, 0))
2794 display_replaced_p = 1;
5f5c8ee5
GM
2795 }
2796
a61b7058 2797 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
2798}
2799
2800
6c577098 2801/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2802 at START_POS in OBJECT. */
2803
2804static struct text_pos
2805display_prop_end (it, object, start_pos)
2806 struct it *it;
2807 Lisp_Object object;
2808 struct text_pos start_pos;
2809{
2810 Lisp_Object end;
2811 struct text_pos end_pos;
5f5c8ee5 2812
016b5642
MB
2813 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2814 Qdisplay, object, Qnil);
6c577098
GM
2815 CHARPOS (end_pos) = XFASTINT (end);
2816 if (STRINGP (object))
5f5c8ee5
GM
2817 compute_string_pos (&end_pos, start_pos, it->string);
2818 else
6c577098 2819 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2820
2821 return end_pos;
2822}
2823
2824
2825/* Set up IT from a single `display' sub-property value PROP. OBJECT
2826 is the object in which the `display' property was found. *POSITION
a61b7058
GM
2827 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2828 means that we previously saw a display sub-property which already
2829 replaced text display with something else, for example an image;
2830 ignore such properties after the first one has been processed.
5f5c8ee5
GM
2831
2832 If PROP is a `space' or `image' sub-property, set *POSITION to the
2833 end position of the `display' property.
2834
4b41cebb 2835 Value is non-zero if something was found which replaces the display
a61b7058 2836 of buffer or string text. */
5f5c8ee5
GM
2837
2838static int
a61b7058
GM
2839handle_single_display_prop (it, prop, object, position,
2840 display_replaced_before_p)
5f5c8ee5
GM
2841 struct it *it;
2842 Lisp_Object prop;
2843 Lisp_Object object;
2844 struct text_pos *position;
a61b7058 2845 int display_replaced_before_p;
5f5c8ee5
GM
2846{
2847 Lisp_Object value;
a61b7058 2848 int replaces_text_display_p = 0;
5f5c8ee5
GM
2849 Lisp_Object form;
2850
d3acf96b 2851 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 2852 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2853 form = Qt;
d3acf96b 2854 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2855 {
2856 prop = XCDR (prop);
2857 if (!CONSP (prop))
2858 return 0;
2859 form = XCAR (prop);
2860 prop = XCDR (prop);
5f5c8ee5
GM
2861 }
2862
2863 if (!NILP (form) && !EQ (form, Qt))
2864 {
b384d6f8 2865 int count = BINDING_STACK_SIZE ();
5f5c8ee5 2866 struct gcpro gcpro1;
5f5c8ee5 2867
b384d6f8
GM
2868 /* Bind `object' to the object having the `display' property, a
2869 buffer or string. Bind `position' to the position in the
2870 object where the property was found, and `buffer-position'
2871 to the current position in the buffer. */
2872 specbind (Qobject, object);
31ac723b
SM
2873 specbind (Qposition, make_number (CHARPOS (*position)));
2874 specbind (Qbuffer_position,
2875 make_number (STRINGP (object)
2876 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 2877 GCPRO1 (form);
116d6f5c 2878 form = safe_eval (form);
b384d6f8
GM
2879 UNGCPRO;
2880 unbind_to (count, Qnil);
5f5c8ee5
GM
2881 }
2882
2883 if (NILP (form))
2884 return 0;
2885
2886 if (CONSP (prop)
2887 && EQ (XCAR (prop), Qheight)
2888 && CONSP (XCDR (prop)))
2889 {
e4093f38 2890 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5
GM
2891 return 0;
2892
2893 /* `(height HEIGHT)'. */
2894 it->font_height = XCAR (XCDR (prop));
2895 if (!NILP (it->font_height))
2896 {
2897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2898 int new_height = -1;
2899
2900 if (CONSP (it->font_height)
2901 && (EQ (XCAR (it->font_height), Qplus)
2902 || EQ (XCAR (it->font_height), Qminus))
2903 && CONSP (XCDR (it->font_height))
2904 && INTEGERP (XCAR (XCDR (it->font_height))))
2905 {
2906 /* `(+ N)' or `(- N)' where N is an integer. */
2907 int steps = XINT (XCAR (XCDR (it->font_height)));
2908 if (EQ (XCAR (it->font_height), Qplus))
2909 steps = - steps;
2910 it->face_id = smaller_face (it->f, it->face_id, steps);
2911 }
0d8b31c0 2912 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2913 {
2914 /* Call function with current height as argument.
2915 Value is the new height. */
116d6f5c
GM
2916 Lisp_Object height;
2917 height = safe_call1 (it->font_height,
2918 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2919 if (NUMBERP (height))
2920 new_height = XFLOATINT (height);
5f5c8ee5
GM
2921 }
2922 else if (NUMBERP (it->font_height))
2923 {
2924 /* Value is a multiple of the canonical char height. */
2925 struct face *face;
2926
2927 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2928 new_height = (XFLOATINT (it->font_height)
2929 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2930 }
2931 else
2932 {
2933 /* Evaluate IT->font_height with `height' bound to the
2934 current specified height to get the new height. */
2935 Lisp_Object value;
2d27dae2 2936 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
2937
2938 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2939 value = safe_eval (it->font_height);
5f5c8ee5
GM
2940 unbind_to (count, Qnil);
2941
2942 if (NUMBERP (value))
2943 new_height = XFLOATINT (value);
2944 }
2945
2946 if (new_height > 0)
2947 it->face_id = face_with_height (it->f, it->face_id, new_height);
2948 }
2949 }
2950 else if (CONSP (prop)
2951 && EQ (XCAR (prop), Qspace_width)
2952 && CONSP (XCDR (prop)))
2953 {
2954 /* `(space_width WIDTH)'. */
e4093f38 2955 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2956 return 0;
5f5c8ee5
GM
2957
2958 value = XCAR (XCDR (prop));
2959 if (NUMBERP (value) && XFLOATINT (value) > 0)
2960 it->space_width = value;
2961 }
2962 else if (CONSP (prop)
2963 && EQ (XCAR (prop), Qraise)
2964 && CONSP (XCDR (prop)))
2965 {
5f5c8ee5 2966 /* `(raise FACTOR)'. */
e4093f38 2967 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2968 return 0;
5f5c8ee5 2969
fb3842a8 2970#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2971 value = XCAR (XCDR (prop));
2972 if (NUMBERP (value))
2973 {
2974 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2975 it->voffset = - (XFLOATINT (value)
1ab3e082 2976 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2977 }
2978#endif /* HAVE_WINDOW_SYSTEM */
2979 }
2980 else if (!it->string_from_display_prop_p)
2981 {
f3751a65 2982 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 2983 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2984 Lisp_Object location, value;
2985 struct text_pos start_pos;
2986 int valid_p;
2987
2988 /* Characters having this form of property are not displayed, so
2989 we have to find the end of the property. */
5f5c8ee5
GM
2990 start_pos = *position;
2991 *position = display_prop_end (it, object, start_pos);
15e26c76 2992 value = Qnil;
5f5c8ee5
GM
2993
2994 /* Let's stop at the new position and assume that all
2995 text properties change there. */
2996 it->stop_charpos = position->charpos;
2997
f3751a65
GM
2998 location = Qunbound;
2999 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3000 {
f3751a65
GM
3001 Lisp_Object tem;
3002
5f5c8ee5 3003 value = XCDR (prop);
f3751a65
GM
3004 if (CONSP (value))
3005 value = XCAR (value);
3006
3007 tem = XCAR (prop);
3008 if (EQ (XCAR (tem), Qmargin)
3009 && (tem = XCDR (tem),
3010 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3011 (NILP (tem)
3012 || EQ (tem, Qleft_margin)
3013 || EQ (tem, Qright_margin))))
3014 location = tem;
5f5c8ee5 3015 }
f3751a65
GM
3016
3017 if (EQ (location, Qunbound))
5f5c8ee5
GM
3018 {
3019 location = Qnil;
3020 value = prop;
3021 }
3022
3023#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 3024 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
3025 valid_p = STRINGP (value);
3026 else
3027 valid_p = (STRINGP (value)
3028 || (CONSP (value) && EQ (XCAR (value), Qspace))
3029 || valid_image_p (value));
3030#else /* not HAVE_WINDOW_SYSTEM */
3031 valid_p = STRINGP (value);
3032#endif /* not HAVE_WINDOW_SYSTEM */
3033
3034 if ((EQ (location, Qleft_margin)
3035 || EQ (location, Qright_margin)
3036 || NILP (location))
a61b7058
GM
3037 && valid_p
3038 && !display_replaced_before_p)
5f5c8ee5 3039 {
a61b7058 3040 replaces_text_display_p = 1;
a21a3943 3041
5f5c8ee5
GM
3042 /* Save current settings of IT so that we can restore them
3043 when we are finished with the glyph property value. */
3044 push_it (it);
3045
3046 if (NILP (location))
3047 it->area = TEXT_AREA;
3048 else if (EQ (location, Qleft_margin))
3049 it->area = LEFT_MARGIN_AREA;
3050 else
3051 it->area = RIGHT_MARGIN_AREA;
3052
3053 if (STRINGP (value))
3054 {
3055 it->string = value;
3056 it->multibyte_p = STRING_MULTIBYTE (it->string);
3057 it->current.overlay_string_index = -1;
3058 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3059 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3060 it->method = next_element_from_string;
3061 it->stop_charpos = 0;
3062 it->string_from_display_prop_p = 1;
e187cf71
GM
3063 /* Say that we haven't consumed the characters with
3064 `display' property yet. The call to pop_it in
3065 set_iterator_to_next will clean this up. */
3066 *position = start_pos;
5f5c8ee5
GM
3067 }
3068 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3069 {
3070 it->method = next_element_from_stretch;
3071 it->object = value;
3072 it->current.pos = it->position = start_pos;
3073 }
3074#ifdef HAVE_WINDOW_SYSTEM
3075 else
3076 {
3077 it->what = IT_IMAGE;
3078 it->image_id = lookup_image (it->f, value);
3079 it->position = start_pos;
3080 it->object = NILP (object) ? it->w->buffer : object;
3081 it->method = next_element_from_image;
3082
02513cdd 3083 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3084 `display' property yet. The call to pop_it in
3085 set_iterator_to_next will clean this up. */
3086 *position = start_pos;
3087 }
3088#endif /* HAVE_WINDOW_SYSTEM */
3089 }
a21a3943
GM
3090 else
3091 /* Invalid property or property not supported. Restore
3092 the position to what it was before. */
3093 *position = start_pos;
5f5c8ee5
GM
3094 }
3095
a61b7058 3096 return replaces_text_display_p;
5f5c8ee5
GM
3097}
3098
3099
06568bbf
GM
3100/* Check if PROP is a display sub-property value whose text should be
3101 treated as intangible. */
3102
3103static int
3104single_display_prop_intangible_p (prop)
3105 Lisp_Object prop;
3106{
3107 /* Skip over `when FORM'. */
3108 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3109 {
3110 prop = XCDR (prop);
3111 if (!CONSP (prop))
3112 return 0;
3113 prop = XCDR (prop);
3114 }
3115
3116 if (!CONSP (prop))
3117 return 0;
3118
3119 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3120 we don't need to treat text as intangible. */
3121 if (EQ (XCAR (prop), Qmargin))
3122 {
3123 prop = XCDR (prop);
3124 if (!CONSP (prop))
3125 return 0;
3126
3127 prop = XCDR (prop);
3128 if (!CONSP (prop)
3129 || EQ (XCAR (prop), Qleft_margin)
3130 || EQ (XCAR (prop), Qright_margin))
3131 return 0;
3132 }
3133
3134 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3135}
3136
3137
3138/* Check if PROP is a display property value whose text should be
3139 treated as intangible. */
3140
3141int
3142display_prop_intangible_p (prop)
3143 Lisp_Object prop;
3144{
3145 if (CONSP (prop)
3146 && CONSP (XCAR (prop))
3147 && !EQ (Qmargin, XCAR (XCAR (prop))))
3148 {
3149 /* A list of sub-properties. */
3150 while (CONSP (prop))
3151 {
3152 if (single_display_prop_intangible_p (XCAR (prop)))
3153 return 1;
3154 prop = XCDR (prop);
3155 }
3156 }
3157 else if (VECTORP (prop))
3158 {
3159 /* A vector of sub-properties. */
3160 int i;
a61b7058
GM
3161 for (i = 0; i < ASIZE (prop); ++i)
3162 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3163 return 1;
3164 }
3165 else
3166 return single_display_prop_intangible_p (prop);
3167
3168 return 0;
3169}
3170
74bd6d65
GM
3171
3172/* Return 1 if PROP is a display sub-property value containing STRING. */
3173
3174static int
3175single_display_prop_string_p (prop, string)
3176 Lisp_Object prop, string;
3177{
74bd6d65
GM
3178 if (EQ (string, prop))
3179 return 1;
3180
3181 /* Skip over `when FORM'. */
3182 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3183 {
3184 prop = XCDR (prop);
3185 if (!CONSP (prop))
3186 return 0;
3187 prop = XCDR (prop);
3188 }
3189
3190 if (CONSP (prop))
3191 /* Skip over `margin LOCATION'. */
3192 if (EQ (XCAR (prop), Qmargin))
3193 {
3194 prop = XCDR (prop);
3195 if (!CONSP (prop))
3196 return 0;
3197
3198 prop = XCDR (prop);
3199 if (!CONSP (prop))
3200 return 0;
3201 }
3202
3203 return CONSP (prop) && EQ (XCAR (prop), string);
3204}
3205
3206
3207/* Return 1 if STRING appears in the `display' property PROP. */
3208
3209static int
3210display_prop_string_p (prop, string)
3211 Lisp_Object prop, string;
3212{
74bd6d65
GM
3213 if (CONSP (prop)
3214 && CONSP (XCAR (prop))
3215 && !EQ (Qmargin, XCAR (XCAR (prop))))
3216 {
3217 /* A list of sub-properties. */
3218 while (CONSP (prop))
3219 {
3220 if (single_display_prop_string_p (XCAR (prop), string))
3221 return 1;
3222 prop = XCDR (prop);
3223 }
3224 }
3225 else if (VECTORP (prop))
3226 {
3227 /* A vector of sub-properties. */
3228 int i;
3229 for (i = 0; i < ASIZE (prop); ++i)
3230 if (single_display_prop_string_p (AREF (prop, i), string))
3231 return 1;
3232 }
3233 else
3234 return single_display_prop_string_p (prop, string);
3235
3236 return 0;
3237}
3238
3239
3240/* Determine from which buffer position in W's buffer STRING comes
3241 from. AROUND_CHARPOS is an approximate position where it could
3242 be from. Value is the buffer position or 0 if it couldn't be
3243 determined.
3244
3245 W's buffer must be current.
3246
3247 This function is necessary because we don't record buffer positions
3248 in glyphs generated from strings (to keep struct glyph small).
3249 This function may only use code that doesn't eval because it is
3250 called asynchronously from note_mouse_highlight. */
3251
3252int
3253string_buffer_position (w, string, around_charpos)
3254 struct window *w;
3255 Lisp_Object string;
3256 int around_charpos;
3257{
74bd6d65
GM
3258 Lisp_Object limit, prop, pos;
3259 const int MAX_DISTANCE = 1000;
3260 int found = 0;
3261
d8731202 3262 pos = make_number (around_charpos);
74bd6d65
GM
3263 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3264 while (!found && !EQ (pos, limit))
3265 {
3266 prop = Fget_char_property (pos, Qdisplay, Qnil);
3267 if (!NILP (prop) && display_prop_string_p (prop, string))
3268 found = 1;
3269 else
134d9283 3270 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3271 }
3272
3273 if (!found)
3274 {
d8731202 3275 pos = make_number (around_charpos);
74bd6d65
GM
3276 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3277 while (!found && !EQ (pos, limit))
3278 {
3279 prop = Fget_char_property (pos, Qdisplay, Qnil);
3280 if (!NILP (prop) && display_prop_string_p (prop, string))
3281 found = 1;
3282 else
134d9283
GM
3283 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3284 limit);
74bd6d65
GM
3285 }
3286 }
3287
3288 return found ? XINT (pos) : 0;
3289}
3290
3291
5f5c8ee5 3292\f
260a86a0
KH
3293/***********************************************************************
3294 `composition' property
3295 ***********************************************************************/
3296
3297/* Set up iterator IT from `composition' property at its current
3298 position. Called from handle_stop. */
3299
3300static enum prop_handled
3301handle_composition_prop (it)
3302 struct it *it;
3303{
3304 Lisp_Object prop, string;
3305 int pos, pos_byte, end;
3306 enum prop_handled handled = HANDLED_NORMALLY;
3307
3308 if (STRINGP (it->string))
3309 {
3310 pos = IT_STRING_CHARPOS (*it);
3311 pos_byte = IT_STRING_BYTEPOS (*it);
3312 string = it->string;
3313 }
3314 else
3315 {
3316 pos = IT_CHARPOS (*it);
3317 pos_byte = IT_BYTEPOS (*it);
3318 string = Qnil;
3319 }
3320
3321 /* If there's a valid composition and point is not inside of the
3322 composition (in the case that the composition is from the current
3323 buffer), draw a glyph composed from the composition components. */
3324 if (find_composition (pos, -1, &pos, &end, &prop, string)
3325 && COMPOSITION_VALID_P (pos, end, prop)
3326 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3327 {
3328 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3329
3330 if (id >= 0)
3331 {
3332 it->method = next_element_from_composition;
3333 it->cmp_id = id;
3334 it->cmp_len = COMPOSITION_LENGTH (prop);
3335 /* For a terminal, draw only the first character of the
3336 components. */
3337 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3338 it->len = (STRINGP (it->string)
3339 ? string_char_to_byte (it->string, end)
3340 : CHAR_TO_BYTE (end)) - pos_byte;
3341 it->stop_charpos = end;
3342 handled = HANDLED_RETURN;
3343 }
3344 }
3345
3346 return handled;
3347}
3348
3349
3350\f
5f5c8ee5
GM
3351/***********************************************************************
3352 Overlay strings
3353 ***********************************************************************/
3354
3355/* The following structure is used to record overlay strings for
3356 later sorting in load_overlay_strings. */
3357
3358struct overlay_entry
3359{
2970b9be 3360 Lisp_Object overlay;
5f5c8ee5
GM
3361 Lisp_Object string;
3362 int priority;
3363 int after_string_p;
3364};
3365
3366
3367/* Set up iterator IT from overlay strings at its current position.
3368 Called from handle_stop. */
3369
3370static enum prop_handled
3371handle_overlay_change (it)
3372 struct it *it;
3373{
5a08cbaf 3374 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3375 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3376 else
2970b9be 3377 return HANDLED_NORMALLY;
5f5c8ee5
GM
3378}
3379
3380
3381/* Set up the next overlay string for delivery by IT, if there is an
3382 overlay string to deliver. Called by set_iterator_to_next when the
3383 end of the current overlay string is reached. If there are more
3384 overlay strings to display, IT->string and
3385 IT->current.overlay_string_index are set appropriately here.
3386 Otherwise IT->string is set to nil. */
3387
3388static void
3389next_overlay_string (it)
3390 struct it *it;
3391{
3392 ++it->current.overlay_string_index;
3393 if (it->current.overlay_string_index == it->n_overlay_strings)
3394 {
3395 /* No more overlay strings. Restore IT's settings to what
3396 they were before overlay strings were processed, and
3397 continue to deliver from current_buffer. */
5a08cbaf
GM
3398 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3399
5f5c8ee5
GM
3400 pop_it (it);
3401 xassert (it->stop_charpos >= BEGV
3402 && it->stop_charpos <= it->end_charpos);
3403 it->string = Qnil;
3404 it->current.overlay_string_index = -1;
3405 SET_TEXT_POS (it->current.string_pos, -1, -1);
3406 it->n_overlay_strings = 0;
3407 it->method = next_element_from_buffer;
2970b9be
GM
3408
3409 /* If we're at the end of the buffer, record that we have
3410 processed the overlay strings there already, so that
3411 next_element_from_buffer doesn't try it again. */
3412 if (IT_CHARPOS (*it) >= it->end_charpos)
3413 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
3414
3415 /* If we have to display `...' for invisible text, set
3416 the iterator up for that. */
3417 if (display_ellipsis_p)
3418 setup_for_ellipsis (it);
5f5c8ee5
GM
3419 }
3420 else
3421 {
3422 /* There are more overlay strings to process. If
3423 IT->current.overlay_string_index has advanced to a position
3424 where we must load IT->overlay_strings with more strings, do
3425 it. */
3426 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3427
3428 if (it->current.overlay_string_index && i == 0)
5a08cbaf 3429 load_overlay_strings (it, 0);
5f5c8ee5
GM
3430
3431 /* Initialize IT to deliver display elements from the overlay
3432 string. */
3433 it->string = it->overlay_strings[i];
3434 it->multibyte_p = STRING_MULTIBYTE (it->string);
3435 SET_TEXT_POS (it->current.string_pos, 0, 0);
3436 it->method = next_element_from_string;
3437 it->stop_charpos = 0;
3438 }
3439
3440 CHECK_IT (it);
3441}
3442
3443
3444/* Compare two overlay_entry structures E1 and E2. Used as a
3445 comparison function for qsort in load_overlay_strings. Overlay
3446 strings for the same position are sorted so that
3447
2970b9be
GM
3448 1. All after-strings come in front of before-strings, except
3449 when they come from the same overlay.
5f5c8ee5
GM
3450
3451 2. Within after-strings, strings are sorted so that overlay strings
3452 from overlays with higher priorities come first.
3453
3454 2. Within before-strings, strings are sorted so that overlay
3455 strings from overlays with higher priorities come last.
3456
3457 Value is analogous to strcmp. */
3458
3459
3460static int
3461compare_overlay_entries (e1, e2)
3462 void *e1, *e2;
3463{
3464 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3465 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3466 int result;
3467
3468 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3469 {
3470 /* Let after-strings appear in front of before-strings if
3471 they come from different overlays. */
3472 if (EQ (entry1->overlay, entry2->overlay))
3473 result = entry1->after_string_p ? 1 : -1;
3474 else
3475 result = entry1->after_string_p ? -1 : 1;
3476 }
5f5c8ee5
GM
3477 else if (entry1->after_string_p)
3478 /* After-strings sorted in order of decreasing priority. */
3479 result = entry2->priority - entry1->priority;
3480 else
3481 /* Before-strings sorted in order of increasing priority. */
3482 result = entry1->priority - entry2->priority;
3483
3484 return result;
3485}
3486
3487
3488/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
3489 current buffer position, or from CHARPOS if that is > 0. Set
3490 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
3491
3492 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3493 a time. On entry into load_overlay_strings,
3494 IT->current.overlay_string_index gives the number of overlay
3495 strings that have already been loaded by previous calls to this
3496 function.
3497
2970b9be
GM
3498 IT->add_overlay_start contains an additional overlay start
3499 position to consider for taking overlay strings from, if non-zero.
3500 This position comes into play when the overlay has an `invisible'
3501 property, and both before and after-strings. When we've skipped to
3502 the end of the overlay, because of its `invisible' property, we
3503 nevertheless want its before-string to appear.
3504 IT->add_overlay_start will contain the overlay start position
3505 in this case.
3506
5f5c8ee5
GM
3507 Overlay strings are sorted so that after-string strings come in
3508 front of before-string strings. Within before and after-strings,
3509 strings are sorted by overlay priority. See also function
3510 compare_overlay_entries. */
3511
3512static void
5a08cbaf 3513load_overlay_strings (it, charpos)
5f5c8ee5 3514 struct it *it;
5a08cbaf 3515 int charpos;
5f5c8ee5
GM
3516{
3517 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3518 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3519 int start, end;
3520 int size = 20;
cafafe0b 3521 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3522 struct overlay_entry *entries
3523 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
3524
3525 if (charpos <= 0)
3526 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3527
3528 /* Append the overlay string STRING of overlay OVERLAY to vector
3529 `entries' which has size `size' and currently contains `n'
3530 elements. AFTER_P non-zero means STRING is an after-string of
3531 OVERLAY. */
3532#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3533 do \
3534 { \
3535 Lisp_Object priority; \
3536 \
3537 if (n == size) \
3538 { \
3539 int new_size = 2 * size; \
3540 struct overlay_entry *old = entries; \
3541 entries = \
3542 (struct overlay_entry *) alloca (new_size \
3543 * sizeof *entries); \
3544 bcopy (old, entries, size * sizeof *entries); \
3545 size = new_size; \
3546 } \
3547 \
3548 entries[n].string = (STRING); \
2970b9be 3549 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3550 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3551 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3552 entries[n].after_string_p = (AFTER_P); \
3553 ++n; \
3554 } \
3555 while (0)
3556
3557 /* Process overlay before the overlay center. */
2970b9be 3558 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3559 {
9472f927 3560 overlay = XCAR (ov);
5f5c8ee5
GM
3561 xassert (OVERLAYP (overlay));
3562 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3563 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3564
cafafe0b 3565 if (end < charpos)
5f5c8ee5
GM
3566 break;
3567
3568 /* Skip this overlay if it doesn't start or end at IT's current
3569 position. */
cafafe0b 3570 if (end != charpos && start != charpos)
5f5c8ee5
GM
3571 continue;
3572
3573 /* Skip this overlay if it doesn't apply to IT->w. */
3574 window = Foverlay_get (overlay, Qwindow);
3575 if (WINDOWP (window) && XWINDOW (window) != it->w)
3576 continue;
3577
cafafe0b
GM
3578 /* If the text ``under'' the overlay is invisible, both before-
3579 and after-strings from this overlay are visible; start and
3580 end position are indistinguishable. */
3581 invisible = Foverlay_get (overlay, Qinvisible);
3582 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3583
5f5c8ee5 3584 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3585 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 3586 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 3587 && SCHARS (str))
5f5c8ee5
GM
3588 RECORD_OVERLAY_STRING (overlay, str, 0);
3589
3590 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3591 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 3592 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 3593 && SCHARS (str))
5f5c8ee5
GM
3594 RECORD_OVERLAY_STRING (overlay, str, 1);
3595 }
3596
3597 /* Process overlays after the overlay center. */
2970b9be 3598 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3599 {
9472f927 3600 overlay = XCAR (ov);
5f5c8ee5
GM
3601 xassert (OVERLAYP (overlay));
3602 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3603 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3604
cafafe0b 3605 if (start > charpos)
5f5c8ee5
GM
3606 break;
3607
3608 /* Skip this overlay if it doesn't start or end at IT's current
3609 position. */
cafafe0b 3610 if (end != charpos && start != charpos)
5f5c8ee5
GM
3611 continue;
3612
3613 /* Skip this overlay if it doesn't apply to IT->w. */
3614 window = Foverlay_get (overlay, Qwindow);
3615 if (WINDOWP (window) && XWINDOW (window) != it->w)
3616 continue;
3617
cafafe0b
GM
3618 /* If the text ``under'' the overlay is invisible, it has a zero
3619 dimension, and both before- and after-strings apply. */
3620 invisible = Foverlay_get (overlay, Qinvisible);
3621 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3622
5f5c8ee5 3623 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3624 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 3625 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 3626 && SCHARS (str))
5f5c8ee5
GM
3627 RECORD_OVERLAY_STRING (overlay, str, 0);
3628
3629 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3630 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 3631 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 3632 && SCHARS (str))
5f5c8ee5
GM
3633 RECORD_OVERLAY_STRING (overlay, str, 1);
3634 }
3635
3636#undef RECORD_OVERLAY_STRING
3637
3638 /* Sort entries. */
cafafe0b 3639 if (n > 1)
2970b9be 3640 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3641
3642 /* Record the total number of strings to process. */
3643 it->n_overlay_strings = n;
3644
3645 /* IT->current.overlay_string_index is the number of overlay strings
3646 that have already been consumed by IT. Copy some of the
3647 remaining overlay strings to IT->overlay_strings. */
3648 i = 0;
3649 j = it->current.overlay_string_index;
3650 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3651 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3652
5f5c8ee5
GM
3653 CHECK_IT (it);
3654}
3655
3656
3657/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
3658 position, or at CHARPOS if that is > 0. Value is non-zero if at
3659 least one overlay string was found. */
5f5c8ee5
GM
3660
3661static int
5a08cbaf 3662get_overlay_strings (it, charpos)
5f5c8ee5 3663 struct it *it;
5a08cbaf 3664 int charpos;
5f5c8ee5
GM
3665{
3666 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3667 process. This fills IT->overlay_strings with strings, and sets
3668 IT->n_overlay_strings to the total number of strings to process.
3669 IT->pos.overlay_string_index has to be set temporarily to zero
3670 because load_overlay_strings needs this; it must be set to -1
3671 when no overlay strings are found because a zero value would
3672 indicate a position in the first overlay string. */
3673 it->current.overlay_string_index = 0;
5a08cbaf 3674 load_overlay_strings (it, charpos);
5f5c8ee5
GM
3675
3676 /* If we found overlay strings, set up IT to deliver display
3677 elements from the first one. Otherwise set up IT to deliver
3678 from current_buffer. */
3679 if (it->n_overlay_strings)
3680 {
3681 /* Make sure we know settings in current_buffer, so that we can
3682 restore meaningful values when we're done with the overlay
3683 strings. */
3684 compute_stop_pos (it);
3685 xassert (it->face_id >= 0);
3686
3687 /* Save IT's settings. They are restored after all overlay
3688 strings have been processed. */
3689 xassert (it->sp == 0);
3690 push_it (it);
3691
3692 /* Set up IT to deliver display elements from the first overlay
3693 string. */
3694 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 3695 it->string = it->overlay_strings[0];
b2046df8 3696 it->stop_charpos = 0;
5f5c8ee5 3697 xassert (STRINGP (it->string));
2051c264
GM
3698 it->end_charpos = SCHARS (it->string);
3699 it->multibyte_p = SMBP (it->string);
5f5c8ee5
GM
3700 it->method = next_element_from_string;
3701 }
3702 else
3703 {
3704 it->string = Qnil;
3705 it->current.overlay_string_index = -1;
3706 it->method = next_element_from_buffer;
3707 }
3708
3709 CHECK_IT (it);
3710
3711 /* Value is non-zero if we found at least one overlay string. */
3712 return STRINGP (it->string);
3713}
3714
3715
3716\f
3717/***********************************************************************
3718 Saving and restoring state
3719 ***********************************************************************/
3720
3721/* Save current settings of IT on IT->stack. Called, for example,
3722 before setting up IT for an overlay string, to be able to restore
3723 IT's settings to what they were after the overlay string has been
3724 processed. */
3725
3726static void
3727push_it (it)
3728 struct it *it;
3729{
3730 struct iterator_stack_entry *p;
3731
3732 xassert (it->sp < 2);
3733 p = it->stack + it->sp;
3734
3735 p->stop_charpos = it->stop_charpos;
3736 xassert (it->face_id >= 0);
3737 p->face_id = it->face_id;
3738 p->string = it->string;
3739 p->pos = it->current;
3740 p->end_charpos = it->end_charpos;
3741 p->string_nchars = it->string_nchars;
3742 p->area = it->area;
3743 p->multibyte_p = it->multibyte_p;
3744 p->space_width = it->space_width;
3745 p->font_height = it->font_height;
3746 p->voffset = it->voffset;
3747 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 3748 p->display_ellipsis_p = 0;
5f5c8ee5
GM
3749 ++it->sp;
3750}
3751
3752
3753/* Restore IT's settings from IT->stack. Called, for example, when no
3754 more overlay strings must be processed, and we return to delivering
3755 display elements from a buffer, or when the end of a string from a
3756 `display' property is reached and we return to delivering display
3757 elements from an overlay string, or from a buffer. */
3758
3759static void
3760pop_it (it)
3761 struct it *it;
3762{
3763 struct iterator_stack_entry *p;
3764
3765 xassert (it->sp > 0);
3766 --it->sp;
3767 p = it->stack + it->sp;
3768 it->stop_charpos = p->stop_charpos;
3769 it->face_id = p->face_id;
3770 it->string = p->string;
3771 it->current = p->pos;
3772 it->end_charpos = p->end_charpos;
3773 it->string_nchars = p->string_nchars;
3774 it->area = p->area;
3775 it->multibyte_p = p->multibyte_p;
3776 it->space_width = p->space_width;
3777 it->font_height = p->font_height;
3778 it->voffset = p->voffset;
3779 it->string_from_display_prop_p = p->string_from_display_prop_p;
3780}
3781
3782
3783\f
3784/***********************************************************************
3785 Moving over lines
3786 ***********************************************************************/
3787
3788/* Set IT's current position to the previous line start. */
3789
3790static void
3791back_to_previous_line_start (it)
3792 struct it *it;
3793{
3794 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3795 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3796}
3797
3798
cafafe0b
GM
3799/* Move IT to the next line start.
3800
3801 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3802 we skipped over part of the text (as opposed to moving the iterator
3803 continuously over the text). Otherwise, don't change the value
3804 of *SKIPPED_P.
3805
3806 Newlines may come from buffer text, overlay strings, or strings
3807 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3808 simply use find_next_newline_no_quit.
3809
3810 Note that this function may not skip over invisible text that is so
3811 because of text properties and immediately follows a newline. If
3812 it would, function reseat_at_next_visible_line_start, when called
3813 from set_iterator_to_next, would effectively make invisible
3814 characters following a newline part of the wrong glyph row, which
3815 leads to wrong cursor motion. */
5f5c8ee5 3816
cafafe0b
GM
3817static int
3818forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3819 struct it *it;
cafafe0b 3820 int *skipped_p;
5f5c8ee5 3821{
54918e2b 3822 int old_selective, newline_found_p, n;
cafafe0b
GM
3823 const int MAX_NEWLINE_DISTANCE = 500;
3824
0fd37545
GM
3825 /* If already on a newline, just consume it to avoid unintended
3826 skipping over invisible text below. */
51695746
GM
3827 if (it->what == IT_CHARACTER
3828 && it->c == '\n'
3829 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
3830 {
3831 set_iterator_to_next (it, 0);
a77dc1ec 3832 it->c = 0;
0fd37545
GM
3833 return 1;
3834 }
3835
54918e2b 3836 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3837 unnecessary because it's done by the caller, and (b) leads to an
3838 infinite recursion because next_element_from_ellipsis indirectly
3839 calls this function. */
54918e2b
GM
3840 old_selective = it->selective;
3841 it->selective = 0;
3842
cafafe0b
GM
3843 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3844 from buffer text. */
3aec8722
GM
3845 for (n = newline_found_p = 0;
3846 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3847 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3848 {
8692ca92
GM
3849 if (!get_next_display_element (it))
3850 break;
d02f1cb8 3851 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3852 set_iterator_to_next (it, 0);
cafafe0b
GM
3853 }
3854
3855 /* If we didn't find a newline near enough, see if we can use a
3856 short-cut. */
3aec8722 3857 if (n == MAX_NEWLINE_DISTANCE)
cafafe0b
GM
3858 {
3859 int start = IT_CHARPOS (*it);
3860 int limit = find_next_newline_no_quit (start, 1);
3861 Lisp_Object pos;
3862
3863 xassert (!STRINGP (it->string));
3864
3865 /* If there isn't any `display' property in sight, and no
3866 overlays, we can just use the position of the newline in
3867 buffer text. */
3868 if (it->stop_charpos >= limit
3869 || ((pos = Fnext_single_property_change (make_number (start),
3870 Qdisplay,
3871 Qnil, make_number (limit)),
3872 NILP (pos))
3873 && next_overlay_change (start) == ZV))
3874 {
3875 IT_CHARPOS (*it) = limit;
3876 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3877 *skipped_p = newline_found_p = 1;
3878 }
3879 else
3880 {
3881 while (get_next_display_element (it)
3882 && !newline_found_p)
3883 {
3884 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3885 set_iterator_to_next (it, 0);
3886 }
3887 }
3888 }
3889
54918e2b 3890 it->selective = old_selective;
cafafe0b 3891 return newline_found_p;
5f5c8ee5
GM
3892}
3893
3894
3895/* Set IT's current position to the previous visible line start. Skip
3896 invisible text that is so either due to text properties or due to
3897 selective display. Caution: this does not change IT->current_x and
3898 IT->hpos. */
3899
3900static void
3901back_to_previous_visible_line_start (it)
3902 struct it *it;
3903{
3904 int visible_p = 0;
3905
3906 /* Go back one newline if not on BEGV already. */
3907 if (IT_CHARPOS (*it) > BEGV)
3908 back_to_previous_line_start (it);
3909
3910 /* Move over lines that are invisible because of selective display
3911 or text properties. */
3912 while (IT_CHARPOS (*it) > BEGV
3913 && !visible_p)
3914 {
3915 visible_p = 1;
3916
3917 /* If selective > 0, then lines indented more than that values
3918 are invisible. */
3919 if (it->selective > 0
3920 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3921 it->selective))
3922 visible_p = 0;
5f5c8ee5
GM
3923 else
3924 {
3925 Lisp_Object prop;
3926
6fc556fd
KR
3927 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3928 Qinvisible, it->window);
5f5c8ee5
GM
3929 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3930 visible_p = 0;
3931 }
5f5c8ee5
GM
3932
3933 /* Back one more newline if the current one is invisible. */
3934 if (!visible_p)
3935 back_to_previous_line_start (it);
3936 }
3937
3938 xassert (IT_CHARPOS (*it) >= BEGV);
3939 xassert (IT_CHARPOS (*it) == BEGV
3940 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3941 CHECK_IT (it);
3942}
3943
3944
3945/* Reseat iterator IT at the previous visible line start. Skip
3946 invisible text that is so either due to text properties or due to
3947 selective display. At the end, update IT's overlay information,
3948 face information etc. */
3949
3950static void
3951reseat_at_previous_visible_line_start (it)
3952 struct it *it;
3953{
3954 back_to_previous_visible_line_start (it);
3955 reseat (it, it->current.pos, 1);
3956 CHECK_IT (it);
3957}
3958
3959
3960/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3961 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3962 preceding the line start. Skip over invisible text that is so
3963 because of selective display. Compute faces, overlays etc at the
3964 new position. Note that this function does not skip over text that
3965 is invisible because of text properties. */
5f5c8ee5
GM
3966
3967static void
312246d1 3968reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3969 struct it *it;
312246d1 3970 int on_newline_p;
5f5c8ee5 3971{
cafafe0b
GM
3972 int newline_found_p, skipped_p = 0;
3973
3974 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3975
3976 /* Skip over lines that are invisible because they are indented
3977 more than the value of IT->selective. */
3978 if (it->selective > 0)
3979 while (IT_CHARPOS (*it) < ZV
a77dc1ec 3980 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
cafafe0b 3981 it->selective))
a77dc1ec
GM
3982 {
3983 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3984 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3985 }
cafafe0b
GM
3986
3987 /* Position on the newline if that's what's requested. */
3988 if (on_newline_p && newline_found_p)
5f5c8ee5 3989 {
cafafe0b 3990 if (STRINGP (it->string))
5f5c8ee5 3991 {
cafafe0b
GM
3992 if (IT_STRING_CHARPOS (*it) > 0)
3993 {
3994 --IT_STRING_CHARPOS (*it);
3995 --IT_STRING_BYTEPOS (*it);
3996 }
5f5c8ee5 3997 }
cafafe0b 3998 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
3999 {
4000 --IT_CHARPOS (*it);
cafafe0b
GM
4001 --IT_BYTEPOS (*it);
4002 reseat (it, it->current.pos, 0);
312246d1 4003 }
5f5c8ee5 4004 }
cafafe0b
GM
4005 else if (skipped_p)
4006 reseat (it, it->current.pos, 0);
5f5c8ee5
GM
4007
4008 CHECK_IT (it);
4009}
4010
4011
4012\f
4013/***********************************************************************
4014 Changing an iterator's position
4015***********************************************************************/
4016
4017/* Change IT's current position to POS in current_buffer. If FORCE_P
4018 is non-zero, always check for text properties at the new position.
4019 Otherwise, text properties are only looked up if POS >=
4020 IT->check_charpos of a property. */
4021
4022static void
4023reseat (it, pos, force_p)
4024 struct it *it;
4025 struct text_pos pos;
4026 int force_p;
4027{
4028 int original_pos = IT_CHARPOS (*it);
4029
4030 reseat_1 (it, pos, 0);
4031
4032 /* Determine where to check text properties. Avoid doing it
4033 where possible because text property lookup is very expensive. */
4034 if (force_p
4035 || CHARPOS (pos) > it->stop_charpos
4036 || CHARPOS (pos) < original_pos)
4037 handle_stop (it);
4038
4039 CHECK_IT (it);
4040}
4041
4042
4043/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4044 IT->stop_pos to POS, also. */
4045
4046static void
4047reseat_1 (it, pos, set_stop_p)
4048 struct it *it;
4049 struct text_pos pos;
4050 int set_stop_p;
4051{
4052 /* Don't call this function when scanning a C string. */
4053 xassert (it->s == NULL);
4054
4055 /* POS must be a reasonable value. */
4056 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4057
4058 it->current.pos = it->position = pos;
4059 XSETBUFFER (it->object, current_buffer);
78c663d8 4060 it->end_charpos = ZV;
5f5c8ee5
GM
4061 it->dpvec = NULL;
4062 it->current.dpvec_index = -1;
4063 it->current.overlay_string_index = -1;
4064 IT_STRING_CHARPOS (*it) = -1;
4065 IT_STRING_BYTEPOS (*it) = -1;
4066 it->string = Qnil;
4067 it->method = next_element_from_buffer;
06fd3792 4068 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4069 it->sp = 0;
4aad61f8 4070 it->face_before_selective_p = 0;
5f5c8ee5
GM
4071
4072 if (set_stop_p)
4073 it->stop_charpos = CHARPOS (pos);
4074}
4075
4076
4077/* Set up IT for displaying a string, starting at CHARPOS in window W.
4078 If S is non-null, it is a C string to iterate over. Otherwise,
4079 STRING gives a Lisp string to iterate over.
4080
4081 If PRECISION > 0, don't return more then PRECISION number of
4082 characters from the string.
4083
4084 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4085 characters have been returned. FIELD_WIDTH < 0 means an infinite
4086 field width.
4087
4088 MULTIBYTE = 0 means disable processing of multibyte characters,
4089 MULTIBYTE > 0 means enable it,
4090 MULTIBYTE < 0 means use IT->multibyte_p.
4091
4092 IT must be initialized via a prior call to init_iterator before
4093 calling this function. */
4094
4095static void
4096reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4097 struct it *it;
4098 unsigned char *s;
4099 Lisp_Object string;
4100 int charpos;
4101 int precision, field_width, multibyte;
4102{
4103 /* No region in strings. */
4104 it->region_beg_charpos = it->region_end_charpos = -1;
4105
4106 /* No text property checks performed by default, but see below. */
4107 it->stop_charpos = -1;
4108
4109 /* Set iterator position and end position. */
4110 bzero (&it->current, sizeof it->current);
4111 it->current.overlay_string_index = -1;
4112 it->current.dpvec_index = -1;
5f5c8ee5
GM
4113 xassert (charpos >= 0);
4114
e719f5ae
GM
4115 /* If STRING is specified, use its multibyteness, otherwise use the
4116 setting of MULTIBYTE, if specified. */
cabf45da 4117 if (multibyte >= 0)
5f5c8ee5
GM
4118 it->multibyte_p = multibyte > 0;
4119
4120 if (s == NULL)
4121 {
4122 xassert (STRINGP (string));
4123 it->string = string;
4124 it->s = NULL;
2051c264 4125 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4126 it->method = next_element_from_string;
4127 it->current.string_pos = string_pos (charpos, string);
4128 }
4129 else
4130 {
4131 it->s = s;
4132 it->string = Qnil;
4133
4134 /* Note that we use IT->current.pos, not it->current.string_pos,
4135 for displaying C strings. */
4136 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4137 if (it->multibyte_p)
4138 {
4139 it->current.pos = c_string_pos (charpos, s, 1);
4140 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4141 }
4142 else
4143 {
4144 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4145 it->end_charpos = it->string_nchars = strlen (s);
4146 }
4147
4148 it->method = next_element_from_c_string;
4149 }
4150
4151 /* PRECISION > 0 means don't return more than PRECISION characters
4152 from the string. */
4153 if (precision > 0 && it->end_charpos - charpos > precision)
4154 it->end_charpos = it->string_nchars = charpos + precision;
4155
4156 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4157 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4158 FIELD_WIDTH < 0 means infinite field width. This is useful for
4159 padding with `-' at the end of a mode line. */
4160 if (field_width < 0)
4161 field_width = INFINITY;
4162 if (field_width > it->end_charpos - charpos)
4163 it->end_charpos = charpos + field_width;
4164
4165 /* Use the standard display table for displaying strings. */
4166 if (DISP_TABLE_P (Vstandard_display_table))
4167 it->dp = XCHAR_TABLE (Vstandard_display_table);
4168
4169 it->stop_charpos = charpos;
4170 CHECK_IT (it);
4171}
4172
4173
4174\f
4175/***********************************************************************
4176 Iteration
4177 ***********************************************************************/
4178
4179/* Load IT's display element fields with information about the next
4180 display element from the current position of IT. Value is zero if
4181 end of buffer (or C string) is reached. */
4182
4183int
4184get_next_display_element (it)
4185 struct it *it;
4186{
4187 /* Non-zero means that we found an display element. Zero means that
4188 we hit the end of what we iterate over. Performance note: the
4189 function pointer `method' used here turns out to be faster than
4190 using a sequence of if-statements. */
4191 int success_p = (*it->method) (it);
5f5c8ee5
GM
4192
4193 if (it->what == IT_CHARACTER)
4194 {
4195 /* Map via display table or translate control characters.
4196 IT->c, IT->len etc. have been set to the next character by
4197 the function call above. If we have a display table, and it
4198 contains an entry for IT->c, translate it. Don't do this if
4199 IT->c itself comes from a display table, otherwise we could
4200 end up in an infinite recursion. (An alternative could be to
4201 count the recursion depth of this function and signal an
4202 error when a certain maximum depth is reached.) Is it worth
4203 it? */
4204 if (success_p && it->dpvec == NULL)
4205 {
4206 Lisp_Object dv;
4207
4208 if (it->dp
4209 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4210 VECTORP (dv)))
4211 {
4212 struct Lisp_Vector *v = XVECTOR (dv);
4213
4214 /* Return the first character from the display table
4215 entry, if not empty. If empty, don't display the
4216 current character. */
4217 if (v->size)
4218 {
4219 it->dpvec_char_len = it->len;
4220 it->dpvec = v->contents;
4221 it->dpend = v->contents + v->size;
4222 it->current.dpvec_index = 0;
4223 it->method = next_element_from_display_vector;
7eee47cc
GM
4224 success_p = get_next_display_element (it);
4225 }
4226 else
4227 {
4228 set_iterator_to_next (it, 0);
4229 success_p = get_next_display_element (it);
5f5c8ee5 4230 }
5f5c8ee5
GM
4231 }
4232
4233 /* Translate control characters into `\003' or `^C' form.
4234 Control characters coming from a display table entry are
4235 currently not translated because we use IT->dpvec to hold
4236 the translation. This could easily be changed but I
197516c2
KH
4237 don't believe that it is worth doing.
4238
4239 Non-printable multibyte characters are also translated
4240 octal form. */
5f5c8ee5
GM
4241 else if ((it->c < ' '
4242 && (it->area != TEXT_AREA
c6e89d6c 4243 || (it->c != '\n' && it->c != '\t')))
54c85a23 4244 || (it->c >= 127
197516c2
KH
4245 && it->len == 1)
4246 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
4247 {
4248 /* IT->c is a control character which must be displayed
4249 either as '\003' or as `^C' where the '\\' and '^'
4250 can be defined in the display table. Fill
4251 IT->ctl_chars with glyphs for what we have to
4252 display. Then, set IT->dpvec to these glyphs. */
4253 GLYPH g;
4254
54c85a23 4255 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4256 {
4257 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4258 if (it->dp
4259 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4260 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4261 g = XINT (DISP_CTRL_GLYPH (it->dp));
4262 else
4263 g = FAST_MAKE_GLYPH ('^', 0);
4264 XSETINT (it->ctl_chars[0], g);
4265
4266 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4267 XSETINT (it->ctl_chars[1], g);
4268
4269 /* Set up IT->dpvec and return first character from it. */
4270 it->dpvec_char_len = it->len;
4271 it->dpvec = it->ctl_chars;
4272 it->dpend = it->dpvec + 2;
4273 it->current.dpvec_index = 0;
4274 it->method = next_element_from_display_vector;
4275 get_next_display_element (it);
4276 }
4277 else
4278 {
260a86a0 4279 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4280 int len;
197516c2
KH
4281 int i;
4282 GLYPH escape_glyph;
4283
5f5c8ee5
GM
4284 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4285 if (it->dp
4286 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4287 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4288 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4289 else
197516c2
KH
4290 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4291
c5924f47
KH
4292 if (SINGLE_BYTE_CHAR_P (it->c))
4293 str[0] = it->c, len = 1;
4294 else
ea9dd091
GM
4295 {
4296 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4297 if (len < 0)
4298 {
4299 /* It's an invalid character, which
4300 shouldn't happen actually, but due to
4301 bugs it may happen. Let's print the char
4302 as is, there's not much meaningful we can
4303 do with it. */
4304 str[0] = it->c;
4305 str[1] = it->c >> 8;
4306 str[2] = it->c >> 16;
4307 str[3] = it->c >> 24;
4308 len = 4;
4309 }
4310 }
c5924f47 4311
197516c2
KH
4312 for (i = 0; i < len; i++)
4313 {
4314 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4315 /* Insert three more glyphs into IT->ctl_chars for
4316 the octal display of the character. */
4317 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4318 XSETINT (it->ctl_chars[i * 4 + 1], g);
4319 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4320 XSETINT (it->ctl_chars[i * 4 + 2], g);
4321 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4322 XSETINT (it->ctl_chars[i * 4 + 3], g);
4323 }
5f5c8ee5
GM
4324
4325 /* Set up IT->dpvec and return the first character
4326 from it. */
4327 it->dpvec_char_len = it->len;
4328 it->dpvec = it->ctl_chars;
197516c2 4329 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4330 it->current.dpvec_index = 0;
4331 it->method = next_element_from_display_vector;
4332 get_next_display_element (it);
4333 }
4334 }
4335 }
4336
980806b6
KH
4337 /* Adjust face id for a multibyte character. There are no
4338 multibyte character in unibyte text. */
5f5c8ee5
GM
4339 if (it->multibyte_p
4340 && success_p
980806b6 4341 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4342 {
980806b6
KH
4343 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4344 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4345 }
4346 }
4347
4348 /* Is this character the last one of a run of characters with
4349 box? If yes, set IT->end_of_box_run_p to 1. */
4350 if (it->face_box_p
4351 && it->s == NULL)
4352 {
4353 int face_id;
4354 struct face *face;
4355
4356 it->end_of_box_run_p
4357 = ((face_id = face_after_it_pos (it),
4358 face_id != it->face_id)
4359 && (face = FACE_FROM_ID (it->f, face_id),
4360 face->box == FACE_NO_BOX));
4361 }
4362
4363 /* Value is 0 if end of buffer or string reached. */
4364 return success_p;
4365}
4366
4367
4368/* Move IT to the next display element.
4369
cafafe0b
GM
4370 RESEAT_P non-zero means if called on a newline in buffer text,
4371 skip to the next visible line start.
4372
5f5c8ee5
GM
4373 Functions get_next_display_element and set_iterator_to_next are
4374 separate because I find this arrangement easier to handle than a
4375 get_next_display_element function that also increments IT's
4376 position. The way it is we can first look at an iterator's current
4377 display element, decide whether it fits on a line, and if it does,
4378 increment the iterator position. The other way around we probably
4379 would either need a flag indicating whether the iterator has to be
4380 incremented the next time, or we would have to implement a
4381 decrement position function which would not be easy to write. */
4382
4383void
cafafe0b 4384set_iterator_to_next (it, reseat_p)
5f5c8ee5 4385 struct it *it;
cafafe0b 4386 int reseat_p;
5f5c8ee5 4387{
483de32b
GM
4388 /* Reset flags indicating start and end of a sequence of characters
4389 with box. Reset them at the start of this function because
4390 moving the iterator to a new position might set them. */
4391 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4392
5f5c8ee5
GM
4393 if (it->method == next_element_from_buffer)
4394 {
4395 /* The current display element of IT is a character from
4396 current_buffer. Advance in the buffer, and maybe skip over
4397 invisible lines that are so because of selective display. */
cafafe0b 4398 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4399 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4400 else
4401 {
4402 xassert (it->len != 0);
4403 IT_BYTEPOS (*it) += it->len;
4404 IT_CHARPOS (*it) += 1;
4405 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4406 }
4407 }
260a86a0
KH
4408 else if (it->method == next_element_from_composition)
4409 {
4410 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4411 if (STRINGP (it->string))
4412 {
4413 IT_STRING_BYTEPOS (*it) += it->len;
4414 IT_STRING_CHARPOS (*it) += it->cmp_len;
4415 it->method = next_element_from_string;
4416 goto consider_string_end;
4417 }
4418 else
4419 {
4420 IT_BYTEPOS (*it) += it->len;
4421 IT_CHARPOS (*it) += it->cmp_len;
4422 it->method = next_element_from_buffer;
4423 }
4424 }
5f5c8ee5
GM
4425 else if (it->method == next_element_from_c_string)
4426 {
4427 /* Current display element of IT is from a C string. */
4428 IT_BYTEPOS (*it) += it->len;
4429 IT_CHARPOS (*it) += 1;
4430 }
4431 else if (it->method == next_element_from_display_vector)
4432 {
4433 /* Current display element of IT is from a display table entry.
4434 Advance in the display table definition. Reset it to null if
4435 end reached, and continue with characters from buffers/
4436 strings. */
4437 ++it->current.dpvec_index;
286bcbc9 4438
980806b6
KH
4439 /* Restore face of the iterator to what they were before the
4440 display vector entry (these entries may contain faces). */
5f5c8ee5 4441 it->face_id = it->saved_face_id;
286bcbc9 4442
5f5c8ee5
GM
4443 if (it->dpvec + it->current.dpvec_index == it->dpend)
4444 {
4445 if (it->s)
4446 it->method = next_element_from_c_string;
4447 else if (STRINGP (it->string))
4448 it->method = next_element_from_string;
4449 else
4450 it->method = next_element_from_buffer;
4451
4452 it->dpvec = NULL;
4453 it->current.dpvec_index = -1;
4454
312246d1
GM
4455 /* Skip over characters which were displayed via IT->dpvec. */
4456 if (it->dpvec_char_len < 0)
4457 reseat_at_next_visible_line_start (it, 1);
4458 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4459 {
4460 it->len = it->dpvec_char_len;
cafafe0b 4461 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4462 }
4463 }
4464 }
4465 else if (it->method == next_element_from_string)
4466 {
4467 /* Current display element is a character from a Lisp string. */
4468 xassert (it->s == NULL && STRINGP (it->string));
4469 IT_STRING_BYTEPOS (*it) += it->len;
4470 IT_STRING_CHARPOS (*it) += 1;
4471
4472 consider_string_end:
4473
4474 if (it->current.overlay_string_index >= 0)
4475 {
4476 /* IT->string is an overlay string. Advance to the
4477 next, if there is one. */
2051c264 4478 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4479 next_overlay_string (it);
4480 }
4481 else
4482 {
4483 /* IT->string is not an overlay string. If we reached
4484 its end, and there is something on IT->stack, proceed
4485 with what is on the stack. This can be either another
4486 string, this time an overlay string, or a buffer. */
2051c264 4487 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
4488 && it->sp > 0)
4489 {
4490 pop_it (it);
4491 if (!STRINGP (it->string))
4492 it->method = next_element_from_buffer;
b2046df8
GM
4493 else
4494 goto consider_string_end;
5f5c8ee5
GM
4495 }
4496 }
4497 }
4498 else if (it->method == next_element_from_image
4499 || it->method == next_element_from_stretch)
4500 {
4501 /* The position etc with which we have to proceed are on
4502 the stack. The position may be at the end of a string,
4503 if the `display' property takes up the whole string. */
4504 pop_it (it);
4505 it->image_id = 0;
4506 if (STRINGP (it->string))
4507 {
4508 it->method = next_element_from_string;
4509 goto consider_string_end;
4510 }
4511 else
4512 it->method = next_element_from_buffer;
4513 }
4514 else
4515 /* There are no other methods defined, so this should be a bug. */
4516 abort ();
4517
5f5c8ee5
GM
4518 xassert (it->method != next_element_from_string
4519 || (STRINGP (it->string)
4520 && IT_STRING_CHARPOS (*it) >= 0));
4521}
4522
4523
4524/* Load IT's display element fields with information about the next
4525 display element which comes from a display table entry or from the
4526 result of translating a control character to one of the forms `^C'
4527 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4528
4529static int
4530next_element_from_display_vector (it)
4531 struct it *it;
4532{
4533 /* Precondition. */
4534 xassert (it->dpvec && it->current.dpvec_index >= 0);
4535
4536 /* Remember the current face id in case glyphs specify faces.
4537 IT's face is restored in set_iterator_to_next. */
4538 it->saved_face_id = it->face_id;
4539
4540 if (INTEGERP (*it->dpvec)
4541 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4542 {
4543 int lface_id;
4544 GLYPH g;
4545
4546 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4547 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4548 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4549
4550 /* The entry may contain a face id to use. Such a face id is
4551 the id of a Lisp face, not a realized face. A face id of
969065c3 4552 zero means no face is specified. */
5f5c8ee5
GM
4553 lface_id = FAST_GLYPH_FACE (g);
4554 if (lface_id)
4555 {
969065c3 4556 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4557 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4558 if (face_id >= 0)
969065c3 4559 it->face_id = face_id;
5f5c8ee5
GM
4560 }
4561 }
4562 else
4563 /* Display table entry is invalid. Return a space. */
4564 it->c = ' ', it->len = 1;
4565
4566 /* Don't change position and object of the iterator here. They are
4567 still the values of the character that had this display table
4568 entry or was translated, and that's what we want. */
4569 it->what = IT_CHARACTER;
4570 return 1;
4571}
4572
4573
4574/* Load IT with the next display element from Lisp string IT->string.
4575 IT->current.string_pos is the current position within the string.
4576 If IT->current.overlay_string_index >= 0, the Lisp string is an
4577 overlay string. */
4578
4579static int
4580next_element_from_string (it)
4581 struct it *it;
4582{
4583 struct text_pos position;
4584
4585 xassert (STRINGP (it->string));
4586 xassert (IT_STRING_CHARPOS (*it) >= 0);
4587 position = it->current.string_pos;
4588
4589 /* Time to check for invisible text? */
4590 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4591 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4592 {
4593 handle_stop (it);
4594
4595 /* Since a handler may have changed IT->method, we must
4596 recurse here. */
4597 return get_next_display_element (it);
4598 }
4599
4600 if (it->current.overlay_string_index >= 0)
4601 {
4602 /* Get the next character from an overlay string. In overlay
4603 strings, There is no field width or padding with spaces to
4604 do. */
2051c264 4605 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4606 {
4607 it->what = IT_EOB;
4608 return 0;
4609 }
4610 else if (STRING_MULTIBYTE (it->string))
4611 {
2051c264
GM
4612 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4613 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4fdb80f2 4614 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4615 }
4616 else
4617 {
2051c264 4618 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
4619 it->len = 1;
4620 }
4621 }
4622 else
4623 {
4624 /* Get the next character from a Lisp string that is not an
4625 overlay string. Such strings come from the mode line, for
4626 example. We may have to pad with spaces, or truncate the
4627 string. See also next_element_from_c_string. */
4628 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4629 {
4630 it->what = IT_EOB;
4631 return 0;
4632 }
4633 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4634 {
4635 /* Pad with spaces. */
4636 it->c = ' ', it->len = 1;
4637 CHARPOS (position) = BYTEPOS (position) = -1;
4638 }
4639 else if (STRING_MULTIBYTE (it->string))
4640 {
2051c264
GM
4641 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4642 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4fdb80f2 4643 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4644 }
4645 else
4646 {
2051c264 4647 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
4648 it->len = 1;
4649 }
4650 }
4651
4652 /* Record what we have and where it came from. Note that we store a
4653 buffer position in IT->position although it could arguably be a
4654 string position. */
4655 it->what = IT_CHARACTER;
4656 it->object = it->string;
4657 it->position = position;
4658 return 1;
4659}
4660
4661
4662/* Load IT with next display element from C string IT->s.
4663 IT->string_nchars is the maximum number of characters to return
4664 from the string. IT->end_charpos may be greater than
4665 IT->string_nchars when this function is called, in which case we
4666 may have to return padding spaces. Value is zero if end of string
4667 reached, including padding spaces. */
4668
4669static int
4670next_element_from_c_string (it)
4671 struct it *it;
4672{
4673 int success_p = 1;
4674
4675 xassert (it->s);
4676 it->what = IT_CHARACTER;
4677 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4678 it->object = Qnil;
4679
4680 /* IT's position can be greater IT->string_nchars in case a field
4681 width or precision has been specified when the iterator was
4682 initialized. */
4683 if (IT_CHARPOS (*it) >= it->end_charpos)
4684 {
4685 /* End of the game. */
4686 it->what = IT_EOB;
4687 success_p = 0;
4688 }
4689 else if (IT_CHARPOS (*it) >= it->string_nchars)
4690 {
4691 /* Pad with spaces. */
4692 it->c = ' ', it->len = 1;
4693 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4694 }
4695 else if (it->multibyte_p)
4696 {
4697 /* Implementation note: The calls to strlen apparently aren't a
4698 performance problem because there is no noticeable performance
4699 difference between Emacs running in unibyte or multibyte mode. */
4700 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4701 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4702 maxlen, &it->len);
5f5c8ee5
GM
4703 }
4704 else
4705 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4706
4707 return success_p;
4708}
4709
4710
4711/* Set up IT to return characters from an ellipsis, if appropriate.
4712 The definition of the ellipsis glyphs may come from a display table
4713 entry. This function Fills IT with the first glyph from the
4714 ellipsis if an ellipsis is to be displayed. */
4715
13f19968 4716static int
5f5c8ee5
GM
4717next_element_from_ellipsis (it)
4718 struct it *it;
4719{
13f19968 4720 if (it->selective_display_ellipsis_p)
5f5c8ee5 4721 {
13f19968
GM
4722 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4723 {
4724 /* Use the display table definition for `...'. Invalid glyphs
4725 will be handled by the method returning elements from dpvec. */
4726 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4727 it->dpvec_char_len = it->len;
4728 it->dpvec = v->contents;
4729 it->dpend = v->contents + v->size;
4730 it->current.dpvec_index = 0;
4731 it->method = next_element_from_display_vector;
4732 }
4733 else
4734 {
4735 /* Use default `...' which is stored in default_invis_vector. */
4736 it->dpvec_char_len = it->len;
4737 it->dpvec = default_invis_vector;
4738 it->dpend = default_invis_vector + 3;
4739 it->current.dpvec_index = 0;
4740 it->method = next_element_from_display_vector;
4741 }
5f5c8ee5 4742 }
13f19968 4743 else
54918e2b 4744 {
4aad61f8
GM
4745 /* The face at the current position may be different from the
4746 face we find after the invisible text. Remember what it
4747 was in IT->saved_face_id, and signal that it's there by
4748 setting face_before_selective_p. */
4749 it->saved_face_id = it->face_id;
54918e2b
GM
4750 it->method = next_element_from_buffer;
4751 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4752 it->face_before_selective_p = 1;
54918e2b 4753 }
13f19968
GM
4754
4755 return get_next_display_element (it);
5f5c8ee5
GM
4756}
4757
4758
4759/* Deliver an image display element. The iterator IT is already
4760 filled with image information (done in handle_display_prop). Value
4761 is always 1. */
4762
4763
4764static int
4765next_element_from_image (it)
4766 struct it *it;
4767{
4768 it->what = IT_IMAGE;
4769 return 1;
4770}
4771
4772
4773/* Fill iterator IT with next display element from a stretch glyph
4774 property. IT->object is the value of the text property. Value is
4775 always 1. */
4776
4777static int
4778next_element_from_stretch (it)
4779 struct it *it;
4780{
4781 it->what = IT_STRETCH;
4782 return 1;
4783}
4784
4785
4786/* Load IT with the next display element from current_buffer. Value
4787 is zero if end of buffer reached. IT->stop_charpos is the next
4788 position at which to stop and check for text properties or buffer
4789 end. */
4790
4791static int
4792next_element_from_buffer (it)
4793 struct it *it;
4794{
4795 int success_p = 1;
4796
4797 /* Check this assumption, otherwise, we would never enter the
4798 if-statement, below. */
4799 xassert (IT_CHARPOS (*it) >= BEGV
4800 && IT_CHARPOS (*it) <= it->stop_charpos);
4801
4802 if (IT_CHARPOS (*it) >= it->stop_charpos)
4803 {
4804 if (IT_CHARPOS (*it) >= it->end_charpos)
4805 {
4806 int overlay_strings_follow_p;
4807
4808 /* End of the game, except when overlay strings follow that
4809 haven't been returned yet. */
4810 if (it->overlay_strings_at_end_processed_p)
4811 overlay_strings_follow_p = 0;
4812 else
4813 {
4814 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 4815 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
4816 }
4817
4818 if (overlay_strings_follow_p)
4819 success_p = get_next_display_element (it);
4820 else
4821 {
4822 it->what = IT_EOB;
4823 it->position = it->current.pos;
4824 success_p = 0;
4825 }
4826 }
4827 else
4828 {
4829 handle_stop (it);
4830 return get_next_display_element (it);
4831 }
4832 }
4833 else
4834 {
4835 /* No face changes, overlays etc. in sight, so just return a
4836 character from current_buffer. */
4837 unsigned char *p;
4838
4839 /* Maybe run the redisplay end trigger hook. Performance note:
4840 This doesn't seem to cost measurable time. */
4841 if (it->redisplay_end_trigger_charpos
4842 && it->glyph_row
4843 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4844 run_redisplay_end_trigger_hook (it);
4845
4846 /* Get the next character, maybe multibyte. */
4847 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4848 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4849 {
4850 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4851 - IT_BYTEPOS (*it));
4fdb80f2 4852 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4853 }
4854 else
4855 it->c = *p, it->len = 1;
4856
4857 /* Record what we have and where it came from. */
4858 it->what = IT_CHARACTER;;
4859 it->object = it->w->buffer;
4860 it->position = it->current.pos;
4861
4862 /* Normally we return the character found above, except when we
4863 really want to return an ellipsis for selective display. */
4864 if (it->selective)
4865 {
4866 if (it->c == '\n')
4867 {
4868 /* A value of selective > 0 means hide lines indented more
4869 than that number of columns. */
4870 if (it->selective > 0
4871 && IT_CHARPOS (*it) + 1 < ZV
4872 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4873 IT_BYTEPOS (*it) + 1,
4874 it->selective))
312246d1 4875 {
13f19968 4876 success_p = next_element_from_ellipsis (it);
312246d1
GM
4877 it->dpvec_char_len = -1;
4878 }
5f5c8ee5
GM
4879 }
4880 else if (it->c == '\r' && it->selective == -1)
4881 {
4882 /* A value of selective == -1 means that everything from the
4883 CR to the end of the line is invisible, with maybe an
4884 ellipsis displayed for it. */
13f19968 4885 success_p = next_element_from_ellipsis (it);
312246d1 4886 it->dpvec_char_len = -1;
5f5c8ee5
GM
4887 }
4888 }
4889 }
4890
4891 /* Value is zero if end of buffer reached. */
c880678e 4892 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4893 return success_p;
4894}
4895
4896
4897/* Run the redisplay end trigger hook for IT. */
4898
4899static void
4900run_redisplay_end_trigger_hook (it)
4901 struct it *it;
4902{
4903 Lisp_Object args[3];
4904
4905 /* IT->glyph_row should be non-null, i.e. we should be actually
4906 displaying something, or otherwise we should not run the hook. */
4907 xassert (it->glyph_row);
4908
4909 /* Set up hook arguments. */
4910 args[0] = Qredisplay_end_trigger_functions;
4911 args[1] = it->window;
4912 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4913 it->redisplay_end_trigger_charpos = 0;
4914
4915 /* Since we are *trying* to run these functions, don't try to run
4916 them again, even if they get an error. */
4917 it->w->redisplay_end_trigger = Qnil;
4918 Frun_hook_with_args (3, args);
4919
4920 /* Notice if it changed the face of the character we are on. */
4921 handle_face_prop (it);
4922}
4923
4924
260a86a0
KH
4925/* Deliver a composition display element. The iterator IT is already
4926 filled with composition information (done in
4927 handle_composition_prop). Value is always 1. */
4928
4929static int
4930next_element_from_composition (it)
4931 struct it *it;
4932{
4933 it->what = IT_COMPOSITION;
4934 it->position = (STRINGP (it->string)
4935 ? it->current.string_pos
4936 : it->current.pos);
4937 return 1;
4938}
4939
4940
5f5c8ee5
GM
4941\f
4942/***********************************************************************
4943 Moving an iterator without producing glyphs
4944 ***********************************************************************/
4945
4946/* Move iterator IT to a specified buffer or X position within one
4947 line on the display without producing glyphs.
4948
c53a1624
RS
4949 OP should be a bit mask including some or all of these bits:
4950 MOVE_TO_X: Stop on reaching x-position TO_X.
4951 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4952 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 4953
c53a1624
RS
4954 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4955 This means, in particular, that TO_X includes window's horizontal
4956 scroll amount.
5f5c8ee5 4957
c53a1624
RS
4958 The return value has several possible values that
4959 say what condition caused the scan to stop:
5f5c8ee5
GM
4960
4961 MOVE_POS_MATCH_OR_ZV
4962 - when TO_POS or ZV was reached.
4963
4964 MOVE_X_REACHED
4965 -when TO_X was reached before TO_POS or ZV were reached.
4966
4967 MOVE_LINE_CONTINUED
4968 - when we reached the end of the display area and the line must
4969 be continued.
4970
4971 MOVE_LINE_TRUNCATED
4972 - when we reached the end of the display area and the line is
4973 truncated.
4974
4975 MOVE_NEWLINE_OR_CR
4976 - when we stopped at a line end, i.e. a newline or a CR and selective
4977 display is on. */
4978
701552dd 4979static enum move_it_result
5f5c8ee5
GM
4980move_it_in_display_line_to (it, to_charpos, to_x, op)
4981 struct it *it;
4982 int to_charpos, to_x, op;
4983{
4984 enum move_it_result result = MOVE_UNDEFINED;
4985 struct glyph_row *saved_glyph_row;
4986
4987 /* Don't produce glyphs in produce_glyphs. */
4988 saved_glyph_row = it->glyph_row;
4989 it->glyph_row = NULL;
4990
5f5c8ee5
GM
4991 while (1)
4992 {
ae26e27d 4993 int x, i, ascent = 0, descent = 0;
5f5c8ee5
GM
4994
4995 /* Stop when ZV or TO_CHARPOS reached. */
4996 if (!get_next_display_element (it)
4997 || ((op & MOVE_TO_POS) != 0
4998 && BUFFERP (it->object)
4999 && IT_CHARPOS (*it) >= to_charpos))
5000 {
5001 result = MOVE_POS_MATCH_OR_ZV;
5002 break;
5003 }
5004
5005 /* The call to produce_glyphs will get the metrics of the
5006 display element IT is loaded with. We record in x the
5007 x-position before this display element in case it does not
5008 fit on the line. */
5009 x = it->current_x;
47589c8c
GM
5010
5011 /* Remember the line height so far in case the next element doesn't
5012 fit on the line. */
5013 if (!it->truncate_lines_p)
5014 {
5015 ascent = it->max_ascent;
5016 descent = it->max_descent;
5017 }
5018
5f5c8ee5
GM
5019 PRODUCE_GLYPHS (it);
5020
5021 if (it->area != TEXT_AREA)
5022 {
cafafe0b 5023 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5024 continue;
5025 }
5026
5027 /* The number of glyphs we get back in IT->nglyphs will normally
5028 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5029 character on a terminal frame, or (iii) a line end. For the
5030 second case, IT->nglyphs - 1 padding glyphs will be present
5031 (on X frames, there is only one glyph produced for a
5032 composite character.
5033
5034 The behavior implemented below means, for continuation lines,
5035 that as many spaces of a TAB as fit on the current line are
5036 displayed there. For terminal frames, as many glyphs of a
5037 multi-glyph character are displayed in the current line, too.
5038 This is what the old redisplay code did, and we keep it that
5039 way. Under X, the whole shape of a complex character must
5040 fit on the line or it will be completely displayed in the
5041 next line.
5042
5043 Note that both for tabs and padding glyphs, all glyphs have
5044 the same width. */
5045 if (it->nglyphs)
5046 {
5047 /* More than one glyph or glyph doesn't fit on line. All
5048 glyphs have the same width. */
5049 int single_glyph_width = it->pixel_width / it->nglyphs;
5050 int new_x;
5051
5052 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5053 {
5054 new_x = x + single_glyph_width;
5055
5056 /* We want to leave anything reaching TO_X to the caller. */
5057 if ((op & MOVE_TO_X) && new_x > to_x)
5058 {
5059 it->current_x = x;
5060 result = MOVE_X_REACHED;
5061 break;
5062 }
5063 else if (/* Lines are continued. */
5064 !it->truncate_lines_p
5065 && (/* And glyph doesn't fit on the line. */
5066 new_x > it->last_visible_x
5067 /* Or it fits exactly and we're on a window
5068 system frame. */
5069 || (new_x == it->last_visible_x
5070 && FRAME_WINDOW_P (it->f))))
5071 {
5072 if (/* IT->hpos == 0 means the very first glyph
5073 doesn't fit on the line, e.g. a wide image. */
5074 it->hpos == 0
5075 || (new_x == it->last_visible_x
5076 && FRAME_WINDOW_P (it->f)))
5077 {
5078 ++it->hpos;
5079 it->current_x = new_x;
5080 if (i == it->nglyphs - 1)
cafafe0b 5081 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5082 }
5083 else
47589c8c
GM
5084 {
5085 it->current_x = x;
5086 it->max_ascent = ascent;
5087 it->max_descent = descent;
5088 }
5089
5090 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5091 IT_CHARPOS (*it)));
5f5c8ee5
GM
5092 result = MOVE_LINE_CONTINUED;
5093 break;
5094 }
5095 else if (new_x > it->first_visible_x)
5096 {
5097 /* Glyph is visible. Increment number of glyphs that
5098 would be displayed. */
5099 ++it->hpos;
5100 }
5101 else
5102 {
5103 /* Glyph is completely off the left margin of the display
5104 area. Nothing to do. */
5105 }
5106 }
5107
5108 if (result != MOVE_UNDEFINED)
5109 break;
5110 }
5111 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5112 {
5113 /* Stop when TO_X specified and reached. This check is
5114 necessary here because of lines consisting of a line end,
5115 only. The line end will not produce any glyphs and we
5116 would never get MOVE_X_REACHED. */
5117 xassert (it->nglyphs == 0);
5118 result = MOVE_X_REACHED;
5119 break;
5120 }
5121
5122 /* Is this a line end? If yes, we're done. */
5123 if (ITERATOR_AT_END_OF_LINE_P (it))
5124 {
5125 result = MOVE_NEWLINE_OR_CR;
5126 break;
5127 }
5128
5129 /* The current display element has been consumed. Advance
5130 to the next. */
cafafe0b 5131 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5132
5133 /* Stop if lines are truncated and IT's current x-position is
5134 past the right edge of the window now. */
5135 if (it->truncate_lines_p
5136 && it->current_x >= it->last_visible_x)
5137 {
5138 result = MOVE_LINE_TRUNCATED;
5139 break;
5140 }
5141 }
5142
5143 /* Restore the iterator settings altered at the beginning of this
5144 function. */
5145 it->glyph_row = saved_glyph_row;
5146 return result;
5147}
5148
5149
9b2bba76
RS
5150/* Move IT forward until it satisfies one or more of the criteria in
5151 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5152
5153 OP is a bit-mask that specifies where to stop, and in particular,
5154 which of those four position arguments makes a difference. See the
5155 description of enum move_operation_enum.
5f5c8ee5
GM
5156
5157 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5158 screen line, this function will set IT to the next position >
5159 TO_CHARPOS. */
5160
5161void
5162move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5163 struct it *it;
5164 int to_charpos, to_x, to_y, to_vpos;
5165 int op;
5166{
5167 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5168 int line_height;
47589c8c 5169 int reached = 0;
5f5c8ee5 5170
47589c8c 5171 for (;;)
5f5c8ee5
GM
5172 {
5173 if (op & MOVE_TO_VPOS)
5174 {
5175 /* If no TO_CHARPOS and no TO_X specified, stop at the
5176 start of the line TO_VPOS. */
5177 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5178 {
5179 if (it->vpos == to_vpos)
47589c8c
GM
5180 {
5181 reached = 1;
5182 break;
5183 }
5184 else
5185 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5186 }
5187 else
5188 {
5189 /* TO_VPOS >= 0 means stop at TO_X in the line at
5190 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5191 if (it->vpos == to_vpos)
5192 {
5193 reached = 2;
5194 break;
5195 }
5196
5f5c8ee5
GM
5197 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5198
5199 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5200 {
5201 reached = 3;
5202 break;
5203 }
5f5c8ee5
GM
5204 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5205 {
5206 /* We have reached TO_X but not in the line we want. */
5207 skip = move_it_in_display_line_to (it, to_charpos,
5208 -1, MOVE_TO_POS);
5209 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5210 {
5211 reached = 4;
5212 break;
5213 }
5f5c8ee5
GM
5214 }
5215 }
5216 }
5217 else if (op & MOVE_TO_Y)
5218 {
5219 struct it it_backup;
5f5c8ee5
GM
5220
5221 /* TO_Y specified means stop at TO_X in the line containing
5222 TO_Y---or at TO_CHARPOS if this is reached first. The
5223 problem is that we can't really tell whether the line
5224 contains TO_Y before we have completely scanned it, and
5225 this may skip past TO_X. What we do is to first scan to
5226 TO_X.
5227
5228 If TO_X is not specified, use a TO_X of zero. The reason
5229 is to make the outcome of this function more predictable.
5230 If we didn't use TO_X == 0, we would stop at the end of
5231 the line which is probably not what a caller would expect
5232 to happen. */
5233 skip = move_it_in_display_line_to (it, to_charpos,
5234 ((op & MOVE_TO_X)
5235 ? to_x : 0),
5236 (MOVE_TO_X
5237 | (op & MOVE_TO_POS)));
5238
5239 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5240 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5241 {
5242 reached = 5;
5243 break;
5244 }
5f5c8ee5
GM
5245
5246 /* If TO_X was reached, we would like to know whether TO_Y
5247 is in the line. This can only be said if we know the
5248 total line height which requires us to scan the rest of
5249 the line. */
5f5c8ee5
GM
5250 if (skip == MOVE_X_REACHED)
5251 {
5252 it_backup = *it;
47589c8c 5253 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5254 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5255 op & MOVE_TO_POS);
47589c8c 5256 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5257 }
5258
5259 /* Now, decide whether TO_Y is in this line. */
5260 line_height = it->max_ascent + it->max_descent;
47589c8c 5261 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5f5c8ee5
GM
5262
5263 if (to_y >= it->current_y
5264 && to_y < it->current_y + line_height)
5265 {
5266 if (skip == MOVE_X_REACHED)
5267 /* If TO_Y is in this line and TO_X was reached above,
5268 we scanned too far. We have to restore IT's settings
5269 to the ones before skipping. */
5270 *it = it_backup;
47589c8c 5271 reached = 6;
5f5c8ee5
GM
5272 }
5273 else if (skip == MOVE_X_REACHED)
5274 {
5275 skip = skip2;
5276 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5277 reached = 7;
5f5c8ee5
GM
5278 }
5279
47589c8c 5280 if (reached)
5f5c8ee5
GM
5281 break;
5282 }
5283 else
5284 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5285
5286 switch (skip)
5287 {
5288 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5289 reached = 8;
5290 goto out;
5f5c8ee5
GM
5291
5292 case MOVE_NEWLINE_OR_CR:
cafafe0b 5293 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5294 it->continuation_lines_width = 0;
5295 break;
5296
5297 case MOVE_LINE_TRUNCATED:
5298 it->continuation_lines_width = 0;
312246d1 5299 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5300 if ((op & MOVE_TO_POS) != 0
5301 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5302 {
5303 reached = 9;
5304 goto out;
5305 }
5f5c8ee5
GM
5306 break;
5307
5308 case MOVE_LINE_CONTINUED:
5309 it->continuation_lines_width += it->current_x;
5310 break;
5311
5312 default:
5313 abort ();
5314 }
5315
5316 /* Reset/increment for the next run. */
5317 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5318 it->current_x = it->hpos = 0;
5319 it->current_y += it->max_ascent + it->max_descent;
5320 ++it->vpos;
5321 last_height = it->max_ascent + it->max_descent;
5322 last_max_ascent = it->max_ascent;
5323 it->max_ascent = it->max_descent = 0;
5324 }
47589c8c
GM
5325
5326 out:
5327
5328 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5329}
5330
5331
5332/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5333
5334 If DY > 0, move IT backward at least that many pixels. DY = 0
5335 means move IT backward to the preceding line start or BEGV. This
5336 function may move over more than DY pixels if IT->current_y - DY
5337 ends up in the middle of a line; in this case IT->current_y will be
5338 set to the top of the line moved to. */
5339
5340void
5341move_it_vertically_backward (it, dy)
5342 struct it *it;
5343 int dy;
5344{
79ddf6f7
GM
5345 int nlines, h;
5346 struct it it2, it3;
5f5c8ee5
GM
5347 int start_pos = IT_CHARPOS (*it);
5348
5349 xassert (dy >= 0);
5350
5351 /* Estimate how many newlines we must move back. */
5352 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5353
5354 /* Set the iterator's position that many lines back. */
5355 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5356 back_to_previous_visible_line_start (it);
5357
5358 /* Reseat the iterator here. When moving backward, we don't want
5359 reseat to skip forward over invisible text, set up the iterator
5360 to deliver from overlay strings at the new position etc. So,
5361 use reseat_1 here. */
5362 reseat_1 (it, it->current.pos, 1);
5363
5364 /* We are now surely at a line start. */
5365 it->current_x = it->hpos = 0;
5366
5367 /* Move forward and see what y-distance we moved. First move to the
5368 start of the next line so that we get its height. We need this
5369 height to be able to tell whether we reached the specified
5370 y-distance. */
5371 it2 = *it;
5372 it2.max_ascent = it2.max_descent = 0;
5373 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5374 MOVE_TO_POS | MOVE_TO_VPOS);
5375 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 5376 it3 = it2;
47589c8c 5377
5f5c8ee5
GM
5378 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5379 xassert (IT_CHARPOS (*it) >= BEGV);
5380 h = it2.current_y - it->current_y;
5381 nlines = it2.vpos - it->vpos;
5382
5383 /* Correct IT's y and vpos position. */
5384 it->vpos -= nlines;
5385 it->current_y -= h;
5386
5387 if (dy == 0)
5388 {
5389 /* DY == 0 means move to the start of the screen line. The
5390 value of nlines is > 0 if continuation lines were involved. */
5391 if (nlines > 0)
5392 move_it_by_lines (it, nlines, 1);
5393 xassert (IT_CHARPOS (*it) <= start_pos);
5394 }
5395 else if (nlines)
5396 {
5397 /* The y-position we try to reach. Note that h has been
5398 subtracted in front of the if-statement. */
5399 int target_y = it->current_y + h - dy;
79ddf6f7
GM
5400 int y0 = it3.current_y;
5401 int y1 = line_bottom_y (&it3);
5402 int line_height = y1 - y0;
f7ccfc8c 5403
5f5c8ee5
GM
5404 /* If we did not reach target_y, try to move further backward if
5405 we can. If we moved too far backward, try to move forward. */
5406 if (target_y < it->current_y
79ddf6f7
GM
5407 /* This is heuristic. In a window that's 3 lines high, with
5408 a line height of 13 pixels each, recentering with point
5409 on the bottom line will try to move -39/2 = 19 pixels
5410 backward. Try to avoid moving into the first line. */
798dbe1f 5411 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
5412 && IT_CHARPOS (*it) > BEGV)
5413 {
f7ccfc8c
GM
5414 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5415 target_y - it->current_y));
5f5c8ee5
GM
5416 move_it_vertically (it, target_y - it->current_y);
5417 xassert (IT_CHARPOS (*it) >= BEGV);
5418 }
5419 else if (target_y >= it->current_y + line_height
5420 && IT_CHARPOS (*it) < ZV)
5421 {
55591976
GM
5422 /* Should move forward by at least one line, maybe more.
5423
5424 Note: Calling move_it_by_lines can be expensive on
5425 terminal frames, where compute_motion is used (via
5426 vmotion) to do the job, when there are very long lines
5427 and truncate-lines is nil. That's the reason for
5428 treating terminal frames specially here. */
5429
5430 if (!FRAME_WINDOW_P (it->f))
5431 move_it_vertically (it, target_y - (it->current_y + line_height));
5432 else
f7ccfc8c 5433 {
55591976
GM
5434 do
5435 {
5436 move_it_by_lines (it, 1, 1);
5437 }
5438 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 5439 }
f7ccfc8c 5440
5f5c8ee5
GM
5441 xassert (IT_CHARPOS (*it) >= BEGV);
5442 }
5443 }
5444}
5445
5446
5447/* Move IT by a specified amount of pixel lines DY. DY negative means
5448 move backwards. DY = 0 means move to start of screen line. At the
5449 end, IT will be on the start of a screen line. */
5450
5451void
5452move_it_vertically (it, dy)
5453 struct it *it;
5454 int dy;
5455{
5456 if (dy <= 0)
5457 move_it_vertically_backward (it, -dy);
5458 else if (dy > 0)
5459 {
47589c8c 5460 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5461 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5462 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5463 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5464
5465 /* If buffer ends in ZV without a newline, move to the start of
5466 the line to satisfy the post-condition. */
5467 if (IT_CHARPOS (*it) == ZV
5468 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5469 move_it_by_lines (it, 0, 0);
5470 }
5471}
5472
5473
47fc2c10
GM
5474/* Move iterator IT past the end of the text line it is in. */
5475
5476void
5477move_it_past_eol (it)
5478 struct it *it;
5479{
5480 enum move_it_result rc;
5481
5482 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5483 if (rc == MOVE_NEWLINE_OR_CR)
5484 set_iterator_to_next (it, 0);
5485}
5486
5487
2c79b732
GM
5488#if 0 /* Currently not used. */
5489
5f5c8ee5
GM
5490/* Return non-zero if some text between buffer positions START_CHARPOS
5491 and END_CHARPOS is invisible. IT->window is the window for text
5492 property lookup. */
5493
5494static int
5495invisible_text_between_p (it, start_charpos, end_charpos)
5496 struct it *it;
5497 int start_charpos, end_charpos;
5498{
5f5c8ee5
GM
5499 Lisp_Object prop, limit;
5500 int invisible_found_p;
5501
5502 xassert (it != NULL && start_charpos <= end_charpos);
5503
5504 /* Is text at START invisible? */
5505 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5506 it->window);
5507 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5508 invisible_found_p = 1;
5509 else
5510 {
016b5642
MB
5511 limit = Fnext_single_char_property_change (make_number (start_charpos),
5512 Qinvisible, Qnil,
5513 make_number (end_charpos));
5f5c8ee5
GM
5514 invisible_found_p = XFASTINT (limit) < end_charpos;
5515 }
5516
5517 return invisible_found_p;
5f5c8ee5
GM
5518}
5519
2c79b732
GM
5520#endif /* 0 */
5521
5f5c8ee5
GM
5522
5523/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5524 negative means move up. DVPOS == 0 means move to the start of the
5525 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5526 NEED_Y_P is zero, IT->current_y will be left unchanged.
5527
5528 Further optimization ideas: If we would know that IT->f doesn't use
5529 a face with proportional font, we could be faster for
5530 truncate-lines nil. */
5531
5532void
5533move_it_by_lines (it, dvpos, need_y_p)
5534 struct it *it;
5535 int dvpos, need_y_p;
5536{
5537 struct position pos;
5538
5539 if (!FRAME_WINDOW_P (it->f))
5540 {
5541 struct text_pos textpos;
5542
5543 /* We can use vmotion on frames without proportional fonts. */
5544 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5545 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5546 reseat (it, textpos, 1);
5547 it->vpos += pos.vpos;
5548 it->current_y += pos.vpos;
5549 }
5550 else if (dvpos == 0)
5551 {
5552 /* DVPOS == 0 means move to the start of the screen line. */
5553 move_it_vertically_backward (it, 0);
5554 xassert (it->current_x == 0 && it->hpos == 0);
5555 }
5556 else if (dvpos > 0)
2c79b732 5557 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5558 else
5559 {
5560 struct it it2;
5561 int start_charpos, i;
2c79b732 5562
e8660d73
GM
5563 /* Start at the beginning of the screen line containing IT's
5564 position. */
5565 move_it_vertically_backward (it, 0);
b0e619b4 5566
5f5c8ee5
GM
5567 /* Go back -DVPOS visible lines and reseat the iterator there. */
5568 start_charpos = IT_CHARPOS (*it);
5569 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5570 back_to_previous_visible_line_start (it);
5571 reseat (it, it->current.pos, 1);
5572 it->current_x = it->hpos = 0;
5573
5574 /* Above call may have moved too far if continuation lines
5575 are involved. Scan forward and see if it did. */
5576 it2 = *it;
5577 it2.vpos = it2.current_y = 0;
5578 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5579 it->vpos -= it2.vpos;
5580 it->current_y -= it2.current_y;
5581 it->current_x = it->hpos = 0;
5582
5583 /* If we moved too far, move IT some lines forward. */
5584 if (it2.vpos > -dvpos)
5585 {
5586 int delta = it2.vpos + dvpos;
5587 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5588 }
5589 }
5590}
5591
5592
5593\f
5594/***********************************************************************
5595 Messages
5596 ***********************************************************************/
5597
5598
937248bc
GM
5599/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5600 to *Messages*. */
5601
5602void
5603add_to_log (format, arg1, arg2)
5604 char *format;
5605 Lisp_Object arg1, arg2;
5606{
5607 Lisp_Object args[3];
5608 Lisp_Object msg, fmt;
5609 char *buffer;
5610 int len;
5611 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5612
ae794295
GM
5613 /* Do nothing if called asynchronously. Inserting text into
5614 a buffer may call after-change-functions and alike and
5615 that would means running Lisp asynchronously. */
5616 if (handling_signal)
5617 return;
5618
937248bc
GM
5619 fmt = msg = Qnil;
5620 GCPRO4 (fmt, msg, arg1, arg2);
5621
5622 args[0] = fmt = build_string (format);
5623 args[1] = arg1;
5624 args[2] = arg2;
6fc556fd 5625 msg = Fformat (3, args);
937248bc 5626
2051c264 5627 len = SBYTES (msg) + 1;
937248bc 5628 buffer = (char *) alloca (len);
2051c264 5629 bcopy (SDATA (msg), buffer, len);
937248bc 5630
796184bc 5631 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5632 UNGCPRO;
5633}
5634
5635
5f5c8ee5
GM
5636/* Output a newline in the *Messages* buffer if "needs" one. */
5637
5638void
5639message_log_maybe_newline ()
5640{
5641 if (message_log_need_newline)
5642 message_dolog ("", 0, 1, 0);
5643}
5644
5645
1e313f28 5646/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5647 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5648 nonzero, means interpret the contents of M as multibyte. This
5649 function calls low-level routines in order to bypass text property
5650 hooks, etc. which might not be safe to run. */
5651
5652void
1e313f28 5653message_dolog (m, nbytes, nlflag, multibyte)
5f5c8ee5 5654 char *m;
1e313f28 5655 int nbytes, nlflag, multibyte;
5f5c8ee5
GM
5656{
5657 if (!NILP (Vmessage_log_max))
5658 {
5659 struct buffer *oldbuf;
5660 Lisp_Object oldpoint, oldbegv, oldzv;
5661 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5662 int point_at_end = 0;
5663 int zv_at_end = 0;
5664 Lisp_Object old_deactivate_mark, tem;
6052529b 5665 struct gcpro gcpro1;
5f5c8ee5
GM
5666
5667 old_deactivate_mark = Vdeactivate_mark;
5668 oldbuf = current_buffer;
6a94510a 5669 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5670 current_buffer->undo_list = Qt;
5671
b14bc55e
RS
5672 oldpoint = message_dolog_marker1;
5673 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5674 oldbegv = message_dolog_marker2;
5675 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5676 oldzv = message_dolog_marker3;
5677 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5678 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
5679
5680 if (PT == Z)
5681 point_at_end = 1;
5682 if (ZV == Z)
5683 zv_at_end = 1;
5684
5685 BEGV = BEG;
5686 BEGV_BYTE = BEG_BYTE;
5687 ZV = Z;
5688 ZV_BYTE = Z_BYTE;
5689 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5690
5691 /* Insert the string--maybe converting multibyte to single byte
5692 or vice versa, so that all the text fits the buffer. */
5693 if (multibyte
5694 && NILP (current_buffer->enable_multibyte_characters))
5695 {
1e313f28 5696 int i, c, char_bytes;
5f5c8ee5
GM
5697 unsigned char work[1];
5698
5699 /* Convert a multibyte string to single-byte
5700 for the *Message* buffer. */
1e313f28 5701 for (i = 0; i < nbytes; i += nbytes)
5f5c8ee5 5702 {
1e313f28 5703 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5704 work[0] = (SINGLE_BYTE_CHAR_P (c)
5705 ? c
5706 : multibyte_char_to_unibyte (c, Qnil));
5707 insert_1_both (work, 1, 1, 1, 0, 0);
5708 }
5709 }
5710 else if (! multibyte
5711 && ! NILP (current_buffer->enable_multibyte_characters))
5712 {
1e313f28 5713 int i, c, char_bytes;
5f5c8ee5 5714 unsigned char *msg = (unsigned char *) m;
260a86a0 5715 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5716 /* Convert a single-byte string to multibyte
5717 for the *Message* buffer. */
1e313f28 5718 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5719 {
5720 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5721 char_bytes = CHAR_STRING (c, str);
5722 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5723 }
5724 }
1e313f28
GM
5725 else if (nbytes)
5726 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5727
5728 if (nlflag)
5729 {
5730 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5731 insert_1 ("\n", 1, 1, 0, 0);
5732
5733 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5734 this_bol = PT;
5735 this_bol_byte = PT_BYTE;
5736
b14bc55e
RS
5737 /* See if this line duplicates the previous one.
5738 If so, combine duplicates. */
5f5c8ee5
GM
5739 if (this_bol > BEG)
5740 {
5741 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5742 prev_bol = PT;
5743 prev_bol_byte = PT_BYTE;
5744
5745 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5746 this_bol, this_bol_byte);
5747 if (dup)
5748 {
5749 del_range_both (prev_bol, prev_bol_byte,
5750 this_bol, this_bol_byte, 0);
5751 if (dup > 1)
5752 {
5753 char dupstr[40];
5754 int duplen;
5755
5756 /* If you change this format, don't forget to also
5757 change message_log_check_duplicate. */
5758 sprintf (dupstr, " [%d times]", dup);
5759 duplen = strlen (dupstr);
5760 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5761 insert_1 (dupstr, duplen, 1, 0, 1);
5762 }
5763 }
5764 }
5765
b14bc55e
RS
5766 /* If we have more than the desired maximum number of lines
5767 in the *Messages* buffer now, delete the oldest ones.
5768 This is safe because we don't have undo in this buffer. */
5769
5f5c8ee5
GM
5770 if (NATNUMP (Vmessage_log_max))
5771 {
5772 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5773 -XFASTINT (Vmessage_log_max) - 1, 0);
5774 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5775 }
5776 }
5777 BEGV = XMARKER (oldbegv)->charpos;
5778 BEGV_BYTE = marker_byte_position (oldbegv);
5779
5780 if (zv_at_end)
5781 {
5782 ZV = Z;
5783 ZV_BYTE = Z_BYTE;
5784 }
5785 else
5786 {
5787 ZV = XMARKER (oldzv)->charpos;
5788 ZV_BYTE = marker_byte_position (oldzv);
5789 }
5790
5791 if (point_at_end)
5792 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5793 else
5794 /* We can't do Fgoto_char (oldpoint) because it will run some
5795 Lisp code. */
5796 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5797 XMARKER (oldpoint)->bytepos);
5798
5799 UNGCPRO;
b14bc55e
RS
5800 unchain_marker (oldpoint);
5801 unchain_marker (oldbegv);
5802 unchain_marker (oldzv);
5f5c8ee5
GM
5803
5804 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5805 set_buffer_internal (oldbuf);
5806 if (NILP (tem))
5807 windows_or_buffers_changed = old_windows_or_buffers_changed;
5808 message_log_need_newline = !nlflag;
5809 Vdeactivate_mark = old_deactivate_mark;
5810 }
5811}
5812
5813
5814/* We are at the end of the buffer after just having inserted a newline.
5815 (Note: We depend on the fact we won't be crossing the gap.)
5816 Check to see if the most recent message looks a lot like the previous one.
5817 Return 0 if different, 1 if the new one should just replace it, or a
5818 value N > 1 if we should also append " [N times]". */
5819
5820static int
5821message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5822 int prev_bol, this_bol;
5823 int prev_bol_byte, this_bol_byte;
5824{
5825 int i;
5826 int len = Z_BYTE - 1 - this_bol_byte;
5827 int seen_dots = 0;
5828 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5829 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5830
5831 for (i = 0; i < len; i++)
5832 {
509633e3 5833 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5834 seen_dots = 1;
5835 if (p1[i] != p2[i])
5836 return seen_dots;
5837 }
5838 p1 += len;
5839 if (*p1 == '\n')
5840 return 2;
5841 if (*p1++ == ' ' && *p1++ == '[')
5842 {
5843 int n = 0;
5844 while (*p1 >= '0' && *p1 <= '9')
5845 n = n * 10 + *p1++ - '0';
5846 if (strncmp (p1, " times]\n", 8) == 0)
5847 return n+1;
5848 }
5849 return 0;
5850}
5851
5852
1e313f28
GM
5853/* Display an echo area message M with a specified length of NBYTES
5854 bytes. The string may include null characters. If M is 0, clear
5855 out any existing message, and let the mini-buffer text show
5856 through.
5f5c8ee5
GM
5857
5858 The buffer M must continue to exist until after the echo area gets
5859 cleared or some other message gets displayed there. This means do
5860 not pass text that is stored in a Lisp string; do not pass text in
5861 a buffer that was alloca'd. */
5862
5863void
1e313f28 5864message2 (m, nbytes, multibyte)
5f5c8ee5 5865 char *m;
1e313f28 5866 int nbytes;
5f5c8ee5
GM
5867 int multibyte;
5868{
5869 /* First flush out any partial line written with print. */
5870 message_log_maybe_newline ();
5871 if (m)
1e313f28
GM
5872 message_dolog (m, nbytes, 1, multibyte);
5873 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5874}
5875
5876
5877/* The non-logging counterpart of message2. */
5878
5879void
1e313f28 5880message2_nolog (m, nbytes, multibyte)
5f5c8ee5 5881 char *m;
1e313f28 5882 int nbytes;
5f5c8ee5 5883{
886bd6f2 5884 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5885 message_enable_multibyte = multibyte;
5886
5887 if (noninteractive)
5888 {
5889 if (noninteractive_need_newline)
5890 putc ('\n', stderr);
5891 noninteractive_need_newline = 0;
5892 if (m)
1e313f28 5893 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
5894 if (cursor_in_echo_area == 0)
5895 fprintf (stderr, "\n");
5896 fflush (stderr);
5897 }
5898 /* A null message buffer means that the frame hasn't really been
5899 initialized yet. Error messages get reported properly by
5900 cmd_error, so this must be just an informative message; toss it. */
5901 else if (INTERACTIVE
886bd6f2
GM
5902 && sf->glyphs_initialized_p
5903 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5904 {
5905 Lisp_Object mini_window;
5906 struct frame *f;
5907
5908 /* Get the frame containing the mini-buffer
5909 that the selected frame is using. */
886bd6f2 5910 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5911 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5912
5913 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5914 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5915 && ! FRAME_VISIBLE_P (f))
5916 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5917
5918 if (m)
5919 {
1e313f28 5920 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5921 if (minibuffer_auto_raise)
5922 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5923 }
5924 else
c6e89d6c 5925 clear_message (1, 1);
5f5c8ee5 5926
c6e89d6c 5927 do_pending_window_change (0);
5f5c8ee5 5928 echo_area_display (1);
c6e89d6c 5929 do_pending_window_change (0);
5f5c8ee5
GM
5930 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5931 (*frame_up_to_date_hook) (f);
5932 }
5933}
5934
5935
c6e89d6c
GM
5936/* Display an echo area message M with a specified length of NBYTES
5937 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5938 string, clear out any existing message, and let the mini-buffer
5939 text show through. */
5940
5941void
c6e89d6c 5942message3 (m, nbytes, multibyte)
5f5c8ee5 5943 Lisp_Object m;
c6e89d6c 5944 int nbytes;
5f5c8ee5
GM
5945 int multibyte;
5946{
5947 struct gcpro gcpro1;
5948
5949 GCPRO1 (m);
5950
5951 /* First flush out any partial line written with print. */
5952 message_log_maybe_newline ();
5953 if (STRINGP (m))
2051c264 5954 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 5955 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5956
5957 UNGCPRO;
5958}
5959
5960
5961/* The non-logging version of message3. */
5962
5963void
c6e89d6c 5964message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5965 Lisp_Object m;
c6e89d6c 5966 int nbytes, multibyte;
5f5c8ee5 5967{
886bd6f2 5968 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5969 message_enable_multibyte = multibyte;
5970
5971 if (noninteractive)
5972 {
5973 if (noninteractive_need_newline)
5974 putc ('\n', stderr);
5975 noninteractive_need_newline = 0;
5976 if (STRINGP (m))
2051c264 5977 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
5978 if (cursor_in_echo_area == 0)
5979 fprintf (stderr, "\n");
5980 fflush (stderr);
5981 }
5982 /* A null message buffer means that the frame hasn't really been
5983 initialized yet. Error messages get reported properly by
5984 cmd_error, so this must be just an informative message; toss it. */
5985 else if (INTERACTIVE
886bd6f2
GM
5986 && sf->glyphs_initialized_p
5987 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5988 {
5989 Lisp_Object mini_window;
c6e89d6c 5990 Lisp_Object frame;
5f5c8ee5
GM
5991 struct frame *f;
5992
5993 /* Get the frame containing the mini-buffer
5994 that the selected frame is using. */
886bd6f2 5995 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5996 frame = XWINDOW (mini_window)->frame;
5997 f = XFRAME (frame);
5f5c8ee5
GM
5998
5999 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6000 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6001 && !FRAME_VISIBLE_P (f))
6002 Fmake_frame_visible (frame);
5f5c8ee5 6003
2051c264 6004 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6005 {
c6e89d6c 6006 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6007 if (minibuffer_auto_raise)
6008 Fraise_frame (frame);
5f5c8ee5
GM
6009 }
6010 else
c6e89d6c 6011 clear_message (1, 1);
5f5c8ee5 6012
c6e89d6c 6013 do_pending_window_change (0);
5f5c8ee5 6014 echo_area_display (1);
c6e89d6c 6015 do_pending_window_change (0);
5f5c8ee5
GM
6016 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6017 (*frame_up_to_date_hook) (f);
6018 }
6019}
6020
6021
6022/* Display a null-terminated echo area message M. If M is 0, clear
6023 out any existing message, and let the mini-buffer text show through.
6024
6025 The buffer M must continue to exist until after the echo area gets
6026 cleared or some other message gets displayed there. Do not pass
6027 text that is stored in a Lisp string. Do not pass text in a buffer
6028 that was alloca'd. */
6029
6030void
6031message1 (m)
6032 char *m;
6033{
6034 message2 (m, (m ? strlen (m) : 0), 0);
6035}
6036
6037
6038/* The non-logging counterpart of message1. */
6039
6040void
6041message1_nolog (m)
6042 char *m;
6043{
6044 message2_nolog (m, (m ? strlen (m) : 0), 0);
6045}
6046
6047/* Display a message M which contains a single %s
6048 which gets replaced with STRING. */
6049
6050void
6051message_with_string (m, string, log)
6052 char *m;
6053 Lisp_Object string;
6054 int log;
6055{
6056 if (noninteractive)
6057 {
6058 if (m)
6059 {
6060 if (noninteractive_need_newline)
6061 putc ('\n', stderr);
6062 noninteractive_need_newline = 0;
2051c264 6063 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6064 if (cursor_in_echo_area == 0)
6065 fprintf (stderr, "\n");
6066 fflush (stderr);
6067 }
6068 }
6069 else if (INTERACTIVE)
6070 {
6071 /* The frame whose minibuffer we're going to display the message on.
6072 It may be larger than the selected frame, so we need
6073 to use its buffer, not the selected frame's buffer. */
6074 Lisp_Object mini_window;
886bd6f2 6075 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6076
6077 /* Get the frame containing the minibuffer
6078 that the selected frame is using. */
886bd6f2 6079 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6080 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6081
6082 /* A null message buffer means that the frame hasn't really been
6083 initialized yet. Error messages get reported properly by
6084 cmd_error, so this must be just an informative message; toss it. */
6085 if (FRAME_MESSAGE_BUF (f))
6086 {
eb484132
GM
6087 Lisp_Object args[2], message;
6088 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6089
eb484132
GM
6090 args[0] = build_string (m);
6091 args[1] = message = string;
78e17433 6092 GCPRO2 (args[0], message);
eb484132
GM
6093 gcpro1.nvars = 2;
6094
6095 message = Fformat (2, args);
5f5c8ee5
GM
6096
6097 if (log)
2051c264 6098 message3 (message, SBYTES (message), SMBP (message));
5f5c8ee5 6099 else
2051c264 6100 message3_nolog (message, SBYTES (message), SMBP (message));
eb484132
GM
6101
6102 UNGCPRO;
5f5c8ee5
GM
6103
6104 /* Print should start at the beginning of the message
6105 buffer next time. */
6106 message_buf_print = 0;
6107 }
6108 }
6109}
6110
6111
5f5c8ee5
GM
6112/* Dump an informative message to the minibuf. If M is 0, clear out
6113 any existing message, and let the mini-buffer text show through. */
6114
6115/* VARARGS 1 */
6116void
6117message (m, a1, a2, a3)
6118 char *m;
6119 EMACS_INT a1, a2, a3;
6120{
6121 if (noninteractive)
6122 {
6123 if (m)
6124 {
6125 if (noninteractive_need_newline)
6126 putc ('\n', stderr);
6127 noninteractive_need_newline = 0;
6128 fprintf (stderr, m, a1, a2, a3);
6129 if (cursor_in_echo_area == 0)
6130 fprintf (stderr, "\n");
6131 fflush (stderr);
6132 }
6133 }
6134 else if (INTERACTIVE)
6135 {
6136 /* The frame whose mini-buffer we're going to display the message
6137 on. It may be larger than the selected frame, so we need to
6138 use its buffer, not the selected frame's buffer. */
6139 Lisp_Object mini_window;
886bd6f2 6140 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6141
6142 /* Get the frame containing the mini-buffer
6143 that the selected frame is using. */
886bd6f2 6144 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6145 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6146
6147 /* A null message buffer means that the frame hasn't really been
6148 initialized yet. Error messages get reported properly by
6149 cmd_error, so this must be just an informative message; toss
6150 it. */
6151 if (FRAME_MESSAGE_BUF (f))
6152 {
6153 if (m)
6154 {
6155 int len;
6156#ifdef NO_ARG_ARRAY
6157 char *a[3];
6158 a[0] = (char *) a1;
6159 a[1] = (char *) a2;
6160 a[2] = (char *) a3;
6161
6162 len = doprnt (FRAME_MESSAGE_BUF (f),
6163 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6164#else
6165 len = doprnt (FRAME_MESSAGE_BUF (f),
6166 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6167 (char **) &a1);
6168#endif /* NO_ARG_ARRAY */
6169
6170 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6171 }
6172 else
6173 message1 (0);
6174
6175 /* Print should start at the beginning of the message
6176 buffer next time. */
6177 message_buf_print = 0;
6178 }
6179 }
6180}
6181
6182
6183/* The non-logging version of message. */
6184
6185void
6186message_nolog (m, a1, a2, a3)
6187 char *m;
6188 EMACS_INT a1, a2, a3;
6189{
6190 Lisp_Object old_log_max;
6191 old_log_max = Vmessage_log_max;
6192 Vmessage_log_max = Qnil;
6193 message (m, a1, a2, a3);
6194 Vmessage_log_max = old_log_max;
6195}
6196
6197
c6e89d6c
GM
6198/* Display the current message in the current mini-buffer. This is
6199 only called from error handlers in process.c, and is not time
6200 critical. */
5f5c8ee5
GM
6201
6202void
6203update_echo_area ()
6204{
c6e89d6c
GM
6205 if (!NILP (echo_area_buffer[0]))
6206 {
6207 Lisp_Object string;
6208 string = Fcurrent_message ();
2051c264 6209 message3 (string, SBYTES (string),
c6e89d6c
GM
6210 !NILP (current_buffer->enable_multibyte_characters));
6211 }
6212}
6213
6214
5bcfeb49
GM
6215/* Make sure echo area buffers in echo_buffers[] are life. If they
6216 aren't, make new ones. */
6217
6218static void
6219ensure_echo_area_buffers ()
6220{
6221 int i;
6222
6223 for (i = 0; i < 2; ++i)
6224 if (!BUFFERP (echo_buffer[i])
6225 || NILP (XBUFFER (echo_buffer[i])->name))
6226 {
6227 char name[30];
ff3d9573
GM
6228 Lisp_Object old_buffer;
6229 int j;
6230
6231 old_buffer = echo_buffer[i];
5bcfeb49
GM
6232 sprintf (name, " *Echo Area %d*", i);
6233 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6234 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6235
6236 for (j = 0; j < 2; ++j)
6237 if (EQ (old_buffer, echo_area_buffer[j]))
6238 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6239 }
6240}
6241
6242
23a96c77 6243/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6244 echo_area_buffer as current buffer.
6245
6246 WHICH zero means use the current message buffer
6247 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6248 from echo_buffer[] and clear it.
6249
6250 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6251 suitable buffer from echo_buffer[] and clear it.
6252
6253 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6254 that the current message becomes the last displayed one, make
6255 choose a suitable buffer for echo_area_buffer[0], and clear it.
6256
4b41cebb 6257 Value is what FN returns. */
c6e89d6c
GM
6258
6259static int
23a96c77 6260with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6261 struct window *w;
6262 int which;
23dd2d97
KR
6263 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6264 EMACS_INT a1;
6265 Lisp_Object a2;
6266 EMACS_INT a3, a4;
c6e89d6c
GM
6267{
6268 Lisp_Object buffer;
15e26c76 6269 int this_one, the_other, clear_buffer_p, rc;
2d27dae2 6270 int count = BINDING_STACK_SIZE ();
c6e89d6c
GM
6271
6272 /* If buffers aren't life, make new ones. */
5bcfeb49 6273 ensure_echo_area_buffers ();
c6e89d6c
GM
6274
6275 clear_buffer_p = 0;
6276
6277 if (which == 0)
6278 this_one = 0, the_other = 1;
6279 else if (which > 0)
6280 this_one = 1, the_other = 0;
5f5c8ee5 6281 else
c6e89d6c
GM
6282 {
6283 this_one = 0, the_other = 1;
6284 clear_buffer_p = 1;
6285
6286 /* We need a fresh one in case the current echo buffer equals
6287 the one containing the last displayed echo area message. */
6288 if (!NILP (echo_area_buffer[this_one])
6289 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6290 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6291 }
6292
6293 /* Choose a suitable buffer from echo_buffer[] is we don't
6294 have one. */
6295 if (NILP (echo_area_buffer[this_one]))
6296 {
6297 echo_area_buffer[this_one]
6298 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6299 ? echo_buffer[the_other]
6300 : echo_buffer[this_one]);
6301 clear_buffer_p = 1;
6302 }
6303
6304 buffer = echo_area_buffer[this_one];
6305
1013f4e3
GM
6306 /* Don't get confused by reusing the buffer used for echoing
6307 for a different purpose. */
032906b1 6308 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6309 cancel_echoing ();
6310
c6e89d6c
GM
6311 record_unwind_protect (unwind_with_echo_area_buffer,
6312 with_echo_area_buffer_unwind_data (w));
6313
6314 /* Make the echo area buffer current. Note that for display
6315 purposes, it is not necessary that the displayed window's buffer
6316 == current_buffer, except for text property lookup. So, let's
6317 only set that buffer temporarily here without doing a full
6318 Fset_window_buffer. We must also change w->pointm, though,
6319 because otherwise an assertions in unshow_buffer fails, and Emacs
6320 aborts. */
9142dd5b 6321 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6322 if (w)
6323 {
6324 w->buffer = buffer;
6325 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6326 }
ad4f174e 6327
c6e89d6c
GM
6328 current_buffer->undo_list = Qt;
6329 current_buffer->read_only = Qnil;
bbbf6d06 6330 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6331 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6332
6333 if (clear_buffer_p && Z > BEG)
6334 del_range (BEG, Z);
6335
6336 xassert (BEGV >= BEG);
6337 xassert (ZV <= Z && ZV >= BEGV);
6338
23a96c77 6339 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6340
6341 xassert (BEGV >= BEG);
6342 xassert (ZV <= Z && ZV >= BEGV);
6343
6344 unbind_to (count, Qnil);
6345 return rc;
5f5c8ee5
GM
6346}
6347
6348
c6e89d6c
GM
6349/* Save state that should be preserved around the call to the function
6350 FN called in with_echo_area_buffer. */
5f5c8ee5 6351
c6e89d6c
GM
6352static Lisp_Object
6353with_echo_area_buffer_unwind_data (w)
6354 struct window *w;
5f5c8ee5 6355{
c6e89d6c
GM
6356 int i = 0;
6357 Lisp_Object vector;
5f5c8ee5 6358
c6e89d6c
GM
6359 /* Reduce consing by keeping one vector in
6360 Vwith_echo_area_save_vector. */
6361 vector = Vwith_echo_area_save_vector;
6362 Vwith_echo_area_save_vector = Qnil;
6363
6364 if (NILP (vector))
9142dd5b 6365 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c 6366
a61b7058
GM
6367 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6368 AREF (vector, i) = Vdeactivate_mark, ++i;
6369 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
c6e89d6c
GM
6370
6371 if (w)
6372 {
a61b7058
GM
6373 XSETWINDOW (AREF (vector, i), w); ++i;
6374 AREF (vector, i) = w->buffer; ++i;
6375 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6376 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6377 }
6378 else
6379 {
6380 int end = i + 4;
a61b7058
GM
6381 for (; i < end; ++i)
6382 AREF (vector, i) = Qnil;
c6e89d6c 6383 }
5f5c8ee5 6384
a61b7058 6385 xassert (i == ASIZE (vector));
c6e89d6c
GM
6386 return vector;
6387}
5f5c8ee5 6388
5f5c8ee5 6389
c6e89d6c
GM
6390/* Restore global state from VECTOR which was created by
6391 with_echo_area_buffer_unwind_data. */
6392
6393static Lisp_Object
6394unwind_with_echo_area_buffer (vector)
6395 Lisp_Object vector;
6396{
bbbf6d06
GM
6397 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6398 Vdeactivate_mark = AREF (vector, 1);
6399 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6400
bbbf6d06 6401 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6402 {
6403 struct window *w;
6404 Lisp_Object buffer, charpos, bytepos;
6405
bbbf6d06
GM
6406 w = XWINDOW (AREF (vector, 3));
6407 buffer = AREF (vector, 4);
6408 charpos = AREF (vector, 5);
6409 bytepos = AREF (vector, 6);
c6e89d6c
GM
6410
6411 w->buffer = buffer;
6412 set_marker_both (w->pointm, buffer,
6413 XFASTINT (charpos), XFASTINT (bytepos));
6414 }
6415
6416 Vwith_echo_area_save_vector = vector;
6417 return Qnil;
6418}
6419
6420
6421/* Set up the echo area for use by print functions. MULTIBYTE_P
6422 non-zero means we will print multibyte. */
6423
6424void
6425setup_echo_area_for_printing (multibyte_p)
6426 int multibyte_p;
6427{
5bcfeb49
GM
6428 ensure_echo_area_buffers ();
6429
c6e89d6c
GM
6430 if (!message_buf_print)
6431 {
6432 /* A message has been output since the last time we printed.
6433 Choose a fresh echo area buffer. */
6434 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6435 echo_area_buffer[0] = echo_buffer[1];
6436 else
6437 echo_area_buffer[0] = echo_buffer[0];
6438
6439 /* Switch to that buffer and clear it. */
6440 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6441 current_buffer->truncate_lines = Qnil;
bbbf6d06 6442
c6e89d6c 6443 if (Z > BEG)
bbbf6d06
GM
6444 {
6445 int count = BINDING_STACK_SIZE ();
6446 specbind (Qinhibit_read_only, Qt);
6447 del_range (BEG, Z);
6448 unbind_to (count, Qnil);
6449 }
c6e89d6c
GM
6450 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6451
6452 /* Set up the buffer for the multibyteness we need. */
6453 if (multibyte_p
6454 != !NILP (current_buffer->enable_multibyte_characters))
6455 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6456
6457 /* Raise the frame containing the echo area. */
6458 if (minibuffer_auto_raise)
6459 {
886bd6f2 6460 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6461 Lisp_Object mini_window;
886bd6f2 6462 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6463 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6464 }
6465
8a4e3c0c 6466 message_log_maybe_newline ();
c6e89d6c
GM
6467 message_buf_print = 1;
6468 }
fa77249f
GM
6469 else
6470 {
6471 if (NILP (echo_area_buffer[0]))
6472 {
6473 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6474 echo_area_buffer[0] = echo_buffer[1];
6475 else
6476 echo_area_buffer[0] = echo_buffer[0];
6477 }
6478
6479 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
6480 {
6481 /* Someone switched buffers between print requests. */
6482 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6483 current_buffer->truncate_lines = Qnil;
6484 }
fa77249f 6485 }
c6e89d6c
GM
6486}
6487
6488
dd2eb166
GM
6489/* Display an echo area message in window W. Value is non-zero if W's
6490 height is changed. If display_last_displayed_message_p is
6491 non-zero, display the message that was last displayed, otherwise
6492 display the current message. */
c6e89d6c
GM
6493
6494static int
6495display_echo_area (w)
6496 struct window *w;
6497{
25edb08f
GM
6498 int i, no_message_p, window_height_changed_p, count;
6499
6500 /* Temporarily disable garbage collections while displaying the echo
6501 area. This is done because a GC can print a message itself.
6502 That message would modify the echo area buffer's contents while a
6503 redisplay of the buffer is going on, and seriously confuse
6504 redisplay. */
6505 count = inhibit_garbage_collection ();
dd2eb166
GM
6506
6507 /* If there is no message, we must call display_echo_area_1
6508 nevertheless because it resizes the window. But we will have to
6509 reset the echo_area_buffer in question to nil at the end because
6510 with_echo_area_buffer will sets it to an empty buffer. */
6511 i = display_last_displayed_message_p ? 1 : 0;
6512 no_message_p = NILP (echo_area_buffer[i]);
6513
6514 window_height_changed_p
6515 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6516 display_echo_area_1,
23dd2d97 6517 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6518
6519 if (no_message_p)
6520 echo_area_buffer[i] = Qnil;
25edb08f
GM
6521
6522 unbind_to (count, Qnil);
dd2eb166 6523 return window_height_changed_p;
c6e89d6c
GM
6524}
6525
6526
6527/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6528 contains the current echo area message in window W, a mini-window,
6529 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6530 Change the height of W so that all of the message is displayed.
6531 Value is non-zero if height of W was changed. */
6532
6533static int
23a96c77 6534display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6535 EMACS_INT a1;
6536 Lisp_Object a2;
6537 EMACS_INT a3, a4;
c6e89d6c 6538{
23a96c77 6539 struct window *w = (struct window *) a1;
c6e89d6c 6540 Lisp_Object window;
c6e89d6c
GM
6541 struct text_pos start;
6542 int window_height_changed_p = 0;
6543
6544 /* Do this before displaying, so that we have a large enough glyph
6545 matrix for the display. */
92a90e89 6546 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6547
6548 /* Display. */
6549 clear_glyph_matrix (w->desired_matrix);
6550 XSETWINDOW (window, w);
6551 SET_TEXT_POS (start, BEG, BEG_BYTE);
6552 try_window (window, start);
6553
c6e89d6c
GM
6554 return window_height_changed_p;
6555}
6556
6557
92a90e89 6558/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
6559 currently displayed message, if there is one. If a mini-buffer
6560 is active, don't shrink it. */
92a90e89
GM
6561
6562void
308a74d8 6563resize_echo_area_exactly ()
92a90e89
GM
6564{
6565 if (BUFFERP (echo_area_buffer[0])
6566 && WINDOWP (echo_area_window))
6567 {
6568 struct window *w = XWINDOW (echo_area_window);
6569 int resized_p;
6d004fea
GM
6570 Lisp_Object resize_exactly;
6571
6572 if (minibuf_level == 0)
6573 resize_exactly = Qt;
6574 else
6575 resize_exactly = Qnil;
92a90e89 6576
23a96c77 6577 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 6578 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
6579 if (resized_p)
6580 {
6581 ++windows_or_buffers_changed;
6582 ++update_mode_lines;
6583 redisplay_internal (0);
6584 }
6585 }
6586}
6587
6588
23a96c77 6589/* Callback function for with_echo_area_buffer, when used from
308a74d8 6590 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
6591 resize, EXACTLY non-nil means resize the mini-window exactly to the
6592 size of the text displayed. A3 and A4 are not used. Value is what
6593 resize_mini_window returns. */
23a96c77
GM
6594
6595static int
6d004fea 6596resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 6597 EMACS_INT a1;
6d004fea 6598 Lisp_Object exactly;
23dd2d97 6599 EMACS_INT a3, a4;
23a96c77 6600{
6d004fea 6601 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
6602}
6603
6604
92a90e89
GM
6605/* Resize mini-window W to fit the size of its contents. EXACT:P
6606 means size the window exactly to the size needed. Otherwise, it's
6607 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 6608 the window height has been changed. */
c6e89d6c 6609
9472f927 6610int
92a90e89 6611resize_mini_window (w, exact_p)
c6e89d6c 6612 struct window *w;
92a90e89 6613 int exact_p;
c6e89d6c
GM
6614{
6615 struct frame *f = XFRAME (w->frame);
6616 int window_height_changed_p = 0;
6617
6618 xassert (MINI_WINDOW_P (w));
97cafc0f 6619
2913a9c0
GM
6620 /* Don't resize windows while redisplaying a window; it would
6621 confuse redisplay functions when the size of the window they are
6622 displaying changes from under them. Such a resizing can happen,
6623 for instance, when which-func prints a long message while
6624 we are running fontification-functions. We're running these
6625 functions with safe_call which binds inhibit-redisplay to t. */
6626 if (!NILP (Vinhibit_redisplay))
64c5be50
GM
6627 return 0;
6628
97cafc0f 6629 /* Nil means don't try to resize. */
6422c1d7 6630 if (NILP (Vresize_mini_windows)
00f6d59e 6631 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6632 return 0;
c6e89d6c
GM
6633
6634 if (!FRAME_MINIBUF_ONLY_P (f))
6635 {
6636 struct it it;
dd2eb166
GM
6637 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6638 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6639 int height, max_height;
6640 int unit = CANON_Y_UNIT (f);
6641 struct text_pos start;
1bfdbe43
GM
6642 struct buffer *old_current_buffer = NULL;
6643
6644 if (current_buffer != XBUFFER (w->buffer))
6645 {
6646 old_current_buffer = current_buffer;
6647 set_buffer_internal (XBUFFER (w->buffer));
6648 }
9142dd5b 6649
c6e89d6c 6650 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6651
dd2eb166
GM
6652 /* Compute the max. number of lines specified by the user. */
6653 if (FLOATP (Vmax_mini_window_height))
18f9986a 6654 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
dd2eb166
GM
6655 else if (INTEGERP (Vmax_mini_window_height))
6656 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6657 else
6658 max_height = total_height / 4;
dd2eb166 6659
4b41cebb 6660 /* Correct that max. height if it's bogus. */
dd2eb166
GM
6661 max_height = max (1, max_height);
6662 max_height = min (total_height, max_height);
6663
6664 /* Find out the height of the text in the window. */
ad4f174e
GM
6665 if (it.truncate_lines_p)
6666 height = 1;
55b064bd 6667 else
ad4f174e
GM
6668 {
6669 last_height = 0;
6670 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6671 if (it.max_ascent == 0 && it.max_descent == 0)
6672 height = it.current_y + last_height;
6673 else
6674 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6675 height -= it.extra_line_spacing;
ad4f174e
GM
6676 height = (height + unit - 1) / unit;
6677 }
dd2eb166
GM
6678
6679 /* Compute a suitable window start. */
6680 if (height > max_height)
6681 {
6682 height = max_height;
6683 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6684 move_it_vertically_backward (&it, (height - 1) * unit);
6685 start = it.current.pos;
6686 }
6687 else
6688 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6689 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6690
6422c1d7 6691 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6692 {
6422c1d7
GM
6693 /* Let it grow only, until we display an empty message, in which
6694 case the window shrinks again. */
6695 if (height > XFASTINT (w->height))
6696 {
6697 int old_height = XFASTINT (w->height);
6698 freeze_window_starts (f, 1);
6699 grow_mini_window (w, height - XFASTINT (w->height));
6700 window_height_changed_p = XFASTINT (w->height) != old_height;
6701 }
6702 else if (height < XFASTINT (w->height)
6703 && (exact_p || BEGV == ZV))
6704 {
6705 int old_height = XFASTINT (w->height);
6706 freeze_window_starts (f, 0);
6707 shrink_mini_window (w);
6708 window_height_changed_p = XFASTINT (w->height) != old_height;
6709 }
da448723 6710 }
6422c1d7 6711 else
da448723 6712 {
6422c1d7
GM
6713 /* Always resize to exact size needed. */
6714 if (height > XFASTINT (w->height))
6715 {
6716 int old_height = XFASTINT (w->height);
6717 freeze_window_starts (f, 1);
6718 grow_mini_window (w, height - XFASTINT (w->height));
6719 window_height_changed_p = XFASTINT (w->height) != old_height;
6720 }
6721 else if (height < XFASTINT (w->height))
6722 {
6723 int old_height = XFASTINT (w->height);
6724 freeze_window_starts (f, 0);
6725 shrink_mini_window (w);
6726
6727 if (height)
6728 {
6729 freeze_window_starts (f, 1);
6730 grow_mini_window (w, height - XFASTINT (w->height));
6731 }
6732
6733 window_height_changed_p = XFASTINT (w->height) != old_height;
6734 }
9142dd5b 6735 }
1bfdbe43
GM
6736
6737 if (old_current_buffer)
6738 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6739 }
6740
6741 return window_height_changed_p;
6742}
6743
6744
6745/* Value is the current message, a string, or nil if there is no
6746 current message. */
6747
6748Lisp_Object
6749current_message ()
6750{
6751 Lisp_Object msg;
6752
6753 if (NILP (echo_area_buffer[0]))
6754 msg = Qnil;
6755 else
6756 {
23a96c77 6757 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6758 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6759 if (NILP (msg))
6760 echo_area_buffer[0] = Qnil;
6761 }
6762
6763 return msg;
6764}
6765
6766
6767static int
23a96c77 6768current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6769 EMACS_INT a1;
6770 Lisp_Object a2;
6771 EMACS_INT a3, a4;
c6e89d6c 6772{
23a96c77
GM
6773 Lisp_Object *msg = (Lisp_Object *) a1;
6774
c6e89d6c
GM
6775 if (Z > BEG)
6776 *msg = make_buffer_string (BEG, Z, 1);
6777 else
6778 *msg = Qnil;
6779 return 0;
6780}
6781
6782
6783/* Push the current message on Vmessage_stack for later restauration
6784 by restore_message. Value is non-zero if the current message isn't
6785 empty. This is a relatively infrequent operation, so it's not
6786 worth optimizing. */
6787
6788int
6789push_message ()
6790{
6791 Lisp_Object msg;
6792 msg = current_message ();
6793 Vmessage_stack = Fcons (msg, Vmessage_stack);
6794 return STRINGP (msg);
6795}
6796
6797
098ec84a
GM
6798/* Handler for record_unwind_protect calling pop_message. */
6799
6800Lisp_Object
6801push_message_unwind (dummy)
6802 Lisp_Object dummy;
6803{
6804 pop_message ();
6805 return Qnil;
6806}
6807
6808
c6e89d6c
GM
6809/* Restore message display from the top of Vmessage_stack. */
6810
6811void
6812restore_message ()
6813{
6814 Lisp_Object msg;
6815
6816 xassert (CONSP (Vmessage_stack));
6817 msg = XCAR (Vmessage_stack);
6818 if (STRINGP (msg))
2051c264 6819 message3_nolog (msg, SBYTES (msg), SMBP (msg));
c6e89d6c
GM
6820 else
6821 message3_nolog (msg, 0, 0);
6822}
6823
6824
6825/* Pop the top-most entry off Vmessage_stack. */
6826
6827void
6828pop_message ()
6829{
6830 xassert (CONSP (Vmessage_stack));
6831 Vmessage_stack = XCDR (Vmessage_stack);
6832}
6833
6834
6835/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6836 exits. If the stack is not empty, we have a missing pop_message
6837 somewhere. */
6838
6839void
6840check_message_stack ()
6841{
6842 if (!NILP (Vmessage_stack))
6843 abort ();
6844}
6845
6846
6847/* Truncate to NCHARS what will be displayed in the echo area the next
6848 time we display it---but don't redisplay it now. */
6849
6850void
6851truncate_echo_area (nchars)
6852 int nchars;
6853{
6854 if (nchars == 0)
6855 echo_area_buffer[0] = Qnil;
6856 /* A null message buffer means that the frame hasn't really been
6857 initialized yet. Error messages get reported properly by
6858 cmd_error, so this must be just an informative message; toss it. */
6859 else if (!noninteractive
6860 && INTERACTIVE
c6e89d6c 6861 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6862 {
6863 struct frame *sf = SELECTED_FRAME ();
6864 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6865 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6866 }
c6e89d6c
GM
6867}
6868
6869
6870/* Helper function for truncate_echo_area. Truncate the current
6871 message to at most NCHARS characters. */
6872
6873static int
23a96c77 6874truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6875 EMACS_INT nchars;
6876 Lisp_Object a2;
6877 EMACS_INT a3, a4;
c6e89d6c
GM
6878{
6879 if (BEG + nchars < Z)
6880 del_range (BEG + nchars, Z);
6881 if (Z == BEG)
6882 echo_area_buffer[0] = Qnil;
6883 return 0;
6884}
6885
6886
6887/* Set the current message to a substring of S or STRING.
6888
6889 If STRING is a Lisp string, set the message to the first NBYTES
6890 bytes from STRING. NBYTES zero means use the whole string. If
6891 STRING is multibyte, the message will be displayed multibyte.
6892
6893 If S is not null, set the message to the first LEN bytes of S. LEN
6894 zero means use the whole string. MULTIBYTE_P non-zero means S is
6895 multibyte. Display the message multibyte in that case. */
6896
6897void
6898set_message (s, string, nbytes, multibyte_p)
6899 char *s;
6900 Lisp_Object string;
6901 int nbytes;
6902{
6903 message_enable_multibyte
6904 = ((s && multibyte_p)
6905 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6906
23a96c77
GM
6907 with_echo_area_buffer (0, -1, set_message_1,
6908 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6909 message_buf_print = 0;
21fdfb65 6910 help_echo_showing_p = 0;
c6e89d6c
GM
6911}
6912
6913
6914/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6915 as there, with A1 corresponding to S and A2 corresponding to STRING
6916 This function is called with the echo area buffer being
c6e89d6c
GM
6917 current. */
6918
6919static int
23a96c77 6920set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6921 EMACS_INT a1;
6922 Lisp_Object a2;
6923 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6924{
23a96c77 6925 char *s = (char *) a1;
23dd2d97 6926 Lisp_Object string = a2;
23a96c77 6927
c6e89d6c
GM
6928 xassert (BEG == Z);
6929
6930 /* Change multibyteness of the echo buffer appropriately. */
6931 if (message_enable_multibyte
6932 != !NILP (current_buffer->enable_multibyte_characters))
6933 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6934
ad4f174e
GM
6935 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6936
c6e89d6c
GM
6937 /* Insert new message at BEG. */
6938 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6939
6940 if (STRINGP (string))
6941 {
6942 int nchars;
6943
6944 if (nbytes == 0)
2051c264 6945 nbytes = SBYTES (string);
c6e89d6c
GM
6946 nchars = string_byte_to_char (string, nbytes);
6947
6948 /* This function takes care of single/multibyte conversion. We
6949 just have to ensure that the echo area buffer has the right
6950 setting of enable_multibyte_characters. */
6951 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6952 }
6953 else if (s)
6954 {
6955 if (nbytes == 0)
6956 nbytes = strlen (s);
6957
6958 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6959 {
6960 /* Convert from multi-byte to single-byte. */
6961 int i, c, n;
6962 unsigned char work[1];
6963
6964 /* Convert a multibyte string to single-byte. */
6965 for (i = 0; i < nbytes; i += n)
6966 {
6967 c = string_char_and_length (s + i, nbytes - i, &n);
6968 work[0] = (SINGLE_BYTE_CHAR_P (c)
6969 ? c
6970 : multibyte_char_to_unibyte (c, Qnil));
6971 insert_1_both (work, 1, 1, 1, 0, 0);
6972 }
6973 }
6974 else if (!multibyte_p
6975 && !NILP (current_buffer->enable_multibyte_characters))
6976 {
6977 /* Convert from single-byte to multi-byte. */
6978 int i, c, n;
6979 unsigned char *msg = (unsigned char *) s;
260a86a0 6980 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6981
6982 /* Convert a single-byte string to multibyte. */
6983 for (i = 0; i < nbytes; i++)
6984 {
6985 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6986 n = CHAR_STRING (c, str);
6987 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6988 }
6989 }
6990 else
6991 insert_1 (s, nbytes, 1, 0, 0);
6992 }
6993
6994 return 0;
6995}
6996
6997
6998/* Clear messages. CURRENT_P non-zero means clear the current
6999 message. LAST_DISPLAYED_P non-zero means clear the message
7000 last displayed. */
7001
7002void
7003clear_message (current_p, last_displayed_p)
7004 int current_p, last_displayed_p;
7005{
7006 if (current_p)
6e019995
GM
7007 {
7008 echo_area_buffer[0] = Qnil;
7009 message_cleared_p = 1;
7010 }
c6e89d6c
GM
7011
7012 if (last_displayed_p)
7013 echo_area_buffer[1] = Qnil;
7014
7015 message_buf_print = 0;
7016}
7017
7018/* Clear garbaged frames.
7019
7020 This function is used where the old redisplay called
7021 redraw_garbaged_frames which in turn called redraw_frame which in
7022 turn called clear_frame. The call to clear_frame was a source of
7023 flickering. I believe a clear_frame is not necessary. It should
7024 suffice in the new redisplay to invalidate all current matrices,
7025 and ensure a complete redisplay of all windows. */
7026
7027static void
7028clear_garbaged_frames ()
7029{
5f5c8ee5
GM
7030 if (frame_garbaged)
7031 {
5f5c8ee5 7032 Lisp_Object tail, frame;
5fb96e96 7033 int changed_count = 0;
5f5c8ee5
GM
7034
7035 FOR_EACH_FRAME (tail, frame)
7036 {
7037 struct frame *f = XFRAME (frame);
7038
7039 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7040 {
6bb95882 7041 if (f->resized_p)
573e57f1 7042 Fredraw_frame (frame);
5f5c8ee5 7043 clear_current_matrices (f);
5fb96e96 7044 changed_count++;
5f5c8ee5 7045 f->garbaged = 0;
6bb95882 7046 f->resized_p = 0;
5f5c8ee5
GM
7047 }
7048 }
7049
7050 frame_garbaged = 0;
5fb96e96
RS
7051 if (changed_count)
7052 ++windows_or_buffers_changed;
5f5c8ee5 7053 }
c6e89d6c 7054}
5f5c8ee5 7055
5f5c8ee5 7056
886bd6f2
GM
7057/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7058 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7059 mini-windows height has been changed. */
5f5c8ee5 7060
c6e89d6c
GM
7061static int
7062echo_area_display (update_frame_p)
7063 int update_frame_p;
7064{
7065 Lisp_Object mini_window;
7066 struct window *w;
7067 struct frame *f;
7068 int window_height_changed_p = 0;
886bd6f2 7069 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7070
886bd6f2 7071 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7072 w = XWINDOW (mini_window);
7073 f = XFRAME (WINDOW_FRAME (w));
7074
7075 /* Don't display if frame is invisible or not yet initialized. */
7076 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7077 return 0;
5f5c8ee5 7078
1a578e9b 7079/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7080#ifndef MAC_OS8
1ab3e082 7081#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7082 /* When Emacs starts, selected_frame may be a visible terminal
7083 frame, even if we run under a window system. If we let this
7084 through, a message would be displayed on the terminal. */
622e3754
GM
7085 if (EQ (selected_frame, Vterminal_frame)
7086 && !NILP (Vwindow_system))
c6e89d6c 7087 return 0;
1ab3e082 7088#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7089#endif
c6e89d6c
GM
7090
7091 /* Redraw garbaged frames. */
7092 if (frame_garbaged)
7093 clear_garbaged_frames ();
7094
7095 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7096 {
7097 echo_area_window = mini_window;
7098 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7099 w->must_be_updated_p = 1;
c59c668a 7100
d4358b37
GM
7101 /* Update the display, unless called from redisplay_internal.
7102 Also don't update the screen during redisplay itself. The
7103 update will happen at the end of redisplay, and an update
7104 here could cause confusion. */
7105 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7106 {
715e84c9 7107 int n = 0;
edc68111 7108
715e84c9
GM
7109 /* If the display update has been interrupted by pending
7110 input, update mode lines in the frame. Due to the
7111 pending input, it might have been that redisplay hasn't
7112 been called, so that mode lines above the echo area are
7113 garbaged. This looks odd, so we prevent it here. */
7114 if (!display_completed)
7115 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7116
8af1308e
GM
7117 if (window_height_changed_p
7118 /* Don't do this if Emacs is shutting down. Redisplay
7119 needs to run hooks. */
7120 && !NILP (Vrun_hooks))
c59c668a 7121 {
c06017fb
GM
7122 /* Must update other windows. Likewise as in other
7123 cases, don't let this update be interrupted by
7124 pending input. */
7125 int count = BINDING_STACK_SIZE ();
7126 specbind (Qredisplay_dont_pause, Qt);
edc68111 7127 windows_or_buffers_changed = 1;
c59c668a 7128 redisplay_internal (0);
c06017fb 7129 unbind_to (count, Qnil);
c59c668a 7130 }
715e84c9 7131 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7132 {
edc68111 7133 /* Window configuration is the same as before.
715e84c9
GM
7134 Can do with a display update of the echo area,
7135 unless we displayed some mode lines. */
5f5c8ee5
GM
7136 update_single_window (w, 1);
7137 rif->flush_display (f);
7138 }
7139 else
7140 update_frame (f, 1, 1);
31b6671b
GM
7141
7142 /* If cursor is in the echo area, make sure that the next
7143 redisplay displays the minibuffer, so that the cursor will
7144 be replaced with what the minibuffer wants. */
7145 if (cursor_in_echo_area)
7146 ++windows_or_buffers_changed;
5f5c8ee5
GM
7147 }
7148 }
7149 else if (!EQ (mini_window, selected_window))
7150 windows_or_buffers_changed++;
c59c668a
GM
7151
7152 /* Last displayed message is now the current message. */
dd2eb166
GM
7153 echo_area_buffer[1] = echo_area_buffer[0];
7154
5f5c8ee5
GM
7155 /* Prevent redisplay optimization in redisplay_internal by resetting
7156 this_line_start_pos. This is done because the mini-buffer now
7157 displays the message instead of its buffer text. */
7158 if (EQ (mini_window, selected_window))
7159 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7160
7161 return window_height_changed_p;
5f5c8ee5
GM
7162}
7163
7164
7165\f
7166/***********************************************************************
7167 Frame Titles
7168 ***********************************************************************/
7169
7170
7171#ifdef HAVE_WINDOW_SYSTEM
7172
7173/* A buffer for constructing frame titles in it; allocated from the
7174 heap in init_xdisp and resized as needed in store_frame_title_char. */
7175
7176static char *frame_title_buf;
7177
7178/* The buffer's end, and a current output position in it. */
7179
7180static char *frame_title_buf_end;
7181static char *frame_title_ptr;
7182
7183
7184/* Store a single character C for the frame title in frame_title_buf.
7185 Re-allocate frame_title_buf if necessary. */
7186
7187static void
7188store_frame_title_char (c)
7189 char c;
7190{
7191 /* If output position has reached the end of the allocated buffer,
7192 double the buffer's size. */
7193 if (frame_title_ptr == frame_title_buf_end)
7194 {
7195 int len = frame_title_ptr - frame_title_buf;
7196 int new_size = 2 * len * sizeof *frame_title_buf;
7197 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7198 frame_title_buf_end = frame_title_buf + new_size;
7199 frame_title_ptr = frame_title_buf + len;
7200 }
7201
7202 *frame_title_ptr++ = c;
7203}
7204
7205
7206/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7207 frame_title_ptr. STR is the string to store. Do not copy
7208 characters that yield more columns than PRECISION; PRECISION <= 0
7209 means copy the whole string. Pad with spaces until FIELD_WIDTH
7210 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7211 pad. Called from display_mode_element when it is used to build a
7212 frame title. */
5f5c8ee5
GM
7213
7214static int
7215store_frame_title (str, field_width, precision)
7216 unsigned char *str;
7217 int field_width, precision;
7218{
7219 int n = 0;
3b552d56 7220 int dummy, nbytes;
5f5c8ee5
GM
7221
7222 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7223 nbytes = strlen (str);
7224 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7225 while (nbytes--)
7226 store_frame_title_char (*str++);
5f5c8ee5
GM
7227
7228 /* Fill up with spaces until FIELD_WIDTH reached. */
7229 while (field_width > 0
7230 && n < field_width)
7231 {
7232 store_frame_title_char (' ');
7233 ++n;
7234 }
7235
7236 return n;
7237}
7238
7239
7240/* Set the title of FRAME, if it has changed. The title format is
7241 Vicon_title_format if FRAME is iconified, otherwise it is
7242 frame_title_format. */
7243
7244static void
7245x_consider_frame_title (frame)
7246 Lisp_Object frame;
7247{
7248 struct frame *f = XFRAME (frame);
7249
7250 if (FRAME_WINDOW_P (f)
7251 || FRAME_MINIBUF_ONLY_P (f)
7252 || f->explicit_name)
7253 {
7254 /* Do we have more than one visible frame on this X display? */
7255 Lisp_Object tail;
7256 Lisp_Object fmt;
7257 struct buffer *obuf;
7258 int len;
7259 struct it it;
7260
9472f927 7261 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7262 {
74779f52
JR
7263 Lisp_Object other_frame = XCAR (tail);
7264 struct frame *tf = XFRAME (other_frame);
5f5c8ee5
GM
7265
7266 if (tf != f
7267 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7268 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7269 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7270 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7271 break;
7272 }
7273
7274 /* Set global variable indicating that multiple frames exist. */
7275 multiple_frames = CONSP (tail);
7276
7277 /* Switch to the buffer of selected window of the frame. Set up
7278 frame_title_ptr so that display_mode_element will output into it;
7279 then display the title. */
7280 obuf = current_buffer;
bb336f8d 7281 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7282 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7283 frame_title_ptr = frame_title_buf;
7284 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7285 NULL, DEFAULT_FACE_ID);
c53a1624 7286 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 7287 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7288 frame_title_ptr = NULL;
bb336f8d 7289 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7290
7291 /* Set the title only if it's changed. This avoids consing in
7292 the common case where it hasn't. (If it turns out that we've
7293 already wasted too much time by walking through the list with
7294 display_mode_element, then we might need to optimize at a
7295 higher level than this.) */
7296 if (! STRINGP (f->name)
2051c264
GM
7297 || SBYTES (f->name) != len
7298 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
7299 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7300 }
7301}
7302
7303#else /* not HAVE_WINDOW_SYSTEM */
7304
7305#define frame_title_ptr ((char *)0)
7306#define store_frame_title(str, mincol, maxcol) 0
7307
7308#endif /* not HAVE_WINDOW_SYSTEM */
7309
7310
7311
7312\f
7313/***********************************************************************
7314 Menu Bars
7315 ***********************************************************************/
7316
7317
7318/* Prepare for redisplay by updating menu-bar item lists when
7319 appropriate. This can call eval. */
7320
7321void
7322prepare_menu_bars ()
7323{
7324 int all_windows;
7325 struct gcpro gcpro1, gcpro2;
7326 struct frame *f;
6b5c4794 7327 Lisp_Object tooltip_frame;
5f5c8ee5 7328
86ffe5cd 7329#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7330 tooltip_frame = tip_frame;
7331#else
6b5c4794 7332 tooltip_frame = Qnil;
5f5c8ee5
GM
7333#endif
7334
7335 /* Update all frame titles based on their buffer names, etc. We do
7336 this before the menu bars so that the buffer-menu will show the
7337 up-to-date frame titles. */
7338#ifdef HAVE_WINDOW_SYSTEM
7339 if (windows_or_buffers_changed || update_mode_lines)
7340 {
7341 Lisp_Object tail, frame;
7342
7343 FOR_EACH_FRAME (tail, frame)
7344 {
7345 f = XFRAME (frame);
6b5c4794 7346 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7347 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7348 x_consider_frame_title (frame);
7349 }
7350 }
7351#endif /* HAVE_WINDOW_SYSTEM */
7352
7353 /* Update the menu bar item lists, if appropriate. This has to be
7354 done before any actual redisplay or generation of display lines. */
7355 all_windows = (update_mode_lines
7356 || buffer_shared > 1
7357 || windows_or_buffers_changed);
7358 if (all_windows)
7359 {
7360 Lisp_Object tail, frame;
2d27dae2 7361 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
7362
7363 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7364
7365 FOR_EACH_FRAME (tail, frame)
7366 {
7367 f = XFRAME (frame);
7368
7369 /* Ignore tooltip frame. */
6b5c4794 7370 if (EQ (frame, tooltip_frame))
5f5c8ee5
GM
7371 continue;
7372
7373 /* If a window on this frame changed size, report that to
7374 the user and clear the size-change flag. */
7375 if (FRAME_WINDOW_SIZES_CHANGED (f))
7376 {
7377 Lisp_Object functions;
7378
7379 /* Clear flag first in case we get an error below. */
7380 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7381 functions = Vwindow_size_change_functions;
7382 GCPRO2 (tail, functions);
7383
7384 while (CONSP (functions))
7385 {
7386 call1 (XCAR (functions), frame);
7387 functions = XCDR (functions);
7388 }
7389 UNGCPRO;
7390 }
7391
7392 GCPRO1 (tail);
7393 update_menu_bar (f, 0);
7394#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7395 update_tool_bar (f, 0);
5f5c8ee5
GM
7396#endif
7397 UNGCPRO;
7398 }
7399
7400 unbind_to (count, Qnil);
7401 }
7402 else
7403 {
886bd6f2
GM
7404 struct frame *sf = SELECTED_FRAME ();
7405 update_menu_bar (sf, 1);
5f5c8ee5 7406#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7407 update_tool_bar (sf, 1);
5f5c8ee5
GM
7408#endif
7409 }
7410
7411 /* Motif needs this. See comment in xmenu.c. Turn it off when
7412 pending_menu_activation is not defined. */
7413#ifdef USE_X_TOOLKIT
7414 pending_menu_activation = 0;
7415#endif
7416}
7417
7418
7419/* Update the menu bar item list for frame F. This has to be done
7420 before we start to fill in any display lines, because it can call
7421 eval.
7422
7423 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7424
7425static void
7426update_menu_bar (f, save_match_data)
7427 struct frame *f;
7428 int save_match_data;
7429{
7430 Lisp_Object window;
7431 register struct window *w;
7432
e1477f43
GM
7433 /* If called recursively during a menu update, do nothing. This can
7434 happen when, for instance, an activate-menubar-hook causes a
7435 redisplay. */
7436 if (inhibit_menubar_update)
7437 return;
7438
5f5c8ee5
GM
7439 window = FRAME_SELECTED_WINDOW (f);
7440 w = XWINDOW (window);
7441
40ae145b 7442#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
7443 condition !NILP (w->update_mode_line), rather than using
7444 update_mode_lines directly, and this if statement may have
7445 been added to make that condition work. Now the if
7446 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
7447 if (update_mode_lines)
7448 w->update_mode_line = Qt;
a5f08374 7449#endif
5f5c8ee5
GM
7450
7451 if (FRAME_WINDOW_P (f)
7452 ?
e0f712ba 7453#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
5f5c8ee5
GM
7454 FRAME_EXTERNAL_MENU_BAR (f)
7455#else
7456 FRAME_MENU_BAR_LINES (f) > 0
7457#endif
7458 : FRAME_MENU_BAR_LINES (f) > 0)
7459 {
7460 /* If the user has switched buffers or windows, we need to
7461 recompute to reflect the new bindings. But we'll
7462 recompute when update_mode_lines is set too; that means
7463 that people can use force-mode-line-update to request
7464 that the menu bar be recomputed. The adverse effect on
7465 the rest of the redisplay algorithm is about the same as
7466 windows_or_buffers_changed anyway. */
7467 if (windows_or_buffers_changed
a5f08374
RS
7468 /* This used to test w->update_mode_line, but we believe
7469 there is no need to recompute the menu in that case. */
7470 || update_mode_lines
5f5c8ee5
GM
7471 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7472 < BUF_MODIFF (XBUFFER (w->buffer)))
7473 != !NILP (w->last_had_star))
7474 || ((!NILP (Vtransient_mark_mode)
7475 && !NILP (XBUFFER (w->buffer)->mark_active))
7476 != !NILP (w->region_showing)))
7477 {
7478 struct buffer *prev = current_buffer;
2d27dae2 7479 int count = BINDING_STACK_SIZE ();
5f5c8ee5 7480
e1477f43
GM
7481 specbind (Qinhibit_menubar_update, Qt);
7482
5f5c8ee5
GM
7483 set_buffer_internal_1 (XBUFFER (w->buffer));
7484 if (save_match_data)
7485 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7486 if (NILP (Voverriding_local_map_menu_flag))
7487 {
7488 specbind (Qoverriding_terminal_local_map, Qnil);
7489 specbind (Qoverriding_local_map, Qnil);
7490 }
7491
7492 /* Run the Lucid hook. */
65048e97 7493 safe_run_hooks (Qactivate_menubar_hook);
5f5c8ee5
GM
7494
7495 /* If it has changed current-menubar from previous value,
7496 really recompute the menu-bar from the value. */
7497 if (! NILP (Vlucid_menu_bar_dirty_flag))
7498 call0 (Qrecompute_lucid_menubar);
7499
7500 safe_run_hooks (Qmenu_bar_update_hook);
7501 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7502
7503 /* Redisplay the menu bar in case we changed it. */
e0f712ba 7504#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
1a578e9b 7505 if (FRAME_WINDOW_P (f)
e0f712ba 7506#if defined (MAC_OS)
1a578e9b
AC
7507 /* All frames on Mac OS share the same menubar. So only the
7508 selected frame should be allowed to set it. */
7509 && f == SELECTED_FRAME ()
7510#endif
7511 )
5f5c8ee5
GM
7512 set_frame_menubar (f, 0, 0);
7513 else
7514 /* On a terminal screen, the menu bar is an ordinary screen
7515 line, and this makes it get updated. */
7516 w->update_mode_line = Qt;
7517#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7518 /* In the non-toolkit version, the menu bar is an ordinary screen
7519 line, and this makes it get updated. */
7520 w->update_mode_line = Qt;
7521#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7522
7523 unbind_to (count, Qnil);
7524 set_buffer_internal_1 (prev);
7525 }
7526 }
7527}
7528
7529
7530\f
7531/***********************************************************************
e037b9ec 7532 Tool-bars
5f5c8ee5
GM
7533 ***********************************************************************/
7534
7535#ifdef HAVE_WINDOW_SYSTEM
7536
e037b9ec 7537/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7538 before we start to fill in any display lines. Called from
7539 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7540 and restore it here. */
7541
7542static void
e037b9ec 7543update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7544 struct frame *f;
7545 int save_match_data;
7546{
e037b9ec
GM
7547 if (WINDOWP (f->tool_bar_window)
7548 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
7549 {
7550 Lisp_Object window;
7551 struct window *w;
7552
7553 window = FRAME_SELECTED_WINDOW (f);
7554 w = XWINDOW (window);
7555
7556 /* If the user has switched buffers or windows, we need to
7557 recompute to reflect the new bindings. But we'll
7558 recompute when update_mode_lines is set too; that means
7559 that people can use force-mode-line-update to request
7560 that the menu bar be recomputed. The adverse effect on
7561 the rest of the redisplay algorithm is about the same as
7562 windows_or_buffers_changed anyway. */
7563 if (windows_or_buffers_changed
7564 || !NILP (w->update_mode_line)
7565 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7566 < BUF_MODIFF (XBUFFER (w->buffer)))
7567 != !NILP (w->last_had_star))
7568 || ((!NILP (Vtransient_mark_mode)
7569 && !NILP (XBUFFER (w->buffer)->mark_active))
7570 != !NILP (w->region_showing)))
7571 {
7572 struct buffer *prev = current_buffer;
2d27dae2 7573 int count = BINDING_STACK_SIZE ();
a2889657 7574
5f5c8ee5
GM
7575 /* Set current_buffer to the buffer of the selected
7576 window of the frame, so that we get the right local
7577 keymaps. */
7578 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7579
5f5c8ee5
GM
7580 /* Save match data, if we must. */
7581 if (save_match_data)
7582 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7583
7584 /* Make sure that we don't accidentally use bogus keymaps. */
7585 if (NILP (Voverriding_local_map_menu_flag))
7586 {
7587 specbind (Qoverriding_terminal_local_map, Qnil);
7588 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7589 }
1f40cad2 7590
e037b9ec 7591 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7592 f->tool_bar_items
7593 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
5f5c8ee5 7594
e037b9ec 7595 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7596 w->update_mode_line = Qt;
7597
7598 unbind_to (count, Qnil);
7599 set_buffer_internal_1 (prev);
81d478f3 7600 }
a2889657
JB
7601 }
7602}
7603
6c4429a5 7604
e037b9ec 7605/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7606 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7607 been set up previously by calling prepare_menu_bars. */
7608
a2889657 7609static void
e037b9ec 7610build_desired_tool_bar_string (f)
5f5c8ee5 7611 struct frame *f;
a2889657 7612{
a23887b9 7613 int i, size, size_needed;
5f5c8ee5
GM
7614 struct gcpro gcpro1, gcpro2, gcpro3;
7615 Lisp_Object image, plist, props;
a2889657 7616
5f5c8ee5
GM
7617 image = plist = props = Qnil;
7618 GCPRO3 (image, plist, props);
a2889657 7619
e037b9ec 7620 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
7621 Otherwise, make a new string. */
7622
7623 /* The size of the string we might be able to reuse. */
e037b9ec 7624 size = (STRINGP (f->desired_tool_bar_string)
2051c264 7625 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
7626 : 0);
7627
b94c0d9c 7628 /* We need one space in the string for each image. */
a23887b9 7629 size_needed = f->n_tool_bar_items;
b94c0d9c
GM
7630
7631 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7632 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7633 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7634 make_number (' '));
5f5c8ee5
GM
7635 else
7636 {
7637 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7638 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7639 props, f->desired_tool_bar_string);
5f5c8ee5 7640 }
a2889657 7641
5f5c8ee5 7642 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7643 put a `menu_item' property on tool-bar items with a value that
7644 is the index of the item in F's tool-bar item vector. */
a23887b9 7645 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7646 {
7464726e 7647#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7648
e037b9ec
GM
7649 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7650 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7651 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7652 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7653
7654 /* If image is a vector, choose the image according to the
7655 button state. */
e037b9ec 7656 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7657 if (VECTORP (image))
7658 {
5f5c8ee5
GM
7659 if (enabled_p)
7660 idx = (selected_p
e037b9ec
GM
7661 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7662 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7663 else
7664 idx = (selected_p
e037b9ec
GM
7665 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7666 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5 7667
37e4e482
GM
7668 xassert (ASIZE (image) >= idx);
7669 image = AREF (image, idx);
5f5c8ee5 7670 }
37e4e482
GM
7671 else
7672 idx = -1;
5f5c8ee5
GM
7673
7674 /* Ignore invalid image specifications. */
7675 if (!valid_image_p (image))
7676 continue;
7677
e037b9ec 7678 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7679 plist = Fcopy_sequence (XCDR (image));
7680
7681 /* Compute margin and relief to draw. */
9f864bf9 7682 relief = (tool_bar_button_relief >= 0
c3d76173
GM
7683 ? tool_bar_button_relief
7684 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7685 hmargin = vmargin = relief;
7686
7687 if (INTEGERP (Vtool_bar_button_margin)
7688 && XINT (Vtool_bar_button_margin) > 0)
7689 {
7690 hmargin += XFASTINT (Vtool_bar_button_margin);
7691 vmargin += XFASTINT (Vtool_bar_button_margin);
7692 }
7693 else if (CONSP (Vtool_bar_button_margin))
7694 {
7695 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7696 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7697 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7698
7699 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7700 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7701 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7702 }
5f5c8ee5 7703
e037b9ec 7704 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7705 {
7706 /* Add a `:relief' property to the image spec if the item is
7707 selected. */
7708 if (selected_p)
7709 {
7710 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7711 hmargin -= relief;
7712 vmargin -= relief;
5f5c8ee5
GM
7713 }
7714 }
7715 else
7716 {
7717 /* If image is selected, display it pressed, i.e. with a
7718 negative relief. If it's not selected, display it with a
7719 raised relief. */
7720 plist = Fplist_put (plist, QCrelief,
7721 (selected_p
7722 ? make_number (-relief)
7723 : make_number (relief)));
35a41507
GM
7724 hmargin -= relief;
7725 vmargin -= relief;
5f5c8ee5
GM
7726 }
7727
7728 /* Put a margin around the image. */
35a41507
GM
7729 if (hmargin || vmargin)
7730 {
7731 if (hmargin == vmargin)
7732 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7733 else
7734 plist = Fplist_put (plist, QCmargin,
7735 Fcons (make_number (hmargin),
7736 make_number (vmargin)));
7737 }
5f5c8ee5 7738
37e4e482
GM
7739 /* If button is not enabled, and we don't have special images
7740 for the disabled state, make the image appear disabled by
5f5c8ee5 7741 applying an appropriate algorithm to it. */
37e4e482 7742 if (!enabled_p && idx < 0)
ecd01a0e 7743 plist = Fplist_put (plist, QCconversion, Qdisabled);
5f5c8ee5
GM
7744
7745 /* Put a `display' text property on the string for the image to
7746 display. Put a `menu-item' property on the string that gives
e037b9ec 7747 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7748 vector. */
7749 image = Fcons (Qimage, plist);
7750 props = list4 (Qdisplay, image,
a23887b9
GM
7751 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7752
7753 /* Let the last image hide all remaining spaces in the tool bar
7754 string. The string can be longer than needed when we reuse a
7755 previous string. */
7756 if (i + 1 == f->n_tool_bar_items)
2051c264 7757 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
7758 else
7759 end = i + 1;
7760 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7761 props, f->desired_tool_bar_string);
5f5c8ee5 7762#undef PROP
a2889657
JB
7763 }
7764
5f5c8ee5
GM
7765 UNGCPRO;
7766}
7767
7768
e037b9ec 7769/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7770
7771static void
e037b9ec 7772display_tool_bar_line (it)
5f5c8ee5
GM
7773 struct it *it;
7774{
7775 struct glyph_row *row = it->glyph_row;
7776 int max_x = it->last_visible_x;
7777 struct glyph *last;
7778
7779 prepare_desired_row (row);
7780 row->y = it->current_y;
90aa2856
GM
7781
7782 /* Note that this isn't made use of if the face hasn't a box,
7783 so there's no need to check the face here. */
7784 it->start_of_box_run_p = 1;
5f5c8ee5
GM
7785
7786 while (it->current_x < max_x)
a2889657 7787 {
5f5c8ee5 7788 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7789
5f5c8ee5
GM
7790 /* Get the next display element. */
7791 if (!get_next_display_element (it))
7792 break;
73af359d 7793
5f5c8ee5
GM
7794 /* Produce glyphs. */
7795 x_before = it->current_x;
7796 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7797 PRODUCE_GLYPHS (it);
daa37602 7798
5f5c8ee5
GM
7799 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7800 i = 0;
7801 x = x_before;
7802 while (i < nglyphs)
7803 {
7804 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7805
7806 if (x + glyph->pixel_width > max_x)
7807 {
7808 /* Glyph doesn't fit on line. */
7809 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7810 it->current_x = x;
7811 goto out;
7812 }
daa37602 7813
5f5c8ee5
GM
7814 ++it->hpos;
7815 x += glyph->pixel_width;
7816 ++i;
7817 }
7818
7819 /* Stop at line ends. */
7820 if (ITERATOR_AT_END_OF_LINE_P (it))
7821 break;
7822
cafafe0b 7823 set_iterator_to_next (it, 1);
a2889657 7824 }
a2889657 7825
5f5c8ee5 7826 out:;
a2889657 7827
5f5c8ee5
GM
7828 row->displays_text_p = row->used[TEXT_AREA] != 0;
7829 extend_face_to_end_of_line (it);
7830 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7831 last->right_box_line_p = 1;
90aa2856
GM
7832 if (last == row->glyphs[TEXT_AREA])
7833 last->left_box_line_p = 1;
5f5c8ee5
GM
7834 compute_line_metrics (it);
7835
e037b9ec 7836 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7837 if (!row->displays_text_p)
7838 {
312246d1
GM
7839 row->height = row->phys_height = it->last_visible_y - row->y;
7840 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
7841 }
7842
7843 row->full_width_p = 1;
7844 row->continued_p = 0;
7845 row->truncated_on_left_p = 0;
7846 row->truncated_on_right_p = 0;
7847
7848 it->current_x = it->hpos = 0;
7849 it->current_y += row->height;
7850 ++it->vpos;
7851 ++it->glyph_row;
a2889657 7852}
96a410bc 7853
5f5c8ee5 7854
e037b9ec 7855/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7856 items of frame F visible. */
96a410bc 7857
d39b6696 7858static int
e037b9ec 7859tool_bar_lines_needed (f)
5f5c8ee5 7860 struct frame *f;
d39b6696 7861{
e037b9ec 7862 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
7863 struct it it;
7864
e037b9ec
GM
7865 /* Initialize an iterator for iteration over
7866 F->desired_tool_bar_string in the tool-bar window of frame F. */
7867 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7868 it.first_visible_x = 0;
7869 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7870 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7871
7872 while (!ITERATOR_AT_END_P (&it))
7873 {
7874 it.glyph_row = w->desired_matrix->rows;
7875 clear_glyph_row (it.glyph_row);
e037b9ec 7876 display_tool_bar_line (&it);
5f5c8ee5
GM
7877 }
7878
7879 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7880}
96a410bc 7881
5f5c8ee5 7882
57c28064
GM
7883DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7884 0, 1, 0,
7ee72033
MB
7885 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7886 (frame)
57c28064
GM
7887 Lisp_Object frame;
7888{
7889 struct frame *f;
7890 struct window *w;
7891 int nlines = 0;
7892
7893 if (NILP (frame))
7894 frame = selected_frame;
7895 else
b7826503 7896 CHECK_FRAME (frame);
57c28064
GM
7897 f = XFRAME (frame);
7898
7899 if (WINDOWP (f->tool_bar_window)
7900 || (w = XWINDOW (f->tool_bar_window),
7901 XFASTINT (w->height) > 0))
7902 {
7903 update_tool_bar (f, 1);
7904 if (f->n_tool_bar_items)
7905 {
7906 build_desired_tool_bar_string (f);
7907 nlines = tool_bar_lines_needed (f);
7908 }
7909 }
7910
7911 return make_number (nlines);
7912}
7913
7914
e037b9ec 7915/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7916 height should be changed. */
7917
7918static int
e037b9ec 7919redisplay_tool_bar (f)
5f5c8ee5 7920 struct frame *f;
96a410bc 7921{
5f5c8ee5
GM
7922 struct window *w;
7923 struct it it;
7924 struct glyph_row *row;
7925 int change_height_p = 0;
7926
e037b9ec
GM
7927 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7928 do anything. This means you must start with tool-bar-lines
5f5c8ee5 7929 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
7930 can turn off tool-bars by specifying tool-bar-lines zero. */
7931 if (!WINDOWP (f->tool_bar_window)
7932 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
7933 XFASTINT (w->height) == 0))
7934 return 0;
96a410bc 7935
e037b9ec
GM
7936 /* Set up an iterator for the tool-bar window. */
7937 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7938 it.first_visible_x = 0;
7939 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7940 row = it.glyph_row;
3450d04c 7941
e037b9ec
GM
7942 /* Build a string that represents the contents of the tool-bar. */
7943 build_desired_tool_bar_string (f);
7944 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 7945
e037b9ec 7946 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 7947 while (it.current_y < it.last_visible_y)
e037b9ec 7948 display_tool_bar_line (&it);
3450d04c 7949
e037b9ec 7950 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
7951 window, so don't do it. */
7952 w->desired_matrix->no_scrolling_p = 1;
7953 w->must_be_updated_p = 1;
3450d04c 7954
e037b9ec 7955 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
7956 {
7957 int nlines;
6e33e6f0
GM
7958
7959 /* If we couldn't display everything, change the tool-bar's
7960 height. */
7961 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7962 change_height_p = 1;
5f5c8ee5
GM
7963
7964 /* If there are blank lines at the end, except for a partially
7965 visible blank line at the end that is smaller than
e037b9ec 7966 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
7967 row = it.glyph_row - 1;
7968 if (!row->displays_text_p
7969 && row->height >= CANON_Y_UNIT (f))
7970 change_height_p = 1;
7971
e037b9ec
GM
7972 /* If row displays tool-bar items, but is partially visible,
7973 change the tool-bar's height. */
5f5c8ee5
GM
7974 if (row->displays_text_p
7975 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7976 change_height_p = 1;
7977
e037b9ec 7978 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
7979 frame parameter. */
7980 if (change_height_p
e037b9ec 7981 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
7982 nlines != XFASTINT (w->height)))
7983 {
e037b9ec 7984 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 7985 Lisp_Object frame;
e85ee976 7986 int old_height = XFASTINT (w->height);
5f5c8ee5
GM
7987
7988 XSETFRAME (frame, f);
7989 clear_glyph_matrix (w->desired_matrix);
7990 Fmodify_frame_parameters (frame,
e037b9ec 7991 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
7992 make_number (nlines)),
7993 Qnil));
e85ee976
GM
7994 if (XFASTINT (w->height) != old_height)
7995 fonts_changed_p = 1;
5f5c8ee5
GM
7996 }
7997 }
3450d04c 7998
5f5c8ee5 7999 return change_height_p;
96a410bc 8000}
90adcf20 8001
5f5c8ee5 8002
e037b9ec
GM
8003/* Get information about the tool-bar item which is displayed in GLYPH
8004 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8005 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8006 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
8007
8008int
e037b9ec 8009tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8010 struct frame *f;
8011 struct glyph *glyph;
8012 int *prop_idx;
90adcf20 8013{
5f5c8ee5
GM
8014 Lisp_Object prop;
8015 int success_p;
be676094
GM
8016 int charpos;
8017
8018 /* This function can be called asynchronously, which means we must
8019 exclude any possibility that Fget_text_property signals an
8020 error. */
2051c264 8021 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8022 charpos = max (0, charpos);
5f5c8ee5
GM
8023
8024 /* Get the text property `menu-item' at pos. The value of that
8025 property is the start index of this item's properties in
7464726e 8026 F->tool_bar_items. */
be676094 8027 prop = Fget_text_property (make_number (charpos),
e037b9ec 8028 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8029 if (INTEGERP (prop))
8030 {
8031 *prop_idx = XINT (prop);
8032 success_p = 1;
8033 }
8034 else
8035 success_p = 0;
90adcf20 8036
5f5c8ee5
GM
8037 return success_p;
8038}
8039
8040#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 8041
feb0c42f 8042
5f5c8ee5
GM
8043\f
8044/************************************************************************
8045 Horizontal scrolling
8046 ************************************************************************/
feb0c42f 8047
5f5c8ee5
GM
8048static int hscroll_window_tree P_ ((Lisp_Object));
8049static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 8050
5f5c8ee5
GM
8051/* For all leaf windows in the window tree rooted at WINDOW, set their
8052 hscroll value so that PT is (i) visible in the window, and (ii) so
8053 that it is not within a certain margin at the window's left and
8054 right border. Value is non-zero if any window's hscroll has been
8055 changed. */
8056
8057static int
8058hscroll_window_tree (window)
8059 Lisp_Object window;
8060{
8061 int hscrolled_p = 0;
e76d28d5 8062 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
8063 int hscroll_step_abs = 0;
8064 double hscroll_step_rel = 0;
8065
8066 if (hscroll_relative_p)
8067 {
e76d28d5 8068 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
8069 if (hscroll_step_rel < 0)
8070 {
8071 hscroll_relative_p = 0;
8072 hscroll_step_abs = 0;
8073 }
8074 }
e76d28d5 8075 else if (INTEGERP (Vhscroll_step))
1df7e8f0 8076 {
e76d28d5 8077 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
8078 if (hscroll_step_abs < 0)
8079 hscroll_step_abs = 0;
8080 }
8081 else
8082 hscroll_step_abs = 0;
8083
5f5c8ee5 8084 while (WINDOWP (window))
90adcf20 8085 {
5f5c8ee5 8086 struct window *w = XWINDOW (window);
1df7e8f0 8087
5f5c8ee5
GM
8088 if (WINDOWP (w->hchild))
8089 hscrolled_p |= hscroll_window_tree (w->hchild);
8090 else if (WINDOWP (w->vchild))
8091 hscrolled_p |= hscroll_window_tree (w->vchild);
8092 else if (w->cursor.vpos >= 0)
8093 {
e76d28d5 8094 int h_margin, text_area_x, text_area_y;
5f5c8ee5 8095 int text_area_width, text_area_height;
92a90e89
GM
8096 struct glyph_row *current_cursor_row
8097 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8098 struct glyph_row *desired_cursor_row
8099 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8100 struct glyph_row *cursor_row
8101 = (desired_cursor_row->enabled_p
8102 ? desired_cursor_row
8103 : current_cursor_row);
a2725ab2 8104
5f5c8ee5
GM
8105 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8106 &text_area_width, &text_area_height);
90adcf20 8107
5f5c8ee5 8108 /* Scroll when cursor is inside this scroll margin. */
e76d28d5 8109 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
1df7e8f0 8110
5f5c8ee5 8111 if ((XFASTINT (w->hscroll)
e76d28d5 8112 && w->cursor.x <= h_margin)
92a90e89
GM
8113 || (cursor_row->enabled_p
8114 && cursor_row->truncated_on_right_p
e76d28d5 8115 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 8116 {
5f5c8ee5
GM
8117 struct it it;
8118 int hscroll;
8119 struct buffer *saved_current_buffer;
8120 int pt;
1df7e8f0 8121 int wanted_x;
5f5c8ee5
GM
8122
8123 /* Find point in a display of infinite width. */
8124 saved_current_buffer = current_buffer;
8125 current_buffer = XBUFFER (w->buffer);
1df7e8f0 8126
5f5c8ee5
GM
8127 if (w == XWINDOW (selected_window))
8128 pt = BUF_PT (current_buffer);
8129 else
08b610e4 8130 {
5f5c8ee5
GM
8131 pt = marker_position (w->pointm);
8132 pt = max (BEGV, pt);
8133 pt = min (ZV, pt);
8134 }
8135
8136 /* Move iterator to pt starting at cursor_row->start in
8137 a line with infinite width. */
8138 init_to_row_start (&it, w, cursor_row);
8139 it.last_visible_x = INFINITY;
8140 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8141 current_buffer = saved_current_buffer;
8142
1df7e8f0
EZ
8143 /* Position cursor in window. */
8144 if (!hscroll_relative_p && hscroll_step_abs == 0)
8145 hscroll = max (0, it.current_x - text_area_width / 2)
8146 / CANON_X_UNIT (it.f);
e76d28d5 8147 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
8148 {
8149 if (hscroll_relative_p)
8150 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 8151 - h_margin;
1df7e8f0
EZ
8152 else
8153 wanted_x = text_area_width
8154 - hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8155 - h_margin;
1df7e8f0
EZ
8156 hscroll
8157 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8158 }
8159 else
8160 {
8161 if (hscroll_relative_p)
8162 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 8163 + h_margin;
1df7e8f0
EZ
8164 else
8165 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
e76d28d5 8166 + h_margin;
1df7e8f0
EZ
8167 hscroll
8168 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8169 }
3172f70b 8170 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
8171
8172 /* Don't call Fset_window_hscroll if value hasn't
8173 changed because it will prevent redisplay
8174 optimizations. */
8175 if (XFASTINT (w->hscroll) != hscroll)
8176 {
3172f70b
GM
8177 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8178 w->hscroll = make_number (hscroll);
5f5c8ee5 8179 hscrolled_p = 1;
08b610e4 8180 }
08b610e4 8181 }
08b610e4 8182 }
a2725ab2 8183
5f5c8ee5 8184 window = w->next;
90adcf20 8185 }
cd6dfed6 8186
5f5c8ee5
GM
8187 /* Value is non-zero if hscroll of any leaf window has been changed. */
8188 return hscrolled_p;
8189}
8190
8191
8192/* Set hscroll so that cursor is visible and not inside horizontal
8193 scroll margins for all windows in the tree rooted at WINDOW. See
8194 also hscroll_window_tree above. Value is non-zero if any window's
8195 hscroll has been changed. If it has, desired matrices on the frame
8196 of WINDOW are cleared. */
8197
8198static int
8199hscroll_windows (window)
8200 Lisp_Object window;
8201{
d475bcb8
GM
8202 int hscrolled_p;
8203
8204 if (automatic_hscrolling_p)
8205 {
8206 hscrolled_p = hscroll_window_tree (window);
8207 if (hscrolled_p)
8208 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8209 }
8210 else
8211 hscrolled_p = 0;
5f5c8ee5 8212 return hscrolled_p;
90adcf20 8213}
5f5c8ee5
GM
8214
8215
90adcf20 8216\f
5f5c8ee5
GM
8217/************************************************************************
8218 Redisplay
8219 ************************************************************************/
8220
8221/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8222 to a non-zero value. This is sometimes handy to have in a debugger
8223 session. */
8224
8225#if GLYPH_DEBUG
a2889657 8226
5f5c8ee5
GM
8227/* First and last unchanged row for try_window_id. */
8228
8229int debug_first_unchanged_at_end_vpos;
8230int debug_last_unchanged_at_beg_vpos;
8231
8232/* Delta vpos and y. */
8233
8234int debug_dvpos, debug_dy;
8235
8236/* Delta in characters and bytes for try_window_id. */
8237
8238int debug_delta, debug_delta_bytes;
8239
8240/* Values of window_end_pos and window_end_vpos at the end of
8241 try_window_id. */
8242
31ade731 8243EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
8244
8245/* Append a string to W->desired_matrix->method. FMT is a printf
8246 format string. A1...A9 are a supplement for a variable-length
8247 argument list. If trace_redisplay_p is non-zero also printf the
8248 resulting string to stderr. */
8249
8250static void
8251debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8252 struct window *w;
8253 char *fmt;
8254 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8255{
8256 char buffer[512];
8257 char *method = w->desired_matrix->method;
8258 int len = strlen (method);
8259 int size = sizeof w->desired_matrix->method;
8260 int remaining = size - len - 1;
8261
8262 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8263 if (len && remaining)
8264 {
8265 method[len] = '|';
8266 --remaining, ++len;
8267 }
8268
8269 strncpy (method + len, buffer, remaining);
8270
8271 if (trace_redisplay_p)
8272 fprintf (stderr, "%p (%s): %s\n",
8273 w,
8274 ((BUFFERP (w->buffer)
8275 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 8276 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
8277 : "no buffer"),
8278 buffer);
8279}
a2889657 8280
5f5c8ee5 8281#endif /* GLYPH_DEBUG */
90adcf20 8282
a2889657 8283
5f5c8ee5
GM
8284/* This counter is used to clear the face cache every once in a while
8285 in redisplay_internal. It is incremented for each redisplay.
8286 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8287 cleared. */
0d231165 8288
bb336f8d 8289#define CLEAR_FACE_CACHE_COUNT 500
463f6b91
RS
8290static int clear_face_cache_count;
8291
20de20dc 8292/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
8293
8294static struct frame *previous_terminal_frame;
8295
8296/* Non-zero while redisplay_internal is in progress. */
8297
8298int redisplaying_p;
8299
8300
8301/* Value is non-zero if all changes in window W, which displays
8302 current_buffer, are in the text between START and END. START is a
8303 buffer position, END is given as a distance from Z. Used in
8304 redisplay_internal for display optimization. */
8305
8306static INLINE int
8307text_outside_line_unchanged_p (w, start, end)
8308 struct window *w;
8309 int start, end;
8310{
8311 int unchanged_p = 1;
8312
8313 /* If text or overlays have changed, see where. */
8314 if (XFASTINT (w->last_modified) < MODIFF
8315 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8316 {
8317 /* Gap in the line? */
8318 if (GPT < start || Z - GPT < end)
8319 unchanged_p = 0;
8320
8321 /* Changes start in front of the line, or end after it? */
8322 if (unchanged_p
9142dd5b
GM
8323 && (BEG_UNCHANGED < start - 1
8324 || END_UNCHANGED < end))
5f5c8ee5
GM
8325 unchanged_p = 0;
8326
8327 /* If selective display, can't optimize if changes start at the
8328 beginning of the line. */
8329 if (unchanged_p
8330 && INTEGERP (current_buffer->selective_display)
8331 && XINT (current_buffer->selective_display) > 0
9142dd5b 8332 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 8333 unchanged_p = 0;
47d57b22
GM
8334
8335 /* If there are overlays at the start or end of the line, these
8336 may have overlay strings with newlines in them. A change at
8337 START, for instance, may actually concern the display of such
8338 overlay strings as well, and they are displayed on different
8339 lines. So, quickly rule out this case. (For the future, it
8340 might be desirable to implement something more telling than
8341 just BEG/END_UNCHANGED.) */
8342 if (unchanged_p)
8343 {
8344 if (BEG + BEG_UNCHANGED == start
8345 && overlay_touches_p (start))
8346 unchanged_p = 0;
8347 if (END_UNCHANGED == end
8348 && overlay_touches_p (Z - end))
8349 unchanged_p = 0;
8350 }
5f5c8ee5
GM
8351 }
8352
8353 return unchanged_p;
8354}
8355
8356
8357/* Do a frame update, taking possible shortcuts into account. This is
8358 the main external entry point for redisplay.
8359
8360 If the last redisplay displayed an echo area message and that message
8361 is no longer requested, we clear the echo area or bring back the
8362 mini-buffer if that is in use. */
20de20dc 8363
a2889657
JB
8364void
8365redisplay ()
e9874cee
RS
8366{
8367 redisplay_internal (0);
8368}
8369
47d57b22 8370
260a86a0
KH
8371/* Return 1 if point moved out of or into a composition. Otherwise
8372 return 0. PREV_BUF and PREV_PT are the last point buffer and
8373 position. BUF and PT are the current point buffer and position. */
8374
8375int
8376check_point_in_composition (prev_buf, prev_pt, buf, pt)
8377 struct buffer *prev_buf, *buf;
8378 int prev_pt, pt;
8379{
8380 int start, end;
8381 Lisp_Object prop;
8382 Lisp_Object buffer;
8383
8384 XSETBUFFER (buffer, buf);
8385 /* Check a composition at the last point if point moved within the
8386 same buffer. */
8387 if (prev_buf == buf)
8388 {
8389 if (prev_pt == pt)
8390 /* Point didn't move. */
8391 return 0;
8392
8393 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8394 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8395 && COMPOSITION_VALID_P (start, end, prop)
8396 && start < prev_pt && end > prev_pt)
8397 /* The last point was within the composition. Return 1 iff
8398 point moved out of the composition. */
8399 return (pt <= start || pt >= end);
8400 }
8401
8402 /* Check a composition at the current point. */
8403 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8404 && find_composition (pt, -1, &start, &end, &prop, buffer)
8405 && COMPOSITION_VALID_P (start, end, prop)
8406 && start < pt && end > pt);
8407}
5f5c8ee5 8408
47d57b22 8409
9142dd5b
GM
8410/* Reconsider the setting of B->clip_changed which is displayed
8411 in window W. */
8412
8413static INLINE void
8414reconsider_clip_changes (w, b)
8415 struct window *w;
8416 struct buffer *b;
8417{
8418 if (b->prevent_redisplay_optimizations_p)
8419 b->clip_changed = 1;
8420 else if (b->clip_changed
8421 && !NILP (w->window_end_valid)
8422 && w->current_matrix->buffer == b
8423 && w->current_matrix->zv == BUF_ZV (b)
8424 && w->current_matrix->begv == BUF_BEGV (b))
8425 b->clip_changed = 0;
260a86a0
KH
8426
8427 /* If display wasn't paused, and W is not a tool bar window, see if
8428 point has been moved into or out of a composition. In that case,
8429 we set b->clip_changed to 1 to force updating the screen. If
8430 b->clip_changed has already been set to 1, we can skip this
8431 check. */
8432 if (!b->clip_changed
8433 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8434 {
8435 int pt;
8436
8437 if (w == XWINDOW (selected_window))
8438 pt = BUF_PT (current_buffer);
8439 else
8440 pt = marker_position (w->pointm);
8441
8442 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 8443 || pt != XINT (w->last_point))
260a86a0 8444 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 8445 XINT (w->last_point),
260a86a0
KH
8446 XBUFFER (w->buffer), pt))
8447 b->clip_changed = 1;
8448 }
9142dd5b
GM
8449}
8450
8451
5f5c8ee5
GM
8452/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8453 response to any user action; therefore, we should preserve the echo
8454 area. (Actually, our caller does that job.) Perhaps in the future
8455 avoid recentering windows if it is not necessary; currently that
8456 causes some problems. */
e9874cee
RS
8457
8458static void
8459redisplay_internal (preserve_echo_area)
8460 int preserve_echo_area;
a2889657 8461{
5f5c8ee5
GM
8462 struct window *w = XWINDOW (selected_window);
8463 struct frame *f = XFRAME (w->frame);
8464 int pause;
a2889657 8465 int must_finish = 0;
5f5c8ee5 8466 struct text_pos tlbufpos, tlendpos;
89819bdd 8467 int number_of_visible_frames;
28514cd9 8468 int count;
886bd6f2 8469 struct frame *sf = SELECTED_FRAME ();
a2889657 8470
5f5c8ee5
GM
8471 /* Non-zero means redisplay has to consider all windows on all
8472 frames. Zero means, only selected_window is considered. */
8473 int consider_all_windows_p;
8474
8475 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8476
8477 /* No redisplay if running in batch mode or frame is not yet fully
8478 initialized, or redisplay is explicitly turned off by setting
8479 Vinhibit_redisplay. */
8480 if (noninteractive
8481 || !NILP (Vinhibit_redisplay)
8482 || !f->glyphs_initialized_p)
a2889657
JB
8483 return;
8484
5f5c8ee5
GM
8485 /* The flag redisplay_performed_directly_p is set by
8486 direct_output_for_insert when it already did the whole screen
8487 update necessary. */
8488 if (redisplay_performed_directly_p)
8489 {
8490 redisplay_performed_directly_p = 0;
8491 if (!hscroll_windows (selected_window))
8492 return;
8493 }
8494
15f0cf78
RS
8495#ifdef USE_X_TOOLKIT
8496 if (popup_activated ())
8497 return;
8498#endif
8499
28514cd9 8500 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 8501 if (redisplaying_p)
735c094c
KH
8502 return;
8503
28514cd9
GM
8504 /* Record a function that resets redisplaying_p to its old value
8505 when we leave this function. */
2d27dae2 8506 count = BINDING_STACK_SIZE ();
28514cd9
GM
8507 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8508 ++redisplaying_p;
8509
8b32d885 8510 retry:
bd9d0f3f 8511 pause = 0;
9142dd5b
GM
8512 reconsider_clip_changes (w, current_buffer);
8513
5f5c8ee5
GM
8514 /* If new fonts have been loaded that make a glyph matrix adjustment
8515 necessary, do it. */
8516 if (fonts_changed_p)
8517 {
8518 adjust_glyphs (NULL);
8519 ++windows_or_buffers_changed;
8520 fonts_changed_p = 0;
8521 }
8522
6961e0c1
GM
8523 /* If face_change_count is non-zero, init_iterator will free all
8524 realized faces, which includes the faces referenced from current
8525 matrices. So, we can't reuse current matrices in this case. */
8526 if (face_change_count)
8527 ++windows_or_buffers_changed;
8528
886bd6f2
GM
8529 if (! FRAME_WINDOW_P (sf)
8530 && previous_terminal_frame != sf)
20de20dc 8531 {
5f5c8ee5
GM
8532 /* Since frames on an ASCII terminal share the same display
8533 area, displaying a different frame means redisplay the whole
8534 thing. */
20de20dc 8535 windows_or_buffers_changed++;
886bd6f2
GM
8536 SET_FRAME_GARBAGED (sf);
8537 XSETFRAME (Vterminal_frame, sf);
20de20dc 8538 }
886bd6f2 8539 previous_terminal_frame = sf;
20de20dc 8540
5f5c8ee5
GM
8541 /* Set the visible flags for all frames. Do this before checking
8542 for resized or garbaged frames; they want to know if their frames
8543 are visible. See the comment in frame.h for
8544 FRAME_SAMPLE_VISIBILITY. */
d724d989 8545 {
35f56f96 8546 Lisp_Object tail, frame;
d724d989 8547
89819bdd
RS
8548 number_of_visible_frames = 0;
8549
35f56f96 8550 FOR_EACH_FRAME (tail, frame)
f82aff7c 8551 {
5f5c8ee5
GM
8552 struct frame *f = XFRAME (frame);
8553
8554 FRAME_SAMPLE_VISIBILITY (f);
8555 if (FRAME_VISIBLE_P (f))
8556 ++number_of_visible_frames;
8557 clear_desired_matrices (f);
f82aff7c 8558 }
d724d989
JB
8559 }
8560
44fa5b1e 8561 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8562 do_pending_window_change (1);
a2889657 8563
5f5c8ee5 8564 /* Clear frames marked as garbaged. */
44fa5b1e 8565 if (frame_garbaged)
c6e89d6c 8566 clear_garbaged_frames ();
a2889657 8567
e037b9ec 8568 /* Build menubar and tool-bar items. */
f82aff7c
RS
8569 prepare_menu_bars ();
8570
28995e67 8571 if (windows_or_buffers_changed)
a2889657
JB
8572 update_mode_lines++;
8573
538f13d4
RS
8574 /* Detect case that we need to write or remove a star in the mode line. */
8575 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8576 {
8577 w->update_mode_line = Qt;
8578 if (buffer_shared > 1)
8579 update_mode_lines++;
8580 }
8581
5f5c8ee5 8582 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8583 if (!NILP (w->column_number_displayed)
8584 /* This alternative quickly identifies a common case
8585 where no change is needed. */
8586 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8587 && XFASTINT (w->last_modified) >= MODIFF
8588 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8589 && XFASTINT (w->column_number_displayed) != current_column ())
8590 w->update_mode_line = Qt;
8591
44fa5b1e 8592 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8593
5f5c8ee5
GM
8594 /* The variable buffer_shared is set in redisplay_window and
8595 indicates that we redisplay a buffer in different windows. See
8596 there. */
5fb96e96
RS
8597 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8598 || cursor_type_changed);
a2889657
JB
8599
8600 /* If specs for an arrow have changed, do thorough redisplay
8601 to ensure we remove any arrow that should no longer exist. */
d45de95b 8602 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8603 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8604 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8605
90adcf20
RS
8606 /* Normally the message* functions will have already displayed and
8607 updated the echo area, but the frame may have been trashed, or
8608 the update may have been preempted, so display the echo area
6e019995 8609 again here. Checking message_cleared_p captures the case that
c6e89d6c 8610 the echo area should be cleared. */
96d5e9a0
GM
8611 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8612 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
8613 || (message_cleared_p
8614 && minibuf_level == 0
8615 /* If the mini-window is currently selected, this means the
8616 echo-area doesn't show through. */
8617 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 8618 {
c6e89d6c 8619 int window_height_changed_p = echo_area_display (0);
90adcf20 8620 must_finish = 1;
6e019995
GM
8621
8622 /* If we don't display the current message, don't clear the
8623 message_cleared_p flag, because, if we did, we wouldn't clear
8624 the echo area in the next redisplay which doesn't preserve
8625 the echo area. */
8626 if (!display_last_displayed_message_p)
8627 message_cleared_p = 0;
dd2eb166 8628
c6e89d6c
GM
8629 if (fonts_changed_p)
8630 goto retry;
8631 else if (window_height_changed_p)
8632 {
8633 consider_all_windows_p = 1;
8634 ++update_mode_lines;
8635 ++windows_or_buffers_changed;
9142dd5b
GM
8636
8637 /* If window configuration was changed, frames may have been
8638 marked garbaged. Clear them or we will experience
8639 surprises wrt scrolling. */
8640 if (frame_garbaged)
8641 clear_garbaged_frames ();
c6e89d6c 8642 }
90adcf20 8643 }
97a36635 8644 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
8645 && (current_buffer->clip_changed
8646 || XFASTINT (w->last_modified) < MODIFF
8647 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 8648 && resize_mini_window (w, 0))
c6e89d6c
GM
8649 {
8650 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
8651 showing if its contents might have changed. */
8652 must_finish = 1;
8653 consider_all_windows_p = 1;
c6e89d6c 8654 ++windows_or_buffers_changed;
dd2eb166 8655 ++update_mode_lines;
9142dd5b
GM
8656
8657 /* If window configuration was changed, frames may have been
8658 marked garbaged. Clear them or we will experience
8659 surprises wrt scrolling. */
8660 if (frame_garbaged)
8661 clear_garbaged_frames ();
c6e89d6c
GM
8662 }
8663
90adcf20 8664
5f5c8ee5
GM
8665 /* If showing the region, and mark has changed, we must redisplay
8666 the whole window. The assignment to this_line_start_pos prevents
8667 the optimization directly below this if-statement. */
bd66d1ba
RS
8668 if (((!NILP (Vtransient_mark_mode)
8669 && !NILP (XBUFFER (w->buffer)->mark_active))
8670 != !NILP (w->region_showing))
82d04750
JB
8671 || (!NILP (w->region_showing)
8672 && !EQ (w->region_showing,
8673 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
8674 CHARPOS (this_line_start_pos) = 0;
8675
8676 /* Optimize the case that only the line containing the cursor in the
8677 selected window has changed. Variables starting with this_ are
8678 set in display_line and record information about the line
8679 containing the cursor. */
8680 tlbufpos = this_line_start_pos;
8681 tlendpos = this_line_end_pos;
8682 if (!consider_all_windows_p
8683 && CHARPOS (tlbufpos) > 0
8684 && NILP (w->update_mode_line)
73af359d 8685 && !current_buffer->clip_changed
44fa5b1e 8686 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 8687 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 8688 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
8689 && this_line_buffer == current_buffer
8690 && current_buffer == XBUFFER (w->buffer)
265a9e55 8691 && NILP (w->force_start)
5f5c8ee5
GM
8692 /* Point must be on the line that we have info recorded about. */
8693 && PT >= CHARPOS (tlbufpos)
8694 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
8695 /* All text outside that line, including its final newline,
8696 must be unchanged */
5f5c8ee5
GM
8697 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8698 CHARPOS (tlendpos)))
8699 {
8700 if (CHARPOS (tlbufpos) > BEGV
8701 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8702 && (CHARPOS (tlbufpos) == ZV
8703 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
8704 /* Former continuation line has disappeared by becoming empty */
8705 goto cancel;
8706 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 8707 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
8708 || MINI_WINDOW_P (w))
8709 {
1c9241f5
KH
8710 /* We have to handle the case of continuation around a
8711 wide-column character (See the comment in indent.c around
8712 line 885).
8713
8714 For instance, in the following case:
8715
8716 -------- Insert --------
8717 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8718 J_I_ ==> J_I_ `^^' are cursors.
8719 ^^ ^^
8720 -------- --------
8721
8722 As we have to redraw the line above, we should goto cancel. */
8723
5f5c8ee5
GM
8724 struct it it;
8725 int line_height_before = this_line_pixel_height;
8726
8727 /* Note that start_display will handle the case that the
8728 line starting at tlbufpos is a continuation lines. */
8729 start_display (&it, w, tlbufpos);
8730
8731 /* Implementation note: It this still necessary? */
8732 if (it.current_x != this_line_start_x)
1c9241f5
KH
8733 goto cancel;
8734
5f5c8ee5
GM
8735 TRACE ((stderr, "trying display optimization 1\n"));
8736 w->cursor.vpos = -1;
a2889657 8737 overlay_arrow_seen = 0;
5f5c8ee5
GM
8738 it.vpos = this_line_vpos;
8739 it.current_y = this_line_y;
8740 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8741 display_line (&it);
8742
a2889657 8743 /* If line contains point, is not continued,
5f5c8ee5
GM
8744 and ends at same distance from eob as before, we win */
8745 if (w->cursor.vpos >= 0
8746 /* Line is not continued, otherwise this_line_start_pos
8747 would have been set to 0 in display_line. */
8748 && CHARPOS (this_line_start_pos)
8749 /* Line ends as before. */
8750 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8751 /* Line has same height as before. Otherwise other lines
8752 would have to be shifted up or down. */
8753 && this_line_pixel_height == line_height_before)
a2889657 8754 {
5f5c8ee5
GM
8755 /* If this is not the window's last line, we must adjust
8756 the charstarts of the lines below. */
8757 if (it.current_y < it.last_visible_y)
8758 {
8759 struct glyph_row *row
8760 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8761 int delta, delta_bytes;
8762
8763 if (Z - CHARPOS (tlendpos) == ZV)
8764 {
8765 /* This line ends at end of (accessible part of)
8766 buffer. There is no newline to count. */
8767 delta = (Z
8768 - CHARPOS (tlendpos)
8769 - MATRIX_ROW_START_CHARPOS (row));
8770 delta_bytes = (Z_BYTE
8771 - BYTEPOS (tlendpos)
8772 - MATRIX_ROW_START_BYTEPOS (row));
8773 }
8774 else
8775 {
8776 /* This line ends in a newline. Must take
8777 account of the newline and the rest of the
8778 text that follows. */
8779 delta = (Z
8780 - CHARPOS (tlendpos)
8781 - MATRIX_ROW_START_CHARPOS (row));
8782 delta_bytes = (Z_BYTE
8783 - BYTEPOS (tlendpos)
8784 - MATRIX_ROW_START_BYTEPOS (row));
8785 }
8786
f2d86d7a
GM
8787 increment_matrix_positions (w->current_matrix,
8788 this_line_vpos + 1,
8789 w->current_matrix->nrows,
8790 delta, delta_bytes);
85bcef6c 8791 }
46db8486 8792
5f5c8ee5
GM
8793 /* If this row displays text now but previously didn't,
8794 or vice versa, w->window_end_vpos may have to be
8795 adjusted. */
8796 if ((it.glyph_row - 1)->displays_text_p)
8797 {
8798 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8799 XSETINT (w->window_end_vpos, this_line_vpos);
8800 }
8801 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8802 && this_line_vpos > 0)
8803 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8804 w->window_end_valid = Qnil;
8805
8806 /* Update hint: No need to try to scroll in update_window. */
8807 w->desired_matrix->no_scrolling_p = 1;
8808
8809#if GLYPH_DEBUG
8810 *w->desired_matrix->method = 0;
8811 debug_method_add (w, "optimization 1");
8812#endif
a2889657
JB
8813 goto update;
8814 }
8815 else
8816 goto cancel;
8817 }
5f5c8ee5
GM
8818 else if (/* Cursor position hasn't changed. */
8819 PT == XFASTINT (w->last_point)
b6f0fe04
RS
8820 /* Make sure the cursor was last displayed
8821 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
8822 && 0 <= w->cursor.vpos
8823 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
8824 {
8825 if (!must_finish)
8826 {
c6e89d6c 8827 do_pending_window_change (1);
5f5c8ee5
GM
8828
8829 /* We used to always goto end_of_redisplay here, but this
8830 isn't enough if we have a blinking cursor. */
8831 if (w->cursor_off_p == w->last_cursor_off_p)
8832 goto end_of_redisplay;
a2889657
JB
8833 }
8834 goto update;
8835 }
8b51f1e3
KH
8836 /* If highlighting the region, or if the cursor is in the echo area,
8837 then we can't just move the cursor. */
bd66d1ba
RS
8838 else if (! (!NILP (Vtransient_mark_mode)
8839 && !NILP (current_buffer->mark_active))
97a36635 8840 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 8841 || highlight_nonselected_windows)
8b51f1e3 8842 && NILP (w->region_showing)
8f897821 8843 && NILP (Vshow_trailing_whitespace)
8b51f1e3 8844 && !cursor_in_echo_area)
a2889657 8845 {
5f5c8ee5
GM
8846 struct it it;
8847 struct glyph_row *row;
8848
8849 /* Skip from tlbufpos to PT and see where it is. Note that
8850 PT may be in invisible text. If so, we will end at the
8851 next visible position. */
8852 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8853 NULL, DEFAULT_FACE_ID);
8854 it.current_x = this_line_start_x;
8855 it.current_y = this_line_y;
8856 it.vpos = this_line_vpos;
8857
8858 /* The call to move_it_to stops in front of PT, but
8859 moves over before-strings. */
8860 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8861
8862 if (it.vpos == this_line_vpos
8863 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8864 row->enabled_p))
a2889657 8865 {
5f5c8ee5
GM
8866 xassert (this_line_vpos == it.vpos);
8867 xassert (this_line_y == it.current_y);
8868 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
8869#if GLYPH_DEBUG
8870 *w->desired_matrix->method = 0;
8871 debug_method_add (w, "optimization 3");
8872#endif
a2889657
JB
8873 goto update;
8874 }
8875 else
8876 goto cancel;
8877 }
5f5c8ee5 8878
a2889657 8879 cancel:
5f5c8ee5
GM
8880 /* Text changed drastically or point moved off of line. */
8881 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
8882 }
8883
5f5c8ee5
GM
8884 CHARPOS (this_line_start_pos) = 0;
8885 consider_all_windows_p |= buffer_shared > 1;
8886 ++clear_face_cache_count;
a2889657 8887
5f5c8ee5 8888
bd9d0f3f
GM
8889 /* Build desired matrices, and update the display. If
8890 consider_all_windows_p is non-zero, do it for all windows on all
8891 frames. Otherwise do it for selected_window, only. */
463f6b91 8892
5f5c8ee5 8893 if (consider_all_windows_p)
a2889657 8894 {
35f56f96 8895 Lisp_Object tail, frame;
0528abe1
GM
8896 int i, n = 0, size = 50;
8897 struct frame **updated
8898 = (struct frame **) alloca (size * sizeof *updated);
a2889657 8899
5f5c8ee5
GM
8900 /* Clear the face cache eventually. */
8901 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 8902 {
5f5c8ee5 8903 clear_face_cache (0);
463f6b91
RS
8904 clear_face_cache_count = 0;
8905 }
31b24551 8906
5f5c8ee5
GM
8907 /* Recompute # windows showing selected buffer. This will be
8908 incremented each time such a window is displayed. */
a2889657
JB
8909 buffer_shared = 0;
8910
35f56f96 8911 FOR_EACH_FRAME (tail, frame)
30c566e4 8912 {
5f5c8ee5 8913 struct frame *f = XFRAME (frame);
bd9d0f3f 8914
886bd6f2 8915 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 8916 {
ae02e06a 8917#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
8918 if (clear_face_cache_count % 50 == 0
8919 && FRAME_WINDOW_P (f))
8920 clear_image_cache (f, 0);
ae02e06a 8921#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 8922
5f5c8ee5
GM
8923 /* Mark all the scroll bars to be removed; we'll redeem
8924 the ones we want when we redisplay their windows. */
9769686d 8925 if (condemn_scroll_bars_hook)
504454e8 8926 condemn_scroll_bars_hook (f);
30c566e4 8927
f21ef775 8928 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 8929 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 8930
5f5c8ee5
GM
8931 /* Any scroll bars which redisplay_windows should have
8932 nuked should now go away. */
9769686d 8933 if (judge_scroll_bars_hook)
504454e8 8934 judge_scroll_bars_hook (f);
bd9d0f3f
GM
8935
8936 /* If fonts changed, display again. */
8937 if (fonts_changed_p)
8938 goto retry;
8939
8940 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8941 {
8942 /* See if we have to hscroll. */
8943 if (hscroll_windows (f->root_window))
8944 goto retry;
8945
8946 /* Prevent various kinds of signals during display
8947 update. stdio is not robust about handling
8948 signals, which can cause an apparent I/O
8949 error. */
8950 if (interrupt_input)
8951 unrequest_sigio ();
8952 stop_polling ();
8953
8954 /* Update the display. */
8955 set_window_update_flags (XWINDOW (f->root_window), 1);
8956 pause |= update_frame (f, 0, 0);
8957 if (pause)
8958 break;
8959
0528abe1
GM
8960 if (n == size)
8961 {
8962 int nbytes = size * sizeof *updated;
8963 struct frame **p = (struct frame **) alloca (2 * nbytes);
8964 bcopy (updated, p, nbytes);
8965 size *= 2;
8966 }
8967
8968 updated[n++] = f;
bd9d0f3f 8969 }
9769686d 8970 }
30c566e4 8971 }
0528abe1
GM
8972
8973 /* Do the mark_window_display_accurate after all windows have
8974 been redisplayed because this call resets flags in buffers
8975 which are needed for proper redisplay. */
8976 for (i = 0; i < n; ++i)
8977 {
8978 struct frame *f = updated[i];
8979 mark_window_display_accurate (f->root_window, 1);
8980 if (frame_up_to_date_hook)
8981 frame_up_to_date_hook (f);
8982 }
a2889657 8983 }
bd9d0f3f
GM
8984 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8985 {
8986 Lisp_Object mini_window;
8987 struct frame *mini_frame;
5f5c8ee5 8988
82a7ab23 8989 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
8990 /* Use list_of_error, not Qerror, so that
8991 we catch only errors and don't run the debugger. */
8992 internal_condition_case_1 (redisplay_window_1, selected_window,
8993 list_of_error,
82a7ab23 8994 redisplay_window_error);
5f5c8ee5 8995
bd9d0f3f
GM
8996 /* Compare desired and current matrices, perform output. */
8997 update:
5f5c8ee5 8998
bd9d0f3f
GM
8999 /* If fonts changed, display again. */
9000 if (fonts_changed_p)
92a90e89 9001 goto retry;
a2889657 9002
bd9d0f3f
GM
9003 /* Prevent various kinds of signals during display update.
9004 stdio is not robust about handling signals,
9005 which can cause an apparent I/O error. */
9006 if (interrupt_input)
9007 unrequest_sigio ();
9008 stop_polling ();
1af9f229 9009
bd9d0f3f 9010 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 9011 {
92a90e89
GM
9012 if (hscroll_windows (selected_window))
9013 goto retry;
9014
5f5c8ee5 9015 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 9016 pause = update_frame (sf, 0, 0);
5f5c8ee5 9017 }
d724d989 9018
8de2d90b 9019 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
9020 function. If the echo area is on another frame, that may
9021 have put text on a frame other than the selected one, so the
9022 above call to update_frame would not have caught it. Catch
8de2d90b 9023 it here. */
bd9d0f3f
GM
9024 mini_window = FRAME_MINIBUF_WINDOW (sf);
9025 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 9026
bd9d0f3f
GM
9027 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9028 {
9029 XWINDOW (mini_window)->must_be_updated_p = 1;
9030 pause |= update_frame (mini_frame, 0, 0);
9031 if (!pause && hscroll_windows (mini_window))
9032 goto retry;
9033 }
6e8290aa 9034 }
a2889657 9035
5f5c8ee5
GM
9036 /* If display was paused because of pending input, make sure we do a
9037 thorough update the next time. */
a2889657
JB
9038 if (pause)
9039 {
5f5c8ee5
GM
9040 /* Prevent the optimization at the beginning of
9041 redisplay_internal that tries a single-line update of the
9042 line containing the cursor in the selected window. */
9043 CHARPOS (this_line_start_pos) = 0;
9044
9045 /* Let the overlay arrow be updated the next time. */
265a9e55 9046 if (!NILP (last_arrow_position))
a2889657
JB
9047 {
9048 last_arrow_position = Qt;
9049 last_arrow_string = Qt;
9050 }
5f5c8ee5
GM
9051
9052 /* If we pause after scrolling, some rows in the current
9053 matrices of some windows are not valid. */
9054 if (!WINDOW_FULL_WIDTH_P (w)
9055 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
9056 update_mode_lines = 1;
9057 }
43c09969 9058 else
a2889657 9059 {
43c09969 9060 if (!consider_all_windows_p)
a2889657 9061 {
43c09969
GM
9062 /* This has already been done above if
9063 consider_all_windows_p is set. */
9064 mark_window_display_accurate_1 (w, 1);
927c5b3b
GM
9065
9066 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9067 last_arrow_string = Voverlay_arrow_string;
9068
efc63ef0 9069 if (frame_up_to_date_hook != 0)
43c09969 9070 frame_up_to_date_hook (sf);
a2889657 9071 }
15e26c76 9072
a2889657
JB
9073 update_mode_lines = 0;
9074 windows_or_buffers_changed = 0;
5fb96e96 9075 cursor_type_changed = 0;
a2889657
JB
9076 }
9077
5f5c8ee5
GM
9078 /* Start SIGIO interrupts coming again. Having them off during the
9079 code above makes it less likely one will discard output, but not
9080 impossible, since there might be stuff in the system buffer here.
a2889657 9081 But it is much hairier to try to do anything about that. */
a2889657
JB
9082 if (interrupt_input)
9083 request_sigio ();
9084 start_polling ();
9085
5f5c8ee5
GM
9086 /* If a frame has become visible which was not before, redisplay
9087 again, so that we display it. Expose events for such a frame
9088 (which it gets when becoming visible) don't call the parts of
9089 redisplay constructing glyphs, so simply exposing a frame won't
9090 display anything in this case. So, we have to display these
9091 frames here explicitly. */
11c52c4f
RS
9092 if (!pause)
9093 {
9094 Lisp_Object tail, frame;
9095 int new_count = 0;
9096
9097 FOR_EACH_FRAME (tail, frame)
9098 {
9099 int this_is_visible = 0;
8e83f802
RS
9100
9101 if (XFRAME (frame)->visible)
9102 this_is_visible = 1;
9103 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9104 if (XFRAME (frame)->visible)
9105 this_is_visible = 1;
11c52c4f
RS
9106
9107 if (this_is_visible)
9108 new_count++;
9109 }
9110
89819bdd 9111 if (new_count != number_of_visible_frames)
11c52c4f
RS
9112 windows_or_buffers_changed++;
9113 }
9114
44fa5b1e 9115 /* Change frame size now if a change is pending. */
c6e89d6c 9116 do_pending_window_change (1);
d8e242fd 9117
8b32d885
RS
9118 /* If we just did a pending size change, or have additional
9119 visible frames, redisplay again. */
3c8c72e0 9120 if (windows_or_buffers_changed && !pause)
8b32d885 9121 goto retry;
5f5c8ee5
GM
9122
9123 end_of_redisplay:;
c6e89d6c 9124
28514cd9 9125 unbind_to (count, Qnil);
a2889657
JB
9126}
9127
5f5c8ee5
GM
9128
9129/* Redisplay, but leave alone any recent echo area message unless
9130 another message has been requested in its place.
a2889657
JB
9131
9132 This is useful in situations where you need to redisplay but no
9133 user action has occurred, making it inappropriate for the message
9134 area to be cleared. See tracking_off and
9bf76936
GM
9135 wait_reading_process_input for examples of these situations.
9136
9137 FROM_WHERE is an integer saying from where this function was
9138 called. This is useful for debugging. */
a2889657 9139
8991bb31 9140void
9bf76936
GM
9141redisplay_preserve_echo_area (from_where)
9142 int from_where;
a2889657 9143{
9bf76936
GM
9144 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9145
c6e89d6c 9146 if (!NILP (echo_area_buffer[1]))
a2889657 9147 {
c6e89d6c
GM
9148 /* We have a previously displayed message, but no current
9149 message. Redisplay the previous message. */
9150 display_last_displayed_message_p = 1;
e9874cee 9151 redisplay_internal (1);
c6e89d6c 9152 display_last_displayed_message_p = 0;
a2889657
JB
9153 }
9154 else
e9874cee 9155 redisplay_internal (1);
a2889657
JB
9156}
9157
5f5c8ee5 9158
28514cd9
GM
9159/* Function registered with record_unwind_protect in
9160 redisplay_internal. Clears the flag indicating that a redisplay is
9161 in progress. */
9162
9163static Lisp_Object
9164unwind_redisplay (old_redisplaying_p)
9165 Lisp_Object old_redisplaying_p;
9166{
9167 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 9168 return Qnil;
28514cd9
GM
9169}
9170
9171
43c09969
GM
9172/* Mark the display of window W as accurate or inaccurate. If
9173 ACCURATE_P is non-zero mark display of W as accurate. If
9174 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9175 redisplay_internal is called. */
5f5c8ee5 9176
43c09969
GM
9177static void
9178mark_window_display_accurate_1 (w, accurate_p)
9179 struct window *w;
5f5c8ee5 9180 int accurate_p;
a2889657 9181{
43c09969 9182 if (BUFFERP (w->buffer))
a2889657 9183 {
43c09969
GM
9184 struct buffer *b = XBUFFER (w->buffer);
9185
9186 w->last_modified
9187 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9188 w->last_overlay_modified
9189 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9190 w->last_had_star
9191 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 9192
43c09969 9193 if (accurate_p)
bd66d1ba 9194 {
43c09969
GM
9195 b->clip_changed = 0;
9196 b->prevent_redisplay_optimizations_p = 0;
9197
9198 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9199 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9200 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9201 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
5f5c8ee5 9202
43c09969
GM
9203 w->current_matrix->buffer = b;
9204 w->current_matrix->begv = BUF_BEGV (b);
9205 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5 9206
43c09969
GM
9207 w->last_cursor = w->cursor;
9208 w->last_cursor_off_p = w->cursor_off_p;
9209
9210 if (w == XWINDOW (selected_window))
9211 w->last_point = make_number (BUF_PT (b));
9212 else
9213 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 9214 }
43c09969 9215 }
bd66d1ba 9216
43c09969
GM
9217 if (accurate_p)
9218 {
d2f84654 9219 w->window_end_valid = w->buffer;
99332eb6 9220#if 0 /* This is incorrect with variable-height lines. */
2913a9c0
GM
9221 xassert (XINT (w->window_end_vpos)
9222 < (XINT (w->height)
9223 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 9224#endif
a2889657 9225 w->update_mode_line = Qnil;
43c09969
GM
9226 }
9227}
9228
9229
9230/* Mark the display of windows in the window tree rooted at WINDOW as
9231 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9232 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9233 be redisplayed the next time redisplay_internal is called. */
9234
9235void
9236mark_window_display_accurate (window, accurate_p)
9237 Lisp_Object window;
9238 int accurate_p;
9239{
9240 struct window *w;
9241
9242 for (; !NILP (window); window = w->next)
9243 {
9244 w = XWINDOW (window);
9245 mark_window_display_accurate_1 (w, accurate_p);
a2889657 9246
265a9e55 9247 if (!NILP (w->vchild))
5f5c8ee5 9248 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 9249 if (!NILP (w->hchild))
5f5c8ee5 9250 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
9251 }
9252
5f5c8ee5 9253 if (accurate_p)
a2889657 9254 {
d45de95b 9255 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
9256 last_arrow_string = Voverlay_arrow_string;
9257 }
9258 else
9259 {
5f5c8ee5
GM
9260 /* Force a thorough redisplay the next time by setting
9261 last_arrow_position and last_arrow_string to t, which is
4b41cebb 9262 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
9263 last_arrow_position = Qt;
9264 last_arrow_string = Qt;
9265 }
9266}
5f5c8ee5
GM
9267
9268
9269/* Return value in display table DP (Lisp_Char_Table *) for character
9270 C. Since a display table doesn't have any parent, we don't have to
9271 follow parent. Do not call this function directly but use the
9272 macro DISP_CHAR_VECTOR. */
9273
9274Lisp_Object
9275disp_char_vector (dp, c)
9276 struct Lisp_Char_Table *dp;
9277 int c;
9278{
9279 int code[4], i;
9280 Lisp_Object val;
9281
9282 if (SINGLE_BYTE_CHAR_P (c))
9283 return (dp->contents[c]);
9284
c5924f47 9285 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
9286 if (code[1] < 32)
9287 code[1] = -1;
9288 else if (code[2] < 32)
9289 code[2] = -1;
5f5c8ee5
GM
9290
9291 /* Here, the possible range of code[0] (== charset ID) is
9292 128..max_charset. Since the top level char table contains data
9293 for multibyte characters after 256th element, we must increment
9294 code[0] by 128 to get a correct index. */
9295 code[0] += 128;
9296 code[3] = -1; /* anchor */
9297
9298 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9299 {
9300 val = dp->contents[code[i]];
9301 if (!SUB_CHAR_TABLE_P (val))
9302 return (NILP (val) ? dp->defalt : val);
9303 }
9304
9305 /* Here, val is a sub char table. We return the default value of
9306 it. */
9307 return (dp->defalt);
9308}
9309
9310
a2889657 9311\f
5f5c8ee5
GM
9312/***********************************************************************
9313 Window Redisplay
9314 ***********************************************************************/
a2725ab2 9315
5f5c8ee5 9316/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
9317
9318static void
5f5c8ee5
GM
9319redisplay_windows (window)
9320 Lisp_Object window;
90adcf20 9321{
5f5c8ee5
GM
9322 while (!NILP (window))
9323 {
9324 struct window *w = XWINDOW (window);
9325
9326 if (!NILP (w->hchild))
9327 redisplay_windows (w->hchild);
9328 else if (!NILP (w->vchild))
9329 redisplay_windows (w->vchild);
9330 else
82a7ab23
RS
9331 {
9332 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
9333 /* Use list_of_error, not Qerror, so that
9334 we catch only errors and don't run the debugger. */
9335 internal_condition_case_1 (redisplay_window_0, window,
9336 list_of_error,
82a7ab23
RS
9337 redisplay_window_error);
9338 }
a2725ab2 9339
5f5c8ee5
GM
9340 window = w->next;
9341 }
9342}
9343
82a7ab23
RS
9344static Lisp_Object
9345redisplay_window_error ()
9346{
9347 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9348 return Qnil;
9349}
9350
9351static Lisp_Object
9352redisplay_window_0 (window)
9353 Lisp_Object window;
9354{
9355 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9356 redisplay_window (window, 0);
9357 return Qnil;
9358}
5f5c8ee5 9359
82a7ab23
RS
9360static Lisp_Object
9361redisplay_window_1 (window)
9362 Lisp_Object window;
9363{
9364 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9365 redisplay_window (window, 1);
9366 return Qnil;
9367}
9368\f
5f5c8ee5
GM
9369/* Set cursor position of W. PT is assumed to be displayed in ROW.
9370 DELTA is the number of bytes by which positions recorded in ROW
9371 differ from current buffer positions. */
9372
9373void
9374set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9375 struct window *w;
9376 struct glyph_row *row;
9377 struct glyph_matrix *matrix;
9378 int delta, delta_bytes, dy, dvpos;
9379{
9380 struct glyph *glyph = row->glyphs[TEXT_AREA];
9381 struct glyph *end = glyph + row->used[TEXT_AREA];
9382 int x = row->x;
9383 int pt_old = PT - delta;
9384
9385 /* Skip over glyphs not having an object at the start of the row.
9386 These are special glyphs like truncation marks on terminal
9387 frames. */
9388 if (row->displays_text_p)
9389 while (glyph < end
6fc556fd 9390 && INTEGERP (glyph->object)
5f5c8ee5
GM
9391 && glyph->charpos < 0)
9392 {
9393 x += glyph->pixel_width;
9394 ++glyph;
9395 }
9396
9397 while (glyph < end
6fc556fd 9398 && !INTEGERP (glyph->object)
5f5c8ee5
GM
9399 && (!BUFFERP (glyph->object)
9400 || glyph->charpos < pt_old))
9401 {
9402 x += glyph->pixel_width;
9403 ++glyph;
9404 }
9405
9406 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9407 w->cursor.x = x;
9408 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9409 w->cursor.y = row->y + dy;
9410
9411 if (w == XWINDOW (selected_window))
9412 {
9413 if (!row->continued_p
9414 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9415 && row->x == 0)
9416 {
9417 this_line_buffer = XBUFFER (w->buffer);
9418
9419 CHARPOS (this_line_start_pos)
9420 = MATRIX_ROW_START_CHARPOS (row) + delta;
9421 BYTEPOS (this_line_start_pos)
9422 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9423
9424 CHARPOS (this_line_end_pos)
9425 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9426 BYTEPOS (this_line_end_pos)
9427 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9428
9429 this_line_y = w->cursor.y;
9430 this_line_pixel_height = row->height;
9431 this_line_vpos = w->cursor.vpos;
9432 this_line_start_x = row->x;
9433 }
9434 else
9435 CHARPOS (this_line_start_pos) = 0;
9436 }
9437}
9438
9439
9440/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
9441 start STARTP. Sets the window start of WINDOW to that position.
9442
9443 We assume that the window's buffer is really current. */
5f5c8ee5
GM
9444
9445static INLINE struct text_pos
9446run_window_scroll_functions (window, startp)
9447 Lisp_Object window;
9448 struct text_pos startp;
9449{
9450 struct window *w = XWINDOW (window);
9451 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
9452
9453 if (current_buffer != XBUFFER (w->buffer))
9454 abort ();
9455
5f5c8ee5
GM
9456 if (!NILP (Vwindow_scroll_functions))
9457 {
9458 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9459 make_number (CHARPOS (startp)));
9460 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
9461 /* In case the hook functions switch buffers. */
9462 if (current_buffer != XBUFFER (w->buffer))
9463 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 9464 }
90adcf20 9465
5f5c8ee5
GM
9466 return startp;
9467}
9468
9469
9470/* Modify the desired matrix of window W and W->vscroll so that the
cb617e7c
GM
9471 line containing the cursor is fully visible. If this requires
9472 larger matrices than are allocated, set fonts_changed_p and return
9473 0. */
5f5c8ee5 9474
cb617e7c 9475static int
5f5c8ee5
GM
9476make_cursor_line_fully_visible (w)
9477 struct window *w;
9478{
9479 struct glyph_matrix *matrix;
9480 struct glyph_row *row;
478d746b 9481 int window_height;
5f5c8ee5
GM
9482
9483 /* It's not always possible to find the cursor, e.g, when a window
9484 is full of overlay strings. Don't do anything in that case. */
9485 if (w->cursor.vpos < 0)
cb617e7c 9486 return 1;
5f5c8ee5
GM
9487
9488 matrix = w->desired_matrix;
9489 row = MATRIX_ROW (matrix, w->cursor.vpos);
9490
b28cb6ed
GM
9491 /* If the cursor row is not partially visible, there's nothing
9492 to do. */
9493 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 9494 return 1;
b28cb6ed
GM
9495
9496 /* If the row the cursor is in is taller than the window's height,
9497 it's not clear what to do, so do nothing. */
9498 window_height = window_box_height (w);
9499 if (row->height >= window_height)
cb617e7c 9500 return 1;
b28cb6ed
GM
9501
9502 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
9503 {
9504 int dy = row->height - row->visible_height;
9505 w->vscroll = 0;
9506 w->cursor.y += dy;
9507 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9508 }
b28cb6ed 9509 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
9510 {
9511 int dy = - (row->height - row->visible_height);
9512 w->vscroll = dy;
9513 w->cursor.y += dy;
9514 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9515 }
b28cb6ed 9516
5f5c8ee5
GM
9517 /* When we change the cursor y-position of the selected window,
9518 change this_line_y as well so that the display optimization for
9519 the cursor line of the selected window in redisplay_internal uses
9520 the correct y-position. */
9521 if (w == XWINDOW (selected_window))
9522 this_line_y = w->cursor.y;
cb617e7c
GM
9523
9524 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9525 redisplay with larger matrices. */
9526 if (matrix->nrows < required_matrix_height (w))
9527 {
9528 fonts_changed_p = 1;
9529 return 0;
9530 }
9531
9532 return 1;
5f5c8ee5
GM
9533}
9534
9535
9536/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9537 non-zero means only WINDOW is redisplayed in redisplay_internal.
9538 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9539 in redisplay_window to bring a partially visible line into view in
9540 the case that only the cursor has moved.
9541
9542 Value is
9543
9544 1 if scrolling succeeded
9545
9546 0 if scrolling didn't find point.
9547
9548 -1 if new fonts have been loaded so that we must interrupt
9549 redisplay, adjust glyph matrices, and try again. */
9550
cb617e7c
GM
9551enum
9552{
9553 SCROLLING_SUCCESS,
9554 SCROLLING_FAILED,
9555 SCROLLING_NEED_LARGER_MATRICES
9556};
9557
5f5c8ee5
GM
9558static int
9559try_scrolling (window, just_this_one_p, scroll_conservatively,
9560 scroll_step, temp_scroll_step)
9561 Lisp_Object window;
9562 int just_this_one_p;
31ade731 9563 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5
GM
9564 int temp_scroll_step;
9565{
9566 struct window *w = XWINDOW (window);
9567 struct frame *f = XFRAME (w->frame);
9568 struct text_pos scroll_margin_pos;
9569 struct text_pos pos;
9570 struct text_pos startp;
9571 struct it it;
9572 Lisp_Object window_end;
9573 int this_scroll_margin;
9574 int dy = 0;
9575 int scroll_max;
b8a63ccb 9576 int rc;
5f5c8ee5
GM
9577 int amount_to_scroll = 0;
9578 Lisp_Object aggressive;
9579 int height;
9580
9581#if GLYPH_DEBUG
9582 debug_method_add (w, "try_scrolling");
78614721 9583#endif
5f5c8ee5
GM
9584
9585 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9586
9587 /* Compute scroll margin height in pixels. We scroll when point is
9588 within this distance from the top or bottom of the window. */
9589 if (scroll_margin > 0)
90adcf20 9590 {
5f5c8ee5
GM
9591 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9592 this_scroll_margin *= CANON_Y_UNIT (f);
9593 }
9594 else
9595 this_scroll_margin = 0;
9596
9597 /* Compute how much we should try to scroll maximally to bring point
9598 into view. */
0894e696
SM
9599 if (scroll_step || scroll_conservatively || temp_scroll_step)
9600 scroll_max = max (scroll_step,
9601 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
9602 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9603 || NUMBERP (current_buffer->scroll_up_aggressively))
9604 /* We're trying to scroll because of aggressive scrolling
9605 but no scroll_step is set. Choose an arbitrary one. Maybe
9606 there should be a variable for this. */
9607 scroll_max = 10;
9608 else
9609 scroll_max = 0;
9610 scroll_max *= CANON_Y_UNIT (f);
9611
9612 /* Decide whether we have to scroll down. Start at the window end
9613 and move this_scroll_margin up to find the position of the scroll
9614 margin. */
9615 window_end = Fwindow_end (window, Qt);
9616 CHARPOS (scroll_margin_pos) = XINT (window_end);
9617 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9618 if (this_scroll_margin)
9619 {
9620 start_display (&it, w, scroll_margin_pos);
9621 move_it_vertically (&it, - this_scroll_margin);
9622 scroll_margin_pos = it.current.pos;
9623 }
9624
9625 if (PT >= CHARPOS (scroll_margin_pos))
9626 {
9627 int y0;
9628
9629 /* Point is in the scroll margin at the bottom of the window, or
9630 below. Compute a new window start that makes point visible. */
47589c8c 9631
5f5c8ee5
GM
9632 /* Compute the distance from the scroll margin to PT.
9633 Give up if the distance is greater than scroll_max. */
9634 start_display (&it, w, scroll_margin_pos);
9635 y0 = it.current_y;
9636 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9637 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
9638
9639 /* To make point visible, we have to move the window start
9640 down so that the line the cursor is in is visible, which
9641 means we have to add in the height of the cursor line. */
9642 dy = line_bottom_y (&it) - y0;
47589c8c 9643
5f5c8ee5 9644 if (dy > scroll_max)
cb617e7c 9645 return SCROLLING_FAILED;
5f5c8ee5
GM
9646
9647 /* Move the window start down. If scrolling conservatively,
9648 move it just enough down to make point visible. If
9649 scroll_step is set, move it down by scroll_step. */
9650 start_display (&it, w, startp);
9651
9652 if (scroll_conservatively)
e89aaabd
GM
9653 amount_to_scroll
9654 = max (max (dy, CANON_Y_UNIT (f)),
9655 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9656 else if (scroll_step || temp_scroll_step)
9657 amount_to_scroll = scroll_max;
9658 else
90adcf20 9659 {
46226c1d 9660 aggressive = current_buffer->scroll_up_aggressively;
5f5c8ee5 9661 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9662 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9663 if (NUMBERP (aggressive))
9664 amount_to_scroll = XFLOATINT (aggressive) * height;
9665 }
a2725ab2 9666
5f5c8ee5 9667 if (amount_to_scroll <= 0)
cb617e7c 9668 return SCROLLING_FAILED;
a2725ab2 9669
5f5c8ee5
GM
9670 move_it_vertically (&it, amount_to_scroll);
9671 startp = it.current.pos;
9672 }
9673 else
9674 {
9675 /* See if point is inside the scroll margin at the top of the
9676 window. */
9677 scroll_margin_pos = startp;
9678 if (this_scroll_margin)
9679 {
9680 start_display (&it, w, startp);
9681 move_it_vertically (&it, this_scroll_margin);
9682 scroll_margin_pos = it.current.pos;
9683 }
9684
9685 if (PT < CHARPOS (scroll_margin_pos))
9686 {
9687 /* Point is in the scroll margin at the top of the window or
9688 above what is displayed in the window. */
9689 int y0;
9690
9691 /* Compute the vertical distance from PT to the scroll
9692 margin position. Give up if distance is greater than
9693 scroll_max. */
9694 SET_TEXT_POS (pos, PT, PT_BYTE);
9695 start_display (&it, w, pos);
9696 y0 = it.current_y;
9697 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9698 it.last_visible_y, -1,
9699 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9700 dy = it.current_y - y0;
9701 if (dy > scroll_max)
cb617e7c 9702 return SCROLLING_FAILED;
5f5c8ee5
GM
9703
9704 /* Compute new window start. */
9705 start_display (&it, w, startp);
9706
9707 if (scroll_conservatively)
0894e696
SM
9708 amount_to_scroll =
9709 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9710 else if (scroll_step || temp_scroll_step)
9711 amount_to_scroll = scroll_max;
538f13d4 9712 else
5f5c8ee5 9713 {
46226c1d 9714 aggressive = current_buffer->scroll_down_aggressively;
5f5c8ee5 9715 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9716 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9717 if (NUMBERP (aggressive))
9718 amount_to_scroll = XFLOATINT (aggressive) * height;
9719 }
a2725ab2 9720
5f5c8ee5 9721 if (amount_to_scroll <= 0)
cb617e7c 9722 return SCROLLING_FAILED;
5f5c8ee5
GM
9723
9724 move_it_vertically (&it, - amount_to_scroll);
9725 startp = it.current.pos;
90adcf20
RS
9726 }
9727 }
a2889657 9728
5f5c8ee5
GM
9729 /* Run window scroll functions. */
9730 startp = run_window_scroll_functions (window, startp);
90adcf20 9731
5f5c8ee5
GM
9732 /* Display the window. Give up if new fonts are loaded, or if point
9733 doesn't appear. */
9734 if (!try_window (window, startp))
cb617e7c 9735 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
9736 else if (w->cursor.vpos < 0)
9737 {
9738 clear_glyph_matrix (w->desired_matrix);
cb617e7c 9739 rc = SCROLLING_FAILED;
5f5c8ee5
GM
9740 }
9741 else
9742 {
9743 /* Maybe forget recorded base line for line number display. */
9744 if (!just_this_one_p
9745 || current_buffer->clip_changed
9142dd5b 9746 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
9747 w->base_line_number = Qnil;
9748
9749 /* If cursor ends up on a partially visible line, shift display
cb617e7c
GM
9750 lines up or down. If that fails because we need larger
9751 matrices, give up. */
9752 if (!make_cursor_line_fully_visible (w))
9753 rc = SCROLLING_NEED_LARGER_MATRICES;
9754 else
9755 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
9756 }
9757
9758 return rc;
a2889657
JB
9759}
9760
5f5c8ee5
GM
9761
9762/* Compute a suitable window start for window W if display of W starts
9763 on a continuation line. Value is non-zero if a new window start
9764 was computed.
9765
9766 The new window start will be computed, based on W's width, starting
9767 from the start of the continued line. It is the start of the
9768 screen line with the minimum distance from the old start W->start. */
9769
9770static int
9771compute_window_start_on_continuation_line (w)
9772 struct window *w;
1f1ff51d 9773{
5f5c8ee5
GM
9774 struct text_pos pos, start_pos;
9775 int window_start_changed_p = 0;
1f1ff51d 9776
5f5c8ee5 9777 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 9778
5f5c8ee5
GM
9779 /* If window start is on a continuation line... Window start may be
9780 < BEGV in case there's invisible text at the start of the
9781 buffer (M-x rmail, for example). */
9782 if (CHARPOS (start_pos) > BEGV
9783 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 9784 {
5f5c8ee5
GM
9785 struct it it;
9786 struct glyph_row *row;
f3751a65
GM
9787
9788 /* Handle the case that the window start is out of range. */
9789 if (CHARPOS (start_pos) < BEGV)
9790 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9791 else if (CHARPOS (start_pos) > ZV)
9792 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
9793
9794 /* Find the start of the continued line. This should be fast
9795 because scan_buffer is fast (newline cache). */
045dee35 9796 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
9797 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9798 row, DEFAULT_FACE_ID);
9799 reseat_at_previous_visible_line_start (&it);
9800
9801 /* If the line start is "too far" away from the window start,
9802 say it takes too much time to compute a new window start. */
9803 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9804 < XFASTINT (w->height) * XFASTINT (w->width))
9805 {
9806 int min_distance, distance;
9807
9808 /* Move forward by display lines to find the new window
9809 start. If window width was enlarged, the new start can
9810 be expected to be > the old start. If window width was
9811 decreased, the new window start will be < the old start.
9812 So, we're looking for the display line start with the
9813 minimum distance from the old window start. */
9814 pos = it.current.pos;
9815 min_distance = INFINITY;
9816 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9817 distance < min_distance)
9818 {
9819 min_distance = distance;
9820 pos = it.current.pos;
9821 move_it_by_lines (&it, 1, 0);
9822 }
9823
9824 /* Set the window start there. */
9825 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9826 window_start_changed_p = 1;
9827 }
1f1ff51d 9828 }
5f5c8ee5
GM
9829
9830 return window_start_changed_p;
1f1ff51d
KH
9831}
9832
5f5c8ee5 9833
1dd5768b 9834/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
9835 with window start STARTP. Value is
9836
cb617e7c 9837 CURSOR_MOVEMENT_SUCCESS if successful
47589c8c 9838
cb617e7c
GM
9839 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9840
9841 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9842 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9843 we want to scroll as if scroll-step were set to 1. See the code.
9844
9845 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9846 which case we have to abort this redisplay, and adjust matrices
9847 first. */
9848
9849enum
9850{
9851 CURSOR_MOVEMENT_SUCCESS,
9852 CURSOR_MOVEMENT_CANNOT_BE_USED,
9853 CURSOR_MOVEMENT_MUST_SCROLL,
9854 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9855};
47589c8c
GM
9856
9857static int
9858try_cursor_movement (window, startp, scroll_step)
9859 Lisp_Object window;
9860 struct text_pos startp;
9861 int *scroll_step;
9862{
9863 struct window *w = XWINDOW (window);
9864 struct frame *f = XFRAME (w->frame);
cb617e7c 9865 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
47589c8c 9866
69d1f7c9 9867#if GLYPH_DEBUG
76cb5e06
GM
9868 if (inhibit_try_cursor_movement)
9869 return rc;
9870#endif
9871
47589c8c
GM
9872 /* Handle case where text has not changed, only point, and it has
9873 not moved off the frame. */
9874 if (/* Point may be in this window. */
9875 PT >= CHARPOS (startp)
47589c8c
GM
9876 /* Selective display hasn't changed. */
9877 && !current_buffer->clip_changed
4db87380
GM
9878 /* Function force-mode-line-update is used to force a thorough
9879 redisplay. It sets either windows_or_buffers_changed or
9880 update_mode_lines. So don't take a shortcut here for these
9881 cases. */
9882 && !update_mode_lines
9883 && !windows_or_buffers_changed
5fb96e96 9884 && !cursor_type_changed
47589c8c
GM
9885 /* Can't use this case if highlighting a region. When a
9886 region exists, cursor movement has to do more than just
9887 set the cursor. */
9888 && !(!NILP (Vtransient_mark_mode)
9889 && !NILP (current_buffer->mark_active))
9890 && NILP (w->region_showing)
9891 && NILP (Vshow_trailing_whitespace)
9892 /* Right after splitting windows, last_point may be nil. */
9893 && INTEGERP (w->last_point)
9894 /* This code is not used for mini-buffer for the sake of the case
9895 of redisplaying to replace an echo area message; since in
9896 that case the mini-buffer contents per se are usually
9897 unchanged. This code is of no real use in the mini-buffer
9898 since the handling of this_line_start_pos, etc., in redisplay
9899 handles the same cases. */
9900 && !EQ (window, minibuf_window)
9901 /* When splitting windows or for new windows, it happens that
9902 redisplay is called with a nil window_end_vpos or one being
9903 larger than the window. This should really be fixed in
9904 window.c. I don't have this on my list, now, so we do
9905 approximately the same as the old redisplay code. --gerd. */
9906 && INTEGERP (w->window_end_vpos)
9907 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9908 && (FRAME_WINDOW_P (f)
9909 || !MARKERP (Voverlay_arrow_position)
9910 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9911 {
9912 int this_scroll_margin;
8ee5b6a3 9913 struct glyph_row *row = NULL;
47589c8c
GM
9914
9915#if GLYPH_DEBUG
9916 debug_method_add (w, "cursor movement");
9917#endif
9918
9919 /* Scroll if point within this distance from the top or bottom
9920 of the window. This is a pixel value. */
9921 this_scroll_margin = max (0, scroll_margin);
9922 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9923 this_scroll_margin *= CANON_Y_UNIT (f);
9924
9925 /* Start with the row the cursor was displayed during the last
9926 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
9927 if (w->last_cursor.vpos < 0
9928 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 9929 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
9930 else
9931 {
9932 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9933 if (row->mode_line_p)
9934 ++row;
9935 if (!row->enabled_p)
cb617e7c 9936 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
9937 }
9938
cb617e7c 9939 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
9940 {
9941 int scroll_p = 0;
68c5d1db
GM
9942 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9943
47589c8c
GM
9944 if (PT > XFASTINT (w->last_point))
9945 {
9946 /* Point has moved forward. */
47589c8c
GM
9947 while (MATRIX_ROW_END_CHARPOS (row) < PT
9948 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9949 {
9950 xassert (row->enabled_p);
9951 ++row;
9952 }
9953
9954 /* The end position of a row equals the start position
9955 of the next row. If PT is there, we would rather
cafafe0b
GM
9956 display it in the next line. */
9957 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9958 && MATRIX_ROW_END_CHARPOS (row) == PT
9959 && !cursor_row_p (w, row))
9960 ++row;
47589c8c
GM
9961
9962 /* If within the scroll margin, scroll. Note that
9963 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9964 the next line would be drawn, and that
9965 this_scroll_margin can be zero. */
9966 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9967 || PT > MATRIX_ROW_END_CHARPOS (row)
9968 /* Line is completely visible last line in window
9969 and PT is to be set in the next line. */
9970 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9971 && PT == MATRIX_ROW_END_CHARPOS (row)
9972 && !row->ends_at_zv_p
9973 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9974 scroll_p = 1;
9975 }
9976 else if (PT < XFASTINT (w->last_point))
9977 {
9978 /* Cursor has to be moved backward. Note that PT >=
9979 CHARPOS (startp) because of the outer
9980 if-statement. */
9981 while (!row->mode_line_p
9982 && (MATRIX_ROW_START_CHARPOS (row) > PT
9983 || (MATRIX_ROW_START_CHARPOS (row) == PT
9984 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9985 && (row->y > this_scroll_margin
9986 || CHARPOS (startp) == BEGV))
9987 {
9988 xassert (row->enabled_p);
9989 --row;
9990 }
9991
9992 /* Consider the following case: Window starts at BEGV,
9993 there is invisible, intangible text at BEGV, so that
9994 display starts at some point START > BEGV. It can
9995 happen that we are called with PT somewhere between
9996 BEGV and START. Try to handle that case. */
9997 if (row < w->current_matrix->rows
9998 || row->mode_line_p)
9999 {
10000 row = w->current_matrix->rows;
10001 if (row->mode_line_p)
10002 ++row;
10003 }
10004
10005 /* Due to newlines in overlay strings, we may have to
10006 skip forward over overlay strings. */
68c5d1db
GM
10007 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10008 && MATRIX_ROW_END_CHARPOS (row) == PT
10009 && !cursor_row_p (w, row))
47589c8c
GM
10010 ++row;
10011
10012 /* If within the scroll margin, scroll. */
10013 if (row->y < this_scroll_margin
10014 && CHARPOS (startp) != BEGV)
10015 scroll_p = 1;
10016 }
10017
10018 if (PT < MATRIX_ROW_START_CHARPOS (row)
10019 || PT > MATRIX_ROW_END_CHARPOS (row))
10020 {
10021 /* if PT is not in the glyph row, give up. */
cb617e7c 10022 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 10023 }
440fc135 10024 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 10025 {
8de4aaf8
GM
10026 if (PT == MATRIX_ROW_END_CHARPOS (row)
10027 && !row->ends_at_zv_p
10028 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 10029 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 10030 else if (row->height > window_box_height (w))
440fc135 10031 {
8de4aaf8
GM
10032 /* If we end up in a partially visible line, let's
10033 make it fully visible, except when it's taller
10034 than the window, in which case we can't do much
10035 about it. */
440fc135 10036 *scroll_step = 1;
cb617e7c 10037 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
10038 }
10039 else
10040 {
10041 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10042 try_window (window, startp);
cb617e7c
GM
10043 if (!make_cursor_line_fully_visible (w))
10044 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10045 else
10046 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 10047 }
47589c8c
GM
10048 }
10049 else if (scroll_p)
cb617e7c 10050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
10051 else
10052 {
10053 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 10054 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
10055 }
10056 }
10057 }
10058
10059 return rc;
10060}
10061
10062
5f5c8ee5
GM
10063/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10064 selected_window is redisplayed. */
90adcf20 10065
a2889657 10066static void
5f5c8ee5 10067redisplay_window (window, just_this_one_p)
a2889657 10068 Lisp_Object window;
5f5c8ee5 10069 int just_this_one_p;
a2889657 10070{
5f5c8ee5
GM
10071 struct window *w = XWINDOW (window);
10072 struct frame *f = XFRAME (w->frame);
10073 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 10074 struct buffer *old = current_buffer;
5f5c8ee5 10075 struct text_pos lpoint, opoint, startp;
e481f960 10076 int update_mode_line;
5f5c8ee5
GM
10077 int tem;
10078 struct it it;
10079 /* Record it now because it's overwritten. */
10080 int current_matrix_up_to_date_p = 0;
5f5c8ee5 10081 int temp_scroll_step = 0;
2d27dae2 10082 int count = BINDING_STACK_SIZE ();
47589c8c 10083 int rc;
a2889657 10084
5f5c8ee5
GM
10085 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10086 opoint = lpoint;
a2889657 10087
5f5c8ee5
GM
10088 /* W must be a leaf window here. */
10089 xassert (!NILP (w->buffer));
10090#if GLYPH_DEBUG
10091 *w->desired_matrix->method = 0;
10092#endif
2e54982e
RS
10093
10094 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
10095
10096 reconsider_clip_changes (w, buffer);
10097
5f5c8ee5
GM
10098 /* Has the mode line to be updated? */
10099 update_mode_line = (!NILP (w->update_mode_line)
10100 || update_mode_lines
10101 || buffer->clip_changed);
8de2d90b
JB
10102
10103 if (MINI_WINDOW_P (w))
10104 {
5f5c8ee5 10105 if (w == XWINDOW (echo_area_window)
c6e89d6c 10106 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
10107 {
10108 if (update_mode_line)
10109 /* We may have to update a tty frame's menu bar or a
e037b9ec 10110 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
10111 goto finish_menu_bars;
10112 else
10113 /* We've already displayed the echo area glyphs in this window. */
10114 goto finish_scroll_bars;
10115 }
73af359d 10116 else if (w != XWINDOW (minibuf_window))
8de2d90b 10117 {
5f5c8ee5
GM
10118 /* W is a mini-buffer window, but it's not the currently
10119 active one, so clear it. */
10120 int yb = window_text_bottom_y (w);
10121 struct glyph_row *row;
10122 int y;
10123
10124 for (y = 0, row = w->desired_matrix->rows;
10125 y < yb;
10126 y += row->height, ++row)
10127 blank_row (w, row, y);
88f22aff 10128 goto finish_scroll_bars;
8de2d90b 10129 }
c095a1dd
GM
10130
10131 clear_glyph_matrix (w->desired_matrix);
8de2d90b 10132 }
a2889657 10133
5f5c8ee5
GM
10134 /* Otherwise set up data on this window; select its buffer and point
10135 value. */
6a93695f
GM
10136 /* Really select the buffer, for the sake of buffer-local
10137 variables. */
10138 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
10139 SET_TEXT_POS (opoint, PT, PT_BYTE);
10140
10141 current_matrix_up_to_date_p
10142 = (!NILP (w->window_end_valid)
10143 && !current_buffer->clip_changed
10144 && XFASTINT (w->last_modified) >= MODIFF
10145 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 10146
5f5c8ee5
GM
10147 /* When windows_or_buffers_changed is non-zero, we can't rely on
10148 the window end being valid, so set it to nil there. */
10149 if (windows_or_buffers_changed)
10150 {
10151 /* If window starts on a continuation line, maybe adjust the
10152 window start in case the window's width changed. */
10153 if (XMARKER (w->start)->buffer == current_buffer)
10154 compute_window_start_on_continuation_line (w);
10155
10156 w->window_end_valid = Qnil;
10157 }
12adba34 10158
5f5c8ee5
GM
10159 /* Some sanity checks. */
10160 CHECK_WINDOW_END (w);
10161 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 10162 abort ();
5f5c8ee5 10163 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 10164 abort ();
a2889657 10165
28995e67
RS
10166 /* If %c is in mode line, update it if needed. */
10167 if (!NILP (w->column_number_displayed)
10168 /* This alternative quickly identifies a common case
10169 where no change is needed. */
10170 && !(PT == XFASTINT (w->last_point)
8850a573
RS
10171 && XFASTINT (w->last_modified) >= MODIFF
10172 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
10173 && XFASTINT (w->column_number_displayed) != current_column ())
10174 update_mode_line = 1;
10175
5f5c8ee5
GM
10176 /* Count number of windows showing the selected buffer. An indirect
10177 buffer counts as its base buffer. */
10178 if (!just_this_one_p)
42640f83
RS
10179 {
10180 struct buffer *current_base, *window_base;
10181 current_base = current_buffer;
10182 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10183 if (current_base->base_buffer)
10184 current_base = current_base->base_buffer;
10185 if (window_base->base_buffer)
10186 window_base = window_base->base_buffer;
10187 if (current_base == window_base)
10188 buffer_shared++;
10189 }
a2889657 10190
5f5c8ee5
GM
10191 /* Point refers normally to the selected window. For any other
10192 window, set up appropriate value. */
a2889657
JB
10193 if (!EQ (window, selected_window))
10194 {
12adba34
RS
10195 int new_pt = XMARKER (w->pointm)->charpos;
10196 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 10197 if (new_pt < BEGV)
a2889657 10198 {
f67a0f51 10199 new_pt = BEGV;
12adba34
RS
10200 new_pt_byte = BEGV_BYTE;
10201 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 10202 }
f67a0f51 10203 else if (new_pt > (ZV - 1))
a2889657 10204 {
f67a0f51 10205 new_pt = ZV;
12adba34
RS
10206 new_pt_byte = ZV_BYTE;
10207 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 10208 }
5f5c8ee5 10209
f67a0f51 10210 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 10211 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
10212 }
10213
f4faa47c 10214 /* If any of the character widths specified in the display table
5f5c8ee5
GM
10215 have changed, invalidate the width run cache. It's true that
10216 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
10217 redisplay goes (non-fatally) haywire when the display table is
10218 changed, so why should we worry about doing any better? */
10219 if (current_buffer->width_run_cache)
10220 {
f908610f 10221 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
10222
10223 if (! disptab_matches_widthtab (disptab,
10224 XVECTOR (current_buffer->width_table)))
10225 {
10226 invalidate_region_cache (current_buffer,
10227 current_buffer->width_run_cache,
10228 BEG, Z);
10229 recompute_width_table (current_buffer, disptab);
10230 }
10231 }
10232
a2889657 10233 /* If window-start is screwed up, choose a new one. */
a2889657
JB
10234 if (XMARKER (w->start)->buffer != current_buffer)
10235 goto recenter;
10236
5f5c8ee5 10237 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 10238
cf0df6ab
RS
10239 /* If someone specified a new starting point but did not insist,
10240 check whether it can be used. */
cfad01b4
GM
10241 if (!NILP (w->optional_new_start)
10242 && CHARPOS (startp) >= BEGV
10243 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
10244 {
10245 w->optional_new_start = Qnil;
5f5c8ee5
GM
10246 start_display (&it, w, startp);
10247 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10248 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10249 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
10250 w->force_start = Qt;
10251 }
10252
8de2d90b 10253 /* Handle case where place to start displaying has been specified,
aa6d10fa 10254 unless the specified location is outside the accessible range. */
9472f927
GM
10255 if (!NILP (w->force_start)
10256 || w->frozen_window_start_p)
a2889657 10257 {
e63574d7 10258 w->force_start = Qnil;
5f5c8ee5 10259 w->vscroll = 0;
b5174a51 10260 w->window_end_valid = Qnil;
5f5c8ee5
GM
10261
10262 /* Forget any recorded base line for line number display. */
10263 if (!current_matrix_up_to_date_p
10264 || current_buffer->clip_changed)
10265 w->base_line_number = Qnil;
10266
75c43375
RS
10267 /* Redisplay the mode line. Select the buffer properly for that.
10268 Also, run the hook window-scroll-functions
10269 because we have scrolled. */
e63574d7
RS
10270 /* Note, we do this after clearing force_start because
10271 if there's an error, it is better to forget about force_start
10272 than to get into an infinite loop calling the hook functions
10273 and having them get more errors. */
75c43375
RS
10274 if (!update_mode_line
10275 || ! NILP (Vwindow_scroll_functions))
e481f960 10276 {
e481f960
RS
10277 update_mode_line = 1;
10278 w->update_mode_line = Qt;
5f5c8ee5 10279 startp = run_window_scroll_functions (window, startp);
e481f960 10280 }
5f5c8ee5 10281
ac90c44f
GM
10282 w->last_modified = make_number (0);
10283 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
10284 if (CHARPOS (startp) < BEGV)
10285 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10286 else if (CHARPOS (startp) > ZV)
10287 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10288
10289 /* Redisplay, then check if cursor has been set during the
10290 redisplay. Give up if new fonts were loaded. */
10291 if (!try_window (window, startp))
10292 {
10293 w->force_start = Qt;
10294 clear_glyph_matrix (w->desired_matrix);
504454e8 10295 goto finish_scroll_bars;
5f5c8ee5
GM
10296 }
10297
9472f927 10298 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 10299 {
b28cb6ed
GM
10300 /* If point does not appear, try to move point so it does
10301 appear. The desired matrix has been built above, so we
10302 can use it here. */
10303 int window_height;
10304 struct glyph_row *row;
10305
10306 window_height = window_box_height (w) / 2;
10307 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10308 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
5f5c8ee5
GM
10309 ++row;
10310
10311 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10312 MATRIX_ROW_START_BYTEPOS (row));
10313
90adcf20 10314 if (w != XWINDOW (selected_window))
12adba34 10315 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
10316 else if (current_buffer == old)
10317 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10318
10319 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10320
10321 /* If we are highlighting the region, then we just changed
10322 the region, so redisplay to show it. */
df0b5ea1
RS
10323 if (!NILP (Vtransient_mark_mode)
10324 && !NILP (current_buffer->mark_active))
6f27fa9b 10325 {
5f5c8ee5
GM
10326 clear_glyph_matrix (w->desired_matrix);
10327 if (!try_window (window, startp))
cb617e7c 10328 goto need_larger_matrices;
6f27fa9b 10329 }
a2889657 10330 }
5f5c8ee5 10331
cb617e7c
GM
10332 if (!make_cursor_line_fully_visible (w))
10333 goto need_larger_matrices;
5f5c8ee5
GM
10334#if GLYPH_DEBUG
10335 debug_method_add (w, "forced window start");
10336#endif
a2889657
JB
10337 goto done;
10338 }
10339
5f5c8ee5
GM
10340 /* Handle case where text has not changed, only point, and it has
10341 not moved off the frame. */
10342 if (current_matrix_up_to_date_p
47589c8c 10343 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 10344 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 10345 {
cb617e7c
GM
10346 switch (rc)
10347 {
10348 case CURSOR_MOVEMENT_SUCCESS:
10349 goto done;
10350
10351 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10352 goto need_larger_matrices;
10353
10354 case CURSOR_MOVEMENT_MUST_SCROLL:
10355 goto try_to_scroll;
10356
10357 default:
10358 abort ();
10359 }
a2889657
JB
10360 }
10361 /* If current starting point was originally the beginning of a line
10362 but no longer is, find a new starting point. */
265a9e55 10363 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
10364 && !(CHARPOS (startp) <= BEGV
10365 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 10366 {
5f5c8ee5
GM
10367#if GLYPH_DEBUG
10368 debug_method_add (w, "recenter 1");
10369#endif
a2889657
JB
10370 goto recenter;
10371 }
5f5c8ee5 10372
27d16f05
GM
10373 /* Try scrolling with try_window_id. Value is > 0 if update has
10374 been done, it is -1 if we know that the same window start will
10375 not work. It is 0 if unsuccessful for some other reason. */
10376 else if ((tem = try_window_id (w)) != 0)
a2889657 10377 {
5f5c8ee5 10378#if GLYPH_DEBUG
ef121659 10379 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
10380#endif
10381
10382 if (fonts_changed_p)
cb617e7c 10383 goto need_larger_matrices;
a2889657
JB
10384 if (tem > 0)
10385 goto done;
ef121659 10386
5f5c8ee5
GM
10387 /* Otherwise try_window_id has returned -1 which means that we
10388 don't want the alternative below this comment to execute. */
a2889657 10389 }
5f5c8ee5
GM
10390 else if (CHARPOS (startp) >= BEGV
10391 && CHARPOS (startp) <= ZV
10392 && PT >= CHARPOS (startp)
10393 && (CHARPOS (startp) < ZV
e9874cee 10394 /* Avoid starting at end of buffer. */
5f5c8ee5 10395 || CHARPOS (startp) == BEGV
8850a573
RS
10396 || (XFASTINT (w->last_modified) >= MODIFF
10397 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 10398 {
5f5c8ee5
GM
10399#if GLYPH_DEBUG
10400 debug_method_add (w, "same window start");
10401#endif
10402
10403 /* Try to redisplay starting at same place as before.
10404 If point has not moved off frame, accept the results. */
10405 if (!current_matrix_up_to_date_p
10406 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
10407 because a window scroll function can have changed the
10408 buffer. */
5f5c8ee5
GM
10409 || !NILP (Vwindow_scroll_functions)
10410 || MINI_WINDOW_P (w)
10411 || !try_window_reusing_current_matrix (w))
10412 {
10413 IF_DEBUG (debug_method_add (w, "1"));
10414 try_window (window, startp);
10415 }
10416
10417 if (fonts_changed_p)
cb617e7c 10418 goto need_larger_matrices;
5f5c8ee5
GM
10419
10420 if (w->cursor.vpos >= 0)
aa6d10fa 10421 {
5f5c8ee5
GM
10422 if (!just_this_one_p
10423 || current_buffer->clip_changed
9142dd5b 10424 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
10425 /* Forget any recorded base line for line number display. */
10426 w->base_line_number = Qnil;
5f5c8ee5 10427
cb617e7c
GM
10428 if (!make_cursor_line_fully_visible (w))
10429 goto need_larger_matrices;
aa6d10fa
RS
10430 goto done;
10431 }
a2889657 10432 else
5f5c8ee5 10433 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
10434 }
10435
5f5c8ee5
GM
10436 try_to_scroll:
10437
ac90c44f
GM
10438 w->last_modified = make_number (0);
10439 w->last_overlay_modified = make_number (0);
5f5c8ee5 10440
e481f960
RS
10441 /* Redisplay the mode line. Select the buffer properly for that. */
10442 if (!update_mode_line)
10443 {
e481f960
RS
10444 update_mode_line = 1;
10445 w->update_mode_line = Qt;
10446 }
a2889657 10447
5f5c8ee5
GM
10448 /* Try to scroll by specified few lines. */
10449 if ((scroll_conservatively
10450 || scroll_step
10451 || temp_scroll_step
10452 || NUMBERP (current_buffer->scroll_up_aggressively)
10453 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 10454 && !current_buffer->clip_changed
5f5c8ee5
GM
10455 && CHARPOS (startp) >= BEGV
10456 && CHARPOS (startp) <= ZV)
0789adb2 10457 {
5f5c8ee5
GM
10458 /* The function returns -1 if new fonts were loaded, 1 if
10459 successful, 0 if not successful. */
10460 int rc = try_scrolling (window, just_this_one_p,
10461 scroll_conservatively,
10462 scroll_step,
10463 temp_scroll_step);
cb617e7c
GM
10464 switch (rc)
10465 {
10466 case SCROLLING_SUCCESS:
10467 goto done;
10468
10469 case SCROLLING_NEED_LARGER_MATRICES:
10470 goto need_larger_matrices;
10471
10472 case SCROLLING_FAILED:
10473 break;
10474
10475 default:
10476 abort ();
10477 }
5f5c8ee5 10478 }
f9c8af06 10479
5f5c8ee5 10480 /* Finally, just choose place to start which centers point */
5936754e 10481
5f5c8ee5 10482 recenter:
44173109 10483
5f5c8ee5
GM
10484#if GLYPH_DEBUG
10485 debug_method_add (w, "recenter");
10486#endif
0789adb2 10487
5f5c8ee5 10488 /* w->vscroll = 0; */
0789adb2 10489
5f5c8ee5
GM
10490 /* Forget any previously recorded base line for line number display. */
10491 if (!current_matrix_up_to_date_p
10492 || current_buffer->clip_changed)
10493 w->base_line_number = Qnil;
10494
10495 /* Move backward half the height of the window. */
10496 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10497 it.current_y = it.last_visible_y;
3a030013 10498 move_it_vertically_backward (&it, window_box_height (w) / 2);
5f5c8ee5
GM
10499 xassert (IT_CHARPOS (it) >= BEGV);
10500
10501 /* The function move_it_vertically_backward may move over more
10502 than the specified y-distance. If it->w is small, e.g. a
10503 mini-buffer window, we may end up in front of the window's
10504 display area. Start displaying at the start of the line
10505 containing PT in this case. */
10506 if (it.current_y <= 0)
10507 {
10508 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10509 move_it_vertically (&it, 0);
10510 xassert (IT_CHARPOS (it) <= PT);
10511 it.current_y = 0;
0789adb2
RS
10512 }
10513
5f5c8ee5
GM
10514 it.current_x = it.hpos = 0;
10515
10516 /* Set startp here explicitly in case that helps avoid an infinite loop
10517 in case the window-scroll-functions functions get errors. */
10518 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10519
10520 /* Run scroll hooks. */
10521 startp = run_window_scroll_functions (window, it.current.pos);
10522
10523 /* Redisplay the window. */
10524 if (!current_matrix_up_to_date_p
10525 || windows_or_buffers_changed
5fb96e96 10526 || cursor_type_changed
5f5c8ee5
GM
10527 /* Don't use try_window_reusing_current_matrix in this case
10528 because it can have changed the buffer. */
10529 || !NILP (Vwindow_scroll_functions)
10530 || !just_this_one_p
10531 || MINI_WINDOW_P (w)
10532 || !try_window_reusing_current_matrix (w))
10533 try_window (window, startp);
10534
10535 /* If new fonts have been loaded (due to fontsets), give up. We
10536 have to start a new redisplay since we need to re-adjust glyph
10537 matrices. */
10538 if (fonts_changed_p)
cb617e7c 10539 goto need_larger_matrices;
5f5c8ee5
GM
10540
10541 /* If cursor did not appear assume that the middle of the window is
10542 in the first line of the window. Do it again with the next line.
10543 (Imagine a window of height 100, displaying two lines of height
10544 60. Moving back 50 from it->last_visible_y will end in the first
10545 line.) */
10546 if (w->cursor.vpos < 0)
a2889657 10547 {
5f5c8ee5
GM
10548 if (!NILP (w->window_end_valid)
10549 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 10550 {
5f5c8ee5
GM
10551 clear_glyph_matrix (w->desired_matrix);
10552 move_it_by_lines (&it, 1, 0);
10553 try_window (window, it.current.pos);
a2889657 10554 }
5f5c8ee5 10555 else if (PT < IT_CHARPOS (it))
a2889657 10556 {
5f5c8ee5
GM
10557 clear_glyph_matrix (w->desired_matrix);
10558 move_it_by_lines (&it, -1, 0);
10559 try_window (window, it.current.pos);
10560 }
10561 else
10562 {
10563 /* Not much we can do about it. */
a2889657 10564 }
a2889657 10565 }
010494d0 10566
5f5c8ee5
GM
10567 /* Consider the following case: Window starts at BEGV, there is
10568 invisible, intangible text at BEGV, so that display starts at
10569 some point START > BEGV. It can happen that we are called with
10570 PT somewhere between BEGV and START. Try to handle that case. */
10571 if (w->cursor.vpos < 0)
835766b6 10572 {
5f5c8ee5
GM
10573 struct glyph_row *row = w->current_matrix->rows;
10574 if (row->mode_line_p)
10575 ++row;
10576 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 10577 }
5f5c8ee5 10578
cb617e7c
GM
10579 if (!make_cursor_line_fully_visible (w))
10580 goto need_larger_matrices;
b5174a51 10581
74d481ac
GM
10582 done:
10583
5f5c8ee5
GM
10584 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10585 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10586 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10587 ? Qt : Qnil);
a2889657 10588
5f5c8ee5 10589 /* Display the mode line, if we must. */
e481f960 10590 if ((update_mode_line
aa6d10fa 10591 /* If window not full width, must redo its mode line
5f5c8ee5
GM
10592 if (a) the window to its side is being redone and
10593 (b) we do a frame-based redisplay. This is a consequence
10594 of how inverted lines are drawn in frame-based redisplay. */
10595 || (!just_this_one_p
10596 && !FRAME_WINDOW_P (f)
10597 && !WINDOW_FULL_WIDTH_P (w))
10598 /* Line number to display. */
155ef550 10599 || INTEGERP (w->base_line_pos)
5f5c8ee5 10600 /* Column number is displayed and different from the one displayed. */
155ef550
KH
10601 || (!NILP (w->column_number_displayed)
10602 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
10603 /* This means that the window has a mode line. */
10604 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 10605 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 10606 {
5f5c8ee5
GM
10607 display_mode_lines (w);
10608
10609 /* If mode line height has changed, arrange for a thorough
10610 immediate redisplay using the correct mode line height. */
10611 if (WINDOW_WANTS_MODELINE_P (w)
10612 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 10613 {
5f5c8ee5
GM
10614 fonts_changed_p = 1;
10615 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10616 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 10617 }
5f5c8ee5
GM
10618
10619 /* If top line height has changed, arrange for a thorough
10620 immediate redisplay using the correct mode line height. */
045dee35
GM
10621 if (WINDOW_WANTS_HEADER_LINE_P (w)
10622 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
10623 {
10624 fonts_changed_p = 1;
045dee35
GM
10625 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10626 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10627 }
10628
10629 if (fonts_changed_p)
cb617e7c 10630 goto need_larger_matrices;
5ba50c51 10631 }
5f5c8ee5
GM
10632
10633 if (!line_number_displayed
10634 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
10635 {
10636 w->base_line_pos = Qnil;
10637 w->base_line_number = Qnil;
10638 }
a2889657 10639
5f5c8ee5
GM
10640 finish_menu_bars:
10641
7ce2c095 10642 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 10643 if (update_mode_line
5f5c8ee5
GM
10644 && EQ (FRAME_SELECTED_WINDOW (f), window))
10645 {
10646 int redisplay_menu_p = 0;
10647
10648 if (FRAME_WINDOW_P (f))
10649 {
e0f712ba 10650#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
5f5c8ee5 10651 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 10652#else
5f5c8ee5 10653 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 10654#endif
5f5c8ee5
GM
10655 }
10656 else
10657 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10658
10659 if (redisplay_menu_p)
10660 display_menu_bar (w);
10661
10662#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
10663 if (WINDOWP (f->tool_bar_window)
10664 && (FRAME_TOOL_BAR_LINES (f) > 0
10665 || auto_resize_tool_bars_p))
10666 redisplay_tool_bar (f);
5f5c8ee5
GM
10667#endif
10668 }
7ce2c095 10669
cb617e7c
GM
10670 need_larger_matrices:
10671 ;
88f22aff 10672 finish_scroll_bars:
5f5c8ee5 10673
88f22aff 10674 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 10675 {
b1d1124b 10676 int start, end, whole;
30c566e4 10677
b1d1124b 10678 /* Calculate the start and end positions for the current window.
3505ea70
JB
10679 At some point, it would be nice to choose between scrollbars
10680 which reflect the whole buffer size, with special markers
10681 indicating narrowing, and scrollbars which reflect only the
10682 visible region.
10683
5f5c8ee5 10684 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 10685 if (!MINI_WINDOW_P (w)
5f5c8ee5 10686 || (w == XWINDOW (minibuf_window)
c6e89d6c 10687 && NILP (echo_area_buffer[0])))
b1d1124b 10688 {
8a9311d7 10689 whole = ZV - BEGV;
4d641a15 10690 start = marker_position (w->start) - BEGV;
b1d1124b
JB
10691 /* I don't think this is guaranteed to be right. For the
10692 moment, we'll pretend it is. */
5f5c8ee5 10693 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 10694
5f5c8ee5
GM
10695 if (end < start)
10696 end = start;
10697 if (whole < (end - start))
10698 whole = end - start;
b1d1124b
JB
10699 }
10700 else
10701 start = end = whole = 0;
30c566e4 10702
88f22aff 10703 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 10704 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 10705
5f5c8ee5
GM
10706 /* Note that we actually used the scroll bar attached to this
10707 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 10708 redeem_scroll_bar_hook (w);
30c566e4 10709 }
b1d1124b 10710
5f5c8ee5
GM
10711 /* Restore current_buffer and value of point in it. */
10712 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 10713 set_buffer_internal_1 (old);
5f5c8ee5 10714 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
10715
10716 unbind_to (count, Qnil);
a2889657 10717}
a2889657 10718
5f5c8ee5
GM
10719
10720/* Build the complete desired matrix of WINDOW with a window start
10721 buffer position POS. Value is non-zero if successful. It is zero
10722 if fonts were loaded during redisplay which makes re-adjusting
10723 glyph matrices necessary. */
10724
10725int
a2889657
JB
10726try_window (window, pos)
10727 Lisp_Object window;
5f5c8ee5
GM
10728 struct text_pos pos;
10729{
10730 struct window *w = XWINDOW (window);
10731 struct it it;
10732 struct glyph_row *last_text_row = NULL;
9cbab4ff 10733
5f5c8ee5
GM
10734 /* Make POS the new window start. */
10735 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 10736
5f5c8ee5
GM
10737 /* Mark cursor position as unknown. No overlay arrow seen. */
10738 w->cursor.vpos = -1;
a2889657 10739 overlay_arrow_seen = 0;
642eefc6 10740
5f5c8ee5
GM
10741 /* Initialize iterator and info to start at POS. */
10742 start_display (&it, w, pos);
a2889657 10743
5f5c8ee5
GM
10744 /* Display all lines of W. */
10745 while (it.current_y < it.last_visible_y)
10746 {
10747 if (display_line (&it))
10748 last_text_row = it.glyph_row - 1;
10749 if (fonts_changed_p)
10750 return 0;
10751 }
a2889657 10752
5f5c8ee5
GM
10753 /* If bottom moved off end of frame, change mode line percentage. */
10754 if (XFASTINT (w->window_end_pos) <= 0
10755 && Z != IT_CHARPOS (it))
a2889657
JB
10756 w->update_mode_line = Qt;
10757
5f5c8ee5
GM
10758 /* Set window_end_pos to the offset of the last character displayed
10759 on the window from the end of current_buffer. Set
10760 window_end_vpos to its row number. */
10761 if (last_text_row)
10762 {
10763 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10764 w->window_end_bytepos
10765 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10766 w->window_end_pos
10767 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10768 w->window_end_vpos
10769 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10770 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10771 ->displays_text_p);
10772 }
10773 else
10774 {
10775 w->window_end_bytepos = 0;
ac90c44f 10776 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10777 }
10778
a2889657
JB
10779 /* But that is not valid info until redisplay finishes. */
10780 w->window_end_valid = Qnil;
5f5c8ee5 10781 return 1;
a2889657 10782}
5f5c8ee5
GM
10783
10784
a2889657 10785\f
5f5c8ee5
GM
10786/************************************************************************
10787 Window redisplay reusing current matrix when buffer has not changed
10788 ************************************************************************/
10789
10790/* Try redisplay of window W showing an unchanged buffer with a
10791 different window start than the last time it was displayed by
10792 reusing its current matrix. Value is non-zero if successful.
10793 W->start is the new window start. */
a2889657
JB
10794
10795static int
5f5c8ee5
GM
10796try_window_reusing_current_matrix (w)
10797 struct window *w;
a2889657 10798{
5f5c8ee5
GM
10799 struct frame *f = XFRAME (w->frame);
10800 struct glyph_row *row, *bottom_row;
10801 struct it it;
10802 struct run run;
10803 struct text_pos start, new_start;
10804 int nrows_scrolled, i;
10805 struct glyph_row *last_text_row;
10806 struct glyph_row *last_reused_text_row;
10807 struct glyph_row *start_row;
10808 int start_vpos, min_y, max_y;
75c5350a 10809
69d1f7c9 10810#if GLYPH_DEBUG
76cb5e06
GM
10811 if (inhibit_try_window_reusing)
10812 return 0;
10813#endif
10814
d18354b6
GM
10815 if (/* This function doesn't handle terminal frames. */
10816 !FRAME_WINDOW_P (f)
10817 /* Don't try to reuse the display if windows have been split
10818 or such. */
5fb96e96
RS
10819 || windows_or_buffers_changed
10820 || cursor_type_changed)
5f5c8ee5 10821 return 0;
a2889657 10822
5f5c8ee5
GM
10823 /* Can't do this if region may have changed. */
10824 if ((!NILP (Vtransient_mark_mode)
10825 && !NILP (current_buffer->mark_active))
8f897821
GM
10826 || !NILP (w->region_showing)
10827 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 10828 return 0;
a2889657 10829
5f5c8ee5 10830 /* If top-line visibility has changed, give up. */
045dee35
GM
10831 if (WINDOW_WANTS_HEADER_LINE_P (w)
10832 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
10833 return 0;
10834
10835 /* Give up if old or new display is scrolled vertically. We could
10836 make this function handle this, but right now it doesn't. */
10837 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10838 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10839 return 0;
10840
10841 /* The variable new_start now holds the new window start. The old
10842 start `start' can be determined from the current matrix. */
10843 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10844 start = start_row->start.pos;
10845 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 10846
5f5c8ee5
GM
10847 /* Clear the desired matrix for the display below. */
10848 clear_glyph_matrix (w->desired_matrix);
10849
10850 if (CHARPOS (new_start) <= CHARPOS (start))
10851 {
10852 int first_row_y;
10853
607ec83c
GM
10854 /* Don't use this method if the display starts with an ellipsis
10855 displayed for invisible text. It's not easy to handle that case
10856 below, and it's certainly not worth the effort since this is
10857 not a frequent case. */
10858 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10859 return 0;
10860
5f5c8ee5
GM
10861 IF_DEBUG (debug_method_add (w, "twu1"));
10862
10863 /* Display up to a row that can be reused. The variable
10864 last_text_row is set to the last row displayed that displays
b48f74cb
GM
10865 text. Note that it.vpos == 0 if or if not there is a
10866 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
10867 start_display (&it, w, new_start);
10868 first_row_y = it.current_y;
10869 w->cursor.vpos = -1;
10870 last_text_row = last_reused_text_row = NULL;
b48f74cb 10871
5f5c8ee5
GM
10872 while (it.current_y < it.last_visible_y
10873 && IT_CHARPOS (it) < CHARPOS (start)
10874 && !fonts_changed_p)
10875 if (display_line (&it))
10876 last_text_row = it.glyph_row - 1;
10877
10878 /* A value of current_y < last_visible_y means that we stopped
10879 at the previous window start, which in turn means that we
10880 have at least one reusable row. */
10881 if (it.current_y < it.last_visible_y)
a2889657 10882 {
b48f74cb 10883 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5
GM
10884 nrows_scrolled = it.vpos;
10885
10886 /* Find PT if not already found in the lines displayed. */
10887 if (w->cursor.vpos < 0)
a2889657 10888 {
5f5c8ee5
GM
10889 int dy = it.current_y - first_row_y;
10890
10891 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
10892 row = row_containing_pos (w, PT, row, NULL, dy);
10893 if (row)
10894 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10895 dy, nrows_scrolled);
10896 else
5f5c8ee5
GM
10897 {
10898 clear_glyph_matrix (w->desired_matrix);
10899 return 0;
10900 }
a2889657 10901 }
5f5c8ee5
GM
10902
10903 /* Scroll the display. Do it before the current matrix is
10904 changed. The problem here is that update has not yet
10905 run, i.e. part of the current matrix is not up to date.
10906 scroll_run_hook will clear the cursor, and use the
10907 current matrix to get the height of the row the cursor is
10908 in. */
10909 run.current_y = first_row_y;
10910 run.desired_y = it.current_y;
10911 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
10912
10913 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 10914 {
5f5c8ee5
GM
10915 update_begin (f);
10916 rif->update_window_begin_hook (w);
64d1e7d3 10917 rif->clear_mouse_face (w);
5f5c8ee5 10918 rif->scroll_run_hook (w, &run);
64d1e7d3 10919 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 10920 update_end (f);
a2889657 10921 }
5f5c8ee5
GM
10922
10923 /* Shift current matrix down by nrows_scrolled lines. */
10924 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10925 rotate_matrix (w->current_matrix,
10926 start_vpos,
10927 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10928 nrows_scrolled);
10929
9c8b8382 10930 /* Disable lines that must be updated. */
5f5c8ee5 10931 for (i = 0; i < it.vpos; ++i)
b48f74cb 10932 (start_row + i)->enabled_p = 0;
9c8b8382 10933
5f5c8ee5 10934 /* Re-compute Y positions. */
045dee35 10935 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10936 max_y = it.last_visible_y;
b48f74cb
GM
10937 for (row = start_row + nrows_scrolled;
10938 row < bottom_row;
10939 ++row)
d2f84654 10940 {
5f5c8ee5 10941 row->y = it.current_y;
75c5350a 10942 row->visible_height = row->height;
5f5c8ee5
GM
10943
10944 if (row->y < min_y)
75c5350a
GM
10945 row->visible_height -= min_y - row->y;
10946 if (row->y + row->height > max_y)
10947 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
10948
10949 it.current_y += row->height;
5f5c8ee5
GM
10950
10951 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10952 last_reused_text_row = row;
10953 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10954 break;
d2f84654 10955 }
9c8b8382
GM
10956
10957 /* Disable lines in the current matrix which are now
10958 below the window. */
bafb434c 10959 for (++row; row < bottom_row; ++row)
9c8b8382 10960 row->enabled_p = 0;
a2889657 10961 }
5f5c8ee5
GM
10962
10963 /* Update window_end_pos etc.; last_reused_text_row is the last
10964 reused row from the current matrix containing text, if any.
10965 The value of last_text_row is the last displayed line
10966 containing text. */
10967 if (last_reused_text_row)
a2889657 10968 {
5f5c8ee5
GM
10969 w->window_end_bytepos
10970 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
10971 w->window_end_pos
10972 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10973 w->window_end_vpos
10974 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10975 w->current_matrix));
a2889657 10976 }
5f5c8ee5
GM
10977 else if (last_text_row)
10978 {
10979 w->window_end_bytepos
10980 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10981 w->window_end_pos
10982 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10983 w->window_end_vpos
10984 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10985 }
10986 else
10987 {
10988 /* This window must be completely empty. */
10989 w->window_end_bytepos = 0;
ac90c44f 10990 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10991 }
10992 w->window_end_valid = Qnil;
a2889657 10993
5f5c8ee5
GM
10994 /* Update hint: don't try scrolling again in update_window. */
10995 w->desired_matrix->no_scrolling_p = 1;
10996
10997#if GLYPH_DEBUG
10998 debug_method_add (w, "try_window_reusing_current_matrix 1");
10999#endif
11000 return 1;
a2889657 11001 }
5f5c8ee5
GM
11002 else if (CHARPOS (new_start) > CHARPOS (start))
11003 {
11004 struct glyph_row *pt_row, *row;
11005 struct glyph_row *first_reusable_row;
11006 struct glyph_row *first_row_to_display;
11007 int dy;
11008 int yb = window_text_bottom_y (w);
11009
5f5c8ee5
GM
11010 /* Find the row starting at new_start, if there is one. Don't
11011 reuse a partially visible line at the end. */
b48f74cb 11012 first_reusable_row = start_row;
5f5c8ee5
GM
11013 while (first_reusable_row->enabled_p
11014 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11015 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11016 < CHARPOS (new_start)))
11017 ++first_reusable_row;
11018
11019 /* Give up if there is no row to reuse. */
11020 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
11021 || !first_reusable_row->enabled_p
11022 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11023 != CHARPOS (new_start)))
5f5c8ee5
GM
11024 return 0;
11025
5f5c8ee5
GM
11026 /* We can reuse fully visible rows beginning with
11027 first_reusable_row to the end of the window. Set
11028 first_row_to_display to the first row that cannot be reused.
11029 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 11030 pt_row = NULL;
ac90c44f
GM
11031 for (first_row_to_display = first_reusable_row;
11032 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11033 ++first_row_to_display)
5f5c8ee5 11034 {
4bde0ebb
GM
11035 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11036 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 11037 pt_row = first_row_to_display;
5f5c8ee5 11038 }
a2889657 11039
5f5c8ee5
GM
11040 /* Start displaying at the start of first_row_to_display. */
11041 xassert (first_row_to_display->y < yb);
11042 init_to_row_start (&it, w, first_row_to_display);
607ec83c 11043
b48f74cb
GM
11044 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11045 - start_vpos);
5f5c8ee5
GM
11046 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11047 - nrows_scrolled);
b48f74cb
GM
11048 it.current_y = (first_row_to_display->y - first_reusable_row->y
11049 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
11050
11051 /* Display lines beginning with first_row_to_display in the
11052 desired matrix. Set last_text_row to the last row displayed
11053 that displays text. */
11054 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11055 if (pt_row == NULL)
11056 w->cursor.vpos = -1;
11057 last_text_row = NULL;
11058 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11059 if (display_line (&it))
11060 last_text_row = it.glyph_row - 1;
11061
11062 /* Give up If point isn't in a row displayed or reused. */
11063 if (w->cursor.vpos < 0)
11064 {
11065 clear_glyph_matrix (w->desired_matrix);
11066 return 0;
11067 }
12adba34 11068
5f5c8ee5
GM
11069 /* If point is in a reused row, adjust y and vpos of the cursor
11070 position. */
11071 if (pt_row)
11072 {
11073 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11074 w->current_matrix);
11075 w->cursor.y -= first_reusable_row->y;
a2889657
JB
11076 }
11077
5f5c8ee5
GM
11078 /* Scroll the display. */
11079 run.current_y = first_reusable_row->y;
045dee35 11080 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11081 run.height = it.last_visible_y - run.current_y;
4da61803
GM
11082 dy = run.current_y - run.desired_y;
11083
5f5c8ee5
GM
11084 if (run.height)
11085 {
11086 struct frame *f = XFRAME (WINDOW_FRAME (w));
11087 update_begin (f);
11088 rif->update_window_begin_hook (w);
64d1e7d3 11089 rif->clear_mouse_face (w);
5f5c8ee5 11090 rif->scroll_run_hook (w, &run);
64d1e7d3 11091 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11092 update_end (f);
11093 }
a2889657 11094
5f5c8ee5
GM
11095 /* Adjust Y positions of reused rows. */
11096 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 11097 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 11098 max_y = it.last_visible_y;
b48f74cb 11099 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
11100 {
11101 row->y -= dy;
75c5350a 11102 row->visible_height = row->height;
5f5c8ee5 11103 if (row->y < min_y)
75c5350a
GM
11104 row->visible_height -= min_y - row->y;
11105 if (row->y + row->height > max_y)
11106 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 11107 }
a2889657 11108
5f5c8ee5
GM
11109 /* Scroll the current matrix. */
11110 xassert (nrows_scrolled > 0);
11111 rotate_matrix (w->current_matrix,
11112 start_vpos,
11113 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11114 -nrows_scrolled);
11115
ac90c44f
GM
11116 /* Disable rows not reused. */
11117 for (row -= nrows_scrolled; row < bottom_row; ++row)
11118 row->enabled_p = 0;
11119
5f5c8ee5
GM
11120 /* Adjust window end. A null value of last_text_row means that
11121 the window end is in reused rows which in turn means that
11122 only its vpos can have changed. */
11123 if (last_text_row)
11124 {
11125 w->window_end_bytepos
11126 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11127 w->window_end_pos
11128 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11129 w->window_end_vpos
11130 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
11131 }
11132 else
a2889657 11133 {
ac90c44f
GM
11134 w->window_end_vpos
11135 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 11136 }
5f5c8ee5
GM
11137
11138 w->window_end_valid = Qnil;
11139 w->desired_matrix->no_scrolling_p = 1;
11140
11141#if GLYPH_DEBUG
11142 debug_method_add (w, "try_window_reusing_current_matrix 2");
11143#endif
11144 return 1;
a2889657 11145 }
5f5c8ee5
GM
11146
11147 return 0;
11148}
a2889657 11149
a2889657 11150
5f5c8ee5
GM
11151\f
11152/************************************************************************
11153 Window redisplay reusing current matrix when buffer has changed
11154 ************************************************************************/
11155
1ec185cb
GM
11156static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11157static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
11158 int *, int *));
11159static struct glyph_row *
11160find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11161 struct glyph_row *));
11162
11163
11164/* Return the last row in MATRIX displaying text. If row START is
11165 non-null, start searching with that row. IT gives the dimensions
11166 of the display. Value is null if matrix is empty; otherwise it is
11167 a pointer to the row found. */
11168
11169static struct glyph_row *
11170find_last_row_displaying_text (matrix, it, start)
11171 struct glyph_matrix *matrix;
11172 struct it *it;
11173 struct glyph_row *start;
11174{
11175 struct glyph_row *row, *row_found;
11176
11177 /* Set row_found to the last row in IT->w's current matrix
11178 displaying text. The loop looks funny but think of partially
11179 visible lines. */
11180 row_found = NULL;
11181 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11182 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11183 {
11184 xassert (row->enabled_p);
11185 row_found = row;
11186 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11187 break;
11188 ++row;
a2889657 11189 }
5f5c8ee5
GM
11190
11191 return row_found;
11192}
11193
a2889657 11194
5f5c8ee5 11195/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
11196 by changes at the start of current_buffer that occurred since W's
11197 current matrix was built. Value is null if no such row exists.
a2889657 11198
d43fbe9d
GM
11199 BEG_UNCHANGED us the number of characters unchanged at the start of
11200 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11201 first changed character in current_buffer. Characters at positions <
11202 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11203 when the current matrix was built. */
5f5c8ee5
GM
11204
11205static struct glyph_row *
1ec185cb 11206find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
11207 struct window *w;
11208{
9142dd5b 11209 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
11210 struct glyph_row *row;
11211 struct glyph_row *row_found = NULL;
11212 int yb = window_text_bottom_y (w);
11213
11214 /* Find the last row displaying unchanged text. */
11215 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11216 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11217 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 11218 {
5f5c8ee5
GM
11219 if (/* If row ends before first_changed_pos, it is unchanged,
11220 except in some case. */
11221 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11222 /* When row ends in ZV and we write at ZV it is not
11223 unchanged. */
11224 && !row->ends_at_zv_p
11225 /* When first_changed_pos is the end of a continued line,
11226 row is not unchanged because it may be no longer
11227 continued. */
11228 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11229 && row->continued_p))
11230 row_found = row;
11231
11232 /* Stop if last visible row. */
11233 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11234 break;
11235
11236 ++row;
a2889657
JB
11237 }
11238
5f5c8ee5 11239 return row_found;
a2889657 11240}
5f5c8ee5
GM
11241
11242
11243/* Find the first glyph row in the current matrix of W that is not
d43fbe9d
GM
11244 affected by changes at the end of current_buffer since the
11245 time W's current matrix was built.
11246
11247 Return in *DELTA the number of chars by which buffer positions in
11248 unchanged text at the end of current_buffer must be adjusted.
11249
11250 Return in *DELTA_BYTES the corresponding number of bytes.
11251
11252 Value is null if no such row exists, i.e. all rows are affected by
11253 changes. */
5f5c8ee5
GM
11254
11255static struct glyph_row *
1ec185cb 11256find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
11257 struct window *w;
11258 int *delta, *delta_bytes;
a2889657 11259{
5f5c8ee5
GM
11260 struct glyph_row *row;
11261 struct glyph_row *row_found = NULL;
c581d710 11262
5f5c8ee5 11263 *delta = *delta_bytes = 0;
b2a76982 11264
1ec185cb
GM
11265 /* Display must not have been paused, otherwise the current matrix
11266 is not up to date. */
11267 if (NILP (w->window_end_valid))
11268 abort ();
11269
11270 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
11271 end is in the range of changed text. If so, there is no
11272 unchanged row at the end of W's current matrix. */
9142dd5b 11273 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
11274 return NULL;
11275
11276 /* Set row to the last row in W's current matrix displaying text. */
11277 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11278
5f5c8ee5
GM
11279 /* If matrix is entirely empty, no unchanged row exists. */
11280 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11281 {
11282 /* The value of row is the last glyph row in the matrix having a
11283 meaningful buffer position in it. The end position of row
11284 corresponds to window_end_pos. This allows us to translate
11285 buffer positions in the current matrix to current buffer
11286 positions for characters not in changed text. */
11287 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11288 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11289 int last_unchanged_pos, last_unchanged_pos_old;
11290 struct glyph_row *first_text_row
11291 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11292
11293 *delta = Z - Z_old;
11294 *delta_bytes = Z_BYTE - Z_BYTE_old;
11295
11296 /* Set last_unchanged_pos to the buffer position of the last
11297 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
11298 index + 1 of the last character in current_buffer, i.e. by
11299 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
11300 unchanged character, and we have to add BEG to get its buffer
11301 position. */
9142dd5b 11302 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
11303 last_unchanged_pos_old = last_unchanged_pos - *delta;
11304
11305 /* Search backward from ROW for a row displaying a line that
11306 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 11307 for (; row > first_text_row; --row)
5f5c8ee5 11308 {
1ec185cb
GM
11309 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11310 abort ();
5f5c8ee5
GM
11311
11312 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11313 row_found = row;
5f5c8ee5
GM
11314 }
11315 }
11316
1ec185cb
GM
11317 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11318 abort ();
11319
5f5c8ee5 11320 return row_found;
c581d710
RS
11321}
11322
c581d710 11323
5f5c8ee5
GM
11324/* Make sure that glyph rows in the current matrix of window W
11325 reference the same glyph memory as corresponding rows in the
11326 frame's frame matrix. This function is called after scrolling W's
11327 current matrix on a terminal frame in try_window_id and
11328 try_window_reusing_current_matrix. */
11329
11330static void
11331sync_frame_with_window_matrix_rows (w)
11332 struct window *w;
c581d710 11333{
5f5c8ee5
GM
11334 struct frame *f = XFRAME (w->frame);
11335 struct glyph_row *window_row, *window_row_end, *frame_row;
11336
11337 /* Preconditions: W must be a leaf window and full-width. Its frame
11338 must have a frame matrix. */
11339 xassert (NILP (w->hchild) && NILP (w->vchild));
11340 xassert (WINDOW_FULL_WIDTH_P (w));
11341 xassert (!FRAME_WINDOW_P (f));
11342
11343 /* If W is a full-width window, glyph pointers in W's current matrix
11344 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
11345 corresponding frame matrix. Note that frame matrices have no
11346 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
11347 window_row = w->current_matrix->rows;
11348 window_row_end = window_row + w->current_matrix->nrows;
11349 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11350 while (window_row < window_row_end)
659a218f 11351 {
7d4cc828
GM
11352 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11353 struct glyph *end = window_row->glyphs[LAST_AREA];
11354
11355 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11356 frame_row->glyphs[TEXT_AREA] = start;
11357 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11358 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
11359
11360 /* Disable frame rows whose corresponding window rows have
11361 been disabled in try_window_id. */
11362 if (!window_row->enabled_p)
11363 frame_row->enabled_p = 0;
11364
5f5c8ee5 11365 ++window_row, ++frame_row;
659a218f 11366 }
a2889657 11367}
5f5c8ee5
GM
11368
11369
e037b9ec
GM
11370/* Find the glyph row in window W containing CHARPOS. Consider all
11371 rows between START and END (not inclusive). END null means search
11372 all rows to the end of the display area of W. Value is the row
11373 containing CHARPOS or null. */
11374
0bef35bc 11375struct glyph_row *
0df56f4d 11376row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
11377 struct window *w;
11378 int charpos;
11379 struct glyph_row *start, *end;
0df56f4d 11380 int dy;
e037b9ec
GM
11381{
11382 struct glyph_row *row = start;
11383 int last_y;
11384
11385 /* If we happen to start on a header-line, skip that. */
11386 if (row->mode_line_p)
11387 ++row;
11388
11389 if ((end && row >= end) || !row->enabled_p)
11390 return NULL;
11391
0df56f4d 11392 last_y = window_text_bottom_y (w) - dy;
e037b9ec
GM
11393
11394 while ((end == NULL || row < end)
0df56f4d 11395 && MATRIX_ROW_BOTTOM_Y (row) < last_y
e037b9ec 11396 && (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 11397 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
11398 /* The end position of a row equals the start
11399 position of the next row. If CHARPOS is there, we
11400 would rather display it in the next line, except
11401 when this line ends in ZV. */
11402 && !row->ends_at_zv_p
11403 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
e037b9ec
GM
11404 ++row;
11405
11406 /* Give up if CHARPOS not found. */
11407 if ((end && row >= end)
11408 || charpos < MATRIX_ROW_START_CHARPOS (row)
11409 || charpos > MATRIX_ROW_END_CHARPOS (row))
11410 row = NULL;
11411
11412 return row;
11413}
11414
11415
5f5c8ee5
GM
11416/* Try to redisplay window W by reusing its existing display. W's
11417 current matrix must be up to date when this function is called,
11418 i.e. window_end_valid must not be nil.
11419
11420 Value is
11421
11422 1 if display has been updated
11423 0 if otherwise unsuccessful
11424 -1 if redisplay with same window start is known not to succeed
11425
11426 The following steps are performed:
11427
11428 1. Find the last row in the current matrix of W that is not
11429 affected by changes at the start of current_buffer. If no such row
11430 is found, give up.
11431
11432 2. Find the first row in W's current matrix that is not affected by
11433 changes at the end of current_buffer. Maybe there is no such row.
11434
11435 3. Display lines beginning with the row + 1 found in step 1 to the
11436 row found in step 2 or, if step 2 didn't find a row, to the end of
11437 the window.
11438
11439 4. If cursor is not known to appear on the window, give up.
11440
11441 5. If display stopped at the row found in step 2, scroll the
11442 display and current matrix as needed.
11443
11444 6. Maybe display some lines at the end of W, if we must. This can
11445 happen under various circumstances, like a partially visible line
11446 becoming fully visible, or because newly displayed lines are displayed
11447 in smaller font sizes.
11448
11449 7. Update W's window end information. */
11450
12adba34 11451static int
5f5c8ee5 11452try_window_id (w)
12adba34 11453 struct window *w;
12adba34 11454{
5f5c8ee5
GM
11455 struct frame *f = XFRAME (w->frame);
11456 struct glyph_matrix *current_matrix = w->current_matrix;
11457 struct glyph_matrix *desired_matrix = w->desired_matrix;
11458 struct glyph_row *last_unchanged_at_beg_row;
11459 struct glyph_row *first_unchanged_at_end_row;
11460 struct glyph_row *row;
11461 struct glyph_row *bottom_row;
11462 int bottom_vpos;
11463 struct it it;
11464 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11465 struct text_pos start_pos;
11466 struct run run;
11467 int first_unchanged_at_end_vpos = 0;
11468 struct glyph_row *last_text_row, *last_text_row_at_end;
11469 struct text_pos start;
27d16f05 11470 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 11471
69d1f7c9 11472#if GLYPH_DEBUG
76cb5e06
GM
11473 if (inhibit_try_window_id)
11474 return 0;
11475#endif
11476
27d16f05
GM
11477 /* This is handy for debugging. */
11478#if 0
11479#define GIVE_UP(X) \
11480 do { \
11481 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11482 return 0; \
11483 } while (0)
11484#else
62397849 11485#define GIVE_UP(X) return 0
27d16f05
GM
11486#endif
11487
5f5c8ee5
GM
11488 SET_TEXT_POS_FROM_MARKER (start, w->start);
11489
27d16f05
GM
11490 /* Don't use this for mini-windows because these can show
11491 messages and mini-buffers, and we don't handle that here. */
11492 if (MINI_WINDOW_P (w))
11493 GIVE_UP (1);
11494
11495 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 11496 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05
GM
11497 GIVE_UP (2);
11498
f5376658 11499 /* Verify that narrowing has not changed. This flag is also set to prevent
27d16f05
GM
11500 redisplay optimizations. It would be nice to further
11501 reduce the number of cases where this prevents try_window_id. */
11502 if (current_buffer->clip_changed)
11503 GIVE_UP (3);
11504
11505 /* Window must either use window-based redisplay or be full width. */
11506 if (!FRAME_WINDOW_P (f)
11507 && (!line_ins_del_ok
11508 || !WINDOW_FULL_WIDTH_P (w)))
11509 GIVE_UP (4);
5f5c8ee5 11510
f5376658 11511 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
11512 if (PT < CHARPOS (start))
11513 GIVE_UP (5);
11514
11515 /* Another way to prevent redisplay optimizations. */
11516 if (XFASTINT (w->last_modified) == 0)
11517 GIVE_UP (6);
11518
f5376658 11519 /* Verify that window is not hscrolled. */
27d16f05
GM
11520 if (XFASTINT (w->hscroll) != 0)
11521 GIVE_UP (7);
11522
f5376658 11523 /* Verify that display wasn't paused. */
27d16f05
GM
11524 if (NILP (w->window_end_valid))
11525 GIVE_UP (8);
11526
11527 /* Can't use this if highlighting a region because a cursor movement
11528 will do more than just set the cursor. */
11529 if (!NILP (Vtransient_mark_mode)
11530 && !NILP (current_buffer->mark_active))
11531 GIVE_UP (9);
11532
11533 /* Likewise if highlighting trailing whitespace. */
11534 if (!NILP (Vshow_trailing_whitespace))
11535 GIVE_UP (11);
11536
11537 /* Likewise if showing a region. */
11538 if (!NILP (w->region_showing))
11539 GIVE_UP (10);
11540
11541 /* Can use this if overlay arrow position and or string have changed. */
11542 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11543 || !EQ (last_arrow_string, Voverlay_arrow_string))
11544 GIVE_UP (12);
11545
11546
5f5c8ee5
GM
11547 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11548 only if buffer has really changed. The reason is that the gap is
11549 initially at Z for freshly visited files. The code below would
11550 set end_unchanged to 0 in that case. */
28ee91c0
GM
11551 if (MODIFF > SAVE_MODIFF
11552 /* This seems to happen sometimes after saving a buffer. */
11553 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 11554 {
9142dd5b
GM
11555 if (GPT - BEG < BEG_UNCHANGED)
11556 BEG_UNCHANGED = GPT - BEG;
11557 if (Z - GPT < END_UNCHANGED)
11558 END_UNCHANGED = Z - GPT;
5f5c8ee5 11559 }
bf9249e3 11560
27d16f05
GM
11561 /* The position of the first and last character that has been changed. */
11562 first_changed_charpos = BEG + BEG_UNCHANGED;
11563 last_changed_charpos = Z - END_UNCHANGED;
11564
5f5c8ee5
GM
11565 /* If window starts after a line end, and the last change is in
11566 front of that newline, then changes don't affect the display.
f2d86d7a
GM
11567 This case happens with stealth-fontification. Note that although
11568 the display is unchanged, glyph positions in the matrix have to
11569 be adjusted, of course. */
5f5c8ee5 11570 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 11571 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 11572 && ((last_changed_charpos < CHARPOS (start)
27d16f05 11573 && CHARPOS (start) == BEGV)
e60f4527 11574 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
11575 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11576 {
11577 int Z_old, delta, Z_BYTE_old, delta_bytes;
11578 struct glyph_row *r0;
11579
11580 /* Compute how many chars/bytes have been added to or removed
11581 from the buffer. */
11582 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11583 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11584 delta = Z - Z_old;
11585 delta_bytes = Z_BYTE - Z_BYTE_old;
11586
11587 /* Give up if PT is not in the window. Note that it already has
11588 been checked at the start of try_window_id that PT is not in
11589 front of the window start. */
11590 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11591 GIVE_UP (13);
11592
11593 /* If window start is unchanged, we can reuse the whole matrix
11594 as is, after adjusting glyph positions. No need to compute
11595 the window end again, since its offset from Z hasn't changed. */
11596 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11597 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11598 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11599 {
11600 /* Adjust positions in the glyph matrix. */
11601 if (delta || delta_bytes)
11602 {
11603 struct glyph_row *r1
11604 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11605 increment_matrix_positions (w->current_matrix,
11606 MATRIX_ROW_VPOS (r0, current_matrix),
11607 MATRIX_ROW_VPOS (r1, current_matrix),
11608 delta, delta_bytes);
11609 }
e5959a9a 11610
27d16f05 11611 /* Set the cursor. */
0df56f4d 11612 row = row_containing_pos (w, PT, r0, NULL, 0);
27d16f05
GM
11613 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11614 return 1;
11615 }
5f5c8ee5
GM
11616 }
11617
27d16f05 11618 /* Handle the case that changes are all below what is displayed in
33ea6c4d 11619 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
11620 be taken if ZV is visible in the window, and text has been added
11621 there that is visible in the window. */
11622 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
11623 /* ZV is not visible in the window, or there are no
11624 changes at ZV, actually. */
11625 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11626 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 11627 {
27d16f05 11628 struct glyph_row *r0;
ef121659 11629
27d16f05
GM
11630 /* Give up if PT is not in the window. Note that it already has
11631 been checked at the start of try_window_id that PT is not in
11632 front of the window start. */
11633 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11634 GIVE_UP (14);
11635
11636 /* If window start is unchanged, we can reuse the whole matrix
11637 as is, without changing glyph positions since no text has
11638 been added/removed in front of the window end. */
11639 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11640 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11641 {
11642 /* We have to compute the window end anew since text
11643 can have been added/removed after it. */
11644 w->window_end_pos
11645 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11646 w->window_end_bytepos
11647 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11648
11649 /* Set the cursor. */
0df56f4d 11650 row = row_containing_pos (w, PT, r0, NULL, 0);
27d16f05
GM
11651 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11652 return 2;
11653 }
5f5c8ee5
GM
11654 }
11655
2b9c25e0
GM
11656 /* Give up if window start is in the changed area.
11657
11658 The condition used to read
11659
11660 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11661
11662 but why that was tested escapes me at the moment. */
11663 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
11664 && CHARPOS (start) <= last_changed_charpos)
11665 GIVE_UP (15);
11666
f5376658
RS
11667 /* Check that window start agrees with the start of the first glyph
11668 row in its current matrix. Check this after we know the window
11669 start is not in changed text, otherwise positions would not be
11670 comparable. */
27d16f05 11671 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 11672 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 11673 GIVE_UP (16);
5f5c8ee5 11674
23e8bd86
GM
11675 /* Give up if the window ends in strings. Overlay strings
11676 at the end are difficult to handle, so don't try. */
11677 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11678 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11679 GIVE_UP (20);
11680
5f5c8ee5
GM
11681 /* Compute the position at which we have to start displaying new
11682 lines. Some of the lines at the top of the window might be
11683 reusable because they are not displaying changed text. Find the
11684 last row in W's current matrix not affected by changes at the
11685 start of current_buffer. Value is null if changes start in the
11686 first line of window. */
1ec185cb 11687 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
11688 if (last_unchanged_at_beg_row)
11689 {
67f1cf4c
GM
11690 /* Avoid starting to display in the moddle of a character, a TAB
11691 for instance. This is easier than to set up the iterator
11692 exactly, and it's not a frequent case, so the additional
11693 effort wouldn't really pay off. */
e74fb0f7
GM
11694 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11695 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
11696 && last_unchanged_at_beg_row > w->current_matrix->rows)
11697 --last_unchanged_at_beg_row;
11698
11699 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 11700 GIVE_UP (17);
47d57b22
GM
11701
11702 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11703 GIVE_UP (18);
5f5c8ee5
GM
11704 start_pos = it.current.pos;
11705
11706 /* Start displaying new lines in the desired matrix at the same
11707 vpos we would use in the current matrix, i.e. below
11708 last_unchanged_at_beg_row. */
11709 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11710 current_matrix);
11711 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11712 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11713
11714 xassert (it.hpos == 0 && it.current_x == 0);
11715 }
11716 else
11717 {
11718 /* There are no reusable lines at the start of the window.
11719 Start displaying in the first line. */
11720 start_display (&it, w, start);
11721 start_pos = it.current.pos;
11722 }
11723
5f5c8ee5
GM
11724 /* Find the first row that is not affected by changes at the end of
11725 the buffer. Value will be null if there is no unchanged row, in
11726 which case we must redisplay to the end of the window. delta
11727 will be set to the value by which buffer positions beginning with
11728 first_unchanged_at_end_row have to be adjusted due to text
11729 changes. */
11730 first_unchanged_at_end_row
1ec185cb 11731 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
11732 IF_DEBUG (debug_delta = delta);
11733 IF_DEBUG (debug_delta_bytes = delta_bytes);
11734
11735 /* Set stop_pos to the buffer position up to which we will have to
11736 display new lines. If first_unchanged_at_end_row != NULL, this
11737 is the buffer position of the start of the line displayed in that
11738 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11739 that we don't stop at a buffer position. */
11740 stop_pos = 0;
11741 if (first_unchanged_at_end_row)
11742 {
11743 xassert (last_unchanged_at_beg_row == NULL
11744 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11745
11746 /* If this is a continuation line, move forward to the next one
11747 that isn't. Changes in lines above affect this line.
11748 Caution: this may move first_unchanged_at_end_row to a row
11749 not displaying text. */
11750 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11751 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11752 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11753 < it.last_visible_y))
11754 ++first_unchanged_at_end_row;
11755
11756 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11757 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11758 >= it.last_visible_y))
11759 first_unchanged_at_end_row = NULL;
11760 else
11761 {
11762 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11763 + delta);
11764 first_unchanged_at_end_vpos
11765 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 11766 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
11767 }
11768 }
11769 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 11770 GIVE_UP (19);
5f5c8ee5
GM
11771
11772
11773#if GLYPH_DEBUG
11774
11775 /* Either there is no unchanged row at the end, or the one we have
11776 now displays text. This is a necessary condition for the window
11777 end pos calculation at the end of this function. */
11778 xassert (first_unchanged_at_end_row == NULL
11779 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11780
11781 debug_last_unchanged_at_beg_vpos
11782 = (last_unchanged_at_beg_row
11783 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11784 : -1);
11785 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11786
11787#endif /* GLYPH_DEBUG != 0 */
11788
33ea6c4d 11789
5f5c8ee5
GM
11790 /* Display new lines. Set last_text_row to the last new line
11791 displayed which has text on it, i.e. might end up as being the
11792 line where the window_end_vpos is. */
11793 w->cursor.vpos = -1;
11794 last_text_row = NULL;
11795 overlay_arrow_seen = 0;
11796 while (it.current_y < it.last_visible_y
11797 && !fonts_changed_p
11798 && (first_unchanged_at_end_row == NULL
11799 || IT_CHARPOS (it) < stop_pos))
11800 {
11801 if (display_line (&it))
11802 last_text_row = it.glyph_row - 1;
11803 }
11804
11805 if (fonts_changed_p)
11806 return -1;
11807
11808
11809 /* Compute differences in buffer positions, y-positions etc. for
11810 lines reused at the bottom of the window. Compute what we can
11811 scroll. */
ca42b2e8
GM
11812 if (first_unchanged_at_end_row
11813 /* No lines reused because we displayed everything up to the
11814 bottom of the window. */
11815 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
11816 {
11817 dvpos = (it.vpos
11818 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11819 current_matrix));
11820 dy = it.current_y - first_unchanged_at_end_row->y;
11821 run.current_y = first_unchanged_at_end_row->y;
11822 run.desired_y = run.current_y + dy;
11823 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11824 }
11825 else
ca42b2e8
GM
11826 {
11827 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11828 first_unchanged_at_end_row = NULL;
11829 }
5f5c8ee5
GM
11830 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11831
8f8ba186 11832
5f5c8ee5
GM
11833 /* Find the cursor if not already found. We have to decide whether
11834 PT will appear on this window (it sometimes doesn't, but this is
11835 not a very frequent case.) This decision has to be made before
11836 the current matrix is altered. A value of cursor.vpos < 0 means
11837 that PT is either in one of the lines beginning at
11838 first_unchanged_at_end_row or below the window. Don't care for
11839 lines that might be displayed later at the window end; as
11840 mentioned, this is not a frequent case. */
11841 if (w->cursor.vpos < 0)
11842 {
5f5c8ee5
GM
11843 /* Cursor in unchanged rows at the top? */
11844 if (PT < CHARPOS (start_pos)
11845 && last_unchanged_at_beg_row)
11846 {
e037b9ec
GM
11847 row = row_containing_pos (w, PT,
11848 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 11849 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
11850 if (row)
11851 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
11852 }
11853
11854 /* Start from first_unchanged_at_end_row looking for PT. */
11855 else if (first_unchanged_at_end_row)
11856 {
e037b9ec 11857 row = row_containing_pos (w, PT - delta,
0df56f4d 11858 first_unchanged_at_end_row, NULL, 0);
e037b9ec 11859 if (row)
468155d7
GM
11860 set_cursor_from_row (w, row, w->current_matrix, delta,
11861 delta_bytes, dy, dvpos);
5f5c8ee5
GM
11862 }
11863
11864 /* Give up if cursor was not found. */
11865 if (w->cursor.vpos < 0)
11866 {
11867 clear_glyph_matrix (w->desired_matrix);
11868 return -1;
11869 }
11870 }
11871
11872 /* Don't let the cursor end in the scroll margins. */
11873 {
11874 int this_scroll_margin, cursor_height;
11875
11876 this_scroll_margin = max (0, scroll_margin);
11877 this_scroll_margin = min (this_scroll_margin,
11878 XFASTINT (w->height) / 4);
11879 this_scroll_margin *= CANON_Y_UNIT (it.f);
11880 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11881
11882 if ((w->cursor.y < this_scroll_margin
11883 && CHARPOS (start) > BEGV)
11884 /* Don't take scroll margin into account at the bottom because
11885 old redisplay didn't do it either. */
11886 || w->cursor.y + cursor_height > it.last_visible_y)
11887 {
11888 w->cursor.vpos = -1;
11889 clear_glyph_matrix (w->desired_matrix);
11890 return -1;
11891 }
11892 }
11893
11894 /* Scroll the display. Do it before changing the current matrix so
11895 that xterm.c doesn't get confused about where the cursor glyph is
11896 found. */
fa77249f 11897 if (dy && run.height)
5f5c8ee5
GM
11898 {
11899 update_begin (f);
11900
11901 if (FRAME_WINDOW_P (f))
11902 {
11903 rif->update_window_begin_hook (w);
64d1e7d3 11904 rif->clear_mouse_face (w);
5f5c8ee5 11905 rif->scroll_run_hook (w, &run);
64d1e7d3 11906 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11907 }
11908 else
11909 {
11910 /* Terminal frame. In this case, dvpos gives the number of
11911 lines to scroll by; dvpos < 0 means scroll up. */
11912 int first_unchanged_at_end_vpos
11913 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11914 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
522ed7fb
GM
11915 int end = (XFASTINT (w->top)
11916 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11917 + window_internal_height (w));
5f5c8ee5
GM
11918
11919 /* Perform the operation on the screen. */
11920 if (dvpos > 0)
11921 {
11922 /* Scroll last_unchanged_at_beg_row to the end of the
11923 window down dvpos lines. */
11924 set_terminal_window (end);
11925
11926 /* On dumb terminals delete dvpos lines at the end
11927 before inserting dvpos empty lines. */
11928 if (!scroll_region_ok)
11929 ins_del_lines (end - dvpos, -dvpos);
11930
11931 /* Insert dvpos empty lines in front of
11932 last_unchanged_at_beg_row. */
11933 ins_del_lines (from, dvpos);
11934 }
11935 else if (dvpos < 0)
11936 {
11937 /* Scroll up last_unchanged_at_beg_vpos to the end of
11938 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11939 set_terminal_window (end);
11940
11941 /* Delete dvpos lines in front of
11942 last_unchanged_at_beg_vpos. ins_del_lines will set
11943 the cursor to the given vpos and emit |dvpos| delete
11944 line sequences. */
11945 ins_del_lines (from + dvpos, dvpos);
11946
11947 /* On a dumb terminal insert dvpos empty lines at the
11948 end. */
11949 if (!scroll_region_ok)
11950 ins_del_lines (end + dvpos, -dvpos);
11951 }
11952
11953 set_terminal_window (0);
11954 }
11955
11956 update_end (f);
11957 }
11958
ca42b2e8
GM
11959 /* Shift reused rows of the current matrix to the right position.
11960 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11961 text. */
11962 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11963 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
11964 if (dvpos < 0)
11965 {
11966 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11967 bottom_vpos, dvpos);
11968 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11969 bottom_vpos, 0);
11970 }
11971 else if (dvpos > 0)
11972 {
11973 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11974 bottom_vpos, dvpos);
11975 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11976 first_unchanged_at_end_vpos + dvpos, 0);
11977 }
11978
11979 /* For frame-based redisplay, make sure that current frame and window
11980 matrix are in sync with respect to glyph memory. */
11981 if (!FRAME_WINDOW_P (f))
11982 sync_frame_with_window_matrix_rows (w);
11983
11984 /* Adjust buffer positions in reused rows. */
11985 if (delta)
f2d86d7a
GM
11986 increment_matrix_positions (current_matrix,
11987 first_unchanged_at_end_vpos + dvpos,
11988 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
11989
11990 /* Adjust Y positions. */
11991 if (dy)
11992 shift_glyph_matrix (w, current_matrix,
11993 first_unchanged_at_end_vpos + dvpos,
11994 bottom_vpos, dy);
11995
11996 if (first_unchanged_at_end_row)
11997 first_unchanged_at_end_row += dvpos;
11998
11999 /* If scrolling up, there may be some lines to display at the end of
12000 the window. */
12001 last_text_row_at_end = NULL;
12002 if (dy < 0)
12003 {
9c6ba6d1
GM
12004 /* Scrolling up can leave for example a partially visible line
12005 at the end of the window to be redisplayed. */
5f5c8ee5
GM
12006 /* Set last_row to the glyph row in the current matrix where the
12007 window end line is found. It has been moved up or down in
12008 the matrix by dvpos. */
12009 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12010 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12011
12012 /* If last_row is the window end line, it should display text. */
12013 xassert (last_row->displays_text_p);
12014
12015 /* If window end line was partially visible before, begin
12016 displaying at that line. Otherwise begin displaying with the
12017 line following it. */
12018 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12019 {
12020 init_to_row_start (&it, w, last_row);
12021 it.vpos = last_vpos;
12022 it.current_y = last_row->y;
12023 }
12024 else
12025 {
12026 init_to_row_end (&it, w, last_row);
12027 it.vpos = 1 + last_vpos;
12028 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12029 ++last_row;
12030 }
12adba34 12031
23e8bd86
GM
12032 /* We may start in a continuation line. If so, we have to
12033 get the right continuation_lines_width and current_x. */
12034 it.continuation_lines_width = last_row->continuation_lines_width;
12035 it.hpos = it.current_x = 0;
12036
12037 /* Display the rest of the lines at the window end. */
12038 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12039 while (it.current_y < it.last_visible_y
12040 && !fonts_changed_p)
5f5c8ee5 12041 {
23e8bd86
GM
12042 /* Is it always sure that the display agrees with lines in
12043 the current matrix? I don't think so, so we mark rows
12044 displayed invalid in the current matrix by setting their
12045 enabled_p flag to zero. */
12046 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12047 if (display_line (&it))
12048 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
12049 }
12050 }
12adba34 12051
5f5c8ee5
GM
12052 /* Update window_end_pos and window_end_vpos. */
12053 if (first_unchanged_at_end_row
12054 && first_unchanged_at_end_row->y < it.last_visible_y
12055 && !last_text_row_at_end)
12056 {
12057 /* Window end line if one of the preserved rows from the current
12058 matrix. Set row to the last row displaying text in current
12059 matrix starting at first_unchanged_at_end_row, after
12060 scrolling. */
12061 xassert (first_unchanged_at_end_row->displays_text_p);
12062 row = find_last_row_displaying_text (w->current_matrix, &it,
12063 first_unchanged_at_end_row);
12064 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12065
ac90c44f 12066 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 12067 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
12068 w->window_end_vpos
12069 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 12070 xassert (w->window_end_bytepos >= 0);
0416532f 12071 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
12072 }
12073 else if (last_text_row_at_end)
12074 {
ac90c44f
GM
12075 w->window_end_pos
12076 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
12077 w->window_end_bytepos
12078 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
12079 w->window_end_vpos
12080 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 12081 xassert (w->window_end_bytepos >= 0);
0416532f 12082 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
12083 }
12084 else if (last_text_row)
12085 {
12086 /* We have displayed either to the end of the window or at the
12087 end of the window, i.e. the last row with text is to be found
12088 in the desired matrix. */
ac90c44f
GM
12089 w->window_end_pos
12090 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
12091 w->window_end_bytepos
12092 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12093 w->window_end_vpos
12094 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 12095 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
12096 }
12097 else if (first_unchanged_at_end_row == NULL
12098 && last_text_row == NULL
12099 && last_text_row_at_end == NULL)
12100 {
12101 /* Displayed to end of window, but no line containing text was
12102 displayed. Lines were deleted at the end of the window. */
0416532f
GM
12103 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12104 int vpos = XFASTINT (w->window_end_vpos);
12105 struct glyph_row *current_row = current_matrix->rows + vpos;
12106 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12107
12108 for (row = NULL;
12109 row == NULL && vpos >= first_vpos;
12110 --vpos, --current_row, --desired_row)
12111 {
12112 if (desired_row->enabled_p)
12113 {
12114 if (desired_row->displays_text_p)
12115 row = desired_row;
12116 }
12117 else if (current_row->displays_text_p)
12118 row = current_row;
12119 }
12adba34 12120
0416532f 12121 xassert (row != NULL);
d88a79d4 12122 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
12123 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12124 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 12125 xassert (w->window_end_bytepos >= 0);
0416532f 12126 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
12127 }
12128 else
12129 abort ();
ac90c44f 12130
8cdb267e
GM
12131#if 0 /* This leads to problems, for instance when the cursor is
12132 at ZV, and the cursor line displays no text. */
ac90c44f
GM
12133 /* Disable rows below what's displayed in the window. This makes
12134 debugging easier. */
12135 enable_glyph_matrix_rows (current_matrix,
12136 XFASTINT (w->window_end_vpos) + 1,
12137 bottom_vpos, 0);
8cdb267e 12138#endif
23e8bd86 12139
5f5c8ee5
GM
12140 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12141 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 12142
5f5c8ee5
GM
12143 /* Record that display has not been completed. */
12144 w->window_end_valid = Qnil;
12145 w->desired_matrix->no_scrolling_p = 1;
ef121659 12146 return 3;
27d16f05
GM
12147
12148#undef GIVE_UP
12adba34 12149}
0f9c0ff0 12150
a2889657 12151
5f5c8ee5
GM
12152\f
12153/***********************************************************************
12154 More debugging support
12155 ***********************************************************************/
a2889657 12156
5f5c8ee5 12157#if GLYPH_DEBUG
a2889657 12158
eaaa65b0 12159void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 12160void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 12161void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 12162
31b24551 12163
9ab436b9 12164/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 12165
9ab436b9
GM
12166 GLYPHS 0 means don't show glyph contents.
12167 GLYPHS 1 means show glyphs in short form
12168 GLYPHS > 1 means show glyphs in long form. */
12169
12170void
12171dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 12172 struct glyph_matrix *matrix;
9ab436b9 12173 int glyphs;
5f5c8ee5 12174{
efc63ef0 12175 int i;
5f5c8ee5 12176 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 12177 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 12178}
31b24551 12179
68a37fa8 12180
e187cf71
GM
12181/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12182 the glyph row and area where the glyph comes from. */
12183
12184void
12185dump_glyph (row, glyph, area)
12186 struct glyph_row *row;
12187 struct glyph *glyph;
12188 int area;
12189{
12190 if (glyph->type == CHAR_GLYPH)
12191 {
12192 fprintf (stderr,
12193 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12194 glyph - row->glyphs[TEXT_AREA],
12195 'C',
12196 glyph->charpos,
12197 (BUFFERP (glyph->object)
12198 ? 'B'
12199 : (STRINGP (glyph->object)
12200 ? 'S'
12201 : '-')),
12202 glyph->pixel_width,
12203 glyph->u.ch,
12204 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12205 ? glyph->u.ch
12206 : '.'),
12207 glyph->face_id,
12208 glyph->left_box_line_p,
12209 glyph->right_box_line_p);
12210 }
12211 else if (glyph->type == STRETCH_GLYPH)
12212 {
12213 fprintf (stderr,
12214 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12215 glyph - row->glyphs[TEXT_AREA],
12216 'S',
12217 glyph->charpos,
12218 (BUFFERP (glyph->object)
12219 ? 'B'
12220 : (STRINGP (glyph->object)
12221 ? 'S'
12222 : '-')),
12223 glyph->pixel_width,
12224 0,
12225 '.',
12226 glyph->face_id,
12227 glyph->left_box_line_p,
12228 glyph->right_box_line_p);
12229 }
12230 else if (glyph->type == IMAGE_GLYPH)
12231 {
12232 fprintf (stderr,
12233 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12234 glyph - row->glyphs[TEXT_AREA],
12235 'I',
12236 glyph->charpos,
12237 (BUFFERP (glyph->object)
12238 ? 'B'
12239 : (STRINGP (glyph->object)
12240 ? 'S'
12241 : '-')),
12242 glyph->pixel_width,
12243 glyph->u.img_id,
12244 '.',
12245 glyph->face_id,
12246 glyph->left_box_line_p,
12247 glyph->right_box_line_p);
12248 }
12249}
12250
12251
5f5c8ee5 12252/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
12253 GLYPHS 0 means don't show glyph contents.
12254 GLYPHS 1 means show glyphs in short form
12255 GLYPHS > 1 means show glyphs in long form. */
a2889657 12256
5f5c8ee5 12257void
eaaa65b0
GM
12258dump_glyph_row (row, vpos, glyphs)
12259 struct glyph_row *row;
9ab436b9 12260 int vpos, glyphs;
5f5c8ee5 12261{
9ab436b9
GM
12262 if (glyphs != 1)
12263 {
b94c0d9c 12264 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 12265 fprintf (stderr, "=======================================================================\n");
5f5c8ee5 12266
0ad38729 12267 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 12268%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 12269 vpos,
9ab436b9
GM
12270 MATRIX_ROW_START_CHARPOS (row),
12271 MATRIX_ROW_END_CHARPOS (row),
12272 row->used[TEXT_AREA],
12273 row->contains_overlapping_glyphs_p,
12274 row->enabled_p,
9ab436b9
GM
12275 row->truncated_on_left_p,
12276 row->truncated_on_right_p,
12277 row->overlay_arrow_p,
12278 row->continued_p,
12279 MATRIX_ROW_CONTINUATION_LINE_P (row),
12280 row->displays_text_p,
12281 row->ends_at_zv_p,
12282 row->fill_line_p,
12283 row->ends_in_middle_of_char_p,
12284 row->starts_in_middle_of_char_p,
b94c0d9c 12285 row->mouse_face_p,
9ab436b9
GM
12286 row->x,
12287 row->y,
12288 row->pixel_width,
12289 row->height,
12290 row->visible_height,
12291 row->ascent,
12292 row->phys_ascent);
33ea6c4d
GM
12293 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12294 row->end.overlay_string_index,
12295 row->continuation_lines_width);
9ab436b9
GM
12296 fprintf (stderr, "%9d %5d\n",
12297 CHARPOS (row->start.string_pos),
12298 CHARPOS (row->end.string_pos));
12299 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12300 row->end.dpvec_index);
12301 }
5f5c8ee5 12302
9ab436b9 12303 if (glyphs > 1)
bd66d1ba 12304 {
e187cf71
GM
12305 int area;
12306
12307 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12308 {
091f8878
GM
12309 struct glyph *glyph = row->glyphs[area];
12310 struct glyph *glyph_end = glyph + row->used[area];
5f5c8ee5 12311
e187cf71 12312 /* Glyph for a line end in text. */
091f8878 12313 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 12314 ++glyph_end;
5f5c8ee5 12315
e187cf71
GM
12316 if (glyph < glyph_end)
12317 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 12318
e187cf71
GM
12319 for (; glyph < glyph_end; ++glyph)
12320 dump_glyph (row, glyph, area);
f7b4b63a 12321 }
f4faa47c 12322 }
9ab436b9
GM
12323 else if (glyphs == 1)
12324 {
e187cf71 12325 int area;
9ab436b9 12326
e187cf71 12327 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 12328 {
e187cf71
GM
12329 char *s = (char *) alloca (row->used[area] + 1);
12330 int i;
12331
12332 for (i = 0; i < row->used[area]; ++i)
12333 {
12334 struct glyph *glyph = row->glyphs[area] + i;
12335 if (glyph->type == CHAR_GLYPH
12336 && glyph->u.ch < 0x80
12337 && glyph->u.ch >= ' ')
12338 s[i] = glyph->u.ch;
12339 else
12340 s[i] = '.';
12341 }
9ab436b9 12342
e187cf71
GM
12343 s[i] = '\0';
12344 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12345 }
9ab436b9 12346 }
5f5c8ee5 12347}
f4faa47c 12348
a2889657 12349
5f5c8ee5
GM
12350DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12351 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 12352 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
12353Shows contents of glyph row structures. With non-nil
12354parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
12355glyphs in short form, otherwise show glyphs in long form. */)
12356 (glyphs)
9ab436b9 12357 Lisp_Object glyphs;
5f5c8ee5
GM
12358{
12359 struct window *w = XWINDOW (selected_window);
12360 struct buffer *buffer = XBUFFER (w->buffer);
12361
12362 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12363 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12364 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12365 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12366 fprintf (stderr, "=============================================\n");
9ab436b9
GM
12367 dump_glyph_matrix (w->current_matrix,
12368 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
12369 return Qnil;
12370}
1c2250c2 12371
1fca3fae 12372
7d4cc828
GM
12373DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12374 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12375 ()
12376{
12377 struct frame *f = XFRAME (selected_frame);
12378 dump_glyph_matrix (f->current_matrix, 1);
12379 return Qnil;
12380}
12381
12382
e9abc296 12383DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 12384 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
12385GLYPH 0 means don't dump glyphs.
12386GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12387GLYPH > 1 or omitted means dump glyphs in long form. */)
12388 (row, glyphs)
e9abc296 12389 Lisp_Object row, glyphs;
5f5c8ee5 12390{
eaaa65b0
GM
12391 struct glyph_matrix *matrix;
12392 int vpos;
12393
b7826503 12394 CHECK_NUMBER (row);
eaaa65b0
GM
12395 matrix = XWINDOW (selected_window)->current_matrix;
12396 vpos = XINT (row);
12397 if (vpos >= 0 && vpos < matrix->nrows)
12398 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12399 vpos,
12400 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12401 return Qnil;
12402}
1fca3fae 12403
67481ae5 12404
b94c0d9c 12405DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 12406 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
12407GLYPH 0 means don't dump glyphs.
12408GLYPH 1 means dump glyphs in short form.
7ee72033
MB
12409GLYPH > 1 or omitted means dump glyphs in long form. */)
12410 (row, glyphs)
b94c0d9c 12411 Lisp_Object row, glyphs;
5f5c8ee5 12412{
886bd6f2 12413 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
12414 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12415 int vpos;
12416
b7826503 12417 CHECK_NUMBER (row);
eaaa65b0
GM
12418 vpos = XINT (row);
12419 if (vpos >= 0 && vpos < m->nrows)
12420 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12421 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
12422 return Qnil;
12423}
ca26e1c8 12424
0f9c0ff0 12425
62397849 12426DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
12427 doc: /* Toggle tracing of redisplay.
12428With ARG, turn tracing on if and only if ARG is positive. */)
12429 (arg)
62397849 12430 Lisp_Object arg;
5f5c8ee5 12431{
62397849
GM
12432 if (NILP (arg))
12433 trace_redisplay_p = !trace_redisplay_p;
12434 else
12435 {
12436 arg = Fprefix_numeric_value (arg);
12437 trace_redisplay_p = XINT (arg) > 0;
12438 }
12439
5f5c8ee5
GM
12440 return Qnil;
12441}
bf9249e3
GM
12442
12443
f4a374a1 12444DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
7ee72033
MB
12445 doc: /* Like `format', but print result to stderr. */)
12446 (nargs, args)
f4a374a1
GM
12447 int nargs;
12448 Lisp_Object *args;
bf9249e3 12449{
f4a374a1 12450 Lisp_Object s = Fformat (nargs, args);
2051c264 12451 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
12452 return Qnil;
12453}
5f5c8ee5
GM
12454
12455#endif /* GLYPH_DEBUG */
ca26e1c8 12456
ca26e1c8 12457
5f5c8ee5
GM
12458\f
12459/***********************************************************************
12460 Building Desired Matrix Rows
12461 ***********************************************************************/
a2889657 12462
5f5c8ee5
GM
12463/* Return a temporary glyph row holding the glyphs of an overlay
12464 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 12465
5f5c8ee5
GM
12466static struct glyph_row *
12467get_overlay_arrow_glyph_row (w)
12468 struct window *w;
12469{
12470 struct frame *f = XFRAME (WINDOW_FRAME (w));
12471 struct buffer *buffer = XBUFFER (w->buffer);
12472 struct buffer *old = current_buffer;
2051c264
GM
12473 unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12474 int arrow_len = SCHARS (Voverlay_arrow_string);
5f5c8ee5
GM
12475 unsigned char *arrow_end = arrow_string + arrow_len;
12476 unsigned char *p;
12477 struct it it;
12478 int multibyte_p;
12479 int n_glyphs_before;
12480
12481 set_buffer_temp (buffer);
12482 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12483 it.glyph_row->used[TEXT_AREA] = 0;
12484 SET_TEXT_POS (it.position, 0, 0);
12485
12486 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12487 p = arrow_string;
12488 while (p < arrow_end)
12489 {
12490 Lisp_Object face, ilisp;
12491
12492 /* Get the next character. */
12493 if (multibyte_p)
4fdb80f2 12494 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
12495 else
12496 it.c = *p, it.len = 1;
12497 p += it.len;
12498
12499 /* Get its face. */
ac90c44f 12500 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
12501 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12502 it.face_id = compute_char_face (f, it.c, face);
12503
12504 /* Compute its width, get its glyphs. */
12505 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 12506 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
12507 PRODUCE_GLYPHS (&it);
12508
12509 /* If this character doesn't fit any more in the line, we have
12510 to remove some glyphs. */
12511 if (it.current_x > it.last_visible_x)
12512 {
12513 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12514 break;
12515 }
12516 }
12517
12518 set_buffer_temp (old);
12519 return it.glyph_row;
12520}
ca26e1c8 12521
b0a0fbda 12522
5f5c8ee5
GM
12523/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12524 glyphs are only inserted for terminal frames since we can't really
12525 win with truncation glyphs when partially visible glyphs are
12526 involved. Which glyphs to insert is determined by
12527 produce_special_glyphs. */
67481ae5 12528
5f5c8ee5
GM
12529static void
12530insert_left_trunc_glyphs (it)
12531 struct it *it;
12532{
12533 struct it truncate_it;
12534 struct glyph *from, *end, *to, *toend;
12535
12536 xassert (!FRAME_WINDOW_P (it->f));
12537
12538 /* Get the truncation glyphs. */
12539 truncate_it = *it;
5f5c8ee5
GM
12540 truncate_it.current_x = 0;
12541 truncate_it.face_id = DEFAULT_FACE_ID;
12542 truncate_it.glyph_row = &scratch_glyph_row;
12543 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12544 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 12545 truncate_it.object = make_number (0);
5f5c8ee5
GM
12546 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12547
12548 /* Overwrite glyphs from IT with truncation glyphs. */
12549 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12550 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12551 to = it->glyph_row->glyphs[TEXT_AREA];
12552 toend = to + it->glyph_row->used[TEXT_AREA];
12553
12554 while (from < end)
12555 *to++ = *from++;
12556
37be86f2
KH
12557 /* There may be padding glyphs left over. Overwrite them too. */
12558 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12559 {
12560 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12561 while (from < end)
12562 *to++ = *from++;
12563 }
5f5c8ee5 12564
37be86f2
KH
12565 if (to > toend)
12566 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 12567}
e0bfbde6 12568
e0bfbde6 12569
5f5c8ee5 12570/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 12571
5f5c8ee5
GM
12572 Most of the time, ascent and height of a display line will be equal
12573 to the max_ascent and max_height values of the display iterator
12574 structure. This is not the case if
67481ae5 12575
5f5c8ee5
GM
12576 1. We hit ZV without displaying anything. In this case, max_ascent
12577 and max_height will be zero.
1c9241f5 12578
5f5c8ee5
GM
12579 2. We have some glyphs that don't contribute to the line height.
12580 (The glyph row flag contributes_to_line_height_p is for future
12581 pixmap extensions).
f6fd109b 12582
5f5c8ee5
GM
12583 The first case is easily covered by using default values because in
12584 these cases, the line height does not really matter, except that it
12585 must not be zero. */
67481ae5 12586
5f5c8ee5
GM
12587static void
12588compute_line_metrics (it)
12589 struct it *it;
12590{
12591 struct glyph_row *row = it->glyph_row;
12592 int area, i;
1c2250c2 12593
5f5c8ee5
GM
12594 if (FRAME_WINDOW_P (it->f))
12595 {
75c5350a 12596 int i, min_y, max_y;
1c2250c2 12597
5f5c8ee5
GM
12598 /* The line may consist of one space only, that was added to
12599 place the cursor on it. If so, the row's height hasn't been
12600 computed yet. */
12601 if (row->height == 0)
12602 {
12603 if (it->max_ascent + it->max_descent == 0)
312246d1 12604 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
12605 row->ascent = it->max_ascent;
12606 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12607 row->phys_ascent = it->max_phys_ascent;
12608 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12609 }
12610
12611 /* Compute the width of this line. */
12612 row->pixel_width = row->x;
12613 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12614 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12615
12616 xassert (row->pixel_width >= 0);
12617 xassert (row->ascent >= 0 && row->height > 0);
12618
312246d1
GM
12619 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12620 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12621
12622 /* If first line's physical ascent is larger than its logical
12623 ascent, use the physical ascent, and make the row taller.
12624 This makes accented characters fully visible. */
b28cb6ed 12625 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
12626 && row->phys_ascent > row->ascent)
12627 {
12628 row->height += row->phys_ascent - row->ascent;
12629 row->ascent = row->phys_ascent;
12630 }
12631
5f5c8ee5
GM
12632 /* Compute how much of the line is visible. */
12633 row->visible_height = row->height;
12634
75c5350a
GM
12635 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12636 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12637
12638 if (row->y < min_y)
12639 row->visible_height -= min_y - row->y;
12640 if (row->y + row->height > max_y)
12641 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
12642 }
12643 else
12644 {
12645 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
12646 if (row->continued_p)
12647 row->pixel_width -= it->continuation_pixel_width;
12648 else if (row->truncated_on_right_p)
12649 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
12650 row->ascent = row->phys_ascent = 0;
12651 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 12652 }
67481ae5 12653
5f5c8ee5
GM
12654 /* Compute a hash code for this row. */
12655 row->hash = 0;
12656 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12657 for (i = 0; i < row->used[area]; ++i)
12658 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12659 + row->glyphs[area][i].u.val
43d120d8
KH
12660 + row->glyphs[area][i].face_id
12661 + row->glyphs[area][i].padding_p
5f5c8ee5 12662 + (row->glyphs[area][i].type << 2));
a2889657 12663
5f5c8ee5 12664 it->max_ascent = it->max_descent = 0;
312246d1 12665 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 12666}
12adba34 12667
ca26e1c8 12668
5f5c8ee5
GM
12669/* Append one space to the glyph row of iterator IT if doing a
12670 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12671 space have the default face, otherwise let it have the same face as
80c6cb1f 12672 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
12673
12674 This function is called to make sure that there is always one glyph
12675 at the end of a glyph row that the cursor can be set on under
12676 window-systems. (If there weren't such a glyph we would not know
12677 how wide and tall a box cursor should be displayed).
12678
12679 At the same time this space let's a nicely handle clearing to the
12680 end of the line if the row ends in italic text. */
ca26e1c8 12681
80c6cb1f 12682static int
5f5c8ee5
GM
12683append_space (it, default_face_p)
12684 struct it *it;
12685 int default_face_p;
12686{
12687 if (FRAME_WINDOW_P (it->f))
12688 {
12689 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 12690
5f5c8ee5
GM
12691 if (it->glyph_row->glyphs[TEXT_AREA] + n
12692 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 12693 {
cafafe0b
GM
12694 /* Save some values that must not be changed.
12695 Must save IT->c and IT->len because otherwise
12696 ITERATOR_AT_END_P wouldn't work anymore after
12697 append_space has been called. */
478d746b 12698 enum display_element_type saved_what = it->what;
cafafe0b
GM
12699 int saved_c = it->c, saved_len = it->len;
12700 int saved_x = it->current_x;
5f5c8ee5 12701 int saved_face_id = it->face_id;
cafafe0b 12702 struct text_pos saved_pos;
5f5c8ee5 12703 Lisp_Object saved_object;
980806b6 12704 struct face *face;
5f5c8ee5
GM
12705
12706 saved_object = it->object;
12707 saved_pos = it->position;
12708
12709 it->what = IT_CHARACTER;
12710 bzero (&it->position, sizeof it->position);
6fc556fd 12711 it->object = make_number (0);
5f5c8ee5
GM
12712 it->c = ' ';
12713 it->len = 1;
5f5c8ee5
GM
12714
12715 if (default_face_p)
12716 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
12717 else if (it->face_before_selective_p)
12718 it->face_id = it->saved_face_id;
980806b6
KH
12719 face = FACE_FROM_ID (it->f, it->face_id);
12720 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 12721
5f5c8ee5
GM
12722 PRODUCE_GLYPHS (it);
12723
12724 it->current_x = saved_x;
12725 it->object = saved_object;
12726 it->position = saved_pos;
12727 it->what = saved_what;
12728 it->face_id = saved_face_id;
cafafe0b
GM
12729 it->len = saved_len;
12730 it->c = saved_c;
80c6cb1f 12731 return 1;
5f5c8ee5
GM
12732 }
12733 }
80c6cb1f
GM
12734
12735 return 0;
5f5c8ee5 12736}
12adba34 12737
1842fc1a 12738
5f5c8ee5
GM
12739/* Extend the face of the last glyph in the text area of IT->glyph_row
12740 to the end of the display line. Called from display_line.
12741 If the glyph row is empty, add a space glyph to it so that we
12742 know the face to draw. Set the glyph row flag fill_line_p. */
12743
12744static void
12745extend_face_to_end_of_line (it)
12746 struct it *it;
12747{
12748 struct face *face;
12749 struct frame *f = it->f;
1842fc1a 12750
5f5c8ee5
GM
12751 /* If line is already filled, do nothing. */
12752 if (it->current_x >= it->last_visible_x)
12753 return;
12754
12755 /* Face extension extends the background and box of IT->face_id
12756 to the end of the line. If the background equals the background
4aad61f8
GM
12757 of the frame, we don't have to do anything. */
12758 if (it->face_before_selective_p)
12759 face = FACE_FROM_ID (it->f, it->saved_face_id);
12760 else
12761 face = FACE_FROM_ID (f, it->face_id);
12762
5f5c8ee5
GM
12763 if (FRAME_WINDOW_P (f)
12764 && face->box == FACE_NO_BOX
12765 && face->background == FRAME_BACKGROUND_PIXEL (f)
12766 && !face->stipple)
12767 return;
1842fc1a 12768
5f5c8ee5
GM
12769 /* Set the glyph row flag indicating that the face of the last glyph
12770 in the text area has to be drawn to the end of the text area. */
12771 it->glyph_row->fill_line_p = 1;
545e04f6 12772
980806b6
KH
12773 /* If current character of IT is not ASCII, make sure we have the
12774 ASCII face. This will be automatically undone the next time
12775 get_next_display_element returns a multibyte character. Note
12776 that the character will always be single byte in unibyte text. */
12777 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 12778 {
980806b6 12779 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 12780 }
545e04f6 12781
5f5c8ee5
GM
12782 if (FRAME_WINDOW_P (f))
12783 {
12784 /* If the row is empty, add a space with the current face of IT,
12785 so that we know which face to draw. */
12786 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 12787 {
5f5c8ee5 12788 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 12789 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 12790 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 12791 }
5f5c8ee5
GM
12792 }
12793 else
12794 {
12795 /* Save some values that must not be changed. */
12796 int saved_x = it->current_x;
12797 struct text_pos saved_pos;
12798 Lisp_Object saved_object;
478d746b 12799 enum display_element_type saved_what = it->what;
4aad61f8 12800 int saved_face_id = it->face_id;
5f5c8ee5
GM
12801
12802 saved_object = it->object;
12803 saved_pos = it->position;
12804
12805 it->what = IT_CHARACTER;
12806 bzero (&it->position, sizeof it->position);
6fc556fd 12807 it->object = make_number (0);
5f5c8ee5
GM
12808 it->c = ' ';
12809 it->len = 1;
4aad61f8 12810 it->face_id = face->id;
5f5c8ee5
GM
12811
12812 PRODUCE_GLYPHS (it);
12813
12814 while (it->current_x <= it->last_visible_x)
12815 PRODUCE_GLYPHS (it);
12816
12817 /* Don't count these blanks really. It would let us insert a left
12818 truncation glyph below and make us set the cursor on them, maybe. */
12819 it->current_x = saved_x;
12820 it->object = saved_object;
12821 it->position = saved_pos;
12822 it->what = saved_what;
4aad61f8 12823 it->face_id = saved_face_id;
5f5c8ee5
GM
12824 }
12825}
12adba34 12826
545e04f6 12827
5f5c8ee5
GM
12828/* Value is non-zero if text starting at CHARPOS in current_buffer is
12829 trailing whitespace. */
1c9241f5 12830
5f5c8ee5
GM
12831static int
12832trailing_whitespace_p (charpos)
12833 int charpos;
12834{
12835 int bytepos = CHAR_TO_BYTE (charpos);
12836 int c = 0;
7bbe686f 12837
5f5c8ee5
GM
12838 while (bytepos < ZV_BYTE
12839 && (c = FETCH_CHAR (bytepos),
12840 c == ' ' || c == '\t'))
12841 ++bytepos;
0d09d1e6 12842
8f897821
GM
12843 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12844 {
12845 if (bytepos != PT_BYTE)
12846 return 1;
12847 }
12848 return 0;
5f5c8ee5 12849}
31b24551 12850
545e04f6 12851
5f5c8ee5 12852/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 12853
5f5c8ee5
GM
12854void
12855highlight_trailing_whitespace (f, row)
12856 struct frame *f;
12857 struct glyph_row *row;
12858{
12859 int used = row->used[TEXT_AREA];
12860
12861 if (used)
12862 {
12863 struct glyph *start = row->glyphs[TEXT_AREA];
12864 struct glyph *glyph = start + used - 1;
12865
ade0bee1
GM
12866 /* Skip over glyphs inserted to display the cursor at the
12867 end of a line, for extending the face of the last glyph
12868 to the end of the line on terminals, and for truncation
12869 and continuation glyphs. */
65008712
GM
12870 while (glyph >= start
12871 && glyph->type == CHAR_GLYPH
65008712 12872 && INTEGERP (glyph->object))
5f5c8ee5
GM
12873 --glyph;
12874
12875 /* If last glyph is a space or stretch, and it's trailing
12876 whitespace, set the face of all trailing whitespace glyphs in
12877 IT->glyph_row to `trailing-whitespace'. */
12878 if (glyph >= start
12879 && BUFFERP (glyph->object)
12880 && (glyph->type == STRETCH_GLYPH
12881 || (glyph->type == CHAR_GLYPH
43d120d8 12882 && glyph->u.ch == ' '))
5f5c8ee5 12883 && trailing_whitespace_p (glyph->charpos))
545e04f6 12884 {
980806b6 12885 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
12886
12887 while (glyph >= start
12888 && BUFFERP (glyph->object)
12889 && (glyph->type == STRETCH_GLYPH
12890 || (glyph->type == CHAR_GLYPH
43d120d8
KH
12891 && glyph->u.ch == ' ')))
12892 (glyph--)->face_id = face_id;
545e04f6 12893 }
a2889657 12894 }
5f5c8ee5 12895}
a2889657 12896
5fcbb24d 12897
cafafe0b 12898/* Value is non-zero if glyph row ROW in window W should be
0fd37545 12899 used to hold the cursor. */
cafafe0b
GM
12900
12901static int
12902cursor_row_p (w, row)
12903 struct window *w;
12904 struct glyph_row *row;
12905{
9a038881
GM
12906 int cursor_row_p = 1;
12907
cafafe0b
GM
12908 if (PT == MATRIX_ROW_END_CHARPOS (row))
12909 {
9a038881
GM
12910 /* If the row ends with a newline from a string, we don't want
12911 the cursor there (if the row is continued it doesn't end in a
12912 newline). */
12913 if (CHARPOS (row->end.string_pos) >= 0
12914 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12915 cursor_row_p = row->continued_p;
12916
12917 /* If the row ends at ZV, display the cursor at the end of that
12918 row instead of at the start of the row below. */
12919 else if (row->ends_at_zv_p)
12920 cursor_row_p = 1;
12921 else
12922 cursor_row_p = 0;
cafafe0b
GM
12923 }
12924
9a038881 12925 return cursor_row_p;
cafafe0b
GM
12926}
12927
12928
5f5c8ee5
GM
12929/* Construct the glyph row IT->glyph_row in the desired matrix of
12930 IT->w from text at the current position of IT. See dispextern.h
12931 for an overview of struct it. Value is non-zero if
12932 IT->glyph_row displays text, as opposed to a line displaying ZV
12933 only. */
12934
12935static int
12936display_line (it)
12937 struct it *it;
12938{
12939 struct glyph_row *row = it->glyph_row;
12940
12941 /* We always start displaying at hpos zero even if hscrolled. */
12942 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 12943
5f5c8ee5
GM
12944 /* We must not display in a row that's not a text row. */
12945 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12946 < it->w->desired_matrix->nrows);
12adba34 12947
5f5c8ee5
GM
12948 /* Is IT->w showing the region? */
12949 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 12950
5f5c8ee5
GM
12951 /* Clear the result glyph row and enable it. */
12952 prepare_desired_row (row);
12adba34 12953
5f5c8ee5
GM
12954 row->y = it->current_y;
12955 row->start = it->current;
12956 row->continuation_lines_width = it->continuation_lines_width;
12957 row->displays_text_p = 1;
91004049
GM
12958 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12959 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
12960
12961 /* Arrange the overlays nicely for our purposes. Usually, we call
12962 display_line on only one line at a time, in which case this
12963 can't really hurt too much, or we call it on lines which appear
12964 one after another in the buffer, in which case all calls to
12965 recenter_overlay_lists but the first will be pretty cheap. */
12966 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12967
5f5c8ee5
GM
12968 /* Move over display elements that are not visible because we are
12969 hscrolled. This may stop at an x-position < IT->first_visible_x
12970 if the first glyph is partially visible or if we hit a line end. */
12971 if (it->current_x < it->first_visible_x)
12972 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12973 MOVE_TO_POS | MOVE_TO_X);
12974
12975 /* Get the initial row height. This is either the height of the
12976 text hscrolled, if there is any, or zero. */
12977 row->ascent = it->max_ascent;
12978 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12979 row->phys_ascent = it->max_phys_ascent;
12980 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12981
12982 /* Loop generating characters. The loop is left with IT on the next
12983 character to display. */
12984 while (1)
12985 {
12986 int n_glyphs_before, hpos_before, x_before;
12987 int x, i, nglyphs;
ae26e27d 12988 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 12989
5f5c8ee5
GM
12990 /* Retrieve the next thing to display. Value is zero if end of
12991 buffer reached. */
12992 if (!get_next_display_element (it))
12993 {
12994 /* Maybe add a space at the end of this line that is used to
1b335865
GM
12995 display the cursor there under X. Set the charpos of the
12996 first glyph of blank lines not corresponding to any text
12997 to -1. */
12998 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12999 || row->used[TEXT_AREA] == 0)
a2889657 13000 {
5f5c8ee5
GM
13001 row->glyphs[TEXT_AREA]->charpos = -1;
13002 row->displays_text_p = 0;
13003
2ad551af 13004 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
13005 && (!MINI_WINDOW_P (it->w)
13006 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 13007 row->indicate_empty_line_p = 1;
a2889657 13008 }
5f5c8ee5
GM
13009
13010 it->continuation_lines_width = 0;
13011 row->ends_at_zv_p = 1;
13012 break;
a2889657 13013 }
a2889657 13014
5f5c8ee5
GM
13015 /* Now, get the metrics of what we want to display. This also
13016 generates glyphs in `row' (which is IT->glyph_row). */
13017 n_glyphs_before = row->used[TEXT_AREA];
13018 x = it->current_x;
e6819faf
GM
13019
13020 /* Remember the line height so far in case the next element doesn't
13021 fit on the line. */
13022 if (!it->truncate_lines_p)
13023 {
13024 ascent = it->max_ascent;
13025 descent = it->max_descent;
13026 phys_ascent = it->max_phys_ascent;
13027 phys_descent = it->max_phys_descent;
13028 }
13029
5f5c8ee5 13030 PRODUCE_GLYPHS (it);
a2889657 13031
5f5c8ee5
GM
13032 /* If this display element was in marginal areas, continue with
13033 the next one. */
13034 if (it->area != TEXT_AREA)
a2889657 13035 {
5f5c8ee5
GM
13036 row->ascent = max (row->ascent, it->max_ascent);
13037 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13038 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13039 row->phys_height = max (row->phys_height,
13040 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 13041 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13042 continue;
13043 }
5936754e 13044
5f5c8ee5
GM
13045 /* Does the display element fit on the line? If we truncate
13046 lines, we should draw past the right edge of the window. If
13047 we don't truncate, we want to stop so that we can display the
13048 continuation glyph before the right margin. If lines are
13049 continued, there are two possible strategies for characters
13050 resulting in more than 1 glyph (e.g. tabs): Display as many
13051 glyphs as possible in this line and leave the rest for the
13052 continuation line, or display the whole element in the next
13053 line. Original redisplay did the former, so we do it also. */
13054 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13055 hpos_before = it->hpos;
13056 x_before = x;
4dcd74e6 13057
4b41cebb 13058 if (/* Not a newline. */
4dcd74e6 13059 nglyphs > 0
202c1b5b 13060 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
13061 && it->current_x < it->last_visible_x)
13062 {
37be86f2 13063 it->hpos += nglyphs;
5f5c8ee5
GM
13064 row->ascent = max (row->ascent, it->max_ascent);
13065 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13066 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13067 row->phys_height = max (row->phys_height,
13068 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13069 if (it->current_x - it->pixel_width < it->first_visible_x)
13070 row->x = x - it->first_visible_x;
13071 }
13072 else
13073 {
13074 int new_x;
13075 struct glyph *glyph;
13076
13077 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 13078 {
5f5c8ee5
GM
13079 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13080 new_x = x + glyph->pixel_width;
13081
13082 if (/* Lines are continued. */
13083 !it->truncate_lines_p
13084 && (/* Glyph doesn't fit on the line. */
13085 new_x > it->last_visible_x
13086 /* Or it fits exactly on a window system frame. */
13087 || (new_x == it->last_visible_x
13088 && FRAME_WINDOW_P (it->f))))
a2889657 13089 {
5f5c8ee5
GM
13090 /* End of a continued line. */
13091
13092 if (it->hpos == 0
13093 || (new_x == it->last_visible_x
13094 && FRAME_WINDOW_P (it->f)))
13095 {
e6819faf
GM
13096 /* Current glyph is the only one on the line or
13097 fits exactly on the line. We must continue
13098 the line because we can't draw the cursor
13099 after the glyph. */
5f5c8ee5
GM
13100 row->continued_p = 1;
13101 it->current_x = new_x;
13102 it->continuation_lines_width += new_x;
13103 ++it->hpos;
13104 if (i == nglyphs - 1)
cafafe0b 13105 set_iterator_to_next (it, 1);
5f5c8ee5 13106 }
3b3c4bf0
GM
13107 else if (CHAR_GLYPH_PADDING_P (*glyph)
13108 && !FRAME_WINDOW_P (it->f))
13109 {
13110 /* A padding glyph that doesn't fit on this line.
13111 This means the whole character doesn't fit
13112 on the line. */
13113 row->used[TEXT_AREA] = n_glyphs_before;
13114
13115 /* Fill the rest of the row with continuation
13116 glyphs like in 20.x. */
13117 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13118 < row->glyphs[1 + TEXT_AREA])
13119 produce_special_glyphs (it, IT_CONTINUATION);
13120
13121 row->continued_p = 1;
13122 it->current_x = x_before;
13123 it->continuation_lines_width += x_before;
13124
13125 /* Restore the height to what it was before the
13126 element not fitting on the line. */
13127 it->max_ascent = ascent;
13128 it->max_descent = descent;
13129 it->max_phys_ascent = phys_ascent;
13130 it->max_phys_descent = phys_descent;
13131 }
0df56f4d
GM
13132 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13133 {
13134 /* A TAB that extends past the right edge of the
13135 window. This produces a single glyph on
13136 window system frames. We leave the glyph in
13137 this row and let it fill the row, but don't
13138 consume the TAB. */
13139 it->continuation_lines_width += it->last_visible_x;
13140 row->ends_in_middle_of_char_p = 1;
13141 row->continued_p = 1;
13142 glyph->pixel_width = it->last_visible_x - x;
13143 it->starts_in_middle_of_char_p = 1;
13144 }
5f5c8ee5 13145 else
5936754e 13146 {
0df56f4d
GM
13147 /* Something other than a TAB that draws past
13148 the right edge of the window. Restore
13149 positions to values before the element. */
5f5c8ee5
GM
13150 row->used[TEXT_AREA] = n_glyphs_before + i;
13151
13152 /* Display continuation glyphs. */
13153 if (!FRAME_WINDOW_P (it->f))
13154 produce_special_glyphs (it, IT_CONTINUATION);
13155 row->continued_p = 1;
653c329b 13156
0df56f4d 13157 it->continuation_lines_width += x;
5f5c8ee5 13158
91004049
GM
13159 if (nglyphs > 1 && i > 0)
13160 {
13161 row->ends_in_middle_of_char_p = 1;
13162 it->starts_in_middle_of_char_p = 1;
13163 }
e6819faf
GM
13164
13165 /* Restore the height to what it was before the
13166 element not fitting on the line. */
13167 it->max_ascent = ascent;
13168 it->max_descent = descent;
13169 it->max_phys_ascent = phys_ascent;
13170 it->max_phys_descent = phys_descent;
5936754e 13171 }
e6819faf 13172
5f5c8ee5
GM
13173 break;
13174 }
13175 else if (new_x > it->first_visible_x)
13176 {
13177 /* Increment number of glyphs actually displayed. */
13178 ++it->hpos;
13179
13180 if (x < it->first_visible_x)
13181 /* Glyph is partially visible, i.e. row starts at
13182 negative X position. */
13183 row->x = x - it->first_visible_x;
13184 }
13185 else
13186 {
13187 /* Glyph is completely off the left margin of the
13188 window. This should not happen because of the
13189 move_it_in_display_line at the start of
13190 this function. */
13191 abort ();
a2889657 13192 }
a2889657 13193 }
5f5c8ee5
GM
13194
13195 row->ascent = max (row->ascent, it->max_ascent);
13196 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13197 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13198 row->phys_height = max (row->phys_height,
13199 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13200
13201 /* End of this display line if row is continued. */
13202 if (row->continued_p)
13203 break;
a2889657 13204 }
a2889657 13205
5f5c8ee5
GM
13206 /* Is this a line end? If yes, we're also done, after making
13207 sure that a non-default face is extended up to the right
13208 margin of the window. */
13209 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 13210 {
5f5c8ee5
GM
13211 int used_before = row->used[TEXT_AREA];
13212
e74fb0f7
GM
13213 row->ends_in_newline_from_string_p = STRINGP (it->object);
13214
5f5c8ee5
GM
13215 /* Add a space at the end of the line that is used to
13216 display the cursor there. */
13217 append_space (it, 0);
13218
13219 /* Extend the face to the end of the line. */
13220 extend_face_to_end_of_line (it);
13221
13222 /* Make sure we have the position. */
13223 if (used_before == 0)
13224 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13225
13226 /* Consume the line end. This skips over invisible lines. */
cafafe0b 13227 set_iterator_to_next (it, 1);
5f5c8ee5
GM
13228 it->continuation_lines_width = 0;
13229 break;
1c9241f5 13230 }
a2889657 13231
5f5c8ee5
GM
13232 /* Proceed with next display element. Note that this skips
13233 over lines invisible because of selective display. */
cafafe0b 13234 set_iterator_to_next (it, 1);
b1d1124b 13235
5f5c8ee5
GM
13236 /* If we truncate lines, we are done when the last displayed
13237 glyphs reach past the right margin of the window. */
13238 if (it->truncate_lines_p
13239 && (FRAME_WINDOW_P (it->f)
13240 ? (it->current_x >= it->last_visible_x)
13241 : (it->current_x > it->last_visible_x)))
75d13c64 13242 {
5f5c8ee5
GM
13243 /* Maybe add truncation glyphs. */
13244 if (!FRAME_WINDOW_P (it->f))
13245 {
a98b5ed9
GM
13246 int i, n;
13247
13248 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13249 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13250 break;
13251
13252 for (n = row->used[TEXT_AREA]; i < n; ++i)
13253 {
13254 row->used[TEXT_AREA] = i;
13255 produce_special_glyphs (it, IT_TRUNCATION);
13256 }
5f5c8ee5
GM
13257 }
13258
13259 row->truncated_on_right_p = 1;
13260 it->continuation_lines_width = 0;
312246d1 13261 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
13262 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13263 it->hpos = hpos_before;
13264 it->current_x = x_before;
13265 break;
75d13c64 13266 }
a2889657 13267 }
a2889657 13268
5f5c8ee5
GM
13269 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13270 at the left window margin. */
13271 if (it->first_visible_x
13272 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13273 {
13274 if (!FRAME_WINDOW_P (it->f))
13275 insert_left_trunc_glyphs (it);
13276 row->truncated_on_left_p = 1;
13277 }
a2889657 13278
5f5c8ee5
GM
13279 /* If the start of this line is the overlay arrow-position, then
13280 mark this glyph row as the one containing the overlay arrow.
13281 This is clearly a mess with variable size fonts. It would be
13282 better to let it be displayed like cursors under X. */
e24c997d 13283 if (MARKERP (Voverlay_arrow_position)
a2889657 13284 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
13285 && (MATRIX_ROW_START_CHARPOS (row)
13286 == marker_position (Voverlay_arrow_position))
e24c997d 13287 && STRINGP (Voverlay_arrow_string)
a2889657
JB
13288 && ! overlay_arrow_seen)
13289 {
b46952ae 13290 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 13291 if (!FRAME_WINDOW_P (it->f))
c4628384 13292 {
5f5c8ee5
GM
13293 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13294 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13295 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13296 struct glyph *p = row->glyphs[TEXT_AREA];
13297 struct glyph *p2, *end;
13298
13299 /* Copy the arrow glyphs. */
13300 while (glyph < arrow_end)
13301 *p++ = *glyph++;
13302
13303 /* Throw away padding glyphs. */
13304 p2 = p;
13305 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13306 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13307 ++p2;
13308 if (p2 > p)
212e4f87 13309 {
5f5c8ee5
GM
13310 while (p2 < end)
13311 *p++ = *p2++;
13312 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 13313 }
c4628384 13314 }
5f5c8ee5 13315
a2889657 13316 overlay_arrow_seen = 1;
5f5c8ee5 13317 row->overlay_arrow_p = 1;
a2889657
JB
13318 }
13319
5f5c8ee5
GM
13320 /* Compute pixel dimensions of this line. */
13321 compute_line_metrics (it);
13322
13323 /* Remember the position at which this line ends. */
13324 row->end = it->current;
13325
173cbca8 13326 /* Maybe set the cursor. */
5f5c8ee5
GM
13327 if (it->w->cursor.vpos < 0
13328 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
13329 && PT <= MATRIX_ROW_END_CHARPOS (row)
13330 && cursor_row_p (it->w, row))
13331 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13332
13333 /* Highlight trailing whitespace. */
8f897821 13334 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
13335 highlight_trailing_whitespace (it->f, it->glyph_row);
13336
13337 /* Prepare for the next line. This line starts horizontally at (X
13338 HPOS) = (0 0). Vertical positions are incremented. As a
13339 convenience for the caller, IT->glyph_row is set to the next
13340 row to be used. */
13341 it->current_x = it->hpos = 0;
13342 it->current_y += row->height;
13343 ++it->vpos;
13344 ++it->glyph_row;
13345 return row->displays_text_p;
a2889657 13346}
5f5c8ee5
GM
13347
13348
a2889657 13349\f
5f5c8ee5
GM
13350/***********************************************************************
13351 Menu Bar
13352 ***********************************************************************/
13353
13354/* Redisplay the menu bar in the frame for window W.
13355
13356 The menu bar of X frames that don't have X toolkit support is
13357 displayed in a special window W->frame->menu_bar_window.
13358
13359 The menu bar of terminal frames is treated specially as far as
13360 glyph matrices are concerned. Menu bar lines are not part of
13361 windows, so the update is done directly on the frame matrix rows
13362 for the menu bar. */
7ce2c095
RS
13363
13364static void
13365display_menu_bar (w)
13366 struct window *w;
13367{
5f5c8ee5
GM
13368 struct frame *f = XFRAME (WINDOW_FRAME (w));
13369 struct it it;
13370 Lisp_Object items;
8351baf2 13371 int i;
7ce2c095 13372
5f5c8ee5 13373 /* Don't do all this for graphical frames. */
dc937613 13374#ifdef HAVE_NTGUI
d129c4c2
KH
13375 if (!NILP (Vwindow_system))
13376 return;
dc937613 13377#endif
dc937613 13378#ifdef USE_X_TOOLKIT
d3413a53 13379 if (FRAME_X_P (f))
7ce2c095 13380 return;
5f5c8ee5 13381#endif
e0f712ba 13382#ifdef MAC_OS
1a578e9b
AC
13383 if (FRAME_MAC_P (f))
13384 return;
13385#endif
5f5c8ee5
GM
13386
13387#ifdef USE_X_TOOLKIT
13388 xassert (!FRAME_WINDOW_P (f));
52377a47 13389 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
13390 it.first_visible_x = 0;
13391 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13392#else /* not USE_X_TOOLKIT */
13393 if (FRAME_WINDOW_P (f))
13394 {
13395 /* Menu bar lines are displayed in the desired matrix of the
13396 dummy window menu_bar_window. */
13397 struct window *menu_w;
13398 xassert (WINDOWP (f->menu_bar_window));
13399 menu_w = XWINDOW (f->menu_bar_window);
13400 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 13401 MENU_FACE_ID);
5f5c8ee5
GM
13402 it.first_visible_x = 0;
13403 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13404 }
13405 else
13406 {
13407 /* This is a TTY frame, i.e. character hpos/vpos are used as
13408 pixel x/y. */
13409 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 13410 MENU_FACE_ID);
5f5c8ee5
GM
13411 it.first_visible_x = 0;
13412 it.last_visible_x = FRAME_WIDTH (f);
13413 }
13414#endif /* not USE_X_TOOLKIT */
13415
1862a24e
MB
13416 if (! mode_line_inverse_video)
13417 /* Force the menu-bar to be displayed in the default face. */
13418 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13419
5f5c8ee5
GM
13420 /* Clear all rows of the menu bar. */
13421 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13422 {
13423 struct glyph_row *row = it.glyph_row + i;
13424 clear_glyph_row (row);
13425 row->enabled_p = 1;
13426 row->full_width_p = 1;
13427 }
7ce2c095 13428
5f5c8ee5
GM
13429 /* Display all items of the menu bar. */
13430 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 13431 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 13432 {
5f5c8ee5
GM
13433 Lisp_Object string;
13434
13435 /* Stop at nil string. */
a61b7058 13436 string = AREF (items, i + 1);
8351baf2
RS
13437 if (NILP (string))
13438 break;
2d66ad19 13439
5f5c8ee5 13440 /* Remember where item was displayed. */
a61b7058 13441 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 13442
5f5c8ee5
GM
13443 /* Display the item, pad with one space. */
13444 if (it.current_x < it.last_visible_x)
13445 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 13446 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
13447 }
13448
2d66ad19 13449 /* Fill out the line with spaces. */
5f5c8ee5
GM
13450 if (it.current_x < it.last_visible_x)
13451 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 13452
5f5c8ee5
GM
13453 /* Compute the total height of the lines. */
13454 compute_line_metrics (&it);
7ce2c095 13455}
5f5c8ee5
GM
13456
13457
7ce2c095 13458\f
5f5c8ee5
GM
13459/***********************************************************************
13460 Mode Line
13461 ***********************************************************************/
13462
715e84c9
GM
13463/* Redisplay mode lines in the window tree whose root is WINDOW. If
13464 FORCE is non-zero, redisplay mode lines unconditionally.
13465 Otherwise, redisplay only mode lines that are garbaged. Value is
13466 the number of windows whose mode lines were redisplayed. */
a2889657 13467
715e84c9
GM
13468static int
13469redisplay_mode_lines (window, force)
13470 Lisp_Object window;
13471 int force;
13472{
13473 int nwindows = 0;
13474
13475 while (!NILP (window))
13476 {
13477 struct window *w = XWINDOW (window);
13478
13479 if (WINDOWP (w->hchild))
13480 nwindows += redisplay_mode_lines (w->hchild, force);
13481 else if (WINDOWP (w->vchild))
13482 nwindows += redisplay_mode_lines (w->vchild, force);
13483 else if (force
13484 || FRAME_GARBAGED_P (XFRAME (w->frame))
13485 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13486 {
715e84c9
GM
13487 struct text_pos lpoint;
13488 struct buffer *old = current_buffer;
13489
13490 /* Set the window's buffer for the mode line display. */
13491 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13492 set_buffer_internal_1 (XBUFFER (w->buffer));
13493
13494 /* Point refers normally to the selected window. For any
13495 other window, set up appropriate value. */
13496 if (!EQ (window, selected_window))
13497 {
13498 struct text_pos pt;
13499
13500 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13501 if (CHARPOS (pt) < BEGV)
13502 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13503 else if (CHARPOS (pt) > (ZV - 1))
13504 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13505 else
13506 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13507 }
13508
715e84c9
GM
13509 /* Display mode lines. */
13510 clear_glyph_matrix (w->desired_matrix);
13511 if (display_mode_lines (w))
13512 {
13513 ++nwindows;
13514 w->must_be_updated_p = 1;
13515 }
13516
13517 /* Restore old settings. */
715e84c9
GM
13518 set_buffer_internal_1 (old);
13519 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13520 }
13521
13522 window = w->next;
13523 }
13524
13525 return nwindows;
13526}
13527
13528
13529/* Display the mode and/or top line of window W. Value is the number
13530 of mode lines displayed. */
13531
13532static int
5f5c8ee5 13533display_mode_lines (w)
a2889657
JB
13534 struct window *w;
13535{
edd1e654 13536 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 13537 int n = 0;
edd1e654
GM
13538
13539 old_selected_frame = selected_frame;
13540 selected_frame = w->frame;
13541 old_selected_window = selected_window;
13542 XSETWINDOW (selected_window, w);
715e84c9 13543
5f5c8ee5 13544 /* These will be set while the mode line specs are processed. */
aa6d10fa 13545 line_number_displayed = 0;
155ef550 13546 w->column_number_displayed = Qnil;
aa6d10fa 13547
5f5c8ee5 13548 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 13549 {
c96e83bf 13550 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 13551
96d2320f 13552 /* Select mode line face based on the real selected window. */
c96e83bf 13553 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
13554 current_buffer->mode_line_format);
13555 ++n;
13556 }
5f5c8ee5 13557
045dee35 13558 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
13559 {
13560 display_mode_line (w, HEADER_LINE_FACE_ID,
13561 current_buffer->header_line_format);
13562 ++n;
13563 }
13564
edd1e654
GM
13565 selected_frame = old_selected_frame;
13566 selected_window = old_selected_window;
715e84c9 13567 return n;
5f5c8ee5 13568}
03b294dc 13569
03b294dc 13570
5f5c8ee5 13571/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 13572 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
13573 FORMAT is the mode line format to display. Value is the pixel
13574 height of the mode line displayed. */
03b294dc 13575
04612a64 13576static int
5f5c8ee5
GM
13577display_mode_line (w, face_id, format)
13578 struct window *w;
13579 enum face_id face_id;
13580 Lisp_Object format;
13581{
13582 struct it it;
13583 struct face *face;
03b294dc 13584
5f5c8ee5
GM
13585 init_iterator (&it, w, -1, -1, NULL, face_id);
13586 prepare_desired_row (it.glyph_row);
13587
1862a24e
MB
13588 if (! mode_line_inverse_video)
13589 /* Force the mode-line to be displayed in the default face. */
13590 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13591
5f5c8ee5
GM
13592 /* Temporarily make frame's keyboard the current kboard so that
13593 kboard-local variables in the mode_line_format will get the right
13594 values. */
13595 push_frame_kboard (it.f);
c53a1624 13596 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 13597 pop_frame_kboard ();
a2889657 13598
5f5c8ee5
GM
13599 /* Fill up with spaces. */
13600 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13601
13602 compute_line_metrics (&it);
13603 it.glyph_row->full_width_p = 1;
13604 it.glyph_row->mode_line_p = 1;
5f5c8ee5
GM
13605 it.glyph_row->continued_p = 0;
13606 it.glyph_row->truncated_on_left_p = 0;
13607 it.glyph_row->truncated_on_right_p = 0;
13608
13609 /* Make a 3D mode-line have a shadow at its right end. */
13610 face = FACE_FROM_ID (it.f, face_id);
13611 extend_face_to_end_of_line (&it);
13612 if (face->box != FACE_NO_BOX)
d7eb09a0 13613 {
5f5c8ee5
GM
13614 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13615 + it.glyph_row->used[TEXT_AREA] - 1);
13616 last->right_box_line_p = 1;
d7eb09a0 13617 }
04612a64
GM
13618
13619 return it.glyph_row->height;
a2889657
JB
13620}
13621
0fcf414f
RS
13622/* Alist that caches the results of :propertize.
13623 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13624Lisp_Object mode_line_proptrans_alist;
a2889657 13625
5f5c8ee5
GM
13626/* Contribute ELT to the mode line for window IT->w. How it
13627 translates into text depends on its data type.
a2889657 13628
5f5c8ee5 13629 IT describes the display environment in which we display, as usual.
a2889657
JB
13630
13631 DEPTH is the depth in recursion. It is used to prevent
13632 infinite recursion here.
13633
5f5c8ee5
GM
13634 FIELD_WIDTH is the number of characters the display of ELT should
13635 occupy in the mode line, and PRECISION is the maximum number of
13636 characters to display from ELT's representation. See
b8523839 13637 display_string for details.
a2889657 13638
c53a1624
RS
13639 Returns the hpos of the end of the text generated by ELT.
13640
13641 PROPS is a property list to add to any string we encounter.
13642
13643 If RISKY is nonzero, remove (disregard) any properties in any string
13644 we encounter, and ignore :eval and :propertize. */
a2889657
JB
13645
13646static int
c53a1624 13647display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 13648 struct it *it;
a2889657 13649 int depth;
5f5c8ee5 13650 int field_width, precision;
0fcf414f 13651 Lisp_Object elt, props;
c53a1624 13652 int risky;
a2889657 13653{
5f5c8ee5 13654 int n = 0, field, prec;
0fcf414f 13655 int literal = 0;
5f5c8ee5 13656
a2889657
JB
13657 tail_recurse:
13658 if (depth > 10)
13659 goto invalid;
13660
13661 depth++;
13662
0220c518 13663 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
13664 {
13665 case Lisp_String:
13666 {
13667 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 13668 unsigned char c;
ae02e06a 13669 unsigned char *this, *lisp_string;
5f5c8ee5 13670
c53a1624 13671 if (!NILP (props) || risky)
0fcf414f
RS
13672 {
13673 Lisp_Object oprops, aelt;
13674 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
13675
13676 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 13677 {
ae02e06a
RS
13678 /* If the starting string has properties,
13679 merge the specified ones onto the existing ones. */
c53a1624 13680 if (! NILP (oprops) && !risky)
ae02e06a
RS
13681 {
13682 Lisp_Object tem;
13683
13684 oprops = Fcopy_sequence (oprops);
13685 tem = props;
13686 while (CONSP (tem))
13687 {
13688 oprops = Fplist_put (oprops, XCAR (tem),
13689 XCAR (XCDR (tem)));
13690 tem = XCDR (XCDR (tem));
13691 }
13692 props = oprops;
13693 }
13694
0fcf414f
RS
13695 aelt = Fassoc (elt, mode_line_proptrans_alist);
13696 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
13697 {
13698 mode_line_proptrans_alist
13699 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13700 elt = XCAR (aelt);
13701 }
0fcf414f
RS
13702 else
13703 {
adb63af1
RS
13704 Lisp_Object tem;
13705
0fcf414f 13706 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
13707 Fset_text_properties (make_number (0), Flength (elt),
13708 props, elt);
adb63af1 13709 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
13710 mode_line_proptrans_alist
13711 = Fcons (Fcons (elt, props),
13712 mode_line_proptrans_alist);
adb63af1
RS
13713 /* Truncate mode_line_proptrans_alist
13714 to at most 50 elements. */
13715 tem = Fnthcdr (make_number (50),
13716 mode_line_proptrans_alist);
13717 if (! NILP (tem))
13718 XSETCDR (tem, Qnil);
0fcf414f
RS
13719 }
13720 }
13721 }
13722
2051c264 13723 this = SDATA (elt);
ae02e06a
RS
13724 lisp_string = this;
13725
0fcf414f
RS
13726 if (literal)
13727 {
13728 prec = precision - n;
13729 if (frame_title_ptr)
2051c264 13730 n += store_frame_title (SDATA (elt), -1, prec);
0fcf414f
RS
13731 else
13732 n += display_string (NULL, elt, Qnil, 0, 0, it,
13733 0, prec, 0, STRING_MULTIBYTE (elt));
13734
13735 break;
13736 }
13737
5f5c8ee5
GM
13738 while ((precision <= 0 || n < precision)
13739 && *this
13740 && (frame_title_ptr
13741 || it->current_x < it->last_visible_x))
a2889657
JB
13742 {
13743 unsigned char *last = this;
5f5c8ee5
GM
13744
13745 /* Advance to end of string or next format specifier. */
a2889657
JB
13746 while ((c = *this++) != '\0' && c != '%')
13747 ;
5f5c8ee5 13748
a2889657
JB
13749 if (this - 1 != last)
13750 {
5f5c8ee5
GM
13751 /* Output to end of string or up to '%'. Field width
13752 is length of string. Don't output more than
13753 PRECISION allows us. */
d26b89b8 13754 --this;
eaaa65b0
GM
13755
13756 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
13757 if (precision > 0 && prec > precision - n)
13758 prec = precision - n;
13759
d39b6696 13760 if (frame_title_ptr)
d26b89b8 13761 n += store_frame_title (last, 0, prec);
d39b6696 13762 else
eaaa65b0
GM
13763 {
13764 int bytepos = last - lisp_string;
13765 int charpos = string_byte_to_char (elt, bytepos);
13766 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
13767 it, 0, prec, 0,
13768 STRING_MULTIBYTE (elt));
eaaa65b0 13769 }
a2889657
JB
13770 }
13771 else /* c == '%' */
13772 {
5f5c8ee5
GM
13773 unsigned char *percent_position = this;
13774
13775 /* Get the specified minimum width. Zero means
13776 don't pad. */
13777 field = 0;
a2889657 13778 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 13779 field = field * 10 + c - '0';
a2889657 13780
5f5c8ee5
GM
13781 /* Don't pad beyond the total padding allowed. */
13782 if (field_width - n > 0 && field > field_width - n)
13783 field = field_width - n;
a2889657 13784
5f5c8ee5
GM
13785 /* Note that either PRECISION <= 0 or N < PRECISION. */
13786 prec = precision - n;
13787
a2889657 13788 if (c == 'M')
5f5c8ee5 13789 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
13790 Vglobal_mode_string, props,
13791 risky);
a2889657 13792 else if (c != 0)
d39b6696 13793 {
72f62cb5 13794 int multibyte;
ae02e06a
RS
13795 int bytepos, charpos;
13796 unsigned char *spec;
13797
13798 bytepos = percent_position - lisp_string;
13799 charpos = (STRING_MULTIBYTE (elt)
13800 ? string_byte_to_char (elt, bytepos)
13801 : bytepos);
13802
13803 spec
72f62cb5
GM
13804 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13805
d39b6696 13806 if (frame_title_ptr)
5f5c8ee5 13807 n += store_frame_title (spec, field, prec);
d39b6696 13808 else
5f5c8ee5 13809 {
ae02e06a 13810 int nglyphs_before, nwritten;
72f62cb5
GM
13811
13812 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
13813 nwritten = display_string (spec, Qnil, elt,
13814 charpos, 0, it,
13815 field, prec, 0,
13816 multibyte);
5f5c8ee5
GM
13817
13818 /* Assign to the glyphs written above the
13819 string where the `%x' came from, position
13820 of the `%'. */
13821 if (nwritten > 0)
13822 {
13823 struct glyph *glyph
13824 = (it->glyph_row->glyphs[TEXT_AREA]
13825 + nglyphs_before);
13826 int i;
13827
13828 for (i = 0; i < nwritten; ++i)
13829 {
13830 glyph[i].object = elt;
13831 glyph[i].charpos = charpos;
13832 }
13833
13834 n += nwritten;
13835 }
13836 }
d39b6696 13837 }
b8523839
AS
13838 else /* c == 0 */
13839 break;
a2889657
JB
13840 }
13841 }
13842 }
13843 break;
13844
13845 case Lisp_Symbol:
13846 /* A symbol: process the value of the symbol recursively
13847 as if it appeared here directly. Avoid error if symbol void.
13848 Special case: if value of symbol is a string, output the string
13849 literally. */
13850 {
13851 register Lisp_Object tem;
c53a1624
RS
13852
13853 /* If the variable is not marked as risky to set
13854 then its contents are risky to use. */
13855 if (NILP (Fget (elt, Qrisky_local_variable)))
13856 risky = 1;
13857
a2889657 13858 tem = Fboundp (elt);
265a9e55 13859 if (!NILP (tem))
a2889657
JB
13860 {
13861 tem = Fsymbol_value (elt);
13862 /* If value is a string, output that string literally:
13863 don't check for % within it. */
e24c997d 13864 if (STRINGP (tem))
0fcf414f
RS
13865 literal = 1;
13866
13867 if (!EQ (tem, elt))
5f5c8ee5
GM
13868 {
13869 /* Give up right away for nil or t. */
13870 elt = tem;
13871 goto tail_recurse;
13872 }
a2889657
JB
13873 }
13874 }
13875 break;
13876
13877 case Lisp_Cons:
13878 {
13879 register Lisp_Object car, tem;
13880
0fcf414f
RS
13881 /* A cons cell: five distinct cases.
13882 If first element is :eval or :propertize, do something special.
a2889657
JB
13883 If first element is a string or a cons, process all the elements
13884 and effectively concatenate them.
13885 If first element is a negative number, truncate displaying cdr to
13886 at most that many characters. If positive, pad (with spaces)
13887 to at least that many characters.
13888 If first element is a symbol, process the cadr or caddr recursively
13889 according to whether the symbol's value is non-nil or nil. */
9472f927 13890 car = XCAR (elt);
0fcf414f 13891 if (EQ (car, QCeval))
5f5c8ee5
GM
13892 {
13893 /* An element of the form (:eval FORM) means evaluate FORM
13894 and use the result as mode line elements. */
0fcf414f 13895
c53a1624
RS
13896 if (risky)
13897 break;
13898
0fcf414f
RS
13899 if (CONSP (XCDR (elt)))
13900 {
13901 Lisp_Object spec;
13902 spec = safe_eval (XCAR (XCDR (elt)));
13903 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
13904 precision - n, spec, props,
13905 risky);
0fcf414f
RS
13906 }
13907 }
13908 else if (EQ (car, QCpropertize))
13909 {
c53a1624
RS
13910 /* An element of the form (:propertize ELT PROPS...)
13911 means display ELT but applying properties PROPS. */
13912
13913 if (risky)
13914 break;
13915
0fcf414f 13916 if (CONSP (XCDR (elt)))
c53a1624
RS
13917 n += display_mode_element (it, depth, field_width - n,
13918 precision - n, XCAR (XCDR (elt)),
13919 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
13920 }
13921 else if (SYMBOLP (car))
a2889657
JB
13922 {
13923 tem = Fboundp (car);
9472f927 13924 elt = XCDR (elt);
e24c997d 13925 if (!CONSP (elt))
a2889657
JB
13926 goto invalid;
13927 /* elt is now the cdr, and we know it is a cons cell.
13928 Use its car if CAR has a non-nil value. */
265a9e55 13929 if (!NILP (tem))
a2889657
JB
13930 {
13931 tem = Fsymbol_value (car);
265a9e55 13932 if (!NILP (tem))
9472f927
GM
13933 {
13934 elt = XCAR (elt);
13935 goto tail_recurse;
13936 }
a2889657
JB
13937 }
13938 /* Symbol's value is nil (or symbol is unbound)
13939 Get the cddr of the original list
13940 and if possible find the caddr and use that. */
9472f927 13941 elt = XCDR (elt);
265a9e55 13942 if (NILP (elt))
a2889657 13943 break;
e24c997d 13944 else if (!CONSP (elt))
a2889657 13945 goto invalid;
9472f927 13946 elt = XCAR (elt);
a2889657
JB
13947 goto tail_recurse;
13948 }
e24c997d 13949 else if (INTEGERP (car))
a2889657
JB
13950 {
13951 register int lim = XINT (car);
9472f927 13952 elt = XCDR (elt);
a2889657 13953 if (lim < 0)
5f5c8ee5
GM
13954 {
13955 /* Negative int means reduce maximum width. */
13956 if (precision <= 0)
13957 precision = -lim;
13958 else
13959 precision = min (precision, -lim);
13960 }
a2889657
JB
13961 else if (lim > 0)
13962 {
13963 /* Padding specified. Don't let it be more than
13964 current maximum. */
5f5c8ee5
GM
13965 if (precision > 0)
13966 lim = min (precision, lim);
13967
a2889657
JB
13968 /* If that's more padding than already wanted, queue it.
13969 But don't reduce padding already specified even if
13970 that is beyond the current truncation point. */
5f5c8ee5 13971 field_width = max (lim, field_width);
a2889657
JB
13972 }
13973 goto tail_recurse;
13974 }
e24c997d 13975 else if (STRINGP (car) || CONSP (car))
a2889657
JB
13976 {
13977 register int limit = 50;
5f5c8ee5
GM
13978 /* Limit is to protect against circular lists. */
13979 while (CONSP (elt)
13980 && --limit > 0
13981 && (precision <= 0 || n < precision))
a2889657 13982 {
5f5c8ee5 13983 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
13984 precision - n, XCAR (elt),
13985 props, risky);
9472f927 13986 elt = XCDR (elt);
a2889657
JB
13987 }
13988 }
13989 }
13990 break;
13991
13992 default:
13993 invalid:
d39b6696 13994 if (frame_title_ptr)
5f5c8ee5 13995 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 13996 else
5f5c8ee5
GM
13997 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13998 precision - n, 0, 0);
13999 return n;
a2889657
JB
14000 }
14001
5f5c8ee5
GM
14002 /* Pad to FIELD_WIDTH. */
14003 if (field_width > 0 && n < field_width)
14004 {
14005 if (frame_title_ptr)
14006 n += store_frame_title ("", field_width - n, 0);
14007 else
14008 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14009 0, 0, 0);
14010 }
14011
14012 return n;
a2889657 14013}
5f5c8ee5
GM
14014
14015
766525bc
RS
14016/* Write a null-terminated, right justified decimal representation of
14017 the positive integer D to BUF using a minimal field width WIDTH. */
14018
14019static void
14020pint2str (buf, width, d)
14021 register char *buf;
14022 register int width;
14023 register int d;
14024{
14025 register char *p = buf;
14026
14027 if (d <= 0)
5f5c8ee5 14028 *p++ = '0';
766525bc 14029 else
5f5c8ee5 14030 {
766525bc 14031 while (d > 0)
5f5c8ee5 14032 {
766525bc
RS
14033 *p++ = d % 10 + '0';
14034 d /= 10;
5f5c8ee5
GM
14035 }
14036 }
14037
14038 for (width -= (int) (p - buf); width > 0; --width)
14039 *p++ = ' ';
766525bc
RS
14040 *p-- = '\0';
14041 while (p > buf)
5f5c8ee5 14042 {
766525bc
RS
14043 d = *buf;
14044 *buf++ = *p;
14045 *p-- = d;
5f5c8ee5 14046 }
766525bc
RS
14047}
14048
5f5c8ee5 14049/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
14050 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14051 type of CODING_SYSTEM. Return updated pointer into BUF. */
14052
6693a99a 14053static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 14054
1c9241f5
KH
14055static char *
14056decode_mode_spec_coding (coding_system, buf, eol_flag)
14057 Lisp_Object coding_system;
14058 register char *buf;
14059 int eol_flag;
14060{
1e1078d6 14061 Lisp_Object val;
916848d8 14062 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
14063 unsigned char *eol_str;
14064 int eol_str_len;
14065 /* The EOL conversion we are using. */
14066 Lisp_Object eoltype;
1e1078d6 14067
4a09dee0 14068 val = Fget (coding_system, Qcoding_system);
085536c2 14069 eoltype = Qnil;
1c9241f5 14070
4a09dee0 14071 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 14072 {
916848d8
RS
14073 if (multibyte)
14074 *buf++ = '-';
21e989e3 14075 if (eol_flag)
302f2b38 14076 eoltype = eol_mnemonic_undecided;
1e1078d6 14077 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
14078 }
14079 else
14080 {
1e1078d6
RS
14081 Lisp_Object eolvalue;
14082
14083 eolvalue = Fget (coding_system, Qeol_type);
14084
916848d8 14085 if (multibyte)
a61b7058 14086 *buf++ = XFASTINT (AREF (val, 1));
916848d8 14087
1c9241f5
KH
14088 if (eol_flag)
14089 {
1e1078d6
RS
14090 /* The EOL conversion that is normal on this system. */
14091
14092 if (NILP (eolvalue)) /* Not yet decided. */
14093 eoltype = eol_mnemonic_undecided;
14094 else if (VECTORP (eolvalue)) /* Not yet decided. */
14095 eoltype = eol_mnemonic_undecided;
14096 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14097 eoltype = (XFASTINT (eolvalue) == 0
14098 ? eol_mnemonic_unix
14099 : (XFASTINT (eolvalue) == 1
14100 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
14101 }
14102 }
5f5c8ee5 14103
302f2b38
EZ
14104 if (eol_flag)
14105 {
14106 /* Mention the EOL conversion if it is not the usual one. */
14107 if (STRINGP (eoltype))
14108 {
2051c264
GM
14109 eol_str = SDATA (eoltype);
14110 eol_str_len = SBYTES (eoltype);
302f2b38 14111 }
f30b3499
KH
14112 else if (INTEGERP (eoltype)
14113 && CHAR_VALID_P (XINT (eoltype), 0))
14114 {
4a09dee0 14115 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 14116 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 14117 }
302f2b38
EZ
14118 else
14119 {
14120 eol_str = invalid_eol_type;
14121 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 14122 }
f30b3499 14123 bcopy (eol_str, buf, eol_str_len);
302f2b38 14124 buf += eol_str_len;
1c9241f5 14125 }
302f2b38 14126
1c9241f5
KH
14127 return buf;
14128}
14129
a2889657 14130/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
14131 generated by character C. PRECISION >= 0 means don't return a
14132 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
14133 string returned with spaces to that value. Return 1 in *MULTIBYTE
14134 if the result is multibyte text. */
a2889657 14135
11e82b76
JB
14136static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14137
a2889657 14138static char *
72f62cb5 14139decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 14140 struct window *w;
68c45bf0 14141 register int c;
5f5c8ee5 14142 int field_width, precision;
72f62cb5 14143 int *multibyte;
a2889657 14144{
0b67772d 14145 Lisp_Object obj;
5f5c8ee5
GM
14146 struct frame *f = XFRAME (WINDOW_FRAME (w));
14147 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 14148 struct buffer *b = XBUFFER (w->buffer);
a2889657 14149
0b67772d 14150 obj = Qnil;
72f62cb5 14151 *multibyte = 0;
a2889657
JB
14152
14153 switch (c)
14154 {
1af9f229
RS
14155 case '*':
14156 if (!NILP (b->read_only))
14157 return "%";
14158 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14159 return "*";
14160 return "-";
14161
14162 case '+':
14163 /* This differs from %* only for a modified read-only buffer. */
14164 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14165 return "*";
14166 if (!NILP (b->read_only))
14167 return "%";
14168 return "-";
14169
14170 case '&':
14171 /* This differs from %* in ignoring read-only-ness. */
14172 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14173 return "*";
14174 return "-";
14175
14176 case '%':
14177 return "%";
14178
14179 case '[':
14180 {
14181 int i;
14182 char *p;
14183
14184 if (command_loop_level > 5)
14185 return "[[[... ";
14186 p = decode_mode_spec_buf;
14187 for (i = 0; i < command_loop_level; i++)
14188 *p++ = '[';
14189 *p = 0;
14190 return decode_mode_spec_buf;
14191 }
14192
14193 case ']':
14194 {
14195 int i;
14196 char *p;
14197
14198 if (command_loop_level > 5)
14199 return " ...]]]";
14200 p = decode_mode_spec_buf;
14201 for (i = 0; i < command_loop_level; i++)
14202 *p++ = ']';
14203 *p = 0;
14204 return decode_mode_spec_buf;
14205 }
14206
14207 case '-':
14208 {
1af9f229 14209 register int i;
5f5c8ee5
GM
14210
14211 /* Let lots_of_dashes be a string of infinite length. */
14212 if (field_width <= 0
14213 || field_width > sizeof (lots_of_dashes))
1af9f229 14214 {
5f5c8ee5
GM
14215 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14216 decode_mode_spec_buf[i] = '-';
14217 decode_mode_spec_buf[i] = '\0';
14218 return decode_mode_spec_buf;
1af9f229 14219 }
5f5c8ee5
GM
14220 else
14221 return lots_of_dashes;
1af9f229
RS
14222 }
14223
a2889657 14224 case 'b':
d39b6696 14225 obj = b->name;
a2889657
JB
14226 break;
14227
1af9f229
RS
14228 case 'c':
14229 {
14230 int col = current_column ();
ac90c44f 14231 w->column_number_displayed = make_number (col);
5f5c8ee5 14232 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
14233 return decode_mode_spec_buf;
14234 }
14235
14236 case 'F':
14237 /* %F displays the frame name. */
5f5c8ee5 14238 if (!NILP (f->title))
2051c264 14239 return (char *) SDATA (f->title);
fd8ff63d 14240 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 14241 return (char *) SDATA (f->name);
9c6da96f 14242 return "Emacs";
1af9f229 14243
a2889657 14244 case 'f':
d39b6696 14245 obj = b->filename;
a2889657
JB
14246 break;
14247
aa6d10fa
RS
14248 case 'l':
14249 {
12adba34
RS
14250 int startpos = XMARKER (w->start)->charpos;
14251 int startpos_byte = marker_byte_position (w->start);
14252 int line, linepos, linepos_byte, topline;
aa6d10fa 14253 int nlines, junk;
aa6d10fa
RS
14254 int height = XFASTINT (w->height);
14255
14256 /* If we decided that this buffer isn't suitable for line numbers,
14257 don't forget that too fast. */
14258 if (EQ (w->base_line_pos, w->buffer))
766525bc 14259 goto no_value;
5300fd39
RS
14260 /* But do forget it, if the window shows a different buffer now. */
14261 else if (BUFFERP (w->base_line_pos))
14262 w->base_line_pos = Qnil;
aa6d10fa
RS
14263
14264 /* If the buffer is very big, don't waste time. */
37d034d3 14265 if (INTEGERP (Vline_number_display_limit)
090703f4 14266 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
14267 {
14268 w->base_line_pos = Qnil;
14269 w->base_line_number = Qnil;
766525bc 14270 goto no_value;
aa6d10fa
RS
14271 }
14272
14273 if (!NILP (w->base_line_number)
14274 && !NILP (w->base_line_pos)
12adba34 14275 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
14276 {
14277 line = XFASTINT (w->base_line_number);
14278 linepos = XFASTINT (w->base_line_pos);
12adba34 14279 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
14280 }
14281 else
14282 {
14283 line = 1;
d39b6696 14284 linepos = BUF_BEGV (b);
12adba34 14285 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
14286 }
14287
14288 /* Count lines from base line to window start position. */
12adba34
RS
14289 nlines = display_count_lines (linepos, linepos_byte,
14290 startpos_byte,
14291 startpos, &junk);
aa6d10fa
RS
14292
14293 topline = nlines + line;
14294
14295 /* Determine a new base line, if the old one is too close
14296 or too far away, or if we did not have one.
14297 "Too close" means it's plausible a scroll-down would
14298 go back past it. */
d39b6696 14299 if (startpos == BUF_BEGV (b))
aa6d10fa 14300 {
ac90c44f
GM
14301 w->base_line_number = make_number (topline);
14302 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
14303 }
14304 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 14305 || linepos == BUF_BEGV (b))
aa6d10fa 14306 {
d39b6696 14307 int limit = BUF_BEGV (b);
12adba34 14308 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 14309 int position;
5d121aec 14310 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
14311
14312 if (startpos - distance > limit)
12adba34
RS
14313 {
14314 limit = startpos - distance;
14315 limit_byte = CHAR_TO_BYTE (limit);
14316 }
aa6d10fa 14317
12adba34
RS
14318 nlines = display_count_lines (startpos, startpos_byte,
14319 limit_byte,
14320 - (height * 2 + 30),
aa6d10fa
RS
14321 &position);
14322 /* If we couldn't find the lines we wanted within
5d121aec 14323 line_number_display_limit_width chars per line,
aa6d10fa 14324 give up on line numbers for this window. */
12adba34 14325 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
14326 {
14327 w->base_line_pos = w->buffer;
14328 w->base_line_number = Qnil;
766525bc 14329 goto no_value;
aa6d10fa
RS
14330 }
14331
ac90c44f
GM
14332 w->base_line_number = make_number (topline - nlines);
14333 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
14334 }
14335
14336 /* Now count lines from the start pos to point. */
12adba34
RS
14337 nlines = display_count_lines (startpos, startpos_byte,
14338 PT_BYTE, PT, &junk);
aa6d10fa
RS
14339
14340 /* Record that we did display the line number. */
14341 line_number_displayed = 1;
14342
14343 /* Make the string to show. */
5f5c8ee5 14344 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 14345 return decode_mode_spec_buf;
766525bc
RS
14346 no_value:
14347 {
14348 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
14349 int pad = field_width - 2;
14350 while (pad-- > 0)
14351 *p++ = ' ';
14352 *p++ = '?';
b357b9d4
KR
14353 *p++ = '?';
14354 *p = '\0';
766525bc
RS
14355 return decode_mode_spec_buf;
14356 }
aa6d10fa
RS
14357 }
14358 break;
14359
a2889657 14360 case 'm':
d39b6696 14361 obj = b->mode_name;
a2889657
JB
14362 break;
14363
14364 case 'n':
d39b6696 14365 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
14366 return " Narrow";
14367 break;
14368
a2889657
JB
14369 case 'p':
14370 {
14371 int pos = marker_position (w->start);
d39b6696 14372 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 14373
d39b6696 14374 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 14375 {
d39b6696 14376 if (pos <= BUF_BEGV (b))
a2889657
JB
14377 return "All";
14378 else
14379 return "Bottom";
14380 }
d39b6696 14381 else if (pos <= BUF_BEGV (b))
a2889657
JB
14382 return "Top";
14383 else
14384 {
3c7d31b9
RS
14385 if (total > 1000000)
14386 /* Do it differently for a large value, to avoid overflow. */
14387 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14388 else
14389 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
14390 /* We can't normally display a 3-digit number,
14391 so get us a 2-digit number that is close. */
14392 if (total == 100)
14393 total = 99;
14394 sprintf (decode_mode_spec_buf, "%2d%%", total);
14395 return decode_mode_spec_buf;
14396 }
14397 }
14398
8ffcb79f
RS
14399 /* Display percentage of size above the bottom of the screen. */
14400 case 'P':
14401 {
14402 int toppos = marker_position (w->start);
d39b6696
KH
14403 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14404 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 14405
d39b6696 14406 if (botpos >= BUF_ZV (b))
8ffcb79f 14407 {
d39b6696 14408 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
14409 return "All";
14410 else
14411 return "Bottom";
14412 }
14413 else
14414 {
3c7d31b9
RS
14415 if (total > 1000000)
14416 /* Do it differently for a large value, to avoid overflow. */
14417 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14418 else
14419 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
14420 /* We can't normally display a 3-digit number,
14421 so get us a 2-digit number that is close. */
14422 if (total == 100)
14423 total = 99;
d39b6696 14424 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
14425 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14426 else
14427 sprintf (decode_mode_spec_buf, "%2d%%", total);
14428 return decode_mode_spec_buf;
14429 }
14430 }
14431
1af9f229
RS
14432 case 's':
14433 /* status of process */
14434 obj = Fget_buffer_process (w->buffer);
14435 if (NILP (obj))
14436 return "no process";
14437#ifdef subprocesses
14438 obj = Fsymbol_name (Fprocess_status (obj));
14439#endif
14440 break;
d39b6696 14441
1af9f229
RS
14442 case 't': /* indicate TEXT or BINARY */
14443#ifdef MODE_LINE_BINARY_TEXT
14444 return MODE_LINE_BINARY_TEXT (b);
14445#else
14446 return "T";
14447#endif
1c9241f5
KH
14448
14449 case 'z':
14450 /* coding-system (not including end-of-line format) */
14451 case 'Z':
14452 /* coding-system (including end-of-line type) */
14453 {
14454 int eol_flag = (c == 'Z');
539b4d41 14455 char *p = decode_mode_spec_buf;
1c9241f5 14456
d30e754b 14457 if (! FRAME_WINDOW_P (f))
1c9241f5 14458 {
11c52c4f
RS
14459 /* No need to mention EOL here--the terminal never needs
14460 to do EOL conversion. */
14461 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14462 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 14463 }
f13c925f 14464 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 14465 p, eol_flag);
f13c925f 14466
11c52c4f 14467#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
14468#ifdef subprocesses
14469 obj = Fget_buffer_process (Fcurrent_buffer ());
14470 if (PROCESSP (obj))
14471 {
14472 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14473 p, eol_flag);
14474 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14475 p, eol_flag);
14476 }
14477#endif /* subprocesses */
11c52c4f 14478#endif /* 0 */
1c9241f5
KH
14479 *p = 0;
14480 return decode_mode_spec_buf;
14481 }
a2889657 14482 }
d39b6696 14483
e24c997d 14484 if (STRINGP (obj))
72f62cb5
GM
14485 {
14486 *multibyte = STRING_MULTIBYTE (obj);
2051c264 14487 return (char *) SDATA (obj);
72f62cb5 14488 }
a2889657
JB
14489 else
14490 return "";
14491}
5f5c8ee5
GM
14492
14493
12adba34
RS
14494/* Count up to COUNT lines starting from START / START_BYTE.
14495 But don't go beyond LIMIT_BYTE.
14496 Return the number of lines thus found (always nonnegative).
59b49f63 14497
12adba34 14498 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
14499
14500static int
12adba34
RS
14501display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14502 int start, start_byte, limit_byte, count;
14503 int *byte_pos_ptr;
59b49f63 14504{
59b49f63
RS
14505 register unsigned char *cursor;
14506 unsigned char *base;
14507
14508 register int ceiling;
14509 register unsigned char *ceiling_addr;
12adba34 14510 int orig_count = count;
59b49f63
RS
14511
14512 /* If we are not in selective display mode,
14513 check only for newlines. */
12adba34
RS
14514 int selective_display = (!NILP (current_buffer->selective_display)
14515 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
14516
14517 if (count > 0)
12adba34
RS
14518 {
14519 while (start_byte < limit_byte)
14520 {
14521 ceiling = BUFFER_CEILING_OF (start_byte);
14522 ceiling = min (limit_byte - 1, ceiling);
14523 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14524 base = (cursor = BYTE_POS_ADDR (start_byte));
14525 while (1)
14526 {
14527 if (selective_display)
14528 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14529 ;
14530 else
14531 while (*cursor != '\n' && ++cursor != ceiling_addr)
14532 ;
14533
14534 if (cursor != ceiling_addr)
14535 {
14536 if (--count == 0)
14537 {
14538 start_byte += cursor - base + 1;
14539 *byte_pos_ptr = start_byte;
14540 return orig_count;
14541 }
14542 else
14543 if (++cursor == ceiling_addr)
14544 break;
14545 }
14546 else
14547 break;
14548 }
14549 start_byte += cursor - base;
14550 }
14551 }
59b49f63
RS
14552 else
14553 {
12adba34
RS
14554 while (start_byte > limit_byte)
14555 {
14556 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14557 ceiling = max (limit_byte, ceiling);
14558 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14559 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
14560 while (1)
14561 {
12adba34
RS
14562 if (selective_display)
14563 while (--cursor != ceiling_addr
14564 && *cursor != '\n' && *cursor != 015)
14565 ;
14566 else
14567 while (--cursor != ceiling_addr && *cursor != '\n')
14568 ;
14569
59b49f63
RS
14570 if (cursor != ceiling_addr)
14571 {
14572 if (++count == 0)
14573 {
12adba34
RS
14574 start_byte += cursor - base + 1;
14575 *byte_pos_ptr = start_byte;
14576 /* When scanning backwards, we should
14577 not count the newline posterior to which we stop. */
14578 return - orig_count - 1;
59b49f63
RS
14579 }
14580 }
14581 else
14582 break;
14583 }
12adba34
RS
14584 /* Here we add 1 to compensate for the last decrement
14585 of CURSOR, which took it past the valid range. */
14586 start_byte += cursor - base + 1;
59b49f63
RS
14587 }
14588 }
14589
12adba34 14590 *byte_pos_ptr = limit_byte;
aa6d10fa 14591
12adba34
RS
14592 if (count < 0)
14593 return - orig_count + count;
14594 return orig_count - count;
aa6d10fa 14595
12adba34 14596}
a2889657 14597
a2889657 14598
5f5c8ee5
GM
14599\f
14600/***********************************************************************
14601 Displaying strings
14602 ***********************************************************************/
278feba9 14603
5f5c8ee5 14604/* Display a NUL-terminated string, starting with index START.
a3788d53 14605
5f5c8ee5
GM
14606 If STRING is non-null, display that C string. Otherwise, the Lisp
14607 string LISP_STRING is displayed.
a2889657 14608
5f5c8ee5
GM
14609 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14610 FACE_STRING. Display STRING or LISP_STRING with the face at
14611 FACE_STRING_POS in FACE_STRING:
a2889657 14612
5f5c8ee5
GM
14613 Display the string in the environment given by IT, but use the
14614 standard display table, temporarily.
a3788d53 14615
5f5c8ee5
GM
14616 FIELD_WIDTH is the minimum number of output glyphs to produce.
14617 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14618 with spaces. If STRING has more characters, more than FIELD_WIDTH
14619 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14620
14621 PRECISION is the maximum number of characters to output from
14622 STRING. PRECISION < 0 means don't truncate the string.
a2889657 14623
5f5c8ee5 14624 This is roughly equivalent to printf format specifiers:
a2889657 14625
5f5c8ee5
GM
14626 FIELD_WIDTH PRECISION PRINTF
14627 ----------------------------------------
14628 -1 -1 %s
14629 -1 10 %.10s
14630 10 -1 %10s
14631 20 10 %20.10s
a2889657 14632
5f5c8ee5
GM
14633 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14634 display them, and < 0 means obey the current buffer's value of
14635 enable_multibyte_characters.
278feba9 14636
5f5c8ee5 14637 Value is the number of glyphs produced. */
b1d1124b 14638
5f5c8ee5
GM
14639static int
14640display_string (string, lisp_string, face_string, face_string_pos,
14641 start, it, field_width, precision, max_x, multibyte)
14642 unsigned char *string;
14643 Lisp_Object lisp_string;
68c45bf0
PE
14644 Lisp_Object face_string;
14645 int face_string_pos;
5f5c8ee5
GM
14646 int start;
14647 struct it *it;
14648 int field_width, precision, max_x;
14649 int multibyte;
14650{
14651 int hpos_at_start = it->hpos;
14652 int saved_face_id = it->face_id;
14653 struct glyph_row *row = it->glyph_row;
14654
14655 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 14656 with index START. */
5f5c8ee5
GM
14657 reseat_to_string (it, string, lisp_string, start,
14658 precision, field_width, multibyte);
14659
14660 /* If displaying STRING, set up the face of the iterator
14661 from LISP_STRING, if that's given. */
14662 if (STRINGP (face_string))
14663 {
14664 int endptr;
14665 struct face *face;
14666
14667 it->face_id
14668 = face_at_string_position (it->w, face_string, face_string_pos,
14669 0, it->region_beg_charpos,
14670 it->region_end_charpos,
5de7c6f2 14671 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
14672 face = FACE_FROM_ID (it->f, it->face_id);
14673 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 14674 }
a2889657 14675
5f5c8ee5
GM
14676 /* Set max_x to the maximum allowed X position. Don't let it go
14677 beyond the right edge of the window. */
14678 if (max_x <= 0)
14679 max_x = it->last_visible_x;
14680 else
14681 max_x = min (max_x, it->last_visible_x);
efc63ef0 14682
5f5c8ee5
GM
14683 /* Skip over display elements that are not visible. because IT->w is
14684 hscrolled. */
14685 if (it->current_x < it->first_visible_x)
14686 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14687 MOVE_TO_POS | MOVE_TO_X);
a2889657 14688
5f5c8ee5
GM
14689 row->ascent = it->max_ascent;
14690 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14691 row->phys_ascent = it->max_phys_ascent;
14692 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 14693
5f5c8ee5
GM
14694 /* This condition is for the case that we are called with current_x
14695 past last_visible_x. */
14696 while (it->current_x < max_x)
a2889657 14697 {
5f5c8ee5 14698 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 14699
5f5c8ee5
GM
14700 /* Get the next display element. */
14701 if (!get_next_display_element (it))
90adcf20 14702 break;
1c9241f5 14703
5f5c8ee5
GM
14704 /* Produce glyphs. */
14705 x_before = it->current_x;
14706 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14707 PRODUCE_GLYPHS (it);
90adcf20 14708
5f5c8ee5
GM
14709 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14710 i = 0;
14711 x = x_before;
14712 while (i < nglyphs)
a2889657 14713 {
5f5c8ee5
GM
14714 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14715
14716 if (!it->truncate_lines_p
14717 && x + glyph->pixel_width > max_x)
14718 {
14719 /* End of continued line or max_x reached. */
37be86f2
KH
14720 if (CHAR_GLYPH_PADDING_P (*glyph))
14721 {
14722 /* A wide character is unbreakable. */
14723 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14724 it->current_x = x_before;
14725 }
14726 else
14727 {
14728 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14729 it->current_x = x;
14730 }
5f5c8ee5
GM
14731 break;
14732 }
14733 else if (x + glyph->pixel_width > it->first_visible_x)
14734 {
14735 /* Glyph is at least partially visible. */
14736 ++it->hpos;
14737 if (x < it->first_visible_x)
14738 it->glyph_row->x = x - it->first_visible_x;
14739 }
14740 else
a2889657 14741 {
5f5c8ee5
GM
14742 /* Glyph is off the left margin of the display area.
14743 Should not happen. */
14744 abort ();
a2889657 14745 }
5f5c8ee5
GM
14746
14747 row->ascent = max (row->ascent, it->max_ascent);
14748 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14749 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14750 row->phys_height = max (row->phys_height,
14751 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14752 x += glyph->pixel_width;
14753 ++i;
a2889657 14754 }
5f5c8ee5
GM
14755
14756 /* Stop if max_x reached. */
14757 if (i < nglyphs)
14758 break;
14759
14760 /* Stop at line ends. */
14761 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 14762 {
5f5c8ee5
GM
14763 it->continuation_lines_width = 0;
14764 break;
a2889657 14765 }
1c9241f5 14766
cafafe0b 14767 set_iterator_to_next (it, 1);
a688bb24 14768
5f5c8ee5
GM
14769 /* Stop if truncating at the right edge. */
14770 if (it->truncate_lines_p
14771 && it->current_x >= it->last_visible_x)
14772 {
14773 /* Add truncation mark, but don't do it if the line is
14774 truncated at a padding space. */
14775 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 14776 {
5f5c8ee5 14777 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
14778 {
14779 int i, n;
14780
9299cb15 14781 if (it->current_x > it->last_visible_x)
37be86f2 14782 {
9299cb15
KH
14783 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14784 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14785 break;
14786 for (n = row->used[TEXT_AREA]; i < n; ++i)
14787 {
14788 row->used[TEXT_AREA] = i;
14789 produce_special_glyphs (it, IT_TRUNCATION);
14790 }
37be86f2 14791 }
9299cb15 14792 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 14793 }
5f5c8ee5 14794 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 14795 }
5f5c8ee5 14796 break;
1c9241f5 14797 }
a2889657
JB
14798 }
14799
5f5c8ee5
GM
14800 /* Maybe insert a truncation at the left. */
14801 if (it->first_visible_x
14802 && IT_CHARPOS (*it) > 0)
a2889657 14803 {
5f5c8ee5
GM
14804 if (!FRAME_WINDOW_P (it->f))
14805 insert_left_trunc_glyphs (it);
14806 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
14807 }
14808
5f5c8ee5
GM
14809 it->face_id = saved_face_id;
14810
14811 /* Value is number of columns displayed. */
14812 return it->hpos - hpos_at_start;
14813}
a2889657 14814
a2889657 14815
a2889657 14816\f
3b6b6db7 14817/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
14818 appears as an element of LIST or as the car of an element of LIST.
14819 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
14820 way, and return 1/2 if any element of PROPVAL is found in LIST.
14821 Otherwise return 0. This function cannot quit.
14822 The return value is 2 if the text is invisible but with an ellipsis
14823 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
14824
14825int
14826invisible_p (propval, list)
14827 register Lisp_Object propval;
14828 Lisp_Object list;
14829{
3b6b6db7
SM
14830 register Lisp_Object tail, proptail;
14831
14832 for (tail = list; CONSP (tail); tail = XCDR (tail))
14833 {
14834 register Lisp_Object tem;
14835 tem = XCAR (tail);
14836 if (EQ (propval, tem))
14837 return 1;
14838 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14839 return NILP (XCDR (tem)) ? 1 : 2;
14840 }
14841
14842 if (CONSP (propval))
14843 {
14844 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14845 {
14846 Lisp_Object propelt;
14847 propelt = XCAR (proptail);
14848 for (tail = list; CONSP (tail); tail = XCDR (tail))
14849 {
14850 register Lisp_Object tem;
14851 tem = XCAR (tail);
14852 if (EQ (propelt, tem))
14853 return 1;
14854 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14855 return NILP (XCDR (tem)) ? 1 : 2;
14856 }
14857 }
14858 }
14859
14860 return 0;
642eefc6 14861}
5f5c8ee5 14862
642eefc6 14863\f
5f5c8ee5
GM
14864/***********************************************************************
14865 Initialization
14866 ***********************************************************************/
14867
a2889657
JB
14868void
14869syms_of_xdisp ()
14870{
c6e89d6c
GM
14871 Vwith_echo_area_save_vector = Qnil;
14872 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 14873
c6e89d6c
GM
14874 Vmessage_stack = Qnil;
14875 staticpro (&Vmessage_stack);
14876
735c094c 14877 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 14878 staticpro (&Qinhibit_redisplay);
735c094c 14879
b14bc55e
RS
14880 message_dolog_marker1 = Fmake_marker ();
14881 staticpro (&message_dolog_marker1);
14882 message_dolog_marker2 = Fmake_marker ();
14883 staticpro (&message_dolog_marker2);
14884 message_dolog_marker3 = Fmake_marker ();
14885 staticpro (&message_dolog_marker3);
14886
5f5c8ee5 14887#if GLYPH_DEBUG
7d4cc828 14888 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
14889 defsubr (&Sdump_glyph_matrix);
14890 defsubr (&Sdump_glyph_row);
e037b9ec 14891 defsubr (&Sdump_tool_bar_row);
62397849 14892 defsubr (&Strace_redisplay);
bf9249e3 14893 defsubr (&Strace_to_stderr);
5f5c8ee5 14894#endif
99a5de87 14895#ifdef HAVE_WINDOW_SYSTEM
57c28064 14896 defsubr (&Stool_bar_lines_needed);
99a5de87 14897#endif
5f5c8ee5 14898
cf074754
RS
14899 staticpro (&Qmenu_bar_update_hook);
14900 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14901
d46fb96a 14902 staticpro (&Qoverriding_terminal_local_map);
7079aefa 14903 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 14904
399164b4
KH
14905 staticpro (&Qoverriding_local_map);
14906 Qoverriding_local_map = intern ("overriding-local-map");
14907
75c43375
RS
14908 staticpro (&Qwindow_scroll_functions);
14909 Qwindow_scroll_functions = intern ("window-scroll-functions");
14910
e0bfbde6
RS
14911 staticpro (&Qredisplay_end_trigger_functions);
14912 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 14913
2e54982e
RS
14914 staticpro (&Qinhibit_point_motion_hooks);
14915 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14916
9499d71b
GM
14917 QCdata = intern (":data");
14918 staticpro (&QCdata);
5f5c8ee5 14919 Qdisplay = intern ("display");
f3751a65 14920 staticpro (&Qdisplay);
5f5c8ee5
GM
14921 Qspace_width = intern ("space-width");
14922 staticpro (&Qspace_width);
5f5c8ee5
GM
14923 Qraise = intern ("raise");
14924 staticpro (&Qraise);
14925 Qspace = intern ("space");
14926 staticpro (&Qspace);
f3751a65
GM
14927 Qmargin = intern ("margin");
14928 staticpro (&Qmargin);
5f5c8ee5 14929 Qleft_margin = intern ("left-margin");
f3751a65 14930 staticpro (&Qleft_margin);
5f5c8ee5 14931 Qright_margin = intern ("right-margin");
f3751a65 14932 staticpro (&Qright_margin);
5f5c8ee5
GM
14933 Qalign_to = intern ("align-to");
14934 staticpro (&Qalign_to);
14935 QCalign_to = intern (":align-to");
14936 staticpro (&QCalign_to);
5f5c8ee5
GM
14937 Qrelative_width = intern ("relative-width");
14938 staticpro (&Qrelative_width);
14939 QCrelative_width = intern (":relative-width");
14940 staticpro (&QCrelative_width);
14941 QCrelative_height = intern (":relative-height");
14942 staticpro (&QCrelative_height);
14943 QCeval = intern (":eval");
14944 staticpro (&QCeval);
0fcf414f
RS
14945 QCpropertize = intern (":propertize");
14946 staticpro (&QCpropertize);
d3acf96b 14947 Qwhen = intern ("when");
f3751a65 14948 staticpro (&Qwhen);
886bd6f2
GM
14949 QCfile = intern (":file");
14950 staticpro (&QCfile);
5f5c8ee5
GM
14951 Qfontified = intern ("fontified");
14952 staticpro (&Qfontified);
14953 Qfontification_functions = intern ("fontification-functions");
14954 staticpro (&Qfontification_functions);
5f5c8ee5
GM
14955 Qtrailing_whitespace = intern ("trailing-whitespace");
14956 staticpro (&Qtrailing_whitespace);
14957 Qimage = intern ("image");
14958 staticpro (&Qimage);
ad4f174e
GM
14959 Qmessage_truncate_lines = intern ("message-truncate-lines");
14960 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
14961 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14962 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
14963 Qgrow_only = intern ("grow-only");
14964 staticpro (&Qgrow_only);
e1477f43
GM
14965 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14966 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
14967 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14968 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
14969 Qposition = intern ("position");
14970 staticpro (&Qposition);
14971 Qbuffer_position = intern ("buffer-position");
14972 staticpro (&Qbuffer_position);
14973 Qobject = intern ("object");
14974 staticpro (&Qobject);
c53a1624
RS
14975 Qrisky_local_variable = intern ("risky-local-variable");
14976 staticpro (&Qrisky_local_variable);
5f5c8ee5 14977
7033d6df
RS
14978 list_of_error = Fcons (intern ("error"), Qnil);
14979 staticpro (&list_of_error);
14980
a2889657
JB
14981 last_arrow_position = Qnil;
14982 last_arrow_string = Qnil;
f3751a65
GM
14983 staticpro (&last_arrow_position);
14984 staticpro (&last_arrow_string);
c6e89d6c
GM
14985
14986 echo_buffer[0] = echo_buffer[1] = Qnil;
14987 staticpro (&echo_buffer[0]);
14988 staticpro (&echo_buffer[1]);
14989
14990 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14991 staticpro (&echo_area_buffer[0]);
14992 staticpro (&echo_area_buffer[1]);
a2889657 14993
6a94510a
GM
14994 Vmessages_buffer_name = build_string ("*Messages*");
14995 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
14996
14997 mode_line_proptrans_alist = Qnil;
14998 staticpro (&mode_line_proptrans_alist);
6a94510a 14999
7ee72033
MB
15000 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15001 doc: /* Non-nil means highlight trailing whitespace.
228299fa 15002The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
15003 Vshow_trailing_whitespace = Qnil;
15004
7ee72033
MB
15005 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15006 doc: /* Non-nil means don't actually do any redisplay.
228299fa 15007This is used for internal purposes. */);
735c094c
KH
15008 Vinhibit_redisplay = Qnil;
15009
7ee72033
MB
15010 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15011 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
15012 Vglobal_mode_string = Qnil;
15013
7ee72033
MB
15014 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15015 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
15016This must be the beginning of a line in order to work.
15017See also `overlay-arrow-string'. */);
a2889657
JB
15018 Voverlay_arrow_position = Qnil;
15019
7ee72033
MB
15020 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15021 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
a2889657
JB
15022 Voverlay_arrow_string = Qnil;
15023
7ee72033
MB
15024 DEFVAR_INT ("scroll-step", &scroll_step,
15025 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
15026If that fails to bring point back on frame, point is centered instead.
15027If this is zero, point is always centered after it moves off frame.
15028If you want scrolling to always be a line at a time, you should set
15029`scroll-conservatively' to a large value rather than set this to 1. */);
15030
7ee72033
MB
15031 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15032 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
15033A value of zero means to scroll the text to center point vertically
15034in the window. */);
0789adb2
RS
15035 scroll_conservatively = 0;
15036
7ee72033
MB
15037 DEFVAR_INT ("scroll-margin", &scroll_margin,
15038 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
15039Recenter the window whenever point gets within this many lines
15040of the top or bottom of the window. */);
9afd2168
RS
15041 scroll_margin = 0;
15042
5f5c8ee5 15043#if GLYPH_DEBUG
7ee72033 15044 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 15045#endif
a2889657
JB
15046
15047 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
15048 &truncate_partial_width_windows,
15049 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
15050 truncate_partial_width_windows = 1;
15051
7ee72033
MB
15052 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15053 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa
GM
15054Any other value means to use the appropriate face, `mode-line',
15055`header-line', or `menu' respectively.
15056
15057This variable is deprecated; please change the above faces instead. */);
1862a24e 15058 mode_line_inverse_video = 1;
aa6d10fa 15059
7ee72033
MB
15060 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15061 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
15062If the buffer is bigger than this, the line number does not appear
15063in the mode line. A value of nil means no limit. */);
090703f4 15064 Vline_number_display_limit = Qnil;
fba9ce76 15065
090703f4 15066 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
15067 &line_number_display_limit_width,
15068 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
15069If the average length of the lines near point is bigger than this, then the
15070line number may be omitted from the mode line. */);
5d121aec
KH
15071 line_number_display_limit_width = 200;
15072
7ee72033
MB
15073 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15074 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 15075 highlight_nonselected_windows = 0;
d39b6696 15076
7ee72033
MB
15077 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15078 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
15079Minibuffer-only frames don't count, but iconified frames do.
15080This variable is not guaranteed to be accurate except while processing
15081`frame-title-format' and `icon-title-format'. */);
15082
7ee72033
MB
15083 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15084 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
15085\(Assuming the window manager supports this feature.)
15086This variable has the same structure as `mode-line-format' (which see),
15087and is used only on frames for which no explicit name has been set
15088\(see `modify-frame-parameters'). */);
7ee72033
MB
15089 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15090 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
15091\(Assuming the window manager supports this feature.)
15092This variable has the same structure as `mode-line-format' (which see),
15093and is used only on frames for which no explicit name has been set
15094\(see `modify-frame-parameters'). */);
d39b6696
KH
15095 Vicon_title_format
15096 = Vframe_title_format
15097 = Fcons (intern ("multiple-frames"),
15098 Fcons (build_string ("%b"),
3ebf0ea9 15099 Fcons (Fcons (empty_string,
d39b6696
KH
15100 Fcons (intern ("invocation-name"),
15101 Fcons (build_string ("@"),
15102 Fcons (intern ("system-name"),
15103 Qnil)))),
15104 Qnil)));
5992c4f7 15105
7ee72033
MB
15106 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15107 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
15108If nil, disable message logging. If t, log messages but don't truncate
15109the buffer when it becomes large. */);
ac90c44f 15110 Vmessage_log_max = make_number (50);
08b610e4 15111
7ee72033
MB
15112 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15113 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
15114The value should be a list of functions that take one argument.
15115Just before redisplay, for each frame, if any of its windows have changed
15116size since the last redisplay, or have been split or deleted,
15117all the functions in the list are called, with the frame as argument. */);
08b610e4 15118 Vwindow_size_change_functions = Qnil;
75c43375 15119
7ee72033
MB
15120 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15121 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
15122Each function is called with two arguments, the window
15123and its new display-start position. Note that the value of `window-end'
15124is not valid when these functions are called. */);
75c43375 15125 Vwindow_scroll_functions = Qnil;
5f5c8ee5 15126
7ee72033
MB
15127 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15128 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
15129This increases a tool-bar's height if not all tool-bar items are visible.
15130It decreases a tool-bar's height when it would display blank lines
15131otherwise. */);
e037b9ec 15132 auto_resize_tool_bars_p = 1;
5f5c8ee5 15133
7ee72033
MB
15134 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15135 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 15136 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 15137
7ee72033
MB
15138 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15139 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 15140If an integer, use that for both horizontal and vertical margins.
f6c89f27 15141Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
15142HORZ specifying the horizontal margin, and VERT specifying the
15143vertical margin. */);
c3d76173 15144 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 15145
7ee72033 15146 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 15147 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 15148 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 15149
7ee72033
MB
15150 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15151 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
15152Each function is called with one argument POS. Functions must
15153fontify a region starting at POS in the current buffer, and give
15154fontified regions the property `fontified'. */);
5f5c8ee5 15155 Vfontification_functions = Qnil;
6b9f0906 15156 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
15157
15158 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
15159 &unibyte_display_via_language_environment,
15160 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
15161Specifically this means that unibyte non-ASCII characters
15162are displayed by converting them to the equivalent multibyte characters
15163according to the current language environment. As a result, they are
15164displayed according to the current fontset. */);
7bbe686f 15165 unibyte_display_via_language_environment = 0;
c6e89d6c 15166
7ee72033
MB
15167 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15168 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
15169If a float, it specifies a fraction of the mini-window frame's height.
15170If an integer, it specifies a number of lines. */);
c6e89d6c 15171 Vmax_mini_window_height = make_float (0.25);
6422c1d7 15172
7ee72033
MB
15173 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15174 doc: /* *How to resize mini-windows.
228299fa
GM
15175A value of nil means don't automatically resize mini-windows.
15176A value of t means resize them to fit the text displayed in them.
15177A value of `grow-only', the default, means let mini-windows grow
15178only, until their display becomes empty, at which point the windows
15179go back to their normal size. */);
6422c1d7
GM
15180 Vresize_mini_windows = Qgrow_only;
15181
d6d26ed3 15182 DEFVAR_BOOL ("cursor-in-non-selected-windows",
7ee72033
MB
15183 &cursor_in_non_selected_windows,
15184 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
f0529b5b 15185nil means don't display a cursor there. */);
d6d26ed3 15186 cursor_in_non_selected_windows = 1;
d475bcb8 15187
e76d28d5 15188 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 15189 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 15190 automatic_hscrolling_p = 1;
1df7e8f0 15191
e76d28d5 15192 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
15193 doc: /* *How many columns away from the window edge point is allowed to get
15194before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 15195 hscroll_margin = 5;
1df7e8f0 15196
e76d28d5 15197 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
15198 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15199When point is less than `automatic-hscroll-margin' columns from the window
15200edge, automatic hscrolling will scroll the window by the amount of columns
15201determined by this variable. If its value is a positive integer, scroll that
15202many columns. If it's a positive floating-point number, it specifies the
15203fraction of the window's width to scroll. If it's nil or zero, point will be
15204centered horizontally after the scroll. Any other value, including negative
15205numbers, are treated as if the value were zero.
15206
15207Automatic hscrolling always moves point outside the scroll margin, so if
15208point was more than scroll step columns inside the margin, the window will
15209scroll more than the value given by the scroll step.
15210
15211Note that the lower bound for automatic hscrolling specified by `scroll-left'
15212and `scroll-right' overrides this variable's effect. */);
e76d28d5 15213 Vhscroll_step = make_number (0);
e00daaa0 15214
7ee72033
MB
15215 DEFVAR_LISP ("image-types", &Vimage_types,
15216 doc: /* List of supported image types.
228299fa 15217Each element of the list is a symbol for a supported image type. */);
e00daaa0 15218 Vimage_types = Qnil;
ad4f174e 15219
7ee72033
MB
15220 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15221 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 15222Bind this around calls to `message' to let it take effect. */);
ad4f174e 15223 message_truncate_lines = 0;
0bca8940 15224
7ee72033
MB
15225 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15226 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 15227Can be used to update submenus whose contents should vary. */);
6422c1d7 15228 Vmenu_bar_update_hook = Qnil;
e1477f43 15229
7ee72033
MB
15230 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15231 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 15232 inhibit_menubar_update = 0;
30a3f61c 15233
7ee72033
MB
15234 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15235 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 15236 inhibit_eval_during_redisplay = 0;
76cb5e06 15237
69d1f7c9 15238#if GLYPH_DEBUG
76cb5e06
GM
15239 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15240 doc: /* Inhibit try_window_id display optimization. */);
15241 inhibit_try_window_id = 0;
15242
15243 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15244 doc: /* Inhibit try_window_reusing display optimization. */);
15245 inhibit_try_window_reusing = 0;
15246
15247 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15248 doc: /* Inhibit try_cursor_movement display optimization. */);
15249 inhibit_try_cursor_movement = 0;
15250#endif /* GLYPH_DEBUG */
a2889657
JB
15251}
15252
5f5c8ee5
GM
15253
15254/* Initialize this module when Emacs starts. */
15255
dfcf069d 15256void
a2889657
JB
15257init_xdisp ()
15258{
15259 Lisp_Object root_window;
5f5c8ee5 15260 struct window *mini_w;
a2889657 15261
04612a64
GM
15262 current_header_line_height = current_mode_line_height = -1;
15263
5f5c8ee5 15264 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
15265
15266 mini_w = XWINDOW (minibuf_window);
11e82b76 15267 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 15268
a2889657
JB
15269 if (!noninteractive)
15270 {
5f5c8ee5
GM
15271 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15272 int i;
15273
ac90c44f 15274 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
12c226c5 15275 set_window_height (root_window,
5f5c8ee5 15276 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 15277 0);
ac90c44f 15278 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
a2889657
JB
15279 set_window_height (minibuf_window, 1, 0);
15280
ac90c44f
GM
15281 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15282 mini_w->width = make_number (FRAME_WIDTH (f));
5f5c8ee5
GM
15283
15284 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15285 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15286 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15287
15288 /* The default ellipsis glyphs `...'. */
15289 for (i = 0; i < 3; ++i)
ac90c44f 15290 default_invis_vector[i] = make_number ('.');
a2889657 15291 }
5f5c8ee5
GM
15292
15293#ifdef HAVE_WINDOW_SYSTEM
15294 {
15295 /* Allocate the buffer for frame titles. */
15296 int size = 100;
15297 frame_title_buf = (char *) xmalloc (size);
15298 frame_title_buf_end = frame_title_buf + size;
15299 frame_title_ptr = NULL;
15300 }
15301#endif /* HAVE_WINDOW_SYSTEM */
21fdfb65
GM
15302
15303 help_echo_showing_p = 0;
a2889657 15304}
5f5c8ee5
GM
15305
15306