(PVEC_TYPE_MASK): Add the bit for hash tables.
[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 {
5f5c8ee5
GM
738 width -= FRAME_SCROLL_BAR_WIDTH (f) + 2 * FRAME_FLAGS_AREA_COLS (f);
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
GM
794 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
795 + FRAME_FLAGS_AREA_WIDTH (f));
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
GM
4933 set_message (NULL, m, nbytes, multibyte);
4934 Fraise_frame (frame);
5f5c8ee5
GM
4935 }
4936 else
c6e89d6c 4937 clear_message (1, 1);
5f5c8ee5 4938
c6e89d6c 4939 do_pending_window_change (0);
5f5c8ee5 4940 echo_area_display (1);
c6e89d6c 4941 do_pending_window_change (0);
5f5c8ee5
GM
4942 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4943 (*frame_up_to_date_hook) (f);
4944 }
4945}
4946
4947
4948/* Display a null-terminated echo area message M. If M is 0, clear
4949 out any existing message, and let the mini-buffer text show through.
4950
4951 The buffer M must continue to exist until after the echo area gets
4952 cleared or some other message gets displayed there. Do not pass
4953 text that is stored in a Lisp string. Do not pass text in a buffer
4954 that was alloca'd. */
4955
4956void
4957message1 (m)
4958 char *m;
4959{
4960 message2 (m, (m ? strlen (m) : 0), 0);
4961}
4962
4963
4964/* The non-logging counterpart of message1. */
4965
4966void
4967message1_nolog (m)
4968 char *m;
4969{
4970 message2_nolog (m, (m ? strlen (m) : 0), 0);
4971}
4972
4973/* Display a message M which contains a single %s
4974 which gets replaced with STRING. */
4975
4976void
4977message_with_string (m, string, log)
4978 char *m;
4979 Lisp_Object string;
4980 int log;
4981{
4982 if (noninteractive)
4983 {
4984 if (m)
4985 {
4986 if (noninteractive_need_newline)
4987 putc ('\n', stderr);
4988 noninteractive_need_newline = 0;
4989 fprintf (stderr, m, XSTRING (string)->data);
4990 if (cursor_in_echo_area == 0)
4991 fprintf (stderr, "\n");
4992 fflush (stderr);
4993 }
4994 }
4995 else if (INTERACTIVE)
4996 {
4997 /* The frame whose minibuffer we're going to display the message on.
4998 It may be larger than the selected frame, so we need
4999 to use its buffer, not the selected frame's buffer. */
5000 Lisp_Object mini_window;
5001 FRAME_PTR f;
5002
5003 /* Get the frame containing the minibuffer
5004 that the selected frame is using. */
5005 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5006 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5007
5008 /* A null message buffer means that the frame hasn't really been
5009 initialized yet. Error messages get reported properly by
5010 cmd_error, so this must be just an informative message; toss it. */
5011 if (FRAME_MESSAGE_BUF (f))
5012 {
5013 int len;
5014 char *a[1];
5015 a[0] = (char *) XSTRING (string)->data;
5016
5017 len = doprnt (FRAME_MESSAGE_BUF (f),
5018 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5019
5020 if (log)
5021 message2 (FRAME_MESSAGE_BUF (f), len,
5022 STRING_MULTIBYTE (string));
5023 else
5024 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5025 STRING_MULTIBYTE (string));
5026
5027 /* Print should start at the beginning of the message
5028 buffer next time. */
5029 message_buf_print = 0;
5030 }
5031 }
5032}
5033
5034
5f5c8ee5
GM
5035/* Dump an informative message to the minibuf. If M is 0, clear out
5036 any existing message, and let the mini-buffer text show through. */
5037
5038/* VARARGS 1 */
5039void
5040message (m, a1, a2, a3)
5041 char *m;
5042 EMACS_INT a1, a2, a3;
5043{
5044 if (noninteractive)
5045 {
5046 if (m)
5047 {
5048 if (noninteractive_need_newline)
5049 putc ('\n', stderr);
5050 noninteractive_need_newline = 0;
5051 fprintf (stderr, m, a1, a2, a3);
5052 if (cursor_in_echo_area == 0)
5053 fprintf (stderr, "\n");
5054 fflush (stderr);
5055 }
5056 }
5057 else if (INTERACTIVE)
5058 {
5059 /* The frame whose mini-buffer we're going to display the message
5060 on. It may be larger than the selected frame, so we need to
5061 use its buffer, not the selected frame's buffer. */
5062 Lisp_Object mini_window;
5063 struct frame *f;
5064
5065 /* Get the frame containing the mini-buffer
5066 that the selected frame is using. */
5067 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5068 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5069
5070 /* A null message buffer means that the frame hasn't really been
5071 initialized yet. Error messages get reported properly by
5072 cmd_error, so this must be just an informative message; toss
5073 it. */
5074 if (FRAME_MESSAGE_BUF (f))
5075 {
5076 if (m)
5077 {
5078 int len;
5079#ifdef NO_ARG_ARRAY
5080 char *a[3];
5081 a[0] = (char *) a1;
5082 a[1] = (char *) a2;
5083 a[2] = (char *) a3;
5084
5085 len = doprnt (FRAME_MESSAGE_BUF (f),
5086 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5087#else
5088 len = doprnt (FRAME_MESSAGE_BUF (f),
5089 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5090 (char **) &a1);
5091#endif /* NO_ARG_ARRAY */
5092
5093 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5094 }
5095 else
5096 message1 (0);
5097
5098 /* Print should start at the beginning of the message
5099 buffer next time. */
5100 message_buf_print = 0;
5101 }
5102 }
5103}
5104
5105
5106/* The non-logging version of message. */
5107
5108void
5109message_nolog (m, a1, a2, a3)
5110 char *m;
5111 EMACS_INT a1, a2, a3;
5112{
5113 Lisp_Object old_log_max;
5114 old_log_max = Vmessage_log_max;
5115 Vmessage_log_max = Qnil;
5116 message (m, a1, a2, a3);
5117 Vmessage_log_max = old_log_max;
5118}
5119
5120
c6e89d6c
GM
5121/* Display the current message in the current mini-buffer. This is
5122 only called from error handlers in process.c, and is not time
5123 critical. */
5f5c8ee5
GM
5124
5125void
5126update_echo_area ()
5127{
c6e89d6c
GM
5128 if (!NILP (echo_area_buffer[0]))
5129 {
5130 Lisp_Object string;
5131 string = Fcurrent_message ();
5132 message3 (string, XSTRING (string)->size,
5133 !NILP (current_buffer->enable_multibyte_characters));
5134 }
5135}
5136
5137
5138/* Call FN with args A1..A5 with either the current or last displayed
5139 echo_area_buffer as current buffer.
5140
5141 WHICH zero means use the current message buffer
5142 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5143 from echo_buffer[] and clear it.
5144
5145 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5146 suitable buffer from echo_buffer[] and clear it.
5147
5148 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5149 that the current message becomes the last displayed one, make
5150 choose a suitable buffer for echo_area_buffer[0], and clear it.
5151
5152 Value is what FN returns. */
5153
5154static int
5155with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5156 struct window *w;
5157 int which;
5158 int (*fn) ();
5159 int a1, a2, a3, a4, a5;
5160{
5161 Lisp_Object buffer;
5162 int i, this_one, the_other, clear_buffer_p, rc;
5163 int count = specpdl_ptr - specpdl;
5164
5165 /* If buffers aren't life, make new ones. */
5166 for (i = 0; i < 2; ++i)
5167 if (!BUFFERP (echo_buffer[i])
5168 || NILP (XBUFFER (echo_buffer[i])->name))
5169 {
5170 char name[30];
5171 sprintf (name, " *Echo Area %d*", i);
5172 echo_buffer[i] = Fget_buffer_create (build_string (name));
5173 }
5174
5175 clear_buffer_p = 0;
5176
5177 if (which == 0)
5178 this_one = 0, the_other = 1;
5179 else if (which > 0)
5180 this_one = 1, the_other = 0;
5f5c8ee5 5181 else
c6e89d6c
GM
5182 {
5183 this_one = 0, the_other = 1;
5184 clear_buffer_p = 1;
5185
5186 /* We need a fresh one in case the current echo buffer equals
5187 the one containing the last displayed echo area message. */
5188 if (!NILP (echo_area_buffer[this_one])
5189 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5190 echo_area_buffer[this_one] = Qnil;
5191
5192 }
5193
5194 /* Choose a suitable buffer from echo_buffer[] is we don't
5195 have one. */
5196 if (NILP (echo_area_buffer[this_one]))
5197 {
5198 echo_area_buffer[this_one]
5199 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5200 ? echo_buffer[the_other]
5201 : echo_buffer[this_one]);
5202 clear_buffer_p = 1;
5203 }
5204
5205 buffer = echo_area_buffer[this_one];
5206
5207 record_unwind_protect (unwind_with_echo_area_buffer,
5208 with_echo_area_buffer_unwind_data (w));
5209
5210 /* Make the echo area buffer current. Note that for display
5211 purposes, it is not necessary that the displayed window's buffer
5212 == current_buffer, except for text property lookup. So, let's
5213 only set that buffer temporarily here without doing a full
5214 Fset_window_buffer. We must also change w->pointm, though,
5215 because otherwise an assertions in unshow_buffer fails, and Emacs
5216 aborts. */
9142dd5b 5217 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5218 if (w)
5219 {
5220 w->buffer = buffer;
5221 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5222 }
5223 current_buffer->truncate_lines = Qnil;
5224 current_buffer->undo_list = Qt;
5225 current_buffer->read_only = Qnil;
5226
5227 if (clear_buffer_p && Z > BEG)
5228 del_range (BEG, Z);
5229
5230 xassert (BEGV >= BEG);
5231 xassert (ZV <= Z && ZV >= BEGV);
5232
5233 rc = fn (a1, a2, a3, a4, a5);
5234
5235 xassert (BEGV >= BEG);
5236 xassert (ZV <= Z && ZV >= BEGV);
5237
5238 unbind_to (count, Qnil);
5239 return rc;
5f5c8ee5
GM
5240}
5241
5242
c6e89d6c
GM
5243/* Save state that should be preserved around the call to the function
5244 FN called in with_echo_area_buffer. */
5f5c8ee5 5245
c6e89d6c
GM
5246static Lisp_Object
5247with_echo_area_buffer_unwind_data (w)
5248 struct window *w;
5f5c8ee5 5249{
c6e89d6c
GM
5250 int i = 0;
5251 Lisp_Object vector;
5f5c8ee5 5252
c6e89d6c
GM
5253 /* Reduce consing by keeping one vector in
5254 Vwith_echo_area_save_vector. */
5255 vector = Vwith_echo_area_save_vector;
5256 Vwith_echo_area_save_vector = Qnil;
5257
5258 if (NILP (vector))
9142dd5b 5259 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5260
5261 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5262 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5263 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5264
5265 if (w)
5266 {
5267 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5268 XVECTOR (vector)->contents[i++] = w->buffer;
5269 XVECTOR (vector)->contents[i++]
5270 = make_number (XMARKER (w->pointm)->charpos);
5271 XVECTOR (vector)->contents[i++]
5272 = make_number (XMARKER (w->pointm)->bytepos);
5273 }
5274 else
5275 {
5276 int end = i + 4;
5277 while (i < end)
5278 XVECTOR (vector)->contents[i++] = Qnil;
5279 }
5f5c8ee5 5280
c6e89d6c
GM
5281 xassert (i == XVECTOR (vector)->size);
5282 return vector;
5283}
5f5c8ee5 5284
5f5c8ee5 5285
c6e89d6c
GM
5286/* Restore global state from VECTOR which was created by
5287 with_echo_area_buffer_unwind_data. */
5288
5289static Lisp_Object
5290unwind_with_echo_area_buffer (vector)
5291 Lisp_Object vector;
5292{
5293 int i = 0;
5294
9142dd5b 5295 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
c6e89d6c
GM
5296 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5297 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
c6e89d6c
GM
5298
5299 if (WINDOWP (XVECTOR (vector)->contents[i]))
5300 {
5301 struct window *w;
5302 Lisp_Object buffer, charpos, bytepos;
5303
5304 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5305 buffer = XVECTOR (vector)->contents[i]; ++i;
5306 charpos = XVECTOR (vector)->contents[i]; ++i;
5307 bytepos = XVECTOR (vector)->contents[i]; ++i;
5308
5309 w->buffer = buffer;
5310 set_marker_both (w->pointm, buffer,
5311 XFASTINT (charpos), XFASTINT (bytepos));
5312 }
5313
5314 Vwith_echo_area_save_vector = vector;
5315 return Qnil;
5316}
5317
5318
5319/* Set up the echo area for use by print functions. MULTIBYTE_P
5320 non-zero means we will print multibyte. */
5321
5322void
5323setup_echo_area_for_printing (multibyte_p)
5324 int multibyte_p;
5325{
5326 if (!message_buf_print)
5327 {
5328 /* A message has been output since the last time we printed.
5329 Choose a fresh echo area buffer. */
5330 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5331 echo_area_buffer[0] = echo_buffer[1];
5332 else
5333 echo_area_buffer[0] = echo_buffer[0];
5334
5335 /* Switch to that buffer and clear it. */
5336 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5337 if (Z > BEG)
5338 del_range (BEG, Z);
5339 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5340
5341 /* Set up the buffer for the multibyteness we need. */
5342 if (multibyte_p
5343 != !NILP (current_buffer->enable_multibyte_characters))
5344 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5345
5346 /* Raise the frame containing the echo area. */
5347 if (minibuffer_auto_raise)
5348 {
5349 Lisp_Object mini_window;
5350 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5351 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5352 }
5353
5354 message_buf_print = 1;
5355 }
5356 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5357 /* Someone switched buffers between print requests. */
5358 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5359}
5360
5361
dd2eb166
GM
5362/* Display an echo area message in window W. Value is non-zero if W's
5363 height is changed. If display_last_displayed_message_p is
5364 non-zero, display the message that was last displayed, otherwise
5365 display the current message. */
c6e89d6c
GM
5366
5367static int
5368display_echo_area (w)
5369 struct window *w;
5370{
dd2eb166
GM
5371 int i, no_message_p, window_height_changed_p;
5372
5373 /* If there is no message, we must call display_echo_area_1
5374 nevertheless because it resizes the window. But we will have to
5375 reset the echo_area_buffer in question to nil at the end because
5376 with_echo_area_buffer will sets it to an empty buffer. */
5377 i = display_last_displayed_message_p ? 1 : 0;
5378 no_message_p = NILP (echo_area_buffer[i]);
5379
5380 window_height_changed_p
5381 = with_echo_area_buffer (w, display_last_displayed_message_p,
5382 (int (*) ()) display_echo_area_1, w);
5383
5384 if (no_message_p)
5385 echo_area_buffer[i] = Qnil;
5386
5387 return window_height_changed_p;
c6e89d6c
GM
5388}
5389
5390
5391/* Helper for display_echo_area. Display the current buffer which
5392 contains the current echo area message in window W, a mini-window.
5393 Change the height of W so that all of the message is displayed.
5394 Value is non-zero if height of W was changed. */
5395
5396static int
5397display_echo_area_1 (w)
5398 struct window *w;
5399{
5400 Lisp_Object window;
5401 struct frame *f = XFRAME (w->frame);
5402 struct text_pos start;
5403 int window_height_changed_p = 0;
5404
5405 /* Do this before displaying, so that we have a large enough glyph
5406 matrix for the display. */
5407 window_height_changed_p = resize_mini_window (w);
5408
5409 /* Display. */
5410 clear_glyph_matrix (w->desired_matrix);
5411 XSETWINDOW (window, w);
5412 SET_TEXT_POS (start, BEG, BEG_BYTE);
5413 try_window (window, start);
5414
c6e89d6c
GM
5415 return window_height_changed_p;
5416}
5417
5418
5419/* Resize mini-window W to fit the size of its contents. Value is
5420 non-zero if the window height has been changed. */
5421
5422static int
5423resize_mini_window (w)
5424 struct window *w;
5425{
5426 struct frame *f = XFRAME (w->frame);
5427 int window_height_changed_p = 0;
5428
5429 xassert (MINI_WINDOW_P (w));
5430
5431 if (!FRAME_MINIBUF_ONLY_P (f))
5432 {
5433 struct it it;
dd2eb166
GM
5434 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5435 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5436 int height, max_height;
5437 int unit = CANON_Y_UNIT (f);
5438 struct text_pos start;
9142dd5b 5439
c6e89d6c 5440 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 5441
dd2eb166
GM
5442 /* Compute the max. number of lines specified by the user. */
5443 if (FLOATP (Vmax_mini_window_height))
5444 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5445 else if (INTEGERP (Vmax_mini_window_height))
5446 max_height = XINT (Vmax_mini_window_height);
5447
5448 /* Correct that max. height if it's bogus. */
5449 max_height = max (1, max_height);
5450 max_height = min (total_height, max_height);
5451
5452 /* Find out the height of the text in the window. */
5453 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5454 height = (unit - 1 + it.current_y + last_height) / unit;
5455 height = max (1, height);
5456
5457 /* Compute a suitable window start. */
5458 if (height > max_height)
5459 {
5460 height = max_height;
5461 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5462 move_it_vertically_backward (&it, (height - 1) * unit);
5463 start = it.current.pos;
5464 }
5465 else
5466 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5467 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a
GM
5468
5469 if (NILP (Vresize_mini_config))
dd2eb166 5470 {
c59c668a
GM
5471 if (height != XFASTINT (w->height))
5472 {
5473 Lisp_Object old_selected_window;
5474 Lisp_Object config;
5475 struct gcpro gcpro1;
5476
5477 resize_mini_initial_height = XFASTINT (w->height);
5478 config = Fcurrent_window_configuration (Qnil);
5479 GCPRO1 (config);
5480
5481 old_selected_window = selected_window;
5482 XSETWINDOW (selected_window, w);
5483 change_window_height (height - XFASTINT (w->height), 0);
5484 selected_window = old_selected_window;
5485
5486 window_height_changed_p = 1;
9142dd5b 5487
c59c668a
GM
5488 /* Set this after changing window sizes, or else
5489 Vresize_mini_config would be reset in
5490 adjust_frame_glyphs. */
5491 resize_mini_frame = XFRAME (w->frame);
5492 Vresize_mini_config = config;
5493 UNGCPRO;
5494 }
c6e89d6c 5495 }
c59c668a 5496 else if (height != XFASTINT (w->height))
9142dd5b 5497 {
c59c668a
GM
5498 if (height == resize_mini_initial_height)
5499 {
5500 Fset_window_configuration (Vresize_mini_config);
5501 Vresize_mini_config = Qnil;
5502 resize_mini_frame = NULL;
5503 }
5504 else
5505 {
5506 Lisp_Object old_selected_window;
5507 old_selected_window = selected_window;
5508 XSETWINDOW (selected_window, w);
5509 change_window_height (height - XFASTINT (w->height), 0);
5510 selected_window = old_selected_window;
5511 }
5512
9142dd5b
GM
5513 window_height_changed_p = 1;
5514 }
c6e89d6c
GM
5515 }
5516
5517 return window_height_changed_p;
5518}
5519
5520
5521/* Value is the current message, a string, or nil if there is no
5522 current message. */
5523
5524Lisp_Object
5525current_message ()
5526{
5527 Lisp_Object msg;
5528
5529 if (NILP (echo_area_buffer[0]))
5530 msg = Qnil;
5531 else
5532 {
5533 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5534 if (NILP (msg))
5535 echo_area_buffer[0] = Qnil;
5536 }
5537
5538 return msg;
5539}
5540
5541
5542static int
5543current_message_1 (msg)
5544 Lisp_Object *msg;
5545{
5546 if (Z > BEG)
5547 *msg = make_buffer_string (BEG, Z, 1);
5548 else
5549 *msg = Qnil;
5550 return 0;
5551}
5552
5553
5554/* Push the current message on Vmessage_stack for later restauration
5555 by restore_message. Value is non-zero if the current message isn't
5556 empty. This is a relatively infrequent operation, so it's not
5557 worth optimizing. */
5558
5559int
5560push_message ()
5561{
5562 Lisp_Object msg;
5563 msg = current_message ();
5564 Vmessage_stack = Fcons (msg, Vmessage_stack);
5565 return STRINGP (msg);
5566}
5567
5568
5569/* Restore message display from the top of Vmessage_stack. */
5570
5571void
5572restore_message ()
5573{
5574 Lisp_Object msg;
5575
5576 xassert (CONSP (Vmessage_stack));
5577 msg = XCAR (Vmessage_stack);
5578 if (STRINGP (msg))
5579 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5580 else
5581 message3_nolog (msg, 0, 0);
5582}
5583
5584
5585/* Pop the top-most entry off Vmessage_stack. */
5586
5587void
5588pop_message ()
5589{
5590 xassert (CONSP (Vmessage_stack));
5591 Vmessage_stack = XCDR (Vmessage_stack);
5592}
5593
5594
5595/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5596 exits. If the stack is not empty, we have a missing pop_message
5597 somewhere. */
5598
5599void
5600check_message_stack ()
5601{
5602 if (!NILP (Vmessage_stack))
5603 abort ();
5604}
5605
5606
5607/* Truncate to NCHARS what will be displayed in the echo area the next
5608 time we display it---but don't redisplay it now. */
5609
5610void
5611truncate_echo_area (nchars)
5612 int nchars;
5613{
5614 if (nchars == 0)
5615 echo_area_buffer[0] = Qnil;
5616 /* A null message buffer means that the frame hasn't really been
5617 initialized yet. Error messages get reported properly by
5618 cmd_error, so this must be just an informative message; toss it. */
5619 else if (!noninteractive
5620 && INTERACTIVE
5621 && FRAME_MESSAGE_BUF (selected_frame)
5622 && !NILP (echo_area_buffer[0]))
5623 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5624}
5625
5626
5627/* Helper function for truncate_echo_area. Truncate the current
5628 message to at most NCHARS characters. */
5629
5630static int
5631truncate_message_1 (nchars)
5632 int nchars;
5633{
5634 if (BEG + nchars < Z)
5635 del_range (BEG + nchars, Z);
5636 if (Z == BEG)
5637 echo_area_buffer[0] = Qnil;
5638 return 0;
5639}
5640
5641
5642/* Set the current message to a substring of S or STRING.
5643
5644 If STRING is a Lisp string, set the message to the first NBYTES
5645 bytes from STRING. NBYTES zero means use the whole string. If
5646 STRING is multibyte, the message will be displayed multibyte.
5647
5648 If S is not null, set the message to the first LEN bytes of S. LEN
5649 zero means use the whole string. MULTIBYTE_P non-zero means S is
5650 multibyte. Display the message multibyte in that case. */
5651
5652void
5653set_message (s, string, nbytes, multibyte_p)
5654 char *s;
5655 Lisp_Object string;
5656 int nbytes;
5657{
5658 message_enable_multibyte
5659 = ((s && multibyte_p)
5660 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5661
5662 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5663 s, string, nbytes, multibyte_p);
5664 message_buf_print = 0;
5665}
5666
5667
5668/* Helper function for set_message. Arguments have the same meaning
5669 as there. This function is called with the echo area buffer being
5670 current. */
5671
5672static int
5673set_message_1 (s, string, nbytes, multibyte_p)
5674 char *s;
5675 Lisp_Object string;
5676 int nbytes, multibyte_p;
5677{
5678 xassert (BEG == Z);
5679
5680 /* Change multibyteness of the echo buffer appropriately. */
5681 if (message_enable_multibyte
5682 != !NILP (current_buffer->enable_multibyte_characters))
5683 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5684
5685 /* Insert new message at BEG. */
5686 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5687
5688 if (STRINGP (string))
5689 {
5690 int nchars;
5691
5692 if (nbytes == 0)
5693 nbytes = XSTRING (string)->size_byte;
5694 nchars = string_byte_to_char (string, nbytes);
5695
5696 /* This function takes care of single/multibyte conversion. We
5697 just have to ensure that the echo area buffer has the right
5698 setting of enable_multibyte_characters. */
5699 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5700 }
5701 else if (s)
5702 {
5703 if (nbytes == 0)
5704 nbytes = strlen (s);
5705
5706 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5707 {
5708 /* Convert from multi-byte to single-byte. */
5709 int i, c, n;
5710 unsigned char work[1];
5711
5712 /* Convert a multibyte string to single-byte. */
5713 for (i = 0; i < nbytes; i += n)
5714 {
5715 c = string_char_and_length (s + i, nbytes - i, &n);
5716 work[0] = (SINGLE_BYTE_CHAR_P (c)
5717 ? c
5718 : multibyte_char_to_unibyte (c, Qnil));
5719 insert_1_both (work, 1, 1, 1, 0, 0);
5720 }
5721 }
5722 else if (!multibyte_p
5723 && !NILP (current_buffer->enable_multibyte_characters))
5724 {
5725 /* Convert from single-byte to multi-byte. */
5726 int i, c, n;
5727 unsigned char *msg = (unsigned char *) s;
5728 unsigned char *str, work[4];
5729
5730 /* Convert a single-byte string to multibyte. */
5731 for (i = 0; i < nbytes; i++)
5732 {
5733 c = unibyte_char_to_multibyte (msg[i]);
5734 n = CHAR_STRING (c, work, str);
5735 insert_1_both (work, 1, n, 1, 0, 0);
5736 }
5737 }
5738 else
5739 insert_1 (s, nbytes, 1, 0, 0);
5740 }
5741
5742 return 0;
5743}
5744
5745
5746/* Clear messages. CURRENT_P non-zero means clear the current
5747 message. LAST_DISPLAYED_P non-zero means clear the message
5748 last displayed. */
5749
5750void
5751clear_message (current_p, last_displayed_p)
5752 int current_p, last_displayed_p;
5753{
5754 if (current_p)
5755 echo_area_buffer[0] = Qnil;
5756
5757 if (last_displayed_p)
5758 echo_area_buffer[1] = Qnil;
5759
5760 message_buf_print = 0;
5761}
5762
5763/* Clear garbaged frames.
5764
5765 This function is used where the old redisplay called
5766 redraw_garbaged_frames which in turn called redraw_frame which in
5767 turn called clear_frame. The call to clear_frame was a source of
5768 flickering. I believe a clear_frame is not necessary. It should
5769 suffice in the new redisplay to invalidate all current matrices,
5770 and ensure a complete redisplay of all windows. */
5771
5772static void
5773clear_garbaged_frames ()
5774{
5f5c8ee5
GM
5775 if (frame_garbaged)
5776 {
5f5c8ee5
GM
5777 Lisp_Object tail, frame;
5778
5779 FOR_EACH_FRAME (tail, frame)
5780 {
5781 struct frame *f = XFRAME (frame);
5782
5783 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5784 {
5785 clear_current_matrices (f);
5786 f->garbaged = 0;
5787 }
5788 }
5789
5790 frame_garbaged = 0;
5791 ++windows_or_buffers_changed;
5792 }
c6e89d6c 5793}
5f5c8ee5 5794
5f5c8ee5 5795
c6e89d6c
GM
5796/* Redisplay the echo area of selected_frame. If UPDATE_FRAME_P is
5797 non-zero update selected_frame. Value is non-zero if the
5798 mini-windows height has been changed. */
5f5c8ee5 5799
c6e89d6c
GM
5800static int
5801echo_area_display (update_frame_p)
5802 int update_frame_p;
5803{
5804 Lisp_Object mini_window;
5805 struct window *w;
5806 struct frame *f;
5807 int window_height_changed_p = 0;
5808
5809 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5810 w = XWINDOW (mini_window);
5811 f = XFRAME (WINDOW_FRAME (w));
5812
5813 /* Don't display if frame is invisible or not yet initialized. */
5814 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5815 return 0;
5f5c8ee5 5816
c6e89d6c
GM
5817 /* When Emacs starts, selected_frame may be a visible terminal
5818 frame, even if we run under a window system. If we let this
5819 through, a message would be displayed on the terminal. */
5820#ifdef HAVE_WINDOW_SYSTEM
5821 if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
5822 return 0;
5823#endif /* HAVE_WINDOW_SYSTEM */
5824
5825 /* Redraw garbaged frames. */
5826 if (frame_garbaged)
5827 clear_garbaged_frames ();
5828
5829 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5830 {
5831 echo_area_window = mini_window;
5832 window_height_changed_p = display_echo_area (w);
5f5c8ee5 5833 w->must_be_updated_p = 1;
c59c668a 5834
5f5c8ee5
GM
5835 if (update_frame_p)
5836 {
c59c668a
GM
5837 /* Not called from redisplay_internal. If we changed
5838 window configuration, we must redisplay thoroughly.
5839 Otherwise, we can do with updating what we displayed
5840 above. */
5841 if (window_height_changed_p)
5842 {
5843 ++windows_or_buffers_changed;
5844 ++update_mode_lines;
5845 redisplay_internal (0);
5846 }
5847 else if (FRAME_WINDOW_P (f))
5f5c8ee5
GM
5848 {
5849 update_single_window (w, 1);
5850 rif->flush_display (f);
5851 }
5852 else
5853 update_frame (f, 1, 1);
5854 }
5855 }
5856 else if (!EQ (mini_window, selected_window))
5857 windows_or_buffers_changed++;
c59c668a
GM
5858
5859 /* Last displayed message is now the current message. */
dd2eb166
GM
5860 echo_area_buffer[1] = echo_area_buffer[0];
5861
5f5c8ee5
GM
5862 /* Prevent redisplay optimization in redisplay_internal by resetting
5863 this_line_start_pos. This is done because the mini-buffer now
5864 displays the message instead of its buffer text. */
5865 if (EQ (mini_window, selected_window))
5866 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
5867
5868 return window_height_changed_p;
5f5c8ee5
GM
5869}
5870
5871
5872\f
5873/***********************************************************************
5874 Frame Titles
5875 ***********************************************************************/
5876
5877
5878#ifdef HAVE_WINDOW_SYSTEM
5879
5880/* A buffer for constructing frame titles in it; allocated from the
5881 heap in init_xdisp and resized as needed in store_frame_title_char. */
5882
5883static char *frame_title_buf;
5884
5885/* The buffer's end, and a current output position in it. */
5886
5887static char *frame_title_buf_end;
5888static char *frame_title_ptr;
5889
5890
5891/* Store a single character C for the frame title in frame_title_buf.
5892 Re-allocate frame_title_buf if necessary. */
5893
5894static void
5895store_frame_title_char (c)
5896 char c;
5897{
5898 /* If output position has reached the end of the allocated buffer,
5899 double the buffer's size. */
5900 if (frame_title_ptr == frame_title_buf_end)
5901 {
5902 int len = frame_title_ptr - frame_title_buf;
5903 int new_size = 2 * len * sizeof *frame_title_buf;
5904 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5905 frame_title_buf_end = frame_title_buf + new_size;
5906 frame_title_ptr = frame_title_buf + len;
5907 }
5908
5909 *frame_title_ptr++ = c;
5910}
5911
5912
5913/* Store part of a frame title in frame_title_buf, beginning at
5914 frame_title_ptr. STR is the string to store. Do not copy more
5915 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5916 the whole string. Pad with spaces until FIELD_WIDTH number of
5917 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5918 Called from display_mode_element when it is used to build a frame
5919 title. */
5920
5921static int
5922store_frame_title (str, field_width, precision)
5923 unsigned char *str;
5924 int field_width, precision;
5925{
5926 int n = 0;
5927
5928 /* Copy at most PRECISION chars from STR. */
5929 while ((precision <= 0 || n < precision)
5930 && *str)
5931 {
5932 store_frame_title_char (*str++);
5933 ++n;
5934 }
5935
5936 /* Fill up with spaces until FIELD_WIDTH reached. */
5937 while (field_width > 0
5938 && n < field_width)
5939 {
5940 store_frame_title_char (' ');
5941 ++n;
5942 }
5943
5944 return n;
5945}
5946
5947
5948/* Set the title of FRAME, if it has changed. The title format is
5949 Vicon_title_format if FRAME is iconified, otherwise it is
5950 frame_title_format. */
5951
5952static void
5953x_consider_frame_title (frame)
5954 Lisp_Object frame;
5955{
5956 struct frame *f = XFRAME (frame);
5957
5958 if (FRAME_WINDOW_P (f)
5959 || FRAME_MINIBUF_ONLY_P (f)
5960 || f->explicit_name)
5961 {
5962 /* Do we have more than one visible frame on this X display? */
5963 Lisp_Object tail;
5964 Lisp_Object fmt;
5965 struct buffer *obuf;
5966 int len;
5967 struct it it;
5968
5969 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
5970 {
5971 struct frame *tf = XFRAME (XCONS (tail)->car);
5972
5973 if (tf != f
5974 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
5975 && !FRAME_MINIBUF_ONLY_P (tf)
5976 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
5977 break;
5978 }
5979
5980 /* Set global variable indicating that multiple frames exist. */
5981 multiple_frames = CONSP (tail);
5982
5983 /* Switch to the buffer of selected window of the frame. Set up
5984 frame_title_ptr so that display_mode_element will output into it;
5985 then display the title. */
5986 obuf = current_buffer;
5987 Fset_buffer (XWINDOW (f->selected_window)->buffer);
5988 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
5989 frame_title_ptr = frame_title_buf;
5990 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
5991 NULL, DEFAULT_FACE_ID);
5992 len = display_mode_element (&it, 0, -1, -1, fmt);
5993 frame_title_ptr = NULL;
5994 set_buffer_internal (obuf);
5995
5996 /* Set the title only if it's changed. This avoids consing in
5997 the common case where it hasn't. (If it turns out that we've
5998 already wasted too much time by walking through the list with
5999 display_mode_element, then we might need to optimize at a
6000 higher level than this.) */
6001 if (! STRINGP (f->name)
6002 || STRING_BYTES (XSTRING (f->name)) != len
6003 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6004 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6005 }
6006}
6007
6008#else /* not HAVE_WINDOW_SYSTEM */
6009
6010#define frame_title_ptr ((char *)0)
6011#define store_frame_title(str, mincol, maxcol) 0
6012
6013#endif /* not HAVE_WINDOW_SYSTEM */
6014
6015
6016
6017\f
6018/***********************************************************************
6019 Menu Bars
6020 ***********************************************************************/
6021
6022
6023/* Prepare for redisplay by updating menu-bar item lists when
6024 appropriate. This can call eval. */
6025
6026void
6027prepare_menu_bars ()
6028{
6029 int all_windows;
6030 struct gcpro gcpro1, gcpro2;
6031 struct frame *f;
6032 struct frame *tooltip_frame;
6033
6034#ifdef HAVE_X_WINDOWS
6035 tooltip_frame = tip_frame;
6036#else
6037 tooltip_frame = NULL;
6038#endif
6039
6040 /* Update all frame titles based on their buffer names, etc. We do
6041 this before the menu bars so that the buffer-menu will show the
6042 up-to-date frame titles. */
6043#ifdef HAVE_WINDOW_SYSTEM
6044 if (windows_or_buffers_changed || update_mode_lines)
6045 {
6046 Lisp_Object tail, frame;
6047
6048 FOR_EACH_FRAME (tail, frame)
6049 {
6050 f = XFRAME (frame);
6051 if (f != tooltip_frame
6052 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6053 x_consider_frame_title (frame);
6054 }
6055 }
6056#endif /* HAVE_WINDOW_SYSTEM */
6057
6058 /* Update the menu bar item lists, if appropriate. This has to be
6059 done before any actual redisplay or generation of display lines. */
6060 all_windows = (update_mode_lines
6061 || buffer_shared > 1
6062 || windows_or_buffers_changed);
6063 if (all_windows)
6064 {
6065 Lisp_Object tail, frame;
6066 int count = specpdl_ptr - specpdl;
6067
6068 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6069
6070 FOR_EACH_FRAME (tail, frame)
6071 {
6072 f = XFRAME (frame);
6073
6074 /* Ignore tooltip frame. */
6075 if (f == tooltip_frame)
6076 continue;
6077
6078 /* If a window on this frame changed size, report that to
6079 the user and clear the size-change flag. */
6080 if (FRAME_WINDOW_SIZES_CHANGED (f))
6081 {
6082 Lisp_Object functions;
6083
6084 /* Clear flag first in case we get an error below. */
6085 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6086 functions = Vwindow_size_change_functions;
6087 GCPRO2 (tail, functions);
6088
6089 while (CONSP (functions))
6090 {
6091 call1 (XCAR (functions), frame);
6092 functions = XCDR (functions);
6093 }
6094 UNGCPRO;
6095 }
6096
6097 GCPRO1 (tail);
6098 update_menu_bar (f, 0);
6099#ifdef HAVE_WINDOW_SYSTEM
6100 update_toolbar (f, 0);
6101#endif
6102 UNGCPRO;
6103 }
6104
6105 unbind_to (count, Qnil);
6106 }
6107 else
6108 {
6109 update_menu_bar (selected_frame, 1);
6110#ifdef HAVE_WINDOW_SYSTEM
6111 update_toolbar (selected_frame, 1);
6112#endif
6113 }
6114
6115 /* Motif needs this. See comment in xmenu.c. Turn it off when
6116 pending_menu_activation is not defined. */
6117#ifdef USE_X_TOOLKIT
6118 pending_menu_activation = 0;
6119#endif
6120}
6121
6122
6123/* Update the menu bar item list for frame F. This has to be done
6124 before we start to fill in any display lines, because it can call
6125 eval.
6126
6127 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6128
6129static void
6130update_menu_bar (f, save_match_data)
6131 struct frame *f;
6132 int save_match_data;
6133{
6134 Lisp_Object window;
6135 register struct window *w;
6136
6137 window = FRAME_SELECTED_WINDOW (f);
6138 w = XWINDOW (window);
6139
6140 if (update_mode_lines)
6141 w->update_mode_line = Qt;
6142
6143 if (FRAME_WINDOW_P (f)
6144 ?
6145#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6146 FRAME_EXTERNAL_MENU_BAR (f)
6147#else
6148 FRAME_MENU_BAR_LINES (f) > 0
6149#endif
6150 : FRAME_MENU_BAR_LINES (f) > 0)
6151 {
6152 /* If the user has switched buffers or windows, we need to
6153 recompute to reflect the new bindings. But we'll
6154 recompute when update_mode_lines is set too; that means
6155 that people can use force-mode-line-update to request
6156 that the menu bar be recomputed. The adverse effect on
6157 the rest of the redisplay algorithm is about the same as
6158 windows_or_buffers_changed anyway. */
6159 if (windows_or_buffers_changed
6160 || !NILP (w->update_mode_line)
6161 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6162 < BUF_MODIFF (XBUFFER (w->buffer)))
6163 != !NILP (w->last_had_star))
6164 || ((!NILP (Vtransient_mark_mode)
6165 && !NILP (XBUFFER (w->buffer)->mark_active))
6166 != !NILP (w->region_showing)))
6167 {
6168 struct buffer *prev = current_buffer;
6169 int count = specpdl_ptr - specpdl;
6170
6171 set_buffer_internal_1 (XBUFFER (w->buffer));
6172 if (save_match_data)
6173 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6174 if (NILP (Voverriding_local_map_menu_flag))
6175 {
6176 specbind (Qoverriding_terminal_local_map, Qnil);
6177 specbind (Qoverriding_local_map, Qnil);
6178 }
6179
6180 /* Run the Lucid hook. */
6181 call1 (Vrun_hooks, Qactivate_menubar_hook);
6182
6183 /* If it has changed current-menubar from previous value,
6184 really recompute the menu-bar from the value. */
6185 if (! NILP (Vlucid_menu_bar_dirty_flag))
6186 call0 (Qrecompute_lucid_menubar);
6187
6188 safe_run_hooks (Qmenu_bar_update_hook);
6189 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6190
6191 /* Redisplay the menu bar in case we changed it. */
6192#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6193 if (FRAME_WINDOW_P (f))
6194 set_frame_menubar (f, 0, 0);
6195 else
6196 /* On a terminal screen, the menu bar is an ordinary screen
6197 line, and this makes it get updated. */
6198 w->update_mode_line = Qt;
6199#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6200 /* In the non-toolkit version, the menu bar is an ordinary screen
6201 line, and this makes it get updated. */
6202 w->update_mode_line = Qt;
6203#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6204
6205 unbind_to (count, Qnil);
6206 set_buffer_internal_1 (prev);
6207 }
6208 }
6209}
6210
6211
6212\f
6213/***********************************************************************
6214 Toolbars
6215 ***********************************************************************/
6216
6217#ifdef HAVE_WINDOW_SYSTEM
6218
6219/* Update the toolbar item list for frame F. This has to be done
6220 before we start to fill in any display lines. Called from
6221 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6222 and restore it here. */
6223
6224static void
6225update_toolbar (f, save_match_data)
6226 struct frame *f;
6227 int save_match_data;
6228{
6229 if (WINDOWP (f->toolbar_window)
6230 && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
6231 {
6232 Lisp_Object window;
6233 struct window *w;
6234
6235 window = FRAME_SELECTED_WINDOW (f);
6236 w = XWINDOW (window);
6237
6238 /* If the user has switched buffers or windows, we need to
6239 recompute to reflect the new bindings. But we'll
6240 recompute when update_mode_lines is set too; that means
6241 that people can use force-mode-line-update to request
6242 that the menu bar be recomputed. The adverse effect on
6243 the rest of the redisplay algorithm is about the same as
6244 windows_or_buffers_changed anyway. */
6245 if (windows_or_buffers_changed
6246 || !NILP (w->update_mode_line)
6247 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6248 < BUF_MODIFF (XBUFFER (w->buffer)))
6249 != !NILP (w->last_had_star))
6250 || ((!NILP (Vtransient_mark_mode)
6251 && !NILP (XBUFFER (w->buffer)->mark_active))
6252 != !NILP (w->region_showing)))
6253 {
6254 struct buffer *prev = current_buffer;
6255 int count = specpdl_ptr - specpdl;
a2889657 6256
5f5c8ee5
GM
6257 /* Set current_buffer to the buffer of the selected
6258 window of the frame, so that we get the right local
6259 keymaps. */
6260 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 6261
5f5c8ee5
GM
6262 /* Save match data, if we must. */
6263 if (save_match_data)
6264 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6265
6266 /* Make sure that we don't accidentally use bogus keymaps. */
6267 if (NILP (Voverriding_local_map_menu_flag))
6268 {
6269 specbind (Qoverriding_terminal_local_map, Qnil);
6270 specbind (Qoverriding_local_map, Qnil);
1f40cad2 6271 }
1f40cad2 6272
5f5c8ee5
GM
6273 /* Build desired toolbar items from keymaps. */
6274 f->desired_toolbar_items
6275 = toolbar_items (f->desired_toolbar_items,
6276 &f->n_desired_toolbar_items);
6277
6278 /* Redisplay the toolbar in case we changed it. */
6279 w->update_mode_line = Qt;
6280
6281 unbind_to (count, Qnil);
6282 set_buffer_internal_1 (prev);
81d478f3 6283 }
a2889657
JB
6284 }
6285}
6286
6c4429a5 6287
5f5c8ee5
GM
6288/* Set F->desired_toolbar_string to a Lisp string representing frame
6289 F's desired toolbar contents. F->desired_toolbar_items must have
6290 been set up previously by calling prepare_menu_bars. */
6291
a2889657 6292static void
5f5c8ee5
GM
6293build_desired_toolbar_string (f)
6294 struct frame *f;
a2889657 6295{
5f5c8ee5
GM
6296 int i, size, size_needed, string_idx;
6297 struct gcpro gcpro1, gcpro2, gcpro3;
6298 Lisp_Object image, plist, props;
a2889657 6299
5f5c8ee5
GM
6300 image = plist = props = Qnil;
6301 GCPRO3 (image, plist, props);
a2889657 6302
5f5c8ee5
GM
6303 /* Prepare F->desired_toolbar_string. If we can reuse it, do so.
6304 Otherwise, make a new string. */
6305
6306 /* The size of the string we might be able to reuse. */
6307 size = (STRINGP (f->desired_toolbar_string)
6308 ? XSTRING (f->desired_toolbar_string)->size
6309 : 0);
6310
6311 /* Each image in the string we build is preceded by a space,
6312 and there is a space at the end. */
6313 size_needed = f->n_desired_toolbar_items + 1;
6314
6315 /* Reuse f->desired_toolbar_string, if possible. */
6316 if (size < size_needed)
6317 f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
6318 else
6319 {
6320 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6321 Fremove_text_properties (make_number (0), make_number (size),
6322 props, f->desired_toolbar_string);
6323 }
a2889657 6324
5f5c8ee5
GM
6325 /* Put a `display' property on the string for the images to display,
6326 put a `menu_item' property on toolbar items with a value that
6327 is the index of the item in F's toolbar item vector. */
6328 for (i = 0, string_idx = 0;
6329 i < f->n_desired_toolbar_items;
6330 ++i, string_idx += 1)
a2889657 6331 {
5f5c8ee5
GM
6332#define PROP(IDX) \
6333 (XVECTOR (f->desired_toolbar_items) \
6334 ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
6335
6336 int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
6337 int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
6338 int margin, relief;
6339 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6340 extern Lisp_Object Qlaplace;
6341
6342 /* If image is a vector, choose the image according to the
6343 button state. */
6344 image = PROP (TOOLBAR_ITEM_IMAGES);
6345 if (VECTORP (image))
6346 {
6347 enum toolbar_item_image idx;
6348
6349 if (enabled_p)
6350 idx = (selected_p
6351 ? TOOLBAR_IMAGE_ENABLED_SELECTED
6352 : TOOLBAR_IMAGE_ENABLED_DESELECTED);
6353 else
6354 idx = (selected_p
6355 ? TOOLBAR_IMAGE_DISABLED_SELECTED
6356 : TOOLBAR_IMAGE_DISABLED_DESELECTED);
6357
6358 xassert (XVECTOR (image)->size >= idx);
6359 image = XVECTOR (image)->contents[idx];
6360 }
6361
6362 /* Ignore invalid image specifications. */
6363 if (!valid_image_p (image))
6364 continue;
6365
6366 /* Display the toolbar button pressed, or depressed. */
6367 plist = Fcopy_sequence (XCDR (image));
6368
6369 /* Compute margin and relief to draw. */
6370 relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
6371 margin = relief + max (0, toolbar_button_margin);
6372
6373 if (auto_raise_toolbar_buttons_p)
6374 {
6375 /* Add a `:relief' property to the image spec if the item is
6376 selected. */
6377 if (selected_p)
6378 {
6379 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6380 margin -= relief;
6381 }
6382 }
6383 else
6384 {
6385 /* If image is selected, display it pressed, i.e. with a
6386 negative relief. If it's not selected, display it with a
6387 raised relief. */
6388 plist = Fplist_put (plist, QCrelief,
6389 (selected_p
6390 ? make_number (-relief)
6391 : make_number (relief)));
6392 margin -= relief;
6393 }
6394
6395 /* Put a margin around the image. */
6396 if (margin)
6397 plist = Fplist_put (plist, QCmargin, make_number (margin));
6398
6399 /* If button is not enabled, make the image appear disabled by
6400 applying an appropriate algorithm to it. */
6401 if (!enabled_p)
6402 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6403
6404 /* Put a `display' text property on the string for the image to
6405 display. Put a `menu-item' property on the string that gives
6406 the start of this item's properties in the toolbar items
6407 vector. */
6408 image = Fcons (Qimage, plist);
6409 props = list4 (Qdisplay, image,
6410 Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
6411 Fadd_text_properties (make_number (string_idx),
6412 make_number (string_idx + 1),
6413 props, f->desired_toolbar_string);
6414#undef PROP
a2889657
JB
6415 }
6416
5f5c8ee5
GM
6417 UNGCPRO;
6418}
6419
6420
6421/* Display one line of the toolbar of frame IT->f. */
6422
6423static void
6424display_toolbar_line (it)
6425 struct it *it;
6426{
6427 struct glyph_row *row = it->glyph_row;
6428 int max_x = it->last_visible_x;
6429 struct glyph *last;
6430
6431 prepare_desired_row (row);
6432 row->y = it->current_y;
6433
6434 while (it->current_x < max_x)
a2889657 6435 {
5f5c8ee5 6436 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 6437
5f5c8ee5
GM
6438 /* Get the next display element. */
6439 if (!get_next_display_element (it))
6440 break;
73af359d 6441
5f5c8ee5
GM
6442 /* Produce glyphs. */
6443 x_before = it->current_x;
6444 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6445 PRODUCE_GLYPHS (it);
daa37602 6446
5f5c8ee5
GM
6447 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6448 i = 0;
6449 x = x_before;
6450 while (i < nglyphs)
6451 {
6452 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6453
6454 if (x + glyph->pixel_width > max_x)
6455 {
6456 /* Glyph doesn't fit on line. */
6457 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6458 it->current_x = x;
6459 goto out;
6460 }
daa37602 6461
5f5c8ee5
GM
6462 ++it->hpos;
6463 x += glyph->pixel_width;
6464 ++i;
6465 }
6466
6467 /* Stop at line ends. */
6468 if (ITERATOR_AT_END_OF_LINE_P (it))
6469 break;
6470
6471 set_iterator_to_next (it);
a2889657 6472 }
a2889657 6473
5f5c8ee5 6474 out:;
a2889657 6475
5f5c8ee5
GM
6476 row->displays_text_p = row->used[TEXT_AREA] != 0;
6477 extend_face_to_end_of_line (it);
6478 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6479 last->right_box_line_p = 1;
6480 compute_line_metrics (it);
6481
6482 /* If line is empty, make it occupy the rest of the toolbar. */
6483 if (!row->displays_text_p)
6484 {
312246d1
GM
6485 row->height = row->phys_height = it->last_visible_y - row->y;
6486 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
6487 }
6488
6489 row->full_width_p = 1;
6490 row->continued_p = 0;
6491 row->truncated_on_left_p = 0;
6492 row->truncated_on_right_p = 0;
6493
6494 it->current_x = it->hpos = 0;
6495 it->current_y += row->height;
6496 ++it->vpos;
6497 ++it->glyph_row;
a2889657 6498}
96a410bc 6499
5f5c8ee5
GM
6500
6501/* Value is the number of screen lines needed to make all toolbar
6502 items of frame F visible. */
96a410bc 6503
d39b6696 6504static int
5f5c8ee5
GM
6505toolbar_lines_needed (f)
6506 struct frame *f;
d39b6696 6507{
5f5c8ee5
GM
6508 struct window *w = XWINDOW (f->toolbar_window);
6509 struct it it;
6510
6511 /* Initialize an iterator for iteration over F->desired_toolbar_string
6512 in the toolbar window of frame F. */
6513 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6514 it.first_visible_x = 0;
6515 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6516 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
6517
6518 while (!ITERATOR_AT_END_P (&it))
6519 {
6520 it.glyph_row = w->desired_matrix->rows;
6521 clear_glyph_row (it.glyph_row);
6522 display_toolbar_line (&it);
6523 }
6524
6525 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 6526}
96a410bc 6527
5f5c8ee5
GM
6528
6529/* Display the toolbar of frame F. Value is non-zero if toolbar's
6530 height should be changed. */
6531
6532static int
6533redisplay_toolbar (f)
6534 struct frame *f;
96a410bc 6535{
5f5c8ee5
GM
6536 struct window *w;
6537 struct it it;
6538 struct glyph_row *row;
6539 int change_height_p = 0;
6540
6541 /* If frame hasn't a toolbar window or if it is zero-height, don't
6542 do anything. This means you must start with toolbar-lines
6543 non-zero to get the auto-sizing effect. Or in other words, you
6544 can turn off toolbars by specifying toolbar-lines zero. */
6545 if (!WINDOWP (f->toolbar_window)
6546 || (w = XWINDOW (f->toolbar_window),
6547 XFASTINT (w->height) == 0))
6548 return 0;
96a410bc 6549
5f5c8ee5
GM
6550 /* Set up an iterator for the toolbar window. */
6551 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6552 it.first_visible_x = 0;
6553 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6554 row = it.glyph_row;
3450d04c 6555
5f5c8ee5
GM
6556 /* Build a string that represents the contents of the toolbar. */
6557 build_desired_toolbar_string (f);
6558 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
3450d04c 6559
5f5c8ee5
GM
6560 /* Display as many lines as needed to display all toolbar items. */
6561 while (it.current_y < it.last_visible_y)
6562 display_toolbar_line (&it);
3450d04c 6563
5f5c8ee5
GM
6564 /* It doesn't make much sense to try scrolling in the toolbar
6565 window, so don't do it. */
6566 w->desired_matrix->no_scrolling_p = 1;
6567 w->must_be_updated_p = 1;
3450d04c 6568
5f5c8ee5
GM
6569 if (auto_resize_toolbars_p)
6570 {
6571 int nlines;
6572
6573 /* If there are blank lines at the end, except for a partially
6574 visible blank line at the end that is smaller than
6575 CANON_Y_UNIT, change the toolbar's height. */
6576 row = it.glyph_row - 1;
6577 if (!row->displays_text_p
6578 && row->height >= CANON_Y_UNIT (f))
6579 change_height_p = 1;
6580
6581 /* If row displays toolbar items, but is partially visible,
6582 change the toolbar's height. */
6583 if (row->displays_text_p
6584 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6585 change_height_p = 1;
6586
6587 /* Resize windows as needed by changing the `toolbar-lines'
6588 frame parameter. */
6589 if (change_height_p
6590 && (nlines = toolbar_lines_needed (f),
6591 nlines != XFASTINT (w->height)))
6592 {
6593 extern Lisp_Object Qtoolbar_lines;
6594 Lisp_Object frame;
6595
6596 XSETFRAME (frame, f);
6597 clear_glyph_matrix (w->desired_matrix);
6598 Fmodify_frame_parameters (frame,
6599 Fcons (Fcons (Qtoolbar_lines,
6600 make_number (nlines)),
6601 Qnil));
6602 fonts_changed_p = 1;
6603 }
6604 }
3450d04c 6605
5f5c8ee5 6606 return change_height_p;
96a410bc 6607}
90adcf20 6608
5f5c8ee5
GM
6609
6610/* Get information about the toolbar item which is displayed in GLYPH
6611 on frame F. Return in *PROP_IDX the index where toolbar item
6612 properties start in F->current_toolbar_items. Value is zero if
6613 GLYPH doesn't display a toolbar item. */
6614
6615int
6616toolbar_item_info (f, glyph, prop_idx)
6617 struct frame *f;
6618 struct glyph *glyph;
6619 int *prop_idx;
90adcf20 6620{
5f5c8ee5
GM
6621 Lisp_Object prop;
6622 int success_p;
6623
6624 /* Get the text property `menu-item' at pos. The value of that
6625 property is the start index of this item's properties in
6626 F->current_toolbar_items. */
6627 prop = Fget_text_property (make_number (glyph->charpos),
6628 Qmenu_item, f->current_toolbar_string);
6629 if (INTEGERP (prop))
6630 {
6631 *prop_idx = XINT (prop);
6632 success_p = 1;
6633 }
6634 else
6635 success_p = 0;
90adcf20 6636
5f5c8ee5
GM
6637 return success_p;
6638}
6639
6640#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6641
feb0c42f 6642
5f5c8ee5
GM
6643\f
6644/************************************************************************
6645 Horizontal scrolling
6646 ************************************************************************/
feb0c42f 6647
5f5c8ee5
GM
6648static int hscroll_window_tree P_ ((Lisp_Object));
6649static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6650
5f5c8ee5
GM
6651/* For all leaf windows in the window tree rooted at WINDOW, set their
6652 hscroll value so that PT is (i) visible in the window, and (ii) so
6653 that it is not within a certain margin at the window's left and
6654 right border. Value is non-zero if any window's hscroll has been
6655 changed. */
6656
6657static int
6658hscroll_window_tree (window)
6659 Lisp_Object window;
6660{
6661 int hscrolled_p = 0;
6662
6663 while (WINDOWP (window))
90adcf20 6664 {
5f5c8ee5
GM
6665 struct window *w = XWINDOW (window);
6666
6667 if (WINDOWP (w->hchild))
6668 hscrolled_p |= hscroll_window_tree (w->hchild);
6669 else if (WINDOWP (w->vchild))
6670 hscrolled_p |= hscroll_window_tree (w->vchild);
6671 else if (w->cursor.vpos >= 0)
6672 {
6673 int hscroll_margin, text_area_x, text_area_y;
6674 int text_area_width, text_area_height;
6675 struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
6676 w->cursor.vpos);
a2725ab2 6677
5f5c8ee5
GM
6678 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6679 &text_area_width, &text_area_height);
90adcf20 6680
5f5c8ee5
GM
6681 /* Scroll when cursor is inside this scroll margin. */
6682 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6683
6684 if ((XFASTINT (w->hscroll)
6685 && w->cursor.x < hscroll_margin)
6686 || (cursor_row->truncated_on_right_p
6687 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 6688 {
5f5c8ee5
GM
6689 struct it it;
6690 int hscroll;
6691 struct buffer *saved_current_buffer;
6692 int pt;
6693
6694 /* Find point in a display of infinite width. */
6695 saved_current_buffer = current_buffer;
6696 current_buffer = XBUFFER (w->buffer);
6697
6698 if (w == XWINDOW (selected_window))
6699 pt = BUF_PT (current_buffer);
6700 else
08b610e4 6701 {
5f5c8ee5
GM
6702 pt = marker_position (w->pointm);
6703 pt = max (BEGV, pt);
6704 pt = min (ZV, pt);
6705 }
6706
6707 /* Move iterator to pt starting at cursor_row->start in
6708 a line with infinite width. */
6709 init_to_row_start (&it, w, cursor_row);
6710 it.last_visible_x = INFINITY;
6711 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6712 current_buffer = saved_current_buffer;
6713
6714 /* Center cursor in window. */
6715 hscroll = (max (0, it.current_x - text_area_width / 2)
6716 / CANON_X_UNIT (it.f));
6717
6718 /* Don't call Fset_window_hscroll if value hasn't
6719 changed because it will prevent redisplay
6720 optimizations. */
6721 if (XFASTINT (w->hscroll) != hscroll)
6722 {
6723 Fset_window_hscroll (window, make_number (hscroll));
6724 hscrolled_p = 1;
08b610e4 6725 }
08b610e4 6726 }
08b610e4 6727 }
a2725ab2 6728
5f5c8ee5 6729 window = w->next;
90adcf20 6730 }
cd6dfed6 6731
5f5c8ee5
GM
6732 /* Value is non-zero if hscroll of any leaf window has been changed. */
6733 return hscrolled_p;
6734}
6735
6736
6737/* Set hscroll so that cursor is visible and not inside horizontal
6738 scroll margins for all windows in the tree rooted at WINDOW. See
6739 also hscroll_window_tree above. Value is non-zero if any window's
6740 hscroll has been changed. If it has, desired matrices on the frame
6741 of WINDOW are cleared. */
6742
6743static int
6744hscroll_windows (window)
6745 Lisp_Object window;
6746{
6747 int hscrolled_p = hscroll_window_tree (window);
6748 if (hscrolled_p)
6749 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6750 return hscrolled_p;
90adcf20 6751}
5f5c8ee5
GM
6752
6753
90adcf20 6754\f
5f5c8ee5
GM
6755/************************************************************************
6756 Redisplay
6757 ************************************************************************/
6758
6759/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6760 to a non-zero value. This is sometimes handy to have in a debugger
6761 session. */
6762
6763#if GLYPH_DEBUG
a2889657 6764
5f5c8ee5
GM
6765/* First and last unchanged row for try_window_id. */
6766
6767int debug_first_unchanged_at_end_vpos;
6768int debug_last_unchanged_at_beg_vpos;
6769
6770/* Delta vpos and y. */
6771
6772int debug_dvpos, debug_dy;
6773
6774/* Delta in characters and bytes for try_window_id. */
6775
6776int debug_delta, debug_delta_bytes;
6777
6778/* Values of window_end_pos and window_end_vpos at the end of
6779 try_window_id. */
6780
6781int debug_end_pos, debug_end_vpos;
6782
6783/* Append a string to W->desired_matrix->method. FMT is a printf
6784 format string. A1...A9 are a supplement for a variable-length
6785 argument list. If trace_redisplay_p is non-zero also printf the
6786 resulting string to stderr. */
6787
6788static void
6789debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6790 struct window *w;
6791 char *fmt;
6792 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6793{
6794 char buffer[512];
6795 char *method = w->desired_matrix->method;
6796 int len = strlen (method);
6797 int size = sizeof w->desired_matrix->method;
6798 int remaining = size - len - 1;
6799
6800 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6801 if (len && remaining)
6802 {
6803 method[len] = '|';
6804 --remaining, ++len;
6805 }
6806
6807 strncpy (method + len, buffer, remaining);
6808
6809 if (trace_redisplay_p)
6810 fprintf (stderr, "%p (%s): %s\n",
6811 w,
6812 ((BUFFERP (w->buffer)
6813 && STRINGP (XBUFFER (w->buffer)->name))
6814 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6815 : "no buffer"),
6816 buffer);
6817}
a2889657 6818
5f5c8ee5 6819#endif /* GLYPH_DEBUG */
90adcf20 6820
a2889657 6821
5f5c8ee5
GM
6822/* This counter is used to clear the face cache every once in a while
6823 in redisplay_internal. It is incremented for each redisplay.
6824 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6825 cleared. */
0d231165 6826
5f5c8ee5 6827#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
6828static int clear_face_cache_count;
6829
20de20dc 6830/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
6831
6832static struct frame *previous_terminal_frame;
6833
6834/* Non-zero while redisplay_internal is in progress. */
6835
6836int redisplaying_p;
6837
6838
6839/* Value is non-zero if all changes in window W, which displays
6840 current_buffer, are in the text between START and END. START is a
6841 buffer position, END is given as a distance from Z. Used in
6842 redisplay_internal for display optimization. */
6843
6844static INLINE int
6845text_outside_line_unchanged_p (w, start, end)
6846 struct window *w;
6847 int start, end;
6848{
6849 int unchanged_p = 1;
6850
6851 /* If text or overlays have changed, see where. */
6852 if (XFASTINT (w->last_modified) < MODIFF
6853 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6854 {
6855 /* Gap in the line? */
6856 if (GPT < start || Z - GPT < end)
6857 unchanged_p = 0;
6858
6859 /* Changes start in front of the line, or end after it? */
6860 if (unchanged_p
9142dd5b
GM
6861 && (BEG_UNCHANGED < start - 1
6862 || END_UNCHANGED < end))
5f5c8ee5
GM
6863 unchanged_p = 0;
6864
6865 /* If selective display, can't optimize if changes start at the
6866 beginning of the line. */
6867 if (unchanged_p
6868 && INTEGERP (current_buffer->selective_display)
6869 && XINT (current_buffer->selective_display) > 0
9142dd5b 6870 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
6871 unchanged_p = 0;
6872 }
6873
6874 return unchanged_p;
6875}
6876
6877
6878/* Do a frame update, taking possible shortcuts into account. This is
6879 the main external entry point for redisplay.
6880
6881 If the last redisplay displayed an echo area message and that message
6882 is no longer requested, we clear the echo area or bring back the
6883 mini-buffer if that is in use. */
20de20dc 6884
a2889657
JB
6885void
6886redisplay ()
e9874cee
RS
6887{
6888 redisplay_internal (0);
6889}
6890
5f5c8ee5 6891
9142dd5b
GM
6892/* Reconsider the setting of B->clip_changed which is displayed
6893 in window W. */
6894
6895static INLINE void
6896reconsider_clip_changes (w, b)
6897 struct window *w;
6898 struct buffer *b;
6899{
6900 if (b->prevent_redisplay_optimizations_p)
6901 b->clip_changed = 1;
6902 else if (b->clip_changed
6903 && !NILP (w->window_end_valid)
6904 && w->current_matrix->buffer == b
6905 && w->current_matrix->zv == BUF_ZV (b)
6906 && w->current_matrix->begv == BUF_BEGV (b))
6907 b->clip_changed = 0;
6908}
6909
6910
5f5c8ee5
GM
6911/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6912 response to any user action; therefore, we should preserve the echo
6913 area. (Actually, our caller does that job.) Perhaps in the future
6914 avoid recentering windows if it is not necessary; currently that
6915 causes some problems. */
e9874cee
RS
6916
6917static void
6918redisplay_internal (preserve_echo_area)
6919 int preserve_echo_area;
a2889657 6920{
5f5c8ee5
GM
6921 struct window *w = XWINDOW (selected_window);
6922 struct frame *f = XFRAME (w->frame);
6923 int pause;
a2889657 6924 int must_finish = 0;
5f5c8ee5 6925 struct text_pos tlbufpos, tlendpos;
89819bdd 6926 int number_of_visible_frames;
28514cd9 6927 int count;
a2889657 6928
5f5c8ee5
GM
6929 /* Non-zero means redisplay has to consider all windows on all
6930 frames. Zero means, only selected_window is considered. */
6931 int consider_all_windows_p;
6932
6933 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6934
6935 /* No redisplay if running in batch mode or frame is not yet fully
6936 initialized, or redisplay is explicitly turned off by setting
6937 Vinhibit_redisplay. */
6938 if (noninteractive
6939 || !NILP (Vinhibit_redisplay)
6940 || !f->glyphs_initialized_p)
a2889657
JB
6941 return;
6942
5f5c8ee5
GM
6943 /* The flag redisplay_performed_directly_p is set by
6944 direct_output_for_insert when it already did the whole screen
6945 update necessary. */
6946 if (redisplay_performed_directly_p)
6947 {
6948 redisplay_performed_directly_p = 0;
6949 if (!hscroll_windows (selected_window))
6950 return;
6951 }
6952
15f0cf78
RS
6953#ifdef USE_X_TOOLKIT
6954 if (popup_activated ())
6955 return;
6956#endif
6957
28514cd9 6958 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 6959 if (redisplaying_p)
735c094c
KH
6960 return;
6961
28514cd9
GM
6962 /* Record a function that resets redisplaying_p to its old value
6963 when we leave this function. */
6964 count = specpdl_ptr - specpdl;
6965 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
6966 ++redisplaying_p;
6967
8b32d885
RS
6968 retry:
6969
9142dd5b
GM
6970 reconsider_clip_changes (w, current_buffer);
6971
5f5c8ee5
GM
6972 /* If new fonts have been loaded that make a glyph matrix adjustment
6973 necessary, do it. */
6974 if (fonts_changed_p)
6975 {
6976 adjust_glyphs (NULL);
6977 ++windows_or_buffers_changed;
6978 fonts_changed_p = 0;
6979 }
6980
fd8ff63d 6981 if (! FRAME_WINDOW_P (selected_frame)
20de20dc
RS
6982 && previous_terminal_frame != selected_frame)
6983 {
5f5c8ee5
GM
6984 /* Since frames on an ASCII terminal share the same display
6985 area, displaying a different frame means redisplay the whole
6986 thing. */
20de20dc
RS
6987 windows_or_buffers_changed++;
6988 SET_FRAME_GARBAGED (selected_frame);
6989 XSETFRAME (Vterminal_frame, selected_frame);
6990 }
6991 previous_terminal_frame = selected_frame;
20de20dc 6992
5f5c8ee5
GM
6993 /* Set the visible flags for all frames. Do this before checking
6994 for resized or garbaged frames; they want to know if their frames
6995 are visible. See the comment in frame.h for
6996 FRAME_SAMPLE_VISIBILITY. */
d724d989 6997 {
35f56f96 6998 Lisp_Object tail, frame;
d724d989 6999
89819bdd
RS
7000 number_of_visible_frames = 0;
7001
35f56f96 7002 FOR_EACH_FRAME (tail, frame)
f82aff7c 7003 {
5f5c8ee5
GM
7004 struct frame *f = XFRAME (frame);
7005
7006 FRAME_SAMPLE_VISIBILITY (f);
7007 if (FRAME_VISIBLE_P (f))
7008 ++number_of_visible_frames;
7009 clear_desired_matrices (f);
f82aff7c 7010 }
d724d989
JB
7011 }
7012
44fa5b1e 7013 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 7014 do_pending_window_change (1);
a2889657 7015
5f5c8ee5 7016 /* Clear frames marked as garbaged. */
44fa5b1e 7017 if (frame_garbaged)
c6e89d6c 7018 clear_garbaged_frames ();
a2889657 7019
5f5c8ee5 7020 /* Build menubar and toolbar items. */
f82aff7c
RS
7021 prepare_menu_bars ();
7022
28995e67 7023 if (windows_or_buffers_changed)
a2889657
JB
7024 update_mode_lines++;
7025
538f13d4
RS
7026 /* Detect case that we need to write or remove a star in the mode line. */
7027 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
7028 {
7029 w->update_mode_line = Qt;
7030 if (buffer_shared > 1)
7031 update_mode_lines++;
7032 }
7033
5f5c8ee5 7034 /* If %c is in the mode line, update it if needed. */
28995e67
RS
7035 if (!NILP (w->column_number_displayed)
7036 /* This alternative quickly identifies a common case
7037 where no change is needed. */
7038 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7039 && XFASTINT (w->last_modified) >= MODIFF
7040 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7041 && XFASTINT (w->column_number_displayed) != current_column ())
7042 w->update_mode_line = Qt;
7043
44fa5b1e 7044 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 7045
5f5c8ee5
GM
7046 /* The variable buffer_shared is set in redisplay_window and
7047 indicates that we redisplay a buffer in different windows. See
7048 there. */
7049 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
7050
7051 /* If specs for an arrow have changed, do thorough redisplay
7052 to ensure we remove any arrow that should no longer exist. */
d45de95b 7053 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 7054 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 7055 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 7056
90adcf20
RS
7057 /* Normally the message* functions will have already displayed and
7058 updated the echo area, but the frame may have been trashed, or
7059 the update may have been preempted, so display the echo area
c6e89d6c
GM
7060 again here. Checking both message buffers captures the case that
7061 the echo area should be cleared. */
7062 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 7063 {
c6e89d6c 7064 int window_height_changed_p = echo_area_display (0);
90adcf20 7065 must_finish = 1;
dd2eb166 7066
c6e89d6c
GM
7067 if (fonts_changed_p)
7068 goto retry;
7069 else if (window_height_changed_p)
7070 {
7071 consider_all_windows_p = 1;
7072 ++update_mode_lines;
7073 ++windows_or_buffers_changed;
9142dd5b
GM
7074
7075 /* If window configuration was changed, frames may have been
7076 marked garbaged. Clear them or we will experience
7077 surprises wrt scrolling. */
7078 if (frame_garbaged)
7079 clear_garbaged_frames ();
c6e89d6c 7080 }
90adcf20 7081 }
dd2eb166
GM
7082 else if (w == XWINDOW (minibuf_window)
7083 && (current_buffer->clip_changed
7084 || XFASTINT (w->last_modified) < MODIFF
7085 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7086 && resize_mini_window (w))
c6e89d6c
GM
7087 {
7088 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
7089 showing if its contents might have changed. */
7090 must_finish = 1;
7091 consider_all_windows_p = 1;
c6e89d6c 7092 ++windows_or_buffers_changed;
dd2eb166 7093 ++update_mode_lines;
9142dd5b
GM
7094
7095 /* If window configuration was changed, frames may have been
7096 marked garbaged. Clear them or we will experience
7097 surprises wrt scrolling. */
7098 if (frame_garbaged)
7099 clear_garbaged_frames ();
c6e89d6c
GM
7100 }
7101
90adcf20 7102
5f5c8ee5
GM
7103 /* If showing the region, and mark has changed, we must redisplay
7104 the whole window. The assignment to this_line_start_pos prevents
7105 the optimization directly below this if-statement. */
bd66d1ba
RS
7106 if (((!NILP (Vtransient_mark_mode)
7107 && !NILP (XBUFFER (w->buffer)->mark_active))
7108 != !NILP (w->region_showing))
82d04750
JB
7109 || (!NILP (w->region_showing)
7110 && !EQ (w->region_showing,
7111 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
7112 CHARPOS (this_line_start_pos) = 0;
7113
7114 /* Optimize the case that only the line containing the cursor in the
7115 selected window has changed. Variables starting with this_ are
7116 set in display_line and record information about the line
7117 containing the cursor. */
7118 tlbufpos = this_line_start_pos;
7119 tlendpos = this_line_end_pos;
7120 if (!consider_all_windows_p
7121 && CHARPOS (tlbufpos) > 0
7122 && NILP (w->update_mode_line)
73af359d 7123 && !current_buffer->clip_changed
44fa5b1e 7124 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 7125 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 7126 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
7127 && this_line_buffer == current_buffer
7128 && current_buffer == XBUFFER (w->buffer)
265a9e55 7129 && NILP (w->force_start)
5f5c8ee5
GM
7130 /* Point must be on the line that we have info recorded about. */
7131 && PT >= CHARPOS (tlbufpos)
7132 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
7133 /* All text outside that line, including its final newline,
7134 must be unchanged */
5f5c8ee5
GM
7135 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7136 CHARPOS (tlendpos)))
7137 {
7138 if (CHARPOS (tlbufpos) > BEGV
7139 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7140 && (CHARPOS (tlbufpos) == ZV
7141 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
7142 /* Former continuation line has disappeared by becoming empty */
7143 goto cancel;
7144 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 7145 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
7146 || MINI_WINDOW_P (w))
7147 {
1c9241f5
KH
7148 /* We have to handle the case of continuation around a
7149 wide-column character (See the comment in indent.c around
7150 line 885).
7151
7152 For instance, in the following case:
7153
7154 -------- Insert --------
7155 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7156 J_I_ ==> J_I_ `^^' are cursors.
7157 ^^ ^^
7158 -------- --------
7159
7160 As we have to redraw the line above, we should goto cancel. */
7161
5f5c8ee5
GM
7162 struct it it;
7163 int line_height_before = this_line_pixel_height;
7164
7165 /* Note that start_display will handle the case that the
7166 line starting at tlbufpos is a continuation lines. */
7167 start_display (&it, w, tlbufpos);
7168
7169 /* Implementation note: It this still necessary? */
7170 if (it.current_x != this_line_start_x)
1c9241f5
KH
7171 goto cancel;
7172
5f5c8ee5
GM
7173 TRACE ((stderr, "trying display optimization 1\n"));
7174 w->cursor.vpos = -1;
a2889657 7175 overlay_arrow_seen = 0;
5f5c8ee5
GM
7176 it.vpos = this_line_vpos;
7177 it.current_y = this_line_y;
7178 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7179 display_line (&it);
7180
a2889657 7181 /* If line contains point, is not continued,
5f5c8ee5
GM
7182 and ends at same distance from eob as before, we win */
7183 if (w->cursor.vpos >= 0
7184 /* Line is not continued, otherwise this_line_start_pos
7185 would have been set to 0 in display_line. */
7186 && CHARPOS (this_line_start_pos)
7187 /* Line ends as before. */
7188 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7189 /* Line has same height as before. Otherwise other lines
7190 would have to be shifted up or down. */
7191 && this_line_pixel_height == line_height_before)
a2889657 7192 {
5f5c8ee5
GM
7193 /* If this is not the window's last line, we must adjust
7194 the charstarts of the lines below. */
7195 if (it.current_y < it.last_visible_y)
7196 {
7197 struct glyph_row *row
7198 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7199 int delta, delta_bytes;
7200
7201 if (Z - CHARPOS (tlendpos) == ZV)
7202 {
7203 /* This line ends at end of (accessible part of)
7204 buffer. There is no newline to count. */
7205 delta = (Z
7206 - CHARPOS (tlendpos)
7207 - MATRIX_ROW_START_CHARPOS (row));
7208 delta_bytes = (Z_BYTE
7209 - BYTEPOS (tlendpos)
7210 - MATRIX_ROW_START_BYTEPOS (row));
7211 }
7212 else
7213 {
7214 /* This line ends in a newline. Must take
7215 account of the newline and the rest of the
7216 text that follows. */
7217 delta = (Z
7218 - CHARPOS (tlendpos)
7219 - MATRIX_ROW_START_CHARPOS (row));
7220 delta_bytes = (Z_BYTE
7221 - BYTEPOS (tlendpos)
7222 - MATRIX_ROW_START_BYTEPOS (row));
7223 }
7224
7225 increment_glyph_matrix_buffer_positions (w->current_matrix,
7226 this_line_vpos + 1,
7227 w->current_matrix->nrows,
7228 delta, delta_bytes);
85bcef6c 7229 }
46db8486 7230
5f5c8ee5
GM
7231 /* If this row displays text now but previously didn't,
7232 or vice versa, w->window_end_vpos may have to be
7233 adjusted. */
7234 if ((it.glyph_row - 1)->displays_text_p)
7235 {
7236 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7237 XSETINT (w->window_end_vpos, this_line_vpos);
7238 }
7239 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7240 && this_line_vpos > 0)
7241 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7242 w->window_end_valid = Qnil;
7243
7244 /* Update hint: No need to try to scroll in update_window. */
7245 w->desired_matrix->no_scrolling_p = 1;
7246
7247#if GLYPH_DEBUG
7248 *w->desired_matrix->method = 0;
7249 debug_method_add (w, "optimization 1");
7250#endif
a2889657
JB
7251 goto update;
7252 }
7253 else
7254 goto cancel;
7255 }
5f5c8ee5
GM
7256 else if (/* Cursor position hasn't changed. */
7257 PT == XFASTINT (w->last_point)
b6f0fe04
RS
7258 /* Make sure the cursor was last displayed
7259 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
7260 && 0 <= w->cursor.vpos
7261 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
7262 {
7263 if (!must_finish)
7264 {
c6e89d6c 7265 do_pending_window_change (1);
5f5c8ee5
GM
7266
7267 /* We used to always goto end_of_redisplay here, but this
7268 isn't enough if we have a blinking cursor. */
7269 if (w->cursor_off_p == w->last_cursor_off_p)
7270 goto end_of_redisplay;
a2889657
JB
7271 }
7272 goto update;
7273 }
8b51f1e3
KH
7274 /* If highlighting the region, or if the cursor is in the echo area,
7275 then we can't just move the cursor. */
bd66d1ba
RS
7276 else if (! (!NILP (Vtransient_mark_mode)
7277 && !NILP (current_buffer->mark_active))
293a54ce
RS
7278 && (w == XWINDOW (current_buffer->last_selected_window)
7279 || highlight_nonselected_windows)
8b51f1e3 7280 && NILP (w->region_showing)
8f897821 7281 && NILP (Vshow_trailing_whitespace)
8b51f1e3 7282 && !cursor_in_echo_area)
a2889657 7283 {
5f5c8ee5
GM
7284 struct it it;
7285 struct glyph_row *row;
7286
7287 /* Skip from tlbufpos to PT and see where it is. Note that
7288 PT may be in invisible text. If so, we will end at the
7289 next visible position. */
7290 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7291 NULL, DEFAULT_FACE_ID);
7292 it.current_x = this_line_start_x;
7293 it.current_y = this_line_y;
7294 it.vpos = this_line_vpos;
7295
7296 /* The call to move_it_to stops in front of PT, but
7297 moves over before-strings. */
7298 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7299
7300 if (it.vpos == this_line_vpos
7301 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7302 row->enabled_p))
a2889657 7303 {
5f5c8ee5
GM
7304 xassert (this_line_vpos == it.vpos);
7305 xassert (this_line_y == it.current_y);
7306 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
7307 goto update;
7308 }
7309 else
7310 goto cancel;
7311 }
5f5c8ee5 7312
a2889657 7313 cancel:
5f5c8ee5
GM
7314 /* Text changed drastically or point moved off of line. */
7315 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
7316 }
7317
5f5c8ee5
GM
7318 CHARPOS (this_line_start_pos) = 0;
7319 consider_all_windows_p |= buffer_shared > 1;
7320 ++clear_face_cache_count;
a2889657 7321
5f5c8ee5
GM
7322
7323 /* Build desired matrices. If consider_all_windows_p is non-zero,
7324 do it for all windows on all frames. Otherwise do it for
7325 selected_window, only. */
463f6b91 7326
5f5c8ee5 7327 if (consider_all_windows_p)
a2889657 7328 {
35f56f96 7329 Lisp_Object tail, frame;
a2889657 7330
5f5c8ee5
GM
7331 /* Clear the face cache eventually. */
7332 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 7333 {
5f5c8ee5 7334 clear_face_cache (0);
463f6b91
RS
7335 clear_face_cache_count = 0;
7336 }
31b24551 7337
5f5c8ee5
GM
7338 /* Recompute # windows showing selected buffer. This will be
7339 incremented each time such a window is displayed. */
a2889657
JB
7340 buffer_shared = 0;
7341
35f56f96 7342 FOR_EACH_FRAME (tail, frame)
30c566e4 7343 {
5f5c8ee5 7344 struct frame *f = XFRAME (frame);
fd8ff63d 7345 if (FRAME_WINDOW_P (f) || f == selected_frame)
9769686d 7346 {
5f5c8ee5
GM
7347 /* Mark all the scroll bars to be removed; we'll redeem
7348 the ones we want when we redisplay their windows. */
9769686d
RS
7349 if (condemn_scroll_bars_hook)
7350 (*condemn_scroll_bars_hook) (f);
30c566e4 7351
f21ef775 7352 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 7353 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 7354
5f5c8ee5
GM
7355 /* Any scroll bars which redisplay_windows should have
7356 nuked should now go away. */
9769686d
RS
7357 if (judge_scroll_bars_hook)
7358 (*judge_scroll_bars_hook) (f);
7359 }
30c566e4 7360 }
a2889657 7361 }
5f5c8ee5
GM
7362 else if (FRAME_VISIBLE_P (selected_frame)
7363 && !FRAME_OBSCURED_P (selected_frame))
7364 redisplay_window (selected_window, 1);
7365
7366
7367 /* Compare desired and current matrices, perform output. */
7368
7369update:
7370
7371 /* If fonts changed, display again. */
7372 if (fonts_changed_p)
7373 goto retry;
a2889657 7374
a2889657
JB
7375 /* Prevent various kinds of signals during display update.
7376 stdio is not robust about handling signals,
7377 which can cause an apparent I/O error. */
7378 if (interrupt_input)
7379 unrequest_sigio ();
7380 stop_polling ();
7381
5f5c8ee5 7382 if (consider_all_windows_p)
a2889657
JB
7383 {
7384 Lisp_Object tail;
7385
7386 pause = 0;
7387
44fa5b1e 7388 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 7389 {
5f5c8ee5 7390 struct frame *f;
a2889657 7391
e24c997d 7392 if (!FRAMEP (XCONS (tail)->car))
a2889657
JB
7393 continue;
7394
44fa5b1e 7395 f = XFRAME (XCONS (tail)->car);
1af9f229 7396
fd8ff63d 7397 if ((FRAME_WINDOW_P (f) || f == selected_frame)
f21ef775 7398 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 7399 {
5f5c8ee5
GM
7400 /* Mark all windows as to be updated. */
7401 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 7402 pause |= update_frame (f, 0, 0);
a2889657 7403 if (!pause)
efc63ef0 7404 {
5f5c8ee5
GM
7405 if (hscroll_windows (f->root_window))
7406 goto retry;
7407
efc63ef0
RS
7408 mark_window_display_accurate (f->root_window, 1);
7409 if (frame_up_to_date_hook != 0)
7410 (*frame_up_to_date_hook) (f);
7411 }
a2889657
JB
7412 }
7413 }
7414 }
7415 else
6e8290aa 7416 {
5f5c8ee5
GM
7417 if (FRAME_VISIBLE_P (selected_frame)
7418 && !FRAME_OBSCURED_P (selected_frame))
7419 {
7420 XWINDOW (selected_window)->must_be_updated_p = 1;
7421 pause = update_frame (selected_frame, 0, 0);
7422 if (!pause && hscroll_windows (selected_window))
7423 goto retry;
7424 }
4d641a15
KH
7425 else
7426 pause = 0;
d724d989 7427
8de2d90b 7428 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
7429 function. If the echo area is on another frame, that may
7430 have put text on a frame other than the selected one, so the
7431 above call to update_frame would not have caught it. Catch
8de2d90b
JB
7432 it here. */
7433 {
84faf44c 7434 Lisp_Object mini_window;
5f5c8ee5 7435 struct frame *mini_frame;
84faf44c
RS
7436
7437 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
7438 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 7439
fd8ff63d 7440 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
7441 {
7442 XWINDOW (mini_window)->must_be_updated_p = 1;
7443 pause |= update_frame (mini_frame, 0, 0);
7444 if (!pause && hscroll_windows (mini_window))
7445 goto retry;
7446 }
8de2d90b 7447 }
6e8290aa 7448 }
a2889657 7449
5f5c8ee5
GM
7450 /* If display was paused because of pending input, make sure we do a
7451 thorough update the next time. */
a2889657
JB
7452 if (pause)
7453 {
5f5c8ee5
GM
7454 /* Prevent the optimization at the beginning of
7455 redisplay_internal that tries a single-line update of the
7456 line containing the cursor in the selected window. */
7457 CHARPOS (this_line_start_pos) = 0;
7458
7459 /* Let the overlay arrow be updated the next time. */
265a9e55 7460 if (!NILP (last_arrow_position))
a2889657
JB
7461 {
7462 last_arrow_position = Qt;
7463 last_arrow_string = Qt;
7464 }
5f5c8ee5
GM
7465
7466 /* If we pause after scrolling, some rows in the current
7467 matrices of some windows are not valid. */
7468 if (!WINDOW_FULL_WIDTH_P (w)
7469 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
7470 update_mode_lines = 1;
7471 }
7472
5f5c8ee5
GM
7473 /* Now text on frame agrees with windows, so put info into the
7474 windows for partial redisplay to follow. */
a2889657
JB
7475 if (!pause)
7476 {
7477 register struct buffer *b = XBUFFER (w->buffer);
7478
9142dd5b
GM
7479 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7480 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7481 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7482 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 7483
5f5c8ee5 7484 if (consider_all_windows_p)
11e82b76 7485 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
a2889657
JB
7486 else
7487 {
5f5c8ee5
GM
7488 XSETFASTINT (w->last_point, BUF_PT (b));
7489 w->last_cursor = w->cursor;
7490 w->last_cursor_off_p = w->cursor_off_p;
7491
28995e67 7492 b->clip_changed = 0;
9142dd5b 7493 b->prevent_redisplay_optimizations_p = 0;
a2889657 7494 w->update_mode_line = Qnil;
c2213350 7495 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 7496 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
7497 w->last_had_star
7498 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7499 ? Qt : Qnil);
3ee4159a
RS
7500
7501 /* Record if we are showing a region, so can make sure to
7502 update it fully at next redisplay. */
7503 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
7504 && (w == XWINDOW (current_buffer->last_selected_window)
7505 || highlight_nonselected_windows)
3ee4159a
RS
7506 && !NILP (XBUFFER (w->buffer)->mark_active)
7507 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7508 : Qnil);
7509
d2f84654 7510 w->window_end_valid = w->buffer;
d45de95b 7511 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 7512 last_arrow_string = Voverlay_arrow_string;
efc63ef0
RS
7513 if (frame_up_to_date_hook != 0)
7514 (*frame_up_to_date_hook) (selected_frame);
9142dd5b
GM
7515
7516 w->current_matrix->buffer = b;
7517 w->current_matrix->begv = BUF_BEGV (b);
7518 w->current_matrix->zv = BUF_ZV (b);
a2889657 7519 }
5f5c8ee5 7520
a2889657
JB
7521 update_mode_lines = 0;
7522 windows_or_buffers_changed = 0;
7523 }
7524
5f5c8ee5
GM
7525 /* Start SIGIO interrupts coming again. Having them off during the
7526 code above makes it less likely one will discard output, but not
7527 impossible, since there might be stuff in the system buffer here.
a2889657 7528 But it is much hairier to try to do anything about that. */
a2889657
JB
7529 if (interrupt_input)
7530 request_sigio ();
7531 start_polling ();
7532
5f5c8ee5
GM
7533 /* If a frame has become visible which was not before, redisplay
7534 again, so that we display it. Expose events for such a frame
7535 (which it gets when becoming visible) don't call the parts of
7536 redisplay constructing glyphs, so simply exposing a frame won't
7537 display anything in this case. So, we have to display these
7538 frames here explicitly. */
11c52c4f
RS
7539 if (!pause)
7540 {
7541 Lisp_Object tail, frame;
7542 int new_count = 0;
7543
7544 FOR_EACH_FRAME (tail, frame)
7545 {
7546 int this_is_visible = 0;
8e83f802
RS
7547
7548 if (XFRAME (frame)->visible)
7549 this_is_visible = 1;
7550 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7551 if (XFRAME (frame)->visible)
7552 this_is_visible = 1;
11c52c4f
RS
7553
7554 if (this_is_visible)
7555 new_count++;
7556 }
7557
89819bdd 7558 if (new_count != number_of_visible_frames)
11c52c4f
RS
7559 windows_or_buffers_changed++;
7560 }
7561
44fa5b1e 7562 /* Change frame size now if a change is pending. */
c6e89d6c 7563 do_pending_window_change (1);
d8e242fd 7564
8b32d885
RS
7565 /* If we just did a pending size change, or have additional
7566 visible frames, redisplay again. */
3c8c72e0 7567 if (windows_or_buffers_changed && !pause)
8b32d885 7568 goto retry;
5f5c8ee5
GM
7569
7570 end_of_redisplay:;
c6e89d6c 7571
28514cd9 7572 unbind_to (count, Qnil);
a2889657
JB
7573}
7574
5f5c8ee5
GM
7575
7576/* Redisplay, but leave alone any recent echo area message unless
7577 another message has been requested in its place.
a2889657
JB
7578
7579 This is useful in situations where you need to redisplay but no
7580 user action has occurred, making it inappropriate for the message
7581 area to be cleared. See tracking_off and
7582 wait_reading_process_input for examples of these situations. */
7583
8991bb31 7584void
a2889657
JB
7585redisplay_preserve_echo_area ()
7586{
c6e89d6c 7587 if (!NILP (echo_area_buffer[1]))
a2889657 7588 {
c6e89d6c
GM
7589 /* We have a previously displayed message, but no current
7590 message. Redisplay the previous message. */
7591 display_last_displayed_message_p = 1;
e9874cee 7592 redisplay_internal (1);
c6e89d6c 7593 display_last_displayed_message_p = 0;
a2889657
JB
7594 }
7595 else
e9874cee 7596 redisplay_internal (1);
a2889657
JB
7597}
7598
5f5c8ee5 7599
28514cd9
GM
7600/* Function registered with record_unwind_protect in
7601 redisplay_internal. Clears the flag indicating that a redisplay is
7602 in progress. */
7603
7604static Lisp_Object
7605unwind_redisplay (old_redisplaying_p)
7606 Lisp_Object old_redisplaying_p;
7607{
7608 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 7609 return Qnil;
28514cd9
GM
7610}
7611
7612
5f5c8ee5
GM
7613/* Mark the display of windows in the window tree rooted at WINDOW as
7614 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7615 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7616 the next time redisplay_internal is called. */
7617
a2889657 7618void
5f5c8ee5 7619mark_window_display_accurate (window, accurate_p)
a2889657 7620 Lisp_Object window;
5f5c8ee5 7621 int accurate_p;
a2889657 7622{
5f5c8ee5
GM
7623 struct window *w;
7624
7625 for (; !NILP (window); window = w->next)
a2889657
JB
7626 {
7627 w = XWINDOW (window);
7628
5f5c8ee5 7629 if (BUFFERP (w->buffer))
bd66d1ba 7630 {
5f5c8ee5
GM
7631 struct buffer *b = XBUFFER (w->buffer);
7632
c2213350 7633 XSETFASTINT (w->last_modified,
5f5c8ee5 7634 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 7635 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
7636 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7637 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7638 ? Qt : Qnil);
bd66d1ba 7639
5f5c8ee5
GM
7640#if 0 /* I don't think this is necessary because display_line does it.
7641 Let's check it. */
bd66d1ba
RS
7642 /* Record if we are showing a region, so can make sure to
7643 update it fully at next redisplay. */
5f5c8ee5
GM
7644 w->region_showing
7645 = (!NILP (Vtransient_mark_mode)
7646 && (w == XWINDOW (current_buffer->last_selected_window)
7647 || highlight_nonselected_windows)
7648 && (!NILP (b->mark_active)
7649 ? Fmarker_position (b->mark)
7650 : Qnil));
7651#endif
7652
7653 if (accurate_p)
7654 {
7655 b->clip_changed = 0;
9142dd5b
GM
7656 b->prevent_redisplay_optimizations_p = 0;
7657 w->current_matrix->buffer = b;
7658 w->current_matrix->begv = BUF_BEGV (b);
7659 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
7660 w->last_cursor = w->cursor;
7661 w->last_cursor_off_p = w->cursor_off_p;
7662 if (w == XWINDOW (selected_window))
7663 w->last_point = BUF_PT (b);
7664 else
7665 w->last_point = XMARKER (w->pointm)->charpos;
7666 }
bd66d1ba
RS
7667 }
7668
d2f84654 7669 w->window_end_valid = w->buffer;
a2889657
JB
7670 w->update_mode_line = Qnil;
7671
265a9e55 7672 if (!NILP (w->vchild))
5f5c8ee5 7673 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 7674 if (!NILP (w->hchild))
5f5c8ee5 7675 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
7676 }
7677
5f5c8ee5 7678 if (accurate_p)
a2889657 7679 {
d45de95b 7680 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
7681 last_arrow_string = Voverlay_arrow_string;
7682 }
7683 else
7684 {
5f5c8ee5
GM
7685 /* Force a thorough redisplay the next time by setting
7686 last_arrow_position and last_arrow_string to t, which is
7687 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
7688 last_arrow_position = Qt;
7689 last_arrow_string = Qt;
7690 }
7691}
5f5c8ee5
GM
7692
7693
7694/* Return value in display table DP (Lisp_Char_Table *) for character
7695 C. Since a display table doesn't have any parent, we don't have to
7696 follow parent. Do not call this function directly but use the
7697 macro DISP_CHAR_VECTOR. */
7698
7699Lisp_Object
7700disp_char_vector (dp, c)
7701 struct Lisp_Char_Table *dp;
7702 int c;
7703{
7704 int code[4], i;
7705 Lisp_Object val;
7706
7707 if (SINGLE_BYTE_CHAR_P (c))
7708 return (dp->contents[c]);
7709
7710 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7711 if (code[0] != CHARSET_COMPOSITION)
7712 {
7713 if (code[1] < 32)
7714 code[1] = -1;
7715 else if (code[2] < 32)
7716 code[2] = -1;
7717 }
7718
7719 /* Here, the possible range of code[0] (== charset ID) is
7720 128..max_charset. Since the top level char table contains data
7721 for multibyte characters after 256th element, we must increment
7722 code[0] by 128 to get a correct index. */
7723 code[0] += 128;
7724 code[3] = -1; /* anchor */
7725
7726 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7727 {
7728 val = dp->contents[code[i]];
7729 if (!SUB_CHAR_TABLE_P (val))
7730 return (NILP (val) ? dp->defalt : val);
7731 }
7732
7733 /* Here, val is a sub char table. We return the default value of
7734 it. */
7735 return (dp->defalt);
7736}
7737
7738
a2889657 7739\f
5f5c8ee5
GM
7740/***********************************************************************
7741 Window Redisplay
7742 ***********************************************************************/
a2725ab2 7743
5f5c8ee5 7744/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
7745
7746static void
5f5c8ee5
GM
7747redisplay_windows (window)
7748 Lisp_Object window;
90adcf20 7749{
5f5c8ee5
GM
7750 while (!NILP (window))
7751 {
7752 struct window *w = XWINDOW (window);
7753
7754 if (!NILP (w->hchild))
7755 redisplay_windows (w->hchild);
7756 else if (!NILP (w->vchild))
7757 redisplay_windows (w->vchild);
7758 else
7759 redisplay_window (window, 0);
a2725ab2 7760
5f5c8ee5
GM
7761 window = w->next;
7762 }
7763}
7764
7765
7766/* Set cursor position of W. PT is assumed to be displayed in ROW.
7767 DELTA is the number of bytes by which positions recorded in ROW
7768 differ from current buffer positions. */
7769
7770void
7771set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7772 struct window *w;
7773 struct glyph_row *row;
7774 struct glyph_matrix *matrix;
7775 int delta, delta_bytes, dy, dvpos;
7776{
7777 struct glyph *glyph = row->glyphs[TEXT_AREA];
7778 struct glyph *end = glyph + row->used[TEXT_AREA];
7779 int x = row->x;
7780 int pt_old = PT - delta;
7781
7782 /* Skip over glyphs not having an object at the start of the row.
7783 These are special glyphs like truncation marks on terminal
7784 frames. */
7785 if (row->displays_text_p)
7786 while (glyph < end
7787 && !glyph->object
7788 && glyph->charpos < 0)
7789 {
7790 x += glyph->pixel_width;
7791 ++glyph;
7792 }
7793
7794 while (glyph < end
7795 && glyph->object
7796 && (!BUFFERP (glyph->object)
7797 || glyph->charpos < pt_old))
7798 {
7799 x += glyph->pixel_width;
7800 ++glyph;
7801 }
7802
7803 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7804 w->cursor.x = x;
7805 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7806 w->cursor.y = row->y + dy;
7807
7808 if (w == XWINDOW (selected_window))
7809 {
7810 if (!row->continued_p
7811 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7812 && row->x == 0)
7813 {
7814 this_line_buffer = XBUFFER (w->buffer);
7815
7816 CHARPOS (this_line_start_pos)
7817 = MATRIX_ROW_START_CHARPOS (row) + delta;
7818 BYTEPOS (this_line_start_pos)
7819 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7820
7821 CHARPOS (this_line_end_pos)
7822 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7823 BYTEPOS (this_line_end_pos)
7824 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7825
7826 this_line_y = w->cursor.y;
7827 this_line_pixel_height = row->height;
7828 this_line_vpos = w->cursor.vpos;
7829 this_line_start_x = row->x;
7830 }
7831 else
7832 CHARPOS (this_line_start_pos) = 0;
7833 }
7834}
7835
7836
7837/* Run window scroll functions, if any, for WINDOW with new window
7838 start STARTP. Sets the window start of WINDOW to that position. */
7839
7840static INLINE struct text_pos
7841run_window_scroll_functions (window, startp)
7842 Lisp_Object window;
7843 struct text_pos startp;
7844{
7845 struct window *w = XWINDOW (window);
7846 SET_MARKER_FROM_TEXT_POS (w->start, startp);
90adcf20 7847
5f5c8ee5
GM
7848 if (!NILP (Vwindow_scroll_functions))
7849 {
7850 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7851 make_number (CHARPOS (startp)));
7852 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7853 }
90adcf20 7854
5f5c8ee5
GM
7855 return startp;
7856}
7857
7858
7859/* Modify the desired matrix of window W and W->vscroll so that the
7860 line containing the cursor is fully visible. */
7861
7862static void
7863make_cursor_line_fully_visible (w)
7864 struct window *w;
7865{
7866 struct glyph_matrix *matrix;
7867 struct glyph_row *row;
7868 int top_line_height;
7869
7870 /* It's not always possible to find the cursor, e.g, when a window
7871 is full of overlay strings. Don't do anything in that case. */
7872 if (w->cursor.vpos < 0)
7873 return;
7874
7875 matrix = w->desired_matrix;
7876 row = MATRIX_ROW (matrix, w->cursor.vpos);
7877
7878 /* If row->y == top y of window display area, the window isn't tall
7879 enough to display a single line. There is nothing we can do
7880 about it. */
7881 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
7882 if (row->y == top_line_height)
7883 return;
7884
7885 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7886 {
7887 int dy = row->height - row->visible_height;
7888 w->vscroll = 0;
7889 w->cursor.y += dy;
7890 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7891 }
7892 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7893 {
7894 int dy = - (row->height - row->visible_height);
7895 w->vscroll = dy;
7896 w->cursor.y += dy;
7897 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7898 }
7899
7900 /* When we change the cursor y-position of the selected window,
7901 change this_line_y as well so that the display optimization for
7902 the cursor line of the selected window in redisplay_internal uses
7903 the correct y-position. */
7904 if (w == XWINDOW (selected_window))
7905 this_line_y = w->cursor.y;
7906}
7907
7908
7909/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7910 non-zero means only WINDOW is redisplayed in redisplay_internal.
7911 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7912 in redisplay_window to bring a partially visible line into view in
7913 the case that only the cursor has moved.
7914
7915 Value is
7916
7917 1 if scrolling succeeded
7918
7919 0 if scrolling didn't find point.
7920
7921 -1 if new fonts have been loaded so that we must interrupt
7922 redisplay, adjust glyph matrices, and try again. */
7923
7924static int
7925try_scrolling (window, just_this_one_p, scroll_conservatively,
7926 scroll_step, temp_scroll_step)
7927 Lisp_Object window;
7928 int just_this_one_p;
7929 int scroll_conservatively, scroll_step;
7930 int temp_scroll_step;
7931{
7932 struct window *w = XWINDOW (window);
7933 struct frame *f = XFRAME (w->frame);
7934 struct text_pos scroll_margin_pos;
7935 struct text_pos pos;
7936 struct text_pos startp;
7937 struct it it;
7938 Lisp_Object window_end;
7939 int this_scroll_margin;
7940 int dy = 0;
7941 int scroll_max;
7942 int line_height, rc;
7943 int amount_to_scroll = 0;
7944 Lisp_Object aggressive;
7945 int height;
7946
7947#if GLYPH_DEBUG
7948 debug_method_add (w, "try_scrolling");
78614721 7949#endif
5f5c8ee5
GM
7950
7951 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7952
7953 /* Compute scroll margin height in pixels. We scroll when point is
7954 within this distance from the top or bottom of the window. */
7955 if (scroll_margin > 0)
90adcf20 7956 {
5f5c8ee5
GM
7957 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
7958 this_scroll_margin *= CANON_Y_UNIT (f);
7959 }
7960 else
7961 this_scroll_margin = 0;
7962
7963 /* Compute how much we should try to scroll maximally to bring point
7964 into view. */
7965 if (scroll_step)
7966 scroll_max = scroll_step;
7967 else if (scroll_conservatively)
7968 scroll_max = scroll_conservatively;
7969 else if (temp_scroll_step)
7970 scroll_max = temp_scroll_step;
7971 else if (NUMBERP (current_buffer->scroll_down_aggressively)
7972 || NUMBERP (current_buffer->scroll_up_aggressively))
7973 /* We're trying to scroll because of aggressive scrolling
7974 but no scroll_step is set. Choose an arbitrary one. Maybe
7975 there should be a variable for this. */
7976 scroll_max = 10;
7977 else
7978 scroll_max = 0;
7979 scroll_max *= CANON_Y_UNIT (f);
7980
7981 /* Decide whether we have to scroll down. Start at the window end
7982 and move this_scroll_margin up to find the position of the scroll
7983 margin. */
7984 window_end = Fwindow_end (window, Qt);
7985 CHARPOS (scroll_margin_pos) = XINT (window_end);
7986 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
7987 if (this_scroll_margin)
7988 {
7989 start_display (&it, w, scroll_margin_pos);
7990 move_it_vertically (&it, - this_scroll_margin);
7991 scroll_margin_pos = it.current.pos;
7992 }
7993
7994 if (PT >= CHARPOS (scroll_margin_pos))
7995 {
7996 int y0;
7997
7998 /* Point is in the scroll margin at the bottom of the window, or
7999 below. Compute a new window start that makes point visible. */
8000
8001 /* Compute the distance from the scroll margin to PT.
8002 Give up if the distance is greater than scroll_max. */
8003 start_display (&it, w, scroll_margin_pos);
8004 y0 = it.current_y;
8005 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8006 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8007 line_height = (it.max_ascent + it.max_descent
8008 ? it.max_ascent + it.max_descent
8009 : last_height);
8010 dy = it.current_y + line_height - y0;
8011 if (dy > scroll_max)
8012 return 0;
8013
8014 /* Move the window start down. If scrolling conservatively,
8015 move it just enough down to make point visible. If
8016 scroll_step is set, move it down by scroll_step. */
8017 start_display (&it, w, startp);
8018
8019 if (scroll_conservatively)
8020 amount_to_scroll = dy;
8021 else if (scroll_step || temp_scroll_step)
8022 amount_to_scroll = scroll_max;
8023 else
90adcf20 8024 {
5f5c8ee5
GM
8025 aggressive = current_buffer->scroll_down_aggressively;
8026 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8027 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
8028 if (NUMBERP (aggressive))
8029 amount_to_scroll = XFLOATINT (aggressive) * height;
8030 }
a2725ab2 8031
5f5c8ee5
GM
8032 if (amount_to_scroll <= 0)
8033 return 0;
a2725ab2 8034
5f5c8ee5
GM
8035 move_it_vertically (&it, amount_to_scroll);
8036 startp = it.current.pos;
8037 }
8038 else
8039 {
8040 /* See if point is inside the scroll margin at the top of the
8041 window. */
8042 scroll_margin_pos = startp;
8043 if (this_scroll_margin)
8044 {
8045 start_display (&it, w, startp);
8046 move_it_vertically (&it, this_scroll_margin);
8047 scroll_margin_pos = it.current.pos;
8048 }
8049
8050 if (PT < CHARPOS (scroll_margin_pos))
8051 {
8052 /* Point is in the scroll margin at the top of the window or
8053 above what is displayed in the window. */
8054 int y0;
8055
8056 /* Compute the vertical distance from PT to the scroll
8057 margin position. Give up if distance is greater than
8058 scroll_max. */
8059 SET_TEXT_POS (pos, PT, PT_BYTE);
8060 start_display (&it, w, pos);
8061 y0 = it.current_y;
8062 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8063 it.last_visible_y, -1,
8064 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8065 dy = it.current_y - y0;
8066 if (dy > scroll_max)
8067 return 0;
8068
8069 /* Compute new window start. */
8070 start_display (&it, w, startp);
8071
8072 if (scroll_conservatively)
8073 amount_to_scroll = dy;
8074 else if (scroll_step || temp_scroll_step)
8075 amount_to_scroll = scroll_max;
538f13d4 8076 else
5f5c8ee5
GM
8077 {
8078 aggressive = current_buffer->scroll_up_aggressively;
8079 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8080 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
8081 if (NUMBERP (aggressive))
8082 amount_to_scroll = XFLOATINT (aggressive) * height;
8083 }
a2725ab2 8084
5f5c8ee5
GM
8085 if (amount_to_scroll <= 0)
8086 return 0;
8087
8088 move_it_vertically (&it, - amount_to_scroll);
8089 startp = it.current.pos;
90adcf20
RS
8090 }
8091 }
a2889657 8092
5f5c8ee5
GM
8093 /* Run window scroll functions. */
8094 startp = run_window_scroll_functions (window, startp);
90adcf20 8095
5f5c8ee5
GM
8096 /* Display the window. Give up if new fonts are loaded, or if point
8097 doesn't appear. */
8098 if (!try_window (window, startp))
8099 rc = -1;
8100 else if (w->cursor.vpos < 0)
8101 {
8102 clear_glyph_matrix (w->desired_matrix);
8103 rc = 0;
8104 }
8105 else
8106 {
8107 /* Maybe forget recorded base line for line number display. */
8108 if (!just_this_one_p
8109 || current_buffer->clip_changed
9142dd5b 8110 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
8111 w->base_line_number = Qnil;
8112
8113 /* If cursor ends up on a partially visible line, shift display
8114 lines up or down. */
8115 make_cursor_line_fully_visible (w);
8116 rc = 1;
8117 }
8118
8119 return rc;
a2889657
JB
8120}
8121
5f5c8ee5
GM
8122
8123/* Compute a suitable window start for window W if display of W starts
8124 on a continuation line. Value is non-zero if a new window start
8125 was computed.
8126
8127 The new window start will be computed, based on W's width, starting
8128 from the start of the continued line. It is the start of the
8129 screen line with the minimum distance from the old start W->start. */
8130
8131static int
8132compute_window_start_on_continuation_line (w)
8133 struct window *w;
1f1ff51d 8134{
5f5c8ee5
GM
8135 struct text_pos pos, start_pos;
8136 int window_start_changed_p = 0;
1f1ff51d 8137
5f5c8ee5 8138 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 8139
5f5c8ee5
GM
8140 /* If window start is on a continuation line... Window start may be
8141 < BEGV in case there's invisible text at the start of the
8142 buffer (M-x rmail, for example). */
8143 if (CHARPOS (start_pos) > BEGV
8144 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 8145 {
5f5c8ee5
GM
8146 struct it it;
8147 struct glyph_row *row;
8148
8149 /* Find the start of the continued line. This should be fast
8150 because scan_buffer is fast (newline cache). */
8151 row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
8152 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8153 row, DEFAULT_FACE_ID);
8154 reseat_at_previous_visible_line_start (&it);
8155
8156 /* If the line start is "too far" away from the window start,
8157 say it takes too much time to compute a new window start. */
8158 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8159 < XFASTINT (w->height) * XFASTINT (w->width))
8160 {
8161 int min_distance, distance;
8162
8163 /* Move forward by display lines to find the new window
8164 start. If window width was enlarged, the new start can
8165 be expected to be > the old start. If window width was
8166 decreased, the new window start will be < the old start.
8167 So, we're looking for the display line start with the
8168 minimum distance from the old window start. */
8169 pos = it.current.pos;
8170 min_distance = INFINITY;
8171 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8172 distance < min_distance)
8173 {
8174 min_distance = distance;
8175 pos = it.current.pos;
8176 move_it_by_lines (&it, 1, 0);
8177 }
8178
8179 /* Set the window start there. */
8180 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8181 window_start_changed_p = 1;
8182 }
1f1ff51d 8183 }
5f5c8ee5
GM
8184
8185 return window_start_changed_p;
1f1ff51d
KH
8186}
8187
5f5c8ee5
GM
8188
8189/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8190 selected_window is redisplayed. */
90adcf20 8191
a2889657 8192static void
5f5c8ee5 8193redisplay_window (window, just_this_one_p)
a2889657 8194 Lisp_Object window;
5f5c8ee5 8195 int just_this_one_p;
a2889657 8196{
5f5c8ee5
GM
8197 struct window *w = XWINDOW (window);
8198 struct frame *f = XFRAME (w->frame);
8199 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 8200 struct buffer *old = current_buffer;
5f5c8ee5 8201 struct text_pos lpoint, opoint, startp;
e481f960 8202 int update_mode_line;
5f5c8ee5
GM
8203 int tem;
8204 struct it it;
8205 /* Record it now because it's overwritten. */
8206 int current_matrix_up_to_date_p = 0;
5ba50c51 8207 int really_switched_buffer = 0;
5f5c8ee5 8208 int temp_scroll_step = 0;
2e54982e 8209 int count = specpdl_ptr - specpdl;
a2889657 8210
5f5c8ee5
GM
8211 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8212 opoint = lpoint;
a2889657 8213
5f5c8ee5
GM
8214 /* W must be a leaf window here. */
8215 xassert (!NILP (w->buffer));
8216#if GLYPH_DEBUG
8217 *w->desired_matrix->method = 0;
8218#endif
2e54982e
RS
8219
8220 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
8221
8222 reconsider_clip_changes (w, buffer);
8223
5f5c8ee5
GM
8224 /* Has the mode line to be updated? */
8225 update_mode_line = (!NILP (w->update_mode_line)
8226 || update_mode_lines
8227 || buffer->clip_changed);
8de2d90b
JB
8228
8229 if (MINI_WINDOW_P (w))
8230 {
5f5c8ee5 8231 if (w == XWINDOW (echo_area_window)
c6e89d6c 8232 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
8233 {
8234 if (update_mode_line)
8235 /* We may have to update a tty frame's menu bar or a
8236 toolbar. Example `M-x C-h C-h C-g'. */
8237 goto finish_menu_bars;
8238 else
8239 /* We've already displayed the echo area glyphs in this window. */
8240 goto finish_scroll_bars;
8241 }
73af359d 8242 else if (w != XWINDOW (minibuf_window))
8de2d90b 8243 {
5f5c8ee5
GM
8244 /* W is a mini-buffer window, but it's not the currently
8245 active one, so clear it. */
8246 int yb = window_text_bottom_y (w);
8247 struct glyph_row *row;
8248 int y;
8249
8250 for (y = 0, row = w->desired_matrix->rows;
8251 y < yb;
8252 y += row->height, ++row)
8253 blank_row (w, row, y);
88f22aff 8254 goto finish_scroll_bars;
8de2d90b
JB
8255 }
8256 }
a2889657 8257
5f5c8ee5
GM
8258 /* Otherwise set up data on this window; select its buffer and point
8259 value. */
e481f960 8260 if (update_mode_line)
5ba50c51 8261 {
5f5c8ee5
GM
8262 /* Really select the buffer, for the sake of buffer-local
8263 variables. */
5ba50c51
RS
8264 set_buffer_internal_1 (XBUFFER (w->buffer));
8265 really_switched_buffer = 1;
8266 }
e481f960
RS
8267 else
8268 set_buffer_temp (XBUFFER (w->buffer));
5f5c8ee5
GM
8269 SET_TEXT_POS (opoint, PT, PT_BYTE);
8270
8271 current_matrix_up_to_date_p
8272 = (!NILP (w->window_end_valid)
8273 && !current_buffer->clip_changed
8274 && XFASTINT (w->last_modified) >= MODIFF
8275 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 8276
5f5c8ee5
GM
8277 /* When windows_or_buffers_changed is non-zero, we can't rely on
8278 the window end being valid, so set it to nil there. */
8279 if (windows_or_buffers_changed)
8280 {
8281 /* If window starts on a continuation line, maybe adjust the
8282 window start in case the window's width changed. */
8283 if (XMARKER (w->start)->buffer == current_buffer)
8284 compute_window_start_on_continuation_line (w);
8285
8286 w->window_end_valid = Qnil;
8287 }
12adba34 8288
5f5c8ee5
GM
8289 /* Some sanity checks. */
8290 CHECK_WINDOW_END (w);
8291 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 8292 abort ();
5f5c8ee5 8293 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 8294 abort ();
a2889657 8295
28995e67
RS
8296 /* If %c is in mode line, update it if needed. */
8297 if (!NILP (w->column_number_displayed)
8298 /* This alternative quickly identifies a common case
8299 where no change is needed. */
8300 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8301 && XFASTINT (w->last_modified) >= MODIFF
8302 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8303 && XFASTINT (w->column_number_displayed) != current_column ())
8304 update_mode_line = 1;
8305
5f5c8ee5
GM
8306 /* Count number of windows showing the selected buffer. An indirect
8307 buffer counts as its base buffer. */
8308 if (!just_this_one_p)
42640f83
RS
8309 {
8310 struct buffer *current_base, *window_base;
8311 current_base = current_buffer;
8312 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8313 if (current_base->base_buffer)
8314 current_base = current_base->base_buffer;
8315 if (window_base->base_buffer)
8316 window_base = window_base->base_buffer;
8317 if (current_base == window_base)
8318 buffer_shared++;
8319 }
a2889657 8320
5f5c8ee5
GM
8321 /* Point refers normally to the selected window. For any other
8322 window, set up appropriate value. */
a2889657
JB
8323 if (!EQ (window, selected_window))
8324 {
12adba34
RS
8325 int new_pt = XMARKER (w->pointm)->charpos;
8326 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 8327 if (new_pt < BEGV)
a2889657 8328 {
f67a0f51 8329 new_pt = BEGV;
12adba34
RS
8330 new_pt_byte = BEGV_BYTE;
8331 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 8332 }
f67a0f51 8333 else if (new_pt > (ZV - 1))
a2889657 8334 {
f67a0f51 8335 new_pt = ZV;
12adba34
RS
8336 new_pt_byte = ZV_BYTE;
8337 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 8338 }
5f5c8ee5 8339
f67a0f51 8340 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 8341 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
8342 }
8343
f4faa47c 8344 /* If any of the character widths specified in the display table
5f5c8ee5
GM
8345 have changed, invalidate the width run cache. It's true that
8346 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
8347 redisplay goes (non-fatally) haywire when the display table is
8348 changed, so why should we worry about doing any better? */
8349 if (current_buffer->width_run_cache)
8350 {
f908610f 8351 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
8352
8353 if (! disptab_matches_widthtab (disptab,
8354 XVECTOR (current_buffer->width_table)))
8355 {
8356 invalidate_region_cache (current_buffer,
8357 current_buffer->width_run_cache,
8358 BEG, Z);
8359 recompute_width_table (current_buffer, disptab);
8360 }
8361 }
8362
a2889657 8363 /* If window-start is screwed up, choose a new one. */
a2889657
JB
8364 if (XMARKER (w->start)->buffer != current_buffer)
8365 goto recenter;
8366
5f5c8ee5 8367 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 8368
cf0df6ab
RS
8369 /* If someone specified a new starting point but did not insist,
8370 check whether it can be used. */
5f5c8ee5 8371 if (!NILP (w->optional_new_start))
cf0df6ab
RS
8372 {
8373 w->optional_new_start = Qnil;
5f5c8ee5
GM
8374 /* This takes a mini-buffer prompt into account. */
8375 start_display (&it, w, startp);
8376 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8377 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8378 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
8379 w->force_start = Qt;
8380 }
8381
8de2d90b 8382 /* Handle case where place to start displaying has been specified,
aa6d10fa 8383 unless the specified location is outside the accessible range. */
265a9e55 8384 if (!NILP (w->force_start))
a2889657 8385 {
e63574d7 8386 w->force_start = Qnil;
5f5c8ee5 8387 w->vscroll = 0;
b5174a51 8388 w->window_end_valid = Qnil;
5f5c8ee5
GM
8389
8390 /* Forget any recorded base line for line number display. */
8391 if (!current_matrix_up_to_date_p
8392 || current_buffer->clip_changed)
8393 w->base_line_number = Qnil;
8394
75c43375
RS
8395 /* Redisplay the mode line. Select the buffer properly for that.
8396 Also, run the hook window-scroll-functions
8397 because we have scrolled. */
e63574d7
RS
8398 /* Note, we do this after clearing force_start because
8399 if there's an error, it is better to forget about force_start
8400 than to get into an infinite loop calling the hook functions
8401 and having them get more errors. */
75c43375
RS
8402 if (!update_mode_line
8403 || ! NILP (Vwindow_scroll_functions))
e481f960 8404 {
5ba50c51
RS
8405 if (!really_switched_buffer)
8406 {
8407 set_buffer_temp (old);
8408 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8409 really_switched_buffer = 1;
5ba50c51 8410 }
5f5c8ee5 8411
e481f960
RS
8412 update_mode_line = 1;
8413 w->update_mode_line = Qt;
5f5c8ee5 8414 startp = run_window_scroll_functions (window, startp);
e481f960 8415 }
5f5c8ee5 8416
c2213350 8417 XSETFASTINT (w->last_modified, 0);
8850a573 8418 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
8419 if (CHARPOS (startp) < BEGV)
8420 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8421 else if (CHARPOS (startp) > ZV)
8422 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8423
8424 /* Redisplay, then check if cursor has been set during the
8425 redisplay. Give up if new fonts were loaded. */
8426 if (!try_window (window, startp))
8427 {
8428 w->force_start = Qt;
8429 clear_glyph_matrix (w->desired_matrix);
8430 goto restore_buffers;
8431 }
8432
8433 if (w->cursor.vpos < 0)
8434 {
8435 /* If point does not appear, or on a line that is not fully
8436 visible, move point so it does appear. The desired
8437 matrix has been built above, so we can use it. */
8438 int height = window_box_height (w) / 2;
8439 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8440
8441 while (row->y < height)
8442 ++row;
8443
8444 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8445 MATRIX_ROW_START_BYTEPOS (row));
8446
90adcf20 8447 if (w != XWINDOW (selected_window))
12adba34 8448 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
8449 else if (current_buffer == old)
8450 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8451
8452 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8453
8454 /* If we are highlighting the region, then we just changed
8455 the region, so redisplay to show it. */
df0b5ea1
RS
8456 if (!NILP (Vtransient_mark_mode)
8457 && !NILP (current_buffer->mark_active))
6f27fa9b 8458 {
5f5c8ee5
GM
8459 clear_glyph_matrix (w->desired_matrix);
8460 if (!try_window (window, startp))
8461 goto restore_buffers;
6f27fa9b 8462 }
a2889657 8463 }
5f5c8ee5
GM
8464
8465 make_cursor_line_fully_visible (w);
8466#if GLYPH_DEBUG
8467 debug_method_add (w, "forced window start");
8468#endif
a2889657
JB
8469 goto done;
8470 }
8471
5f5c8ee5
GM
8472 /* Handle case where text has not changed, only point, and it has
8473 not moved off the frame. */
8474 if (current_matrix_up_to_date_p
8475 /* Point may be in this window. */
8476 && PT >= CHARPOS (startp)
8477 /* If we don't check this, we are called to move the cursor in a
8478 horizontally split window with a current matrix that doesn't
8479 fit the display. */
8480 && !windows_or_buffers_changed
8481 /* Selective display hasn't changed. */
8482 && !current_buffer->clip_changed
b1aa6cb3
RS
8483 /* If force-mode-line-update was called, really redisplay;
8484 that's how redisplay is forced after e.g. changing
8485 buffer-invisibility-spec. */
632ab665 8486 && NILP (w->update_mode_line)
5f5c8ee5
GM
8487 /* Can't use this case if highlighting a region. When a
8488 region exists, cursor movement has to do more than just
8489 set the cursor. */
8490 && !(!NILP (Vtransient_mark_mode)
8491 && !NILP (current_buffer->mark_active))
bd66d1ba 8492 && NILP (w->region_showing)
8f897821 8493 && NILP (Vshow_trailing_whitespace)
5f5c8ee5
GM
8494 /* Right after splitting windows, last_point may be nil. */
8495 && INTEGERP (w->last_point)
8496 /* This code is not used for mini-buffer for the sake of the case
8497 of redisplaying to replace an echo area message; since in
8498 that case the mini-buffer contents per se are usually
8499 unchanged. This code is of no real use in the mini-buffer
8500 since the handling of this_line_start_pos, etc., in redisplay
8501 handles the same cases. */
d45de95b 8502 && !EQ (window, minibuf_window)
5f5c8ee5
GM
8503 /* When splitting windows or for new windows, it happens that
8504 redisplay is called with a nil window_end_vpos or one being
8505 larger than the window. This should really be fixed in
8506 window.c. I don't have this on my list, now, so we do
8507 approximately the same as the old redisplay code. --gerd. */
8508 && INTEGERP (w->window_end_vpos)
8509 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8510 && (FRAME_WINDOW_P (f)
8511 || !MARKERP (Voverlay_arrow_position)
377dbd97 8512 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 8513 {
5f5c8ee5
GM
8514 int this_scroll_margin;
8515 struct glyph_row *row;
8516 int scroll_p;
a2889657 8517
5f5c8ee5
GM
8518#if GLYPH_DEBUG
8519 debug_method_add (w, "cursor movement");
8520#endif
9afd2168 8521
5f5c8ee5
GM
8522 /* Scroll if point within this distance from the top or bottom
8523 of the window. This is a pixel value. */
8524 this_scroll_margin = max (0, scroll_margin);
8525 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8526 this_scroll_margin *= CANON_Y_UNIT (f);
8527
8528 /* Start with the row the cursor was displayed during the last
8529 not paused redisplay. Give up if that row is not valid. */
8530 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8531 goto try_to_scroll;
8532 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8533 if (row->mode_line_p)
8534 ++row;
8535 if (!row->enabled_p)
8536 goto try_to_scroll;
8537
8538 scroll_p = 0;
8539 if (PT > XFASTINT (w->last_point))
8540 {
8541 /* Point has moved forward. */
8542 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8543
8544 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8545 /* The end position of a row equals the start
8546 position of the next row. If PT is there, we
8547 would rather display it in the next line, except
8548 when this line ends in ZV. */
8549 || (MATRIX_ROW_END_CHARPOS (row) == PT
8550 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8551 || !row->ends_at_zv_p)))
8552 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8553 {
8554 xassert (row->enabled_p);
8555 ++row;
8556 }
9afd2168 8557
5f5c8ee5
GM
8558 /* If within the scroll margin, scroll. Note that
8559 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8560 next line would be drawn, and that this_scroll_margin can
8561 be zero. */
8562 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8563 || PT > MATRIX_ROW_END_CHARPOS (row)
8564 /* Line is completely visible last line in window and PT
8565 is to be set in the next line. */
8566 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8567 && PT == MATRIX_ROW_END_CHARPOS (row)
8568 && !row->ends_at_zv_p
8569 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8570 scroll_p = 1;
8571 }
8572 else if (PT < XFASTINT (w->last_point))
a2889657 8573 {
5f5c8ee5
GM
8574 /* Cursor has to be moved backward. Note that PT >=
8575 CHARPOS (startp) because of the outer if-statement. */
8576 while (!row->mode_line_p
8577 && (MATRIX_ROW_START_CHARPOS (row) > PT
8578 || (MATRIX_ROW_START_CHARPOS (row) == PT
8579 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8580 && (row->y > this_scroll_margin
8581 || CHARPOS (startp) == BEGV))
a2889657 8582 {
5f5c8ee5
GM
8583 xassert (row->enabled_p);
8584 --row;
a2889657 8585 }
abb4c08f 8586
5f5c8ee5
GM
8587 /* Consider the following case: Window starts at BEGV, there
8588 is invisible, intangible text at BEGV, so that display
8589 starts at some point START > BEGV. It can happen that
8590 we are called with PT somewhere between BEGV and START.
8591 Try to handle that case. */
8592 if (row < w->current_matrix->rows
8593 || row->mode_line_p)
8594 {
8595 row = w->current_matrix->rows;
8596 if (row->mode_line_p)
8597 ++row;
8598 }
8599
8600 /* Due to newlines in overlay strings, we may have to skip
8601 forward over overlay strings. */
8602 while (MATRIX_ROW_END_CHARPOS (row) == PT
8603 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8604 && !row->ends_at_zv_p)
8605 ++row;
8606
8607 /* If within the scroll margin, scroll. */
8608 if (row->y < this_scroll_margin
8609 && CHARPOS (startp) != BEGV)
8610 scroll_p = 1;
8611 }
8612
8613 /* if PT is not in the glyph row, give up. */
8614 if (PT < MATRIX_ROW_START_CHARPOS (row)
8615 || PT > MATRIX_ROW_END_CHARPOS (row))
8616 goto try_to_scroll;
8617
8618 /* If we end up in a partially visible line, let's make it fully
8619 visible. This can be done most easily by using the existing
8620 scrolling code. */
8621 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8622 {
8623 temp_scroll_step = 1;
8624 goto try_to_scroll;
a2889657 8625 }
5f5c8ee5
GM
8626 else if (scroll_p)
8627 goto try_to_scroll;
8628
8629 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8630 goto done;
a2889657 8631 }
5f5c8ee5 8632
a2889657
JB
8633 /* If current starting point was originally the beginning of a line
8634 but no longer is, find a new starting point. */
265a9e55 8635 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
8636 && !(CHARPOS (startp) <= BEGV
8637 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 8638 {
5f5c8ee5
GM
8639#if GLYPH_DEBUG
8640 debug_method_add (w, "recenter 1");
8641#endif
a2889657
JB
8642 goto recenter;
8643 }
5f5c8ee5
GM
8644
8645 /* Try scrolling with try_window_id. */
9142dd5b
GM
8646 else if (/* Windows and buffers haven't changed. */
8647 !windows_or_buffers_changed
5f5c8ee5
GM
8648 /* Window must be either use window-based redisplay or
8649 be full width. */
8650 && (FRAME_WINDOW_P (f)
c59c668a 8651 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
8652 && !MINI_WINDOW_P (w)
8653 /* Point is not known NOT to appear in window. */
8654 && PT >= CHARPOS (startp)
a2889657 8655 && XFASTINT (w->last_modified)
5f5c8ee5
GM
8656 /* Window is not hscrolled. */
8657 && XFASTINT (w->hscroll) == 0
8658 /* Selective display has not changed. */
8659 && !current_buffer->clip_changed
8660 /* Current matrix is up to date. */
8661 && !NILP (w->window_end_valid)
8662 /* Can't use this case if highlighting a region because
8663 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
8664 && !(!NILP (Vtransient_mark_mode)
8665 && !NILP (current_buffer->mark_active))
8666 && NILP (w->region_showing)
8f897821 8667 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 8668 /* Overlay arrow position and string not changed. */
d45de95b 8669 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 8670 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
8671 /* Value is > 0 if update has been done, it is -1 if we
8672 know that the same window start will not work. It is 0
8673 if unsuccessful for some other reason. */
8674 && (tem = try_window_id (w)) != 0)
a2889657 8675 {
5f5c8ee5
GM
8676#if GLYPH_DEBUG
8677 debug_method_add (w, "try_window_id");
8678#endif
8679
8680 if (fonts_changed_p)
8681 goto restore_buffers;
a2889657
JB
8682 if (tem > 0)
8683 goto done;
5f5c8ee5
GM
8684 /* Otherwise try_window_id has returned -1 which means that we
8685 don't want the alternative below this comment to execute. */
a2889657 8686 }
5f5c8ee5
GM
8687 else if (CHARPOS (startp) >= BEGV
8688 && CHARPOS (startp) <= ZV
8689 && PT >= CHARPOS (startp)
8690 && (CHARPOS (startp) < ZV
e9874cee 8691 /* Avoid starting at end of buffer. */
5f5c8ee5 8692 || CHARPOS (startp) == BEGV
8850a573
RS
8693 || (XFASTINT (w->last_modified) >= MODIFF
8694 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 8695 {
5f5c8ee5
GM
8696#if GLYPH_DEBUG
8697 debug_method_add (w, "same window start");
8698#endif
8699
8700 /* Try to redisplay starting at same place as before.
8701 If point has not moved off frame, accept the results. */
8702 if (!current_matrix_up_to_date_p
8703 /* Don't use try_window_reusing_current_matrix in this case
8704 because it can have changed the buffer. */
8705 || !NILP (Vwindow_scroll_functions)
8706 || MINI_WINDOW_P (w)
8707 || !try_window_reusing_current_matrix (w))
8708 {
8709 IF_DEBUG (debug_method_add (w, "1"));
8710 try_window (window, startp);
8711 }
8712
8713 if (fonts_changed_p)
8714 goto restore_buffers;
8715
8716 if (w->cursor.vpos >= 0)
aa6d10fa 8717 {
5f5c8ee5
GM
8718 if (!just_this_one_p
8719 || current_buffer->clip_changed
9142dd5b 8720 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
8721 /* Forget any recorded base line for line number display. */
8722 w->base_line_number = Qnil;
5f5c8ee5
GM
8723
8724 make_cursor_line_fully_visible (w);
aa6d10fa
RS
8725 goto done;
8726 }
a2889657 8727 else
5f5c8ee5 8728 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
8729 }
8730
5f5c8ee5
GM
8731 try_to_scroll:
8732
c2213350 8733 XSETFASTINT (w->last_modified, 0);
8850a573 8734 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 8735
e481f960
RS
8736 /* Redisplay the mode line. Select the buffer properly for that. */
8737 if (!update_mode_line)
8738 {
5ba50c51
RS
8739 if (!really_switched_buffer)
8740 {
8741 set_buffer_temp (old);
8742 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8743 really_switched_buffer = 1;
5ba50c51 8744 }
e481f960
RS
8745 update_mode_line = 1;
8746 w->update_mode_line = Qt;
8747 }
a2889657 8748
5f5c8ee5
GM
8749 /* Try to scroll by specified few lines. */
8750 if ((scroll_conservatively
8751 || scroll_step
8752 || temp_scroll_step
8753 || NUMBERP (current_buffer->scroll_up_aggressively)
8754 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 8755 && !current_buffer->clip_changed
5f5c8ee5
GM
8756 && CHARPOS (startp) >= BEGV
8757 && CHARPOS (startp) <= ZV)
0789adb2 8758 {
5f5c8ee5
GM
8759 /* The function returns -1 if new fonts were loaded, 1 if
8760 successful, 0 if not successful. */
8761 int rc = try_scrolling (window, just_this_one_p,
8762 scroll_conservatively,
8763 scroll_step,
8764 temp_scroll_step);
8765 if (rc > 0)
8766 goto done;
8767 else if (rc < 0)
8768 goto restore_buffers;
8769 }
f9c8af06 8770
5f5c8ee5 8771 /* Finally, just choose place to start which centers point */
5936754e 8772
5f5c8ee5 8773 recenter:
44173109 8774
5f5c8ee5
GM
8775#if GLYPH_DEBUG
8776 debug_method_add (w, "recenter");
8777#endif
0789adb2 8778
5f5c8ee5 8779 /* w->vscroll = 0; */
0789adb2 8780
5f5c8ee5
GM
8781 /* Forget any previously recorded base line for line number display. */
8782 if (!current_matrix_up_to_date_p
8783 || current_buffer->clip_changed)
8784 w->base_line_number = Qnil;
8785
8786 /* Move backward half the height of the window. */
8787 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8788 it.current_y = it.last_visible_y;
8789 move_it_vertically_backward (&it, it.last_visible_y / 2);
8790 xassert (IT_CHARPOS (it) >= BEGV);
8791
8792 /* The function move_it_vertically_backward may move over more
8793 than the specified y-distance. If it->w is small, e.g. a
8794 mini-buffer window, we may end up in front of the window's
8795 display area. Start displaying at the start of the line
8796 containing PT in this case. */
8797 if (it.current_y <= 0)
8798 {
8799 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8800 move_it_vertically (&it, 0);
8801 xassert (IT_CHARPOS (it) <= PT);
8802 it.current_y = 0;
0789adb2
RS
8803 }
8804
5f5c8ee5
GM
8805 it.current_x = it.hpos = 0;
8806
8807 /* Set startp here explicitly in case that helps avoid an infinite loop
8808 in case the window-scroll-functions functions get errors. */
8809 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8810
8811 /* Run scroll hooks. */
8812 startp = run_window_scroll_functions (window, it.current.pos);
8813
8814 /* Redisplay the window. */
8815 if (!current_matrix_up_to_date_p
8816 || windows_or_buffers_changed
8817 /* Don't use try_window_reusing_current_matrix in this case
8818 because it can have changed the buffer. */
8819 || !NILP (Vwindow_scroll_functions)
8820 || !just_this_one_p
8821 || MINI_WINDOW_P (w)
8822 || !try_window_reusing_current_matrix (w))
8823 try_window (window, startp);
8824
8825 /* If new fonts have been loaded (due to fontsets), give up. We
8826 have to start a new redisplay since we need to re-adjust glyph
8827 matrices. */
8828 if (fonts_changed_p)
8829 goto restore_buffers;
8830
8831 /* If cursor did not appear assume that the middle of the window is
8832 in the first line of the window. Do it again with the next line.
8833 (Imagine a window of height 100, displaying two lines of height
8834 60. Moving back 50 from it->last_visible_y will end in the first
8835 line.) */
8836 if (w->cursor.vpos < 0)
a2889657 8837 {
5f5c8ee5
GM
8838 if (!NILP (w->window_end_valid)
8839 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 8840 {
5f5c8ee5
GM
8841 clear_glyph_matrix (w->desired_matrix);
8842 move_it_by_lines (&it, 1, 0);
8843 try_window (window, it.current.pos);
a2889657 8844 }
5f5c8ee5 8845 else if (PT < IT_CHARPOS (it))
a2889657 8846 {
5f5c8ee5
GM
8847 clear_glyph_matrix (w->desired_matrix);
8848 move_it_by_lines (&it, -1, 0);
8849 try_window (window, it.current.pos);
8850 }
8851 else
8852 {
8853 /* Not much we can do about it. */
a2889657 8854 }
a2889657 8855 }
010494d0 8856
5f5c8ee5
GM
8857 /* Consider the following case: Window starts at BEGV, there is
8858 invisible, intangible text at BEGV, so that display starts at
8859 some point START > BEGV. It can happen that we are called with
8860 PT somewhere between BEGV and START. Try to handle that case. */
8861 if (w->cursor.vpos < 0)
835766b6 8862 {
5f5c8ee5
GM
8863 struct glyph_row *row = w->current_matrix->rows;
8864 if (row->mode_line_p)
8865 ++row;
8866 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 8867 }
5f5c8ee5
GM
8868
8869 make_cursor_line_fully_visible (w);
b5174a51 8870
5f5c8ee5
GM
8871 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8872 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8873 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8874 ? Qt : Qnil);
a2889657 8875
5f5c8ee5 8876 done:
a2889657 8877
5f5c8ee5 8878 /* Display the mode line, if we must. */
e481f960 8879 if ((update_mode_line
aa6d10fa 8880 /* If window not full width, must redo its mode line
5f5c8ee5
GM
8881 if (a) the window to its side is being redone and
8882 (b) we do a frame-based redisplay. This is a consequence
8883 of how inverted lines are drawn in frame-based redisplay. */
8884 || (!just_this_one_p
8885 && !FRAME_WINDOW_P (f)
8886 && !WINDOW_FULL_WIDTH_P (w))
8887 /* Line number to display. */
155ef550 8888 || INTEGERP (w->base_line_pos)
5f5c8ee5 8889 /* Column number is displayed and different from the one displayed. */
155ef550
KH
8890 || (!NILP (w->column_number_displayed)
8891 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
8892 /* This means that the window has a mode line. */
8893 && (WINDOW_WANTS_MODELINE_P (w)
8894 || WINDOW_WANTS_TOP_LINE_P (w)))
5ba50c51 8895 {
5f5c8ee5
GM
8896 display_mode_lines (w);
8897
8898 /* If mode line height has changed, arrange for a thorough
8899 immediate redisplay using the correct mode line height. */
8900 if (WINDOW_WANTS_MODELINE_P (w)
8901 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 8902 {
5f5c8ee5
GM
8903 fonts_changed_p = 1;
8904 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8905 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 8906 }
5f5c8ee5
GM
8907
8908 /* If top line height has changed, arrange for a thorough
8909 immediate redisplay using the correct mode line height. */
8910 if (WINDOW_WANTS_TOP_LINE_P (w)
8911 && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
8912 {
8913 fonts_changed_p = 1;
8914 MATRIX_TOP_LINE_ROW (w->current_matrix)->height
8915 = DESIRED_TOP_LINE_HEIGHT (w);
8916 }
8917
8918 if (fonts_changed_p)
8919 goto restore_buffers;
5ba50c51 8920 }
5f5c8ee5
GM
8921
8922 if (!line_number_displayed
8923 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
8924 {
8925 w->base_line_pos = Qnil;
8926 w->base_line_number = Qnil;
8927 }
a2889657 8928
5f5c8ee5
GM
8929 finish_menu_bars:
8930
7ce2c095 8931 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 8932 if (update_mode_line
5f5c8ee5
GM
8933 && EQ (FRAME_SELECTED_WINDOW (f), window))
8934 {
8935 int redisplay_menu_p = 0;
8936
8937 if (FRAME_WINDOW_P (f))
8938 {
dc937613 8939#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 8940 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 8941#else
5f5c8ee5 8942 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 8943#endif
5f5c8ee5
GM
8944 }
8945 else
8946 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8947
8948 if (redisplay_menu_p)
8949 display_menu_bar (w);
8950
8951#ifdef HAVE_WINDOW_SYSTEM
8952 if (WINDOWP (f->toolbar_window)
8953 && (FRAME_TOOLBAR_LINES (f) > 0
8954 || auto_resize_toolbars_p))
8955 redisplay_toolbar (f);
8956#endif
8957 }
7ce2c095 8958
88f22aff 8959 finish_scroll_bars:
5f5c8ee5 8960
88f22aff 8961 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 8962 {
b1d1124b 8963 int start, end, whole;
30c566e4 8964
b1d1124b 8965 /* Calculate the start and end positions for the current window.
3505ea70
JB
8966 At some point, it would be nice to choose between scrollbars
8967 which reflect the whole buffer size, with special markers
8968 indicating narrowing, and scrollbars which reflect only the
8969 visible region.
8970
5f5c8ee5 8971 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 8972 if (!MINI_WINDOW_P (w)
5f5c8ee5 8973 || (w == XWINDOW (minibuf_window)
c6e89d6c 8974 && NILP (echo_area_buffer[0])))
b1d1124b 8975 {
8a9311d7 8976 whole = ZV - BEGV;
4d641a15 8977 start = marker_position (w->start) - BEGV;
b1d1124b
JB
8978 /* I don't think this is guaranteed to be right. For the
8979 moment, we'll pretend it is. */
5f5c8ee5 8980 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 8981
5f5c8ee5
GM
8982 if (end < start)
8983 end = start;
8984 if (whole < (end - start))
8985 whole = end - start;
b1d1124b
JB
8986 }
8987 else
8988 start = end = whole = 0;
30c566e4 8989
88f22aff 8990 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 8991 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 8992
5f5c8ee5
GM
8993 /* Note that we actually used the scroll bar attached to this
8994 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 8995 (*redeem_scroll_bar_hook) (w);
30c566e4 8996 }
b1d1124b 8997
5f5c8ee5
GM
8998 restore_buffers:
8999
9000 /* Restore current_buffer and value of point in it. */
9001 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
5ba50c51 9002 if (really_switched_buffer)
f72df6ac 9003 set_buffer_internal_1 (old);
e481f960
RS
9004 else
9005 set_buffer_temp (old);
5f5c8ee5 9006 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
9007
9008 unbind_to (count, Qnil);
a2889657 9009}
a2889657 9010
5f5c8ee5
GM
9011
9012/* Build the complete desired matrix of WINDOW with a window start
9013 buffer position POS. Value is non-zero if successful. It is zero
9014 if fonts were loaded during redisplay which makes re-adjusting
9015 glyph matrices necessary. */
9016
9017int
a2889657
JB
9018try_window (window, pos)
9019 Lisp_Object window;
5f5c8ee5
GM
9020 struct text_pos pos;
9021{
9022 struct window *w = XWINDOW (window);
9023 struct it it;
9024 struct glyph_row *last_text_row = NULL;
9cbab4ff 9025
5f5c8ee5
GM
9026 /* Make POS the new window start. */
9027 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 9028
5f5c8ee5
GM
9029 /* Mark cursor position as unknown. No overlay arrow seen. */
9030 w->cursor.vpos = -1;
a2889657 9031 overlay_arrow_seen = 0;
642eefc6 9032
5f5c8ee5
GM
9033 /* Initialize iterator and info to start at POS. */
9034 start_display (&it, w, pos);
a2889657 9035
5f5c8ee5
GM
9036 /* Display all lines of W. */
9037 while (it.current_y < it.last_visible_y)
9038 {
9039 if (display_line (&it))
9040 last_text_row = it.glyph_row - 1;
9041 if (fonts_changed_p)
9042 return 0;
9043 }
a2889657 9044
5f5c8ee5
GM
9045 /* If bottom moved off end of frame, change mode line percentage. */
9046 if (XFASTINT (w->window_end_pos) <= 0
9047 && Z != IT_CHARPOS (it))
a2889657
JB
9048 w->update_mode_line = Qt;
9049
5f5c8ee5
GM
9050 /* Set window_end_pos to the offset of the last character displayed
9051 on the window from the end of current_buffer. Set
9052 window_end_vpos to its row number. */
9053 if (last_text_row)
9054 {
9055 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9056 w->window_end_bytepos
9057 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9058 XSETFASTINT (w->window_end_pos,
9059 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9060 XSETFASTINT (w->window_end_vpos,
9061 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9062 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9063 ->displays_text_p);
9064 }
9065 else
9066 {
9067 w->window_end_bytepos = 0;
9068 XSETFASTINT (w->window_end_pos, 0);
9069 XSETFASTINT (w->window_end_vpos, 0);
9070 }
9071
a2889657
JB
9072 /* But that is not valid info until redisplay finishes. */
9073 w->window_end_valid = Qnil;
5f5c8ee5 9074 return 1;
a2889657 9075}
5f5c8ee5
GM
9076
9077
a2889657 9078\f
5f5c8ee5
GM
9079/************************************************************************
9080 Window redisplay reusing current matrix when buffer has not changed
9081 ************************************************************************/
9082
9083/* Try redisplay of window W showing an unchanged buffer with a
9084 different window start than the last time it was displayed by
9085 reusing its current matrix. Value is non-zero if successful.
9086 W->start is the new window start. */
a2889657
JB
9087
9088static int
5f5c8ee5
GM
9089try_window_reusing_current_matrix (w)
9090 struct window *w;
a2889657 9091{
5f5c8ee5
GM
9092 struct frame *f = XFRAME (w->frame);
9093 struct glyph_row *row, *bottom_row;
9094 struct it it;
9095 struct run run;
9096 struct text_pos start, new_start;
9097 int nrows_scrolled, i;
9098 struct glyph_row *last_text_row;
9099 struct glyph_row *last_reused_text_row;
9100 struct glyph_row *start_row;
9101 int start_vpos, min_y, max_y;
9102
9103 /* Right now this function doesn't handle terminal frames. */
9104 if (!FRAME_WINDOW_P (f))
9105 return 0;
a2889657 9106
5f5c8ee5
GM
9107 /* Can't do this if region may have changed. */
9108 if ((!NILP (Vtransient_mark_mode)
9109 && !NILP (current_buffer->mark_active))
8f897821
GM
9110 || !NILP (w->region_showing)
9111 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 9112 return 0;
a2889657 9113
5f5c8ee5
GM
9114 /* If top-line visibility has changed, give up. */
9115 if (WINDOW_WANTS_TOP_LINE_P (w)
9116 != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
9117 return 0;
9118
9119 /* Give up if old or new display is scrolled vertically. We could
9120 make this function handle this, but right now it doesn't. */
9121 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9122 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9123 return 0;
9124
9125 /* The variable new_start now holds the new window start. The old
9126 start `start' can be determined from the current matrix. */
9127 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9128 start = start_row->start.pos;
9129 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 9130
5f5c8ee5
GM
9131 /* Clear the desired matrix for the display below. */
9132 clear_glyph_matrix (w->desired_matrix);
9133
9134 if (CHARPOS (new_start) <= CHARPOS (start))
9135 {
9136 int first_row_y;
9137
9138 IF_DEBUG (debug_method_add (w, "twu1"));
9139
9140 /* Display up to a row that can be reused. The variable
9141 last_text_row is set to the last row displayed that displays
9142 text. */
9143 start_display (&it, w, new_start);
9144 first_row_y = it.current_y;
9145 w->cursor.vpos = -1;
9146 last_text_row = last_reused_text_row = NULL;
9147 while (it.current_y < it.last_visible_y
9148 && IT_CHARPOS (it) < CHARPOS (start)
9149 && !fonts_changed_p)
9150 if (display_line (&it))
9151 last_text_row = it.glyph_row - 1;
9152
9153 /* A value of current_y < last_visible_y means that we stopped
9154 at the previous window start, which in turn means that we
9155 have at least one reusable row. */
9156 if (it.current_y < it.last_visible_y)
a2889657 9157 {
5f5c8ee5
GM
9158 nrows_scrolled = it.vpos;
9159
9160 /* Find PT if not already found in the lines displayed. */
9161 if (w->cursor.vpos < 0)
a2889657 9162 {
5f5c8ee5
GM
9163 int dy = it.current_y - first_row_y;
9164
9165 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9166 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9167 {
9168 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9169 && PT < MATRIX_ROW_END_CHARPOS (row))
9170 {
9171 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9172 dy, nrows_scrolled);
9173 break;
9174 }
9175
9176 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9177 break;
9178
9179 ++row;
9180 }
9181
9182 /* Give up if point was not found. This shouldn't
9183 happen often; not more often than with try_window
9184 itself. */
9185 if (w->cursor.vpos < 0)
9186 {
9187 clear_glyph_matrix (w->desired_matrix);
9188 return 0;
9189 }
a2889657 9190 }
5f5c8ee5
GM
9191
9192 /* Scroll the display. Do it before the current matrix is
9193 changed. The problem here is that update has not yet
9194 run, i.e. part of the current matrix is not up to date.
9195 scroll_run_hook will clear the cursor, and use the
9196 current matrix to get the height of the row the cursor is
9197 in. */
9198 run.current_y = first_row_y;
9199 run.desired_y = it.current_y;
9200 run.height = it.last_visible_y - it.current_y;
9201 if (run.height > 0)
a2889657 9202 {
5f5c8ee5
GM
9203 update_begin (f);
9204 rif->update_window_begin_hook (w);
9205 rif->scroll_run_hook (w, &run);
9206 rif->update_window_end_hook (w, 0);
9207 update_end (f);
a2889657 9208 }
5f5c8ee5
GM
9209
9210 /* Shift current matrix down by nrows_scrolled lines. */
9211 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9212 rotate_matrix (w->current_matrix,
9213 start_vpos,
9214 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9215 nrows_scrolled);
9216
9217 /* Disable lines not reused. */
9218 for (i = 0; i < it.vpos; ++i)
9219 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9220
9221 /* Re-compute Y positions. */
9222 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9223 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9224 max_y = it.last_visible_y;
9225 while (row < bottom_row)
d2f84654 9226 {
5f5c8ee5
GM
9227 row->y = it.current_y;
9228
9229 if (row->y < min_y)
9230 row->visible_height = row->height - (min_y - row->y);
9231 else if (row->y + row->height > max_y)
9232 row->visible_height
9233 = row->height - (row->y + row->height - max_y);
9234 else
9235 row->visible_height = row->height;
9236
9237 it.current_y += row->height;
9238 ++it.vpos;
9239
9240 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9241 last_reused_text_row = row;
9242 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9243 break;
9244 ++row;
d2f84654 9245 }
a2889657 9246 }
5f5c8ee5
GM
9247
9248 /* Update window_end_pos etc.; last_reused_text_row is the last
9249 reused row from the current matrix containing text, if any.
9250 The value of last_text_row is the last displayed line
9251 containing text. */
9252 if (last_reused_text_row)
a2889657 9253 {
5f5c8ee5
GM
9254 w->window_end_bytepos
9255 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9256 XSETFASTINT (w->window_end_pos,
9257 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9258 XSETFASTINT (w->window_end_vpos,
9259 MATRIX_ROW_VPOS (last_reused_text_row,
9260 w->current_matrix));
a2889657 9261 }
5f5c8ee5
GM
9262 else if (last_text_row)
9263 {
9264 w->window_end_bytepos
9265 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9266 XSETFASTINT (w->window_end_pos,
9267 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9268 XSETFASTINT (w->window_end_vpos,
9269 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9270 }
9271 else
9272 {
9273 /* This window must be completely empty. */
9274 w->window_end_bytepos = 0;
9275 XSETFASTINT (w->window_end_pos, 0);
9276 XSETFASTINT (w->window_end_vpos, 0);
9277 }
9278 w->window_end_valid = Qnil;
a2889657 9279
5f5c8ee5
GM
9280 /* Update hint: don't try scrolling again in update_window. */
9281 w->desired_matrix->no_scrolling_p = 1;
9282
9283#if GLYPH_DEBUG
9284 debug_method_add (w, "try_window_reusing_current_matrix 1");
9285#endif
9286 return 1;
a2889657 9287 }
5f5c8ee5
GM
9288 else if (CHARPOS (new_start) > CHARPOS (start))
9289 {
9290 struct glyph_row *pt_row, *row;
9291 struct glyph_row *first_reusable_row;
9292 struct glyph_row *first_row_to_display;
9293 int dy;
9294 int yb = window_text_bottom_y (w);
9295
9296 IF_DEBUG (debug_method_add (w, "twu2"));
9297
9298 /* Find the row starting at new_start, if there is one. Don't
9299 reuse a partially visible line at the end. */
9300 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9301 while (first_reusable_row->enabled_p
9302 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9303 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9304 < CHARPOS (new_start)))
9305 ++first_reusable_row;
9306
9307 /* Give up if there is no row to reuse. */
9308 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
9309 || !first_reusable_row->enabled_p
9310 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9311 != CHARPOS (new_start)))
5f5c8ee5
GM
9312 return 0;
9313
5f5c8ee5
GM
9314 /* We can reuse fully visible rows beginning with
9315 first_reusable_row to the end of the window. Set
9316 first_row_to_display to the first row that cannot be reused.
9317 Set pt_row to the row containing point, if there is any. */
9318 first_row_to_display = first_reusable_row;
9319 pt_row = NULL;
9320 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9321 {
9322 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9323 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9324 pt_row = first_row_to_display;
a2889657 9325
5f5c8ee5
GM
9326 ++first_row_to_display;
9327 }
a2889657 9328
5f5c8ee5
GM
9329 /* Start displaying at the start of first_row_to_display. */
9330 xassert (first_row_to_display->y < yb);
9331 init_to_row_start (&it, w, first_row_to_display);
9332 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9333 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9334 - nrows_scrolled);
9335 it.current_y = first_row_to_display->y - first_reusable_row->y;
9336
9337 /* Display lines beginning with first_row_to_display in the
9338 desired matrix. Set last_text_row to the last row displayed
9339 that displays text. */
9340 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9341 if (pt_row == NULL)
9342 w->cursor.vpos = -1;
9343 last_text_row = NULL;
9344 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9345 if (display_line (&it))
9346 last_text_row = it.glyph_row - 1;
9347
9348 /* Give up If point isn't in a row displayed or reused. */
9349 if (w->cursor.vpos < 0)
9350 {
9351 clear_glyph_matrix (w->desired_matrix);
9352 return 0;
9353 }
12adba34 9354
5f5c8ee5
GM
9355 /* If point is in a reused row, adjust y and vpos of the cursor
9356 position. */
9357 if (pt_row)
9358 {
9359 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9360 w->current_matrix);
9361 w->cursor.y -= first_reusable_row->y;
a2889657
JB
9362 }
9363
5f5c8ee5
GM
9364 /* Scroll the display. */
9365 run.current_y = first_reusable_row->y;
9366 run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9367 run.height = it.last_visible_y - run.current_y;
9368 if (run.height)
9369 {
9370 struct frame *f = XFRAME (WINDOW_FRAME (w));
9371 update_begin (f);
9372 rif->update_window_begin_hook (w);
9373 rif->scroll_run_hook (w, &run);
9374 rif->update_window_end_hook (w, 0);
9375 update_end (f);
9376 }
a2889657 9377
5f5c8ee5
GM
9378 /* Adjust Y positions of reused rows. */
9379 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9380 row = first_reusable_row;
9381 dy = first_reusable_row->y;
9382 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9383 max_y = it.last_visible_y;
9384 while (row < first_row_to_display)
9385 {
9386 row->y -= dy;
9387 if (row->y < min_y)
9388 row->visible_height = row->height - (min_y - row->y);
9389 else if (row->y + row->height > max_y)
9390 row->visible_height
9391 = row->height - (row->y + row->height - max_y);
9392 else
9393 row->visible_height = row->height;
9394 ++row;
9395 }
a2889657 9396
5f5c8ee5
GM
9397 /* Disable rows not reused. */
9398 while (row < bottom_row)
9399 {
9400 row->enabled_p = 0;
9401 ++row;
9402 }
9403
9404 /* Scroll the current matrix. */
9405 xassert (nrows_scrolled > 0);
9406 rotate_matrix (w->current_matrix,
9407 start_vpos,
9408 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9409 -nrows_scrolled);
9410
9411 /* Adjust window end. A null value of last_text_row means that
9412 the window end is in reused rows which in turn means that
9413 only its vpos can have changed. */
9414 if (last_text_row)
9415 {
9416 w->window_end_bytepos
9417 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9418 XSETFASTINT (w->window_end_pos,
9419 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9420 XSETFASTINT (w->window_end_vpos,
9421 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9422 }
9423 else
a2889657 9424 {
e8e536a9 9425 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 9426 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 9427 }
5f5c8ee5
GM
9428
9429 w->window_end_valid = Qnil;
9430 w->desired_matrix->no_scrolling_p = 1;
9431
9432#if GLYPH_DEBUG
9433 debug_method_add (w, "try_window_reusing_current_matrix 2");
9434#endif
9435 return 1;
a2889657 9436 }
5f5c8ee5
GM
9437
9438 return 0;
9439}
a2889657 9440
a2889657 9441
5f5c8ee5
GM
9442\f
9443/************************************************************************
9444 Window redisplay reusing current matrix when buffer has changed
9445 ************************************************************************/
9446
9447static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9448static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9449 int *, int *));
9450static struct glyph_row *
9451find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9452 struct glyph_row *));
9453
9454
9455/* Return the last row in MATRIX displaying text. If row START is
9456 non-null, start searching with that row. IT gives the dimensions
9457 of the display. Value is null if matrix is empty; otherwise it is
9458 a pointer to the row found. */
9459
9460static struct glyph_row *
9461find_last_row_displaying_text (matrix, it, start)
9462 struct glyph_matrix *matrix;
9463 struct it *it;
9464 struct glyph_row *start;
9465{
9466 struct glyph_row *row, *row_found;
9467
9468 /* Set row_found to the last row in IT->w's current matrix
9469 displaying text. The loop looks funny but think of partially
9470 visible lines. */
9471 row_found = NULL;
9472 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9473 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9474 {
9475 xassert (row->enabled_p);
9476 row_found = row;
9477 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9478 break;
9479 ++row;
a2889657 9480 }
5f5c8ee5
GM
9481
9482 return row_found;
9483}
9484
a2889657 9485
5f5c8ee5
GM
9486/* Return the last row in the current matrix of W that is not affected
9487 by changes at the start of current_buffer that occurred since the
9488 last time W was redisplayed. Value is null if no such row exists.
a2889657 9489
5f5c8ee5
GM
9490 The global variable beg_unchanged has to contain the number of
9491 bytes unchanged at the start of current_buffer. BEG +
9492 beg_unchanged is the buffer position of the first changed byte in
9493 current_buffer. Characters at positions < BEG + beg_unchanged are
9494 at the same buffer positions as they were when the current matrix
9495 was built. */
9496
9497static struct glyph_row *
9498get_last_unchanged_at_beg_row (w)
9499 struct window *w;
9500{
9142dd5b 9501 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
9502 struct glyph_row *row;
9503 struct glyph_row *row_found = NULL;
9504 int yb = window_text_bottom_y (w);
9505
9506 /* Find the last row displaying unchanged text. */
9507 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9508 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9509 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 9510 {
5f5c8ee5
GM
9511 if (/* If row ends before first_changed_pos, it is unchanged,
9512 except in some case. */
9513 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9514 /* When row ends in ZV and we write at ZV it is not
9515 unchanged. */
9516 && !row->ends_at_zv_p
9517 /* When first_changed_pos is the end of a continued line,
9518 row is not unchanged because it may be no longer
9519 continued. */
9520 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9521 && row->continued_p))
9522 row_found = row;
9523
9524 /* Stop if last visible row. */
9525 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9526 break;
9527
9528 ++row;
a2889657
JB
9529 }
9530
5f5c8ee5 9531 return row_found;
a2889657 9532}
5f5c8ee5
GM
9533
9534
9535/* Find the first glyph row in the current matrix of W that is not
9536 affected by changes at the end of current_buffer since the last
9537 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
9538 chars by which buffer positions in unchanged text at the end of
9539 current_buffer must be adjusted. Return in *DELTA_BYTES the
9540 corresponding number of bytes. Value is null if no such row
9541 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
9542
9543static struct glyph_row *
9544get_first_unchanged_at_end_row (w, delta, delta_bytes)
9545 struct window *w;
9546 int *delta, *delta_bytes;
a2889657 9547{
5f5c8ee5
GM
9548 struct glyph_row *row;
9549 struct glyph_row *row_found = NULL;
c581d710 9550
5f5c8ee5 9551 *delta = *delta_bytes = 0;
b2a76982 9552
5f5c8ee5
GM
9553 /* A value of window_end_pos >= end_unchanged means that the window
9554 end is in the range of changed text. If so, there is no
9555 unchanged row at the end of W's current matrix. */
9556 xassert (!NILP (w->window_end_valid));
9142dd5b 9557 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
9558 return NULL;
9559
9560 /* Set row to the last row in W's current matrix displaying text. */
9561 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9562
5f5c8ee5
GM
9563 /* If matrix is entirely empty, no unchanged row exists. */
9564 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9565 {
9566 /* The value of row is the last glyph row in the matrix having a
9567 meaningful buffer position in it. The end position of row
9568 corresponds to window_end_pos. This allows us to translate
9569 buffer positions in the current matrix to current buffer
9570 positions for characters not in changed text. */
9571 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9572 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9573 int last_unchanged_pos, last_unchanged_pos_old;
9574 struct glyph_row *first_text_row
9575 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9576
9577 *delta = Z - Z_old;
9578 *delta_bytes = Z_BYTE - Z_BYTE_old;
9579
9580 /* Set last_unchanged_pos to the buffer position of the last
9581 character in the buffer that has not been changed. Z is the
9582 index + 1 of the last byte in current_buffer, i.e. by
9583 subtracting end_unchanged we get the index of the last
9584 unchanged character, and we have to add BEG to get its buffer
9585 position. */
9142dd5b 9586 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
9587 last_unchanged_pos_old = last_unchanged_pos - *delta;
9588
9589 /* Search backward from ROW for a row displaying a line that
9590 starts at a minimum position >= last_unchanged_pos_old. */
9591 while (row >= first_text_row)
9592 {
9593 xassert (row->enabled_p);
9594 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9595
9596 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9597 row_found = row;
9598 --row;
9599 }
9600 }
9601
9602 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9603 return row_found;
c581d710
RS
9604}
9605
c581d710 9606
5f5c8ee5
GM
9607/* Make sure that glyph rows in the current matrix of window W
9608 reference the same glyph memory as corresponding rows in the
9609 frame's frame matrix. This function is called after scrolling W's
9610 current matrix on a terminal frame in try_window_id and
9611 try_window_reusing_current_matrix. */
9612
9613static void
9614sync_frame_with_window_matrix_rows (w)
9615 struct window *w;
c581d710 9616{
5f5c8ee5
GM
9617 struct frame *f = XFRAME (w->frame);
9618 struct glyph_row *window_row, *window_row_end, *frame_row;
9619
9620 /* Preconditions: W must be a leaf window and full-width. Its frame
9621 must have a frame matrix. */
9622 xassert (NILP (w->hchild) && NILP (w->vchild));
9623 xassert (WINDOW_FULL_WIDTH_P (w));
9624 xassert (!FRAME_WINDOW_P (f));
9625
9626 /* If W is a full-width window, glyph pointers in W's current matrix
9627 have, by definition, to be the same as glyph pointers in the
9628 corresponding frame matrix. */
9629 window_row = w->current_matrix->rows;
9630 window_row_end = window_row + w->current_matrix->nrows;
9631 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9632 while (window_row < window_row_end)
659a218f 9633 {
5f5c8ee5
GM
9634 int area;
9635 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9636 frame_row->glyphs[area] = window_row->glyphs[area];
9637 ++window_row, ++frame_row;
659a218f 9638 }
a2889657 9639}
5f5c8ee5
GM
9640
9641
9642/* Try to redisplay window W by reusing its existing display. W's
9643 current matrix must be up to date when this function is called,
9644 i.e. window_end_valid must not be nil.
9645
9646 Value is
9647
9648 1 if display has been updated
9649 0 if otherwise unsuccessful
9650 -1 if redisplay with same window start is known not to succeed
9651
9652 The following steps are performed:
9653
9654 1. Find the last row in the current matrix of W that is not
9655 affected by changes at the start of current_buffer. If no such row
9656 is found, give up.
9657
9658 2. Find the first row in W's current matrix that is not affected by
9659 changes at the end of current_buffer. Maybe there is no such row.
9660
9661 3. Display lines beginning with the row + 1 found in step 1 to the
9662 row found in step 2 or, if step 2 didn't find a row, to the end of
9663 the window.
9664
9665 4. If cursor is not known to appear on the window, give up.
9666
9667 5. If display stopped at the row found in step 2, scroll the
9668 display and current matrix as needed.
9669
9670 6. Maybe display some lines at the end of W, if we must. This can
9671 happen under various circumstances, like a partially visible line
9672 becoming fully visible, or because newly displayed lines are displayed
9673 in smaller font sizes.
9674
9675 7. Update W's window end information. */
9676
9677 /* Check that window end is what we expect it to be. */
12adba34
RS
9678
9679static int
5f5c8ee5 9680try_window_id (w)
12adba34 9681 struct window *w;
12adba34 9682{
5f5c8ee5
GM
9683 struct frame *f = XFRAME (w->frame);
9684 struct glyph_matrix *current_matrix = w->current_matrix;
9685 struct glyph_matrix *desired_matrix = w->desired_matrix;
9686 struct glyph_row *last_unchanged_at_beg_row;
9687 struct glyph_row *first_unchanged_at_end_row;
9688 struct glyph_row *row;
9689 struct glyph_row *bottom_row;
9690 int bottom_vpos;
9691 struct it it;
9692 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9693 struct text_pos start_pos;
9694 struct run run;
9695 int first_unchanged_at_end_vpos = 0;
9696 struct glyph_row *last_text_row, *last_text_row_at_end;
9697 struct text_pos start;
9698
9699 SET_TEXT_POS_FROM_MARKER (start, w->start);
9700
9701 /* Check pre-conditions. Window end must be valid, otherwise
9702 the current matrix would not be up to date. */
9703 xassert (!NILP (w->window_end_valid));
9704 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9705 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9706
9707 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9708 only if buffer has really changed. The reason is that the gap is
9709 initially at Z for freshly visited files. The code below would
9710 set end_unchanged to 0 in that case. */
9711 if (MODIFF > SAVE_MODIFF)
9712 {
9142dd5b
GM
9713 if (GPT - BEG < BEG_UNCHANGED)
9714 BEG_UNCHANGED = GPT - BEG;
9715 if (Z - GPT < END_UNCHANGED)
9716 END_UNCHANGED = Z - GPT;
5f5c8ee5
GM
9717 }
9718
9719 /* If window starts after a line end, and the last change is in
9720 front of that newline, then changes don't affect the display.
9721 This case happens with stealth-fontification. */
9722 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9723 if (CHARPOS (start) > BEGV
9142dd5b 9724 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
9725 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9726 && PT < MATRIX_ROW_END_CHARPOS (row))
9727 {
9728 /* We have to update window end positions because the buffer's
9729 size has changed. */
9730 w->window_end_pos
9731 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9732 w->window_end_bytepos
9733 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9734 return 1;
9735 }
9736
9737 /* Return quickly if changes are all below what is displayed in the
9738 window, and if PT is in the window. */
9142dd5b 9739 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
9740 && PT < MATRIX_ROW_END_CHARPOS (row))
9741 {
9742 /* We have to update window end positions because the buffer's
9743 size has changed. */
9744 w->window_end_pos
9745 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9746 w->window_end_bytepos
9747 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9748 return 1;
9749 }
9750
9751 /* Check that window start agrees with the start of the first glyph
9752 row in its current matrix. Check this after we know the window
9753 start is not in changed text, otherwise positions would not be
9754 comparable. */
9755 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9756 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9757 return 0;
9758
5f5c8ee5
GM
9759 /* Compute the position at which we have to start displaying new
9760 lines. Some of the lines at the top of the window might be
9761 reusable because they are not displaying changed text. Find the
9762 last row in W's current matrix not affected by changes at the
9763 start of current_buffer. Value is null if changes start in the
9764 first line of window. */
9765 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9766 if (last_unchanged_at_beg_row)
9767 {
9768 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9769 start_pos = it.current.pos;
9770
9771 /* Start displaying new lines in the desired matrix at the same
9772 vpos we would use in the current matrix, i.e. below
9773 last_unchanged_at_beg_row. */
9774 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9775 current_matrix);
9776 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9777 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9778
9779 xassert (it.hpos == 0 && it.current_x == 0);
9780 }
9781 else
9782 {
9783 /* There are no reusable lines at the start of the window.
9784 Start displaying in the first line. */
9785 start_display (&it, w, start);
9786 start_pos = it.current.pos;
9787 }
9788
9789 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
9790 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
9791
9792 /* Find the first row that is not affected by changes at the end of
9793 the buffer. Value will be null if there is no unchanged row, in
9794 which case we must redisplay to the end of the window. delta
9795 will be set to the value by which buffer positions beginning with
9796 first_unchanged_at_end_row have to be adjusted due to text
9797 changes. */
9798 first_unchanged_at_end_row
9799 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9800 IF_DEBUG (debug_delta = delta);
9801 IF_DEBUG (debug_delta_bytes = delta_bytes);
9802
9803 /* Set stop_pos to the buffer position up to which we will have to
9804 display new lines. If first_unchanged_at_end_row != NULL, this
9805 is the buffer position of the start of the line displayed in that
9806 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9807 that we don't stop at a buffer position. */
9808 stop_pos = 0;
9809 if (first_unchanged_at_end_row)
9810 {
9811 xassert (last_unchanged_at_beg_row == NULL
9812 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9813
9814 /* If this is a continuation line, move forward to the next one
9815 that isn't. Changes in lines above affect this line.
9816 Caution: this may move first_unchanged_at_end_row to a row
9817 not displaying text. */
9818 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9819 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9820 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9821 < it.last_visible_y))
9822 ++first_unchanged_at_end_row;
9823
9824 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9825 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9826 >= it.last_visible_y))
9827 first_unchanged_at_end_row = NULL;
9828 else
9829 {
9830 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9831 + delta);
9832 first_unchanged_at_end_vpos
9833 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 9834 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
9835 }
9836 }
9837 else if (last_unchanged_at_beg_row == NULL)
9838 return 0;
9839
9840
9841#if GLYPH_DEBUG
9842
9843 /* Either there is no unchanged row at the end, or the one we have
9844 now displays text. This is a necessary condition for the window
9845 end pos calculation at the end of this function. */
9846 xassert (first_unchanged_at_end_row == NULL
9847 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9848
9849 debug_last_unchanged_at_beg_vpos
9850 = (last_unchanged_at_beg_row
9851 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9852 : -1);
9853 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9854
9855#endif /* GLYPH_DEBUG != 0 */
9856
9857
9858 /* Display new lines. Set last_text_row to the last new line
9859 displayed which has text on it, i.e. might end up as being the
9860 line where the window_end_vpos is. */
9861 w->cursor.vpos = -1;
9862 last_text_row = NULL;
9863 overlay_arrow_seen = 0;
9864 while (it.current_y < it.last_visible_y
9865 && !fonts_changed_p
9866 && (first_unchanged_at_end_row == NULL
9867 || IT_CHARPOS (it) < stop_pos))
9868 {
9869 if (display_line (&it))
9870 last_text_row = it.glyph_row - 1;
9871 }
9872
9873 if (fonts_changed_p)
9874 return -1;
9875
9876
9877 /* Compute differences in buffer positions, y-positions etc. for
9878 lines reused at the bottom of the window. Compute what we can
9879 scroll. */
9880 if (first_unchanged_at_end_row)
9881 {
9882 dvpos = (it.vpos
9883 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
9884 current_matrix));
9885 dy = it.current_y - first_unchanged_at_end_row->y;
9886 run.current_y = first_unchanged_at_end_row->y;
9887 run.desired_y = run.current_y + dy;
9888 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
9889 }
9890 else
9891 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
9892 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
9893
9894
9895 /* Find the cursor if not already found. We have to decide whether
9896 PT will appear on this window (it sometimes doesn't, but this is
9897 not a very frequent case.) This decision has to be made before
9898 the current matrix is altered. A value of cursor.vpos < 0 means
9899 that PT is either in one of the lines beginning at
9900 first_unchanged_at_end_row or below the window. Don't care for
9901 lines that might be displayed later at the window end; as
9902 mentioned, this is not a frequent case. */
9903 if (w->cursor.vpos < 0)
9904 {
9905 int last_y = min (it.last_visible_y, it.last_visible_y + dy);
9906
9907 /* Cursor in unchanged rows at the top? */
9908 if (PT < CHARPOS (start_pos)
9909 && last_unchanged_at_beg_row)
9910 {
9911 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9912 while (row <= last_unchanged_at_beg_row
9913 && MATRIX_ROW_END_CHARPOS (row) <= PT)
9914 ++row;
9915 xassert (row <= last_unchanged_at_beg_row);
9916 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9917 }
9918
9919 /* Start from first_unchanged_at_end_row looking for PT. */
9920 else if (first_unchanged_at_end_row)
9921 {
9922 row = first_unchanged_at_end_row;
9923 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9924 {
9925 if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
9926 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
9927 {
9928 set_cursor_from_row (w, row, w->current_matrix, delta,
9929 delta_bytes, dy, dvpos);
9930 break;
9931 }
9932 else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
9933 break;
9934 ++row;
9935 }
9936 }
9937
9938 /* Give up if cursor was not found. */
9939 if (w->cursor.vpos < 0)
9940 {
9941 clear_glyph_matrix (w->desired_matrix);
9942 return -1;
9943 }
9944 }
9945
9946 /* Don't let the cursor end in the scroll margins. */
9947 {
9948 int this_scroll_margin, cursor_height;
9949
9950 this_scroll_margin = max (0, scroll_margin);
9951 this_scroll_margin = min (this_scroll_margin,
9952 XFASTINT (w->height) / 4);
9953 this_scroll_margin *= CANON_Y_UNIT (it.f);
9954 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
9955
9956 if ((w->cursor.y < this_scroll_margin
9957 && CHARPOS (start) > BEGV)
9958 /* Don't take scroll margin into account at the bottom because
9959 old redisplay didn't do it either. */
9960 || w->cursor.y + cursor_height > it.last_visible_y)
9961 {
9962 w->cursor.vpos = -1;
9963 clear_glyph_matrix (w->desired_matrix);
9964 return -1;
9965 }
9966 }
9967
9968 /* Scroll the display. Do it before changing the current matrix so
9969 that xterm.c doesn't get confused about where the cursor glyph is
9970 found. */
9971 if (dy)
9972 {
9973 update_begin (f);
9974
9975 if (FRAME_WINDOW_P (f))
9976 {
9977 rif->update_window_begin_hook (w);
9978 rif->scroll_run_hook (w, &run);
9979 rif->update_window_end_hook (w, 0);
9980 }
9981 else
9982 {
9983 /* Terminal frame. In this case, dvpos gives the number of
9984 lines to scroll by; dvpos < 0 means scroll up. */
9985 int first_unchanged_at_end_vpos
9986 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
9987 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
9988 int end = XFASTINT (w->top) + window_internal_height (w);
9989
9990 /* Perform the operation on the screen. */
9991 if (dvpos > 0)
9992 {
9993 /* Scroll last_unchanged_at_beg_row to the end of the
9994 window down dvpos lines. */
9995 set_terminal_window (end);
9996
9997 /* On dumb terminals delete dvpos lines at the end
9998 before inserting dvpos empty lines. */
9999 if (!scroll_region_ok)
10000 ins_del_lines (end - dvpos, -dvpos);
10001
10002 /* Insert dvpos empty lines in front of
10003 last_unchanged_at_beg_row. */
10004 ins_del_lines (from, dvpos);
10005 }
10006 else if (dvpos < 0)
10007 {
10008 /* Scroll up last_unchanged_at_beg_vpos to the end of
10009 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10010 set_terminal_window (end);
10011
10012 /* Delete dvpos lines in front of
10013 last_unchanged_at_beg_vpos. ins_del_lines will set
10014 the cursor to the given vpos and emit |dvpos| delete
10015 line sequences. */
10016 ins_del_lines (from + dvpos, dvpos);
10017
10018 /* On a dumb terminal insert dvpos empty lines at the
10019 end. */
10020 if (!scroll_region_ok)
10021 ins_del_lines (end + dvpos, -dvpos);
10022 }
10023
10024 set_terminal_window (0);
10025 }
10026
10027 update_end (f);
10028 }
10029
10030 /* Shift reused rows of the current matrix to the right position. */
10031 if (dvpos < 0)
10032 {
10033 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10034 bottom_vpos, dvpos);
10035 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10036 bottom_vpos, 0);
10037 }
10038 else if (dvpos > 0)
10039 {
10040 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10041 bottom_vpos, dvpos);
10042 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10043 first_unchanged_at_end_vpos + dvpos, 0);
10044 }
10045
10046 /* For frame-based redisplay, make sure that current frame and window
10047 matrix are in sync with respect to glyph memory. */
10048 if (!FRAME_WINDOW_P (f))
10049 sync_frame_with_window_matrix_rows (w);
10050
10051 /* Adjust buffer positions in reused rows. */
10052 if (delta)
10053 increment_glyph_matrix_buffer_positions (current_matrix,
10054 first_unchanged_at_end_vpos + dvpos,
10055 bottom_vpos, delta, delta_bytes);
10056
10057 /* Adjust Y positions. */
10058 if (dy)
10059 shift_glyph_matrix (w, current_matrix,
10060 first_unchanged_at_end_vpos + dvpos,
10061 bottom_vpos, dy);
10062
10063 if (first_unchanged_at_end_row)
10064 first_unchanged_at_end_row += dvpos;
10065
10066 /* If scrolling up, there may be some lines to display at the end of
10067 the window. */
10068 last_text_row_at_end = NULL;
10069 if (dy < 0)
10070 {
10071 /* Set last_row to the glyph row in the current matrix where the
10072 window end line is found. It has been moved up or down in
10073 the matrix by dvpos. */
10074 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10075 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10076
10077 /* If last_row is the window end line, it should display text. */
10078 xassert (last_row->displays_text_p);
10079
10080 /* If window end line was partially visible before, begin
10081 displaying at that line. Otherwise begin displaying with the
10082 line following it. */
10083 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10084 {
10085 init_to_row_start (&it, w, last_row);
10086 it.vpos = last_vpos;
10087 it.current_y = last_row->y;
10088 }
10089 else
10090 {
10091 init_to_row_end (&it, w, last_row);
10092 it.vpos = 1 + last_vpos;
10093 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10094 ++last_row;
10095 }
12adba34 10096
5f5c8ee5
GM
10097 /* We may start in a continuation line. If so, we have to get
10098 the right continuation_lines_width and current_x. */
10099 it.continuation_lines_width = last_row->continuation_lines_width;
10100 it.hpos = it.current_x = 0;
10101
10102 /* Display the rest of the lines at the window end. */
10103 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10104 while (it.current_y < it.last_visible_y
10105 && !fonts_changed_p)
10106 {
10107 /* Is it always sure that the display agrees with lines in
10108 the current matrix? I don't think so, so we mark rows
10109 displayed invalid in the current matrix by setting their
10110 enabled_p flag to zero. */
10111 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10112 if (display_line (&it))
10113 last_text_row_at_end = it.glyph_row - 1;
10114 }
10115 }
12adba34 10116
5f5c8ee5
GM
10117 /* Update window_end_pos and window_end_vpos. */
10118 if (first_unchanged_at_end_row
10119 && first_unchanged_at_end_row->y < it.last_visible_y
10120 && !last_text_row_at_end)
10121 {
10122 /* Window end line if one of the preserved rows from the current
10123 matrix. Set row to the last row displaying text in current
10124 matrix starting at first_unchanged_at_end_row, after
10125 scrolling. */
10126 xassert (first_unchanged_at_end_row->displays_text_p);
10127 row = find_last_row_displaying_text (w->current_matrix, &it,
10128 first_unchanged_at_end_row);
10129 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10130
10131 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10132 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10133 XSETFASTINT (w->window_end_vpos,
10134 MATRIX_ROW_VPOS (row, w->current_matrix));
10135 }
10136 else if (last_text_row_at_end)
10137 {
10138 XSETFASTINT (w->window_end_pos,
10139 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10140 w->window_end_bytepos
10141 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10142 XSETFASTINT (w->window_end_vpos,
10143 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10144 }
10145 else if (last_text_row)
10146 {
10147 /* We have displayed either to the end of the window or at the
10148 end of the window, i.e. the last row with text is to be found
10149 in the desired matrix. */
10150 XSETFASTINT (w->window_end_pos,
10151 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10152 w->window_end_bytepos
10153 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10154 XSETFASTINT (w->window_end_vpos,
10155 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10156 }
10157 else if (first_unchanged_at_end_row == NULL
10158 && last_text_row == NULL
10159 && last_text_row_at_end == NULL)
10160 {
10161 /* Displayed to end of window, but no line containing text was
10162 displayed. Lines were deleted at the end of the window. */
10163 int vpos;
10164 int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
10165
10166 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10167 if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
10168 && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
10169 || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
10170 && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
10171 break;
12adba34 10172
5f5c8ee5
GM
10173 w->window_end_vpos = make_number (vpos);
10174 }
10175 else
10176 abort ();
10177
10178 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10179 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 10180
5f5c8ee5
GM
10181 /* Record that display has not been completed. */
10182 w->window_end_valid = Qnil;
10183 w->desired_matrix->no_scrolling_p = 1;
10184 return 1;
12adba34 10185}
0f9c0ff0 10186
a2889657 10187
5f5c8ee5
GM
10188\f
10189/***********************************************************************
10190 More debugging support
10191 ***********************************************************************/
a2889657 10192
5f5c8ee5 10193#if GLYPH_DEBUG
a2889657 10194
5f5c8ee5
GM
10195 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10196static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 10197
31b24551 10198
5f5c8ee5
GM
10199/* Dump the contents of glyph matrix MATRIX on stderr. If
10200 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 10201
5f5c8ee5
GM
10202void
10203dump_glyph_matrix (matrix, with_glyphs_p)
10204 struct glyph_matrix *matrix;
10205 int with_glyphs_p;
10206{
efc63ef0 10207 int i;
5f5c8ee5
GM
10208 for (i = 0; i < matrix->nrows; ++i)
10209 dump_glyph_row (matrix, i, with_glyphs_p);
10210}
31b24551 10211
68a37fa8 10212
5f5c8ee5
GM
10213/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10214 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 10215
5f5c8ee5
GM
10216void
10217dump_glyph_row (matrix, vpos, with_glyphs_p)
10218 struct glyph_matrix *matrix;
10219 int vpos, with_glyphs_p;
10220{
10221 struct glyph_row *row;
10222
10223 if (vpos < 0 || vpos >= matrix->nrows)
10224 return;
10225
10226 row = MATRIX_ROW (matrix, vpos);
10227
10228 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10229 fprintf (stderr, "=============================================\n");
10230
10231 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",
10232 row - matrix->rows,
10233 MATRIX_ROW_START_CHARPOS (row),
10234 MATRIX_ROW_END_CHARPOS (row),
10235 row->used[TEXT_AREA],
10236 row->contains_overlapping_glyphs_p,
10237 row->enabled_p,
10238 row->inverse_p,
10239 row->truncated_on_left_p,
10240 row->truncated_on_right_p,
10241 row->overlay_arrow_p,
10242 row->continued_p,
10243 MATRIX_ROW_CONTINUATION_LINE_P (row),
10244 row->displays_text_p,
10245 row->ends_at_zv_p,
10246 row->fill_line_p,
10247 row->x,
10248 row->y,
10249 row->pixel_width);
10250 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10251 row->end.overlay_string_index);
10252 fprintf (stderr, "%9d %5d\n",
10253 CHARPOS (row->start.string_pos),
10254 CHARPOS (row->end.string_pos));
10255 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10256 row->end.dpvec_index);
10257
10258 if (with_glyphs_p)
bd66d1ba 10259 {
5f5c8ee5
GM
10260 struct glyph *glyph, *glyph_end;
10261 int prev_had_glyphs_p;
10262
10263 glyph = row->glyphs[TEXT_AREA];
10264 glyph_end = glyph + row->used[TEXT_AREA];
10265
10266 /* Glyph for a line end in text. */
10267 if (glyph == glyph_end && glyph->charpos > 0)
10268 ++glyph_end;
10269
10270 if (glyph < glyph_end)
bd66d1ba 10271 {
5f5c8ee5
GM
10272 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10273 prev_had_glyphs_p = 1;
bd66d1ba
RS
10274 }
10275 else
5f5c8ee5
GM
10276 prev_had_glyphs_p = 0;
10277
10278 while (glyph < glyph_end)
f7b4b63a 10279 {
5f5c8ee5
GM
10280 if (glyph->type == CHAR_GLYPH)
10281 {
10282 fprintf (stderr,
10283 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10284 glyph - row->glyphs[TEXT_AREA],
10285 'C',
10286 glyph->charpos,
10287 glyph->pixel_width,
10288 glyph->u.ch.code,
10289 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
10290 ? glyph->u.ch.code
10291 : '.'),
10292 glyph->u.ch.face_id,
10293 glyph->left_box_line_p,
10294 glyph->right_box_line_p);
10295 }
10296 else if (glyph->type == STRETCH_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 'S',
10302 glyph->charpos,
10303 glyph->pixel_width,
10304 0,
10305 '.',
10306 glyph->u.stretch.face_id,
10307 glyph->left_box_line_p,
10308 glyph->right_box_line_p);
10309 }
10310 else if (glyph->type == IMAGE_GLYPH)
10311 {
10312 fprintf (stderr,
10313 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10314 glyph - row->glyphs[TEXT_AREA],
10315 'I',
10316 glyph->charpos,
10317 glyph->pixel_width,
10318 glyph->u.img.id,
10319 '.',
10320 glyph->u.img.face_id,
10321 glyph->left_box_line_p,
10322 glyph->right_box_line_p);
10323 }
10324 ++glyph;
f7b4b63a 10325 }
f4faa47c 10326 }
5f5c8ee5 10327}
f4faa47c 10328
a2889657 10329
5f5c8ee5
GM
10330DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10331 Sdump_glyph_matrix, 0, 1, "p",
10332 "Dump the current matrix of the selected window to stderr.\n\
10333Shows contents of glyph row structures. With non-nil optional\n\
10334parameter WITH-GLYPHS-P, dump glyphs as well.")
10335 (with_glyphs_p)
10336{
10337 struct window *w = XWINDOW (selected_window);
10338 struct buffer *buffer = XBUFFER (w->buffer);
10339
10340 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10341 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10342 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10343 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10344 fprintf (stderr, "=============================================\n");
10345 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10346 return Qnil;
10347}
1c2250c2 10348
1fca3fae 10349
5f5c8ee5
GM
10350DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10351 "Dump glyph row ROW to stderr.")
10352 (row)
10353 Lisp_Object row;
10354{
10355 CHECK_NUMBER (row, 0);
10356 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10357 return Qnil;
10358}
1fca3fae 10359
67481ae5 10360
5f5c8ee5
GM
10361DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
10362 0, 0, "", "")
10363 ()
10364{
10365 struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
10366 ->current_matrix);
10367 dump_glyph_row (m, 0, 1);
10368 return Qnil;
10369}
ca26e1c8 10370
0f9c0ff0 10371
5f5c8ee5
GM
10372DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10373 Strace_redisplay_toggle, 0, 0, "",
10374 "Toggle tracing of redisplay.")
10375 ()
10376{
10377 trace_redisplay_p = !trace_redisplay_p;
10378 return Qnil;
10379}
10380
10381
10382#endif /* GLYPH_DEBUG */
ca26e1c8 10383
ca26e1c8 10384
5f5c8ee5
GM
10385\f
10386/***********************************************************************
10387 Building Desired Matrix Rows
10388 ***********************************************************************/
a2889657 10389
5f5c8ee5
GM
10390/* Return a temporary glyph row holding the glyphs of an overlay
10391 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 10392
5f5c8ee5
GM
10393static struct glyph_row *
10394get_overlay_arrow_glyph_row (w)
10395 struct window *w;
10396{
10397 struct frame *f = XFRAME (WINDOW_FRAME (w));
10398 struct buffer *buffer = XBUFFER (w->buffer);
10399 struct buffer *old = current_buffer;
10400 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10401 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10402 unsigned char *arrow_end = arrow_string + arrow_len;
10403 unsigned char *p;
10404 struct it it;
10405 int multibyte_p;
10406 int n_glyphs_before;
10407
10408 set_buffer_temp (buffer);
10409 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10410 it.glyph_row->used[TEXT_AREA] = 0;
10411 SET_TEXT_POS (it.position, 0, 0);
10412
10413 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10414 p = arrow_string;
10415 while (p < arrow_end)
10416 {
10417 Lisp_Object face, ilisp;
10418
10419 /* Get the next character. */
10420 if (multibyte_p)
4fdb80f2 10421 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
10422 else
10423 it.c = *p, it.len = 1;
10424 p += it.len;
10425
10426 /* Get its face. */
10427 XSETFASTINT (ilisp, p - arrow_string);
10428 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10429 it.face_id = compute_char_face (f, it.c, face);
10430
10431 /* Compute its width, get its glyphs. */
10432 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 10433 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
10434 PRODUCE_GLYPHS (&it);
10435
10436 /* If this character doesn't fit any more in the line, we have
10437 to remove some glyphs. */
10438 if (it.current_x > it.last_visible_x)
10439 {
10440 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10441 break;
10442 }
10443 }
10444
10445 set_buffer_temp (old);
10446 return it.glyph_row;
10447}
ca26e1c8 10448
b0a0fbda 10449
5f5c8ee5
GM
10450/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10451 glyphs are only inserted for terminal frames since we can't really
10452 win with truncation glyphs when partially visible glyphs are
10453 involved. Which glyphs to insert is determined by
10454 produce_special_glyphs. */
67481ae5 10455
5f5c8ee5
GM
10456static void
10457insert_left_trunc_glyphs (it)
10458 struct it *it;
10459{
10460 struct it truncate_it;
10461 struct glyph *from, *end, *to, *toend;
10462
10463 xassert (!FRAME_WINDOW_P (it->f));
10464
10465 /* Get the truncation glyphs. */
10466 truncate_it = *it;
10467 truncate_it.charset = -1;
10468 truncate_it.current_x = 0;
10469 truncate_it.face_id = DEFAULT_FACE_ID;
10470 truncate_it.glyph_row = &scratch_glyph_row;
10471 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10472 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10473 truncate_it.object = 0;
10474 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10475
10476 /* Overwrite glyphs from IT with truncation glyphs. */
10477 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10478 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10479 to = it->glyph_row->glyphs[TEXT_AREA];
10480 toend = to + it->glyph_row->used[TEXT_AREA];
10481
10482 while (from < end)
10483 *to++ = *from++;
10484
10485 /* There may be padding glyphs left over. Remove them. */
10486 from = to;
10487 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10488 ++from;
10489 while (from < toend)
10490 *to++ = *from++;
10491
10492 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10493}
e0bfbde6 10494
e0bfbde6 10495
5f5c8ee5 10496/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 10497
5f5c8ee5
GM
10498 Most of the time, ascent and height of a display line will be equal
10499 to the max_ascent and max_height values of the display iterator
10500 structure. This is not the case if
67481ae5 10501
5f5c8ee5
GM
10502 1. We hit ZV without displaying anything. In this case, max_ascent
10503 and max_height will be zero.
1c9241f5 10504
5f5c8ee5
GM
10505 2. We have some glyphs that don't contribute to the line height.
10506 (The glyph row flag contributes_to_line_height_p is for future
10507 pixmap extensions).
f6fd109b 10508
5f5c8ee5
GM
10509 The first case is easily covered by using default values because in
10510 these cases, the line height does not really matter, except that it
10511 must not be zero. */
67481ae5 10512
5f5c8ee5
GM
10513static void
10514compute_line_metrics (it)
10515 struct it *it;
10516{
10517 struct glyph_row *row = it->glyph_row;
10518 int area, i;
1c2250c2 10519
5f5c8ee5
GM
10520 if (FRAME_WINDOW_P (it->f))
10521 {
10522 int i, top_line_height;
1c2250c2 10523
5f5c8ee5
GM
10524 /* The line may consist of one space only, that was added to
10525 place the cursor on it. If so, the row's height hasn't been
10526 computed yet. */
10527 if (row->height == 0)
10528 {
10529 if (it->max_ascent + it->max_descent == 0)
312246d1 10530 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
10531 row->ascent = it->max_ascent;
10532 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10533 row->phys_ascent = it->max_phys_ascent;
10534 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10535 }
10536
10537 /* Compute the width of this line. */
10538 row->pixel_width = row->x;
10539 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10540 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10541
10542 xassert (row->pixel_width >= 0);
10543 xassert (row->ascent >= 0 && row->height > 0);
10544
312246d1
GM
10545 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10546 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10547
10548 /* If first line's physical ascent is larger than its logical
10549 ascent, use the physical ascent, and make the row taller.
10550 This makes accented characters fully visible. */
10551 if (row == it->w->desired_matrix->rows
10552 && row->phys_ascent > row->ascent)
10553 {
10554 row->height += row->phys_ascent - row->ascent;
10555 row->ascent = row->phys_ascent;
10556 }
10557
5f5c8ee5
GM
10558 /* Compute how much of the line is visible. */
10559 row->visible_height = row->height;
10560
10561 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
10562 if (row->y < top_line_height)
10563 row->visible_height -= top_line_height - row->y;
10564 else
10565 {
10566 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10567 if (row->y + row->height > max_y)
10568 row->visible_height -= row->y + row->height - max_y;
10569 }
10570 }
10571 else
10572 {
10573 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
10574 row->ascent = row->phys_ascent = 0;
10575 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 10576 }
67481ae5 10577
5f5c8ee5
GM
10578 /* Compute a hash code for this row. */
10579 row->hash = 0;
10580 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10581 for (i = 0; i < row->used[area]; ++i)
10582 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10583 + row->glyphs[area][i].u.val
10584 + (row->glyphs[area][i].type << 2));
a2889657 10585
5f5c8ee5 10586 it->max_ascent = it->max_descent = 0;
312246d1 10587 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 10588}
12adba34 10589
ca26e1c8 10590
5f5c8ee5
GM
10591/* Append one space to the glyph row of iterator IT if doing a
10592 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10593 space have the default face, otherwise let it have the same face as
c6e89d6c
GM
10594 IT->face_id.
10595
10596 This function is called to make sure that there is always one glyph
10597 at the end of a glyph row that the cursor can be set on under
10598 window-systems. (If there weren't such a glyph we would not know
10599 how wide and tall a box cursor should be displayed).
10600
10601 At the same time this space let's a nicely handle clearing to the
10602 end of the line if the row ends in italic text. */
ca26e1c8 10603
5f5c8ee5
GM
10604static void
10605append_space (it, default_face_p)
10606 struct it *it;
10607 int default_face_p;
10608{
10609 if (FRAME_WINDOW_P (it->f))
10610 {
10611 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 10612
5f5c8ee5
GM
10613 if (it->glyph_row->glyphs[TEXT_AREA] + n
10614 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 10615 {
5f5c8ee5
GM
10616 /* Save some values that must not be changed. */
10617 int saved_x = it->current_x;
10618 struct text_pos saved_pos;
10619 int saved_what = it->what;
10620 int saved_face_id = it->face_id;
10621 int saved_charset = it->charset;
10622 Lisp_Object saved_object;
10623
10624 saved_object = it->object;
10625 saved_pos = it->position;
10626
10627 it->what = IT_CHARACTER;
10628 bzero (&it->position, sizeof it->position);
10629 it->object = 0;
10630 it->c = ' ';
10631 it->len = 1;
10632 it->charset = CHARSET_ASCII;
10633
10634 if (default_face_p)
10635 it->face_id = DEFAULT_FACE_ID;
10636 if (it->multibyte_p)
10637 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10638 else
10639 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
1842fc1a 10640
5f5c8ee5
GM
10641 PRODUCE_GLYPHS (it);
10642
10643 it->current_x = saved_x;
10644 it->object = saved_object;
10645 it->position = saved_pos;
10646 it->what = saved_what;
10647 it->face_id = saved_face_id;
10648 it->charset = saved_charset;
10649 }
10650 }
10651}
12adba34 10652
1842fc1a 10653
5f5c8ee5
GM
10654/* Extend the face of the last glyph in the text area of IT->glyph_row
10655 to the end of the display line. Called from display_line.
10656 If the glyph row is empty, add a space glyph to it so that we
10657 know the face to draw. Set the glyph row flag fill_line_p. */
10658
10659static void
10660extend_face_to_end_of_line (it)
10661 struct it *it;
10662{
10663 struct face *face;
10664 struct frame *f = it->f;
1842fc1a 10665
5f5c8ee5
GM
10666 /* If line is already filled, do nothing. */
10667 if (it->current_x >= it->last_visible_x)
10668 return;
10669
10670 /* Face extension extends the background and box of IT->face_id
10671 to the end of the line. If the background equals the background
10672 of the frame, we haven't to do anything. */
10673 face = FACE_FROM_ID (f, it->face_id);
10674 if (FRAME_WINDOW_P (f)
10675 && face->box == FACE_NO_BOX
10676 && face->background == FRAME_BACKGROUND_PIXEL (f)
10677 && !face->stipple)
10678 return;
1842fc1a 10679
5f5c8ee5
GM
10680 /* Set the glyph row flag indicating that the face of the last glyph
10681 in the text area has to be drawn to the end of the text area. */
10682 it->glyph_row->fill_line_p = 1;
545e04f6 10683
5f5c8ee5
GM
10684 /* If current charset of IT is not ASCII, make sure we have the
10685 ASCII face. This will be automatically undone the next time
10686 get_next_display_element returns a character from a different
10687 charset. Note that the charset will always be ASCII in unibyte
10688 text. */
10689 if (it->charset != CHARSET_ASCII)
10690 {
10691 it->charset = CHARSET_ASCII;
10692 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10693 }
545e04f6 10694
5f5c8ee5
GM
10695 if (FRAME_WINDOW_P (f))
10696 {
10697 /* If the row is empty, add a space with the current face of IT,
10698 so that we know which face to draw. */
10699 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 10700 {
5f5c8ee5
GM
10701 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10702 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10703 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 10704 }
5f5c8ee5
GM
10705 }
10706 else
10707 {
10708 /* Save some values that must not be changed. */
10709 int saved_x = it->current_x;
10710 struct text_pos saved_pos;
10711 Lisp_Object saved_object;
10712 int saved_what = it->what;
10713
10714 saved_object = it->object;
10715 saved_pos = it->position;
10716
10717 it->what = IT_CHARACTER;
10718 bzero (&it->position, sizeof it->position);
10719 it->object = 0;
10720 it->c = ' ';
10721 it->len = 1;
10722
10723 PRODUCE_GLYPHS (it);
10724
10725 while (it->current_x <= it->last_visible_x)
10726 PRODUCE_GLYPHS (it);
10727
10728 /* Don't count these blanks really. It would let us insert a left
10729 truncation glyph below and make us set the cursor on them, maybe. */
10730 it->current_x = saved_x;
10731 it->object = saved_object;
10732 it->position = saved_pos;
10733 it->what = saved_what;
10734 }
10735}
12adba34 10736
545e04f6 10737
5f5c8ee5
GM
10738/* Value is non-zero if text starting at CHARPOS in current_buffer is
10739 trailing whitespace. */
1c9241f5 10740
5f5c8ee5
GM
10741static int
10742trailing_whitespace_p (charpos)
10743 int charpos;
10744{
10745 int bytepos = CHAR_TO_BYTE (charpos);
10746 int c = 0;
7bbe686f 10747
5f5c8ee5
GM
10748 while (bytepos < ZV_BYTE
10749 && (c = FETCH_CHAR (bytepos),
10750 c == ' ' || c == '\t'))
10751 ++bytepos;
0d09d1e6 10752
8f897821
GM
10753 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
10754 {
10755 if (bytepos != PT_BYTE)
10756 return 1;
10757 }
10758 return 0;
5f5c8ee5 10759}
31b24551 10760
545e04f6 10761
5f5c8ee5 10762/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 10763
5f5c8ee5
GM
10764void
10765highlight_trailing_whitespace (f, row)
10766 struct frame *f;
10767 struct glyph_row *row;
10768{
10769 int used = row->used[TEXT_AREA];
10770
10771 if (used)
10772 {
10773 struct glyph *start = row->glyphs[TEXT_AREA];
10774 struct glyph *glyph = start + used - 1;
10775
10776 /* Skip over the space glyph inserted to display the
10777 cursor at the end of a line. */
10778 if (glyph->type == CHAR_GLYPH
10779 && glyph->u.ch.code == ' '
10780 && glyph->object == 0)
10781 --glyph;
10782
10783 /* If last glyph is a space or stretch, and it's trailing
10784 whitespace, set the face of all trailing whitespace glyphs in
10785 IT->glyph_row to `trailing-whitespace'. */
10786 if (glyph >= start
10787 && BUFFERP (glyph->object)
10788 && (glyph->type == STRETCH_GLYPH
10789 || (glyph->type == CHAR_GLYPH
10790 && glyph->u.ch.code == ' '))
10791 && trailing_whitespace_p (glyph->charpos))
545e04f6 10792 {
5f5c8ee5
GM
10793 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10794 CHARSET_ASCII);
10795
10796 while (glyph >= start
10797 && BUFFERP (glyph->object)
10798 && (glyph->type == STRETCH_GLYPH
10799 || (glyph->type == CHAR_GLYPH
10800 && glyph->u.ch.code == ' ')))
545e04f6 10801 {
5f5c8ee5
GM
10802 if (glyph->type == STRETCH_GLYPH)
10803 glyph->u.stretch.face_id = face_id;
10804 else
10805 glyph->u.ch.face_id = face_id;
10806 --glyph;
545e04f6
KH
10807 }
10808 }
a2889657 10809 }
5f5c8ee5 10810}
a2889657 10811
5fcbb24d 10812
5f5c8ee5
GM
10813/* Construct the glyph row IT->glyph_row in the desired matrix of
10814 IT->w from text at the current position of IT. See dispextern.h
10815 for an overview of struct it. Value is non-zero if
10816 IT->glyph_row displays text, as opposed to a line displaying ZV
10817 only. */
10818
10819static int
10820display_line (it)
10821 struct it *it;
10822{
10823 struct glyph_row *row = it->glyph_row;
10824
10825 /* We always start displaying at hpos zero even if hscrolled. */
10826 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 10827
5f5c8ee5
GM
10828 /* We must not display in a row that's not a text row. */
10829 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10830 < it->w->desired_matrix->nrows);
12adba34 10831
5f5c8ee5
GM
10832 /* Is IT->w showing the region? */
10833 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 10834
5f5c8ee5
GM
10835 /* Clear the result glyph row and enable it. */
10836 prepare_desired_row (row);
12adba34 10837
5f5c8ee5
GM
10838 row->y = it->current_y;
10839 row->start = it->current;
10840 row->continuation_lines_width = it->continuation_lines_width;
10841 row->displays_text_p = 1;
10842
10843 /* Arrange the overlays nicely for our purposes. Usually, we call
10844 display_line on only one line at a time, in which case this
10845 can't really hurt too much, or we call it on lines which appear
10846 one after another in the buffer, in which case all calls to
10847 recenter_overlay_lists but the first will be pretty cheap. */
10848 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10849
5f5c8ee5
GM
10850 /* Move over display elements that are not visible because we are
10851 hscrolled. This may stop at an x-position < IT->first_visible_x
10852 if the first glyph is partially visible or if we hit a line end. */
10853 if (it->current_x < it->first_visible_x)
10854 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10855 MOVE_TO_POS | MOVE_TO_X);
10856
10857 /* Get the initial row height. This is either the height of the
10858 text hscrolled, if there is any, or zero. */
10859 row->ascent = it->max_ascent;
10860 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10861 row->phys_ascent = it->max_phys_ascent;
10862 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10863
10864 /* Loop generating characters. The loop is left with IT on the next
10865 character to display. */
10866 while (1)
10867 {
10868 int n_glyphs_before, hpos_before, x_before;
10869 int x, i, nglyphs;
10870
10871 /* Retrieve the next thing to display. Value is zero if end of
10872 buffer reached. */
10873 if (!get_next_display_element (it))
10874 {
10875 /* Maybe add a space at the end of this line that is used to
10876 display the cursor there under X. */
10877 append_space (it, 1);
10878
10879 /* The position -1 below indicates a blank line not
10880 corresponding to any text, as opposed to an empty line
10881 corresponding to a line end. */
10882 if (row->used[TEXT_AREA] <= 1)
a2889657 10883 {
5f5c8ee5
GM
10884 row->glyphs[TEXT_AREA]->charpos = -1;
10885 row->displays_text_p = 0;
10886
10887 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
10888 row->indicate_empty_line_p = 1;
a2889657 10889 }
5f5c8ee5
GM
10890
10891 it->continuation_lines_width = 0;
10892 row->ends_at_zv_p = 1;
10893 break;
a2889657 10894 }
a2889657 10895
5f5c8ee5
GM
10896 /* Now, get the metrics of what we want to display. This also
10897 generates glyphs in `row' (which is IT->glyph_row). */
10898 n_glyphs_before = row->used[TEXT_AREA];
10899 x = it->current_x;
10900 PRODUCE_GLYPHS (it);
a2889657 10901
5f5c8ee5
GM
10902 /* If this display element was in marginal areas, continue with
10903 the next one. */
10904 if (it->area != TEXT_AREA)
a2889657 10905 {
5f5c8ee5
GM
10906 row->ascent = max (row->ascent, it->max_ascent);
10907 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10908 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10909 row->phys_height = max (row->phys_height,
10910 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10911 set_iterator_to_next (it);
10912 continue;
10913 }
5936754e 10914
5f5c8ee5
GM
10915 /* Does the display element fit on the line? If we truncate
10916 lines, we should draw past the right edge of the window. If
10917 we don't truncate, we want to stop so that we can display the
10918 continuation glyph before the right margin. If lines are
10919 continued, there are two possible strategies for characters
10920 resulting in more than 1 glyph (e.g. tabs): Display as many
10921 glyphs as possible in this line and leave the rest for the
10922 continuation line, or display the whole element in the next
10923 line. Original redisplay did the former, so we do it also. */
10924 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10925 hpos_before = it->hpos;
10926 x_before = x;
10927
10928 if (nglyphs == 1
10929 && it->current_x < it->last_visible_x)
10930 {
10931 ++it->hpos;
10932 row->ascent = max (row->ascent, it->max_ascent);
10933 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10934 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10935 row->phys_height = max (row->phys_height,
10936 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10937 if (it->current_x - it->pixel_width < it->first_visible_x)
10938 row->x = x - it->first_visible_x;
10939 }
10940 else
10941 {
10942 int new_x;
10943 struct glyph *glyph;
10944
10945 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 10946 {
5f5c8ee5
GM
10947 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10948 new_x = x + glyph->pixel_width;
10949
10950 if (/* Lines are continued. */
10951 !it->truncate_lines_p
10952 && (/* Glyph doesn't fit on the line. */
10953 new_x > it->last_visible_x
10954 /* Or it fits exactly on a window system frame. */
10955 || (new_x == it->last_visible_x
10956 && FRAME_WINDOW_P (it->f))))
a2889657 10957 {
5f5c8ee5
GM
10958 /* End of a continued line. */
10959
10960 if (it->hpos == 0
10961 || (new_x == it->last_visible_x
10962 && FRAME_WINDOW_P (it->f)))
10963 {
10964 /* Current glyph fits exactly on the line. We
10965 must continue the line because we can't draw
10966 the cursor after the glyph. */
10967 row->continued_p = 1;
10968 it->current_x = new_x;
10969 it->continuation_lines_width += new_x;
10970 ++it->hpos;
10971 if (i == nglyphs - 1)
10972 set_iterator_to_next (it);
10973 }
10974 else
5936754e 10975 {
5f5c8ee5
GM
10976 /* Display element draws past the right edge of
10977 the window. Restore positions to values
10978 before the element. The next line starts
10979 with current_x before the glyph that could
10980 not be displayed, so that TAB works right. */
10981 row->used[TEXT_AREA] = n_glyphs_before + i;
10982
10983 /* Display continuation glyphs. */
10984 if (!FRAME_WINDOW_P (it->f))
10985 produce_special_glyphs (it, IT_CONTINUATION);
10986 row->continued_p = 1;
10987
10988 it->current_x = x;
10989 it->continuation_lines_width += x;
5936754e 10990 }
5f5c8ee5
GM
10991 break;
10992 }
10993 else if (new_x > it->first_visible_x)
10994 {
10995 /* Increment number of glyphs actually displayed. */
10996 ++it->hpos;
10997
10998 if (x < it->first_visible_x)
10999 /* Glyph is partially visible, i.e. row starts at
11000 negative X position. */
11001 row->x = x - it->first_visible_x;
11002 }
11003 else
11004 {
11005 /* Glyph is completely off the left margin of the
11006 window. This should not happen because of the
11007 move_it_in_display_line at the start of
11008 this function. */
11009 abort ();
a2889657 11010 }
a2889657 11011 }
5f5c8ee5
GM
11012
11013 row->ascent = max (row->ascent, it->max_ascent);
11014 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11015 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11016 row->phys_height = max (row->phys_height,
11017 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11018
11019 /* End of this display line if row is continued. */
11020 if (row->continued_p)
11021 break;
a2889657 11022 }
a2889657 11023
5f5c8ee5
GM
11024 /* Is this a line end? If yes, we're also done, after making
11025 sure that a non-default face is extended up to the right
11026 margin of the window. */
11027 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 11028 {
5f5c8ee5
GM
11029 int used_before = row->used[TEXT_AREA];
11030
11031 /* Add a space at the end of the line that is used to
11032 display the cursor there. */
11033 append_space (it, 0);
11034
11035 /* Extend the face to the end of the line. */
11036 extend_face_to_end_of_line (it);
11037
11038 /* Make sure we have the position. */
11039 if (used_before == 0)
11040 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11041
11042 /* Consume the line end. This skips over invisible lines. */
11043 set_iterator_to_next (it);
11044 it->continuation_lines_width = 0;
11045 break;
1c9241f5 11046 }
a2889657 11047
5f5c8ee5
GM
11048 /* Proceed with next display element. Note that this skips
11049 over lines invisible because of selective display. */
11050 set_iterator_to_next (it);
b1d1124b 11051
5f5c8ee5
GM
11052 /* If we truncate lines, we are done when the last displayed
11053 glyphs reach past the right margin of the window. */
11054 if (it->truncate_lines_p
11055 && (FRAME_WINDOW_P (it->f)
11056 ? (it->current_x >= it->last_visible_x)
11057 : (it->current_x > it->last_visible_x)))
75d13c64 11058 {
5f5c8ee5
GM
11059 /* Maybe add truncation glyphs. */
11060 if (!FRAME_WINDOW_P (it->f))
11061 {
11062 --it->glyph_row->used[TEXT_AREA];
11063 produce_special_glyphs (it, IT_TRUNCATION);
11064 }
11065
11066 row->truncated_on_right_p = 1;
11067 it->continuation_lines_width = 0;
312246d1 11068 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
11069 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11070 it->hpos = hpos_before;
11071 it->current_x = x_before;
11072 break;
75d13c64 11073 }
a2889657 11074 }
a2889657 11075
5f5c8ee5
GM
11076 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11077 at the left window margin. */
11078 if (it->first_visible_x
11079 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11080 {
11081 if (!FRAME_WINDOW_P (it->f))
11082 insert_left_trunc_glyphs (it);
11083 row->truncated_on_left_p = 1;
11084 }
a2889657 11085
5f5c8ee5
GM
11086 /* If the start of this line is the overlay arrow-position, then
11087 mark this glyph row as the one containing the overlay arrow.
11088 This is clearly a mess with variable size fonts. It would be
11089 better to let it be displayed like cursors under X. */
e24c997d 11090 if (MARKERP (Voverlay_arrow_position)
a2889657 11091 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
11092 && (MATRIX_ROW_START_CHARPOS (row)
11093 == marker_position (Voverlay_arrow_position))
e24c997d 11094 && STRINGP (Voverlay_arrow_string)
a2889657
JB
11095 && ! overlay_arrow_seen)
11096 {
5f5c8ee5
GM
11097 /* Overlay arrow in window redisplay is a bitmap. */
11098 if (!FRAME_WINDOW_P (it->f))
c4628384 11099 {
5f5c8ee5
GM
11100 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11101 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11102 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11103 struct glyph *p = row->glyphs[TEXT_AREA];
11104 struct glyph *p2, *end;
11105
11106 /* Copy the arrow glyphs. */
11107 while (glyph < arrow_end)
11108 *p++ = *glyph++;
11109
11110 /* Throw away padding glyphs. */
11111 p2 = p;
11112 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11113 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11114 ++p2;
11115 if (p2 > p)
212e4f87 11116 {
5f5c8ee5
GM
11117 while (p2 < end)
11118 *p++ = *p2++;
11119 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 11120 }
c4628384 11121 }
5f5c8ee5 11122
a2889657 11123 overlay_arrow_seen = 1;
5f5c8ee5 11124 row->overlay_arrow_p = 1;
a2889657
JB
11125 }
11126
5f5c8ee5
GM
11127 /* Compute pixel dimensions of this line. */
11128 compute_line_metrics (it);
11129
11130 /* Remember the position at which this line ends. */
11131 row->end = it->current;
11132
11133 /* Maybe set the cursor. If you change this, it's probably a good
11134 idea to also change the code in redisplay_window for cursor
11135 movement in an unchanged window. */
11136 if (it->w->cursor.vpos < 0
11137 && PT >= MATRIX_ROW_START_CHARPOS (row)
11138 && MATRIX_ROW_END_CHARPOS (row) >= PT
11139 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11140 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11141 || !row->ends_at_zv_p)))
11142 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11143
11144 /* Highlight trailing whitespace. */
8f897821 11145 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
11146 highlight_trailing_whitespace (it->f, it->glyph_row);
11147
11148 /* Prepare for the next line. This line starts horizontally at (X
11149 HPOS) = (0 0). Vertical positions are incremented. As a
11150 convenience for the caller, IT->glyph_row is set to the next
11151 row to be used. */
11152 it->current_x = it->hpos = 0;
11153 it->current_y += row->height;
11154 ++it->vpos;
11155 ++it->glyph_row;
11156 return row->displays_text_p;
a2889657 11157}
5f5c8ee5
GM
11158
11159
a2889657 11160\f
5f5c8ee5
GM
11161/***********************************************************************
11162 Menu Bar
11163 ***********************************************************************/
11164
11165/* Redisplay the menu bar in the frame for window W.
11166
11167 The menu bar of X frames that don't have X toolkit support is
11168 displayed in a special window W->frame->menu_bar_window.
11169
11170 The menu bar of terminal frames is treated specially as far as
11171 glyph matrices are concerned. Menu bar lines are not part of
11172 windows, so the update is done directly on the frame matrix rows
11173 for the menu bar. */
7ce2c095
RS
11174
11175static void
11176display_menu_bar (w)
11177 struct window *w;
11178{
5f5c8ee5
GM
11179 struct frame *f = XFRAME (WINDOW_FRAME (w));
11180 struct it it;
11181 Lisp_Object items;
8351baf2 11182 int i;
7ce2c095 11183
5f5c8ee5 11184 /* Don't do all this for graphical frames. */
dc937613 11185#ifdef HAVE_NTGUI
d129c4c2
KH
11186 if (!NILP (Vwindow_system))
11187 return;
dc937613 11188#endif
dc937613 11189#ifdef USE_X_TOOLKIT
d3413a53 11190 if (FRAME_X_P (f))
7ce2c095 11191 return;
5f5c8ee5
GM
11192#endif
11193
11194#ifdef USE_X_TOOLKIT
11195 xassert (!FRAME_WINDOW_P (f));
11196 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
11197 it.first_visible_x = 0;
11198 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11199#else /* not USE_X_TOOLKIT */
11200 if (FRAME_WINDOW_P (f))
11201 {
11202 /* Menu bar lines are displayed in the desired matrix of the
11203 dummy window menu_bar_window. */
11204 struct window *menu_w;
11205 xassert (WINDOWP (f->menu_bar_window));
11206 menu_w = XWINDOW (f->menu_bar_window);
11207 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11208 MODE_LINE_FACE_ID);
11209 it.first_visible_x = 0;
11210 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11211 }
11212 else
11213 {
11214 /* This is a TTY frame, i.e. character hpos/vpos are used as
11215 pixel x/y. */
11216 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11217 MODE_LINE_FACE_ID);
11218 it.first_visible_x = 0;
11219 it.last_visible_x = FRAME_WIDTH (f);
11220 }
11221#endif /* not USE_X_TOOLKIT */
11222
11223 /* Clear all rows of the menu bar. */
11224 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11225 {
11226 struct glyph_row *row = it.glyph_row + i;
11227 clear_glyph_row (row);
11228 row->enabled_p = 1;
11229 row->full_width_p = 1;
11230 }
7ce2c095 11231
5f5c8ee5
GM
11232 /* Make the first line of the menu bar appear in reverse video. */
11233 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 11234
5f5c8ee5
GM
11235 /* Display all items of the menu bar. */
11236 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 11237 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 11238 {
5f5c8ee5
GM
11239 Lisp_Object string;
11240
11241 /* Stop at nil string. */
8351baf2
RS
11242 string = XVECTOR (items)->contents[i + 1];
11243 if (NILP (string))
11244 break;
2d66ad19 11245
5f5c8ee5
GM
11246 /* Remember where item was displayed. */
11247 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 11248
5f5c8ee5
GM
11249 /* Display the item, pad with one space. */
11250 if (it.current_x < it.last_visible_x)
11251 display_string (NULL, string, Qnil, 0, 0, &it,
11252 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
11253 }
11254
2d66ad19 11255 /* Fill out the line with spaces. */
5f5c8ee5
GM
11256 if (it.current_x < it.last_visible_x)
11257 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 11258
5f5c8ee5
GM
11259 /* Compute the total height of the lines. */
11260 compute_line_metrics (&it);
7ce2c095 11261}
5f5c8ee5
GM
11262
11263
7ce2c095 11264\f
5f5c8ee5
GM
11265/***********************************************************************
11266 Mode Line
11267 ***********************************************************************/
11268
11269/* Display the mode and/or top line of window W. */
a2889657
JB
11270
11271static void
5f5c8ee5 11272display_mode_lines (w)
a2889657
JB
11273 struct window *w;
11274{
5f5c8ee5 11275 /* These will be set while the mode line specs are processed. */
aa6d10fa 11276 line_number_displayed = 0;
155ef550 11277 w->column_number_displayed = Qnil;
aa6d10fa 11278
5f5c8ee5
GM
11279 if (WINDOW_WANTS_MODELINE_P (w))
11280 display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
11281
11282 if (WINDOW_WANTS_TOP_LINE_P (w))
11283 display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
11284}
03b294dc 11285
03b294dc 11286
5f5c8ee5
GM
11287/* Display mode or top line of window W. FACE_ID specifies which line
11288 to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
11289 FORMAT is the mode line format to display. */
03b294dc 11290
5f5c8ee5
GM
11291static void
11292display_mode_line (w, face_id, format)
11293 struct window *w;
11294 enum face_id face_id;
11295 Lisp_Object format;
11296{
11297 struct it it;
11298 struct face *face;
03b294dc 11299
5f5c8ee5
GM
11300 init_iterator (&it, w, -1, -1, NULL, face_id);
11301 prepare_desired_row (it.glyph_row);
11302
11303 /* Temporarily make frame's keyboard the current kboard so that
11304 kboard-local variables in the mode_line_format will get the right
11305 values. */
11306 push_frame_kboard (it.f);
11307 display_mode_element (&it, 0, 0, 0, format);
11308 pop_frame_kboard ();
a2889657 11309
5f5c8ee5
GM
11310 /* Fill up with spaces. */
11311 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11312
11313 compute_line_metrics (&it);
11314 it.glyph_row->full_width_p = 1;
11315 it.glyph_row->mode_line_p = 1;
11316 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11317 it.glyph_row->continued_p = 0;
11318 it.glyph_row->truncated_on_left_p = 0;
11319 it.glyph_row->truncated_on_right_p = 0;
11320
11321 /* Make a 3D mode-line have a shadow at its right end. */
11322 face = FACE_FROM_ID (it.f, face_id);
11323 extend_face_to_end_of_line (&it);
11324 if (face->box != FACE_NO_BOX)
d7eb09a0 11325 {
5f5c8ee5
GM
11326 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11327 + it.glyph_row->used[TEXT_AREA] - 1);
11328 last->right_box_line_p = 1;
d7eb09a0 11329 }
a2889657
JB
11330}
11331
a2889657 11332
5f5c8ee5
GM
11333/* Contribute ELT to the mode line for window IT->w. How it
11334 translates into text depends on its data type.
a2889657 11335
5f5c8ee5 11336 IT describes the display environment in which we display, as usual.
a2889657
JB
11337
11338 DEPTH is the depth in recursion. It is used to prevent
11339 infinite recursion here.
11340
5f5c8ee5
GM
11341 FIELD_WIDTH is the number of characters the display of ELT should
11342 occupy in the mode line, and PRECISION is the maximum number of
11343 characters to display from ELT's representation. See
11344 display_string for details. *
a2889657 11345
5f5c8ee5 11346 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
11347
11348static int
5f5c8ee5
GM
11349display_mode_element (it, depth, field_width, precision, elt)
11350 struct it *it;
a2889657 11351 int depth;
5f5c8ee5
GM
11352 int field_width, precision;
11353 Lisp_Object elt;
a2889657 11354{
5f5c8ee5
GM
11355 int n = 0, field, prec;
11356
a2889657
JB
11357 tail_recurse:
11358 if (depth > 10)
11359 goto invalid;
11360
11361 depth++;
11362
0220c518 11363 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
11364 {
11365 case Lisp_String:
11366 {
11367 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
11368 unsigned char c;
11369 unsigned char *this = XSTRING (elt)->data;
11370 unsigned char *lisp_string = this;
11371
11372 while ((precision <= 0 || n < precision)
11373 && *this
11374 && (frame_title_ptr
11375 || it->current_x < it->last_visible_x))
a2889657
JB
11376 {
11377 unsigned char *last = this;
5f5c8ee5
GM
11378
11379 /* Advance to end of string or next format specifier. */
a2889657
JB
11380 while ((c = *this++) != '\0' && c != '%')
11381 ;
5f5c8ee5 11382
a2889657
JB
11383 if (this - 1 != last)
11384 {
5f5c8ee5
GM
11385 /* Output to end of string or up to '%'. Field width
11386 is length of string. Don't output more than
11387 PRECISION allows us. */
11388 prec = --this - last;
11389 if (precision > 0 && prec > precision - n)
11390 prec = precision - n;
11391
d39b6696 11392 if (frame_title_ptr)
5f5c8ee5 11393 n += store_frame_title (last, prec, prec);
d39b6696 11394 else
5f5c8ee5
GM
11395 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11396 it, 0, prec, 0, -1);
a2889657
JB
11397 }
11398 else /* c == '%' */
11399 {
5f5c8ee5
GM
11400 unsigned char *percent_position = this;
11401
11402 /* Get the specified minimum width. Zero means
11403 don't pad. */
11404 field = 0;
a2889657 11405 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 11406 field = field * 10 + c - '0';
a2889657 11407
5f5c8ee5
GM
11408 /* Don't pad beyond the total padding allowed. */
11409 if (field_width - n > 0 && field > field_width - n)
11410 field = field_width - n;
a2889657 11411
5f5c8ee5
GM
11412 /* Note that either PRECISION <= 0 or N < PRECISION. */
11413 prec = precision - n;
11414
a2889657 11415 if (c == 'M')
5f5c8ee5
GM
11416 n += display_mode_element (it, depth, field, prec,
11417 Vglobal_mode_string);
a2889657 11418 else if (c != 0)
d39b6696 11419 {
5f5c8ee5
GM
11420 unsigned char *spec
11421 = decode_mode_spec (it->w, c, field, prec);
11422
d39b6696 11423 if (frame_title_ptr)
5f5c8ee5 11424 n += store_frame_title (spec, field, prec);
d39b6696 11425 else
5f5c8ee5
GM
11426 {
11427 int nglyphs_before
11428 = it->glyph_row->used[TEXT_AREA];
11429 int charpos
11430 = percent_position - XSTRING (elt)->data;
11431 int nwritten
11432 = display_string (spec, Qnil, elt, charpos, 0, it,
11433 field, prec, 0, -1);
11434
11435 /* Assign to the glyphs written above the
11436 string where the `%x' came from, position
11437 of the `%'. */
11438 if (nwritten > 0)
11439 {
11440 struct glyph *glyph
11441 = (it->glyph_row->glyphs[TEXT_AREA]
11442 + nglyphs_before);
11443 int i;
11444
11445 for (i = 0; i < nwritten; ++i)
11446 {
11447 glyph[i].object = elt;
11448 glyph[i].charpos = charpos;
11449 }
11450
11451 n += nwritten;
11452 }
11453 }
d39b6696 11454 }
a2889657
JB
11455 }
11456 }
11457 }
11458 break;
11459
11460 case Lisp_Symbol:
11461 /* A symbol: process the value of the symbol recursively
11462 as if it appeared here directly. Avoid error if symbol void.
11463 Special case: if value of symbol is a string, output the string
11464 literally. */
11465 {
11466 register Lisp_Object tem;
11467 tem = Fboundp (elt);
265a9e55 11468 if (!NILP (tem))
a2889657
JB
11469 {
11470 tem = Fsymbol_value (elt);
11471 /* If value is a string, output that string literally:
11472 don't check for % within it. */
e24c997d 11473 if (STRINGP (tem))
d39b6696 11474 {
5f5c8ee5
GM
11475 prec = XSTRING (tem)->size;
11476 if (precision > 0 && prec > precision - n)
11477 prec = precision - n;
d39b6696 11478 if (frame_title_ptr)
5f5c8ee5 11479 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 11480 else
5f5c8ee5
GM
11481 n += display_string (NULL, tem, Qnil, 0, 0, it,
11482 0, prec, 0, -1);
d39b6696 11483 }
a2889657 11484 else if (!EQ (tem, elt))
5f5c8ee5
GM
11485 {
11486 /* Give up right away for nil or t. */
11487 elt = tem;
11488 goto tail_recurse;
11489 }
a2889657
JB
11490 }
11491 }
11492 break;
11493
11494 case Lisp_Cons:
11495 {
11496 register Lisp_Object car, tem;
11497
11498 /* A cons cell: three distinct cases.
11499 If first element is a string or a cons, process all the elements
11500 and effectively concatenate them.
11501 If first element is a negative number, truncate displaying cdr to
11502 at most that many characters. If positive, pad (with spaces)
11503 to at least that many characters.
11504 If first element is a symbol, process the cadr or caddr recursively
11505 according to whether the symbol's value is non-nil or nil. */
11506 car = XCONS (elt)->car;
5f5c8ee5
GM
11507 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11508 {
11509 /* An element of the form (:eval FORM) means evaluate FORM
11510 and use the result as mode line elements. */
11511 struct gcpro gcpro1;
11512 Lisp_Object spec;
11513
11514 spec = eval_form (XCAR (XCDR (elt)));
11515 GCPRO1 (spec);
11516 n += display_mode_element (it, depth, field_width - n,
11517 precision - n, spec);
11518 UNGCPRO;
11519 }
11520 else if (SYMBOLP (car))
a2889657
JB
11521 {
11522 tem = Fboundp (car);
11523 elt = XCONS (elt)->cdr;
e24c997d 11524 if (!CONSP (elt))
a2889657
JB
11525 goto invalid;
11526 /* elt is now the cdr, and we know it is a cons cell.
11527 Use its car if CAR has a non-nil value. */
265a9e55 11528 if (!NILP (tem))
a2889657
JB
11529 {
11530 tem = Fsymbol_value (car);
265a9e55 11531 if (!NILP (tem))
a2889657
JB
11532 { elt = XCONS (elt)->car; goto tail_recurse; }
11533 }
11534 /* Symbol's value is nil (or symbol is unbound)
11535 Get the cddr of the original list
11536 and if possible find the caddr and use that. */
11537 elt = XCONS (elt)->cdr;
265a9e55 11538 if (NILP (elt))
a2889657 11539 break;
e24c997d 11540 else if (!CONSP (elt))
a2889657
JB
11541 goto invalid;
11542 elt = XCONS (elt)->car;
11543 goto tail_recurse;
11544 }
e24c997d 11545 else if (INTEGERP (car))
a2889657
JB
11546 {
11547 register int lim = XINT (car);
11548 elt = XCONS (elt)->cdr;
11549 if (lim < 0)
5f5c8ee5
GM
11550 {
11551 /* Negative int means reduce maximum width. */
11552 if (precision <= 0)
11553 precision = -lim;
11554 else
11555 precision = min (precision, -lim);
11556 }
a2889657
JB
11557 else if (lim > 0)
11558 {
11559 /* Padding specified. Don't let it be more than
11560 current maximum. */
5f5c8ee5
GM
11561 if (precision > 0)
11562 lim = min (precision, lim);
11563
a2889657
JB
11564 /* If that's more padding than already wanted, queue it.
11565 But don't reduce padding already specified even if
11566 that is beyond the current truncation point. */
5f5c8ee5 11567 field_width = max (lim, field_width);
a2889657
JB
11568 }
11569 goto tail_recurse;
11570 }
e24c997d 11571 else if (STRINGP (car) || CONSP (car))
a2889657
JB
11572 {
11573 register int limit = 50;
5f5c8ee5
GM
11574 /* Limit is to protect against circular lists. */
11575 while (CONSP (elt)
11576 && --limit > 0
11577 && (precision <= 0 || n < precision))
a2889657 11578 {
5f5c8ee5
GM
11579 n += display_mode_element (it, depth, field_width - n,
11580 precision - n, XCONS (elt)->car);
a2889657
JB
11581 elt = XCONS (elt)->cdr;
11582 }
11583 }
11584 }
11585 break;
11586
11587 default:
11588 invalid:
d39b6696 11589 if (frame_title_ptr)
5f5c8ee5 11590 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 11591 else
5f5c8ee5
GM
11592 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11593 precision - n, 0, 0);
11594 return n;
a2889657
JB
11595 }
11596
5f5c8ee5
GM
11597 /* Pad to FIELD_WIDTH. */
11598 if (field_width > 0 && n < field_width)
11599 {
11600 if (frame_title_ptr)
11601 n += store_frame_title ("", field_width - n, 0);
11602 else
11603 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11604 0, 0, 0);
11605 }
11606
11607 return n;
a2889657 11608}
5f5c8ee5
GM
11609
11610
766525bc
RS
11611/* Write a null-terminated, right justified decimal representation of
11612 the positive integer D to BUF using a minimal field width WIDTH. */
11613
11614static void
11615pint2str (buf, width, d)
11616 register char *buf;
11617 register int width;
11618 register int d;
11619{
11620 register char *p = buf;
11621
11622 if (d <= 0)
5f5c8ee5 11623 *p++ = '0';
766525bc 11624 else
5f5c8ee5 11625 {
766525bc 11626 while (d > 0)
5f5c8ee5 11627 {
766525bc
RS
11628 *p++ = d % 10 + '0';
11629 d /= 10;
5f5c8ee5
GM
11630 }
11631 }
11632
11633 for (width -= (int) (p - buf); width > 0; --width)
11634 *p++ = ' ';
766525bc
RS
11635 *p-- = '\0';
11636 while (p > buf)
5f5c8ee5 11637 {
766525bc
RS
11638 d = *buf;
11639 *buf++ = *p;
11640 *p-- = d;
5f5c8ee5 11641 }
766525bc
RS
11642}
11643
5f5c8ee5 11644/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
11645 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11646 type of CODING_SYSTEM. Return updated pointer into BUF. */
11647
6693a99a 11648static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 11649
1c9241f5
KH
11650static char *
11651decode_mode_spec_coding (coding_system, buf, eol_flag)
11652 Lisp_Object coding_system;
11653 register char *buf;
11654 int eol_flag;
11655{
1e1078d6 11656 Lisp_Object val;
916848d8 11657 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
11658 unsigned char *eol_str;
11659 int eol_str_len;
11660 /* The EOL conversion we are using. */
11661 Lisp_Object eoltype;
1e1078d6
RS
11662
11663 val = coding_system;
1c9241f5
KH
11664
11665 if (NILP (val)) /* Not yet decided. */
11666 {
916848d8
RS
11667 if (multibyte)
11668 *buf++ = '-';
21e989e3 11669 if (eol_flag)
302f2b38 11670 eoltype = eol_mnemonic_undecided;
1e1078d6 11671 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
11672 }
11673 else
11674 {
1e1078d6
RS
11675 Lisp_Object eolvalue;
11676
11677 eolvalue = Fget (coding_system, Qeol_type);
11678
1c9241f5 11679 while (!NILP (val) && SYMBOLP (val))
1e1078d6
RS
11680 {
11681 val = Fget (val, Qcoding_system);
11682 if (NILP (eolvalue))
b070c1d7 11683 eolvalue = Fget (val, Qeol_type);
1e1078d6
RS
11684 }
11685
916848d8
RS
11686 if (multibyte)
11687 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11688
1c9241f5
KH
11689 if (eol_flag)
11690 {
1e1078d6
RS
11691 /* The EOL conversion that is normal on this system. */
11692
11693 if (NILP (eolvalue)) /* Not yet decided. */
11694 eoltype = eol_mnemonic_undecided;
11695 else if (VECTORP (eolvalue)) /* Not yet decided. */
11696 eoltype = eol_mnemonic_undecided;
11697 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11698 eoltype = (XFASTINT (eolvalue) == 0
11699 ? eol_mnemonic_unix
11700 : (XFASTINT (eolvalue) == 1
11701 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
11702 }
11703 }
5f5c8ee5 11704
302f2b38
EZ
11705 if (eol_flag)
11706 {
11707 /* Mention the EOL conversion if it is not the usual one. */
11708 if (STRINGP (eoltype))
11709 {
11710 eol_str = XSTRING (eoltype)->data;
11711 eol_str_len = XSTRING (eoltype)->size;
11712 }
f30b3499
KH
11713 else if (INTEGERP (eoltype)
11714 && CHAR_VALID_P (XINT (eoltype), 0))
11715 {
f30b3499
KH
11716 unsigned char work[4];
11717
11718 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11719 }
302f2b38
EZ
11720 else
11721 {
11722 eol_str = invalid_eol_type;
11723 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 11724 }
f30b3499 11725 bcopy (eol_str, buf, eol_str_len);
302f2b38 11726 buf += eol_str_len;
1c9241f5 11727 }
302f2b38 11728
1c9241f5
KH
11729 return buf;
11730}
11731
a2889657 11732/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
11733 generated by character C. PRECISION >= 0 means don't return a
11734 string longer than that value. FIELD_WIDTH > 0 means pad the
11735 string returned with spaces to that value. */
a2889657 11736
11e82b76
JB
11737static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11738
a2889657 11739static char *
5f5c8ee5 11740decode_mode_spec (w, c, field_width, precision)
a2889657
JB
11741 struct window *w;
11742 register char c;
5f5c8ee5 11743 int field_width, precision;
a2889657 11744{
0b67772d 11745 Lisp_Object obj;
5f5c8ee5
GM
11746 struct frame *f = XFRAME (WINDOW_FRAME (w));
11747 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 11748 struct buffer *b = XBUFFER (w->buffer);
a2889657 11749
0b67772d 11750 obj = Qnil;
a2889657
JB
11751
11752 switch (c)
11753 {
1af9f229
RS
11754 case '*':
11755 if (!NILP (b->read_only))
11756 return "%";
11757 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11758 return "*";
11759 return "-";
11760
11761 case '+':
11762 /* This differs from %* only for a modified read-only buffer. */
11763 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11764 return "*";
11765 if (!NILP (b->read_only))
11766 return "%";
11767 return "-";
11768
11769 case '&':
11770 /* This differs from %* in ignoring read-only-ness. */
11771 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11772 return "*";
11773 return "-";
11774
11775 case '%':
11776 return "%";
11777
11778 case '[':
11779 {
11780 int i;
11781 char *p;
11782
11783 if (command_loop_level > 5)
11784 return "[[[... ";
11785 p = decode_mode_spec_buf;
11786 for (i = 0; i < command_loop_level; i++)
11787 *p++ = '[';
11788 *p = 0;
11789 return decode_mode_spec_buf;
11790 }
11791
11792 case ']':
11793 {
11794 int i;
11795 char *p;
11796
11797 if (command_loop_level > 5)
11798 return " ...]]]";
11799 p = decode_mode_spec_buf;
11800 for (i = 0; i < command_loop_level; i++)
11801 *p++ = ']';
11802 *p = 0;
11803 return decode_mode_spec_buf;
11804 }
11805
11806 case '-':
11807 {
1af9f229 11808 register int i;
5f5c8ee5
GM
11809
11810 /* Let lots_of_dashes be a string of infinite length. */
11811 if (field_width <= 0
11812 || field_width > sizeof (lots_of_dashes))
1af9f229 11813 {
5f5c8ee5
GM
11814 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11815 decode_mode_spec_buf[i] = '-';
11816 decode_mode_spec_buf[i] = '\0';
11817 return decode_mode_spec_buf;
1af9f229 11818 }
5f5c8ee5
GM
11819 else
11820 return lots_of_dashes;
1af9f229
RS
11821 }
11822
a2889657 11823 case 'b':
d39b6696 11824 obj = b->name;
a2889657
JB
11825 break;
11826
1af9f229
RS
11827 case 'c':
11828 {
11829 int col = current_column ();
11830 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 11831 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
11832 return decode_mode_spec_buf;
11833 }
11834
11835 case 'F':
11836 /* %F displays the frame name. */
5f5c8ee5 11837 if (!NILP (f->title))
95184b48 11838 return (char *) XSTRING (f->title)->data;
fd8ff63d 11839 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 11840 return (char *) XSTRING (f->name)->data;
9c6da96f 11841 return "Emacs";
1af9f229 11842
a2889657 11843 case 'f':
d39b6696 11844 obj = b->filename;
a2889657
JB
11845 break;
11846
aa6d10fa
RS
11847 case 'l':
11848 {
12adba34
RS
11849 int startpos = XMARKER (w->start)->charpos;
11850 int startpos_byte = marker_byte_position (w->start);
11851 int line, linepos, linepos_byte, topline;
aa6d10fa 11852 int nlines, junk;
aa6d10fa
RS
11853 int height = XFASTINT (w->height);
11854
11855 /* If we decided that this buffer isn't suitable for line numbers,
11856 don't forget that too fast. */
11857 if (EQ (w->base_line_pos, w->buffer))
766525bc 11858 goto no_value;
5300fd39
RS
11859 /* But do forget it, if the window shows a different buffer now. */
11860 else if (BUFFERP (w->base_line_pos))
11861 w->base_line_pos = Qnil;
aa6d10fa
RS
11862
11863 /* If the buffer is very big, don't waste time. */
d39b6696 11864 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
11865 {
11866 w->base_line_pos = Qnil;
11867 w->base_line_number = Qnil;
766525bc 11868 goto no_value;
aa6d10fa
RS
11869 }
11870
11871 if (!NILP (w->base_line_number)
11872 && !NILP (w->base_line_pos)
12adba34 11873 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
11874 {
11875 line = XFASTINT (w->base_line_number);
11876 linepos = XFASTINT (w->base_line_pos);
12adba34 11877 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
11878 }
11879 else
11880 {
11881 line = 1;
d39b6696 11882 linepos = BUF_BEGV (b);
12adba34 11883 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
11884 }
11885
11886 /* Count lines from base line to window start position. */
12adba34
RS
11887 nlines = display_count_lines (linepos, linepos_byte,
11888 startpos_byte,
11889 startpos, &junk);
aa6d10fa
RS
11890
11891 topline = nlines + line;
11892
11893 /* Determine a new base line, if the old one is too close
11894 or too far away, or if we did not have one.
11895 "Too close" means it's plausible a scroll-down would
11896 go back past it. */
d39b6696 11897 if (startpos == BUF_BEGV (b))
aa6d10fa 11898 {
c2213350
KH
11899 XSETFASTINT (w->base_line_number, topline);
11900 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
11901 }
11902 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 11903 || linepos == BUF_BEGV (b))
aa6d10fa 11904 {
d39b6696 11905 int limit = BUF_BEGV (b);
12adba34 11906 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 11907 int position;
5d121aec 11908 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
11909
11910 if (startpos - distance > limit)
12adba34
RS
11911 {
11912 limit = startpos - distance;
11913 limit_byte = CHAR_TO_BYTE (limit);
11914 }
aa6d10fa 11915
12adba34
RS
11916 nlines = display_count_lines (startpos, startpos_byte,
11917 limit_byte,
11918 - (height * 2 + 30),
aa6d10fa
RS
11919 &position);
11920 /* If we couldn't find the lines we wanted within
5d121aec 11921 line_number_display_limit_width chars per line,
aa6d10fa 11922 give up on line numbers for this window. */
12adba34 11923 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
11924 {
11925 w->base_line_pos = w->buffer;
11926 w->base_line_number = Qnil;
766525bc 11927 goto no_value;
aa6d10fa
RS
11928 }
11929
c2213350 11930 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 11931 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
11932 }
11933
11934 /* Now count lines from the start pos to point. */
12adba34
RS
11935 nlines = display_count_lines (startpos, startpos_byte,
11936 PT_BYTE, PT, &junk);
aa6d10fa
RS
11937
11938 /* Record that we did display the line number. */
11939 line_number_displayed = 1;
11940
11941 /* Make the string to show. */
5f5c8ee5 11942 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 11943 return decode_mode_spec_buf;
766525bc
RS
11944 no_value:
11945 {
11946 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
11947 int pad = field_width - 2;
11948 while (pad-- > 0)
11949 *p++ = ' ';
11950 *p++ = '?';
11951 *p = '?';
766525bc
RS
11952 return decode_mode_spec_buf;
11953 }
aa6d10fa
RS
11954 }
11955 break;
11956
a2889657 11957 case 'm':
d39b6696 11958 obj = b->mode_name;
a2889657
JB
11959 break;
11960
11961 case 'n':
d39b6696 11962 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
11963 return " Narrow";
11964 break;
11965
a2889657
JB
11966 case 'p':
11967 {
11968 int pos = marker_position (w->start);
d39b6696 11969 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 11970
d39b6696 11971 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 11972 {
d39b6696 11973 if (pos <= BUF_BEGV (b))
a2889657
JB
11974 return "All";
11975 else
11976 return "Bottom";
11977 }
d39b6696 11978 else if (pos <= BUF_BEGV (b))
a2889657
JB
11979 return "Top";
11980 else
11981 {
3c7d31b9
RS
11982 if (total > 1000000)
11983 /* Do it differently for a large value, to avoid overflow. */
11984 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11985 else
11986 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
11987 /* We can't normally display a 3-digit number,
11988 so get us a 2-digit number that is close. */
11989 if (total == 100)
11990 total = 99;
11991 sprintf (decode_mode_spec_buf, "%2d%%", total);
11992 return decode_mode_spec_buf;
11993 }
11994 }
11995
8ffcb79f
RS
11996 /* Display percentage of size above the bottom of the screen. */
11997 case 'P':
11998 {
11999 int toppos = marker_position (w->start);
d39b6696
KH
12000 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12001 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 12002
d39b6696 12003 if (botpos >= BUF_ZV (b))
8ffcb79f 12004 {
d39b6696 12005 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12006 return "All";
12007 else
12008 return "Bottom";
12009 }
12010 else
12011 {
3c7d31b9
RS
12012 if (total > 1000000)
12013 /* Do it differently for a large value, to avoid overflow. */
12014 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12015 else
12016 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
12017 /* We can't normally display a 3-digit number,
12018 so get us a 2-digit number that is close. */
12019 if (total == 100)
12020 total = 99;
d39b6696 12021 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12022 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12023 else
12024 sprintf (decode_mode_spec_buf, "%2d%%", total);
12025 return decode_mode_spec_buf;
12026 }
12027 }
12028
1af9f229
RS
12029 case 's':
12030 /* status of process */
12031 obj = Fget_buffer_process (w->buffer);
12032 if (NILP (obj))
12033 return "no process";
12034#ifdef subprocesses
12035 obj = Fsymbol_name (Fprocess_status (obj));
12036#endif
12037 break;
d39b6696 12038
1af9f229
RS
12039 case 't': /* indicate TEXT or BINARY */
12040#ifdef MODE_LINE_BINARY_TEXT
12041 return MODE_LINE_BINARY_TEXT (b);
12042#else
12043 return "T";
12044#endif
1c9241f5
KH
12045
12046 case 'z':
12047 /* coding-system (not including end-of-line format) */
12048 case 'Z':
12049 /* coding-system (including end-of-line type) */
12050 {
12051 int eol_flag = (c == 'Z');
539b4d41 12052 char *p = decode_mode_spec_buf;
1c9241f5 12053
d30e754b 12054 if (! FRAME_WINDOW_P (f))
1c9241f5 12055 {
11c52c4f
RS
12056 /* No need to mention EOL here--the terminal never needs
12057 to do EOL conversion. */
12058 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12059 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 12060 }
f13c925f 12061 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 12062 p, eol_flag);
f13c925f 12063
11c52c4f 12064#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
12065#ifdef subprocesses
12066 obj = Fget_buffer_process (Fcurrent_buffer ());
12067 if (PROCESSP (obj))
12068 {
12069 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12070 p, eol_flag);
12071 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12072 p, eol_flag);
12073 }
12074#endif /* subprocesses */
11c52c4f 12075#endif /* 0 */
1c9241f5
KH
12076 *p = 0;
12077 return decode_mode_spec_buf;
12078 }
a2889657 12079 }
d39b6696 12080
e24c997d 12081 if (STRINGP (obj))
a2889657
JB
12082 return (char *) XSTRING (obj)->data;
12083 else
12084 return "";
12085}
5f5c8ee5
GM
12086
12087
12adba34
RS
12088/* Count up to COUNT lines starting from START / START_BYTE.
12089 But don't go beyond LIMIT_BYTE.
12090 Return the number of lines thus found (always nonnegative).
59b49f63 12091
12adba34 12092 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
12093
12094static int
12adba34
RS
12095display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12096 int start, start_byte, limit_byte, count;
12097 int *byte_pos_ptr;
59b49f63 12098{
59b49f63
RS
12099 register unsigned char *cursor;
12100 unsigned char *base;
12101
12102 register int ceiling;
12103 register unsigned char *ceiling_addr;
12adba34 12104 int orig_count = count;
59b49f63
RS
12105
12106 /* If we are not in selective display mode,
12107 check only for newlines. */
12adba34
RS
12108 int selective_display = (!NILP (current_buffer->selective_display)
12109 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
12110
12111 if (count > 0)
12adba34
RS
12112 {
12113 while (start_byte < limit_byte)
12114 {
12115 ceiling = BUFFER_CEILING_OF (start_byte);
12116 ceiling = min (limit_byte - 1, ceiling);
12117 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12118 base = (cursor = BYTE_POS_ADDR (start_byte));
12119 while (1)
12120 {
12121 if (selective_display)
12122 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12123 ;
12124 else
12125 while (*cursor != '\n' && ++cursor != ceiling_addr)
12126 ;
12127
12128 if (cursor != ceiling_addr)
12129 {
12130 if (--count == 0)
12131 {
12132 start_byte += cursor - base + 1;
12133 *byte_pos_ptr = start_byte;
12134 return orig_count;
12135 }
12136 else
12137 if (++cursor == ceiling_addr)
12138 break;
12139 }
12140 else
12141 break;
12142 }
12143 start_byte += cursor - base;
12144 }
12145 }
59b49f63
RS
12146 else
12147 {
12adba34
RS
12148 while (start_byte > limit_byte)
12149 {
12150 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12151 ceiling = max (limit_byte, ceiling);
12152 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12153 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
12154 while (1)
12155 {
12adba34
RS
12156 if (selective_display)
12157 while (--cursor != ceiling_addr
12158 && *cursor != '\n' && *cursor != 015)
12159 ;
12160 else
12161 while (--cursor != ceiling_addr && *cursor != '\n')
12162 ;
12163
59b49f63
RS
12164 if (cursor != ceiling_addr)
12165 {
12166 if (++count == 0)
12167 {
12adba34
RS
12168 start_byte += cursor - base + 1;
12169 *byte_pos_ptr = start_byte;
12170 /* When scanning backwards, we should
12171 not count the newline posterior to which we stop. */
12172 return - orig_count - 1;
59b49f63
RS
12173 }
12174 }
12175 else
12176 break;
12177 }
12adba34
RS
12178 /* Here we add 1 to compensate for the last decrement
12179 of CURSOR, which took it past the valid range. */
12180 start_byte += cursor - base + 1;
59b49f63
RS
12181 }
12182 }
12183
12adba34 12184 *byte_pos_ptr = limit_byte;
aa6d10fa 12185
12adba34
RS
12186 if (count < 0)
12187 return - orig_count + count;
12188 return orig_count - count;
aa6d10fa 12189
12adba34 12190}
a2889657 12191
a2889657 12192
5f5c8ee5
GM
12193\f
12194/***********************************************************************
12195 Displaying strings
12196 ***********************************************************************/
278feba9 12197
5f5c8ee5 12198/* Display a NUL-terminated string, starting with index START.
a3788d53 12199
5f5c8ee5
GM
12200 If STRING is non-null, display that C string. Otherwise, the Lisp
12201 string LISP_STRING is displayed.
a2889657 12202
5f5c8ee5
GM
12203 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12204 FACE_STRING. Display STRING or LISP_STRING with the face at
12205 FACE_STRING_POS in FACE_STRING:
a2889657 12206
5f5c8ee5
GM
12207 Display the string in the environment given by IT, but use the
12208 standard display table, temporarily.
a3788d53 12209
5f5c8ee5
GM
12210 FIELD_WIDTH is the minimum number of output glyphs to produce.
12211 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12212 with spaces. If STRING has more characters, more than FIELD_WIDTH
12213 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12214
12215 PRECISION is the maximum number of characters to output from
12216 STRING. PRECISION < 0 means don't truncate the string.
a2889657 12217
5f5c8ee5 12218 This is roughly equivalent to printf format specifiers:
a2889657 12219
5f5c8ee5
GM
12220 FIELD_WIDTH PRECISION PRINTF
12221 ----------------------------------------
12222 -1 -1 %s
12223 -1 10 %.10s
12224 10 -1 %10s
12225 20 10 %20.10s
a2889657 12226
5f5c8ee5
GM
12227 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12228 display them, and < 0 means obey the current buffer's value of
12229 enable_multibyte_characters.
278feba9 12230
5f5c8ee5 12231 Value is the number of glyphs produced. */
b1d1124b 12232
5f5c8ee5
GM
12233static int
12234display_string (string, lisp_string, face_string, face_string_pos,
12235 start, it, field_width, precision, max_x, multibyte)
12236 unsigned char *string;
12237 Lisp_Object lisp_string;
12238 int start;
12239 struct it *it;
12240 int field_width, precision, max_x;
12241 int multibyte;
12242{
12243 int hpos_at_start = it->hpos;
12244 int saved_face_id = it->face_id;
12245 struct glyph_row *row = it->glyph_row;
12246
12247 /* Initialize the iterator IT for iteration over STRING beginning
12248 with index START. We assume that IT may be modified here (which
12249 means that display_line has to do something when displaying a
12250 mini-buffer prompt, which it does). */
12251 reseat_to_string (it, string, lisp_string, start,
12252 precision, field_width, multibyte);
12253
12254 /* If displaying STRING, set up the face of the iterator
12255 from LISP_STRING, if that's given. */
12256 if (STRINGP (face_string))
12257 {
12258 int endptr;
12259 struct face *face;
12260
12261 it->face_id
12262 = face_at_string_position (it->w, face_string, face_string_pos,
12263 0, it->region_beg_charpos,
12264 it->region_end_charpos,
12265 &endptr, it->base_face_id);
12266 face = FACE_FROM_ID (it->f, it->face_id);
12267 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 12268 }
a2889657 12269
5f5c8ee5
GM
12270 /* Set max_x to the maximum allowed X position. Don't let it go
12271 beyond the right edge of the window. */
12272 if (max_x <= 0)
12273 max_x = it->last_visible_x;
12274 else
12275 max_x = min (max_x, it->last_visible_x);
efc63ef0 12276
5f5c8ee5
GM
12277 /* Skip over display elements that are not visible. because IT->w is
12278 hscrolled. */
12279 if (it->current_x < it->first_visible_x)
12280 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12281 MOVE_TO_POS | MOVE_TO_X);
a2889657 12282
5f5c8ee5
GM
12283 row->ascent = it->max_ascent;
12284 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12285 row->phys_ascent = it->max_phys_ascent;
12286 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 12287
5f5c8ee5
GM
12288 /* This condition is for the case that we are called with current_x
12289 past last_visible_x. */
12290 while (it->current_x < max_x)
a2889657 12291 {
5f5c8ee5 12292 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 12293
5f5c8ee5
GM
12294 /* Get the next display element. */
12295 if (!get_next_display_element (it))
90adcf20 12296 break;
1c9241f5 12297
5f5c8ee5
GM
12298 /* Produce glyphs. */
12299 x_before = it->current_x;
12300 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12301 PRODUCE_GLYPHS (it);
90adcf20 12302
5f5c8ee5
GM
12303 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12304 i = 0;
12305 x = x_before;
12306 while (i < nglyphs)
a2889657 12307 {
5f5c8ee5
GM
12308 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12309
12310 if (!it->truncate_lines_p
12311 && x + glyph->pixel_width > max_x)
12312 {
12313 /* End of continued line or max_x reached. */
12314 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12315 it->current_x = x;
12316 break;
12317 }
12318 else if (x + glyph->pixel_width > it->first_visible_x)
12319 {
12320 /* Glyph is at least partially visible. */
12321 ++it->hpos;
12322 if (x < it->first_visible_x)
12323 it->glyph_row->x = x - it->first_visible_x;
12324 }
12325 else
a2889657 12326 {
5f5c8ee5
GM
12327 /* Glyph is off the left margin of the display area.
12328 Should not happen. */
12329 abort ();
a2889657 12330 }
5f5c8ee5
GM
12331
12332 row->ascent = max (row->ascent, it->max_ascent);
12333 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12334 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12335 row->phys_height = max (row->phys_height,
12336 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12337 x += glyph->pixel_width;
12338 ++i;
a2889657 12339 }
5f5c8ee5
GM
12340
12341 /* Stop if max_x reached. */
12342 if (i < nglyphs)
12343 break;
12344
12345 /* Stop at line ends. */
12346 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 12347 {
5f5c8ee5
GM
12348 it->continuation_lines_width = 0;
12349 break;
a2889657 12350 }
1c9241f5 12351
5f5c8ee5 12352 set_iterator_to_next (it);
a688bb24 12353
5f5c8ee5
GM
12354 /* Stop if truncating at the right edge. */
12355 if (it->truncate_lines_p
12356 && it->current_x >= it->last_visible_x)
12357 {
12358 /* Add truncation mark, but don't do it if the line is
12359 truncated at a padding space. */
12360 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 12361 {
5f5c8ee5
GM
12362 if (!FRAME_WINDOW_P (it->f))
12363 produce_special_glyphs (it, IT_TRUNCATION);
12364 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 12365 }
5f5c8ee5 12366 break;
1c9241f5 12367 }
a2889657
JB
12368 }
12369
5f5c8ee5
GM
12370 /* Maybe insert a truncation at the left. */
12371 if (it->first_visible_x
12372 && IT_CHARPOS (*it) > 0)
a2889657 12373 {
5f5c8ee5
GM
12374 if (!FRAME_WINDOW_P (it->f))
12375 insert_left_trunc_glyphs (it);
12376 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
12377 }
12378
5f5c8ee5
GM
12379 it->face_id = saved_face_id;
12380
12381 /* Value is number of columns displayed. */
12382 return it->hpos - hpos_at_start;
12383}
a2889657 12384
a2889657 12385
a2889657 12386\f
5f5c8ee5
GM
12387/* This is like a combination of memq and assq. Return 1 if PROPVAL
12388 appears as an element of LIST or as the car of an element of LIST.
12389 If PROPVAL is a list, compare each element against LIST in that
12390 way, and return 1 if any element of PROPVAL is found in LIST.
12391 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12392
12393int
12394invisible_p (propval, list)
12395 register Lisp_Object propval;
12396 Lisp_Object list;
12397{
af460d46
RS
12398 register Lisp_Object tail, proptail;
12399 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
12400 {
12401 register Lisp_Object tem;
af460d46 12402 tem = XCONS (tail)->car;
642eefc6
RS
12403 if (EQ (propval, tem))
12404 return 1;
12405 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
12406 return 1;
12407 }
af460d46
RS
12408 if (CONSP (propval))
12409 for (proptail = propval; CONSP (proptail);
12410 proptail = XCONS (proptail)->cdr)
12411 {
12412 Lisp_Object propelt;
12413 propelt = XCONS (proptail)->car;
12414 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12415 {
12416 register Lisp_Object tem;
12417 tem = XCONS (tail)->car;
12418 if (EQ (propelt, tem))
12419 return 1;
12420 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
12421 return 1;
12422 }
12423 }
642eefc6
RS
12424 return 0;
12425}
12426
5f5c8ee5
GM
12427
12428/* Return 1 if PROPVAL appears as the car of an element of LIST and
12429 the cdr of that element is non-nil. If PROPVAL is a list, check
12430 each element of PROPVAL in that way, and the first time some
12431 element is found, return 1 if the cdr of that element is non-nil.
12432 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12433
12434int
12435invisible_ellipsis_p (propval, list)
12436 register Lisp_Object propval;
12437 Lisp_Object list;
12438{
af460d46
RS
12439 register Lisp_Object tail, proptail;
12440 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
12441 {
12442 register Lisp_Object tem;
af460d46 12443 tem = XCONS (tail)->car;
642eefc6
RS
12444 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
12445 return ! NILP (XCONS (tem)->cdr);
12446 }
af460d46
RS
12447 if (CONSP (propval))
12448 for (proptail = propval; CONSP (proptail);
12449 proptail = XCONS (proptail)->cdr)
12450 {
12451 Lisp_Object propelt;
12452 propelt = XCONS (proptail)->car;
12453 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12454 {
12455 register Lisp_Object tem;
12456 tem = XCONS (tail)->car;
12457 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
12458 return ! NILP (XCONS (tem)->cdr);
12459 }
12460 }
642eefc6
RS
12461 return 0;
12462}
5f5c8ee5
GM
12463
12464
642eefc6 12465\f
5f5c8ee5
GM
12466/***********************************************************************
12467 Initialization
12468 ***********************************************************************/
12469
a2889657
JB
12470void
12471syms_of_xdisp ()
12472{
9142dd5b
GM
12473 Vresize_mini_config = Qnil;
12474 staticpro (&Vresize_mini_config);
12475
c6e89d6c
GM
12476 Vwith_echo_area_save_vector = Qnil;
12477 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 12478
c6e89d6c
GM
12479 Vmessage_stack = Qnil;
12480 staticpro (&Vmessage_stack);
12481
735c094c 12482 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 12483 staticpro (&Qinhibit_redisplay);
735c094c 12484
5f5c8ee5
GM
12485#if GLYPH_DEBUG
12486 defsubr (&Sdump_glyph_matrix);
12487 defsubr (&Sdump_glyph_row);
12488 defsubr (&Sdump_toolbar_row);
12489 defsubr (&Strace_redisplay_toggle);
12490#endif
12491
cf074754
RS
12492 staticpro (&Qmenu_bar_update_hook);
12493 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12494
d46fb96a 12495 staticpro (&Qoverriding_terminal_local_map);
7079aefa 12496 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 12497
399164b4
KH
12498 staticpro (&Qoverriding_local_map);
12499 Qoverriding_local_map = intern ("overriding-local-map");
12500
75c43375
RS
12501 staticpro (&Qwindow_scroll_functions);
12502 Qwindow_scroll_functions = intern ("window-scroll-functions");
12503
e0bfbde6
RS
12504 staticpro (&Qredisplay_end_trigger_functions);
12505 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 12506
2e54982e
RS
12507 staticpro (&Qinhibit_point_motion_hooks);
12508 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12509
5f5c8ee5
GM
12510 staticpro (&Qdisplay);
12511 Qdisplay = intern ("display");
12512 staticpro (&Qleft_margin);
12513 Qspace_width = intern ("space-width");
12514 staticpro (&Qspace_width);
12515 Qheight = intern ("height");
12516 staticpro (&Qheight);
12517 Qraise = intern ("raise");
12518 staticpro (&Qraise);
12519 Qspace = intern ("space");
12520 staticpro (&Qspace);
12521 Qleft_margin = intern ("left-margin");
12522 staticpro (&Qright_margin);
12523 Qright_margin = intern ("right-margin");
12524 Qalign_to = intern ("align-to");
12525 staticpro (&Qalign_to);
12526 QCalign_to = intern (":align-to");
12527 staticpro (&QCalign_to);
12528 Qwidth = intern ("width");
12529 staticpro (&Qwidth);
12530 Qrelative_width = intern ("relative-width");
12531 staticpro (&Qrelative_width);
12532 QCrelative_width = intern (":relative-width");
12533 staticpro (&QCrelative_width);
12534 QCrelative_height = intern (":relative-height");
12535 staticpro (&QCrelative_height);
12536 QCeval = intern (":eval");
12537 staticpro (&QCeval);
12538 QCwhen = intern (":when");
12539 staticpro (&QCwhen);
12540 Qfontified = intern ("fontified");
12541 staticpro (&Qfontified);
12542 Qfontification_functions = intern ("fontification-functions");
12543 staticpro (&Qfontification_functions);
5f5c8ee5
GM
12544 Qtrailing_whitespace = intern ("trailing-whitespace");
12545 staticpro (&Qtrailing_whitespace);
12546 Qimage = intern ("image");
12547 staticpro (&Qimage);
12548
a2889657
JB
12549 staticpro (&last_arrow_position);
12550 staticpro (&last_arrow_string);
12551 last_arrow_position = Qnil;
12552 last_arrow_string = Qnil;
c6e89d6c
GM
12553
12554 echo_buffer[0] = echo_buffer[1] = Qnil;
12555 staticpro (&echo_buffer[0]);
12556 staticpro (&echo_buffer[1]);
12557
12558 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12559 staticpro (&echo_area_buffer[0]);
12560 staticpro (&echo_area_buffer[1]);
a2889657 12561
8f897821
GM
12562 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12563 "Non-nil means highlight trailing whitespace.\n\
12564The face used for trailing whitespace is `trailing-whitespace'.");
12565 Vshow_trailing_whitespace = Qnil;
12566
735c094c
KH
12567 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12568 "Non-nil means don't actually do any redisplay.\n\
12569This is used for internal purposes.");
12570 Vinhibit_redisplay = Qnil;
12571
a2889657 12572 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 12573 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
12574 Vglobal_mode_string = Qnil;
12575
12576 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12577 "Marker for where to display an arrow on top of the buffer text.\n\
12578This must be the beginning of a line in order to work.\n\
12579See also `overlay-arrow-string'.");
12580 Voverlay_arrow_position = Qnil;
12581
12582 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12583 "String to display as an arrow. See also `overlay-arrow-position'.");
12584 Voverlay_arrow_string = Qnil;
12585
12586 DEFVAR_INT ("scroll-step", &scroll_step,
12587 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
12588If that fails to bring point back on frame, point is centered instead.\n\
12589If this is zero, point is always centered after it moves off frame.");
a2889657 12590
0789adb2
RS
12591 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12592 "*Scroll up to this many lines, to bring point back on screen.");
12593 scroll_conservatively = 0;
12594
9afd2168
RS
12595 DEFVAR_INT ("scroll-margin", &scroll_margin,
12596 "*Number of lines of margin at the top and bottom of a window.\n\
12597Recenter the window whenever point gets within this many lines\n\
12598of the top or bottom of the window.");
12599 scroll_margin = 0;
12600
5f5c8ee5 12601#if GLYPH_DEBUG
a2889657 12602 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 12603#endif
a2889657
JB
12604
12605 DEFVAR_BOOL ("truncate-partial-width-windows",
12606 &truncate_partial_width_windows,
44fa5b1e 12607 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
12608 truncate_partial_width_windows = 1;
12609
12610 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12611 "*Non-nil means use inverse video for the mode line.");
12612 mode_line_inverse_video = 1;
aa6d10fa
RS
12613
12614 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
5f5c8ee5 12615 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 12616If the buffer is bigger than this, the line number does not appear\n\
9f027393 12617in the mode line.");
aa6d10fa 12618 line_number_display_limit = 1000000;
fba9ce76 12619
5d121aec
KH
12620 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12621 "*Maximum line width (in characters) for line number display.\n\
12622If the average length of the lines near point is bigger than this, then the\n\
12623line number may be omitted from the mode line.");
12624 line_number_display_limit_width = 200;
12625
fba9ce76
RS
12626 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12627 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 12628 highlight_nonselected_windows = 0;
d39b6696
KH
12629
12630 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
12631 "Non-nil if more than one frame is visible on this display.\n\
12632Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
12633This variable is not guaranteed to be accurate except while processing\n\
12634`frame-title-format' and `icon-title-format'.");
d39b6696
KH
12635
12636 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 12637 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
12638\(Assuming the window manager supports this feature.)\n\
12639This variable has the same structure as `mode-line-format' (which see),\n\
12640and is used only on frames for which no explicit name has been set\n\
12641\(see `modify-frame-parameters').");
12642 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 12643 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
12644\(Assuming the window manager supports this feature.)\n\
12645This variable has the same structure as `mode-line-format' (which see),\n\
12646and is used only on frames for which no explicit name has been set\n\
12647\(see `modify-frame-parameters').");
12648 Vicon_title_format
12649 = Vframe_title_format
12650 = Fcons (intern ("multiple-frames"),
12651 Fcons (build_string ("%b"),
12652 Fcons (Fcons (build_string (""),
12653 Fcons (intern ("invocation-name"),
12654 Fcons (build_string ("@"),
12655 Fcons (intern ("system-name"),
12656 Qnil)))),
12657 Qnil)));
5992c4f7
KH
12658
12659 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12660 "Maximum number of lines to keep in the message log buffer.\n\
12661If nil, disable message logging. If t, log messages but don't truncate\n\
12662the buffer when it becomes large.");
12663 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
12664
12665 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12666 "Functions called before redisplay, if window sizes have changed.\n\
12667The value should be a list of functions that take one argument.\n\
12668Just before redisplay, for each frame, if any of its windows have changed\n\
12669size since the last redisplay, or have been split or deleted,\n\
12670all the functions in the list are called, with the frame as argument.");
12671 Vwindow_size_change_functions = Qnil;
75c43375
RS
12672
12673 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 12674 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 12675Each function is called with two arguments, the window\n\
8d9583b0
RS
12676and its new display-start position. Note that the value of `window-end'\n\
12677is not valid when these functions are called.");
75c43375 12678 Vwindow_scroll_functions = Qnil;
5f5c8ee5
GM
12679
12680 DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
12681 "*Non-nil means automatically resize toolbars.\n\
12682This increases a toolbar's height if not all toolbar items are visible.\n\
12683It decreases a toolbar's height when it would display blank lines\n\
12684otherwise.");
12685 auto_resize_toolbars_p = 1;
12686
12687 DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
12688 "*Non-nil means raise toolbar buttons when the mouse moves over them.");
12689 auto_raise_toolbar_buttons_p = 1;
12690
12691 DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
12692 "*Margin around toolbar buttons in pixels.");
12693 toolbar_button_margin = 1;
12694
12695 DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
12696 "Relief thickness of toolbar buttons.");
12697 toolbar_button_relief = 3;
12698
12699 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12700 "List of functions to call to fontify regions of text.\n\
12701Each function is called with one argument POS. Functions must\n\
12702fontify a region starting at POS in the current buffer, and give\n\
12703fontified regions the property `fontified'.\n\
12704This variable automatically becomes buffer-local when set.");
12705 Vfontification_functions = Qnil;
12706 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
12707
12708 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
12709 &unibyte_display_via_language_environment,
12710 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
12711Specifically this means that unibyte non-ASCII characters\n\
12712are displayed by converting them to the equivalent multibyte characters\n\
12713according to the current language environment. As a result, they are\n\
12714displayed according to the current fontset.");
12715 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
12716
12717 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
12718 "*Maximum height for resizing mini-windows.\n\
12719If a float, it specifies a fraction of the mini-window frame's height.\n\
12720If an integer, it specifies a number of lines.");
12721 Vmax_mini_window_height = make_float (0.25);
a2889657
JB
12722}
12723
5f5c8ee5
GM
12724
12725/* Initialize this module when Emacs starts. */
12726
dfcf069d 12727void
a2889657
JB
12728init_xdisp ()
12729{
12730 Lisp_Object root_window;
5f5c8ee5 12731 struct window *mini_w;
a2889657 12732
5f5c8ee5 12733 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
12734
12735 mini_w = XWINDOW (minibuf_window);
11e82b76 12736 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 12737
a2889657
JB
12738 if (!noninteractive)
12739 {
5f5c8ee5
GM
12740 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12741 int i;
12742
12743 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 12744 set_window_height (root_window,
5f5c8ee5 12745 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 12746 0);
c2213350 12747 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
12748 set_window_height (minibuf_window, 1, 0);
12749
c2213350
KH
12750 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12751 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
12752
12753 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12754 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12755 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12756
12757 /* The default ellipsis glyphs `...'. */
12758 for (i = 0; i < 3; ++i)
12759 XSETFASTINT (default_invis_vector[i], '.');
a2889657 12760 }
5f5c8ee5
GM
12761
12762#ifdef HAVE_WINDOW_SYSTEM
12763 {
12764 /* Allocate the buffer for frame titles. */
12765 int size = 100;
12766 frame_title_buf = (char *) xmalloc (size);
12767 frame_title_buf_end = frame_title_buf + size;
12768 frame_title_ptr = NULL;
12769 }
12770#endif /* HAVE_WINDOW_SYSTEM */
a2889657 12771}
5f5c8ee5
GM
12772
12773