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