(Fforward_word): Stop at a mini-buffer prompt end
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5f5c8ee5
GM
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
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>
5f5c8ee5
GM
172#ifdef STDC_HEADERS
173#include <stdlib.h>
174#endif
a2889657 175#include "lisp.h"
44fa5b1e 176#include "frame.h"
a2889657
JB
177#include "window.h"
178#include "termchar.h"
179#include "dispextern.h"
180#include "buffer.h"
1c9241f5 181#include "charset.h"
a2889657
JB
182#include "indent.h"
183#include "commands.h"
184#include "macros.h"
185#include "disptab.h"
30c566e4 186#include "termhooks.h"
b0a0fbda 187#include "intervals.h"
fe8b0cf8 188#include "keyboard.h"
1c9241f5
KH
189#include "coding.h"
190#include "process.h"
dfcf069d
AS
191#include "region-cache.h"
192
6d55d620 193#ifdef HAVE_X_WINDOWS
dfcf069d
AS
194#include "xterm.h"
195#endif
a2889657 196
5f5c8ee5
GM
197#define min(a, b) ((a) < (b) ? (a) : (b))
198#define max(a, b) ((a) > (b) ? (a) : (b))
199
200#define INFINITY 10000000
201
8f3343d0 202#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
76412d64 203extern void set_frame_menubar ();
cd6dfed6 204extern int pending_menu_activation;
76412d64
RS
205#endif
206
a2889657
JB
207extern int interrupt_input;
208extern int command_loop_level;
209
b6436d4e
RS
210extern int minibuffer_auto_raise;
211
c4628384
RS
212extern Lisp_Object Qface;
213
399164b4
KH
214extern Lisp_Object Voverriding_local_map;
215extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 216extern Lisp_Object Qmenu_item;
399164b4 217
d46fb96a 218Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 219Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 220Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 221Lisp_Object Qinhibit_point_motion_hooks;
5f5c8ee5
GM
222Lisp_Object QCeval, QCwhen;
223Lisp_Object Qfontified;
224
225/* Functions called to fontify regions of text. */
226
227Lisp_Object Vfontification_functions;
228Lisp_Object Qfontification_functions;
229
230/* Non-zero means draw toolbar buttons raised when the mouse moves
231 over them. */
232
233int auto_raise_toolbar_buttons_p;
234
235/* Margin around toolbar buttons in pixels. */
236
237int toolbar_button_margin;
238
239/* Thickness of shadow to draw around toolbar buttons. */
240
241int toolbar_button_relief;
242
243/* Non-zero means automatically resize toolbars so that all toolbar
244 items are visible, and no blank lines remain. */
245
246int auto_resize_toolbars_p;
399164b4 247
735c094c
KH
248/* Non-nil means don't actually do any redisplay. */
249
250Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
251
5f5c8ee5
GM
252/* Names of text properties relevant for redisplay. */
253
254Lisp_Object Qdisplay, Qrelative_width, Qwidth, Qalign_to;
255extern Lisp_Object Qface, Qinvisible, Qimage;
256
257/* Symbols used in text property values. */
258
259Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;
261
8f897821 262/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 263
8f897821 264Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
265
266/* Name of the face used to highlight trailing whitespace. */
267
268Lisp_Object Qtrailing_whitespace;
269
270/* The symbol `image' which is the car of the lists used to represent
271 images in Lisp. */
272
273Lisp_Object Qimage;
274
275/* Non-zero means print newline to stdout before next mini-buffer
276 message. */
a2889657
JB
277
278int noninteractive_need_newline;
279
5f5c8ee5 280/* Non-zero means print newline to message log before next message. */
f88eb0b6 281
3c6595e0 282static int message_log_need_newline;
f88eb0b6 283
5f5c8ee5
GM
284\f
285/* The buffer position of the first character appearing entirely or
286 partially on the line of the selected window which contains the
287 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
288 redisplay optimization in redisplay_internal. */
a2889657 289
5f5c8ee5 290static struct text_pos this_line_start_pos;
a2889657 291
5f5c8ee5
GM
292/* Number of characters past the end of the line above, including the
293 terminating newline. */
294
295static struct text_pos this_line_end_pos;
296
297/* The vertical positions and the height of this line. */
a2889657 298
a2889657 299static int this_line_vpos;
5f5c8ee5
GM
300static int this_line_y;
301static int this_line_pixel_height;
302
303/* X position at which this display line starts. Usually zero;
304 negative if first character is partially visible. */
305
306static int this_line_start_x;
a2889657 307
5f5c8ee5 308/* Buffer that this_line_.* variables are referring to. */
a2889657 309
a2889657
JB
310static struct buffer *this_line_buffer;
311
5f5c8ee5
GM
312/* Nonzero means truncate lines in all windows less wide than the
313 frame. */
a2889657 314
a2889657
JB
315int truncate_partial_width_windows;
316
7bbe686f 317/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 318
7bbe686f 319int unibyte_display_via_language_environment;
5f5c8ee5
GM
320
321/* Nonzero means we have more than one non-mini-buffer-only frame.
322 Not guaranteed to be accurate except while parsing
323 frame-title-format. */
7bbe686f 324
d39b6696
KH
325int multiple_frames;
326
a2889657
JB
327Lisp_Object Vglobal_mode_string;
328
329/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 330
a2889657
JB
331Lisp_Object Voverlay_arrow_position;
332
5f5c8ee5
GM
333/* String to display for the arrow. Only used on terminal frames. */
334
a2889657
JB
335Lisp_Object Voverlay_arrow_string;
336
5f5c8ee5
GM
337/* Values of those variables at last redisplay. However, if
338 Voverlay_arrow_position is a marker, last_arrow_position is its
339 numerical position. */
340
d45de95b
RS
341static Lisp_Object last_arrow_position, last_arrow_string;
342
5f5c8ee5
GM
343/* Like mode-line-format, but for the title bar on a visible frame. */
344
d39b6696
KH
345Lisp_Object Vframe_title_format;
346
5f5c8ee5
GM
347/* Like mode-line-format, but for the title bar on an iconified frame. */
348
d39b6696
KH
349Lisp_Object Vicon_title_format;
350
08b610e4
RS
351/* List of functions to call when a window's size changes. These
352 functions get one arg, a frame on which one or more windows' sizes
353 have changed. */
5f5c8ee5 354
08b610e4
RS
355static Lisp_Object Vwindow_size_change_functions;
356
cf074754
RS
357Lisp_Object Qmenu_bar_update_hook;
358
a2889657 359/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 360
5f5c8ee5 361static int overlay_arrow_seen;
ca26e1c8 362
fba9ce76 363/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 364
5f5c8ee5
GM
365int highlight_nonselected_windows;
366
367/* If cursor motion alone moves point off frame, try scrolling this
368 many lines up or down if that will bring it back. */
369
14510fee 370static int scroll_step;
a2889657 371
5f5c8ee5
GM
372/* Non-0 means scroll just far enough to bring point back on the
373 screen, when appropriate. */
374
0789adb2
RS
375static int scroll_conservatively;
376
5f5c8ee5
GM
377/* Recenter the window whenever point gets within this many lines of
378 the top or bottom of the window. This value is translated into a
379 pixel value by multiplying it with CANON_Y_UNIT, which means that
380 there is really a fixed pixel height scroll margin. */
381
9afd2168
RS
382int scroll_margin;
383
5f5c8ee5
GM
384/* Number of windows showing the buffer of the selected window (or
385 another buffer with the same base buffer). keyboard.c refers to
386 this. */
a2889657 387
a2889657
JB
388int buffer_shared;
389
5f5c8ee5 390/* Vector containing glyphs for an ellipsis `...'. */
a2889657 391
5f5c8ee5 392static Lisp_Object default_invis_vector[3];
a2889657 393
5f5c8ee5 394/* Nonzero means display mode line highlighted. */
a2889657 395
a2889657
JB
396int mode_line_inverse_video;
397
5f5c8ee5
GM
398/* Prompt to display in front of the mini-buffer contents. */
399
8c5b6a0a 400Lisp_Object minibuf_prompt;
a2889657 401
5f5c8ee5
GM
402/* Width of current mini-buffer prompt. Only set after display_line
403 of the line that contains the prompt. */
404
a2889657 405int minibuf_prompt_width;
5f5c8ee5
GM
406int minibuf_prompt_pixel_width;
407
5f5c8ee5
GM
408/* This is the window where the echo area message was displayed. It
409 is always a mini-buffer window, but it may not be the same window
410 currently active as a mini-buffer. */
411
73af359d
RS
412Lisp_Object echo_area_window;
413
c6e89d6c
GM
414/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
415 pushes the current message and the value of
416 message_enable_multibyte on the stack, the function restore_message
417 pops the stack and displays MESSAGE again. */
418
419Lisp_Object Vmessage_stack;
420
a3788d53
RS
421/* Nonzero means multibyte characters were enabled when the echo area
422 message was specified. */
5f5c8ee5 423
a3788d53
RS
424int message_enable_multibyte;
425
5f5c8ee5
GM
426/* True if we should redraw the mode lines on the next redisplay. */
427
a2889657
JB
428int update_mode_lines;
429
5f5c8ee5
GM
430/* Nonzero if window sizes or contents have changed since last
431 redisplay that finished */
432
a2889657
JB
433int windows_or_buffers_changed;
434
5f5c8ee5
GM
435/* Nonzero after display_mode_line if %l was used and it displayed a
436 line number. */
437
aa6d10fa
RS
438int line_number_displayed;
439
440/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 441
14510fee 442static int line_number_display_limit;
5992c4f7 443
5d121aec
KH
444/* line width to consider when repostioning for line number display */
445
446static int line_number_display_limit_width;
447
5f5c8ee5
GM
448/* Number of lines to keep in the message log buffer. t means
449 infinite. nil means don't log at all. */
450
5992c4f7 451Lisp_Object Vmessage_log_max;
d45de95b 452
c6e89d6c
GM
453/* Current, index 0, and last displayed echo area message. Either
454 buffers from echo_buffers, or nil to indicate no message. */
455
456Lisp_Object echo_area_buffer[2];
457
458/* The buffers referenced from echo_area_buffer. */
459
460static Lisp_Object echo_buffer[2];
461
462/* A vector saved used in with_area_buffer to reduce consing. */
463
464static Lisp_Object Vwith_echo_area_save_vector;
465
466/* Non-zero means display_echo_area should display the last echo area
467 message again. Set by redisplay_preserve_echo_area. */
468
469static int display_last_displayed_message_p;
470
471/* Nonzero if echo area is being used by print; zero if being used by
472 message. */
473
474int message_buf_print;
475
9142dd5b
GM
476/* Maximum height for resizing mini-windows. Either a float
477 specifying a fraction of the available height, or an integer
478 specifying a number of lines. */
c6e89d6c
GM
479
480static Lisp_Object Vmax_mini_window_height;
481
9142dd5b
GM
482/* Window configuration saved in resize_mini_window. */
483
c59c668a
GM
484Lisp_Object Vresize_mini_config;
485
486/* Frmae of the mini-window being resized by resize_mini_window. */
487
488struct frame *resize_mini_frame;
489
490/* Initial height of the mini-window having been resized
491 by resize_mini_window. */
492
493int resize_mini_initial_height;
9142dd5b 494
5f5c8ee5
GM
495/* A scratch glyph row with contents used for generating truncation
496 glyphs. Also used in direct_output_for_insert. */
12adba34 497
5f5c8ee5
GM
498#define MAX_SCRATCH_GLYPHS 100
499struct glyph_row scratch_glyph_row;
500static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 501
5f5c8ee5
GM
502/* Ascent and height of the last line processed by move_it_to. */
503
504static int last_max_ascent, last_height;
505
506/* The maximum distance to look ahead for text properties. Values
507 that are too small let us call compute_char_face and similar
508 functions too often which is expensive. Values that are too large
509 let us call compute_char_face and alike too often because we
510 might not be interested in text properties that far away. */
511
512#define TEXT_PROP_DISTANCE_LIMIT 100
513
514/* Non-zero means print traces of redisplay if compiled with
515 GLYPH_DEBUG != 0. */
516
517#if GLYPH_DEBUG
518int trace_redisplay_p;
519#endif
520
521/* Value returned from text property handlers (see below). */
522
523enum prop_handled
3c6595e0 524{
5f5c8ee5
GM
525 HANDLED_NORMALLY,
526 HANDLED_RECOMPUTE_PROPS,
527 HANDLED_OVERLAY_STRING_CONSUMED,
528 HANDLED_RETURN
529};
3c6595e0 530
5f5c8ee5
GM
531/* A description of text properties that redisplay is interested
532 in. */
3c6595e0 533
5f5c8ee5
GM
534struct props
535{
536 /* The name of the property. */
537 Lisp_Object *name;
90adcf20 538
5f5c8ee5
GM
539 /* A unique index for the property. */
540 enum prop_idx idx;
541
542 /* A handler function called to set up iterator IT from the property
543 at IT's current position. Value is used to steer handle_stop. */
544 enum prop_handled (*handler) P_ ((struct it *it));
545};
546
547static enum prop_handled handle_face_prop P_ ((struct it *));
548static enum prop_handled handle_invisible_prop P_ ((struct it *));
549static enum prop_handled handle_display_prop P_ ((struct it *));
550static enum prop_handled handle_overlay_change P_ ((struct it *));
551static enum prop_handled handle_fontified_prop P_ ((struct it *));
552
553/* Properties handled by iterators. */
554
555static struct props it_props[] =
5992c4f7 556{
5f5c8ee5
GM
557 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
558 /* Handle `face' before `display' because some sub-properties of
559 `display' need to know the face. */
560 {&Qface, FACE_PROP_IDX, handle_face_prop},
561 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
562 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
563 {NULL, 0, NULL}
564};
5992c4f7 565
5f5c8ee5
GM
566/* Value is the position described by X. If X is a marker, value is
567 the marker_position of X. Otherwise, value is X. */
12adba34 568
5f5c8ee5 569#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 570
5f5c8ee5 571/* Enumeration returned by some move_it_.* functions internally. */
12adba34 572
5f5c8ee5
GM
573enum move_it_result
574{
575 /* Not used. Undefined value. */
576 MOVE_UNDEFINED,
bab29e15 577
5f5c8ee5
GM
578 /* Move ended at the requested buffer position or ZV. */
579 MOVE_POS_MATCH_OR_ZV,
bab29e15 580
5f5c8ee5
GM
581 /* Move ended at the requested X pixel position. */
582 MOVE_X_REACHED,
12adba34 583
5f5c8ee5
GM
584 /* Move within a line ended at the end of a line that must be
585 continued. */
586 MOVE_LINE_CONTINUED,
587
588 /* Move within a line ended at the end of a line that would
589 be displayed truncated. */
590 MOVE_LINE_TRUNCATED,
ff6c30e5 591
5f5c8ee5
GM
592 /* Move within a line ended at a line end. */
593 MOVE_NEWLINE_OR_CR
594};
12adba34 595
ff6c30e5 596
5f5c8ee5
GM
597\f
598/* Function prototypes. */
599
c6e89d6c
GM
600static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
601static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
602static int resize_mini_window P_ ((struct window *));
603static void clear_garbaged_frames P_ ((void));
604static int current_message_1 P_ ((Lisp_Object *));
605static int truncate_message_1 P_ ((int));
606static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
607static int display_echo_area P_ ((struct window *));
608static int display_echo_area_1 P_ ((struct window *));
28514cd9 609static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 610static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
611static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
612 struct text_pos));
613static int compute_window_start_on_continuation_line P_ ((struct window *));
614static Lisp_Object eval_handler P_ ((Lisp_Object));
615static Lisp_Object eval_form P_ ((Lisp_Object));
616static void insert_left_trunc_glyphs P_ ((struct it *));
617static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
618static void extend_face_to_end_of_line P_ ((struct it *));
619static void append_space P_ ((struct it *, int));
620static void make_cursor_line_fully_visible P_ ((struct window *));
621static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
622static int trailing_whitespace_p P_ ((int));
623static int message_log_check_duplicate P_ ((int, int, int, int));
624int invisible_p P_ ((Lisp_Object, Lisp_Object));
625int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
626static void push_it P_ ((struct it *));
627static void pop_it P_ ((struct it *));
628static void sync_frame_with_window_matrix_rows P_ ((struct window *));
629static void redisplay_internal P_ ((int));
c6e89d6c 630static int echo_area_display P_ ((int));
5f5c8ee5
GM
631static void redisplay_windows P_ ((Lisp_Object));
632static void redisplay_window P_ ((Lisp_Object, int));
633static void update_menu_bar P_ ((struct frame *, int));
634static int try_window_reusing_current_matrix P_ ((struct window *));
635static int try_window_id P_ ((struct window *));
636static int display_line P_ ((struct it *));
637static void display_mode_lines P_ ((struct window *));
638static void display_mode_line P_ ((struct window *, enum face_id,
639 Lisp_Object));
640static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
641static char *decode_mode_spec P_ ((struct window *, char, int, int));
642static void display_menu_bar P_ ((struct window *));
643static int display_count_lines P_ ((int, int, int, int, int *));
644static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
645 int, int, struct it *, int, int, int, int));
646static void compute_line_metrics P_ ((struct it *));
647static void run_redisplay_end_trigger_hook P_ ((struct it *));
648static int get_overlay_strings P_ ((struct it *));
649static void next_overlay_string P_ ((struct it *));
650void set_iterator_to_next P_ ((struct it *));
651static void reseat P_ ((struct it *, struct text_pos, int));
652static void reseat_1 P_ ((struct it *, struct text_pos, int));
653static void back_to_previous_visible_line_start P_ ((struct it *));
654static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 655static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
656static int next_element_from_display_vector P_ ((struct it *));
657static int next_element_from_string P_ ((struct it *));
658static int next_element_from_c_string P_ ((struct it *));
659static int next_element_from_buffer P_ ((struct it *));
660static int next_element_from_image P_ ((struct it *));
661static int next_element_from_stretch P_ ((struct it *));
662static void load_overlay_strings P_ ((struct it *));
663static void init_from_display_pos P_ ((struct it *, struct window *,
664 struct display_pos *));
665static void reseat_to_string P_ ((struct it *, unsigned char *,
666 Lisp_Object, int, int, int, int));
667static int charset_at_position P_ ((struct text_pos));
668static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
669 int, int, int));
670void move_it_vertically_backward P_ ((struct it *, int));
671static void init_to_row_start P_ ((struct it *, struct window *,
672 struct glyph_row *));
673static void init_to_row_end P_ ((struct it *, struct window *,
674 struct glyph_row *));
675static void back_to_previous_line_start P_ ((struct it *));
676static void forward_to_next_line_start P_ ((struct it *));
677static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
678 Lisp_Object, int));
679static struct text_pos string_pos P_ ((int, Lisp_Object));
680static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
681static int number_of_chars P_ ((unsigned char *, int));
682static void compute_stop_pos P_ ((struct it *));
683static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
684 Lisp_Object));
685static int face_before_or_after_it_pos P_ ((struct it *, int));
686static int next_overlay_change P_ ((int));
687static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
688 Lisp_Object, struct text_pos *));
689
690#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
691#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 692
5f5c8ee5 693#ifdef HAVE_WINDOW_SYSTEM
12adba34 694
5f5c8ee5
GM
695static void update_toolbar P_ ((struct frame *, int));
696static void build_desired_toolbar_string P_ ((struct frame *f));
697static int redisplay_toolbar P_ ((struct frame *));
698static void display_toolbar_line P_ ((struct it *));
12adba34 699
5f5c8ee5 700#endif /* HAVE_WINDOW_SYSTEM */
12adba34 701
5f5c8ee5
GM
702\f
703/***********************************************************************
704 Window display dimensions
705 ***********************************************************************/
12adba34 706
5f5c8ee5
GM
707/* Return the window-relative maximum y + 1 for glyph rows displaying
708 text in window W. This is the height of W minus the height of a
709 mode line, if any. */
710
711INLINE int
712window_text_bottom_y (w)
713 struct window *w;
714{
715 struct frame *f = XFRAME (w->frame);
716 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
717
718 if (WINDOW_WANTS_MODELINE_P (w))
719 height -= CURRENT_MODE_LINE_HEIGHT (w);
720 return height;
f88eb0b6
KH
721}
722
f82aff7c 723
5f5c8ee5
GM
724/* Return the pixel width of display area AREA of window W. AREA < 0
725 means return the total width of W, not including bitmap areas to
726 the left and right of the window. */
ff6c30e5 727
5f5c8ee5
GM
728INLINE int
729window_box_width (w, area)
730 struct window *w;
731 int area;
732{
733 struct frame *f = XFRAME (w->frame);
734 int width = XFASTINT (w->width);
735
736 if (!w->pseudo_window_p)
ff6c30e5 737 {
050d82d7 738 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
739
740 if (area == TEXT_AREA)
741 {
742 if (INTEGERP (w->left_margin_width))
743 width -= XFASTINT (w->left_margin_width);
744 if (INTEGERP (w->right_margin_width))
745 width -= XFASTINT (w->right_margin_width);
746 }
747 else if (area == LEFT_MARGIN_AREA)
748 width = (INTEGERP (w->left_margin_width)
749 ? XFASTINT (w->left_margin_width) : 0);
750 else if (area == RIGHT_MARGIN_AREA)
751 width = (INTEGERP (w->right_margin_width)
752 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 753 }
5f5c8ee5
GM
754
755 return width * CANON_X_UNIT (f);
ff6c30e5 756}
1adc55de 757
1adc55de 758
5f5c8ee5
GM
759/* Return the pixel height of the display area of window W, not
760 including mode lines of W, if any.. */
f88eb0b6 761
5f5c8ee5
GM
762INLINE int
763window_box_height (w)
764 struct window *w;
f88eb0b6 765{
5f5c8ee5
GM
766 struct frame *f = XFRAME (w->frame);
767 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
768
769 if (WINDOW_WANTS_MODELINE_P (w))
770 height -= CURRENT_MODE_LINE_HEIGHT (w);
771
772 if (WINDOW_WANTS_TOP_LINE_P (w))
773 height -= CURRENT_TOP_LINE_HEIGHT (w);
774
775 return height;
5992c4f7
KH
776}
777
778
5f5c8ee5
GM
779/* Return the frame-relative coordinate of the left edge of display
780 area AREA of window W. AREA < 0 means return the left edge of the
781 whole window, to the right of any bitmap area at the left side of
782 W. */
5992c4f7 783
5f5c8ee5
GM
784INLINE int
785window_box_left (w, area)
786 struct window *w;
787 int area;
90adcf20 788{
5f5c8ee5
GM
789 struct frame *f = XFRAME (w->frame);
790 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 791
5f5c8ee5 792 if (!w->pseudo_window_p)
90adcf20 793 {
5f5c8ee5 794 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 795 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
796
797 if (area == TEXT_AREA)
798 x += window_box_width (w, LEFT_MARGIN_AREA);
799 else if (area == RIGHT_MARGIN_AREA)
800 x += (window_box_width (w, LEFT_MARGIN_AREA)
801 + window_box_width (w, TEXT_AREA));
90adcf20 802 }
73af359d 803
5f5c8ee5
GM
804 return x;
805}
90adcf20 806
b6436d4e 807
5f5c8ee5
GM
808/* Return the frame-relative coordinate of the right edge of display
809 area AREA of window W. AREA < 0 means return the left edge of the
810 whole window, to the left of any bitmap area at the right side of
811 W. */
ded34426 812
5f5c8ee5
GM
813INLINE int
814window_box_right (w, area)
815 struct window *w;
816 int area;
817{
818 return window_box_left (w, area) + window_box_width (w, area);
819}
820
821
822/* Get the bounding box of the display area AREA of window W, without
823 mode lines, in frame-relative coordinates. AREA < 0 means the
824 whole window, not including bitmap areas to the left and right of
825 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
826 coordinates of the upper-left corner of the box. Return in
827 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
828
829INLINE void
830window_box (w, area, box_x, box_y, box_width, box_height)
831 struct window *w;
832 int area;
833 int *box_x, *box_y, *box_width, *box_height;
834{
835 struct frame *f = XFRAME (w->frame);
836
837 *box_width = window_box_width (w, area);
838 *box_height = window_box_height (w);
839 *box_x = window_box_left (w, area);
840 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
841 + XFASTINT (w->top) * CANON_Y_UNIT (f));
842 if (WINDOW_WANTS_TOP_LINE_P (w))
843 *box_y += CURRENT_TOP_LINE_HEIGHT (w);
ded34426 844}
1adc55de 845
1adc55de 846
5f5c8ee5
GM
847/* Get the bounding box of the display area AREA of window W, without
848 mode lines. AREA < 0 means the whole window, not including bitmap
849 areas to the left and right of the window. Return in *TOP_LEFT_X
850 and TOP_LEFT_Y the frame-relative pixel coordinates of the
851 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
852 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
853 box. */
ded34426 854
5f5c8ee5
GM
855INLINE void
856window_box_edges (w, area, top_left_x, top_left_y,
857 bottom_right_x, bottom_right_y)
858 struct window *w;
859 int area;
860 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 861{
5f5c8ee5
GM
862 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
863 bottom_right_y);
864 *bottom_right_x += *top_left_x;
865 *bottom_right_y += *top_left_y;
48ae5f0a
KH
866}
867
5f5c8ee5
GM
868
869\f
870/***********************************************************************
871 Utilities
872 ***********************************************************************/
873
4fdb80f2
GM
874/* Return the next character from STR which is MAXLEN bytes long.
875 Return in *LEN the length of the character. This is like
876 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
877 we find one, we return a `?', but with the length of the illegal
878 character. */
879
880static INLINE int
7a5b8a93 881string_char_and_length (str, maxlen, len)
4fdb80f2 882 unsigned char *str;
7a5b8a93 883 int maxlen, *len;
4fdb80f2
GM
884{
885 int c;
886
887 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
888 if (!CHAR_VALID_P (c, 1))
889 /* We may not change the length here because other places in Emacs
890 don't use this function, i.e. they silently accept illegal
891 characters. */
892 c = '?';
893
894 return c;
895}
896
897
898
5f5c8ee5
GM
899/* Given a position POS containing a valid character and byte position
900 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
901
902static struct text_pos
903string_pos_nchars_ahead (pos, string, nchars)
904 struct text_pos pos;
905 Lisp_Object string;
906 int nchars;
0b1005ef 907{
5f5c8ee5
GM
908 xassert (STRINGP (string) && nchars >= 0);
909
910 if (STRING_MULTIBYTE (string))
911 {
912 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
913 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
914 int len;
915
916 while (nchars--)
917 {
4fdb80f2 918 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
919 p += len, rest -= len;
920 xassert (rest >= 0);
921 CHARPOS (pos) += 1;
922 BYTEPOS (pos) += len;
923 }
924 }
925 else
926 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
927
928 return pos;
0a9dc68b
RS
929}
930
0a9dc68b 931
5f5c8ee5
GM
932/* Value is the text position, i.e. character and byte position,
933 for character position CHARPOS in STRING. */
934
935static INLINE struct text_pos
936string_pos (charpos, string)
937 int charpos;
0a9dc68b 938 Lisp_Object string;
0a9dc68b 939{
5f5c8ee5
GM
940 struct text_pos pos;
941 xassert (STRINGP (string));
942 xassert (charpos >= 0);
943 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
944 return pos;
945}
946
947
948/* Value is a text position, i.e. character and byte position, for
949 character position CHARPOS in C string S. MULTIBYTE_P non-zero
950 means recognize multibyte characters. */
951
952static struct text_pos
953c_string_pos (charpos, s, multibyte_p)
954 int charpos;
955 unsigned char *s;
956 int multibyte_p;
957{
958 struct text_pos pos;
959
960 xassert (s != NULL);
961 xassert (charpos >= 0);
962
963 if (multibyte_p)
0a9dc68b 964 {
5f5c8ee5
GM
965 int rest = strlen (s), len;
966
967 SET_TEXT_POS (pos, 0, 0);
968 while (charpos--)
0a9dc68b 969 {
4fdb80f2 970 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
971 s += len, rest -= len;
972 xassert (rest >= 0);
973 CHARPOS (pos) += 1;
974 BYTEPOS (pos) += len;
0a9dc68b
RS
975 }
976 }
5f5c8ee5
GM
977 else
978 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 979
5f5c8ee5
GM
980 return pos;
981}
0a9dc68b 982
0a9dc68b 983
5f5c8ee5
GM
984/* Value is the number of characters in C string S. MULTIBYTE_P
985 non-zero means recognize multibyte characters. */
0a9dc68b 986
5f5c8ee5
GM
987static int
988number_of_chars (s, multibyte_p)
989 unsigned char *s;
990 int multibyte_p;
991{
992 int nchars;
993
994 if (multibyte_p)
995 {
996 int rest = strlen (s), len;
997 unsigned char *p = (unsigned char *) s;
0a9dc68b 998
5f5c8ee5
GM
999 for (nchars = 0; rest > 0; ++nchars)
1000 {
4fdb80f2 1001 string_char_and_length (p, rest, &len);
5f5c8ee5 1002 rest -= len, p += len;
0a9dc68b
RS
1003 }
1004 }
5f5c8ee5
GM
1005 else
1006 nchars = strlen (s);
1007
1008 return nchars;
0b1005ef
KH
1009}
1010
5f5c8ee5
GM
1011
1012/* Compute byte position NEWPOS->bytepos corresponding to
1013 NEWPOS->charpos. POS is a known position in string STRING.
1014 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1015
5f5c8ee5
GM
1016static void
1017compute_string_pos (newpos, pos, string)
1018 struct text_pos *newpos, pos;
1019 Lisp_Object string;
76412d64 1020{
5f5c8ee5
GM
1021 xassert (STRINGP (string));
1022 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1023
1024 if (STRING_MULTIBYTE (string))
1025 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1026 string);
1027 else
1028 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1029}
1030
9c74a0dd 1031
5f5c8ee5
GM
1032/* Return the charset of the character at position POS in
1033 current_buffer. */
1adc55de 1034
5f5c8ee5
GM
1035static int
1036charset_at_position (pos)
1037 struct text_pos pos;
a2889657 1038{
5f5c8ee5
GM
1039 int c, multibyte_p;
1040 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1041
1042 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1043 if (multibyte_p)
a2889657 1044 {
5f5c8ee5
GM
1045 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1046 - BYTEPOS (pos));
1047 int len;
4fdb80f2 1048 c = string_char_and_length (p, maxlen, &len);
a2889657 1049 }
5f5c8ee5
GM
1050 else
1051 c = *p;
1052
1053 return CHAR_CHARSET (c);
1054}
1055
1056
1057\f
1058/***********************************************************************
1059 Lisp form evaluation
1060 ***********************************************************************/
1061
1062/* Error handler for eval_form. */
1063
1064static Lisp_Object
1065eval_handler (arg)
1066 Lisp_Object arg;
1067{
1068 return Qnil;
1069}
1070
1071
1072/* Evaluate SEXPR and return the result, or nil if something went
1073 wrong. */
1074
1075static Lisp_Object
1076eval_form (sexpr)
1077 Lisp_Object sexpr;
1078{
1079 int count = specpdl_ptr - specpdl;
1080 Lisp_Object val;
1081 specbind (Qinhibit_redisplay, Qt);
1082 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1083 return unbind_to (count, val);
1084}
1085
1086
1087\f
1088/***********************************************************************
1089 Debugging
1090 ***********************************************************************/
1091
1092#if 0
1093
1094/* Define CHECK_IT to perform sanity checks on iterators.
1095 This is for debugging. It is too slow to do unconditionally. */
1096
1097static void
1098check_it (it)
1099 struct it *it;
1100{
1101 if (it->method == next_element_from_string)
a2889657 1102 {
5f5c8ee5
GM
1103 xassert (STRINGP (it->string));
1104 xassert (IT_STRING_CHARPOS (*it) >= 0);
1105 }
1106 else if (it->method == next_element_from_buffer)
1107 {
1108 /* Check that character and byte positions agree. */
1109 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1110 }
73af359d 1111
5f5c8ee5
GM
1112 if (it->dpvec)
1113 xassert (it->current.dpvec_index >= 0);
1114 else
1115 xassert (it->current.dpvec_index < 0);
1116}
1f40cad2 1117
5f5c8ee5
GM
1118#define CHECK_IT(IT) check_it ((IT))
1119
1120#else /* not 0 */
1121
1122#define CHECK_IT(IT) (void) 0
1123
1124#endif /* not 0 */
1125
1126
1127#if GLYPH_DEBUG
1128
1129/* Check that the window end of window W is what we expect it
1130 to be---the last row in the current matrix displaying text. */
1131
1132static void
1133check_window_end (w)
1134 struct window *w;
1135{
1136 if (!MINI_WINDOW_P (w)
1137 && !NILP (w->window_end_valid))
1138 {
1139 struct glyph_row *row;
1140 xassert ((row = MATRIX_ROW (w->current_matrix,
1141 XFASTINT (w->window_end_vpos)),
1142 !row->enabled_p
1143 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1144 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1145 }
1146}
1147
1148#define CHECK_WINDOW_END(W) check_window_end ((W))
1149
1150#else /* not GLYPH_DEBUG */
1151
1152#define CHECK_WINDOW_END(W) (void) 0
1153
1154#endif /* not GLYPH_DEBUG */
1155
1156
1157\f
1158/***********************************************************************
1159 Iterator initialization
1160 ***********************************************************************/
1161
1162/* Initialize IT for displaying current_buffer in window W, starting
1163 at character position CHARPOS. CHARPOS < 0 means that no buffer
1164 position is specified which is useful when the iterator is assigned
1165 a position later. BYTEPOS is the byte position corresponding to
1166 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1167
1168 If ROW is not null, calls to produce_glyphs with IT as parameter
1169 will produce glyphs in that row.
1170
1171 BASE_FACE_ID is the id of a base face to use. It must be one of
1172 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1173 TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
1174 displaying the toolbar.
1175
1176 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1177 TOP_LINE_FACE_ID, the iterator will be initialized to use the
1178 corresponding mode line glyph row of the desired matrix of W. */
1179
1180void
1181init_iterator (it, w, charpos, bytepos, row, base_face_id)
1182 struct it *it;
1183 struct window *w;
1184 int charpos, bytepos;
1185 struct glyph_row *row;
1186 enum face_id base_face_id;
1187{
1188 int highlight_region_p;
5f5c8ee5
GM
1189
1190 /* Some precondition checks. */
1191 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1192 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1193
1194 /* If face attributes have been changed since the last redisplay,
1195 free realized faces now because they depend on face definitions
1196 that might have changed. */
1197 if (face_change_count)
1198 {
1199 face_change_count = 0;
1200 free_all_realized_faces (Qnil);
1201 }
1202
1203 /* Use one of the mode line rows of W's desired matrix if
1204 appropriate. */
1205 if (row == NULL)
1206 {
1207 if (base_face_id == MODE_LINE_FACE_ID)
1208 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1209 else if (base_face_id == TOP_LINE_FACE_ID)
1210 row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
1211 }
1212
1213 /* Clear IT. */
1214 bzero (it, sizeof *it);
1215 it->current.overlay_string_index = -1;
1216 it->current.dpvec_index = -1;
1217 it->charset = CHARSET_ASCII;
1218 it->base_face_id = base_face_id;
1219
1220 /* The window in which we iterate over current_buffer: */
1221 XSETWINDOW (it->window, w);
1222 it->w = w;
1223 it->f = XFRAME (w->frame);
1224
1225 /* If realized faces have been removed, e.g. because of face
1226 attribute changes of named faces, recompute them. */
1227 if (FRAME_FACE_CACHE (it->f)->used == 0)
1228 recompute_basic_faces (it->f);
1229
5f5c8ee5
GM
1230 /* Current value of the `space-width', and 'height' properties. */
1231 it->space_width = Qnil;
1232 it->font_height = Qnil;
1233
1234 /* Are control characters displayed as `^C'? */
1235 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1236
1237 /* -1 means everything between a CR and the following line end
1238 is invisible. >0 means lines indented more than this value are
1239 invisible. */
1240 it->selective = (INTEGERP (current_buffer->selective_display)
1241 ? XFASTINT (current_buffer->selective_display)
1242 : (!NILP (current_buffer->selective_display)
1243 ? -1 : 0));
1244 it->selective_display_ellipsis_p
1245 = !NILP (current_buffer->selective_display_ellipses);
1246
1247 /* Display table to use. */
1248 it->dp = window_display_table (w);
1249
1250 /* Are multibyte characters enabled in current_buffer? */
1251 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1252
1253 /* Non-zero if we should highlight the region. */
1254 highlight_region_p
1255 = (!NILP (Vtransient_mark_mode)
1256 && !NILP (current_buffer->mark_active)
1257 && XMARKER (current_buffer->mark)->buffer != 0);
1258
1259 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1260 start and end of a visible region in window IT->w. Set both to
1261 -1 to indicate no region. */
1262 if (highlight_region_p
1263 /* Maybe highlight only in selected window. */
1264 && (/* Either show region everywhere. */
1265 highlight_nonselected_windows
1266 /* Or show region in the selected window. */
1267 || w == XWINDOW (selected_window)
1268 /* Or show the region if we are in the mini-buffer and W is
1269 the window the mini-buffer refers to. */
1270 || (MINI_WINDOW_P (XWINDOW (selected_window))
1271 && w == XWINDOW (Vminibuf_scroll_window))))
1272 {
1273 int charpos = marker_position (current_buffer->mark);
1274 it->region_beg_charpos = min (PT, charpos);
1275 it->region_end_charpos = max (PT, charpos);
1276 }
1277 else
1278 it->region_beg_charpos = it->region_end_charpos = -1;
1279
1280 /* Get the position at which the redisplay_end_trigger hook should
1281 be run, if it is to be run at all. */
1282 if (MARKERP (w->redisplay_end_trigger)
1283 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1284 it->redisplay_end_trigger_charpos
1285 = marker_position (w->redisplay_end_trigger);
1286 else if (INTEGERP (w->redisplay_end_trigger))
1287 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1288
1289 /* Correct bogus values of tab_width. */
1290 it->tab_width = XINT (current_buffer->tab_width);
1291 if (it->tab_width <= 0 || it->tab_width > 1000)
1292 it->tab_width = 8;
1293
1294 /* Are lines in the display truncated? */
1295 it->truncate_lines_p
1296 = (base_face_id != DEFAULT_FACE_ID
1297 || XINT (it->w->hscroll)
1298 || (truncate_partial_width_windows
1299 && !WINDOW_FULL_WIDTH_P (it->w))
1300 || !NILP (current_buffer->truncate_lines));
1301
1302 /* Get dimensions of truncation and continuation glyphs. These are
1303 displayed as bitmaps under X, so we don't need them for such
1304 frames. */
1305 if (!FRAME_WINDOW_P (it->f))
1306 {
1307 if (it->truncate_lines_p)
1308 {
1309 /* We will need the truncation glyph. */
1310 xassert (it->glyph_row == NULL);
1311 produce_special_glyphs (it, IT_TRUNCATION);
1312 it->truncation_pixel_width = it->pixel_width;
1313 }
1314 else
1315 {
1316 /* We will need the continuation glyph. */
1317 xassert (it->glyph_row == NULL);
1318 produce_special_glyphs (it, IT_CONTINUATION);
1319 it->continuation_pixel_width = it->pixel_width;
1320 }
1321
1322 /* Reset these values to zero becaue the produce_special_glyphs
1323 above has changed them. */
1324 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1325 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1326 }
1327
1328 /* Set this after getting the dimensions of truncation and
1329 continuation glyphs, so that we don't produce glyphs when calling
1330 produce_special_glyphs, above. */
1331 it->glyph_row = row;
1332 it->area = TEXT_AREA;
1333
1334 /* Get the dimensions of the display area. The display area
1335 consists of the visible window area plus a horizontally scrolled
1336 part to the left of the window. All x-values are relative to the
1337 start of this total display area. */
1338 if (base_face_id != DEFAULT_FACE_ID)
1339 {
1340 /* Mode lines, menu bar in terminal frames. */
1341 it->first_visible_x = 0;
1342 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1343 }
1344 else
1345 {
1346 it->first_visible_x
1347 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1348 it->last_visible_x = (it->first_visible_x
1349 + window_box_width (w, TEXT_AREA));
1350
1351 /* If we truncate lines, leave room for the truncator glyph(s) at
1352 the right margin. Otherwise, leave room for the continuation
1353 glyph(s). Truncation and continuation glyphs are not inserted
1354 for window-based redisplay. */
1355 if (!FRAME_WINDOW_P (it->f))
1356 {
1357 if (it->truncate_lines_p)
1358 it->last_visible_x -= it->truncation_pixel_width;
1359 else
1360 it->last_visible_x -= it->continuation_pixel_width;
1361 }
1362
1363 it->top_line_p = WINDOW_WANTS_TOP_LINE_P (w);
1364 it->current_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w) + w->vscroll;
1365 }
1366
1367 /* Leave room for a border glyph. */
1368 if (!FRAME_WINDOW_P (it->f)
1369 && !WINDOW_RIGHTMOST_P (it->w))
1370 it->last_visible_x -= 1;
1371
1372 it->last_visible_y = window_text_bottom_y (w);
1373
1374 /* For mode lines and alike, arrange for the first glyph having a
1375 left box line if the face specifies a box. */
1376 if (base_face_id != DEFAULT_FACE_ID)
1377 {
1378 struct face *face;
1379
1380 it->face_id = base_face_id;
1381
1382 /* If we have a boxed mode line, make the first character appear
1383 with a left box line. */
1384 face = FACE_FROM_ID (it->f, base_face_id);
1385 if (face->box != FACE_NO_BOX)
1386 it->start_of_box_run_p = 1;
1387 }
1388
1389 /* If a buffer position was specified, set the iterator there,
1390 getting overlays and face properties from that position. */
1391 if (charpos > 0)
1392 {
1393 it->end_charpos = ZV;
1394 it->face_id = -1;
1395 IT_CHARPOS (*it) = charpos;
1396
1397 /* Compute byte position if not specified. */
1398 if (bytepos <= 0)
1399 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1400 else
1401 IT_BYTEPOS (*it) = bytepos;
1402
1403 /* Compute faces etc. */
1404 reseat (it, it->current.pos, 1);
1405 }
1406
1407 CHECK_IT (it);
1408}
1409
1410
1411/* Initialize IT for the display of window W with window start POS. */
1412
1413void
1414start_display (it, w, pos)
1415 struct it *it;
1416 struct window *w;
1417 struct text_pos pos;
1418{
1419 int start_at_line_beg_p;
1420 struct glyph_row *row;
1421 int first_vpos = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
1422 int first_y;
1423
1424 row = w->desired_matrix->rows + first_vpos;
1425 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1426 first_y = it->current_y;
1427
1428 /* If window start is not at a line start, move back to the line
1429 start. This makes sure that we take continuation lines into
1430 account. */
1431 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1432 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1433 if (!start_at_line_beg_p)
1434 reseat_at_previous_visible_line_start (it);
1435
5f5c8ee5
GM
1436 /* If window start is not at a line start, skip forward to POS to
1437 get the correct continuation_lines_width and current_x. */
1438 if (!start_at_line_beg_p)
1439 {
1440 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1441
1442 /* If lines are continued, this line may end in the middle of a
1443 multi-glyph character (e.g. a control character displayed as
1444 \003, or in the middle of an overlay string). In this case
1445 move_it_to above will not have taken us to the start of
1446 the continuation line but to the end of the continued line. */
1447 if (!it->truncate_lines_p && it->current_x > 0)
1448 {
1449 if (it->current.dpvec_index >= 0
1450 || it->current.overlay_string_index >= 0)
1451 {
1452 set_iterator_to_next (it);
1453 move_it_in_display_line_to (it, -1, -1, 0);
1454 }
1455 it->continuation_lines_width += it->current_x;
1456 }
1457
1458 it->current_y = first_y;
1459 it->vpos = 0;
1460 it->current_x = it->hpos = 0;
1461 }
1462
1463#if 0 /* Don't assert the following because start_display is sometimes
1464 called intentionally with a window start that is not at a
1465 line start. Please leave this code in as a comment. */
1466
1467 /* Window start should be on a line start, now. */
1468 xassert (it->continuation_lines_width
1469 || IT_CHARPOS (it) == BEGV
1470 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1471#endif /* 0 */
1472}
1473
1474
1475/* Initialize IT for stepping through current_buffer in window W,
1476 starting at position POS that includes overlay string and display
1477 vector/ control character translation position information. */
1478
1479static void
1480init_from_display_pos (it, w, pos)
1481 struct it *it;
1482 struct window *w;
1483 struct display_pos *pos;
1484{
1485 /* Keep in mind: the call to reseat in init_iterator skips invisible
1486 text, so we might end up at a position different from POS. This
1487 is only a problem when POS is a row start after a newline and an
1488 overlay starts there with an after-string, and the overlay has an
1489 invisible property. Since we don't skip invisible text in
1490 display_line and elsewhere immediately after consuming the
1491 newline before the row start, such a POS will not be in a string,
1492 but the call to init_iterator below will move us to the
1493 after-string. */
1494 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1495 NULL, DEFAULT_FACE_ID);
1496
1497 /* If position is within an overlay string, set up IT to
1498 the right overlay string. */
1499 if (pos->overlay_string_index >= 0)
1500 {
1501 int relative_index;
1502
1503 /* We already have the first chunk of overlay strings in
1504 IT->overlay_strings. Load more until the one for
1505 pos->overlay_string_index is in IT->overlay_strings. */
1506 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1507 {
1508 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1509 it->current.overlay_string_index = 0;
1510 while (n--)
1511 {
1512 load_overlay_strings (it);
1513 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1514 }
1515 }
1516
1517 it->current.overlay_string_index = pos->overlay_string_index;
1518 relative_index = (it->current.overlay_string_index
1519 % OVERLAY_STRING_CHUNK_SIZE);
1520 it->string = it->overlay_strings[relative_index];
1521 it->current.string_pos = pos->string_pos;
1522 it->method = next_element_from_string;
1523 }
1524 else if (CHARPOS (pos->string_pos) >= 0)
1525 {
1526 /* Recorded position is not in an overlay string, but in another
1527 string. This can only be a string from a `display' property.
1528 IT should already be filled with that string. */
1529 it->current.string_pos = pos->string_pos;
1530 xassert (STRINGP (it->string));
1531 }
1532
1533 /* Restore position in display vector translations or control
1534 character translations. */
1535 if (pos->dpvec_index >= 0)
1536 {
1537 /* This fills IT->dpvec. */
1538 get_next_display_element (it);
1539 xassert (it->dpvec && it->current.dpvec_index == 0);
1540 it->current.dpvec_index = pos->dpvec_index;
1541 }
1542
1543 CHECK_IT (it);
1544}
1545
1546
1547/* Initialize IT for stepping through current_buffer in window W
1548 starting at ROW->start. */
1549
1550static void
1551init_to_row_start (it, w, row)
1552 struct it *it;
1553 struct window *w;
1554 struct glyph_row *row;
1555{
1556 init_from_display_pos (it, w, &row->start);
1557 it->continuation_lines_width = row->continuation_lines_width;
1558 CHECK_IT (it);
1559}
1560
1561
1562/* Initialize IT for stepping through current_buffer in window W
1563 starting in the line following ROW, i.e. starting at ROW->end. */
1564
1565static void
1566init_to_row_end (it, w, row)
1567 struct it *it;
1568 struct window *w;
1569 struct glyph_row *row;
1570{
1571 init_from_display_pos (it, w, &row->end);
1572
1573 if (row->continued_p)
1574 it->continuation_lines_width = (row->continuation_lines_width
1575 + row->pixel_width);
1576 CHECK_IT (it);
1577}
1578
1579
1580
1581\f
1582/***********************************************************************
1583 Text properties
1584 ***********************************************************************/
1585
1586/* Called when IT reaches IT->stop_charpos. Handle text property and
1587 overlay changes. Set IT->stop_charpos to the next position where
1588 to stop. */
1589
1590static void
1591handle_stop (it)
1592 struct it *it;
1593{
1594 enum prop_handled handled;
1595 int handle_overlay_change_p = 1;
1596 struct props *p;
1597
1598 it->dpvec = NULL;
1599 it->current.dpvec_index = -1;
1600
1601 do
1602 {
1603 handled = HANDLED_NORMALLY;
1604
1605 /* Call text property handlers. */
1606 for (p = it_props; p->handler; ++p)
1607 {
1608 handled = p->handler (it);
1609
1610 if (handled == HANDLED_RECOMPUTE_PROPS)
1611 break;
1612 else if (handled == HANDLED_RETURN)
1613 return;
1614 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1615 handle_overlay_change_p = 0;
1616 }
1617
1618 if (handled != HANDLED_RECOMPUTE_PROPS)
1619 {
1620 /* Don't check for overlay strings below when set to deliver
1621 characters from a display vector. */
1622 if (it->method == next_element_from_display_vector)
1623 handle_overlay_change_p = 0;
1624
1625 /* Handle overlay changes. */
1626 if (handle_overlay_change_p)
1627 handled = handle_overlay_change (it);
1628
1629 /* Determine where to stop next. */
1630 if (handled == HANDLED_NORMALLY)
1631 compute_stop_pos (it);
1632 }
1633 }
1634 while (handled == HANDLED_RECOMPUTE_PROPS);
1635}
1636
1637
1638/* Compute IT->stop_charpos from text property and overlay change
1639 information for IT's current position. */
1640
1641static void
1642compute_stop_pos (it)
1643 struct it *it;
1644{
1645 register INTERVAL iv, next_iv;
1646 Lisp_Object object, limit, position;
1647
1648 /* If nowhere else, stop at the end. */
1649 it->stop_charpos = it->end_charpos;
1650
1651 if (STRINGP (it->string))
1652 {
1653 /* Strings are usually short, so don't limit the search for
1654 properties. */
1655 object = it->string;
1656 limit = Qnil;
1657 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1658 }
1659 else
1660 {
1661 int charpos;
1662
1663 /* If next overlay change is in front of the current stop pos
1664 (which is IT->end_charpos), stop there. Note: value of
1665 next_overlay_change is point-max if no overlay change
1666 follows. */
1667 charpos = next_overlay_change (IT_CHARPOS (*it));
1668 if (charpos < it->stop_charpos)
1669 it->stop_charpos = charpos;
1670
1671 /* If showing the region, we have to stop at the region
1672 start or end because the face might change there. */
1673 if (it->region_beg_charpos > 0)
1674 {
1675 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1676 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1677 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1678 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1679 }
1680
1681 /* Set up variables for computing the stop position from text
1682 property changes. */
1683 XSETBUFFER (object, current_buffer);
1684 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1685 XSETFASTINT (position, IT_CHARPOS (*it));
1686
1687 }
1688
1689 /* Get the interval containing IT's position. Value is a null
1690 interval if there isn't such an interval. */
1691 iv = validate_interval_range (object, &position, &position, 0);
1692 if (!NULL_INTERVAL_P (iv))
1693 {
1694 Lisp_Object values_here[LAST_PROP_IDX];
1695 struct props *p;
1696
1697 /* Get properties here. */
1698 for (p = it_props; p->handler; ++p)
1699 values_here[p->idx] = textget (iv->plist, *p->name);
1700
1701 /* Look for an interval following iv that has different
1702 properties. */
1703 for (next_iv = next_interval (iv);
1704 (!NULL_INTERVAL_P (next_iv)
1705 && (NILP (limit)
1706 || XFASTINT (limit) > next_iv->position));
1707 next_iv = next_interval (next_iv))
1708 {
1709 for (p = it_props; p->handler; ++p)
1710 {
1711 Lisp_Object new_value;
1712
1713 new_value = textget (next_iv->plist, *p->name);
1714 if (!EQ (values_here[p->idx], new_value))
1715 break;
1716 }
1717
1718 if (p->handler)
1719 break;
1720 }
1721
1722 if (!NULL_INTERVAL_P (next_iv))
1723 {
1724 if (INTEGERP (limit)
1725 && next_iv->position >= XFASTINT (limit))
1726 /* No text property change up to limit. */
1727 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1728 else
1729 /* Text properties change in next_iv. */
1730 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1731 }
1732 }
1733
1734 xassert (STRINGP (it->string)
1735 || (it->stop_charpos >= BEGV
1736 && it->stop_charpos >= IT_CHARPOS (*it)));
1737}
1738
1739
1740/* Return the position of the next overlay change after POS in
1741 current_buffer. Value is point-max if no overlay change
1742 follows. This is like `next-overlay-change' but doesn't use
1743 xmalloc. */
1744
1745static int
1746next_overlay_change (pos)
1747 int pos;
1748{
1749 int noverlays;
1750 int endpos;
1751 Lisp_Object *overlays;
1752 int len;
1753 int i;
1754
1755 /* Get all overlays at the given position. */
1756 len = 10;
1757 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1758 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1759 if (noverlays > len)
1760 {
1761 len = noverlays;
1762 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1763 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1764 }
1765
1766 /* If any of these overlays ends before endpos,
1767 use its ending point instead. */
1768 for (i = 0; i < noverlays; ++i)
1769 {
1770 Lisp_Object oend;
1771 int oendpos;
1772
1773 oend = OVERLAY_END (overlays[i]);
1774 oendpos = OVERLAY_POSITION (oend);
1775 endpos = min (endpos, oendpos);
1776 }
1777
1778 return endpos;
1779}
1780
1781
1782\f
1783/***********************************************************************
1784 Fontification
1785 ***********************************************************************/
1786
1787/* Handle changes in the `fontified' property of the current buffer by
1788 calling hook functions from Qfontification_functions to fontify
1789 regions of text. */
1790
1791static enum prop_handled
1792handle_fontified_prop (it)
1793 struct it *it;
1794{
1795 Lisp_Object prop, pos;
1796 enum prop_handled handled = HANDLED_NORMALLY;
1797
1798 /* Get the value of the `fontified' property at IT's current buffer
1799 position. (The `fontified' property doesn't have a special
1800 meaning in strings.) If the value is nil, call functions from
1801 Qfontification_functions. */
1802 if (!STRINGP (it->string)
1803 && it->s == NULL
1804 && !NILP (Vfontification_functions)
1805 && (pos = make_number (IT_CHARPOS (*it)),
1806 prop = Fget_char_property (pos, Qfontified, Qnil),
1807 NILP (prop)))
1808 {
1809 Lisp_Object args[2];
1810
1811 /* Run the hook functions. */
1812 args[0] = Qfontification_functions;
1813 args[1] = pos;
1814 Frun_hook_with_args (make_number (2), args);
1815
1816 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1817 something. This avoids an endless loop if they failed to
1818 fontify the text for which reason ever. */
1819 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1820 handled = HANDLED_RECOMPUTE_PROPS;
1821 }
1822
1823 return handled;
1824}
1825
1826
1827\f
1828/***********************************************************************
1829 Faces
1830 ***********************************************************************/
1831
1832/* Set up iterator IT from face properties at its current position.
1833 Called from handle_stop. */
1834
1835static enum prop_handled
1836handle_face_prop (it)
1837 struct it *it;
1838{
1839 int new_face_id, next_stop;
1840
1841 if (!STRINGP (it->string))
1842 {
1843 new_face_id
1844 = face_at_buffer_position (it->w,
1845 IT_CHARPOS (*it),
1846 it->region_beg_charpos,
1847 it->region_end_charpos,
1848 &next_stop,
1849 (IT_CHARPOS (*it)
1850 + TEXT_PROP_DISTANCE_LIMIT),
1851 0);
1852
1853 /* Is this a start of a run of characters with box face?
1854 Caveat: this can be called for a freshly initialized
1855 iterator; face_id is -1 is this case. We know that the new
1856 face will not change until limit, i.e. if the new face has a
1857 box, all characters up to limit will have one. But, as
1858 usual, we don't know whether limit is really the end. */
1859 if (new_face_id != it->face_id)
1860 {
1861 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1862
1863 /* If new face has a box but old face has not, this is
1864 the start of a run of characters with box, i.e. it has
1865 a shadow on the left side. The value of face_id of the
1866 iterator will be -1 if this is the initial call that gets
1867 the face. In this case, we have to look in front of IT's
1868 position and see whether there is a face != new_face_id. */
1869 it->start_of_box_run_p
1870 = (new_face->box != FACE_NO_BOX
1871 && (it->face_id >= 0
1872 || IT_CHARPOS (*it) == BEG
1873 || new_face_id != face_before_it_pos (it)));
1874 it->face_box_p = new_face->box != FACE_NO_BOX;
1875 }
1876 }
1877 else
1878 {
1879 new_face_id
1880 = face_at_string_position (it->w,
1881 it->string,
1882 IT_STRING_CHARPOS (*it),
1883 (it->current.overlay_string_index >= 0
1884 ? IT_CHARPOS (*it)
1885 : 0),
1886 it->region_beg_charpos,
1887 it->region_end_charpos,
1888 &next_stop,
1889 it->base_face_id);
1890
1891#if 0 /* This shouldn't be neccessary. Let's check it. */
1892 /* If IT is used to display a mode line we would really like to
1893 use the mode line face instead of the frame's default face. */
1894 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1895 && new_face_id == DEFAULT_FACE_ID)
1896 new_face_id = MODE_LINE_FACE_ID;
1897#endif
1898
1899 /* Is this a start of a run of characters with box? Caveat:
1900 this can be called for a freshly allocated iterator; face_id
1901 is -1 is this case. We know that the new face will not
1902 change until the next check pos, i.e. if the new face has a
1903 box, all characters up to that position will have a
1904 box. But, as usual, we don't know whether that position
1905 is really the end. */
1906 if (new_face_id != it->face_id)
1907 {
1908 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1909 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1910
1911 /* If new face has a box but old face hasn't, this is the
1912 start of a run of characters with box, i.e. it has a
1913 shadow on the left side. */
1914 it->start_of_box_run_p
1915 = new_face->box && (old_face == NULL || !old_face->box);
1916 it->face_box_p = new_face->box != FACE_NO_BOX;
1917 }
1918 }
1919
1920 it->face_id = new_face_id;
1921 it->charset = CHARSET_ASCII;
1922 return HANDLED_NORMALLY;
1923}
1924
1925
1926/* Compute the face one character before or after the current position
1927 of IT. BEFORE_P non-zero means get the face in front of IT's
1928 position. Value is the id of the face. */
1929
1930static int
1931face_before_or_after_it_pos (it, before_p)
1932 struct it *it;
1933 int before_p;
1934{
1935 int face_id, limit;
1936 int next_check_charpos;
1937 struct text_pos pos;
1938
1939 xassert (it->s == NULL);
1940
1941 if (STRINGP (it->string))
1942 {
1943 /* No face change past the end of the string (for the case
1944 we are padding with spaces). No face change before the
1945 string start. */
1946 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1947 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1948 return it->face_id;
1949
1950 /* Set pos to the position before or after IT's current position. */
1951 if (before_p)
1952 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1953 else
1954 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1955
1956 /* Get the face for ASCII, or unibyte. */
1957 face_id
1958 = face_at_string_position (it->w,
1959 it->string,
1960 CHARPOS (pos),
1961 (it->current.overlay_string_index >= 0
1962 ? IT_CHARPOS (*it)
1963 : 0),
1964 it->region_beg_charpos,
1965 it->region_end_charpos,
1966 &next_check_charpos,
1967 it->base_face_id);
1968
1969 /* Correct the face for charsets different from ASCII. Do it
1970 for the multibyte case only. The face returned above is
1971 suitable for unibyte text if IT->string is unibyte. */
1972 if (STRING_MULTIBYTE (it->string))
1973 {
1974 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1975 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1976 int c, len, charset;
1977
4fdb80f2 1978 c = string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1979 charset = CHAR_CHARSET (c);
1980 if (charset != CHARSET_ASCII)
1981 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1982 }
1983 }
1984 else
1985 {
70851746
GM
1986 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1987 || (IT_CHARPOS (*it) <= BEGV && before_p))
1988 return it->face_id;
1989
5f5c8ee5
GM
1990 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1991 pos = it->current.pos;
1992
1993 if (before_p)
1994 DEC_TEXT_POS (pos);
1995 else
1996 INC_TEXT_POS (pos);
70851746 1997
5f5c8ee5
GM
1998 /* Determine face for CHARSET_ASCII, or unibyte. */
1999 face_id = face_at_buffer_position (it->w,
2000 CHARPOS (pos),
2001 it->region_beg_charpos,
2002 it->region_end_charpos,
2003 &next_check_charpos,
2004 limit, 0);
2005
2006 /* Correct the face for charsets different from ASCII. Do it
2007 for the multibyte case only. The face returned above is
2008 suitable for unibyte text if current_buffer is unibyte. */
2009 if (it->multibyte_p)
2010 {
2011 int charset = charset_at_position (pos);
2012 if (charset != CHARSET_ASCII)
2013 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2014 }
2015 }
2016
2017 return face_id;
2018}
2019
2020
2021\f
2022/***********************************************************************
2023 Invisible text
2024 ***********************************************************************/
2025
2026/* Set up iterator IT from invisible properties at its current
2027 position. Called from handle_stop. */
2028
2029static enum prop_handled
2030handle_invisible_prop (it)
2031 struct it *it;
2032{
2033 enum prop_handled handled = HANDLED_NORMALLY;
2034
2035 if (STRINGP (it->string))
2036 {
2037 extern Lisp_Object Qinvisible;
2038 Lisp_Object prop, end_charpos, limit, charpos;
2039
2040 /* Get the value of the invisible text property at the
2041 current position. Value will be nil if there is no such
2042 property. */
2043 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2044 prop = Fget_text_property (charpos, Qinvisible, it->string);
2045
2046 if (!NILP (prop))
2047 {
2048 handled = HANDLED_RECOMPUTE_PROPS;
2049
2050 /* Get the position at which the next change of the
2051 invisible text property can be found in IT->string.
2052 Value will be nil if the property value is the same for
2053 all the rest of IT->string. */
2054 XSETINT (limit, XSTRING (it->string)->size);
2055 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2056 it->string, limit);
2057
2058 /* Text at current position is invisible. The next
2059 change in the property is at position end_charpos.
2060 Move IT's current position to that position. */
2061 if (INTEGERP (end_charpos)
2062 && XFASTINT (end_charpos) < XFASTINT (limit))
2063 {
2064 struct text_pos old;
2065 old = it->current.string_pos;
2066 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2067 compute_string_pos (&it->current.string_pos, old, it->string);
2068 }
2069 else
2070 {
2071 /* The rest of the string is invisible. If this is an
2072 overlay string, proceed with the next overlay string
2073 or whatever comes and return a character from there. */
2074 if (it->current.overlay_string_index >= 0)
2075 {
2076 next_overlay_string (it);
2077 /* Don't check for overlay strings when we just
2078 finished processing them. */
2079 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2080 }
2081 else
2082 {
2083 struct Lisp_String *s = XSTRING (it->string);
2084 IT_STRING_CHARPOS (*it) = s->size;
2085 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2086 }
2087 }
2088 }
2089 }
2090 else
2091 {
2092 int visible_p, newpos, next_stop;
2093 Lisp_Object pos, prop;
2094
2095 /* First of all, is there invisible text at this position? */
2096 XSETFASTINT (pos, IT_CHARPOS (*it));
2097 prop = Fget_char_property (pos, Qinvisible, it->window);
2098
2099 /* If we are on invisible text, skip over it. */
2100 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2101 {
2102 /* Record whether we have to display an ellipsis for the
2103 invisible text. */
2104 int display_ellipsis_p
2105 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2106
2107 handled = HANDLED_RECOMPUTE_PROPS;
2108
2109 /* Loop skipping over invisible text. The loop is left at
2110 ZV or with IT on the first char being visible again. */
2111 do
2112 {
2113 /* Try to skip some invisible text. Return value is the
2114 position reached which can be equal to IT's position
2115 if there is nothing invisible here. This skips both
2116 over invisible text properties and overlays with
2117 invisible property. */
2118 newpos = skip_invisible (IT_CHARPOS (*it),
2119 &next_stop, ZV, it->window);
2120
2121 /* If we skipped nothing at all we weren't at invisible
2122 text in the first place. If everything to the end of
2123 the buffer was skipped, end the loop. */
2124 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2125 visible_p = 1;
2126 else
2127 {
2128 /* We skipped some characters but not necessarily
2129 all there are. Check if we ended up on visible
2130 text. Fget_char_property returns the property of
2131 the char before the given position, i.e. if we
2132 get visible_p = 1, this means that the char at
2133 newpos is visible. */
2134 XSETFASTINT (pos, newpos);
2135 prop = Fget_char_property (pos, Qinvisible, it->window);
2136 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2137 }
2138
2139 /* If we ended up on invisible text, proceed to
2140 skip starting with next_stop. */
2141 if (!visible_p)
2142 IT_CHARPOS (*it) = next_stop;
2143 }
2144 while (!visible_p);
2145
2146 /* The position newpos is now either ZV or on visible text. */
2147 IT_CHARPOS (*it) = newpos;
2148 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2149
2150 /* Maybe return `...' next for the end of the invisible text. */
2151 if (display_ellipsis_p)
2152 {
2153 if (it->dp
2154 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2155 {
2156 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2157 it->dpvec = v->contents;
2158 it->dpend = v->contents + v->size;
2159 }
2160 else
2161 {
2162 /* Default `...'. */
2163 it->dpvec = default_invis_vector;
2164 it->dpend = default_invis_vector + 3;
2165 }
2166
2167 /* The ellipsis display does not replace the display of
2168 the character at the new position. Indicate this by
2169 setting IT->dpvec_char_len to zero. */
2170 it->dpvec_char_len = 0;
2171
2172 it->current.dpvec_index = 0;
2173 it->method = next_element_from_display_vector;
2174 }
2175 }
2176 }
2177
2178 return handled;
2179}
2180
2181
2182\f
2183/***********************************************************************
2184 'display' property
2185 ***********************************************************************/
2186
2187/* Set up iterator IT from `display' property at its current position.
2188 Called from handle_stop. */
2189
2190static enum prop_handled
2191handle_display_prop (it)
2192 struct it *it;
2193{
2194 Lisp_Object prop, object;
2195 struct text_pos *position;
2196 int space_or_image_found_p;
2197
2198 if (STRINGP (it->string))
2199 {
2200 object = it->string;
2201 position = &it->current.string_pos;
2202 }
2203 else
2204 {
2205 object = Qnil;
2206 position = &it->current.pos;
2207 }
2208
2209 /* Reset those iterator values set from display property values. */
2210 it->font_height = Qnil;
2211 it->space_width = Qnil;
2212 it->voffset = 0;
2213
2214 /* We don't support recursive `display' properties, i.e. string
2215 values that have a string `display' property, that have a string
2216 `display' property etc. */
2217 if (!it->string_from_display_prop_p)
2218 it->area = TEXT_AREA;
2219
2220 prop = Fget_char_property (make_number (position->charpos),
2221 Qdisplay, object);
2222 if (NILP (prop))
2223 return HANDLED_NORMALLY;
2224
2225 space_or_image_found_p = 0;
2226 if (CONSP (prop) && CONSP (XCAR (prop)))
2227 {
2228 while (CONSP (prop))
2229 {
2230 if (handle_single_display_prop (it, XCAR (prop), object, position))
2231 space_or_image_found_p = 1;
2232 prop = XCDR (prop);
2233 }
2234 }
2235 else if (VECTORP (prop))
2236 {
2237 int i;
2238 for (i = 0; i < XVECTOR (prop)->size; ++i)
2239 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2240 object, position))
2241 space_or_image_found_p = 1;
2242 }
2243 else
2244 {
2245 if (handle_single_display_prop (it, prop, object, position))
2246 space_or_image_found_p = 1;
2247 }
2248
2249 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2250}
2251
2252
2253/* Value is the position of the end of the `display' property stating
2254 at START_POS in OBJECT. */
2255
2256static struct text_pos
2257display_prop_end (it, object, start_pos)
2258 struct it *it;
2259 Lisp_Object object;
2260 struct text_pos start_pos;
2261{
2262 Lisp_Object end;
2263 struct text_pos end_pos;
2264
2265 /* Characters having this form of property are not displayed, so
2266 we have to find the end of the property. */
2267 end = Fnext_single_property_change (make_number (start_pos.charpos),
2268 Qdisplay, object, Qnil);
2269 if (NILP (end))
2270 {
2271 /* A nil value of `end' means there are no changes of the
2272 property to the end of the buffer or string. */
2273 if (it->current.overlay_string_index >= 0)
2274 end_pos.charpos = XSTRING (it->string)->size;
2275 else
2276 end_pos.charpos = it->end_charpos;
2277 }
2278 else
2279 end_pos.charpos = XFASTINT (end);
2280
2281 if (STRINGP (it->string))
2282 compute_string_pos (&end_pos, start_pos, it->string);
2283 else
2284 end_pos.bytepos = CHAR_TO_BYTE (end_pos.charpos);
2285
2286 return end_pos;
2287}
2288
2289
2290/* Set up IT from a single `display' sub-property value PROP. OBJECT
2291 is the object in which the `display' property was found. *POSITION
2292 is the position at which it was found.
2293
2294 If PROP is a `space' or `image' sub-property, set *POSITION to the
2295 end position of the `display' property.
2296
2297 Value is non-zero if a `space' or `image' property value was found. */
2298
2299static int
2300handle_single_display_prop (it, prop, object, position)
2301 struct it *it;
2302 Lisp_Object prop;
2303 Lisp_Object object;
2304 struct text_pos *position;
2305{
2306 Lisp_Object value;
2307 int space_or_image_found_p = 0;
2308
2309 Lisp_Object form;
2310
2311 /* If PROP is a list of the form `(:when FORM VALUE)', FORM is
2312 evaluated. If the result is nil, VALUE is ignored. */
2313 form = Qt;
2314 if (CONSP (prop) && EQ (XCAR (prop), QCwhen))
2315 {
2316 prop = XCDR (prop);
2317 if (!CONSP (prop))
2318 return 0;
2319 form = XCAR (prop);
2320 prop = XCDR (prop);
2321 if (!CONSP (prop))
2322 return 0;
2323 prop = XCAR (prop);
2324 }
2325
2326 if (!NILP (form) && !EQ (form, Qt))
2327 {
2328 struct gcpro gcpro1;
2329 struct text_pos end_pos, pt;
2330
2331 end_pos = display_prop_end (it, object, *position);
2332 GCPRO1 (form);
2333
2334 /* Temporarily set point to the end position, and then evaluate
2335 the form. This makes `(eolp)' work as FORM. */
2336 CHARPOS (pt) = PT;
2337 BYTEPOS (pt) = PT_BYTE;
2338 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2339 form = eval_form (form);
2340 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2341 UNGCPRO;
2342 }
2343
2344 if (NILP (form))
2345 return 0;
2346
2347 if (CONSP (prop)
2348 && EQ (XCAR (prop), Qheight)
2349 && CONSP (XCDR (prop)))
2350 {
2351 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2352 return 0;
2353
2354 /* `(height HEIGHT)'. */
2355 it->font_height = XCAR (XCDR (prop));
2356 if (!NILP (it->font_height))
2357 {
2358 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2359 int new_height = -1;
2360
2361 if (CONSP (it->font_height)
2362 && (EQ (XCAR (it->font_height), Qplus)
2363 || EQ (XCAR (it->font_height), Qminus))
2364 && CONSP (XCDR (it->font_height))
2365 && INTEGERP (XCAR (XCDR (it->font_height))))
2366 {
2367 /* `(+ N)' or `(- N)' where N is an integer. */
2368 int steps = XINT (XCAR (XCDR (it->font_height)));
2369 if (EQ (XCAR (it->font_height), Qplus))
2370 steps = - steps;
2371 it->face_id = smaller_face (it->f, it->face_id, steps);
2372 }
2373 else if (SYMBOLP (it->font_height))
2374 {
2375 /* Call function with current height as argument.
2376 Value is the new height. */
2377 Lisp_Object form, height;
2378 struct gcpro gcpro1;
2379
2380 height = face->lface[LFACE_HEIGHT_INDEX];
2381 form = Fcons (it->font_height, Fcons (height, Qnil));
2382 GCPRO1 (form);
2383 height = eval_form (form);
2384 if (NUMBERP (height))
2385 new_height = XFLOATINT (height);
2386 UNGCPRO;
2387 }
2388 else if (NUMBERP (it->font_height))
2389 {
2390 /* Value is a multiple of the canonical char height. */
2391 struct face *face;
2392
2393 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2394 new_height = (XFLOATINT (it->font_height)
2395 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2396 }
2397 else
2398 {
2399 /* Evaluate IT->font_height with `height' bound to the
2400 current specified height to get the new height. */
2401 Lisp_Object value;
2402 int count = specpdl_ptr - specpdl;
2403
2404 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2405 value = eval_form (it->font_height);
2406 unbind_to (count, Qnil);
2407
2408 if (NUMBERP (value))
2409 new_height = XFLOATINT (value);
2410 }
2411
2412 if (new_height > 0)
2413 it->face_id = face_with_height (it->f, it->face_id, new_height);
2414 }
2415 }
2416 else if (CONSP (prop)
2417 && EQ (XCAR (prop), Qspace_width)
2418 && CONSP (XCDR (prop)))
2419 {
2420 /* `(space_width WIDTH)'. */
2421 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2422 return 0;
2423
2424 value = XCAR (XCDR (prop));
2425 if (NUMBERP (value) && XFLOATINT (value) > 0)
2426 it->space_width = value;
2427 }
2428 else if (CONSP (prop)
2429 && EQ (XCAR (prop), Qraise)
2430 && CONSP (XCDR (prop)))
2431 {
2432#ifdef HAVE_WINDOW_SYSTEM
2433 /* `(raise FACTOR)'. */
2434 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2435 return 0;
2436
2437 value = XCAR (XCDR (prop));
2438 if (NUMBERP (value))
2439 {
2440 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2441 it->voffset = - (XFLOATINT (value)
2442 * (face->font->ascent + face->font->descent));
2443 }
2444#endif /* HAVE_WINDOW_SYSTEM */
2445 }
2446 else if (!it->string_from_display_prop_p)
2447 {
2448 /* `(left-margin VALUE)' or `(right-margin VALUE)
2449 or `(nil VALUE)' or VALUE. */
2450 Lisp_Object location, value;
2451 struct text_pos start_pos;
2452 int valid_p;
2453
2454 /* Characters having this form of property are not displayed, so
2455 we have to find the end of the property. */
2456 space_or_image_found_p = 1;
2457 start_pos = *position;
2458 *position = display_prop_end (it, object, start_pos);
2459
2460 /* Let's stop at the new position and assume that all
2461 text properties change there. */
2462 it->stop_charpos = position->charpos;
2463
2464 if (CONSP (prop)
2465 && !EQ (XCAR (prop), Qspace)
2466 && !EQ (XCAR (prop), Qimage))
2467 {
2468 location = XCAR (prop);
2469 value = XCDR (prop);
2470 }
2471 else
2472 {
2473 location = Qnil;
2474 value = prop;
2475 }
2476
2477#ifdef HAVE_WINDOW_SYSTEM
2478 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2479 valid_p = STRINGP (value);
2480 else
2481 valid_p = (STRINGP (value)
2482 || (CONSP (value) && EQ (XCAR (value), Qspace))
2483 || valid_image_p (value));
2484#else /* not HAVE_WINDOW_SYSTEM */
2485 valid_p = STRINGP (value);
2486#endif /* not HAVE_WINDOW_SYSTEM */
2487
2488 if ((EQ (location, Qleft_margin)
2489 || EQ (location, Qright_margin)
2490 || NILP (location))
2491 && valid_p)
2492 {
2493 /* Save current settings of IT so that we can restore them
2494 when we are finished with the glyph property value. */
2495 push_it (it);
2496
2497 if (NILP (location))
2498 it->area = TEXT_AREA;
2499 else if (EQ (location, Qleft_margin))
2500 it->area = LEFT_MARGIN_AREA;
2501 else
2502 it->area = RIGHT_MARGIN_AREA;
2503
2504 if (STRINGP (value))
2505 {
2506 it->string = value;
2507 it->multibyte_p = STRING_MULTIBYTE (it->string);
2508 it->current.overlay_string_index = -1;
2509 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2510 it->end_charpos = it->string_nchars
2511 = XSTRING (it->string)->size;
2512 it->method = next_element_from_string;
2513 it->stop_charpos = 0;
2514 it->string_from_display_prop_p = 1;
2515 }
2516 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2517 {
2518 it->method = next_element_from_stretch;
2519 it->object = value;
2520 it->current.pos = it->position = start_pos;
2521 }
2522#ifdef HAVE_WINDOW_SYSTEM
2523 else
2524 {
2525 it->what = IT_IMAGE;
2526 it->image_id = lookup_image (it->f, value);
2527 it->position = start_pos;
2528 it->object = NILP (object) ? it->w->buffer : object;
2529 it->method = next_element_from_image;
2530
2531 /* Say that we don't have consumed the characters with
2532 `display' property yet. The call to pop_it in
2533 set_iterator_to_next will clean this up. */
2534 *position = start_pos;
2535 }
2536#endif /* HAVE_WINDOW_SYSTEM */
2537 }
2538 }
2539
2540 return space_or_image_found_p;
2541}
2542
2543
2544\f
2545/***********************************************************************
2546 Overlay strings
2547 ***********************************************************************/
2548
2549/* The following structure is used to record overlay strings for
2550 later sorting in load_overlay_strings. */
2551
2552struct overlay_entry
2553{
2554 Lisp_Object string;
2555 int priority;
2556 int after_string_p;
2557};
2558
2559
2560/* Set up iterator IT from overlay strings at its current position.
2561 Called from handle_stop. */
2562
2563static enum prop_handled
2564handle_overlay_change (it)
2565 struct it *it;
2566{
2567 /* Overlays are handled in current_buffer only. */
2568 if (STRINGP (it->string))
2569 return HANDLED_NORMALLY;
2570 else
2571 return (get_overlay_strings (it)
2572 ? HANDLED_RECOMPUTE_PROPS
2573 : HANDLED_NORMALLY);
2574}
2575
2576
2577/* Set up the next overlay string for delivery by IT, if there is an
2578 overlay string to deliver. Called by set_iterator_to_next when the
2579 end of the current overlay string is reached. If there are more
2580 overlay strings to display, IT->string and
2581 IT->current.overlay_string_index are set appropriately here.
2582 Otherwise IT->string is set to nil. */
2583
2584static void
2585next_overlay_string (it)
2586 struct it *it;
2587{
2588 ++it->current.overlay_string_index;
2589 if (it->current.overlay_string_index == it->n_overlay_strings)
2590 {
2591 /* No more overlay strings. Restore IT's settings to what
2592 they were before overlay strings were processed, and
2593 continue to deliver from current_buffer. */
2594 pop_it (it);
2595 xassert (it->stop_charpos >= BEGV
2596 && it->stop_charpos <= it->end_charpos);
2597 it->string = Qnil;
2598 it->current.overlay_string_index = -1;
2599 SET_TEXT_POS (it->current.string_pos, -1, -1);
2600 it->n_overlay_strings = 0;
2601 it->method = next_element_from_buffer;
2602 }
2603 else
2604 {
2605 /* There are more overlay strings to process. If
2606 IT->current.overlay_string_index has advanced to a position
2607 where we must load IT->overlay_strings with more strings, do
2608 it. */
2609 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2610
2611 if (it->current.overlay_string_index && i == 0)
2612 load_overlay_strings (it);
2613
2614 /* Initialize IT to deliver display elements from the overlay
2615 string. */
2616 it->string = it->overlay_strings[i];
2617 it->multibyte_p = STRING_MULTIBYTE (it->string);
2618 SET_TEXT_POS (it->current.string_pos, 0, 0);
2619 it->method = next_element_from_string;
2620 it->stop_charpos = 0;
2621 }
2622
2623 CHECK_IT (it);
2624}
2625
2626
2627/* Compare two overlay_entry structures E1 and E2. Used as a
2628 comparison function for qsort in load_overlay_strings. Overlay
2629 strings for the same position are sorted so that
2630
2631 1. All after-strings come in front of before-strings.
2632
2633 2. Within after-strings, strings are sorted so that overlay strings
2634 from overlays with higher priorities come first.
2635
2636 2. Within before-strings, strings are sorted so that overlay
2637 strings from overlays with higher priorities come last.
2638
2639 Value is analogous to strcmp. */
2640
2641
2642static int
2643compare_overlay_entries (e1, e2)
2644 void *e1, *e2;
2645{
2646 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2647 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2648 int result;
2649
2650 if (entry1->after_string_p != entry2->after_string_p)
2651 /* Let after-strings appear in front of before-strings. */
2652 result = entry1->after_string_p ? -1 : 1;
2653 else if (entry1->after_string_p)
2654 /* After-strings sorted in order of decreasing priority. */
2655 result = entry2->priority - entry1->priority;
2656 else
2657 /* Before-strings sorted in order of increasing priority. */
2658 result = entry1->priority - entry2->priority;
2659
2660 return result;
2661}
2662
2663
2664/* Load the vector IT->overlay_strings with overlay strings from IT's
2665 current buffer position. Set IT->n_overlays to the total number of
2666 overlay strings found.
2667
2668 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2669 a time. On entry into load_overlay_strings,
2670 IT->current.overlay_string_index gives the number of overlay
2671 strings that have already been loaded by previous calls to this
2672 function.
2673
2674 Overlay strings are sorted so that after-string strings come in
2675 front of before-string strings. Within before and after-strings,
2676 strings are sorted by overlay priority. See also function
2677 compare_overlay_entries. */
2678
2679static void
2680load_overlay_strings (it)
2681 struct it *it;
2682{
2683 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2684 Lisp_Object ov, overlay, window, str;
2685 int start, end;
2686 int size = 20;
2687 int n = 0, i, j;
2688 struct overlay_entry *entries
2689 = (struct overlay_entry *) alloca (size * sizeof *entries);
2690
2691 /* Append the overlay string STRING of overlay OVERLAY to vector
2692 `entries' which has size `size' and currently contains `n'
2693 elements. AFTER_P non-zero means STRING is an after-string of
2694 OVERLAY. */
2695#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2696 do \
2697 { \
2698 Lisp_Object priority; \
2699 \
2700 if (n == size) \
2701 { \
2702 int new_size = 2 * size; \
2703 struct overlay_entry *old = entries; \
2704 entries = \
2705 (struct overlay_entry *) alloca (new_size \
2706 * sizeof *entries); \
2707 bcopy (old, entries, size * sizeof *entries); \
2708 size = new_size; \
2709 } \
2710 \
2711 entries[n].string = (STRING); \
2712 priority = Foverlay_get ((OVERLAY), Qpriority); \
2713 entries[n].priority \
2714 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2715 entries[n].after_string_p = (AFTER_P); \
2716 ++n; \
2717 } \
2718 while (0)
2719
2720 /* Process overlay before the overlay center. */
2721 for (ov = current_buffer->overlays_before;
2722 CONSP (ov);
2723 ov = XCONS (ov)->cdr)
2724 {
2725 overlay = XCONS (ov)->car;
2726 xassert (OVERLAYP (overlay));
2727 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2728 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2729
2730 if (end < IT_CHARPOS (*it))
2731 break;
2732
2733 /* Skip this overlay if it doesn't start or end at IT's current
2734 position. */
2735 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2736 continue;
2737
2738 /* Skip this overlay if it doesn't apply to IT->w. */
2739 window = Foverlay_get (overlay, Qwindow);
2740 if (WINDOWP (window) && XWINDOW (window) != it->w)
2741 continue;
2742
2743 /* If overlay has a non-empty before-string, record it. */
2744 if (start == IT_CHARPOS (*it)
2745 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2746 && XSTRING (str)->size)
2747 RECORD_OVERLAY_STRING (overlay, str, 0);
2748
2749 /* If overlay has a non-empty after-string, record it. */
2750 if (end == IT_CHARPOS (*it)
2751 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2752 && XSTRING (str)->size)
2753 RECORD_OVERLAY_STRING (overlay, str, 1);
2754 }
2755
2756 /* Process overlays after the overlay center. */
2757 for (ov = current_buffer->overlays_after;
2758 CONSP (ov);
2759 ov = XCONS (ov)->cdr)
2760 {
2761 overlay = XCONS (ov)->car;
2762 xassert (OVERLAYP (overlay));
2763 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2764 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2765
2766 if (start > IT_CHARPOS (*it))
2767 break;
2768
2769 /* Skip this overlay if it doesn't start or end at IT's current
2770 position. */
2771 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2772 continue;
2773
2774 /* Skip this overlay if it doesn't apply to IT->w. */
2775 window = Foverlay_get (overlay, Qwindow);
2776 if (WINDOWP (window) && XWINDOW (window) != it->w)
2777 continue;
2778
2779 /* If overlay has a non-empty before-string, record it. */
2780 if (start == IT_CHARPOS (*it)
2781 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2782 && XSTRING (str)->size)
2783 RECORD_OVERLAY_STRING (overlay, str, 0);
2784
2785 /* If overlay has a non-empty after-string, record it. */
2786 if (end == IT_CHARPOS (*it)
2787 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2788 && XSTRING (str)->size)
2789 RECORD_OVERLAY_STRING (overlay, str, 1);
2790 }
2791
2792#undef RECORD_OVERLAY_STRING
2793
2794 /* Sort entries. */
2795 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2796
2797 /* Record the total number of strings to process. */
2798 it->n_overlay_strings = n;
2799
2800 /* IT->current.overlay_string_index is the number of overlay strings
2801 that have already been consumed by IT. Copy some of the
2802 remaining overlay strings to IT->overlay_strings. */
2803 i = 0;
2804 j = it->current.overlay_string_index;
2805 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2806 it->overlay_strings[i++] = entries[j++].string;
2807
2808 CHECK_IT (it);
2809}
2810
2811
2812/* Get the first chunk of overlay strings at IT's current buffer
2813 position. Value is non-zero if at least one overlay string was
2814 found. */
2815
2816static int
2817get_overlay_strings (it)
2818 struct it *it;
2819{
2820 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2821 process. This fills IT->overlay_strings with strings, and sets
2822 IT->n_overlay_strings to the total number of strings to process.
2823 IT->pos.overlay_string_index has to be set temporarily to zero
2824 because load_overlay_strings needs this; it must be set to -1
2825 when no overlay strings are found because a zero value would
2826 indicate a position in the first overlay string. */
2827 it->current.overlay_string_index = 0;
2828 load_overlay_strings (it);
2829
2830 /* If we found overlay strings, set up IT to deliver display
2831 elements from the first one. Otherwise set up IT to deliver
2832 from current_buffer. */
2833 if (it->n_overlay_strings)
2834 {
2835 /* Make sure we know settings in current_buffer, so that we can
2836 restore meaningful values when we're done with the overlay
2837 strings. */
2838 compute_stop_pos (it);
2839 xassert (it->face_id >= 0);
2840
2841 /* Save IT's settings. They are restored after all overlay
2842 strings have been processed. */
2843 xassert (it->sp == 0);
2844 push_it (it);
2845
2846 /* Set up IT to deliver display elements from the first overlay
2847 string. */
2848 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2849 it->stop_charpos = 0;
2850 it->string = it->overlay_strings[0];
2851 it->multibyte_p = STRING_MULTIBYTE (it->string);
2852 xassert (STRINGP (it->string));
2853 it->method = next_element_from_string;
2854 }
2855 else
2856 {
2857 it->string = Qnil;
2858 it->current.overlay_string_index = -1;
2859 it->method = next_element_from_buffer;
2860 }
2861
2862 CHECK_IT (it);
2863
2864 /* Value is non-zero if we found at least one overlay string. */
2865 return STRINGP (it->string);
2866}
2867
2868
2869\f
2870/***********************************************************************
2871 Saving and restoring state
2872 ***********************************************************************/
2873
2874/* Save current settings of IT on IT->stack. Called, for example,
2875 before setting up IT for an overlay string, to be able to restore
2876 IT's settings to what they were after the overlay string has been
2877 processed. */
2878
2879static void
2880push_it (it)
2881 struct it *it;
2882{
2883 struct iterator_stack_entry *p;
2884
2885 xassert (it->sp < 2);
2886 p = it->stack + it->sp;
2887
2888 p->stop_charpos = it->stop_charpos;
2889 xassert (it->face_id >= 0);
2890 p->face_id = it->face_id;
2891 p->string = it->string;
2892 p->pos = it->current;
2893 p->end_charpos = it->end_charpos;
2894 p->string_nchars = it->string_nchars;
2895 p->area = it->area;
2896 p->multibyte_p = it->multibyte_p;
2897 p->space_width = it->space_width;
2898 p->font_height = it->font_height;
2899 p->voffset = it->voffset;
2900 p->string_from_display_prop_p = it->string_from_display_prop_p;
2901 ++it->sp;
2902}
2903
2904
2905/* Restore IT's settings from IT->stack. Called, for example, when no
2906 more overlay strings must be processed, and we return to delivering
2907 display elements from a buffer, or when the end of a string from a
2908 `display' property is reached and we return to delivering display
2909 elements from an overlay string, or from a buffer. */
2910
2911static void
2912pop_it (it)
2913 struct it *it;
2914{
2915 struct iterator_stack_entry *p;
2916
2917 xassert (it->sp > 0);
2918 --it->sp;
2919 p = it->stack + it->sp;
2920 it->stop_charpos = p->stop_charpos;
2921 it->face_id = p->face_id;
2922 it->string = p->string;
2923 it->current = p->pos;
2924 it->end_charpos = p->end_charpos;
2925 it->string_nchars = p->string_nchars;
2926 it->area = p->area;
2927 it->multibyte_p = p->multibyte_p;
2928 it->space_width = p->space_width;
2929 it->font_height = p->font_height;
2930 it->voffset = p->voffset;
2931 it->string_from_display_prop_p = p->string_from_display_prop_p;
2932}
2933
2934
2935\f
2936/***********************************************************************
2937 Moving over lines
2938 ***********************************************************************/
2939
2940/* Set IT's current position to the previous line start. */
2941
2942static void
2943back_to_previous_line_start (it)
2944 struct it *it;
2945{
2946 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2947 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2948}
2949
2950
2951/* Set IT's current position to the next line start. */
2952
2953static void
2954forward_to_next_line_start (it)
2955 struct it *it;
2956{
2957 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2958 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2959}
2960
2961
2962/* Set IT's current position to the previous visible line start. Skip
2963 invisible text that is so either due to text properties or due to
2964 selective display. Caution: this does not change IT->current_x and
2965 IT->hpos. */
2966
2967static void
2968back_to_previous_visible_line_start (it)
2969 struct it *it;
2970{
2971 int visible_p = 0;
2972
2973 /* Go back one newline if not on BEGV already. */
2974 if (IT_CHARPOS (*it) > BEGV)
2975 back_to_previous_line_start (it);
2976
2977 /* Move over lines that are invisible because of selective display
2978 or text properties. */
2979 while (IT_CHARPOS (*it) > BEGV
2980 && !visible_p)
2981 {
2982 visible_p = 1;
2983
2984 /* If selective > 0, then lines indented more than that values
2985 are invisible. */
2986 if (it->selective > 0
2987 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2988 it->selective))
2989 visible_p = 0;
2990#ifdef USE_TEXT_PROPERTIES
2991 else
2992 {
2993 Lisp_Object prop;
2994
2995 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
2996 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2997 visible_p = 0;
2998 }
2999#endif /* USE_TEXT_PROPERTIES */
3000
3001 /* Back one more newline if the current one is invisible. */
3002 if (!visible_p)
3003 back_to_previous_line_start (it);
3004 }
3005
3006 xassert (IT_CHARPOS (*it) >= BEGV);
3007 xassert (IT_CHARPOS (*it) == BEGV
3008 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3009 CHECK_IT (it);
3010}
3011
3012
3013/* Reseat iterator IT at the previous visible line start. Skip
3014 invisible text that is so either due to text properties or due to
3015 selective display. At the end, update IT's overlay information,
3016 face information etc. */
3017
3018static void
3019reseat_at_previous_visible_line_start (it)
3020 struct it *it;
3021{
3022 back_to_previous_visible_line_start (it);
3023 reseat (it, it->current.pos, 1);
3024 CHECK_IT (it);
3025}
3026
3027
3028/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3029 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3030 preceding the line start. Skip over invisible text that is so
3031 because of selective display. Compute faces, overlays etc at the
3032 new position. Note that this function does not skip over text that
3033 is invisible because of text properties. */
5f5c8ee5
GM
3034
3035static void
312246d1 3036reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3037 struct it *it;
312246d1 3038 int on_newline_p;
5f5c8ee5
GM
3039{
3040 /* Restore the buffer position when currently not delivering display
3041 elements from the current buffer. This is the case, for example,
3042 when called at the end of a truncated overlay string. */
3043 while (it->sp)
3044 pop_it (it);
3045 it->method = next_element_from_buffer;
3046
3047 /* Otherwise, scan_buffer would not work. */
3048 if (IT_CHARPOS (*it) < ZV)
3049 {
3050 /* If on a newline, advance past it. Otherwise, find the next
3051 newline which automatically gives us the position following
3052 the newline. */
3053 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3054 {
3055 ++IT_CHARPOS (*it);
3056 ++IT_BYTEPOS (*it);
3057 }
3058 else
3059 forward_to_next_line_start (it);
3060
3061 /* We must either have reached the end of the buffer or end up
3062 after a newline. */
3063 xassert (IT_CHARPOS (*it) == ZV
3064 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3065
3066 /* Skip over lines that are invisible because they are indented
3067 more than the value of IT->selective. */
3068 if (it->selective > 0)
3069 while (IT_CHARPOS (*it) < ZV
3070 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3071 it->selective))
3072 forward_to_next_line_start (it);
312246d1
GM
3073
3074 /* Position on the newline if we should. */
3075 if (on_newline_p && IT_CHARPOS (*it) > BEGV)
3076 {
3077 --IT_CHARPOS (*it);
3078 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3079 }
5f5c8ee5
GM
3080
3081 /* Set the iterator there. The 0 as the last parameter of
3082 reseat means don't force a text property lookup. The lookup
3083 is then only done if we've skipped past the iterator's
3084 check_charpos'es. This optimization is important because
3085 text property lookups tend to be expensive. */
3086 reseat (it, it->current.pos, 0);
3087 }
3088
3089 CHECK_IT (it);
3090}
3091
3092
3093\f
3094/***********************************************************************
3095 Changing an iterator's position
3096***********************************************************************/
3097
3098/* Change IT's current position to POS in current_buffer. If FORCE_P
3099 is non-zero, always check for text properties at the new position.
3100 Otherwise, text properties are only looked up if POS >=
3101 IT->check_charpos of a property. */
3102
3103static void
3104reseat (it, pos, force_p)
3105 struct it *it;
3106 struct text_pos pos;
3107 int force_p;
3108{
3109 int original_pos = IT_CHARPOS (*it);
3110
3111 reseat_1 (it, pos, 0);
3112
3113 /* Determine where to check text properties. Avoid doing it
3114 where possible because text property lookup is very expensive. */
3115 if (force_p
3116 || CHARPOS (pos) > it->stop_charpos
3117 || CHARPOS (pos) < original_pos)
3118 handle_stop (it);
3119
3120 CHECK_IT (it);
3121}
3122
3123
3124/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3125 IT->stop_pos to POS, also. */
3126
3127static void
3128reseat_1 (it, pos, set_stop_p)
3129 struct it *it;
3130 struct text_pos pos;
3131 int set_stop_p;
3132{
3133 /* Don't call this function when scanning a C string. */
3134 xassert (it->s == NULL);
3135
3136 /* POS must be a reasonable value. */
3137 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3138
3139 it->current.pos = it->position = pos;
3140 XSETBUFFER (it->object, current_buffer);
3141 it->dpvec = NULL;
3142 it->current.dpvec_index = -1;
3143 it->current.overlay_string_index = -1;
3144 IT_STRING_CHARPOS (*it) = -1;
3145 IT_STRING_BYTEPOS (*it) = -1;
3146 it->string = Qnil;
3147 it->method = next_element_from_buffer;
3148 it->sp = 0;
3149
3150 if (set_stop_p)
3151 it->stop_charpos = CHARPOS (pos);
3152}
3153
3154
3155/* Set up IT for displaying a string, starting at CHARPOS in window W.
3156 If S is non-null, it is a C string to iterate over. Otherwise,
3157 STRING gives a Lisp string to iterate over.
3158
3159 If PRECISION > 0, don't return more then PRECISION number of
3160 characters from the string.
3161
3162 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3163 characters have been returned. FIELD_WIDTH < 0 means an infinite
3164 field width.
3165
3166 MULTIBYTE = 0 means disable processing of multibyte characters,
3167 MULTIBYTE > 0 means enable it,
3168 MULTIBYTE < 0 means use IT->multibyte_p.
3169
3170 IT must be initialized via a prior call to init_iterator before
3171 calling this function. */
3172
3173static void
3174reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3175 struct it *it;
3176 unsigned char *s;
3177 Lisp_Object string;
3178 int charpos;
3179 int precision, field_width, multibyte;
3180{
3181 /* No region in strings. */
3182 it->region_beg_charpos = it->region_end_charpos = -1;
3183
3184 /* No text property checks performed by default, but see below. */
3185 it->stop_charpos = -1;
3186
3187 /* Set iterator position and end position. */
3188 bzero (&it->current, sizeof it->current);
3189 it->current.overlay_string_index = -1;
3190 it->current.dpvec_index = -1;
3191 it->charset = CHARSET_ASCII;
3192 xassert (charpos >= 0);
3193
3194 /* Use the setting of MULTIBYTE if specified. */
3195 if (multibyte >= 0)
3196 it->multibyte_p = multibyte > 0;
3197
3198 if (s == NULL)
3199 {
3200 xassert (STRINGP (string));
3201 it->string = string;
3202 it->s = NULL;
3203 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3204 it->method = next_element_from_string;
3205 it->current.string_pos = string_pos (charpos, string);
3206 }
3207 else
3208 {
3209 it->s = s;
3210 it->string = Qnil;
3211
3212 /* Note that we use IT->current.pos, not it->current.string_pos,
3213 for displaying C strings. */
3214 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3215 if (it->multibyte_p)
3216 {
3217 it->current.pos = c_string_pos (charpos, s, 1);
3218 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3219 }
3220 else
3221 {
3222 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3223 it->end_charpos = it->string_nchars = strlen (s);
3224 }
3225
3226 it->method = next_element_from_c_string;
3227 }
3228
3229 /* PRECISION > 0 means don't return more than PRECISION characters
3230 from the string. */
3231 if (precision > 0 && it->end_charpos - charpos > precision)
3232 it->end_charpos = it->string_nchars = charpos + precision;
3233
3234 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3235 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3236 FIELD_WIDTH < 0 means infinite field width. This is useful for
3237 padding with `-' at the end of a mode line. */
3238 if (field_width < 0)
3239 field_width = INFINITY;
3240 if (field_width > it->end_charpos - charpos)
3241 it->end_charpos = charpos + field_width;
3242
3243 /* Use the standard display table for displaying strings. */
3244 if (DISP_TABLE_P (Vstandard_display_table))
3245 it->dp = XCHAR_TABLE (Vstandard_display_table);
3246
3247 it->stop_charpos = charpos;
3248 CHECK_IT (it);
3249}
3250
3251
3252\f
3253/***********************************************************************
3254 Iteration
3255 ***********************************************************************/
3256
3257/* Load IT's display element fields with information about the next
3258 display element from the current position of IT. Value is zero if
3259 end of buffer (or C string) is reached. */
3260
3261int
3262get_next_display_element (it)
3263 struct it *it;
3264{
3265 /* Non-zero means that we found an display element. Zero means that
3266 we hit the end of what we iterate over. Performance note: the
3267 function pointer `method' used here turns out to be faster than
3268 using a sequence of if-statements. */
3269 int success_p = (*it->method) (it);
3270 int charset;
3271
3272 if (it->what == IT_CHARACTER)
3273 {
3274 /* Map via display table or translate control characters.
3275 IT->c, IT->len etc. have been set to the next character by
3276 the function call above. If we have a display table, and it
3277 contains an entry for IT->c, translate it. Don't do this if
3278 IT->c itself comes from a display table, otherwise we could
3279 end up in an infinite recursion. (An alternative could be to
3280 count the recursion depth of this function and signal an
3281 error when a certain maximum depth is reached.) Is it worth
3282 it? */
3283 if (success_p && it->dpvec == NULL)
3284 {
3285 Lisp_Object dv;
3286
3287 if (it->dp
3288 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3289 VECTORP (dv)))
3290 {
3291 struct Lisp_Vector *v = XVECTOR (dv);
3292
3293 /* Return the first character from the display table
3294 entry, if not empty. If empty, don't display the
3295 current character. */
3296 if (v->size)
3297 {
3298 it->dpvec_char_len = it->len;
3299 it->dpvec = v->contents;
3300 it->dpend = v->contents + v->size;
3301 it->current.dpvec_index = 0;
3302 it->method = next_element_from_display_vector;
3303 }
3304
3305 success_p = get_next_display_element (it);
3306 }
3307
3308 /* Translate control characters into `\003' or `^C' form.
3309 Control characters coming from a display table entry are
3310 currently not translated because we use IT->dpvec to hold
3311 the translation. This could easily be changed but I
3312 don't believe that it is worth doing. */
3313 else if ((it->c < ' '
3314 && (it->area != TEXT_AREA
c6e89d6c 3315 || (it->c != '\n' && it->c != '\t')))
54c85a23 3316 || (it->c >= 127
5f5c8ee5
GM
3317 && it->len == 1))
3318 {
3319 /* IT->c is a control character which must be displayed
3320 either as '\003' or as `^C' where the '\\' and '^'
3321 can be defined in the display table. Fill
3322 IT->ctl_chars with glyphs for what we have to
3323 display. Then, set IT->dpvec to these glyphs. */
3324 GLYPH g;
3325
54c85a23 3326 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3327 {
3328 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3329 if (it->dp
3330 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3331 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3332 g = XINT (DISP_CTRL_GLYPH (it->dp));
3333 else
3334 g = FAST_MAKE_GLYPH ('^', 0);
3335 XSETINT (it->ctl_chars[0], g);
3336
3337 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3338 XSETINT (it->ctl_chars[1], g);
3339
3340 /* Set up IT->dpvec and return first character from it. */
3341 it->dpvec_char_len = it->len;
3342 it->dpvec = it->ctl_chars;
3343 it->dpend = it->dpvec + 2;
3344 it->current.dpvec_index = 0;
3345 it->method = next_element_from_display_vector;
3346 get_next_display_element (it);
3347 }
3348 else
3349 {
3350 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3351 if (it->dp
3352 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3353 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3354 g = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3355 else
3356 g = FAST_MAKE_GLYPH ('\\', 0);
3357 XSETINT (it->ctl_chars[0], g);
3358
3359 /* Insert three more glyphs into IT->ctl_chars for
3360 the octal display of the character. */
3361 g = FAST_MAKE_GLYPH (((it->c >> 6) & 7) + '0', 0);
3362 XSETINT (it->ctl_chars[1], g);
3363 g = FAST_MAKE_GLYPH (((it->c >> 3) & 7) + '0', 0);
3364 XSETINT (it->ctl_chars[2], g);
3365 g = FAST_MAKE_GLYPH ((it->c & 7) + '0', 0);
3366 XSETINT (it->ctl_chars[3], g);
3367
3368 /* Set up IT->dpvec and return the first character
3369 from it. */
3370 it->dpvec_char_len = it->len;
3371 it->dpvec = it->ctl_chars;
3372 it->dpend = it->dpvec + 4;
3373 it->current.dpvec_index = 0;
3374 it->method = next_element_from_display_vector;
3375 get_next_display_element (it);
3376 }
3377 }
3378 }
3379
3380 /* Adjust face id if charset changes. There are no charset
3381 changes in unibyte text because Emacs' charsets are not
3382 applicable there. */
3383 if (it->multibyte_p
3384 && success_p
3385 && (charset = CHAR_CHARSET (it->c),
3386 charset != it->charset))
3387 {
3388 it->charset = charset;
3389 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3390 }
3391 }
3392
3393 /* Is this character the last one of a run of characters with
3394 box? If yes, set IT->end_of_box_run_p to 1. */
3395 if (it->face_box_p
3396 && it->s == NULL)
3397 {
3398 int face_id;
3399 struct face *face;
3400
3401 it->end_of_box_run_p
3402 = ((face_id = face_after_it_pos (it),
3403 face_id != it->face_id)
3404 && (face = FACE_FROM_ID (it->f, face_id),
3405 face->box == FACE_NO_BOX));
3406 }
3407
3408 /* Value is 0 if end of buffer or string reached. */
3409 return success_p;
3410}
3411
3412
3413/* Move IT to the next display element.
3414
3415 Functions get_next_display_element and set_iterator_to_next are
3416 separate because I find this arrangement easier to handle than a
3417 get_next_display_element function that also increments IT's
3418 position. The way it is we can first look at an iterator's current
3419 display element, decide whether it fits on a line, and if it does,
3420 increment the iterator position. The other way around we probably
3421 would either need a flag indicating whether the iterator has to be
3422 incremented the next time, or we would have to implement a
3423 decrement position function which would not be easy to write. */
3424
3425void
3426set_iterator_to_next (it)
3427 struct it *it;
3428{
3429 if (it->method == next_element_from_buffer)
3430 {
3431 /* The current display element of IT is a character from
3432 current_buffer. Advance in the buffer, and maybe skip over
3433 invisible lines that are so because of selective display. */
3434 if (ITERATOR_AT_END_OF_LINE_P (it))
312246d1 3435 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
3436 else
3437 {
3438 xassert (it->len != 0);
3439 IT_BYTEPOS (*it) += it->len;
3440 IT_CHARPOS (*it) += 1;
3441 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3442 }
3443 }
3444 else if (it->method == next_element_from_c_string)
3445 {
3446 /* Current display element of IT is from a C string. */
3447 IT_BYTEPOS (*it) += it->len;
3448 IT_CHARPOS (*it) += 1;
3449 }
3450 else if (it->method == next_element_from_display_vector)
3451 {
3452 /* Current display element of IT is from a display table entry.
3453 Advance in the display table definition. Reset it to null if
3454 end reached, and continue with characters from buffers/
3455 strings. */
3456 ++it->current.dpvec_index;
286bcbc9
GM
3457
3458 /* Restore face and charset of the iterator to what they were
3459 before the display vector entry (these entries may contain
3460 faces, and of course characters of different charsets). */
5f5c8ee5 3461 it->face_id = it->saved_face_id;
286bcbc9
GM
3462 it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
3463
5f5c8ee5
GM
3464 if (it->dpvec + it->current.dpvec_index == it->dpend)
3465 {
3466 if (it->s)
3467 it->method = next_element_from_c_string;
3468 else if (STRINGP (it->string))
3469 it->method = next_element_from_string;
3470 else
3471 it->method = next_element_from_buffer;
3472
3473 it->dpvec = NULL;
3474 it->current.dpvec_index = -1;
3475
312246d1
GM
3476 /* Skip over characters which were displayed via IT->dpvec. */
3477 if (it->dpvec_char_len < 0)
3478 reseat_at_next_visible_line_start (it, 1);
3479 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
3480 {
3481 it->len = it->dpvec_char_len;
3482 set_iterator_to_next (it);
3483 }
3484 }
3485 }
3486 else if (it->method == next_element_from_string)
3487 {
3488 /* Current display element is a character from a Lisp string. */
3489 xassert (it->s == NULL && STRINGP (it->string));
3490 IT_STRING_BYTEPOS (*it) += it->len;
3491 IT_STRING_CHARPOS (*it) += 1;
3492
3493 consider_string_end:
3494
3495 if (it->current.overlay_string_index >= 0)
3496 {
3497 /* IT->string is an overlay string. Advance to the
3498 next, if there is one. */
3499 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3500 next_overlay_string (it);
3501 }
3502 else
3503 {
3504 /* IT->string is not an overlay string. If we reached
3505 its end, and there is something on IT->stack, proceed
3506 with what is on the stack. This can be either another
3507 string, this time an overlay string, or a buffer. */
3508 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3509 && it->sp > 0)
3510 {
3511 pop_it (it);
3512 if (!STRINGP (it->string))
3513 it->method = next_element_from_buffer;
3514 }
3515 }
3516 }
3517 else if (it->method == next_element_from_image
3518 || it->method == next_element_from_stretch)
3519 {
3520 /* The position etc with which we have to proceed are on
3521 the stack. The position may be at the end of a string,
3522 if the `display' property takes up the whole string. */
3523 pop_it (it);
3524 it->image_id = 0;
3525 if (STRINGP (it->string))
3526 {
3527 it->method = next_element_from_string;
3528 goto consider_string_end;
3529 }
3530 else
3531 it->method = next_element_from_buffer;
3532 }
3533 else
3534 /* There are no other methods defined, so this should be a bug. */
3535 abort ();
3536
3537 /* Reset flags indicating start and end of a sequence of
3538 characters with box. */
3539 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3540
3541 xassert (it->method != next_element_from_string
3542 || (STRINGP (it->string)
3543 && IT_STRING_CHARPOS (*it) >= 0));
3544}
3545
3546
3547/* Load IT's display element fields with information about the next
3548 display element which comes from a display table entry or from the
3549 result of translating a control character to one of the forms `^C'
3550 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3551
3552static int
3553next_element_from_display_vector (it)
3554 struct it *it;
3555{
3556 /* Precondition. */
3557 xassert (it->dpvec && it->current.dpvec_index >= 0);
3558
3559 /* Remember the current face id in case glyphs specify faces.
3560 IT's face is restored in set_iterator_to_next. */
3561 it->saved_face_id = it->face_id;
3562
3563 if (INTEGERP (*it->dpvec)
3564 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3565 {
3566 int lface_id;
3567 GLYPH g;
3568
3569 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3570 it->c = FAST_GLYPH_CHAR (g);
3571 it->len = CHAR_LEN (it->c);
3572
3573 /* The entry may contain a face id to use. Such a face id is
3574 the id of a Lisp face, not a realized face. A face id of
3575 zero means no face. */
3576 lface_id = FAST_GLYPH_FACE (g);
3577 if (lface_id)
3578 {
3579 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3580 if (face_id >= 0)
3581 {
3582 it->face_id = face_id;
3583 it->charset = CHARSET_ASCII;
3584 }
3585 }
3586 }
3587 else
3588 /* Display table entry is invalid. Return a space. */
3589 it->c = ' ', it->len = 1;
3590
3591 /* Don't change position and object of the iterator here. They are
3592 still the values of the character that had this display table
3593 entry or was translated, and that's what we want. */
3594 it->what = IT_CHARACTER;
3595 return 1;
3596}
3597
3598
3599/* Load IT with the next display element from Lisp string IT->string.
3600 IT->current.string_pos is the current position within the string.
3601 If IT->current.overlay_string_index >= 0, the Lisp string is an
3602 overlay string. */
3603
3604static int
3605next_element_from_string (it)
3606 struct it *it;
3607{
3608 struct text_pos position;
3609
3610 xassert (STRINGP (it->string));
3611 xassert (IT_STRING_CHARPOS (*it) >= 0);
3612 position = it->current.string_pos;
3613
3614 /* Time to check for invisible text? */
3615 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3616 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3617 {
3618 handle_stop (it);
3619
3620 /* Since a handler may have changed IT->method, we must
3621 recurse here. */
3622 return get_next_display_element (it);
3623 }
3624
3625 if (it->current.overlay_string_index >= 0)
3626 {
3627 /* Get the next character from an overlay string. In overlay
3628 strings, There is no field width or padding with spaces to
3629 do. */
3630 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3631 {
3632 it->what = IT_EOB;
3633 return 0;
3634 }
3635 else if (STRING_MULTIBYTE (it->string))
3636 {
3637 int remaining = (STRING_BYTES (XSTRING (it->string))
3638 - IT_STRING_BYTEPOS (*it));
3639 unsigned char *s = (XSTRING (it->string)->data
3640 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3641 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
3642 }
3643 else
3644 {
3645 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3646 it->len = 1;
3647 }
3648 }
3649 else
3650 {
3651 /* Get the next character from a Lisp string that is not an
3652 overlay string. Such strings come from the mode line, for
3653 example. We may have to pad with spaces, or truncate the
3654 string. See also next_element_from_c_string. */
3655 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3656 {
3657 it->what = IT_EOB;
3658 return 0;
3659 }
3660 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3661 {
3662 /* Pad with spaces. */
3663 it->c = ' ', it->len = 1;
3664 CHARPOS (position) = BYTEPOS (position) = -1;
3665 }
3666 else if (STRING_MULTIBYTE (it->string))
3667 {
3668 int maxlen = (STRING_BYTES (XSTRING (it->string))
3669 - IT_STRING_BYTEPOS (*it));
3670 unsigned char *s = (XSTRING (it->string)->data
3671 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3672 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
3673 }
3674 else
3675 {
3676 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3677 it->len = 1;
3678 }
3679 }
3680
3681 /* Record what we have and where it came from. Note that we store a
3682 buffer position in IT->position although it could arguably be a
3683 string position. */
3684 it->what = IT_CHARACTER;
3685 it->object = it->string;
3686 it->position = position;
3687 return 1;
3688}
3689
3690
3691/* Load IT with next display element from C string IT->s.
3692 IT->string_nchars is the maximum number of characters to return
3693 from the string. IT->end_charpos may be greater than
3694 IT->string_nchars when this function is called, in which case we
3695 may have to return padding spaces. Value is zero if end of string
3696 reached, including padding spaces. */
3697
3698static int
3699next_element_from_c_string (it)
3700 struct it *it;
3701{
3702 int success_p = 1;
3703
3704 xassert (it->s);
3705 it->what = IT_CHARACTER;
3706 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3707 it->object = Qnil;
3708
3709 /* IT's position can be greater IT->string_nchars in case a field
3710 width or precision has been specified when the iterator was
3711 initialized. */
3712 if (IT_CHARPOS (*it) >= it->end_charpos)
3713 {
3714 /* End of the game. */
3715 it->what = IT_EOB;
3716 success_p = 0;
3717 }
3718 else if (IT_CHARPOS (*it) >= it->string_nchars)
3719 {
3720 /* Pad with spaces. */
3721 it->c = ' ', it->len = 1;
3722 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3723 }
3724 else if (it->multibyte_p)
3725 {
3726 /* Implementation note: The calls to strlen apparently aren't a
3727 performance problem because there is no noticeable performance
3728 difference between Emacs running in unibyte or multibyte mode. */
3729 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
3730 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3731 maxlen, &it->len);
5f5c8ee5
GM
3732 }
3733 else
3734 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3735
3736 return success_p;
3737}
3738
3739
3740/* Set up IT to return characters from an ellipsis, if appropriate.
3741 The definition of the ellipsis glyphs may come from a display table
3742 entry. This function Fills IT with the first glyph from the
3743 ellipsis if an ellipsis is to be displayed. */
3744
3745static void
3746next_element_from_ellipsis (it)
3747 struct it *it;
3748{
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3750 {
3751 /* Use the display table definition for `...'. Invalid glyphs
3752 will be handled by the method returning elements from dpvec. */
3753 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3754 it->dpvec_char_len = it->len;
3755 it->dpvec = v->contents;
3756 it->dpend = v->contents + v->size;
3757 it->current.dpvec_index = 0;
3758 it->method = next_element_from_display_vector;
3759 get_next_display_element (it);
3760 }
3761 else if (it->selective_display_ellipsis_p)
3762 {
3763 /* Use default `...' which is stored in default_invis_vector. */
3764 it->dpvec_char_len = it->len;
3765 it->dpvec = default_invis_vector;
3766 it->dpend = default_invis_vector + 3;
3767 it->current.dpvec_index = 0;
3768 it->method = next_element_from_display_vector;
3769 get_next_display_element (it);
3770 }
3771}
3772
3773
3774/* Deliver an image display element. The iterator IT is already
3775 filled with image information (done in handle_display_prop). Value
3776 is always 1. */
3777
3778
3779static int
3780next_element_from_image (it)
3781 struct it *it;
3782{
3783 it->what = IT_IMAGE;
3784 return 1;
3785}
3786
3787
3788/* Fill iterator IT with next display element from a stretch glyph
3789 property. IT->object is the value of the text property. Value is
3790 always 1. */
3791
3792static int
3793next_element_from_stretch (it)
3794 struct it *it;
3795{
3796 it->what = IT_STRETCH;
3797 return 1;
3798}
3799
3800
3801/* Load IT with the next display element from current_buffer. Value
3802 is zero if end of buffer reached. IT->stop_charpos is the next
3803 position at which to stop and check for text properties or buffer
3804 end. */
3805
3806static int
3807next_element_from_buffer (it)
3808 struct it *it;
3809{
3810 int success_p = 1;
3811
3812 /* Check this assumption, otherwise, we would never enter the
3813 if-statement, below. */
3814 xassert (IT_CHARPOS (*it) >= BEGV
3815 && IT_CHARPOS (*it) <= it->stop_charpos);
3816
3817 if (IT_CHARPOS (*it) >= it->stop_charpos)
3818 {
3819 if (IT_CHARPOS (*it) >= it->end_charpos)
3820 {
3821 int overlay_strings_follow_p;
3822
3823 /* End of the game, except when overlay strings follow that
3824 haven't been returned yet. */
3825 if (it->overlay_strings_at_end_processed_p)
3826 overlay_strings_follow_p = 0;
3827 else
3828 {
3829 it->overlay_strings_at_end_processed_p = 1;
3830 overlay_strings_follow_p
3831 = get_overlay_strings (it);
3832 }
3833
3834 if (overlay_strings_follow_p)
3835 success_p = get_next_display_element (it);
3836 else
3837 {
3838 it->what = IT_EOB;
3839 it->position = it->current.pos;
3840 success_p = 0;
3841 }
3842 }
3843 else
3844 {
3845 handle_stop (it);
3846 return get_next_display_element (it);
3847 }
3848 }
3849 else
3850 {
3851 /* No face changes, overlays etc. in sight, so just return a
3852 character from current_buffer. */
3853 unsigned char *p;
3854
3855 /* Maybe run the redisplay end trigger hook. Performance note:
3856 This doesn't seem to cost measurable time. */
3857 if (it->redisplay_end_trigger_charpos
3858 && it->glyph_row
3859 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3860 run_redisplay_end_trigger_hook (it);
3861
3862 /* Get the next character, maybe multibyte. */
3863 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3864 if (it->multibyte_p)
3865 {
3866 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3867 - IT_BYTEPOS (*it));
4fdb80f2 3868 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
3869 }
3870 else
3871 it->c = *p, it->len = 1;
3872
3873 /* Record what we have and where it came from. */
3874 it->what = IT_CHARACTER;;
3875 it->object = it->w->buffer;
3876 it->position = it->current.pos;
3877
3878 /* Normally we return the character found above, except when we
3879 really want to return an ellipsis for selective display. */
3880 if (it->selective)
3881 {
3882 if (it->c == '\n')
3883 {
3884 /* A value of selective > 0 means hide lines indented more
3885 than that number of columns. */
3886 if (it->selective > 0
3887 && IT_CHARPOS (*it) + 1 < ZV
3888 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3889 IT_BYTEPOS (*it) + 1,
3890 it->selective))
312246d1
GM
3891 {
3892 next_element_from_ellipsis (it);
3893 it->dpvec_char_len = -1;
3894 }
5f5c8ee5
GM
3895 }
3896 else if (it->c == '\r' && it->selective == -1)
3897 {
3898 /* A value of selective == -1 means that everything from the
3899 CR to the end of the line is invisible, with maybe an
3900 ellipsis displayed for it. */
3901 next_element_from_ellipsis (it);
312246d1 3902 it->dpvec_char_len = -1;
5f5c8ee5
GM
3903 }
3904 }
3905 }
3906
3907 /* Value is zero if end of buffer reached. */
3908 xassert (!success_p || it->len > 0);
3909 return success_p;
3910}
3911
3912
3913/* Run the redisplay end trigger hook for IT. */
3914
3915static void
3916run_redisplay_end_trigger_hook (it)
3917 struct it *it;
3918{
3919 Lisp_Object args[3];
3920
3921 /* IT->glyph_row should be non-null, i.e. we should be actually
3922 displaying something, or otherwise we should not run the hook. */
3923 xassert (it->glyph_row);
3924
3925 /* Set up hook arguments. */
3926 args[0] = Qredisplay_end_trigger_functions;
3927 args[1] = it->window;
3928 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3929 it->redisplay_end_trigger_charpos = 0;
3930
3931 /* Since we are *trying* to run these functions, don't try to run
3932 them again, even if they get an error. */
3933 it->w->redisplay_end_trigger = Qnil;
3934 Frun_hook_with_args (3, args);
3935
3936 /* Notice if it changed the face of the character we are on. */
3937 handle_face_prop (it);
3938}
3939
3940
3941\f
3942/***********************************************************************
3943 Moving an iterator without producing glyphs
3944 ***********************************************************************/
3945
3946/* Move iterator IT to a specified buffer or X position within one
3947 line on the display without producing glyphs.
3948
3949 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3950 whichever is reached first.
3951
3952 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3953
3954 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3955 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3956 TO_X includes the amount by which a window is horizontally
3957 scrolled.
3958
3959 Value is
3960
3961 MOVE_POS_MATCH_OR_ZV
3962 - when TO_POS or ZV was reached.
3963
3964 MOVE_X_REACHED
3965 -when TO_X was reached before TO_POS or ZV were reached.
3966
3967 MOVE_LINE_CONTINUED
3968 - when we reached the end of the display area and the line must
3969 be continued.
3970
3971 MOVE_LINE_TRUNCATED
3972 - when we reached the end of the display area and the line is
3973 truncated.
3974
3975 MOVE_NEWLINE_OR_CR
3976 - when we stopped at a line end, i.e. a newline or a CR and selective
3977 display is on. */
3978
3979enum move_it_result
3980move_it_in_display_line_to (it, to_charpos, to_x, op)
3981 struct it *it;
3982 int to_charpos, to_x, op;
3983{
3984 enum move_it_result result = MOVE_UNDEFINED;
3985 struct glyph_row *saved_glyph_row;
3986
3987 /* Don't produce glyphs in produce_glyphs. */
3988 saved_glyph_row = it->glyph_row;
3989 it->glyph_row = NULL;
3990
5f5c8ee5
GM
3991 while (1)
3992 {
3993 int x, i;
3994
3995 /* Stop when ZV or TO_CHARPOS reached. */
3996 if (!get_next_display_element (it)
3997 || ((op & MOVE_TO_POS) != 0
3998 && BUFFERP (it->object)
3999 && IT_CHARPOS (*it) >= to_charpos))
4000 {
4001 result = MOVE_POS_MATCH_OR_ZV;
4002 break;
4003 }
4004
4005 /* The call to produce_glyphs will get the metrics of the
4006 display element IT is loaded with. We record in x the
4007 x-position before this display element in case it does not
4008 fit on the line. */
4009 x = it->current_x;
4010 PRODUCE_GLYPHS (it);
4011
4012 if (it->area != TEXT_AREA)
4013 {
4014 set_iterator_to_next (it);
4015 continue;
4016 }
4017
4018 /* The number of glyphs we get back in IT->nglyphs will normally
4019 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4020 character on a terminal frame, or (iii) a line end. For the
4021 second case, IT->nglyphs - 1 padding glyphs will be present
4022 (on X frames, there is only one glyph produced for a
4023 composite character.
4024
4025 The behavior implemented below means, for continuation lines,
4026 that as many spaces of a TAB as fit on the current line are
4027 displayed there. For terminal frames, as many glyphs of a
4028 multi-glyph character are displayed in the current line, too.
4029 This is what the old redisplay code did, and we keep it that
4030 way. Under X, the whole shape of a complex character must
4031 fit on the line or it will be completely displayed in the
4032 next line.
4033
4034 Note that both for tabs and padding glyphs, all glyphs have
4035 the same width. */
4036 if (it->nglyphs)
4037 {
4038 /* More than one glyph or glyph doesn't fit on line. All
4039 glyphs have the same width. */
4040 int single_glyph_width = it->pixel_width / it->nglyphs;
4041 int new_x;
4042
4043 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4044 {
4045 new_x = x + single_glyph_width;
4046
4047 /* We want to leave anything reaching TO_X to the caller. */
4048 if ((op & MOVE_TO_X) && new_x > to_x)
4049 {
4050 it->current_x = x;
4051 result = MOVE_X_REACHED;
4052 break;
4053 }
4054 else if (/* Lines are continued. */
4055 !it->truncate_lines_p
4056 && (/* And glyph doesn't fit on the line. */
4057 new_x > it->last_visible_x
4058 /* Or it fits exactly and we're on a window
4059 system frame. */
4060 || (new_x == it->last_visible_x
4061 && FRAME_WINDOW_P (it->f))))
4062 {
4063 if (/* IT->hpos == 0 means the very first glyph
4064 doesn't fit on the line, e.g. a wide image. */
4065 it->hpos == 0
4066 || (new_x == it->last_visible_x
4067 && FRAME_WINDOW_P (it->f)))
4068 {
4069 ++it->hpos;
4070 it->current_x = new_x;
4071 if (i == it->nglyphs - 1)
4072 set_iterator_to_next (it);
4073 }
4074 else
4075 it->current_x = x;
4076
4077 result = MOVE_LINE_CONTINUED;
4078 break;
4079 }
4080 else if (new_x > it->first_visible_x)
4081 {
4082 /* Glyph is visible. Increment number of glyphs that
4083 would be displayed. */
4084 ++it->hpos;
4085 }
4086 else
4087 {
4088 /* Glyph is completely off the left margin of the display
4089 area. Nothing to do. */
4090 }
4091 }
4092
4093 if (result != MOVE_UNDEFINED)
4094 break;
4095 }
4096 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4097 {
4098 /* Stop when TO_X specified and reached. This check is
4099 necessary here because of lines consisting of a line end,
4100 only. The line end will not produce any glyphs and we
4101 would never get MOVE_X_REACHED. */
4102 xassert (it->nglyphs == 0);
4103 result = MOVE_X_REACHED;
4104 break;
4105 }
4106
4107 /* Is this a line end? If yes, we're done. */
4108 if (ITERATOR_AT_END_OF_LINE_P (it))
4109 {
4110 result = MOVE_NEWLINE_OR_CR;
4111 break;
4112 }
4113
4114 /* The current display element has been consumed. Advance
4115 to the next. */
4116 set_iterator_to_next (it);
4117
4118 /* Stop if lines are truncated and IT's current x-position is
4119 past the right edge of the window now. */
4120 if (it->truncate_lines_p
4121 && it->current_x >= it->last_visible_x)
4122 {
4123 result = MOVE_LINE_TRUNCATED;
4124 break;
4125 }
4126 }
4127
4128 /* Restore the iterator settings altered at the beginning of this
4129 function. */
4130 it->glyph_row = saved_glyph_row;
4131 return result;
4132}
4133
4134
4135/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4136 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4137 the description of enum move_operation_enum.
4138
4139 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4140 screen line, this function will set IT to the next position >
4141 TO_CHARPOS. */
4142
4143void
4144move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4145 struct it *it;
4146 int to_charpos, to_x, to_y, to_vpos;
4147 int op;
4148{
4149 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4150 int line_height;
4151
5f5c8ee5
GM
4152 while (1)
4153 {
4154 if (op & MOVE_TO_VPOS)
4155 {
4156 /* If no TO_CHARPOS and no TO_X specified, stop at the
4157 start of the line TO_VPOS. */
4158 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4159 {
4160 if (it->vpos == to_vpos)
4161 break;
4162 skip = move_it_in_display_line_to (it, -1, -1, 0);
4163 }
4164 else
4165 {
4166 /* TO_VPOS >= 0 means stop at TO_X in the line at
4167 TO_VPOS, or at TO_POS, whichever comes first. */
4168 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4169
4170 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4171 break;
4172 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4173 {
4174 /* We have reached TO_X but not in the line we want. */
4175 skip = move_it_in_display_line_to (it, to_charpos,
4176 -1, MOVE_TO_POS);
4177 if (skip == MOVE_POS_MATCH_OR_ZV)
4178 break;
4179 }
4180 }
4181 }
4182 else if (op & MOVE_TO_Y)
4183 {
4184 struct it it_backup;
4185 int done_p;
4186
4187 /* TO_Y specified means stop at TO_X in the line containing
4188 TO_Y---or at TO_CHARPOS if this is reached first. The
4189 problem is that we can't really tell whether the line
4190 contains TO_Y before we have completely scanned it, and
4191 this may skip past TO_X. What we do is to first scan to
4192 TO_X.
4193
4194 If TO_X is not specified, use a TO_X of zero. The reason
4195 is to make the outcome of this function more predictable.
4196 If we didn't use TO_X == 0, we would stop at the end of
4197 the line which is probably not what a caller would expect
4198 to happen. */
4199 skip = move_it_in_display_line_to (it, to_charpos,
4200 ((op & MOVE_TO_X)
4201 ? to_x : 0),
4202 (MOVE_TO_X
4203 | (op & MOVE_TO_POS)));
4204
4205 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4206 if (skip == MOVE_POS_MATCH_OR_ZV)
4207 break;
4208
4209 /* If TO_X was reached, we would like to know whether TO_Y
4210 is in the line. This can only be said if we know the
4211 total line height which requires us to scan the rest of
4212 the line. */
4213 done_p = 0;
4214 if (skip == MOVE_X_REACHED)
4215 {
4216 it_backup = *it;
4217 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4218 op & MOVE_TO_POS);
4219 }
4220
4221 /* Now, decide whether TO_Y is in this line. */
4222 line_height = it->max_ascent + it->max_descent;
4223
4224 if (to_y >= it->current_y
4225 && to_y < it->current_y + line_height)
4226 {
4227 if (skip == MOVE_X_REACHED)
4228 /* If TO_Y is in this line and TO_X was reached above,
4229 we scanned too far. We have to restore IT's settings
4230 to the ones before skipping. */
4231 *it = it_backup;
4232 done_p = 1;
4233 }
4234 else if (skip == MOVE_X_REACHED)
4235 {
4236 skip = skip2;
4237 if (skip == MOVE_POS_MATCH_OR_ZV)
4238 done_p = 1;
4239 }
4240
4241 if (done_p)
4242 break;
4243 }
4244 else
4245 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4246
4247 switch (skip)
4248 {
4249 case MOVE_POS_MATCH_OR_ZV:
4250 return;
4251
4252 case MOVE_NEWLINE_OR_CR:
4253 set_iterator_to_next (it);
4254 it->continuation_lines_width = 0;
4255 break;
4256
4257 case MOVE_LINE_TRUNCATED:
4258 it->continuation_lines_width = 0;
312246d1 4259 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4260 if ((op & MOVE_TO_POS) != 0
4261 && IT_CHARPOS (*it) > to_charpos)
4262 goto out;
4263 break;
4264
4265 case MOVE_LINE_CONTINUED:
4266 it->continuation_lines_width += it->current_x;
4267 break;
4268
4269 default:
4270 abort ();
4271 }
4272
4273 /* Reset/increment for the next run. */
4274 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4275 it->current_x = it->hpos = 0;
4276 it->current_y += it->max_ascent + it->max_descent;
4277 ++it->vpos;
4278 last_height = it->max_ascent + it->max_descent;
4279 last_max_ascent = it->max_ascent;
4280 it->max_ascent = it->max_descent = 0;
4281 }
4282 out:;
4283}
4284
4285
4286/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4287
4288 If DY > 0, move IT backward at least that many pixels. DY = 0
4289 means move IT backward to the preceding line start or BEGV. This
4290 function may move over more than DY pixels if IT->current_y - DY
4291 ends up in the middle of a line; in this case IT->current_y will be
4292 set to the top of the line moved to. */
4293
4294void
4295move_it_vertically_backward (it, dy)
4296 struct it *it;
4297 int dy;
4298{
4299 int nlines, h, line_height;
4300 struct it it2;
4301 int start_pos = IT_CHARPOS (*it);
4302
4303 xassert (dy >= 0);
4304
4305 /* Estimate how many newlines we must move back. */
4306 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4307
4308 /* Set the iterator's position that many lines back. */
4309 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4310 back_to_previous_visible_line_start (it);
4311
4312 /* Reseat the iterator here. When moving backward, we don't want
4313 reseat to skip forward over invisible text, set up the iterator
4314 to deliver from overlay strings at the new position etc. So,
4315 use reseat_1 here. */
4316 reseat_1 (it, it->current.pos, 1);
4317
4318 /* We are now surely at a line start. */
4319 it->current_x = it->hpos = 0;
4320
4321 /* Move forward and see what y-distance we moved. First move to the
4322 start of the next line so that we get its height. We need this
4323 height to be able to tell whether we reached the specified
4324 y-distance. */
4325 it2 = *it;
4326 it2.max_ascent = it2.max_descent = 0;
4327 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4328 MOVE_TO_POS | MOVE_TO_VPOS);
4329 xassert (IT_CHARPOS (*it) >= BEGV);
4330 line_height = it2.max_ascent + it2.max_descent;
4331 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4332 xassert (IT_CHARPOS (*it) >= BEGV);
4333 h = it2.current_y - it->current_y;
4334 nlines = it2.vpos - it->vpos;
4335
4336 /* Correct IT's y and vpos position. */
4337 it->vpos -= nlines;
4338 it->current_y -= h;
4339
4340 if (dy == 0)
4341 {
4342 /* DY == 0 means move to the start of the screen line. The
4343 value of nlines is > 0 if continuation lines were involved. */
4344 if (nlines > 0)
4345 move_it_by_lines (it, nlines, 1);
4346 xassert (IT_CHARPOS (*it) <= start_pos);
4347 }
4348 else if (nlines)
4349 {
4350 /* The y-position we try to reach. Note that h has been
4351 subtracted in front of the if-statement. */
4352 int target_y = it->current_y + h - dy;
4353
4354 /* If we did not reach target_y, try to move further backward if
4355 we can. If we moved too far backward, try to move forward. */
4356 if (target_y < it->current_y
4357 && IT_CHARPOS (*it) > BEGV)
4358 {
4359 move_it_vertically (it, target_y - it->current_y);
4360 xassert (IT_CHARPOS (*it) >= BEGV);
4361 }
4362 else if (target_y >= it->current_y + line_height
4363 && IT_CHARPOS (*it) < ZV)
4364 {
4365 move_it_vertically (it, target_y - (it->current_y + line_height));
4366 xassert (IT_CHARPOS (*it) >= BEGV);
4367 }
4368 }
4369}
4370
4371
4372/* Move IT by a specified amount of pixel lines DY. DY negative means
4373 move backwards. DY = 0 means move to start of screen line. At the
4374 end, IT will be on the start of a screen line. */
4375
4376void
4377move_it_vertically (it, dy)
4378 struct it *it;
4379 int dy;
4380{
4381 if (dy <= 0)
4382 move_it_vertically_backward (it, -dy);
4383 else if (dy > 0)
4384 {
4385 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4386 MOVE_TO_POS | MOVE_TO_Y);
4387
4388 /* If buffer ends in ZV without a newline, move to the start of
4389 the line to satisfy the post-condition. */
4390 if (IT_CHARPOS (*it) == ZV
4391 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4392 move_it_by_lines (it, 0, 0);
4393 }
4394}
4395
4396
4397/* Return non-zero if some text between buffer positions START_CHARPOS
4398 and END_CHARPOS is invisible. IT->window is the window for text
4399 property lookup. */
4400
4401static int
4402invisible_text_between_p (it, start_charpos, end_charpos)
4403 struct it *it;
4404 int start_charpos, end_charpos;
4405{
4406#ifdef USE_TEXT_PROPERTIES
4407 Lisp_Object prop, limit;
4408 int invisible_found_p;
4409
4410 xassert (it != NULL && start_charpos <= end_charpos);
4411
4412 /* Is text at START invisible? */
4413 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4414 it->window);
4415 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4416 invisible_found_p = 1;
4417 else
4418 {
4419 limit = Fnext_single_property_change (make_number (start_charpos),
4420 Qinvisible,
4421 Fcurrent_buffer (),
4422 make_number (end_charpos));
4423 invisible_found_p = XFASTINT (limit) < end_charpos;
4424 }
4425
4426 return invisible_found_p;
4427
4428#else /* not USE_TEXT_PROPERTIES */
4429 return 0;
4430#endif /* not USE_TEXT_PROPERTIES */
4431}
4432
4433
4434/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4435 negative means move up. DVPOS == 0 means move to the start of the
4436 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4437 NEED_Y_P is zero, IT->current_y will be left unchanged.
4438
4439 Further optimization ideas: If we would know that IT->f doesn't use
4440 a face with proportional font, we could be faster for
4441 truncate-lines nil. */
4442
4443void
4444move_it_by_lines (it, dvpos, need_y_p)
4445 struct it *it;
4446 int dvpos, need_y_p;
4447{
4448 struct position pos;
4449
4450 if (!FRAME_WINDOW_P (it->f))
4451 {
4452 struct text_pos textpos;
4453
4454 /* We can use vmotion on frames without proportional fonts. */
4455 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4456 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4457 reseat (it, textpos, 1);
4458 it->vpos += pos.vpos;
4459 it->current_y += pos.vpos;
4460 }
4461 else if (dvpos == 0)
4462 {
4463 /* DVPOS == 0 means move to the start of the screen line. */
4464 move_it_vertically_backward (it, 0);
4465 xassert (it->current_x == 0 && it->hpos == 0);
4466 }
4467 else if (dvpos > 0)
4468 {
4469 /* If there are no continuation lines, and if there is no
4470 selective display, try the simple method of moving forward
4471 DVPOS newlines, then see where we are. */
4472 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4473 {
4474 int shortage = 0, charpos;
4475
4476 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4477 charpos = IT_CHARPOS (*it) + 1;
4478 else
4479 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4480 &shortage, 0);
4481
4482 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4483 {
4484 struct text_pos pos;
4485 CHARPOS (pos) = charpos;
4486 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4487 reseat (it, pos, 1);
4488 it->vpos += dvpos - shortage;
4489 it->hpos = it->current_x = 0;
4490 return;
4491 }
4492 }
4493
4494 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4495 }
4496 else
4497 {
4498 struct it it2;
4499 int start_charpos, i;
4500
4501 /* If there are no continuation lines, and if there is no
4502 selective display, try the simple method of moving backward
4503 -DVPOS newlines. */
4504 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4505 {
4506 int shortage;
4507 int charpos = IT_CHARPOS (*it);
4508 int bytepos = IT_BYTEPOS (*it);
4509
4510 /* If in the middle of a line, go to its start. */
4511 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4512 {
4513 charpos = find_next_newline_no_quit (charpos, -1);
4514 bytepos = CHAR_TO_BYTE (charpos);
4515 }
4516
4517 if (charpos == BEGV)
4518 {
4519 struct text_pos pos;
4520 CHARPOS (pos) = charpos;
4521 BYTEPOS (pos) = bytepos;
4522 reseat (it, pos, 1);
4523 it->hpos = it->current_x = 0;
4524 return;
4525 }
4526 else
4527 {
4528 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4529 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4530 {
4531 struct text_pos pos;
4532 CHARPOS (pos) = charpos;
4533 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4534 reseat (it, pos, 1);
4535 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4536 it->hpos = it->current_x = 0;
4537 return;
4538 }
4539 }
4540 }
4541
4542 /* Go back -DVPOS visible lines and reseat the iterator there. */
4543 start_charpos = IT_CHARPOS (*it);
4544 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4545 back_to_previous_visible_line_start (it);
4546 reseat (it, it->current.pos, 1);
4547 it->current_x = it->hpos = 0;
4548
4549 /* Above call may have moved too far if continuation lines
4550 are involved. Scan forward and see if it did. */
4551 it2 = *it;
4552 it2.vpos = it2.current_y = 0;
4553 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4554 it->vpos -= it2.vpos;
4555 it->current_y -= it2.current_y;
4556 it->current_x = it->hpos = 0;
4557
4558 /* If we moved too far, move IT some lines forward. */
4559 if (it2.vpos > -dvpos)
4560 {
4561 int delta = it2.vpos + dvpos;
4562 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4563 }
4564 }
4565}
4566
4567
4568\f
4569/***********************************************************************
4570 Messages
4571 ***********************************************************************/
4572
4573
4574/* Output a newline in the *Messages* buffer if "needs" one. */
4575
4576void
4577message_log_maybe_newline ()
4578{
4579 if (message_log_need_newline)
4580 message_dolog ("", 0, 1, 0);
4581}
4582
4583
4584/* Add a string M of length LEN to the message log, optionally
4585 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4586 nonzero, means interpret the contents of M as multibyte. This
4587 function calls low-level routines in order to bypass text property
4588 hooks, etc. which might not be safe to run. */
4589
4590void
4591message_dolog (m, len, nlflag, multibyte)
4592 char *m;
4593 int len, nlflag, multibyte;
4594{
4595 if (!NILP (Vmessage_log_max))
4596 {
4597 struct buffer *oldbuf;
4598 Lisp_Object oldpoint, oldbegv, oldzv;
4599 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4600 int point_at_end = 0;
4601 int zv_at_end = 0;
4602 Lisp_Object old_deactivate_mark, tem;
4603 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4604
4605 old_deactivate_mark = Vdeactivate_mark;
4606 oldbuf = current_buffer;
4607 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4608 current_buffer->undo_list = Qt;
4609
4610 oldpoint = Fpoint_marker ();
4611 oldbegv = Fpoint_min_marker ();
4612 oldzv = Fpoint_max_marker ();
4613 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4614
4615 if (PT == Z)
4616 point_at_end = 1;
4617 if (ZV == Z)
4618 zv_at_end = 1;
4619
4620 BEGV = BEG;
4621 BEGV_BYTE = BEG_BYTE;
4622 ZV = Z;
4623 ZV_BYTE = Z_BYTE;
4624 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4625
4626 /* Insert the string--maybe converting multibyte to single byte
4627 or vice versa, so that all the text fits the buffer. */
4628 if (multibyte
4629 && NILP (current_buffer->enable_multibyte_characters))
4630 {
4631 int i, c, nbytes;
4632 unsigned char work[1];
4633
4634 /* Convert a multibyte string to single-byte
4635 for the *Message* buffer. */
4636 for (i = 0; i < len; i += nbytes)
4637 {
4fdb80f2 4638 c = string_char_and_length (m + i, len - i, &nbytes);
5f5c8ee5
GM
4639 work[0] = (SINGLE_BYTE_CHAR_P (c)
4640 ? c
4641 : multibyte_char_to_unibyte (c, Qnil));
4642 insert_1_both (work, 1, 1, 1, 0, 0);
4643 }
4644 }
4645 else if (! multibyte
4646 && ! NILP (current_buffer->enable_multibyte_characters))
4647 {
4648 int i, c, nbytes;
4649 unsigned char *msg = (unsigned char *) m;
4650 unsigned char *str, work[4];
4651 /* Convert a single-byte string to multibyte
4652 for the *Message* buffer. */
4653 for (i = 0; i < len; i++)
4654 {
4655 c = unibyte_char_to_multibyte (msg[i]);
4656 nbytes = CHAR_STRING (c, work, str);
4657 insert_1_both (work, 1, nbytes, 1, 0, 0);
4658 }
4659 }
4660 else if (len)
4661 insert_1 (m, len, 1, 0, 0);
4662
4663 if (nlflag)
4664 {
4665 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4666 insert_1 ("\n", 1, 1, 0, 0);
4667
4668 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4669 this_bol = PT;
4670 this_bol_byte = PT_BYTE;
4671
4672 if (this_bol > BEG)
4673 {
4674 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4675 prev_bol = PT;
4676 prev_bol_byte = PT_BYTE;
4677
4678 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4679 this_bol, this_bol_byte);
4680 if (dup)
4681 {
4682 del_range_both (prev_bol, prev_bol_byte,
4683 this_bol, this_bol_byte, 0);
4684 if (dup > 1)
4685 {
4686 char dupstr[40];
4687 int duplen;
4688
4689 /* If you change this format, don't forget to also
4690 change message_log_check_duplicate. */
4691 sprintf (dupstr, " [%d times]", dup);
4692 duplen = strlen (dupstr);
4693 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4694 insert_1 (dupstr, duplen, 1, 0, 1);
4695 }
4696 }
4697 }
4698
4699 if (NATNUMP (Vmessage_log_max))
4700 {
4701 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4702 -XFASTINT (Vmessage_log_max) - 1, 0);
4703 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4704 }
4705 }
4706 BEGV = XMARKER (oldbegv)->charpos;
4707 BEGV_BYTE = marker_byte_position (oldbegv);
4708
4709 if (zv_at_end)
4710 {
4711 ZV = Z;
4712 ZV_BYTE = Z_BYTE;
4713 }
4714 else
4715 {
4716 ZV = XMARKER (oldzv)->charpos;
4717 ZV_BYTE = marker_byte_position (oldzv);
4718 }
4719
4720 if (point_at_end)
4721 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4722 else
4723 /* We can't do Fgoto_char (oldpoint) because it will run some
4724 Lisp code. */
4725 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4726 XMARKER (oldpoint)->bytepos);
4727
4728 UNGCPRO;
4729 free_marker (oldpoint);
4730 free_marker (oldbegv);
4731 free_marker (oldzv);
4732
4733 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4734 set_buffer_internal (oldbuf);
4735 if (NILP (tem))
4736 windows_or_buffers_changed = old_windows_or_buffers_changed;
4737 message_log_need_newline = !nlflag;
4738 Vdeactivate_mark = old_deactivate_mark;
4739 }
4740}
4741
4742
4743/* We are at the end of the buffer after just having inserted a newline.
4744 (Note: We depend on the fact we won't be crossing the gap.)
4745 Check to see if the most recent message looks a lot like the previous one.
4746 Return 0 if different, 1 if the new one should just replace it, or a
4747 value N > 1 if we should also append " [N times]". */
4748
4749static int
4750message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4751 int prev_bol, this_bol;
4752 int prev_bol_byte, this_bol_byte;
4753{
4754 int i;
4755 int len = Z_BYTE - 1 - this_bol_byte;
4756 int seen_dots = 0;
4757 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4758 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4759
4760 for (i = 0; i < len; i++)
4761 {
4762 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4763 && p1[i] != '\n')
4764 seen_dots = 1;
4765 if (p1[i] != p2[i])
4766 return seen_dots;
4767 }
4768 p1 += len;
4769 if (*p1 == '\n')
4770 return 2;
4771 if (*p1++ == ' ' && *p1++ == '[')
4772 {
4773 int n = 0;
4774 while (*p1 >= '0' && *p1 <= '9')
4775 n = n * 10 + *p1++ - '0';
4776 if (strncmp (p1, " times]\n", 8) == 0)
4777 return n+1;
4778 }
4779 return 0;
4780}
4781
4782
4783/* Display an echo area message M with a specified length of LEN
4784 chars. The string may include null characters. If M is 0, clear
4785 out any existing message, and let the mini-buffer text show through.
4786
4787 The buffer M must continue to exist until after the echo area gets
4788 cleared or some other message gets displayed there. This means do
4789 not pass text that is stored in a Lisp string; do not pass text in
4790 a buffer that was alloca'd. */
4791
4792void
4793message2 (m, len, multibyte)
4794 char *m;
4795 int len;
4796 int multibyte;
4797{
4798 /* First flush out any partial line written with print. */
4799 message_log_maybe_newline ();
4800 if (m)
4801 message_dolog (m, len, 1, multibyte);
4802 message2_nolog (m, len, multibyte);
4803}
4804
4805
4806/* The non-logging counterpart of message2. */
4807
4808void
4809message2_nolog (m, len, multibyte)
4810 char *m;
4811 int len;
4812{
4813 message_enable_multibyte = multibyte;
4814
4815 if (noninteractive)
4816 {
4817 if (noninteractive_need_newline)
4818 putc ('\n', stderr);
4819 noninteractive_need_newline = 0;
4820 if (m)
4821 fwrite (m, len, 1, stderr);
4822 if (cursor_in_echo_area == 0)
4823 fprintf (stderr, "\n");
4824 fflush (stderr);
4825 }
4826 /* A null message buffer means that the frame hasn't really been
4827 initialized yet. Error messages get reported properly by
4828 cmd_error, so this must be just an informative message; toss it. */
4829 else if (INTERACTIVE
4830 && selected_frame->glyphs_initialized_p
4831 && FRAME_MESSAGE_BUF (selected_frame))
4832 {
4833 Lisp_Object mini_window;
4834 struct frame *f;
4835
4836 /* Get the frame containing the mini-buffer
4837 that the selected frame is using. */
4838 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4839 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4840
4841 FRAME_SAMPLE_VISIBILITY (f);
4842 if (FRAME_VISIBLE_P (selected_frame)
4843 && ! FRAME_VISIBLE_P (f))
4844 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4845
4846 if (m)
4847 {
c6e89d6c 4848 set_message (m, Qnil, len, multibyte);
5f5c8ee5
GM
4849 if (minibuffer_auto_raise)
4850 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4851 }
4852 else
c6e89d6c 4853 clear_message (1, 1);
5f5c8ee5 4854
c6e89d6c 4855 do_pending_window_change (0);
5f5c8ee5 4856 echo_area_display (1);
c6e89d6c 4857 do_pending_window_change (0);
5f5c8ee5
GM
4858 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4859 (*frame_up_to_date_hook) (f);
4860 }
4861}
4862
4863
c6e89d6c
GM
4864/* Display an echo area message M with a specified length of NBYTES
4865 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
4866 string, clear out any existing message, and let the mini-buffer
4867 text show through. */
4868
4869void
c6e89d6c 4870message3 (m, nbytes, multibyte)
5f5c8ee5 4871 Lisp_Object m;
c6e89d6c 4872 int nbytes;
5f5c8ee5
GM
4873 int multibyte;
4874{
4875 struct gcpro gcpro1;
4876
4877 GCPRO1 (m);
4878
4879 /* First flush out any partial line written with print. */
4880 message_log_maybe_newline ();
4881 if (STRINGP (m))
c6e89d6c
GM
4882 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4883 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
4884
4885 UNGCPRO;
4886}
4887
4888
4889/* The non-logging version of message3. */
4890
4891void
c6e89d6c 4892message3_nolog (m, nbytes, multibyte)
5f5c8ee5 4893 Lisp_Object m;
c6e89d6c 4894 int nbytes, multibyte;
5f5c8ee5
GM
4895{
4896 message_enable_multibyte = multibyte;
4897
4898 if (noninteractive)
4899 {
4900 if (noninteractive_need_newline)
4901 putc ('\n', stderr);
4902 noninteractive_need_newline = 0;
4903 if (STRINGP (m))
c6e89d6c 4904 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
4905 if (cursor_in_echo_area == 0)
4906 fprintf (stderr, "\n");
4907 fflush (stderr);
4908 }
4909 /* A null message buffer means that the frame hasn't really been
4910 initialized yet. Error messages get reported properly by
4911 cmd_error, so this must be just an informative message; toss it. */
4912 else if (INTERACTIVE
4913 && selected_frame->glyphs_initialized_p
4914 && FRAME_MESSAGE_BUF (selected_frame))
4915 {
4916 Lisp_Object mini_window;
c6e89d6c 4917 Lisp_Object frame;
5f5c8ee5
GM
4918 struct frame *f;
4919
4920 /* Get the frame containing the mini-buffer
4921 that the selected frame is using. */
4922 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
c6e89d6c
GM
4923 frame = XWINDOW (mini_window)->frame;
4924 f = XFRAME (frame);
5f5c8ee5
GM
4925
4926 FRAME_SAMPLE_VISIBILITY (f);
4927 if (FRAME_VISIBLE_P (selected_frame)
c6e89d6c
GM
4928 && !FRAME_VISIBLE_P (f))
4929 Fmake_frame_visible (frame);
5f5c8ee5 4930
c6e89d6c 4931 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 4932 {
c6e89d6c 4933 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
4934 if (minibuffer_auto_raise)
4935 Fraise_frame (frame);
5f5c8ee5
GM
4936 }
4937 else
c6e89d6c 4938 clear_message (1, 1);
5f5c8ee5 4939
c6e89d6c 4940 do_pending_window_change (0);
5f5c8ee5 4941 echo_area_display (1);
c6e89d6c 4942 do_pending_window_change (0);
5f5c8ee5
GM
4943 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4944 (*frame_up_to_date_hook) (f);
4945 }
4946}
4947
4948
4949/* Display a null-terminated echo area message M. If M is 0, clear
4950 out any existing message, and let the mini-buffer text show through.
4951
4952 The buffer M must continue to exist until after the echo area gets
4953 cleared or some other message gets displayed there. Do not pass
4954 text that is stored in a Lisp string. Do not pass text in a buffer
4955 that was alloca'd. */
4956
4957void
4958message1 (m)
4959 char *m;
4960{
4961 message2 (m, (m ? strlen (m) : 0), 0);
4962}
4963
4964
4965/* The non-logging counterpart of message1. */
4966
4967void
4968message1_nolog (m)
4969 char *m;
4970{
4971 message2_nolog (m, (m ? strlen (m) : 0), 0);
4972}
4973
4974/* Display a message M which contains a single %s
4975 which gets replaced with STRING. */
4976
4977void
4978message_with_string (m, string, log)
4979 char *m;
4980 Lisp_Object string;
4981 int log;
4982{
4983 if (noninteractive)
4984 {
4985 if (m)
4986 {
4987 if (noninteractive_need_newline)
4988 putc ('\n', stderr);
4989 noninteractive_need_newline = 0;
4990 fprintf (stderr, m, XSTRING (string)->data);
4991 if (cursor_in_echo_area == 0)
4992 fprintf (stderr, "\n");
4993 fflush (stderr);
4994 }
4995 }
4996 else if (INTERACTIVE)
4997 {
4998 /* The frame whose minibuffer we're going to display the message on.
4999 It may be larger than the selected frame, so we need
5000 to use its buffer, not the selected frame's buffer. */
5001 Lisp_Object mini_window;
5002 FRAME_PTR f;
5003
5004 /* Get the frame containing the minibuffer
5005 that the selected frame is using. */
5006 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5007 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5008
5009 /* A null message buffer means that the frame hasn't really been
5010 initialized yet. Error messages get reported properly by
5011 cmd_error, so this must be just an informative message; toss it. */
5012 if (FRAME_MESSAGE_BUF (f))
5013 {
5014 int len;
5015 char *a[1];
5016 a[0] = (char *) XSTRING (string)->data;
5017
5018 len = doprnt (FRAME_MESSAGE_BUF (f),
5019 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5020
5021 if (log)
5022 message2 (FRAME_MESSAGE_BUF (f), len,
5023 STRING_MULTIBYTE (string));
5024 else
5025 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5026 STRING_MULTIBYTE (string));
5027
5028 /* Print should start at the beginning of the message
5029 buffer next time. */
5030 message_buf_print = 0;
5031 }
5032 }
5033}
5034
5035
5f5c8ee5
GM
5036/* Dump an informative message to the minibuf. If M is 0, clear out
5037 any existing message, and let the mini-buffer text show through. */
5038
5039/* VARARGS 1 */
5040void
5041message (m, a1, a2, a3)
5042 char *m;
5043 EMACS_INT a1, a2, a3;
5044{
5045 if (noninteractive)
5046 {
5047 if (m)
5048 {
5049 if (noninteractive_need_newline)
5050 putc ('\n', stderr);
5051 noninteractive_need_newline = 0;
5052 fprintf (stderr, m, a1, a2, a3);
5053 if (cursor_in_echo_area == 0)
5054 fprintf (stderr, "\n");
5055 fflush (stderr);
5056 }
5057 }
5058 else if (INTERACTIVE)
5059 {
5060 /* The frame whose mini-buffer we're going to display the message
5061 on. It may be larger than the selected frame, so we need to
5062 use its buffer, not the selected frame's buffer. */
5063 Lisp_Object mini_window;
5064 struct frame *f;
5065
5066 /* Get the frame containing the mini-buffer
5067 that the selected frame is using. */
5068 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5069 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5070
5071 /* A null message buffer means that the frame hasn't really been
5072 initialized yet. Error messages get reported properly by
5073 cmd_error, so this must be just an informative message; toss
5074 it. */
5075 if (FRAME_MESSAGE_BUF (f))
5076 {
5077 if (m)
5078 {
5079 int len;
5080#ifdef NO_ARG_ARRAY
5081 char *a[3];
5082 a[0] = (char *) a1;
5083 a[1] = (char *) a2;
5084 a[2] = (char *) a3;
5085
5086 len = doprnt (FRAME_MESSAGE_BUF (f),
5087 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5088#else
5089 len = doprnt (FRAME_MESSAGE_BUF (f),
5090 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5091 (char **) &a1);
5092#endif /* NO_ARG_ARRAY */
5093
5094 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5095 }
5096 else
5097 message1 (0);
5098
5099 /* Print should start at the beginning of the message
5100 buffer next time. */
5101 message_buf_print = 0;
5102 }
5103 }
5104}
5105
5106
5107/* The non-logging version of message. */
5108
5109void
5110message_nolog (m, a1, a2, a3)
5111 char *m;
5112 EMACS_INT a1, a2, a3;
5113{
5114 Lisp_Object old_log_max;
5115 old_log_max = Vmessage_log_max;
5116 Vmessage_log_max = Qnil;
5117 message (m, a1, a2, a3);
5118 Vmessage_log_max = old_log_max;
5119}
5120
5121
c6e89d6c
GM
5122/* Display the current message in the current mini-buffer. This is
5123 only called from error handlers in process.c, and is not time
5124 critical. */
5f5c8ee5
GM
5125
5126void
5127update_echo_area ()
5128{
c6e89d6c
GM
5129 if (!NILP (echo_area_buffer[0]))
5130 {
5131 Lisp_Object string;
5132 string = Fcurrent_message ();
5133 message3 (string, XSTRING (string)->size,
5134 !NILP (current_buffer->enable_multibyte_characters));
5135 }
5136}
5137
5138
5139/* Call FN with args A1..A5 with either the current or last displayed
5140 echo_area_buffer as current buffer.
5141
5142 WHICH zero means use the current message buffer
5143 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5144 from echo_buffer[] and clear it.
5145
5146 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5147 suitable buffer from echo_buffer[] and clear it.
5148
5149 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5150 that the current message becomes the last displayed one, make
5151 choose a suitable buffer for echo_area_buffer[0], and clear it.
5152
5153 Value is what FN returns. */
5154
5155static int
5156with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5157 struct window *w;
5158 int which;
5159 int (*fn) ();
5160 int a1, a2, a3, a4, a5;
5161{
5162 Lisp_Object buffer;
5163 int i, this_one, the_other, clear_buffer_p, rc;
5164 int count = specpdl_ptr - specpdl;
5165
5166 /* If buffers aren't life, make new ones. */
5167 for (i = 0; i < 2; ++i)
5168 if (!BUFFERP (echo_buffer[i])
5169 || NILP (XBUFFER (echo_buffer[i])->name))
5170 {
5171 char name[30];
5172 sprintf (name, " *Echo Area %d*", i);
5173 echo_buffer[i] = Fget_buffer_create (build_string (name));
5174 }
5175
5176 clear_buffer_p = 0;
5177
5178 if (which == 0)
5179 this_one = 0, the_other = 1;
5180 else if (which > 0)
5181 this_one = 1, the_other = 0;
5f5c8ee5 5182 else
c6e89d6c
GM
5183 {
5184 this_one = 0, the_other = 1;
5185 clear_buffer_p = 1;
5186
5187 /* We need a fresh one in case the current echo buffer equals
5188 the one containing the last displayed echo area message. */
5189 if (!NILP (echo_area_buffer[this_one])
5190 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5191 echo_area_buffer[this_one] = Qnil;
5192
5193 }
5194
5195 /* Choose a suitable buffer from echo_buffer[] is we don't
5196 have one. */
5197 if (NILP (echo_area_buffer[this_one]))
5198 {
5199 echo_area_buffer[this_one]
5200 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5201 ? echo_buffer[the_other]
5202 : echo_buffer[this_one]);
5203 clear_buffer_p = 1;
5204 }
5205
5206 buffer = echo_area_buffer[this_one];
5207
5208 record_unwind_protect (unwind_with_echo_area_buffer,
5209 with_echo_area_buffer_unwind_data (w));
5210
5211 /* Make the echo area buffer current. Note that for display
5212 purposes, it is not necessary that the displayed window's buffer
5213 == current_buffer, except for text property lookup. So, let's
5214 only set that buffer temporarily here without doing a full
5215 Fset_window_buffer. We must also change w->pointm, though,
5216 because otherwise an assertions in unshow_buffer fails, and Emacs
5217 aborts. */
9142dd5b 5218 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5219 if (w)
5220 {
5221 w->buffer = buffer;
5222 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5223 }
5224 current_buffer->truncate_lines = Qnil;
5225 current_buffer->undo_list = Qt;
5226 current_buffer->read_only = Qnil;
5227
5228 if (clear_buffer_p && Z > BEG)
5229 del_range (BEG, Z);
5230
5231 xassert (BEGV >= BEG);
5232 xassert (ZV <= Z && ZV >= BEGV);
5233
5234 rc = fn (a1, a2, a3, a4, a5);
5235
5236 xassert (BEGV >= BEG);
5237 xassert (ZV <= Z && ZV >= BEGV);
5238
5239 unbind_to (count, Qnil);
5240 return rc;
5f5c8ee5
GM
5241}
5242
5243
c6e89d6c
GM
5244/* Save state that should be preserved around the call to the function
5245 FN called in with_echo_area_buffer. */
5f5c8ee5 5246
c6e89d6c
GM
5247static Lisp_Object
5248with_echo_area_buffer_unwind_data (w)
5249 struct window *w;
5f5c8ee5 5250{
c6e89d6c
GM
5251 int i = 0;
5252 Lisp_Object vector;
5f5c8ee5 5253
c6e89d6c
GM
5254 /* Reduce consing by keeping one vector in
5255 Vwith_echo_area_save_vector. */
5256 vector = Vwith_echo_area_save_vector;
5257 Vwith_echo_area_save_vector = Qnil;
5258
5259 if (NILP (vector))
9142dd5b 5260 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5261
5262 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5263 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5264 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5265
5266 if (w)
5267 {
5268 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5269 XVECTOR (vector)->contents[i++] = w->buffer;
5270 XVECTOR (vector)->contents[i++]
5271 = make_number (XMARKER (w->pointm)->charpos);
5272 XVECTOR (vector)->contents[i++]
5273 = make_number (XMARKER (w->pointm)->bytepos);
5274 }
5275 else
5276 {
5277 int end = i + 4;
5278 while (i < end)
5279 XVECTOR (vector)->contents[i++] = Qnil;
5280 }
5f5c8ee5 5281
c6e89d6c
GM
5282 xassert (i == XVECTOR (vector)->size);
5283 return vector;
5284}
5f5c8ee5 5285
5f5c8ee5 5286
c6e89d6c
GM
5287/* Restore global state from VECTOR which was created by
5288 with_echo_area_buffer_unwind_data. */
5289
5290static Lisp_Object
5291unwind_with_echo_area_buffer (vector)
5292 Lisp_Object vector;
5293{
5294 int i = 0;
5295
9142dd5b 5296 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
c6e89d6c
GM
5297 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5298 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
c6e89d6c
GM
5299
5300 if (WINDOWP (XVECTOR (vector)->contents[i]))
5301 {
5302 struct window *w;
5303 Lisp_Object buffer, charpos, bytepos;
5304
5305 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5306 buffer = XVECTOR (vector)->contents[i]; ++i;
5307 charpos = XVECTOR (vector)->contents[i]; ++i;
5308 bytepos = XVECTOR (vector)->contents[i]; ++i;
5309
5310 w->buffer = buffer;
5311 set_marker_both (w->pointm, buffer,
5312 XFASTINT (charpos), XFASTINT (bytepos));
5313 }
5314
5315 Vwith_echo_area_save_vector = vector;
5316 return Qnil;
5317}
5318
5319
5320/* Set up the echo area for use by print functions. MULTIBYTE_P
5321 non-zero means we will print multibyte. */
5322
5323void
5324setup_echo_area_for_printing (multibyte_p)
5325 int multibyte_p;
5326{
5327 if (!message_buf_print)
5328 {
5329 /* A message has been output since the last time we printed.
5330 Choose a fresh echo area buffer. */
5331 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5332 echo_area_buffer[0] = echo_buffer[1];
5333 else
5334 echo_area_buffer[0] = echo_buffer[0];
5335
5336 /* Switch to that buffer and clear it. */
5337 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5338 if (Z > BEG)
5339 del_range (BEG, Z);
5340 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5341
5342 /* Set up the buffer for the multibyteness we need. */
5343 if (multibyte_p
5344 != !NILP (current_buffer->enable_multibyte_characters))
5345 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5346
5347 /* Raise the frame containing the echo area. */
5348 if (minibuffer_auto_raise)
5349 {
5350 Lisp_Object mini_window;
5351 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5352 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5353 }
5354
5355 message_buf_print = 1;
5356 }
5357 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5358 /* Someone switched buffers between print requests. */
5359 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5360}
5361
5362
dd2eb166
GM
5363/* Display an echo area message in window W. Value is non-zero if W's
5364 height is changed. If display_last_displayed_message_p is
5365 non-zero, display the message that was last displayed, otherwise
5366 display the current message. */
c6e89d6c
GM
5367
5368static int
5369display_echo_area (w)
5370 struct window *w;
5371{
dd2eb166
GM
5372 int i, no_message_p, window_height_changed_p;
5373
5374 /* If there is no message, we must call display_echo_area_1
5375 nevertheless because it resizes the window. But we will have to
5376 reset the echo_area_buffer in question to nil at the end because
5377 with_echo_area_buffer will sets it to an empty buffer. */
5378 i = display_last_displayed_message_p ? 1 : 0;
5379 no_message_p = NILP (echo_area_buffer[i]);
5380
5381 window_height_changed_p
5382 = with_echo_area_buffer (w, display_last_displayed_message_p,
5383 (int (*) ()) display_echo_area_1, w);
5384
5385 if (no_message_p)
5386 echo_area_buffer[i] = Qnil;
5387
5388 return window_height_changed_p;
c6e89d6c
GM
5389}
5390
5391
5392/* Helper for display_echo_area. Display the current buffer which
5393 contains the current echo area message in window W, a mini-window.
5394 Change the height of W so that all of the message is displayed.
5395 Value is non-zero if height of W was changed. */
5396
5397static int
5398display_echo_area_1 (w)
5399 struct window *w;
5400{
5401 Lisp_Object window;
5402 struct frame *f = XFRAME (w->frame);
5403 struct text_pos start;
5404 int window_height_changed_p = 0;
5405
5406 /* Do this before displaying, so that we have a large enough glyph
5407 matrix for the display. */
5408 window_height_changed_p = resize_mini_window (w);
5409
5410 /* Display. */
5411 clear_glyph_matrix (w->desired_matrix);
5412 XSETWINDOW (window, w);
5413 SET_TEXT_POS (start, BEG, BEG_BYTE);
5414 try_window (window, start);
5415
c6e89d6c
GM
5416 return window_height_changed_p;
5417}
5418
5419
5420/* Resize mini-window W to fit the size of its contents. Value is
5421 non-zero if the window height has been changed. */
5422
5423static int
5424resize_mini_window (w)
5425 struct window *w;
5426{
5427 struct frame *f = XFRAME (w->frame);
5428 int window_height_changed_p = 0;
5429
5430 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
5431
5432 /* Nil means don't try to resize. */
5433 if (NILP (Vmax_mini_window_height))
5434 return 0;
c6e89d6c
GM
5435
5436 if (!FRAME_MINIBUF_ONLY_P (f))
5437 {
5438 struct it it;
dd2eb166
GM
5439 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5440 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5441 int height, max_height;
5442 int unit = CANON_Y_UNIT (f);
5443 struct text_pos start;
9142dd5b 5444
c6e89d6c 5445 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 5446
dd2eb166
GM
5447 /* Compute the max. number of lines specified by the user. */
5448 if (FLOATP (Vmax_mini_window_height))
5449 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5450 else if (INTEGERP (Vmax_mini_window_height))
5451 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
5452 else
5453 max_height = total_height / 4;
dd2eb166
GM
5454
5455 /* Correct that max. height if it's bogus. */
5456 max_height = max (1, max_height);
5457 max_height = min (total_height, max_height);
5458
5459 /* Find out the height of the text in the window. */
5460 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5461 height = (unit - 1 + it.current_y + last_height) / unit;
5462 height = max (1, height);
5463
5464 /* Compute a suitable window start. */
5465 if (height > max_height)
5466 {
5467 height = max_height;
5468 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5469 move_it_vertically_backward (&it, (height - 1) * unit);
5470 start = it.current.pos;
5471 }
5472 else
5473 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5474 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a
GM
5475
5476 if (NILP (Vresize_mini_config))
dd2eb166 5477 {
c59c668a
GM
5478 if (height != XFASTINT (w->height))
5479 {
5480 Lisp_Object old_selected_window;
5481 Lisp_Object config;
5482 struct gcpro gcpro1;
5483
5484 resize_mini_initial_height = XFASTINT (w->height);
5485 config = Fcurrent_window_configuration (Qnil);
5486 GCPRO1 (config);
5487
5488 old_selected_window = selected_window;
5489 XSETWINDOW (selected_window, w);
5490 change_window_height (height - XFASTINT (w->height), 0);
5491 selected_window = old_selected_window;
5492
5493 window_height_changed_p = 1;
9142dd5b 5494
c59c668a
GM
5495 /* Set this after changing window sizes, or else
5496 Vresize_mini_config would be reset in
5497 adjust_frame_glyphs. */
5498 resize_mini_frame = XFRAME (w->frame);
5499 Vresize_mini_config = config;
5500 UNGCPRO;
5501 }
c6e89d6c 5502 }
c59c668a 5503 else if (height != XFASTINT (w->height))
9142dd5b 5504 {
c59c668a
GM
5505 if (height == resize_mini_initial_height)
5506 {
5507 Fset_window_configuration (Vresize_mini_config);
5508 Vresize_mini_config = Qnil;
5509 resize_mini_frame = NULL;
5510 }
5511 else
5512 {
5513 Lisp_Object old_selected_window;
5514 old_selected_window = selected_window;
5515 XSETWINDOW (selected_window, w);
5516 change_window_height (height - XFASTINT (w->height), 0);
5517 selected_window = old_selected_window;
5518 }
5519
9142dd5b
GM
5520 window_height_changed_p = 1;
5521 }
c6e89d6c
GM
5522 }
5523
5524 return window_height_changed_p;
5525}
5526
5527
5528/* Value is the current message, a string, or nil if there is no
5529 current message. */
5530
5531Lisp_Object
5532current_message ()
5533{
5534 Lisp_Object msg;
5535
5536 if (NILP (echo_area_buffer[0]))
5537 msg = Qnil;
5538 else
5539 {
5540 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5541 if (NILP (msg))
5542 echo_area_buffer[0] = Qnil;
5543 }
5544
5545 return msg;
5546}
5547
5548
5549static int
5550current_message_1 (msg)
5551 Lisp_Object *msg;
5552{
5553 if (Z > BEG)
5554 *msg = make_buffer_string (BEG, Z, 1);
5555 else
5556 *msg = Qnil;
5557 return 0;
5558}
5559
5560
5561/* Push the current message on Vmessage_stack for later restauration
5562 by restore_message. Value is non-zero if the current message isn't
5563 empty. This is a relatively infrequent operation, so it's not
5564 worth optimizing. */
5565
5566int
5567push_message ()
5568{
5569 Lisp_Object msg;
5570 msg = current_message ();
5571 Vmessage_stack = Fcons (msg, Vmessage_stack);
5572 return STRINGP (msg);
5573}
5574
5575
5576/* Restore message display from the top of Vmessage_stack. */
5577
5578void
5579restore_message ()
5580{
5581 Lisp_Object msg;
5582
5583 xassert (CONSP (Vmessage_stack));
5584 msg = XCAR (Vmessage_stack);
5585 if (STRINGP (msg))
5586 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5587 else
5588 message3_nolog (msg, 0, 0);
5589}
5590
5591
5592/* Pop the top-most entry off Vmessage_stack. */
5593
5594void
5595pop_message ()
5596{
5597 xassert (CONSP (Vmessage_stack));
5598 Vmessage_stack = XCDR (Vmessage_stack);
5599}
5600
5601
5602/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5603 exits. If the stack is not empty, we have a missing pop_message
5604 somewhere. */
5605
5606void
5607check_message_stack ()
5608{
5609 if (!NILP (Vmessage_stack))
5610 abort ();
5611}
5612
5613
5614/* Truncate to NCHARS what will be displayed in the echo area the next
5615 time we display it---but don't redisplay it now. */
5616
5617void
5618truncate_echo_area (nchars)
5619 int nchars;
5620{
5621 if (nchars == 0)
5622 echo_area_buffer[0] = Qnil;
5623 /* A null message buffer means that the frame hasn't really been
5624 initialized yet. Error messages get reported properly by
5625 cmd_error, so this must be just an informative message; toss it. */
5626 else if (!noninteractive
5627 && INTERACTIVE
5628 && FRAME_MESSAGE_BUF (selected_frame)
5629 && !NILP (echo_area_buffer[0]))
5630 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5631}
5632
5633
5634/* Helper function for truncate_echo_area. Truncate the current
5635 message to at most NCHARS characters. */
5636
5637static int
5638truncate_message_1 (nchars)
5639 int nchars;
5640{
5641 if (BEG + nchars < Z)
5642 del_range (BEG + nchars, Z);
5643 if (Z == BEG)
5644 echo_area_buffer[0] = Qnil;
5645 return 0;
5646}
5647
5648
5649/* Set the current message to a substring of S or STRING.
5650
5651 If STRING is a Lisp string, set the message to the first NBYTES
5652 bytes from STRING. NBYTES zero means use the whole string. If
5653 STRING is multibyte, the message will be displayed multibyte.
5654
5655 If S is not null, set the message to the first LEN bytes of S. LEN
5656 zero means use the whole string. MULTIBYTE_P non-zero means S is
5657 multibyte. Display the message multibyte in that case. */
5658
5659void
5660set_message (s, string, nbytes, multibyte_p)
5661 char *s;
5662 Lisp_Object string;
5663 int nbytes;
5664{
5665 message_enable_multibyte
5666 = ((s && multibyte_p)
5667 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5668
5669 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5670 s, string, nbytes, multibyte_p);
5671 message_buf_print = 0;
5672}
5673
5674
5675/* Helper function for set_message. Arguments have the same meaning
5676 as there. This function is called with the echo area buffer being
5677 current. */
5678
5679static int
5680set_message_1 (s, string, nbytes, multibyte_p)
5681 char *s;
5682 Lisp_Object string;
5683 int nbytes, multibyte_p;
5684{
5685 xassert (BEG == Z);
5686
5687 /* Change multibyteness of the echo buffer appropriately. */
5688 if (message_enable_multibyte
5689 != !NILP (current_buffer->enable_multibyte_characters))
5690 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5691
5692 /* Insert new message at BEG. */
5693 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5694
5695 if (STRINGP (string))
5696 {
5697 int nchars;
5698
5699 if (nbytes == 0)
5700 nbytes = XSTRING (string)->size_byte;
5701 nchars = string_byte_to_char (string, nbytes);
5702
5703 /* This function takes care of single/multibyte conversion. We
5704 just have to ensure that the echo area buffer has the right
5705 setting of enable_multibyte_characters. */
5706 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5707 }
5708 else if (s)
5709 {
5710 if (nbytes == 0)
5711 nbytes = strlen (s);
5712
5713 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5714 {
5715 /* Convert from multi-byte to single-byte. */
5716 int i, c, n;
5717 unsigned char work[1];
5718
5719 /* Convert a multibyte string to single-byte. */
5720 for (i = 0; i < nbytes; i += n)
5721 {
5722 c = string_char_and_length (s + i, nbytes - i, &n);
5723 work[0] = (SINGLE_BYTE_CHAR_P (c)
5724 ? c
5725 : multibyte_char_to_unibyte (c, Qnil));
5726 insert_1_both (work, 1, 1, 1, 0, 0);
5727 }
5728 }
5729 else if (!multibyte_p
5730 && !NILP (current_buffer->enable_multibyte_characters))
5731 {
5732 /* Convert from single-byte to multi-byte. */
5733 int i, c, n;
5734 unsigned char *msg = (unsigned char *) s;
5735 unsigned char *str, work[4];
5736
5737 /* Convert a single-byte string to multibyte. */
5738 for (i = 0; i < nbytes; i++)
5739 {
5740 c = unibyte_char_to_multibyte (msg[i]);
5741 n = CHAR_STRING (c, work, str);
5742 insert_1_both (work, 1, n, 1, 0, 0);
5743 }
5744 }
5745 else
5746 insert_1 (s, nbytes, 1, 0, 0);
5747 }
5748
5749 return 0;
5750}
5751
5752
5753/* Clear messages. CURRENT_P non-zero means clear the current
5754 message. LAST_DISPLAYED_P non-zero means clear the message
5755 last displayed. */
5756
5757void
5758clear_message (current_p, last_displayed_p)
5759 int current_p, last_displayed_p;
5760{
5761 if (current_p)
5762 echo_area_buffer[0] = Qnil;
5763
5764 if (last_displayed_p)
5765 echo_area_buffer[1] = Qnil;
5766
5767 message_buf_print = 0;
5768}
5769
5770/* Clear garbaged frames.
5771
5772 This function is used where the old redisplay called
5773 redraw_garbaged_frames which in turn called redraw_frame which in
5774 turn called clear_frame. The call to clear_frame was a source of
5775 flickering. I believe a clear_frame is not necessary. It should
5776 suffice in the new redisplay to invalidate all current matrices,
5777 and ensure a complete redisplay of all windows. */
5778
5779static void
5780clear_garbaged_frames ()
5781{
5f5c8ee5
GM
5782 if (frame_garbaged)
5783 {
5f5c8ee5
GM
5784 Lisp_Object tail, frame;
5785
5786 FOR_EACH_FRAME (tail, frame)
5787 {
5788 struct frame *f = XFRAME (frame);
5789
5790 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5791 {
5792 clear_current_matrices (f);
5793 f->garbaged = 0;
5794 }
5795 }
5796
5797 frame_garbaged = 0;
5798 ++windows_or_buffers_changed;
5799 }
c6e89d6c 5800}
5f5c8ee5 5801
5f5c8ee5 5802
c6e89d6c
GM
5803/* Redisplay the echo area of selected_frame. If UPDATE_FRAME_P is
5804 non-zero update selected_frame. Value is non-zero if the
5805 mini-windows height has been changed. */
5f5c8ee5 5806
c6e89d6c
GM
5807static int
5808echo_area_display (update_frame_p)
5809 int update_frame_p;
5810{
5811 Lisp_Object mini_window;
5812 struct window *w;
5813 struct frame *f;
5814 int window_height_changed_p = 0;
5815
5816 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5817 w = XWINDOW (mini_window);
5818 f = XFRAME (WINDOW_FRAME (w));
5819
5820 /* Don't display if frame is invisible or not yet initialized. */
5821 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5822 return 0;
5f5c8ee5 5823
c6e89d6c
GM
5824 /* When Emacs starts, selected_frame may be a visible terminal
5825 frame, even if we run under a window system. If we let this
5826 through, a message would be displayed on the terminal. */
5827#ifdef HAVE_WINDOW_SYSTEM
5828 if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
5829 return 0;
5830#endif /* HAVE_WINDOW_SYSTEM */
5831
5832 /* Redraw garbaged frames. */
5833 if (frame_garbaged)
5834 clear_garbaged_frames ();
5835
5836 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5837 {
5838 echo_area_window = mini_window;
5839 window_height_changed_p = display_echo_area (w);
5f5c8ee5 5840 w->must_be_updated_p = 1;
c59c668a 5841
5f5c8ee5
GM
5842 if (update_frame_p)
5843 {
c59c668a
GM
5844 /* Not called from redisplay_internal. If we changed
5845 window configuration, we must redisplay thoroughly.
5846 Otherwise, we can do with updating what we displayed
5847 above. */
5848 if (window_height_changed_p)
5849 {
5850 ++windows_or_buffers_changed;
5851 ++update_mode_lines;
5852 redisplay_internal (0);
5853 }
5854 else if (FRAME_WINDOW_P (f))
5f5c8ee5
GM
5855 {
5856 update_single_window (w, 1);
5857 rif->flush_display (f);
5858 }
5859 else
5860 update_frame (f, 1, 1);
5861 }
5862 }
5863 else if (!EQ (mini_window, selected_window))
5864 windows_or_buffers_changed++;
c59c668a
GM
5865
5866 /* Last displayed message is now the current message. */
dd2eb166
GM
5867 echo_area_buffer[1] = echo_area_buffer[0];
5868
5f5c8ee5
GM
5869 /* Prevent redisplay optimization in redisplay_internal by resetting
5870 this_line_start_pos. This is done because the mini-buffer now
5871 displays the message instead of its buffer text. */
5872 if (EQ (mini_window, selected_window))
5873 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
5874
5875 return window_height_changed_p;
5f5c8ee5
GM
5876}
5877
5878
5879\f
5880/***********************************************************************
5881 Frame Titles
5882 ***********************************************************************/
5883
5884
5885#ifdef HAVE_WINDOW_SYSTEM
5886
5887/* A buffer for constructing frame titles in it; allocated from the
5888 heap in init_xdisp and resized as needed in store_frame_title_char. */
5889
5890static char *frame_title_buf;
5891
5892/* The buffer's end, and a current output position in it. */
5893
5894static char *frame_title_buf_end;
5895static char *frame_title_ptr;
5896
5897
5898/* Store a single character C for the frame title in frame_title_buf.
5899 Re-allocate frame_title_buf if necessary. */
5900
5901static void
5902store_frame_title_char (c)
5903 char c;
5904{
5905 /* If output position has reached the end of the allocated buffer,
5906 double the buffer's size. */
5907 if (frame_title_ptr == frame_title_buf_end)
5908 {
5909 int len = frame_title_ptr - frame_title_buf;
5910 int new_size = 2 * len * sizeof *frame_title_buf;
5911 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5912 frame_title_buf_end = frame_title_buf + new_size;
5913 frame_title_ptr = frame_title_buf + len;
5914 }
5915
5916 *frame_title_ptr++ = c;
5917}
5918
5919
5920/* Store part of a frame title in frame_title_buf, beginning at
5921 frame_title_ptr. STR is the string to store. Do not copy more
5922 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5923 the whole string. Pad with spaces until FIELD_WIDTH number of
5924 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5925 Called from display_mode_element when it is used to build a frame
5926 title. */
5927
5928static int
5929store_frame_title (str, field_width, precision)
5930 unsigned char *str;
5931 int field_width, precision;
5932{
5933 int n = 0;
5934
5935 /* Copy at most PRECISION chars from STR. */
5936 while ((precision <= 0 || n < precision)
5937 && *str)
5938 {
5939 store_frame_title_char (*str++);
5940 ++n;
5941 }
5942
5943 /* Fill up with spaces until FIELD_WIDTH reached. */
5944 while (field_width > 0
5945 && n < field_width)
5946 {
5947 store_frame_title_char (' ');
5948 ++n;
5949 }
5950
5951 return n;
5952}
5953
5954
5955/* Set the title of FRAME, if it has changed. The title format is
5956 Vicon_title_format if FRAME is iconified, otherwise it is
5957 frame_title_format. */
5958
5959static void
5960x_consider_frame_title (frame)
5961 Lisp_Object frame;
5962{
5963 struct frame *f = XFRAME (frame);
5964
5965 if (FRAME_WINDOW_P (f)
5966 || FRAME_MINIBUF_ONLY_P (f)
5967 || f->explicit_name)
5968 {
5969 /* Do we have more than one visible frame on this X display? */
5970 Lisp_Object tail;
5971 Lisp_Object fmt;
5972 struct buffer *obuf;
5973 int len;
5974 struct it it;
5975
5976 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
5977 {
5978 struct frame *tf = XFRAME (XCONS (tail)->car);
5979
5980 if (tf != f
5981 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
5982 && !FRAME_MINIBUF_ONLY_P (tf)
5983 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
5984 break;
5985 }
5986
5987 /* Set global variable indicating that multiple frames exist. */
5988 multiple_frames = CONSP (tail);
5989
5990 /* Switch to the buffer of selected window of the frame. Set up
5991 frame_title_ptr so that display_mode_element will output into it;
5992 then display the title. */
5993 obuf = current_buffer;
5994 Fset_buffer (XWINDOW (f->selected_window)->buffer);
5995 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
5996 frame_title_ptr = frame_title_buf;
5997 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
5998 NULL, DEFAULT_FACE_ID);
5999 len = display_mode_element (&it, 0, -1, -1, fmt);
6000 frame_title_ptr = NULL;
6001 set_buffer_internal (obuf);
6002
6003 /* Set the title only if it's changed. This avoids consing in
6004 the common case where it hasn't. (If it turns out that we've
6005 already wasted too much time by walking through the list with
6006 display_mode_element, then we might need to optimize at a
6007 higher level than this.) */
6008 if (! STRINGP (f->name)
6009 || STRING_BYTES (XSTRING (f->name)) != len
6010 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6011 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6012 }
6013}
6014
6015#else /* not HAVE_WINDOW_SYSTEM */
6016
6017#define frame_title_ptr ((char *)0)
6018#define store_frame_title(str, mincol, maxcol) 0
6019
6020#endif /* not HAVE_WINDOW_SYSTEM */
6021
6022
6023
6024\f
6025/***********************************************************************
6026 Menu Bars
6027 ***********************************************************************/
6028
6029
6030/* Prepare for redisplay by updating menu-bar item lists when
6031 appropriate. This can call eval. */
6032
6033void
6034prepare_menu_bars ()
6035{
6036 int all_windows;
6037 struct gcpro gcpro1, gcpro2;
6038 struct frame *f;
6039 struct frame *tooltip_frame;
6040
6041#ifdef HAVE_X_WINDOWS
6042 tooltip_frame = tip_frame;
6043#else
6044 tooltip_frame = NULL;
6045#endif
6046
6047 /* Update all frame titles based on their buffer names, etc. We do
6048 this before the menu bars so that the buffer-menu will show the
6049 up-to-date frame titles. */
6050#ifdef HAVE_WINDOW_SYSTEM
6051 if (windows_or_buffers_changed || update_mode_lines)
6052 {
6053 Lisp_Object tail, frame;
6054
6055 FOR_EACH_FRAME (tail, frame)
6056 {
6057 f = XFRAME (frame);
6058 if (f != tooltip_frame
6059 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6060 x_consider_frame_title (frame);
6061 }
6062 }
6063#endif /* HAVE_WINDOW_SYSTEM */
6064
6065 /* Update the menu bar item lists, if appropriate. This has to be
6066 done before any actual redisplay or generation of display lines. */
6067 all_windows = (update_mode_lines
6068 || buffer_shared > 1
6069 || windows_or_buffers_changed);
6070 if (all_windows)
6071 {
6072 Lisp_Object tail, frame;
6073 int count = specpdl_ptr - specpdl;
6074
6075 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6076
6077 FOR_EACH_FRAME (tail, frame)
6078 {
6079 f = XFRAME (frame);
6080
6081 /* Ignore tooltip frame. */
6082 if (f == tooltip_frame)
6083 continue;
6084
6085 /* If a window on this frame changed size, report that to
6086 the user and clear the size-change flag. */
6087 if (FRAME_WINDOW_SIZES_CHANGED (f))
6088 {
6089 Lisp_Object functions;
6090
6091 /* Clear flag first in case we get an error below. */
6092 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6093 functions = Vwindow_size_change_functions;
6094 GCPRO2 (tail, functions);
6095
6096 while (CONSP (functions))
6097 {
6098 call1 (XCAR (functions), frame);
6099 functions = XCDR (functions);
6100 }
6101 UNGCPRO;
6102 }
6103
6104 GCPRO1 (tail);
6105 update_menu_bar (f, 0);
6106#ifdef HAVE_WINDOW_SYSTEM
6107 update_toolbar (f, 0);
6108#endif
6109 UNGCPRO;
6110 }
6111
6112 unbind_to (count, Qnil);
6113 }
6114 else
6115 {
6116 update_menu_bar (selected_frame, 1);
6117#ifdef HAVE_WINDOW_SYSTEM
6118 update_toolbar (selected_frame, 1);
6119#endif
6120 }
6121
6122 /* Motif needs this. See comment in xmenu.c. Turn it off when
6123 pending_menu_activation is not defined. */
6124#ifdef USE_X_TOOLKIT
6125 pending_menu_activation = 0;
6126#endif
6127}
6128
6129
6130/* Update the menu bar item list for frame F. This has to be done
6131 before we start to fill in any display lines, because it can call
6132 eval.
6133
6134 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6135
6136static void
6137update_menu_bar (f, save_match_data)
6138 struct frame *f;
6139 int save_match_data;
6140{
6141 Lisp_Object window;
6142 register struct window *w;
6143
6144 window = FRAME_SELECTED_WINDOW (f);
6145 w = XWINDOW (window);
6146
6147 if (update_mode_lines)
6148 w->update_mode_line = Qt;
6149
6150 if (FRAME_WINDOW_P (f)
6151 ?
6152#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6153 FRAME_EXTERNAL_MENU_BAR (f)
6154#else
6155 FRAME_MENU_BAR_LINES (f) > 0
6156#endif
6157 : FRAME_MENU_BAR_LINES (f) > 0)
6158 {
6159 /* If the user has switched buffers or windows, we need to
6160 recompute to reflect the new bindings. But we'll
6161 recompute when update_mode_lines is set too; that means
6162 that people can use force-mode-line-update to request
6163 that the menu bar be recomputed. The adverse effect on
6164 the rest of the redisplay algorithm is about the same as
6165 windows_or_buffers_changed anyway. */
6166 if (windows_or_buffers_changed
6167 || !NILP (w->update_mode_line)
6168 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6169 < BUF_MODIFF (XBUFFER (w->buffer)))
6170 != !NILP (w->last_had_star))
6171 || ((!NILP (Vtransient_mark_mode)
6172 && !NILP (XBUFFER (w->buffer)->mark_active))
6173 != !NILP (w->region_showing)))
6174 {
6175 struct buffer *prev = current_buffer;
6176 int count = specpdl_ptr - specpdl;
6177
6178 set_buffer_internal_1 (XBUFFER (w->buffer));
6179 if (save_match_data)
6180 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6181 if (NILP (Voverriding_local_map_menu_flag))
6182 {
6183 specbind (Qoverriding_terminal_local_map, Qnil);
6184 specbind (Qoverriding_local_map, Qnil);
6185 }
6186
6187 /* Run the Lucid hook. */
6188 call1 (Vrun_hooks, Qactivate_menubar_hook);
6189
6190 /* If it has changed current-menubar from previous value,
6191 really recompute the menu-bar from the value. */
6192 if (! NILP (Vlucid_menu_bar_dirty_flag))
6193 call0 (Qrecompute_lucid_menubar);
6194
6195 safe_run_hooks (Qmenu_bar_update_hook);
6196 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6197
6198 /* Redisplay the menu bar in case we changed it. */
6199#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6200 if (FRAME_WINDOW_P (f))
6201 set_frame_menubar (f, 0, 0);
6202 else
6203 /* On a terminal screen, the menu bar is an ordinary screen
6204 line, and this makes it get updated. */
6205 w->update_mode_line = Qt;
6206#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6207 /* In the non-toolkit version, the menu bar is an ordinary screen
6208 line, and this makes it get updated. */
6209 w->update_mode_line = Qt;
6210#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6211
6212 unbind_to (count, Qnil);
6213 set_buffer_internal_1 (prev);
6214 }
6215 }
6216}
6217
6218
6219\f
6220/***********************************************************************
6221 Toolbars
6222 ***********************************************************************/
6223
6224#ifdef HAVE_WINDOW_SYSTEM
6225
6226/* Update the toolbar item list for frame F. This has to be done
6227 before we start to fill in any display lines. Called from
6228 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6229 and restore it here. */
6230
6231static void
6232update_toolbar (f, save_match_data)
6233 struct frame *f;
6234 int save_match_data;
6235{
6236 if (WINDOWP (f->toolbar_window)
6237 && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
6238 {
6239 Lisp_Object window;
6240 struct window *w;
6241
6242 window = FRAME_SELECTED_WINDOW (f);
6243 w = XWINDOW (window);
6244
6245 /* If the user has switched buffers or windows, we need to
6246 recompute to reflect the new bindings. But we'll
6247 recompute when update_mode_lines is set too; that means
6248 that people can use force-mode-line-update to request
6249 that the menu bar be recomputed. The adverse effect on
6250 the rest of the redisplay algorithm is about the same as
6251 windows_or_buffers_changed anyway. */
6252 if (windows_or_buffers_changed
6253 || !NILP (w->update_mode_line)
6254 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6255 < BUF_MODIFF (XBUFFER (w->buffer)))
6256 != !NILP (w->last_had_star))
6257 || ((!NILP (Vtransient_mark_mode)
6258 && !NILP (XBUFFER (w->buffer)->mark_active))
6259 != !NILP (w->region_showing)))
6260 {
6261 struct buffer *prev = current_buffer;
6262 int count = specpdl_ptr - specpdl;
a2889657 6263
5f5c8ee5
GM
6264 /* Set current_buffer to the buffer of the selected
6265 window of the frame, so that we get the right local
6266 keymaps. */
6267 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 6268
5f5c8ee5
GM
6269 /* Save match data, if we must. */
6270 if (save_match_data)
6271 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6272
6273 /* Make sure that we don't accidentally use bogus keymaps. */
6274 if (NILP (Voverriding_local_map_menu_flag))
6275 {
6276 specbind (Qoverriding_terminal_local_map, Qnil);
6277 specbind (Qoverriding_local_map, Qnil);
1f40cad2 6278 }
1f40cad2 6279
5f5c8ee5
GM
6280 /* Build desired toolbar items from keymaps. */
6281 f->desired_toolbar_items
6282 = toolbar_items (f->desired_toolbar_items,
6283 &f->n_desired_toolbar_items);
6284
6285 /* Redisplay the toolbar in case we changed it. */
6286 w->update_mode_line = Qt;
6287
6288 unbind_to (count, Qnil);
6289 set_buffer_internal_1 (prev);
81d478f3 6290 }
a2889657
JB
6291 }
6292}
6293
6c4429a5 6294
5f5c8ee5
GM
6295/* Set F->desired_toolbar_string to a Lisp string representing frame
6296 F's desired toolbar contents. F->desired_toolbar_items must have
6297 been set up previously by calling prepare_menu_bars. */
6298
a2889657 6299static void
5f5c8ee5
GM
6300build_desired_toolbar_string (f)
6301 struct frame *f;
a2889657 6302{
5f5c8ee5
GM
6303 int i, size, size_needed, string_idx;
6304 struct gcpro gcpro1, gcpro2, gcpro3;
6305 Lisp_Object image, plist, props;
a2889657 6306
5f5c8ee5
GM
6307 image = plist = props = Qnil;
6308 GCPRO3 (image, plist, props);
a2889657 6309
5f5c8ee5
GM
6310 /* Prepare F->desired_toolbar_string. If we can reuse it, do so.
6311 Otherwise, make a new string. */
6312
6313 /* The size of the string we might be able to reuse. */
6314 size = (STRINGP (f->desired_toolbar_string)
6315 ? XSTRING (f->desired_toolbar_string)->size
6316 : 0);
6317
6318 /* Each image in the string we build is preceded by a space,
6319 and there is a space at the end. */
6320 size_needed = f->n_desired_toolbar_items + 1;
6321
6322 /* Reuse f->desired_toolbar_string, if possible. */
6323 if (size < size_needed)
6324 f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
6325 else
6326 {
6327 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6328 Fremove_text_properties (make_number (0), make_number (size),
6329 props, f->desired_toolbar_string);
6330 }
a2889657 6331
5f5c8ee5
GM
6332 /* Put a `display' property on the string for the images to display,
6333 put a `menu_item' property on toolbar items with a value that
6334 is the index of the item in F's toolbar item vector. */
6335 for (i = 0, string_idx = 0;
6336 i < f->n_desired_toolbar_items;
6337 ++i, string_idx += 1)
a2889657 6338 {
5f5c8ee5
GM
6339#define PROP(IDX) \
6340 (XVECTOR (f->desired_toolbar_items) \
6341 ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
6342
6343 int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
6344 int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
6345 int margin, relief;
6346 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6347 extern Lisp_Object Qlaplace;
6348
6349 /* If image is a vector, choose the image according to the
6350 button state. */
6351 image = PROP (TOOLBAR_ITEM_IMAGES);
6352 if (VECTORP (image))
6353 {
6354 enum toolbar_item_image idx;
6355
6356 if (enabled_p)
6357 idx = (selected_p
6358 ? TOOLBAR_IMAGE_ENABLED_SELECTED
6359 : TOOLBAR_IMAGE_ENABLED_DESELECTED);
6360 else
6361 idx = (selected_p
6362 ? TOOLBAR_IMAGE_DISABLED_SELECTED
6363 : TOOLBAR_IMAGE_DISABLED_DESELECTED);
6364
6365 xassert (XVECTOR (image)->size >= idx);
6366 image = XVECTOR (image)->contents[idx];
6367 }
6368
6369 /* Ignore invalid image specifications. */
6370 if (!valid_image_p (image))
6371 continue;
6372
6373 /* Display the toolbar button pressed, or depressed. */
6374 plist = Fcopy_sequence (XCDR (image));
6375
6376 /* Compute margin and relief to draw. */
6377 relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
6378 margin = relief + max (0, toolbar_button_margin);
6379
6380 if (auto_raise_toolbar_buttons_p)
6381 {
6382 /* Add a `:relief' property to the image spec if the item is
6383 selected. */
6384 if (selected_p)
6385 {
6386 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6387 margin -= relief;
6388 }
6389 }
6390 else
6391 {
6392 /* If image is selected, display it pressed, i.e. with a
6393 negative relief. If it's not selected, display it with a
6394 raised relief. */
6395 plist = Fplist_put (plist, QCrelief,
6396 (selected_p
6397 ? make_number (-relief)
6398 : make_number (relief)));
6399 margin -= relief;
6400 }
6401
6402 /* Put a margin around the image. */
6403 if (margin)
6404 plist = Fplist_put (plist, QCmargin, make_number (margin));
6405
6406 /* If button is not enabled, make the image appear disabled by
6407 applying an appropriate algorithm to it. */
6408 if (!enabled_p)
6409 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6410
6411 /* Put a `display' text property on the string for the image to
6412 display. Put a `menu-item' property on the string that gives
6413 the start of this item's properties in the toolbar items
6414 vector. */
6415 image = Fcons (Qimage, plist);
6416 props = list4 (Qdisplay, image,
6417 Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
6418 Fadd_text_properties (make_number (string_idx),
6419 make_number (string_idx + 1),
6420 props, f->desired_toolbar_string);
6421#undef PROP
a2889657
JB
6422 }
6423
5f5c8ee5
GM
6424 UNGCPRO;
6425}
6426
6427
6428/* Display one line of the toolbar of frame IT->f. */
6429
6430static void
6431display_toolbar_line (it)
6432 struct it *it;
6433{
6434 struct glyph_row *row = it->glyph_row;
6435 int max_x = it->last_visible_x;
6436 struct glyph *last;
6437
6438 prepare_desired_row (row);
6439 row->y = it->current_y;
6440
6441 while (it->current_x < max_x)
a2889657 6442 {
5f5c8ee5 6443 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 6444
5f5c8ee5
GM
6445 /* Get the next display element. */
6446 if (!get_next_display_element (it))
6447 break;
73af359d 6448
5f5c8ee5
GM
6449 /* Produce glyphs. */
6450 x_before = it->current_x;
6451 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6452 PRODUCE_GLYPHS (it);
daa37602 6453
5f5c8ee5
GM
6454 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6455 i = 0;
6456 x = x_before;
6457 while (i < nglyphs)
6458 {
6459 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6460
6461 if (x + glyph->pixel_width > max_x)
6462 {
6463 /* Glyph doesn't fit on line. */
6464 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6465 it->current_x = x;
6466 goto out;
6467 }
daa37602 6468
5f5c8ee5
GM
6469 ++it->hpos;
6470 x += glyph->pixel_width;
6471 ++i;
6472 }
6473
6474 /* Stop at line ends. */
6475 if (ITERATOR_AT_END_OF_LINE_P (it))
6476 break;
6477
6478 set_iterator_to_next (it);
a2889657 6479 }
a2889657 6480
5f5c8ee5 6481 out:;
a2889657 6482
5f5c8ee5
GM
6483 row->displays_text_p = row->used[TEXT_AREA] != 0;
6484 extend_face_to_end_of_line (it);
6485 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6486 last->right_box_line_p = 1;
6487 compute_line_metrics (it);
6488
6489 /* If line is empty, make it occupy the rest of the toolbar. */
6490 if (!row->displays_text_p)
6491 {
312246d1
GM
6492 row->height = row->phys_height = it->last_visible_y - row->y;
6493 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
6494 }
6495
6496 row->full_width_p = 1;
6497 row->continued_p = 0;
6498 row->truncated_on_left_p = 0;
6499 row->truncated_on_right_p = 0;
6500
6501 it->current_x = it->hpos = 0;
6502 it->current_y += row->height;
6503 ++it->vpos;
6504 ++it->glyph_row;
a2889657 6505}
96a410bc 6506
5f5c8ee5
GM
6507
6508/* Value is the number of screen lines needed to make all toolbar
6509 items of frame F visible. */
96a410bc 6510
d39b6696 6511static int
5f5c8ee5
GM
6512toolbar_lines_needed (f)
6513 struct frame *f;
d39b6696 6514{
5f5c8ee5
GM
6515 struct window *w = XWINDOW (f->toolbar_window);
6516 struct it it;
6517
6518 /* Initialize an iterator for iteration over F->desired_toolbar_string
6519 in the toolbar window of frame F. */
6520 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6521 it.first_visible_x = 0;
6522 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6523 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
6524
6525 while (!ITERATOR_AT_END_P (&it))
6526 {
6527 it.glyph_row = w->desired_matrix->rows;
6528 clear_glyph_row (it.glyph_row);
6529 display_toolbar_line (&it);
6530 }
6531
6532 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 6533}
96a410bc 6534
5f5c8ee5
GM
6535
6536/* Display the toolbar of frame F. Value is non-zero if toolbar's
6537 height should be changed. */
6538
6539static int
6540redisplay_toolbar (f)
6541 struct frame *f;
96a410bc 6542{
5f5c8ee5
GM
6543 struct window *w;
6544 struct it it;
6545 struct glyph_row *row;
6546 int change_height_p = 0;
6547
6548 /* If frame hasn't a toolbar window or if it is zero-height, don't
6549 do anything. This means you must start with toolbar-lines
6550 non-zero to get the auto-sizing effect. Or in other words, you
6551 can turn off toolbars by specifying toolbar-lines zero. */
6552 if (!WINDOWP (f->toolbar_window)
6553 || (w = XWINDOW (f->toolbar_window),
6554 XFASTINT (w->height) == 0))
6555 return 0;
96a410bc 6556
5f5c8ee5
GM
6557 /* Set up an iterator for the toolbar window. */
6558 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6559 it.first_visible_x = 0;
6560 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6561 row = it.glyph_row;
3450d04c 6562
5f5c8ee5
GM
6563 /* Build a string that represents the contents of the toolbar. */
6564 build_desired_toolbar_string (f);
6565 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
3450d04c 6566
5f5c8ee5
GM
6567 /* Display as many lines as needed to display all toolbar items. */
6568 while (it.current_y < it.last_visible_y)
6569 display_toolbar_line (&it);
3450d04c 6570
5f5c8ee5
GM
6571 /* It doesn't make much sense to try scrolling in the toolbar
6572 window, so don't do it. */
6573 w->desired_matrix->no_scrolling_p = 1;
6574 w->must_be_updated_p = 1;
3450d04c 6575
5f5c8ee5
GM
6576 if (auto_resize_toolbars_p)
6577 {
6578 int nlines;
6579
6580 /* If there are blank lines at the end, except for a partially
6581 visible blank line at the end that is smaller than
6582 CANON_Y_UNIT, change the toolbar's height. */
6583 row = it.glyph_row - 1;
6584 if (!row->displays_text_p
6585 && row->height >= CANON_Y_UNIT (f))
6586 change_height_p = 1;
6587
6588 /* If row displays toolbar items, but is partially visible,
6589 change the toolbar's height. */
6590 if (row->displays_text_p
6591 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6592 change_height_p = 1;
6593
6594 /* Resize windows as needed by changing the `toolbar-lines'
6595 frame parameter. */
6596 if (change_height_p
6597 && (nlines = toolbar_lines_needed (f),
6598 nlines != XFASTINT (w->height)))
6599 {
6600 extern Lisp_Object Qtoolbar_lines;
6601 Lisp_Object frame;
6602
6603 XSETFRAME (frame, f);
6604 clear_glyph_matrix (w->desired_matrix);
6605 Fmodify_frame_parameters (frame,
6606 Fcons (Fcons (Qtoolbar_lines,
6607 make_number (nlines)),
6608 Qnil));
6609 fonts_changed_p = 1;
6610 }
6611 }
3450d04c 6612
5f5c8ee5 6613 return change_height_p;
96a410bc 6614}
90adcf20 6615
5f5c8ee5
GM
6616
6617/* Get information about the toolbar item which is displayed in GLYPH
6618 on frame F. Return in *PROP_IDX the index where toolbar item
6619 properties start in F->current_toolbar_items. Value is zero if
6620 GLYPH doesn't display a toolbar item. */
6621
6622int
6623toolbar_item_info (f, glyph, prop_idx)
6624 struct frame *f;
6625 struct glyph *glyph;
6626 int *prop_idx;
90adcf20 6627{
5f5c8ee5
GM
6628 Lisp_Object prop;
6629 int success_p;
6630
6631 /* Get the text property `menu-item' at pos. The value of that
6632 property is the start index of this item's properties in
6633 F->current_toolbar_items. */
6634 prop = Fget_text_property (make_number (glyph->charpos),
6635 Qmenu_item, f->current_toolbar_string);
6636 if (INTEGERP (prop))
6637 {
6638 *prop_idx = XINT (prop);
6639 success_p = 1;
6640 }
6641 else
6642 success_p = 0;
90adcf20 6643
5f5c8ee5
GM
6644 return success_p;
6645}
6646
6647#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6648
feb0c42f 6649
5f5c8ee5
GM
6650\f
6651/************************************************************************
6652 Horizontal scrolling
6653 ************************************************************************/
feb0c42f 6654
5f5c8ee5
GM
6655static int hscroll_window_tree P_ ((Lisp_Object));
6656static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6657
5f5c8ee5
GM
6658/* For all leaf windows in the window tree rooted at WINDOW, set their
6659 hscroll value so that PT is (i) visible in the window, and (ii) so
6660 that it is not within a certain margin at the window's left and
6661 right border. Value is non-zero if any window's hscroll has been
6662 changed. */
6663
6664static int
6665hscroll_window_tree (window)
6666 Lisp_Object window;
6667{
6668 int hscrolled_p = 0;
6669
6670 while (WINDOWP (window))
90adcf20 6671 {
5f5c8ee5
GM
6672 struct window *w = XWINDOW (window);
6673
6674 if (WINDOWP (w->hchild))
6675 hscrolled_p |= hscroll_window_tree (w->hchild);
6676 else if (WINDOWP (w->vchild))
6677 hscrolled_p |= hscroll_window_tree (w->vchild);
6678 else if (w->cursor.vpos >= 0)
6679 {
6680 int hscroll_margin, text_area_x, text_area_y;
6681 int text_area_width, text_area_height;
6682 struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
6683 w->cursor.vpos);
a2725ab2 6684
5f5c8ee5
GM
6685 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6686 &text_area_width, &text_area_height);
90adcf20 6687
5f5c8ee5
GM
6688 /* Scroll when cursor is inside this scroll margin. */
6689 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6690
6691 if ((XFASTINT (w->hscroll)
6692 && w->cursor.x < hscroll_margin)
6693 || (cursor_row->truncated_on_right_p
6694 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 6695 {
5f5c8ee5
GM
6696 struct it it;
6697 int hscroll;
6698 struct buffer *saved_current_buffer;
6699 int pt;
6700
6701 /* Find point in a display of infinite width. */
6702 saved_current_buffer = current_buffer;
6703 current_buffer = XBUFFER (w->buffer);
6704
6705 if (w == XWINDOW (selected_window))
6706 pt = BUF_PT (current_buffer);
6707 else
08b610e4 6708 {
5f5c8ee5
GM
6709 pt = marker_position (w->pointm);
6710 pt = max (BEGV, pt);
6711 pt = min (ZV, pt);
6712 }
6713
6714 /* Move iterator to pt starting at cursor_row->start in
6715 a line with infinite width. */
6716 init_to_row_start (&it, w, cursor_row);
6717 it.last_visible_x = INFINITY;
6718 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6719 current_buffer = saved_current_buffer;
6720
6721 /* Center cursor in window. */
6722 hscroll = (max (0, it.current_x - text_area_width / 2)
6723 / CANON_X_UNIT (it.f));
6724
6725 /* Don't call Fset_window_hscroll if value hasn't
6726 changed because it will prevent redisplay
6727 optimizations. */
6728 if (XFASTINT (w->hscroll) != hscroll)
6729 {
6730 Fset_window_hscroll (window, make_number (hscroll));
6731 hscrolled_p = 1;
08b610e4 6732 }
08b610e4 6733 }
08b610e4 6734 }
a2725ab2 6735
5f5c8ee5 6736 window = w->next;
90adcf20 6737 }
cd6dfed6 6738
5f5c8ee5
GM
6739 /* Value is non-zero if hscroll of any leaf window has been changed. */
6740 return hscrolled_p;
6741}
6742
6743
6744/* Set hscroll so that cursor is visible and not inside horizontal
6745 scroll margins for all windows in the tree rooted at WINDOW. See
6746 also hscroll_window_tree above. Value is non-zero if any window's
6747 hscroll has been changed. If it has, desired matrices on the frame
6748 of WINDOW are cleared. */
6749
6750static int
6751hscroll_windows (window)
6752 Lisp_Object window;
6753{
6754 int hscrolled_p = hscroll_window_tree (window);
6755 if (hscrolled_p)
6756 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6757 return hscrolled_p;
90adcf20 6758}
5f5c8ee5
GM
6759
6760
90adcf20 6761\f
5f5c8ee5
GM
6762/************************************************************************
6763 Redisplay
6764 ************************************************************************/
6765
6766/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6767 to a non-zero value. This is sometimes handy to have in a debugger
6768 session. */
6769
6770#if GLYPH_DEBUG
a2889657 6771
5f5c8ee5
GM
6772/* First and last unchanged row for try_window_id. */
6773
6774int debug_first_unchanged_at_end_vpos;
6775int debug_last_unchanged_at_beg_vpos;
6776
6777/* Delta vpos and y. */
6778
6779int debug_dvpos, debug_dy;
6780
6781/* Delta in characters and bytes for try_window_id. */
6782
6783int debug_delta, debug_delta_bytes;
6784
6785/* Values of window_end_pos and window_end_vpos at the end of
6786 try_window_id. */
6787
6788int debug_end_pos, debug_end_vpos;
6789
6790/* Append a string to W->desired_matrix->method. FMT is a printf
6791 format string. A1...A9 are a supplement for a variable-length
6792 argument list. If trace_redisplay_p is non-zero also printf the
6793 resulting string to stderr. */
6794
6795static void
6796debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6797 struct window *w;
6798 char *fmt;
6799 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6800{
6801 char buffer[512];
6802 char *method = w->desired_matrix->method;
6803 int len = strlen (method);
6804 int size = sizeof w->desired_matrix->method;
6805 int remaining = size - len - 1;
6806
6807 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6808 if (len && remaining)
6809 {
6810 method[len] = '|';
6811 --remaining, ++len;
6812 }
6813
6814 strncpy (method + len, buffer, remaining);
6815
6816 if (trace_redisplay_p)
6817 fprintf (stderr, "%p (%s): %s\n",
6818 w,
6819 ((BUFFERP (w->buffer)
6820 && STRINGP (XBUFFER (w->buffer)->name))
6821 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6822 : "no buffer"),
6823 buffer);
6824}
a2889657 6825
5f5c8ee5 6826#endif /* GLYPH_DEBUG */
90adcf20 6827
a2889657 6828
5f5c8ee5
GM
6829/* This counter is used to clear the face cache every once in a while
6830 in redisplay_internal. It is incremented for each redisplay.
6831 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6832 cleared. */
0d231165 6833
5f5c8ee5 6834#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
6835static int clear_face_cache_count;
6836
20de20dc 6837/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
6838
6839static struct frame *previous_terminal_frame;
6840
6841/* Non-zero while redisplay_internal is in progress. */
6842
6843int redisplaying_p;
6844
6845
6846/* Value is non-zero if all changes in window W, which displays
6847 current_buffer, are in the text between START and END. START is a
6848 buffer position, END is given as a distance from Z. Used in
6849 redisplay_internal for display optimization. */
6850
6851static INLINE int
6852text_outside_line_unchanged_p (w, start, end)
6853 struct window *w;
6854 int start, end;
6855{
6856 int unchanged_p = 1;
6857
6858 /* If text or overlays have changed, see where. */
6859 if (XFASTINT (w->last_modified) < MODIFF
6860 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6861 {
6862 /* Gap in the line? */
6863 if (GPT < start || Z - GPT < end)
6864 unchanged_p = 0;
6865
6866 /* Changes start in front of the line, or end after it? */
6867 if (unchanged_p
9142dd5b
GM
6868 && (BEG_UNCHANGED < start - 1
6869 || END_UNCHANGED < end))
5f5c8ee5
GM
6870 unchanged_p = 0;
6871
6872 /* If selective display, can't optimize if changes start at the
6873 beginning of the line. */
6874 if (unchanged_p
6875 && INTEGERP (current_buffer->selective_display)
6876 && XINT (current_buffer->selective_display) > 0
9142dd5b 6877 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
6878 unchanged_p = 0;
6879 }
6880
6881 return unchanged_p;
6882}
6883
6884
6885/* Do a frame update, taking possible shortcuts into account. This is
6886 the main external entry point for redisplay.
6887
6888 If the last redisplay displayed an echo area message and that message
6889 is no longer requested, we clear the echo area or bring back the
6890 mini-buffer if that is in use. */
20de20dc 6891
a2889657
JB
6892void
6893redisplay ()
e9874cee
RS
6894{
6895 redisplay_internal (0);
6896}
6897
5f5c8ee5 6898
9142dd5b
GM
6899/* Reconsider the setting of B->clip_changed which is displayed
6900 in window W. */
6901
6902static INLINE void
6903reconsider_clip_changes (w, b)
6904 struct window *w;
6905 struct buffer *b;
6906{
6907 if (b->prevent_redisplay_optimizations_p)
6908 b->clip_changed = 1;
6909 else if (b->clip_changed
6910 && !NILP (w->window_end_valid)
6911 && w->current_matrix->buffer == b
6912 && w->current_matrix->zv == BUF_ZV (b)
6913 && w->current_matrix->begv == BUF_BEGV (b))
6914 b->clip_changed = 0;
6915}
6916
6917
5f5c8ee5
GM
6918/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6919 response to any user action; therefore, we should preserve the echo
6920 area. (Actually, our caller does that job.) Perhaps in the future
6921 avoid recentering windows if it is not necessary; currently that
6922 causes some problems. */
e9874cee
RS
6923
6924static void
6925redisplay_internal (preserve_echo_area)
6926 int preserve_echo_area;
a2889657 6927{
5f5c8ee5
GM
6928 struct window *w = XWINDOW (selected_window);
6929 struct frame *f = XFRAME (w->frame);
6930 int pause;
a2889657 6931 int must_finish = 0;
5f5c8ee5 6932 struct text_pos tlbufpos, tlendpos;
89819bdd 6933 int number_of_visible_frames;
28514cd9 6934 int count;
a2889657 6935
5f5c8ee5
GM
6936 /* Non-zero means redisplay has to consider all windows on all
6937 frames. Zero means, only selected_window is considered. */
6938 int consider_all_windows_p;
6939
6940 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6941
6942 /* No redisplay if running in batch mode or frame is not yet fully
6943 initialized, or redisplay is explicitly turned off by setting
6944 Vinhibit_redisplay. */
6945 if (noninteractive
6946 || !NILP (Vinhibit_redisplay)
6947 || !f->glyphs_initialized_p)
a2889657
JB
6948 return;
6949
5f5c8ee5
GM
6950 /* The flag redisplay_performed_directly_p is set by
6951 direct_output_for_insert when it already did the whole screen
6952 update necessary. */
6953 if (redisplay_performed_directly_p)
6954 {
6955 redisplay_performed_directly_p = 0;
6956 if (!hscroll_windows (selected_window))
6957 return;
6958 }
6959
15f0cf78
RS
6960#ifdef USE_X_TOOLKIT
6961 if (popup_activated ())
6962 return;
6963#endif
6964
28514cd9 6965 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 6966 if (redisplaying_p)
735c094c
KH
6967 return;
6968
28514cd9
GM
6969 /* Record a function that resets redisplaying_p to its old value
6970 when we leave this function. */
6971 count = specpdl_ptr - specpdl;
6972 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
6973 ++redisplaying_p;
6974
8b32d885
RS
6975 retry:
6976
9142dd5b
GM
6977 reconsider_clip_changes (w, current_buffer);
6978
5f5c8ee5
GM
6979 /* If new fonts have been loaded that make a glyph matrix adjustment
6980 necessary, do it. */
6981 if (fonts_changed_p)
6982 {
6983 adjust_glyphs (NULL);
6984 ++windows_or_buffers_changed;
6985 fonts_changed_p = 0;
6986 }
6987
fd8ff63d 6988 if (! FRAME_WINDOW_P (selected_frame)
20de20dc
RS
6989 && previous_terminal_frame != selected_frame)
6990 {
5f5c8ee5
GM
6991 /* Since frames on an ASCII terminal share the same display
6992 area, displaying a different frame means redisplay the whole
6993 thing. */
20de20dc
RS
6994 windows_or_buffers_changed++;
6995 SET_FRAME_GARBAGED (selected_frame);
6996 XSETFRAME (Vterminal_frame, selected_frame);
6997 }
6998 previous_terminal_frame = selected_frame;
20de20dc 6999
5f5c8ee5
GM
7000 /* Set the visible flags for all frames. Do this before checking
7001 for resized or garbaged frames; they want to know if their frames
7002 are visible. See the comment in frame.h for
7003 FRAME_SAMPLE_VISIBILITY. */
d724d989 7004 {
35f56f96 7005 Lisp_Object tail, frame;
d724d989 7006
89819bdd
RS
7007 number_of_visible_frames = 0;
7008
35f56f96 7009 FOR_EACH_FRAME (tail, frame)
f82aff7c 7010 {
5f5c8ee5
GM
7011 struct frame *f = XFRAME (frame);
7012
7013 FRAME_SAMPLE_VISIBILITY (f);
7014 if (FRAME_VISIBLE_P (f))
7015 ++number_of_visible_frames;
7016 clear_desired_matrices (f);
f82aff7c 7017 }
d724d989
JB
7018 }
7019
44fa5b1e 7020 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 7021 do_pending_window_change (1);
a2889657 7022
5f5c8ee5 7023 /* Clear frames marked as garbaged. */
44fa5b1e 7024 if (frame_garbaged)
c6e89d6c 7025 clear_garbaged_frames ();
a2889657 7026
5f5c8ee5 7027 /* Build menubar and toolbar items. */
f82aff7c
RS
7028 prepare_menu_bars ();
7029
28995e67 7030 if (windows_or_buffers_changed)
a2889657
JB
7031 update_mode_lines++;
7032
538f13d4
RS
7033 /* Detect case that we need to write or remove a star in the mode line. */
7034 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
7035 {
7036 w->update_mode_line = Qt;
7037 if (buffer_shared > 1)
7038 update_mode_lines++;
7039 }
7040
5f5c8ee5 7041 /* If %c is in the mode line, update it if needed. */
28995e67
RS
7042 if (!NILP (w->column_number_displayed)
7043 /* This alternative quickly identifies a common case
7044 where no change is needed. */
7045 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7046 && XFASTINT (w->last_modified) >= MODIFF
7047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7048 && XFASTINT (w->column_number_displayed) != current_column ())
7049 w->update_mode_line = Qt;
7050
44fa5b1e 7051 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 7052
5f5c8ee5
GM
7053 /* The variable buffer_shared is set in redisplay_window and
7054 indicates that we redisplay a buffer in different windows. See
7055 there. */
7056 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
7057
7058 /* If specs for an arrow have changed, do thorough redisplay
7059 to ensure we remove any arrow that should no longer exist. */
d45de95b 7060 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 7061 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 7062 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 7063
90adcf20
RS
7064 /* Normally the message* functions will have already displayed and
7065 updated the echo area, but the frame may have been trashed, or
7066 the update may have been preempted, so display the echo area
c6e89d6c
GM
7067 again here. Checking both message buffers captures the case that
7068 the echo area should be cleared. */
7069 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 7070 {
c6e89d6c 7071 int window_height_changed_p = echo_area_display (0);
90adcf20 7072 must_finish = 1;
dd2eb166 7073
c6e89d6c
GM
7074 if (fonts_changed_p)
7075 goto retry;
7076 else if (window_height_changed_p)
7077 {
7078 consider_all_windows_p = 1;
7079 ++update_mode_lines;
7080 ++windows_or_buffers_changed;
9142dd5b
GM
7081
7082 /* If window configuration was changed, frames may have been
7083 marked garbaged. Clear them or we will experience
7084 surprises wrt scrolling. */
7085 if (frame_garbaged)
7086 clear_garbaged_frames ();
c6e89d6c 7087 }
90adcf20 7088 }
dd2eb166
GM
7089 else if (w == XWINDOW (minibuf_window)
7090 && (current_buffer->clip_changed
7091 || XFASTINT (w->last_modified) < MODIFF
7092 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7093 && resize_mini_window (w))
c6e89d6c
GM
7094 {
7095 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
7096 showing if its contents might have changed. */
7097 must_finish = 1;
7098 consider_all_windows_p = 1;
c6e89d6c 7099 ++windows_or_buffers_changed;
dd2eb166 7100 ++update_mode_lines;
9142dd5b
GM
7101
7102 /* If window configuration was changed, frames may have been
7103 marked garbaged. Clear them or we will experience
7104 surprises wrt scrolling. */
7105 if (frame_garbaged)
7106 clear_garbaged_frames ();
c6e89d6c
GM
7107 }
7108
90adcf20 7109
5f5c8ee5
GM
7110 /* If showing the region, and mark has changed, we must redisplay
7111 the whole window. The assignment to this_line_start_pos prevents
7112 the optimization directly below this if-statement. */
bd66d1ba
RS
7113 if (((!NILP (Vtransient_mark_mode)
7114 && !NILP (XBUFFER (w->buffer)->mark_active))
7115 != !NILP (w->region_showing))
82d04750
JB
7116 || (!NILP (w->region_showing)
7117 && !EQ (w->region_showing,
7118 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
7119 CHARPOS (this_line_start_pos) = 0;
7120
7121 /* Optimize the case that only the line containing the cursor in the
7122 selected window has changed. Variables starting with this_ are
7123 set in display_line and record information about the line
7124 containing the cursor. */
7125 tlbufpos = this_line_start_pos;
7126 tlendpos = this_line_end_pos;
7127 if (!consider_all_windows_p
7128 && CHARPOS (tlbufpos) > 0
7129 && NILP (w->update_mode_line)
73af359d 7130 && !current_buffer->clip_changed
44fa5b1e 7131 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 7132 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 7133 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
7134 && this_line_buffer == current_buffer
7135 && current_buffer == XBUFFER (w->buffer)
265a9e55 7136 && NILP (w->force_start)
5f5c8ee5
GM
7137 /* Point must be on the line that we have info recorded about. */
7138 && PT >= CHARPOS (tlbufpos)
7139 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
7140 /* All text outside that line, including its final newline,
7141 must be unchanged */
5f5c8ee5
GM
7142 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7143 CHARPOS (tlendpos)))
7144 {
7145 if (CHARPOS (tlbufpos) > BEGV
7146 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7147 && (CHARPOS (tlbufpos) == ZV
7148 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
7149 /* Former continuation line has disappeared by becoming empty */
7150 goto cancel;
7151 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 7152 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
7153 || MINI_WINDOW_P (w))
7154 {
1c9241f5
KH
7155 /* We have to handle the case of continuation around a
7156 wide-column character (See the comment in indent.c around
7157 line 885).
7158
7159 For instance, in the following case:
7160
7161 -------- Insert --------
7162 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7163 J_I_ ==> J_I_ `^^' are cursors.
7164 ^^ ^^
7165 -------- --------
7166
7167 As we have to redraw the line above, we should goto cancel. */
7168
5f5c8ee5
GM
7169 struct it it;
7170 int line_height_before = this_line_pixel_height;
7171
7172 /* Note that start_display will handle the case that the
7173 line starting at tlbufpos is a continuation lines. */
7174 start_display (&it, w, tlbufpos);
7175
7176 /* Implementation note: It this still necessary? */
7177 if (it.current_x != this_line_start_x)
1c9241f5
KH
7178 goto cancel;
7179
5f5c8ee5
GM
7180 TRACE ((stderr, "trying display optimization 1\n"));
7181 w->cursor.vpos = -1;
a2889657 7182 overlay_arrow_seen = 0;
5f5c8ee5
GM
7183 it.vpos = this_line_vpos;
7184 it.current_y = this_line_y;
7185 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7186 display_line (&it);
7187
a2889657 7188 /* If line contains point, is not continued,
5f5c8ee5
GM
7189 and ends at same distance from eob as before, we win */
7190 if (w->cursor.vpos >= 0
7191 /* Line is not continued, otherwise this_line_start_pos
7192 would have been set to 0 in display_line. */
7193 && CHARPOS (this_line_start_pos)
7194 /* Line ends as before. */
7195 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7196 /* Line has same height as before. Otherwise other lines
7197 would have to be shifted up or down. */
7198 && this_line_pixel_height == line_height_before)
a2889657 7199 {
5f5c8ee5
GM
7200 /* If this is not the window's last line, we must adjust
7201 the charstarts of the lines below. */
7202 if (it.current_y < it.last_visible_y)
7203 {
7204 struct glyph_row *row
7205 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7206 int delta, delta_bytes;
7207
7208 if (Z - CHARPOS (tlendpos) == ZV)
7209 {
7210 /* This line ends at end of (accessible part of)
7211 buffer. There is no newline to count. */
7212 delta = (Z
7213 - CHARPOS (tlendpos)
7214 - MATRIX_ROW_START_CHARPOS (row));
7215 delta_bytes = (Z_BYTE
7216 - BYTEPOS (tlendpos)
7217 - MATRIX_ROW_START_BYTEPOS (row));
7218 }
7219 else
7220 {
7221 /* This line ends in a newline. Must take
7222 account of the newline and the rest of the
7223 text that follows. */
7224 delta = (Z
7225 - CHARPOS (tlendpos)
7226 - MATRIX_ROW_START_CHARPOS (row));
7227 delta_bytes = (Z_BYTE
7228 - BYTEPOS (tlendpos)
7229 - MATRIX_ROW_START_BYTEPOS (row));
7230 }
7231
7232 increment_glyph_matrix_buffer_positions (w->current_matrix,
7233 this_line_vpos + 1,
7234 w->current_matrix->nrows,
7235 delta, delta_bytes);
85bcef6c 7236 }
46db8486 7237
5f5c8ee5
GM
7238 /* If this row displays text now but previously didn't,
7239 or vice versa, w->window_end_vpos may have to be
7240 adjusted. */
7241 if ((it.glyph_row - 1)->displays_text_p)
7242 {
7243 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7244 XSETINT (w->window_end_vpos, this_line_vpos);
7245 }
7246 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7247 && this_line_vpos > 0)
7248 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7249 w->window_end_valid = Qnil;
7250
7251 /* Update hint: No need to try to scroll in update_window. */
7252 w->desired_matrix->no_scrolling_p = 1;
7253
7254#if GLYPH_DEBUG
7255 *w->desired_matrix->method = 0;
7256 debug_method_add (w, "optimization 1");
7257#endif
a2889657
JB
7258 goto update;
7259 }
7260 else
7261 goto cancel;
7262 }
5f5c8ee5
GM
7263 else if (/* Cursor position hasn't changed. */
7264 PT == XFASTINT (w->last_point)
b6f0fe04
RS
7265 /* Make sure the cursor was last displayed
7266 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
7267 && 0 <= w->cursor.vpos
7268 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
7269 {
7270 if (!must_finish)
7271 {
c6e89d6c 7272 do_pending_window_change (1);
5f5c8ee5
GM
7273
7274 /* We used to always goto end_of_redisplay here, but this
7275 isn't enough if we have a blinking cursor. */
7276 if (w->cursor_off_p == w->last_cursor_off_p)
7277 goto end_of_redisplay;
a2889657
JB
7278 }
7279 goto update;
7280 }
8b51f1e3
KH
7281 /* If highlighting the region, or if the cursor is in the echo area,
7282 then we can't just move the cursor. */
bd66d1ba
RS
7283 else if (! (!NILP (Vtransient_mark_mode)
7284 && !NILP (current_buffer->mark_active))
293a54ce
RS
7285 && (w == XWINDOW (current_buffer->last_selected_window)
7286 || highlight_nonselected_windows)
8b51f1e3 7287 && NILP (w->region_showing)
8f897821 7288 && NILP (Vshow_trailing_whitespace)
8b51f1e3 7289 && !cursor_in_echo_area)
a2889657 7290 {
5f5c8ee5
GM
7291 struct it it;
7292 struct glyph_row *row;
7293
7294 /* Skip from tlbufpos to PT and see where it is. Note that
7295 PT may be in invisible text. If so, we will end at the
7296 next visible position. */
7297 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7298 NULL, DEFAULT_FACE_ID);
7299 it.current_x = this_line_start_x;
7300 it.current_y = this_line_y;
7301 it.vpos = this_line_vpos;
7302
7303 /* The call to move_it_to stops in front of PT, but
7304 moves over before-strings. */
7305 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7306
7307 if (it.vpos == this_line_vpos
7308 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7309 row->enabled_p))
a2889657 7310 {
5f5c8ee5
GM
7311 xassert (this_line_vpos == it.vpos);
7312 xassert (this_line_y == it.current_y);
7313 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
7314 goto update;
7315 }
7316 else
7317 goto cancel;
7318 }
5f5c8ee5 7319
a2889657 7320 cancel:
5f5c8ee5
GM
7321 /* Text changed drastically or point moved off of line. */
7322 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
7323 }
7324
5f5c8ee5
GM
7325 CHARPOS (this_line_start_pos) = 0;
7326 consider_all_windows_p |= buffer_shared > 1;
7327 ++clear_face_cache_count;
a2889657 7328
5f5c8ee5
GM
7329
7330 /* Build desired matrices. If consider_all_windows_p is non-zero,
7331 do it for all windows on all frames. Otherwise do it for
7332 selected_window, only. */
463f6b91 7333
5f5c8ee5 7334 if (consider_all_windows_p)
a2889657 7335 {
35f56f96 7336 Lisp_Object tail, frame;
a2889657 7337
5f5c8ee5
GM
7338 /* Clear the face cache eventually. */
7339 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 7340 {
5f5c8ee5 7341 clear_face_cache (0);
463f6b91
RS
7342 clear_face_cache_count = 0;
7343 }
31b24551 7344
5f5c8ee5
GM
7345 /* Recompute # windows showing selected buffer. This will be
7346 incremented each time such a window is displayed. */
a2889657
JB
7347 buffer_shared = 0;
7348
35f56f96 7349 FOR_EACH_FRAME (tail, frame)
30c566e4 7350 {
5f5c8ee5 7351 struct frame *f = XFRAME (frame);
fd8ff63d 7352 if (FRAME_WINDOW_P (f) || f == selected_frame)
9769686d 7353 {
5f5c8ee5
GM
7354 /* Mark all the scroll bars to be removed; we'll redeem
7355 the ones we want when we redisplay their windows. */
9769686d
RS
7356 if (condemn_scroll_bars_hook)
7357 (*condemn_scroll_bars_hook) (f);
30c566e4 7358
f21ef775 7359 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 7360 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 7361
5f5c8ee5
GM
7362 /* Any scroll bars which redisplay_windows should have
7363 nuked should now go away. */
9769686d
RS
7364 if (judge_scroll_bars_hook)
7365 (*judge_scroll_bars_hook) (f);
7366 }
30c566e4 7367 }
a2889657 7368 }
5f5c8ee5
GM
7369 else if (FRAME_VISIBLE_P (selected_frame)
7370 && !FRAME_OBSCURED_P (selected_frame))
7371 redisplay_window (selected_window, 1);
7372
7373
7374 /* Compare desired and current matrices, perform output. */
7375
7376update:
7377
7378 /* If fonts changed, display again. */
7379 if (fonts_changed_p)
7380 goto retry;
a2889657 7381
a2889657
JB
7382 /* Prevent various kinds of signals during display update.
7383 stdio is not robust about handling signals,
7384 which can cause an apparent I/O error. */
7385 if (interrupt_input)
7386 unrequest_sigio ();
7387 stop_polling ();
7388
5f5c8ee5 7389 if (consider_all_windows_p)
a2889657
JB
7390 {
7391 Lisp_Object tail;
7392
7393 pause = 0;
7394
44fa5b1e 7395 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 7396 {
5f5c8ee5 7397 struct frame *f;
a2889657 7398
e24c997d 7399 if (!FRAMEP (XCONS (tail)->car))
a2889657
JB
7400 continue;
7401
44fa5b1e 7402 f = XFRAME (XCONS (tail)->car);
1af9f229 7403
fd8ff63d 7404 if ((FRAME_WINDOW_P (f) || f == selected_frame)
f21ef775 7405 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 7406 {
5f5c8ee5
GM
7407 /* Mark all windows as to be updated. */
7408 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 7409 pause |= update_frame (f, 0, 0);
a2889657 7410 if (!pause)
efc63ef0 7411 {
5f5c8ee5
GM
7412 if (hscroll_windows (f->root_window))
7413 goto retry;
7414
efc63ef0
RS
7415 mark_window_display_accurate (f->root_window, 1);
7416 if (frame_up_to_date_hook != 0)
7417 (*frame_up_to_date_hook) (f);
7418 }
a2889657
JB
7419 }
7420 }
7421 }
7422 else
6e8290aa 7423 {
5f5c8ee5
GM
7424 if (FRAME_VISIBLE_P (selected_frame)
7425 && !FRAME_OBSCURED_P (selected_frame))
7426 {
7427 XWINDOW (selected_window)->must_be_updated_p = 1;
7428 pause = update_frame (selected_frame, 0, 0);
7429 if (!pause && hscroll_windows (selected_window))
7430 goto retry;
7431 }
4d641a15
KH
7432 else
7433 pause = 0;
d724d989 7434
8de2d90b 7435 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
7436 function. If the echo area is on another frame, that may
7437 have put text on a frame other than the selected one, so the
7438 above call to update_frame would not have caught it. Catch
8de2d90b
JB
7439 it here. */
7440 {
84faf44c 7441 Lisp_Object mini_window;
5f5c8ee5 7442 struct frame *mini_frame;
84faf44c
RS
7443
7444 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
7445 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 7446
fd8ff63d 7447 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
7448 {
7449 XWINDOW (mini_window)->must_be_updated_p = 1;
7450 pause |= update_frame (mini_frame, 0, 0);
7451 if (!pause && hscroll_windows (mini_window))
7452 goto retry;
7453 }
8de2d90b 7454 }
6e8290aa 7455 }
a2889657 7456
5f5c8ee5
GM
7457 /* If display was paused because of pending input, make sure we do a
7458 thorough update the next time. */
a2889657
JB
7459 if (pause)
7460 {
5f5c8ee5
GM
7461 /* Prevent the optimization at the beginning of
7462 redisplay_internal that tries a single-line update of the
7463 line containing the cursor in the selected window. */
7464 CHARPOS (this_line_start_pos) = 0;
7465
7466 /* Let the overlay arrow be updated the next time. */
265a9e55 7467 if (!NILP (last_arrow_position))
a2889657
JB
7468 {
7469 last_arrow_position = Qt;
7470 last_arrow_string = Qt;
7471 }
5f5c8ee5
GM
7472
7473 /* If we pause after scrolling, some rows in the current
7474 matrices of some windows are not valid. */
7475 if (!WINDOW_FULL_WIDTH_P (w)
7476 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
7477 update_mode_lines = 1;
7478 }
7479
5f5c8ee5
GM
7480 /* Now text on frame agrees with windows, so put info into the
7481 windows for partial redisplay to follow. */
a2889657
JB
7482 if (!pause)
7483 {
7484 register struct buffer *b = XBUFFER (w->buffer);
7485
9142dd5b
GM
7486 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7487 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7488 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7489 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 7490
5f5c8ee5 7491 if (consider_all_windows_p)
11e82b76 7492 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
a2889657
JB
7493 else
7494 {
5f5c8ee5
GM
7495 XSETFASTINT (w->last_point, BUF_PT (b));
7496 w->last_cursor = w->cursor;
7497 w->last_cursor_off_p = w->cursor_off_p;
7498
28995e67 7499 b->clip_changed = 0;
9142dd5b 7500 b->prevent_redisplay_optimizations_p = 0;
a2889657 7501 w->update_mode_line = Qnil;
c2213350 7502 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 7503 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
7504 w->last_had_star
7505 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7506 ? Qt : Qnil);
3ee4159a
RS
7507
7508 /* Record if we are showing a region, so can make sure to
7509 update it fully at next redisplay. */
7510 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
7511 && (w == XWINDOW (current_buffer->last_selected_window)
7512 || highlight_nonselected_windows)
3ee4159a
RS
7513 && !NILP (XBUFFER (w->buffer)->mark_active)
7514 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7515 : Qnil);
7516
d2f84654 7517 w->window_end_valid = w->buffer;
d45de95b 7518 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 7519 last_arrow_string = Voverlay_arrow_string;
efc63ef0
RS
7520 if (frame_up_to_date_hook != 0)
7521 (*frame_up_to_date_hook) (selected_frame);
9142dd5b
GM
7522
7523 w->current_matrix->buffer = b;
7524 w->current_matrix->begv = BUF_BEGV (b);
7525 w->current_matrix->zv = BUF_ZV (b);
a2889657 7526 }
5f5c8ee5 7527
a2889657
JB
7528 update_mode_lines = 0;
7529 windows_or_buffers_changed = 0;
7530 }
7531
5f5c8ee5
GM
7532 /* Start SIGIO interrupts coming again. Having them off during the
7533 code above makes it less likely one will discard output, but not
7534 impossible, since there might be stuff in the system buffer here.
a2889657 7535 But it is much hairier to try to do anything about that. */
a2889657
JB
7536 if (interrupt_input)
7537 request_sigio ();
7538 start_polling ();
7539
5f5c8ee5
GM
7540 /* If a frame has become visible which was not before, redisplay
7541 again, so that we display it. Expose events for such a frame
7542 (which it gets when becoming visible) don't call the parts of
7543 redisplay constructing glyphs, so simply exposing a frame won't
7544 display anything in this case. So, we have to display these
7545 frames here explicitly. */
11c52c4f
RS
7546 if (!pause)
7547 {
7548 Lisp_Object tail, frame;
7549 int new_count = 0;
7550
7551 FOR_EACH_FRAME (tail, frame)
7552 {
7553 int this_is_visible = 0;
8e83f802
RS
7554
7555 if (XFRAME (frame)->visible)
7556 this_is_visible = 1;
7557 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7558 if (XFRAME (frame)->visible)
7559 this_is_visible = 1;
11c52c4f
RS
7560
7561 if (this_is_visible)
7562 new_count++;
7563 }
7564
89819bdd 7565 if (new_count != number_of_visible_frames)
11c52c4f
RS
7566 windows_or_buffers_changed++;
7567 }
7568
44fa5b1e 7569 /* Change frame size now if a change is pending. */
c6e89d6c 7570 do_pending_window_change (1);
d8e242fd 7571
8b32d885
RS
7572 /* If we just did a pending size change, or have additional
7573 visible frames, redisplay again. */
3c8c72e0 7574 if (windows_or_buffers_changed && !pause)
8b32d885 7575 goto retry;
5f5c8ee5
GM
7576
7577 end_of_redisplay:;
c6e89d6c 7578
28514cd9 7579 unbind_to (count, Qnil);
a2889657
JB
7580}
7581
5f5c8ee5
GM
7582
7583/* Redisplay, but leave alone any recent echo area message unless
7584 another message has been requested in its place.
a2889657
JB
7585
7586 This is useful in situations where you need to redisplay but no
7587 user action has occurred, making it inappropriate for the message
7588 area to be cleared. See tracking_off and
7589 wait_reading_process_input for examples of these situations. */
7590
8991bb31 7591void
a2889657
JB
7592redisplay_preserve_echo_area ()
7593{
c6e89d6c 7594 if (!NILP (echo_area_buffer[1]))
a2889657 7595 {
c6e89d6c
GM
7596 /* We have a previously displayed message, but no current
7597 message. Redisplay the previous message. */
7598 display_last_displayed_message_p = 1;
e9874cee 7599 redisplay_internal (1);
c6e89d6c 7600 display_last_displayed_message_p = 0;
a2889657
JB
7601 }
7602 else
e9874cee 7603 redisplay_internal (1);
a2889657
JB
7604}
7605
5f5c8ee5 7606
28514cd9
GM
7607/* Function registered with record_unwind_protect in
7608 redisplay_internal. Clears the flag indicating that a redisplay is
7609 in progress. */
7610
7611static Lisp_Object
7612unwind_redisplay (old_redisplaying_p)
7613 Lisp_Object old_redisplaying_p;
7614{
7615 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 7616 return Qnil;
28514cd9
GM
7617}
7618
7619
5f5c8ee5
GM
7620/* Mark the display of windows in the window tree rooted at WINDOW as
7621 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7622 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7623 the next time redisplay_internal is called. */
7624
a2889657 7625void
5f5c8ee5 7626mark_window_display_accurate (window, accurate_p)
a2889657 7627 Lisp_Object window;
5f5c8ee5 7628 int accurate_p;
a2889657 7629{
5f5c8ee5
GM
7630 struct window *w;
7631
7632 for (; !NILP (window); window = w->next)
a2889657
JB
7633 {
7634 w = XWINDOW (window);
7635
5f5c8ee5 7636 if (BUFFERP (w->buffer))
bd66d1ba 7637 {
5f5c8ee5
GM
7638 struct buffer *b = XBUFFER (w->buffer);
7639
c2213350 7640 XSETFASTINT (w->last_modified,
5f5c8ee5 7641 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 7642 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
7643 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7644 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7645 ? Qt : Qnil);
bd66d1ba 7646
5f5c8ee5
GM
7647#if 0 /* I don't think this is necessary because display_line does it.
7648 Let's check it. */
bd66d1ba
RS
7649 /* Record if we are showing a region, so can make sure to
7650 update it fully at next redisplay. */
5f5c8ee5
GM
7651 w->region_showing
7652 = (!NILP (Vtransient_mark_mode)
7653 && (w == XWINDOW (current_buffer->last_selected_window)
7654 || highlight_nonselected_windows)
7655 && (!NILP (b->mark_active)
7656 ? Fmarker_position (b->mark)
7657 : Qnil));
7658#endif
7659
7660 if (accurate_p)
7661 {
7662 b->clip_changed = 0;
9142dd5b
GM
7663 b->prevent_redisplay_optimizations_p = 0;
7664 w->current_matrix->buffer = b;
7665 w->current_matrix->begv = BUF_BEGV (b);
7666 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
7667 w->last_cursor = w->cursor;
7668 w->last_cursor_off_p = w->cursor_off_p;
7669 if (w == XWINDOW (selected_window))
7670 w->last_point = BUF_PT (b);
7671 else
7672 w->last_point = XMARKER (w->pointm)->charpos;
7673 }
bd66d1ba
RS
7674 }
7675
d2f84654 7676 w->window_end_valid = w->buffer;
a2889657
JB
7677 w->update_mode_line = Qnil;
7678
265a9e55 7679 if (!NILP (w->vchild))
5f5c8ee5 7680 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 7681 if (!NILP (w->hchild))
5f5c8ee5 7682 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
7683 }
7684
5f5c8ee5 7685 if (accurate_p)
a2889657 7686 {
d45de95b 7687 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
7688 last_arrow_string = Voverlay_arrow_string;
7689 }
7690 else
7691 {
5f5c8ee5
GM
7692 /* Force a thorough redisplay the next time by setting
7693 last_arrow_position and last_arrow_string to t, which is
7694 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
7695 last_arrow_position = Qt;
7696 last_arrow_string = Qt;
7697 }
7698}
5f5c8ee5
GM
7699
7700
7701/* Return value in display table DP (Lisp_Char_Table *) for character
7702 C. Since a display table doesn't have any parent, we don't have to
7703 follow parent. Do not call this function directly but use the
7704 macro DISP_CHAR_VECTOR. */
7705
7706Lisp_Object
7707disp_char_vector (dp, c)
7708 struct Lisp_Char_Table *dp;
7709 int c;
7710{
7711 int code[4], i;
7712 Lisp_Object val;
7713
7714 if (SINGLE_BYTE_CHAR_P (c))
7715 return (dp->contents[c]);
7716
7717 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7718 if (code[0] != CHARSET_COMPOSITION)
7719 {
7720 if (code[1] < 32)
7721 code[1] = -1;
7722 else if (code[2] < 32)
7723 code[2] = -1;
7724 }
7725
7726 /* Here, the possible range of code[0] (== charset ID) is
7727 128..max_charset. Since the top level char table contains data
7728 for multibyte characters after 256th element, we must increment
7729 code[0] by 128 to get a correct index. */
7730 code[0] += 128;
7731 code[3] = -1; /* anchor */
7732
7733 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7734 {
7735 val = dp->contents[code[i]];
7736 if (!SUB_CHAR_TABLE_P (val))
7737 return (NILP (val) ? dp->defalt : val);
7738 }
7739
7740 /* Here, val is a sub char table. We return the default value of
7741 it. */
7742 return (dp->defalt);
7743}
7744
7745
a2889657 7746\f
5f5c8ee5
GM
7747/***********************************************************************
7748 Window Redisplay
7749 ***********************************************************************/
a2725ab2 7750
5f5c8ee5 7751/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
7752
7753static void
5f5c8ee5
GM
7754redisplay_windows (window)
7755 Lisp_Object window;
90adcf20 7756{
5f5c8ee5
GM
7757 while (!NILP (window))
7758 {
7759 struct window *w = XWINDOW (window);
7760
7761 if (!NILP (w->hchild))
7762 redisplay_windows (w->hchild);
7763 else if (!NILP (w->vchild))
7764 redisplay_windows (w->vchild);
7765 else
7766 redisplay_window (window, 0);
a2725ab2 7767
5f5c8ee5
GM
7768 window = w->next;
7769 }
7770}
7771
7772
7773/* Set cursor position of W. PT is assumed to be displayed in ROW.
7774 DELTA is the number of bytes by which positions recorded in ROW
7775 differ from current buffer positions. */
7776
7777void
7778set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7779 struct window *w;
7780 struct glyph_row *row;
7781 struct glyph_matrix *matrix;
7782 int delta, delta_bytes, dy, dvpos;
7783{
7784 struct glyph *glyph = row->glyphs[TEXT_AREA];
7785 struct glyph *end = glyph + row->used[TEXT_AREA];
7786 int x = row->x;
7787 int pt_old = PT - delta;
7788
7789 /* Skip over glyphs not having an object at the start of the row.
7790 These are special glyphs like truncation marks on terminal
7791 frames. */
7792 if (row->displays_text_p)
7793 while (glyph < end
7794 && !glyph->object
7795 && glyph->charpos < 0)
7796 {
7797 x += glyph->pixel_width;
7798 ++glyph;
7799 }
7800
7801 while (glyph < end
7802 && glyph->object
7803 && (!BUFFERP (glyph->object)
7804 || glyph->charpos < pt_old))
7805 {
7806 x += glyph->pixel_width;
7807 ++glyph;
7808 }
7809
7810 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7811 w->cursor.x = x;
7812 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7813 w->cursor.y = row->y + dy;
7814
7815 if (w == XWINDOW (selected_window))
7816 {
7817 if (!row->continued_p
7818 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7819 && row->x == 0)
7820 {
7821 this_line_buffer = XBUFFER (w->buffer);
7822
7823 CHARPOS (this_line_start_pos)
7824 = MATRIX_ROW_START_CHARPOS (row) + delta;
7825 BYTEPOS (this_line_start_pos)
7826 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7827
7828 CHARPOS (this_line_end_pos)
7829 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7830 BYTEPOS (this_line_end_pos)
7831 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7832
7833 this_line_y = w->cursor.y;
7834 this_line_pixel_height = row->height;
7835 this_line_vpos = w->cursor.vpos;
7836 this_line_start_x = row->x;
7837 }
7838 else
7839 CHARPOS (this_line_start_pos) = 0;
7840 }
7841}
7842
7843
7844/* Run window scroll functions, if any, for WINDOW with new window
7845 start STARTP. Sets the window start of WINDOW to that position. */
7846
7847static INLINE struct text_pos
7848run_window_scroll_functions (window, startp)
7849 Lisp_Object window;
7850 struct text_pos startp;
7851{
7852 struct window *w = XWINDOW (window);
7853 SET_MARKER_FROM_TEXT_POS (w->start, startp);
90adcf20 7854
5f5c8ee5
GM
7855 if (!NILP (Vwindow_scroll_functions))
7856 {
7857 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7858 make_number (CHARPOS (startp)));
7859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7860 }
90adcf20 7861
5f5c8ee5
GM
7862 return startp;
7863}
7864
7865
7866/* Modify the desired matrix of window W and W->vscroll so that the
7867 line containing the cursor is fully visible. */
7868
7869static void
7870make_cursor_line_fully_visible (w)
7871 struct window *w;
7872{
7873 struct glyph_matrix *matrix;
7874 struct glyph_row *row;
7875 int top_line_height;
7876
7877 /* It's not always possible to find the cursor, e.g, when a window
7878 is full of overlay strings. Don't do anything in that case. */
7879 if (w->cursor.vpos < 0)
7880 return;
7881
7882 matrix = w->desired_matrix;
7883 row = MATRIX_ROW (matrix, w->cursor.vpos);
7884
7885 /* If row->y == top y of window display area, the window isn't tall
7886 enough to display a single line. There is nothing we can do
7887 about it. */
7888 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
7889 if (row->y == top_line_height)
7890 return;
7891
7892 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7893 {
7894 int dy = row->height - row->visible_height;
7895 w->vscroll = 0;
7896 w->cursor.y += dy;
7897 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7898 }
7899 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7900 {
7901 int dy = - (row->height - row->visible_height);
7902 w->vscroll = dy;
7903 w->cursor.y += dy;
7904 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7905 }
7906
7907 /* When we change the cursor y-position of the selected window,
7908 change this_line_y as well so that the display optimization for
7909 the cursor line of the selected window in redisplay_internal uses
7910 the correct y-position. */
7911 if (w == XWINDOW (selected_window))
7912 this_line_y = w->cursor.y;
7913}
7914
7915
7916/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7917 non-zero means only WINDOW is redisplayed in redisplay_internal.
7918 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7919 in redisplay_window to bring a partially visible line into view in
7920 the case that only the cursor has moved.
7921
7922 Value is
7923
7924 1 if scrolling succeeded
7925
7926 0 if scrolling didn't find point.
7927
7928 -1 if new fonts have been loaded so that we must interrupt
7929 redisplay, adjust glyph matrices, and try again. */
7930
7931static int
7932try_scrolling (window, just_this_one_p, scroll_conservatively,
7933 scroll_step, temp_scroll_step)
7934 Lisp_Object window;
7935 int just_this_one_p;
7936 int scroll_conservatively, scroll_step;
7937 int temp_scroll_step;
7938{
7939 struct window *w = XWINDOW (window);
7940 struct frame *f = XFRAME (w->frame);
7941 struct text_pos scroll_margin_pos;
7942 struct text_pos pos;
7943 struct text_pos startp;
7944 struct it it;
7945 Lisp_Object window_end;
7946 int this_scroll_margin;
7947 int dy = 0;
7948 int scroll_max;
7949 int line_height, rc;
7950 int amount_to_scroll = 0;
7951 Lisp_Object aggressive;
7952 int height;
7953
7954#if GLYPH_DEBUG
7955 debug_method_add (w, "try_scrolling");
78614721 7956#endif
5f5c8ee5
GM
7957
7958 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7959
7960 /* Compute scroll margin height in pixels. We scroll when point is
7961 within this distance from the top or bottom of the window. */
7962 if (scroll_margin > 0)
90adcf20 7963 {
5f5c8ee5
GM
7964 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
7965 this_scroll_margin *= CANON_Y_UNIT (f);
7966 }
7967 else
7968 this_scroll_margin = 0;
7969
7970 /* Compute how much we should try to scroll maximally to bring point
7971 into view. */
7972 if (scroll_step)
7973 scroll_max = scroll_step;
7974 else if (scroll_conservatively)
7975 scroll_max = scroll_conservatively;
7976 else if (temp_scroll_step)
7977 scroll_max = temp_scroll_step;
7978 else if (NUMBERP (current_buffer->scroll_down_aggressively)
7979 || NUMBERP (current_buffer->scroll_up_aggressively))
7980 /* We're trying to scroll because of aggressive scrolling
7981 but no scroll_step is set. Choose an arbitrary one. Maybe
7982 there should be a variable for this. */
7983 scroll_max = 10;
7984 else
7985 scroll_max = 0;
7986 scroll_max *= CANON_Y_UNIT (f);
7987
7988 /* Decide whether we have to scroll down. Start at the window end
7989 and move this_scroll_margin up to find the position of the scroll
7990 margin. */
7991 window_end = Fwindow_end (window, Qt);
7992 CHARPOS (scroll_margin_pos) = XINT (window_end);
7993 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
7994 if (this_scroll_margin)
7995 {
7996 start_display (&it, w, scroll_margin_pos);
7997 move_it_vertically (&it, - this_scroll_margin);
7998 scroll_margin_pos = it.current.pos;
7999 }
8000
8001 if (PT >= CHARPOS (scroll_margin_pos))
8002 {
8003 int y0;
8004
8005 /* Point is in the scroll margin at the bottom of the window, or
8006 below. Compute a new window start that makes point visible. */
8007
8008 /* Compute the distance from the scroll margin to PT.
8009 Give up if the distance is greater than scroll_max. */
8010 start_display (&it, w, scroll_margin_pos);
8011 y0 = it.current_y;
8012 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8013 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8014 line_height = (it.max_ascent + it.max_descent
8015 ? it.max_ascent + it.max_descent
8016 : last_height);
8017 dy = it.current_y + line_height - y0;
8018 if (dy > scroll_max)
8019 return 0;
8020
8021 /* Move the window start down. If scrolling conservatively,
8022 move it just enough down to make point visible. If
8023 scroll_step is set, move it down by scroll_step. */
8024 start_display (&it, w, startp);
8025
8026 if (scroll_conservatively)
8027 amount_to_scroll = dy;
8028 else if (scroll_step || temp_scroll_step)
8029 amount_to_scroll = scroll_max;
8030 else
90adcf20 8031 {
5f5c8ee5
GM
8032 aggressive = current_buffer->scroll_down_aggressively;
8033 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8034 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
8035 if (NUMBERP (aggressive))
8036 amount_to_scroll = XFLOATINT (aggressive) * height;
8037 }
a2725ab2 8038
5f5c8ee5
GM
8039 if (amount_to_scroll <= 0)
8040 return 0;
a2725ab2 8041
5f5c8ee5
GM
8042 move_it_vertically (&it, amount_to_scroll);
8043 startp = it.current.pos;
8044 }
8045 else
8046 {
8047 /* See if point is inside the scroll margin at the top of the
8048 window. */
8049 scroll_margin_pos = startp;
8050 if (this_scroll_margin)
8051 {
8052 start_display (&it, w, startp);
8053 move_it_vertically (&it, this_scroll_margin);
8054 scroll_margin_pos = it.current.pos;
8055 }
8056
8057 if (PT < CHARPOS (scroll_margin_pos))
8058 {
8059 /* Point is in the scroll margin at the top of the window or
8060 above what is displayed in the window. */
8061 int y0;
8062
8063 /* Compute the vertical distance from PT to the scroll
8064 margin position. Give up if distance is greater than
8065 scroll_max. */
8066 SET_TEXT_POS (pos, PT, PT_BYTE);
8067 start_display (&it, w, pos);
8068 y0 = it.current_y;
8069 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8070 it.last_visible_y, -1,
8071 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8072 dy = it.current_y - y0;
8073 if (dy > scroll_max)
8074 return 0;
8075
8076 /* Compute new window start. */
8077 start_display (&it, w, startp);
8078
8079 if (scroll_conservatively)
8080 amount_to_scroll = dy;
8081 else if (scroll_step || temp_scroll_step)
8082 amount_to_scroll = scroll_max;
538f13d4 8083 else
5f5c8ee5
GM
8084 {
8085 aggressive = current_buffer->scroll_up_aggressively;
8086 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8087 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
8088 if (NUMBERP (aggressive))
8089 amount_to_scroll = XFLOATINT (aggressive) * height;
8090 }
a2725ab2 8091
5f5c8ee5
GM
8092 if (amount_to_scroll <= 0)
8093 return 0;
8094
8095 move_it_vertically (&it, - amount_to_scroll);
8096 startp = it.current.pos;
90adcf20
RS
8097 }
8098 }
a2889657 8099
5f5c8ee5
GM
8100 /* Run window scroll functions. */
8101 startp = run_window_scroll_functions (window, startp);
90adcf20 8102
5f5c8ee5
GM
8103 /* Display the window. Give up if new fonts are loaded, or if point
8104 doesn't appear. */
8105 if (!try_window (window, startp))
8106 rc = -1;
8107 else if (w->cursor.vpos < 0)
8108 {
8109 clear_glyph_matrix (w->desired_matrix);
8110 rc = 0;
8111 }
8112 else
8113 {
8114 /* Maybe forget recorded base line for line number display. */
8115 if (!just_this_one_p
8116 || current_buffer->clip_changed
9142dd5b 8117 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
8118 w->base_line_number = Qnil;
8119
8120 /* If cursor ends up on a partially visible line, shift display
8121 lines up or down. */
8122 make_cursor_line_fully_visible (w);
8123 rc = 1;
8124 }
8125
8126 return rc;
a2889657
JB
8127}
8128
5f5c8ee5
GM
8129
8130/* Compute a suitable window start for window W if display of W starts
8131 on a continuation line. Value is non-zero if a new window start
8132 was computed.
8133
8134 The new window start will be computed, based on W's width, starting
8135 from the start of the continued line. It is the start of the
8136 screen line with the minimum distance from the old start W->start. */
8137
8138static int
8139compute_window_start_on_continuation_line (w)
8140 struct window *w;
1f1ff51d 8141{
5f5c8ee5
GM
8142 struct text_pos pos, start_pos;
8143 int window_start_changed_p = 0;
1f1ff51d 8144
5f5c8ee5 8145 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 8146
5f5c8ee5
GM
8147 /* If window start is on a continuation line... Window start may be
8148 < BEGV in case there's invisible text at the start of the
8149 buffer (M-x rmail, for example). */
8150 if (CHARPOS (start_pos) > BEGV
8151 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 8152 {
5f5c8ee5
GM
8153 struct it it;
8154 struct glyph_row *row;
8155
8156 /* Find the start of the continued line. This should be fast
8157 because scan_buffer is fast (newline cache). */
8158 row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
8159 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8160 row, DEFAULT_FACE_ID);
8161 reseat_at_previous_visible_line_start (&it);
8162
8163 /* If the line start is "too far" away from the window start,
8164 say it takes too much time to compute a new window start. */
8165 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8166 < XFASTINT (w->height) * XFASTINT (w->width))
8167 {
8168 int min_distance, distance;
8169
8170 /* Move forward by display lines to find the new window
8171 start. If window width was enlarged, the new start can
8172 be expected to be > the old start. If window width was
8173 decreased, the new window start will be < the old start.
8174 So, we're looking for the display line start with the
8175 minimum distance from the old window start. */
8176 pos = it.current.pos;
8177 min_distance = INFINITY;
8178 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8179 distance < min_distance)
8180 {
8181 min_distance = distance;
8182 pos = it.current.pos;
8183 move_it_by_lines (&it, 1, 0);
8184 }
8185
8186 /* Set the window start there. */
8187 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8188 window_start_changed_p = 1;
8189 }
1f1ff51d 8190 }
5f5c8ee5
GM
8191
8192 return window_start_changed_p;
1f1ff51d
KH
8193}
8194
5f5c8ee5
GM
8195
8196/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8197 selected_window is redisplayed. */
90adcf20 8198
a2889657 8199static void
5f5c8ee5 8200redisplay_window (window, just_this_one_p)
a2889657 8201 Lisp_Object window;
5f5c8ee5 8202 int just_this_one_p;
a2889657 8203{
5f5c8ee5
GM
8204 struct window *w = XWINDOW (window);
8205 struct frame *f = XFRAME (w->frame);
8206 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 8207 struct buffer *old = current_buffer;
5f5c8ee5 8208 struct text_pos lpoint, opoint, startp;
e481f960 8209 int update_mode_line;
5f5c8ee5
GM
8210 int tem;
8211 struct it it;
8212 /* Record it now because it's overwritten. */
8213 int current_matrix_up_to_date_p = 0;
5ba50c51 8214 int really_switched_buffer = 0;
5f5c8ee5 8215 int temp_scroll_step = 0;
2e54982e 8216 int count = specpdl_ptr - specpdl;
a2889657 8217
5f5c8ee5
GM
8218 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8219 opoint = lpoint;
a2889657 8220
5f5c8ee5
GM
8221 /* W must be a leaf window here. */
8222 xassert (!NILP (w->buffer));
8223#if GLYPH_DEBUG
8224 *w->desired_matrix->method = 0;
8225#endif
2e54982e
RS
8226
8227 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
8228
8229 reconsider_clip_changes (w, buffer);
8230
5f5c8ee5
GM
8231 /* Has the mode line to be updated? */
8232 update_mode_line = (!NILP (w->update_mode_line)
8233 || update_mode_lines
8234 || buffer->clip_changed);
8de2d90b
JB
8235
8236 if (MINI_WINDOW_P (w))
8237 {
5f5c8ee5 8238 if (w == XWINDOW (echo_area_window)
c6e89d6c 8239 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
8240 {
8241 if (update_mode_line)
8242 /* We may have to update a tty frame's menu bar or a
8243 toolbar. Example `M-x C-h C-h C-g'. */
8244 goto finish_menu_bars;
8245 else
8246 /* We've already displayed the echo area glyphs in this window. */
8247 goto finish_scroll_bars;
8248 }
73af359d 8249 else if (w != XWINDOW (minibuf_window))
8de2d90b 8250 {
5f5c8ee5
GM
8251 /* W is a mini-buffer window, but it's not the currently
8252 active one, so clear it. */
8253 int yb = window_text_bottom_y (w);
8254 struct glyph_row *row;
8255 int y;
8256
8257 for (y = 0, row = w->desired_matrix->rows;
8258 y < yb;
8259 y += row->height, ++row)
8260 blank_row (w, row, y);
88f22aff 8261 goto finish_scroll_bars;
8de2d90b
JB
8262 }
8263 }
a2889657 8264
5f5c8ee5
GM
8265 /* Otherwise set up data on this window; select its buffer and point
8266 value. */
e481f960 8267 if (update_mode_line)
5ba50c51 8268 {
5f5c8ee5
GM
8269 /* Really select the buffer, for the sake of buffer-local
8270 variables. */
5ba50c51
RS
8271 set_buffer_internal_1 (XBUFFER (w->buffer));
8272 really_switched_buffer = 1;
8273 }
e481f960
RS
8274 else
8275 set_buffer_temp (XBUFFER (w->buffer));
5f5c8ee5
GM
8276 SET_TEXT_POS (opoint, PT, PT_BYTE);
8277
8278 current_matrix_up_to_date_p
8279 = (!NILP (w->window_end_valid)
8280 && !current_buffer->clip_changed
8281 && XFASTINT (w->last_modified) >= MODIFF
8282 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 8283
5f5c8ee5
GM
8284 /* When windows_or_buffers_changed is non-zero, we can't rely on
8285 the window end being valid, so set it to nil there. */
8286 if (windows_or_buffers_changed)
8287 {
8288 /* If window starts on a continuation line, maybe adjust the
8289 window start in case the window's width changed. */
8290 if (XMARKER (w->start)->buffer == current_buffer)
8291 compute_window_start_on_continuation_line (w);
8292
8293 w->window_end_valid = Qnil;
8294 }
12adba34 8295
5f5c8ee5
GM
8296 /* Some sanity checks. */
8297 CHECK_WINDOW_END (w);
8298 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 8299 abort ();
5f5c8ee5 8300 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 8301 abort ();
a2889657 8302
28995e67
RS
8303 /* If %c is in mode line, update it if needed. */
8304 if (!NILP (w->column_number_displayed)
8305 /* This alternative quickly identifies a common case
8306 where no change is needed. */
8307 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8308 && XFASTINT (w->last_modified) >= MODIFF
8309 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8310 && XFASTINT (w->column_number_displayed) != current_column ())
8311 update_mode_line = 1;
8312
5f5c8ee5
GM
8313 /* Count number of windows showing the selected buffer. An indirect
8314 buffer counts as its base buffer. */
8315 if (!just_this_one_p)
42640f83
RS
8316 {
8317 struct buffer *current_base, *window_base;
8318 current_base = current_buffer;
8319 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8320 if (current_base->base_buffer)
8321 current_base = current_base->base_buffer;
8322 if (window_base->base_buffer)
8323 window_base = window_base->base_buffer;
8324 if (current_base == window_base)
8325 buffer_shared++;
8326 }
a2889657 8327
5f5c8ee5
GM
8328 /* Point refers normally to the selected window. For any other
8329 window, set up appropriate value. */
a2889657
JB
8330 if (!EQ (window, selected_window))
8331 {
12adba34
RS
8332 int new_pt = XMARKER (w->pointm)->charpos;
8333 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 8334 if (new_pt < BEGV)
a2889657 8335 {
f67a0f51 8336 new_pt = BEGV;
12adba34
RS
8337 new_pt_byte = BEGV_BYTE;
8338 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 8339 }
f67a0f51 8340 else if (new_pt > (ZV - 1))
a2889657 8341 {
f67a0f51 8342 new_pt = ZV;
12adba34
RS
8343 new_pt_byte = ZV_BYTE;
8344 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 8345 }
5f5c8ee5 8346
f67a0f51 8347 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 8348 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
8349 }
8350
f4faa47c 8351 /* If any of the character widths specified in the display table
5f5c8ee5
GM
8352 have changed, invalidate the width run cache. It's true that
8353 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
8354 redisplay goes (non-fatally) haywire when the display table is
8355 changed, so why should we worry about doing any better? */
8356 if (current_buffer->width_run_cache)
8357 {
f908610f 8358 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
8359
8360 if (! disptab_matches_widthtab (disptab,
8361 XVECTOR (current_buffer->width_table)))
8362 {
8363 invalidate_region_cache (current_buffer,
8364 current_buffer->width_run_cache,
8365 BEG, Z);
8366 recompute_width_table (current_buffer, disptab);
8367 }
8368 }
8369
a2889657 8370 /* If window-start is screwed up, choose a new one. */
a2889657
JB
8371 if (XMARKER (w->start)->buffer != current_buffer)
8372 goto recenter;
8373
5f5c8ee5 8374 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 8375
cf0df6ab
RS
8376 /* If someone specified a new starting point but did not insist,
8377 check whether it can be used. */
5f5c8ee5 8378 if (!NILP (w->optional_new_start))
cf0df6ab
RS
8379 {
8380 w->optional_new_start = Qnil;
5f5c8ee5
GM
8381 /* This takes a mini-buffer prompt into account. */
8382 start_display (&it, w, startp);
8383 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8384 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8385 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
8386 w->force_start = Qt;
8387 }
8388
8de2d90b 8389 /* Handle case where place to start displaying has been specified,
aa6d10fa 8390 unless the specified location is outside the accessible range. */
265a9e55 8391 if (!NILP (w->force_start))
a2889657 8392 {
e63574d7 8393 w->force_start = Qnil;
5f5c8ee5 8394 w->vscroll = 0;
b5174a51 8395 w->window_end_valid = Qnil;
5f5c8ee5
GM
8396
8397 /* Forget any recorded base line for line number display. */
8398 if (!current_matrix_up_to_date_p
8399 || current_buffer->clip_changed)
8400 w->base_line_number = Qnil;
8401
75c43375
RS
8402 /* Redisplay the mode line. Select the buffer properly for that.
8403 Also, run the hook window-scroll-functions
8404 because we have scrolled. */
e63574d7
RS
8405 /* Note, we do this after clearing force_start because
8406 if there's an error, it is better to forget about force_start
8407 than to get into an infinite loop calling the hook functions
8408 and having them get more errors. */
75c43375
RS
8409 if (!update_mode_line
8410 || ! NILP (Vwindow_scroll_functions))
e481f960 8411 {
5ba50c51
RS
8412 if (!really_switched_buffer)
8413 {
8414 set_buffer_temp (old);
8415 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8416 really_switched_buffer = 1;
5ba50c51 8417 }
5f5c8ee5 8418
e481f960
RS
8419 update_mode_line = 1;
8420 w->update_mode_line = Qt;
5f5c8ee5 8421 startp = run_window_scroll_functions (window, startp);
e481f960 8422 }
5f5c8ee5 8423
c2213350 8424 XSETFASTINT (w->last_modified, 0);
8850a573 8425 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
8426 if (CHARPOS (startp) < BEGV)
8427 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8428 else if (CHARPOS (startp) > ZV)
8429 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8430
8431 /* Redisplay, then check if cursor has been set during the
8432 redisplay. Give up if new fonts were loaded. */
8433 if (!try_window (window, startp))
8434 {
8435 w->force_start = Qt;
8436 clear_glyph_matrix (w->desired_matrix);
8437 goto restore_buffers;
8438 }
8439
8440 if (w->cursor.vpos < 0)
8441 {
8442 /* If point does not appear, or on a line that is not fully
8443 visible, move point so it does appear. The desired
8444 matrix has been built above, so we can use it. */
8445 int height = window_box_height (w) / 2;
8446 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8447
8448 while (row->y < height)
8449 ++row;
8450
8451 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8452 MATRIX_ROW_START_BYTEPOS (row));
8453
90adcf20 8454 if (w != XWINDOW (selected_window))
12adba34 8455 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
8456 else if (current_buffer == old)
8457 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8458
8459 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8460
8461 /* If we are highlighting the region, then we just changed
8462 the region, so redisplay to show it. */
df0b5ea1
RS
8463 if (!NILP (Vtransient_mark_mode)
8464 && !NILP (current_buffer->mark_active))
6f27fa9b 8465 {
5f5c8ee5
GM
8466 clear_glyph_matrix (w->desired_matrix);
8467 if (!try_window (window, startp))
8468 goto restore_buffers;
6f27fa9b 8469 }
a2889657 8470 }
5f5c8ee5
GM
8471
8472 make_cursor_line_fully_visible (w);
8473#if GLYPH_DEBUG
8474 debug_method_add (w, "forced window start");
8475#endif
a2889657
JB
8476 goto done;
8477 }
8478
5f5c8ee5
GM
8479 /* Handle case where text has not changed, only point, and it has
8480 not moved off the frame. */
8481 if (current_matrix_up_to_date_p
8482 /* Point may be in this window. */
8483 && PT >= CHARPOS (startp)
8484 /* If we don't check this, we are called to move the cursor in a
8485 horizontally split window with a current matrix that doesn't
8486 fit the display. */
8487 && !windows_or_buffers_changed
8488 /* Selective display hasn't changed. */
8489 && !current_buffer->clip_changed
b1aa6cb3
RS
8490 /* If force-mode-line-update was called, really redisplay;
8491 that's how redisplay is forced after e.g. changing
8492 buffer-invisibility-spec. */
632ab665 8493 && NILP (w->update_mode_line)
5f5c8ee5
GM
8494 /* Can't use this case if highlighting a region. When a
8495 region exists, cursor movement has to do more than just
8496 set the cursor. */
8497 && !(!NILP (Vtransient_mark_mode)
8498 && !NILP (current_buffer->mark_active))
bd66d1ba 8499 && NILP (w->region_showing)
8f897821 8500 && NILP (Vshow_trailing_whitespace)
5f5c8ee5
GM
8501 /* Right after splitting windows, last_point may be nil. */
8502 && INTEGERP (w->last_point)
8503 /* This code is not used for mini-buffer for the sake of the case
8504 of redisplaying to replace an echo area message; since in
8505 that case the mini-buffer contents per se are usually
8506 unchanged. This code is of no real use in the mini-buffer
8507 since the handling of this_line_start_pos, etc., in redisplay
8508 handles the same cases. */
d45de95b 8509 && !EQ (window, minibuf_window)
5f5c8ee5
GM
8510 /* When splitting windows or for new windows, it happens that
8511 redisplay is called with a nil window_end_vpos or one being
8512 larger than the window. This should really be fixed in
8513 window.c. I don't have this on my list, now, so we do
8514 approximately the same as the old redisplay code. --gerd. */
8515 && INTEGERP (w->window_end_vpos)
8516 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8517 && (FRAME_WINDOW_P (f)
8518 || !MARKERP (Voverlay_arrow_position)
377dbd97 8519 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 8520 {
5f5c8ee5
GM
8521 int this_scroll_margin;
8522 struct glyph_row *row;
8523 int scroll_p;
a2889657 8524
5f5c8ee5
GM
8525#if GLYPH_DEBUG
8526 debug_method_add (w, "cursor movement");
8527#endif
9afd2168 8528
5f5c8ee5
GM
8529 /* Scroll if point within this distance from the top or bottom
8530 of the window. This is a pixel value. */
8531 this_scroll_margin = max (0, scroll_margin);
8532 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8533 this_scroll_margin *= CANON_Y_UNIT (f);
8534
8535 /* Start with the row the cursor was displayed during the last
8536 not paused redisplay. Give up if that row is not valid. */
8537 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8538 goto try_to_scroll;
8539 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8540 if (row->mode_line_p)
8541 ++row;
8542 if (!row->enabled_p)
8543 goto try_to_scroll;
8544
8545 scroll_p = 0;
8546 if (PT > XFASTINT (w->last_point))
8547 {
8548 /* Point has moved forward. */
8549 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8550
8551 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8552 /* The end position of a row equals the start
8553 position of the next row. If PT is there, we
8554 would rather display it in the next line, except
8555 when this line ends in ZV. */
8556 || (MATRIX_ROW_END_CHARPOS (row) == PT
8557 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8558 || !row->ends_at_zv_p)))
8559 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8560 {
8561 xassert (row->enabled_p);
8562 ++row;
8563 }
9afd2168 8564
5f5c8ee5
GM
8565 /* If within the scroll margin, scroll. Note that
8566 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8567 next line would be drawn, and that this_scroll_margin can
8568 be zero. */
8569 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8570 || PT > MATRIX_ROW_END_CHARPOS (row)
8571 /* Line is completely visible last line in window and PT
8572 is to be set in the next line. */
8573 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8574 && PT == MATRIX_ROW_END_CHARPOS (row)
8575 && !row->ends_at_zv_p
8576 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8577 scroll_p = 1;
8578 }
8579 else if (PT < XFASTINT (w->last_point))
a2889657 8580 {
5f5c8ee5
GM
8581 /* Cursor has to be moved backward. Note that PT >=
8582 CHARPOS (startp) because of the outer if-statement. */
8583 while (!row->mode_line_p
8584 && (MATRIX_ROW_START_CHARPOS (row) > PT
8585 || (MATRIX_ROW_START_CHARPOS (row) == PT
8586 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8587 && (row->y > this_scroll_margin
8588 || CHARPOS (startp) == BEGV))
a2889657 8589 {
5f5c8ee5
GM
8590 xassert (row->enabled_p);
8591 --row;
a2889657 8592 }
abb4c08f 8593
5f5c8ee5
GM
8594 /* Consider the following case: Window starts at BEGV, there
8595 is invisible, intangible text at BEGV, so that display
8596 starts at some point START > BEGV. It can happen that
8597 we are called with PT somewhere between BEGV and START.
8598 Try to handle that case. */
8599 if (row < w->current_matrix->rows
8600 || row->mode_line_p)
8601 {
8602 row = w->current_matrix->rows;
8603 if (row->mode_line_p)
8604 ++row;
8605 }
8606
8607 /* Due to newlines in overlay strings, we may have to skip
8608 forward over overlay strings. */
8609 while (MATRIX_ROW_END_CHARPOS (row) == PT
8610 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8611 && !row->ends_at_zv_p)
8612 ++row;
8613
8614 /* If within the scroll margin, scroll. */
8615 if (row->y < this_scroll_margin
8616 && CHARPOS (startp) != BEGV)
8617 scroll_p = 1;
8618 }
8619
8620 /* if PT is not in the glyph row, give up. */
8621 if (PT < MATRIX_ROW_START_CHARPOS (row)
8622 || PT > MATRIX_ROW_END_CHARPOS (row))
8623 goto try_to_scroll;
8624
8625 /* If we end up in a partially visible line, let's make it fully
8626 visible. This can be done most easily by using the existing
8627 scrolling code. */
8628 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8629 {
8630 temp_scroll_step = 1;
8631 goto try_to_scroll;
a2889657 8632 }
5f5c8ee5
GM
8633 else if (scroll_p)
8634 goto try_to_scroll;
8635
8636 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8637 goto done;
a2889657 8638 }
5f5c8ee5 8639
a2889657
JB
8640 /* If current starting point was originally the beginning of a line
8641 but no longer is, find a new starting point. */
265a9e55 8642 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
8643 && !(CHARPOS (startp) <= BEGV
8644 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 8645 {
5f5c8ee5
GM
8646#if GLYPH_DEBUG
8647 debug_method_add (w, "recenter 1");
8648#endif
a2889657
JB
8649 goto recenter;
8650 }
5f5c8ee5
GM
8651
8652 /* Try scrolling with try_window_id. */
9142dd5b
GM
8653 else if (/* Windows and buffers haven't changed. */
8654 !windows_or_buffers_changed
5f5c8ee5
GM
8655 /* Window must be either use window-based redisplay or
8656 be full width. */
8657 && (FRAME_WINDOW_P (f)
c59c668a 8658 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
8659 && !MINI_WINDOW_P (w)
8660 /* Point is not known NOT to appear in window. */
8661 && PT >= CHARPOS (startp)
a2889657 8662 && XFASTINT (w->last_modified)
5f5c8ee5
GM
8663 /* Window is not hscrolled. */
8664 && XFASTINT (w->hscroll) == 0
8665 /* Selective display has not changed. */
8666 && !current_buffer->clip_changed
8667 /* Current matrix is up to date. */
8668 && !NILP (w->window_end_valid)
8669 /* Can't use this case if highlighting a region because
8670 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
8671 && !(!NILP (Vtransient_mark_mode)
8672 && !NILP (current_buffer->mark_active))
8673 && NILP (w->region_showing)
8f897821 8674 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 8675 /* Overlay arrow position and string not changed. */
d45de95b 8676 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 8677 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
8678 /* Value is > 0 if update has been done, it is -1 if we
8679 know that the same window start will not work. It is 0
8680 if unsuccessful for some other reason. */
8681 && (tem = try_window_id (w)) != 0)
a2889657 8682 {
5f5c8ee5
GM
8683#if GLYPH_DEBUG
8684 debug_method_add (w, "try_window_id");
8685#endif
8686
8687 if (fonts_changed_p)
8688 goto restore_buffers;
a2889657
JB
8689 if (tem > 0)
8690 goto done;
5f5c8ee5
GM
8691 /* Otherwise try_window_id has returned -1 which means that we
8692 don't want the alternative below this comment to execute. */
a2889657 8693 }
5f5c8ee5
GM
8694 else if (CHARPOS (startp) >= BEGV
8695 && CHARPOS (startp) <= ZV
8696 && PT >= CHARPOS (startp)
8697 && (CHARPOS (startp) < ZV
e9874cee 8698 /* Avoid starting at end of buffer. */
5f5c8ee5 8699 || CHARPOS (startp) == BEGV
8850a573
RS
8700 || (XFASTINT (w->last_modified) >= MODIFF
8701 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 8702 {
5f5c8ee5
GM
8703#if GLYPH_DEBUG
8704 debug_method_add (w, "same window start");
8705#endif
8706
8707 /* Try to redisplay starting at same place as before.
8708 If point has not moved off frame, accept the results. */
8709 if (!current_matrix_up_to_date_p
8710 /* Don't use try_window_reusing_current_matrix in this case
8711 because it can have changed the buffer. */
8712 || !NILP (Vwindow_scroll_functions)
8713 || MINI_WINDOW_P (w)
8714 || !try_window_reusing_current_matrix (w))
8715 {
8716 IF_DEBUG (debug_method_add (w, "1"));
8717 try_window (window, startp);
8718 }
8719
8720 if (fonts_changed_p)
8721 goto restore_buffers;
8722
8723 if (w->cursor.vpos >= 0)
aa6d10fa 8724 {
5f5c8ee5
GM
8725 if (!just_this_one_p
8726 || current_buffer->clip_changed
9142dd5b 8727 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
8728 /* Forget any recorded base line for line number display. */
8729 w->base_line_number = Qnil;
5f5c8ee5
GM
8730
8731 make_cursor_line_fully_visible (w);
aa6d10fa
RS
8732 goto done;
8733 }
a2889657 8734 else
5f5c8ee5 8735 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
8736 }
8737
5f5c8ee5
GM
8738 try_to_scroll:
8739
c2213350 8740 XSETFASTINT (w->last_modified, 0);
8850a573 8741 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 8742
e481f960
RS
8743 /* Redisplay the mode line. Select the buffer properly for that. */
8744 if (!update_mode_line)
8745 {
5ba50c51
RS
8746 if (!really_switched_buffer)
8747 {
8748 set_buffer_temp (old);
8749 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8750 really_switched_buffer = 1;
5ba50c51 8751 }
e481f960
RS
8752 update_mode_line = 1;
8753 w->update_mode_line = Qt;
8754 }
a2889657 8755
5f5c8ee5
GM
8756 /* Try to scroll by specified few lines. */
8757 if ((scroll_conservatively
8758 || scroll_step
8759 || temp_scroll_step
8760 || NUMBERP (current_buffer->scroll_up_aggressively)
8761 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 8762 && !current_buffer->clip_changed
5f5c8ee5
GM
8763 && CHARPOS (startp) >= BEGV
8764 && CHARPOS (startp) <= ZV)
0789adb2 8765 {
5f5c8ee5
GM
8766 /* The function returns -1 if new fonts were loaded, 1 if
8767 successful, 0 if not successful. */
8768 int rc = try_scrolling (window, just_this_one_p,
8769 scroll_conservatively,
8770 scroll_step,
8771 temp_scroll_step);
8772 if (rc > 0)
8773 goto done;
8774 else if (rc < 0)
8775 goto restore_buffers;
8776 }
f9c8af06 8777
5f5c8ee5 8778 /* Finally, just choose place to start which centers point */
5936754e 8779
5f5c8ee5 8780 recenter:
44173109 8781
5f5c8ee5
GM
8782#if GLYPH_DEBUG
8783 debug_method_add (w, "recenter");
8784#endif
0789adb2 8785
5f5c8ee5 8786 /* w->vscroll = 0; */
0789adb2 8787
5f5c8ee5
GM
8788 /* Forget any previously recorded base line for line number display. */
8789 if (!current_matrix_up_to_date_p
8790 || current_buffer->clip_changed)
8791 w->base_line_number = Qnil;
8792
8793 /* Move backward half the height of the window. */
8794 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8795 it.current_y = it.last_visible_y;
8796 move_it_vertically_backward (&it, it.last_visible_y / 2);
8797 xassert (IT_CHARPOS (it) >= BEGV);
8798
8799 /* The function move_it_vertically_backward may move over more
8800 than the specified y-distance. If it->w is small, e.g. a
8801 mini-buffer window, we may end up in front of the window's
8802 display area. Start displaying at the start of the line
8803 containing PT in this case. */
8804 if (it.current_y <= 0)
8805 {
8806 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8807 move_it_vertically (&it, 0);
8808 xassert (IT_CHARPOS (it) <= PT);
8809 it.current_y = 0;
0789adb2
RS
8810 }
8811
5f5c8ee5
GM
8812 it.current_x = it.hpos = 0;
8813
8814 /* Set startp here explicitly in case that helps avoid an infinite loop
8815 in case the window-scroll-functions functions get errors. */
8816 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8817
8818 /* Run scroll hooks. */
8819 startp = run_window_scroll_functions (window, it.current.pos);
8820
8821 /* Redisplay the window. */
8822 if (!current_matrix_up_to_date_p
8823 || windows_or_buffers_changed
8824 /* Don't use try_window_reusing_current_matrix in this case
8825 because it can have changed the buffer. */
8826 || !NILP (Vwindow_scroll_functions)
8827 || !just_this_one_p
8828 || MINI_WINDOW_P (w)
8829 || !try_window_reusing_current_matrix (w))
8830 try_window (window, startp);
8831
8832 /* If new fonts have been loaded (due to fontsets), give up. We
8833 have to start a new redisplay since we need to re-adjust glyph
8834 matrices. */
8835 if (fonts_changed_p)
8836 goto restore_buffers;
8837
8838 /* If cursor did not appear assume that the middle of the window is
8839 in the first line of the window. Do it again with the next line.
8840 (Imagine a window of height 100, displaying two lines of height
8841 60. Moving back 50 from it->last_visible_y will end in the first
8842 line.) */
8843 if (w->cursor.vpos < 0)
a2889657 8844 {
5f5c8ee5
GM
8845 if (!NILP (w->window_end_valid)
8846 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 8847 {
5f5c8ee5
GM
8848 clear_glyph_matrix (w->desired_matrix);
8849 move_it_by_lines (&it, 1, 0);
8850 try_window (window, it.current.pos);
a2889657 8851 }
5f5c8ee5 8852 else if (PT < IT_CHARPOS (it))
a2889657 8853 {
5f5c8ee5
GM
8854 clear_glyph_matrix (w->desired_matrix);
8855 move_it_by_lines (&it, -1, 0);
8856 try_window (window, it.current.pos);
8857 }
8858 else
8859 {
8860 /* Not much we can do about it. */
a2889657 8861 }
a2889657 8862 }
010494d0 8863
5f5c8ee5
GM
8864 /* Consider the following case: Window starts at BEGV, there is
8865 invisible, intangible text at BEGV, so that display starts at
8866 some point START > BEGV. It can happen that we are called with
8867 PT somewhere between BEGV and START. Try to handle that case. */
8868 if (w->cursor.vpos < 0)
835766b6 8869 {
5f5c8ee5
GM
8870 struct glyph_row *row = w->current_matrix->rows;
8871 if (row->mode_line_p)
8872 ++row;
8873 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 8874 }
5f5c8ee5
GM
8875
8876 make_cursor_line_fully_visible (w);
b5174a51 8877
5f5c8ee5
GM
8878 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8879 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8880 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8881 ? Qt : Qnil);
a2889657 8882
5f5c8ee5 8883 done:
a2889657 8884
5f5c8ee5 8885 /* Display the mode line, if we must. */
e481f960 8886 if ((update_mode_line
aa6d10fa 8887 /* If window not full width, must redo its mode line
5f5c8ee5
GM
8888 if (a) the window to its side is being redone and
8889 (b) we do a frame-based redisplay. This is a consequence
8890 of how inverted lines are drawn in frame-based redisplay. */
8891 || (!just_this_one_p
8892 && !FRAME_WINDOW_P (f)
8893 && !WINDOW_FULL_WIDTH_P (w))
8894 /* Line number to display. */
155ef550 8895 || INTEGERP (w->base_line_pos)
5f5c8ee5 8896 /* Column number is displayed and different from the one displayed. */
155ef550
KH
8897 || (!NILP (w->column_number_displayed)
8898 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
8899 /* This means that the window has a mode line. */
8900 && (WINDOW_WANTS_MODELINE_P (w)
8901 || WINDOW_WANTS_TOP_LINE_P (w)))
5ba50c51 8902 {
5f5c8ee5
GM
8903 display_mode_lines (w);
8904
8905 /* If mode line height has changed, arrange for a thorough
8906 immediate redisplay using the correct mode line height. */
8907 if (WINDOW_WANTS_MODELINE_P (w)
8908 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 8909 {
5f5c8ee5
GM
8910 fonts_changed_p = 1;
8911 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8912 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 8913 }
5f5c8ee5
GM
8914
8915 /* If top line height has changed, arrange for a thorough
8916 immediate redisplay using the correct mode line height. */
8917 if (WINDOW_WANTS_TOP_LINE_P (w)
8918 && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
8919 {
8920 fonts_changed_p = 1;
8921 MATRIX_TOP_LINE_ROW (w->current_matrix)->height
8922 = DESIRED_TOP_LINE_HEIGHT (w);
8923 }
8924
8925 if (fonts_changed_p)
8926 goto restore_buffers;
5ba50c51 8927 }
5f5c8ee5
GM
8928
8929 if (!line_number_displayed
8930 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
8931 {
8932 w->base_line_pos = Qnil;
8933 w->base_line_number = Qnil;
8934 }
a2889657 8935
5f5c8ee5
GM
8936 finish_menu_bars:
8937
7ce2c095 8938 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 8939 if (update_mode_line
5f5c8ee5
GM
8940 && EQ (FRAME_SELECTED_WINDOW (f), window))
8941 {
8942 int redisplay_menu_p = 0;
8943
8944 if (FRAME_WINDOW_P (f))
8945 {
dc937613 8946#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 8947 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 8948#else
5f5c8ee5 8949 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 8950#endif
5f5c8ee5
GM
8951 }
8952 else
8953 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8954
8955 if (redisplay_menu_p)
8956 display_menu_bar (w);
8957
8958#ifdef HAVE_WINDOW_SYSTEM
8959 if (WINDOWP (f->toolbar_window)
8960 && (FRAME_TOOLBAR_LINES (f) > 0
8961 || auto_resize_toolbars_p))
8962 redisplay_toolbar (f);
8963#endif
8964 }
7ce2c095 8965
88f22aff 8966 finish_scroll_bars:
5f5c8ee5 8967
88f22aff 8968 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 8969 {
b1d1124b 8970 int start, end, whole;
30c566e4 8971
b1d1124b 8972 /* Calculate the start and end positions for the current window.
3505ea70
JB
8973 At some point, it would be nice to choose between scrollbars
8974 which reflect the whole buffer size, with special markers
8975 indicating narrowing, and scrollbars which reflect only the
8976 visible region.
8977
5f5c8ee5 8978 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 8979 if (!MINI_WINDOW_P (w)
5f5c8ee5 8980 || (w == XWINDOW (minibuf_window)
c6e89d6c 8981 && NILP (echo_area_buffer[0])))
b1d1124b 8982 {
8a9311d7 8983 whole = ZV - BEGV;
4d641a15 8984 start = marker_position (w->start) - BEGV;
b1d1124b
JB
8985 /* I don't think this is guaranteed to be right. For the
8986 moment, we'll pretend it is. */
5f5c8ee5 8987 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 8988
5f5c8ee5
GM
8989 if (end < start)
8990 end = start;
8991 if (whole < (end - start))
8992 whole = end - start;
b1d1124b
JB
8993 }
8994 else
8995 start = end = whole = 0;
30c566e4 8996
88f22aff 8997 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 8998 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 8999
5f5c8ee5
GM
9000 /* Note that we actually used the scroll bar attached to this
9001 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 9002 (*redeem_scroll_bar_hook) (w);
30c566e4 9003 }
b1d1124b 9004
5f5c8ee5
GM
9005 restore_buffers:
9006
9007 /* Restore current_buffer and value of point in it. */
9008 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
5ba50c51 9009 if (really_switched_buffer)
f72df6ac 9010 set_buffer_internal_1 (old);
e481f960
RS
9011 else
9012 set_buffer_temp (old);
5f5c8ee5 9013 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
9014
9015 unbind_to (count, Qnil);
a2889657 9016}
a2889657 9017
5f5c8ee5
GM
9018
9019/* Build the complete desired matrix of WINDOW with a window start
9020 buffer position POS. Value is non-zero if successful. It is zero
9021 if fonts were loaded during redisplay which makes re-adjusting
9022 glyph matrices necessary. */
9023
9024int
a2889657
JB
9025try_window (window, pos)
9026 Lisp_Object window;
5f5c8ee5
GM
9027 struct text_pos pos;
9028{
9029 struct window *w = XWINDOW (window);
9030 struct it it;
9031 struct glyph_row *last_text_row = NULL;
9cbab4ff 9032
5f5c8ee5
GM
9033 /* Make POS the new window start. */
9034 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 9035
5f5c8ee5
GM
9036 /* Mark cursor position as unknown. No overlay arrow seen. */
9037 w->cursor.vpos = -1;
a2889657 9038 overlay_arrow_seen = 0;
642eefc6 9039
5f5c8ee5
GM
9040 /* Initialize iterator and info to start at POS. */
9041 start_display (&it, w, pos);
a2889657 9042
5f5c8ee5
GM
9043 /* Display all lines of W. */
9044 while (it.current_y < it.last_visible_y)
9045 {
9046 if (display_line (&it))
9047 last_text_row = it.glyph_row - 1;
9048 if (fonts_changed_p)
9049 return 0;
9050 }
a2889657 9051
5f5c8ee5
GM
9052 /* If bottom moved off end of frame, change mode line percentage. */
9053 if (XFASTINT (w->window_end_pos) <= 0
9054 && Z != IT_CHARPOS (it))
a2889657
JB
9055 w->update_mode_line = Qt;
9056
5f5c8ee5
GM
9057 /* Set window_end_pos to the offset of the last character displayed
9058 on the window from the end of current_buffer. Set
9059 window_end_vpos to its row number. */
9060 if (last_text_row)
9061 {
9062 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9063 w->window_end_bytepos
9064 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9065 XSETFASTINT (w->window_end_pos,
9066 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9067 XSETFASTINT (w->window_end_vpos,
9068 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9069 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9070 ->displays_text_p);
9071 }
9072 else
9073 {
9074 w->window_end_bytepos = 0;
9075 XSETFASTINT (w->window_end_pos, 0);
9076 XSETFASTINT (w->window_end_vpos, 0);
9077 }
9078
a2889657
JB
9079 /* But that is not valid info until redisplay finishes. */
9080 w->window_end_valid = Qnil;
5f5c8ee5 9081 return 1;
a2889657 9082}
5f5c8ee5
GM
9083
9084
a2889657 9085\f
5f5c8ee5
GM
9086/************************************************************************
9087 Window redisplay reusing current matrix when buffer has not changed
9088 ************************************************************************/
9089
9090/* Try redisplay of window W showing an unchanged buffer with a
9091 different window start than the last time it was displayed by
9092 reusing its current matrix. Value is non-zero if successful.
9093 W->start is the new window start. */
a2889657
JB
9094
9095static int
5f5c8ee5
GM
9096try_window_reusing_current_matrix (w)
9097 struct window *w;
a2889657 9098{
5f5c8ee5
GM
9099 struct frame *f = XFRAME (w->frame);
9100 struct glyph_row *row, *bottom_row;
9101 struct it it;
9102 struct run run;
9103 struct text_pos start, new_start;
9104 int nrows_scrolled, i;
9105 struct glyph_row *last_text_row;
9106 struct glyph_row *last_reused_text_row;
9107 struct glyph_row *start_row;
9108 int start_vpos, min_y, max_y;
9109
9110 /* Right now this function doesn't handle terminal frames. */
9111 if (!FRAME_WINDOW_P (f))
9112 return 0;
a2889657 9113
5f5c8ee5
GM
9114 /* Can't do this if region may have changed. */
9115 if ((!NILP (Vtransient_mark_mode)
9116 && !NILP (current_buffer->mark_active))
8f897821
GM
9117 || !NILP (w->region_showing)
9118 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 9119 return 0;
a2889657 9120
5f5c8ee5
GM
9121 /* If top-line visibility has changed, give up. */
9122 if (WINDOW_WANTS_TOP_LINE_P (w)
9123 != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
9124 return 0;
9125
9126 /* Give up if old or new display is scrolled vertically. We could
9127 make this function handle this, but right now it doesn't. */
9128 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9129 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9130 return 0;
9131
9132 /* The variable new_start now holds the new window start. The old
9133 start `start' can be determined from the current matrix. */
9134 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9135 start = start_row->start.pos;
9136 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 9137
5f5c8ee5
GM
9138 /* Clear the desired matrix for the display below. */
9139 clear_glyph_matrix (w->desired_matrix);
9140
9141 if (CHARPOS (new_start) <= CHARPOS (start))
9142 {
9143 int first_row_y;
9144
9145 IF_DEBUG (debug_method_add (w, "twu1"));
9146
9147 /* Display up to a row that can be reused. The variable
9148 last_text_row is set to the last row displayed that displays
9149 text. */
9150 start_display (&it, w, new_start);
9151 first_row_y = it.current_y;
9152 w->cursor.vpos = -1;
9153 last_text_row = last_reused_text_row = NULL;
9154 while (it.current_y < it.last_visible_y
9155 && IT_CHARPOS (it) < CHARPOS (start)
9156 && !fonts_changed_p)
9157 if (display_line (&it))
9158 last_text_row = it.glyph_row - 1;
9159
9160 /* A value of current_y < last_visible_y means that we stopped
9161 at the previous window start, which in turn means that we
9162 have at least one reusable row. */
9163 if (it.current_y < it.last_visible_y)
a2889657 9164 {
5f5c8ee5
GM
9165 nrows_scrolled = it.vpos;
9166
9167 /* Find PT if not already found in the lines displayed. */
9168 if (w->cursor.vpos < 0)
a2889657 9169 {
5f5c8ee5
GM
9170 int dy = it.current_y - first_row_y;
9171
9172 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9173 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9174 {
9175 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9176 && PT < MATRIX_ROW_END_CHARPOS (row))
9177 {
9178 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9179 dy, nrows_scrolled);
9180 break;
9181 }
9182
9183 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9184 break;
9185
9186 ++row;
9187 }
9188
9189 /* Give up if point was not found. This shouldn't
9190 happen often; not more often than with try_window
9191 itself. */
9192 if (w->cursor.vpos < 0)
9193 {
9194 clear_glyph_matrix (w->desired_matrix);
9195 return 0;
9196 }
a2889657 9197 }
5f5c8ee5
GM
9198
9199 /* Scroll the display. Do it before the current matrix is
9200 changed. The problem here is that update has not yet
9201 run, i.e. part of the current matrix is not up to date.
9202 scroll_run_hook will clear the cursor, and use the
9203 current matrix to get the height of the row the cursor is
9204 in. */
9205 run.current_y = first_row_y;
9206 run.desired_y = it.current_y;
9207 run.height = it.last_visible_y - it.current_y;
9208 if (run.height > 0)
a2889657 9209 {
5f5c8ee5
GM
9210 update_begin (f);
9211 rif->update_window_begin_hook (w);
9212 rif->scroll_run_hook (w, &run);
9213 rif->update_window_end_hook (w, 0);
9214 update_end (f);
a2889657 9215 }
5f5c8ee5
GM
9216
9217 /* Shift current matrix down by nrows_scrolled lines. */
9218 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9219 rotate_matrix (w->current_matrix,
9220 start_vpos,
9221 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9222 nrows_scrolled);
9223
9224 /* Disable lines not reused. */
9225 for (i = 0; i < it.vpos; ++i)
9226 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9227
9228 /* Re-compute Y positions. */
9229 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9230 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9231 max_y = it.last_visible_y;
9232 while (row < bottom_row)
d2f84654 9233 {
5f5c8ee5
GM
9234 row->y = it.current_y;
9235
9236 if (row->y < min_y)
9237 row->visible_height = row->height - (min_y - row->y);
9238 else if (row->y + row->height > max_y)
9239 row->visible_height
9240 = row->height - (row->y + row->height - max_y);
9241 else
9242 row->visible_height = row->height;
9243
9244 it.current_y += row->height;
9245 ++it.vpos;
9246
9247 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9248 last_reused_text_row = row;
9249 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9250 break;
9251 ++row;
d2f84654 9252 }
a2889657 9253 }
5f5c8ee5
GM
9254
9255 /* Update window_end_pos etc.; last_reused_text_row is the last
9256 reused row from the current matrix containing text, if any.
9257 The value of last_text_row is the last displayed line
9258 containing text. */
9259 if (last_reused_text_row)
a2889657 9260 {
5f5c8ee5
GM
9261 w->window_end_bytepos
9262 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9263 XSETFASTINT (w->window_end_pos,
9264 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9265 XSETFASTINT (w->window_end_vpos,
9266 MATRIX_ROW_VPOS (last_reused_text_row,
9267 w->current_matrix));
a2889657 9268 }
5f5c8ee5
GM
9269 else if (last_text_row)
9270 {
9271 w->window_end_bytepos
9272 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9273 XSETFASTINT (w->window_end_pos,
9274 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9275 XSETFASTINT (w->window_end_vpos,
9276 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9277 }
9278 else
9279 {
9280 /* This window must be completely empty. */
9281 w->window_end_bytepos = 0;
9282 XSETFASTINT (w->window_end_pos, 0);
9283 XSETFASTINT (w->window_end_vpos, 0);
9284 }
9285 w->window_end_valid = Qnil;
a2889657 9286
5f5c8ee5
GM
9287 /* Update hint: don't try scrolling again in update_window. */
9288 w->desired_matrix->no_scrolling_p = 1;
9289
9290#if GLYPH_DEBUG
9291 debug_method_add (w, "try_window_reusing_current_matrix 1");
9292#endif
9293 return 1;
a2889657 9294 }
5f5c8ee5
GM
9295 else if (CHARPOS (new_start) > CHARPOS (start))
9296 {
9297 struct glyph_row *pt_row, *row;
9298 struct glyph_row *first_reusable_row;
9299 struct glyph_row *first_row_to_display;
9300 int dy;
9301 int yb = window_text_bottom_y (w);
9302
9303 IF_DEBUG (debug_method_add (w, "twu2"));
9304
9305 /* Find the row starting at new_start, if there is one. Don't
9306 reuse a partially visible line at the end. */
9307 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9308 while (first_reusable_row->enabled_p
9309 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9310 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9311 < CHARPOS (new_start)))
9312 ++first_reusable_row;
9313
9314 /* Give up if there is no row to reuse. */
9315 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
9316 || !first_reusable_row->enabled_p
9317 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9318 != CHARPOS (new_start)))
5f5c8ee5
GM
9319 return 0;
9320
5f5c8ee5
GM
9321 /* We can reuse fully visible rows beginning with
9322 first_reusable_row to the end of the window. Set
9323 first_row_to_display to the first row that cannot be reused.
9324 Set pt_row to the row containing point, if there is any. */
9325 first_row_to_display = first_reusable_row;
9326 pt_row = NULL;
9327 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9328 {
9329 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9330 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9331 pt_row = first_row_to_display;
a2889657 9332
5f5c8ee5
GM
9333 ++first_row_to_display;
9334 }
a2889657 9335
5f5c8ee5
GM
9336 /* Start displaying at the start of first_row_to_display. */
9337 xassert (first_row_to_display->y < yb);
9338 init_to_row_start (&it, w, first_row_to_display);
9339 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9340 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9341 - nrows_scrolled);
9342 it.current_y = first_row_to_display->y - first_reusable_row->y;
9343
9344 /* Display lines beginning with first_row_to_display in the
9345 desired matrix. Set last_text_row to the last row displayed
9346 that displays text. */
9347 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9348 if (pt_row == NULL)
9349 w->cursor.vpos = -1;
9350 last_text_row = NULL;
9351 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9352 if (display_line (&it))
9353 last_text_row = it.glyph_row - 1;
9354
9355 /* Give up If point isn't in a row displayed or reused. */
9356 if (w->cursor.vpos < 0)
9357 {
9358 clear_glyph_matrix (w->desired_matrix);
9359 return 0;
9360 }
12adba34 9361
5f5c8ee5
GM
9362 /* If point is in a reused row, adjust y and vpos of the cursor
9363 position. */
9364 if (pt_row)
9365 {
9366 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9367 w->current_matrix);
9368 w->cursor.y -= first_reusable_row->y;
a2889657
JB
9369 }
9370
5f5c8ee5
GM
9371 /* Scroll the display. */
9372 run.current_y = first_reusable_row->y;
9373 run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9374 run.height = it.last_visible_y - run.current_y;
9375 if (run.height)
9376 {
9377 struct frame *f = XFRAME (WINDOW_FRAME (w));
9378 update_begin (f);
9379 rif->update_window_begin_hook (w);
9380 rif->scroll_run_hook (w, &run);
9381 rif->update_window_end_hook (w, 0);
9382 update_end (f);
9383 }
a2889657 9384
5f5c8ee5
GM
9385 /* Adjust Y positions of reused rows. */
9386 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9387 row = first_reusable_row;
9388 dy = first_reusable_row->y;
9389 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9390 max_y = it.last_visible_y;
9391 while (row < first_row_to_display)
9392 {
9393 row->y -= dy;
9394 if (row->y < min_y)
9395 row->visible_height = row->height - (min_y - row->y);
9396 else if (row->y + row->height > max_y)
9397 row->visible_height
9398 = row->height - (row->y + row->height - max_y);
9399 else
9400 row->visible_height = row->height;
9401 ++row;
9402 }
a2889657 9403
5f5c8ee5
GM
9404 /* Disable rows not reused. */
9405 while (row < bottom_row)
9406 {
9407 row->enabled_p = 0;
9408 ++row;
9409 }
9410
9411 /* Scroll the current matrix. */
9412 xassert (nrows_scrolled > 0);
9413 rotate_matrix (w->current_matrix,
9414 start_vpos,
9415 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9416 -nrows_scrolled);
9417
9418 /* Adjust window end. A null value of last_text_row means that
9419 the window end is in reused rows which in turn means that
9420 only its vpos can have changed. */
9421 if (last_text_row)
9422 {
9423 w->window_end_bytepos
9424 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9425 XSETFASTINT (w->window_end_pos,
9426 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9427 XSETFASTINT (w->window_end_vpos,
9428 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9429 }
9430 else
a2889657 9431 {
e8e536a9 9432 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 9433 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 9434 }
5f5c8ee5
GM
9435
9436 w->window_end_valid = Qnil;
9437 w->desired_matrix->no_scrolling_p = 1;
9438
9439#if GLYPH_DEBUG
9440 debug_method_add (w, "try_window_reusing_current_matrix 2");
9441#endif
9442 return 1;
a2889657 9443 }
5f5c8ee5
GM
9444
9445 return 0;
9446}
a2889657 9447
a2889657 9448
5f5c8ee5
GM
9449\f
9450/************************************************************************
9451 Window redisplay reusing current matrix when buffer has changed
9452 ************************************************************************/
9453
9454static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9455static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9456 int *, int *));
9457static struct glyph_row *
9458find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9459 struct glyph_row *));
9460
9461
9462/* Return the last row in MATRIX displaying text. If row START is
9463 non-null, start searching with that row. IT gives the dimensions
9464 of the display. Value is null if matrix is empty; otherwise it is
9465 a pointer to the row found. */
9466
9467static struct glyph_row *
9468find_last_row_displaying_text (matrix, it, start)
9469 struct glyph_matrix *matrix;
9470 struct it *it;
9471 struct glyph_row *start;
9472{
9473 struct glyph_row *row, *row_found;
9474
9475 /* Set row_found to the last row in IT->w's current matrix
9476 displaying text. The loop looks funny but think of partially
9477 visible lines. */
9478 row_found = NULL;
9479 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9480 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9481 {
9482 xassert (row->enabled_p);
9483 row_found = row;
9484 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9485 break;
9486 ++row;
a2889657 9487 }
5f5c8ee5
GM
9488
9489 return row_found;
9490}
9491
a2889657 9492
5f5c8ee5
GM
9493/* Return the last row in the current matrix of W that is not affected
9494 by changes at the start of current_buffer that occurred since the
9495 last time W was redisplayed. Value is null if no such row exists.
a2889657 9496
5f5c8ee5
GM
9497 The global variable beg_unchanged has to contain the number of
9498 bytes unchanged at the start of current_buffer. BEG +
9499 beg_unchanged is the buffer position of the first changed byte in
9500 current_buffer. Characters at positions < BEG + beg_unchanged are
9501 at the same buffer positions as they were when the current matrix
9502 was built. */
9503
9504static struct glyph_row *
9505get_last_unchanged_at_beg_row (w)
9506 struct window *w;
9507{
9142dd5b 9508 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
9509 struct glyph_row *row;
9510 struct glyph_row *row_found = NULL;
9511 int yb = window_text_bottom_y (w);
9512
9513 /* Find the last row displaying unchanged text. */
9514 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9515 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9516 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 9517 {
5f5c8ee5
GM
9518 if (/* If row ends before first_changed_pos, it is unchanged,
9519 except in some case. */
9520 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9521 /* When row ends in ZV and we write at ZV it is not
9522 unchanged. */
9523 && !row->ends_at_zv_p
9524 /* When first_changed_pos is the end of a continued line,
9525 row is not unchanged because it may be no longer
9526 continued. */
9527 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9528 && row->continued_p))
9529 row_found = row;
9530
9531 /* Stop if last visible row. */
9532 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9533 break;
9534
9535 ++row;
a2889657
JB
9536 }
9537
5f5c8ee5 9538 return row_found;
a2889657 9539}
5f5c8ee5
GM
9540
9541
9542/* Find the first glyph row in the current matrix of W that is not
9543 affected by changes at the end of current_buffer since the last
9544 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
9545 chars by which buffer positions in unchanged text at the end of
9546 current_buffer must be adjusted. Return in *DELTA_BYTES the
9547 corresponding number of bytes. Value is null if no such row
9548 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
9549
9550static struct glyph_row *
9551get_first_unchanged_at_end_row (w, delta, delta_bytes)
9552 struct window *w;
9553 int *delta, *delta_bytes;
a2889657 9554{
5f5c8ee5
GM
9555 struct glyph_row *row;
9556 struct glyph_row *row_found = NULL;
c581d710 9557
5f5c8ee5 9558 *delta = *delta_bytes = 0;
b2a76982 9559
5f5c8ee5
GM
9560 /* A value of window_end_pos >= end_unchanged means that the window
9561 end is in the range of changed text. If so, there is no
9562 unchanged row at the end of W's current matrix. */
9563 xassert (!NILP (w->window_end_valid));
9142dd5b 9564 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
9565 return NULL;
9566
9567 /* Set row to the last row in W's current matrix displaying text. */
9568 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9569
5f5c8ee5
GM
9570 /* If matrix is entirely empty, no unchanged row exists. */
9571 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9572 {
9573 /* The value of row is the last glyph row in the matrix having a
9574 meaningful buffer position in it. The end position of row
9575 corresponds to window_end_pos. This allows us to translate
9576 buffer positions in the current matrix to current buffer
9577 positions for characters not in changed text. */
9578 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9579 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9580 int last_unchanged_pos, last_unchanged_pos_old;
9581 struct glyph_row *first_text_row
9582 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9583
9584 *delta = Z - Z_old;
9585 *delta_bytes = Z_BYTE - Z_BYTE_old;
9586
9587 /* Set last_unchanged_pos to the buffer position of the last
9588 character in the buffer that has not been changed. Z is the
9589 index + 1 of the last byte in current_buffer, i.e. by
9590 subtracting end_unchanged we get the index of the last
9591 unchanged character, and we have to add BEG to get its buffer
9592 position. */
9142dd5b 9593 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
9594 last_unchanged_pos_old = last_unchanged_pos - *delta;
9595
9596 /* Search backward from ROW for a row displaying a line that
9597 starts at a minimum position >= last_unchanged_pos_old. */
9598 while (row >= first_text_row)
9599 {
9600 xassert (row->enabled_p);
9601 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9602
9603 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9604 row_found = row;
9605 --row;
9606 }
9607 }
9608
9609 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9610 return row_found;
c581d710
RS
9611}
9612
c581d710 9613
5f5c8ee5
GM
9614/* Make sure that glyph rows in the current matrix of window W
9615 reference the same glyph memory as corresponding rows in the
9616 frame's frame matrix. This function is called after scrolling W's
9617 current matrix on a terminal frame in try_window_id and
9618 try_window_reusing_current_matrix. */
9619
9620static void
9621sync_frame_with_window_matrix_rows (w)
9622 struct window *w;
c581d710 9623{
5f5c8ee5
GM
9624 struct frame *f = XFRAME (w->frame);
9625 struct glyph_row *window_row, *window_row_end, *frame_row;
9626
9627 /* Preconditions: W must be a leaf window and full-width. Its frame
9628 must have a frame matrix. */
9629 xassert (NILP (w->hchild) && NILP (w->vchild));
9630 xassert (WINDOW_FULL_WIDTH_P (w));
9631 xassert (!FRAME_WINDOW_P (f));
9632
9633 /* If W is a full-width window, glyph pointers in W's current matrix
9634 have, by definition, to be the same as glyph pointers in the
9635 corresponding frame matrix. */
9636 window_row = w->current_matrix->rows;
9637 window_row_end = window_row + w->current_matrix->nrows;
9638 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9639 while (window_row < window_row_end)
659a218f 9640 {
5f5c8ee5
GM
9641 int area;
9642 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9643 frame_row->glyphs[area] = window_row->glyphs[area];
9644 ++window_row, ++frame_row;
659a218f 9645 }
a2889657 9646}
5f5c8ee5
GM
9647
9648
9649/* Try to redisplay window W by reusing its existing display. W's
9650 current matrix must be up to date when this function is called,
9651 i.e. window_end_valid must not be nil.
9652
9653 Value is
9654
9655 1 if display has been updated
9656 0 if otherwise unsuccessful
9657 -1 if redisplay with same window start is known not to succeed
9658
9659 The following steps are performed:
9660
9661 1. Find the last row in the current matrix of W that is not
9662 affected by changes at the start of current_buffer. If no such row
9663 is found, give up.
9664
9665 2. Find the first row in W's current matrix that is not affected by
9666 changes at the end of current_buffer. Maybe there is no such row.
9667
9668 3. Display lines beginning with the row + 1 found in step 1 to the
9669 row found in step 2 or, if step 2 didn't find a row, to the end of
9670 the window.
9671
9672 4. If cursor is not known to appear on the window, give up.
9673
9674 5. If display stopped at the row found in step 2, scroll the
9675 display and current matrix as needed.
9676
9677 6. Maybe display some lines at the end of W, if we must. This can
9678 happen under various circumstances, like a partially visible line
9679 becoming fully visible, or because newly displayed lines are displayed
9680 in smaller font sizes.
9681
9682 7. Update W's window end information. */
9683
9684 /* Check that window end is what we expect it to be. */
12adba34
RS
9685
9686static int
5f5c8ee5 9687try_window_id (w)
12adba34 9688 struct window *w;
12adba34 9689{
5f5c8ee5
GM
9690 struct frame *f = XFRAME (w->frame);
9691 struct glyph_matrix *current_matrix = w->current_matrix;
9692 struct glyph_matrix *desired_matrix = w->desired_matrix;
9693 struct glyph_row *last_unchanged_at_beg_row;
9694 struct glyph_row *first_unchanged_at_end_row;
9695 struct glyph_row *row;
9696 struct glyph_row *bottom_row;
9697 int bottom_vpos;
9698 struct it it;
9699 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9700 struct text_pos start_pos;
9701 struct run run;
9702 int first_unchanged_at_end_vpos = 0;
9703 struct glyph_row *last_text_row, *last_text_row_at_end;
9704 struct text_pos start;
9705
9706 SET_TEXT_POS_FROM_MARKER (start, w->start);
9707
9708 /* Check pre-conditions. Window end must be valid, otherwise
9709 the current matrix would not be up to date. */
9710 xassert (!NILP (w->window_end_valid));
9711 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9712 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9713
9714 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9715 only if buffer has really changed. The reason is that the gap is
9716 initially at Z for freshly visited files. The code below would
9717 set end_unchanged to 0 in that case. */
9718 if (MODIFF > SAVE_MODIFF)
9719 {
9142dd5b
GM
9720 if (GPT - BEG < BEG_UNCHANGED)
9721 BEG_UNCHANGED = GPT - BEG;
9722 if (Z - GPT < END_UNCHANGED)
9723 END_UNCHANGED = Z - GPT;
5f5c8ee5
GM
9724 }
9725
9726 /* If window starts after a line end, and the last change is in
9727 front of that newline, then changes don't affect the display.
9728 This case happens with stealth-fontification. */
9729 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9730 if (CHARPOS (start) > BEGV
9142dd5b 9731 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
9732 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9733 && PT < MATRIX_ROW_END_CHARPOS (row))
9734 {
9735 /* We have to update window end positions because the buffer's
9736 size has changed. */
9737 w->window_end_pos
9738 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9739 w->window_end_bytepos
9740 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9741 return 1;
9742 }
9743
9744 /* Return quickly if changes are all below what is displayed in the
9745 window, and if PT is in the window. */
9142dd5b 9746 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
9747 && PT < MATRIX_ROW_END_CHARPOS (row))
9748 {
9749 /* We have to update window end positions because the buffer's
9750 size has changed. */
9751 w->window_end_pos
9752 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9753 w->window_end_bytepos
9754 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9755 return 1;
9756 }
9757
9758 /* Check that window start agrees with the start of the first glyph
9759 row in its current matrix. Check this after we know the window
9760 start is not in changed text, otherwise positions would not be
9761 comparable. */
9762 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9763 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9764 return 0;
9765
5f5c8ee5
GM
9766 /* Compute the position at which we have to start displaying new
9767 lines. Some of the lines at the top of the window might be
9768 reusable because they are not displaying changed text. Find the
9769 last row in W's current matrix not affected by changes at the
9770 start of current_buffer. Value is null if changes start in the
9771 first line of window. */
9772 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9773 if (last_unchanged_at_beg_row)
9774 {
9775 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9776 start_pos = it.current.pos;
9777
9778 /* Start displaying new lines in the desired matrix at the same
9779 vpos we would use in the current matrix, i.e. below
9780 last_unchanged_at_beg_row. */
9781 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9782 current_matrix);
9783 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9784 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9785
9786 xassert (it.hpos == 0 && it.current_x == 0);
9787 }
9788 else
9789 {
9790 /* There are no reusable lines at the start of the window.
9791 Start displaying in the first line. */
9792 start_display (&it, w, start);
9793 start_pos = it.current.pos;
9794 }
9795
9796 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
9797 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
9798
9799 /* Find the first row that is not affected by changes at the end of
9800 the buffer. Value will be null if there is no unchanged row, in
9801 which case we must redisplay to the end of the window. delta
9802 will be set to the value by which buffer positions beginning with
9803 first_unchanged_at_end_row have to be adjusted due to text
9804 changes. */
9805 first_unchanged_at_end_row
9806 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9807 IF_DEBUG (debug_delta = delta);
9808 IF_DEBUG (debug_delta_bytes = delta_bytes);
9809
9810 /* Set stop_pos to the buffer position up to which we will have to
9811 display new lines. If first_unchanged_at_end_row != NULL, this
9812 is the buffer position of the start of the line displayed in that
9813 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9814 that we don't stop at a buffer position. */
9815 stop_pos = 0;
9816 if (first_unchanged_at_end_row)
9817 {
9818 xassert (last_unchanged_at_beg_row == NULL
9819 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9820
9821 /* If this is a continuation line, move forward to the next one
9822 that isn't. Changes in lines above affect this line.
9823 Caution: this may move first_unchanged_at_end_row to a row
9824 not displaying text. */
9825 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9826 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9827 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9828 < it.last_visible_y))
9829 ++first_unchanged_at_end_row;
9830
9831 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9832 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9833 >= it.last_visible_y))
9834 first_unchanged_at_end_row = NULL;
9835 else
9836 {
9837 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9838 + delta);
9839 first_unchanged_at_end_vpos
9840 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 9841 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
9842 }
9843 }
9844 else if (last_unchanged_at_beg_row == NULL)
9845 return 0;
9846
9847
9848#if GLYPH_DEBUG
9849
9850 /* Either there is no unchanged row at the end, or the one we have
9851 now displays text. This is a necessary condition for the window
9852 end pos calculation at the end of this function. */
9853 xassert (first_unchanged_at_end_row == NULL
9854 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9855
9856 debug_last_unchanged_at_beg_vpos
9857 = (last_unchanged_at_beg_row
9858 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9859 : -1);
9860 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9861
9862#endif /* GLYPH_DEBUG != 0 */
9863
9864
9865 /* Display new lines. Set last_text_row to the last new line
9866 displayed which has text on it, i.e. might end up as being the
9867 line where the window_end_vpos is. */
9868 w->cursor.vpos = -1;
9869 last_text_row = NULL;
9870 overlay_arrow_seen = 0;
9871 while (it.current_y < it.last_visible_y
9872 && !fonts_changed_p
9873 && (first_unchanged_at_end_row == NULL
9874 || IT_CHARPOS (it) < stop_pos))
9875 {
9876 if (display_line (&it))
9877 last_text_row = it.glyph_row - 1;
9878 }
9879
9880 if (fonts_changed_p)
9881 return -1;
9882
9883
9884 /* Compute differences in buffer positions, y-positions etc. for
9885 lines reused at the bottom of the window. Compute what we can
9886 scroll. */
9887 if (first_unchanged_at_end_row)
9888 {
9889 dvpos = (it.vpos
9890 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
9891 current_matrix));
9892 dy = it.current_y - first_unchanged_at_end_row->y;
9893 run.current_y = first_unchanged_at_end_row->y;
9894 run.desired_y = run.current_y + dy;
9895 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
9896 }
9897 else
9898 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
9899 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
9900
8f8ba186 9901
5f5c8ee5
GM
9902 /* Find the cursor if not already found. We have to decide whether
9903 PT will appear on this window (it sometimes doesn't, but this is
9904 not a very frequent case.) This decision has to be made before
9905 the current matrix is altered. A value of cursor.vpos < 0 means
9906 that PT is either in one of the lines beginning at
9907 first_unchanged_at_end_row or below the window. Don't care for
9908 lines that might be displayed later at the window end; as
9909 mentioned, this is not a frequent case. */
9910 if (w->cursor.vpos < 0)
9911 {
9912 int last_y = min (it.last_visible_y, it.last_visible_y + dy);
9913
9914 /* Cursor in unchanged rows at the top? */
9915 if (PT < CHARPOS (start_pos)
9916 && last_unchanged_at_beg_row)
9917 {
9918 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9919 while (row <= last_unchanged_at_beg_row
9920 && MATRIX_ROW_END_CHARPOS (row) <= PT)
9921 ++row;
9922 xassert (row <= last_unchanged_at_beg_row);
9923 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9924 }
9925
9926 /* Start from first_unchanged_at_end_row looking for PT. */
9927 else if (first_unchanged_at_end_row)
9928 {
9929 row = first_unchanged_at_end_row;
468155d7 9930
5f5c8ee5
GM
9931 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9932 {
9933 if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
9934 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
9935 {
9936 set_cursor_from_row (w, row, w->current_matrix, delta,
9937 delta_bytes, dy, dvpos);
9938 break;
9939 }
9940 else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
9941 break;
9942 ++row;
9943 }
468155d7
GM
9944
9945 /* If PT is at ZV, this is not in a line displaying text.
9946 Check that case. */
9947 if (w->cursor.vpos < 0
9948 && PT - delta == MATRIX_ROW_START_CHARPOS (row)
9949 && row->ends_at_zv_p)
9950 set_cursor_from_row (w, row, w->current_matrix, delta,
9951 delta_bytes, dy, dvpos);
5f5c8ee5
GM
9952 }
9953
9954 /* Give up if cursor was not found. */
9955 if (w->cursor.vpos < 0)
9956 {
9957 clear_glyph_matrix (w->desired_matrix);
9958 return -1;
9959 }
9960 }
9961
9962 /* Don't let the cursor end in the scroll margins. */
9963 {
9964 int this_scroll_margin, cursor_height;
9965
9966 this_scroll_margin = max (0, scroll_margin);
9967 this_scroll_margin = min (this_scroll_margin,
9968 XFASTINT (w->height) / 4);
9969 this_scroll_margin *= CANON_Y_UNIT (it.f);
9970 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
9971
9972 if ((w->cursor.y < this_scroll_margin
9973 && CHARPOS (start) > BEGV)
9974 /* Don't take scroll margin into account at the bottom because
9975 old redisplay didn't do it either. */
9976 || w->cursor.y + cursor_height > it.last_visible_y)
9977 {
9978 w->cursor.vpos = -1;
9979 clear_glyph_matrix (w->desired_matrix);
9980 return -1;
9981 }
9982 }
9983
9984 /* Scroll the display. Do it before changing the current matrix so
9985 that xterm.c doesn't get confused about where the cursor glyph is
9986 found. */
9987 if (dy)
9988 {
9989 update_begin (f);
9990
9991 if (FRAME_WINDOW_P (f))
9992 {
9993 rif->update_window_begin_hook (w);
9994 rif->scroll_run_hook (w, &run);
9995 rif->update_window_end_hook (w, 0);
9996 }
9997 else
9998 {
9999 /* Terminal frame. In this case, dvpos gives the number of
10000 lines to scroll by; dvpos < 0 means scroll up. */
10001 int first_unchanged_at_end_vpos
10002 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10003 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10004 int end = XFASTINT (w->top) + window_internal_height (w);
10005
10006 /* Perform the operation on the screen. */
10007 if (dvpos > 0)
10008 {
10009 /* Scroll last_unchanged_at_beg_row to the end of the
10010 window down dvpos lines. */
10011 set_terminal_window (end);
10012
10013 /* On dumb terminals delete dvpos lines at the end
10014 before inserting dvpos empty lines. */
10015 if (!scroll_region_ok)
10016 ins_del_lines (end - dvpos, -dvpos);
10017
10018 /* Insert dvpos empty lines in front of
10019 last_unchanged_at_beg_row. */
10020 ins_del_lines (from, dvpos);
10021 }
10022 else if (dvpos < 0)
10023 {
10024 /* Scroll up last_unchanged_at_beg_vpos to the end of
10025 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10026 set_terminal_window (end);
10027
10028 /* Delete dvpos lines in front of
10029 last_unchanged_at_beg_vpos. ins_del_lines will set
10030 the cursor to the given vpos and emit |dvpos| delete
10031 line sequences. */
10032 ins_del_lines (from + dvpos, dvpos);
10033
10034 /* On a dumb terminal insert dvpos empty lines at the
10035 end. */
10036 if (!scroll_region_ok)
10037 ins_del_lines (end + dvpos, -dvpos);
10038 }
10039
10040 set_terminal_window (0);
10041 }
10042
10043 update_end (f);
10044 }
10045
10046 /* Shift reused rows of the current matrix to the right position. */
10047 if (dvpos < 0)
10048 {
10049 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10050 bottom_vpos, dvpos);
10051 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10052 bottom_vpos, 0);
10053 }
10054 else if (dvpos > 0)
10055 {
10056 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10057 bottom_vpos, dvpos);
10058 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10059 first_unchanged_at_end_vpos + dvpos, 0);
10060 }
10061
10062 /* For frame-based redisplay, make sure that current frame and window
10063 matrix are in sync with respect to glyph memory. */
10064 if (!FRAME_WINDOW_P (f))
10065 sync_frame_with_window_matrix_rows (w);
10066
10067 /* Adjust buffer positions in reused rows. */
10068 if (delta)
10069 increment_glyph_matrix_buffer_positions (current_matrix,
10070 first_unchanged_at_end_vpos + dvpos,
10071 bottom_vpos, delta, delta_bytes);
10072
10073 /* Adjust Y positions. */
10074 if (dy)
10075 shift_glyph_matrix (w, current_matrix,
10076 first_unchanged_at_end_vpos + dvpos,
10077 bottom_vpos, dy);
10078
10079 if (first_unchanged_at_end_row)
10080 first_unchanged_at_end_row += dvpos;
10081
10082 /* If scrolling up, there may be some lines to display at the end of
10083 the window. */
10084 last_text_row_at_end = NULL;
10085 if (dy < 0)
10086 {
10087 /* Set last_row to the glyph row in the current matrix where the
10088 window end line is found. It has been moved up or down in
10089 the matrix by dvpos. */
10090 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10091 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10092
10093 /* If last_row is the window end line, it should display text. */
10094 xassert (last_row->displays_text_p);
10095
10096 /* If window end line was partially visible before, begin
10097 displaying at that line. Otherwise begin displaying with the
10098 line following it. */
10099 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10100 {
10101 init_to_row_start (&it, w, last_row);
10102 it.vpos = last_vpos;
10103 it.current_y = last_row->y;
10104 }
10105 else
10106 {
10107 init_to_row_end (&it, w, last_row);
10108 it.vpos = 1 + last_vpos;
10109 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10110 ++last_row;
10111 }
12adba34 10112
5f5c8ee5
GM
10113 /* We may start in a continuation line. If so, we have to get
10114 the right continuation_lines_width and current_x. */
10115 it.continuation_lines_width = last_row->continuation_lines_width;
10116 it.hpos = it.current_x = 0;
10117
10118 /* Display the rest of the lines at the window end. */
10119 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10120 while (it.current_y < it.last_visible_y
10121 && !fonts_changed_p)
10122 {
10123 /* Is it always sure that the display agrees with lines in
10124 the current matrix? I don't think so, so we mark rows
10125 displayed invalid in the current matrix by setting their
10126 enabled_p flag to zero. */
10127 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10128 if (display_line (&it))
10129 last_text_row_at_end = it.glyph_row - 1;
10130 }
10131 }
12adba34 10132
5f5c8ee5
GM
10133 /* Update window_end_pos and window_end_vpos. */
10134 if (first_unchanged_at_end_row
10135 && first_unchanged_at_end_row->y < it.last_visible_y
10136 && !last_text_row_at_end)
10137 {
10138 /* Window end line if one of the preserved rows from the current
10139 matrix. Set row to the last row displaying text in current
10140 matrix starting at first_unchanged_at_end_row, after
10141 scrolling. */
10142 xassert (first_unchanged_at_end_row->displays_text_p);
10143 row = find_last_row_displaying_text (w->current_matrix, &it,
10144 first_unchanged_at_end_row);
10145 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10146
10147 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10148 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10149 XSETFASTINT (w->window_end_vpos,
10150 MATRIX_ROW_VPOS (row, w->current_matrix));
10151 }
10152 else if (last_text_row_at_end)
10153 {
10154 XSETFASTINT (w->window_end_pos,
10155 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10156 w->window_end_bytepos
10157 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10158 XSETFASTINT (w->window_end_vpos,
10159 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10160 }
10161 else if (last_text_row)
10162 {
10163 /* We have displayed either to the end of the window or at the
10164 end of the window, i.e. the last row with text is to be found
10165 in the desired matrix. */
10166 XSETFASTINT (w->window_end_pos,
10167 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10168 w->window_end_bytepos
10169 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10170 XSETFASTINT (w->window_end_vpos,
10171 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10172 }
10173 else if (first_unchanged_at_end_row == NULL
10174 && last_text_row == NULL
10175 && last_text_row_at_end == NULL)
10176 {
10177 /* Displayed to end of window, but no line containing text was
10178 displayed. Lines were deleted at the end of the window. */
10179 int vpos;
10180 int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
10181
10182 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10183 if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
10184 && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
10185 || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
10186 && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
10187 break;
12adba34 10188
5f5c8ee5
GM
10189 w->window_end_vpos = make_number (vpos);
10190 }
10191 else
10192 abort ();
10193
10194 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10195 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 10196
5f5c8ee5
GM
10197 /* Record that display has not been completed. */
10198 w->window_end_valid = Qnil;
10199 w->desired_matrix->no_scrolling_p = 1;
10200 return 1;
12adba34 10201}
0f9c0ff0 10202
a2889657 10203
5f5c8ee5
GM
10204\f
10205/***********************************************************************
10206 More debugging support
10207 ***********************************************************************/
a2889657 10208
5f5c8ee5 10209#if GLYPH_DEBUG
a2889657 10210
5f5c8ee5
GM
10211 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10212static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 10213
31b24551 10214
5f5c8ee5
GM
10215/* Dump the contents of glyph matrix MATRIX on stderr. If
10216 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 10217
5f5c8ee5
GM
10218void
10219dump_glyph_matrix (matrix, with_glyphs_p)
10220 struct glyph_matrix *matrix;
10221 int with_glyphs_p;
10222{
efc63ef0 10223 int i;
5f5c8ee5
GM
10224 for (i = 0; i < matrix->nrows; ++i)
10225 dump_glyph_row (matrix, i, with_glyphs_p);
10226}
31b24551 10227
68a37fa8 10228
5f5c8ee5
GM
10229/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10230 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 10231
5f5c8ee5
GM
10232void
10233dump_glyph_row (matrix, vpos, with_glyphs_p)
10234 struct glyph_matrix *matrix;
10235 int vpos, with_glyphs_p;
10236{
10237 struct glyph_row *row;
10238
10239 if (vpos < 0 || vpos >= matrix->nrows)
10240 return;
10241
10242 row = MATRIX_ROW (matrix, vpos);
10243
10244 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10245 fprintf (stderr, "=============================================\n");
10246
10247 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
10248 row - matrix->rows,
10249 MATRIX_ROW_START_CHARPOS (row),
10250 MATRIX_ROW_END_CHARPOS (row),
10251 row->used[TEXT_AREA],
10252 row->contains_overlapping_glyphs_p,
10253 row->enabled_p,
10254 row->inverse_p,
10255 row->truncated_on_left_p,
10256 row->truncated_on_right_p,
10257 row->overlay_arrow_p,
10258 row->continued_p,
10259 MATRIX_ROW_CONTINUATION_LINE_P (row),
10260 row->displays_text_p,
10261 row->ends_at_zv_p,
10262 row->fill_line_p,
10263 row->x,
10264 row->y,
10265 row->pixel_width);
10266 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10267 row->end.overlay_string_index);
10268 fprintf (stderr, "%9d %5d\n",
10269 CHARPOS (row->start.string_pos),
10270 CHARPOS (row->end.string_pos));
10271 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10272 row->end.dpvec_index);
10273
10274 if (with_glyphs_p)
bd66d1ba 10275 {
5f5c8ee5
GM
10276 struct glyph *glyph, *glyph_end;
10277 int prev_had_glyphs_p;
10278
10279 glyph = row->glyphs[TEXT_AREA];
10280 glyph_end = glyph + row->used[TEXT_AREA];
10281
10282 /* Glyph for a line end in text. */
10283 if (glyph == glyph_end && glyph->charpos > 0)
10284 ++glyph_end;
10285
10286 if (glyph < glyph_end)
bd66d1ba 10287 {
5f5c8ee5
GM
10288 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10289 prev_had_glyphs_p = 1;
bd66d1ba
RS
10290 }
10291 else
5f5c8ee5
GM
10292 prev_had_glyphs_p = 0;
10293
10294 while (glyph < glyph_end)
f7b4b63a 10295 {
5f5c8ee5
GM
10296 if (glyph->type == CHAR_GLYPH)
10297 {
10298 fprintf (stderr,
10299 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10300 glyph - row->glyphs[TEXT_AREA],
10301 'C',
10302 glyph->charpos,
10303 glyph->pixel_width,
10304 glyph->u.ch.code,
10305 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
10306 ? glyph->u.ch.code
10307 : '.'),
10308 glyph->u.ch.face_id,
10309 glyph->left_box_line_p,
10310 glyph->right_box_line_p);
10311 }
10312 else if (glyph->type == STRETCH_GLYPH)
10313 {
10314 fprintf (stderr,
10315 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10316 glyph - row->glyphs[TEXT_AREA],
10317 'S',
10318 glyph->charpos,
10319 glyph->pixel_width,
10320 0,
10321 '.',
10322 glyph->u.stretch.face_id,
10323 glyph->left_box_line_p,
10324 glyph->right_box_line_p);
10325 }
10326 else if (glyph->type == IMAGE_GLYPH)
10327 {
10328 fprintf (stderr,
10329 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10330 glyph - row->glyphs[TEXT_AREA],
10331 'I',
10332 glyph->charpos,
10333 glyph->pixel_width,
10334 glyph->u.img.id,
10335 '.',
10336 glyph->u.img.face_id,
10337 glyph->left_box_line_p,
10338 glyph->right_box_line_p);
10339 }
10340 ++glyph;
f7b4b63a 10341 }
f4faa47c 10342 }
5f5c8ee5 10343}
f4faa47c 10344
a2889657 10345
5f5c8ee5
GM
10346DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10347 Sdump_glyph_matrix, 0, 1, "p",
10348 "Dump the current matrix of the selected window to stderr.\n\
10349Shows contents of glyph row structures. With non-nil optional\n\
10350parameter WITH-GLYPHS-P, dump glyphs as well.")
10351 (with_glyphs_p)
10352{
10353 struct window *w = XWINDOW (selected_window);
10354 struct buffer *buffer = XBUFFER (w->buffer);
10355
10356 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10357 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10358 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10359 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10360 fprintf (stderr, "=============================================\n");
10361 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10362 return Qnil;
10363}
1c2250c2 10364
1fca3fae 10365
5f5c8ee5
GM
10366DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10367 "Dump glyph row ROW to stderr.")
10368 (row)
10369 Lisp_Object row;
10370{
10371 CHECK_NUMBER (row, 0);
10372 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10373 return Qnil;
10374}
1fca3fae 10375
67481ae5 10376
5f5c8ee5
GM
10377DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
10378 0, 0, "", "")
10379 ()
10380{
10381 struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
10382 ->current_matrix);
10383 dump_glyph_row (m, 0, 1);
10384 return Qnil;
10385}
ca26e1c8 10386
0f9c0ff0 10387
5f5c8ee5
GM
10388DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10389 Strace_redisplay_toggle, 0, 0, "",
10390 "Toggle tracing of redisplay.")
10391 ()
10392{
10393 trace_redisplay_p = !trace_redisplay_p;
10394 return Qnil;
10395}
10396
10397
10398#endif /* GLYPH_DEBUG */
ca26e1c8 10399
ca26e1c8 10400
5f5c8ee5
GM
10401\f
10402/***********************************************************************
10403 Building Desired Matrix Rows
10404 ***********************************************************************/
a2889657 10405
5f5c8ee5
GM
10406/* Return a temporary glyph row holding the glyphs of an overlay
10407 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 10408
5f5c8ee5
GM
10409static struct glyph_row *
10410get_overlay_arrow_glyph_row (w)
10411 struct window *w;
10412{
10413 struct frame *f = XFRAME (WINDOW_FRAME (w));
10414 struct buffer *buffer = XBUFFER (w->buffer);
10415 struct buffer *old = current_buffer;
10416 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10417 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10418 unsigned char *arrow_end = arrow_string + arrow_len;
10419 unsigned char *p;
10420 struct it it;
10421 int multibyte_p;
10422 int n_glyphs_before;
10423
10424 set_buffer_temp (buffer);
10425 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10426 it.glyph_row->used[TEXT_AREA] = 0;
10427 SET_TEXT_POS (it.position, 0, 0);
10428
10429 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10430 p = arrow_string;
10431 while (p < arrow_end)
10432 {
10433 Lisp_Object face, ilisp;
10434
10435 /* Get the next character. */
10436 if (multibyte_p)
4fdb80f2 10437 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
10438 else
10439 it.c = *p, it.len = 1;
10440 p += it.len;
10441
10442 /* Get its face. */
10443 XSETFASTINT (ilisp, p - arrow_string);
10444 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10445 it.face_id = compute_char_face (f, it.c, face);
10446
10447 /* Compute its width, get its glyphs. */
10448 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 10449 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
10450 PRODUCE_GLYPHS (&it);
10451
10452 /* If this character doesn't fit any more in the line, we have
10453 to remove some glyphs. */
10454 if (it.current_x > it.last_visible_x)
10455 {
10456 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10457 break;
10458 }
10459 }
10460
10461 set_buffer_temp (old);
10462 return it.glyph_row;
10463}
ca26e1c8 10464
b0a0fbda 10465
5f5c8ee5
GM
10466/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10467 glyphs are only inserted for terminal frames since we can't really
10468 win with truncation glyphs when partially visible glyphs are
10469 involved. Which glyphs to insert is determined by
10470 produce_special_glyphs. */
67481ae5 10471
5f5c8ee5
GM
10472static void
10473insert_left_trunc_glyphs (it)
10474 struct it *it;
10475{
10476 struct it truncate_it;
10477 struct glyph *from, *end, *to, *toend;
10478
10479 xassert (!FRAME_WINDOW_P (it->f));
10480
10481 /* Get the truncation glyphs. */
10482 truncate_it = *it;
10483 truncate_it.charset = -1;
10484 truncate_it.current_x = 0;
10485 truncate_it.face_id = DEFAULT_FACE_ID;
10486 truncate_it.glyph_row = &scratch_glyph_row;
10487 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10488 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10489 truncate_it.object = 0;
10490 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10491
10492 /* Overwrite glyphs from IT with truncation glyphs. */
10493 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10494 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10495 to = it->glyph_row->glyphs[TEXT_AREA];
10496 toend = to + it->glyph_row->used[TEXT_AREA];
10497
10498 while (from < end)
10499 *to++ = *from++;
10500
10501 /* There may be padding glyphs left over. Remove them. */
10502 from = to;
10503 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10504 ++from;
10505 while (from < toend)
10506 *to++ = *from++;
10507
10508 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10509}
e0bfbde6 10510
e0bfbde6 10511
5f5c8ee5 10512/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 10513
5f5c8ee5
GM
10514 Most of the time, ascent and height of a display line will be equal
10515 to the max_ascent and max_height values of the display iterator
10516 structure. This is not the case if
67481ae5 10517
5f5c8ee5
GM
10518 1. We hit ZV without displaying anything. In this case, max_ascent
10519 and max_height will be zero.
1c9241f5 10520
5f5c8ee5
GM
10521 2. We have some glyphs that don't contribute to the line height.
10522 (The glyph row flag contributes_to_line_height_p is for future
10523 pixmap extensions).
f6fd109b 10524
5f5c8ee5
GM
10525 The first case is easily covered by using default values because in
10526 these cases, the line height does not really matter, except that it
10527 must not be zero. */
67481ae5 10528
5f5c8ee5
GM
10529static void
10530compute_line_metrics (it)
10531 struct it *it;
10532{
10533 struct glyph_row *row = it->glyph_row;
10534 int area, i;
1c2250c2 10535
5f5c8ee5
GM
10536 if (FRAME_WINDOW_P (it->f))
10537 {
10538 int i, top_line_height;
1c2250c2 10539
5f5c8ee5
GM
10540 /* The line may consist of one space only, that was added to
10541 place the cursor on it. If so, the row's height hasn't been
10542 computed yet. */
10543 if (row->height == 0)
10544 {
10545 if (it->max_ascent + it->max_descent == 0)
312246d1 10546 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
10547 row->ascent = it->max_ascent;
10548 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10549 row->phys_ascent = it->max_phys_ascent;
10550 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10551 }
10552
10553 /* Compute the width of this line. */
10554 row->pixel_width = row->x;
10555 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10556 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10557
10558 xassert (row->pixel_width >= 0);
10559 xassert (row->ascent >= 0 && row->height > 0);
10560
312246d1
GM
10561 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10562 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10563
10564 /* If first line's physical ascent is larger than its logical
10565 ascent, use the physical ascent, and make the row taller.
10566 This makes accented characters fully visible. */
10567 if (row == it->w->desired_matrix->rows
10568 && row->phys_ascent > row->ascent)
10569 {
10570 row->height += row->phys_ascent - row->ascent;
10571 row->ascent = row->phys_ascent;
10572 }
10573
5f5c8ee5
GM
10574 /* Compute how much of the line is visible. */
10575 row->visible_height = row->height;
10576
10577 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
10578 if (row->y < top_line_height)
10579 row->visible_height -= top_line_height - row->y;
10580 else
10581 {
10582 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10583 if (row->y + row->height > max_y)
10584 row->visible_height -= row->y + row->height - max_y;
10585 }
10586 }
10587 else
10588 {
10589 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
10590 row->ascent = row->phys_ascent = 0;
10591 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 10592 }
67481ae5 10593
5f5c8ee5
GM
10594 /* Compute a hash code for this row. */
10595 row->hash = 0;
10596 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10597 for (i = 0; i < row->used[area]; ++i)
10598 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10599 + row->glyphs[area][i].u.val
10600 + (row->glyphs[area][i].type << 2));
a2889657 10601
5f5c8ee5 10602 it->max_ascent = it->max_descent = 0;
312246d1 10603 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 10604}
12adba34 10605
ca26e1c8 10606
5f5c8ee5
GM
10607/* Append one space to the glyph row of iterator IT if doing a
10608 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10609 space have the default face, otherwise let it have the same face as
c6e89d6c
GM
10610 IT->face_id.
10611
10612 This function is called to make sure that there is always one glyph
10613 at the end of a glyph row that the cursor can be set on under
10614 window-systems. (If there weren't such a glyph we would not know
10615 how wide and tall a box cursor should be displayed).
10616
10617 At the same time this space let's a nicely handle clearing to the
10618 end of the line if the row ends in italic text. */
ca26e1c8 10619
5f5c8ee5
GM
10620static void
10621append_space (it, default_face_p)
10622 struct it *it;
10623 int default_face_p;
10624{
10625 if (FRAME_WINDOW_P (it->f))
10626 {
10627 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 10628
5f5c8ee5
GM
10629 if (it->glyph_row->glyphs[TEXT_AREA] + n
10630 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 10631 {
5f5c8ee5
GM
10632 /* Save some values that must not be changed. */
10633 int saved_x = it->current_x;
10634 struct text_pos saved_pos;
10635 int saved_what = it->what;
10636 int saved_face_id = it->face_id;
10637 int saved_charset = it->charset;
10638 Lisp_Object saved_object;
10639
10640 saved_object = it->object;
10641 saved_pos = it->position;
10642
10643 it->what = IT_CHARACTER;
10644 bzero (&it->position, sizeof it->position);
10645 it->object = 0;
10646 it->c = ' ';
10647 it->len = 1;
10648 it->charset = CHARSET_ASCII;
10649
10650 if (default_face_p)
10651 it->face_id = DEFAULT_FACE_ID;
10652 if (it->multibyte_p)
10653 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10654 else
10655 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
1842fc1a 10656
5f5c8ee5
GM
10657 PRODUCE_GLYPHS (it);
10658
10659 it->current_x = saved_x;
10660 it->object = saved_object;
10661 it->position = saved_pos;
10662 it->what = saved_what;
10663 it->face_id = saved_face_id;
10664 it->charset = saved_charset;
10665 }
10666 }
10667}
12adba34 10668
1842fc1a 10669
5f5c8ee5
GM
10670/* Extend the face of the last glyph in the text area of IT->glyph_row
10671 to the end of the display line. Called from display_line.
10672 If the glyph row is empty, add a space glyph to it so that we
10673 know the face to draw. Set the glyph row flag fill_line_p. */
10674
10675static void
10676extend_face_to_end_of_line (it)
10677 struct it *it;
10678{
10679 struct face *face;
10680 struct frame *f = it->f;
1842fc1a 10681
5f5c8ee5
GM
10682 /* If line is already filled, do nothing. */
10683 if (it->current_x >= it->last_visible_x)
10684 return;
10685
10686 /* Face extension extends the background and box of IT->face_id
10687 to the end of the line. If the background equals the background
10688 of the frame, we haven't to do anything. */
10689 face = FACE_FROM_ID (f, it->face_id);
10690 if (FRAME_WINDOW_P (f)
10691 && face->box == FACE_NO_BOX
10692 && face->background == FRAME_BACKGROUND_PIXEL (f)
10693 && !face->stipple)
10694 return;
1842fc1a 10695
5f5c8ee5
GM
10696 /* Set the glyph row flag indicating that the face of the last glyph
10697 in the text area has to be drawn to the end of the text area. */
10698 it->glyph_row->fill_line_p = 1;
545e04f6 10699
5f5c8ee5
GM
10700 /* If current charset of IT is not ASCII, make sure we have the
10701 ASCII face. This will be automatically undone the next time
10702 get_next_display_element returns a character from a different
10703 charset. Note that the charset will always be ASCII in unibyte
10704 text. */
10705 if (it->charset != CHARSET_ASCII)
10706 {
10707 it->charset = CHARSET_ASCII;
10708 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10709 }
545e04f6 10710
5f5c8ee5
GM
10711 if (FRAME_WINDOW_P (f))
10712 {
10713 /* If the row is empty, add a space with the current face of IT,
10714 so that we know which face to draw. */
10715 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 10716 {
5f5c8ee5
GM
10717 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10718 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10719 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 10720 }
5f5c8ee5
GM
10721 }
10722 else
10723 {
10724 /* Save some values that must not be changed. */
10725 int saved_x = it->current_x;
10726 struct text_pos saved_pos;
10727 Lisp_Object saved_object;
10728 int saved_what = it->what;
10729
10730 saved_object = it->object;
10731 saved_pos = it->position;
10732
10733 it->what = IT_CHARACTER;
10734 bzero (&it->position, sizeof it->position);
10735 it->object = 0;
10736 it->c = ' ';
10737 it->len = 1;
10738
10739 PRODUCE_GLYPHS (it);
10740
10741 while (it->current_x <= it->last_visible_x)
10742 PRODUCE_GLYPHS (it);
10743
10744 /* Don't count these blanks really. It would let us insert a left
10745 truncation glyph below and make us set the cursor on them, maybe. */
10746 it->current_x = saved_x;
10747 it->object = saved_object;
10748 it->position = saved_pos;
10749 it->what = saved_what;
10750 }
10751}
12adba34 10752
545e04f6 10753
5f5c8ee5
GM
10754/* Value is non-zero if text starting at CHARPOS in current_buffer is
10755 trailing whitespace. */
1c9241f5 10756
5f5c8ee5
GM
10757static int
10758trailing_whitespace_p (charpos)
10759 int charpos;
10760{
10761 int bytepos = CHAR_TO_BYTE (charpos);
10762 int c = 0;
7bbe686f 10763
5f5c8ee5
GM
10764 while (bytepos < ZV_BYTE
10765 && (c = FETCH_CHAR (bytepos),
10766 c == ' ' || c == '\t'))
10767 ++bytepos;
0d09d1e6 10768
8f897821
GM
10769 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
10770 {
10771 if (bytepos != PT_BYTE)
10772 return 1;
10773 }
10774 return 0;
5f5c8ee5 10775}
31b24551 10776
545e04f6 10777
5f5c8ee5 10778/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 10779
5f5c8ee5
GM
10780void
10781highlight_trailing_whitespace (f, row)
10782 struct frame *f;
10783 struct glyph_row *row;
10784{
10785 int used = row->used[TEXT_AREA];
10786
10787 if (used)
10788 {
10789 struct glyph *start = row->glyphs[TEXT_AREA];
10790 struct glyph *glyph = start + used - 1;
10791
10792 /* Skip over the space glyph inserted to display the
10793 cursor at the end of a line. */
10794 if (glyph->type == CHAR_GLYPH
10795 && glyph->u.ch.code == ' '
10796 && glyph->object == 0)
10797 --glyph;
10798
10799 /* If last glyph is a space or stretch, and it's trailing
10800 whitespace, set the face of all trailing whitespace glyphs in
10801 IT->glyph_row to `trailing-whitespace'. */
10802 if (glyph >= start
10803 && BUFFERP (glyph->object)
10804 && (glyph->type == STRETCH_GLYPH
10805 || (glyph->type == CHAR_GLYPH
10806 && glyph->u.ch.code == ' '))
10807 && trailing_whitespace_p (glyph->charpos))
545e04f6 10808 {
5f5c8ee5
GM
10809 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10810 CHARSET_ASCII);
10811
10812 while (glyph >= start
10813 && BUFFERP (glyph->object)
10814 && (glyph->type == STRETCH_GLYPH
10815 || (glyph->type == CHAR_GLYPH
10816 && glyph->u.ch.code == ' ')))
545e04f6 10817 {
5f5c8ee5
GM
10818 if (glyph->type == STRETCH_GLYPH)
10819 glyph->u.stretch.face_id = face_id;
10820 else
10821 glyph->u.ch.face_id = face_id;
10822 --glyph;
545e04f6
KH
10823 }
10824 }
a2889657 10825 }
5f5c8ee5 10826}
a2889657 10827
5fcbb24d 10828
5f5c8ee5
GM
10829/* Construct the glyph row IT->glyph_row in the desired matrix of
10830 IT->w from text at the current position of IT. See dispextern.h
10831 for an overview of struct it. Value is non-zero if
10832 IT->glyph_row displays text, as opposed to a line displaying ZV
10833 only. */
10834
10835static int
10836display_line (it)
10837 struct it *it;
10838{
10839 struct glyph_row *row = it->glyph_row;
10840
10841 /* We always start displaying at hpos zero even if hscrolled. */
10842 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 10843
5f5c8ee5
GM
10844 /* We must not display in a row that's not a text row. */
10845 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10846 < it->w->desired_matrix->nrows);
12adba34 10847
5f5c8ee5
GM
10848 /* Is IT->w showing the region? */
10849 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 10850
5f5c8ee5
GM
10851 /* Clear the result glyph row and enable it. */
10852 prepare_desired_row (row);
12adba34 10853
5f5c8ee5
GM
10854 row->y = it->current_y;
10855 row->start = it->current;
10856 row->continuation_lines_width = it->continuation_lines_width;
10857 row->displays_text_p = 1;
10858
10859 /* Arrange the overlays nicely for our purposes. Usually, we call
10860 display_line on only one line at a time, in which case this
10861 can't really hurt too much, or we call it on lines which appear
10862 one after another in the buffer, in which case all calls to
10863 recenter_overlay_lists but the first will be pretty cheap. */
10864 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10865
5f5c8ee5
GM
10866 /* Move over display elements that are not visible because we are
10867 hscrolled. This may stop at an x-position < IT->first_visible_x
10868 if the first glyph is partially visible or if we hit a line end. */
10869 if (it->current_x < it->first_visible_x)
10870 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10871 MOVE_TO_POS | MOVE_TO_X);
10872
10873 /* Get the initial row height. This is either the height of the
10874 text hscrolled, if there is any, or zero. */
10875 row->ascent = it->max_ascent;
10876 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10877 row->phys_ascent = it->max_phys_ascent;
10878 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10879
10880 /* Loop generating characters. The loop is left with IT on the next
10881 character to display. */
10882 while (1)
10883 {
10884 int n_glyphs_before, hpos_before, x_before;
10885 int x, i, nglyphs;
10886
10887 /* Retrieve the next thing to display. Value is zero if end of
10888 buffer reached. */
10889 if (!get_next_display_element (it))
10890 {
10891 /* Maybe add a space at the end of this line that is used to
10892 display the cursor there under X. */
10893 append_space (it, 1);
10894
10895 /* The position -1 below indicates a blank line not
10896 corresponding to any text, as opposed to an empty line
10897 corresponding to a line end. */
10898 if (row->used[TEXT_AREA] <= 1)
a2889657 10899 {
5f5c8ee5
GM
10900 row->glyphs[TEXT_AREA]->charpos = -1;
10901 row->displays_text_p = 0;
10902
10903 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
10904 row->indicate_empty_line_p = 1;
a2889657 10905 }
5f5c8ee5
GM
10906
10907 it->continuation_lines_width = 0;
10908 row->ends_at_zv_p = 1;
10909 break;
a2889657 10910 }
a2889657 10911
5f5c8ee5
GM
10912 /* Now, get the metrics of what we want to display. This also
10913 generates glyphs in `row' (which is IT->glyph_row). */
10914 n_glyphs_before = row->used[TEXT_AREA];
10915 x = it->current_x;
10916 PRODUCE_GLYPHS (it);
a2889657 10917
5f5c8ee5
GM
10918 /* If this display element was in marginal areas, continue with
10919 the next one. */
10920 if (it->area != TEXT_AREA)
a2889657 10921 {
5f5c8ee5
GM
10922 row->ascent = max (row->ascent, it->max_ascent);
10923 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10924 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10925 row->phys_height = max (row->phys_height,
10926 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10927 set_iterator_to_next (it);
10928 continue;
10929 }
5936754e 10930
5f5c8ee5
GM
10931 /* Does the display element fit on the line? If we truncate
10932 lines, we should draw past the right edge of the window. If
10933 we don't truncate, we want to stop so that we can display the
10934 continuation glyph before the right margin. If lines are
10935 continued, there are two possible strategies for characters
10936 resulting in more than 1 glyph (e.g. tabs): Display as many
10937 glyphs as possible in this line and leave the rest for the
10938 continuation line, or display the whole element in the next
10939 line. Original redisplay did the former, so we do it also. */
10940 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10941 hpos_before = it->hpos;
10942 x_before = x;
10943
10944 if (nglyphs == 1
10945 && it->current_x < it->last_visible_x)
10946 {
10947 ++it->hpos;
10948 row->ascent = max (row->ascent, it->max_ascent);
10949 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10950 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10951 row->phys_height = max (row->phys_height,
10952 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10953 if (it->current_x - it->pixel_width < it->first_visible_x)
10954 row->x = x - it->first_visible_x;
10955 }
10956 else
10957 {
10958 int new_x;
10959 struct glyph *glyph;
10960
10961 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 10962 {
5f5c8ee5
GM
10963 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10964 new_x = x + glyph->pixel_width;
10965
10966 if (/* Lines are continued. */
10967 !it->truncate_lines_p
10968 && (/* Glyph doesn't fit on the line. */
10969 new_x > it->last_visible_x
10970 /* Or it fits exactly on a window system frame. */
10971 || (new_x == it->last_visible_x
10972 && FRAME_WINDOW_P (it->f))))
a2889657 10973 {
5f5c8ee5
GM
10974 /* End of a continued line. */
10975
10976 if (it->hpos == 0
10977 || (new_x == it->last_visible_x
10978 && FRAME_WINDOW_P (it->f)))
10979 {
10980 /* Current glyph fits exactly on the line. We
10981 must continue the line because we can't draw
10982 the cursor after the glyph. */
10983 row->continued_p = 1;
10984 it->current_x = new_x;
10985 it->continuation_lines_width += new_x;
10986 ++it->hpos;
10987 if (i == nglyphs - 1)
10988 set_iterator_to_next (it);
10989 }
10990 else
5936754e 10991 {
5f5c8ee5
GM
10992 /* Display element draws past the right edge of
10993 the window. Restore positions to values
10994 before the element. The next line starts
10995 with current_x before the glyph that could
10996 not be displayed, so that TAB works right. */
10997 row->used[TEXT_AREA] = n_glyphs_before + i;
10998
10999 /* Display continuation glyphs. */
11000 if (!FRAME_WINDOW_P (it->f))
11001 produce_special_glyphs (it, IT_CONTINUATION);
11002 row->continued_p = 1;
11003
11004 it->current_x = x;
11005 it->continuation_lines_width += x;
5936754e 11006 }
5f5c8ee5
GM
11007 break;
11008 }
11009 else if (new_x > it->first_visible_x)
11010 {
11011 /* Increment number of glyphs actually displayed. */
11012 ++it->hpos;
11013
11014 if (x < it->first_visible_x)
11015 /* Glyph is partially visible, i.e. row starts at
11016 negative X position. */
11017 row->x = x - it->first_visible_x;
11018 }
11019 else
11020 {
11021 /* Glyph is completely off the left margin of the
11022 window. This should not happen because of the
11023 move_it_in_display_line at the start of
11024 this function. */
11025 abort ();
a2889657 11026 }
a2889657 11027 }
5f5c8ee5
GM
11028
11029 row->ascent = max (row->ascent, it->max_ascent);
11030 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11031 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11032 row->phys_height = max (row->phys_height,
11033 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11034
11035 /* End of this display line if row is continued. */
11036 if (row->continued_p)
11037 break;
a2889657 11038 }
a2889657 11039
5f5c8ee5
GM
11040 /* Is this a line end? If yes, we're also done, after making
11041 sure that a non-default face is extended up to the right
11042 margin of the window. */
11043 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 11044 {
5f5c8ee5
GM
11045 int used_before = row->used[TEXT_AREA];
11046
11047 /* Add a space at the end of the line that is used to
11048 display the cursor there. */
11049 append_space (it, 0);
11050
11051 /* Extend the face to the end of the line. */
11052 extend_face_to_end_of_line (it);
11053
11054 /* Make sure we have the position. */
11055 if (used_before == 0)
11056 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11057
11058 /* Consume the line end. This skips over invisible lines. */
11059 set_iterator_to_next (it);
11060 it->continuation_lines_width = 0;
11061 break;
1c9241f5 11062 }
a2889657 11063
5f5c8ee5
GM
11064 /* Proceed with next display element. Note that this skips
11065 over lines invisible because of selective display. */
11066 set_iterator_to_next (it);
b1d1124b 11067
5f5c8ee5
GM
11068 /* If we truncate lines, we are done when the last displayed
11069 glyphs reach past the right margin of the window. */
11070 if (it->truncate_lines_p
11071 && (FRAME_WINDOW_P (it->f)
11072 ? (it->current_x >= it->last_visible_x)
11073 : (it->current_x > it->last_visible_x)))
75d13c64 11074 {
5f5c8ee5
GM
11075 /* Maybe add truncation glyphs. */
11076 if (!FRAME_WINDOW_P (it->f))
11077 {
11078 --it->glyph_row->used[TEXT_AREA];
11079 produce_special_glyphs (it, IT_TRUNCATION);
11080 }
11081
11082 row->truncated_on_right_p = 1;
11083 it->continuation_lines_width = 0;
312246d1 11084 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
11085 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11086 it->hpos = hpos_before;
11087 it->current_x = x_before;
11088 break;
75d13c64 11089 }
a2889657 11090 }
a2889657 11091
5f5c8ee5
GM
11092 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11093 at the left window margin. */
11094 if (it->first_visible_x
11095 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11096 {
11097 if (!FRAME_WINDOW_P (it->f))
11098 insert_left_trunc_glyphs (it);
11099 row->truncated_on_left_p = 1;
11100 }
a2889657 11101
5f5c8ee5
GM
11102 /* If the start of this line is the overlay arrow-position, then
11103 mark this glyph row as the one containing the overlay arrow.
11104 This is clearly a mess with variable size fonts. It would be
11105 better to let it be displayed like cursors under X. */
e24c997d 11106 if (MARKERP (Voverlay_arrow_position)
a2889657 11107 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
11108 && (MATRIX_ROW_START_CHARPOS (row)
11109 == marker_position (Voverlay_arrow_position))
e24c997d 11110 && STRINGP (Voverlay_arrow_string)
a2889657
JB
11111 && ! overlay_arrow_seen)
11112 {
5f5c8ee5
GM
11113 /* Overlay arrow in window redisplay is a bitmap. */
11114 if (!FRAME_WINDOW_P (it->f))
c4628384 11115 {
5f5c8ee5
GM
11116 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11117 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11118 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11119 struct glyph *p = row->glyphs[TEXT_AREA];
11120 struct glyph *p2, *end;
11121
11122 /* Copy the arrow glyphs. */
11123 while (glyph < arrow_end)
11124 *p++ = *glyph++;
11125
11126 /* Throw away padding glyphs. */
11127 p2 = p;
11128 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11129 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11130 ++p2;
11131 if (p2 > p)
212e4f87 11132 {
5f5c8ee5
GM
11133 while (p2 < end)
11134 *p++ = *p2++;
11135 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 11136 }
c4628384 11137 }
5f5c8ee5 11138
a2889657 11139 overlay_arrow_seen = 1;
5f5c8ee5 11140 row->overlay_arrow_p = 1;
a2889657
JB
11141 }
11142
5f5c8ee5
GM
11143 /* Compute pixel dimensions of this line. */
11144 compute_line_metrics (it);
11145
11146 /* Remember the position at which this line ends. */
11147 row->end = it->current;
11148
11149 /* Maybe set the cursor. If you change this, it's probably a good
11150 idea to also change the code in redisplay_window for cursor
11151 movement in an unchanged window. */
11152 if (it->w->cursor.vpos < 0
11153 && PT >= MATRIX_ROW_START_CHARPOS (row)
11154 && MATRIX_ROW_END_CHARPOS (row) >= PT
11155 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11156 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11157 || !row->ends_at_zv_p)))
11158 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11159
11160 /* Highlight trailing whitespace. */
8f897821 11161 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
11162 highlight_trailing_whitespace (it->f, it->glyph_row);
11163
11164 /* Prepare for the next line. This line starts horizontally at (X
11165 HPOS) = (0 0). Vertical positions are incremented. As a
11166 convenience for the caller, IT->glyph_row is set to the next
11167 row to be used. */
11168 it->current_x = it->hpos = 0;
11169 it->current_y += row->height;
11170 ++it->vpos;
11171 ++it->glyph_row;
11172 return row->displays_text_p;
a2889657 11173}
5f5c8ee5
GM
11174
11175
a2889657 11176\f
5f5c8ee5
GM
11177/***********************************************************************
11178 Menu Bar
11179 ***********************************************************************/
11180
11181/* Redisplay the menu bar in the frame for window W.
11182
11183 The menu bar of X frames that don't have X toolkit support is
11184 displayed in a special window W->frame->menu_bar_window.
11185
11186 The menu bar of terminal frames is treated specially as far as
11187 glyph matrices are concerned. Menu bar lines are not part of
11188 windows, so the update is done directly on the frame matrix rows
11189 for the menu bar. */
7ce2c095
RS
11190
11191static void
11192display_menu_bar (w)
11193 struct window *w;
11194{
5f5c8ee5
GM
11195 struct frame *f = XFRAME (WINDOW_FRAME (w));
11196 struct it it;
11197 Lisp_Object items;
8351baf2 11198 int i;
7ce2c095 11199
5f5c8ee5 11200 /* Don't do all this for graphical frames. */
dc937613 11201#ifdef HAVE_NTGUI
d129c4c2
KH
11202 if (!NILP (Vwindow_system))
11203 return;
dc937613 11204#endif
dc937613 11205#ifdef USE_X_TOOLKIT
d3413a53 11206 if (FRAME_X_P (f))
7ce2c095 11207 return;
5f5c8ee5
GM
11208#endif
11209
11210#ifdef USE_X_TOOLKIT
11211 xassert (!FRAME_WINDOW_P (f));
11212 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
11213 it.first_visible_x = 0;
11214 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11215#else /* not USE_X_TOOLKIT */
11216 if (FRAME_WINDOW_P (f))
11217 {
11218 /* Menu bar lines are displayed in the desired matrix of the
11219 dummy window menu_bar_window. */
11220 struct window *menu_w;
11221 xassert (WINDOWP (f->menu_bar_window));
11222 menu_w = XWINDOW (f->menu_bar_window);
11223 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11224 MODE_LINE_FACE_ID);
11225 it.first_visible_x = 0;
11226 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11227 }
11228 else
11229 {
11230 /* This is a TTY frame, i.e. character hpos/vpos are used as
11231 pixel x/y. */
11232 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11233 MODE_LINE_FACE_ID);
11234 it.first_visible_x = 0;
11235 it.last_visible_x = FRAME_WIDTH (f);
11236 }
11237#endif /* not USE_X_TOOLKIT */
11238
11239 /* Clear all rows of the menu bar. */
11240 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11241 {
11242 struct glyph_row *row = it.glyph_row + i;
11243 clear_glyph_row (row);
11244 row->enabled_p = 1;
11245 row->full_width_p = 1;
11246 }
7ce2c095 11247
5f5c8ee5
GM
11248 /* Make the first line of the menu bar appear in reverse video. */
11249 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 11250
5f5c8ee5
GM
11251 /* Display all items of the menu bar. */
11252 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 11253 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 11254 {
5f5c8ee5
GM
11255 Lisp_Object string;
11256
11257 /* Stop at nil string. */
8351baf2
RS
11258 string = XVECTOR (items)->contents[i + 1];
11259 if (NILP (string))
11260 break;
2d66ad19 11261
5f5c8ee5
GM
11262 /* Remember where item was displayed. */
11263 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 11264
5f5c8ee5
GM
11265 /* Display the item, pad with one space. */
11266 if (it.current_x < it.last_visible_x)
11267 display_string (NULL, string, Qnil, 0, 0, &it,
11268 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
11269 }
11270
2d66ad19 11271 /* Fill out the line with spaces. */
5f5c8ee5
GM
11272 if (it.current_x < it.last_visible_x)
11273 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 11274
5f5c8ee5
GM
11275 /* Compute the total height of the lines. */
11276 compute_line_metrics (&it);
7ce2c095 11277}
5f5c8ee5
GM
11278
11279
7ce2c095 11280\f
5f5c8ee5
GM
11281/***********************************************************************
11282 Mode Line
11283 ***********************************************************************/
11284
11285/* Display the mode and/or top line of window W. */
a2889657
JB
11286
11287static void
5f5c8ee5 11288display_mode_lines (w)
a2889657
JB
11289 struct window *w;
11290{
5f5c8ee5 11291 /* These will be set while the mode line specs are processed. */
aa6d10fa 11292 line_number_displayed = 0;
155ef550 11293 w->column_number_displayed = Qnil;
aa6d10fa 11294
5f5c8ee5
GM
11295 if (WINDOW_WANTS_MODELINE_P (w))
11296 display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
11297
11298 if (WINDOW_WANTS_TOP_LINE_P (w))
11299 display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
11300}
03b294dc 11301
03b294dc 11302
5f5c8ee5
GM
11303/* Display mode or top line of window W. FACE_ID specifies which line
11304 to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
11305 FORMAT is the mode line format to display. */
03b294dc 11306
5f5c8ee5
GM
11307static void
11308display_mode_line (w, face_id, format)
11309 struct window *w;
11310 enum face_id face_id;
11311 Lisp_Object format;
11312{
11313 struct it it;
11314 struct face *face;
03b294dc 11315
5f5c8ee5
GM
11316 init_iterator (&it, w, -1, -1, NULL, face_id);
11317 prepare_desired_row (it.glyph_row);
11318
11319 /* Temporarily make frame's keyboard the current kboard so that
11320 kboard-local variables in the mode_line_format will get the right
11321 values. */
11322 push_frame_kboard (it.f);
11323 display_mode_element (&it, 0, 0, 0, format);
11324 pop_frame_kboard ();
a2889657 11325
5f5c8ee5
GM
11326 /* Fill up with spaces. */
11327 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11328
11329 compute_line_metrics (&it);
11330 it.glyph_row->full_width_p = 1;
11331 it.glyph_row->mode_line_p = 1;
11332 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11333 it.glyph_row->continued_p = 0;
11334 it.glyph_row->truncated_on_left_p = 0;
11335 it.glyph_row->truncated_on_right_p = 0;
11336
11337 /* Make a 3D mode-line have a shadow at its right end. */
11338 face = FACE_FROM_ID (it.f, face_id);
11339 extend_face_to_end_of_line (&it);
11340 if (face->box != FACE_NO_BOX)
d7eb09a0 11341 {
5f5c8ee5
GM
11342 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11343 + it.glyph_row->used[TEXT_AREA] - 1);
11344 last->right_box_line_p = 1;
d7eb09a0 11345 }
a2889657
JB
11346}
11347
a2889657 11348
5f5c8ee5
GM
11349/* Contribute ELT to the mode line for window IT->w. How it
11350 translates into text depends on its data type.
a2889657 11351
5f5c8ee5 11352 IT describes the display environment in which we display, as usual.
a2889657
JB
11353
11354 DEPTH is the depth in recursion. It is used to prevent
11355 infinite recursion here.
11356
5f5c8ee5
GM
11357 FIELD_WIDTH is the number of characters the display of ELT should
11358 occupy in the mode line, and PRECISION is the maximum number of
11359 characters to display from ELT's representation. See
11360 display_string for details. *
a2889657 11361
5f5c8ee5 11362 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
11363
11364static int
5f5c8ee5
GM
11365display_mode_element (it, depth, field_width, precision, elt)
11366 struct it *it;
a2889657 11367 int depth;
5f5c8ee5
GM
11368 int field_width, precision;
11369 Lisp_Object elt;
a2889657 11370{
5f5c8ee5
GM
11371 int n = 0, field, prec;
11372
a2889657
JB
11373 tail_recurse:
11374 if (depth > 10)
11375 goto invalid;
11376
11377 depth++;
11378
0220c518 11379 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
11380 {
11381 case Lisp_String:
11382 {
11383 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
11384 unsigned char c;
11385 unsigned char *this = XSTRING (elt)->data;
11386 unsigned char *lisp_string = this;
11387
11388 while ((precision <= 0 || n < precision)
11389 && *this
11390 && (frame_title_ptr
11391 || it->current_x < it->last_visible_x))
a2889657
JB
11392 {
11393 unsigned char *last = this;
5f5c8ee5
GM
11394
11395 /* Advance to end of string or next format specifier. */
a2889657
JB
11396 while ((c = *this++) != '\0' && c != '%')
11397 ;
5f5c8ee5 11398
a2889657
JB
11399 if (this - 1 != last)
11400 {
5f5c8ee5
GM
11401 /* Output to end of string or up to '%'. Field width
11402 is length of string. Don't output more than
11403 PRECISION allows us. */
11404 prec = --this - last;
11405 if (precision > 0 && prec > precision - n)
11406 prec = precision - n;
11407
d39b6696 11408 if (frame_title_ptr)
5f5c8ee5 11409 n += store_frame_title (last, prec, prec);
d39b6696 11410 else
5f5c8ee5
GM
11411 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11412 it, 0, prec, 0, -1);
a2889657
JB
11413 }
11414 else /* c == '%' */
11415 {
5f5c8ee5
GM
11416 unsigned char *percent_position = this;
11417
11418 /* Get the specified minimum width. Zero means
11419 don't pad. */
11420 field = 0;
a2889657 11421 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 11422 field = field * 10 + c - '0';
a2889657 11423
5f5c8ee5
GM
11424 /* Don't pad beyond the total padding allowed. */
11425 if (field_width - n > 0 && field > field_width - n)
11426 field = field_width - n;
a2889657 11427
5f5c8ee5
GM
11428 /* Note that either PRECISION <= 0 or N < PRECISION. */
11429 prec = precision - n;
11430
a2889657 11431 if (c == 'M')
5f5c8ee5
GM
11432 n += display_mode_element (it, depth, field, prec,
11433 Vglobal_mode_string);
a2889657 11434 else if (c != 0)
d39b6696 11435 {
5f5c8ee5
GM
11436 unsigned char *spec
11437 = decode_mode_spec (it->w, c, field, prec);
11438
d39b6696 11439 if (frame_title_ptr)
5f5c8ee5 11440 n += store_frame_title (spec, field, prec);
d39b6696 11441 else
5f5c8ee5
GM
11442 {
11443 int nglyphs_before
11444 = it->glyph_row->used[TEXT_AREA];
11445 int charpos
11446 = percent_position - XSTRING (elt)->data;
11447 int nwritten
11448 = display_string (spec, Qnil, elt, charpos, 0, it,
11449 field, prec, 0, -1);
11450
11451 /* Assign to the glyphs written above the
11452 string where the `%x' came from, position
11453 of the `%'. */
11454 if (nwritten > 0)
11455 {
11456 struct glyph *glyph
11457 = (it->glyph_row->glyphs[TEXT_AREA]
11458 + nglyphs_before);
11459 int i;
11460
11461 for (i = 0; i < nwritten; ++i)
11462 {
11463 glyph[i].object = elt;
11464 glyph[i].charpos = charpos;
11465 }
11466
11467 n += nwritten;
11468 }
11469 }
d39b6696 11470 }
a2889657
JB
11471 }
11472 }
11473 }
11474 break;
11475
11476 case Lisp_Symbol:
11477 /* A symbol: process the value of the symbol recursively
11478 as if it appeared here directly. Avoid error if symbol void.
11479 Special case: if value of symbol is a string, output the string
11480 literally. */
11481 {
11482 register Lisp_Object tem;
11483 tem = Fboundp (elt);
265a9e55 11484 if (!NILP (tem))
a2889657
JB
11485 {
11486 tem = Fsymbol_value (elt);
11487 /* If value is a string, output that string literally:
11488 don't check for % within it. */
e24c997d 11489 if (STRINGP (tem))
d39b6696 11490 {
5f5c8ee5
GM
11491 prec = XSTRING (tem)->size;
11492 if (precision > 0 && prec > precision - n)
11493 prec = precision - n;
d39b6696 11494 if (frame_title_ptr)
5f5c8ee5 11495 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 11496 else
5f5c8ee5
GM
11497 n += display_string (NULL, tem, Qnil, 0, 0, it,
11498 0, prec, 0, -1);
d39b6696 11499 }
a2889657 11500 else if (!EQ (tem, elt))
5f5c8ee5
GM
11501 {
11502 /* Give up right away for nil or t. */
11503 elt = tem;
11504 goto tail_recurse;
11505 }
a2889657
JB
11506 }
11507 }
11508 break;
11509
11510 case Lisp_Cons:
11511 {
11512 register Lisp_Object car, tem;
11513
11514 /* A cons cell: three distinct cases.
11515 If first element is a string or a cons, process all the elements
11516 and effectively concatenate them.
11517 If first element is a negative number, truncate displaying cdr to
11518 at most that many characters. If positive, pad (with spaces)
11519 to at least that many characters.
11520 If first element is a symbol, process the cadr or caddr recursively
11521 according to whether the symbol's value is non-nil or nil. */
11522 car = XCONS (elt)->car;
5f5c8ee5
GM
11523 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11524 {
11525 /* An element of the form (:eval FORM) means evaluate FORM
11526 and use the result as mode line elements. */
11527 struct gcpro gcpro1;
11528 Lisp_Object spec;
11529
11530 spec = eval_form (XCAR (XCDR (elt)));
11531 GCPRO1 (spec);
11532 n += display_mode_element (it, depth, field_width - n,
11533 precision - n, spec);
11534 UNGCPRO;
11535 }
11536 else if (SYMBOLP (car))
a2889657
JB
11537 {
11538 tem = Fboundp (car);
11539 elt = XCONS (elt)->cdr;
e24c997d 11540 if (!CONSP (elt))
a2889657
JB
11541 goto invalid;
11542 /* elt is now the cdr, and we know it is a cons cell.
11543 Use its car if CAR has a non-nil value. */
265a9e55 11544 if (!NILP (tem))
a2889657
JB
11545 {
11546 tem = Fsymbol_value (car);
265a9e55 11547 if (!NILP (tem))
a2889657
JB
11548 { elt = XCONS (elt)->car; goto tail_recurse; }
11549 }
11550 /* Symbol's value is nil (or symbol is unbound)
11551 Get the cddr of the original list
11552 and if possible find the caddr and use that. */
11553 elt = XCONS (elt)->cdr;
265a9e55 11554 if (NILP (elt))
a2889657 11555 break;
e24c997d 11556 else if (!CONSP (elt))
a2889657
JB
11557 goto invalid;
11558 elt = XCONS (elt)->car;
11559 goto tail_recurse;
11560 }
e24c997d 11561 else if (INTEGERP (car))
a2889657
JB
11562 {
11563 register int lim = XINT (car);
11564 elt = XCONS (elt)->cdr;
11565 if (lim < 0)
5f5c8ee5
GM
11566 {
11567 /* Negative int means reduce maximum width. */
11568 if (precision <= 0)
11569 precision = -lim;
11570 else
11571 precision = min (precision, -lim);
11572 }
a2889657
JB
11573 else if (lim > 0)
11574 {
11575 /* Padding specified. Don't let it be more than
11576 current maximum. */
5f5c8ee5
GM
11577 if (precision > 0)
11578 lim = min (precision, lim);
11579
a2889657
JB
11580 /* If that's more padding than already wanted, queue it.
11581 But don't reduce padding already specified even if
11582 that is beyond the current truncation point. */
5f5c8ee5 11583 field_width = max (lim, field_width);
a2889657
JB
11584 }
11585 goto tail_recurse;
11586 }
e24c997d 11587 else if (STRINGP (car) || CONSP (car))
a2889657
JB
11588 {
11589 register int limit = 50;
5f5c8ee5
GM
11590 /* Limit is to protect against circular lists. */
11591 while (CONSP (elt)
11592 && --limit > 0
11593 && (precision <= 0 || n < precision))
a2889657 11594 {
5f5c8ee5
GM
11595 n += display_mode_element (it, depth, field_width - n,
11596 precision - n, XCONS (elt)->car);
a2889657
JB
11597 elt = XCONS (elt)->cdr;
11598 }
11599 }
11600 }
11601 break;
11602
11603 default:
11604 invalid:
d39b6696 11605 if (frame_title_ptr)
5f5c8ee5 11606 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 11607 else
5f5c8ee5
GM
11608 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11609 precision - n, 0, 0);
11610 return n;
a2889657
JB
11611 }
11612
5f5c8ee5
GM
11613 /* Pad to FIELD_WIDTH. */
11614 if (field_width > 0 && n < field_width)
11615 {
11616 if (frame_title_ptr)
11617 n += store_frame_title ("", field_width - n, 0);
11618 else
11619 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11620 0, 0, 0);
11621 }
11622
11623 return n;
a2889657 11624}
5f5c8ee5
GM
11625
11626
766525bc
RS
11627/* Write a null-terminated, right justified decimal representation of
11628 the positive integer D to BUF using a minimal field width WIDTH. */
11629
11630static void
11631pint2str (buf, width, d)
11632 register char *buf;
11633 register int width;
11634 register int d;
11635{
11636 register char *p = buf;
11637
11638 if (d <= 0)
5f5c8ee5 11639 *p++ = '0';
766525bc 11640 else
5f5c8ee5 11641 {
766525bc 11642 while (d > 0)
5f5c8ee5 11643 {
766525bc
RS
11644 *p++ = d % 10 + '0';
11645 d /= 10;
5f5c8ee5
GM
11646 }
11647 }
11648
11649 for (width -= (int) (p - buf); width > 0; --width)
11650 *p++ = ' ';
766525bc
RS
11651 *p-- = '\0';
11652 while (p > buf)
5f5c8ee5 11653 {
766525bc
RS
11654 d = *buf;
11655 *buf++ = *p;
11656 *p-- = d;
5f5c8ee5 11657 }
766525bc
RS
11658}
11659
5f5c8ee5 11660/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
11661 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11662 type of CODING_SYSTEM. Return updated pointer into BUF. */
11663
6693a99a 11664static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 11665
1c9241f5
KH
11666static char *
11667decode_mode_spec_coding (coding_system, buf, eol_flag)
11668 Lisp_Object coding_system;
11669 register char *buf;
11670 int eol_flag;
11671{
1e1078d6 11672 Lisp_Object val;
916848d8 11673 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
11674 unsigned char *eol_str;
11675 int eol_str_len;
11676 /* The EOL conversion we are using. */
11677 Lisp_Object eoltype;
1e1078d6
RS
11678
11679 val = coding_system;
1c9241f5
KH
11680
11681 if (NILP (val)) /* Not yet decided. */
11682 {
916848d8
RS
11683 if (multibyte)
11684 *buf++ = '-';
21e989e3 11685 if (eol_flag)
302f2b38 11686 eoltype = eol_mnemonic_undecided;
1e1078d6 11687 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
11688 }
11689 else
11690 {
1e1078d6
RS
11691 Lisp_Object eolvalue;
11692
11693 eolvalue = Fget (coding_system, Qeol_type);
11694
1c9241f5 11695 while (!NILP (val) && SYMBOLP (val))
1e1078d6
RS
11696 {
11697 val = Fget (val, Qcoding_system);
11698 if (NILP (eolvalue))
b070c1d7 11699 eolvalue = Fget (val, Qeol_type);
1e1078d6
RS
11700 }
11701
916848d8
RS
11702 if (multibyte)
11703 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11704
1c9241f5
KH
11705 if (eol_flag)
11706 {
1e1078d6
RS
11707 /* The EOL conversion that is normal on this system. */
11708
11709 if (NILP (eolvalue)) /* Not yet decided. */
11710 eoltype = eol_mnemonic_undecided;
11711 else if (VECTORP (eolvalue)) /* Not yet decided. */
11712 eoltype = eol_mnemonic_undecided;
11713 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11714 eoltype = (XFASTINT (eolvalue) == 0
11715 ? eol_mnemonic_unix
11716 : (XFASTINT (eolvalue) == 1
11717 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
11718 }
11719 }
5f5c8ee5 11720
302f2b38
EZ
11721 if (eol_flag)
11722 {
11723 /* Mention the EOL conversion if it is not the usual one. */
11724 if (STRINGP (eoltype))
11725 {
11726 eol_str = XSTRING (eoltype)->data;
11727 eol_str_len = XSTRING (eoltype)->size;
11728 }
f30b3499
KH
11729 else if (INTEGERP (eoltype)
11730 && CHAR_VALID_P (XINT (eoltype), 0))
11731 {
f30b3499
KH
11732 unsigned char work[4];
11733
11734 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11735 }
302f2b38
EZ
11736 else
11737 {
11738 eol_str = invalid_eol_type;
11739 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 11740 }
f30b3499 11741 bcopy (eol_str, buf, eol_str_len);
302f2b38 11742 buf += eol_str_len;
1c9241f5 11743 }
302f2b38 11744
1c9241f5
KH
11745 return buf;
11746}
11747
a2889657 11748/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
11749 generated by character C. PRECISION >= 0 means don't return a
11750 string longer than that value. FIELD_WIDTH > 0 means pad the
11751 string returned with spaces to that value. */
a2889657 11752
11e82b76
JB
11753static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11754
a2889657 11755static char *
5f5c8ee5 11756decode_mode_spec (w, c, field_width, precision)
a2889657
JB
11757 struct window *w;
11758 register char c;
5f5c8ee5 11759 int field_width, precision;
a2889657 11760{
0b67772d 11761 Lisp_Object obj;
5f5c8ee5
GM
11762 struct frame *f = XFRAME (WINDOW_FRAME (w));
11763 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 11764 struct buffer *b = XBUFFER (w->buffer);
a2889657 11765
0b67772d 11766 obj = Qnil;
a2889657
JB
11767
11768 switch (c)
11769 {
1af9f229
RS
11770 case '*':
11771 if (!NILP (b->read_only))
11772 return "%";
11773 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11774 return "*";
11775 return "-";
11776
11777 case '+':
11778 /* This differs from %* only for a modified read-only buffer. */
11779 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11780 return "*";
11781 if (!NILP (b->read_only))
11782 return "%";
11783 return "-";
11784
11785 case '&':
11786 /* This differs from %* in ignoring read-only-ness. */
11787 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11788 return "*";
11789 return "-";
11790
11791 case '%':
11792 return "%";
11793
11794 case '[':
11795 {
11796 int i;
11797 char *p;
11798
11799 if (command_loop_level > 5)
11800 return "[[[... ";
11801 p = decode_mode_spec_buf;
11802 for (i = 0; i < command_loop_level; i++)
11803 *p++ = '[';
11804 *p = 0;
11805 return decode_mode_spec_buf;
11806 }
11807
11808 case ']':
11809 {
11810 int i;
11811 char *p;
11812
11813 if (command_loop_level > 5)
11814 return " ...]]]";
11815 p = decode_mode_spec_buf;
11816 for (i = 0; i < command_loop_level; i++)
11817 *p++ = ']';
11818 *p = 0;
11819 return decode_mode_spec_buf;
11820 }
11821
11822 case '-':
11823 {
1af9f229 11824 register int i;
5f5c8ee5
GM
11825
11826 /* Let lots_of_dashes be a string of infinite length. */
11827 if (field_width <= 0
11828 || field_width > sizeof (lots_of_dashes))
1af9f229 11829 {
5f5c8ee5
GM
11830 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11831 decode_mode_spec_buf[i] = '-';
11832 decode_mode_spec_buf[i] = '\0';
11833 return decode_mode_spec_buf;
1af9f229 11834 }
5f5c8ee5
GM
11835 else
11836 return lots_of_dashes;
1af9f229
RS
11837 }
11838
a2889657 11839 case 'b':
d39b6696 11840 obj = b->name;
a2889657
JB
11841 break;
11842
1af9f229
RS
11843 case 'c':
11844 {
11845 int col = current_column ();
11846 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 11847 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
11848 return decode_mode_spec_buf;
11849 }
11850
11851 case 'F':
11852 /* %F displays the frame name. */
5f5c8ee5 11853 if (!NILP (f->title))
95184b48 11854 return (char *) XSTRING (f->title)->data;
fd8ff63d 11855 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 11856 return (char *) XSTRING (f->name)->data;
9c6da96f 11857 return "Emacs";
1af9f229 11858
a2889657 11859 case 'f':
d39b6696 11860 obj = b->filename;
a2889657
JB
11861 break;
11862
aa6d10fa
RS
11863 case 'l':
11864 {
12adba34
RS
11865 int startpos = XMARKER (w->start)->charpos;
11866 int startpos_byte = marker_byte_position (w->start);
11867 int line, linepos, linepos_byte, topline;
aa6d10fa 11868 int nlines, junk;
aa6d10fa
RS
11869 int height = XFASTINT (w->height);
11870
11871 /* If we decided that this buffer isn't suitable for line numbers,
11872 don't forget that too fast. */
11873 if (EQ (w->base_line_pos, w->buffer))
766525bc 11874 goto no_value;
5300fd39
RS
11875 /* But do forget it, if the window shows a different buffer now. */
11876 else if (BUFFERP (w->base_line_pos))
11877 w->base_line_pos = Qnil;
aa6d10fa
RS
11878
11879 /* If the buffer is very big, don't waste time. */
d39b6696 11880 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
11881 {
11882 w->base_line_pos = Qnil;
11883 w->base_line_number = Qnil;
766525bc 11884 goto no_value;
aa6d10fa
RS
11885 }
11886
11887 if (!NILP (w->base_line_number)
11888 && !NILP (w->base_line_pos)
12adba34 11889 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
11890 {
11891 line = XFASTINT (w->base_line_number);
11892 linepos = XFASTINT (w->base_line_pos);
12adba34 11893 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
11894 }
11895 else
11896 {
11897 line = 1;
d39b6696 11898 linepos = BUF_BEGV (b);
12adba34 11899 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
11900 }
11901
11902 /* Count lines from base line to window start position. */
12adba34
RS
11903 nlines = display_count_lines (linepos, linepos_byte,
11904 startpos_byte,
11905 startpos, &junk);
aa6d10fa
RS
11906
11907 topline = nlines + line;
11908
11909 /* Determine a new base line, if the old one is too close
11910 or too far away, or if we did not have one.
11911 "Too close" means it's plausible a scroll-down would
11912 go back past it. */
d39b6696 11913 if (startpos == BUF_BEGV (b))
aa6d10fa 11914 {
c2213350
KH
11915 XSETFASTINT (w->base_line_number, topline);
11916 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
11917 }
11918 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 11919 || linepos == BUF_BEGV (b))
aa6d10fa 11920 {
d39b6696 11921 int limit = BUF_BEGV (b);
12adba34 11922 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 11923 int position;
5d121aec 11924 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
11925
11926 if (startpos - distance > limit)
12adba34
RS
11927 {
11928 limit = startpos - distance;
11929 limit_byte = CHAR_TO_BYTE (limit);
11930 }
aa6d10fa 11931
12adba34
RS
11932 nlines = display_count_lines (startpos, startpos_byte,
11933 limit_byte,
11934 - (height * 2 + 30),
aa6d10fa
RS
11935 &position);
11936 /* If we couldn't find the lines we wanted within
5d121aec 11937 line_number_display_limit_width chars per line,
aa6d10fa 11938 give up on line numbers for this window. */
12adba34 11939 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
11940 {
11941 w->base_line_pos = w->buffer;
11942 w->base_line_number = Qnil;
766525bc 11943 goto no_value;
aa6d10fa
RS
11944 }
11945
c2213350 11946 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 11947 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
11948 }
11949
11950 /* Now count lines from the start pos to point. */
12adba34
RS
11951 nlines = display_count_lines (startpos, startpos_byte,
11952 PT_BYTE, PT, &junk);
aa6d10fa
RS
11953
11954 /* Record that we did display the line number. */
11955 line_number_displayed = 1;
11956
11957 /* Make the string to show. */
5f5c8ee5 11958 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 11959 return decode_mode_spec_buf;
766525bc
RS
11960 no_value:
11961 {
11962 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
11963 int pad = field_width - 2;
11964 while (pad-- > 0)
11965 *p++ = ' ';
11966 *p++ = '?';
11967 *p = '?';
766525bc
RS
11968 return decode_mode_spec_buf;
11969 }
aa6d10fa
RS
11970 }
11971 break;
11972
a2889657 11973 case 'm':
d39b6696 11974 obj = b->mode_name;
a2889657
JB
11975 break;
11976
11977 case 'n':
d39b6696 11978 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
11979 return " Narrow";
11980 break;
11981
a2889657
JB
11982 case 'p':
11983 {
11984 int pos = marker_position (w->start);
d39b6696 11985 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 11986
d39b6696 11987 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 11988 {
d39b6696 11989 if (pos <= BUF_BEGV (b))
a2889657
JB
11990 return "All";
11991 else
11992 return "Bottom";
11993 }
d39b6696 11994 else if (pos <= BUF_BEGV (b))
a2889657
JB
11995 return "Top";
11996 else
11997 {
3c7d31b9
RS
11998 if (total > 1000000)
11999 /* Do it differently for a large value, to avoid overflow. */
12000 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12001 else
12002 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
12003 /* We can't normally display a 3-digit number,
12004 so get us a 2-digit number that is close. */
12005 if (total == 100)
12006 total = 99;
12007 sprintf (decode_mode_spec_buf, "%2d%%", total);
12008 return decode_mode_spec_buf;
12009 }
12010 }
12011
8ffcb79f
RS
12012 /* Display percentage of size above the bottom of the screen. */
12013 case 'P':
12014 {
12015 int toppos = marker_position (w->start);
d39b6696
KH
12016 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12017 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 12018
d39b6696 12019 if (botpos >= BUF_ZV (b))
8ffcb79f 12020 {
d39b6696 12021 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12022 return "All";
12023 else
12024 return "Bottom";
12025 }
12026 else
12027 {
3c7d31b9
RS
12028 if (total > 1000000)
12029 /* Do it differently for a large value, to avoid overflow. */
12030 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12031 else
12032 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
12033 /* We can't normally display a 3-digit number,
12034 so get us a 2-digit number that is close. */
12035 if (total == 100)
12036 total = 99;
d39b6696 12037 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12038 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12039 else
12040 sprintf (decode_mode_spec_buf, "%2d%%", total);
12041 return decode_mode_spec_buf;
12042 }
12043 }
12044
1af9f229
RS
12045 case 's':
12046 /* status of process */
12047 obj = Fget_buffer_process (w->buffer);
12048 if (NILP (obj))
12049 return "no process";
12050#ifdef subprocesses
12051 obj = Fsymbol_name (Fprocess_status (obj));
12052#endif
12053 break;
d39b6696 12054
1af9f229
RS
12055 case 't': /* indicate TEXT or BINARY */
12056#ifdef MODE_LINE_BINARY_TEXT
12057 return MODE_LINE_BINARY_TEXT (b);
12058#else
12059 return "T";
12060#endif
1c9241f5
KH
12061
12062 case 'z':
12063 /* coding-system (not including end-of-line format) */
12064 case 'Z':
12065 /* coding-system (including end-of-line type) */
12066 {
12067 int eol_flag = (c == 'Z');
539b4d41 12068 char *p = decode_mode_spec_buf;
1c9241f5 12069
d30e754b 12070 if (! FRAME_WINDOW_P (f))
1c9241f5 12071 {
11c52c4f
RS
12072 /* No need to mention EOL here--the terminal never needs
12073 to do EOL conversion. */
12074 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12075 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 12076 }
f13c925f 12077 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 12078 p, eol_flag);
f13c925f 12079
11c52c4f 12080#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
12081#ifdef subprocesses
12082 obj = Fget_buffer_process (Fcurrent_buffer ());
12083 if (PROCESSP (obj))
12084 {
12085 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12086 p, eol_flag);
12087 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12088 p, eol_flag);
12089 }
12090#endif /* subprocesses */
11c52c4f 12091#endif /* 0 */
1c9241f5
KH
12092 *p = 0;
12093 return decode_mode_spec_buf;
12094 }
a2889657 12095 }
d39b6696 12096
e24c997d 12097 if (STRINGP (obj))
a2889657
JB
12098 return (char *) XSTRING (obj)->data;
12099 else
12100 return "";
12101}
5f5c8ee5
GM
12102
12103
12adba34
RS
12104/* Count up to COUNT lines starting from START / START_BYTE.
12105 But don't go beyond LIMIT_BYTE.
12106 Return the number of lines thus found (always nonnegative).
59b49f63 12107
12adba34 12108 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
12109
12110static int
12adba34
RS
12111display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12112 int start, start_byte, limit_byte, count;
12113 int *byte_pos_ptr;
59b49f63 12114{
59b49f63
RS
12115 register unsigned char *cursor;
12116 unsigned char *base;
12117
12118 register int ceiling;
12119 register unsigned char *ceiling_addr;
12adba34 12120 int orig_count = count;
59b49f63
RS
12121
12122 /* If we are not in selective display mode,
12123 check only for newlines. */
12adba34
RS
12124 int selective_display = (!NILP (current_buffer->selective_display)
12125 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
12126
12127 if (count > 0)
12adba34
RS
12128 {
12129 while (start_byte < limit_byte)
12130 {
12131 ceiling = BUFFER_CEILING_OF (start_byte);
12132 ceiling = min (limit_byte - 1, ceiling);
12133 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12134 base = (cursor = BYTE_POS_ADDR (start_byte));
12135 while (1)
12136 {
12137 if (selective_display)
12138 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12139 ;
12140 else
12141 while (*cursor != '\n' && ++cursor != ceiling_addr)
12142 ;
12143
12144 if (cursor != ceiling_addr)
12145 {
12146 if (--count == 0)
12147 {
12148 start_byte += cursor - base + 1;
12149 *byte_pos_ptr = start_byte;
12150 return orig_count;
12151 }
12152 else
12153 if (++cursor == ceiling_addr)
12154 break;
12155 }
12156 else
12157 break;
12158 }
12159 start_byte += cursor - base;
12160 }
12161 }
59b49f63
RS
12162 else
12163 {
12adba34
RS
12164 while (start_byte > limit_byte)
12165 {
12166 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12167 ceiling = max (limit_byte, ceiling);
12168 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12169 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
12170 while (1)
12171 {
12adba34
RS
12172 if (selective_display)
12173 while (--cursor != ceiling_addr
12174 && *cursor != '\n' && *cursor != 015)
12175 ;
12176 else
12177 while (--cursor != ceiling_addr && *cursor != '\n')
12178 ;
12179
59b49f63
RS
12180 if (cursor != ceiling_addr)
12181 {
12182 if (++count == 0)
12183 {
12adba34
RS
12184 start_byte += cursor - base + 1;
12185 *byte_pos_ptr = start_byte;
12186 /* When scanning backwards, we should
12187 not count the newline posterior to which we stop. */
12188 return - orig_count - 1;
59b49f63
RS
12189 }
12190 }
12191 else
12192 break;
12193 }
12adba34
RS
12194 /* Here we add 1 to compensate for the last decrement
12195 of CURSOR, which took it past the valid range. */
12196 start_byte += cursor - base + 1;
59b49f63
RS
12197 }
12198 }
12199
12adba34 12200 *byte_pos_ptr = limit_byte;
aa6d10fa 12201
12adba34
RS
12202 if (count < 0)
12203 return - orig_count + count;
12204 return orig_count - count;
aa6d10fa 12205
12adba34 12206}
a2889657 12207
a2889657 12208
5f5c8ee5
GM
12209\f
12210/***********************************************************************
12211 Displaying strings
12212 ***********************************************************************/
278feba9 12213
5f5c8ee5 12214/* Display a NUL-terminated string, starting with index START.
a3788d53 12215
5f5c8ee5
GM
12216 If STRING is non-null, display that C string. Otherwise, the Lisp
12217 string LISP_STRING is displayed.
a2889657 12218
5f5c8ee5
GM
12219 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12220 FACE_STRING. Display STRING or LISP_STRING with the face at
12221 FACE_STRING_POS in FACE_STRING:
a2889657 12222
5f5c8ee5
GM
12223 Display the string in the environment given by IT, but use the
12224 standard display table, temporarily.
a3788d53 12225
5f5c8ee5
GM
12226 FIELD_WIDTH is the minimum number of output glyphs to produce.
12227 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12228 with spaces. If STRING has more characters, more than FIELD_WIDTH
12229 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12230
12231 PRECISION is the maximum number of characters to output from
12232 STRING. PRECISION < 0 means don't truncate the string.
a2889657 12233
5f5c8ee5 12234 This is roughly equivalent to printf format specifiers:
a2889657 12235
5f5c8ee5
GM
12236 FIELD_WIDTH PRECISION PRINTF
12237 ----------------------------------------
12238 -1 -1 %s
12239 -1 10 %.10s
12240 10 -1 %10s
12241 20 10 %20.10s
a2889657 12242
5f5c8ee5
GM
12243 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12244 display them, and < 0 means obey the current buffer's value of
12245 enable_multibyte_characters.
278feba9 12246
5f5c8ee5 12247 Value is the number of glyphs produced. */
b1d1124b 12248
5f5c8ee5
GM
12249static int
12250display_string (string, lisp_string, face_string, face_string_pos,
12251 start, it, field_width, precision, max_x, multibyte)
12252 unsigned char *string;
12253 Lisp_Object lisp_string;
12254 int start;
12255 struct it *it;
12256 int field_width, precision, max_x;
12257 int multibyte;
12258{
12259 int hpos_at_start = it->hpos;
12260 int saved_face_id = it->face_id;
12261 struct glyph_row *row = it->glyph_row;
12262
12263 /* Initialize the iterator IT for iteration over STRING beginning
12264 with index START. We assume that IT may be modified here (which
12265 means that display_line has to do something when displaying a
12266 mini-buffer prompt, which it does). */
12267 reseat_to_string (it, string, lisp_string, start,
12268 precision, field_width, multibyte);
12269
12270 /* If displaying STRING, set up the face of the iterator
12271 from LISP_STRING, if that's given. */
12272 if (STRINGP (face_string))
12273 {
12274 int endptr;
12275 struct face *face;
12276
12277 it->face_id
12278 = face_at_string_position (it->w, face_string, face_string_pos,
12279 0, it->region_beg_charpos,
12280 it->region_end_charpos,
12281 &endptr, it->base_face_id);
12282 face = FACE_FROM_ID (it->f, it->face_id);
12283 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 12284 }
a2889657 12285
5f5c8ee5
GM
12286 /* Set max_x to the maximum allowed X position. Don't let it go
12287 beyond the right edge of the window. */
12288 if (max_x <= 0)
12289 max_x = it->last_visible_x;
12290 else
12291 max_x = min (max_x, it->last_visible_x);
efc63ef0 12292
5f5c8ee5
GM
12293 /* Skip over display elements that are not visible. because IT->w is
12294 hscrolled. */
12295 if (it->current_x < it->first_visible_x)
12296 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12297 MOVE_TO_POS | MOVE_TO_X);
a2889657 12298
5f5c8ee5
GM
12299 row->ascent = it->max_ascent;
12300 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12301 row->phys_ascent = it->max_phys_ascent;
12302 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 12303
5f5c8ee5
GM
12304 /* This condition is for the case that we are called with current_x
12305 past last_visible_x. */
12306 while (it->current_x < max_x)
a2889657 12307 {
5f5c8ee5 12308 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 12309
5f5c8ee5
GM
12310 /* Get the next display element. */
12311 if (!get_next_display_element (it))
90adcf20 12312 break;
1c9241f5 12313
5f5c8ee5
GM
12314 /* Produce glyphs. */
12315 x_before = it->current_x;
12316 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12317 PRODUCE_GLYPHS (it);
90adcf20 12318
5f5c8ee5
GM
12319 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12320 i = 0;
12321 x = x_before;
12322 while (i < nglyphs)
a2889657 12323 {
5f5c8ee5
GM
12324 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12325
12326 if (!it->truncate_lines_p
12327 && x + glyph->pixel_width > max_x)
12328 {
12329 /* End of continued line or max_x reached. */
12330 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12331 it->current_x = x;
12332 break;
12333 }
12334 else if (x + glyph->pixel_width > it->first_visible_x)
12335 {
12336 /* Glyph is at least partially visible. */
12337 ++it->hpos;
12338 if (x < it->first_visible_x)
12339 it->glyph_row->x = x - it->first_visible_x;
12340 }
12341 else
a2889657 12342 {
5f5c8ee5
GM
12343 /* Glyph is off the left margin of the display area.
12344 Should not happen. */
12345 abort ();
a2889657 12346 }
5f5c8ee5
GM
12347
12348 row->ascent = max (row->ascent, it->max_ascent);
12349 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12350 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12351 row->phys_height = max (row->phys_height,
12352 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12353 x += glyph->pixel_width;
12354 ++i;
a2889657 12355 }
5f5c8ee5
GM
12356
12357 /* Stop if max_x reached. */
12358 if (i < nglyphs)
12359 break;
12360
12361 /* Stop at line ends. */
12362 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 12363 {
5f5c8ee5
GM
12364 it->continuation_lines_width = 0;
12365 break;
a2889657 12366 }
1c9241f5 12367
5f5c8ee5 12368 set_iterator_to_next (it);
a688bb24 12369
5f5c8ee5
GM
12370 /* Stop if truncating at the right edge. */
12371 if (it->truncate_lines_p
12372 && it->current_x >= it->last_visible_x)
12373 {
12374 /* Add truncation mark, but don't do it if the line is
12375 truncated at a padding space. */
12376 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 12377 {
5f5c8ee5
GM
12378 if (!FRAME_WINDOW_P (it->f))
12379 produce_special_glyphs (it, IT_TRUNCATION);
12380 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 12381 }
5f5c8ee5 12382 break;
1c9241f5 12383 }
a2889657
JB
12384 }
12385
5f5c8ee5
GM
12386 /* Maybe insert a truncation at the left. */
12387 if (it->first_visible_x
12388 && IT_CHARPOS (*it) > 0)
a2889657 12389 {
5f5c8ee5
GM
12390 if (!FRAME_WINDOW_P (it->f))
12391 insert_left_trunc_glyphs (it);
12392 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
12393 }
12394
5f5c8ee5
GM
12395 it->face_id = saved_face_id;
12396
12397 /* Value is number of columns displayed. */
12398 return it->hpos - hpos_at_start;
12399}
a2889657 12400
a2889657 12401
a2889657 12402\f
5f5c8ee5
GM
12403/* This is like a combination of memq and assq. Return 1 if PROPVAL
12404 appears as an element of LIST or as the car of an element of LIST.
12405 If PROPVAL is a list, compare each element against LIST in that
12406 way, and return 1 if any element of PROPVAL is found in LIST.
12407 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12408
12409int
12410invisible_p (propval, list)
12411 register Lisp_Object propval;
12412 Lisp_Object list;
12413{
af460d46
RS
12414 register Lisp_Object tail, proptail;
12415 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
12416 {
12417 register Lisp_Object tem;
af460d46 12418 tem = XCONS (tail)->car;
642eefc6
RS
12419 if (EQ (propval, tem))
12420 return 1;
12421 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
12422 return 1;
12423 }
af460d46
RS
12424 if (CONSP (propval))
12425 for (proptail = propval; CONSP (proptail);
12426 proptail = XCONS (proptail)->cdr)
12427 {
12428 Lisp_Object propelt;
12429 propelt = XCONS (proptail)->car;
12430 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12431 {
12432 register Lisp_Object tem;
12433 tem = XCONS (tail)->car;
12434 if (EQ (propelt, tem))
12435 return 1;
12436 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
12437 return 1;
12438 }
12439 }
642eefc6
RS
12440 return 0;
12441}
12442
5f5c8ee5
GM
12443
12444/* Return 1 if PROPVAL appears as the car of an element of LIST and
12445 the cdr of that element is non-nil. If PROPVAL is a list, check
12446 each element of PROPVAL in that way, and the first time some
12447 element is found, return 1 if the cdr of that element is non-nil.
12448 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12449
12450int
12451invisible_ellipsis_p (propval, list)
12452 register Lisp_Object propval;
12453 Lisp_Object list;
12454{
af460d46
RS
12455 register Lisp_Object tail, proptail;
12456 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
12457 {
12458 register Lisp_Object tem;
af460d46 12459 tem = XCONS (tail)->car;
642eefc6
RS
12460 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
12461 return ! NILP (XCONS (tem)->cdr);
12462 }
af460d46
RS
12463 if (CONSP (propval))
12464 for (proptail = propval; CONSP (proptail);
12465 proptail = XCONS (proptail)->cdr)
12466 {
12467 Lisp_Object propelt;
12468 propelt = XCONS (proptail)->car;
12469 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12470 {
12471 register Lisp_Object tem;
12472 tem = XCONS (tail)->car;
12473 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
12474 return ! NILP (XCONS (tem)->cdr);
12475 }
12476 }
642eefc6
RS
12477 return 0;
12478}
5f5c8ee5
GM
12479
12480
642eefc6 12481\f
5f5c8ee5
GM
12482/***********************************************************************
12483 Initialization
12484 ***********************************************************************/
12485
a2889657
JB
12486void
12487syms_of_xdisp ()
12488{
9142dd5b
GM
12489 Vresize_mini_config = Qnil;
12490 staticpro (&Vresize_mini_config);
12491
c6e89d6c
GM
12492 Vwith_echo_area_save_vector = Qnil;
12493 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 12494
c6e89d6c
GM
12495 Vmessage_stack = Qnil;
12496 staticpro (&Vmessage_stack);
12497
735c094c 12498 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 12499 staticpro (&Qinhibit_redisplay);
735c094c 12500
5f5c8ee5
GM
12501#if GLYPH_DEBUG
12502 defsubr (&Sdump_glyph_matrix);
12503 defsubr (&Sdump_glyph_row);
12504 defsubr (&Sdump_toolbar_row);
12505 defsubr (&Strace_redisplay_toggle);
12506#endif
12507
cf074754
RS
12508 staticpro (&Qmenu_bar_update_hook);
12509 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12510
d46fb96a 12511 staticpro (&Qoverriding_terminal_local_map);
7079aefa 12512 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 12513
399164b4
KH
12514 staticpro (&Qoverriding_local_map);
12515 Qoverriding_local_map = intern ("overriding-local-map");
12516
75c43375
RS
12517 staticpro (&Qwindow_scroll_functions);
12518 Qwindow_scroll_functions = intern ("window-scroll-functions");
12519
e0bfbde6
RS
12520 staticpro (&Qredisplay_end_trigger_functions);
12521 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 12522
2e54982e
RS
12523 staticpro (&Qinhibit_point_motion_hooks);
12524 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12525
5f5c8ee5
GM
12526 staticpro (&Qdisplay);
12527 Qdisplay = intern ("display");
12528 staticpro (&Qleft_margin);
12529 Qspace_width = intern ("space-width");
12530 staticpro (&Qspace_width);
12531 Qheight = intern ("height");
12532 staticpro (&Qheight);
12533 Qraise = intern ("raise");
12534 staticpro (&Qraise);
12535 Qspace = intern ("space");
12536 staticpro (&Qspace);
12537 Qleft_margin = intern ("left-margin");
12538 staticpro (&Qright_margin);
12539 Qright_margin = intern ("right-margin");
12540 Qalign_to = intern ("align-to");
12541 staticpro (&Qalign_to);
12542 QCalign_to = intern (":align-to");
12543 staticpro (&QCalign_to);
12544 Qwidth = intern ("width");
12545 staticpro (&Qwidth);
12546 Qrelative_width = intern ("relative-width");
12547 staticpro (&Qrelative_width);
12548 QCrelative_width = intern (":relative-width");
12549 staticpro (&QCrelative_width);
12550 QCrelative_height = intern (":relative-height");
12551 staticpro (&QCrelative_height);
12552 QCeval = intern (":eval");
12553 staticpro (&QCeval);
12554 QCwhen = intern (":when");
12555 staticpro (&QCwhen);
12556 Qfontified = intern ("fontified");
12557 staticpro (&Qfontified);
12558 Qfontification_functions = intern ("fontification-functions");
12559 staticpro (&Qfontification_functions);
5f5c8ee5
GM
12560 Qtrailing_whitespace = intern ("trailing-whitespace");
12561 staticpro (&Qtrailing_whitespace);
12562 Qimage = intern ("image");
12563 staticpro (&Qimage);
12564
a2889657
JB
12565 staticpro (&last_arrow_position);
12566 staticpro (&last_arrow_string);
12567 last_arrow_position = Qnil;
12568 last_arrow_string = Qnil;
c6e89d6c
GM
12569
12570 echo_buffer[0] = echo_buffer[1] = Qnil;
12571 staticpro (&echo_buffer[0]);
12572 staticpro (&echo_buffer[1]);
12573
12574 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12575 staticpro (&echo_area_buffer[0]);
12576 staticpro (&echo_area_buffer[1]);
a2889657 12577
8f897821
GM
12578 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12579 "Non-nil means highlight trailing whitespace.\n\
12580The face used for trailing whitespace is `trailing-whitespace'.");
12581 Vshow_trailing_whitespace = Qnil;
12582
735c094c
KH
12583 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12584 "Non-nil means don't actually do any redisplay.\n\
12585This is used for internal purposes.");
12586 Vinhibit_redisplay = Qnil;
12587
a2889657 12588 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 12589 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
12590 Vglobal_mode_string = Qnil;
12591
12592 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12593 "Marker for where to display an arrow on top of the buffer text.\n\
12594This must be the beginning of a line in order to work.\n\
12595See also `overlay-arrow-string'.");
12596 Voverlay_arrow_position = Qnil;
12597
12598 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12599 "String to display as an arrow. See also `overlay-arrow-position'.");
12600 Voverlay_arrow_string = Qnil;
12601
12602 DEFVAR_INT ("scroll-step", &scroll_step,
12603 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
12604If that fails to bring point back on frame, point is centered instead.\n\
12605If this is zero, point is always centered after it moves off frame.");
a2889657 12606
0789adb2
RS
12607 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12608 "*Scroll up to this many lines, to bring point back on screen.");
12609 scroll_conservatively = 0;
12610
9afd2168
RS
12611 DEFVAR_INT ("scroll-margin", &scroll_margin,
12612 "*Number of lines of margin at the top and bottom of a window.\n\
12613Recenter the window whenever point gets within this many lines\n\
12614of the top or bottom of the window.");
12615 scroll_margin = 0;
12616
5f5c8ee5 12617#if GLYPH_DEBUG
a2889657 12618 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 12619#endif
a2889657
JB
12620
12621 DEFVAR_BOOL ("truncate-partial-width-windows",
12622 &truncate_partial_width_windows,
44fa5b1e 12623 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
12624 truncate_partial_width_windows = 1;
12625
12626 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12627 "*Non-nil means use inverse video for the mode line.");
12628 mode_line_inverse_video = 1;
aa6d10fa
RS
12629
12630 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
5f5c8ee5 12631 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 12632If the buffer is bigger than this, the line number does not appear\n\
9f027393 12633in the mode line.");
aa6d10fa 12634 line_number_display_limit = 1000000;
fba9ce76 12635
5d121aec
KH
12636 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12637 "*Maximum line width (in characters) for line number display.\n\
12638If the average length of the lines near point is bigger than this, then the\n\
12639line number may be omitted from the mode line.");
12640 line_number_display_limit_width = 200;
12641
fba9ce76
RS
12642 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12643 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 12644 highlight_nonselected_windows = 0;
d39b6696
KH
12645
12646 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
12647 "Non-nil if more than one frame is visible on this display.\n\
12648Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
12649This variable is not guaranteed to be accurate except while processing\n\
12650`frame-title-format' and `icon-title-format'.");
d39b6696
KH
12651
12652 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 12653 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
12654\(Assuming the window manager supports this feature.)\n\
12655This variable has the same structure as `mode-line-format' (which see),\n\
12656and is used only on frames for which no explicit name has been set\n\
12657\(see `modify-frame-parameters').");
12658 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 12659 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
12660\(Assuming the window manager supports this feature.)\n\
12661This variable has the same structure as `mode-line-format' (which see),\n\
12662and is used only on frames for which no explicit name has been set\n\
12663\(see `modify-frame-parameters').");
12664 Vicon_title_format
12665 = Vframe_title_format
12666 = Fcons (intern ("multiple-frames"),
12667 Fcons (build_string ("%b"),
12668 Fcons (Fcons (build_string (""),
12669 Fcons (intern ("invocation-name"),
12670 Fcons (build_string ("@"),
12671 Fcons (intern ("system-name"),
12672 Qnil)))),
12673 Qnil)));
5992c4f7
KH
12674
12675 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12676 "Maximum number of lines to keep in the message log buffer.\n\
12677If nil, disable message logging. If t, log messages but don't truncate\n\
12678the buffer when it becomes large.");
12679 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
12680
12681 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12682 "Functions called before redisplay, if window sizes have changed.\n\
12683The value should be a list of functions that take one argument.\n\
12684Just before redisplay, for each frame, if any of its windows have changed\n\
12685size since the last redisplay, or have been split or deleted,\n\
12686all the functions in the list are called, with the frame as argument.");
12687 Vwindow_size_change_functions = Qnil;
75c43375
RS
12688
12689 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 12690 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 12691Each function is called with two arguments, the window\n\
8d9583b0
RS
12692and its new display-start position. Note that the value of `window-end'\n\
12693is not valid when these functions are called.");
75c43375 12694 Vwindow_scroll_functions = Qnil;
5f5c8ee5
GM
12695
12696 DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
12697 "*Non-nil means automatically resize toolbars.\n\
12698This increases a toolbar's height if not all toolbar items are visible.\n\
12699It decreases a toolbar's height when it would display blank lines\n\
12700otherwise.");
12701 auto_resize_toolbars_p = 1;
12702
12703 DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
12704 "*Non-nil means raise toolbar buttons when the mouse moves over them.");
12705 auto_raise_toolbar_buttons_p = 1;
12706
12707 DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
12708 "*Margin around toolbar buttons in pixels.");
12709 toolbar_button_margin = 1;
12710
12711 DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
12712 "Relief thickness of toolbar buttons.");
12713 toolbar_button_relief = 3;
12714
12715 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12716 "List of functions to call to fontify regions of text.\n\
12717Each function is called with one argument POS. Functions must\n\
12718fontify a region starting at POS in the current buffer, and give\n\
12719fontified regions the property `fontified'.\n\
12720This variable automatically becomes buffer-local when set.");
12721 Vfontification_functions = Qnil;
12722 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
12723
12724 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
12725 &unibyte_display_via_language_environment,
12726 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
12727Specifically this means that unibyte non-ASCII characters\n\
12728are displayed by converting them to the equivalent multibyte characters\n\
12729according to the current language environment. As a result, they are\n\
12730displayed according to the current fontset.");
12731 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
12732
12733 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
12734 "*Maximum height for resizing mini-windows.\n\
12735If a float, it specifies a fraction of the mini-window frame's height.\n\
97cafc0f
GM
12736If an integer, it specifies a number of lines.\n\
12737If nil, don't resize.");
c6e89d6c 12738 Vmax_mini_window_height = make_float (0.25);
a2889657
JB
12739}
12740
5f5c8ee5
GM
12741
12742/* Initialize this module when Emacs starts. */
12743
dfcf069d 12744void
a2889657
JB
12745init_xdisp ()
12746{
12747 Lisp_Object root_window;
5f5c8ee5 12748 struct window *mini_w;
a2889657 12749
5f5c8ee5 12750 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
12751
12752 mini_w = XWINDOW (minibuf_window);
11e82b76 12753 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 12754
a2889657
JB
12755 if (!noninteractive)
12756 {
5f5c8ee5
GM
12757 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12758 int i;
12759
12760 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 12761 set_window_height (root_window,
5f5c8ee5 12762 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 12763 0);
c2213350 12764 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
12765 set_window_height (minibuf_window, 1, 0);
12766
c2213350
KH
12767 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12768 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
12769
12770 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12771 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12772 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12773
12774 /* The default ellipsis glyphs `...'. */
12775 for (i = 0; i < 3; ++i)
12776 XSETFASTINT (default_invis_vector[i], '.');
a2889657 12777 }
5f5c8ee5
GM
12778
12779#ifdef HAVE_WINDOW_SYSTEM
12780 {
12781 /* Allocate the buffer for frame titles. */
12782 int size = 100;
12783 frame_title_buf = (char *) xmalloc (size);
12784 frame_title_buf_end = frame_title_buf + size;
12785 frame_title_ptr = NULL;
12786 }
12787#endif /* HAVE_WINDOW_SYSTEM */
a2889657 12788}
5f5c8ee5
GM
12789
12790