Add the latest changes to etags behaviour.
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
098ec84a 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
5f5c8ee5 3 Free Software Foundation, Inc.
a2889657
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
b1d1124b 9the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
a2889657 21
5f5c8ee5
GM
22/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
a2889657 169
18160b98 170#include <config.h>
a2889657 171#include <stdio.h>
a2889657 172#include "lisp.h"
546a4f00 173#include "keyboard.h"
44fa5b1e 174#include "frame.h"
a2889657
JB
175#include "window.h"
176#include "termchar.h"
177#include "dispextern.h"
178#include "buffer.h"
1c9241f5 179#include "charset.h"
a2889657
JB
180#include "indent.h"
181#include "commands.h"
182#include "macros.h"
183#include "disptab.h"
30c566e4 184#include "termhooks.h"
b0a0fbda 185#include "intervals.h"
1c9241f5
KH
186#include "coding.h"
187#include "process.h"
dfcf069d 188#include "region-cache.h"
0ef75e87 189#include "fontset.h"
dfcf069d 190
6d55d620 191#ifdef HAVE_X_WINDOWS
dfcf069d
AS
192#include "xterm.h"
193#endif
a75dfea0
AI
194#ifdef WINDOWSNT
195#include "w32term.h"
196#endif
1a578e9b
AC
197#ifdef macintosh
198#include "macterm.h"
199#endif
a2889657 200
5f5c8ee5
GM
201#define min(a, b) ((a) < (b) ? (a) : (b))
202#define max(a, b) ((a) > (b) ? (a) : (b))
203
204#define INFINITY 10000000
205
1a578e9b 206#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
2e621225 207extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 208extern int pending_menu_activation;
76412d64
RS
209#endif
210
a2889657
JB
211extern int interrupt_input;
212extern int command_loop_level;
213
b6436d4e
RS
214extern int minibuffer_auto_raise;
215
c4628384
RS
216extern Lisp_Object Qface;
217
399164b4
KH
218extern Lisp_Object Voverriding_local_map;
219extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 220extern Lisp_Object Qmenu_item;
399164b4 221
d46fb96a 222Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 223Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 224Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 225Lisp_Object Qinhibit_point_motion_hooks;
9499d71b 226Lisp_Object QCeval, Qwhen, QCfile, QCdata;
5f5c8ee5 227Lisp_Object Qfontified;
6422c1d7 228Lisp_Object Qgrow_only;
869fb12c 229Lisp_Object Qinhibit_eval_during_redisplay;
5f5c8ee5
GM
230
231/* Functions called to fontify regions of text. */
232
233Lisp_Object Vfontification_functions;
234Lisp_Object Qfontification_functions;
235
e037b9ec 236/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
237 over them. */
238
e037b9ec 239int auto_raise_tool_bar_buttons_p;
5f5c8ee5 240
e037b9ec 241/* Margin around tool bar buttons in pixels. */
5f5c8ee5 242
35a41507 243Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 244
e037b9ec 245/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 246
e037b9ec 247int tool_bar_button_relief;
5f5c8ee5 248
e037b9ec 249/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
250 items are visible, and no blank lines remain. */
251
e037b9ec 252int auto_resize_tool_bars_p;
399164b4 253
735c094c
KH
254/* Non-nil means don't actually do any redisplay. */
255
256Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257
30a3f61c
GM
258/* Non-zero means Lisp evaluation during redisplay is inhibited. */
259
869fb12c 260int inhibit_eval_during_redisplay;
30a3f61c 261
5f5c8ee5
GM
262/* Names of text properties relevant for redisplay. */
263
a7e27ef7
DL
264Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
265extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
5f5c8ee5
GM
266
267/* Symbols used in text property values. */
268
269Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 270Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 271Lisp_Object Qmargin;
a7e27ef7 272extern Lisp_Object Qheight;
5f5c8ee5 273
8f897821 274/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 275
8f897821 276Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
277
278/* Name of the face used to highlight trailing whitespace. */
279
280Lisp_Object Qtrailing_whitespace;
281
282/* The symbol `image' which is the car of the lists used to represent
283 images in Lisp. */
284
285Lisp_Object Qimage;
286
287/* Non-zero means print newline to stdout before next mini-buffer
288 message. */
a2889657
JB
289
290int noninteractive_need_newline;
291
5f5c8ee5 292/* Non-zero means print newline to message log before next message. */
f88eb0b6 293
3c6595e0 294static int message_log_need_newline;
f88eb0b6 295
5f5c8ee5
GM
296\f
297/* The buffer position of the first character appearing entirely or
298 partially on the line of the selected window which contains the
299 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
300 redisplay optimization in redisplay_internal. */
a2889657 301
5f5c8ee5 302static struct text_pos this_line_start_pos;
a2889657 303
5f5c8ee5
GM
304/* Number of characters past the end of the line above, including the
305 terminating newline. */
306
307static struct text_pos this_line_end_pos;
308
309/* The vertical positions and the height of this line. */
a2889657 310
a2889657 311static int this_line_vpos;
5f5c8ee5
GM
312static int this_line_y;
313static int this_line_pixel_height;
314
315/* X position at which this display line starts. Usually zero;
316 negative if first character is partially visible. */
317
318static int this_line_start_x;
a2889657 319
5f5c8ee5 320/* Buffer that this_line_.* variables are referring to. */
a2889657 321
a2889657
JB
322static struct buffer *this_line_buffer;
323
5f5c8ee5
GM
324/* Nonzero means truncate lines in all windows less wide than the
325 frame. */
a2889657 326
a2889657
JB
327int truncate_partial_width_windows;
328
7bbe686f 329/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 330
7bbe686f 331int unibyte_display_via_language_environment;
5f5c8ee5
GM
332
333/* Nonzero means we have more than one non-mini-buffer-only frame.
334 Not guaranteed to be accurate except while parsing
335 frame-title-format. */
7bbe686f 336
d39b6696
KH
337int multiple_frames;
338
a2889657
JB
339Lisp_Object Vglobal_mode_string;
340
341/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 342
a2889657
JB
343Lisp_Object Voverlay_arrow_position;
344
5f5c8ee5
GM
345/* String to display for the arrow. Only used on terminal frames. */
346
a2889657
JB
347Lisp_Object Voverlay_arrow_string;
348
5f5c8ee5
GM
349/* Values of those variables at last redisplay. However, if
350 Voverlay_arrow_position is a marker, last_arrow_position is its
351 numerical position. */
352
d45de95b
RS
353static Lisp_Object last_arrow_position, last_arrow_string;
354
5f5c8ee5
GM
355/* Like mode-line-format, but for the title bar on a visible frame. */
356
d39b6696
KH
357Lisp_Object Vframe_title_format;
358
5f5c8ee5
GM
359/* Like mode-line-format, but for the title bar on an iconified frame. */
360
d39b6696
KH
361Lisp_Object Vicon_title_format;
362
08b610e4
RS
363/* List of functions to call when a window's size changes. These
364 functions get one arg, a frame on which one or more windows' sizes
365 have changed. */
5f5c8ee5 366
08b610e4
RS
367static Lisp_Object Vwindow_size_change_functions;
368
0bca8940 369Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 370
a2889657 371/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 372
5f5c8ee5 373static int overlay_arrow_seen;
ca26e1c8 374
fba9ce76 375/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 376
5f5c8ee5
GM
377int highlight_nonselected_windows;
378
379/* If cursor motion alone moves point off frame, try scrolling this
380 many lines up or down if that will bring it back. */
381
14510fee 382static int scroll_step;
a2889657 383
5f5c8ee5
GM
384/* Non-0 means scroll just far enough to bring point back on the
385 screen, when appropriate. */
386
0789adb2
RS
387static int scroll_conservatively;
388
5f5c8ee5
GM
389/* Recenter the window whenever point gets within this many lines of
390 the top or bottom of the window. This value is translated into a
391 pixel value by multiplying it with CANON_Y_UNIT, which means that
392 there is really a fixed pixel height scroll margin. */
393
9afd2168
RS
394int scroll_margin;
395
5f5c8ee5
GM
396/* Number of windows showing the buffer of the selected window (or
397 another buffer with the same base buffer). keyboard.c refers to
398 this. */
a2889657 399
a2889657
JB
400int buffer_shared;
401
5f5c8ee5 402/* Vector containing glyphs for an ellipsis `...'. */
a2889657 403
5f5c8ee5 404static Lisp_Object default_invis_vector[3];
a2889657 405
1862a24e
MB
406/* Zero means display the mode-line/header-line/menu-bar in the default face
407 (this slightly odd definition is for compatibility with previous versions
408 of emacs), non-zero means display them using their respective faces.
409
410 This variable is deprecated. */
a2889657 411
a2889657
JB
412int mode_line_inverse_video;
413
5f5c8ee5
GM
414/* Prompt to display in front of the mini-buffer contents. */
415
8c5b6a0a 416Lisp_Object minibuf_prompt;
a2889657 417
5f5c8ee5
GM
418/* Width of current mini-buffer prompt. Only set after display_line
419 of the line that contains the prompt. */
420
a2889657 421int minibuf_prompt_width;
5f5c8ee5
GM
422int minibuf_prompt_pixel_width;
423
5f5c8ee5
GM
424/* This is the window where the echo area message was displayed. It
425 is always a mini-buffer window, but it may not be the same window
426 currently active as a mini-buffer. */
427
73af359d
RS
428Lisp_Object echo_area_window;
429
c6e89d6c
GM
430/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
431 pushes the current message and the value of
432 message_enable_multibyte on the stack, the function restore_message
433 pops the stack and displays MESSAGE again. */
434
435Lisp_Object Vmessage_stack;
436
a3788d53
RS
437/* Nonzero means multibyte characters were enabled when the echo area
438 message was specified. */
5f5c8ee5 439
a3788d53
RS
440int message_enable_multibyte;
441
5f5c8ee5
GM
442/* True if we should redraw the mode lines on the next redisplay. */
443
a2889657
JB
444int update_mode_lines;
445
5f5c8ee5
GM
446/* Nonzero if window sizes or contents have changed since last
447 redisplay that finished */
448
a2889657
JB
449int windows_or_buffers_changed;
450
5f5c8ee5
GM
451/* Nonzero after display_mode_line if %l was used and it displayed a
452 line number. */
453
aa6d10fa
RS
454int line_number_displayed;
455
456/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 457
090703f4 458Lisp_Object Vline_number_display_limit;
5992c4f7 459
5d121aec
KH
460/* line width to consider when repostioning for line number display */
461
462static int line_number_display_limit_width;
463
5f5c8ee5
GM
464/* Number of lines to keep in the message log buffer. t means
465 infinite. nil means don't log at all. */
466
5992c4f7 467Lisp_Object Vmessage_log_max;
d45de95b 468
6a94510a
GM
469/* The name of the *Messages* buffer, a string. */
470
471static Lisp_Object Vmessages_buffer_name;
472
c6e89d6c
GM
473/* Current, index 0, and last displayed echo area message. Either
474 buffers from echo_buffers, or nil to indicate no message. */
475
476Lisp_Object echo_area_buffer[2];
477
478/* The buffers referenced from echo_area_buffer. */
479
480static Lisp_Object echo_buffer[2];
481
482/* A vector saved used in with_area_buffer to reduce consing. */
483
484static Lisp_Object Vwith_echo_area_save_vector;
485
486/* Non-zero means display_echo_area should display the last echo area
487 message again. Set by redisplay_preserve_echo_area. */
488
489static int display_last_displayed_message_p;
490
491/* Nonzero if echo area is being used by print; zero if being used by
492 message. */
493
494int message_buf_print;
495
e1477f43
GM
496/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
497
498Lisp_Object Qinhibit_menubar_update;
499int inhibit_menubar_update;
500
9142dd5b
GM
501/* Maximum height for resizing mini-windows. Either a float
502 specifying a fraction of the available height, or an integer
503 specifying a number of lines. */
c6e89d6c 504
ad4f174e
GM
505Lisp_Object Vmax_mini_window_height;
506
507/* Non-zero means messages should be displayed with truncated
508 lines instead of being continued. */
509
510int message_truncate_lines;
511Lisp_Object Qmessage_truncate_lines;
c6e89d6c 512
d6d26ed3
GM
513/* Non-zero means we want a hollow cursor in windows that are not
514 selected. Zero means there's no cursor in such windows. */
515
516int cursor_in_non_selected_windows;
517
5f5c8ee5
GM
518/* A scratch glyph row with contents used for generating truncation
519 glyphs. Also used in direct_output_for_insert. */
12adba34 520
5f5c8ee5
GM
521#define MAX_SCRATCH_GLYPHS 100
522struct glyph_row scratch_glyph_row;
523static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 524
5f5c8ee5
GM
525/* Ascent and height of the last line processed by move_it_to. */
526
527static int last_max_ascent, last_height;
528
21fdfb65
GM
529/* Non-zero if there's a help-echo in the echo area. */
530
531int help_echo_showing_p;
532
04612a64
GM
533/* If >= 0, computed, exact values of mode-line and header-line height
534 to use in the macros CURRENT_MODE_LINE_HEIGHT and
535 CURRENT_HEADER_LINE_HEIGHT. */
536
537int current_mode_line_height, current_header_line_height;
538
5f5c8ee5
GM
539/* The maximum distance to look ahead for text properties. Values
540 that are too small let us call compute_char_face and similar
541 functions too often which is expensive. Values that are too large
542 let us call compute_char_face and alike too often because we
543 might not be interested in text properties that far away. */
544
545#define TEXT_PROP_DISTANCE_LIMIT 100
546
47589c8c
GM
547#if GLYPH_DEBUG
548
5f5c8ee5
GM
549/* Non-zero means print traces of redisplay if compiled with
550 GLYPH_DEBUG != 0. */
551
5f5c8ee5 552int trace_redisplay_p;
47589c8c 553
546a4f00 554#endif /* GLYPH_DEBUG */
47589c8c 555
546a4f00
AI
556#ifdef DEBUG_TRACE_MOVE
557/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
558int trace_move;
559
47589c8c
GM
560#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
561#else
91c3f500 562#define TRACE_MOVE(x) (void) 0
5f5c8ee5 563#endif
546a4f00 564
d475bcb8
GM
565/* Non-zero means automatically scroll windows horizontally to make
566 point visible. */
567
568int automatic_hscrolling_p;
569
e00daaa0
GM
570/* A list of symbols, one for each supported image type. */
571
572Lisp_Object Vimage_types;
573
6422c1d7 574/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 575 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
576 display. If `grow-only', let mini-windows grow only until they
577 become empty. */
578
579Lisp_Object Vresize_mini_windows;
580
5f5c8ee5
GM
581/* Value returned from text property handlers (see below). */
582
583enum prop_handled
3c6595e0 584{
5f5c8ee5
GM
585 HANDLED_NORMALLY,
586 HANDLED_RECOMPUTE_PROPS,
587 HANDLED_OVERLAY_STRING_CONSUMED,
588 HANDLED_RETURN
589};
3c6595e0 590
5f5c8ee5
GM
591/* A description of text properties that redisplay is interested
592 in. */
3c6595e0 593
5f5c8ee5
GM
594struct props
595{
596 /* The name of the property. */
597 Lisp_Object *name;
90adcf20 598
5f5c8ee5
GM
599 /* A unique index for the property. */
600 enum prop_idx idx;
601
602 /* A handler function called to set up iterator IT from the property
603 at IT's current position. Value is used to steer handle_stop. */
604 enum prop_handled (*handler) P_ ((struct it *it));
605};
606
607static enum prop_handled handle_face_prop P_ ((struct it *));
608static enum prop_handled handle_invisible_prop P_ ((struct it *));
609static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 610static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
611static enum prop_handled handle_overlay_change P_ ((struct it *));
612static enum prop_handled handle_fontified_prop P_ ((struct it *));
613
614/* Properties handled by iterators. */
615
616static struct props it_props[] =
5992c4f7 617{
5f5c8ee5
GM
618 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
619 /* Handle `face' before `display' because some sub-properties of
620 `display' need to know the face. */
621 {&Qface, FACE_PROP_IDX, handle_face_prop},
622 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
623 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 624 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
625 {NULL, 0, NULL}
626};
5992c4f7 627
5f5c8ee5
GM
628/* Value is the position described by X. If X is a marker, value is
629 the marker_position of X. Otherwise, value is X. */
12adba34 630
5f5c8ee5 631#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 632
5f5c8ee5 633/* Enumeration returned by some move_it_.* functions internally. */
12adba34 634
5f5c8ee5
GM
635enum move_it_result
636{
637 /* Not used. Undefined value. */
638 MOVE_UNDEFINED,
bab29e15 639
5f5c8ee5
GM
640 /* Move ended at the requested buffer position or ZV. */
641 MOVE_POS_MATCH_OR_ZV,
bab29e15 642
5f5c8ee5
GM
643 /* Move ended at the requested X pixel position. */
644 MOVE_X_REACHED,
12adba34 645
5f5c8ee5
GM
646 /* Move within a line ended at the end of a line that must be
647 continued. */
648 MOVE_LINE_CONTINUED,
649
650 /* Move within a line ended at the end of a line that would
651 be displayed truncated. */
652 MOVE_LINE_TRUNCATED,
ff6c30e5 653
5f5c8ee5
GM
654 /* Move within a line ended at a line end. */
655 MOVE_NEWLINE_OR_CR
656};
12adba34 657
ff6c30e5 658
5f5c8ee5
GM
659\f
660/* Function prototypes. */
661
43c09969 662static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
663static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
664static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 665static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 666static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225
GM
667static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
668static int invisible_text_between_p P_ ((struct it *, int, int));
669static int next_element_from_ellipsis P_ ((struct it *));
670static void pint2str P_ ((char *, int, int));
671static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
672 struct text_pos));
673static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
674static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
675static void store_frame_title_char P_ ((char));
676static int store_frame_title P_ ((unsigned char *, int, int));
677static void x_consider_frame_title P_ ((Lisp_Object));
678static void handle_stop P_ ((struct it *));
679static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 680static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 681static void ensure_echo_area_buffers P_ ((void));
e037b9ec
GM
682static struct glyph_row *row_containing_pos P_ ((struct window *, int,
683 struct glyph_row *,
684 struct glyph_row *));
c6e89d6c
GM
685static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
686static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 687static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
688 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
689 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 690static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
691static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
693static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 694static int display_echo_area P_ ((struct window *));
23dd2d97
KR
695static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
696static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 697static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 698static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
699static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
700 struct text_pos));
701static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 702static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
703static void insert_left_trunc_glyphs P_ ((struct it *));
704static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
705static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 706static int append_space P_ ((struct it *, int));
5f5c8ee5
GM
707static void make_cursor_line_fully_visible P_ ((struct window *));
708static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
47589c8c 709static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
710static int trailing_whitespace_p P_ ((int));
711static int message_log_check_duplicate P_ ((int, int, int, int));
712int invisible_p P_ ((Lisp_Object, Lisp_Object));
713int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
714static void push_it P_ ((struct it *));
715static void pop_it P_ ((struct it *));
716static void sync_frame_with_window_matrix_rows P_ ((struct window *));
717static void redisplay_internal P_ ((int));
c6e89d6c 718static int echo_area_display P_ ((int));
5f5c8ee5
GM
719static void redisplay_windows P_ ((Lisp_Object));
720static void redisplay_window P_ ((Lisp_Object, int));
721static void update_menu_bar P_ ((struct frame *, int));
722static int try_window_reusing_current_matrix P_ ((struct window *));
723static int try_window_id P_ ((struct window *));
724static int display_line P_ ((struct it *));
715e84c9 725static int display_mode_lines P_ ((struct window *));
04612a64 726static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
5f5c8ee5 727static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 728static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
729static void display_menu_bar P_ ((struct window *));
730static int display_count_lines P_ ((int, int, int, int, int *));
731static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
732 int, int, struct it *, int, int, int, int));
733static void compute_line_metrics P_ ((struct it *));
734static void run_redisplay_end_trigger_hook P_ ((struct it *));
735static int get_overlay_strings P_ ((struct it *));
736static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
737static void reseat P_ ((struct it *, struct text_pos, int));
738static void reseat_1 P_ ((struct it *, struct text_pos, int));
739static void back_to_previous_visible_line_start P_ ((struct it *));
740static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 741static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
742static int next_element_from_display_vector P_ ((struct it *));
743static int next_element_from_string P_ ((struct it *));
744static int next_element_from_c_string P_ ((struct it *));
745static int next_element_from_buffer P_ ((struct it *));
260a86a0 746static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
747static int next_element_from_image P_ ((struct it *));
748static int next_element_from_stretch P_ ((struct it *));
749static void load_overlay_strings P_ ((struct it *));
750static void init_from_display_pos P_ ((struct it *, struct window *,
751 struct display_pos *));
752static void reseat_to_string P_ ((struct it *, unsigned char *,
753 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
754static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
755 int, int, int));
756void move_it_vertically_backward P_ ((struct it *, int));
757static void init_to_row_start P_ ((struct it *, struct window *,
758 struct glyph_row *));
759static void init_to_row_end P_ ((struct it *, struct window *,
760 struct glyph_row *));
761static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 762static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
763static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
764 Lisp_Object, int));
765static struct text_pos string_pos P_ ((int, Lisp_Object));
766static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
767static int number_of_chars P_ ((unsigned char *, int));
768static void compute_stop_pos P_ ((struct it *));
769static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
770 Lisp_Object));
771static int face_before_or_after_it_pos P_ ((struct it *, int));
772static int next_overlay_change P_ ((int));
773static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
774 Lisp_Object, struct text_pos *,
775 int));
06a12811 776static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
777static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
778 struct window *));
5f5c8ee5
GM
779
780#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
781#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 782
5f5c8ee5 783#ifdef HAVE_WINDOW_SYSTEM
12adba34 784
e037b9ec
GM
785static void update_tool_bar P_ ((struct frame *, int));
786static void build_desired_tool_bar_string P_ ((struct frame *f));
787static int redisplay_tool_bar P_ ((struct frame *));
788static void display_tool_bar_line P_ ((struct it *));
12adba34 789
5f5c8ee5 790#endif /* HAVE_WINDOW_SYSTEM */
12adba34 791
5f5c8ee5
GM
792\f
793/***********************************************************************
794 Window display dimensions
795 ***********************************************************************/
12adba34 796
5f5c8ee5
GM
797/* Return the window-relative maximum y + 1 for glyph rows displaying
798 text in window W. This is the height of W minus the height of a
799 mode line, if any. */
800
801INLINE int
802window_text_bottom_y (w)
803 struct window *w;
804{
805 struct frame *f = XFRAME (w->frame);
806 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 807
5f5c8ee5
GM
808 if (WINDOW_WANTS_MODELINE_P (w))
809 height -= CURRENT_MODE_LINE_HEIGHT (w);
810 return height;
f88eb0b6
KH
811}
812
f82aff7c 813
5f5c8ee5
GM
814/* Return the pixel width of display area AREA of window W. AREA < 0
815 means return the total width of W, not including bitmap areas to
816 the left and right of the window. */
ff6c30e5 817
5f5c8ee5
GM
818INLINE int
819window_box_width (w, area)
820 struct window *w;
821 int area;
822{
823 struct frame *f = XFRAME (w->frame);
824 int width = XFASTINT (w->width);
825
826 if (!w->pseudo_window_p)
ff6c30e5 827 {
050d82d7 828 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
829
830 if (area == TEXT_AREA)
831 {
832 if (INTEGERP (w->left_margin_width))
833 width -= XFASTINT (w->left_margin_width);
834 if (INTEGERP (w->right_margin_width))
835 width -= XFASTINT (w->right_margin_width);
836 }
837 else if (area == LEFT_MARGIN_AREA)
838 width = (INTEGERP (w->left_margin_width)
839 ? XFASTINT (w->left_margin_width) : 0);
840 else if (area == RIGHT_MARGIN_AREA)
841 width = (INTEGERP (w->right_margin_width)
842 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 843 }
5f5c8ee5
GM
844
845 return width * CANON_X_UNIT (f);
ff6c30e5 846}
1adc55de 847
1adc55de 848
5f5c8ee5
GM
849/* Return the pixel height of the display area of window W, not
850 including mode lines of W, if any.. */
f88eb0b6 851
5f5c8ee5
GM
852INLINE int
853window_box_height (w)
854 struct window *w;
f88eb0b6 855{
5f5c8ee5
GM
856 struct frame *f = XFRAME (w->frame);
857 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
858
859 xassert (height >= 0);
5f5c8ee5 860
d9c9e99c
MB
861 /* Note: the code below that determines the mode-line/header-line
862 height is essentially the same as that contained in the macro
863 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
864 the appropriate glyph row has its `mode_line_p' flag set,
865 and if it doesn't, uses estimate_mode_line_height instead. */
866
5f5c8ee5 867 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
868 {
869 struct glyph_row *ml_row
870 = (w->current_matrix && w->current_matrix->rows
871 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
872 : 0);
873 if (ml_row && ml_row->mode_line_p)
874 height -= ml_row->height;
875 else
876 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
877 }
5f5c8ee5 878
045dee35 879 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
880 {
881 struct glyph_row *hl_row
882 = (w->current_matrix && w->current_matrix->rows
883 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
884 : 0);
885 if (hl_row && hl_row->mode_line_p)
886 height -= hl_row->height;
887 else
888 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
889 }
5f5c8ee5
GM
890
891 return height;
5992c4f7
KH
892}
893
894
5f5c8ee5
GM
895/* Return the frame-relative coordinate of the left edge of display
896 area AREA of window W. AREA < 0 means return the left edge of the
897 whole window, to the right of any bitmap area at the left side of
898 W. */
5992c4f7 899
5f5c8ee5
GM
900INLINE int
901window_box_left (w, area)
902 struct window *w;
903 int area;
90adcf20 904{
5f5c8ee5
GM
905 struct frame *f = XFRAME (w->frame);
906 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 907
5f5c8ee5 908 if (!w->pseudo_window_p)
90adcf20 909 {
5f5c8ee5 910 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 911 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
912
913 if (area == TEXT_AREA)
914 x += window_box_width (w, LEFT_MARGIN_AREA);
915 else if (area == RIGHT_MARGIN_AREA)
916 x += (window_box_width (w, LEFT_MARGIN_AREA)
917 + window_box_width (w, TEXT_AREA));
90adcf20 918 }
73af359d 919
5f5c8ee5
GM
920 return x;
921}
90adcf20 922
b6436d4e 923
5f5c8ee5
GM
924/* Return the frame-relative coordinate of the right edge of display
925 area AREA of window W. AREA < 0 means return the left edge of the
926 whole window, to the left of any bitmap area at the right side of
927 W. */
ded34426 928
5f5c8ee5
GM
929INLINE int
930window_box_right (w, area)
931 struct window *w;
932 int area;
933{
934 return window_box_left (w, area) + window_box_width (w, area);
935}
936
937
938/* Get the bounding box of the display area AREA of window W, without
939 mode lines, in frame-relative coordinates. AREA < 0 means the
940 whole window, not including bitmap areas to the left and right of
941 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
942 coordinates of the upper-left corner of the box. Return in
943 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
944
945INLINE void
946window_box (w, area, box_x, box_y, box_width, box_height)
947 struct window *w;
948 int area;
949 int *box_x, *box_y, *box_width, *box_height;
950{
951 struct frame *f = XFRAME (w->frame);
952
953 *box_width = window_box_width (w, area);
954 *box_height = window_box_height (w);
955 *box_x = window_box_left (w, area);
956 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
957 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
958 if (WINDOW_WANTS_HEADER_LINE_P (w))
959 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 960}
1adc55de 961
1adc55de 962
5f5c8ee5
GM
963/* Get the bounding box of the display area AREA of window W, without
964 mode lines. AREA < 0 means the whole window, not including bitmap
965 areas to the left and right of the window. Return in *TOP_LEFT_X
966 and TOP_LEFT_Y the frame-relative pixel coordinates of the
967 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
968 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
969 box. */
ded34426 970
5f5c8ee5
GM
971INLINE void
972window_box_edges (w, area, top_left_x, top_left_y,
973 bottom_right_x, bottom_right_y)
974 struct window *w;
975 int area;
976 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 977{
5f5c8ee5
GM
978 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
979 bottom_right_y);
980 *bottom_right_x += *top_left_x;
981 *bottom_right_y += *top_left_y;
48ae5f0a
KH
982}
983
5f5c8ee5
GM
984
985\f
986/***********************************************************************
987 Utilities
988 ***********************************************************************/
989
8b6ea97f
GM
990/* Return the bottom y-position of the line the iterator IT is in.
991 This can modify IT's settings. */
992
993int
994line_bottom_y (it)
995 struct it *it;
996{
997 int line_height = it->max_ascent + it->max_descent;
998 int line_top_y = it->current_y;
999
1000 if (line_height == 0)
1001 {
1002 if (last_height)
1003 line_height = last_height;
1004 else if (IT_CHARPOS (*it) < ZV)
1005 {
1006 move_it_by_lines (it, 1, 1);
1007 line_height = (it->max_ascent || it->max_descent
1008 ? it->max_ascent + it->max_descent
1009 : last_height);
1010 }
1011 else
1012 {
1013 struct glyph_row *row = it->glyph_row;
1014
1015 /* Use the default character height. */
1016 it->glyph_row = NULL;
1017 it->what = IT_CHARACTER;
1018 it->c = ' ';
1019 it->len = 1;
1020 PRODUCE_GLYPHS (it);
1021 line_height = it->ascent + it->descent;
1022 it->glyph_row = row;
1023 }
1024 }
1025
1026 return line_top_y + line_height;
1027}
1028
1029
0db95684
GM
1030/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1031 1 if POS is visible and the line containing POS is fully visible.
1032 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1033 and header-lines heights. */
3a641a69
GM
1034
1035int
04612a64 1036pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1037 struct window *w;
04612a64 1038 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1039{
1040 struct it it;
1041 struct text_pos top;
fcab1954
GM
1042 int visible_p;
1043 struct buffer *old_buffer = NULL;
1044
1045 if (XBUFFER (w->buffer) != current_buffer)
1046 {
1047 old_buffer = current_buffer;
1048 set_buffer_internal_1 (XBUFFER (w->buffer));
1049 }
3a641a69
GM
1050
1051 *fully = visible_p = 0;
1052 SET_TEXT_POS_FROM_MARKER (top, w->start);
04612a64
GM
1053
1054 /* Compute exact mode line heights, if requested. */
1055 if (exact_mode_line_heights_p)
1056 {
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 current_mode_line_height
1059 = display_mode_line (w, MODE_LINE_FACE_ID,
1060 current_buffer->mode_line_format);
1061
1062 if (WINDOW_WANTS_HEADER_LINE_P (w))
1063 current_header_line_height
1064 = display_mode_line (w, HEADER_LINE_FACE_ID,
1065 current_buffer->header_line_format);
1066 }
3a641a69 1067
04612a64 1068 start_display (&it, w, top);
3a641a69
GM
1069 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1070 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1071
1072 /* Note that we may overshoot because of invisible text. */
1073 if (IT_CHARPOS (it) >= charpos)
3a641a69 1074 {
8b6ea97f
GM
1075 int top_y = it.current_y;
1076 int bottom_y = line_bottom_y (&it);
fcab1954 1077 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
3a641a69 1078
8b6ea97f
GM
1079 if (top_y < window_top_y)
1080 visible_p = bottom_y > window_top_y;
1081 else if (top_y < it.last_visible_y)
fcab1954
GM
1082 {
1083 visible_p = 1;
8b6ea97f 1084 *fully = bottom_y <= it.last_visible_y;
fcab1954 1085 }
3a641a69
GM
1086 }
1087 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1088 {
1089 move_it_by_lines (&it, 1, 0);
1090 if (charpos < IT_CHARPOS (it))
1091 {
1092 visible_p = 1;
1093 *fully = 0;
1094 }
1095 }
fcab1954
GM
1096
1097 if (old_buffer)
1098 set_buffer_internal_1 (old_buffer);
04612a64
GM
1099
1100 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1101 return visible_p;
1102}
1103
1104
4fdb80f2
GM
1105/* Return the next character from STR which is MAXLEN bytes long.
1106 Return in *LEN the length of the character. This is like
1107 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1108 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1109 character. */
1110
1111static INLINE int
7a5b8a93 1112string_char_and_length (str, maxlen, len)
4fdb80f2 1113 unsigned char *str;
7a5b8a93 1114 int maxlen, *len;
4fdb80f2
GM
1115{
1116 int c;
1117
1118 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1119 if (!CHAR_VALID_P (c, 1))
1120 /* We may not change the length here because other places in Emacs
9ab8560d 1121 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1122 characters. */
1123 c = '?';
1124
1125 return c;
1126}
1127
1128
1129
5f5c8ee5
GM
1130/* Given a position POS containing a valid character and byte position
1131 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1132
1133static struct text_pos
1134string_pos_nchars_ahead (pos, string, nchars)
1135 struct text_pos pos;
1136 Lisp_Object string;
1137 int nchars;
0b1005ef 1138{
5f5c8ee5
GM
1139 xassert (STRINGP (string) && nchars >= 0);
1140
1141 if (STRING_MULTIBYTE (string))
1142 {
1143 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1144 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1145 int len;
1146
1147 while (nchars--)
1148 {
4fdb80f2 1149 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1150 p += len, rest -= len;
1151 xassert (rest >= 0);
1152 CHARPOS (pos) += 1;
1153 BYTEPOS (pos) += len;
1154 }
1155 }
1156 else
1157 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1158
1159 return pos;
0a9dc68b
RS
1160}
1161
0a9dc68b 1162
5f5c8ee5
GM
1163/* Value is the text position, i.e. character and byte position,
1164 for character position CHARPOS in STRING. */
1165
1166static INLINE struct text_pos
1167string_pos (charpos, string)
1168 int charpos;
0a9dc68b 1169 Lisp_Object string;
0a9dc68b 1170{
5f5c8ee5
GM
1171 struct text_pos pos;
1172 xassert (STRINGP (string));
1173 xassert (charpos >= 0);
1174 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1175 return pos;
1176}
1177
1178
1179/* Value is a text position, i.e. character and byte position, for
1180 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1181 means recognize multibyte characters. */
1182
1183static struct text_pos
1184c_string_pos (charpos, s, multibyte_p)
1185 int charpos;
1186 unsigned char *s;
1187 int multibyte_p;
1188{
1189 struct text_pos pos;
1190
1191 xassert (s != NULL);
1192 xassert (charpos >= 0);
1193
1194 if (multibyte_p)
0a9dc68b 1195 {
5f5c8ee5
GM
1196 int rest = strlen (s), len;
1197
1198 SET_TEXT_POS (pos, 0, 0);
1199 while (charpos--)
0a9dc68b 1200 {
4fdb80f2 1201 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1202 s += len, rest -= len;
1203 xassert (rest >= 0);
1204 CHARPOS (pos) += 1;
1205 BYTEPOS (pos) += len;
0a9dc68b
RS
1206 }
1207 }
5f5c8ee5
GM
1208 else
1209 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1210
5f5c8ee5
GM
1211 return pos;
1212}
0a9dc68b 1213
0a9dc68b 1214
5f5c8ee5
GM
1215/* Value is the number of characters in C string S. MULTIBYTE_P
1216 non-zero means recognize multibyte characters. */
0a9dc68b 1217
5f5c8ee5
GM
1218static int
1219number_of_chars (s, multibyte_p)
1220 unsigned char *s;
1221 int multibyte_p;
1222{
1223 int nchars;
1224
1225 if (multibyte_p)
1226 {
1227 int rest = strlen (s), len;
1228 unsigned char *p = (unsigned char *) s;
0a9dc68b 1229
5f5c8ee5
GM
1230 for (nchars = 0; rest > 0; ++nchars)
1231 {
4fdb80f2 1232 string_char_and_length (p, rest, &len);
5f5c8ee5 1233 rest -= len, p += len;
0a9dc68b
RS
1234 }
1235 }
5f5c8ee5
GM
1236 else
1237 nchars = strlen (s);
1238
1239 return nchars;
0b1005ef
KH
1240}
1241
5f5c8ee5
GM
1242
1243/* Compute byte position NEWPOS->bytepos corresponding to
1244 NEWPOS->charpos. POS is a known position in string STRING.
1245 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1246
5f5c8ee5
GM
1247static void
1248compute_string_pos (newpos, pos, string)
1249 struct text_pos *newpos, pos;
1250 Lisp_Object string;
76412d64 1251{
5f5c8ee5
GM
1252 xassert (STRINGP (string));
1253 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1254
1255 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1256 *newpos = string_pos_nchars_ahead (pos, string,
1257 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1258 else
1259 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1260}
1261
9c74a0dd 1262
5f5c8ee5
GM
1263\f
1264/***********************************************************************
1265 Lisp form evaluation
1266 ***********************************************************************/
1267
116d6f5c 1268/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1269
1270static Lisp_Object
116d6f5c 1271safe_eval_handler (arg)
5f5c8ee5
GM
1272 Lisp_Object arg;
1273{
0dbf9fd2 1274 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1275 return Qnil;
1276}
1277
1278
1279/* Evaluate SEXPR and return the result, or nil if something went
1280 wrong. */
1281
71e5b1b8 1282Lisp_Object
116d6f5c 1283safe_eval (sexpr)
5f5c8ee5
GM
1284 Lisp_Object sexpr;
1285{
5f5c8ee5 1286 Lisp_Object val;
30a3f61c
GM
1287
1288 if (inhibit_eval_during_redisplay)
1289 val = Qnil;
1290 else
1291 {
1292 int count = BINDING_STACK_SIZE ();
1293 struct gcpro gcpro1;
0d8b31c0 1294
30a3f61c
GM
1295 GCPRO1 (sexpr);
1296 specbind (Qinhibit_redisplay, Qt);
1297 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1298 safe_eval_handler);
1299 UNGCPRO;
1300 val = unbind_to (count, val);
1301 }
1302
1303 return val;
0d8b31c0
GM
1304}
1305
1306
1307/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1308 Return the result, or nil if something went wrong. */
1309
1310Lisp_Object
116d6f5c 1311safe_call (nargs, args)
0d8b31c0
GM
1312 int nargs;
1313 Lisp_Object *args;
1314{
0d8b31c0 1315 Lisp_Object val;
30a3f61c
GM
1316
1317 if (inhibit_eval_during_redisplay)
1318 val = Qnil;
1319 else
1320 {
1321 int count = BINDING_STACK_SIZE ();
1322 struct gcpro gcpro1;
0d8b31c0 1323
30a3f61c
GM
1324 GCPRO1 (args[0]);
1325 gcpro1.nvars = nargs;
1326 specbind (Qinhibit_redisplay, Qt);
1327 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1328 safe_eval_handler);
1329 UNGCPRO;
1330 val = unbind_to (count, val);
1331 }
1332
1333 return val;
5f5c8ee5
GM
1334}
1335
1336
116d6f5c
GM
1337/* Call function FN with one argument ARG.
1338 Return the result, or nil if something went wrong. */
1339
1340Lisp_Object
1341safe_call1 (fn, arg)
1342 Lisp_Object fn, arg;
1343{
1344 Lisp_Object args[2];
1345 args[0] = fn;
1346 args[1] = arg;
1347 return safe_call (2, args);
1348}
1349
1350
5f5c8ee5
GM
1351\f
1352/***********************************************************************
1353 Debugging
1354 ***********************************************************************/
1355
1356#if 0
1357
1358/* Define CHECK_IT to perform sanity checks on iterators.
1359 This is for debugging. It is too slow to do unconditionally. */
1360
1361static void
1362check_it (it)
1363 struct it *it;
1364{
1365 if (it->method == next_element_from_string)
a2889657 1366 {
5f5c8ee5
GM
1367 xassert (STRINGP (it->string));
1368 xassert (IT_STRING_CHARPOS (*it) >= 0);
1369 }
1370 else if (it->method == next_element_from_buffer)
1371 {
1372 /* Check that character and byte positions agree. */
1373 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1374 }
73af359d 1375
5f5c8ee5
GM
1376 if (it->dpvec)
1377 xassert (it->current.dpvec_index >= 0);
1378 else
1379 xassert (it->current.dpvec_index < 0);
1380}
1f40cad2 1381
5f5c8ee5
GM
1382#define CHECK_IT(IT) check_it ((IT))
1383
1384#else /* not 0 */
1385
1386#define CHECK_IT(IT) (void) 0
1387
1388#endif /* not 0 */
1389
1390
1391#if GLYPH_DEBUG
1392
1393/* Check that the window end of window W is what we expect it
1394 to be---the last row in the current matrix displaying text. */
1395
1396static void
1397check_window_end (w)
1398 struct window *w;
1399{
1400 if (!MINI_WINDOW_P (w)
1401 && !NILP (w->window_end_valid))
1402 {
1403 struct glyph_row *row;
1404 xassert ((row = MATRIX_ROW (w->current_matrix,
1405 XFASTINT (w->window_end_vpos)),
1406 !row->enabled_p
1407 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1408 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1409 }
1410}
1411
1412#define CHECK_WINDOW_END(W) check_window_end ((W))
1413
1414#else /* not GLYPH_DEBUG */
1415
1416#define CHECK_WINDOW_END(W) (void) 0
1417
1418#endif /* not GLYPH_DEBUG */
1419
1420
1421\f
1422/***********************************************************************
1423 Iterator initialization
1424 ***********************************************************************/
1425
1426/* Initialize IT for displaying current_buffer in window W, starting
1427 at character position CHARPOS. CHARPOS < 0 means that no buffer
1428 position is specified which is useful when the iterator is assigned
1429 a position later. BYTEPOS is the byte position corresponding to
1430 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1431
1432 If ROW is not null, calls to produce_glyphs with IT as parameter
1433 will produce glyphs in that row.
1434
1435 BASE_FACE_ID is the id of a base face to use. It must be one of
1436 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
045dee35 1437 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1438 displaying the tool-bar.
5f5c8ee5
GM
1439
1440 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1441 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
1442 corresponding mode line glyph row of the desired matrix of W. */
1443
1444void
1445init_iterator (it, w, charpos, bytepos, row, base_face_id)
1446 struct it *it;
1447 struct window *w;
1448 int charpos, bytepos;
1449 struct glyph_row *row;
1450 enum face_id base_face_id;
1451{
1452 int highlight_region_p;
5f5c8ee5
GM
1453
1454 /* Some precondition checks. */
1455 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1456 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1457
1458 /* If face attributes have been changed since the last redisplay,
1459 free realized faces now because they depend on face definitions
1460 that might have changed. */
1461 if (face_change_count)
1462 {
1463 face_change_count = 0;
1464 free_all_realized_faces (Qnil);
1465 }
1466
1467 /* Use one of the mode line rows of W's desired matrix if
1468 appropriate. */
1469 if (row == NULL)
1470 {
1471 if (base_face_id == MODE_LINE_FACE_ID)
1472 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1473 else if (base_face_id == HEADER_LINE_FACE_ID)
1474 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1475 }
1476
1477 /* Clear IT. */
1478 bzero (it, sizeof *it);
1479 it->current.overlay_string_index = -1;
1480 it->current.dpvec_index = -1;
5f5c8ee5
GM
1481 it->base_face_id = base_face_id;
1482
1483 /* The window in which we iterate over current_buffer: */
1484 XSETWINDOW (it->window, w);
1485 it->w = w;
1486 it->f = XFRAME (w->frame);
1487
d475bcb8
GM
1488 /* Extra space between lines (on window systems only). */
1489 if (base_face_id == DEFAULT_FACE_ID
1490 && FRAME_WINDOW_P (it->f))
1491 {
1492 if (NATNUMP (current_buffer->extra_line_spacing))
1493 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1494 else if (it->f->extra_line_spacing > 0)
1495 it->extra_line_spacing = it->f->extra_line_spacing;
1496 }
1497
5f5c8ee5 1498 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1499 attribute changes of named faces, recompute them. When running
1500 in batch mode, the face cache of Vterminal_frame is null. If
1501 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1502 if (
1503#ifndef WINDOWSNT
1504 noninteractive &&
1505#endif
1506 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1507 init_frame_faces (it->f);
5f5c8ee5
GM
1508 if (FRAME_FACE_CACHE (it->f)->used == 0)
1509 recompute_basic_faces (it->f);
1510
5f5c8ee5
GM
1511 /* Current value of the `space-width', and 'height' properties. */
1512 it->space_width = Qnil;
1513 it->font_height = Qnil;
1514
1515 /* Are control characters displayed as `^C'? */
1516 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1517
1518 /* -1 means everything between a CR and the following line end
1519 is invisible. >0 means lines indented more than this value are
1520 invisible. */
1521 it->selective = (INTEGERP (current_buffer->selective_display)
1522 ? XFASTINT (current_buffer->selective_display)
1523 : (!NILP (current_buffer->selective_display)
1524 ? -1 : 0));
1525 it->selective_display_ellipsis_p
1526 = !NILP (current_buffer->selective_display_ellipses);
1527
1528 /* Display table to use. */
1529 it->dp = window_display_table (w);
1530
1531 /* Are multibyte characters enabled in current_buffer? */
1532 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1533
1534 /* Non-zero if we should highlight the region. */
1535 highlight_region_p
1536 = (!NILP (Vtransient_mark_mode)
1537 && !NILP (current_buffer->mark_active)
1538 && XMARKER (current_buffer->mark)->buffer != 0);
1539
1540 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1541 start and end of a visible region in window IT->w. Set both to
1542 -1 to indicate no region. */
1543 if (highlight_region_p
1544 /* Maybe highlight only in selected window. */
1545 && (/* Either show region everywhere. */
1546 highlight_nonselected_windows
1547 /* Or show region in the selected window. */
1548 || w == XWINDOW (selected_window)
1549 /* Or show the region if we are in the mini-buffer and W is
1550 the window the mini-buffer refers to. */
1551 || (MINI_WINDOW_P (XWINDOW (selected_window))
d8731202 1552 && WINDOWP (Vminibuf_scroll_window)
5f5c8ee5
GM
1553 && w == XWINDOW (Vminibuf_scroll_window))))
1554 {
1555 int charpos = marker_position (current_buffer->mark);
1556 it->region_beg_charpos = min (PT, charpos);
1557 it->region_end_charpos = max (PT, charpos);
1558 }
1559 else
1560 it->region_beg_charpos = it->region_end_charpos = -1;
1561
1562 /* Get the position at which the redisplay_end_trigger hook should
1563 be run, if it is to be run at all. */
1564 if (MARKERP (w->redisplay_end_trigger)
1565 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1566 it->redisplay_end_trigger_charpos
1567 = marker_position (w->redisplay_end_trigger);
1568 else if (INTEGERP (w->redisplay_end_trigger))
1569 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1570
1571 /* Correct bogus values of tab_width. */
1572 it->tab_width = XINT (current_buffer->tab_width);
1573 if (it->tab_width <= 0 || it->tab_width > 1000)
1574 it->tab_width = 8;
1575
1576 /* Are lines in the display truncated? */
1577 it->truncate_lines_p
1578 = (base_face_id != DEFAULT_FACE_ID
1579 || XINT (it->w->hscroll)
1580 || (truncate_partial_width_windows
1581 && !WINDOW_FULL_WIDTH_P (it->w))
1582 || !NILP (current_buffer->truncate_lines));
1583
1584 /* Get dimensions of truncation and continuation glyphs. These are
1585 displayed as bitmaps under X, so we don't need them for such
1586 frames. */
1587 if (!FRAME_WINDOW_P (it->f))
1588 {
1589 if (it->truncate_lines_p)
1590 {
1591 /* We will need the truncation glyph. */
1592 xassert (it->glyph_row == NULL);
1593 produce_special_glyphs (it, IT_TRUNCATION);
1594 it->truncation_pixel_width = it->pixel_width;
1595 }
1596 else
1597 {
1598 /* We will need the continuation glyph. */
1599 xassert (it->glyph_row == NULL);
1600 produce_special_glyphs (it, IT_CONTINUATION);
1601 it->continuation_pixel_width = it->pixel_width;
1602 }
1603
1604 /* Reset these values to zero becaue the produce_special_glyphs
1605 above has changed them. */
1606 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1607 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1608 }
1609
1610 /* Set this after getting the dimensions of truncation and
1611 continuation glyphs, so that we don't produce glyphs when calling
1612 produce_special_glyphs, above. */
1613 it->glyph_row = row;
1614 it->area = TEXT_AREA;
1615
1616 /* Get the dimensions of the display area. The display area
1617 consists of the visible window area plus a horizontally scrolled
1618 part to the left of the window. All x-values are relative to the
1619 start of this total display area. */
1620 if (base_face_id != DEFAULT_FACE_ID)
1621 {
1622 /* Mode lines, menu bar in terminal frames. */
1623 it->first_visible_x = 0;
1624 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1625 }
1626 else
1627 {
1628 it->first_visible_x
1629 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1630 it->last_visible_x = (it->first_visible_x
1631 + window_box_width (w, TEXT_AREA));
1632
1633 /* If we truncate lines, leave room for the truncator glyph(s) at
1634 the right margin. Otherwise, leave room for the continuation
1635 glyph(s). Truncation and continuation glyphs are not inserted
1636 for window-based redisplay. */
1637 if (!FRAME_WINDOW_P (it->f))
1638 {
1639 if (it->truncate_lines_p)
1640 it->last_visible_x -= it->truncation_pixel_width;
1641 else
1642 it->last_visible_x -= it->continuation_pixel_width;
1643 }
1644
045dee35
GM
1645 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1646 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1647 }
1648
1649 /* Leave room for a border glyph. */
1650 if (!FRAME_WINDOW_P (it->f)
1651 && !WINDOW_RIGHTMOST_P (it->w))
1652 it->last_visible_x -= 1;
1653
1654 it->last_visible_y = window_text_bottom_y (w);
1655
1656 /* For mode lines and alike, arrange for the first glyph having a
1657 left box line if the face specifies a box. */
1658 if (base_face_id != DEFAULT_FACE_ID)
1659 {
1660 struct face *face;
1661
1662 it->face_id = base_face_id;
1663
1664 /* If we have a boxed mode line, make the first character appear
1665 with a left box line. */
1666 face = FACE_FROM_ID (it->f, base_face_id);
1667 if (face->box != FACE_NO_BOX)
1668 it->start_of_box_run_p = 1;
1669 }
1670
1671 /* If a buffer position was specified, set the iterator there,
1672 getting overlays and face properties from that position. */
1673 if (charpos > 0)
1674 {
1675 it->end_charpos = ZV;
1676 it->face_id = -1;
1677 IT_CHARPOS (*it) = charpos;
1678
1679 /* Compute byte position if not specified. */
1680 if (bytepos <= 0)
1681 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1682 else
1683 IT_BYTEPOS (*it) = bytepos;
1684
1685 /* Compute faces etc. */
1686 reseat (it, it->current.pos, 1);
1687 }
1688
1689 CHECK_IT (it);
1690}
1691
1692
1693/* Initialize IT for the display of window W with window start POS. */
1694
1695void
1696start_display (it, w, pos)
1697 struct it *it;
1698 struct window *w;
1699 struct text_pos pos;
1700{
1701 int start_at_line_beg_p;
1702 struct glyph_row *row;
045dee35 1703 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1704 int first_y;
1705
1706 row = w->desired_matrix->rows + first_vpos;
1707 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1708 first_y = it->current_y;
1709
1710 /* If window start is not at a line start, move back to the line
1711 start. This makes sure that we take continuation lines into
1712 account. */
1713 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1714 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1715 if (!start_at_line_beg_p)
1716 reseat_at_previous_visible_line_start (it);
1717
5f5c8ee5
GM
1718 /* If window start is not at a line start, skip forward to POS to
1719 get the correct continuation_lines_width and current_x. */
1720 if (!start_at_line_beg_p)
1721 {
1722 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1723
1724 /* If lines are continued, this line may end in the middle of a
1725 multi-glyph character (e.g. a control character displayed as
1726 \003, or in the middle of an overlay string). In this case
1727 move_it_to above will not have taken us to the start of
1728 the continuation line but to the end of the continued line. */
b28cb6ed 1729 if (!it->truncate_lines_p)
5f5c8ee5 1730 {
b28cb6ed 1731 if (it->current_x > 0)
5f5c8ee5 1732 {
b28cb6ed
GM
1733 if (it->current.dpvec_index >= 0
1734 || it->current.overlay_string_index >= 0)
1735 {
cafafe0b 1736 set_iterator_to_next (it, 1);
b28cb6ed
GM
1737 move_it_in_display_line_to (it, -1, -1, 0);
1738 }
1739
1740 it->continuation_lines_width += it->current_x;
5f5c8ee5 1741 }
b28cb6ed
GM
1742
1743 /* We're starting a new display line, not affected by the
1744 height of the continued line, so clear the appropriate
1745 fields in the iterator structure. */
1746 it->max_ascent = it->max_descent = 0;
1747 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5
GM
1748 }
1749
1750 it->current_y = first_y;
1751 it->vpos = 0;
1752 it->current_x = it->hpos = 0;
1753 }
1754
1755#if 0 /* Don't assert the following because start_display is sometimes
1756 called intentionally with a window start that is not at a
1757 line start. Please leave this code in as a comment. */
1758
1759 /* Window start should be on a line start, now. */
1760 xassert (it->continuation_lines_width
1761 || IT_CHARPOS (it) == BEGV
1762 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1763#endif /* 0 */
1764}
1765
1766
4bde0ebb
GM
1767/* Return 1 if POS is a position in ellipses displayed for invisible
1768 text. W is the window we display, for text property lookup. */
5f5c8ee5 1769
4bde0ebb
GM
1770static int
1771in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 1772 struct display_pos *pos;
4bde0ebb 1773 struct window *w;
5f5c8ee5 1774{
ac90c44f 1775 Lisp_Object prop, window;
4bde0ebb
GM
1776 int ellipses_p = 0;
1777 int charpos = CHARPOS (pos->pos);
ac90c44f
GM
1778
1779 /* If POS specifies a position in a display vector, this might
1780 be for an ellipsis displayed for invisible text. We won't
1781 get the iterator set up for delivering that ellipsis unless
1782 we make sure that it gets aware of the invisible text. */
1783 if (pos->dpvec_index >= 0
1784 && pos->overlay_string_index < 0
1785 && CHARPOS (pos->string_pos) < 0
1786 && charpos > BEGV
1787 && (XSETWINDOW (window, w),
1788 prop = Fget_char_property (make_number (charpos),
1789 Qinvisible, window),
20fbd925 1790 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
1791 {
1792 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1793 window);
1794 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1795 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
4bde0ebb
GM
1796 ellipses_p = 1;
1797 }
1798
1799 return ellipses_p;
1800}
1801
1802
1803/* Initialize IT for stepping through current_buffer in window W,
1804 starting at position POS that includes overlay string and display
1805 vector/ control character translation position information. */
1806
1807static void
1808init_from_display_pos (it, w, pos)
1809 struct it *it;
1810 struct window *w;
1811 struct display_pos *pos;
1812{
1813 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1814
1815 /* If POS specifies a position in a display vector, this might
1816 be for an ellipsis displayed for invisible text. We won't
1817 get the iterator set up for delivering that ellipsis unless
1818 we make sure that it gets aware of the invisible text. */
1819 if (in_ellipses_for_invisible_text_p (pos, w))
1820 {
1821 --charpos;
1822 bytepos = 0;
ac90c44f
GM
1823 }
1824
5f5c8ee5
GM
1825 /* Keep in mind: the call to reseat in init_iterator skips invisible
1826 text, so we might end up at a position different from POS. This
1827 is only a problem when POS is a row start after a newline and an
1828 overlay starts there with an after-string, and the overlay has an
1829 invisible property. Since we don't skip invisible text in
1830 display_line and elsewhere immediately after consuming the
1831 newline before the row start, such a POS will not be in a string,
1832 but the call to init_iterator below will move us to the
1833 after-string. */
ac90c44f 1834 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5
GM
1835
1836 /* If position is within an overlay string, set up IT to
1837 the right overlay string. */
1838 if (pos->overlay_string_index >= 0)
1839 {
1840 int relative_index;
1841
1842 /* We already have the first chunk of overlay strings in
1843 IT->overlay_strings. Load more until the one for
1844 pos->overlay_string_index is in IT->overlay_strings. */
1845 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1846 {
1847 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1848 it->current.overlay_string_index = 0;
1849 while (n--)
1850 {
1851 load_overlay_strings (it);
1852 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1853 }
1854 }
1855
1856 it->current.overlay_string_index = pos->overlay_string_index;
1857 relative_index = (it->current.overlay_string_index
1858 % OVERLAY_STRING_CHUNK_SIZE);
1859 it->string = it->overlay_strings[relative_index];
cafafe0b 1860 xassert (STRINGP (it->string));
5f5c8ee5
GM
1861 it->current.string_pos = pos->string_pos;
1862 it->method = next_element_from_string;
1863 }
2be8f184
GM
1864 else if (it->current.overlay_string_index >= 0)
1865 {
1866 /* If POS says we're already after an overlay string ending at
1867 POS, make sure to pop the iterator because it will be in
1868 front of that overlay string. When POS is ZV, we've thereby
1869 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1870 while (it->sp)
1871 pop_it (it);
2be8f184
GM
1872 it->current.overlay_string_index = -1;
1873 it->method = next_element_from_buffer;
1874 if (CHARPOS (pos->pos) == ZV)
1875 it->overlay_strings_at_end_processed_p = 1;
1876 }
1877
1878 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1879 {
1880 /* Recorded position is not in an overlay string, but in another
1881 string. This can only be a string from a `display' property.
1882 IT should already be filled with that string. */
1883 it->current.string_pos = pos->string_pos;
1884 xassert (STRINGP (it->string));
1885 }
1886
ac90c44f
GM
1887 /* Restore position in display vector translations, control
1888 character translations or ellipses. */
5f5c8ee5
GM
1889 if (pos->dpvec_index >= 0)
1890 {
ac90c44f
GM
1891 if (it->dpvec == NULL)
1892 get_next_display_element (it);
5f5c8ee5
GM
1893 xassert (it->dpvec && it->current.dpvec_index == 0);
1894 it->current.dpvec_index = pos->dpvec_index;
1895 }
1896
1897 CHECK_IT (it);
1898}
1899
1900
1901/* Initialize IT for stepping through current_buffer in window W
1902 starting at ROW->start. */
1903
1904static void
1905init_to_row_start (it, w, row)
1906 struct it *it;
1907 struct window *w;
1908 struct glyph_row *row;
1909{
1910 init_from_display_pos (it, w, &row->start);
1911 it->continuation_lines_width = row->continuation_lines_width;
1912 CHECK_IT (it);
1913}
1914
1915
1916/* Initialize IT for stepping through current_buffer in window W
1917 starting in the line following ROW, i.e. starting at ROW->end. */
1918
1919static void
1920init_to_row_end (it, w, row)
1921 struct it *it;
1922 struct window *w;
1923 struct glyph_row *row;
1924{
1925 init_from_display_pos (it, w, &row->end);
1926
1927 if (row->continued_p)
1928 it->continuation_lines_width = (row->continuation_lines_width
1929 + row->pixel_width);
1930 CHECK_IT (it);
1931}
1932
1933
1934
1935\f
1936/***********************************************************************
1937 Text properties
1938 ***********************************************************************/
1939
1940/* Called when IT reaches IT->stop_charpos. Handle text property and
1941 overlay changes. Set IT->stop_charpos to the next position where
1942 to stop. */
1943
1944static void
1945handle_stop (it)
1946 struct it *it;
1947{
1948 enum prop_handled handled;
1949 int handle_overlay_change_p = 1;
1950 struct props *p;
1951
1952 it->dpvec = NULL;
1953 it->current.dpvec_index = -1;
1954
1955 do
1956 {
1957 handled = HANDLED_NORMALLY;
1958
1959 /* Call text property handlers. */
1960 for (p = it_props; p->handler; ++p)
1961 {
1962 handled = p->handler (it);
2970b9be 1963
5f5c8ee5
GM
1964 if (handled == HANDLED_RECOMPUTE_PROPS)
1965 break;
1966 else if (handled == HANDLED_RETURN)
1967 return;
1968 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1969 handle_overlay_change_p = 0;
1970 }
1971
1972 if (handled != HANDLED_RECOMPUTE_PROPS)
1973 {
1974 /* Don't check for overlay strings below when set to deliver
1975 characters from a display vector. */
1976 if (it->method == next_element_from_display_vector)
1977 handle_overlay_change_p = 0;
1978
1979 /* Handle overlay changes. */
1980 if (handle_overlay_change_p)
1981 handled = handle_overlay_change (it);
1982
1983 /* Determine where to stop next. */
1984 if (handled == HANDLED_NORMALLY)
1985 compute_stop_pos (it);
1986 }
1987 }
1988 while (handled == HANDLED_RECOMPUTE_PROPS);
1989}
1990
1991
1992/* Compute IT->stop_charpos from text property and overlay change
1993 information for IT's current position. */
1994
1995static void
1996compute_stop_pos (it)
1997 struct it *it;
1998{
1999 register INTERVAL iv, next_iv;
2000 Lisp_Object object, limit, position;
2001
2002 /* If nowhere else, stop at the end. */
2003 it->stop_charpos = it->end_charpos;
2004
2005 if (STRINGP (it->string))
2006 {
2007 /* Strings are usually short, so don't limit the search for
2008 properties. */
2009 object = it->string;
2010 limit = Qnil;
ac90c44f 2011 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2012 }
2013 else
2014 {
2015 int charpos;
2016
2017 /* If next overlay change is in front of the current stop pos
2018 (which is IT->end_charpos), stop there. Note: value of
2019 next_overlay_change is point-max if no overlay change
2020 follows. */
2021 charpos = next_overlay_change (IT_CHARPOS (*it));
2022 if (charpos < it->stop_charpos)
2023 it->stop_charpos = charpos;
2024
2025 /* If showing the region, we have to stop at the region
2026 start or end because the face might change there. */
2027 if (it->region_beg_charpos > 0)
2028 {
2029 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2030 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2031 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2032 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2033 }
2034
2035 /* Set up variables for computing the stop position from text
2036 property changes. */
2037 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2038 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2039 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2040
2041 }
2042
2043 /* Get the interval containing IT's position. Value is a null
2044 interval if there isn't such an interval. */
2045 iv = validate_interval_range (object, &position, &position, 0);
2046 if (!NULL_INTERVAL_P (iv))
2047 {
2048 Lisp_Object values_here[LAST_PROP_IDX];
2049 struct props *p;
2050
2051 /* Get properties here. */
2052 for (p = it_props; p->handler; ++p)
2053 values_here[p->idx] = textget (iv->plist, *p->name);
2054
2055 /* Look for an interval following iv that has different
2056 properties. */
2057 for (next_iv = next_interval (iv);
2058 (!NULL_INTERVAL_P (next_iv)
2059 && (NILP (limit)
2060 || XFASTINT (limit) > next_iv->position));
2061 next_iv = next_interval (next_iv))
2062 {
2063 for (p = it_props; p->handler; ++p)
2064 {
2065 Lisp_Object new_value;
2066
2067 new_value = textget (next_iv->plist, *p->name);
2068 if (!EQ (values_here[p->idx], new_value))
2069 break;
2070 }
2071
2072 if (p->handler)
2073 break;
2074 }
2075
2076 if (!NULL_INTERVAL_P (next_iv))
2077 {
2078 if (INTEGERP (limit)
2079 && next_iv->position >= XFASTINT (limit))
2080 /* No text property change up to limit. */
2081 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2082 else
2083 /* Text properties change in next_iv. */
2084 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2085 }
2086 }
2087
2088 xassert (STRINGP (it->string)
2089 || (it->stop_charpos >= BEGV
2090 && it->stop_charpos >= IT_CHARPOS (*it)));
2091}
2092
2093
2094/* Return the position of the next overlay change after POS in
2095 current_buffer. Value is point-max if no overlay change
2096 follows. This is like `next-overlay-change' but doesn't use
2097 xmalloc. */
2098
2099static int
2100next_overlay_change (pos)
2101 int pos;
2102{
2103 int noverlays;
2104 int endpos;
2105 Lisp_Object *overlays;
2106 int len;
2107 int i;
2108
2109 /* Get all overlays at the given position. */
2110 len = 10;
2111 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2112 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2113 if (noverlays > len)
2114 {
2115 len = noverlays;
2116 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2117 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2118 }
2119
2120 /* If any of these overlays ends before endpos,
2121 use its ending point instead. */
2122 for (i = 0; i < noverlays; ++i)
2123 {
2124 Lisp_Object oend;
2125 int oendpos;
2126
2127 oend = OVERLAY_END (overlays[i]);
2128 oendpos = OVERLAY_POSITION (oend);
2129 endpos = min (endpos, oendpos);
2130 }
2131
2132 return endpos;
2133}
2134
2135
2136\f
2137/***********************************************************************
2138 Fontification
2139 ***********************************************************************/
2140
2141/* Handle changes in the `fontified' property of the current buffer by
2142 calling hook functions from Qfontification_functions to fontify
2143 regions of text. */
2144
2145static enum prop_handled
2146handle_fontified_prop (it)
2147 struct it *it;
2148{
2149 Lisp_Object prop, pos;
2150 enum prop_handled handled = HANDLED_NORMALLY;
2151
2152 /* Get the value of the `fontified' property at IT's current buffer
2153 position. (The `fontified' property doesn't have a special
2154 meaning in strings.) If the value is nil, call functions from
2155 Qfontification_functions. */
2156 if (!STRINGP (it->string)
2157 && it->s == NULL
2158 && !NILP (Vfontification_functions)
085536c2 2159 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2160 && (pos = make_number (IT_CHARPOS (*it)),
2161 prop = Fget_char_property (pos, Qfontified, Qnil),
2162 NILP (prop)))
2163 {
2d27dae2 2164 int count = BINDING_STACK_SIZE ();
085536c2
GM
2165 Lisp_Object val;
2166
2167 val = Vfontification_functions;
2168 specbind (Qfontification_functions, Qnil);
2169 specbind (Qafter_change_functions, Qnil);
2170
2171 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2172 safe_call1 (val, pos);
085536c2
GM
2173 else
2174 {
2175 Lisp_Object globals, fn;
2176 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2177
085536c2
GM
2178 globals = Qnil;
2179 GCPRO2 (val, globals);
2180
2181 for (; CONSP (val); val = XCDR (val))
2182 {
2183 fn = XCAR (val);
2184
2185 if (EQ (fn, Qt))
2186 {
2187 /* A value of t indicates this hook has a local
2188 binding; it means to run the global binding too.
2189 In a global value, t should not occur. If it
2190 does, we must ignore it to avoid an endless
2191 loop. */
2192 for (globals = Fdefault_value (Qfontification_functions);
2193 CONSP (globals);
2194 globals = XCDR (globals))
2195 {
2196 fn = XCAR (globals);
2197 if (!EQ (fn, Qt))
116d6f5c 2198 safe_call1 (fn, pos);
085536c2
GM
2199 }
2200 }
2201 else
116d6f5c 2202 safe_call1 (fn, pos);
085536c2
GM
2203 }
2204
2205 UNGCPRO;
2206 }
2207
2208 unbind_to (count, Qnil);
5f5c8ee5
GM
2209
2210 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2211 something. This avoids an endless loop if they failed to
2212 fontify the text for which reason ever. */
2213 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2214 handled = HANDLED_RECOMPUTE_PROPS;
2215 }
2216
2217 return handled;
2218}
2219
2220
2221\f
2222/***********************************************************************
2223 Faces
2224 ***********************************************************************/
2225
2226/* Set up iterator IT from face properties at its current position.
2227 Called from handle_stop. */
2228
2229static enum prop_handled
2230handle_face_prop (it)
2231 struct it *it;
2232{
2233 int new_face_id, next_stop;
2234
2235 if (!STRINGP (it->string))
2236 {
2237 new_face_id
2238 = face_at_buffer_position (it->w,
2239 IT_CHARPOS (*it),
2240 it->region_beg_charpos,
2241 it->region_end_charpos,
2242 &next_stop,
2243 (IT_CHARPOS (*it)
2244 + TEXT_PROP_DISTANCE_LIMIT),
2245 0);
2246
2247 /* Is this a start of a run of characters with box face?
2248 Caveat: this can be called for a freshly initialized
2249 iterator; face_id is -1 is this case. We know that the new
2250 face will not change until limit, i.e. if the new face has a
2251 box, all characters up to limit will have one. But, as
2252 usual, we don't know whether limit is really the end. */
2253 if (new_face_id != it->face_id)
2254 {
2255 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2256
2257 /* If new face has a box but old face has not, this is
2258 the start of a run of characters with box, i.e. it has
2259 a shadow on the left side. The value of face_id of the
2260 iterator will be -1 if this is the initial call that gets
2261 the face. In this case, we have to look in front of IT's
2262 position and see whether there is a face != new_face_id. */
2263 it->start_of_box_run_p
2264 = (new_face->box != FACE_NO_BOX
2265 && (it->face_id >= 0
2266 || IT_CHARPOS (*it) == BEG
2267 || new_face_id != face_before_it_pos (it)));
2268 it->face_box_p = new_face->box != FACE_NO_BOX;
2269 }
2270 }
2271 else
2272 {
06a12811
GM
2273 int base_face_id, bufpos;
2274
2275 if (it->current.overlay_string_index >= 0)
2276 bufpos = IT_CHARPOS (*it);
2277 else
2278 bufpos = 0;
2279
2280 /* For strings from a buffer, i.e. overlay strings or strings
2281 from a `display' property, use the face at IT's current
2282 buffer position as the base face to merge with, so that
2283 overlay strings appear in the same face as surrounding
2284 text, unless they specify their own faces. */
2285 base_face_id = underlying_face_id (it);
2286
2287 new_face_id = face_at_string_position (it->w,
2288 it->string,
2289 IT_STRING_CHARPOS (*it),
2290 bufpos,
2291 it->region_beg_charpos,
2292 it->region_end_charpos,
2293 &next_stop,
5de7c6f2 2294 base_face_id, 0);
5f5c8ee5
GM
2295
2296#if 0 /* This shouldn't be neccessary. Let's check it. */
2297 /* If IT is used to display a mode line we would really like to
2298 use the mode line face instead of the frame's default face. */
2299 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2300 && new_face_id == DEFAULT_FACE_ID)
2301 new_face_id = MODE_LINE_FACE_ID;
2302#endif
2303
2304 /* Is this a start of a run of characters with box? Caveat:
2305 this can be called for a freshly allocated iterator; face_id
2306 is -1 is this case. We know that the new face will not
2307 change until the next check pos, i.e. if the new face has a
2308 box, all characters up to that position will have a
2309 box. But, as usual, we don't know whether that position
2310 is really the end. */
2311 if (new_face_id != it->face_id)
2312 {
2313 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2314 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2315
2316 /* If new face has a box but old face hasn't, this is the
2317 start of a run of characters with box, i.e. it has a
2318 shadow on the left side. */
2319 it->start_of_box_run_p
2320 = new_face->box && (old_face == NULL || !old_face->box);
2321 it->face_box_p = new_face->box != FACE_NO_BOX;
2322 }
2323 }
2324
2325 it->face_id = new_face_id;
5f5c8ee5
GM
2326 return HANDLED_NORMALLY;
2327}
2328
2329
06a12811
GM
2330/* Return the ID of the face ``underlying'' IT's current position,
2331 which is in a string. If the iterator is associated with a
2332 buffer, return the face at IT's current buffer position.
2333 Otherwise, use the iterator's base_face_id. */
2334
2335static int
2336underlying_face_id (it)
2337 struct it *it;
2338{
2339 int face_id = it->base_face_id, i;
2340
2341 xassert (STRINGP (it->string));
2342
2343 for (i = it->sp - 1; i >= 0; --i)
2344 if (NILP (it->stack[i].string))
2345 face_id = it->stack[i].face_id;
2346
2347 return face_id;
2348}
2349
2350
5f5c8ee5
GM
2351/* Compute the face one character before or after the current position
2352 of IT. BEFORE_P non-zero means get the face in front of IT's
2353 position. Value is the id of the face. */
2354
2355static int
2356face_before_or_after_it_pos (it, before_p)
2357 struct it *it;
2358 int before_p;
2359{
2360 int face_id, limit;
2361 int next_check_charpos;
2362 struct text_pos pos;
2363
2364 xassert (it->s == NULL);
2365
2366 if (STRINGP (it->string))
2367 {
06a12811
GM
2368 int bufpos, base_face_id;
2369
5f5c8ee5
GM
2370 /* No face change past the end of the string (for the case
2371 we are padding with spaces). No face change before the
2372 string start. */
2373 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2374 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2375 return it->face_id;
2376
2377 /* Set pos to the position before or after IT's current position. */
2378 if (before_p)
2379 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2380 else
260a86a0
KH
2381 /* For composition, we must check the character after the
2382 composition. */
2383 pos = (it->what == IT_COMPOSITION
2384 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2385 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2386
06a12811
GM
2387 if (it->current.overlay_string_index >= 0)
2388 bufpos = IT_CHARPOS (*it);
2389 else
2390 bufpos = 0;
2391
2392 base_face_id = underlying_face_id (it);
2393
5f5c8ee5 2394 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2395 face_id = face_at_string_position (it->w,
2396 it->string,
2397 CHARPOS (pos),
2398 bufpos,
2399 it->region_beg_charpos,
2400 it->region_end_charpos,
2401 &next_check_charpos,
5de7c6f2 2402 base_face_id, 0);
5f5c8ee5
GM
2403
2404 /* Correct the face for charsets different from ASCII. Do it
2405 for the multibyte case only. The face returned above is
2406 suitable for unibyte text if IT->string is unibyte. */
2407 if (STRING_MULTIBYTE (it->string))
2408 {
2409 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2410 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
2411 int c, len;
2412 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 2413
4fdb80f2 2414 c = string_char_and_length (p, rest, &len);
980806b6 2415 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2416 }
2417 }
2418 else
2419 {
70851746
GM
2420 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2421 || (IT_CHARPOS (*it) <= BEGV && before_p))
2422 return it->face_id;
2423
5f5c8ee5
GM
2424 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2425 pos = it->current.pos;
2426
2427 if (before_p)
c1005d06 2428 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2429 else
260a86a0
KH
2430 {
2431 if (it->what == IT_COMPOSITION)
2432 /* For composition, we must check the position after the
2433 composition. */
2434 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2435 else
c1005d06 2436 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2437 }
06a12811 2438
5f5c8ee5
GM
2439 /* Determine face for CHARSET_ASCII, or unibyte. */
2440 face_id = face_at_buffer_position (it->w,
2441 CHARPOS (pos),
2442 it->region_beg_charpos,
2443 it->region_end_charpos,
2444 &next_check_charpos,
2445 limit, 0);
2446
2447 /* Correct the face for charsets different from ASCII. Do it
2448 for the multibyte case only. The face returned above is
2449 suitable for unibyte text if current_buffer is unibyte. */
2450 if (it->multibyte_p)
2451 {
980806b6
KH
2452 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2453 struct face *face = FACE_FROM_ID (it->f, face_id);
2454 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2455 }
2456 }
2457
2458 return face_id;
2459}
2460
2461
2462\f
2463/***********************************************************************
2464 Invisible text
2465 ***********************************************************************/
2466
2467/* Set up iterator IT from invisible properties at its current
2468 position. Called from handle_stop. */
2469
2470static enum prop_handled
2471handle_invisible_prop (it)
2472 struct it *it;
2473{
2474 enum prop_handled handled = HANDLED_NORMALLY;
2475
2476 if (STRINGP (it->string))
2477 {
2478 extern Lisp_Object Qinvisible;
2479 Lisp_Object prop, end_charpos, limit, charpos;
2480
2481 /* Get the value of the invisible text property at the
2482 current position. Value will be nil if there is no such
2483 property. */
ac90c44f 2484 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2485 prop = Fget_text_property (charpos, Qinvisible, it->string);
2486
eadc0bf8
GM
2487 if (!NILP (prop)
2488 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2489 {
2490 handled = HANDLED_RECOMPUTE_PROPS;
2491
2492 /* Get the position at which the next change of the
2493 invisible text property can be found in IT->string.
2494 Value will be nil if the property value is the same for
2495 all the rest of IT->string. */
2496 XSETINT (limit, XSTRING (it->string)->size);
2497 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2498 it->string, limit);
2499
2500 /* Text at current position is invisible. The next
2501 change in the property is at position end_charpos.
2502 Move IT's current position to that position. */
2503 if (INTEGERP (end_charpos)
2504 && XFASTINT (end_charpos) < XFASTINT (limit))
2505 {
2506 struct text_pos old;
2507 old = it->current.string_pos;
2508 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2509 compute_string_pos (&it->current.string_pos, old, it->string);
2510 }
2511 else
2512 {
2513 /* The rest of the string is invisible. If this is an
2514 overlay string, proceed with the next overlay string
2515 or whatever comes and return a character from there. */
2516 if (it->current.overlay_string_index >= 0)
2517 {
2518 next_overlay_string (it);
2519 /* Don't check for overlay strings when we just
2520 finished processing them. */
2521 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2522 }
2523 else
2524 {
2525 struct Lisp_String *s = XSTRING (it->string);
2526 IT_STRING_CHARPOS (*it) = s->size;
2527 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2528 }
2529 }
2530 }
2531 }
2532 else
2533 {
2534 int visible_p, newpos, next_stop;
2535 Lisp_Object pos, prop;
2536
2537 /* First of all, is there invisible text at this position? */
ac90c44f 2538 pos = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2539 prop = Fget_char_property (pos, Qinvisible, it->window);
2540
2541 /* If we are on invisible text, skip over it. */
eadc0bf8
GM
2542 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2543 && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2544 {
2545 /* Record whether we have to display an ellipsis for the
2546 invisible text. */
2547 int display_ellipsis_p
2548 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2549
2550 handled = HANDLED_RECOMPUTE_PROPS;
2551
2552 /* Loop skipping over invisible text. The loop is left at
2553 ZV or with IT on the first char being visible again. */
2554 do
2555 {
2556 /* Try to skip some invisible text. Return value is the
2557 position reached which can be equal to IT's position
2558 if there is nothing invisible here. This skips both
2559 over invisible text properties and overlays with
2560 invisible property. */
2561 newpos = skip_invisible (IT_CHARPOS (*it),
2562 &next_stop, ZV, it->window);
2563
2564 /* If we skipped nothing at all we weren't at invisible
2565 text in the first place. If everything to the end of
2566 the buffer was skipped, end the loop. */
2567 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2568 visible_p = 1;
2569 else
2570 {
2571 /* We skipped some characters but not necessarily
2572 all there are. Check if we ended up on visible
2573 text. Fget_char_property returns the property of
2574 the char before the given position, i.e. if we
2575 get visible_p = 1, this means that the char at
2576 newpos is visible. */
ac90c44f 2577 pos = make_number (newpos);
5f5c8ee5
GM
2578 prop = Fget_char_property (pos, Qinvisible, it->window);
2579 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2580 }
2581
2582 /* If we ended up on invisible text, proceed to
2583 skip starting with next_stop. */
2584 if (!visible_p)
2585 IT_CHARPOS (*it) = next_stop;
2586 }
2587 while (!visible_p);
2588
2589 /* The position newpos is now either ZV or on visible text. */
2590 IT_CHARPOS (*it) = newpos;
2591 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2592
2593 /* Maybe return `...' next for the end of the invisible text. */
2594 if (display_ellipsis_p)
2595 {
2596 if (it->dp
2597 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2598 {
2599 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2600 it->dpvec = v->contents;
2601 it->dpend = v->contents + v->size;
2602 }
2603 else
2604 {
2605 /* Default `...'. */
2606 it->dpvec = default_invis_vector;
2607 it->dpend = default_invis_vector + 3;
2608 }
2609
2610 /* The ellipsis display does not replace the display of
2611 the character at the new position. Indicate this by
2612 setting IT->dpvec_char_len to zero. */
2613 it->dpvec_char_len = 0;
2614
2615 it->current.dpvec_index = 0;
2616 it->method = next_element_from_display_vector;
2617 }
2618 }
2619 }
2620
2621 return handled;
2622}
2623
2624
2625\f
2626/***********************************************************************
2627 'display' property
2628 ***********************************************************************/
2629
2630/* Set up iterator IT from `display' property at its current position.
2631 Called from handle_stop. */
2632
2633static enum prop_handled
2634handle_display_prop (it)
2635 struct it *it;
2636{
2637 Lisp_Object prop, object;
2638 struct text_pos *position;
a61b7058 2639 int display_replaced_p = 0;
5f5c8ee5
GM
2640
2641 if (STRINGP (it->string))
2642 {
2643 object = it->string;
2644 position = &it->current.string_pos;
2645 }
2646 else
2647 {
221dd3e7 2648 object = it->w->buffer;
5f5c8ee5
GM
2649 position = &it->current.pos;
2650 }
2651
2652 /* Reset those iterator values set from display property values. */
2653 it->font_height = Qnil;
2654 it->space_width = Qnil;
2655 it->voffset = 0;
2656
2657 /* We don't support recursive `display' properties, i.e. string
2658 values that have a string `display' property, that have a string
2659 `display' property etc. */
2660 if (!it->string_from_display_prop_p)
2661 it->area = TEXT_AREA;
2662
2663 prop = Fget_char_property (make_number (position->charpos),
2664 Qdisplay, object);
2665 if (NILP (prop))
2666 return HANDLED_NORMALLY;
2667
f3751a65 2668 if (CONSP (prop)
12700f40
GM
2669 /* Simple properties. */
2670 && !EQ (XCAR (prop), Qimage)
2671 && !EQ (XCAR (prop), Qspace)
2672 && !EQ (XCAR (prop), Qwhen)
2673 && !EQ (XCAR (prop), Qspace_width)
2674 && !EQ (XCAR (prop), Qheight)
2675 && !EQ (XCAR (prop), Qraise)
2676 /* Marginal area specifications. */
2677 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2678 && !NILP (XCAR (prop)))
5f5c8ee5 2679 {
a61b7058 2680 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 2681 {
a61b7058
GM
2682 if (handle_single_display_prop (it, XCAR (prop), object,
2683 position, display_replaced_p))
2684 display_replaced_p = 1;
5f5c8ee5
GM
2685 }
2686 }
2687 else if (VECTORP (prop))
2688 {
2689 int i;
a61b7058
GM
2690 for (i = 0; i < ASIZE (prop); ++i)
2691 if (handle_single_display_prop (it, AREF (prop, i), object,
2692 position, display_replaced_p))
2693 display_replaced_p = 1;
5f5c8ee5
GM
2694 }
2695 else
2696 {
a61b7058
GM
2697 if (handle_single_display_prop (it, prop, object, position, 0))
2698 display_replaced_p = 1;
5f5c8ee5
GM
2699 }
2700
a61b7058 2701 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
2702}
2703
2704
6c577098 2705/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2706 at START_POS in OBJECT. */
2707
2708static struct text_pos
2709display_prop_end (it, object, start_pos)
2710 struct it *it;
2711 Lisp_Object object;
2712 struct text_pos start_pos;
2713{
2714 Lisp_Object end;
2715 struct text_pos end_pos;
5f5c8ee5 2716
016b5642
MB
2717 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2718 Qdisplay, object, Qnil);
6c577098
GM
2719 CHARPOS (end_pos) = XFASTINT (end);
2720 if (STRINGP (object))
5f5c8ee5
GM
2721 compute_string_pos (&end_pos, start_pos, it->string);
2722 else
6c577098 2723 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2724
2725 return end_pos;
2726}
2727
2728
2729/* Set up IT from a single `display' sub-property value PROP. OBJECT
2730 is the object in which the `display' property was found. *POSITION
a61b7058
GM
2731 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2732 means that we previously saw a display sub-property which already
2733 replaced text display with something else, for example an image;
2734 ignore such properties after the first one has been processed.
5f5c8ee5
GM
2735
2736 If PROP is a `space' or `image' sub-property, set *POSITION to the
2737 end position of the `display' property.
2738
a61b7058
GM
2739 Value is non-zero something was found which replaces the display
2740 of buffer or string text. */
5f5c8ee5
GM
2741
2742static int
a61b7058
GM
2743handle_single_display_prop (it, prop, object, position,
2744 display_replaced_before_p)
5f5c8ee5
GM
2745 struct it *it;
2746 Lisp_Object prop;
2747 Lisp_Object object;
2748 struct text_pos *position;
a61b7058 2749 int display_replaced_before_p;
5f5c8ee5
GM
2750{
2751 Lisp_Object value;
a61b7058 2752 int replaces_text_display_p = 0;
5f5c8ee5
GM
2753 Lisp_Object form;
2754
d3acf96b 2755 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2756 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2757 form = Qt;
d3acf96b 2758 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2759 {
2760 prop = XCDR (prop);
2761 if (!CONSP (prop))
2762 return 0;
2763 form = XCAR (prop);
2764 prop = XCDR (prop);
5f5c8ee5
GM
2765 }
2766
2767 if (!NILP (form) && !EQ (form, Qt))
2768 {
2769 struct gcpro gcpro1;
2770 struct text_pos end_pos, pt;
2771
5f5c8ee5 2772 GCPRO1 (form);
c7aca1ad 2773 end_pos = display_prop_end (it, object, *position);
5f5c8ee5
GM
2774
2775 /* Temporarily set point to the end position, and then evaluate
2776 the form. This makes `(eolp)' work as FORM. */
c7aca1ad
GM
2777 if (BUFFERP (object))
2778 {
2779 CHARPOS (pt) = PT;
2780 BYTEPOS (pt) = PT_BYTE;
2781 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2782 }
2783
116d6f5c 2784 form = safe_eval (form);
c7aca1ad
GM
2785
2786 if (BUFFERP (object))
2787 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
5f5c8ee5
GM
2788 UNGCPRO;
2789 }
2790
2791 if (NILP (form))
2792 return 0;
2793
2794 if (CONSP (prop)
2795 && EQ (XCAR (prop), Qheight)
2796 && CONSP (XCDR (prop)))
2797 {
e4093f38 2798 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5
GM
2799 return 0;
2800
2801 /* `(height HEIGHT)'. */
2802 it->font_height = XCAR (XCDR (prop));
2803 if (!NILP (it->font_height))
2804 {
2805 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2806 int new_height = -1;
2807
2808 if (CONSP (it->font_height)
2809 && (EQ (XCAR (it->font_height), Qplus)
2810 || EQ (XCAR (it->font_height), Qminus))
2811 && CONSP (XCDR (it->font_height))
2812 && INTEGERP (XCAR (XCDR (it->font_height))))
2813 {
2814 /* `(+ N)' or `(- N)' where N is an integer. */
2815 int steps = XINT (XCAR (XCDR (it->font_height)));
2816 if (EQ (XCAR (it->font_height), Qplus))
2817 steps = - steps;
2818 it->face_id = smaller_face (it->f, it->face_id, steps);
2819 }
0d8b31c0 2820 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2821 {
2822 /* Call function with current height as argument.
2823 Value is the new height. */
116d6f5c
GM
2824 Lisp_Object height;
2825 height = safe_call1 (it->font_height,
2826 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2827 if (NUMBERP (height))
2828 new_height = XFLOATINT (height);
5f5c8ee5
GM
2829 }
2830 else if (NUMBERP (it->font_height))
2831 {
2832 /* Value is a multiple of the canonical char height. */
2833 struct face *face;
2834
2835 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2836 new_height = (XFLOATINT (it->font_height)
2837 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2838 }
2839 else
2840 {
2841 /* Evaluate IT->font_height with `height' bound to the
2842 current specified height to get the new height. */
2843 Lisp_Object value;
2d27dae2 2844 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
2845
2846 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2847 value = safe_eval (it->font_height);
5f5c8ee5
GM
2848 unbind_to (count, Qnil);
2849
2850 if (NUMBERP (value))
2851 new_height = XFLOATINT (value);
2852 }
2853
2854 if (new_height > 0)
2855 it->face_id = face_with_height (it->f, it->face_id, new_height);
2856 }
2857 }
2858 else if (CONSP (prop)
2859 && EQ (XCAR (prop), Qspace_width)
2860 && CONSP (XCDR (prop)))
2861 {
2862 /* `(space_width WIDTH)'. */
e4093f38 2863 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2864 return 0;
5f5c8ee5
GM
2865
2866 value = XCAR (XCDR (prop));
2867 if (NUMBERP (value) && XFLOATINT (value) > 0)
2868 it->space_width = value;
2869 }
2870 else if (CONSP (prop)
2871 && EQ (XCAR (prop), Qraise)
2872 && CONSP (XCDR (prop)))
2873 {
5f5c8ee5 2874 /* `(raise FACTOR)'. */
e4093f38 2875 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2876 return 0;
5f5c8ee5 2877
fb3842a8 2878#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2879 value = XCAR (XCDR (prop));
2880 if (NUMBERP (value))
2881 {
2882 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2883 it->voffset = - (XFLOATINT (value)
1ab3e082 2884 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2885 }
2886#endif /* HAVE_WINDOW_SYSTEM */
2887 }
2888 else if (!it->string_from_display_prop_p)
2889 {
f3751a65
GM
2890 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2891 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2892 Lisp_Object location, value;
2893 struct text_pos start_pos;
2894 int valid_p;
2895
2896 /* Characters having this form of property are not displayed, so
2897 we have to find the end of the property. */
5f5c8ee5
GM
2898 start_pos = *position;
2899 *position = display_prop_end (it, object, start_pos);
15e26c76 2900 value = Qnil;
5f5c8ee5
GM
2901
2902 /* Let's stop at the new position and assume that all
2903 text properties change there. */
2904 it->stop_charpos = position->charpos;
2905
f3751a65
GM
2906 location = Qunbound;
2907 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2908 {
f3751a65
GM
2909 Lisp_Object tem;
2910
5f5c8ee5 2911 value = XCDR (prop);
f3751a65
GM
2912 if (CONSP (value))
2913 value = XCAR (value);
2914
2915 tem = XCAR (prop);
2916 if (EQ (XCAR (tem), Qmargin)
2917 && (tem = XCDR (tem),
2918 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2919 (NILP (tem)
2920 || EQ (tem, Qleft_margin)
2921 || EQ (tem, Qright_margin))))
2922 location = tem;
5f5c8ee5 2923 }
f3751a65
GM
2924
2925 if (EQ (location, Qunbound))
5f5c8ee5
GM
2926 {
2927 location = Qnil;
2928 value = prop;
2929 }
2930
2931#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 2932 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
2933 valid_p = STRINGP (value);
2934 else
2935 valid_p = (STRINGP (value)
2936 || (CONSP (value) && EQ (XCAR (value), Qspace))
2937 || valid_image_p (value));
2938#else /* not HAVE_WINDOW_SYSTEM */
2939 valid_p = STRINGP (value);
2940#endif /* not HAVE_WINDOW_SYSTEM */
2941
2942 if ((EQ (location, Qleft_margin)
2943 || EQ (location, Qright_margin)
2944 || NILP (location))
a61b7058
GM
2945 && valid_p
2946 && !display_replaced_before_p)
5f5c8ee5 2947 {
a61b7058 2948 replaces_text_display_p = 1;
a21a3943 2949
5f5c8ee5
GM
2950 /* Save current settings of IT so that we can restore them
2951 when we are finished with the glyph property value. */
2952 push_it (it);
2953
2954 if (NILP (location))
2955 it->area = TEXT_AREA;
2956 else if (EQ (location, Qleft_margin))
2957 it->area = LEFT_MARGIN_AREA;
2958 else
2959 it->area = RIGHT_MARGIN_AREA;
2960
2961 if (STRINGP (value))
2962 {
2963 it->string = value;
2964 it->multibyte_p = STRING_MULTIBYTE (it->string);
2965 it->current.overlay_string_index = -1;
2966 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2967 it->end_charpos = it->string_nchars
2968 = XSTRING (it->string)->size;
2969 it->method = next_element_from_string;
2970 it->stop_charpos = 0;
2971 it->string_from_display_prop_p = 1;
e187cf71
GM
2972 /* Say that we haven't consumed the characters with
2973 `display' property yet. The call to pop_it in
2974 set_iterator_to_next will clean this up. */
2975 *position = start_pos;
5f5c8ee5
GM
2976 }
2977 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2978 {
2979 it->method = next_element_from_stretch;
2980 it->object = value;
2981 it->current.pos = it->position = start_pos;
2982 }
2983#ifdef HAVE_WINDOW_SYSTEM
2984 else
2985 {
2986 it->what = IT_IMAGE;
2987 it->image_id = lookup_image (it->f, value);
2988 it->position = start_pos;
2989 it->object = NILP (object) ? it->w->buffer : object;
2990 it->method = next_element_from_image;
2991
02513cdd 2992 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
2993 `display' property yet. The call to pop_it in
2994 set_iterator_to_next will clean this up. */
2995 *position = start_pos;
2996 }
2997#endif /* HAVE_WINDOW_SYSTEM */
2998 }
a21a3943
GM
2999 else
3000 /* Invalid property or property not supported. Restore
3001 the position to what it was before. */
3002 *position = start_pos;
5f5c8ee5
GM
3003 }
3004
a61b7058 3005 return replaces_text_display_p;
5f5c8ee5
GM
3006}
3007
3008
06568bbf
GM
3009/* Check if PROP is a display sub-property value whose text should be
3010 treated as intangible. */
3011
3012static int
3013single_display_prop_intangible_p (prop)
3014 Lisp_Object prop;
3015{
3016 /* Skip over `when FORM'. */
3017 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3018 {
3019 prop = XCDR (prop);
3020 if (!CONSP (prop))
3021 return 0;
3022 prop = XCDR (prop);
3023 }
3024
3025 if (!CONSP (prop))
3026 return 0;
3027
3028 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3029 we don't need to treat text as intangible. */
3030 if (EQ (XCAR (prop), Qmargin))
3031 {
3032 prop = XCDR (prop);
3033 if (!CONSP (prop))
3034 return 0;
3035
3036 prop = XCDR (prop);
3037 if (!CONSP (prop)
3038 || EQ (XCAR (prop), Qleft_margin)
3039 || EQ (XCAR (prop), Qright_margin))
3040 return 0;
3041 }
3042
3043 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3044}
3045
3046
3047/* Check if PROP is a display property value whose text should be
3048 treated as intangible. */
3049
3050int
3051display_prop_intangible_p (prop)
3052 Lisp_Object prop;
3053{
3054 if (CONSP (prop)
3055 && CONSP (XCAR (prop))
3056 && !EQ (Qmargin, XCAR (XCAR (prop))))
3057 {
3058 /* A list of sub-properties. */
3059 while (CONSP (prop))
3060 {
3061 if (single_display_prop_intangible_p (XCAR (prop)))
3062 return 1;
3063 prop = XCDR (prop);
3064 }
3065 }
3066 else if (VECTORP (prop))
3067 {
3068 /* A vector of sub-properties. */
3069 int i;
a61b7058
GM
3070 for (i = 0; i < ASIZE (prop); ++i)
3071 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3072 return 1;
3073 }
3074 else
3075 return single_display_prop_intangible_p (prop);
3076
3077 return 0;
3078}
3079
74bd6d65
GM
3080
3081/* Return 1 if PROP is a display sub-property value containing STRING. */
3082
3083static int
3084single_display_prop_string_p (prop, string)
3085 Lisp_Object prop, string;
3086{
3087 extern Lisp_Object Qwhen, Qmargin;
3088
3089 if (EQ (string, prop))
3090 return 1;
3091
3092 /* Skip over `when FORM'. */
3093 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3094 {
3095 prop = XCDR (prop);
3096 if (!CONSP (prop))
3097 return 0;
3098 prop = XCDR (prop);
3099 }
3100
3101 if (CONSP (prop))
3102 /* Skip over `margin LOCATION'. */
3103 if (EQ (XCAR (prop), Qmargin))
3104 {
3105 prop = XCDR (prop);
3106 if (!CONSP (prop))
3107 return 0;
3108
3109 prop = XCDR (prop);
3110 if (!CONSP (prop))
3111 return 0;
3112 }
3113
3114 return CONSP (prop) && EQ (XCAR (prop), string);
3115}
3116
3117
3118/* Return 1 if STRING appears in the `display' property PROP. */
3119
3120static int
3121display_prop_string_p (prop, string)
3122 Lisp_Object prop, string;
3123{
3124 extern Lisp_Object Qwhen, Qmargin;
3125
3126 if (CONSP (prop)
3127 && CONSP (XCAR (prop))
3128 && !EQ (Qmargin, XCAR (XCAR (prop))))
3129 {
3130 /* A list of sub-properties. */
3131 while (CONSP (prop))
3132 {
3133 if (single_display_prop_string_p (XCAR (prop), string))
3134 return 1;
3135 prop = XCDR (prop);
3136 }
3137 }
3138 else if (VECTORP (prop))
3139 {
3140 /* A vector of sub-properties. */
3141 int i;
3142 for (i = 0; i < ASIZE (prop); ++i)
3143 if (single_display_prop_string_p (AREF (prop, i), string))
3144 return 1;
3145 }
3146 else
3147 return single_display_prop_string_p (prop, string);
3148
3149 return 0;
3150}
3151
3152
3153/* Determine from which buffer position in W's buffer STRING comes
3154 from. AROUND_CHARPOS is an approximate position where it could
3155 be from. Value is the buffer position or 0 if it couldn't be
3156 determined.
3157
3158 W's buffer must be current.
3159
3160 This function is necessary because we don't record buffer positions
3161 in glyphs generated from strings (to keep struct glyph small).
3162 This function may only use code that doesn't eval because it is
3163 called asynchronously from note_mouse_highlight. */
3164
3165int
3166string_buffer_position (w, string, around_charpos)
3167 struct window *w;
3168 Lisp_Object string;
3169 int around_charpos;
3170{
3171 Lisp_Object around = make_number (around_charpos);
3172 Lisp_Object limit, prop, pos;
3173 const int MAX_DISTANCE = 1000;
3174 int found = 0;
3175
d8731202 3176 pos = make_number (around_charpos);
74bd6d65
GM
3177 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3178 while (!found && !EQ (pos, limit))
3179 {
3180 prop = Fget_char_property (pos, Qdisplay, Qnil);
3181 if (!NILP (prop) && display_prop_string_p (prop, string))
3182 found = 1;
3183 else
3184 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3185 }
3186
3187 if (!found)
3188 {
d8731202 3189 pos = make_number (around_charpos);
74bd6d65
GM
3190 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3191 while (!found && !EQ (pos, limit))
3192 {
3193 prop = Fget_char_property (pos, Qdisplay, Qnil);
3194 if (!NILP (prop) && display_prop_string_p (prop, string))
3195 found = 1;
3196 else
3197 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3198 limit);
3199 }
3200 }
3201
3202 return found ? XINT (pos) : 0;
3203}
3204
3205
5f5c8ee5 3206\f
260a86a0
KH
3207/***********************************************************************
3208 `composition' property
3209 ***********************************************************************/
3210
3211/* Set up iterator IT from `composition' property at its current
3212 position. Called from handle_stop. */
3213
3214static enum prop_handled
3215handle_composition_prop (it)
3216 struct it *it;
3217{
3218 Lisp_Object prop, string;
3219 int pos, pos_byte, end;
3220 enum prop_handled handled = HANDLED_NORMALLY;
3221
3222 if (STRINGP (it->string))
3223 {
3224 pos = IT_STRING_CHARPOS (*it);
3225 pos_byte = IT_STRING_BYTEPOS (*it);
3226 string = it->string;
3227 }
3228 else
3229 {
3230 pos = IT_CHARPOS (*it);
3231 pos_byte = IT_BYTEPOS (*it);
3232 string = Qnil;
3233 }
3234
3235 /* If there's a valid composition and point is not inside of the
3236 composition (in the case that the composition is from the current
3237 buffer), draw a glyph composed from the composition components. */
3238 if (find_composition (pos, -1, &pos, &end, &prop, string)
3239 && COMPOSITION_VALID_P (pos, end, prop)
3240 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3241 {
3242 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3243
3244 if (id >= 0)
3245 {
3246 it->method = next_element_from_composition;
3247 it->cmp_id = id;
3248 it->cmp_len = COMPOSITION_LENGTH (prop);
3249 /* For a terminal, draw only the first character of the
3250 components. */
3251 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3252 it->len = (STRINGP (it->string)
3253 ? string_char_to_byte (it->string, end)
3254 : CHAR_TO_BYTE (end)) - pos_byte;
3255 it->stop_charpos = end;
3256 handled = HANDLED_RETURN;
3257 }
3258 }
3259
3260 return handled;
3261}
3262
3263
3264\f
5f5c8ee5
GM
3265/***********************************************************************
3266 Overlay strings
3267 ***********************************************************************/
3268
3269/* The following structure is used to record overlay strings for
3270 later sorting in load_overlay_strings. */
3271
3272struct overlay_entry
3273{
2970b9be 3274 Lisp_Object overlay;
5f5c8ee5
GM
3275 Lisp_Object string;
3276 int priority;
3277 int after_string_p;
3278};
3279
3280
3281/* Set up iterator IT from overlay strings at its current position.
3282 Called from handle_stop. */
3283
3284static enum prop_handled
3285handle_overlay_change (it)
3286 struct it *it;
3287{
2970b9be
GM
3288 if (!STRINGP (it->string) && get_overlay_strings (it))
3289 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3290 else
2970b9be 3291 return HANDLED_NORMALLY;
5f5c8ee5
GM
3292}
3293
3294
3295/* Set up the next overlay string for delivery by IT, if there is an
3296 overlay string to deliver. Called by set_iterator_to_next when the
3297 end of the current overlay string is reached. If there are more
3298 overlay strings to display, IT->string and
3299 IT->current.overlay_string_index are set appropriately here.
3300 Otherwise IT->string is set to nil. */
3301
3302static void
3303next_overlay_string (it)
3304 struct it *it;
3305{
3306 ++it->current.overlay_string_index;
3307 if (it->current.overlay_string_index == it->n_overlay_strings)
3308 {
3309 /* No more overlay strings. Restore IT's settings to what
3310 they were before overlay strings were processed, and
3311 continue to deliver from current_buffer. */
3312 pop_it (it);
3313 xassert (it->stop_charpos >= BEGV
3314 && it->stop_charpos <= it->end_charpos);
3315 it->string = Qnil;
3316 it->current.overlay_string_index = -1;
3317 SET_TEXT_POS (it->current.string_pos, -1, -1);
3318 it->n_overlay_strings = 0;
3319 it->method = next_element_from_buffer;
2970b9be
GM
3320
3321 /* If we're at the end of the buffer, record that we have
3322 processed the overlay strings there already, so that
3323 next_element_from_buffer doesn't try it again. */
3324 if (IT_CHARPOS (*it) >= it->end_charpos)
3325 it->overlay_strings_at_end_processed_p = 1;
5f5c8ee5
GM
3326 }
3327 else
3328 {
3329 /* There are more overlay strings to process. If
3330 IT->current.overlay_string_index has advanced to a position
3331 where we must load IT->overlay_strings with more strings, do
3332 it. */
3333 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3334
3335 if (it->current.overlay_string_index && i == 0)
3336 load_overlay_strings (it);
3337
3338 /* Initialize IT to deliver display elements from the overlay
3339 string. */
3340 it->string = it->overlay_strings[i];
3341 it->multibyte_p = STRING_MULTIBYTE (it->string);
3342 SET_TEXT_POS (it->current.string_pos, 0, 0);
3343 it->method = next_element_from_string;
3344 it->stop_charpos = 0;
3345 }
3346
3347 CHECK_IT (it);
3348}
3349
3350
3351/* Compare two overlay_entry structures E1 and E2. Used as a
3352 comparison function for qsort in load_overlay_strings. Overlay
3353 strings for the same position are sorted so that
3354
2970b9be
GM
3355 1. All after-strings come in front of before-strings, except
3356 when they come from the same overlay.
5f5c8ee5
GM
3357
3358 2. Within after-strings, strings are sorted so that overlay strings
3359 from overlays with higher priorities come first.
3360
3361 2. Within before-strings, strings are sorted so that overlay
3362 strings from overlays with higher priorities come last.
3363
3364 Value is analogous to strcmp. */
3365
3366
3367static int
3368compare_overlay_entries (e1, e2)
3369 void *e1, *e2;
3370{
3371 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3372 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3373 int result;
3374
3375 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3376 {
3377 /* Let after-strings appear in front of before-strings if
3378 they come from different overlays. */
3379 if (EQ (entry1->overlay, entry2->overlay))
3380 result = entry1->after_string_p ? 1 : -1;
3381 else
3382 result = entry1->after_string_p ? -1 : 1;
3383 }
5f5c8ee5
GM
3384 else if (entry1->after_string_p)
3385 /* After-strings sorted in order of decreasing priority. */
3386 result = entry2->priority - entry1->priority;
3387 else
3388 /* Before-strings sorted in order of increasing priority. */
3389 result = entry1->priority - entry2->priority;
3390
3391 return result;
3392}
3393
3394
3395/* Load the vector IT->overlay_strings with overlay strings from IT's
3396 current buffer position. Set IT->n_overlays to the total number of
3397 overlay strings found.
3398
3399 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3400 a time. On entry into load_overlay_strings,
3401 IT->current.overlay_string_index gives the number of overlay
3402 strings that have already been loaded by previous calls to this
3403 function.
3404
2970b9be
GM
3405 IT->add_overlay_start contains an additional overlay start
3406 position to consider for taking overlay strings from, if non-zero.
3407 This position comes into play when the overlay has an `invisible'
3408 property, and both before and after-strings. When we've skipped to
3409 the end of the overlay, because of its `invisible' property, we
3410 nevertheless want its before-string to appear.
3411 IT->add_overlay_start will contain the overlay start position
3412 in this case.
3413
5f5c8ee5
GM
3414 Overlay strings are sorted so that after-string strings come in
3415 front of before-string strings. Within before and after-strings,
3416 strings are sorted by overlay priority. See also function
3417 compare_overlay_entries. */
3418
3419static void
3420load_overlay_strings (it)
3421 struct it *it;
3422{
3423 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3424 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3425 int start, end;
3426 int size = 20;
cafafe0b 3427 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3428 struct overlay_entry *entries
3429 = (struct overlay_entry *) alloca (size * sizeof *entries);
cafafe0b 3430 int charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3431
3432 /* Append the overlay string STRING of overlay OVERLAY to vector
3433 `entries' which has size `size' and currently contains `n'
3434 elements. AFTER_P non-zero means STRING is an after-string of
3435 OVERLAY. */
3436#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3437 do \
3438 { \
3439 Lisp_Object priority; \
3440 \
3441 if (n == size) \
3442 { \
3443 int new_size = 2 * size; \
3444 struct overlay_entry *old = entries; \
3445 entries = \
3446 (struct overlay_entry *) alloca (new_size \
3447 * sizeof *entries); \
3448 bcopy (old, entries, size * sizeof *entries); \
3449 size = new_size; \
3450 } \
3451 \
3452 entries[n].string = (STRING); \
2970b9be 3453 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3454 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3455 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3456 entries[n].after_string_p = (AFTER_P); \
3457 ++n; \
3458 } \
3459 while (0)
3460
3461 /* Process overlay before the overlay center. */
2970b9be 3462 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3463 {
9472f927 3464 overlay = XCAR (ov);
5f5c8ee5
GM
3465 xassert (OVERLAYP (overlay));
3466 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3467 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3468
cafafe0b 3469 if (end < charpos)
5f5c8ee5
GM
3470 break;
3471
3472 /* Skip this overlay if it doesn't start or end at IT's current
3473 position. */
cafafe0b 3474 if (end != charpos && start != charpos)
5f5c8ee5
GM
3475 continue;
3476
3477 /* Skip this overlay if it doesn't apply to IT->w. */
3478 window = Foverlay_get (overlay, Qwindow);
3479 if (WINDOWP (window) && XWINDOW (window) != it->w)
3480 continue;
3481
cafafe0b
GM
3482 /* If the text ``under'' the overlay is invisible, both before-
3483 and after-strings from this overlay are visible; start and
3484 end position are indistinguishable. */
3485 invisible = Foverlay_get (overlay, Qinvisible);
3486 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3487
5f5c8ee5 3488 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3489 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3490 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3491 && XSTRING (str)->size)
3492 RECORD_OVERLAY_STRING (overlay, str, 0);
3493
3494 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3495 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3496 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3497 && XSTRING (str)->size)
3498 RECORD_OVERLAY_STRING (overlay, str, 1);
3499 }
3500
3501 /* Process overlays after the overlay center. */
2970b9be 3502 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3503 {
9472f927 3504 overlay = XCAR (ov);
5f5c8ee5
GM
3505 xassert (OVERLAYP (overlay));
3506 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3507 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3508
cafafe0b 3509 if (start > charpos)
5f5c8ee5
GM
3510 break;
3511
3512 /* Skip this overlay if it doesn't start or end at IT's current
3513 position. */
cafafe0b 3514 if (end != charpos && start != charpos)
5f5c8ee5
GM
3515 continue;
3516
3517 /* Skip this overlay if it doesn't apply to IT->w. */
3518 window = Foverlay_get (overlay, Qwindow);
3519 if (WINDOWP (window) && XWINDOW (window) != it->w)
3520 continue;
3521
cafafe0b
GM
3522 /* If the text ``under'' the overlay is invisible, it has a zero
3523 dimension, and both before- and after-strings apply. */
3524 invisible = Foverlay_get (overlay, Qinvisible);
3525 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3526
5f5c8ee5 3527 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3528 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3529 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3530 && XSTRING (str)->size)
3531 RECORD_OVERLAY_STRING (overlay, str, 0);
3532
3533 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3534 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3535 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3536 && XSTRING (str)->size)
3537 RECORD_OVERLAY_STRING (overlay, str, 1);
3538 }
3539
3540#undef RECORD_OVERLAY_STRING
3541
3542 /* Sort entries. */
cafafe0b 3543 if (n > 1)
2970b9be 3544 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3545
3546 /* Record the total number of strings to process. */
3547 it->n_overlay_strings = n;
3548
3549 /* IT->current.overlay_string_index is the number of overlay strings
3550 that have already been consumed by IT. Copy some of the
3551 remaining overlay strings to IT->overlay_strings. */
3552 i = 0;
3553 j = it->current.overlay_string_index;
3554 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3555 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3556
5f5c8ee5
GM
3557 CHECK_IT (it);
3558}
3559
3560
3561/* Get the first chunk of overlay strings at IT's current buffer
3562 position. Value is non-zero if at least one overlay string was
3563 found. */
3564
3565static int
3566get_overlay_strings (it)
3567 struct it *it;
3568{
3569 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3570 process. This fills IT->overlay_strings with strings, and sets
3571 IT->n_overlay_strings to the total number of strings to process.
3572 IT->pos.overlay_string_index has to be set temporarily to zero
3573 because load_overlay_strings needs this; it must be set to -1
3574 when no overlay strings are found because a zero value would
3575 indicate a position in the first overlay string. */
3576 it->current.overlay_string_index = 0;
3577 load_overlay_strings (it);
3578
3579 /* If we found overlay strings, set up IT to deliver display
3580 elements from the first one. Otherwise set up IT to deliver
3581 from current_buffer. */
3582 if (it->n_overlay_strings)
3583 {
3584 /* Make sure we know settings in current_buffer, so that we can
3585 restore meaningful values when we're done with the overlay
3586 strings. */
3587 compute_stop_pos (it);
3588 xassert (it->face_id >= 0);
3589
3590 /* Save IT's settings. They are restored after all overlay
3591 strings have been processed. */
3592 xassert (it->sp == 0);
3593 push_it (it);
3594
3595 /* Set up IT to deliver display elements from the first overlay
3596 string. */
3597 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 3598 it->string = it->overlay_strings[0];
b2046df8
GM
3599 it->stop_charpos = 0;
3600 it->end_charpos = XSTRING (it->string)->size;
5f5c8ee5
GM
3601 it->multibyte_p = STRING_MULTIBYTE (it->string);
3602 xassert (STRINGP (it->string));
3603 it->method = next_element_from_string;
3604 }
3605 else
3606 {
3607 it->string = Qnil;
3608 it->current.overlay_string_index = -1;
3609 it->method = next_element_from_buffer;
3610 }
3611
3612 CHECK_IT (it);
3613
3614 /* Value is non-zero if we found at least one overlay string. */
3615 return STRINGP (it->string);
3616}
3617
3618
3619\f
3620/***********************************************************************
3621 Saving and restoring state
3622 ***********************************************************************/
3623
3624/* Save current settings of IT on IT->stack. Called, for example,
3625 before setting up IT for an overlay string, to be able to restore
3626 IT's settings to what they were after the overlay string has been
3627 processed. */
3628
3629static void
3630push_it (it)
3631 struct it *it;
3632{
3633 struct iterator_stack_entry *p;
3634
3635 xassert (it->sp < 2);
3636 p = it->stack + it->sp;
3637
3638 p->stop_charpos = it->stop_charpos;
3639 xassert (it->face_id >= 0);
3640 p->face_id = it->face_id;
3641 p->string = it->string;
3642 p->pos = it->current;
3643 p->end_charpos = it->end_charpos;
3644 p->string_nchars = it->string_nchars;
3645 p->area = it->area;
3646 p->multibyte_p = it->multibyte_p;
3647 p->space_width = it->space_width;
3648 p->font_height = it->font_height;
3649 p->voffset = it->voffset;
3650 p->string_from_display_prop_p = it->string_from_display_prop_p;
3651 ++it->sp;
3652}
3653
3654
3655/* Restore IT's settings from IT->stack. Called, for example, when no
3656 more overlay strings must be processed, and we return to delivering
3657 display elements from a buffer, or when the end of a string from a
3658 `display' property is reached and we return to delivering display
3659 elements from an overlay string, or from a buffer. */
3660
3661static void
3662pop_it (it)
3663 struct it *it;
3664{
3665 struct iterator_stack_entry *p;
3666
3667 xassert (it->sp > 0);
3668 --it->sp;
3669 p = it->stack + it->sp;
3670 it->stop_charpos = p->stop_charpos;
3671 it->face_id = p->face_id;
3672 it->string = p->string;
3673 it->current = p->pos;
3674 it->end_charpos = p->end_charpos;
3675 it->string_nchars = p->string_nchars;
3676 it->area = p->area;
3677 it->multibyte_p = p->multibyte_p;
3678 it->space_width = p->space_width;
3679 it->font_height = p->font_height;
3680 it->voffset = p->voffset;
3681 it->string_from_display_prop_p = p->string_from_display_prop_p;
3682}
3683
3684
3685\f
3686/***********************************************************************
3687 Moving over lines
3688 ***********************************************************************/
3689
3690/* Set IT's current position to the previous line start. */
3691
3692static void
3693back_to_previous_line_start (it)
3694 struct it *it;
3695{
3696 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3697 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3698}
3699
3700
cafafe0b
GM
3701/* Move IT to the next line start.
3702
3703 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3704 we skipped over part of the text (as opposed to moving the iterator
3705 continuously over the text). Otherwise, don't change the value
3706 of *SKIPPED_P.
3707
3708 Newlines may come from buffer text, overlay strings, or strings
3709 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3710 simply use find_next_newline_no_quit.
3711
3712 Note that this function may not skip over invisible text that is so
3713 because of text properties and immediately follows a newline. If
3714 it would, function reseat_at_next_visible_line_start, when called
3715 from set_iterator_to_next, would effectively make invisible
3716 characters following a newline part of the wrong glyph row, which
3717 leads to wrong cursor motion. */
5f5c8ee5 3718
cafafe0b
GM
3719static int
3720forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3721 struct it *it;
cafafe0b 3722 int *skipped_p;
5f5c8ee5 3723{
54918e2b 3724 int old_selective, newline_found_p, n;
cafafe0b
GM
3725 const int MAX_NEWLINE_DISTANCE = 500;
3726
0fd37545
GM
3727 /* If already on a newline, just consume it to avoid unintended
3728 skipping over invisible text below. */
51695746
GM
3729 if (it->what == IT_CHARACTER
3730 && it->c == '\n'
3731 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
3732 {
3733 set_iterator_to_next (it, 0);
a77dc1ec 3734 it->c = 0;
0fd37545
GM
3735 return 1;
3736 }
3737
54918e2b 3738 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3739 unnecessary because it's done by the caller, and (b) leads to an
3740 infinite recursion because next_element_from_ellipsis indirectly
3741 calls this function. */
54918e2b
GM
3742 old_selective = it->selective;
3743 it->selective = 0;
3744
cafafe0b
GM
3745 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3746 from buffer text. */
3aec8722
GM
3747 for (n = newline_found_p = 0;
3748 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3749 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3750 {
8692ca92
GM
3751 if (!get_next_display_element (it))
3752 break;
d02f1cb8 3753 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3754 set_iterator_to_next (it, 0);
cafafe0b
GM
3755 }
3756
3757 /* If we didn't find a newline near enough, see if we can use a
3758 short-cut. */
3aec8722 3759 if (n == MAX_NEWLINE_DISTANCE)
cafafe0b
GM
3760 {
3761 int start = IT_CHARPOS (*it);
3762 int limit = find_next_newline_no_quit (start, 1);
3763 Lisp_Object pos;
3764
3765 xassert (!STRINGP (it->string));
3766
3767 /* If there isn't any `display' property in sight, and no
3768 overlays, we can just use the position of the newline in
3769 buffer text. */
3770 if (it->stop_charpos >= limit
3771 || ((pos = Fnext_single_property_change (make_number (start),
3772 Qdisplay,
3773 Qnil, make_number (limit)),
3774 NILP (pos))
3775 && next_overlay_change (start) == ZV))
3776 {
3777 IT_CHARPOS (*it) = limit;
3778 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3779 *skipped_p = newline_found_p = 1;
3780 }
3781 else
3782 {
3783 while (get_next_display_element (it)
3784 && !newline_found_p)
3785 {
3786 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3787 set_iterator_to_next (it, 0);
3788 }
3789 }
3790 }
3791
54918e2b 3792 it->selective = old_selective;
cafafe0b 3793 return newline_found_p;
5f5c8ee5
GM
3794}
3795
3796
3797/* Set IT's current position to the previous visible line start. Skip
3798 invisible text that is so either due to text properties or due to
3799 selective display. Caution: this does not change IT->current_x and
3800 IT->hpos. */
3801
3802static void
3803back_to_previous_visible_line_start (it)
3804 struct it *it;
3805{
3806 int visible_p = 0;
3807
3808 /* Go back one newline if not on BEGV already. */
3809 if (IT_CHARPOS (*it) > BEGV)
3810 back_to_previous_line_start (it);
3811
3812 /* Move over lines that are invisible because of selective display
3813 or text properties. */
3814 while (IT_CHARPOS (*it) > BEGV
3815 && !visible_p)
3816 {
3817 visible_p = 1;
3818
3819 /* If selective > 0, then lines indented more than that values
3820 are invisible. */
3821 if (it->selective > 0
3822 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3823 it->selective))
3824 visible_p = 0;
5f5c8ee5
GM
3825 else
3826 {
3827 Lisp_Object prop;
3828
6fc556fd
KR
3829 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3830 Qinvisible, it->window);
5f5c8ee5
GM
3831 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3832 visible_p = 0;
3833 }
5f5c8ee5
GM
3834
3835 /* Back one more newline if the current one is invisible. */
3836 if (!visible_p)
3837 back_to_previous_line_start (it);
3838 }
3839
3840 xassert (IT_CHARPOS (*it) >= BEGV);
3841 xassert (IT_CHARPOS (*it) == BEGV
3842 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3843 CHECK_IT (it);
3844}
3845
3846
3847/* Reseat iterator IT at the previous visible line start. Skip
3848 invisible text that is so either due to text properties or due to
3849 selective display. At the end, update IT's overlay information,
3850 face information etc. */
3851
3852static void
3853reseat_at_previous_visible_line_start (it)
3854 struct it *it;
3855{
3856 back_to_previous_visible_line_start (it);
3857 reseat (it, it->current.pos, 1);
3858 CHECK_IT (it);
3859}
3860
3861
3862/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3863 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3864 preceding the line start. Skip over invisible text that is so
3865 because of selective display. Compute faces, overlays etc at the
3866 new position. Note that this function does not skip over text that
3867 is invisible because of text properties. */
5f5c8ee5
GM
3868
3869static void
312246d1 3870reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3871 struct it *it;
312246d1 3872 int on_newline_p;
5f5c8ee5 3873{
cafafe0b
GM
3874 int newline_found_p, skipped_p = 0;
3875
3876 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3877
3878 /* Skip over lines that are invisible because they are indented
3879 more than the value of IT->selective. */
3880 if (it->selective > 0)
3881 while (IT_CHARPOS (*it) < ZV
a77dc1ec 3882 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
cafafe0b 3883 it->selective))
a77dc1ec
GM
3884 {
3885 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3886 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3887 }
cafafe0b
GM
3888
3889 /* Position on the newline if that's what's requested. */
3890 if (on_newline_p && newline_found_p)
5f5c8ee5 3891 {
cafafe0b 3892 if (STRINGP (it->string))
5f5c8ee5 3893 {
cafafe0b
GM
3894 if (IT_STRING_CHARPOS (*it) > 0)
3895 {
3896 --IT_STRING_CHARPOS (*it);
3897 --IT_STRING_BYTEPOS (*it);
3898 }
5f5c8ee5 3899 }
cafafe0b 3900 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
3901 {
3902 --IT_CHARPOS (*it);
cafafe0b
GM
3903 --IT_BYTEPOS (*it);
3904 reseat (it, it->current.pos, 0);
312246d1 3905 }
5f5c8ee5 3906 }
cafafe0b
GM
3907 else if (skipped_p)
3908 reseat (it, it->current.pos, 0);
5f5c8ee5
GM
3909
3910 CHECK_IT (it);
3911}
3912
3913
3914\f
3915/***********************************************************************
3916 Changing an iterator's position
3917***********************************************************************/
3918
3919/* Change IT's current position to POS in current_buffer. If FORCE_P
3920 is non-zero, always check for text properties at the new position.
3921 Otherwise, text properties are only looked up if POS >=
3922 IT->check_charpos of a property. */
3923
3924static void
3925reseat (it, pos, force_p)
3926 struct it *it;
3927 struct text_pos pos;
3928 int force_p;
3929{
3930 int original_pos = IT_CHARPOS (*it);
3931
3932 reseat_1 (it, pos, 0);
3933
3934 /* Determine where to check text properties. Avoid doing it
3935 where possible because text property lookup is very expensive. */
3936 if (force_p
3937 || CHARPOS (pos) > it->stop_charpos
3938 || CHARPOS (pos) < original_pos)
3939 handle_stop (it);
3940
3941 CHECK_IT (it);
3942}
3943
3944
3945/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3946 IT->stop_pos to POS, also. */
3947
3948static void
3949reseat_1 (it, pos, set_stop_p)
3950 struct it *it;
3951 struct text_pos pos;
3952 int set_stop_p;
3953{
3954 /* Don't call this function when scanning a C string. */
3955 xassert (it->s == NULL);
3956
3957 /* POS must be a reasonable value. */
3958 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3959
3960 it->current.pos = it->position = pos;
3961 XSETBUFFER (it->object, current_buffer);
78c663d8 3962 it->end_charpos = ZV;
5f5c8ee5
GM
3963 it->dpvec = NULL;
3964 it->current.dpvec_index = -1;
3965 it->current.overlay_string_index = -1;
3966 IT_STRING_CHARPOS (*it) = -1;
3967 IT_STRING_BYTEPOS (*it) = -1;
3968 it->string = Qnil;
3969 it->method = next_element_from_buffer;
3970 it->sp = 0;
4aad61f8 3971 it->face_before_selective_p = 0;
5f5c8ee5
GM
3972
3973 if (set_stop_p)
3974 it->stop_charpos = CHARPOS (pos);
3975}
3976
3977
3978/* Set up IT for displaying a string, starting at CHARPOS in window W.
3979 If S is non-null, it is a C string to iterate over. Otherwise,
3980 STRING gives a Lisp string to iterate over.
3981
3982 If PRECISION > 0, don't return more then PRECISION number of
3983 characters from the string.
3984
3985 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3986 characters have been returned. FIELD_WIDTH < 0 means an infinite
3987 field width.
3988
3989 MULTIBYTE = 0 means disable processing of multibyte characters,
3990 MULTIBYTE > 0 means enable it,
3991 MULTIBYTE < 0 means use IT->multibyte_p.
3992
3993 IT must be initialized via a prior call to init_iterator before
3994 calling this function. */
3995
3996static void
3997reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3998 struct it *it;
3999 unsigned char *s;
4000 Lisp_Object string;
4001 int charpos;
4002 int precision, field_width, multibyte;
4003{
4004 /* No region in strings. */
4005 it->region_beg_charpos = it->region_end_charpos = -1;
4006
4007 /* No text property checks performed by default, but see below. */
4008 it->stop_charpos = -1;
4009
4010 /* Set iterator position and end position. */
4011 bzero (&it->current, sizeof it->current);
4012 it->current.overlay_string_index = -1;
4013 it->current.dpvec_index = -1;
5f5c8ee5
GM
4014 xassert (charpos >= 0);
4015
e719f5ae
GM
4016 /* If STRING is specified, use its multibyteness, otherwise use the
4017 setting of MULTIBYTE, if specified. */
cabf45da 4018 if (multibyte >= 0)
5f5c8ee5
GM
4019 it->multibyte_p = multibyte > 0;
4020
4021 if (s == NULL)
4022 {
4023 xassert (STRINGP (string));
4024 it->string = string;
4025 it->s = NULL;
4026 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4027 it->method = next_element_from_string;
4028 it->current.string_pos = string_pos (charpos, string);
4029 }
4030 else
4031 {
4032 it->s = s;
4033 it->string = Qnil;
4034
4035 /* Note that we use IT->current.pos, not it->current.string_pos,
4036 for displaying C strings. */
4037 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4038 if (it->multibyte_p)
4039 {
4040 it->current.pos = c_string_pos (charpos, s, 1);
4041 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4042 }
4043 else
4044 {
4045 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4046 it->end_charpos = it->string_nchars = strlen (s);
4047 }
4048
4049 it->method = next_element_from_c_string;
4050 }
4051
4052 /* PRECISION > 0 means don't return more than PRECISION characters
4053 from the string. */
4054 if (precision > 0 && it->end_charpos - charpos > precision)
4055 it->end_charpos = it->string_nchars = charpos + precision;
4056
4057 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4058 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4059 FIELD_WIDTH < 0 means infinite field width. This is useful for
4060 padding with `-' at the end of a mode line. */
4061 if (field_width < 0)
4062 field_width = INFINITY;
4063 if (field_width > it->end_charpos - charpos)
4064 it->end_charpos = charpos + field_width;
4065
4066 /* Use the standard display table for displaying strings. */
4067 if (DISP_TABLE_P (Vstandard_display_table))
4068 it->dp = XCHAR_TABLE (Vstandard_display_table);
4069
4070 it->stop_charpos = charpos;
4071 CHECK_IT (it);
4072}
4073
4074
4075\f
4076/***********************************************************************
4077 Iteration
4078 ***********************************************************************/
4079
4080/* Load IT's display element fields with information about the next
4081 display element from the current position of IT. Value is zero if
4082 end of buffer (or C string) is reached. */
4083
4084int
4085get_next_display_element (it)
4086 struct it *it;
4087{
4088 /* Non-zero means that we found an display element. Zero means that
4089 we hit the end of what we iterate over. Performance note: the
4090 function pointer `method' used here turns out to be faster than
4091 using a sequence of if-statements. */
4092 int success_p = (*it->method) (it);
5f5c8ee5
GM
4093
4094 if (it->what == IT_CHARACTER)
4095 {
4096 /* Map via display table or translate control characters.
4097 IT->c, IT->len etc. have been set to the next character by
4098 the function call above. If we have a display table, and it
4099 contains an entry for IT->c, translate it. Don't do this if
4100 IT->c itself comes from a display table, otherwise we could
4101 end up in an infinite recursion. (An alternative could be to
4102 count the recursion depth of this function and signal an
4103 error when a certain maximum depth is reached.) Is it worth
4104 it? */
4105 if (success_p && it->dpvec == NULL)
4106 {
4107 Lisp_Object dv;
4108
4109 if (it->dp
4110 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4111 VECTORP (dv)))
4112 {
4113 struct Lisp_Vector *v = XVECTOR (dv);
4114
4115 /* Return the first character from the display table
4116 entry, if not empty. If empty, don't display the
4117 current character. */
4118 if (v->size)
4119 {
4120 it->dpvec_char_len = it->len;
4121 it->dpvec = v->contents;
4122 it->dpend = v->contents + v->size;
4123 it->current.dpvec_index = 0;
4124 it->method = next_element_from_display_vector;
7eee47cc
GM
4125 success_p = get_next_display_element (it);
4126 }
4127 else
4128 {
4129 set_iterator_to_next (it, 0);
4130 success_p = get_next_display_element (it);
5f5c8ee5 4131 }
5f5c8ee5
GM
4132 }
4133
4134 /* Translate control characters into `\003' or `^C' form.
4135 Control characters coming from a display table entry are
4136 currently not translated because we use IT->dpvec to hold
4137 the translation. This could easily be changed but I
197516c2
KH
4138 don't believe that it is worth doing.
4139
4140 Non-printable multibyte characters are also translated
4141 octal form. */
5f5c8ee5
GM
4142 else if ((it->c < ' '
4143 && (it->area != TEXT_AREA
c6e89d6c 4144 || (it->c != '\n' && it->c != '\t')))
54c85a23 4145 || (it->c >= 127
197516c2
KH
4146 && it->len == 1)
4147 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
4148 {
4149 /* IT->c is a control character which must be displayed
4150 either as '\003' or as `^C' where the '\\' and '^'
4151 can be defined in the display table. Fill
4152 IT->ctl_chars with glyphs for what we have to
4153 display. Then, set IT->dpvec to these glyphs. */
4154 GLYPH g;
4155
54c85a23 4156 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4157 {
4158 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4159 if (it->dp
4160 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4161 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4162 g = XINT (DISP_CTRL_GLYPH (it->dp));
4163 else
4164 g = FAST_MAKE_GLYPH ('^', 0);
4165 XSETINT (it->ctl_chars[0], g);
4166
4167 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4168 XSETINT (it->ctl_chars[1], g);
4169
4170 /* Set up IT->dpvec and return first character from it. */
4171 it->dpvec_char_len = it->len;
4172 it->dpvec = it->ctl_chars;
4173 it->dpend = it->dpvec + 2;
4174 it->current.dpvec_index = 0;
4175 it->method = next_element_from_display_vector;
4176 get_next_display_element (it);
4177 }
4178 else
4179 {
260a86a0 4180 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4181 int len;
197516c2
KH
4182 int i;
4183 GLYPH escape_glyph;
4184
5f5c8ee5
GM
4185 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4186 if (it->dp
4187 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4188 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4189 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4190 else
197516c2
KH
4191 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4192
c5924f47
KH
4193 if (SINGLE_BYTE_CHAR_P (it->c))
4194 str[0] = it->c, len = 1;
4195 else
4196 len = CHAR_STRING (it->c, str);
4197
197516c2
KH
4198 for (i = 0; i < len; i++)
4199 {
4200 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4201 /* Insert three more glyphs into IT->ctl_chars for
4202 the octal display of the character. */
4203 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4204 XSETINT (it->ctl_chars[i * 4 + 1], g);
4205 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4206 XSETINT (it->ctl_chars[i * 4 + 2], g);
4207 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4208 XSETINT (it->ctl_chars[i * 4 + 3], g);
4209 }
5f5c8ee5
GM
4210
4211 /* Set up IT->dpvec and return the first character
4212 from it. */
4213 it->dpvec_char_len = it->len;
4214 it->dpvec = it->ctl_chars;
197516c2 4215 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4216 it->current.dpvec_index = 0;
4217 it->method = next_element_from_display_vector;
4218 get_next_display_element (it);
4219 }
4220 }
4221 }
4222
980806b6
KH
4223 /* Adjust face id for a multibyte character. There are no
4224 multibyte character in unibyte text. */
5f5c8ee5
GM
4225 if (it->multibyte_p
4226 && success_p
980806b6 4227 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4228 {
980806b6
KH
4229 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4230 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4231 }
4232 }
4233
4234 /* Is this character the last one of a run of characters with
4235 box? If yes, set IT->end_of_box_run_p to 1. */
4236 if (it->face_box_p
4237 && it->s == NULL)
4238 {
4239 int face_id;
4240 struct face *face;
4241
4242 it->end_of_box_run_p
4243 = ((face_id = face_after_it_pos (it),
4244 face_id != it->face_id)
4245 && (face = FACE_FROM_ID (it->f, face_id),
4246 face->box == FACE_NO_BOX));
4247 }
4248
4249 /* Value is 0 if end of buffer or string reached. */
4250 return success_p;
4251}
4252
4253
4254/* Move IT to the next display element.
4255
cafafe0b
GM
4256 RESEAT_P non-zero means if called on a newline in buffer text,
4257 skip to the next visible line start.
4258
5f5c8ee5
GM
4259 Functions get_next_display_element and set_iterator_to_next are
4260 separate because I find this arrangement easier to handle than a
4261 get_next_display_element function that also increments IT's
4262 position. The way it is we can first look at an iterator's current
4263 display element, decide whether it fits on a line, and if it does,
4264 increment the iterator position. The other way around we probably
4265 would either need a flag indicating whether the iterator has to be
4266 incremented the next time, or we would have to implement a
4267 decrement position function which would not be easy to write. */
4268
4269void
cafafe0b 4270set_iterator_to_next (it, reseat_p)
5f5c8ee5 4271 struct it *it;
cafafe0b 4272 int reseat_p;
5f5c8ee5 4273{
483de32b
GM
4274 /* Reset flags indicating start and end of a sequence of characters
4275 with box. Reset them at the start of this function because
4276 moving the iterator to a new position might set them. */
4277 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4278
5f5c8ee5
GM
4279 if (it->method == next_element_from_buffer)
4280 {
4281 /* The current display element of IT is a character from
4282 current_buffer. Advance in the buffer, and maybe skip over
4283 invisible lines that are so because of selective display. */
cafafe0b 4284 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4285 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4286 else
4287 {
4288 xassert (it->len != 0);
4289 IT_BYTEPOS (*it) += it->len;
4290 IT_CHARPOS (*it) += 1;
4291 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4292 }
4293 }
260a86a0
KH
4294 else if (it->method == next_element_from_composition)
4295 {
4296 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4297 if (STRINGP (it->string))
4298 {
4299 IT_STRING_BYTEPOS (*it) += it->len;
4300 IT_STRING_CHARPOS (*it) += it->cmp_len;
4301 it->method = next_element_from_string;
4302 goto consider_string_end;
4303 }
4304 else
4305 {
4306 IT_BYTEPOS (*it) += it->len;
4307 IT_CHARPOS (*it) += it->cmp_len;
4308 it->method = next_element_from_buffer;
4309 }
4310 }
5f5c8ee5
GM
4311 else if (it->method == next_element_from_c_string)
4312 {
4313 /* Current display element of IT is from a C string. */
4314 IT_BYTEPOS (*it) += it->len;
4315 IT_CHARPOS (*it) += 1;
4316 }
4317 else if (it->method == next_element_from_display_vector)
4318 {
4319 /* Current display element of IT is from a display table entry.
4320 Advance in the display table definition. Reset it to null if
4321 end reached, and continue with characters from buffers/
4322 strings. */
4323 ++it->current.dpvec_index;
286bcbc9 4324
980806b6
KH
4325 /* Restore face of the iterator to what they were before the
4326 display vector entry (these entries may contain faces). */
5f5c8ee5 4327 it->face_id = it->saved_face_id;
286bcbc9 4328
5f5c8ee5
GM
4329 if (it->dpvec + it->current.dpvec_index == it->dpend)
4330 {
4331 if (it->s)
4332 it->method = next_element_from_c_string;
4333 else if (STRINGP (it->string))
4334 it->method = next_element_from_string;
4335 else
4336 it->method = next_element_from_buffer;
4337
4338 it->dpvec = NULL;
4339 it->current.dpvec_index = -1;
4340
312246d1
GM
4341 /* Skip over characters which were displayed via IT->dpvec. */
4342 if (it->dpvec_char_len < 0)
4343 reseat_at_next_visible_line_start (it, 1);
4344 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4345 {
4346 it->len = it->dpvec_char_len;
cafafe0b 4347 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4348 }
4349 }
4350 }
4351 else if (it->method == next_element_from_string)
4352 {
4353 /* Current display element is a character from a Lisp string. */
4354 xassert (it->s == NULL && STRINGP (it->string));
4355 IT_STRING_BYTEPOS (*it) += it->len;
4356 IT_STRING_CHARPOS (*it) += 1;
4357
4358 consider_string_end:
4359
4360 if (it->current.overlay_string_index >= 0)
4361 {
4362 /* IT->string is an overlay string. Advance to the
4363 next, if there is one. */
4364 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4365 next_overlay_string (it);
4366 }
4367 else
4368 {
4369 /* IT->string is not an overlay string. If we reached
4370 its end, and there is something on IT->stack, proceed
4371 with what is on the stack. This can be either another
4372 string, this time an overlay string, or a buffer. */
4373 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4374 && it->sp > 0)
4375 {
4376 pop_it (it);
4377 if (!STRINGP (it->string))
4378 it->method = next_element_from_buffer;
b2046df8
GM
4379 else
4380 goto consider_string_end;
5f5c8ee5
GM
4381 }
4382 }
4383 }
4384 else if (it->method == next_element_from_image
4385 || it->method == next_element_from_stretch)
4386 {
4387 /* The position etc with which we have to proceed are on
4388 the stack. The position may be at the end of a string,
4389 if the `display' property takes up the whole string. */
4390 pop_it (it);
4391 it->image_id = 0;
4392 if (STRINGP (it->string))
4393 {
4394 it->method = next_element_from_string;
4395 goto consider_string_end;
4396 }
4397 else
4398 it->method = next_element_from_buffer;
4399 }
4400 else
4401 /* There are no other methods defined, so this should be a bug. */
4402 abort ();
4403
5f5c8ee5
GM
4404 xassert (it->method != next_element_from_string
4405 || (STRINGP (it->string)
4406 && IT_STRING_CHARPOS (*it) >= 0));
4407}
4408
4409
4410/* Load IT's display element fields with information about the next
4411 display element which comes from a display table entry or from the
4412 result of translating a control character to one of the forms `^C'
4413 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4414
4415static int
4416next_element_from_display_vector (it)
4417 struct it *it;
4418{
4419 /* Precondition. */
4420 xassert (it->dpvec && it->current.dpvec_index >= 0);
4421
4422 /* Remember the current face id in case glyphs specify faces.
4423 IT's face is restored in set_iterator_to_next. */
4424 it->saved_face_id = it->face_id;
4425
4426 if (INTEGERP (*it->dpvec)
4427 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4428 {
4429 int lface_id;
4430 GLYPH g;
4431
4432 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4433 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4434 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4435
4436 /* The entry may contain a face id to use. Such a face id is
4437 the id of a Lisp face, not a realized face. A face id of
969065c3 4438 zero means no face is specified. */
5f5c8ee5
GM
4439 lface_id = FAST_GLYPH_FACE (g);
4440 if (lface_id)
4441 {
969065c3 4442 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4443 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4444 if (face_id >= 0)
969065c3 4445 it->face_id = face_id;
5f5c8ee5
GM
4446 }
4447 }
4448 else
4449 /* Display table entry is invalid. Return a space. */
4450 it->c = ' ', it->len = 1;
4451
4452 /* Don't change position and object of the iterator here. They are
4453 still the values of the character that had this display table
4454 entry or was translated, and that's what we want. */
4455 it->what = IT_CHARACTER;
4456 return 1;
4457}
4458
4459
4460/* Load IT with the next display element from Lisp string IT->string.
4461 IT->current.string_pos is the current position within the string.
4462 If IT->current.overlay_string_index >= 0, the Lisp string is an
4463 overlay string. */
4464
4465static int
4466next_element_from_string (it)
4467 struct it *it;
4468{
4469 struct text_pos position;
4470
4471 xassert (STRINGP (it->string));
4472 xassert (IT_STRING_CHARPOS (*it) >= 0);
4473 position = it->current.string_pos;
4474
4475 /* Time to check for invisible text? */
4476 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4477 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4478 {
4479 handle_stop (it);
4480
4481 /* Since a handler may have changed IT->method, we must
4482 recurse here. */
4483 return get_next_display_element (it);
4484 }
4485
4486 if (it->current.overlay_string_index >= 0)
4487 {
4488 /* Get the next character from an overlay string. In overlay
4489 strings, There is no field width or padding with spaces to
4490 do. */
4491 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4492 {
4493 it->what = IT_EOB;
4494 return 0;
4495 }
4496 else if (STRING_MULTIBYTE (it->string))
4497 {
4498 int remaining = (STRING_BYTES (XSTRING (it->string))
4499 - IT_STRING_BYTEPOS (*it));
4500 unsigned char *s = (XSTRING (it->string)->data
4501 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4502 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4503 }
4504 else
4505 {
4506 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4507 it->len = 1;
4508 }
4509 }
4510 else
4511 {
4512 /* Get the next character from a Lisp string that is not an
4513 overlay string. Such strings come from the mode line, for
4514 example. We may have to pad with spaces, or truncate the
4515 string. See also next_element_from_c_string. */
4516 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4517 {
4518 it->what = IT_EOB;
4519 return 0;
4520 }
4521 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4522 {
4523 /* Pad with spaces. */
4524 it->c = ' ', it->len = 1;
4525 CHARPOS (position) = BYTEPOS (position) = -1;
4526 }
4527 else if (STRING_MULTIBYTE (it->string))
4528 {
4529 int maxlen = (STRING_BYTES (XSTRING (it->string))
4530 - IT_STRING_BYTEPOS (*it));
4531 unsigned char *s = (XSTRING (it->string)->data
4532 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4533 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4534 }
4535 else
4536 {
4537 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4538 it->len = 1;
4539 }
4540 }
4541
4542 /* Record what we have and where it came from. Note that we store a
4543 buffer position in IT->position although it could arguably be a
4544 string position. */
4545 it->what = IT_CHARACTER;
4546 it->object = it->string;
4547 it->position = position;
4548 return 1;
4549}
4550
4551
4552/* Load IT with next display element from C string IT->s.
4553 IT->string_nchars is the maximum number of characters to return
4554 from the string. IT->end_charpos may be greater than
4555 IT->string_nchars when this function is called, in which case we
4556 may have to return padding spaces. Value is zero if end of string
4557 reached, including padding spaces. */
4558
4559static int
4560next_element_from_c_string (it)
4561 struct it *it;
4562{
4563 int success_p = 1;
4564
4565 xassert (it->s);
4566 it->what = IT_CHARACTER;
4567 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4568 it->object = Qnil;
4569
4570 /* IT's position can be greater IT->string_nchars in case a field
4571 width or precision has been specified when the iterator was
4572 initialized. */
4573 if (IT_CHARPOS (*it) >= it->end_charpos)
4574 {
4575 /* End of the game. */
4576 it->what = IT_EOB;
4577 success_p = 0;
4578 }
4579 else if (IT_CHARPOS (*it) >= it->string_nchars)
4580 {
4581 /* Pad with spaces. */
4582 it->c = ' ', it->len = 1;
4583 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4584 }
4585 else if (it->multibyte_p)
4586 {
4587 /* Implementation note: The calls to strlen apparently aren't a
4588 performance problem because there is no noticeable performance
4589 difference between Emacs running in unibyte or multibyte mode. */
4590 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4591 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4592 maxlen, &it->len);
5f5c8ee5
GM
4593 }
4594 else
4595 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4596
4597 return success_p;
4598}
4599
4600
4601/* Set up IT to return characters from an ellipsis, if appropriate.
4602 The definition of the ellipsis glyphs may come from a display table
4603 entry. This function Fills IT with the first glyph from the
4604 ellipsis if an ellipsis is to be displayed. */
4605
13f19968 4606static int
5f5c8ee5
GM
4607next_element_from_ellipsis (it)
4608 struct it *it;
4609{
13f19968 4610 if (it->selective_display_ellipsis_p)
5f5c8ee5 4611 {
13f19968
GM
4612 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4613 {
4614 /* Use the display table definition for `...'. Invalid glyphs
4615 will be handled by the method returning elements from dpvec. */
4616 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4617 it->dpvec_char_len = it->len;
4618 it->dpvec = v->contents;
4619 it->dpend = v->contents + v->size;
4620 it->current.dpvec_index = 0;
4621 it->method = next_element_from_display_vector;
4622 }
4623 else
4624 {
4625 /* Use default `...' which is stored in default_invis_vector. */
4626 it->dpvec_char_len = it->len;
4627 it->dpvec = default_invis_vector;
4628 it->dpend = default_invis_vector + 3;
4629 it->current.dpvec_index = 0;
4630 it->method = next_element_from_display_vector;
4631 }
5f5c8ee5 4632 }
13f19968 4633 else
54918e2b 4634 {
4aad61f8
GM
4635 /* The face at the current position may be different from the
4636 face we find after the invisible text. Remember what it
4637 was in IT->saved_face_id, and signal that it's there by
4638 setting face_before_selective_p. */
4639 it->saved_face_id = it->face_id;
54918e2b
GM
4640 it->method = next_element_from_buffer;
4641 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4642 it->face_before_selective_p = 1;
54918e2b 4643 }
13f19968
GM
4644
4645 return get_next_display_element (it);
5f5c8ee5
GM
4646}
4647
4648
4649/* Deliver an image display element. The iterator IT is already
4650 filled with image information (done in handle_display_prop). Value
4651 is always 1. */
4652
4653
4654static int
4655next_element_from_image (it)
4656 struct it *it;
4657{
4658 it->what = IT_IMAGE;
4659 return 1;
4660}
4661
4662
4663/* Fill iterator IT with next display element from a stretch glyph
4664 property. IT->object is the value of the text property. Value is
4665 always 1. */
4666
4667static int
4668next_element_from_stretch (it)
4669 struct it *it;
4670{
4671 it->what = IT_STRETCH;
4672 return 1;
4673}
4674
4675
4676/* Load IT with the next display element from current_buffer. Value
4677 is zero if end of buffer reached. IT->stop_charpos is the next
4678 position at which to stop and check for text properties or buffer
4679 end. */
4680
4681static int
4682next_element_from_buffer (it)
4683 struct it *it;
4684{
4685 int success_p = 1;
4686
4687 /* Check this assumption, otherwise, we would never enter the
4688 if-statement, below. */
4689 xassert (IT_CHARPOS (*it) >= BEGV
4690 && IT_CHARPOS (*it) <= it->stop_charpos);
4691
4692 if (IT_CHARPOS (*it) >= it->stop_charpos)
4693 {
4694 if (IT_CHARPOS (*it) >= it->end_charpos)
4695 {
4696 int overlay_strings_follow_p;
4697
4698 /* End of the game, except when overlay strings follow that
4699 haven't been returned yet. */
4700 if (it->overlay_strings_at_end_processed_p)
4701 overlay_strings_follow_p = 0;
4702 else
4703 {
4704 it->overlay_strings_at_end_processed_p = 1;
c880678e 4705 overlay_strings_follow_p = get_overlay_strings (it);
5f5c8ee5
GM
4706 }
4707
4708 if (overlay_strings_follow_p)
4709 success_p = get_next_display_element (it);
4710 else
4711 {
4712 it->what = IT_EOB;
4713 it->position = it->current.pos;
4714 success_p = 0;
4715 }
4716 }
4717 else
4718 {
4719 handle_stop (it);
4720 return get_next_display_element (it);
4721 }
4722 }
4723 else
4724 {
4725 /* No face changes, overlays etc. in sight, so just return a
4726 character from current_buffer. */
4727 unsigned char *p;
4728
4729 /* Maybe run the redisplay end trigger hook. Performance note:
4730 This doesn't seem to cost measurable time. */
4731 if (it->redisplay_end_trigger_charpos
4732 && it->glyph_row
4733 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4734 run_redisplay_end_trigger_hook (it);
4735
4736 /* Get the next character, maybe multibyte. */
4737 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4738 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4739 {
4740 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4741 - IT_BYTEPOS (*it));
4fdb80f2 4742 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4743 }
4744 else
4745 it->c = *p, it->len = 1;
4746
4747 /* Record what we have and where it came from. */
4748 it->what = IT_CHARACTER;;
4749 it->object = it->w->buffer;
4750 it->position = it->current.pos;
4751
4752 /* Normally we return the character found above, except when we
4753 really want to return an ellipsis for selective display. */
4754 if (it->selective)
4755 {
4756 if (it->c == '\n')
4757 {
4758 /* A value of selective > 0 means hide lines indented more
4759 than that number of columns. */
4760 if (it->selective > 0
4761 && IT_CHARPOS (*it) + 1 < ZV
4762 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4763 IT_BYTEPOS (*it) + 1,
4764 it->selective))
312246d1 4765 {
13f19968 4766 success_p = next_element_from_ellipsis (it);
312246d1
GM
4767 it->dpvec_char_len = -1;
4768 }
5f5c8ee5
GM
4769 }
4770 else if (it->c == '\r' && it->selective == -1)
4771 {
4772 /* A value of selective == -1 means that everything from the
4773 CR to the end of the line is invisible, with maybe an
4774 ellipsis displayed for it. */
13f19968 4775 success_p = next_element_from_ellipsis (it);
312246d1 4776 it->dpvec_char_len = -1;
5f5c8ee5
GM
4777 }
4778 }
4779 }
4780
4781 /* Value is zero if end of buffer reached. */
c880678e 4782 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4783 return success_p;
4784}
4785
4786
4787/* Run the redisplay end trigger hook for IT. */
4788
4789static void
4790run_redisplay_end_trigger_hook (it)
4791 struct it *it;
4792{
4793 Lisp_Object args[3];
4794
4795 /* IT->glyph_row should be non-null, i.e. we should be actually
4796 displaying something, or otherwise we should not run the hook. */
4797 xassert (it->glyph_row);
4798
4799 /* Set up hook arguments. */
4800 args[0] = Qredisplay_end_trigger_functions;
4801 args[1] = it->window;
4802 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4803 it->redisplay_end_trigger_charpos = 0;
4804
4805 /* Since we are *trying* to run these functions, don't try to run
4806 them again, even if they get an error. */
4807 it->w->redisplay_end_trigger = Qnil;
4808 Frun_hook_with_args (3, args);
4809
4810 /* Notice if it changed the face of the character we are on. */
4811 handle_face_prop (it);
4812}
4813
4814
260a86a0
KH
4815/* Deliver a composition display element. The iterator IT is already
4816 filled with composition information (done in
4817 handle_composition_prop). Value is always 1. */
4818
4819static int
4820next_element_from_composition (it)
4821 struct it *it;
4822{
4823 it->what = IT_COMPOSITION;
4824 it->position = (STRINGP (it->string)
4825 ? it->current.string_pos
4826 : it->current.pos);
4827 return 1;
4828}
4829
4830
5f5c8ee5
GM
4831\f
4832/***********************************************************************
4833 Moving an iterator without producing glyphs
4834 ***********************************************************************/
4835
4836/* Move iterator IT to a specified buffer or X position within one
4837 line on the display without producing glyphs.
4838
4839 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4840 whichever is reached first.
4841
4842 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4843
4844 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4845 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4846 TO_X includes the amount by which a window is horizontally
4847 scrolled.
4848
4849 Value is
4850
4851 MOVE_POS_MATCH_OR_ZV
4852 - when TO_POS or ZV was reached.
4853
4854 MOVE_X_REACHED
4855 -when TO_X was reached before TO_POS or ZV were reached.
4856
4857 MOVE_LINE_CONTINUED
4858 - when we reached the end of the display area and the line must
4859 be continued.
4860
4861 MOVE_LINE_TRUNCATED
4862 - when we reached the end of the display area and the line is
4863 truncated.
4864
4865 MOVE_NEWLINE_OR_CR
4866 - when we stopped at a line end, i.e. a newline or a CR and selective
4867 display is on. */
4868
701552dd 4869static enum move_it_result
5f5c8ee5
GM
4870move_it_in_display_line_to (it, to_charpos, to_x, op)
4871 struct it *it;
4872 int to_charpos, to_x, op;
4873{
4874 enum move_it_result result = MOVE_UNDEFINED;
4875 struct glyph_row *saved_glyph_row;
4876
4877 /* Don't produce glyphs in produce_glyphs. */
4878 saved_glyph_row = it->glyph_row;
4879 it->glyph_row = NULL;
4880
5f5c8ee5
GM
4881 while (1)
4882 {
ae26e27d 4883 int x, i, ascent = 0, descent = 0;
5f5c8ee5
GM
4884
4885 /* Stop when ZV or TO_CHARPOS reached. */
4886 if (!get_next_display_element (it)
4887 || ((op & MOVE_TO_POS) != 0
4888 && BUFFERP (it->object)
4889 && IT_CHARPOS (*it) >= to_charpos))
4890 {
4891 result = MOVE_POS_MATCH_OR_ZV;
4892 break;
4893 }
4894
4895 /* The call to produce_glyphs will get the metrics of the
4896 display element IT is loaded with. We record in x the
4897 x-position before this display element in case it does not
4898 fit on the line. */
4899 x = it->current_x;
47589c8c
GM
4900
4901 /* Remember the line height so far in case the next element doesn't
4902 fit on the line. */
4903 if (!it->truncate_lines_p)
4904 {
4905 ascent = it->max_ascent;
4906 descent = it->max_descent;
4907 }
4908
5f5c8ee5
GM
4909 PRODUCE_GLYPHS (it);
4910
4911 if (it->area != TEXT_AREA)
4912 {
cafafe0b 4913 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4914 continue;
4915 }
4916
4917 /* The number of glyphs we get back in IT->nglyphs will normally
4918 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4919 character on a terminal frame, or (iii) a line end. For the
4920 second case, IT->nglyphs - 1 padding glyphs will be present
4921 (on X frames, there is only one glyph produced for a
4922 composite character.
4923
4924 The behavior implemented below means, for continuation lines,
4925 that as many spaces of a TAB as fit on the current line are
4926 displayed there. For terminal frames, as many glyphs of a
4927 multi-glyph character are displayed in the current line, too.
4928 This is what the old redisplay code did, and we keep it that
4929 way. Under X, the whole shape of a complex character must
4930 fit on the line or it will be completely displayed in the
4931 next line.
4932
4933 Note that both for tabs and padding glyphs, all glyphs have
4934 the same width. */
4935 if (it->nglyphs)
4936 {
4937 /* More than one glyph or glyph doesn't fit on line. All
4938 glyphs have the same width. */
4939 int single_glyph_width = it->pixel_width / it->nglyphs;
4940 int new_x;
4941
4942 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4943 {
4944 new_x = x + single_glyph_width;
4945
4946 /* We want to leave anything reaching TO_X to the caller. */
4947 if ((op & MOVE_TO_X) && new_x > to_x)
4948 {
4949 it->current_x = x;
4950 result = MOVE_X_REACHED;
4951 break;
4952 }
4953 else if (/* Lines are continued. */
4954 !it->truncate_lines_p
4955 && (/* And glyph doesn't fit on the line. */
4956 new_x > it->last_visible_x
4957 /* Or it fits exactly and we're on a window
4958 system frame. */
4959 || (new_x == it->last_visible_x
4960 && FRAME_WINDOW_P (it->f))))
4961 {
4962 if (/* IT->hpos == 0 means the very first glyph
4963 doesn't fit on the line, e.g. a wide image. */
4964 it->hpos == 0
4965 || (new_x == it->last_visible_x
4966 && FRAME_WINDOW_P (it->f)))
4967 {
4968 ++it->hpos;
4969 it->current_x = new_x;
4970 if (i == it->nglyphs - 1)
cafafe0b 4971 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4972 }
4973 else
47589c8c
GM
4974 {
4975 it->current_x = x;
4976 it->max_ascent = ascent;
4977 it->max_descent = descent;
4978 }
4979
4980 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4981 IT_CHARPOS (*it)));
5f5c8ee5
GM
4982 result = MOVE_LINE_CONTINUED;
4983 break;
4984 }
4985 else if (new_x > it->first_visible_x)
4986 {
4987 /* Glyph is visible. Increment number of glyphs that
4988 would be displayed. */
4989 ++it->hpos;
4990 }
4991 else
4992 {
4993 /* Glyph is completely off the left margin of the display
4994 area. Nothing to do. */
4995 }
4996 }
4997
4998 if (result != MOVE_UNDEFINED)
4999 break;
5000 }
5001 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5002 {
5003 /* Stop when TO_X specified and reached. This check is
5004 necessary here because of lines consisting of a line end,
5005 only. The line end will not produce any glyphs and we
5006 would never get MOVE_X_REACHED. */
5007 xassert (it->nglyphs == 0);
5008 result = MOVE_X_REACHED;
5009 break;
5010 }
5011
5012 /* Is this a line end? If yes, we're done. */
5013 if (ITERATOR_AT_END_OF_LINE_P (it))
5014 {
5015 result = MOVE_NEWLINE_OR_CR;
5016 break;
5017 }
5018
5019 /* The current display element has been consumed. Advance
5020 to the next. */
cafafe0b 5021 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5022
5023 /* Stop if lines are truncated and IT's current x-position is
5024 past the right edge of the window now. */
5025 if (it->truncate_lines_p
5026 && it->current_x >= it->last_visible_x)
5027 {
5028 result = MOVE_LINE_TRUNCATED;
5029 break;
5030 }
5031 }
5032
5033 /* Restore the iterator settings altered at the beginning of this
5034 function. */
5035 it->glyph_row = saved_glyph_row;
5036 return result;
5037}
5038
5039
5040/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5041 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5042 the description of enum move_operation_enum.
5043
5044 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5045 screen line, this function will set IT to the next position >
5046 TO_CHARPOS. */
5047
5048void
5049move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5050 struct it *it;
5051 int to_charpos, to_x, to_y, to_vpos;
5052 int op;
5053{
5054 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5055 int line_height;
47589c8c 5056 int reached = 0;
5f5c8ee5 5057
47589c8c 5058 for (;;)
5f5c8ee5
GM
5059 {
5060 if (op & MOVE_TO_VPOS)
5061 {
5062 /* If no TO_CHARPOS and no TO_X specified, stop at the
5063 start of the line TO_VPOS. */
5064 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5065 {
5066 if (it->vpos == to_vpos)
47589c8c
GM
5067 {
5068 reached = 1;
5069 break;
5070 }
5071 else
5072 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5073 }
5074 else
5075 {
5076 /* TO_VPOS >= 0 means stop at TO_X in the line at
5077 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5078 if (it->vpos == to_vpos)
5079 {
5080 reached = 2;
5081 break;
5082 }
5083
5f5c8ee5
GM
5084 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5085
5086 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5087 {
5088 reached = 3;
5089 break;
5090 }
5f5c8ee5
GM
5091 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5092 {
5093 /* We have reached TO_X but not in the line we want. */
5094 skip = move_it_in_display_line_to (it, to_charpos,
5095 -1, MOVE_TO_POS);
5096 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5097 {
5098 reached = 4;
5099 break;
5100 }
5f5c8ee5
GM
5101 }
5102 }
5103 }
5104 else if (op & MOVE_TO_Y)
5105 {
5106 struct it it_backup;
5f5c8ee5
GM
5107
5108 /* TO_Y specified means stop at TO_X in the line containing
5109 TO_Y---or at TO_CHARPOS if this is reached first. The
5110 problem is that we can't really tell whether the line
5111 contains TO_Y before we have completely scanned it, and
5112 this may skip past TO_X. What we do is to first scan to
5113 TO_X.
5114
5115 If TO_X is not specified, use a TO_X of zero. The reason
5116 is to make the outcome of this function more predictable.
5117 If we didn't use TO_X == 0, we would stop at the end of
5118 the line which is probably not what a caller would expect
5119 to happen. */
5120 skip = move_it_in_display_line_to (it, to_charpos,
5121 ((op & MOVE_TO_X)
5122 ? to_x : 0),
5123 (MOVE_TO_X
5124 | (op & MOVE_TO_POS)));
5125
5126 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5127 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5128 {
5129 reached = 5;
5130 break;
5131 }
5f5c8ee5
GM
5132
5133 /* If TO_X was reached, we would like to know whether TO_Y
5134 is in the line. This can only be said if we know the
5135 total line height which requires us to scan the rest of
5136 the line. */
5f5c8ee5
GM
5137 if (skip == MOVE_X_REACHED)
5138 {
5139 it_backup = *it;
47589c8c 5140 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5141 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5142 op & MOVE_TO_POS);
47589c8c 5143 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5144 }
5145
5146 /* Now, decide whether TO_Y is in this line. */
5147 line_height = it->max_ascent + it->max_descent;
47589c8c 5148 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5f5c8ee5
GM
5149
5150 if (to_y >= it->current_y
5151 && to_y < it->current_y + line_height)
5152 {
5153 if (skip == MOVE_X_REACHED)
5154 /* If TO_Y is in this line and TO_X was reached above,
5155 we scanned too far. We have to restore IT's settings
5156 to the ones before skipping. */
5157 *it = it_backup;
47589c8c 5158 reached = 6;
5f5c8ee5
GM
5159 }
5160 else if (skip == MOVE_X_REACHED)
5161 {
5162 skip = skip2;
5163 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5164 reached = 7;
5f5c8ee5
GM
5165 }
5166
47589c8c 5167 if (reached)
5f5c8ee5
GM
5168 break;
5169 }
5170 else
5171 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5172
5173 switch (skip)
5174 {
5175 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5176 reached = 8;
5177 goto out;
5f5c8ee5
GM
5178
5179 case MOVE_NEWLINE_OR_CR:
cafafe0b 5180 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5181 it->continuation_lines_width = 0;
5182 break;
5183
5184 case MOVE_LINE_TRUNCATED:
5185 it->continuation_lines_width = 0;
312246d1 5186 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5187 if ((op & MOVE_TO_POS) != 0
5188 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5189 {
5190 reached = 9;
5191 goto out;
5192 }
5f5c8ee5
GM
5193 break;
5194
5195 case MOVE_LINE_CONTINUED:
5196 it->continuation_lines_width += it->current_x;
5197 break;
5198
5199 default:
5200 abort ();
5201 }
5202
5203 /* Reset/increment for the next run. */
5204 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5205 it->current_x = it->hpos = 0;
5206 it->current_y += it->max_ascent + it->max_descent;
5207 ++it->vpos;
5208 last_height = it->max_ascent + it->max_descent;
5209 last_max_ascent = it->max_ascent;
5210 it->max_ascent = it->max_descent = 0;
5211 }
47589c8c
GM
5212
5213 out:
5214
5215 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5216}
5217
5218
5219/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5220
5221 If DY > 0, move IT backward at least that many pixels. DY = 0
5222 means move IT backward to the preceding line start or BEGV. This
5223 function may move over more than DY pixels if IT->current_y - DY
5224 ends up in the middle of a line; in this case IT->current_y will be
5225 set to the top of the line moved to. */
5226
5227void
5228move_it_vertically_backward (it, dy)
5229 struct it *it;
5230 int dy;
5231{
5232 int nlines, h, line_height;
5233 struct it it2;
5234 int start_pos = IT_CHARPOS (*it);
5235
5236 xassert (dy >= 0);
5237
5238 /* Estimate how many newlines we must move back. */
5239 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5240
5241 /* Set the iterator's position that many lines back. */
5242 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5243 back_to_previous_visible_line_start (it);
5244
5245 /* Reseat the iterator here. When moving backward, we don't want
5246 reseat to skip forward over invisible text, set up the iterator
5247 to deliver from overlay strings at the new position etc. So,
5248 use reseat_1 here. */
5249 reseat_1 (it, it->current.pos, 1);
5250
5251 /* We are now surely at a line start. */
5252 it->current_x = it->hpos = 0;
5253
5254 /* Move forward and see what y-distance we moved. First move to the
5255 start of the next line so that we get its height. We need this
5256 height to be able to tell whether we reached the specified
5257 y-distance. */
5258 it2 = *it;
5259 it2.max_ascent = it2.max_descent = 0;
5260 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5261 MOVE_TO_POS | MOVE_TO_VPOS);
5262 xassert (IT_CHARPOS (*it) >= BEGV);
5263 line_height = it2.max_ascent + it2.max_descent;
47589c8c 5264
5f5c8ee5
GM
5265 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5266 xassert (IT_CHARPOS (*it) >= BEGV);
5267 h = it2.current_y - it->current_y;
5268 nlines = it2.vpos - it->vpos;
5269
5270 /* Correct IT's y and vpos position. */
5271 it->vpos -= nlines;
5272 it->current_y -= h;
5273
5274 if (dy == 0)
5275 {
5276 /* DY == 0 means move to the start of the screen line. The
5277 value of nlines is > 0 if continuation lines were involved. */
5278 if (nlines > 0)
5279 move_it_by_lines (it, nlines, 1);
5280 xassert (IT_CHARPOS (*it) <= start_pos);
5281 }
5282 else if (nlines)
5283 {
5284 /* The y-position we try to reach. Note that h has been
5285 subtracted in front of the if-statement. */
5286 int target_y = it->current_y + h - dy;
5287
5288 /* If we did not reach target_y, try to move further backward if
5289 we can. If we moved too far backward, try to move forward. */
5290 if (target_y < it->current_y
5291 && IT_CHARPOS (*it) > BEGV)
5292 {
5293 move_it_vertically (it, target_y - it->current_y);
5294 xassert (IT_CHARPOS (*it) >= BEGV);
5295 }
5296 else if (target_y >= it->current_y + line_height
5297 && IT_CHARPOS (*it) < ZV)
5298 {
5299 move_it_vertically (it, target_y - (it->current_y + line_height));
5300 xassert (IT_CHARPOS (*it) >= BEGV);
5301 }
5302 }
5303}
5304
5305
5306/* Move IT by a specified amount of pixel lines DY. DY negative means
5307 move backwards. DY = 0 means move to start of screen line. At the
5308 end, IT will be on the start of a screen line. */
5309
5310void
5311move_it_vertically (it, dy)
5312 struct it *it;
5313 int dy;
5314{
5315 if (dy <= 0)
5316 move_it_vertically_backward (it, -dy);
5317 else if (dy > 0)
5318 {
47589c8c 5319 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5320 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5321 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5322 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5323
5324 /* If buffer ends in ZV without a newline, move to the start of
5325 the line to satisfy the post-condition. */
5326 if (IT_CHARPOS (*it) == ZV
5327 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5328 move_it_by_lines (it, 0, 0);
5329 }
5330}
5331
5332
47fc2c10
GM
5333/* Move iterator IT past the end of the text line it is in. */
5334
5335void
5336move_it_past_eol (it)
5337 struct it *it;
5338{
5339 enum move_it_result rc;
5340
5341 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5342 if (rc == MOVE_NEWLINE_OR_CR)
5343 set_iterator_to_next (it, 0);
5344}
5345
5346
2c79b732
GM
5347#if 0 /* Currently not used. */
5348
5f5c8ee5
GM
5349/* Return non-zero if some text between buffer positions START_CHARPOS
5350 and END_CHARPOS is invisible. IT->window is the window for text
5351 property lookup. */
5352
5353static int
5354invisible_text_between_p (it, start_charpos, end_charpos)
5355 struct it *it;
5356 int start_charpos, end_charpos;
5357{
5f5c8ee5
GM
5358 Lisp_Object prop, limit;
5359 int invisible_found_p;
5360
5361 xassert (it != NULL && start_charpos <= end_charpos);
5362
5363 /* Is text at START invisible? */
5364 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5365 it->window);
5366 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5367 invisible_found_p = 1;
5368 else
5369 {
016b5642
MB
5370 limit = Fnext_single_char_property_change (make_number (start_charpos),
5371 Qinvisible, Qnil,
5372 make_number (end_charpos));
5f5c8ee5
GM
5373 invisible_found_p = XFASTINT (limit) < end_charpos;
5374 }
5375
5376 return invisible_found_p;
5f5c8ee5
GM
5377}
5378
2c79b732
GM
5379#endif /* 0 */
5380
5f5c8ee5
GM
5381
5382/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5383 negative means move up. DVPOS == 0 means move to the start of the
5384 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5385 NEED_Y_P is zero, IT->current_y will be left unchanged.
5386
5387 Further optimization ideas: If we would know that IT->f doesn't use
5388 a face with proportional font, we could be faster for
5389 truncate-lines nil. */
5390
5391void
5392move_it_by_lines (it, dvpos, need_y_p)
5393 struct it *it;
5394 int dvpos, need_y_p;
5395{
5396 struct position pos;
5397
5398 if (!FRAME_WINDOW_P (it->f))
5399 {
5400 struct text_pos textpos;
5401
5402 /* We can use vmotion on frames without proportional fonts. */
5403 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5404 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5405 reseat (it, textpos, 1);
5406 it->vpos += pos.vpos;
5407 it->current_y += pos.vpos;
5408 }
5409 else if (dvpos == 0)
5410 {
5411 /* DVPOS == 0 means move to the start of the screen line. */
5412 move_it_vertically_backward (it, 0);
5413 xassert (it->current_x == 0 && it->hpos == 0);
5414 }
5415 else if (dvpos > 0)
2c79b732 5416 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5417 else
5418 {
5419 struct it it2;
5420 int start_charpos, i;
2c79b732 5421
5f5c8ee5
GM
5422 /* Go back -DVPOS visible lines and reseat the iterator there. */
5423 start_charpos = IT_CHARPOS (*it);
5424 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5425 back_to_previous_visible_line_start (it);
5426 reseat (it, it->current.pos, 1);
5427 it->current_x = it->hpos = 0;
5428
5429 /* Above call may have moved too far if continuation lines
5430 are involved. Scan forward and see if it did. */
5431 it2 = *it;
5432 it2.vpos = it2.current_y = 0;
5433 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5434 it->vpos -= it2.vpos;
5435 it->current_y -= it2.current_y;
5436 it->current_x = it->hpos = 0;
5437
5438 /* If we moved too far, move IT some lines forward. */
5439 if (it2.vpos > -dvpos)
5440 {
5441 int delta = it2.vpos + dvpos;
5442 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5443 }
5444 }
5445}
5446
5447
5448\f
5449/***********************************************************************
5450 Messages
5451 ***********************************************************************/
5452
5453
937248bc
GM
5454/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5455 to *Messages*. */
5456
5457void
5458add_to_log (format, arg1, arg2)
5459 char *format;
5460 Lisp_Object arg1, arg2;
5461{
5462 Lisp_Object args[3];
5463 Lisp_Object msg, fmt;
5464 char *buffer;
5465 int len;
5466 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5467
5468 fmt = msg = Qnil;
5469 GCPRO4 (fmt, msg, arg1, arg2);
5470
5471 args[0] = fmt = build_string (format);
5472 args[1] = arg1;
5473 args[2] = arg2;
6fc556fd 5474 msg = Fformat (3, args);
937248bc
GM
5475
5476 len = STRING_BYTES (XSTRING (msg)) + 1;
5477 buffer = (char *) alloca (len);
5478 strcpy (buffer, XSTRING (msg)->data);
5479
796184bc 5480 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5481 UNGCPRO;
5482}
5483
5484
5f5c8ee5
GM
5485/* Output a newline in the *Messages* buffer if "needs" one. */
5486
5487void
5488message_log_maybe_newline ()
5489{
5490 if (message_log_need_newline)
5491 message_dolog ("", 0, 1, 0);
5492}
5493
5494
1e313f28 5495/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5496 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5497 nonzero, means interpret the contents of M as multibyte. This
5498 function calls low-level routines in order to bypass text property
5499 hooks, etc. which might not be safe to run. */
5500
5501void
1e313f28 5502message_dolog (m, nbytes, nlflag, multibyte)
5f5c8ee5 5503 char *m;
1e313f28 5504 int nbytes, nlflag, multibyte;
5f5c8ee5
GM
5505{
5506 if (!NILP (Vmessage_log_max))
5507 {
5508 struct buffer *oldbuf;
5509 Lisp_Object oldpoint, oldbegv, oldzv;
5510 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5511 int point_at_end = 0;
5512 int zv_at_end = 0;
5513 Lisp_Object old_deactivate_mark, tem;
5514 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5515
5516 old_deactivate_mark = Vdeactivate_mark;
5517 oldbuf = current_buffer;
6a94510a 5518 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5519 current_buffer->undo_list = Qt;
5520
5521 oldpoint = Fpoint_marker ();
5522 oldbegv = Fpoint_min_marker ();
5523 oldzv = Fpoint_max_marker ();
5524 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5525
5526 if (PT == Z)
5527 point_at_end = 1;
5528 if (ZV == Z)
5529 zv_at_end = 1;
5530
5531 BEGV = BEG;
5532 BEGV_BYTE = BEG_BYTE;
5533 ZV = Z;
5534 ZV_BYTE = Z_BYTE;
5535 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5536
5537 /* Insert the string--maybe converting multibyte to single byte
5538 or vice versa, so that all the text fits the buffer. */
5539 if (multibyte
5540 && NILP (current_buffer->enable_multibyte_characters))
5541 {
1e313f28 5542 int i, c, char_bytes;
5f5c8ee5
GM
5543 unsigned char work[1];
5544
5545 /* Convert a multibyte string to single-byte
5546 for the *Message* buffer. */
1e313f28 5547 for (i = 0; i < nbytes; i += nbytes)
5f5c8ee5 5548 {
1e313f28 5549 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5550 work[0] = (SINGLE_BYTE_CHAR_P (c)
5551 ? c
5552 : multibyte_char_to_unibyte (c, Qnil));
5553 insert_1_both (work, 1, 1, 1, 0, 0);
5554 }
5555 }
5556 else if (! multibyte
5557 && ! NILP (current_buffer->enable_multibyte_characters))
5558 {
1e313f28 5559 int i, c, char_bytes;
5f5c8ee5 5560 unsigned char *msg = (unsigned char *) m;
260a86a0 5561 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5562 /* Convert a single-byte string to multibyte
5563 for the *Message* buffer. */
1e313f28 5564 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5565 {
5566 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5567 char_bytes = CHAR_STRING (c, str);
5568 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5569 }
5570 }
1e313f28
GM
5571 else if (nbytes)
5572 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5573
5574 if (nlflag)
5575 {
5576 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5577 insert_1 ("\n", 1, 1, 0, 0);
5578
5579 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5580 this_bol = PT;
5581 this_bol_byte = PT_BYTE;
5582
5583 if (this_bol > BEG)
5584 {
5585 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5586 prev_bol = PT;
5587 prev_bol_byte = PT_BYTE;
5588
5589 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5590 this_bol, this_bol_byte);
5591 if (dup)
5592 {
5593 del_range_both (prev_bol, prev_bol_byte,
5594 this_bol, this_bol_byte, 0);
5595 if (dup > 1)
5596 {
5597 char dupstr[40];
5598 int duplen;
5599
5600 /* If you change this format, don't forget to also
5601 change message_log_check_duplicate. */
5602 sprintf (dupstr, " [%d times]", dup);
5603 duplen = strlen (dupstr);
5604 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5605 insert_1 (dupstr, duplen, 1, 0, 1);
5606 }
5607 }
5608 }
5609
5610 if (NATNUMP (Vmessage_log_max))
5611 {
5612 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5613 -XFASTINT (Vmessage_log_max) - 1, 0);
5614 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5615 }
5616 }
5617 BEGV = XMARKER (oldbegv)->charpos;
5618 BEGV_BYTE = marker_byte_position (oldbegv);
5619
5620 if (zv_at_end)
5621 {
5622 ZV = Z;
5623 ZV_BYTE = Z_BYTE;
5624 }
5625 else
5626 {
5627 ZV = XMARKER (oldzv)->charpos;
5628 ZV_BYTE = marker_byte_position (oldzv);
5629 }
5630
5631 if (point_at_end)
5632 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5633 else
5634 /* We can't do Fgoto_char (oldpoint) because it will run some
5635 Lisp code. */
5636 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5637 XMARKER (oldpoint)->bytepos);
5638
5639 UNGCPRO;
5640 free_marker (oldpoint);
5641 free_marker (oldbegv);
5642 free_marker (oldzv);
5643
5644 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5645 set_buffer_internal (oldbuf);
5646 if (NILP (tem))
5647 windows_or_buffers_changed = old_windows_or_buffers_changed;
5648 message_log_need_newline = !nlflag;
5649 Vdeactivate_mark = old_deactivate_mark;
5650 }
5651}
5652
5653
5654/* We are at the end of the buffer after just having inserted a newline.
5655 (Note: We depend on the fact we won't be crossing the gap.)
5656 Check to see if the most recent message looks a lot like the previous one.
5657 Return 0 if different, 1 if the new one should just replace it, or a
5658 value N > 1 if we should also append " [N times]". */
5659
5660static int
5661message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5662 int prev_bol, this_bol;
5663 int prev_bol_byte, this_bol_byte;
5664{
5665 int i;
5666 int len = Z_BYTE - 1 - this_bol_byte;
5667 int seen_dots = 0;
5668 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5669 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5670
5671 for (i = 0; i < len; i++)
5672 {
509633e3 5673 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5674 seen_dots = 1;
5675 if (p1[i] != p2[i])
5676 return seen_dots;
5677 }
5678 p1 += len;
5679 if (*p1 == '\n')
5680 return 2;
5681 if (*p1++ == ' ' && *p1++ == '[')
5682 {
5683 int n = 0;
5684 while (*p1 >= '0' && *p1 <= '9')
5685 n = n * 10 + *p1++ - '0';
5686 if (strncmp (p1, " times]\n", 8) == 0)
5687 return n+1;
5688 }
5689 return 0;
5690}
5691
5692
1e313f28
GM
5693/* Display an echo area message M with a specified length of NBYTES
5694 bytes. The string may include null characters. If M is 0, clear
5695 out any existing message, and let the mini-buffer text show
5696 through.
5f5c8ee5
GM
5697
5698 The buffer M must continue to exist until after the echo area gets
5699 cleared or some other message gets displayed there. This means do
5700 not pass text that is stored in a Lisp string; do not pass text in
5701 a buffer that was alloca'd. */
5702
5703void
1e313f28 5704message2 (m, nbytes, multibyte)
5f5c8ee5 5705 char *m;
1e313f28 5706 int nbytes;
5f5c8ee5
GM
5707 int multibyte;
5708{
5709 /* First flush out any partial line written with print. */
5710 message_log_maybe_newline ();
5711 if (m)
1e313f28
GM
5712 message_dolog (m, nbytes, 1, multibyte);
5713 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5714}
5715
5716
5717/* The non-logging counterpart of message2. */
5718
5719void
1e313f28 5720message2_nolog (m, nbytes, multibyte)
5f5c8ee5 5721 char *m;
1e313f28 5722 int nbytes;
5f5c8ee5 5723{
886bd6f2 5724 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5725 message_enable_multibyte = multibyte;
5726
5727 if (noninteractive)
5728 {
5729 if (noninteractive_need_newline)
5730 putc ('\n', stderr);
5731 noninteractive_need_newline = 0;
5732 if (m)
1e313f28 5733 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
5734 if (cursor_in_echo_area == 0)
5735 fprintf (stderr, "\n");
5736 fflush (stderr);
5737 }
5738 /* A null message buffer means that the frame hasn't really been
5739 initialized yet. Error messages get reported properly by
5740 cmd_error, so this must be just an informative message; toss it. */
5741 else if (INTERACTIVE
886bd6f2
GM
5742 && sf->glyphs_initialized_p
5743 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5744 {
5745 Lisp_Object mini_window;
5746 struct frame *f;
5747
5748 /* Get the frame containing the mini-buffer
5749 that the selected frame is using. */
886bd6f2 5750 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5751 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5752
5753 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5754 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5755 && ! FRAME_VISIBLE_P (f))
5756 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5757
5758 if (m)
5759 {
1e313f28 5760 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5761 if (minibuffer_auto_raise)
5762 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5763 }
5764 else
c6e89d6c 5765 clear_message (1, 1);
5f5c8ee5 5766
c6e89d6c 5767 do_pending_window_change (0);
5f5c8ee5 5768 echo_area_display (1);
c6e89d6c 5769 do_pending_window_change (0);
5f5c8ee5
GM
5770 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5771 (*frame_up_to_date_hook) (f);
5772 }
5773}
5774
5775
c6e89d6c
GM
5776/* Display an echo area message M with a specified length of NBYTES
5777 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5778 string, clear out any existing message, and let the mini-buffer
5779 text show through. */
5780
5781void
c6e89d6c 5782message3 (m, nbytes, multibyte)
5f5c8ee5 5783 Lisp_Object m;
c6e89d6c 5784 int nbytes;
5f5c8ee5
GM
5785 int multibyte;
5786{
5787 struct gcpro gcpro1;
5788
5789 GCPRO1 (m);
5790
5791 /* First flush out any partial line written with print. */
5792 message_log_maybe_newline ();
5793 if (STRINGP (m))
c6e89d6c
GM
5794 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5795 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5796
5797 UNGCPRO;
5798}
5799
5800
5801/* The non-logging version of message3. */
5802
5803void
c6e89d6c 5804message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5805 Lisp_Object m;
c6e89d6c 5806 int nbytes, multibyte;
5f5c8ee5 5807{
886bd6f2 5808 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5809 message_enable_multibyte = multibyte;
5810
5811 if (noninteractive)
5812 {
5813 if (noninteractive_need_newline)
5814 putc ('\n', stderr);
5815 noninteractive_need_newline = 0;
5816 if (STRINGP (m))
c6e89d6c 5817 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5818 if (cursor_in_echo_area == 0)
5819 fprintf (stderr, "\n");
5820 fflush (stderr);
5821 }
5822 /* A null message buffer means that the frame hasn't really been
5823 initialized yet. Error messages get reported properly by
5824 cmd_error, so this must be just an informative message; toss it. */
5825 else if (INTERACTIVE
886bd6f2
GM
5826 && sf->glyphs_initialized_p
5827 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5828 {
5829 Lisp_Object mini_window;
c6e89d6c 5830 Lisp_Object frame;
5f5c8ee5
GM
5831 struct frame *f;
5832
5833 /* Get the frame containing the mini-buffer
5834 that the selected frame is using. */
886bd6f2 5835 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5836 frame = XWINDOW (mini_window)->frame;
5837 f = XFRAME (frame);
5f5c8ee5
GM
5838
5839 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5840 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5841 && !FRAME_VISIBLE_P (f))
5842 Fmake_frame_visible (frame);
5f5c8ee5 5843
c6e89d6c 5844 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5845 {
c6e89d6c 5846 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5847 if (minibuffer_auto_raise)
5848 Fraise_frame (frame);
5f5c8ee5
GM
5849 }
5850 else
c6e89d6c 5851 clear_message (1, 1);
5f5c8ee5 5852
c6e89d6c 5853 do_pending_window_change (0);
5f5c8ee5 5854 echo_area_display (1);
c6e89d6c 5855 do_pending_window_change (0);
5f5c8ee5
GM
5856 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5857 (*frame_up_to_date_hook) (f);
5858 }
5859}
5860
5861
5862/* Display a null-terminated echo area message M. If M is 0, clear
5863 out any existing message, and let the mini-buffer text show through.
5864
5865 The buffer M must continue to exist until after the echo area gets
5866 cleared or some other message gets displayed there. Do not pass
5867 text that is stored in a Lisp string. Do not pass text in a buffer
5868 that was alloca'd. */
5869
5870void
5871message1 (m)
5872 char *m;
5873{
5874 message2 (m, (m ? strlen (m) : 0), 0);
5875}
5876
5877
5878/* The non-logging counterpart of message1. */
5879
5880void
5881message1_nolog (m)
5882 char *m;
5883{
5884 message2_nolog (m, (m ? strlen (m) : 0), 0);
5885}
5886
5887/* Display a message M which contains a single %s
5888 which gets replaced with STRING. */
5889
5890void
5891message_with_string (m, string, log)
5892 char *m;
5893 Lisp_Object string;
5894 int log;
5895{
5896 if (noninteractive)
5897 {
5898 if (m)
5899 {
5900 if (noninteractive_need_newline)
5901 putc ('\n', stderr);
5902 noninteractive_need_newline = 0;
5903 fprintf (stderr, m, XSTRING (string)->data);
5904 if (cursor_in_echo_area == 0)
5905 fprintf (stderr, "\n");
5906 fflush (stderr);
5907 }
5908 }
5909 else if (INTERACTIVE)
5910 {
5911 /* The frame whose minibuffer we're going to display the message on.
5912 It may be larger than the selected frame, so we need
5913 to use its buffer, not the selected frame's buffer. */
5914 Lisp_Object mini_window;
886bd6f2 5915 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5916
5917 /* Get the frame containing the minibuffer
5918 that the selected frame is using. */
886bd6f2 5919 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5920 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5921
5922 /* A null message buffer means that the frame hasn't really been
5923 initialized yet. Error messages get reported properly by
5924 cmd_error, so this must be just an informative message; toss it. */
5925 if (FRAME_MESSAGE_BUF (f))
5926 {
5927 int len;
5928 char *a[1];
5929 a[0] = (char *) XSTRING (string)->data;
5930
5931 len = doprnt (FRAME_MESSAGE_BUF (f),
5932 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5933
5934 if (log)
5935 message2 (FRAME_MESSAGE_BUF (f), len,
5936 STRING_MULTIBYTE (string));
5937 else
5938 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5939 STRING_MULTIBYTE (string));
5940
5941 /* Print should start at the beginning of the message
5942 buffer next time. */
5943 message_buf_print = 0;
5944 }
5945 }
5946}
5947
5948
5f5c8ee5
GM
5949/* Dump an informative message to the minibuf. If M is 0, clear out
5950 any existing message, and let the mini-buffer text show through. */
5951
5952/* VARARGS 1 */
5953void
5954message (m, a1, a2, a3)
5955 char *m;
5956 EMACS_INT a1, a2, a3;
5957{
5958 if (noninteractive)
5959 {
5960 if (m)
5961 {
5962 if (noninteractive_need_newline)
5963 putc ('\n', stderr);
5964 noninteractive_need_newline = 0;
5965 fprintf (stderr, m, a1, a2, a3);
5966 if (cursor_in_echo_area == 0)
5967 fprintf (stderr, "\n");
5968 fflush (stderr);
5969 }
5970 }
5971 else if (INTERACTIVE)
5972 {
5973 /* The frame whose mini-buffer we're going to display the message
5974 on. It may be larger than the selected frame, so we need to
5975 use its buffer, not the selected frame's buffer. */
5976 Lisp_Object mini_window;
886bd6f2 5977 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5978
5979 /* Get the frame containing the mini-buffer
5980 that the selected frame is using. */
886bd6f2 5981 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5982 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5983
5984 /* A null message buffer means that the frame hasn't really been
5985 initialized yet. Error messages get reported properly by
5986 cmd_error, so this must be just an informative message; toss
5987 it. */
5988 if (FRAME_MESSAGE_BUF (f))
5989 {
5990 if (m)
5991 {
5992 int len;
5993#ifdef NO_ARG_ARRAY
5994 char *a[3];
5995 a[0] = (char *) a1;
5996 a[1] = (char *) a2;
5997 a[2] = (char *) a3;
5998
5999 len = doprnt (FRAME_MESSAGE_BUF (f),
6000 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6001#else
6002 len = doprnt (FRAME_MESSAGE_BUF (f),
6003 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6004 (char **) &a1);
6005#endif /* NO_ARG_ARRAY */
6006
6007 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6008 }
6009 else
6010 message1 (0);
6011
6012 /* Print should start at the beginning of the message
6013 buffer next time. */
6014 message_buf_print = 0;
6015 }
6016 }
6017}
6018
6019
6020/* The non-logging version of message. */
6021
6022void
6023message_nolog (m, a1, a2, a3)
6024 char *m;
6025 EMACS_INT a1, a2, a3;
6026{
6027 Lisp_Object old_log_max;
6028 old_log_max = Vmessage_log_max;
6029 Vmessage_log_max = Qnil;
6030 message (m, a1, a2, a3);
6031 Vmessage_log_max = old_log_max;
6032}
6033
6034
c6e89d6c
GM
6035/* Display the current message in the current mini-buffer. This is
6036 only called from error handlers in process.c, and is not time
6037 critical. */
5f5c8ee5
GM
6038
6039void
6040update_echo_area ()
6041{
c6e89d6c
GM
6042 if (!NILP (echo_area_buffer[0]))
6043 {
6044 Lisp_Object string;
6045 string = Fcurrent_message ();
6046 message3 (string, XSTRING (string)->size,
6047 !NILP (current_buffer->enable_multibyte_characters));
6048 }
6049}
6050
6051
5bcfeb49
GM
6052/* Make sure echo area buffers in echo_buffers[] are life. If they
6053 aren't, make new ones. */
6054
6055static void
6056ensure_echo_area_buffers ()
6057{
6058 int i;
6059
6060 for (i = 0; i < 2; ++i)
6061 if (!BUFFERP (echo_buffer[i])
6062 || NILP (XBUFFER (echo_buffer[i])->name))
6063 {
6064 char name[30];
ff3d9573
GM
6065 Lisp_Object old_buffer;
6066 int j;
6067
6068 old_buffer = echo_buffer[i];
5bcfeb49
GM
6069 sprintf (name, " *Echo Area %d*", i);
6070 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6071 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6072
6073 for (j = 0; j < 2; ++j)
6074 if (EQ (old_buffer, echo_area_buffer[j]))
6075 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6076 }
6077}
6078
6079
23a96c77 6080/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6081 echo_area_buffer as current buffer.
6082
6083 WHICH zero means use the current message buffer
6084 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6085 from echo_buffer[] and clear it.
6086
6087 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6088 suitable buffer from echo_buffer[] and clear it.
6089
6090 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6091 that the current message becomes the last displayed one, make
6092 choose a suitable buffer for echo_area_buffer[0], and clear it.
6093
6094 Value is what FN returns. */
6095
6096static int
23a96c77 6097with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6098 struct window *w;
6099 int which;
23dd2d97
KR
6100 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6101 EMACS_INT a1;
6102 Lisp_Object a2;
6103 EMACS_INT a3, a4;
c6e89d6c
GM
6104{
6105 Lisp_Object buffer;
15e26c76 6106 int this_one, the_other, clear_buffer_p, rc;
2d27dae2 6107 int count = BINDING_STACK_SIZE ();
c6e89d6c
GM
6108
6109 /* If buffers aren't life, make new ones. */
5bcfeb49 6110 ensure_echo_area_buffers ();
c6e89d6c
GM
6111
6112 clear_buffer_p = 0;
6113
6114 if (which == 0)
6115 this_one = 0, the_other = 1;
6116 else if (which > 0)
6117 this_one = 1, the_other = 0;
5f5c8ee5 6118 else
c6e89d6c
GM
6119 {
6120 this_one = 0, the_other = 1;
6121 clear_buffer_p = 1;
6122
6123 /* We need a fresh one in case the current echo buffer equals
6124 the one containing the last displayed echo area message. */
6125 if (!NILP (echo_area_buffer[this_one])
6126 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6127 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6128 }
6129
6130 /* Choose a suitable buffer from echo_buffer[] is we don't
6131 have one. */
6132 if (NILP (echo_area_buffer[this_one]))
6133 {
6134 echo_area_buffer[this_one]
6135 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6136 ? echo_buffer[the_other]
6137 : echo_buffer[this_one]);
6138 clear_buffer_p = 1;
6139 }
6140
6141 buffer = echo_area_buffer[this_one];
6142
1013f4e3
GM
6143 /* Don't get confused by reusing the buffer used for echoing
6144 for a different purpose. */
6145 if (!echoing && EQ (buffer, echo_message_buffer))
6146 cancel_echoing ();
6147
c6e89d6c
GM
6148 record_unwind_protect (unwind_with_echo_area_buffer,
6149 with_echo_area_buffer_unwind_data (w));
6150
6151 /* Make the echo area buffer current. Note that for display
6152 purposes, it is not necessary that the displayed window's buffer
6153 == current_buffer, except for text property lookup. So, let's
6154 only set that buffer temporarily here without doing a full
6155 Fset_window_buffer. We must also change w->pointm, though,
6156 because otherwise an assertions in unshow_buffer fails, and Emacs
6157 aborts. */
9142dd5b 6158 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6159 if (w)
6160 {
6161 w->buffer = buffer;
6162 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6163 }
ad4f174e 6164
c6e89d6c
GM
6165 current_buffer->undo_list = Qt;
6166 current_buffer->read_only = Qnil;
bbbf6d06 6167 specbind (Qinhibit_read_only, Qt);
c6e89d6c
GM
6168
6169 if (clear_buffer_p && Z > BEG)
6170 del_range (BEG, Z);
6171
6172 xassert (BEGV >= BEG);
6173 xassert (ZV <= Z && ZV >= BEGV);
6174
23a96c77 6175 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6176
6177 xassert (BEGV >= BEG);
6178 xassert (ZV <= Z && ZV >= BEGV);
6179
6180 unbind_to (count, Qnil);
6181 return rc;
5f5c8ee5
GM
6182}
6183
6184
c6e89d6c
GM
6185/* Save state that should be preserved around the call to the function
6186 FN called in with_echo_area_buffer. */
5f5c8ee5 6187
c6e89d6c
GM
6188static Lisp_Object
6189with_echo_area_buffer_unwind_data (w)
6190 struct window *w;
5f5c8ee5 6191{
c6e89d6c
GM
6192 int i = 0;
6193 Lisp_Object vector;
5f5c8ee5 6194
c6e89d6c
GM
6195 /* Reduce consing by keeping one vector in
6196 Vwith_echo_area_save_vector. */
6197 vector = Vwith_echo_area_save_vector;
6198 Vwith_echo_area_save_vector = Qnil;
6199
6200 if (NILP (vector))
9142dd5b 6201 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c 6202
a61b7058
GM
6203 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6204 AREF (vector, i) = Vdeactivate_mark, ++i;
6205 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
c6e89d6c
GM
6206
6207 if (w)
6208 {
a61b7058
GM
6209 XSETWINDOW (AREF (vector, i), w); ++i;
6210 AREF (vector, i) = w->buffer; ++i;
6211 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6212 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6213 }
6214 else
6215 {
6216 int end = i + 4;
a61b7058
GM
6217 for (; i < end; ++i)
6218 AREF (vector, i) = Qnil;
c6e89d6c 6219 }
5f5c8ee5 6220
a61b7058 6221 xassert (i == ASIZE (vector));
c6e89d6c
GM
6222 return vector;
6223}
5f5c8ee5 6224
5f5c8ee5 6225
c6e89d6c
GM
6226/* Restore global state from VECTOR which was created by
6227 with_echo_area_buffer_unwind_data. */
6228
6229static Lisp_Object
6230unwind_with_echo_area_buffer (vector)
6231 Lisp_Object vector;
6232{
bbbf6d06
GM
6233 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6234 Vdeactivate_mark = AREF (vector, 1);
6235 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6236
bbbf6d06 6237 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6238 {
6239 struct window *w;
6240 Lisp_Object buffer, charpos, bytepos;
6241
bbbf6d06
GM
6242 w = XWINDOW (AREF (vector, 3));
6243 buffer = AREF (vector, 4);
6244 charpos = AREF (vector, 5);
6245 bytepos = AREF (vector, 6);
c6e89d6c
GM
6246
6247 w->buffer = buffer;
6248 set_marker_both (w->pointm, buffer,
6249 XFASTINT (charpos), XFASTINT (bytepos));
6250 }
6251
6252 Vwith_echo_area_save_vector = vector;
6253 return Qnil;
6254}
6255
6256
6257/* Set up the echo area for use by print functions. MULTIBYTE_P
6258 non-zero means we will print multibyte. */
6259
6260void
6261setup_echo_area_for_printing (multibyte_p)
6262 int multibyte_p;
6263{
5bcfeb49
GM
6264 ensure_echo_area_buffers ();
6265
c6e89d6c
GM
6266 if (!message_buf_print)
6267 {
6268 /* A message has been output since the last time we printed.
6269 Choose a fresh echo area buffer. */
6270 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6271 echo_area_buffer[0] = echo_buffer[1];
6272 else
6273 echo_area_buffer[0] = echo_buffer[0];
6274
6275 /* Switch to that buffer and clear it. */
6276 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6277 current_buffer->truncate_lines = Qnil;
bbbf6d06 6278
c6e89d6c 6279 if (Z > BEG)
bbbf6d06
GM
6280 {
6281 int count = BINDING_STACK_SIZE ();
6282 specbind (Qinhibit_read_only, Qt);
6283 del_range (BEG, Z);
6284 unbind_to (count, Qnil);
6285 }
c6e89d6c
GM
6286 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6287
6288 /* Set up the buffer for the multibyteness we need. */
6289 if (multibyte_p
6290 != !NILP (current_buffer->enable_multibyte_characters))
6291 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6292
6293 /* Raise the frame containing the echo area. */
6294 if (minibuffer_auto_raise)
6295 {
886bd6f2 6296 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6297 Lisp_Object mini_window;
886bd6f2 6298 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6299 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6300 }
6301
8a4e3c0c 6302 message_log_maybe_newline ();
c6e89d6c
GM
6303 message_buf_print = 1;
6304 }
fa77249f
GM
6305 else
6306 {
6307 if (NILP (echo_area_buffer[0]))
6308 {
6309 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6310 echo_area_buffer[0] = echo_buffer[1];
6311 else
6312 echo_area_buffer[0] = echo_buffer[0];
6313 }
6314
6315 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
6316 {
6317 /* Someone switched buffers between print requests. */
6318 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6319 current_buffer->truncate_lines = Qnil;
6320 }
fa77249f 6321 }
c6e89d6c
GM
6322}
6323
6324
dd2eb166
GM
6325/* Display an echo area message in window W. Value is non-zero if W's
6326 height is changed. If display_last_displayed_message_p is
6327 non-zero, display the message that was last displayed, otherwise
6328 display the current message. */
c6e89d6c
GM
6329
6330static int
6331display_echo_area (w)
6332 struct window *w;
6333{
25edb08f
GM
6334 int i, no_message_p, window_height_changed_p, count;
6335
6336 /* Temporarily disable garbage collections while displaying the echo
6337 area. This is done because a GC can print a message itself.
6338 That message would modify the echo area buffer's contents while a
6339 redisplay of the buffer is going on, and seriously confuse
6340 redisplay. */
6341 count = inhibit_garbage_collection ();
dd2eb166
GM
6342
6343 /* If there is no message, we must call display_echo_area_1
6344 nevertheless because it resizes the window. But we will have to
6345 reset the echo_area_buffer in question to nil at the end because
6346 with_echo_area_buffer will sets it to an empty buffer. */
6347 i = display_last_displayed_message_p ? 1 : 0;
6348 no_message_p = NILP (echo_area_buffer[i]);
6349
6350 window_height_changed_p
6351 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6352 display_echo_area_1,
23dd2d97 6353 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6354
6355 if (no_message_p)
6356 echo_area_buffer[i] = Qnil;
25edb08f
GM
6357
6358 unbind_to (count, Qnil);
dd2eb166 6359 return window_height_changed_p;
c6e89d6c
GM
6360}
6361
6362
6363/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6364 contains the current echo area message in window W, a mini-window,
6365 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6366 Change the height of W so that all of the message is displayed.
6367 Value is non-zero if height of W was changed. */
6368
6369static int
23a96c77 6370display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6371 EMACS_INT a1;
6372 Lisp_Object a2;
6373 EMACS_INT a3, a4;
c6e89d6c 6374{
23a96c77 6375 struct window *w = (struct window *) a1;
c6e89d6c 6376 Lisp_Object window;
c6e89d6c
GM
6377 struct text_pos start;
6378 int window_height_changed_p = 0;
6379
6380 /* Do this before displaying, so that we have a large enough glyph
6381 matrix for the display. */
92a90e89 6382 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6383
6384 /* Display. */
6385 clear_glyph_matrix (w->desired_matrix);
6386 XSETWINDOW (window, w);
6387 SET_TEXT_POS (start, BEG, BEG_BYTE);
6388 try_window (window, start);
6389
c6e89d6c
GM
6390 return window_height_changed_p;
6391}
6392
6393
92a90e89
GM
6394/* Resize the echo area window to exactly the size needed for the
6395 currently displayed message, if there is one. */
6396
6397void
6398resize_echo_area_axactly ()
6399{
6400 if (BUFFERP (echo_area_buffer[0])
6401 && WINDOWP (echo_area_window))
6402 {
6403 struct window *w = XWINDOW (echo_area_window);
6404 int resized_p;
6405
23a96c77 6406 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
23dd2d97 6407 (EMACS_INT) w, Qnil, 0, 0);
92a90e89
GM
6408 if (resized_p)
6409 {
6410 ++windows_or_buffers_changed;
6411 ++update_mode_lines;
6412 redisplay_internal (0);
6413 }
6414 }
6415}
6416
6417
23a96c77
GM
6418/* Callback function for with_echo_area_buffer, when used from
6419 resize_echo_area_axactly. A1 contains a pointer to the window to
6420 resize, A2 to A4 are not used. Value is what resize_mini_window
6421 returns. */
6422
6423static int
6424resize_mini_window_1 (a1, a2, a3, a4)
23dd2d97
KR
6425 EMACS_INT a1;
6426 Lisp_Object a2;
6427 EMACS_INT a3, a4;
23a96c77
GM
6428{
6429 return resize_mini_window ((struct window *) a1, 1);
6430}
6431
6432
92a90e89
GM
6433/* Resize mini-window W to fit the size of its contents. EXACT:P
6434 means size the window exactly to the size needed. Otherwise, it's
6435 only enlarged until W's buffer is empty. Value is non-zero if
6436 the window height has been changed. */
c6e89d6c 6437
9472f927 6438int
92a90e89 6439resize_mini_window (w, exact_p)
c6e89d6c 6440 struct window *w;
92a90e89 6441 int exact_p;
c6e89d6c
GM
6442{
6443 struct frame *f = XFRAME (w->frame);
6444 int window_height_changed_p = 0;
6445
6446 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
6447
6448 /* Nil means don't try to resize. */
6422c1d7 6449 if (NILP (Vresize_mini_windows)
00f6d59e 6450 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6451 return 0;
c6e89d6c
GM
6452
6453 if (!FRAME_MINIBUF_ONLY_P (f))
6454 {
6455 struct it it;
dd2eb166
GM
6456 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6457 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6458 int height, max_height;
6459 int unit = CANON_Y_UNIT (f);
6460 struct text_pos start;
1bfdbe43
GM
6461 struct buffer *old_current_buffer = NULL;
6462
6463 if (current_buffer != XBUFFER (w->buffer))
6464 {
6465 old_current_buffer = current_buffer;
6466 set_buffer_internal (XBUFFER (w->buffer));
6467 }
9142dd5b 6468
c6e89d6c 6469 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6470
dd2eb166
GM
6471 /* Compute the max. number of lines specified by the user. */
6472 if (FLOATP (Vmax_mini_window_height))
6473 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6474 else if (INTEGERP (Vmax_mini_window_height))
6475 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6476 else
6477 max_height = total_height / 4;
dd2eb166
GM
6478
6479 /* Correct that max. height if it's bogus. */
6480 max_height = max (1, max_height);
6481 max_height = min (total_height, max_height);
6482
6483 /* Find out the height of the text in the window. */
ad4f174e
GM
6484 if (it.truncate_lines_p)
6485 height = 1;
55b064bd 6486 else
ad4f174e
GM
6487 {
6488 last_height = 0;
6489 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6490 if (it.max_ascent == 0 && it.max_descent == 0)
6491 height = it.current_y + last_height;
6492 else
6493 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6494 height -= it.extra_line_spacing;
ad4f174e
GM
6495 height = (height + unit - 1) / unit;
6496 }
dd2eb166
GM
6497
6498 /* Compute a suitable window start. */
6499 if (height > max_height)
6500 {
6501 height = max_height;
6502 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6503 move_it_vertically_backward (&it, (height - 1) * unit);
6504 start = it.current.pos;
6505 }
6506 else
6507 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6508 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6509
6422c1d7 6510 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6511 {
6422c1d7
GM
6512 /* Let it grow only, until we display an empty message, in which
6513 case the window shrinks again. */
6514 if (height > XFASTINT (w->height))
6515 {
6516 int old_height = XFASTINT (w->height);
6517 freeze_window_starts (f, 1);
6518 grow_mini_window (w, height - XFASTINT (w->height));
6519 window_height_changed_p = XFASTINT (w->height) != old_height;
6520 }
6521 else if (height < XFASTINT (w->height)
6522 && (exact_p || BEGV == ZV))
6523 {
6524 int old_height = XFASTINT (w->height);
6525 freeze_window_starts (f, 0);
6526 shrink_mini_window (w);
6527 window_height_changed_p = XFASTINT (w->height) != old_height;
6528 }
da448723 6529 }
6422c1d7 6530 else
da448723 6531 {
6422c1d7
GM
6532 /* Always resize to exact size needed. */
6533 if (height > XFASTINT (w->height))
6534 {
6535 int old_height = XFASTINT (w->height);
6536 freeze_window_starts (f, 1);
6537 grow_mini_window (w, height - XFASTINT (w->height));
6538 window_height_changed_p = XFASTINT (w->height) != old_height;
6539 }
6540 else if (height < XFASTINT (w->height))
6541 {
6542 int old_height = XFASTINT (w->height);
6543 freeze_window_starts (f, 0);
6544 shrink_mini_window (w);
6545
6546 if (height)
6547 {
6548 freeze_window_starts (f, 1);
6549 grow_mini_window (w, height - XFASTINT (w->height));
6550 }
6551
6552 window_height_changed_p = XFASTINT (w->height) != old_height;
6553 }
9142dd5b 6554 }
1bfdbe43
GM
6555
6556 if (old_current_buffer)
6557 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6558 }
6559
6560 return window_height_changed_p;
6561}
6562
6563
6564/* Value is the current message, a string, or nil if there is no
6565 current message. */
6566
6567Lisp_Object
6568current_message ()
6569{
6570 Lisp_Object msg;
6571
6572 if (NILP (echo_area_buffer[0]))
6573 msg = Qnil;
6574 else
6575 {
23a96c77 6576 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6577 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6578 if (NILP (msg))
6579 echo_area_buffer[0] = Qnil;
6580 }
6581
6582 return msg;
6583}
6584
6585
6586static int
23a96c77 6587current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6588 EMACS_INT a1;
6589 Lisp_Object a2;
6590 EMACS_INT a3, a4;
c6e89d6c 6591{
23a96c77
GM
6592 Lisp_Object *msg = (Lisp_Object *) a1;
6593
c6e89d6c
GM
6594 if (Z > BEG)
6595 *msg = make_buffer_string (BEG, Z, 1);
6596 else
6597 *msg = Qnil;
6598 return 0;
6599}
6600
6601
6602/* Push the current message on Vmessage_stack for later restauration
6603 by restore_message. Value is non-zero if the current message isn't
6604 empty. This is a relatively infrequent operation, so it's not
6605 worth optimizing. */
6606
6607int
6608push_message ()
6609{
6610 Lisp_Object msg;
6611 msg = current_message ();
6612 Vmessage_stack = Fcons (msg, Vmessage_stack);
6613 return STRINGP (msg);
6614}
6615
6616
098ec84a
GM
6617/* Handler for record_unwind_protect calling pop_message. */
6618
6619Lisp_Object
6620push_message_unwind (dummy)
6621 Lisp_Object dummy;
6622{
6623 pop_message ();
6624 return Qnil;
6625}
6626
6627
c6e89d6c
GM
6628/* Restore message display from the top of Vmessage_stack. */
6629
6630void
6631restore_message ()
6632{
6633 Lisp_Object msg;
6634
6635 xassert (CONSP (Vmessage_stack));
6636 msg = XCAR (Vmessage_stack);
6637 if (STRINGP (msg))
6638 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6639 else
6640 message3_nolog (msg, 0, 0);
6641}
6642
6643
6644/* Pop the top-most entry off Vmessage_stack. */
6645
6646void
6647pop_message ()
6648{
6649 xassert (CONSP (Vmessage_stack));
6650 Vmessage_stack = XCDR (Vmessage_stack);
6651}
6652
6653
6654/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6655 exits. If the stack is not empty, we have a missing pop_message
6656 somewhere. */
6657
6658void
6659check_message_stack ()
6660{
6661 if (!NILP (Vmessage_stack))
6662 abort ();
6663}
6664
6665
6666/* Truncate to NCHARS what will be displayed in the echo area the next
6667 time we display it---but don't redisplay it now. */
6668
6669void
6670truncate_echo_area (nchars)
6671 int nchars;
6672{
6673 if (nchars == 0)
6674 echo_area_buffer[0] = Qnil;
6675 /* A null message buffer means that the frame hasn't really been
6676 initialized yet. Error messages get reported properly by
6677 cmd_error, so this must be just an informative message; toss it. */
6678 else if (!noninteractive
6679 && INTERACTIVE
c6e89d6c 6680 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6681 {
6682 struct frame *sf = SELECTED_FRAME ();
6683 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6684 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6685 }
c6e89d6c
GM
6686}
6687
6688
6689/* Helper function for truncate_echo_area. Truncate the current
6690 message to at most NCHARS characters. */
6691
6692static int
23a96c77 6693truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6694 EMACS_INT nchars;
6695 Lisp_Object a2;
6696 EMACS_INT a3, a4;
c6e89d6c
GM
6697{
6698 if (BEG + nchars < Z)
6699 del_range (BEG + nchars, Z);
6700 if (Z == BEG)
6701 echo_area_buffer[0] = Qnil;
6702 return 0;
6703}
6704
6705
6706/* Set the current message to a substring of S or STRING.
6707
6708 If STRING is a Lisp string, set the message to the first NBYTES
6709 bytes from STRING. NBYTES zero means use the whole string. If
6710 STRING is multibyte, the message will be displayed multibyte.
6711
6712 If S is not null, set the message to the first LEN bytes of S. LEN
6713 zero means use the whole string. MULTIBYTE_P non-zero means S is
6714 multibyte. Display the message multibyte in that case. */
6715
6716void
6717set_message (s, string, nbytes, multibyte_p)
6718 char *s;
6719 Lisp_Object string;
6720 int nbytes;
6721{
6722 message_enable_multibyte
6723 = ((s && multibyte_p)
6724 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6725
23a96c77
GM
6726 with_echo_area_buffer (0, -1, set_message_1,
6727 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6728 message_buf_print = 0;
21fdfb65 6729 help_echo_showing_p = 0;
c6e89d6c
GM
6730}
6731
6732
6733/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6734 as there, with A1 corresponding to S and A2 corresponding to STRING
6735 This function is called with the echo area buffer being
c6e89d6c
GM
6736 current. */
6737
6738static int
23a96c77 6739set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6740 EMACS_INT a1;
6741 Lisp_Object a2;
6742 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6743{
23a96c77 6744 char *s = (char *) a1;
23dd2d97 6745 Lisp_Object string = a2;
23a96c77 6746
c6e89d6c
GM
6747 xassert (BEG == Z);
6748
6749 /* Change multibyteness of the echo buffer appropriately. */
6750 if (message_enable_multibyte
6751 != !NILP (current_buffer->enable_multibyte_characters))
6752 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6753
ad4f174e
GM
6754 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6755
c6e89d6c
GM
6756 /* Insert new message at BEG. */
6757 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6758
6759 if (STRINGP (string))
6760 {
6761 int nchars;
6762
6763 if (nbytes == 0)
6764 nbytes = XSTRING (string)->size_byte;
6765 nchars = string_byte_to_char (string, nbytes);
6766
6767 /* This function takes care of single/multibyte conversion. We
6768 just have to ensure that the echo area buffer has the right
6769 setting of enable_multibyte_characters. */
6770 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6771 }
6772 else if (s)
6773 {
6774 if (nbytes == 0)
6775 nbytes = strlen (s);
6776
6777 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6778 {
6779 /* Convert from multi-byte to single-byte. */
6780 int i, c, n;
6781 unsigned char work[1];
6782
6783 /* Convert a multibyte string to single-byte. */
6784 for (i = 0; i < nbytes; i += n)
6785 {
6786 c = string_char_and_length (s + i, nbytes - i, &n);
6787 work[0] = (SINGLE_BYTE_CHAR_P (c)
6788 ? c
6789 : multibyte_char_to_unibyte (c, Qnil));
6790 insert_1_both (work, 1, 1, 1, 0, 0);
6791 }
6792 }
6793 else if (!multibyte_p
6794 && !NILP (current_buffer->enable_multibyte_characters))
6795 {
6796 /* Convert from single-byte to multi-byte. */
6797 int i, c, n;
6798 unsigned char *msg = (unsigned char *) s;
260a86a0 6799 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6800
6801 /* Convert a single-byte string to multibyte. */
6802 for (i = 0; i < nbytes; i++)
6803 {
6804 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6805 n = CHAR_STRING (c, str);
6806 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6807 }
6808 }
6809 else
6810 insert_1 (s, nbytes, 1, 0, 0);
6811 }
6812
6813 return 0;
6814}
6815
6816
6817/* Clear messages. CURRENT_P non-zero means clear the current
6818 message. LAST_DISPLAYED_P non-zero means clear the message
6819 last displayed. */
6820
6821void
6822clear_message (current_p, last_displayed_p)
6823 int current_p, last_displayed_p;
6824{
6825 if (current_p)
6826 echo_area_buffer[0] = Qnil;
6827
6828 if (last_displayed_p)
6829 echo_area_buffer[1] = Qnil;
6830
6831 message_buf_print = 0;
6832}
6833
6834/* Clear garbaged frames.
6835
6836 This function is used where the old redisplay called
6837 redraw_garbaged_frames which in turn called redraw_frame which in
6838 turn called clear_frame. The call to clear_frame was a source of
6839 flickering. I believe a clear_frame is not necessary. It should
6840 suffice in the new redisplay to invalidate all current matrices,
6841 and ensure a complete redisplay of all windows. */
6842
6843static void
6844clear_garbaged_frames ()
6845{
5f5c8ee5
GM
6846 if (frame_garbaged)
6847 {
5f5c8ee5
GM
6848 Lisp_Object tail, frame;
6849
6850 FOR_EACH_FRAME (tail, frame)
6851 {
6852 struct frame *f = XFRAME (frame);
6853
6854 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6855 {
6856 clear_current_matrices (f);
6857 f->garbaged = 0;
6858 }
6859 }
6860
6861 frame_garbaged = 0;
6862 ++windows_or_buffers_changed;
6863 }
c6e89d6c 6864}
5f5c8ee5 6865
5f5c8ee5 6866
886bd6f2
GM
6867/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6868 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 6869 mini-windows height has been changed. */
5f5c8ee5 6870
c6e89d6c
GM
6871static int
6872echo_area_display (update_frame_p)
6873 int update_frame_p;
6874{
6875 Lisp_Object mini_window;
6876 struct window *w;
6877 struct frame *f;
6878 int window_height_changed_p = 0;
886bd6f2 6879 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6880
886bd6f2 6881 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6882 w = XWINDOW (mini_window);
6883 f = XFRAME (WINDOW_FRAME (w));
6884
6885 /* Don't display if frame is invisible or not yet initialized. */
6886 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6887 return 0;
5f5c8ee5 6888
1a578e9b
AC
6889/* The terminal frame is used as the first Emacs frame on the Mac OS. */
6890#ifndef macintosh
1ab3e082 6891#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
6892 /* When Emacs starts, selected_frame may be a visible terminal
6893 frame, even if we run under a window system. If we let this
6894 through, a message would be displayed on the terminal. */
622e3754
GM
6895 if (EQ (selected_frame, Vterminal_frame)
6896 && !NILP (Vwindow_system))
c6e89d6c 6897 return 0;
1ab3e082 6898#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 6899#endif
c6e89d6c
GM
6900
6901 /* Redraw garbaged frames. */
6902 if (frame_garbaged)
6903 clear_garbaged_frames ();
6904
6905 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6906 {
6907 echo_area_window = mini_window;
6908 window_height_changed_p = display_echo_area (w);
5f5c8ee5 6909 w->must_be_updated_p = 1;
c59c668a 6910
d4358b37
GM
6911 /* Update the display, unless called from redisplay_internal.
6912 Also don't update the screen during redisplay itself. The
6913 update will happen at the end of redisplay, and an update
6914 here could cause confusion. */
6915 if (update_frame_p && !redisplaying_p)
5f5c8ee5 6916 {
715e84c9 6917 int n = 0;
edc68111 6918
715e84c9
GM
6919 /* If the display update has been interrupted by pending
6920 input, update mode lines in the frame. Due to the
6921 pending input, it might have been that redisplay hasn't
6922 been called, so that mode lines above the echo area are
6923 garbaged. This looks odd, so we prevent it here. */
6924 if (!display_completed)
6925 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6926
8af1308e
GM
6927 if (window_height_changed_p
6928 /* Don't do this if Emacs is shutting down. Redisplay
6929 needs to run hooks. */
6930 && !NILP (Vrun_hooks))
c59c668a 6931 {
c06017fb
GM
6932 /* Must update other windows. Likewise as in other
6933 cases, don't let this update be interrupted by
6934 pending input. */
6935 int count = BINDING_STACK_SIZE ();
6936 specbind (Qredisplay_dont_pause, Qt);
edc68111 6937 windows_or_buffers_changed = 1;
c59c668a 6938 redisplay_internal (0);
c06017fb 6939 unbind_to (count, Qnil);
c59c668a 6940 }
715e84c9 6941 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 6942 {
edc68111 6943 /* Window configuration is the same as before.
715e84c9
GM
6944 Can do with a display update of the echo area,
6945 unless we displayed some mode lines. */
5f5c8ee5
GM
6946 update_single_window (w, 1);
6947 rif->flush_display (f);
6948 }
6949 else
6950 update_frame (f, 1, 1);
31b6671b
GM
6951
6952 /* If cursor is in the echo area, make sure that the next
6953 redisplay displays the minibuffer, so that the cursor will
6954 be replaced with what the minibuffer wants. */
6955 if (cursor_in_echo_area)
6956 ++windows_or_buffers_changed;
5f5c8ee5
GM
6957 }
6958 }
6959 else if (!EQ (mini_window, selected_window))
6960 windows_or_buffers_changed++;
c59c668a
GM
6961
6962 /* Last displayed message is now the current message. */
dd2eb166
GM
6963 echo_area_buffer[1] = echo_area_buffer[0];
6964
5f5c8ee5
GM
6965 /* Prevent redisplay optimization in redisplay_internal by resetting
6966 this_line_start_pos. This is done because the mini-buffer now
6967 displays the message instead of its buffer text. */
6968 if (EQ (mini_window, selected_window))
6969 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
6970
6971 return window_height_changed_p;
5f5c8ee5
GM
6972}
6973
6974
6975\f
6976/***********************************************************************
6977 Frame Titles
6978 ***********************************************************************/
6979
6980
6981#ifdef HAVE_WINDOW_SYSTEM
6982
6983/* A buffer for constructing frame titles in it; allocated from the
6984 heap in init_xdisp and resized as needed in store_frame_title_char. */
6985
6986static char *frame_title_buf;
6987
6988/* The buffer's end, and a current output position in it. */
6989
6990static char *frame_title_buf_end;
6991static char *frame_title_ptr;
6992
6993
6994/* Store a single character C for the frame title in frame_title_buf.
6995 Re-allocate frame_title_buf if necessary. */
6996
6997static void
6998store_frame_title_char (c)
6999 char c;
7000{
7001 /* If output position has reached the end of the allocated buffer,
7002 double the buffer's size. */
7003 if (frame_title_ptr == frame_title_buf_end)
7004 {
7005 int len = frame_title_ptr - frame_title_buf;
7006 int new_size = 2 * len * sizeof *frame_title_buf;
7007 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7008 frame_title_buf_end = frame_title_buf + new_size;
7009 frame_title_ptr = frame_title_buf + len;
7010 }
7011
7012 *frame_title_ptr++ = c;
7013}
7014
7015
7016/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7017 frame_title_ptr. STR is the string to store. Do not copy
7018 characters that yield more columns than PRECISION; PRECISION <= 0
7019 means copy the whole string. Pad with spaces until FIELD_WIDTH
7020 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7021 pad. Called from display_mode_element when it is used to build a
7022 frame title. */
5f5c8ee5
GM
7023
7024static int
7025store_frame_title (str, field_width, precision)
7026 unsigned char *str;
7027 int field_width, precision;
7028{
7029 int n = 0;
d26b89b8 7030 int dummy, nbytes, width;
5f5c8ee5
GM
7031
7032 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7033 nbytes = strlen (str);
7034 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7035 while (nbytes--)
7036 store_frame_title_char (*str++);
5f5c8ee5
GM
7037
7038 /* Fill up with spaces until FIELD_WIDTH reached. */
7039 while (field_width > 0
7040 && n < field_width)
7041 {
7042 store_frame_title_char (' ');
7043 ++n;
7044 }
7045
7046 return n;
7047}
7048
7049
7050/* Set the title of FRAME, if it has changed. The title format is
7051 Vicon_title_format if FRAME is iconified, otherwise it is
7052 frame_title_format. */
7053
7054static void
7055x_consider_frame_title (frame)
7056 Lisp_Object frame;
7057{
7058 struct frame *f = XFRAME (frame);
7059
7060 if (FRAME_WINDOW_P (f)
7061 || FRAME_MINIBUF_ONLY_P (f)
7062 || f->explicit_name)
7063 {
7064 /* Do we have more than one visible frame on this X display? */
7065 Lisp_Object tail;
7066 Lisp_Object fmt;
7067 struct buffer *obuf;
7068 int len;
7069 struct it it;
7070
9472f927 7071 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7072 {
9472f927 7073 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
7074
7075 if (tf != f
7076 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7077 && !FRAME_MINIBUF_ONLY_P (tf)
7078 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7079 break;
7080 }
7081
7082 /* Set global variable indicating that multiple frames exist. */
7083 multiple_frames = CONSP (tail);
7084
7085 /* Switch to the buffer of selected window of the frame. Set up
7086 frame_title_ptr so that display_mode_element will output into it;
7087 then display the title. */
7088 obuf = current_buffer;
7089 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7090 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7091 frame_title_ptr = frame_title_buf;
7092 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7093 NULL, DEFAULT_FACE_ID);
d26b89b8
KH
7094 display_mode_element (&it, 0, -1, -1, fmt);
7095 len = frame_title_ptr - frame_title_buf;
5f5c8ee5
GM
7096 frame_title_ptr = NULL;
7097 set_buffer_internal (obuf);
7098
7099 /* Set the title only if it's changed. This avoids consing in
7100 the common case where it hasn't. (If it turns out that we've
7101 already wasted too much time by walking through the list with
7102 display_mode_element, then we might need to optimize at a
7103 higher level than this.) */
7104 if (! STRINGP (f->name)
7105 || STRING_BYTES (XSTRING (f->name)) != len
7106 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7107 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7108 }
7109}
7110
7111#else /* not HAVE_WINDOW_SYSTEM */
7112
7113#define frame_title_ptr ((char *)0)
7114#define store_frame_title(str, mincol, maxcol) 0
7115
7116#endif /* not HAVE_WINDOW_SYSTEM */
7117
7118
7119
7120\f
7121/***********************************************************************
7122 Menu Bars
7123 ***********************************************************************/
7124
7125
7126/* Prepare for redisplay by updating menu-bar item lists when
7127 appropriate. This can call eval. */
7128
7129void
7130prepare_menu_bars ()
7131{
7132 int all_windows;
7133 struct gcpro gcpro1, gcpro2;
7134 struct frame *f;
6b5c4794 7135 Lisp_Object tooltip_frame;
5f5c8ee5
GM
7136
7137#ifdef HAVE_X_WINDOWS
7138 tooltip_frame = tip_frame;
7139#else
6b5c4794 7140 tooltip_frame = Qnil;
5f5c8ee5
GM
7141#endif
7142
7143 /* Update all frame titles based on their buffer names, etc. We do
7144 this before the menu bars so that the buffer-menu will show the
7145 up-to-date frame titles. */
7146#ifdef HAVE_WINDOW_SYSTEM
7147 if (windows_or_buffers_changed || update_mode_lines)
7148 {
7149 Lisp_Object tail, frame;
7150
7151 FOR_EACH_FRAME (tail, frame)
7152 {
7153 f = XFRAME (frame);
6b5c4794 7154 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7155 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7156 x_consider_frame_title (frame);
7157 }
7158 }
7159#endif /* HAVE_WINDOW_SYSTEM */
7160
7161 /* Update the menu bar item lists, if appropriate. This has to be
7162 done before any actual redisplay or generation of display lines. */
7163 all_windows = (update_mode_lines
7164 || buffer_shared > 1
7165 || windows_or_buffers_changed);
7166 if (all_windows)
7167 {
7168 Lisp_Object tail, frame;
2d27dae2 7169 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
7170
7171 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7172
7173 FOR_EACH_FRAME (tail, frame)
7174 {
7175 f = XFRAME (frame);
7176
7177 /* Ignore tooltip frame. */
6b5c4794 7178 if (EQ (frame, tooltip_frame))
5f5c8ee5
GM
7179 continue;
7180
7181 /* If a window on this frame changed size, report that to
7182 the user and clear the size-change flag. */
7183 if (FRAME_WINDOW_SIZES_CHANGED (f))
7184 {
7185 Lisp_Object functions;
7186
7187 /* Clear flag first in case we get an error below. */
7188 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7189 functions = Vwindow_size_change_functions;
7190 GCPRO2 (tail, functions);
7191
7192 while (CONSP (functions))
7193 {
7194 call1 (XCAR (functions), frame);
7195 functions = XCDR (functions);
7196 }
7197 UNGCPRO;
7198 }
7199
7200 GCPRO1 (tail);
7201 update_menu_bar (f, 0);
7202#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7203 update_tool_bar (f, 0);
5f5c8ee5
GM
7204#endif
7205 UNGCPRO;
7206 }
7207
7208 unbind_to (count, Qnil);
7209 }
7210 else
7211 {
886bd6f2
GM
7212 struct frame *sf = SELECTED_FRAME ();
7213 update_menu_bar (sf, 1);
5f5c8ee5 7214#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7215 update_tool_bar (sf, 1);
5f5c8ee5
GM
7216#endif
7217 }
7218
7219 /* Motif needs this. See comment in xmenu.c. Turn it off when
7220 pending_menu_activation is not defined. */
7221#ifdef USE_X_TOOLKIT
7222 pending_menu_activation = 0;
7223#endif
7224}
7225
7226
7227/* Update the menu bar item list for frame F. This has to be done
7228 before we start to fill in any display lines, because it can call
7229 eval.
7230
7231 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7232
7233static void
7234update_menu_bar (f, save_match_data)
7235 struct frame *f;
7236 int save_match_data;
7237{
7238 Lisp_Object window;
7239 register struct window *w;
7240
e1477f43
GM
7241 /* If called recursively during a menu update, do nothing. This can
7242 happen when, for instance, an activate-menubar-hook causes a
7243 redisplay. */
7244 if (inhibit_menubar_update)
7245 return;
7246
5f5c8ee5
GM
7247 window = FRAME_SELECTED_WINDOW (f);
7248 w = XWINDOW (window);
7249
7250 if (update_mode_lines)
7251 w->update_mode_line = Qt;
7252
7253 if (FRAME_WINDOW_P (f)
7254 ?
1a578e9b 7255#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5
GM
7256 FRAME_EXTERNAL_MENU_BAR (f)
7257#else
7258 FRAME_MENU_BAR_LINES (f) > 0
7259#endif
7260 : FRAME_MENU_BAR_LINES (f) > 0)
7261 {
7262 /* If the user has switched buffers or windows, we need to
7263 recompute to reflect the new bindings. But we'll
7264 recompute when update_mode_lines is set too; that means
7265 that people can use force-mode-line-update to request
7266 that the menu bar be recomputed. The adverse effect on
7267 the rest of the redisplay algorithm is about the same as
7268 windows_or_buffers_changed anyway. */
7269 if (windows_or_buffers_changed
7270 || !NILP (w->update_mode_line)
7271 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7272 < BUF_MODIFF (XBUFFER (w->buffer)))
7273 != !NILP (w->last_had_star))
7274 || ((!NILP (Vtransient_mark_mode)
7275 && !NILP (XBUFFER (w->buffer)->mark_active))
7276 != !NILP (w->region_showing)))
7277 {
7278 struct buffer *prev = current_buffer;
2d27dae2 7279 int count = BINDING_STACK_SIZE ();
5f5c8ee5 7280
e1477f43
GM
7281 specbind (Qinhibit_menubar_update, Qt);
7282
5f5c8ee5
GM
7283 set_buffer_internal_1 (XBUFFER (w->buffer));
7284 if (save_match_data)
7285 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7286 if (NILP (Voverriding_local_map_menu_flag))
7287 {
7288 specbind (Qoverriding_terminal_local_map, Qnil);
7289 specbind (Qoverriding_local_map, Qnil);
7290 }
7291
7292 /* Run the Lucid hook. */
65048e97 7293 safe_run_hooks (Qactivate_menubar_hook);
5f5c8ee5
GM
7294
7295 /* If it has changed current-menubar from previous value,
7296 really recompute the menu-bar from the value. */
7297 if (! NILP (Vlucid_menu_bar_dirty_flag))
7298 call0 (Qrecompute_lucid_menubar);
7299
7300 safe_run_hooks (Qmenu_bar_update_hook);
7301 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7302
7303 /* Redisplay the menu bar in case we changed it. */
1a578e9b
AC
7304#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7305 if (FRAME_WINDOW_P (f)
7306#if defined (macintosh)
7307 /* All frames on Mac OS share the same menubar. So only the
7308 selected frame should be allowed to set it. */
7309 && f == SELECTED_FRAME ()
7310#endif
7311 )
5f5c8ee5
GM
7312 set_frame_menubar (f, 0, 0);
7313 else
7314 /* On a terminal screen, the menu bar is an ordinary screen
7315 line, and this makes it get updated. */
7316 w->update_mode_line = Qt;
7317#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7318 /* In the non-toolkit version, the menu bar is an ordinary screen
7319 line, and this makes it get updated. */
7320 w->update_mode_line = Qt;
7321#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7322
7323 unbind_to (count, Qnil);
7324 set_buffer_internal_1 (prev);
7325 }
7326 }
7327}
7328
7329
7330\f
7331/***********************************************************************
e037b9ec 7332 Tool-bars
5f5c8ee5
GM
7333 ***********************************************************************/
7334
7335#ifdef HAVE_WINDOW_SYSTEM
7336
e037b9ec 7337/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7338 before we start to fill in any display lines. Called from
7339 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7340 and restore it here. */
7341
7342static void
e037b9ec 7343update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7344 struct frame *f;
7345 int save_match_data;
7346{
e037b9ec
GM
7347 if (WINDOWP (f->tool_bar_window)
7348 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
7349 {
7350 Lisp_Object window;
7351 struct window *w;
7352
7353 window = FRAME_SELECTED_WINDOW (f);
7354 w = XWINDOW (window);
7355
7356 /* If the user has switched buffers or windows, we need to
7357 recompute to reflect the new bindings. But we'll
7358 recompute when update_mode_lines is set too; that means
7359 that people can use force-mode-line-update to request
7360 that the menu bar be recomputed. The adverse effect on
7361 the rest of the redisplay algorithm is about the same as
7362 windows_or_buffers_changed anyway. */
7363 if (windows_or_buffers_changed
7364 || !NILP (w->update_mode_line)
7365 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7366 < BUF_MODIFF (XBUFFER (w->buffer)))
7367 != !NILP (w->last_had_star))
7368 || ((!NILP (Vtransient_mark_mode)
7369 && !NILP (XBUFFER (w->buffer)->mark_active))
7370 != !NILP (w->region_showing)))
7371 {
7372 struct buffer *prev = current_buffer;
2d27dae2 7373 int count = BINDING_STACK_SIZE ();
a2889657 7374
5f5c8ee5
GM
7375 /* Set current_buffer to the buffer of the selected
7376 window of the frame, so that we get the right local
7377 keymaps. */
7378 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7379
5f5c8ee5
GM
7380 /* Save match data, if we must. */
7381 if (save_match_data)
7382 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7383
7384 /* Make sure that we don't accidentally use bogus keymaps. */
7385 if (NILP (Voverriding_local_map_menu_flag))
7386 {
7387 specbind (Qoverriding_terminal_local_map, Qnil);
7388 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7389 }
1f40cad2 7390
e037b9ec 7391 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7392 f->tool_bar_items
7393 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
5f5c8ee5 7394
e037b9ec 7395 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7396 w->update_mode_line = Qt;
7397
7398 unbind_to (count, Qnil);
7399 set_buffer_internal_1 (prev);
81d478f3 7400 }
a2889657
JB
7401 }
7402}
7403
6c4429a5 7404
e037b9ec 7405/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7406 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7407 been set up previously by calling prepare_menu_bars. */
7408
a2889657 7409static void
e037b9ec 7410build_desired_tool_bar_string (f)
5f5c8ee5 7411 struct frame *f;
a2889657 7412{
a23887b9 7413 int i, size, size_needed;
5f5c8ee5
GM
7414 struct gcpro gcpro1, gcpro2, gcpro3;
7415 Lisp_Object image, plist, props;
a2889657 7416
5f5c8ee5
GM
7417 image = plist = props = Qnil;
7418 GCPRO3 (image, plist, props);
a2889657 7419
e037b9ec 7420 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
7421 Otherwise, make a new string. */
7422
7423 /* The size of the string we might be able to reuse. */
e037b9ec
GM
7424 size = (STRINGP (f->desired_tool_bar_string)
7425 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
7426 : 0);
7427
b94c0d9c 7428 /* We need one space in the string for each image. */
a23887b9 7429 size_needed = f->n_tool_bar_items;
b94c0d9c
GM
7430
7431 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7432 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7433 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7434 make_number (' '));
5f5c8ee5
GM
7435 else
7436 {
7437 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7438 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7439 props, f->desired_tool_bar_string);
5f5c8ee5 7440 }
a2889657 7441
5f5c8ee5 7442 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7443 put a `menu_item' property on tool-bar items with a value that
7444 is the index of the item in F's tool-bar item vector. */
a23887b9 7445 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7446 {
7464726e 7447#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7448
e037b9ec
GM
7449 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7450 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7451 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7452 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7453 extern Lisp_Object Qlaplace;
7454
7455 /* If image is a vector, choose the image according to the
7456 button state. */
e037b9ec 7457 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7458 if (VECTORP (image))
7459 {
5f5c8ee5
GM
7460 if (enabled_p)
7461 idx = (selected_p
e037b9ec
GM
7462 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7463 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7464 else
7465 idx = (selected_p
e037b9ec
GM
7466 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7467 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5 7468
37e4e482
GM
7469 xassert (ASIZE (image) >= idx);
7470 image = AREF (image, idx);
5f5c8ee5 7471 }
37e4e482
GM
7472 else
7473 idx = -1;
5f5c8ee5
GM
7474
7475 /* Ignore invalid image specifications. */
7476 if (!valid_image_p (image))
7477 continue;
7478
e037b9ec 7479 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7480 plist = Fcopy_sequence (XCDR (image));
7481
7482 /* Compute margin and relief to draw. */
c3d76173
GM
7483 relief = (tool_bar_button_relief > 0
7484 ? tool_bar_button_relief
7485 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7486 hmargin = vmargin = relief;
7487
7488 if (INTEGERP (Vtool_bar_button_margin)
7489 && XINT (Vtool_bar_button_margin) > 0)
7490 {
7491 hmargin += XFASTINT (Vtool_bar_button_margin);
7492 vmargin += XFASTINT (Vtool_bar_button_margin);
7493 }
7494 else if (CONSP (Vtool_bar_button_margin))
7495 {
7496 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7497 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7498 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7499
7500 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7501 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7502 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7503 }
5f5c8ee5 7504
e037b9ec 7505 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7506 {
7507 /* Add a `:relief' property to the image spec if the item is
7508 selected. */
7509 if (selected_p)
7510 {
7511 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7512 hmargin -= relief;
7513 vmargin -= relief;
5f5c8ee5
GM
7514 }
7515 }
7516 else
7517 {
7518 /* If image is selected, display it pressed, i.e. with a
7519 negative relief. If it's not selected, display it with a
7520 raised relief. */
7521 plist = Fplist_put (plist, QCrelief,
7522 (selected_p
7523 ? make_number (-relief)
7524 : make_number (relief)));
35a41507
GM
7525 hmargin -= relief;
7526 vmargin -= relief;
5f5c8ee5
GM
7527 }
7528
7529 /* Put a margin around the image. */
35a41507
GM
7530 if (hmargin || vmargin)
7531 {
7532 if (hmargin == vmargin)
7533 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7534 else
7535 plist = Fplist_put (plist, QCmargin,
7536 Fcons (make_number (hmargin),
7537 make_number (vmargin)));
7538 }
5f5c8ee5 7539
37e4e482
GM
7540 /* If button is not enabled, and we don't have special images
7541 for the disabled state, make the image appear disabled by
5f5c8ee5 7542 applying an appropriate algorithm to it. */
37e4e482 7543 if (!enabled_p && idx < 0)
ecd01a0e 7544 plist = Fplist_put (plist, QCconversion, Qdisabled);
5f5c8ee5
GM
7545
7546 /* Put a `display' text property on the string for the image to
7547 display. Put a `menu-item' property on the string that gives
e037b9ec 7548 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7549 vector. */
7550 image = Fcons (Qimage, plist);
7551 props = list4 (Qdisplay, image,
a23887b9
GM
7552 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7553
7554 /* Let the last image hide all remaining spaces in the tool bar
7555 string. The string can be longer than needed when we reuse a
7556 previous string. */
7557 if (i + 1 == f->n_tool_bar_items)
7558 end = XSTRING (f->desired_tool_bar_string)->size;
7559 else
7560 end = i + 1;
7561 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7562 props, f->desired_tool_bar_string);
5f5c8ee5 7563#undef PROP
a2889657
JB
7564 }
7565
5f5c8ee5
GM
7566 UNGCPRO;
7567}
7568
7569
e037b9ec 7570/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7571
7572static void
e037b9ec 7573display_tool_bar_line (it)
5f5c8ee5
GM
7574 struct it *it;
7575{
7576 struct glyph_row *row = it->glyph_row;
7577 int max_x = it->last_visible_x;
7578 struct glyph *last;
7579
7580 prepare_desired_row (row);
7581 row->y = it->current_y;
90aa2856
GM
7582
7583 /* Note that this isn't made use of if the face hasn't a box,
7584 so there's no need to check the face here. */
7585 it->start_of_box_run_p = 1;
5f5c8ee5
GM
7586
7587 while (it->current_x < max_x)
a2889657 7588 {
5f5c8ee5 7589 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7590
5f5c8ee5
GM
7591 /* Get the next display element. */
7592 if (!get_next_display_element (it))
7593 break;
73af359d 7594
5f5c8ee5
GM
7595 /* Produce glyphs. */
7596 x_before = it->current_x;
7597 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7598 PRODUCE_GLYPHS (it);
daa37602 7599
5f5c8ee5
GM
7600 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7601 i = 0;
7602 x = x_before;
7603 while (i < nglyphs)
7604 {
7605 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7606
7607 if (x + glyph->pixel_width > max_x)
7608 {
7609 /* Glyph doesn't fit on line. */
7610 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7611 it->current_x = x;
7612 goto out;
7613 }
daa37602 7614
5f5c8ee5
GM
7615 ++it->hpos;
7616 x += glyph->pixel_width;
7617 ++i;
7618 }
7619
7620 /* Stop at line ends. */
7621 if (ITERATOR_AT_END_OF_LINE_P (it))
7622 break;
7623
cafafe0b 7624 set_iterator_to_next (it, 1);
a2889657 7625 }
a2889657 7626
5f5c8ee5 7627 out:;
a2889657 7628
5f5c8ee5
GM
7629 row->displays_text_p = row->used[TEXT_AREA] != 0;
7630 extend_face_to_end_of_line (it);
7631 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7632 last->right_box_line_p = 1;
90aa2856
GM
7633 if (last == row->glyphs[TEXT_AREA])
7634 last->left_box_line_p = 1;
5f5c8ee5
GM
7635 compute_line_metrics (it);
7636
e037b9ec 7637 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7638 if (!row->displays_text_p)
7639 {
312246d1
GM
7640 row->height = row->phys_height = it->last_visible_y - row->y;
7641 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
7642 }
7643
7644 row->full_width_p = 1;
7645 row->continued_p = 0;
7646 row->truncated_on_left_p = 0;
7647 row->truncated_on_right_p = 0;
7648
7649 it->current_x = it->hpos = 0;
7650 it->current_y += row->height;
7651 ++it->vpos;
7652 ++it->glyph_row;
a2889657 7653}
96a410bc 7654
5f5c8ee5 7655
e037b9ec 7656/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7657 items of frame F visible. */
96a410bc 7658
d39b6696 7659static int
e037b9ec 7660tool_bar_lines_needed (f)
5f5c8ee5 7661 struct frame *f;
d39b6696 7662{
e037b9ec 7663 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
7664 struct it it;
7665
e037b9ec
GM
7666 /* Initialize an iterator for iteration over
7667 F->desired_tool_bar_string in the tool-bar window of frame F. */
7668 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7669 it.first_visible_x = 0;
7670 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7671 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7672
7673 while (!ITERATOR_AT_END_P (&it))
7674 {
7675 it.glyph_row = w->desired_matrix->rows;
7676 clear_glyph_row (it.glyph_row);
e037b9ec 7677 display_tool_bar_line (&it);
5f5c8ee5
GM
7678 }
7679
7680 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7681}
96a410bc 7682
5f5c8ee5 7683
57c28064
GM
7684DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7685 0, 1, 0,
7686 "Return the number of lines occupied by the tool bar of FRAME.")
7687 (frame)
7688 Lisp_Object frame;
7689{
7690 struct frame *f;
7691 struct window *w;
7692 int nlines = 0;
7693
7694 if (NILP (frame))
7695 frame = selected_frame;
7696 else
7697 CHECK_FRAME (frame, 0);
7698 f = XFRAME (frame);
7699
7700 if (WINDOWP (f->tool_bar_window)
7701 || (w = XWINDOW (f->tool_bar_window),
7702 XFASTINT (w->height) > 0))
7703 {
7704 update_tool_bar (f, 1);
7705 if (f->n_tool_bar_items)
7706 {
7707 build_desired_tool_bar_string (f);
7708 nlines = tool_bar_lines_needed (f);
7709 }
7710 }
7711
7712 return make_number (nlines);
7713}
7714
7715
e037b9ec 7716/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7717 height should be changed. */
7718
7719static int
e037b9ec 7720redisplay_tool_bar (f)
5f5c8ee5 7721 struct frame *f;
96a410bc 7722{
5f5c8ee5
GM
7723 struct window *w;
7724 struct it it;
7725 struct glyph_row *row;
7726 int change_height_p = 0;
7727
e037b9ec
GM
7728 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7729 do anything. This means you must start with tool-bar-lines
5f5c8ee5 7730 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
7731 can turn off tool-bars by specifying tool-bar-lines zero. */
7732 if (!WINDOWP (f->tool_bar_window)
7733 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
7734 XFASTINT (w->height) == 0))
7735 return 0;
96a410bc 7736
e037b9ec
GM
7737 /* Set up an iterator for the tool-bar window. */
7738 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7739 it.first_visible_x = 0;
7740 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7741 row = it.glyph_row;
3450d04c 7742
e037b9ec
GM
7743 /* Build a string that represents the contents of the tool-bar. */
7744 build_desired_tool_bar_string (f);
7745 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 7746
e037b9ec 7747 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 7748 while (it.current_y < it.last_visible_y)
e037b9ec 7749 display_tool_bar_line (&it);
3450d04c 7750
e037b9ec 7751 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
7752 window, so don't do it. */
7753 w->desired_matrix->no_scrolling_p = 1;
7754 w->must_be_updated_p = 1;
3450d04c 7755
e037b9ec 7756 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
7757 {
7758 int nlines;
6e33e6f0
GM
7759
7760 /* If we couldn't display everything, change the tool-bar's
7761 height. */
7762 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7763 change_height_p = 1;
5f5c8ee5
GM
7764
7765 /* If there are blank lines at the end, except for a partially
7766 visible blank line at the end that is smaller than
e037b9ec 7767 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
7768 row = it.glyph_row - 1;
7769 if (!row->displays_text_p
7770 && row->height >= CANON_Y_UNIT (f))
7771 change_height_p = 1;
7772
e037b9ec
GM
7773 /* If row displays tool-bar items, but is partially visible,
7774 change the tool-bar's height. */
5f5c8ee5
GM
7775 if (row->displays_text_p
7776 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7777 change_height_p = 1;
7778
e037b9ec 7779 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
7780 frame parameter. */
7781 if (change_height_p
e037b9ec 7782 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
7783 nlines != XFASTINT (w->height)))
7784 {
e037b9ec 7785 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 7786 Lisp_Object frame;
e85ee976 7787 int old_height = XFASTINT (w->height);
5f5c8ee5
GM
7788
7789 XSETFRAME (frame, f);
7790 clear_glyph_matrix (w->desired_matrix);
7791 Fmodify_frame_parameters (frame,
e037b9ec 7792 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
7793 make_number (nlines)),
7794 Qnil));
e85ee976
GM
7795 if (XFASTINT (w->height) != old_height)
7796 fonts_changed_p = 1;
5f5c8ee5
GM
7797 }
7798 }
3450d04c 7799
5f5c8ee5 7800 return change_height_p;
96a410bc 7801}
90adcf20 7802
5f5c8ee5 7803
e037b9ec
GM
7804/* Get information about the tool-bar item which is displayed in GLYPH
7805 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 7806 properties start in F->tool_bar_items. Value is zero if
e037b9ec 7807 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
7808
7809int
e037b9ec 7810tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
7811 struct frame *f;
7812 struct glyph *glyph;
7813 int *prop_idx;
90adcf20 7814{
5f5c8ee5
GM
7815 Lisp_Object prop;
7816 int success_p;
7817
7818 /* Get the text property `menu-item' at pos. The value of that
7819 property is the start index of this item's properties in
7464726e 7820 F->tool_bar_items. */
5f5c8ee5 7821 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 7822 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
7823 if (INTEGERP (prop))
7824 {
7825 *prop_idx = XINT (prop);
7826 success_p = 1;
7827 }
7828 else
7829 success_p = 0;
90adcf20 7830
5f5c8ee5
GM
7831 return success_p;
7832}
7833
7834#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 7835
feb0c42f 7836
5f5c8ee5
GM
7837\f
7838/************************************************************************
7839 Horizontal scrolling
7840 ************************************************************************/
feb0c42f 7841
5f5c8ee5
GM
7842static int hscroll_window_tree P_ ((Lisp_Object));
7843static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 7844
5f5c8ee5
GM
7845/* For all leaf windows in the window tree rooted at WINDOW, set their
7846 hscroll value so that PT is (i) visible in the window, and (ii) so
7847 that it is not within a certain margin at the window's left and
7848 right border. Value is non-zero if any window's hscroll has been
7849 changed. */
7850
7851static int
7852hscroll_window_tree (window)
7853 Lisp_Object window;
7854{
7855 int hscrolled_p = 0;
7856
7857 while (WINDOWP (window))
90adcf20 7858 {
5f5c8ee5
GM
7859 struct window *w = XWINDOW (window);
7860
7861 if (WINDOWP (w->hchild))
7862 hscrolled_p |= hscroll_window_tree (w->hchild);
7863 else if (WINDOWP (w->vchild))
7864 hscrolled_p |= hscroll_window_tree (w->vchild);
7865 else if (w->cursor.vpos >= 0)
7866 {
7867 int hscroll_margin, text_area_x, text_area_y;
7868 int text_area_width, text_area_height;
92a90e89
GM
7869 struct glyph_row *current_cursor_row
7870 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7871 struct glyph_row *desired_cursor_row
7872 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7873 struct glyph_row *cursor_row
7874 = (desired_cursor_row->enabled_p
7875 ? desired_cursor_row
7876 : current_cursor_row);
a2725ab2 7877
5f5c8ee5
GM
7878 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7879 &text_area_width, &text_area_height);
90adcf20 7880
5f5c8ee5
GM
7881 /* Scroll when cursor is inside this scroll margin. */
7882 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7883
7884 if ((XFASTINT (w->hscroll)
7885 && w->cursor.x < hscroll_margin)
92a90e89
GM
7886 || (cursor_row->enabled_p
7887 && cursor_row->truncated_on_right_p
5f5c8ee5 7888 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 7889 {
5f5c8ee5
GM
7890 struct it it;
7891 int hscroll;
7892 struct buffer *saved_current_buffer;
7893 int pt;
7894
7895 /* Find point in a display of infinite width. */
7896 saved_current_buffer = current_buffer;
7897 current_buffer = XBUFFER (w->buffer);
7898
7899 if (w == XWINDOW (selected_window))
7900 pt = BUF_PT (current_buffer);
7901 else
08b610e4 7902 {
5f5c8ee5
GM
7903 pt = marker_position (w->pointm);
7904 pt = max (BEGV, pt);
7905 pt = min (ZV, pt);
7906 }
7907
7908 /* Move iterator to pt starting at cursor_row->start in
7909 a line with infinite width. */
7910 init_to_row_start (&it, w, cursor_row);
7911 it.last_visible_x = INFINITY;
7912 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7913 current_buffer = saved_current_buffer;
7914
7915 /* Center cursor in window. */
7916 hscroll = (max (0, it.current_x - text_area_width / 2)
7917 / CANON_X_UNIT (it.f));
3172f70b 7918 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
7919
7920 /* Don't call Fset_window_hscroll if value hasn't
7921 changed because it will prevent redisplay
7922 optimizations. */
7923 if (XFASTINT (w->hscroll) != hscroll)
7924 {
3172f70b
GM
7925 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7926 w->hscroll = make_number (hscroll);
5f5c8ee5 7927 hscrolled_p = 1;
08b610e4 7928 }
08b610e4 7929 }
08b610e4 7930 }
a2725ab2 7931
5f5c8ee5 7932 window = w->next;
90adcf20 7933 }
cd6dfed6 7934
5f5c8ee5
GM
7935 /* Value is non-zero if hscroll of any leaf window has been changed. */
7936 return hscrolled_p;
7937}
7938
7939
7940/* Set hscroll so that cursor is visible and not inside horizontal
7941 scroll margins for all windows in the tree rooted at WINDOW. See
7942 also hscroll_window_tree above. Value is non-zero if any window's
7943 hscroll has been changed. If it has, desired matrices on the frame
7944 of WINDOW are cleared. */
7945
7946static int
7947hscroll_windows (window)
7948 Lisp_Object window;
7949{
d475bcb8
GM
7950 int hscrolled_p;
7951
7952 if (automatic_hscrolling_p)
7953 {
7954 hscrolled_p = hscroll_window_tree (window);
7955 if (hscrolled_p)
7956 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7957 }
7958 else
7959 hscrolled_p = 0;
5f5c8ee5 7960 return hscrolled_p;
90adcf20 7961}
5f5c8ee5
GM
7962
7963
90adcf20 7964\f
5f5c8ee5
GM
7965/************************************************************************
7966 Redisplay
7967 ************************************************************************/
7968
7969/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7970 to a non-zero value. This is sometimes handy to have in a debugger
7971 session. */
7972
7973#if GLYPH_DEBUG
a2889657 7974
5f5c8ee5
GM
7975/* First and last unchanged row for try_window_id. */
7976
7977int debug_first_unchanged_at_end_vpos;
7978int debug_last_unchanged_at_beg_vpos;
7979
7980/* Delta vpos and y. */
7981
7982int debug_dvpos, debug_dy;
7983
7984/* Delta in characters and bytes for try_window_id. */
7985
7986int debug_delta, debug_delta_bytes;
7987
7988/* Values of window_end_pos and window_end_vpos at the end of
7989 try_window_id. */
7990
7991int debug_end_pos, debug_end_vpos;
7992
7993/* Append a string to W->desired_matrix->method. FMT is a printf
7994 format string. A1...A9 are a supplement for a variable-length
7995 argument list. If trace_redisplay_p is non-zero also printf the
7996 resulting string to stderr. */
7997
7998static void
7999debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8000 struct window *w;
8001 char *fmt;
8002 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8003{
8004 char buffer[512];
8005 char *method = w->desired_matrix->method;
8006 int len = strlen (method);
8007 int size = sizeof w->desired_matrix->method;
8008 int remaining = size - len - 1;
8009
8010 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8011 if (len && remaining)
8012 {
8013 method[len] = '|';
8014 --remaining, ++len;
8015 }
8016
8017 strncpy (method + len, buffer, remaining);
8018
8019 if (trace_redisplay_p)
8020 fprintf (stderr, "%p (%s): %s\n",
8021 w,
8022 ((BUFFERP (w->buffer)
8023 && STRINGP (XBUFFER (w->buffer)->name))
8024 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8025 : "no buffer"),
8026 buffer);
8027}
a2889657 8028
5f5c8ee5 8029#endif /* GLYPH_DEBUG */
90adcf20 8030
a2889657 8031
5f5c8ee5
GM
8032/* This counter is used to clear the face cache every once in a while
8033 in redisplay_internal. It is incremented for each redisplay.
8034 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8035 cleared. */
0d231165 8036
5f5c8ee5 8037#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
8038static int clear_face_cache_count;
8039
20de20dc 8040/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
8041
8042static struct frame *previous_terminal_frame;
8043
8044/* Non-zero while redisplay_internal is in progress. */
8045
8046int redisplaying_p;
8047
8048
8049/* Value is non-zero if all changes in window W, which displays
8050 current_buffer, are in the text between START and END. START is a
8051 buffer position, END is given as a distance from Z. Used in
8052 redisplay_internal for display optimization. */
8053
8054static INLINE int
8055text_outside_line_unchanged_p (w, start, end)
8056 struct window *w;
8057 int start, end;
8058{
8059 int unchanged_p = 1;
8060
8061 /* If text or overlays have changed, see where. */
8062 if (XFASTINT (w->last_modified) < MODIFF
8063 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8064 {
8065 /* Gap in the line? */
8066 if (GPT < start || Z - GPT < end)
8067 unchanged_p = 0;
8068
8069 /* Changes start in front of the line, or end after it? */
8070 if (unchanged_p
9142dd5b
GM
8071 && (BEG_UNCHANGED < start - 1
8072 || END_UNCHANGED < end))
5f5c8ee5
GM
8073 unchanged_p = 0;
8074
8075 /* If selective display, can't optimize if changes start at the
8076 beginning of the line. */
8077 if (unchanged_p
8078 && INTEGERP (current_buffer->selective_display)
8079 && XINT (current_buffer->selective_display) > 0
9142dd5b 8080 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
8081 unchanged_p = 0;
8082 }
8083
8084 return unchanged_p;
8085}
8086
8087
8088/* Do a frame update, taking possible shortcuts into account. This is
8089 the main external entry point for redisplay.
8090
8091 If the last redisplay displayed an echo area message and that message
8092 is no longer requested, we clear the echo area or bring back the
8093 mini-buffer if that is in use. */
20de20dc 8094
a2889657
JB
8095void
8096redisplay ()
e9874cee
RS
8097{
8098 redisplay_internal (0);
8099}
8100
260a86a0
KH
8101/* Return 1 if point moved out of or into a composition. Otherwise
8102 return 0. PREV_BUF and PREV_PT are the last point buffer and
8103 position. BUF and PT are the current point buffer and position. */
8104
8105int
8106check_point_in_composition (prev_buf, prev_pt, buf, pt)
8107 struct buffer *prev_buf, *buf;
8108 int prev_pt, pt;
8109{
8110 int start, end;
8111 Lisp_Object prop;
8112 Lisp_Object buffer;
8113
8114 XSETBUFFER (buffer, buf);
8115 /* Check a composition at the last point if point moved within the
8116 same buffer. */
8117 if (prev_buf == buf)
8118 {
8119 if (prev_pt == pt)
8120 /* Point didn't move. */
8121 return 0;
8122
8123 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8124 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8125 && COMPOSITION_VALID_P (start, end, prop)
8126 && start < prev_pt && end > prev_pt)
8127 /* The last point was within the composition. Return 1 iff
8128 point moved out of the composition. */
8129 return (pt <= start || pt >= end);
8130 }
8131
8132 /* Check a composition at the current point. */
8133 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8134 && find_composition (pt, -1, &start, &end, &prop, buffer)
8135 && COMPOSITION_VALID_P (start, end, prop)
8136 && start < pt && end > pt);
8137}
5f5c8ee5 8138
9142dd5b
GM
8139/* Reconsider the setting of B->clip_changed which is displayed
8140 in window W. */
8141
8142static INLINE void
8143reconsider_clip_changes (w, b)
8144 struct window *w;
8145 struct buffer *b;
8146{
8147 if (b->prevent_redisplay_optimizations_p)
8148 b->clip_changed = 1;
8149 else if (b->clip_changed
8150 && !NILP (w->window_end_valid)
8151 && w->current_matrix->buffer == b
8152 && w->current_matrix->zv == BUF_ZV (b)
8153 && w->current_matrix->begv == BUF_BEGV (b))
8154 b->clip_changed = 0;
260a86a0
KH
8155
8156 /* If display wasn't paused, and W is not a tool bar window, see if
8157 point has been moved into or out of a composition. In that case,
8158 we set b->clip_changed to 1 to force updating the screen. If
8159 b->clip_changed has already been set to 1, we can skip this
8160 check. */
8161 if (!b->clip_changed
8162 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8163 {
8164 int pt;
8165
8166 if (w == XWINDOW (selected_window))
8167 pt = BUF_PT (current_buffer);
8168 else
8169 pt = marker_position (w->pointm);
8170
8171 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 8172 || pt != XINT (w->last_point))
260a86a0 8173 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 8174 XINT (w->last_point),
260a86a0
KH
8175 XBUFFER (w->buffer), pt))
8176 b->clip_changed = 1;
8177 }
9142dd5b
GM
8178}
8179
8180
5f5c8ee5
GM
8181/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8182 response to any user action; therefore, we should preserve the echo
8183 area. (Actually, our caller does that job.) Perhaps in the future
8184 avoid recentering windows if it is not necessary; currently that
8185 causes some problems. */
e9874cee
RS
8186
8187static void
8188redisplay_internal (preserve_echo_area)
8189 int preserve_echo_area;
a2889657 8190{
5f5c8ee5
GM
8191 struct window *w = XWINDOW (selected_window);
8192 struct frame *f = XFRAME (w->frame);
8193 int pause;
a2889657 8194 int must_finish = 0;
5f5c8ee5 8195 struct text_pos tlbufpos, tlendpos;
89819bdd 8196 int number_of_visible_frames;
28514cd9 8197 int count;
886bd6f2 8198 struct frame *sf = SELECTED_FRAME ();
a2889657 8199
5f5c8ee5
GM
8200 /* Non-zero means redisplay has to consider all windows on all
8201 frames. Zero means, only selected_window is considered. */
8202 int consider_all_windows_p;
8203
8204 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8205
8206 /* No redisplay if running in batch mode or frame is not yet fully
8207 initialized, or redisplay is explicitly turned off by setting
8208 Vinhibit_redisplay. */
8209 if (noninteractive
8210 || !NILP (Vinhibit_redisplay)
8211 || !f->glyphs_initialized_p)
a2889657
JB
8212 return;
8213
5f5c8ee5
GM
8214 /* The flag redisplay_performed_directly_p is set by
8215 direct_output_for_insert when it already did the whole screen
8216 update necessary. */
8217 if (redisplay_performed_directly_p)
8218 {
8219 redisplay_performed_directly_p = 0;
8220 if (!hscroll_windows (selected_window))
8221 return;
8222 }
8223
15f0cf78
RS
8224#ifdef USE_X_TOOLKIT
8225 if (popup_activated ())
8226 return;
8227#endif
8228
28514cd9 8229 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 8230 if (redisplaying_p)
735c094c
KH
8231 return;
8232
28514cd9
GM
8233 /* Record a function that resets redisplaying_p to its old value
8234 when we leave this function. */
2d27dae2 8235 count = BINDING_STACK_SIZE ();
28514cd9
GM
8236 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8237 ++redisplaying_p;
8238
8b32d885 8239 retry:
bd9d0f3f 8240 pause = 0;
9142dd5b
GM
8241 reconsider_clip_changes (w, current_buffer);
8242
5f5c8ee5
GM
8243 /* If new fonts have been loaded that make a glyph matrix adjustment
8244 necessary, do it. */
8245 if (fonts_changed_p)
8246 {
8247 adjust_glyphs (NULL);
8248 ++windows_or_buffers_changed;
8249 fonts_changed_p = 0;
8250 }
8251
6961e0c1
GM
8252 /* If face_change_count is non-zero, init_iterator will free all
8253 realized faces, which includes the faces referenced from current
8254 matrices. So, we can't reuse current matrices in this case. */
8255 if (face_change_count)
8256 ++windows_or_buffers_changed;
8257
886bd6f2
GM
8258 if (! FRAME_WINDOW_P (sf)
8259 && previous_terminal_frame != sf)
20de20dc 8260 {
5f5c8ee5
GM
8261 /* Since frames on an ASCII terminal share the same display
8262 area, displaying a different frame means redisplay the whole
8263 thing. */
20de20dc 8264 windows_or_buffers_changed++;
886bd6f2
GM
8265 SET_FRAME_GARBAGED (sf);
8266 XSETFRAME (Vterminal_frame, sf);
20de20dc 8267 }
886bd6f2 8268 previous_terminal_frame = sf;
20de20dc 8269
5f5c8ee5
GM
8270 /* Set the visible flags for all frames. Do this before checking
8271 for resized or garbaged frames; they want to know if their frames
8272 are visible. See the comment in frame.h for
8273 FRAME_SAMPLE_VISIBILITY. */
d724d989 8274 {
35f56f96 8275 Lisp_Object tail, frame;
d724d989 8276
89819bdd
RS
8277 number_of_visible_frames = 0;
8278
35f56f96 8279 FOR_EACH_FRAME (tail, frame)
f82aff7c 8280 {
5f5c8ee5
GM
8281 struct frame *f = XFRAME (frame);
8282
8283 FRAME_SAMPLE_VISIBILITY (f);
8284 if (FRAME_VISIBLE_P (f))
8285 ++number_of_visible_frames;
8286 clear_desired_matrices (f);
f82aff7c 8287 }
d724d989
JB
8288 }
8289
44fa5b1e 8290 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8291 do_pending_window_change (1);
a2889657 8292
5f5c8ee5 8293 /* Clear frames marked as garbaged. */
44fa5b1e 8294 if (frame_garbaged)
c6e89d6c 8295 clear_garbaged_frames ();
a2889657 8296
e037b9ec 8297 /* Build menubar and tool-bar items. */
f82aff7c
RS
8298 prepare_menu_bars ();
8299
28995e67 8300 if (windows_or_buffers_changed)
a2889657
JB
8301 update_mode_lines++;
8302
538f13d4
RS
8303 /* Detect case that we need to write or remove a star in the mode line. */
8304 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8305 {
8306 w->update_mode_line = Qt;
8307 if (buffer_shared > 1)
8308 update_mode_lines++;
8309 }
8310
5f5c8ee5 8311 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8312 if (!NILP (w->column_number_displayed)
8313 /* This alternative quickly identifies a common case
8314 where no change is needed. */
8315 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8316 && XFASTINT (w->last_modified) >= MODIFF
8317 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8318 && XFASTINT (w->column_number_displayed) != current_column ())
8319 w->update_mode_line = Qt;
8320
44fa5b1e 8321 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8322
5f5c8ee5
GM
8323 /* The variable buffer_shared is set in redisplay_window and
8324 indicates that we redisplay a buffer in different windows. See
8325 there. */
8326 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
8327
8328 /* If specs for an arrow have changed, do thorough redisplay
8329 to ensure we remove any arrow that should no longer exist. */
d45de95b 8330 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8331 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8332 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8333
90adcf20
RS
8334 /* Normally the message* functions will have already displayed and
8335 updated the echo area, but the frame may have been trashed, or
8336 the update may have been preempted, so display the echo area
c6e89d6c
GM
8337 again here. Checking both message buffers captures the case that
8338 the echo area should be cleared. */
8339 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 8340 {
c6e89d6c 8341 int window_height_changed_p = echo_area_display (0);
90adcf20 8342 must_finish = 1;
dd2eb166 8343
c6e89d6c
GM
8344 if (fonts_changed_p)
8345 goto retry;
8346 else if (window_height_changed_p)
8347 {
8348 consider_all_windows_p = 1;
8349 ++update_mode_lines;
8350 ++windows_or_buffers_changed;
9142dd5b
GM
8351
8352 /* If window configuration was changed, frames may have been
8353 marked garbaged. Clear them or we will experience
8354 surprises wrt scrolling. */
8355 if (frame_garbaged)
8356 clear_garbaged_frames ();
c6e89d6c 8357 }
90adcf20 8358 }
97a36635 8359 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
8360 && (current_buffer->clip_changed
8361 || XFASTINT (w->last_modified) < MODIFF
8362 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 8363 && resize_mini_window (w, 0))
c6e89d6c
GM
8364 {
8365 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
8366 showing if its contents might have changed. */
8367 must_finish = 1;
8368 consider_all_windows_p = 1;
c6e89d6c 8369 ++windows_or_buffers_changed;
dd2eb166 8370 ++update_mode_lines;
9142dd5b
GM
8371
8372 /* If window configuration was changed, frames may have been
8373 marked garbaged. Clear them or we will experience
8374 surprises wrt scrolling. */
8375 if (frame_garbaged)
8376 clear_garbaged_frames ();
c6e89d6c
GM
8377 }
8378
90adcf20 8379
5f5c8ee5
GM
8380 /* If showing the region, and mark has changed, we must redisplay
8381 the whole window. The assignment to this_line_start_pos prevents
8382 the optimization directly below this if-statement. */
bd66d1ba
RS
8383 if (((!NILP (Vtransient_mark_mode)
8384 && !NILP (XBUFFER (w->buffer)->mark_active))
8385 != !NILP (w->region_showing))
82d04750
JB
8386 || (!NILP (w->region_showing)
8387 && !EQ (w->region_showing,
8388 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
8389 CHARPOS (this_line_start_pos) = 0;
8390
8391 /* Optimize the case that only the line containing the cursor in the
8392 selected window has changed. Variables starting with this_ are
8393 set in display_line and record information about the line
8394 containing the cursor. */
8395 tlbufpos = this_line_start_pos;
8396 tlendpos = this_line_end_pos;
8397 if (!consider_all_windows_p
8398 && CHARPOS (tlbufpos) > 0
8399 && NILP (w->update_mode_line)
73af359d 8400 && !current_buffer->clip_changed
44fa5b1e 8401 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 8402 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 8403 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
8404 && this_line_buffer == current_buffer
8405 && current_buffer == XBUFFER (w->buffer)
265a9e55 8406 && NILP (w->force_start)
5f5c8ee5
GM
8407 /* Point must be on the line that we have info recorded about. */
8408 && PT >= CHARPOS (tlbufpos)
8409 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
8410 /* All text outside that line, including its final newline,
8411 must be unchanged */
5f5c8ee5
GM
8412 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8413 CHARPOS (tlendpos)))
8414 {
8415 if (CHARPOS (tlbufpos) > BEGV
8416 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8417 && (CHARPOS (tlbufpos) == ZV
8418 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
8419 /* Former continuation line has disappeared by becoming empty */
8420 goto cancel;
8421 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 8422 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
8423 || MINI_WINDOW_P (w))
8424 {
1c9241f5
KH
8425 /* We have to handle the case of continuation around a
8426 wide-column character (See the comment in indent.c around
8427 line 885).
8428
8429 For instance, in the following case:
8430
8431 -------- Insert --------
8432 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8433 J_I_ ==> J_I_ `^^' are cursors.
8434 ^^ ^^
8435 -------- --------
8436
8437 As we have to redraw the line above, we should goto cancel. */
8438
5f5c8ee5
GM
8439 struct it it;
8440 int line_height_before = this_line_pixel_height;
8441
8442 /* Note that start_display will handle the case that the
8443 line starting at tlbufpos is a continuation lines. */
8444 start_display (&it, w, tlbufpos);
8445
8446 /* Implementation note: It this still necessary? */
8447 if (it.current_x != this_line_start_x)
1c9241f5
KH
8448 goto cancel;
8449
5f5c8ee5
GM
8450 TRACE ((stderr, "trying display optimization 1\n"));
8451 w->cursor.vpos = -1;
a2889657 8452 overlay_arrow_seen = 0;
5f5c8ee5
GM
8453 it.vpos = this_line_vpos;
8454 it.current_y = this_line_y;
8455 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8456 display_line (&it);
8457
a2889657 8458 /* If line contains point, is not continued,
5f5c8ee5
GM
8459 and ends at same distance from eob as before, we win */
8460 if (w->cursor.vpos >= 0
8461 /* Line is not continued, otherwise this_line_start_pos
8462 would have been set to 0 in display_line. */
8463 && CHARPOS (this_line_start_pos)
8464 /* Line ends as before. */
8465 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8466 /* Line has same height as before. Otherwise other lines
8467 would have to be shifted up or down. */
8468 && this_line_pixel_height == line_height_before)
a2889657 8469 {
5f5c8ee5
GM
8470 /* If this is not the window's last line, we must adjust
8471 the charstarts of the lines below. */
8472 if (it.current_y < it.last_visible_y)
8473 {
8474 struct glyph_row *row
8475 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8476 int delta, delta_bytes;
8477
8478 if (Z - CHARPOS (tlendpos) == ZV)
8479 {
8480 /* This line ends at end of (accessible part of)
8481 buffer. There is no newline to count. */
8482 delta = (Z
8483 - CHARPOS (tlendpos)
8484 - MATRIX_ROW_START_CHARPOS (row));
8485 delta_bytes = (Z_BYTE
8486 - BYTEPOS (tlendpos)
8487 - MATRIX_ROW_START_BYTEPOS (row));
8488 }
8489 else
8490 {
8491 /* This line ends in a newline. Must take
8492 account of the newline and the rest of the
8493 text that follows. */
8494 delta = (Z
8495 - CHARPOS (tlendpos)
8496 - MATRIX_ROW_START_CHARPOS (row));
8497 delta_bytes = (Z_BYTE
8498 - BYTEPOS (tlendpos)
8499 - MATRIX_ROW_START_BYTEPOS (row));
8500 }
8501
f2d86d7a
GM
8502 increment_matrix_positions (w->current_matrix,
8503 this_line_vpos + 1,
8504 w->current_matrix->nrows,
8505 delta, delta_bytes);
85bcef6c 8506 }
46db8486 8507
5f5c8ee5
GM
8508 /* If this row displays text now but previously didn't,
8509 or vice versa, w->window_end_vpos may have to be
8510 adjusted. */
8511 if ((it.glyph_row - 1)->displays_text_p)
8512 {
8513 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8514 XSETINT (w->window_end_vpos, this_line_vpos);
8515 }
8516 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8517 && this_line_vpos > 0)
8518 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8519 w->window_end_valid = Qnil;
8520
8521 /* Update hint: No need to try to scroll in update_window. */
8522 w->desired_matrix->no_scrolling_p = 1;
8523
8524#if GLYPH_DEBUG
8525 *w->desired_matrix->method = 0;
8526 debug_method_add (w, "optimization 1");
8527#endif
a2889657
JB
8528 goto update;
8529 }
8530 else
8531 goto cancel;
8532 }
5f5c8ee5
GM
8533 else if (/* Cursor position hasn't changed. */
8534 PT == XFASTINT (w->last_point)
b6f0fe04
RS
8535 /* Make sure the cursor was last displayed
8536 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
8537 && 0 <= w->cursor.vpos
8538 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
8539 {
8540 if (!must_finish)
8541 {
c6e89d6c 8542 do_pending_window_change (1);
5f5c8ee5
GM
8543
8544 /* We used to always goto end_of_redisplay here, but this
8545 isn't enough if we have a blinking cursor. */
8546 if (w->cursor_off_p == w->last_cursor_off_p)
8547 goto end_of_redisplay;
a2889657
JB
8548 }
8549 goto update;
8550 }
8b51f1e3
KH
8551 /* If highlighting the region, or if the cursor is in the echo area,
8552 then we can't just move the cursor. */
bd66d1ba
RS
8553 else if (! (!NILP (Vtransient_mark_mode)
8554 && !NILP (current_buffer->mark_active))
97a36635 8555 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 8556 || highlight_nonselected_windows)
8b51f1e3 8557 && NILP (w->region_showing)
8f897821 8558 && NILP (Vshow_trailing_whitespace)
8b51f1e3 8559 && !cursor_in_echo_area)
a2889657 8560 {
5f5c8ee5
GM
8561 struct it it;
8562 struct glyph_row *row;
8563
8564 /* Skip from tlbufpos to PT and see where it is. Note that
8565 PT may be in invisible text. If so, we will end at the
8566 next visible position. */
8567 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8568 NULL, DEFAULT_FACE_ID);
8569 it.current_x = this_line_start_x;
8570 it.current_y = this_line_y;
8571 it.vpos = this_line_vpos;
8572
8573 /* The call to move_it_to stops in front of PT, but
8574 moves over before-strings. */
8575 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8576
8577 if (it.vpos == this_line_vpos
8578 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8579 row->enabled_p))
a2889657 8580 {
5f5c8ee5
GM
8581 xassert (this_line_vpos == it.vpos);
8582 xassert (this_line_y == it.current_y);
8583 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
8584#if GLYPH_DEBUG
8585 *w->desired_matrix->method = 0;
8586 debug_method_add (w, "optimization 3");
8587#endif
a2889657
JB
8588 goto update;
8589 }
8590 else
8591 goto cancel;
8592 }
5f5c8ee5 8593
a2889657 8594 cancel:
5f5c8ee5
GM
8595 /* Text changed drastically or point moved off of line. */
8596 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
8597 }
8598
5f5c8ee5
GM
8599 CHARPOS (this_line_start_pos) = 0;
8600 consider_all_windows_p |= buffer_shared > 1;
8601 ++clear_face_cache_count;
a2889657 8602
5f5c8ee5 8603
bd9d0f3f
GM
8604 /* Build desired matrices, and update the display. If
8605 consider_all_windows_p is non-zero, do it for all windows on all
8606 frames. Otherwise do it for selected_window, only. */
463f6b91 8607
5f5c8ee5 8608 if (consider_all_windows_p)
a2889657 8609 {
35f56f96 8610 Lisp_Object tail, frame;
0528abe1
GM
8611 int i, n = 0, size = 50;
8612 struct frame **updated
8613 = (struct frame **) alloca (size * sizeof *updated);
a2889657 8614
5f5c8ee5
GM
8615 /* Clear the face cache eventually. */
8616 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 8617 {
5f5c8ee5 8618 clear_face_cache (0);
463f6b91
RS
8619 clear_face_cache_count = 0;
8620 }
31b24551 8621
5f5c8ee5
GM
8622 /* Recompute # windows showing selected buffer. This will be
8623 incremented each time such a window is displayed. */
a2889657
JB
8624 buffer_shared = 0;
8625
35f56f96 8626 FOR_EACH_FRAME (tail, frame)
30c566e4 8627 {
5f5c8ee5 8628 struct frame *f = XFRAME (frame);
bd9d0f3f 8629
886bd6f2 8630 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 8631 {
5f5c8ee5
GM
8632 /* Mark all the scroll bars to be removed; we'll redeem
8633 the ones we want when we redisplay their windows. */
9769686d 8634 if (condemn_scroll_bars_hook)
504454e8 8635 condemn_scroll_bars_hook (f);
30c566e4 8636
f21ef775 8637 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 8638 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 8639
5f5c8ee5
GM
8640 /* Any scroll bars which redisplay_windows should have
8641 nuked should now go away. */
9769686d 8642 if (judge_scroll_bars_hook)
504454e8 8643 judge_scroll_bars_hook (f);
bd9d0f3f
GM
8644
8645 /* If fonts changed, display again. */
8646 if (fonts_changed_p)
8647 goto retry;
8648
8649 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8650 {
8651 /* See if we have to hscroll. */
8652 if (hscroll_windows (f->root_window))
8653 goto retry;
8654
8655 /* Prevent various kinds of signals during display
8656 update. stdio is not robust about handling
8657 signals, which can cause an apparent I/O
8658 error. */
8659 if (interrupt_input)
8660 unrequest_sigio ();
8661 stop_polling ();
8662
8663 /* Update the display. */
8664 set_window_update_flags (XWINDOW (f->root_window), 1);
8665 pause |= update_frame (f, 0, 0);
8666 if (pause)
8667 break;
8668
0528abe1
GM
8669 if (n == size)
8670 {
8671 int nbytes = size * sizeof *updated;
8672 struct frame **p = (struct frame **) alloca (2 * nbytes);
8673 bcopy (updated, p, nbytes);
8674 size *= 2;
8675 }
8676
8677 updated[n++] = f;
bd9d0f3f 8678 }
9769686d 8679 }
30c566e4 8680 }
0528abe1
GM
8681
8682 /* Do the mark_window_display_accurate after all windows have
8683 been redisplayed because this call resets flags in buffers
8684 which are needed for proper redisplay. */
8685 for (i = 0; i < n; ++i)
8686 {
8687 struct frame *f = updated[i];
8688 mark_window_display_accurate (f->root_window, 1);
8689 if (frame_up_to_date_hook)
8690 frame_up_to_date_hook (f);
8691 }
a2889657 8692 }
bd9d0f3f
GM
8693 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8694 {
8695 Lisp_Object mini_window;
8696 struct frame *mini_frame;
5f5c8ee5 8697
bd9d0f3f 8698 redisplay_window (selected_window, 1);
5f5c8ee5 8699
bd9d0f3f
GM
8700 /* Compare desired and current matrices, perform output. */
8701 update:
5f5c8ee5 8702
bd9d0f3f
GM
8703 /* If fonts changed, display again. */
8704 if (fonts_changed_p)
92a90e89 8705 goto retry;
a2889657 8706
bd9d0f3f
GM
8707 /* Prevent various kinds of signals during display update.
8708 stdio is not robust about handling signals,
8709 which can cause an apparent I/O error. */
8710 if (interrupt_input)
8711 unrequest_sigio ();
8712 stop_polling ();
1af9f229 8713
bd9d0f3f 8714 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 8715 {
92a90e89
GM
8716 if (hscroll_windows (selected_window))
8717 goto retry;
8718
5f5c8ee5 8719 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 8720 pause = update_frame (sf, 0, 0);
5f5c8ee5 8721 }
d724d989 8722
8de2d90b 8723 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
8724 function. If the echo area is on another frame, that may
8725 have put text on a frame other than the selected one, so the
8726 above call to update_frame would not have caught it. Catch
8de2d90b 8727 it here. */
bd9d0f3f
GM
8728 mini_window = FRAME_MINIBUF_WINDOW (sf);
8729 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 8730
bd9d0f3f
GM
8731 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8732 {
8733 XWINDOW (mini_window)->must_be_updated_p = 1;
8734 pause |= update_frame (mini_frame, 0, 0);
8735 if (!pause && hscroll_windows (mini_window))
8736 goto retry;
8737 }
6e8290aa 8738 }
a2889657 8739
5f5c8ee5
GM
8740 /* If display was paused because of pending input, make sure we do a
8741 thorough update the next time. */
a2889657
JB
8742 if (pause)
8743 {
5f5c8ee5
GM
8744 /* Prevent the optimization at the beginning of
8745 redisplay_internal that tries a single-line update of the
8746 line containing the cursor in the selected window. */
8747 CHARPOS (this_line_start_pos) = 0;
8748
8749 /* Let the overlay arrow be updated the next time. */
265a9e55 8750 if (!NILP (last_arrow_position))
a2889657
JB
8751 {
8752 last_arrow_position = Qt;
8753 last_arrow_string = Qt;
8754 }
5f5c8ee5
GM
8755
8756 /* If we pause after scrolling, some rows in the current
8757 matrices of some windows are not valid. */
8758 if (!WINDOW_FULL_WIDTH_P (w)
8759 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
8760 update_mode_lines = 1;
8761 }
43c09969 8762 else
a2889657 8763 {
43c09969 8764 if (!consider_all_windows_p)
a2889657 8765 {
43c09969
GM
8766 /* This has already been done above if
8767 consider_all_windows_p is set. */
8768 mark_window_display_accurate_1 (w, 1);
927c5b3b
GM
8769
8770 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8771 last_arrow_string = Voverlay_arrow_string;
8772
efc63ef0 8773 if (frame_up_to_date_hook != 0)
43c09969 8774 frame_up_to_date_hook (sf);
a2889657 8775 }
15e26c76 8776
a2889657
JB
8777 update_mode_lines = 0;
8778 windows_or_buffers_changed = 0;
8779 }
8780
5f5c8ee5
GM
8781 /* Start SIGIO interrupts coming again. Having them off during the
8782 code above makes it less likely one will discard output, but not
8783 impossible, since there might be stuff in the system buffer here.
a2889657 8784 But it is much hairier to try to do anything about that. */
a2889657
JB
8785 if (interrupt_input)
8786 request_sigio ();
8787 start_polling ();
8788
5f5c8ee5
GM
8789 /* If a frame has become visible which was not before, redisplay
8790 again, so that we display it. Expose events for such a frame
8791 (which it gets when becoming visible) don't call the parts of
8792 redisplay constructing glyphs, so simply exposing a frame won't
8793 display anything in this case. So, we have to display these
8794 frames here explicitly. */
11c52c4f
RS
8795 if (!pause)
8796 {
8797 Lisp_Object tail, frame;
8798 int new_count = 0;
8799
8800 FOR_EACH_FRAME (tail, frame)
8801 {
8802 int this_is_visible = 0;
8e83f802
RS
8803
8804 if (XFRAME (frame)->visible)
8805 this_is_visible = 1;
8806 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8807 if (XFRAME (frame)->visible)
8808 this_is_visible = 1;
11c52c4f
RS
8809
8810 if (this_is_visible)
8811 new_count++;
8812 }
8813
89819bdd 8814 if (new_count != number_of_visible_frames)
11c52c4f
RS
8815 windows_or_buffers_changed++;
8816 }
8817
44fa5b1e 8818 /* Change frame size now if a change is pending. */
c6e89d6c 8819 do_pending_window_change (1);
d8e242fd 8820
8b32d885
RS
8821 /* If we just did a pending size change, or have additional
8822 visible frames, redisplay again. */
3c8c72e0 8823 if (windows_or_buffers_changed && !pause)
8b32d885 8824 goto retry;
5f5c8ee5
GM
8825
8826 end_of_redisplay:;
c6e89d6c 8827
28514cd9 8828 unbind_to (count, Qnil);
a2889657
JB
8829}
8830
5f5c8ee5
GM
8831
8832/* Redisplay, but leave alone any recent echo area message unless
8833 another message has been requested in its place.
a2889657
JB
8834
8835 This is useful in situations where you need to redisplay but no
8836 user action has occurred, making it inappropriate for the message
8837 area to be cleared. See tracking_off and
9bf76936
GM
8838 wait_reading_process_input for examples of these situations.
8839
8840 FROM_WHERE is an integer saying from where this function was
8841 called. This is useful for debugging. */
a2889657 8842
8991bb31 8843void
9bf76936
GM
8844redisplay_preserve_echo_area (from_where)
8845 int from_where;
a2889657 8846{
9bf76936
GM
8847 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8848
c6e89d6c 8849 if (!NILP (echo_area_buffer[1]))
a2889657 8850 {
c6e89d6c
GM
8851 /* We have a previously displayed message, but no current
8852 message. Redisplay the previous message. */
8853 display_last_displayed_message_p = 1;
e9874cee 8854 redisplay_internal (1);
c6e89d6c 8855 display_last_displayed_message_p = 0;
a2889657
JB
8856 }
8857 else
e9874cee 8858 redisplay_internal (1);
a2889657
JB
8859}
8860
5f5c8ee5 8861
28514cd9
GM
8862/* Function registered with record_unwind_protect in
8863 redisplay_internal. Clears the flag indicating that a redisplay is
8864 in progress. */
8865
8866static Lisp_Object
8867unwind_redisplay (old_redisplaying_p)
8868 Lisp_Object old_redisplaying_p;
8869{
8870 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 8871 return Qnil;
28514cd9
GM
8872}
8873
8874
43c09969
GM
8875/* Mark the display of window W as accurate or inaccurate. If
8876 ACCURATE_P is non-zero mark display of W as accurate. If
8877 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8878 redisplay_internal is called. */
5f5c8ee5 8879
43c09969
GM
8880static void
8881mark_window_display_accurate_1 (w, accurate_p)
8882 struct window *w;
5f5c8ee5 8883 int accurate_p;
a2889657 8884{
43c09969 8885 if (BUFFERP (w->buffer))
a2889657 8886 {
43c09969
GM
8887 struct buffer *b = XBUFFER (w->buffer);
8888
8889 w->last_modified
8890 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8891 w->last_overlay_modified
8892 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8893 w->last_had_star
8894 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 8895
43c09969 8896 if (accurate_p)
bd66d1ba 8897 {
43c09969
GM
8898 b->clip_changed = 0;
8899 b->prevent_redisplay_optimizations_p = 0;
8900
8901 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8902 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8903 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8904 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
5f5c8ee5 8905
43c09969
GM
8906 w->current_matrix->buffer = b;
8907 w->current_matrix->begv = BUF_BEGV (b);
8908 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5 8909
43c09969
GM
8910 w->last_cursor = w->cursor;
8911 w->last_cursor_off_p = w->cursor_off_p;
8912
8913 if (w == XWINDOW (selected_window))
8914 w->last_point = make_number (BUF_PT (b));
8915 else
8916 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 8917 }
43c09969 8918 }
bd66d1ba 8919
43c09969
GM
8920 if (accurate_p)
8921 {
d2f84654 8922 w->window_end_valid = w->buffer;
a2889657 8923 w->update_mode_line = Qnil;
43c09969
GM
8924 }
8925}
8926
8927
8928/* Mark the display of windows in the window tree rooted at WINDOW as
8929 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8930 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8931 be redisplayed the next time redisplay_internal is called. */
8932
8933void
8934mark_window_display_accurate (window, accurate_p)
8935 Lisp_Object window;
8936 int accurate_p;
8937{
8938 struct window *w;
8939
8940 for (; !NILP (window); window = w->next)
8941 {
8942 w = XWINDOW (window);
8943 mark_window_display_accurate_1 (w, accurate_p);
a2889657 8944
265a9e55 8945 if (!NILP (w->vchild))
5f5c8ee5 8946 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 8947 if (!NILP (w->hchild))
5f5c8ee5 8948 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
8949 }
8950
5f5c8ee5 8951 if (accurate_p)
a2889657 8952 {
d45de95b 8953 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
8954 last_arrow_string = Voverlay_arrow_string;
8955 }
8956 else
8957 {
5f5c8ee5
GM
8958 /* Force a thorough redisplay the next time by setting
8959 last_arrow_position and last_arrow_string to t, which is
8960 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
8961 last_arrow_position = Qt;
8962 last_arrow_string = Qt;
8963 }
8964}
5f5c8ee5
GM
8965
8966
8967/* Return value in display table DP (Lisp_Char_Table *) for character
8968 C. Since a display table doesn't have any parent, we don't have to
8969 follow parent. Do not call this function directly but use the
8970 macro DISP_CHAR_VECTOR. */
8971
8972Lisp_Object
8973disp_char_vector (dp, c)
8974 struct Lisp_Char_Table *dp;
8975 int c;
8976{
8977 int code[4], i;
8978 Lisp_Object val;
8979
8980 if (SINGLE_BYTE_CHAR_P (c))
8981 return (dp->contents[c]);
8982
c5924f47 8983 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
8984 if (code[1] < 32)
8985 code[1] = -1;
8986 else if (code[2] < 32)
8987 code[2] = -1;
5f5c8ee5
GM
8988
8989 /* Here, the possible range of code[0] (== charset ID) is
8990 128..max_charset. Since the top level char table contains data
8991 for multibyte characters after 256th element, we must increment
8992 code[0] by 128 to get a correct index. */
8993 code[0] += 128;
8994 code[3] = -1; /* anchor */
8995
8996 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8997 {
8998 val = dp->contents[code[i]];
8999 if (!SUB_CHAR_TABLE_P (val))
9000 return (NILP (val) ? dp->defalt : val);
9001 }
9002
9003 /* Here, val is a sub char table. We return the default value of
9004 it. */
9005 return (dp->defalt);
9006}
9007
9008
a2889657 9009\f
5f5c8ee5
GM
9010/***********************************************************************
9011 Window Redisplay
9012 ***********************************************************************/
a2725ab2 9013
5f5c8ee5 9014/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
9015
9016static void
5f5c8ee5
GM
9017redisplay_windows (window)
9018 Lisp_Object window;
90adcf20 9019{
5f5c8ee5
GM
9020 while (!NILP (window))
9021 {
9022 struct window *w = XWINDOW (window);
9023
9024 if (!NILP (w->hchild))
9025 redisplay_windows (w->hchild);
9026 else if (!NILP (w->vchild))
9027 redisplay_windows (w->vchild);
9028 else
9029 redisplay_window (window, 0);
a2725ab2 9030
5f5c8ee5
GM
9031 window = w->next;
9032 }
9033}
9034
9035
9036/* Set cursor position of W. PT is assumed to be displayed in ROW.
9037 DELTA is the number of bytes by which positions recorded in ROW
9038 differ from current buffer positions. */
9039
9040void
9041set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9042 struct window *w;
9043 struct glyph_row *row;
9044 struct glyph_matrix *matrix;
9045 int delta, delta_bytes, dy, dvpos;
9046{
9047 struct glyph *glyph = row->glyphs[TEXT_AREA];
9048 struct glyph *end = glyph + row->used[TEXT_AREA];
9049 int x = row->x;
9050 int pt_old = PT - delta;
9051
9052 /* Skip over glyphs not having an object at the start of the row.
9053 These are special glyphs like truncation marks on terminal
9054 frames. */
9055 if (row->displays_text_p)
9056 while (glyph < end
6fc556fd 9057 && INTEGERP (glyph->object)
5f5c8ee5
GM
9058 && glyph->charpos < 0)
9059 {
9060 x += glyph->pixel_width;
9061 ++glyph;
9062 }
9063
9064 while (glyph < end
6fc556fd 9065 && !INTEGERP (glyph->object)
5f5c8ee5
GM
9066 && (!BUFFERP (glyph->object)
9067 || glyph->charpos < pt_old))
9068 {
9069 x += glyph->pixel_width;
9070 ++glyph;
9071 }
9072
9073 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9074 w->cursor.x = x;
9075 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9076 w->cursor.y = row->y + dy;
9077
9078 if (w == XWINDOW (selected_window))
9079 {
9080 if (!row->continued_p
9081 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9082 && row->x == 0)
9083 {
9084 this_line_buffer = XBUFFER (w->buffer);
9085
9086 CHARPOS (this_line_start_pos)
9087 = MATRIX_ROW_START_CHARPOS (row) + delta;
9088 BYTEPOS (this_line_start_pos)
9089 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9090
9091 CHARPOS (this_line_end_pos)
9092 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9093 BYTEPOS (this_line_end_pos)
9094 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9095
9096 this_line_y = w->cursor.y;
9097 this_line_pixel_height = row->height;
9098 this_line_vpos = w->cursor.vpos;
9099 this_line_start_x = row->x;
9100 }
9101 else
9102 CHARPOS (this_line_start_pos) = 0;
9103 }
9104}
9105
9106
9107/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
9108 start STARTP. Sets the window start of WINDOW to that position.
9109
9110 We assume that the window's buffer is really current. */
5f5c8ee5
GM
9111
9112static INLINE struct text_pos
9113run_window_scroll_functions (window, startp)
9114 Lisp_Object window;
9115 struct text_pos startp;
9116{
9117 struct window *w = XWINDOW (window);
9118 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
9119
9120 if (current_buffer != XBUFFER (w->buffer))
9121 abort ();
9122
5f5c8ee5
GM
9123 if (!NILP (Vwindow_scroll_functions))
9124 {
9125 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9126 make_number (CHARPOS (startp)));
9127 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
9128 /* In case the hook functions switch buffers. */
9129 if (current_buffer != XBUFFER (w->buffer))
9130 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 9131 }
90adcf20 9132
5f5c8ee5
GM
9133 return startp;
9134}
9135
9136
9137/* Modify the desired matrix of window W and W->vscroll so that the
9138 line containing the cursor is fully visible. */
9139
9140static void
9141make_cursor_line_fully_visible (w)
9142 struct window *w;
9143{
9144 struct glyph_matrix *matrix;
9145 struct glyph_row *row;
478d746b 9146 int window_height;
5f5c8ee5
GM
9147
9148 /* It's not always possible to find the cursor, e.g, when a window
9149 is full of overlay strings. Don't do anything in that case. */
9150 if (w->cursor.vpos < 0)
9151 return;
9152
9153 matrix = w->desired_matrix;
9154 row = MATRIX_ROW (matrix, w->cursor.vpos);
9155
b28cb6ed
GM
9156 /* If the cursor row is not partially visible, there's nothing
9157 to do. */
9158 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9159 return;
9160
9161 /* If the row the cursor is in is taller than the window's height,
9162 it's not clear what to do, so do nothing. */
9163 window_height = window_box_height (w);
9164 if (row->height >= window_height)
9165 return;
9166
9167 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
9168 {
9169 int dy = row->height - row->visible_height;
9170 w->vscroll = 0;
9171 w->cursor.y += dy;
9172 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9173 }
b28cb6ed 9174 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
9175 {
9176 int dy = - (row->height - row->visible_height);
9177 w->vscroll = dy;
9178 w->cursor.y += dy;
9179 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9180 }
b28cb6ed 9181
5f5c8ee5
GM
9182 /* When we change the cursor y-position of the selected window,
9183 change this_line_y as well so that the display optimization for
9184 the cursor line of the selected window in redisplay_internal uses
9185 the correct y-position. */
9186 if (w == XWINDOW (selected_window))
9187 this_line_y = w->cursor.y;
9188}
9189
9190
9191/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9192 non-zero means only WINDOW is redisplayed in redisplay_internal.
9193 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9194 in redisplay_window to bring a partially visible line into view in
9195 the case that only the cursor has moved.
9196
9197 Value is
9198
9199 1 if scrolling succeeded
9200
9201 0 if scrolling didn't find point.
9202
9203 -1 if new fonts have been loaded so that we must interrupt
9204 redisplay, adjust glyph matrices, and try again. */
9205
9206static int
9207try_scrolling (window, just_this_one_p, scroll_conservatively,
9208 scroll_step, temp_scroll_step)
9209 Lisp_Object window;
9210 int just_this_one_p;
9211 int scroll_conservatively, scroll_step;
9212 int temp_scroll_step;
9213{
9214 struct window *w = XWINDOW (window);
9215 struct frame *f = XFRAME (w->frame);
9216 struct text_pos scroll_margin_pos;
9217 struct text_pos pos;
9218 struct text_pos startp;
9219 struct it it;
9220 Lisp_Object window_end;
9221 int this_scroll_margin;
9222 int dy = 0;
9223 int scroll_max;
b8a63ccb 9224 int rc;
5f5c8ee5
GM
9225 int amount_to_scroll = 0;
9226 Lisp_Object aggressive;
9227 int height;
9228
9229#if GLYPH_DEBUG
9230 debug_method_add (w, "try_scrolling");
78614721 9231#endif
5f5c8ee5
GM
9232
9233 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9234
9235 /* Compute scroll margin height in pixels. We scroll when point is
9236 within this distance from the top or bottom of the window. */
9237 if (scroll_margin > 0)
90adcf20 9238 {
5f5c8ee5
GM
9239 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9240 this_scroll_margin *= CANON_Y_UNIT (f);
9241 }
9242 else
9243 this_scroll_margin = 0;
9244
9245 /* Compute how much we should try to scroll maximally to bring point
9246 into view. */
0894e696
SM
9247 if (scroll_step || scroll_conservatively || temp_scroll_step)
9248 scroll_max = max (scroll_step,
9249 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
9250 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9251 || NUMBERP (current_buffer->scroll_up_aggressively))
9252 /* We're trying to scroll because of aggressive scrolling
9253 but no scroll_step is set. Choose an arbitrary one. Maybe
9254 there should be a variable for this. */
9255 scroll_max = 10;
9256 else
9257 scroll_max = 0;
9258 scroll_max *= CANON_Y_UNIT (f);
9259
9260 /* Decide whether we have to scroll down. Start at the window end
9261 and move this_scroll_margin up to find the position of the scroll
9262 margin. */
9263 window_end = Fwindow_end (window, Qt);
9264 CHARPOS (scroll_margin_pos) = XINT (window_end);
9265 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9266 if (this_scroll_margin)
9267 {
9268 start_display (&it, w, scroll_margin_pos);
9269 move_it_vertically (&it, - this_scroll_margin);
9270 scroll_margin_pos = it.current.pos;
9271 }
9272
9273 if (PT >= CHARPOS (scroll_margin_pos))
9274 {
9275 int y0;
9276
9277 /* Point is in the scroll margin at the bottom of the window, or
9278 below. Compute a new window start that makes point visible. */
47589c8c 9279
5f5c8ee5
GM
9280 /* Compute the distance from the scroll margin to PT.
9281 Give up if the distance is greater than scroll_max. */
9282 start_display (&it, w, scroll_margin_pos);
9283 y0 = it.current_y;
9284 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9285 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
e89aaabd 9286
82e274d1
GM
9287 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9288 end, which is one line below the window. The iterator's
9289 current_y will be same as y0 in that case, but we have to
9290 scroll a line to make PT visible. That's the reason why 1 is
9291 added below. */
9292 dy = 1 + it.current_y - y0;
47589c8c 9293
5f5c8ee5
GM
9294 if (dy > scroll_max)
9295 return 0;
9296
9297 /* Move the window start down. If scrolling conservatively,
9298 move it just enough down to make point visible. If
9299 scroll_step is set, move it down by scroll_step. */
9300 start_display (&it, w, startp);
9301
9302 if (scroll_conservatively)
e89aaabd
GM
9303 amount_to_scroll
9304 = max (max (dy, CANON_Y_UNIT (f)),
9305 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9306 else if (scroll_step || temp_scroll_step)
9307 amount_to_scroll = scroll_max;
9308 else
90adcf20 9309 {
5f5c8ee5
GM
9310 aggressive = current_buffer->scroll_down_aggressively;
9311 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9312 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9313 if (NUMBERP (aggressive))
9314 amount_to_scroll = XFLOATINT (aggressive) * height;
9315 }
a2725ab2 9316
5f5c8ee5
GM
9317 if (amount_to_scroll <= 0)
9318 return 0;
a2725ab2 9319
5f5c8ee5
GM
9320 move_it_vertically (&it, amount_to_scroll);
9321 startp = it.current.pos;
9322 }
9323 else
9324 {
9325 /* See if point is inside the scroll margin at the top of the
9326 window. */
9327 scroll_margin_pos = startp;
9328 if (this_scroll_margin)
9329 {
9330 start_display (&it, w, startp);
9331 move_it_vertically (&it, this_scroll_margin);
9332 scroll_margin_pos = it.current.pos;
9333 }
9334
9335 if (PT < CHARPOS (scroll_margin_pos))
9336 {
9337 /* Point is in the scroll margin at the top of the window or
9338 above what is displayed in the window. */
9339 int y0;
9340
9341 /* Compute the vertical distance from PT to the scroll
9342 margin position. Give up if distance is greater than
9343 scroll_max. */
9344 SET_TEXT_POS (pos, PT, PT_BYTE);
9345 start_display (&it, w, pos);
9346 y0 = it.current_y;
9347 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9348 it.last_visible_y, -1,
9349 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9350 dy = it.current_y - y0;
9351 if (dy > scroll_max)
9352 return 0;
9353
9354 /* Compute new window start. */
9355 start_display (&it, w, startp);
9356
9357 if (scroll_conservatively)
0894e696
SM
9358 amount_to_scroll =
9359 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9360 else if (scroll_step || temp_scroll_step)
9361 amount_to_scroll = scroll_max;
538f13d4 9362 else
5f5c8ee5
GM
9363 {
9364 aggressive = current_buffer->scroll_up_aggressively;
9365 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9366 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9367 if (NUMBERP (aggressive))
9368 amount_to_scroll = XFLOATINT (aggressive) * height;
9369 }
a2725ab2 9370
5f5c8ee5
GM
9371 if (amount_to_scroll <= 0)
9372 return 0;
9373
9374 move_it_vertically (&it, - amount_to_scroll);
9375 startp = it.current.pos;
90adcf20
RS
9376 }
9377 }
a2889657 9378
5f5c8ee5
GM
9379 /* Run window scroll functions. */
9380 startp = run_window_scroll_functions (window, startp);
90adcf20 9381
5f5c8ee5
GM
9382 /* Display the window. Give up if new fonts are loaded, or if point
9383 doesn't appear. */
9384 if (!try_window (window, startp))
9385 rc = -1;
9386 else if (w->cursor.vpos < 0)
9387 {
9388 clear_glyph_matrix (w->desired_matrix);
9389 rc = 0;
9390 }
9391 else
9392 {
9393 /* Maybe forget recorded base line for line number display. */
9394 if (!just_this_one_p
9395 || current_buffer->clip_changed
9142dd5b 9396 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
9397 w->base_line_number = Qnil;
9398
9399 /* If cursor ends up on a partially visible line, shift display
9400 lines up or down. */
9401 make_cursor_line_fully_visible (w);
9402 rc = 1;
9403 }
9404
9405 return rc;
a2889657
JB
9406}
9407
5f5c8ee5
GM
9408
9409/* Compute a suitable window start for window W if display of W starts
9410 on a continuation line. Value is non-zero if a new window start
9411 was computed.
9412
9413 The new window start will be computed, based on W's width, starting
9414 from the start of the continued line. It is the start of the
9415 screen line with the minimum distance from the old start W->start. */
9416
9417static int
9418compute_window_start_on_continuation_line (w)
9419 struct window *w;
1f1ff51d 9420{
5f5c8ee5
GM
9421 struct text_pos pos, start_pos;
9422 int window_start_changed_p = 0;
1f1ff51d 9423
5f5c8ee5 9424 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 9425
5f5c8ee5
GM
9426 /* If window start is on a continuation line... Window start may be
9427 < BEGV in case there's invisible text at the start of the
9428 buffer (M-x rmail, for example). */
9429 if (CHARPOS (start_pos) > BEGV
9430 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 9431 {
5f5c8ee5
GM
9432 struct it it;
9433 struct glyph_row *row;
f3751a65
GM
9434
9435 /* Handle the case that the window start is out of range. */
9436 if (CHARPOS (start_pos) < BEGV)
9437 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9438 else if (CHARPOS (start_pos) > ZV)
9439 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
9440
9441 /* Find the start of the continued line. This should be fast
9442 because scan_buffer is fast (newline cache). */
045dee35 9443 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
9444 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9445 row, DEFAULT_FACE_ID);
9446 reseat_at_previous_visible_line_start (&it);
9447
9448 /* If the line start is "too far" away from the window start,
9449 say it takes too much time to compute a new window start. */
9450 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9451 < XFASTINT (w->height) * XFASTINT (w->width))
9452 {
9453 int min_distance, distance;
9454
9455 /* Move forward by display lines to find the new window
9456 start. If window width was enlarged, the new start can
9457 be expected to be > the old start. If window width was
9458 decreased, the new window start will be < the old start.
9459 So, we're looking for the display line start with the
9460 minimum distance from the old window start. */
9461 pos = it.current.pos;
9462 min_distance = INFINITY;
9463 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9464 distance < min_distance)
9465 {
9466 min_distance = distance;
9467 pos = it.current.pos;
9468 move_it_by_lines (&it, 1, 0);
9469 }
9470
9471 /* Set the window start there. */
9472 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9473 window_start_changed_p = 1;
9474 }
1f1ff51d 9475 }
5f5c8ee5
GM
9476
9477 return window_start_changed_p;
1f1ff51d
KH
9478}
9479
5f5c8ee5 9480
47589c8c
GM
9481/* Try cursor movement in case text has not changes in window WINDOW,
9482 with window start STARTP. Value is
9483
9484 1 if successful
9485
9486 0 if this method cannot be used
9487
9488 -1 if we know we have to scroll the display. *SCROLL_STEP is
9489 set to 1, under certain circumstances, if we want to scroll as
9490 if scroll-step were set to 1. See the code. */
9491
9492static int
9493try_cursor_movement (window, startp, scroll_step)
9494 Lisp_Object window;
9495 struct text_pos startp;
9496 int *scroll_step;
9497{
9498 struct window *w = XWINDOW (window);
9499 struct frame *f = XFRAME (w->frame);
9500 int rc = 0;
9501
9502 /* Handle case where text has not changed, only point, and it has
9503 not moved off the frame. */
9504 if (/* Point may be in this window. */
9505 PT >= CHARPOS (startp)
47589c8c
GM
9506 /* Selective display hasn't changed. */
9507 && !current_buffer->clip_changed
4db87380
GM
9508 /* Function force-mode-line-update is used to force a thorough
9509 redisplay. It sets either windows_or_buffers_changed or
9510 update_mode_lines. So don't take a shortcut here for these
9511 cases. */
9512 && !update_mode_lines
9513 && !windows_or_buffers_changed
47589c8c
GM
9514 /* Can't use this case if highlighting a region. When a
9515 region exists, cursor movement has to do more than just
9516 set the cursor. */
9517 && !(!NILP (Vtransient_mark_mode)
9518 && !NILP (current_buffer->mark_active))
9519 && NILP (w->region_showing)
9520 && NILP (Vshow_trailing_whitespace)
9521 /* Right after splitting windows, last_point may be nil. */
9522 && INTEGERP (w->last_point)
9523 /* This code is not used for mini-buffer for the sake of the case
9524 of redisplaying to replace an echo area message; since in
9525 that case the mini-buffer contents per se are usually
9526 unchanged. This code is of no real use in the mini-buffer
9527 since the handling of this_line_start_pos, etc., in redisplay
9528 handles the same cases. */
9529 && !EQ (window, minibuf_window)
9530 /* When splitting windows or for new windows, it happens that
9531 redisplay is called with a nil window_end_vpos or one being
9532 larger than the window. This should really be fixed in
9533 window.c. I don't have this on my list, now, so we do
9534 approximately the same as the old redisplay code. --gerd. */
9535 && INTEGERP (w->window_end_vpos)
9536 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9537 && (FRAME_WINDOW_P (f)
9538 || !MARKERP (Voverlay_arrow_position)
9539 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9540 {
9541 int this_scroll_margin;
9542 struct glyph_row *row;
9543
9544#if GLYPH_DEBUG
9545 debug_method_add (w, "cursor movement");
9546#endif
9547
9548 /* Scroll if point within this distance from the top or bottom
9549 of the window. This is a pixel value. */
9550 this_scroll_margin = max (0, scroll_margin);
9551 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9552 this_scroll_margin *= CANON_Y_UNIT (f);
9553
9554 /* Start with the row the cursor was displayed during the last
9555 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
9556 if (w->last_cursor.vpos < 0
9557 || w->last_cursor.vpos >= w->current_matrix->nrows)
47589c8c
GM
9558 rc = -1;
9559 else
9560 {
9561 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9562 if (row->mode_line_p)
9563 ++row;
9564 if (!row->enabled_p)
9565 rc = -1;
9566 }
9567
9568 if (rc == 0)
9569 {
9570 int scroll_p = 0;
68c5d1db
GM
9571 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9572
47589c8c
GM
9573 if (PT > XFASTINT (w->last_point))
9574 {
9575 /* Point has moved forward. */
47589c8c
GM
9576 while (MATRIX_ROW_END_CHARPOS (row) < PT
9577 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9578 {
9579 xassert (row->enabled_p);
9580 ++row;
9581 }
9582
9583 /* The end position of a row equals the start position
9584 of the next row. If PT is there, we would rather
cafafe0b
GM
9585 display it in the next line. */
9586 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9587 && MATRIX_ROW_END_CHARPOS (row) == PT
9588 && !cursor_row_p (w, row))
9589 ++row;
47589c8c
GM
9590
9591 /* If within the scroll margin, scroll. Note that
9592 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9593 the next line would be drawn, and that
9594 this_scroll_margin can be zero. */
9595 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9596 || PT > MATRIX_ROW_END_CHARPOS (row)
9597 /* Line is completely visible last line in window
9598 and PT is to be set in the next line. */
9599 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9600 && PT == MATRIX_ROW_END_CHARPOS (row)
9601 && !row->ends_at_zv_p
9602 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9603 scroll_p = 1;
9604 }
9605 else if (PT < XFASTINT (w->last_point))
9606 {
9607 /* Cursor has to be moved backward. Note that PT >=
9608 CHARPOS (startp) because of the outer
9609 if-statement. */
9610 while (!row->mode_line_p
9611 && (MATRIX_ROW_START_CHARPOS (row) > PT
9612 || (MATRIX_ROW_START_CHARPOS (row) == PT
9613 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9614 && (row->y > this_scroll_margin
9615 || CHARPOS (startp) == BEGV))
9616 {
9617 xassert (row->enabled_p);
9618 --row;
9619 }
9620
9621 /* Consider the following case: Window starts at BEGV,
9622 there is invisible, intangible text at BEGV, so that
9623 display starts at some point START > BEGV. It can
9624 happen that we are called with PT somewhere between
9625 BEGV and START. Try to handle that case. */
9626 if (row < w->current_matrix->rows
9627 || row->mode_line_p)
9628 {
9629 row = w->current_matrix->rows;
9630 if (row->mode_line_p)
9631 ++row;
9632 }
9633
9634 /* Due to newlines in overlay strings, we may have to
9635 skip forward over overlay strings. */
68c5d1db
GM
9636 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9637 && MATRIX_ROW_END_CHARPOS (row) == PT
9638 && !cursor_row_p (w, row))
47589c8c
GM
9639 ++row;
9640
9641 /* If within the scroll margin, scroll. */
9642 if (row->y < this_scroll_margin
9643 && CHARPOS (startp) != BEGV)
9644 scroll_p = 1;
9645 }
9646
9647 if (PT < MATRIX_ROW_START_CHARPOS (row)
9648 || PT > MATRIX_ROW_END_CHARPOS (row))
9649 {
9650 /* if PT is not in the glyph row, give up. */
9651 rc = -1;
9652 }
440fc135 9653 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 9654 {
8de4aaf8
GM
9655 if (PT == MATRIX_ROW_END_CHARPOS (row)
9656 && !row->ends_at_zv_p
9657 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
3f7e3031
GM
9658 rc = -1;
9659 else if (row->height > window_box_height (w))
440fc135 9660 {
8de4aaf8
GM
9661 /* If we end up in a partially visible line, let's
9662 make it fully visible, except when it's taller
9663 than the window, in which case we can't do much
9664 about it. */
440fc135
GM
9665 *scroll_step = 1;
9666 rc = -1;
9667 }
9668 else
9669 {
9670 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9671 try_window (window, startp);
9672 make_cursor_line_fully_visible (w);
9673 rc = 1;
9674 }
47589c8c
GM
9675 }
9676 else if (scroll_p)
9677 rc = -1;
9678 else
9679 {
9680 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9681 rc = 1;
9682 }
9683 }
9684 }
9685
9686 return rc;
9687}
9688
9689
5f5c8ee5
GM
9690/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9691 selected_window is redisplayed. */
90adcf20 9692
a2889657 9693static void
5f5c8ee5 9694redisplay_window (window, just_this_one_p)
a2889657 9695 Lisp_Object window;
5f5c8ee5 9696 int just_this_one_p;
a2889657 9697{
5f5c8ee5
GM
9698 struct window *w = XWINDOW (window);
9699 struct frame *f = XFRAME (w->frame);
9700 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 9701 struct buffer *old = current_buffer;
5f5c8ee5 9702 struct text_pos lpoint, opoint, startp;
e481f960 9703 int update_mode_line;
5f5c8ee5
GM
9704 int tem;
9705 struct it it;
9706 /* Record it now because it's overwritten. */
9707 int current_matrix_up_to_date_p = 0;
5f5c8ee5 9708 int temp_scroll_step = 0;
2d27dae2 9709 int count = BINDING_STACK_SIZE ();
47589c8c 9710 int rc;
a2889657 9711
5f5c8ee5
GM
9712 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9713 opoint = lpoint;
a2889657 9714
5f5c8ee5
GM
9715 /* W must be a leaf window here. */
9716 xassert (!NILP (w->buffer));
9717#if GLYPH_DEBUG
9718 *w->desired_matrix->method = 0;
9719#endif
2e54982e
RS
9720
9721 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
9722
9723 reconsider_clip_changes (w, buffer);
9724
5f5c8ee5
GM
9725 /* Has the mode line to be updated? */
9726 update_mode_line = (!NILP (w->update_mode_line)
9727 || update_mode_lines
9728 || buffer->clip_changed);
8de2d90b
JB
9729
9730 if (MINI_WINDOW_P (w))
9731 {
5f5c8ee5 9732 if (w == XWINDOW (echo_area_window)
c6e89d6c 9733 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
9734 {
9735 if (update_mode_line)
9736 /* We may have to update a tty frame's menu bar or a
e037b9ec 9737 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
9738 goto finish_menu_bars;
9739 else
9740 /* We've already displayed the echo area glyphs in this window. */
9741 goto finish_scroll_bars;
9742 }
73af359d 9743 else if (w != XWINDOW (minibuf_window))
8de2d90b 9744 {
5f5c8ee5
GM
9745 /* W is a mini-buffer window, but it's not the currently
9746 active one, so clear it. */
9747 int yb = window_text_bottom_y (w);
9748 struct glyph_row *row;
9749 int y;
9750
9751 for (y = 0, row = w->desired_matrix->rows;
9752 y < yb;
9753 y += row->height, ++row)
9754 blank_row (w, row, y);
88f22aff 9755 goto finish_scroll_bars;
8de2d90b 9756 }
c095a1dd
GM
9757
9758 clear_glyph_matrix (w->desired_matrix);
8de2d90b 9759 }
a2889657 9760
5f5c8ee5
GM
9761 /* Otherwise set up data on this window; select its buffer and point
9762 value. */
6a93695f
GM
9763 /* Really select the buffer, for the sake of buffer-local
9764 variables. */
9765 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
9766 SET_TEXT_POS (opoint, PT, PT_BYTE);
9767
9768 current_matrix_up_to_date_p
9769 = (!NILP (w->window_end_valid)
9770 && !current_buffer->clip_changed
9771 && XFASTINT (w->last_modified) >= MODIFF
9772 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 9773
5f5c8ee5
GM
9774 /* When windows_or_buffers_changed is non-zero, we can't rely on
9775 the window end being valid, so set it to nil there. */
9776 if (windows_or_buffers_changed)
9777 {
9778 /* If window starts on a continuation line, maybe adjust the
9779 window start in case the window's width changed. */
9780 if (XMARKER (w->start)->buffer == current_buffer)
9781 compute_window_start_on_continuation_line (w);
9782
9783 w->window_end_valid = Qnil;
9784 }
12adba34 9785
5f5c8ee5
GM
9786 /* Some sanity checks. */
9787 CHECK_WINDOW_END (w);
9788 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 9789 abort ();
5f5c8ee5 9790 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 9791 abort ();
a2889657 9792
28995e67
RS
9793 /* If %c is in mode line, update it if needed. */
9794 if (!NILP (w->column_number_displayed)
9795 /* This alternative quickly identifies a common case
9796 where no change is needed. */
9797 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9798 && XFASTINT (w->last_modified) >= MODIFF
9799 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
9800 && XFASTINT (w->column_number_displayed) != current_column ())
9801 update_mode_line = 1;
9802
5f5c8ee5
GM
9803 /* Count number of windows showing the selected buffer. An indirect
9804 buffer counts as its base buffer. */
9805 if (!just_this_one_p)
42640f83
RS
9806 {
9807 struct buffer *current_base, *window_base;
9808 current_base = current_buffer;
9809 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9810 if (current_base->base_buffer)
9811 current_base = current_base->base_buffer;
9812 if (window_base->base_buffer)
9813 window_base = window_base->base_buffer;
9814 if (current_base == window_base)
9815 buffer_shared++;
9816 }
a2889657 9817
5f5c8ee5
GM
9818 /* Point refers normally to the selected window. For any other
9819 window, set up appropriate value. */
a2889657
JB
9820 if (!EQ (window, selected_window))
9821 {
12adba34
RS
9822 int new_pt = XMARKER (w->pointm)->charpos;
9823 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 9824 if (new_pt < BEGV)
a2889657 9825 {
f67a0f51 9826 new_pt = BEGV;
12adba34
RS
9827 new_pt_byte = BEGV_BYTE;
9828 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 9829 }
f67a0f51 9830 else if (new_pt > (ZV - 1))
a2889657 9831 {
f67a0f51 9832 new_pt = ZV;
12adba34
RS
9833 new_pt_byte = ZV_BYTE;
9834 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 9835 }
5f5c8ee5 9836
f67a0f51 9837 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 9838 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
9839 }
9840
f4faa47c 9841 /* If any of the character widths specified in the display table
5f5c8ee5
GM
9842 have changed, invalidate the width run cache. It's true that
9843 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
9844 redisplay goes (non-fatally) haywire when the display table is
9845 changed, so why should we worry about doing any better? */
9846 if (current_buffer->width_run_cache)
9847 {
f908610f 9848 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
9849
9850 if (! disptab_matches_widthtab (disptab,
9851 XVECTOR (current_buffer->width_table)))
9852 {
9853 invalidate_region_cache (current_buffer,
9854 current_buffer->width_run_cache,
9855 BEG, Z);
9856 recompute_width_table (current_buffer, disptab);
9857 }
9858 }
9859
a2889657 9860 /* If window-start is screwed up, choose a new one. */
a2889657
JB
9861 if (XMARKER (w->start)->buffer != current_buffer)
9862 goto recenter;
9863
5f5c8ee5 9864 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 9865
cf0df6ab
RS
9866 /* If someone specified a new starting point but did not insist,
9867 check whether it can be used. */
cfad01b4
GM
9868 if (!NILP (w->optional_new_start)
9869 && CHARPOS (startp) >= BEGV
9870 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
9871 {
9872 w->optional_new_start = Qnil;
5f5c8ee5
GM
9873 start_display (&it, w, startp);
9874 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9875 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9876 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
9877 w->force_start = Qt;
9878 }
9879
8de2d90b 9880 /* Handle case where place to start displaying has been specified,
aa6d10fa 9881 unless the specified location is outside the accessible range. */
9472f927
GM
9882 if (!NILP (w->force_start)
9883 || w->frozen_window_start_p)
a2889657 9884 {
e63574d7 9885 w->force_start = Qnil;
5f5c8ee5 9886 w->vscroll = 0;
b5174a51 9887 w->window_end_valid = Qnil;
5f5c8ee5
GM
9888
9889 /* Forget any recorded base line for line number display. */
9890 if (!current_matrix_up_to_date_p
9891 || current_buffer->clip_changed)
9892 w->base_line_number = Qnil;
9893
75c43375
RS
9894 /* Redisplay the mode line. Select the buffer properly for that.
9895 Also, run the hook window-scroll-functions
9896 because we have scrolled. */
e63574d7
RS
9897 /* Note, we do this after clearing force_start because
9898 if there's an error, it is better to forget about force_start
9899 than to get into an infinite loop calling the hook functions
9900 and having them get more errors. */
75c43375
RS
9901 if (!update_mode_line
9902 || ! NILP (Vwindow_scroll_functions))
e481f960 9903 {
e481f960
RS
9904 update_mode_line = 1;
9905 w->update_mode_line = Qt;
5f5c8ee5 9906 startp = run_window_scroll_functions (window, startp);
e481f960 9907 }
5f5c8ee5 9908
ac90c44f
GM
9909 w->last_modified = make_number (0);
9910 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
9911 if (CHARPOS (startp) < BEGV)
9912 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9913 else if (CHARPOS (startp) > ZV)
9914 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9915
9916 /* Redisplay, then check if cursor has been set during the
9917 redisplay. Give up if new fonts were loaded. */
9918 if (!try_window (window, startp))
9919 {
9920 w->force_start = Qt;
9921 clear_glyph_matrix (w->desired_matrix);
504454e8 9922 goto finish_scroll_bars;
5f5c8ee5
GM
9923 }
9924
9472f927 9925 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 9926 {
b28cb6ed
GM
9927 /* If point does not appear, try to move point so it does
9928 appear. The desired matrix has been built above, so we
9929 can use it here. */
9930 int window_height;
9931 struct glyph_row *row;
9932
9933 window_height = window_box_height (w) / 2;
9934 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9935 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
5f5c8ee5
GM
9936 ++row;
9937
9938 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9939 MATRIX_ROW_START_BYTEPOS (row));
9940
90adcf20 9941 if (w != XWINDOW (selected_window))
12adba34 9942 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
9943 else if (current_buffer == old)
9944 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9945
9946 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9947
9948 /* If we are highlighting the region, then we just changed
9949 the region, so redisplay to show it. */
df0b5ea1
RS
9950 if (!NILP (Vtransient_mark_mode)
9951 && !NILP (current_buffer->mark_active))
6f27fa9b 9952 {
5f5c8ee5
GM
9953 clear_glyph_matrix (w->desired_matrix);
9954 if (!try_window (window, startp))
504454e8 9955 goto finish_scroll_bars;
6f27fa9b 9956 }
a2889657 9957 }
5f5c8ee5
GM
9958
9959 make_cursor_line_fully_visible (w);
9960#if GLYPH_DEBUG
9961 debug_method_add (w, "forced window start");
9962#endif
a2889657
JB
9963 goto done;
9964 }
9965
5f5c8ee5
GM
9966 /* Handle case where text has not changed, only point, and it has
9967 not moved off the frame. */
9968 if (current_matrix_up_to_date_p
47589c8c
GM
9969 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9970 rc != 0))
a2889657 9971 {
47589c8c 9972 if (rc == -1)
5f5c8ee5 9973 goto try_to_scroll;
47589c8c
GM
9974 else
9975 goto done;
a2889657
JB
9976 }
9977 /* If current starting point was originally the beginning of a line
9978 but no longer is, find a new starting point. */
265a9e55 9979 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
9980 && !(CHARPOS (startp) <= BEGV
9981 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 9982 {
5f5c8ee5
GM
9983#if GLYPH_DEBUG
9984 debug_method_add (w, "recenter 1");
9985#endif
a2889657
JB
9986 goto recenter;
9987 }
5f5c8ee5 9988
27d16f05
GM
9989 /* Try scrolling with try_window_id. Value is > 0 if update has
9990 been done, it is -1 if we know that the same window start will
9991 not work. It is 0 if unsuccessful for some other reason. */
9992 else if ((tem = try_window_id (w)) != 0)
a2889657 9993 {
5f5c8ee5 9994#if GLYPH_DEBUG
ef121659 9995 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
9996#endif
9997
9998 if (fonts_changed_p)
504454e8 9999 goto finish_scroll_bars;
a2889657
JB
10000 if (tem > 0)
10001 goto done;
ef121659 10002
5f5c8ee5
GM
10003 /* Otherwise try_window_id has returned -1 which means that we
10004 don't want the alternative below this comment to execute. */
a2889657 10005 }
5f5c8ee5
GM
10006 else if (CHARPOS (startp) >= BEGV
10007 && CHARPOS (startp) <= ZV
10008 && PT >= CHARPOS (startp)
10009 && (CHARPOS (startp) < ZV
e9874cee 10010 /* Avoid starting at end of buffer. */
5f5c8ee5 10011 || CHARPOS (startp) == BEGV
8850a573
RS
10012 || (XFASTINT (w->last_modified) >= MODIFF
10013 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 10014 {
5f5c8ee5
GM
10015#if GLYPH_DEBUG
10016 debug_method_add (w, "same window start");
10017#endif
10018
10019 /* Try to redisplay starting at same place as before.
10020 If point has not moved off frame, accept the results. */
10021 if (!current_matrix_up_to_date_p
10022 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
10023 because a window scroll function can have changed the
10024 buffer. */
5f5c8ee5
GM
10025 || !NILP (Vwindow_scroll_functions)
10026 || MINI_WINDOW_P (w)
10027 || !try_window_reusing_current_matrix (w))
10028 {
10029 IF_DEBUG (debug_method_add (w, "1"));
10030 try_window (window, startp);
10031 }
10032
10033 if (fonts_changed_p)
504454e8 10034 goto finish_scroll_bars;
5f5c8ee5
GM
10035
10036 if (w->cursor.vpos >= 0)
aa6d10fa 10037 {
5f5c8ee5
GM
10038 if (!just_this_one_p
10039 || current_buffer->clip_changed
9142dd5b 10040 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
10041 /* Forget any recorded base line for line number display. */
10042 w->base_line_number = Qnil;
5f5c8ee5
GM
10043
10044 make_cursor_line_fully_visible (w);
aa6d10fa
RS
10045 goto done;
10046 }
a2889657 10047 else
5f5c8ee5 10048 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
10049 }
10050
5f5c8ee5
GM
10051 try_to_scroll:
10052
ac90c44f
GM
10053 w->last_modified = make_number (0);
10054 w->last_overlay_modified = make_number (0);
5f5c8ee5 10055
e481f960
RS
10056 /* Redisplay the mode line. Select the buffer properly for that. */
10057 if (!update_mode_line)
10058 {
e481f960
RS
10059 update_mode_line = 1;
10060 w->update_mode_line = Qt;
10061 }
a2889657 10062
5f5c8ee5
GM
10063 /* Try to scroll by specified few lines. */
10064 if ((scroll_conservatively
10065 || scroll_step
10066 || temp_scroll_step
10067 || NUMBERP (current_buffer->scroll_up_aggressively)
10068 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 10069 && !current_buffer->clip_changed
5f5c8ee5
GM
10070 && CHARPOS (startp) >= BEGV
10071 && CHARPOS (startp) <= ZV)
0789adb2 10072 {
5f5c8ee5
GM
10073 /* The function returns -1 if new fonts were loaded, 1 if
10074 successful, 0 if not successful. */
10075 int rc = try_scrolling (window, just_this_one_p,
10076 scroll_conservatively,
10077 scroll_step,
10078 temp_scroll_step);
10079 if (rc > 0)
10080 goto done;
10081 else if (rc < 0)
504454e8 10082 goto finish_scroll_bars;
5f5c8ee5 10083 }
f9c8af06 10084
5f5c8ee5 10085 /* Finally, just choose place to start which centers point */
5936754e 10086
5f5c8ee5 10087 recenter:
44173109 10088
5f5c8ee5
GM
10089#if GLYPH_DEBUG
10090 debug_method_add (w, "recenter");
10091#endif
0789adb2 10092
5f5c8ee5 10093 /* w->vscroll = 0; */
0789adb2 10094
5f5c8ee5
GM
10095 /* Forget any previously recorded base line for line number display. */
10096 if (!current_matrix_up_to_date_p
10097 || current_buffer->clip_changed)
10098 w->base_line_number = Qnil;
10099
10100 /* Move backward half the height of the window. */
10101 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10102 it.current_y = it.last_visible_y;
3a030013 10103 move_it_vertically_backward (&it, window_box_height (w) / 2);
5f5c8ee5
GM
10104 xassert (IT_CHARPOS (it) >= BEGV);
10105
10106 /* The function move_it_vertically_backward may move over more
10107 than the specified y-distance. If it->w is small, e.g. a
10108 mini-buffer window, we may end up in front of the window's
10109 display area. Start displaying at the start of the line
10110 containing PT in this case. */
10111 if (it.current_y <= 0)
10112 {
10113 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10114 move_it_vertically (&it, 0);
10115 xassert (IT_CHARPOS (it) <= PT);
10116 it.current_y = 0;
0789adb2
RS
10117 }
10118
5f5c8ee5
GM
10119 it.current_x = it.hpos = 0;
10120
10121 /* Set startp here explicitly in case that helps avoid an infinite loop
10122 in case the window-scroll-functions functions get errors. */
10123 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10124
10125 /* Run scroll hooks. */
10126 startp = run_window_scroll_functions (window, it.current.pos);
10127
10128 /* Redisplay the window. */
10129 if (!current_matrix_up_to_date_p
10130 || windows_or_buffers_changed
10131 /* Don't use try_window_reusing_current_matrix in this case
10132 because it can have changed the buffer. */
10133 || !NILP (Vwindow_scroll_functions)
10134 || !just_this_one_p
10135 || MINI_WINDOW_P (w)
10136 || !try_window_reusing_current_matrix (w))
10137 try_window (window, startp);
10138
10139 /* If new fonts have been loaded (due to fontsets), give up. We
10140 have to start a new redisplay since we need to re-adjust glyph
10141 matrices. */
10142 if (fonts_changed_p)
504454e8 10143 goto finish_scroll_bars;
5f5c8ee5
GM
10144
10145 /* If cursor did not appear assume that the middle of the window is
10146 in the first line of the window. Do it again with the next line.
10147 (Imagine a window of height 100, displaying two lines of height
10148 60. Moving back 50 from it->last_visible_y will end in the first
10149 line.) */
10150 if (w->cursor.vpos < 0)
a2889657 10151 {
5f5c8ee5
GM
10152 if (!NILP (w->window_end_valid)
10153 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 10154 {
5f5c8ee5
GM
10155 clear_glyph_matrix (w->desired_matrix);
10156 move_it_by_lines (&it, 1, 0);
10157 try_window (window, it.current.pos);
a2889657 10158 }
5f5c8ee5 10159 else if (PT < IT_CHARPOS (it))
a2889657 10160 {
5f5c8ee5
GM
10161 clear_glyph_matrix (w->desired_matrix);
10162 move_it_by_lines (&it, -1, 0);
10163 try_window (window, it.current.pos);
10164 }
10165 else
10166 {
10167 /* Not much we can do about it. */
a2889657 10168 }
a2889657 10169 }
010494d0 10170
5f5c8ee5
GM
10171 /* Consider the following case: Window starts at BEGV, there is
10172 invisible, intangible text at BEGV, so that display starts at
10173 some point START > BEGV. It can happen that we are called with
10174 PT somewhere between BEGV and START. Try to handle that case. */
10175 if (w->cursor.vpos < 0)
835766b6 10176 {
5f5c8ee5
GM
10177 struct glyph_row *row = w->current_matrix->rows;
10178 if (row->mode_line_p)
10179 ++row;
10180 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 10181 }
5f5c8ee5
GM
10182
10183 make_cursor_line_fully_visible (w);
b5174a51 10184
74d481ac
GM
10185 done:
10186
5f5c8ee5
GM
10187 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10188 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10189 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10190 ? Qt : Qnil);
a2889657 10191
5f5c8ee5 10192 /* Display the mode line, if we must. */
e481f960 10193 if ((update_mode_line
aa6d10fa 10194 /* If window not full width, must redo its mode line
5f5c8ee5
GM
10195 if (a) the window to its side is being redone and
10196 (b) we do a frame-based redisplay. This is a consequence
10197 of how inverted lines are drawn in frame-based redisplay. */
10198 || (!just_this_one_p
10199 && !FRAME_WINDOW_P (f)
10200 && !WINDOW_FULL_WIDTH_P (w))
10201 /* Line number to display. */
155ef550 10202 || INTEGERP (w->base_line_pos)
5f5c8ee5 10203 /* Column number is displayed and different from the one displayed. */
155ef550
KH
10204 || (!NILP (w->column_number_displayed)
10205 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
10206 /* This means that the window has a mode line. */
10207 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 10208 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 10209 {
5da11938
GM
10210 Lisp_Object old_selected_frame;
10211
10212 old_selected_frame = selected_frame;
10213
5da11938 10214 XSETFRAME (selected_frame, f);
5f5c8ee5 10215 display_mode_lines (w);
5da11938 10216 selected_frame = old_selected_frame;
5f5c8ee5
GM
10217
10218 /* If mode line height has changed, arrange for a thorough
10219 immediate redisplay using the correct mode line height. */
10220 if (WINDOW_WANTS_MODELINE_P (w)
10221 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 10222 {
5f5c8ee5
GM
10223 fonts_changed_p = 1;
10224 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10225 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 10226 }
5f5c8ee5
GM
10227
10228 /* If top line height has changed, arrange for a thorough
10229 immediate redisplay using the correct mode line height. */
045dee35
GM
10230 if (WINDOW_WANTS_HEADER_LINE_P (w)
10231 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
10232 {
10233 fonts_changed_p = 1;
045dee35
GM
10234 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10235 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10236 }
10237
10238 if (fonts_changed_p)
504454e8 10239 goto finish_scroll_bars;
5ba50c51 10240 }
5f5c8ee5
GM
10241
10242 if (!line_number_displayed
10243 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
10244 {
10245 w->base_line_pos = Qnil;
10246 w->base_line_number = Qnil;
10247 }
a2889657 10248
5f5c8ee5
GM
10249 finish_menu_bars:
10250
7ce2c095 10251 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 10252 if (update_mode_line
5f5c8ee5
GM
10253 && EQ (FRAME_SELECTED_WINDOW (f), window))
10254 {
10255 int redisplay_menu_p = 0;
10256
10257 if (FRAME_WINDOW_P (f))
10258 {
1a578e9b 10259#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5 10260 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 10261#else
5f5c8ee5 10262 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 10263#endif
5f5c8ee5
GM
10264 }
10265 else
10266 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10267
10268 if (redisplay_menu_p)
10269 display_menu_bar (w);
10270
10271#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
10272 if (WINDOWP (f->tool_bar_window)
10273 && (FRAME_TOOL_BAR_LINES (f) > 0
10274 || auto_resize_tool_bars_p))
10275 redisplay_tool_bar (f);
5f5c8ee5
GM
10276#endif
10277 }
7ce2c095 10278
88f22aff 10279 finish_scroll_bars:
5f5c8ee5 10280
88f22aff 10281 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 10282 {
b1d1124b 10283 int start, end, whole;
30c566e4 10284
b1d1124b 10285 /* Calculate the start and end positions for the current window.
3505ea70
JB
10286 At some point, it would be nice to choose between scrollbars
10287 which reflect the whole buffer size, with special markers
10288 indicating narrowing, and scrollbars which reflect only the
10289 visible region.
10290
5f5c8ee5 10291 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 10292 if (!MINI_WINDOW_P (w)
5f5c8ee5 10293 || (w == XWINDOW (minibuf_window)
c6e89d6c 10294 && NILP (echo_area_buffer[0])))
b1d1124b 10295 {
8a9311d7 10296 whole = ZV - BEGV;
4d641a15 10297 start = marker_position (w->start) - BEGV;
b1d1124b
JB
10298 /* I don't think this is guaranteed to be right. For the
10299 moment, we'll pretend it is. */
5f5c8ee5 10300 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 10301
5f5c8ee5
GM
10302 if (end < start)
10303 end = start;
10304 if (whole < (end - start))
10305 whole = end - start;
b1d1124b
JB
10306 }
10307 else
10308 start = end = whole = 0;
30c566e4 10309
88f22aff 10310 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 10311 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 10312
5f5c8ee5
GM
10313 /* Note that we actually used the scroll bar attached to this
10314 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 10315 redeem_scroll_bar_hook (w);
30c566e4 10316 }
b1d1124b 10317
5f5c8ee5
GM
10318 /* Restore current_buffer and value of point in it. */
10319 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 10320 set_buffer_internal_1 (old);
5f5c8ee5 10321 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
10322
10323 unbind_to (count, Qnil);
a2889657 10324}
a2889657 10325
5f5c8ee5
GM
10326
10327/* Build the complete desired matrix of WINDOW with a window start
10328 buffer position POS. Value is non-zero if successful. It is zero
10329 if fonts were loaded during redisplay which makes re-adjusting
10330 glyph matrices necessary. */
10331
10332int
a2889657
JB
10333try_window (window, pos)
10334 Lisp_Object window;
5f5c8ee5
GM
10335 struct text_pos pos;
10336{
10337 struct window *w = XWINDOW (window);
10338 struct it it;
10339 struct glyph_row *last_text_row = NULL;
9cbab4ff 10340
5f5c8ee5
GM
10341 /* Make POS the new window start. */
10342 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 10343
5f5c8ee5
GM
10344 /* Mark cursor position as unknown. No overlay arrow seen. */
10345 w->cursor.vpos = -1;
a2889657 10346 overlay_arrow_seen = 0;
642eefc6 10347
5f5c8ee5
GM
10348 /* Initialize iterator and info to start at POS. */
10349 start_display (&it, w, pos);
a2889657 10350
5f5c8ee5
GM
10351 /* Display all lines of W. */
10352 while (it.current_y < it.last_visible_y)
10353 {
10354 if (display_line (&it))
10355 last_text_row = it.glyph_row - 1;
10356 if (fonts_changed_p)
10357 return 0;
10358 }
a2889657 10359
5f5c8ee5
GM
10360 /* If bottom moved off end of frame, change mode line percentage. */
10361 if (XFASTINT (w->window_end_pos) <= 0
10362 && Z != IT_CHARPOS (it))
a2889657
JB
10363 w->update_mode_line = Qt;
10364
5f5c8ee5
GM
10365 /* Set window_end_pos to the offset of the last character displayed
10366 on the window from the end of current_buffer. Set
10367 window_end_vpos to its row number. */
10368 if (last_text_row)
10369 {
10370 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10371 w->window_end_bytepos
10372 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10373 w->window_end_pos
10374 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10375 w->window_end_vpos
10376 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10377 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10378 ->displays_text_p);
10379 }
10380 else
10381 {
10382 w->window_end_bytepos = 0;
ac90c44f 10383 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10384 }
10385
a2889657
JB
10386 /* But that is not valid info until redisplay finishes. */
10387 w->window_end_valid = Qnil;
5f5c8ee5 10388 return 1;
a2889657 10389}
5f5c8ee5
GM
10390
10391
a2889657 10392\f
5f5c8ee5
GM
10393/************************************************************************
10394 Window redisplay reusing current matrix when buffer has not changed
10395 ************************************************************************/
10396
10397/* Try redisplay of window W showing an unchanged buffer with a
10398 different window start than the last time it was displayed by
10399 reusing its current matrix. Value is non-zero if successful.
10400 W->start is the new window start. */
a2889657
JB
10401
10402static int
5f5c8ee5
GM
10403try_window_reusing_current_matrix (w)
10404 struct window *w;
a2889657 10405{
5f5c8ee5
GM
10406 struct frame *f = XFRAME (w->frame);
10407 struct glyph_row *row, *bottom_row;
10408 struct it it;
10409 struct run run;
10410 struct text_pos start, new_start;
10411 int nrows_scrolled, i;
10412 struct glyph_row *last_text_row;
10413 struct glyph_row *last_reused_text_row;
10414 struct glyph_row *start_row;
10415 int start_vpos, min_y, max_y;
d18354b6
GM
10416
10417 if (/* This function doesn't handle terminal frames. */
10418 !FRAME_WINDOW_P (f)
10419 /* Don't try to reuse the display if windows have been split
10420 or such. */
10421 || windows_or_buffers_changed)
5f5c8ee5 10422 return 0;
a2889657 10423
5f5c8ee5
GM
10424 /* Can't do this if region may have changed. */
10425 if ((!NILP (Vtransient_mark_mode)
10426 && !NILP (current_buffer->mark_active))
8f897821
GM
10427 || !NILP (w->region_showing)
10428 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 10429 return 0;
a2889657 10430
5f5c8ee5 10431 /* If top-line visibility has changed, give up. */
045dee35
GM
10432 if (WINDOW_WANTS_HEADER_LINE_P (w)
10433 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
10434 return 0;
10435
10436 /* Give up if old or new display is scrolled vertically. We could
10437 make this function handle this, but right now it doesn't. */
10438 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10439 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10440 return 0;
10441
10442 /* The variable new_start now holds the new window start. The old
10443 start `start' can be determined from the current matrix. */
10444 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10445 start = start_row->start.pos;
10446 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 10447
5f5c8ee5
GM
10448 /* Clear the desired matrix for the display below. */
10449 clear_glyph_matrix (w->desired_matrix);
10450
10451 if (CHARPOS (new_start) <= CHARPOS (start))
10452 {
10453 int first_row_y;
10454
607ec83c
GM
10455 /* Don't use this method if the display starts with an ellipsis
10456 displayed for invisible text. It's not easy to handle that case
10457 below, and it's certainly not worth the effort since this is
10458 not a frequent case. */
10459 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10460 return 0;
10461
5f5c8ee5
GM
10462 IF_DEBUG (debug_method_add (w, "twu1"));
10463
10464 /* Display up to a row that can be reused. The variable
10465 last_text_row is set to the last row displayed that displays
b48f74cb
GM
10466 text. Note that it.vpos == 0 if or if not there is a
10467 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
10468 start_display (&it, w, new_start);
10469 first_row_y = it.current_y;
10470 w->cursor.vpos = -1;
10471 last_text_row = last_reused_text_row = NULL;
b48f74cb 10472
5f5c8ee5
GM
10473 while (it.current_y < it.last_visible_y
10474 && IT_CHARPOS (it) < CHARPOS (start)
10475 && !fonts_changed_p)
10476 if (display_line (&it))
10477 last_text_row = it.glyph_row - 1;
10478
10479 /* A value of current_y < last_visible_y means that we stopped
10480 at the previous window start, which in turn means that we
10481 have at least one reusable row. */
10482 if (it.current_y < it.last_visible_y)
a2889657 10483 {
b48f74cb 10484 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5
GM
10485 nrows_scrolled = it.vpos;
10486
10487 /* Find PT if not already found in the lines displayed. */
10488 if (w->cursor.vpos < 0)
a2889657 10489 {
5f5c8ee5
GM
10490 int dy = it.current_y - first_row_y;
10491
10492 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10493 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10494 {
4bde0ebb
GM
10495 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10496 && PT < MATRIX_ROW_END_CHARPOS (row))
5f5c8ee5
GM
10497 {
10498 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10499 dy, nrows_scrolled);
10500 break;
10501 }
10502
10503 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10504 break;
10505
10506 ++row;
10507 }
10508
10509 /* Give up if point was not found. This shouldn't
10510 happen often; not more often than with try_window
10511 itself. */
10512 if (w->cursor.vpos < 0)
10513 {
10514 clear_glyph_matrix (w->desired_matrix);
10515 return 0;
10516 }
a2889657 10517 }
5f5c8ee5
GM
10518
10519 /* Scroll the display. Do it before the current matrix is
10520 changed. The problem here is that update has not yet
10521 run, i.e. part of the current matrix is not up to date.
10522 scroll_run_hook will clear the cursor, and use the
10523 current matrix to get the height of the row the cursor is
10524 in. */
10525 run.current_y = first_row_y;
10526 run.desired_y = it.current_y;
10527 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
10528
10529 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 10530 {
5f5c8ee5
GM
10531 update_begin (f);
10532 rif->update_window_begin_hook (w);
64d1e7d3 10533 rif->clear_mouse_face (w);
5f5c8ee5 10534 rif->scroll_run_hook (w, &run);
64d1e7d3 10535 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 10536 update_end (f);
a2889657 10537 }
5f5c8ee5
GM
10538
10539 /* Shift current matrix down by nrows_scrolled lines. */
10540 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10541 rotate_matrix (w->current_matrix,
10542 start_vpos,
10543 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10544 nrows_scrolled);
10545
9c8b8382 10546 /* Disable lines that must be updated. */
5f5c8ee5 10547 for (i = 0; i < it.vpos; ++i)
b48f74cb 10548 (start_row + i)->enabled_p = 0;
9c8b8382 10549
5f5c8ee5 10550 /* Re-compute Y positions. */
045dee35 10551 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10552 max_y = it.last_visible_y;
b48f74cb
GM
10553 for (row = start_row + nrows_scrolled;
10554 row < bottom_row;
10555 ++row)
d2f84654 10556 {
5f5c8ee5
GM
10557 row->y = it.current_y;
10558
10559 if (row->y < min_y)
10560 row->visible_height = row->height - (min_y - row->y);
10561 else if (row->y + row->height > max_y)
10562 row->visible_height
10563 = row->height - (row->y + row->height - max_y);
10564 else
10565 row->visible_height = row->height;
10566
10567 it.current_y += row->height;
5f5c8ee5
GM
10568
10569 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10570 last_reused_text_row = row;
10571 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10572 break;
d2f84654 10573 }
9c8b8382
GM
10574
10575 /* Disable lines in the current matrix which are now
10576 below the window. */
bafb434c 10577 for (++row; row < bottom_row; ++row)
9c8b8382 10578 row->enabled_p = 0;
a2889657 10579 }
5f5c8ee5
GM
10580
10581 /* Update window_end_pos etc.; last_reused_text_row is the last
10582 reused row from the current matrix containing text, if any.
10583 The value of last_text_row is the last displayed line
10584 containing text. */
10585 if (last_reused_text_row)
a2889657 10586 {
5f5c8ee5
GM
10587 w->window_end_bytepos
10588 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
10589 w->window_end_pos
10590 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10591 w->window_end_vpos
10592 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10593 w->current_matrix));
a2889657 10594 }
5f5c8ee5
GM
10595 else if (last_text_row)
10596 {
10597 w->window_end_bytepos
10598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10599 w->window_end_pos
10600 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10601 w->window_end_vpos
10602 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10603 }
10604 else
10605 {
10606 /* This window must be completely empty. */
10607 w->window_end_bytepos = 0;
ac90c44f 10608 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10609 }
10610 w->window_end_valid = Qnil;
a2889657 10611
5f5c8ee5
GM
10612 /* Update hint: don't try scrolling again in update_window. */
10613 w->desired_matrix->no_scrolling_p = 1;
10614
10615#if GLYPH_DEBUG
10616 debug_method_add (w, "try_window_reusing_current_matrix 1");
10617#endif
10618 return 1;
a2889657 10619 }
5f5c8ee5
GM
10620 else if (CHARPOS (new_start) > CHARPOS (start))
10621 {
10622 struct glyph_row *pt_row, *row;
10623 struct glyph_row *first_reusable_row;
10624 struct glyph_row *first_row_to_display;
10625 int dy;
10626 int yb = window_text_bottom_y (w);
10627
5f5c8ee5
GM
10628 /* Find the row starting at new_start, if there is one. Don't
10629 reuse a partially visible line at the end. */
b48f74cb 10630 first_reusable_row = start_row;
5f5c8ee5
GM
10631 while (first_reusable_row->enabled_p
10632 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10633 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10634 < CHARPOS (new_start)))
10635 ++first_reusable_row;
10636
10637 /* Give up if there is no row to reuse. */
10638 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
10639 || !first_reusable_row->enabled_p
10640 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10641 != CHARPOS (new_start)))
5f5c8ee5
GM
10642 return 0;
10643
5f5c8ee5
GM
10644 /* We can reuse fully visible rows beginning with
10645 first_reusable_row to the end of the window. Set
10646 first_row_to_display to the first row that cannot be reused.
10647 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 10648 pt_row = NULL;
ac90c44f
GM
10649 for (first_row_to_display = first_reusable_row;
10650 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10651 ++first_row_to_display)
5f5c8ee5 10652 {
4bde0ebb
GM
10653 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10654 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 10655 pt_row = first_row_to_display;
5f5c8ee5 10656 }
a2889657 10657
5f5c8ee5
GM
10658 /* Start displaying at the start of first_row_to_display. */
10659 xassert (first_row_to_display->y < yb);
10660 init_to_row_start (&it, w, first_row_to_display);
607ec83c 10661
b48f74cb
GM
10662 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10663 - start_vpos);
5f5c8ee5
GM
10664 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10665 - nrows_scrolled);
b48f74cb
GM
10666 it.current_y = (first_row_to_display->y - first_reusable_row->y
10667 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
10668
10669 /* Display lines beginning with first_row_to_display in the
10670 desired matrix. Set last_text_row to the last row displayed
10671 that displays text. */
10672 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10673 if (pt_row == NULL)
10674 w->cursor.vpos = -1;
10675 last_text_row = NULL;
10676 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10677 if (display_line (&it))
10678 last_text_row = it.glyph_row - 1;
10679
10680 /* Give up If point isn't in a row displayed or reused. */
10681 if (w->cursor.vpos < 0)
10682 {
10683 clear_glyph_matrix (w->desired_matrix);
10684 return 0;
10685 }
12adba34 10686
5f5c8ee5
GM
10687 /* If point is in a reused row, adjust y and vpos of the cursor
10688 position. */
10689 if (pt_row)
10690 {
10691 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10692 w->current_matrix);
10693 w->cursor.y -= first_reusable_row->y;
a2889657
JB
10694 }
10695
5f5c8ee5
GM
10696 /* Scroll the display. */
10697 run.current_y = first_reusable_row->y;
045dee35 10698 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10699 run.height = it.last_visible_y - run.current_y;
4da61803
GM
10700 dy = run.current_y - run.desired_y;
10701
5f5c8ee5
GM
10702 if (run.height)
10703 {
10704 struct frame *f = XFRAME (WINDOW_FRAME (w));
10705 update_begin (f);
10706 rif->update_window_begin_hook (w);
64d1e7d3 10707 rif->clear_mouse_face (w);
5f5c8ee5 10708 rif->scroll_run_hook (w, &run);
64d1e7d3 10709 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
10710 update_end (f);
10711 }
a2889657 10712
5f5c8ee5
GM
10713 /* Adjust Y positions of reused rows. */
10714 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 10715 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10716 max_y = it.last_visible_y;
b48f74cb 10717 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
10718 {
10719 row->y -= dy;
10720 if (row->y < min_y)
10721 row->visible_height = row->height - (min_y - row->y);
10722 else if (row->y + row->height > max_y)
10723 row->visible_height
10724 = row->height - (row->y + row->height - max_y);
10725 else
10726 row->visible_height = row->height;
5f5c8ee5 10727 }
a2889657 10728
5f5c8ee5
GM
10729 /* Scroll the current matrix. */
10730 xassert (nrows_scrolled > 0);
10731 rotate_matrix (w->current_matrix,
10732 start_vpos,
10733 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10734 -nrows_scrolled);
10735
ac90c44f
GM
10736 /* Disable rows not reused. */
10737 for (row -= nrows_scrolled; row < bottom_row; ++row)
10738 row->enabled_p = 0;
10739
5f5c8ee5
GM
10740 /* Adjust window end. A null value of last_text_row means that
10741 the window end is in reused rows which in turn means that
10742 only its vpos can have changed. */
10743 if (last_text_row)
10744 {
10745 w->window_end_bytepos
10746 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10747 w->window_end_pos
10748 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10749 w->window_end_vpos
10750 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10751 }
10752 else
a2889657 10753 {
ac90c44f
GM
10754 w->window_end_vpos
10755 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 10756 }
5f5c8ee5
GM
10757
10758 w->window_end_valid = Qnil;
10759 w->desired_matrix->no_scrolling_p = 1;
10760
10761#if GLYPH_DEBUG
10762 debug_method_add (w, "try_window_reusing_current_matrix 2");
10763#endif
10764 return 1;
a2889657 10765 }
5f5c8ee5
GM
10766
10767 return 0;
10768}
a2889657 10769
a2889657 10770
5f5c8ee5
GM
10771\f
10772/************************************************************************
10773 Window redisplay reusing current matrix when buffer has changed
10774 ************************************************************************/
10775
1ec185cb
GM
10776static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10777static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
10778 int *, int *));
10779static struct glyph_row *
10780find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10781 struct glyph_row *));
10782
10783
10784/* Return the last row in MATRIX displaying text. If row START is
10785 non-null, start searching with that row. IT gives the dimensions
10786 of the display. Value is null if matrix is empty; otherwise it is
10787 a pointer to the row found. */
10788
10789static struct glyph_row *
10790find_last_row_displaying_text (matrix, it, start)
10791 struct glyph_matrix *matrix;
10792 struct it *it;
10793 struct glyph_row *start;
10794{
10795 struct glyph_row *row, *row_found;
10796
10797 /* Set row_found to the last row in IT->w's current matrix
10798 displaying text. The loop looks funny but think of partially
10799 visible lines. */
10800 row_found = NULL;
10801 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10802 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10803 {
10804 xassert (row->enabled_p);
10805 row_found = row;
10806 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10807 break;
10808 ++row;
a2889657 10809 }
5f5c8ee5
GM
10810
10811 return row_found;
10812}
10813
a2889657 10814
5f5c8ee5 10815/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
10816 by changes at the start of current_buffer that occurred since W's
10817 current matrix was built. Value is null if no such row exists.
a2889657 10818
d43fbe9d
GM
10819 BEG_UNCHANGED us the number of characters unchanged at the start of
10820 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
10821 first changed character in current_buffer. Characters at positions <
10822 BEG + BEG_UNCHANGED are at the same buffer positions as they were
10823 when the current matrix was built. */
5f5c8ee5
GM
10824
10825static struct glyph_row *
1ec185cb 10826find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
10827 struct window *w;
10828{
9142dd5b 10829 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
10830 struct glyph_row *row;
10831 struct glyph_row *row_found = NULL;
10832 int yb = window_text_bottom_y (w);
10833
10834 /* Find the last row displaying unchanged text. */
10835 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10836 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10837 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 10838 {
5f5c8ee5
GM
10839 if (/* If row ends before first_changed_pos, it is unchanged,
10840 except in some case. */
10841 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10842 /* When row ends in ZV and we write at ZV it is not
10843 unchanged. */
10844 && !row->ends_at_zv_p
10845 /* When first_changed_pos is the end of a continued line,
10846 row is not unchanged because it may be no longer
10847 continued. */
10848 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10849 && row->continued_p))
10850 row_found = row;
10851
10852 /* Stop if last visible row. */
10853 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10854 break;
10855
10856 ++row;
a2889657
JB
10857 }
10858
5f5c8ee5 10859 return row_found;
a2889657 10860}
5f5c8ee5
GM
10861
10862
10863/* Find the first glyph row in the current matrix of W that is not
d43fbe9d
GM
10864 affected by changes at the end of current_buffer since the
10865 time W's current matrix was built.
10866
10867 Return in *DELTA the number of chars by which buffer positions in
10868 unchanged text at the end of current_buffer must be adjusted.
10869
10870 Return in *DELTA_BYTES the corresponding number of bytes.
10871
10872 Value is null if no such row exists, i.e. all rows are affected by
10873 changes. */
5f5c8ee5
GM
10874
10875static struct glyph_row *
1ec185cb 10876find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
10877 struct window *w;
10878 int *delta, *delta_bytes;
a2889657 10879{
5f5c8ee5
GM
10880 struct glyph_row *row;
10881 struct glyph_row *row_found = NULL;
c581d710 10882
5f5c8ee5 10883 *delta = *delta_bytes = 0;
b2a76982 10884
1ec185cb
GM
10885 /* Display must not have been paused, otherwise the current matrix
10886 is not up to date. */
10887 if (NILP (w->window_end_valid))
10888 abort ();
10889
10890 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
10891 end is in the range of changed text. If so, there is no
10892 unchanged row at the end of W's current matrix. */
9142dd5b 10893 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
10894 return NULL;
10895
10896 /* Set row to the last row in W's current matrix displaying text. */
10897 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10898
5f5c8ee5
GM
10899 /* If matrix is entirely empty, no unchanged row exists. */
10900 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10901 {
10902 /* The value of row is the last glyph row in the matrix having a
10903 meaningful buffer position in it. The end position of row
10904 corresponds to window_end_pos. This allows us to translate
10905 buffer positions in the current matrix to current buffer
10906 positions for characters not in changed text. */
10907 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10908 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10909 int last_unchanged_pos, last_unchanged_pos_old;
10910 struct glyph_row *first_text_row
10911 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10912
10913 *delta = Z - Z_old;
10914 *delta_bytes = Z_BYTE - Z_BYTE_old;
10915
10916 /* Set last_unchanged_pos to the buffer position of the last
10917 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
10918 index + 1 of the last character in current_buffer, i.e. by
10919 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
10920 unchanged character, and we have to add BEG to get its buffer
10921 position. */
9142dd5b 10922 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
10923 last_unchanged_pos_old = last_unchanged_pos - *delta;
10924
10925 /* Search backward from ROW for a row displaying a line that
10926 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 10927 for (; row > first_text_row; --row)
5f5c8ee5 10928 {
1ec185cb
GM
10929 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10930 abort ();
5f5c8ee5
GM
10931
10932 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10933 row_found = row;
5f5c8ee5
GM
10934 }
10935 }
10936
1ec185cb
GM
10937 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10938 abort ();
10939
5f5c8ee5 10940 return row_found;
c581d710
RS
10941}
10942
c581d710 10943
5f5c8ee5
GM
10944/* Make sure that glyph rows in the current matrix of window W
10945 reference the same glyph memory as corresponding rows in the
10946 frame's frame matrix. This function is called after scrolling W's
10947 current matrix on a terminal frame in try_window_id and
10948 try_window_reusing_current_matrix. */
10949
10950static void
10951sync_frame_with_window_matrix_rows (w)
10952 struct window *w;
c581d710 10953{
5f5c8ee5
GM
10954 struct frame *f = XFRAME (w->frame);
10955 struct glyph_row *window_row, *window_row_end, *frame_row;
10956
10957 /* Preconditions: W must be a leaf window and full-width. Its frame
10958 must have a frame matrix. */
10959 xassert (NILP (w->hchild) && NILP (w->vchild));
10960 xassert (WINDOW_FULL_WIDTH_P (w));
10961 xassert (!FRAME_WINDOW_P (f));
10962
10963 /* If W is a full-width window, glyph pointers in W's current matrix
10964 have, by definition, to be the same as glyph pointers in the
10965 corresponding frame matrix. */
10966 window_row = w->current_matrix->rows;
10967 window_row_end = window_row + w->current_matrix->nrows;
10968 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10969 while (window_row < window_row_end)
659a218f 10970 {
5f5c8ee5 10971 int area;
f002db93 10972
5f5c8ee5
GM
10973 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10974 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
10975
10976 /* Disable frame rows whose corresponding window rows have
10977 been disabled in try_window_id. */
10978 if (!window_row->enabled_p)
10979 frame_row->enabled_p = 0;
10980
5f5c8ee5 10981 ++window_row, ++frame_row;
659a218f 10982 }
a2889657 10983}
5f5c8ee5
GM
10984
10985
e037b9ec
GM
10986/* Find the glyph row in window W containing CHARPOS. Consider all
10987 rows between START and END (not inclusive). END null means search
10988 all rows to the end of the display area of W. Value is the row
10989 containing CHARPOS or null. */
10990
10991static struct glyph_row *
10992row_containing_pos (w, charpos, start, end)
10993 struct window *w;
10994 int charpos;
10995 struct glyph_row *start, *end;
10996{
10997 struct glyph_row *row = start;
10998 int last_y;
10999
11000 /* If we happen to start on a header-line, skip that. */
11001 if (row->mode_line_p)
11002 ++row;
11003
11004 if ((end && row >= end) || !row->enabled_p)
11005 return NULL;
11006
11007 last_y = window_text_bottom_y (w);
11008
11009 while ((end == NULL || row < end)
11010 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11011 /* The end position of a row equals the start
11012 position of the next row. If CHARPOS is there, we
11013 would rather display it in the next line, except
11014 when this line ends in ZV. */
11015 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11016 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11017 || !row->ends_at_zv_p)))
11018 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11019 ++row;
11020
11021 /* Give up if CHARPOS not found. */
11022 if ((end && row >= end)
11023 || charpos < MATRIX_ROW_START_CHARPOS (row)
11024 || charpos > MATRIX_ROW_END_CHARPOS (row))
11025 row = NULL;
11026
11027 return row;
11028}
11029
11030
5f5c8ee5
GM
11031/* Try to redisplay window W by reusing its existing display. W's
11032 current matrix must be up to date when this function is called,
11033 i.e. window_end_valid must not be nil.
11034
11035 Value is
11036
11037 1 if display has been updated
11038 0 if otherwise unsuccessful
11039 -1 if redisplay with same window start is known not to succeed
11040
11041 The following steps are performed:
11042
11043 1. Find the last row in the current matrix of W that is not
11044 affected by changes at the start of current_buffer. If no such row
11045 is found, give up.
11046
11047 2. Find the first row in W's current matrix that is not affected by
11048 changes at the end of current_buffer. Maybe there is no such row.
11049
11050 3. Display lines beginning with the row + 1 found in step 1 to the
11051 row found in step 2 or, if step 2 didn't find a row, to the end of
11052 the window.
11053
11054 4. If cursor is not known to appear on the window, give up.
11055
11056 5. If display stopped at the row found in step 2, scroll the
11057 display and current matrix as needed.
11058
11059 6. Maybe display some lines at the end of W, if we must. This can
11060 happen under various circumstances, like a partially visible line
11061 becoming fully visible, or because newly displayed lines are displayed
11062 in smaller font sizes.
11063
11064 7. Update W's window end information. */
11065
12adba34 11066static int
5f5c8ee5 11067try_window_id (w)
12adba34 11068 struct window *w;
12adba34 11069{
5f5c8ee5
GM
11070 struct frame *f = XFRAME (w->frame);
11071 struct glyph_matrix *current_matrix = w->current_matrix;
11072 struct glyph_matrix *desired_matrix = w->desired_matrix;
11073 struct glyph_row *last_unchanged_at_beg_row;
11074 struct glyph_row *first_unchanged_at_end_row;
11075 struct glyph_row *row;
11076 struct glyph_row *bottom_row;
11077 int bottom_vpos;
11078 struct it it;
11079 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11080 struct text_pos start_pos;
11081 struct run run;
11082 int first_unchanged_at_end_vpos = 0;
11083 struct glyph_row *last_text_row, *last_text_row_at_end;
11084 struct text_pos start;
27d16f05 11085 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 11086
27d16f05
GM
11087 /* This is handy for debugging. */
11088#if 0
11089#define GIVE_UP(X) \
11090 do { \
11091 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11092 return 0; \
11093 } while (0)
11094#else
62397849 11095#define GIVE_UP(X) return 0
27d16f05
GM
11096#endif
11097
5f5c8ee5
GM
11098 SET_TEXT_POS_FROM_MARKER (start, w->start);
11099
27d16f05
GM
11100 /* Don't use this for mini-windows because these can show
11101 messages and mini-buffers, and we don't handle that here. */
11102 if (MINI_WINDOW_P (w))
11103 GIVE_UP (1);
11104
11105 /* This flag is used to prevent redisplay optimizations. */
11106 if (windows_or_buffers_changed)
11107 GIVE_UP (2);
11108
11109 /* Narrowing has not changed. This flag is also set to prevent
11110 redisplay optimizations. It would be nice to further
11111 reduce the number of cases where this prevents try_window_id. */
11112 if (current_buffer->clip_changed)
11113 GIVE_UP (3);
11114
11115 /* Window must either use window-based redisplay or be full width. */
11116 if (!FRAME_WINDOW_P (f)
11117 && (!line_ins_del_ok
11118 || !WINDOW_FULL_WIDTH_P (w)))
11119 GIVE_UP (4);
5f5c8ee5 11120
27d16f05
GM
11121 /* Point is not known NOT to appear in W. */
11122 if (PT < CHARPOS (start))
11123 GIVE_UP (5);
11124
11125 /* Another way to prevent redisplay optimizations. */
11126 if (XFASTINT (w->last_modified) == 0)
11127 GIVE_UP (6);
11128
11129 /* Window is not hscrolled. */
11130 if (XFASTINT (w->hscroll) != 0)
11131 GIVE_UP (7);
11132
11133 /* Display wasn't paused. */
11134 if (NILP (w->window_end_valid))
11135 GIVE_UP (8);
11136
11137 /* Can't use this if highlighting a region because a cursor movement
11138 will do more than just set the cursor. */
11139 if (!NILP (Vtransient_mark_mode)
11140 && !NILP (current_buffer->mark_active))
11141 GIVE_UP (9);
11142
11143 /* Likewise if highlighting trailing whitespace. */
11144 if (!NILP (Vshow_trailing_whitespace))
11145 GIVE_UP (11);
11146
11147 /* Likewise if showing a region. */
11148 if (!NILP (w->region_showing))
11149 GIVE_UP (10);
11150
11151 /* Can use this if overlay arrow position and or string have changed. */
11152 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11153 || !EQ (last_arrow_string, Voverlay_arrow_string))
11154 GIVE_UP (12);
11155
11156
5f5c8ee5
GM
11157 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11158 only if buffer has really changed. The reason is that the gap is
11159 initially at Z for freshly visited files. The code below would
11160 set end_unchanged to 0 in that case. */
28ee91c0
GM
11161 if (MODIFF > SAVE_MODIFF
11162 /* This seems to happen sometimes after saving a buffer. */
11163 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 11164 {
9142dd5b
GM
11165 if (GPT - BEG < BEG_UNCHANGED)
11166 BEG_UNCHANGED = GPT - BEG;
11167 if (Z - GPT < END_UNCHANGED)
11168 END_UNCHANGED = Z - GPT;
5f5c8ee5 11169 }
bf9249e3 11170
27d16f05
GM
11171 /* The position of the first and last character that has been changed. */
11172 first_changed_charpos = BEG + BEG_UNCHANGED;
11173 last_changed_charpos = Z - END_UNCHANGED;
11174
5f5c8ee5
GM
11175 /* If window starts after a line end, and the last change is in
11176 front of that newline, then changes don't affect the display.
f2d86d7a
GM
11177 This case happens with stealth-fontification. Note that although
11178 the display is unchanged, glyph positions in the matrix have to
11179 be adjusted, of course. */
5f5c8ee5 11180 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 11181 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 11182 && ((last_changed_charpos < CHARPOS (start)
27d16f05 11183 && CHARPOS (start) == BEGV)
e60f4527 11184 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
11185 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11186 {
11187 int Z_old, delta, Z_BYTE_old, delta_bytes;
11188 struct glyph_row *r0;
11189
11190 /* Compute how many chars/bytes have been added to or removed
11191 from the buffer. */
11192 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11193 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11194 delta = Z - Z_old;
11195 delta_bytes = Z_BYTE - Z_BYTE_old;
11196
11197 /* Give up if PT is not in the window. Note that it already has
11198 been checked at the start of try_window_id that PT is not in
11199 front of the window start. */
11200 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11201 GIVE_UP (13);
11202
11203 /* If window start is unchanged, we can reuse the whole matrix
11204 as is, after adjusting glyph positions. No need to compute
11205 the window end again, since its offset from Z hasn't changed. */
11206 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11207 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11208 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11209 {
11210 /* Adjust positions in the glyph matrix. */
11211 if (delta || delta_bytes)
11212 {
11213 struct glyph_row *r1
11214 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11215 increment_matrix_positions (w->current_matrix,
11216 MATRIX_ROW_VPOS (r0, current_matrix),
11217 MATRIX_ROW_VPOS (r1, current_matrix),
11218 delta, delta_bytes);
11219 }
e5959a9a 11220
27d16f05
GM
11221 /* Set the cursor. */
11222 row = row_containing_pos (w, PT, r0, NULL);
11223 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11224 return 1;
11225 }
5f5c8ee5
GM
11226 }
11227
27d16f05
GM
11228 /* Handle the case that changes are all below what is displayed in
11229 the window, and that PT is in the window. */
11230 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row))
5f5c8ee5 11231 {
27d16f05 11232 struct glyph_row *r0;
ef121659 11233
27d16f05
GM
11234 /* Give up if PT is not in the window. Note that it already has
11235 been checked at the start of try_window_id that PT is not in
11236 front of the window start. */
11237 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11238 GIVE_UP (14);
11239
11240 /* If window start is unchanged, we can reuse the whole matrix
11241 as is, without changing glyph positions since no text has
11242 been added/removed in front of the window end. */
11243 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11244 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11245 {
11246 /* We have to compute the window end anew since text
11247 can have been added/removed after it. */
11248 w->window_end_pos
11249 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11250 w->window_end_bytepos
11251 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11252
11253 /* Set the cursor. */
11254 row = row_containing_pos (w, PT, r0, NULL);
11255 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11256 return 2;
11257 }
5f5c8ee5
GM
11258 }
11259
11260 /* Check that window start agrees with the start of the first glyph
11261 row in its current matrix. Check this after we know the window
11262 start is not in changed text, otherwise positions would not be
11263 comparable. */
27d16f05
GM
11264 if (BEG_UNCHANGED + END_UNCHANGED != Z - BEG
11265 && CHARPOS (start) >= first_changed_charpos
11266 && CHARPOS (start) <= last_changed_charpos)
11267 GIVE_UP (15);
11268
11269 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 11270 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 11271 GIVE_UP (16);
5f5c8ee5 11272
5f5c8ee5
GM
11273 /* Compute the position at which we have to start displaying new
11274 lines. Some of the lines at the top of the window might be
11275 reusable because they are not displaying changed text. Find the
11276 last row in W's current matrix not affected by changes at the
11277 start of current_buffer. Value is null if changes start in the
11278 first line of window. */
1ec185cb 11279 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
11280 if (last_unchanged_at_beg_row)
11281 {
67f1cf4c
GM
11282 /* Avoid starting to display in the moddle of a character, a TAB
11283 for instance. This is easier than to set up the iterator
11284 exactly, and it's not a frequent case, so the additional
11285 effort wouldn't really pay off. */
11286 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11287 && last_unchanged_at_beg_row > w->current_matrix->rows)
11288 --last_unchanged_at_beg_row;
11289
11290 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 11291 GIVE_UP (17);
67f1cf4c 11292
5f5c8ee5
GM
11293 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11294 start_pos = it.current.pos;
11295
11296 /* Start displaying new lines in the desired matrix at the same
11297 vpos we would use in the current matrix, i.e. below
11298 last_unchanged_at_beg_row. */
11299 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11300 current_matrix);
11301 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11302 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11303
11304 xassert (it.hpos == 0 && it.current_x == 0);
11305 }
11306 else
11307 {
11308 /* There are no reusable lines at the start of the window.
11309 Start displaying in the first line. */
11310 start_display (&it, w, start);
11311 start_pos = it.current.pos;
11312 }
11313
5f5c8ee5
GM
11314 /* Find the first row that is not affected by changes at the end of
11315 the buffer. Value will be null if there is no unchanged row, in
11316 which case we must redisplay to the end of the window. delta
11317 will be set to the value by which buffer positions beginning with
11318 first_unchanged_at_end_row have to be adjusted due to text
11319 changes. */
11320 first_unchanged_at_end_row
1ec185cb 11321 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
11322 IF_DEBUG (debug_delta = delta);
11323 IF_DEBUG (debug_delta_bytes = delta_bytes);
11324
11325 /* Set stop_pos to the buffer position up to which we will have to
11326 display new lines. If first_unchanged_at_end_row != NULL, this
11327 is the buffer position of the start of the line displayed in that
11328 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11329 that we don't stop at a buffer position. */
11330 stop_pos = 0;
11331 if (first_unchanged_at_end_row)
11332 {
11333 xassert (last_unchanged_at_beg_row == NULL
11334 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11335
11336 /* If this is a continuation line, move forward to the next one
11337 that isn't. Changes in lines above affect this line.
11338 Caution: this may move first_unchanged_at_end_row to a row
11339 not displaying text. */
11340 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11341 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11342 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11343 < it.last_visible_y))
11344 ++first_unchanged_at_end_row;
11345
11346 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11347 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11348 >= it.last_visible_y))
11349 first_unchanged_at_end_row = NULL;
11350 else
11351 {
11352 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11353 + delta);
11354 first_unchanged_at_end_vpos
11355 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 11356 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
11357 }
11358 }
11359 else if (last_unchanged_at_beg_row == NULL)
27d16f05 11360 GIVE_UP (18);
5f5c8ee5
GM
11361
11362
11363#if GLYPH_DEBUG
11364
11365 /* Either there is no unchanged row at the end, or the one we have
11366 now displays text. This is a necessary condition for the window
11367 end pos calculation at the end of this function. */
11368 xassert (first_unchanged_at_end_row == NULL
11369 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11370
11371 debug_last_unchanged_at_beg_vpos
11372 = (last_unchanged_at_beg_row
11373 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11374 : -1);
11375 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11376
11377#endif /* GLYPH_DEBUG != 0 */
11378
11379
11380 /* Display new lines. Set last_text_row to the last new line
11381 displayed which has text on it, i.e. might end up as being the
11382 line where the window_end_vpos is. */
11383 w->cursor.vpos = -1;
11384 last_text_row = NULL;
11385 overlay_arrow_seen = 0;
11386 while (it.current_y < it.last_visible_y
11387 && !fonts_changed_p
11388 && (first_unchanged_at_end_row == NULL
11389 || IT_CHARPOS (it) < stop_pos))
11390 {
11391 if (display_line (&it))
11392 last_text_row = it.glyph_row - 1;
11393 }
11394
11395 if (fonts_changed_p)
11396 return -1;
11397
11398
11399 /* Compute differences in buffer positions, y-positions etc. for
11400 lines reused at the bottom of the window. Compute what we can
11401 scroll. */
ca42b2e8
GM
11402 if (first_unchanged_at_end_row
11403 /* No lines reused because we displayed everything up to the
11404 bottom of the window. */
11405 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
11406 {
11407 dvpos = (it.vpos
11408 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11409 current_matrix));
11410 dy = it.current_y - first_unchanged_at_end_row->y;
11411 run.current_y = first_unchanged_at_end_row->y;
11412 run.desired_y = run.current_y + dy;
11413 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11414 }
11415 else
ca42b2e8
GM
11416 {
11417 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11418 first_unchanged_at_end_row = NULL;
11419 }
5f5c8ee5
GM
11420 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11421
8f8ba186 11422
5f5c8ee5
GM
11423 /* Find the cursor if not already found. We have to decide whether
11424 PT will appear on this window (it sometimes doesn't, but this is
11425 not a very frequent case.) This decision has to be made before
11426 the current matrix is altered. A value of cursor.vpos < 0 means
11427 that PT is either in one of the lines beginning at
11428 first_unchanged_at_end_row or below the window. Don't care for
11429 lines that might be displayed later at the window end; as
11430 mentioned, this is not a frequent case. */
11431 if (w->cursor.vpos < 0)
11432 {
5f5c8ee5
GM
11433 /* Cursor in unchanged rows at the top? */
11434 if (PT < CHARPOS (start_pos)
11435 && last_unchanged_at_beg_row)
11436 {
e037b9ec
GM
11437 row = row_containing_pos (w, PT,
11438 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11439 last_unchanged_at_beg_row + 1);
bfe0ee88
GM
11440 if (row)
11441 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
11442 }
11443
11444 /* Start from first_unchanged_at_end_row looking for PT. */
11445 else if (first_unchanged_at_end_row)
11446 {
e037b9ec
GM
11447 row = row_containing_pos (w, PT - delta,
11448 first_unchanged_at_end_row, NULL);
11449 if (row)
468155d7
GM
11450 set_cursor_from_row (w, row, w->current_matrix, delta,
11451 delta_bytes, dy, dvpos);
5f5c8ee5
GM
11452 }
11453
11454 /* Give up if cursor was not found. */
11455 if (w->cursor.vpos < 0)
11456 {
11457 clear_glyph_matrix (w->desired_matrix);
11458 return -1;
11459 }
11460 }
11461
11462 /* Don't let the cursor end in the scroll margins. */
11463 {
11464 int this_scroll_margin, cursor_height;
11465
11466 this_scroll_margin = max (0, scroll_margin);
11467 this_scroll_margin = min (this_scroll_margin,
11468 XFASTINT (w->height) / 4);
11469 this_scroll_margin *= CANON_Y_UNIT (it.f);
11470 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11471
11472 if ((w->cursor.y < this_scroll_margin
11473 && CHARPOS (start) > BEGV)
11474 /* Don't take scroll margin into account at the bottom because
11475 old redisplay didn't do it either. */
11476 || w->cursor.y + cursor_height > it.last_visible_y)
11477 {
11478 w->cursor.vpos = -1;
11479 clear_glyph_matrix (w->desired_matrix);
11480 return -1;
11481 }
11482 }
11483
11484 /* Scroll the display. Do it before changing the current matrix so
11485 that xterm.c doesn't get confused about where the cursor glyph is
11486 found. */
fa77249f 11487 if (dy && run.height)
5f5c8ee5
GM
11488 {
11489 update_begin (f);
11490
11491 if (FRAME_WINDOW_P (f))
11492 {
11493 rif->update_window_begin_hook (w);
64d1e7d3 11494 rif->clear_mouse_face (w);
5f5c8ee5 11495 rif->scroll_run_hook (w, &run);
64d1e7d3 11496 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11497 }
11498 else
11499 {
11500 /* Terminal frame. In this case, dvpos gives the number of
11501 lines to scroll by; dvpos < 0 means scroll up. */
11502 int first_unchanged_at_end_vpos
11503 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11504 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
522ed7fb
GM
11505 int end = (XFASTINT (w->top)
11506 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11507 + window_internal_height (w));
5f5c8ee5
GM
11508
11509 /* Perform the operation on the screen. */
11510 if (dvpos > 0)
11511 {
11512 /* Scroll last_unchanged_at_beg_row to the end of the
11513 window down dvpos lines. */
11514 set_terminal_window (end);
11515
11516 /* On dumb terminals delete dvpos lines at the end
11517 before inserting dvpos empty lines. */
11518 if (!scroll_region_ok)
11519 ins_del_lines (end - dvpos, -dvpos);
11520
11521 /* Insert dvpos empty lines in front of
11522 last_unchanged_at_beg_row. */
11523 ins_del_lines (from, dvpos);
11524 }
11525 else if (dvpos < 0)
11526 {
11527 /* Scroll up last_unchanged_at_beg_vpos to the end of
11528 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11529 set_terminal_window (end);
11530
11531 /* Delete dvpos lines in front of
11532 last_unchanged_at_beg_vpos. ins_del_lines will set
11533 the cursor to the given vpos and emit |dvpos| delete
11534 line sequences. */
11535 ins_del_lines (from + dvpos, dvpos);
11536
11537 /* On a dumb terminal insert dvpos empty lines at the
11538 end. */
11539 if (!scroll_region_ok)
11540 ins_del_lines (end + dvpos, -dvpos);
11541 }
11542
11543 set_terminal_window (0);
11544 }
11545
11546 update_end (f);
11547 }
11548
ca42b2e8
GM
11549 /* Shift reused rows of the current matrix to the right position.
11550 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11551 text. */
11552 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11553 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
11554 if (dvpos < 0)
11555 {
11556 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11557 bottom_vpos, dvpos);
11558 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11559 bottom_vpos, 0);
11560 }
11561 else if (dvpos > 0)
11562 {
11563 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11564 bottom_vpos, dvpos);
11565 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11566 first_unchanged_at_end_vpos + dvpos, 0);
11567 }
11568
11569 /* For frame-based redisplay, make sure that current frame and window
11570 matrix are in sync with respect to glyph memory. */
11571 if (!FRAME_WINDOW_P (f))
11572 sync_frame_with_window_matrix_rows (w);
11573
11574 /* Adjust buffer positions in reused rows. */
11575 if (delta)
f2d86d7a
GM
11576 increment_matrix_positions (current_matrix,
11577 first_unchanged_at_end_vpos + dvpos,
11578 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
11579
11580 /* Adjust Y positions. */
11581 if (dy)
11582 shift_glyph_matrix (w, current_matrix,
11583 first_unchanged_at_end_vpos + dvpos,
11584 bottom_vpos, dy);
11585
11586 if (first_unchanged_at_end_row)
11587 first_unchanged_at_end_row += dvpos;
11588
11589 /* If scrolling up, there may be some lines to display at the end of
11590 the window. */
11591 last_text_row_at_end = NULL;
11592 if (dy < 0)
11593 {
11594 /* Set last_row to the glyph row in the current matrix where the
11595 window end line is found. It has been moved up or down in
11596 the matrix by dvpos. */
11597 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11598 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11599
11600 /* If last_row is the window end line, it should display text. */
11601 xassert (last_row->displays_text_p);
11602
11603 /* If window end line was partially visible before, begin
11604 displaying at that line. Otherwise begin displaying with the
11605 line following it. */
11606 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11607 {
11608 init_to_row_start (&it, w, last_row);
11609 it.vpos = last_vpos;
11610 it.current_y = last_row->y;
11611 }
11612 else
11613 {
11614 init_to_row_end (&it, w, last_row);
11615 it.vpos = 1 + last_vpos;
11616 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11617 ++last_row;
11618 }
12adba34 11619
5f5c8ee5
GM
11620 /* We may start in a continuation line. If so, we have to get
11621 the right continuation_lines_width and current_x. */
11622 it.continuation_lines_width = last_row->continuation_lines_width;
11623 it.hpos = it.current_x = 0;
11624
11625 /* Display the rest of the lines at the window end. */
11626 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11627 while (it.current_y < it.last_visible_y
11628 && !fonts_changed_p)
11629 {
11630 /* Is it always sure that the display agrees with lines in
11631 the current matrix? I don't think so, so we mark rows
11632 displayed invalid in the current matrix by setting their
11633 enabled_p flag to zero. */
11634 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11635 if (display_line (&it))
11636 last_text_row_at_end = it.glyph_row - 1;
11637 }
11638 }
12adba34 11639
5f5c8ee5
GM
11640 /* Update window_end_pos and window_end_vpos. */
11641 if (first_unchanged_at_end_row
11642 && first_unchanged_at_end_row->y < it.last_visible_y
11643 && !last_text_row_at_end)
11644 {
11645 /* Window end line if one of the preserved rows from the current
11646 matrix. Set row to the last row displaying text in current
11647 matrix starting at first_unchanged_at_end_row, after
11648 scrolling. */
11649 xassert (first_unchanged_at_end_row->displays_text_p);
11650 row = find_last_row_displaying_text (w->current_matrix, &it,
11651 first_unchanged_at_end_row);
11652 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11653
ac90c44f 11654 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 11655 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
11656 w->window_end_vpos
11657 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 11658 xassert (w->window_end_bytepos >= 0);
0416532f 11659 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
11660 }
11661 else if (last_text_row_at_end)
11662 {
ac90c44f
GM
11663 w->window_end_pos
11664 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
11665 w->window_end_bytepos
11666 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
11667 w->window_end_vpos
11668 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 11669 xassert (w->window_end_bytepos >= 0);
0416532f 11670 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
11671 }
11672 else if (last_text_row)
11673 {
11674 /* We have displayed either to the end of the window or at the
11675 end of the window, i.e. the last row with text is to be found
11676 in the desired matrix. */
ac90c44f
GM
11677 w->window_end_pos
11678 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
11679 w->window_end_bytepos
11680 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11681 w->window_end_vpos
11682 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 11683 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
11684 }
11685 else if (first_unchanged_at_end_row == NULL
11686 && last_text_row == NULL
11687 && last_text_row_at_end == NULL)
11688 {
11689 /* Displayed to end of window, but no line containing text was
11690 displayed. Lines were deleted at the end of the window. */
0416532f
GM
11691 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11692 int vpos = XFASTINT (w->window_end_vpos);
11693 struct glyph_row *current_row = current_matrix->rows + vpos;
11694 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11695
11696 for (row = NULL;
11697 row == NULL && vpos >= first_vpos;
11698 --vpos, --current_row, --desired_row)
11699 {
11700 if (desired_row->enabled_p)
11701 {
11702 if (desired_row->displays_text_p)
11703 row = desired_row;
11704 }
11705 else if (current_row->displays_text_p)
11706 row = current_row;
11707 }
12adba34 11708
0416532f 11709 xassert (row != NULL);
d88a79d4 11710 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
11711 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11712 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 11713 xassert (w->window_end_bytepos >= 0);
0416532f 11714 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
11715 }
11716 else
11717 abort ();
ac90c44f 11718
8cdb267e
GM
11719#if 0 /* This leads to problems, for instance when the cursor is
11720 at ZV, and the cursor line displays no text. */
ac90c44f
GM
11721 /* Disable rows below what's displayed in the window. This makes
11722 debugging easier. */
11723 enable_glyph_matrix_rows (current_matrix,
11724 XFASTINT (w->window_end_vpos) + 1,
11725 bottom_vpos, 0);
8cdb267e 11726#endif
5f5c8ee5
GM
11727
11728 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11729 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 11730
5f5c8ee5
GM
11731 /* Record that display has not been completed. */
11732 w->window_end_valid = Qnil;
11733 w->desired_matrix->no_scrolling_p = 1;
ef121659 11734 return 3;
27d16f05
GM
11735
11736#undef GIVE_UP
12adba34 11737}
0f9c0ff0 11738
a2889657 11739
5f5c8ee5
GM
11740\f
11741/***********************************************************************
11742 More debugging support
11743 ***********************************************************************/
a2889657 11744
5f5c8ee5 11745#if GLYPH_DEBUG
a2889657 11746
9ab436b9
GM
11747void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11748void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 11749void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 11750
31b24551 11751
9ab436b9 11752/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 11753
9ab436b9
GM
11754 GLYPHS 0 means don't show glyph contents.
11755 GLYPHS 1 means show glyphs in short form
11756 GLYPHS > 1 means show glyphs in long form. */
11757
11758void
11759dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 11760 struct glyph_matrix *matrix;
9ab436b9 11761 int glyphs;
5f5c8ee5 11762{
efc63ef0 11763 int i;
5f5c8ee5 11764 for (i = 0; i < matrix->nrows; ++i)
9ab436b9 11765 dump_glyph_row (matrix, i, glyphs);
5f5c8ee5 11766}
31b24551 11767
68a37fa8 11768
e187cf71
GM
11769/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11770 the glyph row and area where the glyph comes from. */
11771
11772void
11773dump_glyph (row, glyph, area)
11774 struct glyph_row *row;
11775 struct glyph *glyph;
11776 int area;
11777{
11778 if (glyph->type == CHAR_GLYPH)
11779 {
11780 fprintf (stderr,
11781 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11782 glyph - row->glyphs[TEXT_AREA],
11783 'C',
11784 glyph->charpos,
11785 (BUFFERP (glyph->object)
11786 ? 'B'
11787 : (STRINGP (glyph->object)
11788 ? 'S'
11789 : '-')),
11790 glyph->pixel_width,
11791 glyph->u.ch,
11792 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11793 ? glyph->u.ch
11794 : '.'),
11795 glyph->face_id,
11796 glyph->left_box_line_p,
11797 glyph->right_box_line_p);
11798 }
11799 else if (glyph->type == STRETCH_GLYPH)
11800 {
11801 fprintf (stderr,
11802 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11803 glyph - row->glyphs[TEXT_AREA],
11804 'S',
11805 glyph->charpos,
11806 (BUFFERP (glyph->object)
11807 ? 'B'
11808 : (STRINGP (glyph->object)
11809 ? 'S'
11810 : '-')),
11811 glyph->pixel_width,
11812 0,
11813 '.',
11814 glyph->face_id,
11815 glyph->left_box_line_p,
11816 glyph->right_box_line_p);
11817 }
11818 else if (glyph->type == IMAGE_GLYPH)
11819 {
11820 fprintf (stderr,
11821 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11822 glyph - row->glyphs[TEXT_AREA],
11823 'I',
11824 glyph->charpos,
11825 (BUFFERP (glyph->object)
11826 ? 'B'
11827 : (STRINGP (glyph->object)
11828 ? 'S'
11829 : '-')),
11830 glyph->pixel_width,
11831 glyph->u.img_id,
11832 '.',
11833 glyph->face_id,
11834 glyph->left_box_line_p,
11835 glyph->right_box_line_p);
11836 }
11837}
11838
11839
5f5c8ee5 11840/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
11841 GLYPHS 0 means don't show glyph contents.
11842 GLYPHS 1 means show glyphs in short form
11843 GLYPHS > 1 means show glyphs in long form. */
a2889657 11844
5f5c8ee5 11845void
9ab436b9 11846dump_glyph_row (matrix, vpos, glyphs)
5f5c8ee5 11847 struct glyph_matrix *matrix;
9ab436b9 11848 int vpos, glyphs;
5f5c8ee5
GM
11849{
11850 struct glyph_row *row;
11851
11852 if (vpos < 0 || vpos >= matrix->nrows)
11853 return;
11854
11855 row = MATRIX_ROW (matrix, vpos);
9ab436b9
GM
11856
11857 if (glyphs != 1)
11858 {
b94c0d9c 11859 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 11860 fprintf (stderr, "=======================================================================\n");
5f5c8ee5 11861
9ab436b9 11862 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
b94c0d9c 11863%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
9ab436b9
GM
11864 row - matrix->rows,
11865 MATRIX_ROW_START_CHARPOS (row),
11866 MATRIX_ROW_END_CHARPOS (row),
11867 row->used[TEXT_AREA],
11868 row->contains_overlapping_glyphs_p,
11869 row->enabled_p,
11870 row->inverse_p,
11871 row->truncated_on_left_p,
11872 row->truncated_on_right_p,
11873 row->overlay_arrow_p,
11874 row->continued_p,
11875 MATRIX_ROW_CONTINUATION_LINE_P (row),
11876 row->displays_text_p,
11877 row->ends_at_zv_p,
11878 row->fill_line_p,
11879 row->ends_in_middle_of_char_p,
11880 row->starts_in_middle_of_char_p,
b94c0d9c 11881 row->mouse_face_p,
9ab436b9
GM
11882 row->x,
11883 row->y,
11884 row->pixel_width,
11885 row->height,
11886 row->visible_height,
11887 row->ascent,
11888 row->phys_ascent);
11889 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11890 row->end.overlay_string_index);
11891 fprintf (stderr, "%9d %5d\n",
11892 CHARPOS (row->start.string_pos),
11893 CHARPOS (row->end.string_pos));
11894 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11895 row->end.dpvec_index);
11896 }
5f5c8ee5 11897
9ab436b9 11898 if (glyphs > 1)
bd66d1ba 11899 {
e187cf71
GM
11900 int area;
11901
11902 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11903 {
091f8878
GM
11904 struct glyph *glyph = row->glyphs[area];
11905 struct glyph *glyph_end = glyph + row->used[area];
5f5c8ee5 11906
e187cf71 11907 /* Glyph for a line end in text. */
091f8878 11908 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 11909 ++glyph_end;
5f5c8ee5 11910
e187cf71
GM
11911 if (glyph < glyph_end)
11912 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 11913
e187cf71
GM
11914 for (; glyph < glyph_end; ++glyph)
11915 dump_glyph (row, glyph, area);
f7b4b63a 11916 }
f4faa47c 11917 }
9ab436b9
GM
11918 else if (glyphs == 1)
11919 {
e187cf71 11920 int area;
9ab436b9 11921
e187cf71 11922 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 11923 {
e187cf71
GM
11924 char *s = (char *) alloca (row->used[area] + 1);
11925 int i;
11926
11927 for (i = 0; i < row->used[area]; ++i)
11928 {
11929 struct glyph *glyph = row->glyphs[area] + i;
11930 if (glyph->type == CHAR_GLYPH
11931 && glyph->u.ch < 0x80
11932 && glyph->u.ch >= ' ')
11933 s[i] = glyph->u.ch;
11934 else
11935 s[i] = '.';
11936 }
9ab436b9 11937
e187cf71
GM
11938 s[i] = '\0';
11939 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11940 }
9ab436b9 11941 }
5f5c8ee5 11942}
f4faa47c 11943
a2889657 11944
5f5c8ee5
GM
11945DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11946 Sdump_glyph_matrix, 0, 1, "p",
11947 "Dump the current matrix of the selected window to stderr.\n\
9ab436b9
GM
11948Shows contents of glyph row structures. With non-nil\n\
11949parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11950glyphs in short form, otherwise show glyphs in long form.")
11951 (glyphs)
11952 Lisp_Object glyphs;
5f5c8ee5
GM
11953{
11954 struct window *w = XWINDOW (selected_window);
11955 struct buffer *buffer = XBUFFER (w->buffer);
11956
11957 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11958 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11959 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11960 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11961 fprintf (stderr, "=============================================\n");
9ab436b9
GM
11962 dump_glyph_matrix (w->current_matrix,
11963 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
11964 return Qnil;
11965}
1c2250c2 11966
1fca3fae 11967
e9abc296
GM
11968DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11969 "Dump glyph row ROW to stderr.\n\
11970GLYPH 0 means don't dump glyphs.\n\
11971GLYPH 1 means dump glyphs in short form.\n\
7ef18b28 11972GLYPH > 1 or omitted means dump glyphs in long form.")
e9abc296
GM
11973 (row, glyphs)
11974 Lisp_Object row, glyphs;
5f5c8ee5
GM
11975{
11976 CHECK_NUMBER (row, 0);
e9abc296
GM
11977 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11978 XINT (row),
11979 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
11980 return Qnil;
11981}
1fca3fae 11982
67481ae5 11983
b94c0d9c
GM
11984DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11985 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11986GLYPH 0 means don't dump glyphs.\n\
11987GLYPH 1 means dump glyphs in short form.\n\
11988GLYPH > 1 or omitted means dump glyphs in long form.")
11989 (row, glyphs)
11990 Lisp_Object row, glyphs;
5f5c8ee5 11991{
886bd6f2 11992 struct frame *sf = SELECTED_FRAME ();
b94c0d9c
GM
11993 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11994 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
11995 return Qnil;
11996}
ca26e1c8 11997
0f9c0ff0 11998
62397849
GM
11999DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12000 "Toggle tracing of redisplay.\n\
12001With ARG, turn tracing on if and only if ARG is positive.")
12002 (arg)
12003 Lisp_Object arg;
5f5c8ee5 12004{
62397849
GM
12005 if (NILP (arg))
12006 trace_redisplay_p = !trace_redisplay_p;
12007 else
12008 {
12009 arg = Fprefix_numeric_value (arg);
12010 trace_redisplay_p = XINT (arg) > 0;
12011 }
12012
5f5c8ee5
GM
12013 return Qnil;
12014}
bf9249e3
GM
12015
12016
12017DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
12018 "Print STRING to stderr.")
12019 (string)
12020 Lisp_Object string;
12021{
12022 CHECK_STRING (string, 0);
12023 fprintf (stderr, "%s", XSTRING (string)->data);
12024 return Qnil;
12025}
5f5c8ee5
GM
12026
12027#endif /* GLYPH_DEBUG */
ca26e1c8 12028
ca26e1c8 12029
5f5c8ee5
GM
12030\f
12031/***********************************************************************
12032 Building Desired Matrix Rows
12033 ***********************************************************************/
a2889657 12034
5f5c8ee5
GM
12035/* Return a temporary glyph row holding the glyphs of an overlay
12036 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 12037
5f5c8ee5
GM
12038static struct glyph_row *
12039get_overlay_arrow_glyph_row (w)
12040 struct window *w;
12041{
12042 struct frame *f = XFRAME (WINDOW_FRAME (w));
12043 struct buffer *buffer = XBUFFER (w->buffer);
12044 struct buffer *old = current_buffer;
12045 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12046 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12047 unsigned char *arrow_end = arrow_string + arrow_len;
12048 unsigned char *p;
12049 struct it it;
12050 int multibyte_p;
12051 int n_glyphs_before;
12052
12053 set_buffer_temp (buffer);
12054 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12055 it.glyph_row->used[TEXT_AREA] = 0;
12056 SET_TEXT_POS (it.position, 0, 0);
12057
12058 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12059 p = arrow_string;
12060 while (p < arrow_end)
12061 {
12062 Lisp_Object face, ilisp;
12063
12064 /* Get the next character. */
12065 if (multibyte_p)
4fdb80f2 12066 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
12067 else
12068 it.c = *p, it.len = 1;
12069 p += it.len;
12070
12071 /* Get its face. */
ac90c44f 12072 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
12073 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12074 it.face_id = compute_char_face (f, it.c, face);
12075
12076 /* Compute its width, get its glyphs. */
12077 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 12078 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
12079 PRODUCE_GLYPHS (&it);
12080
12081 /* If this character doesn't fit any more in the line, we have
12082 to remove some glyphs. */
12083 if (it.current_x > it.last_visible_x)
12084 {
12085 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12086 break;
12087 }
12088 }
12089
12090 set_buffer_temp (old);
12091 return it.glyph_row;
12092}
ca26e1c8 12093
b0a0fbda 12094
5f5c8ee5
GM
12095/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12096 glyphs are only inserted for terminal frames since we can't really
12097 win with truncation glyphs when partially visible glyphs are
12098 involved. Which glyphs to insert is determined by
12099 produce_special_glyphs. */
67481ae5 12100
5f5c8ee5
GM
12101static void
12102insert_left_trunc_glyphs (it)
12103 struct it *it;
12104{
12105 struct it truncate_it;
12106 struct glyph *from, *end, *to, *toend;
12107
12108 xassert (!FRAME_WINDOW_P (it->f));
12109
12110 /* Get the truncation glyphs. */
12111 truncate_it = *it;
5f5c8ee5
GM
12112 truncate_it.current_x = 0;
12113 truncate_it.face_id = DEFAULT_FACE_ID;
12114 truncate_it.glyph_row = &scratch_glyph_row;
12115 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12116 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 12117 truncate_it.object = make_number (0);
5f5c8ee5
GM
12118 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12119
12120 /* Overwrite glyphs from IT with truncation glyphs. */
12121 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12122 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12123 to = it->glyph_row->glyphs[TEXT_AREA];
12124 toend = to + it->glyph_row->used[TEXT_AREA];
12125
12126 while (from < end)
12127 *to++ = *from++;
12128
37be86f2
KH
12129 /* There may be padding glyphs left over. Overwrite them too. */
12130 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12131 {
12132 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12133 while (from < end)
12134 *to++ = *from++;
12135 }
5f5c8ee5 12136
37be86f2
KH
12137 if (to > toend)
12138 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 12139}
e0bfbde6 12140
e0bfbde6 12141
5f5c8ee5 12142/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 12143
5f5c8ee5
GM
12144 Most of the time, ascent and height of a display line will be equal
12145 to the max_ascent and max_height values of the display iterator
12146 structure. This is not the case if
67481ae5 12147
5f5c8ee5
GM
12148 1. We hit ZV without displaying anything. In this case, max_ascent
12149 and max_height will be zero.
1c9241f5 12150
5f5c8ee5
GM
12151 2. We have some glyphs that don't contribute to the line height.
12152 (The glyph row flag contributes_to_line_height_p is for future
12153 pixmap extensions).
f6fd109b 12154
5f5c8ee5
GM
12155 The first case is easily covered by using default values because in
12156 these cases, the line height does not really matter, except that it
12157 must not be zero. */
67481ae5 12158
5f5c8ee5
GM
12159static void
12160compute_line_metrics (it)
12161 struct it *it;
12162{
12163 struct glyph_row *row = it->glyph_row;
12164 int area, i;
1c2250c2 12165
5f5c8ee5
GM
12166 if (FRAME_WINDOW_P (it->f))
12167 {
045dee35 12168 int i, header_line_height;
1c2250c2 12169
5f5c8ee5
GM
12170 /* The line may consist of one space only, that was added to
12171 place the cursor on it. If so, the row's height hasn't been
12172 computed yet. */
12173 if (row->height == 0)
12174 {
12175 if (it->max_ascent + it->max_descent == 0)
312246d1 12176 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
12177 row->ascent = it->max_ascent;
12178 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12179 row->phys_ascent = it->max_phys_ascent;
12180 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12181 }
12182
12183 /* Compute the width of this line. */
12184 row->pixel_width = row->x;
12185 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12186 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12187
12188 xassert (row->pixel_width >= 0);
12189 xassert (row->ascent >= 0 && row->height > 0);
12190
312246d1
GM
12191 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12192 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12193
12194 /* If first line's physical ascent is larger than its logical
12195 ascent, use the physical ascent, and make the row taller.
12196 This makes accented characters fully visible. */
b28cb6ed 12197 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
12198 && row->phys_ascent > row->ascent)
12199 {
12200 row->height += row->phys_ascent - row->ascent;
12201 row->ascent = row->phys_ascent;
12202 }
12203
5f5c8ee5
GM
12204 /* Compute how much of the line is visible. */
12205 row->visible_height = row->height;
12206
045dee35
GM
12207 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12208 if (row->y < header_line_height)
12209 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
12210 else
12211 {
12212 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12213 if (row->y + row->height > max_y)
12214 row->visible_height -= row->y + row->height - max_y;
12215 }
12216 }
12217 else
12218 {
12219 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
12220 row->ascent = row->phys_ascent = 0;
12221 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 12222 }
67481ae5 12223
5f5c8ee5
GM
12224 /* Compute a hash code for this row. */
12225 row->hash = 0;
12226 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12227 for (i = 0; i < row->used[area]; ++i)
12228 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12229 + row->glyphs[area][i].u.val
43d120d8
KH
12230 + row->glyphs[area][i].face_id
12231 + row->glyphs[area][i].padding_p
5f5c8ee5 12232 + (row->glyphs[area][i].type << 2));
a2889657 12233
5f5c8ee5 12234 it->max_ascent = it->max_descent = 0;
312246d1 12235 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 12236}
12adba34 12237
ca26e1c8 12238
5f5c8ee5
GM
12239/* Append one space to the glyph row of iterator IT if doing a
12240 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12241 space have the default face, otherwise let it have the same face as
80c6cb1f 12242 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
12243
12244 This function is called to make sure that there is always one glyph
12245 at the end of a glyph row that the cursor can be set on under
12246 window-systems. (If there weren't such a glyph we would not know
12247 how wide and tall a box cursor should be displayed).
12248
12249 At the same time this space let's a nicely handle clearing to the
12250 end of the line if the row ends in italic text. */
ca26e1c8 12251
80c6cb1f 12252static int
5f5c8ee5
GM
12253append_space (it, default_face_p)
12254 struct it *it;
12255 int default_face_p;
12256{
12257 if (FRAME_WINDOW_P (it->f))
12258 {
12259 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 12260
5f5c8ee5
GM
12261 if (it->glyph_row->glyphs[TEXT_AREA] + n
12262 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 12263 {
cafafe0b
GM
12264 /* Save some values that must not be changed.
12265 Must save IT->c and IT->len because otherwise
12266 ITERATOR_AT_END_P wouldn't work anymore after
12267 append_space has been called. */
478d746b 12268 enum display_element_type saved_what = it->what;
cafafe0b
GM
12269 int saved_c = it->c, saved_len = it->len;
12270 int saved_x = it->current_x;
5f5c8ee5 12271 int saved_face_id = it->face_id;
cafafe0b 12272 struct text_pos saved_pos;
5f5c8ee5 12273 Lisp_Object saved_object;
980806b6 12274 struct face *face;
5f5c8ee5
GM
12275
12276 saved_object = it->object;
12277 saved_pos = it->position;
12278
12279 it->what = IT_CHARACTER;
12280 bzero (&it->position, sizeof it->position);
6fc556fd 12281 it->object = make_number (0);
5f5c8ee5
GM
12282 it->c = ' ';
12283 it->len = 1;
5f5c8ee5
GM
12284
12285 if (default_face_p)
12286 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
12287 else if (it->face_before_selective_p)
12288 it->face_id = it->saved_face_id;
980806b6
KH
12289 face = FACE_FROM_ID (it->f, it->face_id);
12290 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 12291
5f5c8ee5
GM
12292 PRODUCE_GLYPHS (it);
12293
12294 it->current_x = saved_x;
12295 it->object = saved_object;
12296 it->position = saved_pos;
12297 it->what = saved_what;
12298 it->face_id = saved_face_id;
cafafe0b
GM
12299 it->len = saved_len;
12300 it->c = saved_c;
80c6cb1f 12301 return 1;
5f5c8ee5
GM
12302 }
12303 }
80c6cb1f
GM
12304
12305 return 0;
5f5c8ee5 12306}
12adba34 12307
1842fc1a 12308
5f5c8ee5
GM
12309/* Extend the face of the last glyph in the text area of IT->glyph_row
12310 to the end of the display line. Called from display_line.
12311 If the glyph row is empty, add a space glyph to it so that we
12312 know the face to draw. Set the glyph row flag fill_line_p. */
12313
12314static void
12315extend_face_to_end_of_line (it)
12316 struct it *it;
12317{
12318 struct face *face;
12319 struct frame *f = it->f;
1842fc1a 12320
5f5c8ee5
GM
12321 /* If line is already filled, do nothing. */
12322 if (it->current_x >= it->last_visible_x)
12323 return;
12324
12325 /* Face extension extends the background and box of IT->face_id
12326 to the end of the line. If the background equals the background
4aad61f8
GM
12327 of the frame, we don't have to do anything. */
12328 if (it->face_before_selective_p)
12329 face = FACE_FROM_ID (it->f, it->saved_face_id);
12330 else
12331 face = FACE_FROM_ID (f, it->face_id);
12332
5f5c8ee5
GM
12333 if (FRAME_WINDOW_P (f)
12334 && face->box == FACE_NO_BOX
12335 && face->background == FRAME_BACKGROUND_PIXEL (f)
12336 && !face->stipple)
12337 return;
1842fc1a 12338
5f5c8ee5
GM
12339 /* Set the glyph row flag indicating that the face of the last glyph
12340 in the text area has to be drawn to the end of the text area. */
12341 it->glyph_row->fill_line_p = 1;
545e04f6 12342
980806b6
KH
12343 /* If current character of IT is not ASCII, make sure we have the
12344 ASCII face. This will be automatically undone the next time
12345 get_next_display_element returns a multibyte character. Note
12346 that the character will always be single byte in unibyte text. */
12347 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 12348 {
980806b6 12349 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 12350 }
545e04f6 12351
5f5c8ee5
GM
12352 if (FRAME_WINDOW_P (f))
12353 {
12354 /* If the row is empty, add a space with the current face of IT,
12355 so that we know which face to draw. */
12356 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 12357 {
5f5c8ee5 12358 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 12359 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 12360 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 12361 }
5f5c8ee5
GM
12362 }
12363 else
12364 {
12365 /* Save some values that must not be changed. */
12366 int saved_x = it->current_x;
12367 struct text_pos saved_pos;
12368 Lisp_Object saved_object;
478d746b 12369 enum display_element_type saved_what = it->what;
4aad61f8 12370 int saved_face_id = it->face_id;
5f5c8ee5
GM
12371
12372 saved_object = it->object;
12373 saved_pos = it->position;
12374
12375 it->what = IT_CHARACTER;
12376 bzero (&it->position, sizeof it->position);
6fc556fd 12377 it->object = make_number (0);
5f5c8ee5
GM
12378 it->c = ' ';
12379 it->len = 1;
4aad61f8 12380 it->face_id = face->id;
5f5c8ee5
GM
12381
12382 PRODUCE_GLYPHS (it);
12383
12384 while (it->current_x <= it->last_visible_x)
12385 PRODUCE_GLYPHS (it);
12386
12387 /* Don't count these blanks really. It would let us insert a left
12388 truncation glyph below and make us set the cursor on them, maybe. */
12389 it->current_x = saved_x;
12390 it->object = saved_object;
12391 it->position = saved_pos;
12392 it->what = saved_what;
4aad61f8 12393 it->face_id = saved_face_id;
5f5c8ee5
GM
12394 }
12395}
12adba34 12396
545e04f6 12397
5f5c8ee5
GM
12398/* Value is non-zero if text starting at CHARPOS in current_buffer is
12399 trailing whitespace. */
1c9241f5 12400
5f5c8ee5
GM
12401static int
12402trailing_whitespace_p (charpos)
12403 int charpos;
12404{
12405 int bytepos = CHAR_TO_BYTE (charpos);
12406 int c = 0;
7bbe686f 12407
5f5c8ee5
GM
12408 while (bytepos < ZV_BYTE
12409 && (c = FETCH_CHAR (bytepos),
12410 c == ' ' || c == '\t'))
12411 ++bytepos;
0d09d1e6 12412
8f897821
GM
12413 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12414 {
12415 if (bytepos != PT_BYTE)
12416 return 1;
12417 }
12418 return 0;
5f5c8ee5 12419}
31b24551 12420
545e04f6 12421
5f5c8ee5 12422/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 12423
5f5c8ee5
GM
12424void
12425highlight_trailing_whitespace (f, row)
12426 struct frame *f;
12427 struct glyph_row *row;
12428{
12429 int used = row->used[TEXT_AREA];
12430
12431 if (used)
12432 {
12433 struct glyph *start = row->glyphs[TEXT_AREA];
12434 struct glyph *glyph = start + used - 1;
12435
ade0bee1
GM
12436 /* Skip over glyphs inserted to display the cursor at the
12437 end of a line, for extending the face of the last glyph
12438 to the end of the line on terminals, and for truncation
12439 and continuation glyphs. */
65008712
GM
12440 while (glyph >= start
12441 && glyph->type == CHAR_GLYPH
65008712 12442 && INTEGERP (glyph->object))
5f5c8ee5
GM
12443 --glyph;
12444
12445 /* If last glyph is a space or stretch, and it's trailing
12446 whitespace, set the face of all trailing whitespace glyphs in
12447 IT->glyph_row to `trailing-whitespace'. */
12448 if (glyph >= start
12449 && BUFFERP (glyph->object)
12450 && (glyph->type == STRETCH_GLYPH
12451 || (glyph->type == CHAR_GLYPH
43d120d8 12452 && glyph->u.ch == ' '))
5f5c8ee5 12453 && trailing_whitespace_p (glyph->charpos))
545e04f6 12454 {
980806b6 12455 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
12456
12457 while (glyph >= start
12458 && BUFFERP (glyph->object)
12459 && (glyph->type == STRETCH_GLYPH
12460 || (glyph->type == CHAR_GLYPH
43d120d8
KH
12461 && glyph->u.ch == ' ')))
12462 (glyph--)->face_id = face_id;
545e04f6 12463 }
a2889657 12464 }
5f5c8ee5 12465}
a2889657 12466
5fcbb24d 12467
cafafe0b 12468/* Value is non-zero if glyph row ROW in window W should be
0fd37545 12469 used to hold the cursor. */
cafafe0b
GM
12470
12471static int
12472cursor_row_p (w, row)
12473 struct window *w;
12474 struct glyph_row *row;
12475{
9a038881
GM
12476 int cursor_row_p = 1;
12477
cafafe0b
GM
12478 if (PT == MATRIX_ROW_END_CHARPOS (row))
12479 {
9a038881
GM
12480 /* If the row ends with a newline from a string, we don't want
12481 the cursor there (if the row is continued it doesn't end in a
12482 newline). */
12483 if (CHARPOS (row->end.string_pos) >= 0
12484 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12485 cursor_row_p = row->continued_p;
12486
12487 /* If the row ends at ZV, display the cursor at the end of that
12488 row instead of at the start of the row below. */
12489 else if (row->ends_at_zv_p)
12490 cursor_row_p = 1;
12491 else
12492 cursor_row_p = 0;
cafafe0b
GM
12493 }
12494
9a038881 12495 return cursor_row_p;
cafafe0b
GM
12496}
12497
12498
5f5c8ee5
GM
12499/* Construct the glyph row IT->glyph_row in the desired matrix of
12500 IT->w from text at the current position of IT. See dispextern.h
12501 for an overview of struct it. Value is non-zero if
12502 IT->glyph_row displays text, as opposed to a line displaying ZV
12503 only. */
12504
12505static int
12506display_line (it)
12507 struct it *it;
12508{
12509 struct glyph_row *row = it->glyph_row;
12510
12511 /* We always start displaying at hpos zero even if hscrolled. */
12512 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 12513
5f5c8ee5
GM
12514 /* We must not display in a row that's not a text row. */
12515 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12516 < it->w->desired_matrix->nrows);
12adba34 12517
5f5c8ee5
GM
12518 /* Is IT->w showing the region? */
12519 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 12520
5f5c8ee5
GM
12521 /* Clear the result glyph row and enable it. */
12522 prepare_desired_row (row);
12adba34 12523
5f5c8ee5
GM
12524 row->y = it->current_y;
12525 row->start = it->current;
12526 row->continuation_lines_width = it->continuation_lines_width;
12527 row->displays_text_p = 1;
91004049
GM
12528 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12529 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
12530
12531 /* Arrange the overlays nicely for our purposes. Usually, we call
12532 display_line on only one line at a time, in which case this
12533 can't really hurt too much, or we call it on lines which appear
12534 one after another in the buffer, in which case all calls to
12535 recenter_overlay_lists but the first will be pretty cheap. */
12536 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12537
5f5c8ee5
GM
12538 /* Move over display elements that are not visible because we are
12539 hscrolled. This may stop at an x-position < IT->first_visible_x
12540 if the first glyph is partially visible or if we hit a line end. */
12541 if (it->current_x < it->first_visible_x)
12542 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12543 MOVE_TO_POS | MOVE_TO_X);
12544
12545 /* Get the initial row height. This is either the height of the
12546 text hscrolled, if there is any, or zero. */
12547 row->ascent = it->max_ascent;
12548 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12549 row->phys_ascent = it->max_phys_ascent;
12550 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12551
12552 /* Loop generating characters. The loop is left with IT on the next
12553 character to display. */
12554 while (1)
12555 {
12556 int n_glyphs_before, hpos_before, x_before;
12557 int x, i, nglyphs;
ae26e27d 12558 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 12559
5f5c8ee5
GM
12560 /* Retrieve the next thing to display. Value is zero if end of
12561 buffer reached. */
12562 if (!get_next_display_element (it))
12563 {
12564 /* Maybe add a space at the end of this line that is used to
1b335865
GM
12565 display the cursor there under X. Set the charpos of the
12566 first glyph of blank lines not corresponding to any text
12567 to -1. */
12568 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12569 || row->used[TEXT_AREA] == 0)
a2889657 12570 {
5f5c8ee5
GM
12571 row->glyphs[TEXT_AREA]->charpos = -1;
12572 row->displays_text_p = 0;
12573
12574 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12575 row->indicate_empty_line_p = 1;
a2889657 12576 }
5f5c8ee5
GM
12577
12578 it->continuation_lines_width = 0;
12579 row->ends_at_zv_p = 1;
12580 break;
a2889657 12581 }
a2889657 12582
5f5c8ee5
GM
12583 /* Now, get the metrics of what we want to display. This also
12584 generates glyphs in `row' (which is IT->glyph_row). */
12585 n_glyphs_before = row->used[TEXT_AREA];
12586 x = it->current_x;
e6819faf
GM
12587
12588 /* Remember the line height so far in case the next element doesn't
12589 fit on the line. */
12590 if (!it->truncate_lines_p)
12591 {
12592 ascent = it->max_ascent;
12593 descent = it->max_descent;
12594 phys_ascent = it->max_phys_ascent;
12595 phys_descent = it->max_phys_descent;
12596 }
12597
5f5c8ee5 12598 PRODUCE_GLYPHS (it);
a2889657 12599
5f5c8ee5
GM
12600 /* If this display element was in marginal areas, continue with
12601 the next one. */
12602 if (it->area != TEXT_AREA)
a2889657 12603 {
5f5c8ee5
GM
12604 row->ascent = max (row->ascent, it->max_ascent);
12605 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12606 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12607 row->phys_height = max (row->phys_height,
12608 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 12609 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12610 continue;
12611 }
5936754e 12612
5f5c8ee5
GM
12613 /* Does the display element fit on the line? If we truncate
12614 lines, we should draw past the right edge of the window. If
12615 we don't truncate, we want to stop so that we can display the
12616 continuation glyph before the right margin. If lines are
12617 continued, there are two possible strategies for characters
12618 resulting in more than 1 glyph (e.g. tabs): Display as many
12619 glyphs as possible in this line and leave the rest for the
12620 continuation line, or display the whole element in the next
12621 line. Original redisplay did the former, so we do it also. */
12622 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12623 hpos_before = it->hpos;
12624 x_before = x;
4dcd74e6
GM
12625
12626 if (/* Not a newline. */
12627 nglyphs > 0
202c1b5b 12628 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
12629 && it->current_x < it->last_visible_x)
12630 {
37be86f2 12631 it->hpos += nglyphs;
5f5c8ee5
GM
12632 row->ascent = max (row->ascent, it->max_ascent);
12633 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12634 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12635 row->phys_height = max (row->phys_height,
12636 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12637 if (it->current_x - it->pixel_width < it->first_visible_x)
12638 row->x = x - it->first_visible_x;
12639 }
12640 else
12641 {
12642 int new_x;
12643 struct glyph *glyph;
12644
12645 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 12646 {
5f5c8ee5
GM
12647 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12648 new_x = x + glyph->pixel_width;
12649
12650 if (/* Lines are continued. */
12651 !it->truncate_lines_p
12652 && (/* Glyph doesn't fit on the line. */
12653 new_x > it->last_visible_x
12654 /* Or it fits exactly on a window system frame. */
12655 || (new_x == it->last_visible_x
12656 && FRAME_WINDOW_P (it->f))))
a2889657 12657 {
5f5c8ee5
GM
12658 /* End of a continued line. */
12659
12660 if (it->hpos == 0
12661 || (new_x == it->last_visible_x
12662 && FRAME_WINDOW_P (it->f)))
12663 {
e6819faf
GM
12664 /* Current glyph is the only one on the line or
12665 fits exactly on the line. We must continue
12666 the line because we can't draw the cursor
12667 after the glyph. */
5f5c8ee5
GM
12668 row->continued_p = 1;
12669 it->current_x = new_x;
12670 it->continuation_lines_width += new_x;
12671 ++it->hpos;
12672 if (i == nglyphs - 1)
cafafe0b 12673 set_iterator_to_next (it, 1);
5f5c8ee5 12674 }
3b3c4bf0
GM
12675 else if (CHAR_GLYPH_PADDING_P (*glyph)
12676 && !FRAME_WINDOW_P (it->f))
12677 {
12678 /* A padding glyph that doesn't fit on this line.
12679 This means the whole character doesn't fit
12680 on the line. */
12681 row->used[TEXT_AREA] = n_glyphs_before;
12682
12683 /* Fill the rest of the row with continuation
12684 glyphs like in 20.x. */
12685 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12686 < row->glyphs[1 + TEXT_AREA])
12687 produce_special_glyphs (it, IT_CONTINUATION);
12688
12689 row->continued_p = 1;
12690 it->current_x = x_before;
12691 it->continuation_lines_width += x_before;
12692
12693 /* Restore the height to what it was before the
12694 element not fitting on the line. */
12695 it->max_ascent = ascent;
12696 it->max_descent = descent;
12697 it->max_phys_ascent = phys_ascent;
12698 it->max_phys_descent = phys_descent;
12699 }
5f5c8ee5 12700 else
5936754e 12701 {
5f5c8ee5
GM
12702 /* Display element draws past the right edge of
12703 the window. Restore positions to values
12704 before the element. The next line starts
12705 with current_x before the glyph that could
12706 not be displayed, so that TAB works right. */
12707 row->used[TEXT_AREA] = n_glyphs_before + i;
12708
12709 /* Display continuation glyphs. */
12710 if (!FRAME_WINDOW_P (it->f))
12711 produce_special_glyphs (it, IT_CONTINUATION);
12712 row->continued_p = 1;
12713
12714 it->current_x = x;
12715 it->continuation_lines_width += x;
91004049
GM
12716 if (nglyphs > 1 && i > 0)
12717 {
12718 row->ends_in_middle_of_char_p = 1;
12719 it->starts_in_middle_of_char_p = 1;
12720 }
e6819faf
GM
12721
12722 /* Restore the height to what it was before the
12723 element not fitting on the line. */
12724 it->max_ascent = ascent;
12725 it->max_descent = descent;
12726 it->max_phys_ascent = phys_ascent;
12727 it->max_phys_descent = phys_descent;
5936754e 12728 }
e6819faf 12729
5f5c8ee5
GM
12730 break;
12731 }
12732 else if (new_x > it->first_visible_x)
12733 {
12734 /* Increment number of glyphs actually displayed. */
12735 ++it->hpos;
12736
12737 if (x < it->first_visible_x)
12738 /* Glyph is partially visible, i.e. row starts at
12739 negative X position. */
12740 row->x = x - it->first_visible_x;
12741 }
12742 else
12743 {
12744 /* Glyph is completely off the left margin of the
12745 window. This should not happen because of the
12746 move_it_in_display_line at the start of
12747 this function. */
12748 abort ();
a2889657 12749 }
a2889657 12750 }
5f5c8ee5
GM
12751
12752 row->ascent = max (row->ascent, it->max_ascent);
12753 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12754 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12755 row->phys_height = max (row->phys_height,
12756 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12757
12758 /* End of this display line if row is continued. */
12759 if (row->continued_p)
12760 break;
a2889657 12761 }
a2889657 12762
5f5c8ee5
GM
12763 /* Is this a line end? If yes, we're also done, after making
12764 sure that a non-default face is extended up to the right
12765 margin of the window. */
12766 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 12767 {
5f5c8ee5
GM
12768 int used_before = row->used[TEXT_AREA];
12769
12770 /* Add a space at the end of the line that is used to
12771 display the cursor there. */
12772 append_space (it, 0);
12773
12774 /* Extend the face to the end of the line. */
12775 extend_face_to_end_of_line (it);
12776
12777 /* Make sure we have the position. */
12778 if (used_before == 0)
12779 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12780
12781 /* Consume the line end. This skips over invisible lines. */
cafafe0b 12782 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12783 it->continuation_lines_width = 0;
12784 break;
1c9241f5 12785 }
a2889657 12786
5f5c8ee5
GM
12787 /* Proceed with next display element. Note that this skips
12788 over lines invisible because of selective display. */
cafafe0b 12789 set_iterator_to_next (it, 1);
b1d1124b 12790
5f5c8ee5
GM
12791 /* If we truncate lines, we are done when the last displayed
12792 glyphs reach past the right margin of the window. */
12793 if (it->truncate_lines_p
12794 && (FRAME_WINDOW_P (it->f)
12795 ? (it->current_x >= it->last_visible_x)
12796 : (it->current_x > it->last_visible_x)))
75d13c64 12797 {
5f5c8ee5
GM
12798 /* Maybe add truncation glyphs. */
12799 if (!FRAME_WINDOW_P (it->f))
12800 {
a98b5ed9
GM
12801 int i, n;
12802
12803 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12804 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12805 break;
12806
12807 for (n = row->used[TEXT_AREA]; i < n; ++i)
12808 {
12809 row->used[TEXT_AREA] = i;
12810 produce_special_glyphs (it, IT_TRUNCATION);
12811 }
5f5c8ee5
GM
12812 }
12813
12814 row->truncated_on_right_p = 1;
12815 it->continuation_lines_width = 0;
312246d1 12816 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
12817 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12818 it->hpos = hpos_before;
12819 it->current_x = x_before;
12820 break;
75d13c64 12821 }
a2889657 12822 }
a2889657 12823
5f5c8ee5
GM
12824 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12825 at the left window margin. */
12826 if (it->first_visible_x
12827 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12828 {
12829 if (!FRAME_WINDOW_P (it->f))
12830 insert_left_trunc_glyphs (it);
12831 row->truncated_on_left_p = 1;
12832 }
a2889657 12833
5f5c8ee5
GM
12834 /* If the start of this line is the overlay arrow-position, then
12835 mark this glyph row as the one containing the overlay arrow.
12836 This is clearly a mess with variable size fonts. It would be
12837 better to let it be displayed like cursors under X. */
e24c997d 12838 if (MARKERP (Voverlay_arrow_position)
a2889657 12839 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
12840 && (MATRIX_ROW_START_CHARPOS (row)
12841 == marker_position (Voverlay_arrow_position))
e24c997d 12842 && STRINGP (Voverlay_arrow_string)
a2889657
JB
12843 && ! overlay_arrow_seen)
12844 {
5f5c8ee5
GM
12845 /* Overlay arrow in window redisplay is a bitmap. */
12846 if (!FRAME_WINDOW_P (it->f))
c4628384 12847 {
5f5c8ee5
GM
12848 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12849 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12850 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12851 struct glyph *p = row->glyphs[TEXT_AREA];
12852 struct glyph *p2, *end;
12853
12854 /* Copy the arrow glyphs. */
12855 while (glyph < arrow_end)
12856 *p++ = *glyph++;
12857
12858 /* Throw away padding glyphs. */
12859 p2 = p;
12860 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12861 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12862 ++p2;
12863 if (p2 > p)
212e4f87 12864 {
5f5c8ee5
GM
12865 while (p2 < end)
12866 *p++ = *p2++;
12867 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 12868 }
c4628384 12869 }
5f5c8ee5 12870
a2889657 12871 overlay_arrow_seen = 1;
5f5c8ee5 12872 row->overlay_arrow_p = 1;
a2889657
JB
12873 }
12874
5f5c8ee5
GM
12875 /* Compute pixel dimensions of this line. */
12876 compute_line_metrics (it);
12877
12878 /* Remember the position at which this line ends. */
12879 row->end = it->current;
12880
173cbca8 12881 /* Maybe set the cursor. */
5f5c8ee5
GM
12882 if (it->w->cursor.vpos < 0
12883 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
12884 && PT <= MATRIX_ROW_END_CHARPOS (row)
12885 && cursor_row_p (it->w, row))
12886 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
12887
12888 /* Highlight trailing whitespace. */
8f897821 12889 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
12890 highlight_trailing_whitespace (it->f, it->glyph_row);
12891
12892 /* Prepare for the next line. This line starts horizontally at (X
12893 HPOS) = (0 0). Vertical positions are incremented. As a
12894 convenience for the caller, IT->glyph_row is set to the next
12895 row to be used. */
12896 it->current_x = it->hpos = 0;
12897 it->current_y += row->height;
12898 ++it->vpos;
12899 ++it->glyph_row;
12900 return row->displays_text_p;
a2889657 12901}
5f5c8ee5
GM
12902
12903
a2889657 12904\f
5f5c8ee5
GM
12905/***********************************************************************
12906 Menu Bar
12907 ***********************************************************************/
12908
12909/* Redisplay the menu bar in the frame for window W.
12910
12911 The menu bar of X frames that don't have X toolkit support is
12912 displayed in a special window W->frame->menu_bar_window.
12913
12914 The menu bar of terminal frames is treated specially as far as
12915 glyph matrices are concerned. Menu bar lines are not part of
12916 windows, so the update is done directly on the frame matrix rows
12917 for the menu bar. */
7ce2c095
RS
12918
12919static void
12920display_menu_bar (w)
12921 struct window *w;
12922{
5f5c8ee5
GM
12923 struct frame *f = XFRAME (WINDOW_FRAME (w));
12924 struct it it;
12925 Lisp_Object items;
8351baf2 12926 int i;
7ce2c095 12927
5f5c8ee5 12928 /* Don't do all this for graphical frames. */
dc937613 12929#ifdef HAVE_NTGUI
d129c4c2
KH
12930 if (!NILP (Vwindow_system))
12931 return;
dc937613 12932#endif
dc937613 12933#ifdef USE_X_TOOLKIT
d3413a53 12934 if (FRAME_X_P (f))
7ce2c095 12935 return;
5f5c8ee5 12936#endif
1a578e9b
AC
12937#ifdef macintosh
12938 if (FRAME_MAC_P (f))
12939 return;
12940#endif
5f5c8ee5
GM
12941
12942#ifdef USE_X_TOOLKIT
12943 xassert (!FRAME_WINDOW_P (f));
52377a47 12944 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
12945 it.first_visible_x = 0;
12946 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12947#else /* not USE_X_TOOLKIT */
12948 if (FRAME_WINDOW_P (f))
12949 {
12950 /* Menu bar lines are displayed in the desired matrix of the
12951 dummy window menu_bar_window. */
12952 struct window *menu_w;
12953 xassert (WINDOWP (f->menu_bar_window));
12954 menu_w = XWINDOW (f->menu_bar_window);
12955 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 12956 MENU_FACE_ID);
5f5c8ee5
GM
12957 it.first_visible_x = 0;
12958 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12959 }
12960 else
12961 {
12962 /* This is a TTY frame, i.e. character hpos/vpos are used as
12963 pixel x/y. */
12964 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 12965 MENU_FACE_ID);
5f5c8ee5
GM
12966 it.first_visible_x = 0;
12967 it.last_visible_x = FRAME_WIDTH (f);
12968 }
12969#endif /* not USE_X_TOOLKIT */
12970
1862a24e
MB
12971 if (! mode_line_inverse_video)
12972 /* Force the menu-bar to be displayed in the default face. */
12973 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12974
5f5c8ee5
GM
12975 /* Clear all rows of the menu bar. */
12976 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12977 {
12978 struct glyph_row *row = it.glyph_row + i;
12979 clear_glyph_row (row);
12980 row->enabled_p = 1;
12981 row->full_width_p = 1;
12982 }
7ce2c095 12983
5f5c8ee5
GM
12984 /* Display all items of the menu bar. */
12985 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 12986 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 12987 {
5f5c8ee5
GM
12988 Lisp_Object string;
12989
12990 /* Stop at nil string. */
a61b7058 12991 string = AREF (items, i + 1);
8351baf2
RS
12992 if (NILP (string))
12993 break;
2d66ad19 12994
5f5c8ee5 12995 /* Remember where item was displayed. */
a61b7058 12996 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 12997
5f5c8ee5
GM
12998 /* Display the item, pad with one space. */
12999 if (it.current_x < it.last_visible_x)
13000 display_string (NULL, string, Qnil, 0, 0, &it,
13001 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
13002 }
13003
2d66ad19 13004 /* Fill out the line with spaces. */
5f5c8ee5
GM
13005 if (it.current_x < it.last_visible_x)
13006 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 13007
5f5c8ee5
GM
13008 /* Compute the total height of the lines. */
13009 compute_line_metrics (&it);
7ce2c095 13010}
5f5c8ee5
GM
13011
13012
7ce2c095 13013\f
5f5c8ee5
GM
13014/***********************************************************************
13015 Mode Line
13016 ***********************************************************************/
13017
715e84c9
GM
13018/* Redisplay mode lines in the window tree whose root is WINDOW. If
13019 FORCE is non-zero, redisplay mode lines unconditionally.
13020 Otherwise, redisplay only mode lines that are garbaged. Value is
13021 the number of windows whose mode lines were redisplayed. */
a2889657 13022
715e84c9
GM
13023static int
13024redisplay_mode_lines (window, force)
13025 Lisp_Object window;
13026 int force;
13027{
13028 int nwindows = 0;
13029
13030 while (!NILP (window))
13031 {
13032 struct window *w = XWINDOW (window);
13033
13034 if (WINDOWP (w->hchild))
13035 nwindows += redisplay_mode_lines (w->hchild, force);
13036 else if (WINDOWP (w->vchild))
13037 nwindows += redisplay_mode_lines (w->vchild, force);
13038 else if (force
13039 || FRAME_GARBAGED_P (XFRAME (w->frame))
13040 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13041 {
13042 Lisp_Object old_selected_frame;
13043 struct text_pos lpoint;
13044 struct buffer *old = current_buffer;
13045
13046 /* Set the window's buffer for the mode line display. */
13047 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13048 set_buffer_internal_1 (XBUFFER (w->buffer));
13049
13050 /* Point refers normally to the selected window. For any
13051 other window, set up appropriate value. */
13052 if (!EQ (window, selected_window))
13053 {
13054 struct text_pos pt;
13055
13056 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13057 if (CHARPOS (pt) < BEGV)
13058 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13059 else if (CHARPOS (pt) > (ZV - 1))
13060 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13061 else
13062 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13063 }
13064
13065 /* Temporarily set up the selected frame. */
13066 old_selected_frame = selected_frame;
13067 selected_frame = w->frame;
13068
13069 /* Display mode lines. */
13070 clear_glyph_matrix (w->desired_matrix);
13071 if (display_mode_lines (w))
13072 {
13073 ++nwindows;
13074 w->must_be_updated_p = 1;
13075 }
13076
13077 /* Restore old settings. */
13078 selected_frame = old_selected_frame;
13079 set_buffer_internal_1 (old);
13080 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13081 }
13082
13083 window = w->next;
13084 }
13085
13086 return nwindows;
13087}
13088
13089
13090/* Display the mode and/or top line of window W. Value is the number
13091 of mode lines displayed. */
13092
13093static int
5f5c8ee5 13094display_mode_lines (w)
a2889657
JB
13095 struct window *w;
13096{
715e84c9
GM
13097 int n = 0;
13098
5f5c8ee5 13099 /* These will be set while the mode line specs are processed. */
aa6d10fa 13100 line_number_displayed = 0;
155ef550 13101 w->column_number_displayed = Qnil;
aa6d10fa 13102
5f5c8ee5 13103 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9
GM
13104 {
13105 display_mode_line (w, MODE_LINE_FACE_ID,
13106 current_buffer->mode_line_format);
13107 ++n;
13108 }
5f5c8ee5 13109
045dee35 13110 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
13111 {
13112 display_mode_line (w, HEADER_LINE_FACE_ID,
13113 current_buffer->header_line_format);
13114 ++n;
13115 }
13116
13117 return n;
5f5c8ee5 13118}
03b294dc 13119
03b294dc 13120
5f5c8ee5 13121/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 13122 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
13123 FORMAT is the mode line format to display. Value is the pixel
13124 height of the mode line displayed. */
03b294dc 13125
04612a64 13126static int
5f5c8ee5
GM
13127display_mode_line (w, face_id, format)
13128 struct window *w;
13129 enum face_id face_id;
13130 Lisp_Object format;
13131{
13132 struct it it;
13133 struct face *face;
03b294dc 13134
5f5c8ee5
GM
13135 init_iterator (&it, w, -1, -1, NULL, face_id);
13136 prepare_desired_row (it.glyph_row);
13137
1862a24e
MB
13138 if (! mode_line_inverse_video)
13139 /* Force the mode-line to be displayed in the default face. */
13140 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13141
5f5c8ee5
GM
13142 /* Temporarily make frame's keyboard the current kboard so that
13143 kboard-local variables in the mode_line_format will get the right
13144 values. */
13145 push_frame_kboard (it.f);
13146 display_mode_element (&it, 0, 0, 0, format);
13147 pop_frame_kboard ();
a2889657 13148
5f5c8ee5
GM
13149 /* Fill up with spaces. */
13150 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13151
13152 compute_line_metrics (&it);
13153 it.glyph_row->full_width_p = 1;
13154 it.glyph_row->mode_line_p = 1;
1862a24e 13155 it.glyph_row->inverse_p = 0;
5f5c8ee5
GM
13156 it.glyph_row->continued_p = 0;
13157 it.glyph_row->truncated_on_left_p = 0;
13158 it.glyph_row->truncated_on_right_p = 0;
13159
13160 /* Make a 3D mode-line have a shadow at its right end. */
13161 face = FACE_FROM_ID (it.f, face_id);
13162 extend_face_to_end_of_line (&it);
13163 if (face->box != FACE_NO_BOX)
d7eb09a0 13164 {
5f5c8ee5
GM
13165 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13166 + it.glyph_row->used[TEXT_AREA] - 1);
13167 last->right_box_line_p = 1;
d7eb09a0 13168 }
04612a64
GM
13169
13170 return it.glyph_row->height;
a2889657
JB
13171}
13172
a2889657 13173
5f5c8ee5
GM
13174/* Contribute ELT to the mode line for window IT->w. How it
13175 translates into text depends on its data type.
a2889657 13176
5f5c8ee5 13177 IT describes the display environment in which we display, as usual.
a2889657
JB
13178
13179 DEPTH is the depth in recursion. It is used to prevent
13180 infinite recursion here.
13181
5f5c8ee5
GM
13182 FIELD_WIDTH is the number of characters the display of ELT should
13183 occupy in the mode line, and PRECISION is the maximum number of
13184 characters to display from ELT's representation. See
13185 display_string for details. *
a2889657 13186
5f5c8ee5 13187 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
13188
13189static int
5f5c8ee5
GM
13190display_mode_element (it, depth, field_width, precision, elt)
13191 struct it *it;
a2889657 13192 int depth;
5f5c8ee5
GM
13193 int field_width, precision;
13194 Lisp_Object elt;
a2889657 13195{
5f5c8ee5
GM
13196 int n = 0, field, prec;
13197
a2889657
JB
13198 tail_recurse:
13199 if (depth > 10)
13200 goto invalid;
13201
13202 depth++;
13203
0220c518 13204 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
13205 {
13206 case Lisp_String:
13207 {
13208 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
13209 unsigned char c;
13210 unsigned char *this = XSTRING (elt)->data;
13211 unsigned char *lisp_string = this;
13212
13213 while ((precision <= 0 || n < precision)
13214 && *this
13215 && (frame_title_ptr
13216 || it->current_x < it->last_visible_x))
a2889657
JB
13217 {
13218 unsigned char *last = this;
5f5c8ee5
GM
13219
13220 /* Advance to end of string or next format specifier. */
a2889657
JB
13221 while ((c = *this++) != '\0' && c != '%')
13222 ;
5f5c8ee5 13223
a2889657
JB
13224 if (this - 1 != last)
13225 {
5f5c8ee5
GM
13226 /* Output to end of string or up to '%'. Field width
13227 is length of string. Don't output more than
13228 PRECISION allows us. */
d26b89b8
KH
13229 --this;
13230 prec = strwidth (last, this - last);
5f5c8ee5
GM
13231 if (precision > 0 && prec > precision - n)
13232 prec = precision - n;
13233
d39b6696 13234 if (frame_title_ptr)
d26b89b8 13235 n += store_frame_title (last, 0, prec);
d39b6696 13236 else
5f5c8ee5
GM
13237 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13238 it, 0, prec, 0, -1);
a2889657
JB
13239 }
13240 else /* c == '%' */
13241 {
5f5c8ee5
GM
13242 unsigned char *percent_position = this;
13243
13244 /* Get the specified minimum width. Zero means
13245 don't pad. */
13246 field = 0;
a2889657 13247 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 13248 field = field * 10 + c - '0';
a2889657 13249
5f5c8ee5
GM
13250 /* Don't pad beyond the total padding allowed. */
13251 if (field_width - n > 0 && field > field_width - n)
13252 field = field_width - n;
a2889657 13253
5f5c8ee5
GM
13254 /* Note that either PRECISION <= 0 or N < PRECISION. */
13255 prec = precision - n;
13256
a2889657 13257 if (c == 'M')
5f5c8ee5
GM
13258 n += display_mode_element (it, depth, field, prec,
13259 Vglobal_mode_string);
a2889657 13260 else if (c != 0)
d39b6696 13261 {
5f5c8ee5
GM
13262 unsigned char *spec
13263 = decode_mode_spec (it->w, c, field, prec);
13264
d39b6696 13265 if (frame_title_ptr)
5f5c8ee5 13266 n += store_frame_title (spec, field, prec);
d39b6696 13267 else
5f5c8ee5
GM
13268 {
13269 int nglyphs_before
13270 = it->glyph_row->used[TEXT_AREA];
13271 int charpos
13272 = percent_position - XSTRING (elt)->data;
13273 int nwritten
13274 = display_string (spec, Qnil, elt, charpos, 0, it,
13275 field, prec, 0, -1);
13276
13277 /* Assign to the glyphs written above the
13278 string where the `%x' came from, position
13279 of the `%'. */
13280 if (nwritten > 0)
13281 {
13282 struct glyph *glyph
13283 = (it->glyph_row->glyphs[TEXT_AREA]
13284 + nglyphs_before);
13285 int i;
13286
13287 for (i = 0; i < nwritten; ++i)
13288 {
13289 glyph[i].object = elt;
13290 glyph[i].charpos = charpos;
13291 }
13292
13293 n += nwritten;
13294 }
13295 }
d39b6696 13296 }
a2889657
JB
13297 }
13298 }
13299 }
13300 break;
13301
13302 case Lisp_Symbol:
13303 /* A symbol: process the value of the symbol recursively
13304 as if it appeared here directly. Avoid error if symbol void.
13305 Special case: if value of symbol is a string, output the string
13306 literally. */
13307 {
13308 register Lisp_Object tem;
13309 tem = Fboundp (elt);
265a9e55 13310 if (!NILP (tem))
a2889657
JB
13311 {
13312 tem = Fsymbol_value (elt);
13313 /* If value is a string, output that string literally:
13314 don't check for % within it. */
e24c997d 13315 if (STRINGP (tem))
d39b6696 13316 {
d26b89b8 13317 prec = precision - n;
d39b6696 13318 if (frame_title_ptr)
5f5c8ee5 13319 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 13320 else
5f5c8ee5
GM
13321 n += display_string (NULL, tem, Qnil, 0, 0, it,
13322 0, prec, 0, -1);
d39b6696 13323 }
a2889657 13324 else if (!EQ (tem, elt))
5f5c8ee5
GM
13325 {
13326 /* Give up right away for nil or t. */
13327 elt = tem;
13328 goto tail_recurse;
13329 }
a2889657
JB
13330 }
13331 }
13332 break;
13333
13334 case Lisp_Cons:
13335 {
13336 register Lisp_Object car, tem;
13337
13338 /* A cons cell: three distinct cases.
13339 If first element is a string or a cons, process all the elements
13340 and effectively concatenate them.
13341 If first element is a negative number, truncate displaying cdr to
13342 at most that many characters. If positive, pad (with spaces)
13343 to at least that many characters.
13344 If first element is a symbol, process the cadr or caddr recursively
13345 according to whether the symbol's value is non-nil or nil. */
9472f927 13346 car = XCAR (elt);
5f5c8ee5
GM
13347 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13348 {
13349 /* An element of the form (:eval FORM) means evaluate FORM
13350 and use the result as mode line elements. */
13351 struct gcpro gcpro1;
13352 Lisp_Object spec;
13353
116d6f5c 13354 spec = safe_eval (XCAR (XCDR (elt)));
5f5c8ee5
GM
13355 GCPRO1 (spec);
13356 n += display_mode_element (it, depth, field_width - n,
13357 precision - n, spec);
13358 UNGCPRO;
13359 }
13360 else if (SYMBOLP (car))
a2889657
JB
13361 {
13362 tem = Fboundp (car);
9472f927 13363 elt = XCDR (elt);
e24c997d 13364 if (!CONSP (elt))
a2889657
JB
13365 goto invalid;
13366 /* elt is now the cdr, and we know it is a cons cell.
13367 Use its car if CAR has a non-nil value. */
265a9e55 13368 if (!NILP (tem))
a2889657
JB
13369 {
13370 tem = Fsymbol_value (car);
265a9e55 13371 if (!NILP (tem))
9472f927
GM
13372 {
13373 elt = XCAR (elt);
13374 goto tail_recurse;
13375 }
a2889657
JB
13376 }
13377 /* Symbol's value is nil (or symbol is unbound)
13378 Get the cddr of the original list
13379 and if possible find the caddr and use that. */
9472f927 13380 elt = XCDR (elt);
265a9e55 13381 if (NILP (elt))
a2889657 13382 break;
e24c997d 13383 else if (!CONSP (elt))
a2889657 13384 goto invalid;
9472f927 13385 elt = XCAR (elt);
a2889657
JB
13386 goto tail_recurse;
13387 }
e24c997d 13388 else if (INTEGERP (car))
a2889657
JB
13389 {
13390 register int lim = XINT (car);
9472f927 13391 elt = XCDR (elt);
a2889657 13392 if (lim < 0)
5f5c8ee5
GM
13393 {
13394 /* Negative int means reduce maximum width. */
13395 if (precision <= 0)
13396 precision = -lim;
13397 else
13398 precision = min (precision, -lim);
13399 }
a2889657
JB
13400 else if (lim > 0)
13401 {
13402 /* Padding specified. Don't let it be more than
13403 current maximum. */
5f5c8ee5
GM
13404 if (precision > 0)
13405 lim = min (precision, lim);
13406
a2889657
JB
13407 /* If that's more padding than already wanted, queue it.
13408 But don't reduce padding already specified even if
13409 that is beyond the current truncation point. */
5f5c8ee5 13410 field_width = max (lim, field_width);
a2889657
JB
13411 }
13412 goto tail_recurse;
13413 }
e24c997d 13414 else if (STRINGP (car) || CONSP (car))
a2889657
JB
13415 {
13416 register int limit = 50;
5f5c8ee5
GM
13417 /* Limit is to protect against circular lists. */
13418 while (CONSP (elt)
13419 && --limit > 0
13420 && (precision <= 0 || n < precision))
a2889657 13421 {
5f5c8ee5 13422 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
13423 precision - n, XCAR (elt));
13424 elt = XCDR (elt);
a2889657
JB
13425 }
13426 }
13427 }
13428 break;
13429
13430 default:
13431 invalid:
d39b6696 13432 if (frame_title_ptr)
5f5c8ee5 13433 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 13434 else
5f5c8ee5
GM
13435 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13436 precision - n, 0, 0);
13437 return n;
a2889657
JB
13438 }
13439
5f5c8ee5
GM
13440 /* Pad to FIELD_WIDTH. */
13441 if (field_width > 0 && n < field_width)
13442 {
13443 if (frame_title_ptr)
13444 n += store_frame_title ("", field_width - n, 0);
13445 else
13446 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13447 0, 0, 0);
13448 }
13449
13450 return n;
a2889657 13451}
5f5c8ee5
GM
13452
13453
766525bc
RS
13454/* Write a null-terminated, right justified decimal representation of
13455 the positive integer D to BUF using a minimal field width WIDTH. */
13456
13457static void
13458pint2str (buf, width, d)
13459 register char *buf;
13460 register int width;
13461 register int d;
13462{
13463 register char *p = buf;
13464
13465 if (d <= 0)
5f5c8ee5 13466 *p++ = '0';
766525bc 13467 else
5f5c8ee5 13468 {
766525bc 13469 while (d > 0)
5f5c8ee5 13470 {
766525bc
RS
13471 *p++ = d % 10 + '0';
13472 d /= 10;
5f5c8ee5
GM
13473 }
13474 }
13475
13476 for (width -= (int) (p - buf); width > 0; --width)
13477 *p++ = ' ';
766525bc
RS
13478 *p-- = '\0';
13479 while (p > buf)
5f5c8ee5 13480 {
766525bc
RS
13481 d = *buf;
13482 *buf++ = *p;
13483 *p-- = d;
5f5c8ee5 13484 }
766525bc
RS
13485}
13486
5f5c8ee5 13487/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
13488 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13489 type of CODING_SYSTEM. Return updated pointer into BUF. */
13490
6693a99a 13491static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 13492
1c9241f5
KH
13493static char *
13494decode_mode_spec_coding (coding_system, buf, eol_flag)
13495 Lisp_Object coding_system;
13496 register char *buf;
13497 int eol_flag;
13498{
1e1078d6 13499 Lisp_Object val;
916848d8 13500 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
13501 unsigned char *eol_str;
13502 int eol_str_len;
13503 /* The EOL conversion we are using. */
13504 Lisp_Object eoltype;
1e1078d6 13505
4a09dee0 13506 val = Fget (coding_system, Qcoding_system);
085536c2 13507 eoltype = Qnil;
1c9241f5 13508
4a09dee0 13509 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 13510 {
916848d8
RS
13511 if (multibyte)
13512 *buf++ = '-';
21e989e3 13513 if (eol_flag)
302f2b38 13514 eoltype = eol_mnemonic_undecided;
1e1078d6 13515 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
13516 }
13517 else
13518 {
1e1078d6
RS
13519 Lisp_Object eolvalue;
13520
13521 eolvalue = Fget (coding_system, Qeol_type);
13522
916848d8 13523 if (multibyte)
a61b7058 13524 *buf++ = XFASTINT (AREF (val, 1));
916848d8 13525
1c9241f5
KH
13526 if (eol_flag)
13527 {
1e1078d6
RS
13528 /* The EOL conversion that is normal on this system. */
13529
13530 if (NILP (eolvalue)) /* Not yet decided. */
13531 eoltype = eol_mnemonic_undecided;
13532 else if (VECTORP (eolvalue)) /* Not yet decided. */
13533 eoltype = eol_mnemonic_undecided;
13534 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13535 eoltype = (XFASTINT (eolvalue) == 0
13536 ? eol_mnemonic_unix
13537 : (XFASTINT (eolvalue) == 1
13538 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
13539 }
13540 }
5f5c8ee5 13541
302f2b38
EZ
13542 if (eol_flag)
13543 {
13544 /* Mention the EOL conversion if it is not the usual one. */
13545 if (STRINGP (eoltype))
13546 {
13547 eol_str = XSTRING (eoltype)->data;
13548 eol_str_len = XSTRING (eoltype)->size;
13549 }
f30b3499
KH
13550 else if (INTEGERP (eoltype)
13551 && CHAR_VALID_P (XINT (eoltype), 0))
13552 {
4a09dee0 13553 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 13554 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 13555 }
302f2b38
EZ
13556 else
13557 {
13558 eol_str = invalid_eol_type;
13559 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 13560 }
f30b3499 13561 bcopy (eol_str, buf, eol_str_len);
302f2b38 13562 buf += eol_str_len;
1c9241f5 13563 }
302f2b38 13564
1c9241f5
KH
13565 return buf;
13566}
13567
a2889657 13568/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
13569 generated by character C. PRECISION >= 0 means don't return a
13570 string longer than that value. FIELD_WIDTH > 0 means pad the
13571 string returned with spaces to that value. */
a2889657 13572
11e82b76
JB
13573static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13574
a2889657 13575static char *
5f5c8ee5 13576decode_mode_spec (w, c, field_width, precision)
a2889657 13577 struct window *w;
68c45bf0 13578 register int c;
5f5c8ee5 13579 int field_width, precision;
a2889657 13580{
0b67772d 13581 Lisp_Object obj;
5f5c8ee5
GM
13582 struct frame *f = XFRAME (WINDOW_FRAME (w));
13583 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 13584 struct buffer *b = XBUFFER (w->buffer);
a2889657 13585
0b67772d 13586 obj = Qnil;
a2889657
JB
13587
13588 switch (c)
13589 {
1af9f229
RS
13590 case '*':
13591 if (!NILP (b->read_only))
13592 return "%";
13593 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13594 return "*";
13595 return "-";
13596
13597 case '+':
13598 /* This differs from %* only for a modified read-only buffer. */
13599 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13600 return "*";
13601 if (!NILP (b->read_only))
13602 return "%";
13603 return "-";
13604
13605 case '&':
13606 /* This differs from %* in ignoring read-only-ness. */
13607 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13608 return "*";
13609 return "-";
13610
13611 case '%':
13612 return "%";
13613
13614 case '[':
13615 {
13616 int i;
13617 char *p;
13618
13619 if (command_loop_level > 5)
13620 return "[[[... ";
13621 p = decode_mode_spec_buf;
13622 for (i = 0; i < command_loop_level; i++)
13623 *p++ = '[';
13624 *p = 0;
13625 return decode_mode_spec_buf;
13626 }
13627
13628 case ']':
13629 {
13630 int i;
13631 char *p;
13632
13633 if (command_loop_level > 5)
13634 return " ...]]]";
13635 p = decode_mode_spec_buf;
13636 for (i = 0; i < command_loop_level; i++)
13637 *p++ = ']';
13638 *p = 0;
13639 return decode_mode_spec_buf;
13640 }
13641
13642 case '-':
13643 {
1af9f229 13644 register int i;
5f5c8ee5
GM
13645
13646 /* Let lots_of_dashes be a string of infinite length. */
13647 if (field_width <= 0
13648 || field_width > sizeof (lots_of_dashes))
1af9f229 13649 {
5f5c8ee5
GM
13650 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13651 decode_mode_spec_buf[i] = '-';
13652 decode_mode_spec_buf[i] = '\0';
13653 return decode_mode_spec_buf;
1af9f229 13654 }
5f5c8ee5
GM
13655 else
13656 return lots_of_dashes;
1af9f229
RS
13657 }
13658
a2889657 13659 case 'b':
d39b6696 13660 obj = b->name;
a2889657
JB
13661 break;
13662
1af9f229
RS
13663 case 'c':
13664 {
13665 int col = current_column ();
ac90c44f 13666 w->column_number_displayed = make_number (col);
5f5c8ee5 13667 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
13668 return decode_mode_spec_buf;
13669 }
13670
13671 case 'F':
13672 /* %F displays the frame name. */
5f5c8ee5 13673 if (!NILP (f->title))
95184b48 13674 return (char *) XSTRING (f->title)->data;
fd8ff63d 13675 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 13676 return (char *) XSTRING (f->name)->data;
9c6da96f 13677 return "Emacs";
1af9f229 13678
a2889657 13679 case 'f':
d39b6696 13680 obj = b->filename;
a2889657
JB
13681 break;
13682
aa6d10fa
RS
13683 case 'l':
13684 {
12adba34
RS
13685 int startpos = XMARKER (w->start)->charpos;
13686 int startpos_byte = marker_byte_position (w->start);
13687 int line, linepos, linepos_byte, topline;
aa6d10fa 13688 int nlines, junk;
aa6d10fa
RS
13689 int height = XFASTINT (w->height);
13690
13691 /* If we decided that this buffer isn't suitable for line numbers,
13692 don't forget that too fast. */
13693 if (EQ (w->base_line_pos, w->buffer))
766525bc 13694 goto no_value;
5300fd39
RS
13695 /* But do forget it, if the window shows a different buffer now. */
13696 else if (BUFFERP (w->base_line_pos))
13697 w->base_line_pos = Qnil;
aa6d10fa
RS
13698
13699 /* If the buffer is very big, don't waste time. */
37d034d3 13700 if (INTEGERP (Vline_number_display_limit)
090703f4 13701 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
13702 {
13703 w->base_line_pos = Qnil;
13704 w->base_line_number = Qnil;
766525bc 13705 goto no_value;
aa6d10fa
RS
13706 }
13707
13708 if (!NILP (w->base_line_number)
13709 && !NILP (w->base_line_pos)
12adba34 13710 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
13711 {
13712 line = XFASTINT (w->base_line_number);
13713 linepos = XFASTINT (w->base_line_pos);
12adba34 13714 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
13715 }
13716 else
13717 {
13718 line = 1;
d39b6696 13719 linepos = BUF_BEGV (b);
12adba34 13720 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
13721 }
13722
13723 /* Count lines from base line to window start position. */
12adba34
RS
13724 nlines = display_count_lines (linepos, linepos_byte,
13725 startpos_byte,
13726 startpos, &junk);
aa6d10fa
RS
13727
13728 topline = nlines + line;
13729
13730 /* Determine a new base line, if the old one is too close
13731 or too far away, or if we did not have one.
13732 "Too close" means it's plausible a scroll-down would
13733 go back past it. */
d39b6696 13734 if (startpos == BUF_BEGV (b))
aa6d10fa 13735 {
ac90c44f
GM
13736 w->base_line_number = make_number (topline);
13737 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
13738 }
13739 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 13740 || linepos == BUF_BEGV (b))
aa6d10fa 13741 {
d39b6696 13742 int limit = BUF_BEGV (b);
12adba34 13743 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 13744 int position;
5d121aec 13745 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
13746
13747 if (startpos - distance > limit)
12adba34
RS
13748 {
13749 limit = startpos - distance;
13750 limit_byte = CHAR_TO_BYTE (limit);
13751 }
aa6d10fa 13752
12adba34
RS
13753 nlines = display_count_lines (startpos, startpos_byte,
13754 limit_byte,
13755 - (height * 2 + 30),
aa6d10fa
RS
13756 &position);
13757 /* If we couldn't find the lines we wanted within
5d121aec 13758 line_number_display_limit_width chars per line,
aa6d10fa 13759 give up on line numbers for this window. */
12adba34 13760 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
13761 {
13762 w->base_line_pos = w->buffer;
13763 w->base_line_number = Qnil;
766525bc 13764 goto no_value;
aa6d10fa
RS
13765 }
13766
ac90c44f
GM
13767 w->base_line_number = make_number (topline - nlines);
13768 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
13769 }
13770
13771 /* Now count lines from the start pos to point. */
12adba34
RS
13772 nlines = display_count_lines (startpos, startpos_byte,
13773 PT_BYTE, PT, &junk);
aa6d10fa
RS
13774
13775 /* Record that we did display the line number. */
13776 line_number_displayed = 1;
13777
13778 /* Make the string to show. */
5f5c8ee5 13779 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 13780 return decode_mode_spec_buf;
766525bc
RS
13781 no_value:
13782 {
13783 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
13784 int pad = field_width - 2;
13785 while (pad-- > 0)
13786 *p++ = ' ';
13787 *p++ = '?';
b357b9d4
KR
13788 *p++ = '?';
13789 *p = '\0';
766525bc
RS
13790 return decode_mode_spec_buf;
13791 }
aa6d10fa
RS
13792 }
13793 break;
13794
a2889657 13795 case 'm':
d39b6696 13796 obj = b->mode_name;
a2889657
JB
13797 break;
13798
13799 case 'n':
d39b6696 13800 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
13801 return " Narrow";
13802 break;
13803
a2889657
JB
13804 case 'p':
13805 {
13806 int pos = marker_position (w->start);
d39b6696 13807 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 13808
d39b6696 13809 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 13810 {
d39b6696 13811 if (pos <= BUF_BEGV (b))
a2889657
JB
13812 return "All";
13813 else
13814 return "Bottom";
13815 }
d39b6696 13816 else if (pos <= BUF_BEGV (b))
a2889657
JB
13817 return "Top";
13818 else
13819 {
3c7d31b9
RS
13820 if (total > 1000000)
13821 /* Do it differently for a large value, to avoid overflow. */
13822 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13823 else
13824 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
13825 /* We can't normally display a 3-digit number,
13826 so get us a 2-digit number that is close. */
13827 if (total == 100)
13828 total = 99;
13829 sprintf (decode_mode_spec_buf, "%2d%%", total);
13830 return decode_mode_spec_buf;
13831 }
13832 }
13833
8ffcb79f
RS
13834 /* Display percentage of size above the bottom of the screen. */
13835 case 'P':
13836 {
13837 int toppos = marker_position (w->start);
d39b6696
KH
13838 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13839 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 13840
d39b6696 13841 if (botpos >= BUF_ZV (b))
8ffcb79f 13842 {
d39b6696 13843 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
13844 return "All";
13845 else
13846 return "Bottom";
13847 }
13848 else
13849 {
3c7d31b9
RS
13850 if (total > 1000000)
13851 /* Do it differently for a large value, to avoid overflow. */
13852 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13853 else
13854 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
13855 /* We can't normally display a 3-digit number,
13856 so get us a 2-digit number that is close. */
13857 if (total == 100)
13858 total = 99;
d39b6696 13859 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
13860 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13861 else
13862 sprintf (decode_mode_spec_buf, "%2d%%", total);
13863 return decode_mode_spec_buf;
13864 }
13865 }
13866
1af9f229
RS
13867 case 's':
13868 /* status of process */
13869 obj = Fget_buffer_process (w->buffer);
13870 if (NILP (obj))
13871 return "no process";
13872#ifdef subprocesses
13873 obj = Fsymbol_name (Fprocess_status (obj));
13874#endif
13875 break;
d39b6696 13876
1af9f229
RS
13877 case 't': /* indicate TEXT or BINARY */
13878#ifdef MODE_LINE_BINARY_TEXT
13879 return MODE_LINE_BINARY_TEXT (b);
13880#else
13881 return "T";
13882#endif
1c9241f5
KH
13883
13884 case 'z':
13885 /* coding-system (not including end-of-line format) */
13886 case 'Z':
13887 /* coding-system (including end-of-line type) */
13888 {
13889 int eol_flag = (c == 'Z');
539b4d41 13890 char *p = decode_mode_spec_buf;
1c9241f5 13891
d30e754b 13892 if (! FRAME_WINDOW_P (f))
1c9241f5 13893 {
11c52c4f
RS
13894 /* No need to mention EOL here--the terminal never needs
13895 to do EOL conversion. */
13896 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13897 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 13898 }
f13c925f 13899 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 13900 p, eol_flag);
f13c925f 13901
11c52c4f 13902#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
13903#ifdef subprocesses
13904 obj = Fget_buffer_process (Fcurrent_buffer ());
13905 if (PROCESSP (obj))
13906 {
13907 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13908 p, eol_flag);
13909 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13910 p, eol_flag);
13911 }
13912#endif /* subprocesses */
11c52c4f 13913#endif /* 0 */
1c9241f5
KH
13914 *p = 0;
13915 return decode_mode_spec_buf;
13916 }
a2889657 13917 }
d39b6696 13918
e24c997d 13919 if (STRINGP (obj))
a2889657
JB
13920 return (char *) XSTRING (obj)->data;
13921 else
13922 return "";
13923}
5f5c8ee5
GM
13924
13925
12adba34
RS
13926/* Count up to COUNT lines starting from START / START_BYTE.
13927 But don't go beyond LIMIT_BYTE.
13928 Return the number of lines thus found (always nonnegative).
59b49f63 13929
12adba34 13930 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
13931
13932static int
12adba34
RS
13933display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13934 int start, start_byte, limit_byte, count;
13935 int *byte_pos_ptr;
59b49f63 13936{
59b49f63
RS
13937 register unsigned char *cursor;
13938 unsigned char *base;
13939
13940 register int ceiling;
13941 register unsigned char *ceiling_addr;
12adba34 13942 int orig_count = count;
59b49f63
RS
13943
13944 /* If we are not in selective display mode,
13945 check only for newlines. */
12adba34
RS
13946 int selective_display = (!NILP (current_buffer->selective_display)
13947 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
13948
13949 if (count > 0)
12adba34
RS
13950 {
13951 while (start_byte < limit_byte)
13952 {
13953 ceiling = BUFFER_CEILING_OF (start_byte);
13954 ceiling = min (limit_byte - 1, ceiling);
13955 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13956 base = (cursor = BYTE_POS_ADDR (start_byte));
13957 while (1)
13958 {
13959 if (selective_display)
13960 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13961 ;
13962 else
13963 while (*cursor != '\n' && ++cursor != ceiling_addr)
13964 ;
13965
13966 if (cursor != ceiling_addr)
13967 {
13968 if (--count == 0)
13969 {
13970 start_byte += cursor - base + 1;
13971 *byte_pos_ptr = start_byte;
13972 return orig_count;
13973 }
13974 else
13975 if (++cursor == ceiling_addr)
13976 break;
13977 }
13978 else
13979 break;
13980 }
13981 start_byte += cursor - base;
13982 }
13983 }
59b49f63
RS
13984 else
13985 {
12adba34
RS
13986 while (start_byte > limit_byte)
13987 {
13988 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13989 ceiling = max (limit_byte, ceiling);
13990 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13991 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
13992 while (1)
13993 {
12adba34
RS
13994 if (selective_display)
13995 while (--cursor != ceiling_addr
13996 && *cursor != '\n' && *cursor != 015)
13997 ;
13998 else
13999 while (--cursor != ceiling_addr && *cursor != '\n')
14000 ;
14001
59b49f63
RS
14002 if (cursor != ceiling_addr)
14003 {
14004 if (++count == 0)
14005 {
12adba34
RS
14006 start_byte += cursor - base + 1;
14007 *byte_pos_ptr = start_byte;
14008 /* When scanning backwards, we should
14009 not count the newline posterior to which we stop. */
14010 return - orig_count - 1;
59b49f63
RS
14011 }
14012 }
14013 else
14014 break;
14015 }
12adba34
RS
14016 /* Here we add 1 to compensate for the last decrement
14017 of CURSOR, which took it past the valid range. */
14018 start_byte += cursor - base + 1;
59b49f63
RS
14019 }
14020 }
14021
12adba34 14022 *byte_pos_ptr = limit_byte;
aa6d10fa 14023
12adba34
RS
14024 if (count < 0)
14025 return - orig_count + count;
14026 return orig_count - count;
aa6d10fa 14027
12adba34 14028}
a2889657 14029
a2889657 14030
5f5c8ee5
GM
14031\f
14032/***********************************************************************
14033 Displaying strings
14034 ***********************************************************************/
278feba9 14035
5f5c8ee5 14036/* Display a NUL-terminated string, starting with index START.
a3788d53 14037
5f5c8ee5
GM
14038 If STRING is non-null, display that C string. Otherwise, the Lisp
14039 string LISP_STRING is displayed.
a2889657 14040
5f5c8ee5
GM
14041 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14042 FACE_STRING. Display STRING or LISP_STRING with the face at
14043 FACE_STRING_POS in FACE_STRING:
a2889657 14044
5f5c8ee5
GM
14045 Display the string in the environment given by IT, but use the
14046 standard display table, temporarily.
a3788d53 14047
5f5c8ee5
GM
14048 FIELD_WIDTH is the minimum number of output glyphs to produce.
14049 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14050 with spaces. If STRING has more characters, more than FIELD_WIDTH
14051 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14052
14053 PRECISION is the maximum number of characters to output from
14054 STRING. PRECISION < 0 means don't truncate the string.
a2889657 14055
5f5c8ee5 14056 This is roughly equivalent to printf format specifiers:
a2889657 14057
5f5c8ee5
GM
14058 FIELD_WIDTH PRECISION PRINTF
14059 ----------------------------------------
14060 -1 -1 %s
14061 -1 10 %.10s
14062 10 -1 %10s
14063 20 10 %20.10s
a2889657 14064
5f5c8ee5
GM
14065 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14066 display them, and < 0 means obey the current buffer's value of
14067 enable_multibyte_characters.
278feba9 14068
5f5c8ee5 14069 Value is the number of glyphs produced. */
b1d1124b 14070
5f5c8ee5
GM
14071static int
14072display_string (string, lisp_string, face_string, face_string_pos,
14073 start, it, field_width, precision, max_x, multibyte)
14074 unsigned char *string;
14075 Lisp_Object lisp_string;
68c45bf0
PE
14076 Lisp_Object face_string;
14077 int face_string_pos;
5f5c8ee5
GM
14078 int start;
14079 struct it *it;
14080 int field_width, precision, max_x;
14081 int multibyte;
14082{
14083 int hpos_at_start = it->hpos;
14084 int saved_face_id = it->face_id;
14085 struct glyph_row *row = it->glyph_row;
14086
14087 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 14088 with index START. */
5f5c8ee5
GM
14089 reseat_to_string (it, string, lisp_string, start,
14090 precision, field_width, multibyte);
14091
14092 /* If displaying STRING, set up the face of the iterator
14093 from LISP_STRING, if that's given. */
14094 if (STRINGP (face_string))
14095 {
14096 int endptr;
14097 struct face *face;
14098
14099 it->face_id
14100 = face_at_string_position (it->w, face_string, face_string_pos,
14101 0, it->region_beg_charpos,
14102 it->region_end_charpos,
5de7c6f2 14103 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
14104 face = FACE_FROM_ID (it->f, it->face_id);
14105 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 14106 }
a2889657 14107
5f5c8ee5
GM
14108 /* Set max_x to the maximum allowed X position. Don't let it go
14109 beyond the right edge of the window. */
14110 if (max_x <= 0)
14111 max_x = it->last_visible_x;
14112 else
14113 max_x = min (max_x, it->last_visible_x);
efc63ef0 14114
5f5c8ee5
GM
14115 /* Skip over display elements that are not visible. because IT->w is
14116 hscrolled. */
14117 if (it->current_x < it->first_visible_x)
14118 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14119 MOVE_TO_POS | MOVE_TO_X);
a2889657 14120
5f5c8ee5
GM
14121 row->ascent = it->max_ascent;
14122 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14123 row->phys_ascent = it->max_phys_ascent;
14124 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 14125
5f5c8ee5
GM
14126 /* This condition is for the case that we are called with current_x
14127 past last_visible_x. */
14128 while (it->current_x < max_x)
a2889657 14129 {
5f5c8ee5 14130 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 14131
5f5c8ee5
GM
14132 /* Get the next display element. */
14133 if (!get_next_display_element (it))
90adcf20 14134 break;
1c9241f5 14135
5f5c8ee5
GM
14136 /* Produce glyphs. */
14137 x_before = it->current_x;
14138 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14139 PRODUCE_GLYPHS (it);
90adcf20 14140
5f5c8ee5
GM
14141 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14142 i = 0;
14143 x = x_before;
14144 while (i < nglyphs)
a2889657 14145 {
5f5c8ee5
GM
14146 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14147
14148 if (!it->truncate_lines_p
14149 && x + glyph->pixel_width > max_x)
14150 {
14151 /* End of continued line or max_x reached. */
37be86f2
KH
14152 if (CHAR_GLYPH_PADDING_P (*glyph))
14153 {
14154 /* A wide character is unbreakable. */
14155 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14156 it->current_x = x_before;
14157 }
14158 else
14159 {
14160 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14161 it->current_x = x;
14162 }
5f5c8ee5
GM
14163 break;
14164 }
14165 else if (x + glyph->pixel_width > it->first_visible_x)
14166 {
14167 /* Glyph is at least partially visible. */
14168 ++it->hpos;
14169 if (x < it->first_visible_x)
14170 it->glyph_row->x = x - it->first_visible_x;
14171 }
14172 else
a2889657 14173 {
5f5c8ee5
GM
14174 /* Glyph is off the left margin of the display area.
14175 Should not happen. */
14176 abort ();
a2889657 14177 }
5f5c8ee5
GM
14178
14179 row->ascent = max (row->ascent, it->max_ascent);
14180 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14181 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14182 row->phys_height = max (row->phys_height,
14183 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14184 x += glyph->pixel_width;
14185 ++i;
a2889657 14186 }
5f5c8ee5
GM
14187
14188 /* Stop if max_x reached. */
14189 if (i < nglyphs)
14190 break;
14191
14192 /* Stop at line ends. */
14193 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 14194 {
5f5c8ee5
GM
14195 it->continuation_lines_width = 0;
14196 break;
a2889657 14197 }
1c9241f5 14198
cafafe0b 14199 set_iterator_to_next (it, 1);
a688bb24 14200
5f5c8ee5
GM
14201 /* Stop if truncating at the right edge. */
14202 if (it->truncate_lines_p
14203 && it->current_x >= it->last_visible_x)
14204 {
14205 /* Add truncation mark, but don't do it if the line is
14206 truncated at a padding space. */
14207 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 14208 {
5f5c8ee5 14209 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
14210 {
14211 int i, n;
14212
9299cb15 14213 if (it->current_x > it->last_visible_x)
37be86f2 14214 {
9299cb15
KH
14215 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14216 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14217 break;
14218 for (n = row->used[TEXT_AREA]; i < n; ++i)
14219 {
14220 row->used[TEXT_AREA] = i;
14221 produce_special_glyphs (it, IT_TRUNCATION);
14222 }
37be86f2 14223 }
9299cb15 14224 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 14225 }
5f5c8ee5 14226 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 14227 }
5f5c8ee5 14228 break;
1c9241f5 14229 }
a2889657
JB
14230 }
14231
5f5c8ee5
GM
14232 /* Maybe insert a truncation at the left. */
14233 if (it->first_visible_x
14234 && IT_CHARPOS (*it) > 0)
a2889657 14235 {
5f5c8ee5
GM
14236 if (!FRAME_WINDOW_P (it->f))
14237 insert_left_trunc_glyphs (it);
14238 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
14239 }
14240
5f5c8ee5
GM
14241 it->face_id = saved_face_id;
14242
14243 /* Value is number of columns displayed. */
14244 return it->hpos - hpos_at_start;
14245}
a2889657 14246
a2889657 14247
a2889657 14248\f
5f5c8ee5
GM
14249/* This is like a combination of memq and assq. Return 1 if PROPVAL
14250 appears as an element of LIST or as the car of an element of LIST.
14251 If PROPVAL is a list, compare each element against LIST in that
14252 way, and return 1 if any element of PROPVAL is found in LIST.
14253 Otherwise return 0. This function cannot quit. */
642eefc6
RS
14254
14255int
14256invisible_p (propval, list)
14257 register Lisp_Object propval;
14258 Lisp_Object list;
14259{
af460d46 14260 register Lisp_Object tail, proptail;
715e84c9 14261
9472f927 14262 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
14263 {
14264 register Lisp_Object tem;
9472f927 14265 tem = XCAR (tail);
642eefc6
RS
14266 if (EQ (propval, tem))
14267 return 1;
9472f927 14268 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
14269 return 1;
14270 }
715e84c9 14271
af460d46 14272 if (CONSP (propval))
715e84c9
GM
14273 {
14274 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14275 {
14276 Lisp_Object propelt;
14277 propelt = XCAR (proptail);
14278 for (tail = list; CONSP (tail); tail = XCDR (tail))
14279 {
14280 register Lisp_Object tem;
14281 tem = XCAR (tail);
14282 if (EQ (propelt, tem))
14283 return 1;
14284 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14285 return 1;
14286 }
14287 }
14288 }
14289
642eefc6
RS
14290 return 0;
14291}
14292
5f5c8ee5
GM
14293
14294/* Return 1 if PROPVAL appears as the car of an element of LIST and
14295 the cdr of that element is non-nil. If PROPVAL is a list, check
14296 each element of PROPVAL in that way, and the first time some
14297 element is found, return 1 if the cdr of that element is non-nil.
14298 Otherwise return 0. This function cannot quit. */
642eefc6
RS
14299
14300int
14301invisible_ellipsis_p (propval, list)
14302 register Lisp_Object propval;
14303 Lisp_Object list;
14304{
af460d46 14305 register Lisp_Object tail, proptail;
9472f927
GM
14306
14307 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
14308 {
14309 register Lisp_Object tem;
9472f927
GM
14310 tem = XCAR (tail);
14311 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14312 return ! NILP (XCDR (tem));
642eefc6 14313 }
9472f927 14314
af460d46 14315 if (CONSP (propval))
9472f927 14316 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
14317 {
14318 Lisp_Object propelt;
9472f927
GM
14319 propelt = XCAR (proptail);
14320 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
14321 {
14322 register Lisp_Object tem;
9472f927
GM
14323 tem = XCAR (tail);
14324 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14325 return ! NILP (XCDR (tem));
af460d46
RS
14326 }
14327 }
9472f927 14328
642eefc6
RS
14329 return 0;
14330}
5f5c8ee5
GM
14331
14332
642eefc6 14333\f
5f5c8ee5
GM
14334/***********************************************************************
14335 Initialization
14336 ***********************************************************************/
14337
a2889657
JB
14338void
14339syms_of_xdisp ()
14340{
c6e89d6c
GM
14341 Vwith_echo_area_save_vector = Qnil;
14342 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 14343
c6e89d6c
GM
14344 Vmessage_stack = Qnil;
14345 staticpro (&Vmessage_stack);
14346
735c094c 14347 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 14348 staticpro (&Qinhibit_redisplay);
735c094c 14349
5f5c8ee5
GM
14350#if GLYPH_DEBUG
14351 defsubr (&Sdump_glyph_matrix);
14352 defsubr (&Sdump_glyph_row);
e037b9ec 14353 defsubr (&Sdump_tool_bar_row);
62397849 14354 defsubr (&Strace_redisplay);
bf9249e3 14355 defsubr (&Strace_to_stderr);
5f5c8ee5 14356#endif
99a5de87 14357#ifdef HAVE_WINDOW_SYSTEM
57c28064 14358 defsubr (&Stool_bar_lines_needed);
99a5de87 14359#endif
5f5c8ee5 14360
cf074754
RS
14361 staticpro (&Qmenu_bar_update_hook);
14362 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14363
d46fb96a 14364 staticpro (&Qoverriding_terminal_local_map);
7079aefa 14365 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 14366
399164b4
KH
14367 staticpro (&Qoverriding_local_map);
14368 Qoverriding_local_map = intern ("overriding-local-map");
14369
75c43375
RS
14370 staticpro (&Qwindow_scroll_functions);
14371 Qwindow_scroll_functions = intern ("window-scroll-functions");
14372
e0bfbde6
RS
14373 staticpro (&Qredisplay_end_trigger_functions);
14374 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 14375
2e54982e
RS
14376 staticpro (&Qinhibit_point_motion_hooks);
14377 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14378
9499d71b
GM
14379 QCdata = intern (":data");
14380 staticpro (&QCdata);
5f5c8ee5 14381 Qdisplay = intern ("display");
f3751a65 14382 staticpro (&Qdisplay);
5f5c8ee5
GM
14383 Qspace_width = intern ("space-width");
14384 staticpro (&Qspace_width);
5f5c8ee5
GM
14385 Qraise = intern ("raise");
14386 staticpro (&Qraise);
14387 Qspace = intern ("space");
14388 staticpro (&Qspace);
f3751a65
GM
14389 Qmargin = intern ("margin");
14390 staticpro (&Qmargin);
5f5c8ee5 14391 Qleft_margin = intern ("left-margin");
f3751a65 14392 staticpro (&Qleft_margin);
5f5c8ee5 14393 Qright_margin = intern ("right-margin");
f3751a65 14394 staticpro (&Qright_margin);
5f5c8ee5
GM
14395 Qalign_to = intern ("align-to");
14396 staticpro (&Qalign_to);
14397 QCalign_to = intern (":align-to");
14398 staticpro (&QCalign_to);
5f5c8ee5
GM
14399 Qrelative_width = intern ("relative-width");
14400 staticpro (&Qrelative_width);
14401 QCrelative_width = intern (":relative-width");
14402 staticpro (&QCrelative_width);
14403 QCrelative_height = intern (":relative-height");
14404 staticpro (&QCrelative_height);
14405 QCeval = intern (":eval");
14406 staticpro (&QCeval);
d3acf96b 14407 Qwhen = intern ("when");
f3751a65 14408 staticpro (&Qwhen);
886bd6f2
GM
14409 QCfile = intern (":file");
14410 staticpro (&QCfile);
5f5c8ee5
GM
14411 Qfontified = intern ("fontified");
14412 staticpro (&Qfontified);
14413 Qfontification_functions = intern ("fontification-functions");
14414 staticpro (&Qfontification_functions);
5f5c8ee5
GM
14415 Qtrailing_whitespace = intern ("trailing-whitespace");
14416 staticpro (&Qtrailing_whitespace);
14417 Qimage = intern ("image");
14418 staticpro (&Qimage);
ad4f174e
GM
14419 Qmessage_truncate_lines = intern ("message-truncate-lines");
14420 staticpro (&Qmessage_truncate_lines);
6422c1d7
GM
14421 Qgrow_only = intern ("grow-only");
14422 staticpro (&Qgrow_only);
e1477f43
GM
14423 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14424 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
14425 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14426 staticpro (&Qinhibit_eval_during_redisplay);
5f5c8ee5 14427
a2889657
JB
14428 last_arrow_position = Qnil;
14429 last_arrow_string = Qnil;
f3751a65
GM
14430 staticpro (&last_arrow_position);
14431 staticpro (&last_arrow_string);
c6e89d6c
GM
14432
14433 echo_buffer[0] = echo_buffer[1] = Qnil;
14434 staticpro (&echo_buffer[0]);
14435 staticpro (&echo_buffer[1]);
14436
14437 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14438 staticpro (&echo_area_buffer[0]);
14439 staticpro (&echo_area_buffer[1]);
a2889657 14440
6a94510a
GM
14441 Vmessages_buffer_name = build_string ("*Messages*");
14442 staticpro (&Vmessages_buffer_name);
14443
8f897821
GM
14444 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14445 "Non-nil means highlight trailing whitespace.\n\
14446The face used for trailing whitespace is `trailing-whitespace'.");
14447 Vshow_trailing_whitespace = Qnil;
14448
735c094c
KH
14449 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14450 "Non-nil means don't actually do any redisplay.\n\
14451This is used for internal purposes.");
14452 Vinhibit_redisplay = Qnil;
14453
a2889657 14454 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 14455 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
14456 Vglobal_mode_string = Qnil;
14457
14458 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14459 "Marker for where to display an arrow on top of the buffer text.\n\
14460This must be the beginning of a line in order to work.\n\
14461See also `overlay-arrow-string'.");
14462 Voverlay_arrow_position = Qnil;
14463
14464 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14465 "String to display as an arrow. See also `overlay-arrow-position'.");
14466 Voverlay_arrow_string = Qnil;
14467
14468 DEFVAR_INT ("scroll-step", &scroll_step,
14469 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e 14470If that fails to bring point back on frame, point is centered instead.\n\
0894e696
SM
14471If this is zero, point is always centered after it moves off frame.\n\
14472If you want scrolling to always be a line at a time, you should set\n\
14473 `scroll-conservatively' to a large value rather than set this to 1.");
a2889657 14474
0789adb2 14475 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
090f7baa
GM
14476 "*Scroll up to this many lines, to bring point back on screen.\n\
14477A value of zero means to scroll the text to center point vertically\n\
14478in the window.");
0789adb2
RS
14479 scroll_conservatively = 0;
14480
9afd2168
RS
14481 DEFVAR_INT ("scroll-margin", &scroll_margin,
14482 "*Number of lines of margin at the top and bottom of a window.\n\
14483Recenter the window whenever point gets within this many lines\n\
14484of the top or bottom of the window.");
14485 scroll_margin = 0;
14486
5f5c8ee5 14487#if GLYPH_DEBUG
a2889657 14488 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 14489#endif
a2889657
JB
14490
14491 DEFVAR_BOOL ("truncate-partial-width-windows",
14492 &truncate_partial_width_windows,
44fa5b1e 14493 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
14494 truncate_partial_width_windows = 1;
14495
14496 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
1862a24e
MB
14497 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14498Any other value means to use the appropriate face, `mode-line',\n\
14499`header-line', or `menu' respectively.\n\
1b6022cf 14500\n\
1862a24e
MB
14501This variable is deprecated; please change the above faces instead.");
14502 mode_line_inverse_video = 1;
aa6d10fa 14503
090703f4 14504 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
5f5c8ee5 14505 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 14506If the buffer is bigger than this, the line number does not appear\n\
090703f4
GM
14507in the mode line. A value of nil means no limit.");
14508 Vline_number_display_limit = Qnil;
fba9ce76 14509
090703f4
GM
14510 DEFVAR_INT ("line-number-display-limit-width",
14511 &line_number_display_limit_width,
5d121aec
KH
14512 "*Maximum line width (in characters) for line number display.\n\
14513If the average length of the lines near point is bigger than this, then the\n\
14514line number may be omitted from the mode line.");
14515 line_number_display_limit_width = 200;
14516
fba9ce76
RS
14517 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14518 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 14519 highlight_nonselected_windows = 0;
d39b6696
KH
14520
14521 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
14522 "Non-nil if more than one frame is visible on this display.\n\
14523Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
14524This variable is not guaranteed to be accurate except while processing\n\
14525`frame-title-format' and `icon-title-format'.");
d39b6696
KH
14526
14527 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 14528 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
14529\(Assuming the window manager supports this feature.)\n\
14530This variable has the same structure as `mode-line-format' (which see),\n\
14531and is used only on frames for which no explicit name has been set\n\
14532\(see `modify-frame-parameters').");
14533 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 14534 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
14535\(Assuming the window manager supports this feature.)\n\
14536This variable has the same structure as `mode-line-format' (which see),\n\
14537and is used only on frames for which no explicit name has been set\n\
14538\(see `modify-frame-parameters').");
14539 Vicon_title_format
14540 = Vframe_title_format
14541 = Fcons (intern ("multiple-frames"),
14542 Fcons (build_string ("%b"),
14543 Fcons (Fcons (build_string (""),
14544 Fcons (intern ("invocation-name"),
14545 Fcons (build_string ("@"),
14546 Fcons (intern ("system-name"),
14547 Qnil)))),
14548 Qnil)));
5992c4f7
KH
14549
14550 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14551 "Maximum number of lines to keep in the message log buffer.\n\
14552If nil, disable message logging. If t, log messages but don't truncate\n\
14553the buffer when it becomes large.");
ac90c44f 14554 Vmessage_log_max = make_number (50);
08b610e4
RS
14555
14556 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14557 "Functions called before redisplay, if window sizes have changed.\n\
14558The value should be a list of functions that take one argument.\n\
14559Just before redisplay, for each frame, if any of its windows have changed\n\
14560size since the last redisplay, or have been split or deleted,\n\
14561all the functions in the list are called, with the frame as argument.");
14562 Vwindow_size_change_functions = Qnil;
75c43375
RS
14563
14564 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 14565 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 14566Each function is called with two arguments, the window\n\
8d9583b0
RS
14567and its new display-start position. Note that the value of `window-end'\n\
14568is not valid when these functions are called.");
75c43375 14569 Vwindow_scroll_functions = Qnil;
5f5c8ee5 14570
e037b9ec
GM
14571 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14572 "*Non-nil means automatically resize tool-bars.\n\
14573This increases a tool-bar's height if not all tool-bar items are visible.\n\
14574It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 14575otherwise.");
e037b9ec 14576 auto_resize_tool_bars_p = 1;
5f5c8ee5 14577
e037b9ec
GM
14578 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14579 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14580 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 14581
35a41507
GM
14582 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14583 "*Margin around tool-bar buttons in pixels.\n\
14584If an integer, use that for both horizontal and vertical margins.\n\
14585Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14586HORZ specifying the horizontal margin, and VERT specifying the\n\
14587vertical margin.");
c3d76173 14588 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 14589
e037b9ec
GM
14590 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14591 "Relief thickness of tool-bar buttons.");
c3d76173 14592 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5
GM
14593
14594 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14595 "List of functions to call to fontify regions of text.\n\
14596Each function is called with one argument POS. Functions must\n\
14597fontify a region starting at POS in the current buffer, and give\n\
14598fontified regions the property `fontified'.\n\
14599This variable automatically becomes buffer-local when set.");
14600 Vfontification_functions = Qnil;
6b9f0906 14601 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
14602
14603 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
14604 &unibyte_display_via_language_environment,
14605 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
14606Specifically this means that unibyte non-ASCII characters\n\
14607are displayed by converting them to the equivalent multibyte characters\n\
14608according to the current language environment. As a result, they are\n\
14609displayed according to the current fontset.");
14610 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
14611
14612 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14613 "*Maximum height for resizing mini-windows.\n\
14614If a float, it specifies a fraction of the mini-window frame's height.\n\
6422c1d7 14615If an integer, it specifies a number of lines.");
c6e89d6c 14616 Vmax_mini_window_height = make_float (0.25);
6422c1d7
GM
14617
14618 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
504454e8 14619 "*How to resize mini-windows.\n\
6422c1d7 14620A value of nil means don't automatically resize mini-windows.\n\
504454e8 14621A value of t means resize them to fit the text displayed in them.\n\
6422c1d7 14622A value of `grow-only', the default, means let mini-windows grow\n\
504454e8
GM
14623only, until their display becomes empty, at which point the windows\n\
14624go back to their normal size.");
6422c1d7
GM
14625 Vresize_mini_windows = Qgrow_only;
14626
d6d26ed3 14627 DEFVAR_BOOL ("cursor-in-non-selected-windows",
f8156156 14628 &cursor_in_non_selected_windows,
d6d26ed3
GM
14629 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14630Nil means don't display a cursor there.");
14631 cursor_in_non_selected_windows = 1;
d475bcb8
GM
14632
14633 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14634 "*Non-nil means scroll the display automatically to make point visible.");
14635 automatic_hscrolling_p = 1;
e00daaa0
GM
14636
14637 DEFVAR_LISP ("image-types", &Vimage_types,
ad4f174e 14638 "List of supported image types.\n\
e00daaa0
GM
14639Each element of the list is a symbol for a supported image type.");
14640 Vimage_types = Qnil;
ad4f174e
GM
14641
14642 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14643 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14644Bind this around calls to `message' to let it take effect.");
14645 message_truncate_lines = 0;
0bca8940
DL
14646
14647 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14648 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14649Can be used to update submenus whose contents should vary.");
6422c1d7 14650 Vmenu_bar_update_hook = Qnil;
e1477f43
GM
14651
14652 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14653 "Non-nil means don't update menu bars. Internal use only.");
14654 inhibit_menubar_update = 0;
30a3f61c
GM
14655
14656 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14657 "Non-nil means don't eval Lisp during redisplay.");
14658 inhibit_eval_during_redisplay = 0;
a2889657
JB
14659}
14660
5f5c8ee5
GM
14661
14662/* Initialize this module when Emacs starts. */
14663
dfcf069d 14664void
a2889657
JB
14665init_xdisp ()
14666{
14667 Lisp_Object root_window;
5f5c8ee5 14668 struct window *mini_w;
a2889657 14669
04612a64
GM
14670 current_header_line_height = current_mode_line_height = -1;
14671
5f5c8ee5 14672 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
14673
14674 mini_w = XWINDOW (minibuf_window);
11e82b76 14675 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 14676
a2889657
JB
14677 if (!noninteractive)
14678 {
5f5c8ee5
GM
14679 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14680 int i;
14681
ac90c44f 14682 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
12c226c5 14683 set_window_height (root_window,
5f5c8ee5 14684 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 14685 0);
ac90c44f 14686 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
a2889657
JB
14687 set_window_height (minibuf_window, 1, 0);
14688
ac90c44f
GM
14689 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14690 mini_w->width = make_number (FRAME_WIDTH (f));
5f5c8ee5
GM
14691
14692 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14693 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14694 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14695
14696 /* The default ellipsis glyphs `...'. */
14697 for (i = 0; i < 3; ++i)
ac90c44f 14698 default_invis_vector[i] = make_number ('.');
a2889657 14699 }
5f5c8ee5
GM
14700
14701#ifdef HAVE_WINDOW_SYSTEM
14702 {
14703 /* Allocate the buffer for frame titles. */
14704 int size = 100;
14705 frame_title_buf = (char *) xmalloc (size);
14706 frame_title_buf_end = frame_title_buf + size;
14707 frame_title_ptr = NULL;
14708 }
14709#endif /* HAVE_WINDOW_SYSTEM */
21fdfb65
GM
14710
14711 help_echo_showing_p = 0;
a2889657 14712}
5f5c8ee5
GM
14713
14714