(run_hook_list_with_args): Gcpro `globals'.
[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
262/* Name of the variable controlling the highlighting of trailing
263 whitespace. The implementation uses find_symbol_value to get its
264 value. */
265
266Lisp_Object Qshow_trailing_whitespace;
267
268/* Name of the face used to highlight trailing whitespace. */
269
270Lisp_Object Qtrailing_whitespace;
271
272/* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
274
275Lisp_Object Qimage;
276
277/* Non-zero means print newline to stdout before next mini-buffer
278 message. */
a2889657
JB
279
280int noninteractive_need_newline;
281
5f5c8ee5 282/* Non-zero means print newline to message log before next message. */
f88eb0b6 283
3c6595e0 284static int message_log_need_newline;
f88eb0b6 285
5f5c8ee5
GM
286\f
287/* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
a2889657 291
5f5c8ee5 292static struct text_pos this_line_start_pos;
a2889657 293
5f5c8ee5
GM
294/* Number of characters past the end of the line above, including the
295 terminating newline. */
296
297static struct text_pos this_line_end_pos;
298
299/* The vertical positions and the height of this line. */
a2889657 300
a2889657 301static int this_line_vpos;
5f5c8ee5
GM
302static int this_line_y;
303static int this_line_pixel_height;
304
305/* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
307
308static int this_line_start_x;
a2889657 309
5f5c8ee5 310/* Buffer that this_line_.* variables are referring to. */
a2889657 311
a2889657
JB
312static struct buffer *this_line_buffer;
313
5f5c8ee5
GM
314/* Nonzero means truncate lines in all windows less wide than the
315 frame. */
a2889657 316
a2889657
JB
317int truncate_partial_width_windows;
318
7bbe686f 319/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 320
7bbe686f 321int unibyte_display_via_language_environment;
5f5c8ee5
GM
322
323/* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
7bbe686f 326
d39b6696
KH
327int multiple_frames;
328
a2889657
JB
329Lisp_Object Vglobal_mode_string;
330
331/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 332
a2889657
JB
333Lisp_Object Voverlay_arrow_position;
334
5f5c8ee5
GM
335/* String to display for the arrow. Only used on terminal frames. */
336
a2889657
JB
337Lisp_Object Voverlay_arrow_string;
338
5f5c8ee5
GM
339/* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
342
d45de95b
RS
343static Lisp_Object last_arrow_position, last_arrow_string;
344
5f5c8ee5
GM
345/* Like mode-line-format, but for the title bar on a visible frame. */
346
d39b6696
KH
347Lisp_Object Vframe_title_format;
348
5f5c8ee5
GM
349/* Like mode-line-format, but for the title bar on an iconified frame. */
350
d39b6696
KH
351Lisp_Object Vicon_title_format;
352
08b610e4
RS
353/* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
5f5c8ee5 356
08b610e4
RS
357static Lisp_Object Vwindow_size_change_functions;
358
cf074754
RS
359Lisp_Object Qmenu_bar_update_hook;
360
a2889657 361/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 362
5f5c8ee5 363static int overlay_arrow_seen;
ca26e1c8 364
fba9ce76 365/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 366
5f5c8ee5
GM
367int highlight_nonselected_windows;
368
369/* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
371
14510fee 372static int scroll_step;
a2889657 373
5f5c8ee5
GM
374/* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
376
0789adb2
RS
377static int scroll_conservatively;
378
5f5c8ee5
GM
379/* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
383
9afd2168
RS
384int scroll_margin;
385
5f5c8ee5
GM
386/* Number of characters of overlap to show, when scrolling a one-line
387 window such as a minibuffer. */
388
010494d0
KH
389static int minibuffer_scroll_overlap;
390
5f5c8ee5
GM
391/* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
a2889657 394
a2889657
JB
395int buffer_shared;
396
5f5c8ee5 397/* Vector containing glyphs for an ellipsis `...'. */
a2889657 398
5f5c8ee5 399static Lisp_Object default_invis_vector[3];
a2889657 400
5f5c8ee5 401/* Nonzero means display mode line highlighted. */
a2889657 402
a2889657
JB
403int mode_line_inverse_video;
404
5f5c8ee5
GM
405/* Prompt to display in front of the mini-buffer contents. */
406
8c5b6a0a 407Lisp_Object minibuf_prompt;
a2889657 408
5f5c8ee5
GM
409/* Width of current mini-buffer prompt. Only set after display_line
410 of the line that contains the prompt. */
411
a2889657 412int minibuf_prompt_width;
5f5c8ee5
GM
413int minibuf_prompt_pixel_width;
414
415/* Message to display instead of mini-buffer contents. This is what
416 the functions error and message make, and command echoing uses it
417 as well. It overrides the minibuf_prompt as well as the buffer. */
a2889657 418
a2889657
JB
419char *echo_area_glyphs;
420
5f5c8ee5
GM
421/* A Lisp string to display instead of mini-buffer contents, analogous
422 to echo_area_glyphs. If this is a string, display that string.
423 Otherwise, if echo_area_glyphs is non-null, display that. */
424
425Lisp_Object echo_area_message;
426
427/* This is the length of the message in echo_area_glyphs or
428 echo_area_message. */
429
90adcf20
RS
430int echo_area_glyphs_length;
431
5f5c8ee5
GM
432/* Value of echo_area_glyphs when it was last acted on. If this is
433 nonzero, there is a message on the frame in the mini-buffer and it
434 should be erased as soon as it is no longer requested to appear. */
435
436char *previous_echo_glyphs;
437Lisp_Object previous_echo_area_message;
438static int previous_echo_glyphs_length;
439
440/* This is the window where the echo area message was displayed. It
441 is always a mini-buffer window, but it may not be the same window
442 currently active as a mini-buffer. */
443
73af359d
RS
444Lisp_Object echo_area_window;
445
a3788d53
RS
446/* Nonzero means multibyte characters were enabled when the echo area
447 message was specified. */
5f5c8ee5 448
a3788d53
RS
449int message_enable_multibyte;
450
5f5c8ee5
GM
451/* True if we should redraw the mode lines on the next redisplay. */
452
a2889657
JB
453int update_mode_lines;
454
5f5c8ee5
GM
455/* Smallest number of characters before the gap at any time since last
456 redisplay that finished. Valid for current buffer when
457 try_window_id can be called. */
458
a2889657
JB
459int beg_unchanged;
460
5f5c8ee5
GM
461/* Smallest number of characters after the gap at any time since last
462 redisplay that finished. Valid for current buffer when
463 try_window_id can be called. */
464
a2889657
JB
465int end_unchanged;
466
5f5c8ee5
GM
467/* MODIFF as of last redisplay that finished; if it matches MODIFF,
468 and overlay_unchanged_modified matches OVERLAY_MODIFF, that means
469 beg_unchanged and end_unchanged contain no useful information. */
470
a2889657
JB
471int unchanged_modified;
472
8850a573 473/* OVERLAY_MODIFF as of last redisplay that finished. */
5f5c8ee5 474
8850a573
RS
475int overlay_unchanged_modified;
476
5f5c8ee5
GM
477/* Nonzero if window sizes or contents have changed since last
478 redisplay that finished */
479
a2889657
JB
480int windows_or_buffers_changed;
481
5f5c8ee5
GM
482/* Nonzero after display_mode_line if %l was used and it displayed a
483 line number. */
484
aa6d10fa
RS
485int line_number_displayed;
486
487/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 488
14510fee 489static int line_number_display_limit;
5992c4f7 490
5f5c8ee5
GM
491/* Number of lines to keep in the message log buffer. t means
492 infinite. nil means don't log at all. */
493
5992c4f7 494Lisp_Object Vmessage_log_max;
d45de95b 495
5f5c8ee5
GM
496/* A scratch glyph row with contents used for generating truncation
497 glyphs. Also used in direct_output_for_insert. */
12adba34 498
5f5c8ee5
GM
499#define MAX_SCRATCH_GLYPHS 100
500struct glyph_row scratch_glyph_row;
501static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 502
5f5c8ee5
GM
503/* Ascent and height of the last line processed by move_it_to. */
504
505static int last_max_ascent, last_height;
506
507/* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
512
513#define TEXT_PROP_DISTANCE_LIMIT 100
514
515/* Non-zero means print traces of redisplay if compiled with
516 GLYPH_DEBUG != 0. */
517
518#if GLYPH_DEBUG
519int trace_redisplay_p;
520#endif
521
522/* Value returned from text property handlers (see below). */
523
524enum prop_handled
3c6595e0 525{
5f5c8ee5
GM
526 HANDLED_NORMALLY,
527 HANDLED_RECOMPUTE_PROPS,
528 HANDLED_OVERLAY_STRING_CONSUMED,
529 HANDLED_RETURN
530};
3c6595e0 531
5f5c8ee5
GM
532/* A description of text properties that redisplay is interested
533 in. */
3c6595e0 534
5f5c8ee5
GM
535struct props
536{
537 /* The name of the property. */
538 Lisp_Object *name;
90adcf20 539
5f5c8ee5
GM
540 /* A unique index for the property. */
541 enum prop_idx idx;
542
543 /* A handler function called to set up iterator IT from the property
544 at IT's current position. Value is used to steer handle_stop. */
545 enum prop_handled (*handler) P_ ((struct it *it));
546};
547
548static enum prop_handled handle_face_prop P_ ((struct it *));
549static enum prop_handled handle_invisible_prop P_ ((struct it *));
550static enum prop_handled handle_display_prop P_ ((struct it *));
551static enum prop_handled handle_overlay_change P_ ((struct it *));
552static enum prop_handled handle_fontified_prop P_ ((struct it *));
553
554/* Properties handled by iterators. */
555
556static struct props it_props[] =
5992c4f7 557{
5f5c8ee5
GM
558 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
559 /* Handle `face' before `display' because some sub-properties of
560 `display' need to know the face. */
561 {&Qface, FACE_PROP_IDX, handle_face_prop},
562 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
563 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
564 {NULL, 0, NULL}
565};
5992c4f7 566
5f5c8ee5
GM
567/* Value is the position described by X. If X is a marker, value is
568 the marker_position of X. Otherwise, value is X. */
12adba34 569
5f5c8ee5 570#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 571
5f5c8ee5 572/* Enumeration returned by some move_it_.* functions internally. */
12adba34 573
5f5c8ee5
GM
574enum move_it_result
575{
576 /* Not used. Undefined value. */
577 MOVE_UNDEFINED,
bab29e15 578
5f5c8ee5
GM
579 /* Move ended at the requested buffer position or ZV. */
580 MOVE_POS_MATCH_OR_ZV,
bab29e15 581
5f5c8ee5
GM
582 /* Move ended at the requested X pixel position. */
583 MOVE_X_REACHED,
12adba34 584
5f5c8ee5
GM
585 /* Move within a line ended at the end of a line that must be
586 continued. */
587 MOVE_LINE_CONTINUED,
588
589 /* Move within a line ended at the end of a line that would
590 be displayed truncated. */
591 MOVE_LINE_TRUNCATED,
ff6c30e5 592
5f5c8ee5
GM
593 /* Move within a line ended at a line end. */
594 MOVE_NEWLINE_OR_CR
595};
12adba34 596
ff6c30e5 597
5f5c8ee5
GM
598\f
599/* Function prototypes. */
600
4fdb80f2 601static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
602static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
603 struct text_pos));
604static int compute_window_start_on_continuation_line P_ ((struct window *));
605static Lisp_Object eval_handler P_ ((Lisp_Object));
606static Lisp_Object eval_form P_ ((Lisp_Object));
607static void insert_left_trunc_glyphs P_ ((struct it *));
608static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
609static void extend_face_to_end_of_line P_ ((struct it *));
610static void append_space P_ ((struct it *, int));
611static void make_cursor_line_fully_visible P_ ((struct window *));
612static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
613static int trailing_whitespace_p P_ ((int));
614static int message_log_check_duplicate P_ ((int, int, int, int));
615int invisible_p P_ ((Lisp_Object, Lisp_Object));
616int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
617static void push_it P_ ((struct it *));
618static void pop_it P_ ((struct it *));
619static void sync_frame_with_window_matrix_rows P_ ((struct window *));
620static void redisplay_internal P_ ((int));
621static void echo_area_display P_ ((int));
622static void redisplay_windows P_ ((Lisp_Object));
623static void redisplay_window P_ ((Lisp_Object, int));
624static void update_menu_bar P_ ((struct frame *, int));
625static int try_window_reusing_current_matrix P_ ((struct window *));
626static int try_window_id P_ ((struct window *));
627static int display_line P_ ((struct it *));
628static void display_mode_lines P_ ((struct window *));
629static void display_mode_line P_ ((struct window *, enum face_id,
630 Lisp_Object));
631static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
632static char *decode_mode_spec P_ ((struct window *, char, int, int));
633static void display_menu_bar P_ ((struct window *));
634static int display_count_lines P_ ((int, int, int, int, int *));
635static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
636 int, int, struct it *, int, int, int, int));
637static void compute_line_metrics P_ ((struct it *));
638static void run_redisplay_end_trigger_hook P_ ((struct it *));
639static int get_overlay_strings P_ ((struct it *));
640static void next_overlay_string P_ ((struct it *));
641void set_iterator_to_next P_ ((struct it *));
642static void reseat P_ ((struct it *, struct text_pos, int));
643static void reseat_1 P_ ((struct it *, struct text_pos, int));
644static void back_to_previous_visible_line_start P_ ((struct it *));
645static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 646static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
647static int next_element_from_display_vector P_ ((struct it *));
648static int next_element_from_string P_ ((struct it *));
649static int next_element_from_c_string P_ ((struct it *));
650static int next_element_from_buffer P_ ((struct it *));
651static int next_element_from_image P_ ((struct it *));
652static int next_element_from_stretch P_ ((struct it *));
653static void load_overlay_strings P_ ((struct it *));
654static void init_from_display_pos P_ ((struct it *, struct window *,
655 struct display_pos *));
656static void reseat_to_string P_ ((struct it *, unsigned char *,
657 Lisp_Object, int, int, int, int));
658static int charset_at_position P_ ((struct text_pos));
659static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
660 int, int, int));
661void move_it_vertically_backward P_ ((struct it *, int));
662static void init_to_row_start P_ ((struct it *, struct window *,
663 struct glyph_row *));
664static void init_to_row_end P_ ((struct it *, struct window *,
665 struct glyph_row *));
666static void back_to_previous_line_start P_ ((struct it *));
667static void forward_to_next_line_start P_ ((struct it *));
668static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
669 Lisp_Object, int));
670static struct text_pos string_pos P_ ((int, Lisp_Object));
671static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
672static int number_of_chars P_ ((unsigned char *, int));
673static void compute_stop_pos P_ ((struct it *));
674static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
675 Lisp_Object));
676static int face_before_or_after_it_pos P_ ((struct it *, int));
677static int next_overlay_change P_ ((int));
678static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
679 Lisp_Object, struct text_pos *));
680
681#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
682#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 683
5f5c8ee5 684#ifdef HAVE_WINDOW_SYSTEM
12adba34 685
5f5c8ee5
GM
686static void update_toolbar P_ ((struct frame *, int));
687static void build_desired_toolbar_string P_ ((struct frame *f));
688static int redisplay_toolbar P_ ((struct frame *));
689static void display_toolbar_line P_ ((struct it *));
12adba34 690
5f5c8ee5 691#endif /* HAVE_WINDOW_SYSTEM */
12adba34 692
5f5c8ee5
GM
693\f
694/***********************************************************************
695 Window display dimensions
696 ***********************************************************************/
12adba34 697
5f5c8ee5
GM
698/* Return the window-relative maximum y + 1 for glyph rows displaying
699 text in window W. This is the height of W minus the height of a
700 mode line, if any. */
701
702INLINE int
703window_text_bottom_y (w)
704 struct window *w;
705{
706 struct frame *f = XFRAME (w->frame);
707 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
708
709 if (WINDOW_WANTS_MODELINE_P (w))
710 height -= CURRENT_MODE_LINE_HEIGHT (w);
711 return height;
f88eb0b6
KH
712}
713
f82aff7c 714
5f5c8ee5
GM
715/* Return the pixel width of display area AREA of window W. AREA < 0
716 means return the total width of W, not including bitmap areas to
717 the left and right of the window. */
ff6c30e5 718
5f5c8ee5
GM
719INLINE int
720window_box_width (w, area)
721 struct window *w;
722 int area;
723{
724 struct frame *f = XFRAME (w->frame);
725 int width = XFASTINT (w->width);
726
727 if (!w->pseudo_window_p)
ff6c30e5 728 {
5f5c8ee5
GM
729 width -= FRAME_SCROLL_BAR_WIDTH (f) + 2 * FRAME_FLAGS_AREA_COLS (f);
730
731 if (area == TEXT_AREA)
732 {
733 if (INTEGERP (w->left_margin_width))
734 width -= XFASTINT (w->left_margin_width);
735 if (INTEGERP (w->right_margin_width))
736 width -= XFASTINT (w->right_margin_width);
737 }
738 else if (area == LEFT_MARGIN_AREA)
739 width = (INTEGERP (w->left_margin_width)
740 ? XFASTINT (w->left_margin_width) : 0);
741 else if (area == RIGHT_MARGIN_AREA)
742 width = (INTEGERP (w->right_margin_width)
743 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 744 }
5f5c8ee5
GM
745
746 return width * CANON_X_UNIT (f);
ff6c30e5 747}
1adc55de 748
1adc55de 749
5f5c8ee5
GM
750/* Return the pixel height of the display area of window W, not
751 including mode lines of W, if any.. */
f88eb0b6 752
5f5c8ee5
GM
753INLINE int
754window_box_height (w)
755 struct window *w;
f88eb0b6 756{
5f5c8ee5
GM
757 struct frame *f = XFRAME (w->frame);
758 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
759
760 if (WINDOW_WANTS_MODELINE_P (w))
761 height -= CURRENT_MODE_LINE_HEIGHT (w);
762
763 if (WINDOW_WANTS_TOP_LINE_P (w))
764 height -= CURRENT_TOP_LINE_HEIGHT (w);
765
766 return height;
5992c4f7
KH
767}
768
769
5f5c8ee5
GM
770/* Return the frame-relative coordinate of the left edge of display
771 area AREA of window W. AREA < 0 means return the left edge of the
772 whole window, to the right of any bitmap area at the left side of
773 W. */
5992c4f7 774
5f5c8ee5
GM
775INLINE int
776window_box_left (w, area)
777 struct window *w;
778 int area;
90adcf20 779{
5f5c8ee5
GM
780 struct frame *f = XFRAME (w->frame);
781 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 782
5f5c8ee5 783 if (!w->pseudo_window_p)
90adcf20 784 {
5f5c8ee5
GM
785 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
786 + FRAME_FLAGS_AREA_WIDTH (f));
787
788 if (area == TEXT_AREA)
789 x += window_box_width (w, LEFT_MARGIN_AREA);
790 else if (area == RIGHT_MARGIN_AREA)
791 x += (window_box_width (w, LEFT_MARGIN_AREA)
792 + window_box_width (w, TEXT_AREA));
90adcf20 793 }
73af359d 794
5f5c8ee5
GM
795 return x;
796}
90adcf20 797
b6436d4e 798
5f5c8ee5
GM
799/* Return the frame-relative coordinate of the right edge of display
800 area AREA of window W. AREA < 0 means return the left edge of the
801 whole window, to the left of any bitmap area at the right side of
802 W. */
ded34426 803
5f5c8ee5
GM
804INLINE int
805window_box_right (w, area)
806 struct window *w;
807 int area;
808{
809 return window_box_left (w, area) + window_box_width (w, area);
810}
811
812
813/* Get the bounding box of the display area AREA of window W, without
814 mode lines, in frame-relative coordinates. AREA < 0 means the
815 whole window, not including bitmap areas to the left and right of
816 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
817 coordinates of the upper-left corner of the box. Return in
818 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
819
820INLINE void
821window_box (w, area, box_x, box_y, box_width, box_height)
822 struct window *w;
823 int area;
824 int *box_x, *box_y, *box_width, *box_height;
825{
826 struct frame *f = XFRAME (w->frame);
827
828 *box_width = window_box_width (w, area);
829 *box_height = window_box_height (w);
830 *box_x = window_box_left (w, area);
831 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
832 + XFASTINT (w->top) * CANON_Y_UNIT (f));
833 if (WINDOW_WANTS_TOP_LINE_P (w))
834 *box_y += CURRENT_TOP_LINE_HEIGHT (w);
ded34426 835}
1adc55de 836
1adc55de 837
5f5c8ee5
GM
838/* Get the bounding box of the display area AREA of window W, without
839 mode lines. AREA < 0 means the whole window, not including bitmap
840 areas to the left and right of the window. Return in *TOP_LEFT_X
841 and TOP_LEFT_Y the frame-relative pixel coordinates of the
842 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
843 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
844 box. */
ded34426 845
5f5c8ee5
GM
846INLINE void
847window_box_edges (w, area, top_left_x, top_left_y,
848 bottom_right_x, bottom_right_y)
849 struct window *w;
850 int area;
851 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 852{
5f5c8ee5
GM
853 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
854 bottom_right_y);
855 *bottom_right_x += *top_left_x;
856 *bottom_right_y += *top_left_y;
48ae5f0a
KH
857}
858
5f5c8ee5
GM
859
860\f
861/***********************************************************************
862 Utilities
863 ***********************************************************************/
864
4fdb80f2
GM
865/* Return the next character from STR which is MAXLEN bytes long.
866 Return in *LEN the length of the character. This is like
867 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
868 we find one, we return a `?', but with the length of the illegal
869 character. */
870
871static INLINE int
7a5b8a93 872string_char_and_length (str, maxlen, len)
4fdb80f2 873 unsigned char *str;
7a5b8a93 874 int maxlen, *len;
4fdb80f2
GM
875{
876 int c;
877
878 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
879 if (!CHAR_VALID_P (c, 1))
880 /* We may not change the length here because other places in Emacs
881 don't use this function, i.e. they silently accept illegal
882 characters. */
883 c = '?';
884
885 return c;
886}
887
888
889
5f5c8ee5
GM
890/* Given a position POS containing a valid character and byte position
891 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
892
893static struct text_pos
894string_pos_nchars_ahead (pos, string, nchars)
895 struct text_pos pos;
896 Lisp_Object string;
897 int nchars;
0b1005ef 898{
5f5c8ee5
GM
899 xassert (STRINGP (string) && nchars >= 0);
900
901 if (STRING_MULTIBYTE (string))
902 {
903 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
904 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
905 int len;
906
907 while (nchars--)
908 {
4fdb80f2 909 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
910 p += len, rest -= len;
911 xassert (rest >= 0);
912 CHARPOS (pos) += 1;
913 BYTEPOS (pos) += len;
914 }
915 }
916 else
917 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
918
919 return pos;
0a9dc68b
RS
920}
921
0a9dc68b 922
5f5c8ee5
GM
923/* Value is the text position, i.e. character and byte position,
924 for character position CHARPOS in STRING. */
925
926static INLINE struct text_pos
927string_pos (charpos, string)
928 int charpos;
0a9dc68b 929 Lisp_Object string;
0a9dc68b 930{
5f5c8ee5
GM
931 struct text_pos pos;
932 xassert (STRINGP (string));
933 xassert (charpos >= 0);
934 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
935 return pos;
936}
937
938
939/* Value is a text position, i.e. character and byte position, for
940 character position CHARPOS in C string S. MULTIBYTE_P non-zero
941 means recognize multibyte characters. */
942
943static struct text_pos
944c_string_pos (charpos, s, multibyte_p)
945 int charpos;
946 unsigned char *s;
947 int multibyte_p;
948{
949 struct text_pos pos;
950
951 xassert (s != NULL);
952 xassert (charpos >= 0);
953
954 if (multibyte_p)
0a9dc68b 955 {
5f5c8ee5
GM
956 int rest = strlen (s), len;
957
958 SET_TEXT_POS (pos, 0, 0);
959 while (charpos--)
0a9dc68b 960 {
4fdb80f2 961 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
962 s += len, rest -= len;
963 xassert (rest >= 0);
964 CHARPOS (pos) += 1;
965 BYTEPOS (pos) += len;
0a9dc68b
RS
966 }
967 }
5f5c8ee5
GM
968 else
969 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 970
5f5c8ee5
GM
971 return pos;
972}
0a9dc68b 973
0a9dc68b 974
5f5c8ee5
GM
975/* Value is the number of characters in C string S. MULTIBYTE_P
976 non-zero means recognize multibyte characters. */
0a9dc68b 977
5f5c8ee5
GM
978static int
979number_of_chars (s, multibyte_p)
980 unsigned char *s;
981 int multibyte_p;
982{
983 int nchars;
984
985 if (multibyte_p)
986 {
987 int rest = strlen (s), len;
988 unsigned char *p = (unsigned char *) s;
0a9dc68b 989
5f5c8ee5
GM
990 for (nchars = 0; rest > 0; ++nchars)
991 {
4fdb80f2 992 string_char_and_length (p, rest, &len);
5f5c8ee5 993 rest -= len, p += len;
0a9dc68b
RS
994 }
995 }
5f5c8ee5
GM
996 else
997 nchars = strlen (s);
998
999 return nchars;
0b1005ef
KH
1000}
1001
5f5c8ee5
GM
1002
1003/* Compute byte position NEWPOS->bytepos corresponding to
1004 NEWPOS->charpos. POS is a known position in string STRING.
1005 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1006
5f5c8ee5
GM
1007static void
1008compute_string_pos (newpos, pos, string)
1009 struct text_pos *newpos, pos;
1010 Lisp_Object string;
76412d64 1011{
5f5c8ee5
GM
1012 xassert (STRINGP (string));
1013 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1014
1015 if (STRING_MULTIBYTE (string))
1016 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1017 string);
1018 else
1019 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1020}
1021
9c74a0dd 1022
5f5c8ee5
GM
1023/* Return the charset of the character at position POS in
1024 current_buffer. */
1adc55de 1025
5f5c8ee5
GM
1026static int
1027charset_at_position (pos)
1028 struct text_pos pos;
a2889657 1029{
5f5c8ee5
GM
1030 int c, multibyte_p;
1031 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1032
1033 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1034 if (multibyte_p)
a2889657 1035 {
5f5c8ee5
GM
1036 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1037 - BYTEPOS (pos));
1038 int len;
4fdb80f2 1039 c = string_char_and_length (p, maxlen, &len);
a2889657 1040 }
5f5c8ee5
GM
1041 else
1042 c = *p;
1043
1044 return CHAR_CHARSET (c);
1045}
1046
1047
1048\f
1049/***********************************************************************
1050 Lisp form evaluation
1051 ***********************************************************************/
1052
1053/* Error handler for eval_form. */
1054
1055static Lisp_Object
1056eval_handler (arg)
1057 Lisp_Object arg;
1058{
1059 return Qnil;
1060}
1061
1062
1063/* Evaluate SEXPR and return the result, or nil if something went
1064 wrong. */
1065
1066static Lisp_Object
1067eval_form (sexpr)
1068 Lisp_Object sexpr;
1069{
1070 int count = specpdl_ptr - specpdl;
1071 Lisp_Object val;
1072 specbind (Qinhibit_redisplay, Qt);
1073 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1074 return unbind_to (count, val);
1075}
1076
1077
1078\f
1079/***********************************************************************
1080 Debugging
1081 ***********************************************************************/
1082
1083#if 0
1084
1085/* Define CHECK_IT to perform sanity checks on iterators.
1086 This is for debugging. It is too slow to do unconditionally. */
1087
1088static void
1089check_it (it)
1090 struct it *it;
1091{
1092 if (it->method == next_element_from_string)
a2889657 1093 {
5f5c8ee5
GM
1094 xassert (STRINGP (it->string));
1095 xassert (IT_STRING_CHARPOS (*it) >= 0);
1096 }
1097 else if (it->method == next_element_from_buffer)
1098 {
1099 /* Check that character and byte positions agree. */
1100 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1101 }
73af359d 1102
5f5c8ee5
GM
1103 if (it->dpvec)
1104 xassert (it->current.dpvec_index >= 0);
1105 else
1106 xassert (it->current.dpvec_index < 0);
1107}
1f40cad2 1108
5f5c8ee5
GM
1109#define CHECK_IT(IT) check_it ((IT))
1110
1111#else /* not 0 */
1112
1113#define CHECK_IT(IT) (void) 0
1114
1115#endif /* not 0 */
1116
1117
1118#if GLYPH_DEBUG
1119
1120/* Check that the window end of window W is what we expect it
1121 to be---the last row in the current matrix displaying text. */
1122
1123static void
1124check_window_end (w)
1125 struct window *w;
1126{
1127 if (!MINI_WINDOW_P (w)
1128 && !NILP (w->window_end_valid))
1129 {
1130 struct glyph_row *row;
1131 xassert ((row = MATRIX_ROW (w->current_matrix,
1132 XFASTINT (w->window_end_vpos)),
1133 !row->enabled_p
1134 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1135 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1136 }
1137}
1138
1139#define CHECK_WINDOW_END(W) check_window_end ((W))
1140
1141#else /* not GLYPH_DEBUG */
1142
1143#define CHECK_WINDOW_END(W) (void) 0
1144
1145#endif /* not GLYPH_DEBUG */
1146
1147
1148\f
1149/***********************************************************************
1150 Iterator initialization
1151 ***********************************************************************/
1152
1153/* Initialize IT for displaying current_buffer in window W, starting
1154 at character position CHARPOS. CHARPOS < 0 means that no buffer
1155 position is specified which is useful when the iterator is assigned
1156 a position later. BYTEPOS is the byte position corresponding to
1157 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1158
1159 If ROW is not null, calls to produce_glyphs with IT as parameter
1160 will produce glyphs in that row.
1161
1162 BASE_FACE_ID is the id of a base face to use. It must be one of
1163 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1164 TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
1165 displaying the toolbar.
1166
1167 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1168 TOP_LINE_FACE_ID, the iterator will be initialized to use the
1169 corresponding mode line glyph row of the desired matrix of W. */
1170
1171void
1172init_iterator (it, w, charpos, bytepos, row, base_face_id)
1173 struct it *it;
1174 struct window *w;
1175 int charpos, bytepos;
1176 struct glyph_row *row;
1177 enum face_id base_face_id;
1178{
1179 int highlight_region_p;
1180 Lisp_Object value;
1181
1182 /* Some precondition checks. */
1183 xassert (w != NULL && it != NULL);
1184 xassert (charpos < 0 || current_buffer == XBUFFER (w->buffer));
1185 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1186
1187 /* If face attributes have been changed since the last redisplay,
1188 free realized faces now because they depend on face definitions
1189 that might have changed. */
1190 if (face_change_count)
1191 {
1192 face_change_count = 0;
1193 free_all_realized_faces (Qnil);
1194 }
1195
1196 /* Use one of the mode line rows of W's desired matrix if
1197 appropriate. */
1198 if (row == NULL)
1199 {
1200 if (base_face_id == MODE_LINE_FACE_ID)
1201 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1202 else if (base_face_id == TOP_LINE_FACE_ID)
1203 row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
1204 }
1205
1206 /* Clear IT. */
1207 bzero (it, sizeof *it);
1208 it->current.overlay_string_index = -1;
1209 it->current.dpvec_index = -1;
1210 it->charset = CHARSET_ASCII;
1211 it->base_face_id = base_face_id;
1212
1213 /* The window in which we iterate over current_buffer: */
1214 XSETWINDOW (it->window, w);
1215 it->w = w;
1216 it->f = XFRAME (w->frame);
1217
1218 /* If realized faces have been removed, e.g. because of face
1219 attribute changes of named faces, recompute them. */
1220 if (FRAME_FACE_CACHE (it->f)->used == 0)
1221 recompute_basic_faces (it->f);
1222
1223 /* Should we highlight trailing whitespace? */
1224 value = find_symbol_value (Qshow_trailing_whitespace);
1225 it->show_trailing_whitespace_p
1226 = EQ (value, Qunbound) ? 0 : !NILP (value);
1227
1228 /* Current value of the `space-width', and 'height' properties. */
1229 it->space_width = Qnil;
1230 it->font_height = Qnil;
1231
1232 /* Are control characters displayed as `^C'? */
1233 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1234
1235 /* -1 means everything between a CR and the following line end
1236 is invisible. >0 means lines indented more than this value are
1237 invisible. */
1238 it->selective = (INTEGERP (current_buffer->selective_display)
1239 ? XFASTINT (current_buffer->selective_display)
1240 : (!NILP (current_buffer->selective_display)
1241 ? -1 : 0));
1242 it->selective_display_ellipsis_p
1243 = !NILP (current_buffer->selective_display_ellipses);
1244
1245 /* Display table to use. */
1246 it->dp = window_display_table (w);
1247
1248 /* Are multibyte characters enabled in current_buffer? */
1249 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1250
1251 /* Non-zero if we should highlight the region. */
1252 highlight_region_p
1253 = (!NILP (Vtransient_mark_mode)
1254 && !NILP (current_buffer->mark_active)
1255 && XMARKER (current_buffer->mark)->buffer != 0);
1256
1257 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1258 start and end of a visible region in window IT->w. Set both to
1259 -1 to indicate no region. */
1260 if (highlight_region_p
1261 /* Maybe highlight only in selected window. */
1262 && (/* Either show region everywhere. */
1263 highlight_nonselected_windows
1264 /* Or show region in the selected window. */
1265 || w == XWINDOW (selected_window)
1266 /* Or show the region if we are in the mini-buffer and W is
1267 the window the mini-buffer refers to. */
1268 || (MINI_WINDOW_P (XWINDOW (selected_window))
1269 && w == XWINDOW (Vminibuf_scroll_window))))
1270 {
1271 int charpos = marker_position (current_buffer->mark);
1272 it->region_beg_charpos = min (PT, charpos);
1273 it->region_end_charpos = max (PT, charpos);
1274 }
1275 else
1276 it->region_beg_charpos = it->region_end_charpos = -1;
1277
1278 /* Get the position at which the redisplay_end_trigger hook should
1279 be run, if it is to be run at all. */
1280 if (MARKERP (w->redisplay_end_trigger)
1281 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1282 it->redisplay_end_trigger_charpos
1283 = marker_position (w->redisplay_end_trigger);
1284 else if (INTEGERP (w->redisplay_end_trigger))
1285 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1286
1287 /* Correct bogus values of tab_width. */
1288 it->tab_width = XINT (current_buffer->tab_width);
1289 if (it->tab_width <= 0 || it->tab_width > 1000)
1290 it->tab_width = 8;
1291
1292 /* Are lines in the display truncated? */
1293 it->truncate_lines_p
1294 = (base_face_id != DEFAULT_FACE_ID
1295 || XINT (it->w->hscroll)
1296 || (truncate_partial_width_windows
1297 && !WINDOW_FULL_WIDTH_P (it->w))
1298 || !NILP (current_buffer->truncate_lines));
1299
1300 /* Get dimensions of truncation and continuation glyphs. These are
1301 displayed as bitmaps under X, so we don't need them for such
1302 frames. */
1303 if (!FRAME_WINDOW_P (it->f))
1304 {
1305 if (it->truncate_lines_p)
1306 {
1307 /* We will need the truncation glyph. */
1308 xassert (it->glyph_row == NULL);
1309 produce_special_glyphs (it, IT_TRUNCATION);
1310 it->truncation_pixel_width = it->pixel_width;
1311 }
1312 else
1313 {
1314 /* We will need the continuation glyph. */
1315 xassert (it->glyph_row == NULL);
1316 produce_special_glyphs (it, IT_CONTINUATION);
1317 it->continuation_pixel_width = it->pixel_width;
1318 }
1319
1320 /* Reset these values to zero becaue the produce_special_glyphs
1321 above has changed them. */
1322 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1323 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1324 }
1325
1326 /* Set this after getting the dimensions of truncation and
1327 continuation glyphs, so that we don't produce glyphs when calling
1328 produce_special_glyphs, above. */
1329 it->glyph_row = row;
1330 it->area = TEXT_AREA;
1331
1332 /* Get the dimensions of the display area. The display area
1333 consists of the visible window area plus a horizontally scrolled
1334 part to the left of the window. All x-values are relative to the
1335 start of this total display area. */
1336 if (base_face_id != DEFAULT_FACE_ID)
1337 {
1338 /* Mode lines, menu bar in terminal frames. */
1339 it->first_visible_x = 0;
1340 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1341 }
1342 else
1343 {
1344 it->first_visible_x
1345 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1346 it->last_visible_x = (it->first_visible_x
1347 + window_box_width (w, TEXT_AREA));
1348
1349 /* If we truncate lines, leave room for the truncator glyph(s) at
1350 the right margin. Otherwise, leave room for the continuation
1351 glyph(s). Truncation and continuation glyphs are not inserted
1352 for window-based redisplay. */
1353 if (!FRAME_WINDOW_P (it->f))
1354 {
1355 if (it->truncate_lines_p)
1356 it->last_visible_x -= it->truncation_pixel_width;
1357 else
1358 it->last_visible_x -= it->continuation_pixel_width;
1359 }
1360
1361 it->top_line_p = WINDOW_WANTS_TOP_LINE_P (w);
1362 it->current_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w) + w->vscroll;
1363 }
1364
1365 /* Leave room for a border glyph. */
1366 if (!FRAME_WINDOW_P (it->f)
1367 && !WINDOW_RIGHTMOST_P (it->w))
1368 it->last_visible_x -= 1;
1369
1370 it->last_visible_y = window_text_bottom_y (w);
1371
1372 /* For mode lines and alike, arrange for the first glyph having a
1373 left box line if the face specifies a box. */
1374 if (base_face_id != DEFAULT_FACE_ID)
1375 {
1376 struct face *face;
1377
1378 it->face_id = base_face_id;
1379
1380 /* If we have a boxed mode line, make the first character appear
1381 with a left box line. */
1382 face = FACE_FROM_ID (it->f, base_face_id);
1383 if (face->box != FACE_NO_BOX)
1384 it->start_of_box_run_p = 1;
1385 }
1386
1387 /* If a buffer position was specified, set the iterator there,
1388 getting overlays and face properties from that position. */
1389 if (charpos > 0)
1390 {
1391 it->end_charpos = ZV;
1392 it->face_id = -1;
1393 IT_CHARPOS (*it) = charpos;
1394
1395 /* Compute byte position if not specified. */
1396 if (bytepos <= 0)
1397 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1398 else
1399 IT_BYTEPOS (*it) = bytepos;
1400
1401 /* Compute faces etc. */
1402 reseat (it, it->current.pos, 1);
1403 }
1404
1405 CHECK_IT (it);
1406}
1407
1408
1409/* Initialize IT for the display of window W with window start POS. */
1410
1411void
1412start_display (it, w, pos)
1413 struct it *it;
1414 struct window *w;
1415 struct text_pos pos;
1416{
1417 int start_at_line_beg_p;
1418 struct glyph_row *row;
1419 int first_vpos = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
1420 int first_y;
1421
1422 row = w->desired_matrix->rows + first_vpos;
1423 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1424 first_y = it->current_y;
1425
1426 /* If window start is not at a line start, move back to the line
1427 start. This makes sure that we take continuation lines into
1428 account. */
1429 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1430 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1431 if (!start_at_line_beg_p)
1432 reseat_at_previous_visible_line_start (it);
1433
1434#if NO_PROMPT_IN_BUFFER
1435 /* Take the mini-buffer prompt width into account for tab
1436 calculations. */
1437 if (MINI_WINDOW_P (w) && IT_CHARPOS (*it) == BEGV)
1438 {
1439 /* Why is mini-buffer_prompt_width guaranteed to be set here? */
1440 it->prompt_width = minibuf_prompt_pixel_width;
1441 }
1442#endif /* NO_PROMPT_IN_BUFFER */
1443
1444 /* If window start is not at a line start, skip forward to POS to
1445 get the correct continuation_lines_width and current_x. */
1446 if (!start_at_line_beg_p)
1447 {
1448 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1449
1450 /* If lines are continued, this line may end in the middle of a
1451 multi-glyph character (e.g. a control character displayed as
1452 \003, or in the middle of an overlay string). In this case
1453 move_it_to above will not have taken us to the start of
1454 the continuation line but to the end of the continued line. */
1455 if (!it->truncate_lines_p && it->current_x > 0)
1456 {
1457 if (it->current.dpvec_index >= 0
1458 || it->current.overlay_string_index >= 0)
1459 {
1460 set_iterator_to_next (it);
1461 move_it_in_display_line_to (it, -1, -1, 0);
1462 }
1463 it->continuation_lines_width += it->current_x;
1464 }
1465
1466 it->current_y = first_y;
1467 it->vpos = 0;
1468 it->current_x = it->hpos = 0;
1469 }
1470
1471#if 0 /* Don't assert the following because start_display is sometimes
1472 called intentionally with a window start that is not at a
1473 line start. Please leave this code in as a comment. */
1474
1475 /* Window start should be on a line start, now. */
1476 xassert (it->continuation_lines_width
1477 || IT_CHARPOS (it) == BEGV
1478 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1479#endif /* 0 */
1480}
1481
1482
1483/* Initialize IT for stepping through current_buffer in window W,
1484 starting at position POS that includes overlay string and display
1485 vector/ control character translation position information. */
1486
1487static void
1488init_from_display_pos (it, w, pos)
1489 struct it *it;
1490 struct window *w;
1491 struct display_pos *pos;
1492{
1493 /* Keep in mind: the call to reseat in init_iterator skips invisible
1494 text, so we might end up at a position different from POS. This
1495 is only a problem when POS is a row start after a newline and an
1496 overlay starts there with an after-string, and the overlay has an
1497 invisible property. Since we don't skip invisible text in
1498 display_line and elsewhere immediately after consuming the
1499 newline before the row start, such a POS will not be in a string,
1500 but the call to init_iterator below will move us to the
1501 after-string. */
1502 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1503 NULL, DEFAULT_FACE_ID);
1504
1505 /* If position is within an overlay string, set up IT to
1506 the right overlay string. */
1507 if (pos->overlay_string_index >= 0)
1508 {
1509 int relative_index;
1510
1511 /* We already have the first chunk of overlay strings in
1512 IT->overlay_strings. Load more until the one for
1513 pos->overlay_string_index is in IT->overlay_strings. */
1514 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1515 {
1516 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1517 it->current.overlay_string_index = 0;
1518 while (n--)
1519 {
1520 load_overlay_strings (it);
1521 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1522 }
1523 }
1524
1525 it->current.overlay_string_index = pos->overlay_string_index;
1526 relative_index = (it->current.overlay_string_index
1527 % OVERLAY_STRING_CHUNK_SIZE);
1528 it->string = it->overlay_strings[relative_index];
1529 it->current.string_pos = pos->string_pos;
1530 it->method = next_element_from_string;
1531 }
1532 else if (CHARPOS (pos->string_pos) >= 0)
1533 {
1534 /* Recorded position is not in an overlay string, but in another
1535 string. This can only be a string from a `display' property.
1536 IT should already be filled with that string. */
1537 it->current.string_pos = pos->string_pos;
1538 xassert (STRINGP (it->string));
1539 }
1540
1541 /* Restore position in display vector translations or control
1542 character translations. */
1543 if (pos->dpvec_index >= 0)
1544 {
1545 /* This fills IT->dpvec. */
1546 get_next_display_element (it);
1547 xassert (it->dpvec && it->current.dpvec_index == 0);
1548 it->current.dpvec_index = pos->dpvec_index;
1549 }
1550
1551 CHECK_IT (it);
1552}
1553
1554
1555/* Initialize IT for stepping through current_buffer in window W
1556 starting at ROW->start. */
1557
1558static void
1559init_to_row_start (it, w, row)
1560 struct it *it;
1561 struct window *w;
1562 struct glyph_row *row;
1563{
1564 init_from_display_pos (it, w, &row->start);
1565 it->continuation_lines_width = row->continuation_lines_width;
1566 CHECK_IT (it);
1567}
1568
1569
1570/* Initialize IT for stepping through current_buffer in window W
1571 starting in the line following ROW, i.e. starting at ROW->end. */
1572
1573static void
1574init_to_row_end (it, w, row)
1575 struct it *it;
1576 struct window *w;
1577 struct glyph_row *row;
1578{
1579 init_from_display_pos (it, w, &row->end);
1580
1581 if (row->continued_p)
1582 it->continuation_lines_width = (row->continuation_lines_width
1583 + row->pixel_width);
1584 CHECK_IT (it);
1585}
1586
1587
1588
1589\f
1590/***********************************************************************
1591 Text properties
1592 ***********************************************************************/
1593
1594/* Called when IT reaches IT->stop_charpos. Handle text property and
1595 overlay changes. Set IT->stop_charpos to the next position where
1596 to stop. */
1597
1598static void
1599handle_stop (it)
1600 struct it *it;
1601{
1602 enum prop_handled handled;
1603 int handle_overlay_change_p = 1;
1604 struct props *p;
1605
1606 it->dpvec = NULL;
1607 it->current.dpvec_index = -1;
1608
1609 do
1610 {
1611 handled = HANDLED_NORMALLY;
1612
1613 /* Call text property handlers. */
1614 for (p = it_props; p->handler; ++p)
1615 {
1616 handled = p->handler (it);
1617
1618 if (handled == HANDLED_RECOMPUTE_PROPS)
1619 break;
1620 else if (handled == HANDLED_RETURN)
1621 return;
1622 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1623 handle_overlay_change_p = 0;
1624 }
1625
1626 if (handled != HANDLED_RECOMPUTE_PROPS)
1627 {
1628 /* Don't check for overlay strings below when set to deliver
1629 characters from a display vector. */
1630 if (it->method == next_element_from_display_vector)
1631 handle_overlay_change_p = 0;
1632
1633 /* Handle overlay changes. */
1634 if (handle_overlay_change_p)
1635 handled = handle_overlay_change (it);
1636
1637 /* Determine where to stop next. */
1638 if (handled == HANDLED_NORMALLY)
1639 compute_stop_pos (it);
1640 }
1641 }
1642 while (handled == HANDLED_RECOMPUTE_PROPS);
1643}
1644
1645
1646/* Compute IT->stop_charpos from text property and overlay change
1647 information for IT's current position. */
1648
1649static void
1650compute_stop_pos (it)
1651 struct it *it;
1652{
1653 register INTERVAL iv, next_iv;
1654 Lisp_Object object, limit, position;
1655
1656 /* If nowhere else, stop at the end. */
1657 it->stop_charpos = it->end_charpos;
1658
1659 if (STRINGP (it->string))
1660 {
1661 /* Strings are usually short, so don't limit the search for
1662 properties. */
1663 object = it->string;
1664 limit = Qnil;
1665 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1666 }
1667 else
1668 {
1669 int charpos;
1670
1671 /* If next overlay change is in front of the current stop pos
1672 (which is IT->end_charpos), stop there. Note: value of
1673 next_overlay_change is point-max if no overlay change
1674 follows. */
1675 charpos = next_overlay_change (IT_CHARPOS (*it));
1676 if (charpos < it->stop_charpos)
1677 it->stop_charpos = charpos;
1678
1679 /* If showing the region, we have to stop at the region
1680 start or end because the face might change there. */
1681 if (it->region_beg_charpos > 0)
1682 {
1683 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1684 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1685 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1686 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1687 }
1688
1689 /* Set up variables for computing the stop position from text
1690 property changes. */
1691 XSETBUFFER (object, current_buffer);
1692 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1693 XSETFASTINT (position, IT_CHARPOS (*it));
1694
1695 }
1696
1697 /* Get the interval containing IT's position. Value is a null
1698 interval if there isn't such an interval. */
1699 iv = validate_interval_range (object, &position, &position, 0);
1700 if (!NULL_INTERVAL_P (iv))
1701 {
1702 Lisp_Object values_here[LAST_PROP_IDX];
1703 struct props *p;
1704
1705 /* Get properties here. */
1706 for (p = it_props; p->handler; ++p)
1707 values_here[p->idx] = textget (iv->plist, *p->name);
1708
1709 /* Look for an interval following iv that has different
1710 properties. */
1711 for (next_iv = next_interval (iv);
1712 (!NULL_INTERVAL_P (next_iv)
1713 && (NILP (limit)
1714 || XFASTINT (limit) > next_iv->position));
1715 next_iv = next_interval (next_iv))
1716 {
1717 for (p = it_props; p->handler; ++p)
1718 {
1719 Lisp_Object new_value;
1720
1721 new_value = textget (next_iv->plist, *p->name);
1722 if (!EQ (values_here[p->idx], new_value))
1723 break;
1724 }
1725
1726 if (p->handler)
1727 break;
1728 }
1729
1730 if (!NULL_INTERVAL_P (next_iv))
1731 {
1732 if (INTEGERP (limit)
1733 && next_iv->position >= XFASTINT (limit))
1734 /* No text property change up to limit. */
1735 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1736 else
1737 /* Text properties change in next_iv. */
1738 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1739 }
1740 }
1741
1742 xassert (STRINGP (it->string)
1743 || (it->stop_charpos >= BEGV
1744 && it->stop_charpos >= IT_CHARPOS (*it)));
1745}
1746
1747
1748/* Return the position of the next overlay change after POS in
1749 current_buffer. Value is point-max if no overlay change
1750 follows. This is like `next-overlay-change' but doesn't use
1751 xmalloc. */
1752
1753static int
1754next_overlay_change (pos)
1755 int pos;
1756{
1757 int noverlays;
1758 int endpos;
1759 Lisp_Object *overlays;
1760 int len;
1761 int i;
1762
1763 /* Get all overlays at the given position. */
1764 len = 10;
1765 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1766 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1767 if (noverlays > len)
1768 {
1769 len = noverlays;
1770 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1771 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1772 }
1773
1774 /* If any of these overlays ends before endpos,
1775 use its ending point instead. */
1776 for (i = 0; i < noverlays; ++i)
1777 {
1778 Lisp_Object oend;
1779 int oendpos;
1780
1781 oend = OVERLAY_END (overlays[i]);
1782 oendpos = OVERLAY_POSITION (oend);
1783 endpos = min (endpos, oendpos);
1784 }
1785
1786 return endpos;
1787}
1788
1789
1790\f
1791/***********************************************************************
1792 Fontification
1793 ***********************************************************************/
1794
1795/* Handle changes in the `fontified' property of the current buffer by
1796 calling hook functions from Qfontification_functions to fontify
1797 regions of text. */
1798
1799static enum prop_handled
1800handle_fontified_prop (it)
1801 struct it *it;
1802{
1803 Lisp_Object prop, pos;
1804 enum prop_handled handled = HANDLED_NORMALLY;
1805
1806 /* Get the value of the `fontified' property at IT's current buffer
1807 position. (The `fontified' property doesn't have a special
1808 meaning in strings.) If the value is nil, call functions from
1809 Qfontification_functions. */
1810 if (!STRINGP (it->string)
1811 && it->s == NULL
1812 && !NILP (Vfontification_functions)
1813 && (pos = make_number (IT_CHARPOS (*it)),
1814 prop = Fget_char_property (pos, Qfontified, Qnil),
1815 NILP (prop)))
1816 {
1817 Lisp_Object args[2];
1818
1819 /* Run the hook functions. */
1820 args[0] = Qfontification_functions;
1821 args[1] = pos;
1822 Frun_hook_with_args (make_number (2), args);
1823
1824 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1825 something. This avoids an endless loop if they failed to
1826 fontify the text for which reason ever. */
1827 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1828 handled = HANDLED_RECOMPUTE_PROPS;
1829 }
1830
1831 return handled;
1832}
1833
1834
1835\f
1836/***********************************************************************
1837 Faces
1838 ***********************************************************************/
1839
1840/* Set up iterator IT from face properties at its current position.
1841 Called from handle_stop. */
1842
1843static enum prop_handled
1844handle_face_prop (it)
1845 struct it *it;
1846{
1847 int new_face_id, next_stop;
1848
1849 if (!STRINGP (it->string))
1850 {
1851 new_face_id
1852 = face_at_buffer_position (it->w,
1853 IT_CHARPOS (*it),
1854 it->region_beg_charpos,
1855 it->region_end_charpos,
1856 &next_stop,
1857 (IT_CHARPOS (*it)
1858 + TEXT_PROP_DISTANCE_LIMIT),
1859 0);
1860
1861 /* Is this a start of a run of characters with box face?
1862 Caveat: this can be called for a freshly initialized
1863 iterator; face_id is -1 is this case. We know that the new
1864 face will not change until limit, i.e. if the new face has a
1865 box, all characters up to limit will have one. But, as
1866 usual, we don't know whether limit is really the end. */
1867 if (new_face_id != it->face_id)
1868 {
1869 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1870
1871 /* If new face has a box but old face has not, this is
1872 the start of a run of characters with box, i.e. it has
1873 a shadow on the left side. The value of face_id of the
1874 iterator will be -1 if this is the initial call that gets
1875 the face. In this case, we have to look in front of IT's
1876 position and see whether there is a face != new_face_id. */
1877 it->start_of_box_run_p
1878 = (new_face->box != FACE_NO_BOX
1879 && (it->face_id >= 0
1880 || IT_CHARPOS (*it) == BEG
1881 || new_face_id != face_before_it_pos (it)));
1882 it->face_box_p = new_face->box != FACE_NO_BOX;
1883 }
1884 }
1885 else
1886 {
1887 new_face_id
1888 = face_at_string_position (it->w,
1889 it->string,
1890 IT_STRING_CHARPOS (*it),
1891 (it->current.overlay_string_index >= 0
1892 ? IT_CHARPOS (*it)
1893 : 0),
1894 it->region_beg_charpos,
1895 it->region_end_charpos,
1896 &next_stop,
1897 it->base_face_id);
1898
1899#if 0 /* This shouldn't be neccessary. Let's check it. */
1900 /* If IT is used to display a mode line we would really like to
1901 use the mode line face instead of the frame's default face. */
1902 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1903 && new_face_id == DEFAULT_FACE_ID)
1904 new_face_id = MODE_LINE_FACE_ID;
1905#endif
1906
1907 /* Is this a start of a run of characters with box? Caveat:
1908 this can be called for a freshly allocated iterator; face_id
1909 is -1 is this case. We know that the new face will not
1910 change until the next check pos, i.e. if the new face has a
1911 box, all characters up to that position will have a
1912 box. But, as usual, we don't know whether that position
1913 is really the end. */
1914 if (new_face_id != it->face_id)
1915 {
1916 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1917 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1918
1919 /* If new face has a box but old face hasn't, this is the
1920 start of a run of characters with box, i.e. it has a
1921 shadow on the left side. */
1922 it->start_of_box_run_p
1923 = new_face->box && (old_face == NULL || !old_face->box);
1924 it->face_box_p = new_face->box != FACE_NO_BOX;
1925 }
1926 }
1927
1928 it->face_id = new_face_id;
1929 it->charset = CHARSET_ASCII;
1930 return HANDLED_NORMALLY;
1931}
1932
1933
1934/* Compute the face one character before or after the current position
1935 of IT. BEFORE_P non-zero means get the face in front of IT's
1936 position. Value is the id of the face. */
1937
1938static int
1939face_before_or_after_it_pos (it, before_p)
1940 struct it *it;
1941 int before_p;
1942{
1943 int face_id, limit;
1944 int next_check_charpos;
1945 struct text_pos pos;
1946
1947 xassert (it->s == NULL);
1948
1949 if (STRINGP (it->string))
1950 {
1951 /* No face change past the end of the string (for the case
1952 we are padding with spaces). No face change before the
1953 string start. */
1954 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1955 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1956 return it->face_id;
1957
1958 /* Set pos to the position before or after IT's current position. */
1959 if (before_p)
1960 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1961 else
1962 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1963
1964 /* Get the face for ASCII, or unibyte. */
1965 face_id
1966 = face_at_string_position (it->w,
1967 it->string,
1968 CHARPOS (pos),
1969 (it->current.overlay_string_index >= 0
1970 ? IT_CHARPOS (*it)
1971 : 0),
1972 it->region_beg_charpos,
1973 it->region_end_charpos,
1974 &next_check_charpos,
1975 it->base_face_id);
1976
1977 /* Correct the face for charsets different from ASCII. Do it
1978 for the multibyte case only. The face returned above is
1979 suitable for unibyte text if IT->string is unibyte. */
1980 if (STRING_MULTIBYTE (it->string))
1981 {
1982 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1983 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1984 int c, len, charset;
1985
4fdb80f2 1986 c = string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1987 charset = CHAR_CHARSET (c);
1988 if (charset != CHARSET_ASCII)
1989 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1990 }
1991 }
1992 else
1993 {
70851746
GM
1994 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1995 || (IT_CHARPOS (*it) <= BEGV && before_p))
1996 return it->face_id;
1997
5f5c8ee5
GM
1998 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1999 pos = it->current.pos;
2000
2001 if (before_p)
2002 DEC_TEXT_POS (pos);
2003 else
2004 INC_TEXT_POS (pos);
70851746 2005
5f5c8ee5
GM
2006 /* Determine face for CHARSET_ASCII, or unibyte. */
2007 face_id = face_at_buffer_position (it->w,
2008 CHARPOS (pos),
2009 it->region_beg_charpos,
2010 it->region_end_charpos,
2011 &next_check_charpos,
2012 limit, 0);
2013
2014 /* Correct the face for charsets different from ASCII. Do it
2015 for the multibyte case only. The face returned above is
2016 suitable for unibyte text if current_buffer is unibyte. */
2017 if (it->multibyte_p)
2018 {
2019 int charset = charset_at_position (pos);
2020 if (charset != CHARSET_ASCII)
2021 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2022 }
2023 }
2024
2025 return face_id;
2026}
2027
2028
2029\f
2030/***********************************************************************
2031 Invisible text
2032 ***********************************************************************/
2033
2034/* Set up iterator IT from invisible properties at its current
2035 position. Called from handle_stop. */
2036
2037static enum prop_handled
2038handle_invisible_prop (it)
2039 struct it *it;
2040{
2041 enum prop_handled handled = HANDLED_NORMALLY;
2042
2043 if (STRINGP (it->string))
2044 {
2045 extern Lisp_Object Qinvisible;
2046 Lisp_Object prop, end_charpos, limit, charpos;
2047
2048 /* Get the value of the invisible text property at the
2049 current position. Value will be nil if there is no such
2050 property. */
2051 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2052 prop = Fget_text_property (charpos, Qinvisible, it->string);
2053
2054 if (!NILP (prop))
2055 {
2056 handled = HANDLED_RECOMPUTE_PROPS;
2057
2058 /* Get the position at which the next change of the
2059 invisible text property can be found in IT->string.
2060 Value will be nil if the property value is the same for
2061 all the rest of IT->string. */
2062 XSETINT (limit, XSTRING (it->string)->size);
2063 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2064 it->string, limit);
2065
2066 /* Text at current position is invisible. The next
2067 change in the property is at position end_charpos.
2068 Move IT's current position to that position. */
2069 if (INTEGERP (end_charpos)
2070 && XFASTINT (end_charpos) < XFASTINT (limit))
2071 {
2072 struct text_pos old;
2073 old = it->current.string_pos;
2074 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2075 compute_string_pos (&it->current.string_pos, old, it->string);
2076 }
2077 else
2078 {
2079 /* The rest of the string is invisible. If this is an
2080 overlay string, proceed with the next overlay string
2081 or whatever comes and return a character from there. */
2082 if (it->current.overlay_string_index >= 0)
2083 {
2084 next_overlay_string (it);
2085 /* Don't check for overlay strings when we just
2086 finished processing them. */
2087 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2088 }
2089 else
2090 {
2091 struct Lisp_String *s = XSTRING (it->string);
2092 IT_STRING_CHARPOS (*it) = s->size;
2093 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2094 }
2095 }
2096 }
2097 }
2098 else
2099 {
2100 int visible_p, newpos, next_stop;
2101 Lisp_Object pos, prop;
2102
2103 /* First of all, is there invisible text at this position? */
2104 XSETFASTINT (pos, IT_CHARPOS (*it));
2105 prop = Fget_char_property (pos, Qinvisible, it->window);
2106
2107 /* If we are on invisible text, skip over it. */
2108 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2109 {
2110 /* Record whether we have to display an ellipsis for the
2111 invisible text. */
2112 int display_ellipsis_p
2113 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2114
2115 handled = HANDLED_RECOMPUTE_PROPS;
2116
2117 /* Loop skipping over invisible text. The loop is left at
2118 ZV or with IT on the first char being visible again. */
2119 do
2120 {
2121 /* Try to skip some invisible text. Return value is the
2122 position reached which can be equal to IT's position
2123 if there is nothing invisible here. This skips both
2124 over invisible text properties and overlays with
2125 invisible property. */
2126 newpos = skip_invisible (IT_CHARPOS (*it),
2127 &next_stop, ZV, it->window);
2128
2129 /* If we skipped nothing at all we weren't at invisible
2130 text in the first place. If everything to the end of
2131 the buffer was skipped, end the loop. */
2132 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2133 visible_p = 1;
2134 else
2135 {
2136 /* We skipped some characters but not necessarily
2137 all there are. Check if we ended up on visible
2138 text. Fget_char_property returns the property of
2139 the char before the given position, i.e. if we
2140 get visible_p = 1, this means that the char at
2141 newpos is visible. */
2142 XSETFASTINT (pos, newpos);
2143 prop = Fget_char_property (pos, Qinvisible, it->window);
2144 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2145 }
2146
2147 /* If we ended up on invisible text, proceed to
2148 skip starting with next_stop. */
2149 if (!visible_p)
2150 IT_CHARPOS (*it) = next_stop;
2151 }
2152 while (!visible_p);
2153
2154 /* The position newpos is now either ZV or on visible text. */
2155 IT_CHARPOS (*it) = newpos;
2156 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2157
2158 /* Maybe return `...' next for the end of the invisible text. */
2159 if (display_ellipsis_p)
2160 {
2161 if (it->dp
2162 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2163 {
2164 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2165 it->dpvec = v->contents;
2166 it->dpend = v->contents + v->size;
2167 }
2168 else
2169 {
2170 /* Default `...'. */
2171 it->dpvec = default_invis_vector;
2172 it->dpend = default_invis_vector + 3;
2173 }
2174
2175 /* The ellipsis display does not replace the display of
2176 the character at the new position. Indicate this by
2177 setting IT->dpvec_char_len to zero. */
2178 it->dpvec_char_len = 0;
2179
2180 it->current.dpvec_index = 0;
2181 it->method = next_element_from_display_vector;
2182 }
2183 }
2184 }
2185
2186 return handled;
2187}
2188
2189
2190\f
2191/***********************************************************************
2192 'display' property
2193 ***********************************************************************/
2194
2195/* Set up iterator IT from `display' property at its current position.
2196 Called from handle_stop. */
2197
2198static enum prop_handled
2199handle_display_prop (it)
2200 struct it *it;
2201{
2202 Lisp_Object prop, object;
2203 struct text_pos *position;
2204 int space_or_image_found_p;
2205
2206 if (STRINGP (it->string))
2207 {
2208 object = it->string;
2209 position = &it->current.string_pos;
2210 }
2211 else
2212 {
2213 object = Qnil;
2214 position = &it->current.pos;
2215 }
2216
2217 /* Reset those iterator values set from display property values. */
2218 it->font_height = Qnil;
2219 it->space_width = Qnil;
2220 it->voffset = 0;
2221
2222 /* We don't support recursive `display' properties, i.e. string
2223 values that have a string `display' property, that have a string
2224 `display' property etc. */
2225 if (!it->string_from_display_prop_p)
2226 it->area = TEXT_AREA;
2227
2228 prop = Fget_char_property (make_number (position->charpos),
2229 Qdisplay, object);
2230 if (NILP (prop))
2231 return HANDLED_NORMALLY;
2232
2233 space_or_image_found_p = 0;
2234 if (CONSP (prop) && CONSP (XCAR (prop)))
2235 {
2236 while (CONSP (prop))
2237 {
2238 if (handle_single_display_prop (it, XCAR (prop), object, position))
2239 space_or_image_found_p = 1;
2240 prop = XCDR (prop);
2241 }
2242 }
2243 else if (VECTORP (prop))
2244 {
2245 int i;
2246 for (i = 0; i < XVECTOR (prop)->size; ++i)
2247 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2248 object, position))
2249 space_or_image_found_p = 1;
2250 }
2251 else
2252 {
2253 if (handle_single_display_prop (it, prop, object, position))
2254 space_or_image_found_p = 1;
2255 }
2256
2257 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2258}
2259
2260
2261/* Value is the position of the end of the `display' property stating
2262 at START_POS in OBJECT. */
2263
2264static struct text_pos
2265display_prop_end (it, object, start_pos)
2266 struct it *it;
2267 Lisp_Object object;
2268 struct text_pos start_pos;
2269{
2270 Lisp_Object end;
2271 struct text_pos end_pos;
2272
2273 /* Characters having this form of property are not displayed, so
2274 we have to find the end of the property. */
2275 end = Fnext_single_property_change (make_number (start_pos.charpos),
2276 Qdisplay, object, Qnil);
2277 if (NILP (end))
2278 {
2279 /* A nil value of `end' means there are no changes of the
2280 property to the end of the buffer or string. */
2281 if (it->current.overlay_string_index >= 0)
2282 end_pos.charpos = XSTRING (it->string)->size;
2283 else
2284 end_pos.charpos = it->end_charpos;
2285 }
2286 else
2287 end_pos.charpos = XFASTINT (end);
2288
2289 if (STRINGP (it->string))
2290 compute_string_pos (&end_pos, start_pos, it->string);
2291 else
2292 end_pos.bytepos = CHAR_TO_BYTE (end_pos.charpos);
2293
2294 return end_pos;
2295}
2296
2297
2298/* Set up IT from a single `display' sub-property value PROP. OBJECT
2299 is the object in which the `display' property was found. *POSITION
2300 is the position at which it was found.
2301
2302 If PROP is a `space' or `image' sub-property, set *POSITION to the
2303 end position of the `display' property.
2304
2305 Value is non-zero if a `space' or `image' property value was found. */
2306
2307static int
2308handle_single_display_prop (it, prop, object, position)
2309 struct it *it;
2310 Lisp_Object prop;
2311 Lisp_Object object;
2312 struct text_pos *position;
2313{
2314 Lisp_Object value;
2315 int space_or_image_found_p = 0;
2316
2317 Lisp_Object form;
2318
2319 /* If PROP is a list of the form `(:when FORM VALUE)', FORM is
2320 evaluated. If the result is nil, VALUE is ignored. */
2321 form = Qt;
2322 if (CONSP (prop) && EQ (XCAR (prop), QCwhen))
2323 {
2324 prop = XCDR (prop);
2325 if (!CONSP (prop))
2326 return 0;
2327 form = XCAR (prop);
2328 prop = XCDR (prop);
2329 if (!CONSP (prop))
2330 return 0;
2331 prop = XCAR (prop);
2332 }
2333
2334 if (!NILP (form) && !EQ (form, Qt))
2335 {
2336 struct gcpro gcpro1;
2337 struct text_pos end_pos, pt;
2338
2339 end_pos = display_prop_end (it, object, *position);
2340 GCPRO1 (form);
2341
2342 /* Temporarily set point to the end position, and then evaluate
2343 the form. This makes `(eolp)' work as FORM. */
2344 CHARPOS (pt) = PT;
2345 BYTEPOS (pt) = PT_BYTE;
2346 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2347 form = eval_form (form);
2348 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2349 UNGCPRO;
2350 }
2351
2352 if (NILP (form))
2353 return 0;
2354
2355 if (CONSP (prop)
2356 && EQ (XCAR (prop), Qheight)
2357 && CONSP (XCDR (prop)))
2358 {
2359 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2360 return 0;
2361
2362 /* `(height HEIGHT)'. */
2363 it->font_height = XCAR (XCDR (prop));
2364 if (!NILP (it->font_height))
2365 {
2366 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2367 int new_height = -1;
2368
2369 if (CONSP (it->font_height)
2370 && (EQ (XCAR (it->font_height), Qplus)
2371 || EQ (XCAR (it->font_height), Qminus))
2372 && CONSP (XCDR (it->font_height))
2373 && INTEGERP (XCAR (XCDR (it->font_height))))
2374 {
2375 /* `(+ N)' or `(- N)' where N is an integer. */
2376 int steps = XINT (XCAR (XCDR (it->font_height)));
2377 if (EQ (XCAR (it->font_height), Qplus))
2378 steps = - steps;
2379 it->face_id = smaller_face (it->f, it->face_id, steps);
2380 }
2381 else if (SYMBOLP (it->font_height))
2382 {
2383 /* Call function with current height as argument.
2384 Value is the new height. */
2385 Lisp_Object form, height;
2386 struct gcpro gcpro1;
2387
2388 height = face->lface[LFACE_HEIGHT_INDEX];
2389 form = Fcons (it->font_height, Fcons (height, Qnil));
2390 GCPRO1 (form);
2391 height = eval_form (form);
2392 if (NUMBERP (height))
2393 new_height = XFLOATINT (height);
2394 UNGCPRO;
2395 }
2396 else if (NUMBERP (it->font_height))
2397 {
2398 /* Value is a multiple of the canonical char height. */
2399 struct face *face;
2400
2401 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2402 new_height = (XFLOATINT (it->font_height)
2403 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2404 }
2405 else
2406 {
2407 /* Evaluate IT->font_height with `height' bound to the
2408 current specified height to get the new height. */
2409 Lisp_Object value;
2410 int count = specpdl_ptr - specpdl;
2411
2412 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2413 value = eval_form (it->font_height);
2414 unbind_to (count, Qnil);
2415
2416 if (NUMBERP (value))
2417 new_height = XFLOATINT (value);
2418 }
2419
2420 if (new_height > 0)
2421 it->face_id = face_with_height (it->f, it->face_id, new_height);
2422 }
2423 }
2424 else if (CONSP (prop)
2425 && EQ (XCAR (prop), Qspace_width)
2426 && CONSP (XCDR (prop)))
2427 {
2428 /* `(space_width WIDTH)'. */
2429 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2430 return 0;
2431
2432 value = XCAR (XCDR (prop));
2433 if (NUMBERP (value) && XFLOATINT (value) > 0)
2434 it->space_width = value;
2435 }
2436 else if (CONSP (prop)
2437 && EQ (XCAR (prop), Qraise)
2438 && CONSP (XCDR (prop)))
2439 {
2440#ifdef HAVE_WINDOW_SYSTEM
2441 /* `(raise FACTOR)'. */
2442 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2443 return 0;
2444
2445 value = XCAR (XCDR (prop));
2446 if (NUMBERP (value))
2447 {
2448 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2449 it->voffset = - (XFLOATINT (value)
2450 * (face->font->ascent + face->font->descent));
2451 }
2452#endif /* HAVE_WINDOW_SYSTEM */
2453 }
2454 else if (!it->string_from_display_prop_p)
2455 {
2456 /* `(left-margin VALUE)' or `(right-margin VALUE)
2457 or `(nil VALUE)' or VALUE. */
2458 Lisp_Object location, value;
2459 struct text_pos start_pos;
2460 int valid_p;
2461
2462 /* Characters having this form of property are not displayed, so
2463 we have to find the end of the property. */
2464 space_or_image_found_p = 1;
2465 start_pos = *position;
2466 *position = display_prop_end (it, object, start_pos);
2467
2468 /* Let's stop at the new position and assume that all
2469 text properties change there. */
2470 it->stop_charpos = position->charpos;
2471
2472 if (CONSP (prop)
2473 && !EQ (XCAR (prop), Qspace)
2474 && !EQ (XCAR (prop), Qimage))
2475 {
2476 location = XCAR (prop);
2477 value = XCDR (prop);
2478 }
2479 else
2480 {
2481 location = Qnil;
2482 value = prop;
2483 }
2484
2485#ifdef HAVE_WINDOW_SYSTEM
2486 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2487 valid_p = STRINGP (value);
2488 else
2489 valid_p = (STRINGP (value)
2490 || (CONSP (value) && EQ (XCAR (value), Qspace))
2491 || valid_image_p (value));
2492#else /* not HAVE_WINDOW_SYSTEM */
2493 valid_p = STRINGP (value);
2494#endif /* not HAVE_WINDOW_SYSTEM */
2495
2496 if ((EQ (location, Qleft_margin)
2497 || EQ (location, Qright_margin)
2498 || NILP (location))
2499 && valid_p)
2500 {
2501 /* Save current settings of IT so that we can restore them
2502 when we are finished with the glyph property value. */
2503 push_it (it);
2504
2505 if (NILP (location))
2506 it->area = TEXT_AREA;
2507 else if (EQ (location, Qleft_margin))
2508 it->area = LEFT_MARGIN_AREA;
2509 else
2510 it->area = RIGHT_MARGIN_AREA;
2511
2512 if (STRINGP (value))
2513 {
2514 it->string = value;
2515 it->multibyte_p = STRING_MULTIBYTE (it->string);
2516 it->current.overlay_string_index = -1;
2517 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2518 it->end_charpos = it->string_nchars
2519 = XSTRING (it->string)->size;
2520 it->method = next_element_from_string;
2521 it->stop_charpos = 0;
2522 it->string_from_display_prop_p = 1;
2523 }
2524 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2525 {
2526 it->method = next_element_from_stretch;
2527 it->object = value;
2528 it->current.pos = it->position = start_pos;
2529 }
2530#ifdef HAVE_WINDOW_SYSTEM
2531 else
2532 {
2533 it->what = IT_IMAGE;
2534 it->image_id = lookup_image (it->f, value);
2535 it->position = start_pos;
2536 it->object = NILP (object) ? it->w->buffer : object;
2537 it->method = next_element_from_image;
2538
2539 /* Say that we don't have consumed the characters with
2540 `display' property yet. The call to pop_it in
2541 set_iterator_to_next will clean this up. */
2542 *position = start_pos;
2543 }
2544#endif /* HAVE_WINDOW_SYSTEM */
2545 }
2546 }
2547
2548 return space_or_image_found_p;
2549}
2550
2551
2552\f
2553/***********************************************************************
2554 Overlay strings
2555 ***********************************************************************/
2556
2557/* The following structure is used to record overlay strings for
2558 later sorting in load_overlay_strings. */
2559
2560struct overlay_entry
2561{
2562 Lisp_Object string;
2563 int priority;
2564 int after_string_p;
2565};
2566
2567
2568/* Set up iterator IT from overlay strings at its current position.
2569 Called from handle_stop. */
2570
2571static enum prop_handled
2572handle_overlay_change (it)
2573 struct it *it;
2574{
2575 /* Overlays are handled in current_buffer only. */
2576 if (STRINGP (it->string))
2577 return HANDLED_NORMALLY;
2578 else
2579 return (get_overlay_strings (it)
2580 ? HANDLED_RECOMPUTE_PROPS
2581 : HANDLED_NORMALLY);
2582}
2583
2584
2585/* Set up the next overlay string for delivery by IT, if there is an
2586 overlay string to deliver. Called by set_iterator_to_next when the
2587 end of the current overlay string is reached. If there are more
2588 overlay strings to display, IT->string and
2589 IT->current.overlay_string_index are set appropriately here.
2590 Otherwise IT->string is set to nil. */
2591
2592static void
2593next_overlay_string (it)
2594 struct it *it;
2595{
2596 ++it->current.overlay_string_index;
2597 if (it->current.overlay_string_index == it->n_overlay_strings)
2598 {
2599 /* No more overlay strings. Restore IT's settings to what
2600 they were before overlay strings were processed, and
2601 continue to deliver from current_buffer. */
2602 pop_it (it);
2603 xassert (it->stop_charpos >= BEGV
2604 && it->stop_charpos <= it->end_charpos);
2605 it->string = Qnil;
2606 it->current.overlay_string_index = -1;
2607 SET_TEXT_POS (it->current.string_pos, -1, -1);
2608 it->n_overlay_strings = 0;
2609 it->method = next_element_from_buffer;
2610 }
2611 else
2612 {
2613 /* There are more overlay strings to process. If
2614 IT->current.overlay_string_index has advanced to a position
2615 where we must load IT->overlay_strings with more strings, do
2616 it. */
2617 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2618
2619 if (it->current.overlay_string_index && i == 0)
2620 load_overlay_strings (it);
2621
2622 /* Initialize IT to deliver display elements from the overlay
2623 string. */
2624 it->string = it->overlay_strings[i];
2625 it->multibyte_p = STRING_MULTIBYTE (it->string);
2626 SET_TEXT_POS (it->current.string_pos, 0, 0);
2627 it->method = next_element_from_string;
2628 it->stop_charpos = 0;
2629 }
2630
2631 CHECK_IT (it);
2632}
2633
2634
2635/* Compare two overlay_entry structures E1 and E2. Used as a
2636 comparison function for qsort in load_overlay_strings. Overlay
2637 strings for the same position are sorted so that
2638
2639 1. All after-strings come in front of before-strings.
2640
2641 2. Within after-strings, strings are sorted so that overlay strings
2642 from overlays with higher priorities come first.
2643
2644 2. Within before-strings, strings are sorted so that overlay
2645 strings from overlays with higher priorities come last.
2646
2647 Value is analogous to strcmp. */
2648
2649
2650static int
2651compare_overlay_entries (e1, e2)
2652 void *e1, *e2;
2653{
2654 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2655 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2656 int result;
2657
2658 if (entry1->after_string_p != entry2->after_string_p)
2659 /* Let after-strings appear in front of before-strings. */
2660 result = entry1->after_string_p ? -1 : 1;
2661 else if (entry1->after_string_p)
2662 /* After-strings sorted in order of decreasing priority. */
2663 result = entry2->priority - entry1->priority;
2664 else
2665 /* Before-strings sorted in order of increasing priority. */
2666 result = entry1->priority - entry2->priority;
2667
2668 return result;
2669}
2670
2671
2672/* Load the vector IT->overlay_strings with overlay strings from IT's
2673 current buffer position. Set IT->n_overlays to the total number of
2674 overlay strings found.
2675
2676 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2677 a time. On entry into load_overlay_strings,
2678 IT->current.overlay_string_index gives the number of overlay
2679 strings that have already been loaded by previous calls to this
2680 function.
2681
2682 Overlay strings are sorted so that after-string strings come in
2683 front of before-string strings. Within before and after-strings,
2684 strings are sorted by overlay priority. See also function
2685 compare_overlay_entries. */
2686
2687static void
2688load_overlay_strings (it)
2689 struct it *it;
2690{
2691 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2692 Lisp_Object ov, overlay, window, str;
2693 int start, end;
2694 int size = 20;
2695 int n = 0, i, j;
2696 struct overlay_entry *entries
2697 = (struct overlay_entry *) alloca (size * sizeof *entries);
2698
2699 /* Append the overlay string STRING of overlay OVERLAY to vector
2700 `entries' which has size `size' and currently contains `n'
2701 elements. AFTER_P non-zero means STRING is an after-string of
2702 OVERLAY. */
2703#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2704 do \
2705 { \
2706 Lisp_Object priority; \
2707 \
2708 if (n == size) \
2709 { \
2710 int new_size = 2 * size; \
2711 struct overlay_entry *old = entries; \
2712 entries = \
2713 (struct overlay_entry *) alloca (new_size \
2714 * sizeof *entries); \
2715 bcopy (old, entries, size * sizeof *entries); \
2716 size = new_size; \
2717 } \
2718 \
2719 entries[n].string = (STRING); \
2720 priority = Foverlay_get ((OVERLAY), Qpriority); \
2721 entries[n].priority \
2722 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2723 entries[n].after_string_p = (AFTER_P); \
2724 ++n; \
2725 } \
2726 while (0)
2727
2728 /* Process overlay before the overlay center. */
2729 for (ov = current_buffer->overlays_before;
2730 CONSP (ov);
2731 ov = XCONS (ov)->cdr)
2732 {
2733 overlay = XCONS (ov)->car;
2734 xassert (OVERLAYP (overlay));
2735 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2736 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2737
2738 if (end < IT_CHARPOS (*it))
2739 break;
2740
2741 /* Skip this overlay if it doesn't start or end at IT's current
2742 position. */
2743 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2744 continue;
2745
2746 /* Skip this overlay if it doesn't apply to IT->w. */
2747 window = Foverlay_get (overlay, Qwindow);
2748 if (WINDOWP (window) && XWINDOW (window) != it->w)
2749 continue;
2750
2751 /* If overlay has a non-empty before-string, record it. */
2752 if (start == IT_CHARPOS (*it)
2753 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2754 && XSTRING (str)->size)
2755 RECORD_OVERLAY_STRING (overlay, str, 0);
2756
2757 /* If overlay has a non-empty after-string, record it. */
2758 if (end == IT_CHARPOS (*it)
2759 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2760 && XSTRING (str)->size)
2761 RECORD_OVERLAY_STRING (overlay, str, 1);
2762 }
2763
2764 /* Process overlays after the overlay center. */
2765 for (ov = current_buffer->overlays_after;
2766 CONSP (ov);
2767 ov = XCONS (ov)->cdr)
2768 {
2769 overlay = XCONS (ov)->car;
2770 xassert (OVERLAYP (overlay));
2771 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2772 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2773
2774 if (start > IT_CHARPOS (*it))
2775 break;
2776
2777 /* Skip this overlay if it doesn't start or end at IT's current
2778 position. */
2779 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2780 continue;
2781
2782 /* Skip this overlay if it doesn't apply to IT->w. */
2783 window = Foverlay_get (overlay, Qwindow);
2784 if (WINDOWP (window) && XWINDOW (window) != it->w)
2785 continue;
2786
2787 /* If overlay has a non-empty before-string, record it. */
2788 if (start == IT_CHARPOS (*it)
2789 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2790 && XSTRING (str)->size)
2791 RECORD_OVERLAY_STRING (overlay, str, 0);
2792
2793 /* If overlay has a non-empty after-string, record it. */
2794 if (end == IT_CHARPOS (*it)
2795 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2796 && XSTRING (str)->size)
2797 RECORD_OVERLAY_STRING (overlay, str, 1);
2798 }
2799
2800#undef RECORD_OVERLAY_STRING
2801
2802 /* Sort entries. */
2803 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2804
2805 /* Record the total number of strings to process. */
2806 it->n_overlay_strings = n;
2807
2808 /* IT->current.overlay_string_index is the number of overlay strings
2809 that have already been consumed by IT. Copy some of the
2810 remaining overlay strings to IT->overlay_strings. */
2811 i = 0;
2812 j = it->current.overlay_string_index;
2813 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2814 it->overlay_strings[i++] = entries[j++].string;
2815
2816 CHECK_IT (it);
2817}
2818
2819
2820/* Get the first chunk of overlay strings at IT's current buffer
2821 position. Value is non-zero if at least one overlay string was
2822 found. */
2823
2824static int
2825get_overlay_strings (it)
2826 struct it *it;
2827{
2828 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2829 process. This fills IT->overlay_strings with strings, and sets
2830 IT->n_overlay_strings to the total number of strings to process.
2831 IT->pos.overlay_string_index has to be set temporarily to zero
2832 because load_overlay_strings needs this; it must be set to -1
2833 when no overlay strings are found because a zero value would
2834 indicate a position in the first overlay string. */
2835 it->current.overlay_string_index = 0;
2836 load_overlay_strings (it);
2837
2838 /* If we found overlay strings, set up IT to deliver display
2839 elements from the first one. Otherwise set up IT to deliver
2840 from current_buffer. */
2841 if (it->n_overlay_strings)
2842 {
2843 /* Make sure we know settings in current_buffer, so that we can
2844 restore meaningful values when we're done with the overlay
2845 strings. */
2846 compute_stop_pos (it);
2847 xassert (it->face_id >= 0);
2848
2849 /* Save IT's settings. They are restored after all overlay
2850 strings have been processed. */
2851 xassert (it->sp == 0);
2852 push_it (it);
2853
2854 /* Set up IT to deliver display elements from the first overlay
2855 string. */
2856 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2857 it->stop_charpos = 0;
2858 it->string = it->overlay_strings[0];
2859 it->multibyte_p = STRING_MULTIBYTE (it->string);
2860 xassert (STRINGP (it->string));
2861 it->method = next_element_from_string;
2862 }
2863 else
2864 {
2865 it->string = Qnil;
2866 it->current.overlay_string_index = -1;
2867 it->method = next_element_from_buffer;
2868 }
2869
2870 CHECK_IT (it);
2871
2872 /* Value is non-zero if we found at least one overlay string. */
2873 return STRINGP (it->string);
2874}
2875
2876
2877\f
2878/***********************************************************************
2879 Saving and restoring state
2880 ***********************************************************************/
2881
2882/* Save current settings of IT on IT->stack. Called, for example,
2883 before setting up IT for an overlay string, to be able to restore
2884 IT's settings to what they were after the overlay string has been
2885 processed. */
2886
2887static void
2888push_it (it)
2889 struct it *it;
2890{
2891 struct iterator_stack_entry *p;
2892
2893 xassert (it->sp < 2);
2894 p = it->stack + it->sp;
2895
2896 p->stop_charpos = it->stop_charpos;
2897 xassert (it->face_id >= 0);
2898 p->face_id = it->face_id;
2899 p->string = it->string;
2900 p->pos = it->current;
2901 p->end_charpos = it->end_charpos;
2902 p->string_nchars = it->string_nchars;
2903 p->area = it->area;
2904 p->multibyte_p = it->multibyte_p;
2905 p->space_width = it->space_width;
2906 p->font_height = it->font_height;
2907 p->voffset = it->voffset;
2908 p->string_from_display_prop_p = it->string_from_display_prop_p;
2909 ++it->sp;
2910}
2911
2912
2913/* Restore IT's settings from IT->stack. Called, for example, when no
2914 more overlay strings must be processed, and we return to delivering
2915 display elements from a buffer, or when the end of a string from a
2916 `display' property is reached and we return to delivering display
2917 elements from an overlay string, or from a buffer. */
2918
2919static void
2920pop_it (it)
2921 struct it *it;
2922{
2923 struct iterator_stack_entry *p;
2924
2925 xassert (it->sp > 0);
2926 --it->sp;
2927 p = it->stack + it->sp;
2928 it->stop_charpos = p->stop_charpos;
2929 it->face_id = p->face_id;
2930 it->string = p->string;
2931 it->current = p->pos;
2932 it->end_charpos = p->end_charpos;
2933 it->string_nchars = p->string_nchars;
2934 it->area = p->area;
2935 it->multibyte_p = p->multibyte_p;
2936 it->space_width = p->space_width;
2937 it->font_height = p->font_height;
2938 it->voffset = p->voffset;
2939 it->string_from_display_prop_p = p->string_from_display_prop_p;
2940}
2941
2942
2943\f
2944/***********************************************************************
2945 Moving over lines
2946 ***********************************************************************/
2947
2948/* Set IT's current position to the previous line start. */
2949
2950static void
2951back_to_previous_line_start (it)
2952 struct it *it;
2953{
2954 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2955 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2956}
2957
2958
2959/* Set IT's current position to the next line start. */
2960
2961static void
2962forward_to_next_line_start (it)
2963 struct it *it;
2964{
2965 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2966 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2967}
2968
2969
2970/* Set IT's current position to the previous visible line start. Skip
2971 invisible text that is so either due to text properties or due to
2972 selective display. Caution: this does not change IT->current_x and
2973 IT->hpos. */
2974
2975static void
2976back_to_previous_visible_line_start (it)
2977 struct it *it;
2978{
2979 int visible_p = 0;
2980
2981 /* Go back one newline if not on BEGV already. */
2982 if (IT_CHARPOS (*it) > BEGV)
2983 back_to_previous_line_start (it);
2984
2985 /* Move over lines that are invisible because of selective display
2986 or text properties. */
2987 while (IT_CHARPOS (*it) > BEGV
2988 && !visible_p)
2989 {
2990 visible_p = 1;
2991
2992 /* If selective > 0, then lines indented more than that values
2993 are invisible. */
2994 if (it->selective > 0
2995 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2996 it->selective))
2997 visible_p = 0;
2998#ifdef USE_TEXT_PROPERTIES
2999 else
3000 {
3001 Lisp_Object prop;
3002
3003 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
3004 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3005 visible_p = 0;
3006 }
3007#endif /* USE_TEXT_PROPERTIES */
3008
3009 /* Back one more newline if the current one is invisible. */
3010 if (!visible_p)
3011 back_to_previous_line_start (it);
3012 }
3013
3014 xassert (IT_CHARPOS (*it) >= BEGV);
3015 xassert (IT_CHARPOS (*it) == BEGV
3016 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3017 CHECK_IT (it);
3018}
3019
3020
3021/* Reseat iterator IT at the previous visible line start. Skip
3022 invisible text that is so either due to text properties or due to
3023 selective display. At the end, update IT's overlay information,
3024 face information etc. */
3025
3026static void
3027reseat_at_previous_visible_line_start (it)
3028 struct it *it;
3029{
3030 back_to_previous_visible_line_start (it);
3031 reseat (it, it->current.pos, 1);
3032 CHECK_IT (it);
3033}
3034
3035
3036/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3037 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3038 preceding the line start. Skip over invisible text that is so
3039 because of selective display. Compute faces, overlays etc at the
3040 new position. Note that this function does not skip over text that
3041 is invisible because of text properties. */
5f5c8ee5
GM
3042
3043static void
312246d1 3044reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3045 struct it *it;
312246d1 3046 int on_newline_p;
5f5c8ee5
GM
3047{
3048 /* Restore the buffer position when currently not delivering display
3049 elements from the current buffer. This is the case, for example,
3050 when called at the end of a truncated overlay string. */
3051 while (it->sp)
3052 pop_it (it);
3053 it->method = next_element_from_buffer;
3054
3055 /* Otherwise, scan_buffer would not work. */
3056 if (IT_CHARPOS (*it) < ZV)
3057 {
3058 /* If on a newline, advance past it. Otherwise, find the next
3059 newline which automatically gives us the position following
3060 the newline. */
3061 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3062 {
3063 ++IT_CHARPOS (*it);
3064 ++IT_BYTEPOS (*it);
3065 }
3066 else
3067 forward_to_next_line_start (it);
3068
3069 /* We must either have reached the end of the buffer or end up
3070 after a newline. */
3071 xassert (IT_CHARPOS (*it) == ZV
3072 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3073
3074 /* Skip over lines that are invisible because they are indented
3075 more than the value of IT->selective. */
3076 if (it->selective > 0)
3077 while (IT_CHARPOS (*it) < ZV
3078 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3079 it->selective))
3080 forward_to_next_line_start (it);
312246d1
GM
3081
3082 /* Position on the newline if we should. */
3083 if (on_newline_p && IT_CHARPOS (*it) > BEGV)
3084 {
3085 --IT_CHARPOS (*it);
3086 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3087 }
5f5c8ee5
GM
3088
3089 /* Set the iterator there. The 0 as the last parameter of
3090 reseat means don't force a text property lookup. The lookup
3091 is then only done if we've skipped past the iterator's
3092 check_charpos'es. This optimization is important because
3093 text property lookups tend to be expensive. */
3094 reseat (it, it->current.pos, 0);
3095 }
3096
3097 CHECK_IT (it);
3098}
3099
3100
3101\f
3102/***********************************************************************
3103 Changing an iterator's position
3104***********************************************************************/
3105
3106/* Change IT's current position to POS in current_buffer. If FORCE_P
3107 is non-zero, always check for text properties at the new position.
3108 Otherwise, text properties are only looked up if POS >=
3109 IT->check_charpos of a property. */
3110
3111static void
3112reseat (it, pos, force_p)
3113 struct it *it;
3114 struct text_pos pos;
3115 int force_p;
3116{
3117 int original_pos = IT_CHARPOS (*it);
3118
3119 reseat_1 (it, pos, 0);
3120
3121 /* Determine where to check text properties. Avoid doing it
3122 where possible because text property lookup is very expensive. */
3123 if (force_p
3124 || CHARPOS (pos) > it->stop_charpos
3125 || CHARPOS (pos) < original_pos)
3126 handle_stop (it);
3127
3128 CHECK_IT (it);
3129}
3130
3131
3132/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3133 IT->stop_pos to POS, also. */
3134
3135static void
3136reseat_1 (it, pos, set_stop_p)
3137 struct it *it;
3138 struct text_pos pos;
3139 int set_stop_p;
3140{
3141 /* Don't call this function when scanning a C string. */
3142 xassert (it->s == NULL);
3143
3144 /* POS must be a reasonable value. */
3145 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3146
3147 it->current.pos = it->position = pos;
3148 XSETBUFFER (it->object, current_buffer);
3149 it->dpvec = NULL;
3150 it->current.dpvec_index = -1;
3151 it->current.overlay_string_index = -1;
3152 IT_STRING_CHARPOS (*it) = -1;
3153 IT_STRING_BYTEPOS (*it) = -1;
3154 it->string = Qnil;
3155 it->method = next_element_from_buffer;
3156 it->sp = 0;
3157
3158 if (set_stop_p)
3159 it->stop_charpos = CHARPOS (pos);
3160}
3161
3162
3163/* Set up IT for displaying a string, starting at CHARPOS in window W.
3164 If S is non-null, it is a C string to iterate over. Otherwise,
3165 STRING gives a Lisp string to iterate over.
3166
3167 If PRECISION > 0, don't return more then PRECISION number of
3168 characters from the string.
3169
3170 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3171 characters have been returned. FIELD_WIDTH < 0 means an infinite
3172 field width.
3173
3174 MULTIBYTE = 0 means disable processing of multibyte characters,
3175 MULTIBYTE > 0 means enable it,
3176 MULTIBYTE < 0 means use IT->multibyte_p.
3177
3178 IT must be initialized via a prior call to init_iterator before
3179 calling this function. */
3180
3181static void
3182reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3183 struct it *it;
3184 unsigned char *s;
3185 Lisp_Object string;
3186 int charpos;
3187 int precision, field_width, multibyte;
3188{
3189 /* No region in strings. */
3190 it->region_beg_charpos = it->region_end_charpos = -1;
3191
3192 /* No text property checks performed by default, but see below. */
3193 it->stop_charpos = -1;
3194
3195 /* Set iterator position and end position. */
3196 bzero (&it->current, sizeof it->current);
3197 it->current.overlay_string_index = -1;
3198 it->current.dpvec_index = -1;
3199 it->charset = CHARSET_ASCII;
3200 xassert (charpos >= 0);
3201
3202 /* Use the setting of MULTIBYTE if specified. */
3203 if (multibyte >= 0)
3204 it->multibyte_p = multibyte > 0;
3205
3206 if (s == NULL)
3207 {
3208 xassert (STRINGP (string));
3209 it->string = string;
3210 it->s = NULL;
3211 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3212 it->method = next_element_from_string;
3213 it->current.string_pos = string_pos (charpos, string);
3214 }
3215 else
3216 {
3217 it->s = s;
3218 it->string = Qnil;
3219
3220 /* Note that we use IT->current.pos, not it->current.string_pos,
3221 for displaying C strings. */
3222 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3223 if (it->multibyte_p)
3224 {
3225 it->current.pos = c_string_pos (charpos, s, 1);
3226 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3227 }
3228 else
3229 {
3230 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3231 it->end_charpos = it->string_nchars = strlen (s);
3232 }
3233
3234 it->method = next_element_from_c_string;
3235 }
3236
3237 /* PRECISION > 0 means don't return more than PRECISION characters
3238 from the string. */
3239 if (precision > 0 && it->end_charpos - charpos > precision)
3240 it->end_charpos = it->string_nchars = charpos + precision;
3241
3242 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3243 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3244 FIELD_WIDTH < 0 means infinite field width. This is useful for
3245 padding with `-' at the end of a mode line. */
3246 if (field_width < 0)
3247 field_width = INFINITY;
3248 if (field_width > it->end_charpos - charpos)
3249 it->end_charpos = charpos + field_width;
3250
3251 /* Use the standard display table for displaying strings. */
3252 if (DISP_TABLE_P (Vstandard_display_table))
3253 it->dp = XCHAR_TABLE (Vstandard_display_table);
3254
3255 it->stop_charpos = charpos;
3256 CHECK_IT (it);
3257}
3258
3259
3260\f
3261/***********************************************************************
3262 Iteration
3263 ***********************************************************************/
3264
3265/* Load IT's display element fields with information about the next
3266 display element from the current position of IT. Value is zero if
3267 end of buffer (or C string) is reached. */
3268
3269int
3270get_next_display_element (it)
3271 struct it *it;
3272{
3273 /* Non-zero means that we found an display element. Zero means that
3274 we hit the end of what we iterate over. Performance note: the
3275 function pointer `method' used here turns out to be faster than
3276 using a sequence of if-statements. */
3277 int success_p = (*it->method) (it);
3278 int charset;
3279
3280 if (it->what == IT_CHARACTER)
3281 {
3282 /* Map via display table or translate control characters.
3283 IT->c, IT->len etc. have been set to the next character by
3284 the function call above. If we have a display table, and it
3285 contains an entry for IT->c, translate it. Don't do this if
3286 IT->c itself comes from a display table, otherwise we could
3287 end up in an infinite recursion. (An alternative could be to
3288 count the recursion depth of this function and signal an
3289 error when a certain maximum depth is reached.) Is it worth
3290 it? */
3291 if (success_p && it->dpvec == NULL)
3292 {
3293 Lisp_Object dv;
3294
3295 if (it->dp
3296 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3297 VECTORP (dv)))
3298 {
3299 struct Lisp_Vector *v = XVECTOR (dv);
3300
3301 /* Return the first character from the display table
3302 entry, if not empty. If empty, don't display the
3303 current character. */
3304 if (v->size)
3305 {
3306 it->dpvec_char_len = it->len;
3307 it->dpvec = v->contents;
3308 it->dpend = v->contents + v->size;
3309 it->current.dpvec_index = 0;
3310 it->method = next_element_from_display_vector;
3311 }
3312
3313 success_p = get_next_display_element (it);
3314 }
3315
3316 /* Translate control characters into `\003' or `^C' form.
3317 Control characters coming from a display table entry are
3318 currently not translated because we use IT->dpvec to hold
3319 the translation. This could easily be changed but I
3320 don't believe that it is worth doing. */
3321 else if ((it->c < ' '
3322 && (it->area != TEXT_AREA
3323 || (it->c != '\n'
3324 && it->c != '\t'
3325 && it->c != '\r')))
54c85a23 3326 || (it->c >= 127
5f5c8ee5
GM
3327 && it->len == 1))
3328 {
3329 /* IT->c is a control character which must be displayed
3330 either as '\003' or as `^C' where the '\\' and '^'
3331 can be defined in the display table. Fill
3332 IT->ctl_chars with glyphs for what we have to
3333 display. Then, set IT->dpvec to these glyphs. */
3334 GLYPH g;
3335
54c85a23 3336 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3337 {
3338 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3339 if (it->dp
3340 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3341 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3342 g = XINT (DISP_CTRL_GLYPH (it->dp));
3343 else
3344 g = FAST_MAKE_GLYPH ('^', 0);
3345 XSETINT (it->ctl_chars[0], g);
3346
3347 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3348 XSETINT (it->ctl_chars[1], g);
3349
3350 /* Set up IT->dpvec and return first character from it. */
3351 it->dpvec_char_len = it->len;
3352 it->dpvec = it->ctl_chars;
3353 it->dpend = it->dpvec + 2;
3354 it->current.dpvec_index = 0;
3355 it->method = next_element_from_display_vector;
3356 get_next_display_element (it);
3357 }
3358 else
3359 {
3360 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3361 if (it->dp
3362 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3363 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3364 g = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3365 else
3366 g = FAST_MAKE_GLYPH ('\\', 0);
3367 XSETINT (it->ctl_chars[0], g);
3368
3369 /* Insert three more glyphs into IT->ctl_chars for
3370 the octal display of the character. */
3371 g = FAST_MAKE_GLYPH (((it->c >> 6) & 7) + '0', 0);
3372 XSETINT (it->ctl_chars[1], g);
3373 g = FAST_MAKE_GLYPH (((it->c >> 3) & 7) + '0', 0);
3374 XSETINT (it->ctl_chars[2], g);
3375 g = FAST_MAKE_GLYPH ((it->c & 7) + '0', 0);
3376 XSETINT (it->ctl_chars[3], g);
3377
3378 /* Set up IT->dpvec and return the first character
3379 from it. */
3380 it->dpvec_char_len = it->len;
3381 it->dpvec = it->ctl_chars;
3382 it->dpend = it->dpvec + 4;
3383 it->current.dpvec_index = 0;
3384 it->method = next_element_from_display_vector;
3385 get_next_display_element (it);
3386 }
3387 }
3388 }
3389
3390 /* Adjust face id if charset changes. There are no charset
3391 changes in unibyte text because Emacs' charsets are not
3392 applicable there. */
3393 if (it->multibyte_p
3394 && success_p
3395 && (charset = CHAR_CHARSET (it->c),
3396 charset != it->charset))
3397 {
3398 it->charset = charset;
3399 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3400 }
3401 }
3402
3403 /* Is this character the last one of a run of characters with
3404 box? If yes, set IT->end_of_box_run_p to 1. */
3405 if (it->face_box_p
3406 && it->s == NULL)
3407 {
3408 int face_id;
3409 struct face *face;
3410
3411 it->end_of_box_run_p
3412 = ((face_id = face_after_it_pos (it),
3413 face_id != it->face_id)
3414 && (face = FACE_FROM_ID (it->f, face_id),
3415 face->box == FACE_NO_BOX));
3416 }
3417
3418 /* Value is 0 if end of buffer or string reached. */
3419 return success_p;
3420}
3421
3422
3423/* Move IT to the next display element.
3424
3425 Functions get_next_display_element and set_iterator_to_next are
3426 separate because I find this arrangement easier to handle than a
3427 get_next_display_element function that also increments IT's
3428 position. The way it is we can first look at an iterator's current
3429 display element, decide whether it fits on a line, and if it does,
3430 increment the iterator position. The other way around we probably
3431 would either need a flag indicating whether the iterator has to be
3432 incremented the next time, or we would have to implement a
3433 decrement position function which would not be easy to write. */
3434
3435void
3436set_iterator_to_next (it)
3437 struct it *it;
3438{
3439 if (it->method == next_element_from_buffer)
3440 {
3441 /* The current display element of IT is a character from
3442 current_buffer. Advance in the buffer, and maybe skip over
3443 invisible lines that are so because of selective display. */
3444 if (ITERATOR_AT_END_OF_LINE_P (it))
312246d1 3445 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
3446 else
3447 {
3448 xassert (it->len != 0);
3449 IT_BYTEPOS (*it) += it->len;
3450 IT_CHARPOS (*it) += 1;
3451 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3452 }
3453 }
3454 else if (it->method == next_element_from_c_string)
3455 {
3456 /* Current display element of IT is from a C string. */
3457 IT_BYTEPOS (*it) += it->len;
3458 IT_CHARPOS (*it) += 1;
3459 }
3460 else if (it->method == next_element_from_display_vector)
3461 {
3462 /* Current display element of IT is from a display table entry.
3463 Advance in the display table definition. Reset it to null if
3464 end reached, and continue with characters from buffers/
3465 strings. */
286bcbc9
GM
3466 struct face *face;
3467
5f5c8ee5 3468 ++it->current.dpvec_index;
286bcbc9
GM
3469
3470 /* Restore face and charset of the iterator to what they were
3471 before the display vector entry (these entries may contain
3472 faces, and of course characters of different charsets). */
5f5c8ee5 3473 it->face_id = it->saved_face_id;
286bcbc9
GM
3474 it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
3475
5f5c8ee5
GM
3476 if (it->dpvec + it->current.dpvec_index == it->dpend)
3477 {
3478 if (it->s)
3479 it->method = next_element_from_c_string;
3480 else if (STRINGP (it->string))
3481 it->method = next_element_from_string;
3482 else
3483 it->method = next_element_from_buffer;
3484
3485 it->dpvec = NULL;
3486 it->current.dpvec_index = -1;
3487
312246d1
GM
3488 /* Skip over characters which were displayed via IT->dpvec. */
3489 if (it->dpvec_char_len < 0)
3490 reseat_at_next_visible_line_start (it, 1);
3491 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
3492 {
3493 it->len = it->dpvec_char_len;
3494 set_iterator_to_next (it);
3495 }
3496 }
3497 }
3498 else if (it->method == next_element_from_string)
3499 {
3500 /* Current display element is a character from a Lisp string. */
3501 xassert (it->s == NULL && STRINGP (it->string));
3502 IT_STRING_BYTEPOS (*it) += it->len;
3503 IT_STRING_CHARPOS (*it) += 1;
3504
3505 consider_string_end:
3506
3507 if (it->current.overlay_string_index >= 0)
3508 {
3509 /* IT->string is an overlay string. Advance to the
3510 next, if there is one. */
3511 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3512 next_overlay_string (it);
3513 }
3514 else
3515 {
3516 /* IT->string is not an overlay string. If we reached
3517 its end, and there is something on IT->stack, proceed
3518 with what is on the stack. This can be either another
3519 string, this time an overlay string, or a buffer. */
3520 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3521 && it->sp > 0)
3522 {
3523 pop_it (it);
3524 if (!STRINGP (it->string))
3525 it->method = next_element_from_buffer;
3526 }
3527 }
3528 }
3529 else if (it->method == next_element_from_image
3530 || it->method == next_element_from_stretch)
3531 {
3532 /* The position etc with which we have to proceed are on
3533 the stack. The position may be at the end of a string,
3534 if the `display' property takes up the whole string. */
3535 pop_it (it);
3536 it->image_id = 0;
3537 if (STRINGP (it->string))
3538 {
3539 it->method = next_element_from_string;
3540 goto consider_string_end;
3541 }
3542 else
3543 it->method = next_element_from_buffer;
3544 }
3545 else
3546 /* There are no other methods defined, so this should be a bug. */
3547 abort ();
3548
3549 /* Reset flags indicating start and end of a sequence of
3550 characters with box. */
3551 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3552
3553 xassert (it->method != next_element_from_string
3554 || (STRINGP (it->string)
3555 && IT_STRING_CHARPOS (*it) >= 0));
3556}
3557
3558
3559/* Load IT's display element fields with information about the next
3560 display element which comes from a display table entry or from the
3561 result of translating a control character to one of the forms `^C'
3562 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3563
3564static int
3565next_element_from_display_vector (it)
3566 struct it *it;
3567{
3568 /* Precondition. */
3569 xassert (it->dpvec && it->current.dpvec_index >= 0);
3570
3571 /* Remember the current face id in case glyphs specify faces.
3572 IT's face is restored in set_iterator_to_next. */
3573 it->saved_face_id = it->face_id;
3574
3575 if (INTEGERP (*it->dpvec)
3576 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3577 {
3578 int lface_id;
3579 GLYPH g;
3580
3581 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3582 it->c = FAST_GLYPH_CHAR (g);
3583 it->len = CHAR_LEN (it->c);
3584
3585 /* The entry may contain a face id to use. Such a face id is
3586 the id of a Lisp face, not a realized face. A face id of
3587 zero means no face. */
3588 lface_id = FAST_GLYPH_FACE (g);
3589 if (lface_id)
3590 {
3591 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3592 if (face_id >= 0)
3593 {
3594 it->face_id = face_id;
3595 it->charset = CHARSET_ASCII;
3596 }
3597 }
3598 }
3599 else
3600 /* Display table entry is invalid. Return a space. */
3601 it->c = ' ', it->len = 1;
3602
3603 /* Don't change position and object of the iterator here. They are
3604 still the values of the character that had this display table
3605 entry or was translated, and that's what we want. */
3606 it->what = IT_CHARACTER;
3607 return 1;
3608}
3609
3610
3611/* Load IT with the next display element from Lisp string IT->string.
3612 IT->current.string_pos is the current position within the string.
3613 If IT->current.overlay_string_index >= 0, the Lisp string is an
3614 overlay string. */
3615
3616static int
3617next_element_from_string (it)
3618 struct it *it;
3619{
3620 struct text_pos position;
3621
3622 xassert (STRINGP (it->string));
3623 xassert (IT_STRING_CHARPOS (*it) >= 0);
3624 position = it->current.string_pos;
3625
3626 /* Time to check for invisible text? */
3627 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3628 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3629 {
3630 handle_stop (it);
3631
3632 /* Since a handler may have changed IT->method, we must
3633 recurse here. */
3634 return get_next_display_element (it);
3635 }
3636
3637 if (it->current.overlay_string_index >= 0)
3638 {
3639 /* Get the next character from an overlay string. In overlay
3640 strings, There is no field width or padding with spaces to
3641 do. */
3642 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3643 {
3644 it->what = IT_EOB;
3645 return 0;
3646 }
3647 else if (STRING_MULTIBYTE (it->string))
3648 {
3649 int remaining = (STRING_BYTES (XSTRING (it->string))
3650 - IT_STRING_BYTEPOS (*it));
3651 unsigned char *s = (XSTRING (it->string)->data
3652 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3653 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
3654 }
3655 else
3656 {
3657 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3658 it->len = 1;
3659 }
3660 }
3661 else
3662 {
3663 /* Get the next character from a Lisp string that is not an
3664 overlay string. Such strings come from the mode line, for
3665 example. We may have to pad with spaces, or truncate the
3666 string. See also next_element_from_c_string. */
3667 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3668 {
3669 it->what = IT_EOB;
3670 return 0;
3671 }
3672 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3673 {
3674 /* Pad with spaces. */
3675 it->c = ' ', it->len = 1;
3676 CHARPOS (position) = BYTEPOS (position) = -1;
3677 }
3678 else if (STRING_MULTIBYTE (it->string))
3679 {
3680 int maxlen = (STRING_BYTES (XSTRING (it->string))
3681 - IT_STRING_BYTEPOS (*it));
3682 unsigned char *s = (XSTRING (it->string)->data
3683 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3684 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
3685 }
3686 else
3687 {
3688 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3689 it->len = 1;
3690 }
3691 }
3692
3693 /* Record what we have and where it came from. Note that we store a
3694 buffer position in IT->position although it could arguably be a
3695 string position. */
3696 it->what = IT_CHARACTER;
3697 it->object = it->string;
3698 it->position = position;
3699 return 1;
3700}
3701
3702
3703/* Load IT with next display element from C string IT->s.
3704 IT->string_nchars is the maximum number of characters to return
3705 from the string. IT->end_charpos may be greater than
3706 IT->string_nchars when this function is called, in which case we
3707 may have to return padding spaces. Value is zero if end of string
3708 reached, including padding spaces. */
3709
3710static int
3711next_element_from_c_string (it)
3712 struct it *it;
3713{
3714 int success_p = 1;
3715
3716 xassert (it->s);
3717 it->what = IT_CHARACTER;
3718 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3719 it->object = Qnil;
3720
3721 /* IT's position can be greater IT->string_nchars in case a field
3722 width or precision has been specified when the iterator was
3723 initialized. */
3724 if (IT_CHARPOS (*it) >= it->end_charpos)
3725 {
3726 /* End of the game. */
3727 it->what = IT_EOB;
3728 success_p = 0;
3729 }
3730 else if (IT_CHARPOS (*it) >= it->string_nchars)
3731 {
3732 /* Pad with spaces. */
3733 it->c = ' ', it->len = 1;
3734 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3735 }
3736 else if (it->multibyte_p)
3737 {
3738 /* Implementation note: The calls to strlen apparently aren't a
3739 performance problem because there is no noticeable performance
3740 difference between Emacs running in unibyte or multibyte mode. */
3741 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
3742 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3743 maxlen, &it->len);
5f5c8ee5
GM
3744 }
3745 else
3746 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3747
3748 return success_p;
3749}
3750
3751
3752/* Set up IT to return characters from an ellipsis, if appropriate.
3753 The definition of the ellipsis glyphs may come from a display table
3754 entry. This function Fills IT with the first glyph from the
3755 ellipsis if an ellipsis is to be displayed. */
3756
3757static void
3758next_element_from_ellipsis (it)
3759 struct it *it;
3760{
3761 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3762 {
3763 /* Use the display table definition for `...'. Invalid glyphs
3764 will be handled by the method returning elements from dpvec. */
3765 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3766 it->dpvec_char_len = it->len;
3767 it->dpvec = v->contents;
3768 it->dpend = v->contents + v->size;
3769 it->current.dpvec_index = 0;
3770 it->method = next_element_from_display_vector;
3771 get_next_display_element (it);
3772 }
3773 else if (it->selective_display_ellipsis_p)
3774 {
3775 /* Use default `...' which is stored in default_invis_vector. */
3776 it->dpvec_char_len = it->len;
3777 it->dpvec = default_invis_vector;
3778 it->dpend = default_invis_vector + 3;
3779 it->current.dpvec_index = 0;
3780 it->method = next_element_from_display_vector;
3781 get_next_display_element (it);
3782 }
3783}
3784
3785
3786/* Deliver an image display element. The iterator IT is already
3787 filled with image information (done in handle_display_prop). Value
3788 is always 1. */
3789
3790
3791static int
3792next_element_from_image (it)
3793 struct it *it;
3794{
3795 it->what = IT_IMAGE;
3796 return 1;
3797}
3798
3799
3800/* Fill iterator IT with next display element from a stretch glyph
3801 property. IT->object is the value of the text property. Value is
3802 always 1. */
3803
3804static int
3805next_element_from_stretch (it)
3806 struct it *it;
3807{
3808 it->what = IT_STRETCH;
3809 return 1;
3810}
3811
3812
3813/* Load IT with the next display element from current_buffer. Value
3814 is zero if end of buffer reached. IT->stop_charpos is the next
3815 position at which to stop and check for text properties or buffer
3816 end. */
3817
3818static int
3819next_element_from_buffer (it)
3820 struct it *it;
3821{
3822 int success_p = 1;
3823
3824 /* Check this assumption, otherwise, we would never enter the
3825 if-statement, below. */
3826 xassert (IT_CHARPOS (*it) >= BEGV
3827 && IT_CHARPOS (*it) <= it->stop_charpos);
3828
3829 if (IT_CHARPOS (*it) >= it->stop_charpos)
3830 {
3831 if (IT_CHARPOS (*it) >= it->end_charpos)
3832 {
3833 int overlay_strings_follow_p;
3834
3835 /* End of the game, except when overlay strings follow that
3836 haven't been returned yet. */
3837 if (it->overlay_strings_at_end_processed_p)
3838 overlay_strings_follow_p = 0;
3839 else
3840 {
3841 it->overlay_strings_at_end_processed_p = 1;
3842 overlay_strings_follow_p
3843 = get_overlay_strings (it);
3844 }
3845
3846 if (overlay_strings_follow_p)
3847 success_p = get_next_display_element (it);
3848 else
3849 {
3850 it->what = IT_EOB;
3851 it->position = it->current.pos;
3852 success_p = 0;
3853 }
3854 }
3855 else
3856 {
3857 handle_stop (it);
3858 return get_next_display_element (it);
3859 }
3860 }
3861 else
3862 {
3863 /* No face changes, overlays etc. in sight, so just return a
3864 character from current_buffer. */
3865 unsigned char *p;
3866
3867 /* Maybe run the redisplay end trigger hook. Performance note:
3868 This doesn't seem to cost measurable time. */
3869 if (it->redisplay_end_trigger_charpos
3870 && it->glyph_row
3871 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3872 run_redisplay_end_trigger_hook (it);
3873
3874 /* Get the next character, maybe multibyte. */
3875 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3876 if (it->multibyte_p)
3877 {
3878 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3879 - IT_BYTEPOS (*it));
4fdb80f2 3880 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
3881 }
3882 else
3883 it->c = *p, it->len = 1;
3884
3885 /* Record what we have and where it came from. */
3886 it->what = IT_CHARACTER;;
3887 it->object = it->w->buffer;
3888 it->position = it->current.pos;
3889
3890 /* Normally we return the character found above, except when we
3891 really want to return an ellipsis for selective display. */
3892 if (it->selective)
3893 {
3894 if (it->c == '\n')
3895 {
3896 /* A value of selective > 0 means hide lines indented more
3897 than that number of columns. */
3898 if (it->selective > 0
3899 && IT_CHARPOS (*it) + 1 < ZV
3900 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3901 IT_BYTEPOS (*it) + 1,
3902 it->selective))
312246d1
GM
3903 {
3904 next_element_from_ellipsis (it);
3905 it->dpvec_char_len = -1;
3906 }
5f5c8ee5
GM
3907 }
3908 else if (it->c == '\r' && it->selective == -1)
3909 {
3910 /* A value of selective == -1 means that everything from the
3911 CR to the end of the line is invisible, with maybe an
3912 ellipsis displayed for it. */
3913 next_element_from_ellipsis (it);
312246d1 3914 it->dpvec_char_len = -1;
5f5c8ee5
GM
3915 }
3916 }
3917 }
3918
3919 /* Value is zero if end of buffer reached. */
3920 xassert (!success_p || it->len > 0);
3921 return success_p;
3922}
3923
3924
3925/* Run the redisplay end trigger hook for IT. */
3926
3927static void
3928run_redisplay_end_trigger_hook (it)
3929 struct it *it;
3930{
3931 Lisp_Object args[3];
3932
3933 /* IT->glyph_row should be non-null, i.e. we should be actually
3934 displaying something, or otherwise we should not run the hook. */
3935 xassert (it->glyph_row);
3936
3937 /* Set up hook arguments. */
3938 args[0] = Qredisplay_end_trigger_functions;
3939 args[1] = it->window;
3940 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3941 it->redisplay_end_trigger_charpos = 0;
3942
3943 /* Since we are *trying* to run these functions, don't try to run
3944 them again, even if they get an error. */
3945 it->w->redisplay_end_trigger = Qnil;
3946 Frun_hook_with_args (3, args);
3947
3948 /* Notice if it changed the face of the character we are on. */
3949 handle_face_prop (it);
3950}
3951
3952
3953\f
3954/***********************************************************************
3955 Moving an iterator without producing glyphs
3956 ***********************************************************************/
3957
3958/* Move iterator IT to a specified buffer or X position within one
3959 line on the display without producing glyphs.
3960
3961 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3962 whichever is reached first.
3963
3964 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3965
3966 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3967 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3968 TO_X includes the amount by which a window is horizontally
3969 scrolled.
3970
3971 Value is
3972
3973 MOVE_POS_MATCH_OR_ZV
3974 - when TO_POS or ZV was reached.
3975
3976 MOVE_X_REACHED
3977 -when TO_X was reached before TO_POS or ZV were reached.
3978
3979 MOVE_LINE_CONTINUED
3980 - when we reached the end of the display area and the line must
3981 be continued.
3982
3983 MOVE_LINE_TRUNCATED
3984 - when we reached the end of the display area and the line is
3985 truncated.
3986
3987 MOVE_NEWLINE_OR_CR
3988 - when we stopped at a line end, i.e. a newline or a CR and selective
3989 display is on. */
3990
3991enum move_it_result
3992move_it_in_display_line_to (it, to_charpos, to_x, op)
3993 struct it *it;
3994 int to_charpos, to_x, op;
3995{
3996 enum move_it_result result = MOVE_UNDEFINED;
3997 struct glyph_row *saved_glyph_row;
3998
3999 /* Don't produce glyphs in produce_glyphs. */
4000 saved_glyph_row = it->glyph_row;
4001 it->glyph_row = NULL;
4002
4003#if NO_PROMPT_IN_BUFFER
4004 /* Take a mini-buffer prompt into account. */
4005 if (MINI_WINDOW_P (it->w)
4006 && IT_CHARPOS (*it) == BEGV)
4007 {
4008 it->current_x = minibuf_prompt_pixel_width;
4009 it->hpos = minibuf_prompt_width;
4010 }
4011#endif
4012
4013 while (1)
4014 {
4015 int x, i;
4016
4017 /* Stop when ZV or TO_CHARPOS reached. */
4018 if (!get_next_display_element (it)
4019 || ((op & MOVE_TO_POS) != 0
4020 && BUFFERP (it->object)
4021 && IT_CHARPOS (*it) >= to_charpos))
4022 {
4023 result = MOVE_POS_MATCH_OR_ZV;
4024 break;
4025 }
4026
4027 /* The call to produce_glyphs will get the metrics of the
4028 display element IT is loaded with. We record in x the
4029 x-position before this display element in case it does not
4030 fit on the line. */
4031 x = it->current_x;
4032 PRODUCE_GLYPHS (it);
4033
4034 if (it->area != TEXT_AREA)
4035 {
4036 set_iterator_to_next (it);
4037 continue;
4038 }
4039
4040 /* The number of glyphs we get back in IT->nglyphs will normally
4041 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4042 character on a terminal frame, or (iii) a line end. For the
4043 second case, IT->nglyphs - 1 padding glyphs will be present
4044 (on X frames, there is only one glyph produced for a
4045 composite character.
4046
4047 The behavior implemented below means, for continuation lines,
4048 that as many spaces of a TAB as fit on the current line are
4049 displayed there. For terminal frames, as many glyphs of a
4050 multi-glyph character are displayed in the current line, too.
4051 This is what the old redisplay code did, and we keep it that
4052 way. Under X, the whole shape of a complex character must
4053 fit on the line or it will be completely displayed in the
4054 next line.
4055
4056 Note that both for tabs and padding glyphs, all glyphs have
4057 the same width. */
4058 if (it->nglyphs)
4059 {
4060 /* More than one glyph or glyph doesn't fit on line. All
4061 glyphs have the same width. */
4062 int single_glyph_width = it->pixel_width / it->nglyphs;
4063 int new_x;
4064
4065 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4066 {
4067 new_x = x + single_glyph_width;
4068
4069 /* We want to leave anything reaching TO_X to the caller. */
4070 if ((op & MOVE_TO_X) && new_x > to_x)
4071 {
4072 it->current_x = x;
4073 result = MOVE_X_REACHED;
4074 break;
4075 }
4076 else if (/* Lines are continued. */
4077 !it->truncate_lines_p
4078 && (/* And glyph doesn't fit on the line. */
4079 new_x > it->last_visible_x
4080 /* Or it fits exactly and we're on a window
4081 system frame. */
4082 || (new_x == it->last_visible_x
4083 && FRAME_WINDOW_P (it->f))))
4084 {
4085 if (/* IT->hpos == 0 means the very first glyph
4086 doesn't fit on the line, e.g. a wide image. */
4087 it->hpos == 0
4088 || (new_x == it->last_visible_x
4089 && FRAME_WINDOW_P (it->f)))
4090 {
4091 ++it->hpos;
4092 it->current_x = new_x;
4093 if (i == it->nglyphs - 1)
4094 set_iterator_to_next (it);
4095 }
4096 else
4097 it->current_x = x;
4098
4099 result = MOVE_LINE_CONTINUED;
4100 break;
4101 }
4102 else if (new_x > it->first_visible_x)
4103 {
4104 /* Glyph is visible. Increment number of glyphs that
4105 would be displayed. */
4106 ++it->hpos;
4107 }
4108 else
4109 {
4110 /* Glyph is completely off the left margin of the display
4111 area. Nothing to do. */
4112 }
4113 }
4114
4115 if (result != MOVE_UNDEFINED)
4116 break;
4117 }
4118 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4119 {
4120 /* Stop when TO_X specified and reached. This check is
4121 necessary here because of lines consisting of a line end,
4122 only. The line end will not produce any glyphs and we
4123 would never get MOVE_X_REACHED. */
4124 xassert (it->nglyphs == 0);
4125 result = MOVE_X_REACHED;
4126 break;
4127 }
4128
4129 /* Is this a line end? If yes, we're done. */
4130 if (ITERATOR_AT_END_OF_LINE_P (it))
4131 {
4132 result = MOVE_NEWLINE_OR_CR;
4133 break;
4134 }
4135
4136 /* The current display element has been consumed. Advance
4137 to the next. */
4138 set_iterator_to_next (it);
4139
4140 /* Stop if lines are truncated and IT's current x-position is
4141 past the right edge of the window now. */
4142 if (it->truncate_lines_p
4143 && it->current_x >= it->last_visible_x)
4144 {
4145 result = MOVE_LINE_TRUNCATED;
4146 break;
4147 }
4148 }
4149
4150 /* Restore the iterator settings altered at the beginning of this
4151 function. */
4152 it->glyph_row = saved_glyph_row;
4153 return result;
4154}
4155
4156
4157/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4158 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4159 the description of enum move_operation_enum.
4160
4161 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4162 screen line, this function will set IT to the next position >
4163 TO_CHARPOS. */
4164
4165void
4166move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4167 struct it *it;
4168 int to_charpos, to_x, to_y, to_vpos;
4169 int op;
4170{
4171 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4172 int line_height;
4173
4174 xassert (XBUFFER (it->w->buffer) == current_buffer);
4175
4176 while (1)
4177 {
4178 if (op & MOVE_TO_VPOS)
4179 {
4180 /* If no TO_CHARPOS and no TO_X specified, stop at the
4181 start of the line TO_VPOS. */
4182 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4183 {
4184 if (it->vpos == to_vpos)
4185 break;
4186 skip = move_it_in_display_line_to (it, -1, -1, 0);
4187 }
4188 else
4189 {
4190 /* TO_VPOS >= 0 means stop at TO_X in the line at
4191 TO_VPOS, or at TO_POS, whichever comes first. */
4192 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4193
4194 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4195 break;
4196 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4197 {
4198 /* We have reached TO_X but not in the line we want. */
4199 skip = move_it_in_display_line_to (it, to_charpos,
4200 -1, MOVE_TO_POS);
4201 if (skip == MOVE_POS_MATCH_OR_ZV)
4202 break;
4203 }
4204 }
4205 }
4206 else if (op & MOVE_TO_Y)
4207 {
4208 struct it it_backup;
4209 int done_p;
4210
4211 /* TO_Y specified means stop at TO_X in the line containing
4212 TO_Y---or at TO_CHARPOS if this is reached first. The
4213 problem is that we can't really tell whether the line
4214 contains TO_Y before we have completely scanned it, and
4215 this may skip past TO_X. What we do is to first scan to
4216 TO_X.
4217
4218 If TO_X is not specified, use a TO_X of zero. The reason
4219 is to make the outcome of this function more predictable.
4220 If we didn't use TO_X == 0, we would stop at the end of
4221 the line which is probably not what a caller would expect
4222 to happen. */
4223 skip = move_it_in_display_line_to (it, to_charpos,
4224 ((op & MOVE_TO_X)
4225 ? to_x : 0),
4226 (MOVE_TO_X
4227 | (op & MOVE_TO_POS)));
4228
4229 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4230 if (skip == MOVE_POS_MATCH_OR_ZV)
4231 break;
4232
4233 /* If TO_X was reached, we would like to know whether TO_Y
4234 is in the line. This can only be said if we know the
4235 total line height which requires us to scan the rest of
4236 the line. */
4237 done_p = 0;
4238 if (skip == MOVE_X_REACHED)
4239 {
4240 it_backup = *it;
4241 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4242 op & MOVE_TO_POS);
4243 }
4244
4245 /* Now, decide whether TO_Y is in this line. */
4246 line_height = it->max_ascent + it->max_descent;
4247
4248 if (to_y >= it->current_y
4249 && to_y < it->current_y + line_height)
4250 {
4251 if (skip == MOVE_X_REACHED)
4252 /* If TO_Y is in this line and TO_X was reached above,
4253 we scanned too far. We have to restore IT's settings
4254 to the ones before skipping. */
4255 *it = it_backup;
4256 done_p = 1;
4257 }
4258 else if (skip == MOVE_X_REACHED)
4259 {
4260 skip = skip2;
4261 if (skip == MOVE_POS_MATCH_OR_ZV)
4262 done_p = 1;
4263 }
4264
4265 if (done_p)
4266 break;
4267 }
4268 else
4269 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4270
4271 switch (skip)
4272 {
4273 case MOVE_POS_MATCH_OR_ZV:
4274 return;
4275
4276 case MOVE_NEWLINE_OR_CR:
4277 set_iterator_to_next (it);
4278 it->continuation_lines_width = 0;
4279 break;
4280
4281 case MOVE_LINE_TRUNCATED:
4282 it->continuation_lines_width = 0;
312246d1 4283 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4284 if ((op & MOVE_TO_POS) != 0
4285 && IT_CHARPOS (*it) > to_charpos)
4286 goto out;
4287 break;
4288
4289 case MOVE_LINE_CONTINUED:
4290 it->continuation_lines_width += it->current_x;
4291 break;
4292
4293 default:
4294 abort ();
4295 }
4296
4297 /* Reset/increment for the next run. */
4298 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4299 it->current_x = it->hpos = 0;
4300 it->current_y += it->max_ascent + it->max_descent;
4301 ++it->vpos;
4302 last_height = it->max_ascent + it->max_descent;
4303 last_max_ascent = it->max_ascent;
4304 it->max_ascent = it->max_descent = 0;
4305 }
4306 out:;
4307}
4308
4309
4310/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4311
4312 If DY > 0, move IT backward at least that many pixels. DY = 0
4313 means move IT backward to the preceding line start or BEGV. This
4314 function may move over more than DY pixels if IT->current_y - DY
4315 ends up in the middle of a line; in this case IT->current_y will be
4316 set to the top of the line moved to. */
4317
4318void
4319move_it_vertically_backward (it, dy)
4320 struct it *it;
4321 int dy;
4322{
4323 int nlines, h, line_height;
4324 struct it it2;
4325 int start_pos = IT_CHARPOS (*it);
4326
4327 xassert (dy >= 0);
4328
4329 /* Estimate how many newlines we must move back. */
4330 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4331
4332 /* Set the iterator's position that many lines back. */
4333 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4334 back_to_previous_visible_line_start (it);
4335
4336 /* Reseat the iterator here. When moving backward, we don't want
4337 reseat to skip forward over invisible text, set up the iterator
4338 to deliver from overlay strings at the new position etc. So,
4339 use reseat_1 here. */
4340 reseat_1 (it, it->current.pos, 1);
4341
4342 /* We are now surely at a line start. */
4343 it->current_x = it->hpos = 0;
4344
4345 /* Move forward and see what y-distance we moved. First move to the
4346 start of the next line so that we get its height. We need this
4347 height to be able to tell whether we reached the specified
4348 y-distance. */
4349 it2 = *it;
4350 it2.max_ascent = it2.max_descent = 0;
4351 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4352 MOVE_TO_POS | MOVE_TO_VPOS);
4353 xassert (IT_CHARPOS (*it) >= BEGV);
4354 line_height = it2.max_ascent + it2.max_descent;
4355 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4356 xassert (IT_CHARPOS (*it) >= BEGV);
4357 h = it2.current_y - it->current_y;
4358 nlines = it2.vpos - it->vpos;
4359
4360 /* Correct IT's y and vpos position. */
4361 it->vpos -= nlines;
4362 it->current_y -= h;
4363
4364 if (dy == 0)
4365 {
4366 /* DY == 0 means move to the start of the screen line. The
4367 value of nlines is > 0 if continuation lines were involved. */
4368 if (nlines > 0)
4369 move_it_by_lines (it, nlines, 1);
4370 xassert (IT_CHARPOS (*it) <= start_pos);
4371 }
4372 else if (nlines)
4373 {
4374 /* The y-position we try to reach. Note that h has been
4375 subtracted in front of the if-statement. */
4376 int target_y = it->current_y + h - dy;
4377
4378 /* If we did not reach target_y, try to move further backward if
4379 we can. If we moved too far backward, try to move forward. */
4380 if (target_y < it->current_y
4381 && IT_CHARPOS (*it) > BEGV)
4382 {
4383 move_it_vertically (it, target_y - it->current_y);
4384 xassert (IT_CHARPOS (*it) >= BEGV);
4385 }
4386 else if (target_y >= it->current_y + line_height
4387 && IT_CHARPOS (*it) < ZV)
4388 {
4389 move_it_vertically (it, target_y - (it->current_y + line_height));
4390 xassert (IT_CHARPOS (*it) >= BEGV);
4391 }
4392 }
4393}
4394
4395
4396/* Move IT by a specified amount of pixel lines DY. DY negative means
4397 move backwards. DY = 0 means move to start of screen line. At the
4398 end, IT will be on the start of a screen line. */
4399
4400void
4401move_it_vertically (it, dy)
4402 struct it *it;
4403 int dy;
4404{
4405 if (dy <= 0)
4406 move_it_vertically_backward (it, -dy);
4407 else if (dy > 0)
4408 {
4409 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4410 MOVE_TO_POS | MOVE_TO_Y);
4411
4412 /* If buffer ends in ZV without a newline, move to the start of
4413 the line to satisfy the post-condition. */
4414 if (IT_CHARPOS (*it) == ZV
4415 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4416 move_it_by_lines (it, 0, 0);
4417 }
4418}
4419
4420
4421/* Return non-zero if some text between buffer positions START_CHARPOS
4422 and END_CHARPOS is invisible. IT->window is the window for text
4423 property lookup. */
4424
4425static int
4426invisible_text_between_p (it, start_charpos, end_charpos)
4427 struct it *it;
4428 int start_charpos, end_charpos;
4429{
4430#ifdef USE_TEXT_PROPERTIES
4431 Lisp_Object prop, limit;
4432 int invisible_found_p;
4433
4434 xassert (it != NULL && start_charpos <= end_charpos);
4435
4436 /* Is text at START invisible? */
4437 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4438 it->window);
4439 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4440 invisible_found_p = 1;
4441 else
4442 {
4443 limit = Fnext_single_property_change (make_number (start_charpos),
4444 Qinvisible,
4445 Fcurrent_buffer (),
4446 make_number (end_charpos));
4447 invisible_found_p = XFASTINT (limit) < end_charpos;
4448 }
4449
4450 return invisible_found_p;
4451
4452#else /* not USE_TEXT_PROPERTIES */
4453 return 0;
4454#endif /* not USE_TEXT_PROPERTIES */
4455}
4456
4457
4458/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4459 negative means move up. DVPOS == 0 means move to the start of the
4460 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4461 NEED_Y_P is zero, IT->current_y will be left unchanged.
4462
4463 Further optimization ideas: If we would know that IT->f doesn't use
4464 a face with proportional font, we could be faster for
4465 truncate-lines nil. */
4466
4467void
4468move_it_by_lines (it, dvpos, need_y_p)
4469 struct it *it;
4470 int dvpos, need_y_p;
4471{
4472 struct position pos;
4473
4474 if (!FRAME_WINDOW_P (it->f))
4475 {
4476 struct text_pos textpos;
4477
4478 /* We can use vmotion on frames without proportional fonts. */
4479 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4480 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4481 reseat (it, textpos, 1);
4482 it->vpos += pos.vpos;
4483 it->current_y += pos.vpos;
4484 }
4485 else if (dvpos == 0)
4486 {
4487 /* DVPOS == 0 means move to the start of the screen line. */
4488 move_it_vertically_backward (it, 0);
4489 xassert (it->current_x == 0 && it->hpos == 0);
4490 }
4491 else if (dvpos > 0)
4492 {
4493 /* If there are no continuation lines, and if there is no
4494 selective display, try the simple method of moving forward
4495 DVPOS newlines, then see where we are. */
4496 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4497 {
4498 int shortage = 0, charpos;
4499
4500 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4501 charpos = IT_CHARPOS (*it) + 1;
4502 else
4503 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4504 &shortage, 0);
4505
4506 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4507 {
4508 struct text_pos pos;
4509 CHARPOS (pos) = charpos;
4510 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4511 reseat (it, pos, 1);
4512 it->vpos += dvpos - shortage;
4513 it->hpos = it->current_x = 0;
4514 return;
4515 }
4516 }
4517
4518 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4519 }
4520 else
4521 {
4522 struct it it2;
4523 int start_charpos, i;
4524
4525 /* If there are no continuation lines, and if there is no
4526 selective display, try the simple method of moving backward
4527 -DVPOS newlines. */
4528 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4529 {
4530 int shortage;
4531 int charpos = IT_CHARPOS (*it);
4532 int bytepos = IT_BYTEPOS (*it);
4533
4534 /* If in the middle of a line, go to its start. */
4535 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4536 {
4537 charpos = find_next_newline_no_quit (charpos, -1);
4538 bytepos = CHAR_TO_BYTE (charpos);
4539 }
4540
4541 if (charpos == BEGV)
4542 {
4543 struct text_pos pos;
4544 CHARPOS (pos) = charpos;
4545 BYTEPOS (pos) = bytepos;
4546 reseat (it, pos, 1);
4547 it->hpos = it->current_x = 0;
4548 return;
4549 }
4550 else
4551 {
4552 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4553 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4554 {
4555 struct text_pos pos;
4556 CHARPOS (pos) = charpos;
4557 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4558 reseat (it, pos, 1);
4559 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4560 it->hpos = it->current_x = 0;
4561 return;
4562 }
4563 }
4564 }
4565
4566 /* Go back -DVPOS visible lines and reseat the iterator there. */
4567 start_charpos = IT_CHARPOS (*it);
4568 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4569 back_to_previous_visible_line_start (it);
4570 reseat (it, it->current.pos, 1);
4571 it->current_x = it->hpos = 0;
4572
4573 /* Above call may have moved too far if continuation lines
4574 are involved. Scan forward and see if it did. */
4575 it2 = *it;
4576 it2.vpos = it2.current_y = 0;
4577 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4578 it->vpos -= it2.vpos;
4579 it->current_y -= it2.current_y;
4580 it->current_x = it->hpos = 0;
4581
4582 /* If we moved too far, move IT some lines forward. */
4583 if (it2.vpos > -dvpos)
4584 {
4585 int delta = it2.vpos + dvpos;
4586 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4587 }
4588 }
4589}
4590
4591
4592\f
4593/***********************************************************************
4594 Messages
4595 ***********************************************************************/
4596
4597
4598/* Output a newline in the *Messages* buffer if "needs" one. */
4599
4600void
4601message_log_maybe_newline ()
4602{
4603 if (message_log_need_newline)
4604 message_dolog ("", 0, 1, 0);
4605}
4606
4607
4608/* Add a string M of length LEN to the message log, optionally
4609 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4610 nonzero, means interpret the contents of M as multibyte. This
4611 function calls low-level routines in order to bypass text property
4612 hooks, etc. which might not be safe to run. */
4613
4614void
4615message_dolog (m, len, nlflag, multibyte)
4616 char *m;
4617 int len, nlflag, multibyte;
4618{
4619 if (!NILP (Vmessage_log_max))
4620 {
4621 struct buffer *oldbuf;
4622 Lisp_Object oldpoint, oldbegv, oldzv;
4623 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4624 int point_at_end = 0;
4625 int zv_at_end = 0;
4626 Lisp_Object old_deactivate_mark, tem;
4627 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4628
4629 old_deactivate_mark = Vdeactivate_mark;
4630 oldbuf = current_buffer;
4631 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4632 current_buffer->undo_list = Qt;
4633
4634 oldpoint = Fpoint_marker ();
4635 oldbegv = Fpoint_min_marker ();
4636 oldzv = Fpoint_max_marker ();
4637 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4638
4639 if (PT == Z)
4640 point_at_end = 1;
4641 if (ZV == Z)
4642 zv_at_end = 1;
4643
4644 BEGV = BEG;
4645 BEGV_BYTE = BEG_BYTE;
4646 ZV = Z;
4647 ZV_BYTE = Z_BYTE;
4648 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4649
4650 /* Insert the string--maybe converting multibyte to single byte
4651 or vice versa, so that all the text fits the buffer. */
4652 if (multibyte
4653 && NILP (current_buffer->enable_multibyte_characters))
4654 {
4655 int i, c, nbytes;
4656 unsigned char work[1];
4657
4658 /* Convert a multibyte string to single-byte
4659 for the *Message* buffer. */
4660 for (i = 0; i < len; i += nbytes)
4661 {
4fdb80f2 4662 c = string_char_and_length (m + i, len - i, &nbytes);
5f5c8ee5
GM
4663 work[0] = (SINGLE_BYTE_CHAR_P (c)
4664 ? c
4665 : multibyte_char_to_unibyte (c, Qnil));
4666 insert_1_both (work, 1, 1, 1, 0, 0);
4667 }
4668 }
4669 else if (! multibyte
4670 && ! NILP (current_buffer->enable_multibyte_characters))
4671 {
4672 int i, c, nbytes;
4673 unsigned char *msg = (unsigned char *) m;
4674 unsigned char *str, work[4];
4675 /* Convert a single-byte string to multibyte
4676 for the *Message* buffer. */
4677 for (i = 0; i < len; i++)
4678 {
4679 c = unibyte_char_to_multibyte (msg[i]);
4680 nbytes = CHAR_STRING (c, work, str);
4681 insert_1_both (work, 1, nbytes, 1, 0, 0);
4682 }
4683 }
4684 else if (len)
4685 insert_1 (m, len, 1, 0, 0);
4686
4687 if (nlflag)
4688 {
4689 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4690 insert_1 ("\n", 1, 1, 0, 0);
4691
4692 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4693 this_bol = PT;
4694 this_bol_byte = PT_BYTE;
4695
4696 if (this_bol > BEG)
4697 {
4698 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4699 prev_bol = PT;
4700 prev_bol_byte = PT_BYTE;
4701
4702 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4703 this_bol, this_bol_byte);
4704 if (dup)
4705 {
4706 del_range_both (prev_bol, prev_bol_byte,
4707 this_bol, this_bol_byte, 0);
4708 if (dup > 1)
4709 {
4710 char dupstr[40];
4711 int duplen;
4712
4713 /* If you change this format, don't forget to also
4714 change message_log_check_duplicate. */
4715 sprintf (dupstr, " [%d times]", dup);
4716 duplen = strlen (dupstr);
4717 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4718 insert_1 (dupstr, duplen, 1, 0, 1);
4719 }
4720 }
4721 }
4722
4723 if (NATNUMP (Vmessage_log_max))
4724 {
4725 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4726 -XFASTINT (Vmessage_log_max) - 1, 0);
4727 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4728 }
4729 }
4730 BEGV = XMARKER (oldbegv)->charpos;
4731 BEGV_BYTE = marker_byte_position (oldbegv);
4732
4733 if (zv_at_end)
4734 {
4735 ZV = Z;
4736 ZV_BYTE = Z_BYTE;
4737 }
4738 else
4739 {
4740 ZV = XMARKER (oldzv)->charpos;
4741 ZV_BYTE = marker_byte_position (oldzv);
4742 }
4743
4744 if (point_at_end)
4745 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4746 else
4747 /* We can't do Fgoto_char (oldpoint) because it will run some
4748 Lisp code. */
4749 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4750 XMARKER (oldpoint)->bytepos);
4751
4752 UNGCPRO;
4753 free_marker (oldpoint);
4754 free_marker (oldbegv);
4755 free_marker (oldzv);
4756
4757 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4758 set_buffer_internal (oldbuf);
4759 if (NILP (tem))
4760 windows_or_buffers_changed = old_windows_or_buffers_changed;
4761 message_log_need_newline = !nlflag;
4762 Vdeactivate_mark = old_deactivate_mark;
4763 }
4764}
4765
4766
4767/* We are at the end of the buffer after just having inserted a newline.
4768 (Note: We depend on the fact we won't be crossing the gap.)
4769 Check to see if the most recent message looks a lot like the previous one.
4770 Return 0 if different, 1 if the new one should just replace it, or a
4771 value N > 1 if we should also append " [N times]". */
4772
4773static int
4774message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4775 int prev_bol, this_bol;
4776 int prev_bol_byte, this_bol_byte;
4777{
4778 int i;
4779 int len = Z_BYTE - 1 - this_bol_byte;
4780 int seen_dots = 0;
4781 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4782 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4783
4784 for (i = 0; i < len; i++)
4785 {
4786 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4787 && p1[i] != '\n')
4788 seen_dots = 1;
4789 if (p1[i] != p2[i])
4790 return seen_dots;
4791 }
4792 p1 += len;
4793 if (*p1 == '\n')
4794 return 2;
4795 if (*p1++ == ' ' && *p1++ == '[')
4796 {
4797 int n = 0;
4798 while (*p1 >= '0' && *p1 <= '9')
4799 n = n * 10 + *p1++ - '0';
4800 if (strncmp (p1, " times]\n", 8) == 0)
4801 return n+1;
4802 }
4803 return 0;
4804}
4805
4806
4807/* Display an echo area message M with a specified length of LEN
4808 chars. The string may include null characters. If M is 0, clear
4809 out any existing message, and let the mini-buffer text show through.
4810
4811 The buffer M must continue to exist until after the echo area gets
4812 cleared or some other message gets displayed there. This means do
4813 not pass text that is stored in a Lisp string; do not pass text in
4814 a buffer that was alloca'd. */
4815
4816void
4817message2 (m, len, multibyte)
4818 char *m;
4819 int len;
4820 int multibyte;
4821{
4822 /* First flush out any partial line written with print. */
4823 message_log_maybe_newline ();
4824 if (m)
4825 message_dolog (m, len, 1, multibyte);
4826 message2_nolog (m, len, multibyte);
4827}
4828
4829
4830/* The non-logging counterpart of message2. */
4831
4832void
4833message2_nolog (m, len, multibyte)
4834 char *m;
4835 int len;
4836{
4837 message_enable_multibyte = multibyte;
4838
4839 if (noninteractive)
4840 {
4841 if (noninteractive_need_newline)
4842 putc ('\n', stderr);
4843 noninteractive_need_newline = 0;
4844 if (m)
4845 fwrite (m, len, 1, stderr);
4846 if (cursor_in_echo_area == 0)
4847 fprintf (stderr, "\n");
4848 fflush (stderr);
4849 }
4850 /* A null message buffer means that the frame hasn't really been
4851 initialized yet. Error messages get reported properly by
4852 cmd_error, so this must be just an informative message; toss it. */
4853 else if (INTERACTIVE
4854 && selected_frame->glyphs_initialized_p
4855 && FRAME_MESSAGE_BUF (selected_frame))
4856 {
4857 Lisp_Object mini_window;
4858 struct frame *f;
4859
4860 /* Get the frame containing the mini-buffer
4861 that the selected frame is using. */
4862 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4863 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4864
4865 FRAME_SAMPLE_VISIBILITY (f);
4866 if (FRAME_VISIBLE_P (selected_frame)
4867 && ! FRAME_VISIBLE_P (f))
4868 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4869
4870 if (m)
4871 {
4872 echo_area_glyphs = m;
4873 echo_area_glyphs_length = len;
4874 echo_area_message = Qnil;
4875
4876 if (minibuffer_auto_raise)
4877 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4878 }
4879 else
4880 {
4881 echo_area_glyphs = previous_echo_glyphs = NULL;
4882 echo_area_message = previous_echo_area_message = Qnil;
4883 }
4884
4885 do_pending_window_change ();
4886 echo_area_display (1);
4887 do_pending_window_change ();
4888 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4889 (*frame_up_to_date_hook) (f);
4890 }
4891}
4892
4893
4894/* Display an echo area message M with a specified length of LEN
4895 chars. The string may include null characters. If M is not a
4896 string, clear out any existing message, and let the mini-buffer
4897 text show through. */
4898
4899void
4900message3 (m, len, multibyte)
4901 Lisp_Object m;
4902 int len;
4903 int multibyte;
4904{
4905 struct gcpro gcpro1;
4906
4907 GCPRO1 (m);
4908
4909 /* First flush out any partial line written with print. */
4910 message_log_maybe_newline ();
4911 if (STRINGP (m))
4912 message_dolog (XSTRING (m)->data, len, 1, multibyte);
4913 message3_nolog (m, len, multibyte);
4914
4915 UNGCPRO;
4916}
4917
4918
4919/* The non-logging version of message3. */
4920
4921void
4922message3_nolog (m, len, multibyte)
4923 Lisp_Object m;
4924 int len, multibyte;
4925{
4926 message_enable_multibyte = multibyte;
4927
4928 if (noninteractive)
4929 {
4930 if (noninteractive_need_newline)
4931 putc ('\n', stderr);
4932 noninteractive_need_newline = 0;
4933 if (STRINGP (m))
4934 fwrite (XSTRING (m)->data, len, 1, stderr);
4935 if (cursor_in_echo_area == 0)
4936 fprintf (stderr, "\n");
4937 fflush (stderr);
4938 }
4939 /* A null message buffer means that the frame hasn't really been
4940 initialized yet. Error messages get reported properly by
4941 cmd_error, so this must be just an informative message; toss it. */
4942 else if (INTERACTIVE
4943 && selected_frame->glyphs_initialized_p
4944 && FRAME_MESSAGE_BUF (selected_frame))
4945 {
4946 Lisp_Object mini_window;
4947 struct frame *f;
4948
4949 /* Get the frame containing the mini-buffer
4950 that the selected frame is using. */
4951 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4953
4954 FRAME_SAMPLE_VISIBILITY (f);
4955 if (FRAME_VISIBLE_P (selected_frame)
4956 && ! FRAME_VISIBLE_P (f))
4957 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4958
4959 if (STRINGP (m))
4960 {
4961 echo_area_glyphs = NULL;
4962 echo_area_message = m;
4963 echo_area_glyphs_length = len;
4964
4965 if (minibuffer_auto_raise)
4966 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4967 }
4968 else
4969 {
4970 echo_area_glyphs = previous_echo_glyphs = NULL;
4971 echo_area_message = previous_echo_area_message = Qnil;
4972 }
4973
4974 do_pending_window_change ();
4975 echo_area_display (1);
4976 do_pending_window_change ();
4977 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4978 (*frame_up_to_date_hook) (f);
4979 }
4980}
4981
4982
4983/* Display a null-terminated echo area message M. If M is 0, clear
4984 out any existing message, and let the mini-buffer text show through.
4985
4986 The buffer M must continue to exist until after the echo area gets
4987 cleared or some other message gets displayed there. Do not pass
4988 text that is stored in a Lisp string. Do not pass text in a buffer
4989 that was alloca'd. */
4990
4991void
4992message1 (m)
4993 char *m;
4994{
4995 message2 (m, (m ? strlen (m) : 0), 0);
4996}
4997
4998
4999/* The non-logging counterpart of message1. */
5000
5001void
5002message1_nolog (m)
5003 char *m;
5004{
5005 message2_nolog (m, (m ? strlen (m) : 0), 0);
5006}
5007
5008/* Display a message M which contains a single %s
5009 which gets replaced with STRING. */
5010
5011void
5012message_with_string (m, string, log)
5013 char *m;
5014 Lisp_Object string;
5015 int log;
5016{
5017 if (noninteractive)
5018 {
5019 if (m)
5020 {
5021 if (noninteractive_need_newline)
5022 putc ('\n', stderr);
5023 noninteractive_need_newline = 0;
5024 fprintf (stderr, m, XSTRING (string)->data);
5025 if (cursor_in_echo_area == 0)
5026 fprintf (stderr, "\n");
5027 fflush (stderr);
5028 }
5029 }
5030 else if (INTERACTIVE)
5031 {
5032 /* The frame whose minibuffer we're going to display the message on.
5033 It may be larger than the selected frame, so we need
5034 to use its buffer, not the selected frame's buffer. */
5035 Lisp_Object mini_window;
5036 FRAME_PTR f;
5037
5038 /* Get the frame containing the minibuffer
5039 that the selected frame is using. */
5040 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5041 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5042
5043 /* A null message buffer means that the frame hasn't really been
5044 initialized yet. Error messages get reported properly by
5045 cmd_error, so this must be just an informative message; toss it. */
5046 if (FRAME_MESSAGE_BUF (f))
5047 {
5048 int len;
5049 char *a[1];
5050 a[0] = (char *) XSTRING (string)->data;
5051
5052 len = doprnt (FRAME_MESSAGE_BUF (f),
5053 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5054
5055 if (log)
5056 message2 (FRAME_MESSAGE_BUF (f), len,
5057 STRING_MULTIBYTE (string));
5058 else
5059 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5060 STRING_MULTIBYTE (string));
5061
5062 /* Print should start at the beginning of the message
5063 buffer next time. */
5064 message_buf_print = 0;
5065 }
5066 }
5067}
5068
5069
5070/* Truncate what will be displayed in the echo area
5071 the next time we display it--but don't redisplay it now. */
5072
5073void
5074truncate_echo_area (len)
5075 int len;
5076{
5077 /* A null message buffer means that the frame hasn't really been
5078 initialized yet. Error messages get reported properly by
5079 cmd_error, so this must be just an informative message; toss it. */
5080 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
5081 echo_area_glyphs_length = len;
5082}
5083
5084
5085/* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by
5086 print; zero if being used by message. */
5087
5088int message_buf_print;
5089
5090
5091/* Dump an informative message to the minibuf. If M is 0, clear out
5092 any existing message, and let the mini-buffer text show through. */
5093
5094/* VARARGS 1 */
5095void
5096message (m, a1, a2, a3)
5097 char *m;
5098 EMACS_INT a1, a2, a3;
5099{
5100 if (noninteractive)
5101 {
5102 if (m)
5103 {
5104 if (noninteractive_need_newline)
5105 putc ('\n', stderr);
5106 noninteractive_need_newline = 0;
5107 fprintf (stderr, m, a1, a2, a3);
5108 if (cursor_in_echo_area == 0)
5109 fprintf (stderr, "\n");
5110 fflush (stderr);
5111 }
5112 }
5113 else if (INTERACTIVE)
5114 {
5115 /* The frame whose mini-buffer we're going to display the message
5116 on. It may be larger than the selected frame, so we need to
5117 use its buffer, not the selected frame's buffer. */
5118 Lisp_Object mini_window;
5119 struct frame *f;
5120
5121 /* Get the frame containing the mini-buffer
5122 that the selected frame is using. */
5123 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5124 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5125
5126 /* A null message buffer means that the frame hasn't really been
5127 initialized yet. Error messages get reported properly by
5128 cmd_error, so this must be just an informative message; toss
5129 it. */
5130 if (FRAME_MESSAGE_BUF (f))
5131 {
5132 if (m)
5133 {
5134 int len;
5135#ifdef NO_ARG_ARRAY
5136 char *a[3];
5137 a[0] = (char *) a1;
5138 a[1] = (char *) a2;
5139 a[2] = (char *) a3;
5140
5141 len = doprnt (FRAME_MESSAGE_BUF (f),
5142 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5143#else
5144 len = doprnt (FRAME_MESSAGE_BUF (f),
5145 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5146 (char **) &a1);
5147#endif /* NO_ARG_ARRAY */
5148
5149 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5150 }
5151 else
5152 message1 (0);
5153
5154 /* Print should start at the beginning of the message
5155 buffer next time. */
5156 message_buf_print = 0;
5157 }
5158 }
5159}
5160
5161
5162/* The non-logging version of message. */
5163
5164void
5165message_nolog (m, a1, a2, a3)
5166 char *m;
5167 EMACS_INT a1, a2, a3;
5168{
5169 Lisp_Object old_log_max;
5170 old_log_max = Vmessage_log_max;
5171 Vmessage_log_max = Qnil;
5172 message (m, a1, a2, a3);
5173 Vmessage_log_max = old_log_max;
5174}
5175
5176
5177/* Display echo_area_message or echo_area_glyphs in the current
5178 mini-buffer. */
5179
5180void
5181update_echo_area ()
5182{
5183 if (STRINGP (echo_area_message))
5184 message3 (echo_area_message, echo_area_glyphs_length,
5185 !NILP (current_buffer->enable_multibyte_characters));
5186 else
5187 message2 (echo_area_glyphs, echo_area_glyphs_length,
5188 !NILP (current_buffer->enable_multibyte_characters));
5189}
5190
5191
5192/* Redisplay the echo area of selected_frame. If UPDATE_FRAME_P is
5193 non-zero update selected_frame. */
5194
5195static void
5196echo_area_display (update_frame_p)
5197 int update_frame_p;
5198{
5199 Lisp_Object mini_window;
5200 struct window *w;
5201 struct frame *f;
5202
5203 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5204 w = XWINDOW (mini_window);
5205 f = XFRAME (WINDOW_FRAME (w));
5206
5207 /* Don't display if frame is invisible or not yet initialized. */
5208 if (!FRAME_VISIBLE_P (f)
5209 || !f->glyphs_initialized_p)
5210 return;
5211
5212 /* When Emacs starts, selected_frame may be a visible terminal
5213 frame, even if we run under a window system. If we let this
5214 through, a message would be displayed on the terminal. */
5215#ifdef HAVE_WINDOW_SYSTEM
5216 if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
5217 return;
5218#endif /* HAVE_WINDOW_SYSTEM */
5219
5220 /* Redraw garbaged frames. */
5221 if (frame_garbaged)
5222 {
5223 /* Old redisplay called redraw_garbaged_frames here which in
5224 turn called redraw_frame which in turn called clear_frame.
5225 The call to clear_frame is a source of flickering. After
5226 checking the places where SET_FRAME_GARBAGED is called, I
5227 believe a clear_frame is not necessary. It should suffice in
5228 the new redisplay to invalidate all current matrices, and
5229 ensure a complete redisplay of all windows. */
5230 Lisp_Object tail, frame;
5231
5232 FOR_EACH_FRAME (tail, frame)
5233 {
5234 struct frame *f = XFRAME (frame);
5235
5236 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5237 {
5238 clear_current_matrices (f);
5239 f->garbaged = 0;
5240 }
5241 }
5242
5243 frame_garbaged = 0;
5244 ++windows_or_buffers_changed;
5245 }
5246
5247 if (echo_area_glyphs
5248 || STRINGP (echo_area_message)
5249 || minibuf_level == 0)
5250 {
5251 struct it it;
5252
5253 echo_area_window = mini_window;
5254 clear_glyph_matrix (w->desired_matrix);
5255 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, DEFAULT_FACE_ID);
5256
5257 if (STRINGP (echo_area_message)
5258 && echo_area_glyphs_length)
5259 {
5260 prepare_desired_row (it.glyph_row);
5261 display_string (NULL, echo_area_message, Qnil, 0, 0,
5262 &it, -1, echo_area_glyphs_length, 0,
5263 message_enable_multibyte);
5264 it.glyph_row->truncated_on_right_p = 0;
5265 compute_line_metrics (&it);
5266 }
5267 else if (echo_area_glyphs
5268 && echo_area_glyphs_length)
5269 {
5270 prepare_desired_row (it.glyph_row);
5271 display_string (echo_area_glyphs, Qnil, Qnil, 0, 0, &it,
5272 -1, echo_area_glyphs_length, 0,
5273 message_enable_multibyte);
5274 it.glyph_row->truncated_on_right_p = 0;
5275 compute_line_metrics (&it);
5276 }
5277 else
5278 blank_row (w, it.glyph_row, 0);
5279
5280 it.glyph_row->y = it.current_y;
5281 it.current_y += it.glyph_row->height;
5282
5283 /* Clear the rest of the lines. */
5284 while (it.current_y < it.last_visible_y)
5285 {
5286 ++it.glyph_row;
5287 blank_row (w, it.glyph_row, it.current_y);
5288 it.current_y += it.glyph_row->height;
5289 }
5290
5291 w->must_be_updated_p = 1;
5292 if (update_frame_p)
5293 {
5294 /* Calling update_single_window is faster when we can use
5295 window-based redisplay. */
5296 if (FRAME_WINDOW_P (f))
5297 {
5298 update_single_window (w, 1);
5299 rif->flush_display (f);
5300 }
5301 else
5302 update_frame (f, 1, 1);
5303 }
5304 }
5305 else if (!EQ (mini_window, selected_window))
5306 windows_or_buffers_changed++;
5307
5308 /* Prevent redisplay optimization in redisplay_internal by resetting
5309 this_line_start_pos. This is done because the mini-buffer now
5310 displays the message instead of its buffer text. */
5311 if (EQ (mini_window, selected_window))
5312 CHARPOS (this_line_start_pos) = 0;
5313
5314 previous_echo_glyphs = echo_area_glyphs;
5315 previous_echo_area_message = echo_area_message;
5316 previous_echo_glyphs_length = echo_area_glyphs_length;
5317}
5318
5319
5320\f
5321/***********************************************************************
5322 Frame Titles
5323 ***********************************************************************/
5324
5325
5326#ifdef HAVE_WINDOW_SYSTEM
5327
5328/* A buffer for constructing frame titles in it; allocated from the
5329 heap in init_xdisp and resized as needed in store_frame_title_char. */
5330
5331static char *frame_title_buf;
5332
5333/* The buffer's end, and a current output position in it. */
5334
5335static char *frame_title_buf_end;
5336static char *frame_title_ptr;
5337
5338
5339/* Store a single character C for the frame title in frame_title_buf.
5340 Re-allocate frame_title_buf if necessary. */
5341
5342static void
5343store_frame_title_char (c)
5344 char c;
5345{
5346 /* If output position has reached the end of the allocated buffer,
5347 double the buffer's size. */
5348 if (frame_title_ptr == frame_title_buf_end)
5349 {
5350 int len = frame_title_ptr - frame_title_buf;
5351 int new_size = 2 * len * sizeof *frame_title_buf;
5352 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5353 frame_title_buf_end = frame_title_buf + new_size;
5354 frame_title_ptr = frame_title_buf + len;
5355 }
5356
5357 *frame_title_ptr++ = c;
5358}
5359
5360
5361/* Store part of a frame title in frame_title_buf, beginning at
5362 frame_title_ptr. STR is the string to store. Do not copy more
5363 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5364 the whole string. Pad with spaces until FIELD_WIDTH number of
5365 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5366 Called from display_mode_element when it is used to build a frame
5367 title. */
5368
5369static int
5370store_frame_title (str, field_width, precision)
5371 unsigned char *str;
5372 int field_width, precision;
5373{
5374 int n = 0;
5375
5376 /* Copy at most PRECISION chars from STR. */
5377 while ((precision <= 0 || n < precision)
5378 && *str)
5379 {
5380 store_frame_title_char (*str++);
5381 ++n;
5382 }
5383
5384 /* Fill up with spaces until FIELD_WIDTH reached. */
5385 while (field_width > 0
5386 && n < field_width)
5387 {
5388 store_frame_title_char (' ');
5389 ++n;
5390 }
5391
5392 return n;
5393}
5394
5395
5396/* Set the title of FRAME, if it has changed. The title format is
5397 Vicon_title_format if FRAME is iconified, otherwise it is
5398 frame_title_format. */
5399
5400static void
5401x_consider_frame_title (frame)
5402 Lisp_Object frame;
5403{
5404 struct frame *f = XFRAME (frame);
5405
5406 if (FRAME_WINDOW_P (f)
5407 || FRAME_MINIBUF_ONLY_P (f)
5408 || f->explicit_name)
5409 {
5410 /* Do we have more than one visible frame on this X display? */
5411 Lisp_Object tail;
5412 Lisp_Object fmt;
5413 struct buffer *obuf;
5414 int len;
5415 struct it it;
5416
5417 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
5418 {
5419 struct frame *tf = XFRAME (XCONS (tail)->car);
5420
5421 if (tf != f
5422 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
5423 && !FRAME_MINIBUF_ONLY_P (tf)
5424 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
5425 break;
5426 }
5427
5428 /* Set global variable indicating that multiple frames exist. */
5429 multiple_frames = CONSP (tail);
5430
5431 /* Switch to the buffer of selected window of the frame. Set up
5432 frame_title_ptr so that display_mode_element will output into it;
5433 then display the title. */
5434 obuf = current_buffer;
5435 Fset_buffer (XWINDOW (f->selected_window)->buffer);
5436 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
5437 frame_title_ptr = frame_title_buf;
5438 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
5439 NULL, DEFAULT_FACE_ID);
5440 len = display_mode_element (&it, 0, -1, -1, fmt);
5441 frame_title_ptr = NULL;
5442 set_buffer_internal (obuf);
5443
5444 /* Set the title only if it's changed. This avoids consing in
5445 the common case where it hasn't. (If it turns out that we've
5446 already wasted too much time by walking through the list with
5447 display_mode_element, then we might need to optimize at a
5448 higher level than this.) */
5449 if (! STRINGP (f->name)
5450 || STRING_BYTES (XSTRING (f->name)) != len
5451 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
5452 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
5453 }
5454}
5455
5456#else /* not HAVE_WINDOW_SYSTEM */
5457
5458#define frame_title_ptr ((char *)0)
5459#define store_frame_title(str, mincol, maxcol) 0
5460
5461#endif /* not HAVE_WINDOW_SYSTEM */
5462
5463
5464
5465\f
5466/***********************************************************************
5467 Menu Bars
5468 ***********************************************************************/
5469
5470
5471/* Prepare for redisplay by updating menu-bar item lists when
5472 appropriate. This can call eval. */
5473
5474void
5475prepare_menu_bars ()
5476{
5477 int all_windows;
5478 struct gcpro gcpro1, gcpro2;
5479 struct frame *f;
5480 struct frame *tooltip_frame;
5481
5482#ifdef HAVE_X_WINDOWS
5483 tooltip_frame = tip_frame;
5484#else
5485 tooltip_frame = NULL;
5486#endif
5487
5488 /* Update all frame titles based on their buffer names, etc. We do
5489 this before the menu bars so that the buffer-menu will show the
5490 up-to-date frame titles. */
5491#ifdef HAVE_WINDOW_SYSTEM
5492 if (windows_or_buffers_changed || update_mode_lines)
5493 {
5494 Lisp_Object tail, frame;
5495
5496 FOR_EACH_FRAME (tail, frame)
5497 {
5498 f = XFRAME (frame);
5499 if (f != tooltip_frame
5500 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
5501 x_consider_frame_title (frame);
5502 }
5503 }
5504#endif /* HAVE_WINDOW_SYSTEM */
5505
5506 /* Update the menu bar item lists, if appropriate. This has to be
5507 done before any actual redisplay or generation of display lines. */
5508 all_windows = (update_mode_lines
5509 || buffer_shared > 1
5510 || windows_or_buffers_changed);
5511 if (all_windows)
5512 {
5513 Lisp_Object tail, frame;
5514 int count = specpdl_ptr - specpdl;
5515
5516 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5517
5518 FOR_EACH_FRAME (tail, frame)
5519 {
5520 f = XFRAME (frame);
5521
5522 /* Ignore tooltip frame. */
5523 if (f == tooltip_frame)
5524 continue;
5525
5526 /* If a window on this frame changed size, report that to
5527 the user and clear the size-change flag. */
5528 if (FRAME_WINDOW_SIZES_CHANGED (f))
5529 {
5530 Lisp_Object functions;
5531
5532 /* Clear flag first in case we get an error below. */
5533 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
5534 functions = Vwindow_size_change_functions;
5535 GCPRO2 (tail, functions);
5536
5537 while (CONSP (functions))
5538 {
5539 call1 (XCAR (functions), frame);
5540 functions = XCDR (functions);
5541 }
5542 UNGCPRO;
5543 }
5544
5545 GCPRO1 (tail);
5546 update_menu_bar (f, 0);
5547#ifdef HAVE_WINDOW_SYSTEM
5548 update_toolbar (f, 0);
5549#endif
5550 UNGCPRO;
5551 }
5552
5553 unbind_to (count, Qnil);
5554 }
5555 else
5556 {
5557 update_menu_bar (selected_frame, 1);
5558#ifdef HAVE_WINDOW_SYSTEM
5559 update_toolbar (selected_frame, 1);
5560#endif
5561 }
5562
5563 /* Motif needs this. See comment in xmenu.c. Turn it off when
5564 pending_menu_activation is not defined. */
5565#ifdef USE_X_TOOLKIT
5566 pending_menu_activation = 0;
5567#endif
5568}
5569
5570
5571/* Update the menu bar item list for frame F. This has to be done
5572 before we start to fill in any display lines, because it can call
5573 eval.
5574
5575 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
5576
5577static void
5578update_menu_bar (f, save_match_data)
5579 struct frame *f;
5580 int save_match_data;
5581{
5582 Lisp_Object window;
5583 register struct window *w;
5584
5585 window = FRAME_SELECTED_WINDOW (f);
5586 w = XWINDOW (window);
5587
5588 if (update_mode_lines)
5589 w->update_mode_line = Qt;
5590
5591 if (FRAME_WINDOW_P (f)
5592 ?
5593#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5594 FRAME_EXTERNAL_MENU_BAR (f)
5595#else
5596 FRAME_MENU_BAR_LINES (f) > 0
5597#endif
5598 : FRAME_MENU_BAR_LINES (f) > 0)
5599 {
5600 /* If the user has switched buffers or windows, we need to
5601 recompute to reflect the new bindings. But we'll
5602 recompute when update_mode_lines is set too; that means
5603 that people can use force-mode-line-update to request
5604 that the menu bar be recomputed. The adverse effect on
5605 the rest of the redisplay algorithm is about the same as
5606 windows_or_buffers_changed anyway. */
5607 if (windows_or_buffers_changed
5608 || !NILP (w->update_mode_line)
5609 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
5610 < BUF_MODIFF (XBUFFER (w->buffer)))
5611 != !NILP (w->last_had_star))
5612 || ((!NILP (Vtransient_mark_mode)
5613 && !NILP (XBUFFER (w->buffer)->mark_active))
5614 != !NILP (w->region_showing)))
5615 {
5616 struct buffer *prev = current_buffer;
5617 int count = specpdl_ptr - specpdl;
5618
5619 set_buffer_internal_1 (XBUFFER (w->buffer));
5620 if (save_match_data)
5621 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5622 if (NILP (Voverriding_local_map_menu_flag))
5623 {
5624 specbind (Qoverriding_terminal_local_map, Qnil);
5625 specbind (Qoverriding_local_map, Qnil);
5626 }
5627
5628 /* Run the Lucid hook. */
5629 call1 (Vrun_hooks, Qactivate_menubar_hook);
5630
5631 /* If it has changed current-menubar from previous value,
5632 really recompute the menu-bar from the value. */
5633 if (! NILP (Vlucid_menu_bar_dirty_flag))
5634 call0 (Qrecompute_lucid_menubar);
5635
5636 safe_run_hooks (Qmenu_bar_update_hook);
5637 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
5638
5639 /* Redisplay the menu bar in case we changed it. */
5640#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5641 if (FRAME_WINDOW_P (f))
5642 set_frame_menubar (f, 0, 0);
5643 else
5644 /* On a terminal screen, the menu bar is an ordinary screen
5645 line, and this makes it get updated. */
5646 w->update_mode_line = Qt;
5647#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
5648 /* In the non-toolkit version, the menu bar is an ordinary screen
5649 line, and this makes it get updated. */
5650 w->update_mode_line = Qt;
5651#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
5652
5653 unbind_to (count, Qnil);
5654 set_buffer_internal_1 (prev);
5655 }
5656 }
5657}
5658
5659
5660\f
5661/***********************************************************************
5662 Toolbars
5663 ***********************************************************************/
5664
5665#ifdef HAVE_WINDOW_SYSTEM
5666
5667/* Update the toolbar item list for frame F. This has to be done
5668 before we start to fill in any display lines. Called from
5669 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
5670 and restore it here. */
5671
5672static void
5673update_toolbar (f, save_match_data)
5674 struct frame *f;
5675 int save_match_data;
5676{
5677 if (WINDOWP (f->toolbar_window)
5678 && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
5679 {
5680 Lisp_Object window;
5681 struct window *w;
5682
5683 window = FRAME_SELECTED_WINDOW (f);
5684 w = XWINDOW (window);
5685
5686 /* If the user has switched buffers or windows, we need to
5687 recompute to reflect the new bindings. But we'll
5688 recompute when update_mode_lines is set too; that means
5689 that people can use force-mode-line-update to request
5690 that the menu bar be recomputed. The adverse effect on
5691 the rest of the redisplay algorithm is about the same as
5692 windows_or_buffers_changed anyway. */
5693 if (windows_or_buffers_changed
5694 || !NILP (w->update_mode_line)
5695 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
5696 < BUF_MODIFF (XBUFFER (w->buffer)))
5697 != !NILP (w->last_had_star))
5698 || ((!NILP (Vtransient_mark_mode)
5699 && !NILP (XBUFFER (w->buffer)->mark_active))
5700 != !NILP (w->region_showing)))
5701 {
5702 struct buffer *prev = current_buffer;
5703 int count = specpdl_ptr - specpdl;
a2889657 5704
5f5c8ee5
GM
5705 /* Set current_buffer to the buffer of the selected
5706 window of the frame, so that we get the right local
5707 keymaps. */
5708 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 5709
5f5c8ee5
GM
5710 /* Save match data, if we must. */
5711 if (save_match_data)
5712 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5713
5714 /* Make sure that we don't accidentally use bogus keymaps. */
5715 if (NILP (Voverriding_local_map_menu_flag))
5716 {
5717 specbind (Qoverriding_terminal_local_map, Qnil);
5718 specbind (Qoverriding_local_map, Qnil);
1f40cad2 5719 }
1f40cad2 5720
5f5c8ee5
GM
5721 /* Build desired toolbar items from keymaps. */
5722 f->desired_toolbar_items
5723 = toolbar_items (f->desired_toolbar_items,
5724 &f->n_desired_toolbar_items);
5725
5726 /* Redisplay the toolbar in case we changed it. */
5727 w->update_mode_line = Qt;
5728
5729 unbind_to (count, Qnil);
5730 set_buffer_internal_1 (prev);
81d478f3 5731 }
a2889657
JB
5732 }
5733}
5734
6c4429a5 5735
5f5c8ee5
GM
5736/* Set F->desired_toolbar_string to a Lisp string representing frame
5737 F's desired toolbar contents. F->desired_toolbar_items must have
5738 been set up previously by calling prepare_menu_bars. */
5739
a2889657 5740static void
5f5c8ee5
GM
5741build_desired_toolbar_string (f)
5742 struct frame *f;
a2889657 5743{
5f5c8ee5
GM
5744 int i, size, size_needed, string_idx;
5745 struct gcpro gcpro1, gcpro2, gcpro3;
5746 Lisp_Object image, plist, props;
a2889657 5747
5f5c8ee5
GM
5748 image = plist = props = Qnil;
5749 GCPRO3 (image, plist, props);
a2889657 5750
5f5c8ee5
GM
5751 /* Prepare F->desired_toolbar_string. If we can reuse it, do so.
5752 Otherwise, make a new string. */
5753
5754 /* The size of the string we might be able to reuse. */
5755 size = (STRINGP (f->desired_toolbar_string)
5756 ? XSTRING (f->desired_toolbar_string)->size
5757 : 0);
5758
5759 /* Each image in the string we build is preceded by a space,
5760 and there is a space at the end. */
5761 size_needed = f->n_desired_toolbar_items + 1;
5762
5763 /* Reuse f->desired_toolbar_string, if possible. */
5764 if (size < size_needed)
5765 f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
5766 else
5767 {
5768 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
5769 Fremove_text_properties (make_number (0), make_number (size),
5770 props, f->desired_toolbar_string);
5771 }
a2889657 5772
5f5c8ee5
GM
5773 /* Put a `display' property on the string for the images to display,
5774 put a `menu_item' property on toolbar items with a value that
5775 is the index of the item in F's toolbar item vector. */
5776 for (i = 0, string_idx = 0;
5777 i < f->n_desired_toolbar_items;
5778 ++i, string_idx += 1)
a2889657 5779 {
5f5c8ee5
GM
5780#define PROP(IDX) \
5781 (XVECTOR (f->desired_toolbar_items) \
5782 ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
5783
5784 int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
5785 int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
5786 int margin, relief;
5787 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
5788 extern Lisp_Object Qlaplace;
5789
5790 /* If image is a vector, choose the image according to the
5791 button state. */
5792 image = PROP (TOOLBAR_ITEM_IMAGES);
5793 if (VECTORP (image))
5794 {
5795 enum toolbar_item_image idx;
5796
5797 if (enabled_p)
5798 idx = (selected_p
5799 ? TOOLBAR_IMAGE_ENABLED_SELECTED
5800 : TOOLBAR_IMAGE_ENABLED_DESELECTED);
5801 else
5802 idx = (selected_p
5803 ? TOOLBAR_IMAGE_DISABLED_SELECTED
5804 : TOOLBAR_IMAGE_DISABLED_DESELECTED);
5805
5806 xassert (XVECTOR (image)->size >= idx);
5807 image = XVECTOR (image)->contents[idx];
5808 }
5809
5810 /* Ignore invalid image specifications. */
5811 if (!valid_image_p (image))
5812 continue;
5813
5814 /* Display the toolbar button pressed, or depressed. */
5815 plist = Fcopy_sequence (XCDR (image));
5816
5817 /* Compute margin and relief to draw. */
5818 relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
5819 margin = relief + max (0, toolbar_button_margin);
5820
5821 if (auto_raise_toolbar_buttons_p)
5822 {
5823 /* Add a `:relief' property to the image spec if the item is
5824 selected. */
5825 if (selected_p)
5826 {
5827 plist = Fplist_put (plist, QCrelief, make_number (-relief));
5828 margin -= relief;
5829 }
5830 }
5831 else
5832 {
5833 /* If image is selected, display it pressed, i.e. with a
5834 negative relief. If it's not selected, display it with a
5835 raised relief. */
5836 plist = Fplist_put (plist, QCrelief,
5837 (selected_p
5838 ? make_number (-relief)
5839 : make_number (relief)));
5840 margin -= relief;
5841 }
5842
5843 /* Put a margin around the image. */
5844 if (margin)
5845 plist = Fplist_put (plist, QCmargin, make_number (margin));
5846
5847 /* If button is not enabled, make the image appear disabled by
5848 applying an appropriate algorithm to it. */
5849 if (!enabled_p)
5850 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
5851
5852 /* Put a `display' text property on the string for the image to
5853 display. Put a `menu-item' property on the string that gives
5854 the start of this item's properties in the toolbar items
5855 vector. */
5856 image = Fcons (Qimage, plist);
5857 props = list4 (Qdisplay, image,
5858 Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
5859 Fadd_text_properties (make_number (string_idx),
5860 make_number (string_idx + 1),
5861 props, f->desired_toolbar_string);
5862#undef PROP
a2889657
JB
5863 }
5864
5f5c8ee5
GM
5865 UNGCPRO;
5866}
5867
5868
5869/* Display one line of the toolbar of frame IT->f. */
5870
5871static void
5872display_toolbar_line (it)
5873 struct it *it;
5874{
5875 struct glyph_row *row = it->glyph_row;
5876 int max_x = it->last_visible_x;
5877 struct glyph *last;
5878
5879 prepare_desired_row (row);
5880 row->y = it->current_y;
5881
5882 while (it->current_x < max_x)
a2889657 5883 {
5f5c8ee5 5884 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 5885
5f5c8ee5
GM
5886 /* Get the next display element. */
5887 if (!get_next_display_element (it))
5888 break;
73af359d 5889
5f5c8ee5
GM
5890 /* Produce glyphs. */
5891 x_before = it->current_x;
5892 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
5893 PRODUCE_GLYPHS (it);
daa37602 5894
5f5c8ee5
GM
5895 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
5896 i = 0;
5897 x = x_before;
5898 while (i < nglyphs)
5899 {
5900 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
5901
5902 if (x + glyph->pixel_width > max_x)
5903 {
5904 /* Glyph doesn't fit on line. */
5905 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
5906 it->current_x = x;
5907 goto out;
5908 }
daa37602 5909
5f5c8ee5
GM
5910 ++it->hpos;
5911 x += glyph->pixel_width;
5912 ++i;
5913 }
5914
5915 /* Stop at line ends. */
5916 if (ITERATOR_AT_END_OF_LINE_P (it))
5917 break;
5918
5919 set_iterator_to_next (it);
a2889657 5920 }
a2889657 5921
5f5c8ee5 5922 out:;
a2889657 5923
5f5c8ee5
GM
5924 row->displays_text_p = row->used[TEXT_AREA] != 0;
5925 extend_face_to_end_of_line (it);
5926 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
5927 last->right_box_line_p = 1;
5928 compute_line_metrics (it);
5929
5930 /* If line is empty, make it occupy the rest of the toolbar. */
5931 if (!row->displays_text_p)
5932 {
312246d1
GM
5933 row->height = row->phys_height = it->last_visible_y - row->y;
5934 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
5935 }
5936
5937 row->full_width_p = 1;
5938 row->continued_p = 0;
5939 row->truncated_on_left_p = 0;
5940 row->truncated_on_right_p = 0;
5941
5942 it->current_x = it->hpos = 0;
5943 it->current_y += row->height;
5944 ++it->vpos;
5945 ++it->glyph_row;
a2889657 5946}
96a410bc 5947
5f5c8ee5
GM
5948
5949/* Value is the number of screen lines needed to make all toolbar
5950 items of frame F visible. */
96a410bc 5951
d39b6696 5952static int
5f5c8ee5
GM
5953toolbar_lines_needed (f)
5954 struct frame *f;
d39b6696 5955{
5f5c8ee5
GM
5956 struct window *w = XWINDOW (f->toolbar_window);
5957 struct it it;
5958
5959 /* Initialize an iterator for iteration over F->desired_toolbar_string
5960 in the toolbar window of frame F. */
5961 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
5962 it.first_visible_x = 0;
5963 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
5964 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
5965
5966 while (!ITERATOR_AT_END_P (&it))
5967 {
5968 it.glyph_row = w->desired_matrix->rows;
5969 clear_glyph_row (it.glyph_row);
5970 display_toolbar_line (&it);
5971 }
5972
5973 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 5974}
96a410bc 5975
5f5c8ee5
GM
5976
5977/* Display the toolbar of frame F. Value is non-zero if toolbar's
5978 height should be changed. */
5979
5980static int
5981redisplay_toolbar (f)
5982 struct frame *f;
96a410bc 5983{
5f5c8ee5
GM
5984 struct window *w;
5985 struct it it;
5986 struct glyph_row *row;
5987 int change_height_p = 0;
5988
5989 /* If frame hasn't a toolbar window or if it is zero-height, don't
5990 do anything. This means you must start with toolbar-lines
5991 non-zero to get the auto-sizing effect. Or in other words, you
5992 can turn off toolbars by specifying toolbar-lines zero. */
5993 if (!WINDOWP (f->toolbar_window)
5994 || (w = XWINDOW (f->toolbar_window),
5995 XFASTINT (w->height) == 0))
5996 return 0;
96a410bc 5997
5f5c8ee5
GM
5998 /* Set up an iterator for the toolbar window. */
5999 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6000 it.first_visible_x = 0;
6001 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6002 row = it.glyph_row;
3450d04c 6003
5f5c8ee5
GM
6004 /* Build a string that represents the contents of the toolbar. */
6005 build_desired_toolbar_string (f);
6006 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
3450d04c 6007
5f5c8ee5
GM
6008 /* Display as many lines as needed to display all toolbar items. */
6009 while (it.current_y < it.last_visible_y)
6010 display_toolbar_line (&it);
3450d04c 6011
5f5c8ee5
GM
6012 /* It doesn't make much sense to try scrolling in the toolbar
6013 window, so don't do it. */
6014 w->desired_matrix->no_scrolling_p = 1;
6015 w->must_be_updated_p = 1;
3450d04c 6016
5f5c8ee5
GM
6017 if (auto_resize_toolbars_p)
6018 {
6019 int nlines;
6020
6021 /* If there are blank lines at the end, except for a partially
6022 visible blank line at the end that is smaller than
6023 CANON_Y_UNIT, change the toolbar's height. */
6024 row = it.glyph_row - 1;
6025 if (!row->displays_text_p
6026 && row->height >= CANON_Y_UNIT (f))
6027 change_height_p = 1;
6028
6029 /* If row displays toolbar items, but is partially visible,
6030 change the toolbar's height. */
6031 if (row->displays_text_p
6032 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6033 change_height_p = 1;
6034
6035 /* Resize windows as needed by changing the `toolbar-lines'
6036 frame parameter. */
6037 if (change_height_p
6038 && (nlines = toolbar_lines_needed (f),
6039 nlines != XFASTINT (w->height)))
6040 {
6041 extern Lisp_Object Qtoolbar_lines;
6042 Lisp_Object frame;
6043
6044 XSETFRAME (frame, f);
6045 clear_glyph_matrix (w->desired_matrix);
6046 Fmodify_frame_parameters (frame,
6047 Fcons (Fcons (Qtoolbar_lines,
6048 make_number (nlines)),
6049 Qnil));
6050 fonts_changed_p = 1;
6051 }
6052 }
3450d04c 6053
5f5c8ee5 6054 return change_height_p;
96a410bc 6055}
90adcf20 6056
5f5c8ee5
GM
6057
6058/* Get information about the toolbar item which is displayed in GLYPH
6059 on frame F. Return in *PROP_IDX the index where toolbar item
6060 properties start in F->current_toolbar_items. Value is zero if
6061 GLYPH doesn't display a toolbar item. */
6062
6063int
6064toolbar_item_info (f, glyph, prop_idx)
6065 struct frame *f;
6066 struct glyph *glyph;
6067 int *prop_idx;
90adcf20 6068{
5f5c8ee5
GM
6069 Lisp_Object prop;
6070 int success_p;
6071
6072 /* Get the text property `menu-item' at pos. The value of that
6073 property is the start index of this item's properties in
6074 F->current_toolbar_items. */
6075 prop = Fget_text_property (make_number (glyph->charpos),
6076 Qmenu_item, f->current_toolbar_string);
6077 if (INTEGERP (prop))
6078 {
6079 *prop_idx = XINT (prop);
6080 success_p = 1;
6081 }
6082 else
6083 success_p = 0;
90adcf20 6084
5f5c8ee5
GM
6085 return success_p;
6086}
6087
6088#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6089
feb0c42f 6090
5f5c8ee5
GM
6091\f
6092/************************************************************************
6093 Horizontal scrolling
6094 ************************************************************************/
feb0c42f 6095
5f5c8ee5
GM
6096static int hscroll_window_tree P_ ((Lisp_Object));
6097static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6098
5f5c8ee5
GM
6099/* For all leaf windows in the window tree rooted at WINDOW, set their
6100 hscroll value so that PT is (i) visible in the window, and (ii) so
6101 that it is not within a certain margin at the window's left and
6102 right border. Value is non-zero if any window's hscroll has been
6103 changed. */
6104
6105static int
6106hscroll_window_tree (window)
6107 Lisp_Object window;
6108{
6109 int hscrolled_p = 0;
6110
6111 while (WINDOWP (window))
90adcf20 6112 {
5f5c8ee5
GM
6113 struct window *w = XWINDOW (window);
6114
6115 if (WINDOWP (w->hchild))
6116 hscrolled_p |= hscroll_window_tree (w->hchild);
6117 else if (WINDOWP (w->vchild))
6118 hscrolled_p |= hscroll_window_tree (w->vchild);
6119 else if (w->cursor.vpos >= 0)
6120 {
6121 int hscroll_margin, text_area_x, text_area_y;
6122 int text_area_width, text_area_height;
6123 struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
6124 w->cursor.vpos);
a2725ab2 6125
5f5c8ee5
GM
6126 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6127 &text_area_width, &text_area_height);
90adcf20 6128
5f5c8ee5
GM
6129 /* Scroll when cursor is inside this scroll margin. */
6130 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6131
6132 if ((XFASTINT (w->hscroll)
6133 && w->cursor.x < hscroll_margin)
6134 || (cursor_row->truncated_on_right_p
6135 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 6136 {
5f5c8ee5
GM
6137 struct it it;
6138 int hscroll;
6139 struct buffer *saved_current_buffer;
6140 int pt;
6141
6142 /* Find point in a display of infinite width. */
6143 saved_current_buffer = current_buffer;
6144 current_buffer = XBUFFER (w->buffer);
6145
6146 if (w == XWINDOW (selected_window))
6147 pt = BUF_PT (current_buffer);
6148 else
08b610e4 6149 {
5f5c8ee5
GM
6150 pt = marker_position (w->pointm);
6151 pt = max (BEGV, pt);
6152 pt = min (ZV, pt);
6153 }
6154
6155 /* Move iterator to pt starting at cursor_row->start in
6156 a line with infinite width. */
6157 init_to_row_start (&it, w, cursor_row);
6158 it.last_visible_x = INFINITY;
6159 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6160 current_buffer = saved_current_buffer;
6161
6162 /* Center cursor in window. */
6163 hscroll = (max (0, it.current_x - text_area_width / 2)
6164 / CANON_X_UNIT (it.f));
6165
6166 /* Don't call Fset_window_hscroll if value hasn't
6167 changed because it will prevent redisplay
6168 optimizations. */
6169 if (XFASTINT (w->hscroll) != hscroll)
6170 {
6171 Fset_window_hscroll (window, make_number (hscroll));
6172 hscrolled_p = 1;
08b610e4 6173 }
08b610e4 6174 }
08b610e4 6175 }
a2725ab2 6176
5f5c8ee5 6177 window = w->next;
90adcf20 6178 }
cd6dfed6 6179
5f5c8ee5
GM
6180 /* Value is non-zero if hscroll of any leaf window has been changed. */
6181 return hscrolled_p;
6182}
6183
6184
6185/* Set hscroll so that cursor is visible and not inside horizontal
6186 scroll margins for all windows in the tree rooted at WINDOW. See
6187 also hscroll_window_tree above. Value is non-zero if any window's
6188 hscroll has been changed. If it has, desired matrices on the frame
6189 of WINDOW are cleared. */
6190
6191static int
6192hscroll_windows (window)
6193 Lisp_Object window;
6194{
6195 int hscrolled_p = hscroll_window_tree (window);
6196 if (hscrolled_p)
6197 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6198 return hscrolled_p;
90adcf20 6199}
5f5c8ee5
GM
6200
6201
90adcf20 6202\f
5f5c8ee5
GM
6203/************************************************************************
6204 Redisplay
6205 ************************************************************************/
6206
6207/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6208 to a non-zero value. This is sometimes handy to have in a debugger
6209 session. */
6210
6211#if GLYPH_DEBUG
a2889657 6212
5f5c8ee5
GM
6213/* Values of beg_unchanged and end_unchanged as of last call to
6214 try_window_id. */
6215
6216int debug_beg_unchanged, debug_end_unchanged;
6217
6218/* First and last unchanged row for try_window_id. */
6219
6220int debug_first_unchanged_at_end_vpos;
6221int debug_last_unchanged_at_beg_vpos;
6222
6223/* Delta vpos and y. */
6224
6225int debug_dvpos, debug_dy;
6226
6227/* Delta in characters and bytes for try_window_id. */
6228
6229int debug_delta, debug_delta_bytes;
6230
6231/* Values of window_end_pos and window_end_vpos at the end of
6232 try_window_id. */
6233
6234int debug_end_pos, debug_end_vpos;
6235
6236/* Append a string to W->desired_matrix->method. FMT is a printf
6237 format string. A1...A9 are a supplement for a variable-length
6238 argument list. If trace_redisplay_p is non-zero also printf the
6239 resulting string to stderr. */
6240
6241static void
6242debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6243 struct window *w;
6244 char *fmt;
6245 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6246{
6247 char buffer[512];
6248 char *method = w->desired_matrix->method;
6249 int len = strlen (method);
6250 int size = sizeof w->desired_matrix->method;
6251 int remaining = size - len - 1;
6252
6253 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6254 if (len && remaining)
6255 {
6256 method[len] = '|';
6257 --remaining, ++len;
6258 }
6259
6260 strncpy (method + len, buffer, remaining);
6261
6262 if (trace_redisplay_p)
6263 fprintf (stderr, "%p (%s): %s\n",
6264 w,
6265 ((BUFFERP (w->buffer)
6266 && STRINGP (XBUFFER (w->buffer)->name))
6267 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6268 : "no buffer"),
6269 buffer);
6270}
a2889657 6271
5f5c8ee5 6272#endif /* GLYPH_DEBUG */
90adcf20 6273
a2889657 6274
5f5c8ee5
GM
6275/* This counter is used to clear the face cache every once in a while
6276 in redisplay_internal. It is incremented for each redisplay.
6277 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6278 cleared. */
0d231165 6279
5f5c8ee5 6280#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
6281static int clear_face_cache_count;
6282
20de20dc 6283/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
6284
6285static struct frame *previous_terminal_frame;
6286
6287/* Non-zero while redisplay_internal is in progress. */
6288
6289int redisplaying_p;
6290
6291
6292/* Value is non-zero if all changes in window W, which displays
6293 current_buffer, are in the text between START and END. START is a
6294 buffer position, END is given as a distance from Z. Used in
6295 redisplay_internal for display optimization. */
6296
6297static INLINE int
6298text_outside_line_unchanged_p (w, start, end)
6299 struct window *w;
6300 int start, end;
6301{
6302 int unchanged_p = 1;
6303
6304 /* If text or overlays have changed, see where. */
6305 if (XFASTINT (w->last_modified) < MODIFF
6306 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6307 {
6308 /* Gap in the line? */
6309 if (GPT < start || Z - GPT < end)
6310 unchanged_p = 0;
6311
6312 /* Changes start in front of the line, or end after it? */
6313 if (unchanged_p
6314 && (beg_unchanged < start - 1
6315 || end_unchanged < end))
6316 unchanged_p = 0;
6317
6318 /* If selective display, can't optimize if changes start at the
6319 beginning of the line. */
6320 if (unchanged_p
6321 && INTEGERP (current_buffer->selective_display)
6322 && XINT (current_buffer->selective_display) > 0
6323 && (beg_unchanged < start || GPT <= start))
6324 unchanged_p = 0;
6325 }
6326
6327 return unchanged_p;
6328}
6329
6330
6331/* Do a frame update, taking possible shortcuts into account. This is
6332 the main external entry point for redisplay.
6333
6334 If the last redisplay displayed an echo area message and that message
6335 is no longer requested, we clear the echo area or bring back the
6336 mini-buffer if that is in use. */
20de20dc 6337
a2889657
JB
6338void
6339redisplay ()
e9874cee
RS
6340{
6341 redisplay_internal (0);
6342}
6343
5f5c8ee5
GM
6344
6345/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6346 response to any user action; therefore, we should preserve the echo
6347 area. (Actually, our caller does that job.) Perhaps in the future
6348 avoid recentering windows if it is not necessary; currently that
6349 causes some problems. */
e9874cee
RS
6350
6351static void
6352redisplay_internal (preserve_echo_area)
6353 int preserve_echo_area;
a2889657 6354{
5f5c8ee5
GM
6355 struct window *w = XWINDOW (selected_window);
6356 struct frame *f = XFRAME (w->frame);
6357 int pause;
a2889657 6358 int must_finish = 0;
5f5c8ee5 6359 struct text_pos tlbufpos, tlendpos;
89819bdd 6360 int number_of_visible_frames;
a2889657 6361
5f5c8ee5
GM
6362 /* Non-zero means redisplay has to consider all windows on all
6363 frames. Zero means, only selected_window is considered. */
6364 int consider_all_windows_p;
6365
6366 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6367
6368 /* No redisplay if running in batch mode or frame is not yet fully
6369 initialized, or redisplay is explicitly turned off by setting
6370 Vinhibit_redisplay. */
6371 if (noninteractive
6372 || !NILP (Vinhibit_redisplay)
6373 || !f->glyphs_initialized_p)
a2889657
JB
6374 return;
6375
5f5c8ee5
GM
6376 /* The flag redisplay_performed_directly_p is set by
6377 direct_output_for_insert when it already did the whole screen
6378 update necessary. */
6379 if (redisplay_performed_directly_p)
6380 {
6381 redisplay_performed_directly_p = 0;
6382 if (!hscroll_windows (selected_window))
6383 return;
6384 }
6385
15f0cf78
RS
6386#ifdef USE_X_TOOLKIT
6387 if (popup_activated ())
6388 return;
6389#endif
6390
5f5c8ee5 6391 if (redisplaying_p)
735c094c 6392 return;
5f5c8ee5 6393 ++redisplaying_p;
735c094c 6394
8b32d885
RS
6395 retry:
6396
5f5c8ee5
GM
6397 /* If new fonts have been loaded that make a glyph matrix adjustment
6398 necessary, do it. */
6399 if (fonts_changed_p)
6400 {
6401 adjust_glyphs (NULL);
6402 ++windows_or_buffers_changed;
6403 fonts_changed_p = 0;
6404 }
6405
fd8ff63d 6406 if (! FRAME_WINDOW_P (selected_frame)
20de20dc
RS
6407 && previous_terminal_frame != selected_frame)
6408 {
5f5c8ee5
GM
6409 /* Since frames on an ASCII terminal share the same display
6410 area, displaying a different frame means redisplay the whole
6411 thing. */
20de20dc
RS
6412 windows_or_buffers_changed++;
6413 SET_FRAME_GARBAGED (selected_frame);
6414 XSETFRAME (Vterminal_frame, selected_frame);
6415 }
6416 previous_terminal_frame = selected_frame;
20de20dc 6417
5f5c8ee5
GM
6418 /* Set the visible flags for all frames. Do this before checking
6419 for resized or garbaged frames; they want to know if their frames
6420 are visible. See the comment in frame.h for
6421 FRAME_SAMPLE_VISIBILITY. */
d724d989 6422 {
35f56f96 6423 Lisp_Object tail, frame;
d724d989 6424
89819bdd
RS
6425 number_of_visible_frames = 0;
6426
35f56f96 6427 FOR_EACH_FRAME (tail, frame)
f82aff7c 6428 {
5f5c8ee5
GM
6429 struct frame *f = XFRAME (frame);
6430
6431 FRAME_SAMPLE_VISIBILITY (f);
6432 if (FRAME_VISIBLE_P (f))
6433 ++number_of_visible_frames;
6434 clear_desired_matrices (f);
f82aff7c 6435 }
d724d989
JB
6436 }
6437
44fa5b1e 6438 /* Notice any pending interrupt request to change frame size. */
a2889657
JB
6439 do_pending_window_change ();
6440
5f5c8ee5 6441 /* Clear frames marked as garbaged. */
44fa5b1e 6442 if (frame_garbaged)
a2889657 6443 {
5f5c8ee5
GM
6444 /* Old redisplay called redraw_garbaged_frames here which in
6445 turn called redraw_frame which in turn called clear_frame.
6446 The call to clear_frame is a source of flickering. After
6447 checking the places where SET_FRAME_GARBAGED is called, I
6448 believe a clear_frame is not necessary. It should suffice in
6449 the new redisplay to invalidate all current matrices, and
6450 ensure a complete redisplay of all windows. */
6451 Lisp_Object tail, frame;
6452
6453 FOR_EACH_FRAME (tail, frame)
6454 {
6455 struct frame *f = XFRAME (frame);
6456
6457 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6458 {
6459 clear_current_matrices (f);
6460 f->garbaged = 0;
6461 }
6462 }
6463
44fa5b1e 6464 frame_garbaged = 0;
5f5c8ee5 6465 ++windows_or_buffers_changed;
a2889657
JB
6466 }
6467
5f5c8ee5 6468 /* Build menubar and toolbar items. */
f82aff7c
RS
6469 prepare_menu_bars ();
6470
28995e67 6471 if (windows_or_buffers_changed)
a2889657
JB
6472 update_mode_lines++;
6473
538f13d4
RS
6474 /* Detect case that we need to write or remove a star in the mode line. */
6475 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
6476 {
6477 w->update_mode_line = Qt;
6478 if (buffer_shared > 1)
6479 update_mode_lines++;
6480 }
6481
5f5c8ee5 6482 /* If %c is in the mode line, update it if needed. */
28995e67
RS
6483 if (!NILP (w->column_number_displayed)
6484 /* This alternative quickly identifies a common case
6485 where no change is needed. */
6486 && !(PT == XFASTINT (w->last_point)
8850a573
RS
6487 && XFASTINT (w->last_modified) >= MODIFF
6488 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
6489 && XFASTINT (w->column_number_displayed) != current_column ())
6490 w->update_mode_line = Qt;
6491
44fa5b1e 6492 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 6493
5f5c8ee5
GM
6494 /* The variable buffer_shared is set in redisplay_window and
6495 indicates that we redisplay a buffer in different windows. See
6496 there. */
6497 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
6498
6499 /* If specs for an arrow have changed, do thorough redisplay
6500 to ensure we remove any arrow that should no longer exist. */
d45de95b 6501 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 6502 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 6503 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 6504
90adcf20
RS
6505 /* Normally the message* functions will have already displayed and
6506 updated the echo area, but the frame may have been trashed, or
6507 the update may have been preempted, so display the echo area
6508 again here. */
5f5c8ee5
GM
6509 if (echo_area_glyphs
6510 || STRINGP (echo_area_message)
6511 || previous_echo_glyphs
6512 || STRINGP (previous_echo_area_message))
90adcf20 6513 {
5f5c8ee5 6514 echo_area_display (0);
90adcf20
RS
6515 must_finish = 1;
6516 }
6517
5f5c8ee5
GM
6518 /* If showing the region, and mark has changed, we must redisplay
6519 the whole window. The assignment to this_line_start_pos prevents
6520 the optimization directly below this if-statement. */
bd66d1ba
RS
6521 if (((!NILP (Vtransient_mark_mode)
6522 && !NILP (XBUFFER (w->buffer)->mark_active))
6523 != !NILP (w->region_showing))
82d04750
JB
6524 || (!NILP (w->region_showing)
6525 && !EQ (w->region_showing,
6526 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
6527 CHARPOS (this_line_start_pos) = 0;
6528
6529 /* Optimize the case that only the line containing the cursor in the
6530 selected window has changed. Variables starting with this_ are
6531 set in display_line and record information about the line
6532 containing the cursor. */
6533 tlbufpos = this_line_start_pos;
6534 tlendpos = this_line_end_pos;
6535 if (!consider_all_windows_p
6536 && CHARPOS (tlbufpos) > 0
6537 && NILP (w->update_mode_line)
73af359d 6538 && !current_buffer->clip_changed
44fa5b1e 6539 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 6540 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 6541 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
6542 && this_line_buffer == current_buffer
6543 && current_buffer == XBUFFER (w->buffer)
265a9e55 6544 && NILP (w->force_start)
5f5c8ee5
GM
6545 /* Point must be on the line that we have info recorded about. */
6546 && PT >= CHARPOS (tlbufpos)
6547 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
6548 /* All text outside that line, including its final newline,
6549 must be unchanged */
5f5c8ee5
GM
6550 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
6551 CHARPOS (tlendpos)))
6552 {
6553 if (CHARPOS (tlbufpos) > BEGV
6554 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
6555 && (CHARPOS (tlbufpos) == ZV
6556 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
6557 /* Former continuation line has disappeared by becoming empty */
6558 goto cancel;
6559 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 6560 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
6561 || MINI_WINDOW_P (w))
6562 {
1c9241f5
KH
6563 /* We have to handle the case of continuation around a
6564 wide-column character (See the comment in indent.c around
6565 line 885).
6566
6567 For instance, in the following case:
6568
6569 -------- Insert --------
6570 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
6571 J_I_ ==> J_I_ `^^' are cursors.
6572 ^^ ^^
6573 -------- --------
6574
6575 As we have to redraw the line above, we should goto cancel. */
6576
5f5c8ee5
GM
6577 struct it it;
6578 int line_height_before = this_line_pixel_height;
6579
6580 /* Note that start_display will handle the case that the
6581 line starting at tlbufpos is a continuation lines. */
6582 start_display (&it, w, tlbufpos);
6583
6584 /* Implementation note: It this still necessary? */
6585 if (it.current_x != this_line_start_x)
1c9241f5
KH
6586 goto cancel;
6587
5f5c8ee5
GM
6588 TRACE ((stderr, "trying display optimization 1\n"));
6589 w->cursor.vpos = -1;
a2889657 6590 overlay_arrow_seen = 0;
5f5c8ee5
GM
6591 it.vpos = this_line_vpos;
6592 it.current_y = this_line_y;
6593 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
6594 display_line (&it);
6595
a2889657 6596 /* If line contains point, is not continued,
5f5c8ee5
GM
6597 and ends at same distance from eob as before, we win */
6598 if (w->cursor.vpos >= 0
6599 /* Line is not continued, otherwise this_line_start_pos
6600 would have been set to 0 in display_line. */
6601 && CHARPOS (this_line_start_pos)
6602 /* Line ends as before. */
6603 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
6604 /* Line has same height as before. Otherwise other lines
6605 would have to be shifted up or down. */
6606 && this_line_pixel_height == line_height_before)
a2889657 6607 {
5f5c8ee5
GM
6608 /* If this is not the window's last line, we must adjust
6609 the charstarts of the lines below. */
6610 if (it.current_y < it.last_visible_y)
6611 {
6612 struct glyph_row *row
6613 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
6614 int delta, delta_bytes;
6615
6616 if (Z - CHARPOS (tlendpos) == ZV)
6617 {
6618 /* This line ends at end of (accessible part of)
6619 buffer. There is no newline to count. */
6620 delta = (Z
6621 - CHARPOS (tlendpos)
6622 - MATRIX_ROW_START_CHARPOS (row));
6623 delta_bytes = (Z_BYTE
6624 - BYTEPOS (tlendpos)
6625 - MATRIX_ROW_START_BYTEPOS (row));
6626 }
6627 else
6628 {
6629 /* This line ends in a newline. Must take
6630 account of the newline and the rest of the
6631 text that follows. */
6632 delta = (Z
6633 - CHARPOS (tlendpos)
6634 - MATRIX_ROW_START_CHARPOS (row));
6635 delta_bytes = (Z_BYTE
6636 - BYTEPOS (tlendpos)
6637 - MATRIX_ROW_START_BYTEPOS (row));
6638 }
6639
6640 increment_glyph_matrix_buffer_positions (w->current_matrix,
6641 this_line_vpos + 1,
6642 w->current_matrix->nrows,
6643 delta, delta_bytes);
85bcef6c 6644 }
46db8486 6645
5f5c8ee5
GM
6646 /* If this row displays text now but previously didn't,
6647 or vice versa, w->window_end_vpos may have to be
6648 adjusted. */
6649 if ((it.glyph_row - 1)->displays_text_p)
6650 {
6651 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
6652 XSETINT (w->window_end_vpos, this_line_vpos);
6653 }
6654 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
6655 && this_line_vpos > 0)
6656 XSETINT (w->window_end_vpos, this_line_vpos - 1);
6657 w->window_end_valid = Qnil;
6658
6659 /* Update hint: No need to try to scroll in update_window. */
6660 w->desired_matrix->no_scrolling_p = 1;
6661
6662#if GLYPH_DEBUG
6663 *w->desired_matrix->method = 0;
6664 debug_method_add (w, "optimization 1");
6665#endif
a2889657
JB
6666 goto update;
6667 }
6668 else
6669 goto cancel;
6670 }
5f5c8ee5
GM
6671 else if (/* Cursor position hasn't changed. */
6672 PT == XFASTINT (w->last_point)
b6f0fe04
RS
6673 /* Make sure the cursor was last displayed
6674 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
6675 && 0 <= w->cursor.vpos
6676 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
6677 {
6678 if (!must_finish)
6679 {
6680 do_pending_window_change ();
5f5c8ee5
GM
6681
6682 /* We used to always goto end_of_redisplay here, but this
6683 isn't enough if we have a blinking cursor. */
6684 if (w->cursor_off_p == w->last_cursor_off_p)
6685 goto end_of_redisplay;
a2889657
JB
6686 }
6687 goto update;
6688 }
8b51f1e3
KH
6689 /* If highlighting the region, or if the cursor is in the echo area,
6690 then we can't just move the cursor. */
bd66d1ba
RS
6691 else if (! (!NILP (Vtransient_mark_mode)
6692 && !NILP (current_buffer->mark_active))
293a54ce
RS
6693 && (w == XWINDOW (current_buffer->last_selected_window)
6694 || highlight_nonselected_windows)
8b51f1e3
KH
6695 && NILP (w->region_showing)
6696 && !cursor_in_echo_area)
a2889657 6697 {
5f5c8ee5
GM
6698 struct it it;
6699 struct glyph_row *row;
6700
6701 /* Skip from tlbufpos to PT and see where it is. Note that
6702 PT may be in invisible text. If so, we will end at the
6703 next visible position. */
6704 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
6705 NULL, DEFAULT_FACE_ID);
6706 it.current_x = this_line_start_x;
6707 it.current_y = this_line_y;
6708 it.vpos = this_line_vpos;
6709
6710 /* The call to move_it_to stops in front of PT, but
6711 moves over before-strings. */
6712 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
6713
6714 if (it.vpos == this_line_vpos
6715 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
6716 row->enabled_p))
a2889657 6717 {
5f5c8ee5
GM
6718 xassert (this_line_vpos == it.vpos);
6719 xassert (this_line_y == it.current_y);
6720 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
6721 goto update;
6722 }
6723 else
6724 goto cancel;
6725 }
5f5c8ee5 6726
a2889657 6727 cancel:
5f5c8ee5
GM
6728 /* Text changed drastically or point moved off of line. */
6729 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
6730 }
6731
5f5c8ee5
GM
6732 CHARPOS (this_line_start_pos) = 0;
6733 consider_all_windows_p |= buffer_shared > 1;
6734 ++clear_face_cache_count;
a2889657 6735
5f5c8ee5
GM
6736
6737 /* Build desired matrices. If consider_all_windows_p is non-zero,
6738 do it for all windows on all frames. Otherwise do it for
6739 selected_window, only. */
463f6b91 6740
5f5c8ee5 6741 if (consider_all_windows_p)
a2889657 6742 {
35f56f96 6743 Lisp_Object tail, frame;
a2889657 6744
5f5c8ee5
GM
6745 /* Clear the face cache eventually. */
6746 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 6747 {
5f5c8ee5 6748 clear_face_cache (0);
463f6b91
RS
6749 clear_face_cache_count = 0;
6750 }
31b24551 6751
5f5c8ee5
GM
6752 /* Recompute # windows showing selected buffer. This will be
6753 incremented each time such a window is displayed. */
a2889657
JB
6754 buffer_shared = 0;
6755
35f56f96 6756 FOR_EACH_FRAME (tail, frame)
30c566e4 6757 {
5f5c8ee5 6758 struct frame *f = XFRAME (frame);
fd8ff63d 6759 if (FRAME_WINDOW_P (f) || f == selected_frame)
9769686d 6760 {
5f5c8ee5
GM
6761 /* Mark all the scroll bars to be removed; we'll redeem
6762 the ones we want when we redisplay their windows. */
9769686d
RS
6763 if (condemn_scroll_bars_hook)
6764 (*condemn_scroll_bars_hook) (f);
30c566e4 6765
f21ef775 6766 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 6767 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 6768
5f5c8ee5
GM
6769 /* Any scroll bars which redisplay_windows should have
6770 nuked should now go away. */
9769686d
RS
6771 if (judge_scroll_bars_hook)
6772 (*judge_scroll_bars_hook) (f);
6773 }
30c566e4 6774 }
a2889657 6775 }
5f5c8ee5
GM
6776 else if (FRAME_VISIBLE_P (selected_frame)
6777 && !FRAME_OBSCURED_P (selected_frame))
6778 redisplay_window (selected_window, 1);
6779
6780
6781 /* Compare desired and current matrices, perform output. */
6782
6783update:
6784
6785 /* If fonts changed, display again. */
6786 if (fonts_changed_p)
6787 goto retry;
a2889657 6788
a2889657
JB
6789 /* Prevent various kinds of signals during display update.
6790 stdio is not robust about handling signals,
6791 which can cause an apparent I/O error. */
6792 if (interrupt_input)
6793 unrequest_sigio ();
6794 stop_polling ();
6795
5f5c8ee5 6796 if (consider_all_windows_p)
a2889657
JB
6797 {
6798 Lisp_Object tail;
6799
6800 pause = 0;
6801
44fa5b1e 6802 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 6803 {
5f5c8ee5 6804 struct frame *f;
a2889657 6805
e24c997d 6806 if (!FRAMEP (XCONS (tail)->car))
a2889657
JB
6807 continue;
6808
44fa5b1e 6809 f = XFRAME (XCONS (tail)->car);
1af9f229 6810
fd8ff63d 6811 if ((FRAME_WINDOW_P (f) || f == selected_frame)
f21ef775 6812 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 6813 {
5f5c8ee5
GM
6814 /* Mark all windows as to be updated. */
6815 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 6816 pause |= update_frame (f, 0, 0);
a2889657 6817 if (!pause)
efc63ef0 6818 {
5f5c8ee5
GM
6819 if (hscroll_windows (f->root_window))
6820 goto retry;
6821
efc63ef0
RS
6822 mark_window_display_accurate (f->root_window, 1);
6823 if (frame_up_to_date_hook != 0)
6824 (*frame_up_to_date_hook) (f);
6825 }
a2889657
JB
6826 }
6827 }
6828 }
6829 else
6e8290aa 6830 {
5f5c8ee5
GM
6831 if (FRAME_VISIBLE_P (selected_frame)
6832 && !FRAME_OBSCURED_P (selected_frame))
6833 {
6834 XWINDOW (selected_window)->must_be_updated_p = 1;
6835 pause = update_frame (selected_frame, 0, 0);
6836 if (!pause && hscroll_windows (selected_window))
6837 goto retry;
6838 }
4d641a15
KH
6839 else
6840 pause = 0;
d724d989 6841
8de2d90b 6842 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
6843 function. If the echo area is on another frame, that may
6844 have put text on a frame other than the selected one, so the
6845 above call to update_frame would not have caught it. Catch
8de2d90b
JB
6846 it here. */
6847 {
84faf44c 6848 Lisp_Object mini_window;
5f5c8ee5 6849 struct frame *mini_frame;
84faf44c
RS
6850
6851 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
6852 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 6853
fd8ff63d 6854 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
6855 {
6856 XWINDOW (mini_window)->must_be_updated_p = 1;
6857 pause |= update_frame (mini_frame, 0, 0);
6858 if (!pause && hscroll_windows (mini_window))
6859 goto retry;
6860 }
8de2d90b 6861 }
6e8290aa 6862 }
a2889657 6863
5f5c8ee5
GM
6864 /* If display was paused because of pending input, make sure we do a
6865 thorough update the next time. */
a2889657
JB
6866 if (pause)
6867 {
5f5c8ee5
GM
6868 /* Prevent the optimization at the beginning of
6869 redisplay_internal that tries a single-line update of the
6870 line containing the cursor in the selected window. */
6871 CHARPOS (this_line_start_pos) = 0;
6872
6873 /* Let the overlay arrow be updated the next time. */
265a9e55 6874 if (!NILP (last_arrow_position))
a2889657
JB
6875 {
6876 last_arrow_position = Qt;
6877 last_arrow_string = Qt;
6878 }
5f5c8ee5
GM
6879
6880 /* If we pause after scrolling, some rows in the current
6881 matrices of some windows are not valid. */
6882 if (!WINDOW_FULL_WIDTH_P (w)
6883 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
6884 update_mode_lines = 1;
6885 }
6886
5f5c8ee5
GM
6887 /* Now text on frame agrees with windows, so put info into the
6888 windows for partial redisplay to follow. */
a2889657
JB
6889 if (!pause)
6890 {
6891 register struct buffer *b = XBUFFER (w->buffer);
6892
a2889657 6893 unchanged_modified = BUF_MODIFF (b);
8850a573 6894 overlay_unchanged_modified = BUF_OVERLAY_MODIFF (b);
a2889657
JB
6895 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
6896 end_unchanged = BUF_Z (b) - BUF_GPT (b);
6897
5f5c8ee5 6898 if (consider_all_windows_p)
11e82b76 6899 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
a2889657
JB
6900 else
6901 {
5f5c8ee5
GM
6902 XSETFASTINT (w->last_point, BUF_PT (b));
6903 w->last_cursor = w->cursor;
6904 w->last_cursor_off_p = w->cursor_off_p;
6905
28995e67 6906 b->clip_changed = 0;
a2889657 6907 w->update_mode_line = Qnil;
c2213350 6908 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 6909 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
6910 w->last_had_star
6911 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6912 ? Qt : Qnil);
3ee4159a
RS
6913
6914 /* Record if we are showing a region, so can make sure to
6915 update it fully at next redisplay. */
6916 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
6917 && (w == XWINDOW (current_buffer->last_selected_window)
6918 || highlight_nonselected_windows)
3ee4159a
RS
6919 && !NILP (XBUFFER (w->buffer)->mark_active)
6920 ? Fmarker_position (XBUFFER (w->buffer)->mark)
6921 : Qnil);
6922
d2f84654 6923 w->window_end_valid = w->buffer;
d45de95b 6924 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 6925 last_arrow_string = Voverlay_arrow_string;
efc63ef0
RS
6926 if (frame_up_to_date_hook != 0)
6927 (*frame_up_to_date_hook) (selected_frame);
a2889657 6928 }
5f5c8ee5 6929
a2889657
JB
6930 update_mode_lines = 0;
6931 windows_or_buffers_changed = 0;
6932 }
6933
5f5c8ee5
GM
6934 /* Start SIGIO interrupts coming again. Having them off during the
6935 code above makes it less likely one will discard output, but not
6936 impossible, since there might be stuff in the system buffer here.
a2889657 6937 But it is much hairier to try to do anything about that. */
a2889657
JB
6938 if (interrupt_input)
6939 request_sigio ();
6940 start_polling ();
6941
5f5c8ee5
GM
6942 /* If a frame has become visible which was not before, redisplay
6943 again, so that we display it. Expose events for such a frame
6944 (which it gets when becoming visible) don't call the parts of
6945 redisplay constructing glyphs, so simply exposing a frame won't
6946 display anything in this case. So, we have to display these
6947 frames here explicitly. */
11c52c4f
RS
6948 if (!pause)
6949 {
6950 Lisp_Object tail, frame;
6951 int new_count = 0;
6952
6953 FOR_EACH_FRAME (tail, frame)
6954 {
6955 int this_is_visible = 0;
8e83f802
RS
6956
6957 if (XFRAME (frame)->visible)
6958 this_is_visible = 1;
6959 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
6960 if (XFRAME (frame)->visible)
6961 this_is_visible = 1;
11c52c4f
RS
6962
6963 if (this_is_visible)
6964 new_count++;
6965 }
6966
89819bdd 6967 if (new_count != number_of_visible_frames)
11c52c4f
RS
6968 windows_or_buffers_changed++;
6969 }
6970
44fa5b1e 6971 /* Change frame size now if a change is pending. */
a2889657 6972 do_pending_window_change ();
d8e242fd 6973
8b32d885
RS
6974 /* If we just did a pending size change, or have additional
6975 visible frames, redisplay again. */
3c8c72e0 6976 if (windows_or_buffers_changed && !pause)
8b32d885 6977 goto retry;
5f5c8ee5
GM
6978
6979 end_of_redisplay:;
6980
6981 if (--redisplaying_p < 0)
6982 redisplaying_p = 0;
a2889657
JB
6983}
6984
5f5c8ee5
GM
6985
6986/* Redisplay, but leave alone any recent echo area message unless
6987 another message has been requested in its place.
a2889657
JB
6988
6989 This is useful in situations where you need to redisplay but no
6990 user action has occurred, making it inappropriate for the message
6991 area to be cleared. See tracking_off and
6992 wait_reading_process_input for examples of these situations. */
6993
8991bb31 6994void
a2889657
JB
6995redisplay_preserve_echo_area ()
6996{
5f5c8ee5
GM
6997 if (!echo_area_glyphs
6998 && !STRINGP (echo_area_message)
6999 && (previous_echo_glyphs
7000 || STRINGP (previous_echo_area_message)))
a2889657
JB
7001 {
7002 echo_area_glyphs = previous_echo_glyphs;
5f5c8ee5
GM
7003 echo_area_message = previous_echo_area_message;
7004 echo_area_glyphs_length = previous_echo_glyphs_length;
e9874cee 7005 redisplay_internal (1);
5f5c8ee5
GM
7006 echo_area_glyphs = NULL;
7007 echo_area_message = Qnil;
a2889657
JB
7008 }
7009 else
e9874cee 7010 redisplay_internal (1);
a2889657
JB
7011}
7012
5f5c8ee5
GM
7013
7014/* Mark the display of windows in the window tree rooted at WINDOW as
7015 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7016 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7017 the next time redisplay_internal is called. */
7018
a2889657 7019void
5f5c8ee5 7020mark_window_display_accurate (window, accurate_p)
a2889657 7021 Lisp_Object window;
5f5c8ee5 7022 int accurate_p;
a2889657 7023{
5f5c8ee5
GM
7024 struct window *w;
7025
7026 for (; !NILP (window); window = w->next)
a2889657
JB
7027 {
7028 w = XWINDOW (window);
7029
5f5c8ee5 7030 if (BUFFERP (w->buffer))
bd66d1ba 7031 {
5f5c8ee5
GM
7032 struct buffer *b = XBUFFER (w->buffer);
7033
c2213350 7034 XSETFASTINT (w->last_modified,
5f5c8ee5 7035 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 7036 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
7037 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7038 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7039 ? Qt : Qnil);
bd66d1ba 7040
5f5c8ee5
GM
7041#if 0 /* I don't think this is necessary because display_line does it.
7042 Let's check it. */
bd66d1ba
RS
7043 /* Record if we are showing a region, so can make sure to
7044 update it fully at next redisplay. */
5f5c8ee5
GM
7045 w->region_showing
7046 = (!NILP (Vtransient_mark_mode)
7047 && (w == XWINDOW (current_buffer->last_selected_window)
7048 || highlight_nonselected_windows)
7049 && (!NILP (b->mark_active)
7050 ? Fmarker_position (b->mark)
7051 : Qnil));
7052#endif
7053
7054 if (accurate_p)
7055 {
7056 b->clip_changed = 0;
7057 w->last_cursor = w->cursor;
7058 w->last_cursor_off_p = w->cursor_off_p;
7059 if (w == XWINDOW (selected_window))
7060 w->last_point = BUF_PT (b);
7061 else
7062 w->last_point = XMARKER (w->pointm)->charpos;
7063 }
bd66d1ba
RS
7064 }
7065
d2f84654 7066 w->window_end_valid = w->buffer;
a2889657
JB
7067 w->update_mode_line = Qnil;
7068
265a9e55 7069 if (!NILP (w->vchild))
5f5c8ee5 7070 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 7071 if (!NILP (w->hchild))
5f5c8ee5 7072 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
7073 }
7074
5f5c8ee5 7075 if (accurate_p)
a2889657 7076 {
d45de95b 7077 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
7078 last_arrow_string = Voverlay_arrow_string;
7079 }
7080 else
7081 {
5f5c8ee5
GM
7082 /* Force a thorough redisplay the next time by setting
7083 last_arrow_position and last_arrow_string to t, which is
7084 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
7085 last_arrow_position = Qt;
7086 last_arrow_string = Qt;
7087 }
7088}
5f5c8ee5
GM
7089
7090
7091/* Return value in display table DP (Lisp_Char_Table *) for character
7092 C. Since a display table doesn't have any parent, we don't have to
7093 follow parent. Do not call this function directly but use the
7094 macro DISP_CHAR_VECTOR. */
7095
7096Lisp_Object
7097disp_char_vector (dp, c)
7098 struct Lisp_Char_Table *dp;
7099 int c;
7100{
7101 int code[4], i;
7102 Lisp_Object val;
7103
7104 if (SINGLE_BYTE_CHAR_P (c))
7105 return (dp->contents[c]);
7106
7107 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7108 if (code[0] != CHARSET_COMPOSITION)
7109 {
7110 if (code[1] < 32)
7111 code[1] = -1;
7112 else if (code[2] < 32)
7113 code[2] = -1;
7114 }
7115
7116 /* Here, the possible range of code[0] (== charset ID) is
7117 128..max_charset. Since the top level char table contains data
7118 for multibyte characters after 256th element, we must increment
7119 code[0] by 128 to get a correct index. */
7120 code[0] += 128;
7121 code[3] = -1; /* anchor */
7122
7123 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7124 {
7125 val = dp->contents[code[i]];
7126 if (!SUB_CHAR_TABLE_P (val))
7127 return (NILP (val) ? dp->defalt : val);
7128 }
7129
7130 /* Here, val is a sub char table. We return the default value of
7131 it. */
7132 return (dp->defalt);
7133}
7134
7135
a2889657 7136\f
5f5c8ee5
GM
7137/***********************************************************************
7138 Window Redisplay
7139 ***********************************************************************/
a2725ab2 7140
5f5c8ee5 7141/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
7142
7143static void
5f5c8ee5
GM
7144redisplay_windows (window)
7145 Lisp_Object window;
90adcf20 7146{
5f5c8ee5
GM
7147 while (!NILP (window))
7148 {
7149 struct window *w = XWINDOW (window);
7150
7151 if (!NILP (w->hchild))
7152 redisplay_windows (w->hchild);
7153 else if (!NILP (w->vchild))
7154 redisplay_windows (w->vchild);
7155 else
7156 redisplay_window (window, 0);
a2725ab2 7157
5f5c8ee5
GM
7158 window = w->next;
7159 }
7160}
7161
7162
7163/* Set cursor position of W. PT is assumed to be displayed in ROW.
7164 DELTA is the number of bytes by which positions recorded in ROW
7165 differ from current buffer positions. */
7166
7167void
7168set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7169 struct window *w;
7170 struct glyph_row *row;
7171 struct glyph_matrix *matrix;
7172 int delta, delta_bytes, dy, dvpos;
7173{
7174 struct glyph *glyph = row->glyphs[TEXT_AREA];
7175 struct glyph *end = glyph + row->used[TEXT_AREA];
7176 int x = row->x;
7177 int pt_old = PT - delta;
7178
7179 /* Skip over glyphs not having an object at the start of the row.
7180 These are special glyphs like truncation marks on terminal
7181 frames. */
7182 if (row->displays_text_p)
7183 while (glyph < end
7184 && !glyph->object
7185 && glyph->charpos < 0)
7186 {
7187 x += glyph->pixel_width;
7188 ++glyph;
7189 }
7190
7191 while (glyph < end
7192 && glyph->object
7193 && (!BUFFERP (glyph->object)
7194 || glyph->charpos < pt_old))
7195 {
7196 x += glyph->pixel_width;
7197 ++glyph;
7198 }
7199
7200 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7201 w->cursor.x = x;
7202 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7203 w->cursor.y = row->y + dy;
7204
7205 if (w == XWINDOW (selected_window))
7206 {
7207 if (!row->continued_p
7208 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7209 && row->x == 0)
7210 {
7211 this_line_buffer = XBUFFER (w->buffer);
7212
7213 CHARPOS (this_line_start_pos)
7214 = MATRIX_ROW_START_CHARPOS (row) + delta;
7215 BYTEPOS (this_line_start_pos)
7216 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7217
7218 CHARPOS (this_line_end_pos)
7219 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7220 BYTEPOS (this_line_end_pos)
7221 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7222
7223 this_line_y = w->cursor.y;
7224 this_line_pixel_height = row->height;
7225 this_line_vpos = w->cursor.vpos;
7226 this_line_start_x = row->x;
7227 }
7228 else
7229 CHARPOS (this_line_start_pos) = 0;
7230 }
7231}
7232
7233
7234/* Run window scroll functions, if any, for WINDOW with new window
7235 start STARTP. Sets the window start of WINDOW to that position. */
7236
7237static INLINE struct text_pos
7238run_window_scroll_functions (window, startp)
7239 Lisp_Object window;
7240 struct text_pos startp;
7241{
7242 struct window *w = XWINDOW (window);
7243 SET_MARKER_FROM_TEXT_POS (w->start, startp);
90adcf20 7244
5f5c8ee5
GM
7245 if (!NILP (Vwindow_scroll_functions))
7246 {
7247 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7248 make_number (CHARPOS (startp)));
7249 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7250 }
90adcf20 7251
5f5c8ee5
GM
7252 return startp;
7253}
7254
7255
7256/* Modify the desired matrix of window W and W->vscroll so that the
7257 line containing the cursor is fully visible. */
7258
7259static void
7260make_cursor_line_fully_visible (w)
7261 struct window *w;
7262{
7263 struct glyph_matrix *matrix;
7264 struct glyph_row *row;
7265 int top_line_height;
7266
7267 /* It's not always possible to find the cursor, e.g, when a window
7268 is full of overlay strings. Don't do anything in that case. */
7269 if (w->cursor.vpos < 0)
7270 return;
7271
7272 matrix = w->desired_matrix;
7273 row = MATRIX_ROW (matrix, w->cursor.vpos);
7274
7275 /* If row->y == top y of window display area, the window isn't tall
7276 enough to display a single line. There is nothing we can do
7277 about it. */
7278 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
7279 if (row->y == top_line_height)
7280 return;
7281
7282 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7283 {
7284 int dy = row->height - row->visible_height;
7285 w->vscroll = 0;
7286 w->cursor.y += dy;
7287 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7288 }
7289 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7290 {
7291 int dy = - (row->height - row->visible_height);
7292 w->vscroll = dy;
7293 w->cursor.y += dy;
7294 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7295 }
7296
7297 /* When we change the cursor y-position of the selected window,
7298 change this_line_y as well so that the display optimization for
7299 the cursor line of the selected window in redisplay_internal uses
7300 the correct y-position. */
7301 if (w == XWINDOW (selected_window))
7302 this_line_y = w->cursor.y;
7303}
7304
7305
7306/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7307 non-zero means only WINDOW is redisplayed in redisplay_internal.
7308 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7309 in redisplay_window to bring a partially visible line into view in
7310 the case that only the cursor has moved.
7311
7312 Value is
7313
7314 1 if scrolling succeeded
7315
7316 0 if scrolling didn't find point.
7317
7318 -1 if new fonts have been loaded so that we must interrupt
7319 redisplay, adjust glyph matrices, and try again. */
7320
7321static int
7322try_scrolling (window, just_this_one_p, scroll_conservatively,
7323 scroll_step, temp_scroll_step)
7324 Lisp_Object window;
7325 int just_this_one_p;
7326 int scroll_conservatively, scroll_step;
7327 int temp_scroll_step;
7328{
7329 struct window *w = XWINDOW (window);
7330 struct frame *f = XFRAME (w->frame);
7331 struct text_pos scroll_margin_pos;
7332 struct text_pos pos;
7333 struct text_pos startp;
7334 struct it it;
7335 Lisp_Object window_end;
7336 int this_scroll_margin;
7337 int dy = 0;
7338 int scroll_max;
7339 int line_height, rc;
7340 int amount_to_scroll = 0;
7341 Lisp_Object aggressive;
7342 int height;
7343
7344#if GLYPH_DEBUG
7345 debug_method_add (w, "try_scrolling");
78614721 7346#endif
5f5c8ee5
GM
7347
7348 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7349
7350 /* Compute scroll margin height in pixels. We scroll when point is
7351 within this distance from the top or bottom of the window. */
7352 if (scroll_margin > 0)
90adcf20 7353 {
5f5c8ee5
GM
7354 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
7355 this_scroll_margin *= CANON_Y_UNIT (f);
7356 }
7357 else
7358 this_scroll_margin = 0;
7359
7360 /* Compute how much we should try to scroll maximally to bring point
7361 into view. */
7362 if (scroll_step)
7363 scroll_max = scroll_step;
7364 else if (scroll_conservatively)
7365 scroll_max = scroll_conservatively;
7366 else if (temp_scroll_step)
7367 scroll_max = temp_scroll_step;
7368 else if (NUMBERP (current_buffer->scroll_down_aggressively)
7369 || NUMBERP (current_buffer->scroll_up_aggressively))
7370 /* We're trying to scroll because of aggressive scrolling
7371 but no scroll_step is set. Choose an arbitrary one. Maybe
7372 there should be a variable for this. */
7373 scroll_max = 10;
7374 else
7375 scroll_max = 0;
7376 scroll_max *= CANON_Y_UNIT (f);
7377
7378 /* Decide whether we have to scroll down. Start at the window end
7379 and move this_scroll_margin up to find the position of the scroll
7380 margin. */
7381 window_end = Fwindow_end (window, Qt);
7382 CHARPOS (scroll_margin_pos) = XINT (window_end);
7383 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
7384 if (this_scroll_margin)
7385 {
7386 start_display (&it, w, scroll_margin_pos);
7387 move_it_vertically (&it, - this_scroll_margin);
7388 scroll_margin_pos = it.current.pos;
7389 }
7390
7391 if (PT >= CHARPOS (scroll_margin_pos))
7392 {
7393 int y0;
7394
7395 /* Point is in the scroll margin at the bottom of the window, or
7396 below. Compute a new window start that makes point visible. */
7397
7398 /* Compute the distance from the scroll margin to PT.
7399 Give up if the distance is greater than scroll_max. */
7400 start_display (&it, w, scroll_margin_pos);
7401 y0 = it.current_y;
7402 move_it_to (&it, PT, 0, it.last_visible_y, -1,
7403 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7404 line_height = (it.max_ascent + it.max_descent
7405 ? it.max_ascent + it.max_descent
7406 : last_height);
7407 dy = it.current_y + line_height - y0;
7408 if (dy > scroll_max)
7409 return 0;
7410
7411 /* Move the window start down. If scrolling conservatively,
7412 move it just enough down to make point visible. If
7413 scroll_step is set, move it down by scroll_step. */
7414 start_display (&it, w, startp);
7415
7416 if (scroll_conservatively)
7417 amount_to_scroll = dy;
7418 else if (scroll_step || temp_scroll_step)
7419 amount_to_scroll = scroll_max;
7420 else
90adcf20 7421 {
5f5c8ee5
GM
7422 aggressive = current_buffer->scroll_down_aggressively;
7423 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
7424 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
7425 if (NUMBERP (aggressive))
7426 amount_to_scroll = XFLOATINT (aggressive) * height;
7427 }
a2725ab2 7428
5f5c8ee5
GM
7429 if (amount_to_scroll <= 0)
7430 return 0;
a2725ab2 7431
5f5c8ee5
GM
7432 move_it_vertically (&it, amount_to_scroll);
7433 startp = it.current.pos;
7434 }
7435 else
7436 {
7437 /* See if point is inside the scroll margin at the top of the
7438 window. */
7439 scroll_margin_pos = startp;
7440 if (this_scroll_margin)
7441 {
7442 start_display (&it, w, startp);
7443 move_it_vertically (&it, this_scroll_margin);
7444 scroll_margin_pos = it.current.pos;
7445 }
7446
7447 if (PT < CHARPOS (scroll_margin_pos))
7448 {
7449 /* Point is in the scroll margin at the top of the window or
7450 above what is displayed in the window. */
7451 int y0;
7452
7453 /* Compute the vertical distance from PT to the scroll
7454 margin position. Give up if distance is greater than
7455 scroll_max. */
7456 SET_TEXT_POS (pos, PT, PT_BYTE);
7457 start_display (&it, w, pos);
7458 y0 = it.current_y;
7459 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
7460 it.last_visible_y, -1,
7461 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7462 dy = it.current_y - y0;
7463 if (dy > scroll_max)
7464 return 0;
7465
7466 /* Compute new window start. */
7467 start_display (&it, w, startp);
7468
7469 if (scroll_conservatively)
7470 amount_to_scroll = dy;
7471 else if (scroll_step || temp_scroll_step)
7472 amount_to_scroll = scroll_max;
538f13d4 7473 else
5f5c8ee5
GM
7474 {
7475 aggressive = current_buffer->scroll_up_aggressively;
7476 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
7477 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
7478 if (NUMBERP (aggressive))
7479 amount_to_scroll = XFLOATINT (aggressive) * height;
7480 }
a2725ab2 7481
5f5c8ee5
GM
7482 if (amount_to_scroll <= 0)
7483 return 0;
7484
7485 move_it_vertically (&it, - amount_to_scroll);
7486 startp = it.current.pos;
90adcf20
RS
7487 }
7488 }
a2889657 7489
5f5c8ee5
GM
7490 /* Run window scroll functions. */
7491 startp = run_window_scroll_functions (window, startp);
90adcf20 7492
5f5c8ee5
GM
7493 /* Display the window. Give up if new fonts are loaded, or if point
7494 doesn't appear. */
7495 if (!try_window (window, startp))
7496 rc = -1;
7497 else if (w->cursor.vpos < 0)
7498 {
7499 clear_glyph_matrix (w->desired_matrix);
7500 rc = 0;
7501 }
7502 else
7503 {
7504 /* Maybe forget recorded base line for line number display. */
7505 if (!just_this_one_p
7506 || current_buffer->clip_changed
7507 || beg_unchanged < CHARPOS (startp))
7508 w->base_line_number = Qnil;
7509
7510 /* If cursor ends up on a partially visible line, shift display
7511 lines up or down. */
7512 make_cursor_line_fully_visible (w);
7513 rc = 1;
7514 }
7515
7516 return rc;
a2889657
JB
7517}
7518
5f5c8ee5
GM
7519
7520/* Compute a suitable window start for window W if display of W starts
7521 on a continuation line. Value is non-zero if a new window start
7522 was computed.
7523
7524 The new window start will be computed, based on W's width, starting
7525 from the start of the continued line. It is the start of the
7526 screen line with the minimum distance from the old start W->start. */
7527
7528static int
7529compute_window_start_on_continuation_line (w)
7530 struct window *w;
1f1ff51d 7531{
5f5c8ee5
GM
7532 struct text_pos pos, start_pos;
7533 int window_start_changed_p = 0;
1f1ff51d 7534
5f5c8ee5 7535 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 7536
5f5c8ee5
GM
7537 /* If window start is on a continuation line... Window start may be
7538 < BEGV in case there's invisible text at the start of the
7539 buffer (M-x rmail, for example). */
7540 if (CHARPOS (start_pos) > BEGV
7541 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 7542 {
5f5c8ee5
GM
7543 struct it it;
7544 struct glyph_row *row;
7545
7546 /* Find the start of the continued line. This should be fast
7547 because scan_buffer is fast (newline cache). */
7548 row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
7549 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
7550 row, DEFAULT_FACE_ID);
7551 reseat_at_previous_visible_line_start (&it);
7552
7553 /* If the line start is "too far" away from the window start,
7554 say it takes too much time to compute a new window start. */
7555 if (CHARPOS (start_pos) - IT_CHARPOS (it)
7556 < XFASTINT (w->height) * XFASTINT (w->width))
7557 {
7558 int min_distance, distance;
7559
7560 /* Move forward by display lines to find the new window
7561 start. If window width was enlarged, the new start can
7562 be expected to be > the old start. If window width was
7563 decreased, the new window start will be < the old start.
7564 So, we're looking for the display line start with the
7565 minimum distance from the old window start. */
7566 pos = it.current.pos;
7567 min_distance = INFINITY;
7568 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
7569 distance < min_distance)
7570 {
7571 min_distance = distance;
7572 pos = it.current.pos;
7573 move_it_by_lines (&it, 1, 0);
7574 }
7575
7576 /* Set the window start there. */
7577 SET_MARKER_FROM_TEXT_POS (w->start, pos);
7578 window_start_changed_p = 1;
7579 }
1f1ff51d 7580 }
5f5c8ee5
GM
7581
7582 return window_start_changed_p;
1f1ff51d
KH
7583}
7584
5f5c8ee5
GM
7585
7586/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
7587 selected_window is redisplayed. */
90adcf20 7588
a2889657 7589static void
5f5c8ee5 7590redisplay_window (window, just_this_one_p)
a2889657 7591 Lisp_Object window;
5f5c8ee5 7592 int just_this_one_p;
a2889657 7593{
5f5c8ee5
GM
7594 struct window *w = XWINDOW (window);
7595 struct frame *f = XFRAME (w->frame);
7596 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 7597 struct buffer *old = current_buffer;
5f5c8ee5 7598 struct text_pos lpoint, opoint, startp;
e481f960 7599 int update_mode_line;
5f5c8ee5
GM
7600 int tem;
7601 struct it it;
7602 /* Record it now because it's overwritten. */
7603 int current_matrix_up_to_date_p = 0;
5ba50c51 7604 int really_switched_buffer = 0;
5f5c8ee5 7605 int temp_scroll_step = 0;
2e54982e 7606 int count = specpdl_ptr - specpdl;
a2889657 7607
5f5c8ee5
GM
7608 SET_TEXT_POS (lpoint, PT, PT_BYTE);
7609 opoint = lpoint;
a2889657 7610
5f5c8ee5
GM
7611 /* W must be a leaf window here. */
7612 xassert (!NILP (w->buffer));
7613#if GLYPH_DEBUG
7614 *w->desired_matrix->method = 0;
7615#endif
2e54982e
RS
7616
7617 specbind (Qinhibit_point_motion_hooks, Qt);
5f5c8ee5
GM
7618
7619 /* Has the mode line to be updated? */
7620 update_mode_line = (!NILP (w->update_mode_line)
7621 || update_mode_lines
7622 || buffer->clip_changed);
8de2d90b
JB
7623
7624 if (MINI_WINDOW_P (w))
7625 {
5f5c8ee5
GM
7626 if (w == XWINDOW (echo_area_window)
7627 && (echo_area_glyphs
7628 || STRINGP (echo_area_message)))
7629 {
7630 if (update_mode_line)
7631 /* We may have to update a tty frame's menu bar or a
7632 toolbar. Example `M-x C-h C-h C-g'. */
7633 goto finish_menu_bars;
7634 else
7635 /* We've already displayed the echo area glyphs in this window. */
7636 goto finish_scroll_bars;
7637 }
73af359d 7638 else if (w != XWINDOW (minibuf_window))
8de2d90b 7639 {
5f5c8ee5
GM
7640 /* W is a mini-buffer window, but it's not the currently
7641 active one, so clear it. */
7642 int yb = window_text_bottom_y (w);
7643 struct glyph_row *row;
7644 int y;
7645
7646 for (y = 0, row = w->desired_matrix->rows;
7647 y < yb;
7648 y += row->height, ++row)
7649 blank_row (w, row, y);
88f22aff 7650 goto finish_scroll_bars;
8de2d90b
JB
7651 }
7652 }
a2889657 7653
5f5c8ee5
GM
7654 /* Otherwise set up data on this window; select its buffer and point
7655 value. */
e481f960 7656 if (update_mode_line)
5ba50c51 7657 {
5f5c8ee5
GM
7658 /* Really select the buffer, for the sake of buffer-local
7659 variables. */
5ba50c51
RS
7660 set_buffer_internal_1 (XBUFFER (w->buffer));
7661 really_switched_buffer = 1;
7662 }
e481f960
RS
7663 else
7664 set_buffer_temp (XBUFFER (w->buffer));
5f5c8ee5
GM
7665 SET_TEXT_POS (opoint, PT, PT_BYTE);
7666
7667 current_matrix_up_to_date_p
7668 = (!NILP (w->window_end_valid)
7669 && !current_buffer->clip_changed
7670 && XFASTINT (w->last_modified) >= MODIFF
7671 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 7672
5f5c8ee5
GM
7673 /* When windows_or_buffers_changed is non-zero, we can't rely on
7674 the window end being valid, so set it to nil there. */
7675 if (windows_or_buffers_changed)
7676 {
7677 /* If window starts on a continuation line, maybe adjust the
7678 window start in case the window's width changed. */
7679 if (XMARKER (w->start)->buffer == current_buffer)
7680 compute_window_start_on_continuation_line (w);
7681
7682 w->window_end_valid = Qnil;
7683 }
12adba34 7684
5f5c8ee5
GM
7685 /* Some sanity checks. */
7686 CHECK_WINDOW_END (w);
7687 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 7688 abort ();
5f5c8ee5 7689 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 7690 abort ();
a2889657 7691
28995e67
RS
7692 /* If %c is in mode line, update it if needed. */
7693 if (!NILP (w->column_number_displayed)
7694 /* This alternative quickly identifies a common case
7695 where no change is needed. */
7696 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7697 && XFASTINT (w->last_modified) >= MODIFF
7698 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7699 && XFASTINT (w->column_number_displayed) != current_column ())
7700 update_mode_line = 1;
7701
5f5c8ee5
GM
7702 /* Count number of windows showing the selected buffer. An indirect
7703 buffer counts as its base buffer. */
7704 if (!just_this_one_p)
42640f83
RS
7705 {
7706 struct buffer *current_base, *window_base;
7707 current_base = current_buffer;
7708 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
7709 if (current_base->base_buffer)
7710 current_base = current_base->base_buffer;
7711 if (window_base->base_buffer)
7712 window_base = window_base->base_buffer;
7713 if (current_base == window_base)
7714 buffer_shared++;
7715 }
a2889657 7716
5f5c8ee5
GM
7717 /* Point refers normally to the selected window. For any other
7718 window, set up appropriate value. */
a2889657
JB
7719 if (!EQ (window, selected_window))
7720 {
12adba34
RS
7721 int new_pt = XMARKER (w->pointm)->charpos;
7722 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 7723 if (new_pt < BEGV)
a2889657 7724 {
f67a0f51 7725 new_pt = BEGV;
12adba34
RS
7726 new_pt_byte = BEGV_BYTE;
7727 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 7728 }
f67a0f51 7729 else if (new_pt > (ZV - 1))
a2889657 7730 {
f67a0f51 7731 new_pt = ZV;
12adba34
RS
7732 new_pt_byte = ZV_BYTE;
7733 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 7734 }
5f5c8ee5 7735
f67a0f51 7736 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 7737 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
7738 }
7739
f4faa47c 7740 /* If any of the character widths specified in the display table
5f5c8ee5
GM
7741 have changed, invalidate the width run cache. It's true that
7742 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
7743 redisplay goes (non-fatally) haywire when the display table is
7744 changed, so why should we worry about doing any better? */
7745 if (current_buffer->width_run_cache)
7746 {
f908610f 7747 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
7748
7749 if (! disptab_matches_widthtab (disptab,
7750 XVECTOR (current_buffer->width_table)))
7751 {
7752 invalidate_region_cache (current_buffer,
7753 current_buffer->width_run_cache,
7754 BEG, Z);
7755 recompute_width_table (current_buffer, disptab);
7756 }
7757 }
7758
a2889657 7759 /* If window-start is screwed up, choose a new one. */
a2889657
JB
7760 if (XMARKER (w->start)->buffer != current_buffer)
7761 goto recenter;
7762
5f5c8ee5 7763 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 7764
cf0df6ab
RS
7765 /* If someone specified a new starting point but did not insist,
7766 check whether it can be used. */
5f5c8ee5 7767 if (!NILP (w->optional_new_start))
cf0df6ab
RS
7768 {
7769 w->optional_new_start = Qnil;
5f5c8ee5
GM
7770 /* This takes a mini-buffer prompt into account. */
7771 start_display (&it, w, startp);
7772 move_it_to (&it, PT, 0, it.last_visible_y, -1,
7773 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7774 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
7775 w->force_start = Qt;
7776 }
7777
8de2d90b 7778 /* Handle case where place to start displaying has been specified,
aa6d10fa 7779 unless the specified location is outside the accessible range. */
265a9e55 7780 if (!NILP (w->force_start))
a2889657 7781 {
e63574d7 7782 w->force_start = Qnil;
5f5c8ee5 7783 w->vscroll = 0;
b5174a51 7784 w->window_end_valid = Qnil;
5f5c8ee5
GM
7785
7786 /* Forget any recorded base line for line number display. */
7787 if (!current_matrix_up_to_date_p
7788 || current_buffer->clip_changed)
7789 w->base_line_number = Qnil;
7790
75c43375
RS
7791 /* Redisplay the mode line. Select the buffer properly for that.
7792 Also, run the hook window-scroll-functions
7793 because we have scrolled. */
e63574d7
RS
7794 /* Note, we do this after clearing force_start because
7795 if there's an error, it is better to forget about force_start
7796 than to get into an infinite loop calling the hook functions
7797 and having them get more errors. */
75c43375
RS
7798 if (!update_mode_line
7799 || ! NILP (Vwindow_scroll_functions))
e481f960 7800 {
5ba50c51
RS
7801 if (!really_switched_buffer)
7802 {
7803 set_buffer_temp (old);
7804 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 7805 really_switched_buffer = 1;
5ba50c51 7806 }
5f5c8ee5 7807
e481f960
RS
7808 update_mode_line = 1;
7809 w->update_mode_line = Qt;
5f5c8ee5 7810 startp = run_window_scroll_functions (window, startp);
e481f960 7811 }
5f5c8ee5 7812
c2213350 7813 XSETFASTINT (w->last_modified, 0);
8850a573 7814 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
7815 if (CHARPOS (startp) < BEGV)
7816 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
7817 else if (CHARPOS (startp) > ZV)
7818 SET_TEXT_POS (startp, ZV, ZV_BYTE);
7819
7820 /* Redisplay, then check if cursor has been set during the
7821 redisplay. Give up if new fonts were loaded. */
7822 if (!try_window (window, startp))
7823 {
7824 w->force_start = Qt;
7825 clear_glyph_matrix (w->desired_matrix);
7826 goto restore_buffers;
7827 }
7828
7829 if (w->cursor.vpos < 0)
7830 {
7831 /* If point does not appear, or on a line that is not fully
7832 visible, move point so it does appear. The desired
7833 matrix has been built above, so we can use it. */
7834 int height = window_box_height (w) / 2;
7835 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
7836
7837 while (row->y < height)
7838 ++row;
7839
7840 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
7841 MATRIX_ROW_START_BYTEPOS (row));
7842
90adcf20 7843 if (w != XWINDOW (selected_window))
12adba34 7844 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
7845 else if (current_buffer == old)
7846 SET_TEXT_POS (lpoint, PT, PT_BYTE);
7847
7848 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
7849
7850 /* If we are highlighting the region, then we just changed
7851 the region, so redisplay to show it. */
df0b5ea1
RS
7852 if (!NILP (Vtransient_mark_mode)
7853 && !NILP (current_buffer->mark_active))
6f27fa9b 7854 {
5f5c8ee5
GM
7855 clear_glyph_matrix (w->desired_matrix);
7856 if (!try_window (window, startp))
7857 goto restore_buffers;
6f27fa9b 7858 }
a2889657 7859 }
5f5c8ee5
GM
7860
7861 make_cursor_line_fully_visible (w);
7862#if GLYPH_DEBUG
7863 debug_method_add (w, "forced window start");
7864#endif
a2889657
JB
7865 goto done;
7866 }
7867
5f5c8ee5
GM
7868 /* Handle case where text has not changed, only point, and it has
7869 not moved off the frame. */
7870 if (current_matrix_up_to_date_p
7871 /* Point may be in this window. */
7872 && PT >= CHARPOS (startp)
7873 /* If we don't check this, we are called to move the cursor in a
7874 horizontally split window with a current matrix that doesn't
7875 fit the display. */
7876 && !windows_or_buffers_changed
7877 /* Selective display hasn't changed. */
7878 && !current_buffer->clip_changed
b1aa6cb3
RS
7879 /* If force-mode-line-update was called, really redisplay;
7880 that's how redisplay is forced after e.g. changing
7881 buffer-invisibility-spec. */
632ab665 7882 && NILP (w->update_mode_line)
5f5c8ee5
GM
7883 /* Can't use this case if highlighting a region. When a
7884 region exists, cursor movement has to do more than just
7885 set the cursor. */
7886 && !(!NILP (Vtransient_mark_mode)
7887 && !NILP (current_buffer->mark_active))
bd66d1ba 7888 && NILP (w->region_showing)
5f5c8ee5
GM
7889 /* Right after splitting windows, last_point may be nil. */
7890 && INTEGERP (w->last_point)
7891 /* This code is not used for mini-buffer for the sake of the case
7892 of redisplaying to replace an echo area message; since in
7893 that case the mini-buffer contents per se are usually
7894 unchanged. This code is of no real use in the mini-buffer
7895 since the handling of this_line_start_pos, etc., in redisplay
7896 handles the same cases. */
d45de95b 7897 && !EQ (window, minibuf_window)
5f5c8ee5
GM
7898 /* When splitting windows or for new windows, it happens that
7899 redisplay is called with a nil window_end_vpos or one being
7900 larger than the window. This should really be fixed in
7901 window.c. I don't have this on my list, now, so we do
7902 approximately the same as the old redisplay code. --gerd. */
7903 && INTEGERP (w->window_end_vpos)
7904 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
7905 && (FRAME_WINDOW_P (f)
7906 || !MARKERP (Voverlay_arrow_position)
377dbd97 7907 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 7908 {
5f5c8ee5
GM
7909 int this_scroll_margin;
7910 struct glyph_row *row;
7911 int scroll_p;
a2889657 7912
5f5c8ee5
GM
7913#if GLYPH_DEBUG
7914 debug_method_add (w, "cursor movement");
7915#endif
9afd2168 7916
5f5c8ee5
GM
7917 /* Scroll if point within this distance from the top or bottom
7918 of the window. This is a pixel value. */
7919 this_scroll_margin = max (0, scroll_margin);
7920 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
7921 this_scroll_margin *= CANON_Y_UNIT (f);
7922
7923 /* Start with the row the cursor was displayed during the last
7924 not paused redisplay. Give up if that row is not valid. */
7925 if (w->last_cursor.vpos >= w->current_matrix->nrows)
7926 goto try_to_scroll;
7927 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
7928 if (row->mode_line_p)
7929 ++row;
7930 if (!row->enabled_p)
7931 goto try_to_scroll;
7932
7933 scroll_p = 0;
7934 if (PT > XFASTINT (w->last_point))
7935 {
7936 /* Point has moved forward. */
7937 int last_y = window_text_bottom_y (w) - this_scroll_margin;
7938
7939 while ((MATRIX_ROW_END_CHARPOS (row) < PT
7940 /* The end position of a row equals the start
7941 position of the next row. If PT is there, we
7942 would rather display it in the next line, except
7943 when this line ends in ZV. */
7944 || (MATRIX_ROW_END_CHARPOS (row) == PT
7945 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
7946 || !row->ends_at_zv_p)))
7947 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
7948 {
7949 xassert (row->enabled_p);
7950 ++row;
7951 }
9afd2168 7952
5f5c8ee5
GM
7953 /* If within the scroll margin, scroll. Note that
7954 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
7955 next line would be drawn, and that this_scroll_margin can
7956 be zero. */
7957 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
7958 || PT > MATRIX_ROW_END_CHARPOS (row)
7959 /* Line is completely visible last line in window and PT
7960 is to be set in the next line. */
7961 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
7962 && PT == MATRIX_ROW_END_CHARPOS (row)
7963 && !row->ends_at_zv_p
7964 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
7965 scroll_p = 1;
7966 }
7967 else if (PT < XFASTINT (w->last_point))
a2889657 7968 {
5f5c8ee5
GM
7969 /* Cursor has to be moved backward. Note that PT >=
7970 CHARPOS (startp) because of the outer if-statement. */
7971 while (!row->mode_line_p
7972 && (MATRIX_ROW_START_CHARPOS (row) > PT
7973 || (MATRIX_ROW_START_CHARPOS (row) == PT
7974 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
7975 && (row->y > this_scroll_margin
7976 || CHARPOS (startp) == BEGV))
a2889657 7977 {
5f5c8ee5
GM
7978 xassert (row->enabled_p);
7979 --row;
a2889657 7980 }
abb4c08f 7981
5f5c8ee5
GM
7982 /* Consider the following case: Window starts at BEGV, there
7983 is invisible, intangible text at BEGV, so that display
7984 starts at some point START > BEGV. It can happen that
7985 we are called with PT somewhere between BEGV and START.
7986 Try to handle that case. */
7987 if (row < w->current_matrix->rows
7988 || row->mode_line_p)
7989 {
7990 row = w->current_matrix->rows;
7991 if (row->mode_line_p)
7992 ++row;
7993 }
7994
7995 /* Due to newlines in overlay strings, we may have to skip
7996 forward over overlay strings. */
7997 while (MATRIX_ROW_END_CHARPOS (row) == PT
7998 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
7999 && !row->ends_at_zv_p)
8000 ++row;
8001
8002 /* If within the scroll margin, scroll. */
8003 if (row->y < this_scroll_margin
8004 && CHARPOS (startp) != BEGV)
8005 scroll_p = 1;
8006 }
8007
8008 /* if PT is not in the glyph row, give up. */
8009 if (PT < MATRIX_ROW_START_CHARPOS (row)
8010 || PT > MATRIX_ROW_END_CHARPOS (row))
8011 goto try_to_scroll;
8012
8013 /* If we end up in a partially visible line, let's make it fully
8014 visible. This can be done most easily by using the existing
8015 scrolling code. */
8016 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8017 {
8018 temp_scroll_step = 1;
8019 goto try_to_scroll;
a2889657 8020 }
5f5c8ee5
GM
8021 else if (scroll_p)
8022 goto try_to_scroll;
8023
8024 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8025 goto done;
a2889657 8026 }
5f5c8ee5 8027
a2889657
JB
8028 /* If current starting point was originally the beginning of a line
8029 but no longer is, find a new starting point. */
265a9e55 8030 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
8031 && !(CHARPOS (startp) <= BEGV
8032 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 8033 {
5f5c8ee5
GM
8034#if GLYPH_DEBUG
8035 debug_method_add (w, "recenter 1");
8036#endif
a2889657
JB
8037 goto recenter;
8038 }
5f5c8ee5
GM
8039
8040 /* Try scrolling with try_window_id. */
8041 else if (!windows_or_buffers_changed
8042 /* Window must be either use window-based redisplay or
8043 be full width. */
8044 && (FRAME_WINDOW_P (f)
8045 || ((line_ins_del_ok && WINDOW_FULL_WIDTH_P (w))
8046 && just_this_one_p))
8047 && !MINI_WINDOW_P (w)
8048 /* Point is not known NOT to appear in window. */
8049 && PT >= CHARPOS (startp)
a2889657 8050 && XFASTINT (w->last_modified)
5f5c8ee5
GM
8051 /* Window is not hscrolled. */
8052 && XFASTINT (w->hscroll) == 0
8053 /* Selective display has not changed. */
8054 && !current_buffer->clip_changed
8055 /* Current matrix is up to date. */
8056 && !NILP (w->window_end_valid)
8057 /* Can't use this case if highlighting a region because
8058 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
8059 && !(!NILP (Vtransient_mark_mode)
8060 && !NILP (current_buffer->mark_active))
8061 && NILP (w->region_showing)
5f5c8ee5 8062 /* Overlay arrow position and string not changed. */
d45de95b 8063 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 8064 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
8065 /* Value is > 0 if update has been done, it is -1 if we
8066 know that the same window start will not work. It is 0
8067 if unsuccessful for some other reason. */
8068 && (tem = try_window_id (w)) != 0)
a2889657 8069 {
5f5c8ee5
GM
8070#if GLYPH_DEBUG
8071 debug_method_add (w, "try_window_id");
8072#endif
8073
8074 if (fonts_changed_p)
8075 goto restore_buffers;
a2889657
JB
8076 if (tem > 0)
8077 goto done;
5f5c8ee5
GM
8078 /* Otherwise try_window_id has returned -1 which means that we
8079 don't want the alternative below this comment to execute. */
a2889657 8080 }
5f5c8ee5
GM
8081 else if (CHARPOS (startp) >= BEGV
8082 && CHARPOS (startp) <= ZV
8083 && PT >= CHARPOS (startp)
8084 && (CHARPOS (startp) < ZV
e9874cee 8085 /* Avoid starting at end of buffer. */
5f5c8ee5 8086 || CHARPOS (startp) == BEGV
8850a573
RS
8087 || (XFASTINT (w->last_modified) >= MODIFF
8088 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 8089 {
5f5c8ee5
GM
8090#if GLYPH_DEBUG
8091 debug_method_add (w, "same window start");
8092#endif
8093
8094 /* Try to redisplay starting at same place as before.
8095 If point has not moved off frame, accept the results. */
8096 if (!current_matrix_up_to_date_p
8097 /* Don't use try_window_reusing_current_matrix in this case
8098 because it can have changed the buffer. */
8099 || !NILP (Vwindow_scroll_functions)
8100 || MINI_WINDOW_P (w)
8101 || !try_window_reusing_current_matrix (w))
8102 {
8103 IF_DEBUG (debug_method_add (w, "1"));
8104 try_window (window, startp);
8105 }
8106
8107 if (fonts_changed_p)
8108 goto restore_buffers;
8109
8110 if (w->cursor.vpos >= 0)
aa6d10fa 8111 {
5f5c8ee5
GM
8112 if (!just_this_one_p
8113 || current_buffer->clip_changed
8114 || beg_unchanged < CHARPOS (startp))
aa6d10fa
RS
8115 /* Forget any recorded base line for line number display. */
8116 w->base_line_number = Qnil;
5f5c8ee5
GM
8117
8118 make_cursor_line_fully_visible (w);
aa6d10fa
RS
8119 goto done;
8120 }
a2889657 8121 else
5f5c8ee5 8122 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
8123 }
8124
5f5c8ee5
GM
8125 try_to_scroll:
8126
c2213350 8127 XSETFASTINT (w->last_modified, 0);
8850a573 8128 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 8129
e481f960
RS
8130 /* Redisplay the mode line. Select the buffer properly for that. */
8131 if (!update_mode_line)
8132 {
5ba50c51
RS
8133 if (!really_switched_buffer)
8134 {
8135 set_buffer_temp (old);
8136 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8137 really_switched_buffer = 1;
5ba50c51 8138 }
e481f960
RS
8139 update_mode_line = 1;
8140 w->update_mode_line = Qt;
8141 }
a2889657 8142
5f5c8ee5
GM
8143 /* Try to scroll by specified few lines. */
8144 if ((scroll_conservatively
8145 || scroll_step
8146 || temp_scroll_step
8147 || NUMBERP (current_buffer->scroll_up_aggressively)
8148 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 8149 && !current_buffer->clip_changed
5f5c8ee5
GM
8150 && CHARPOS (startp) >= BEGV
8151 && CHARPOS (startp) <= ZV)
0789adb2 8152 {
5f5c8ee5
GM
8153 /* The function returns -1 if new fonts were loaded, 1 if
8154 successful, 0 if not successful. */
8155 int rc = try_scrolling (window, just_this_one_p,
8156 scroll_conservatively,
8157 scroll_step,
8158 temp_scroll_step);
8159 if (rc > 0)
8160 goto done;
8161 else if (rc < 0)
8162 goto restore_buffers;
8163 }
f9c8af06 8164
5f5c8ee5 8165 /* Finally, just choose place to start which centers point */
5936754e 8166
5f5c8ee5 8167 recenter:
44173109 8168
5f5c8ee5
GM
8169#if GLYPH_DEBUG
8170 debug_method_add (w, "recenter");
8171#endif
0789adb2 8172
5f5c8ee5 8173 /* w->vscroll = 0; */
0789adb2 8174
5f5c8ee5
GM
8175 /* Forget any previously recorded base line for line number display. */
8176 if (!current_matrix_up_to_date_p
8177 || current_buffer->clip_changed)
8178 w->base_line_number = Qnil;
8179
8180 /* Move backward half the height of the window. */
8181 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8182 it.current_y = it.last_visible_y;
8183 move_it_vertically_backward (&it, it.last_visible_y / 2);
8184 xassert (IT_CHARPOS (it) >= BEGV);
8185
8186 /* The function move_it_vertically_backward may move over more
8187 than the specified y-distance. If it->w is small, e.g. a
8188 mini-buffer window, we may end up in front of the window's
8189 display area. Start displaying at the start of the line
8190 containing PT in this case. */
8191 if (it.current_y <= 0)
8192 {
8193 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8194 move_it_vertically (&it, 0);
8195 xassert (IT_CHARPOS (it) <= PT);
8196 it.current_y = 0;
0789adb2
RS
8197 }
8198
5f5c8ee5
GM
8199 it.current_x = it.hpos = 0;
8200
8201 /* Set startp here explicitly in case that helps avoid an infinite loop
8202 in case the window-scroll-functions functions get errors. */
8203 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8204
8205 /* Run scroll hooks. */
8206 startp = run_window_scroll_functions (window, it.current.pos);
8207
8208 /* Redisplay the window. */
8209 if (!current_matrix_up_to_date_p
8210 || windows_or_buffers_changed
8211 /* Don't use try_window_reusing_current_matrix in this case
8212 because it can have changed the buffer. */
8213 || !NILP (Vwindow_scroll_functions)
8214 || !just_this_one_p
8215 || MINI_WINDOW_P (w)
8216 || !try_window_reusing_current_matrix (w))
8217 try_window (window, startp);
8218
8219 /* If new fonts have been loaded (due to fontsets), give up. We
8220 have to start a new redisplay since we need to re-adjust glyph
8221 matrices. */
8222 if (fonts_changed_p)
8223 goto restore_buffers;
8224
8225 /* If cursor did not appear assume that the middle of the window is
8226 in the first line of the window. Do it again with the next line.
8227 (Imagine a window of height 100, displaying two lines of height
8228 60. Moving back 50 from it->last_visible_y will end in the first
8229 line.) */
8230 if (w->cursor.vpos < 0)
a2889657 8231 {
5f5c8ee5
GM
8232 if (!NILP (w->window_end_valid)
8233 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 8234 {
5f5c8ee5
GM
8235 clear_glyph_matrix (w->desired_matrix);
8236 move_it_by_lines (&it, 1, 0);
8237 try_window (window, it.current.pos);
a2889657 8238 }
5f5c8ee5 8239 else if (PT < IT_CHARPOS (it))
a2889657 8240 {
5f5c8ee5
GM
8241 clear_glyph_matrix (w->desired_matrix);
8242 move_it_by_lines (&it, -1, 0);
8243 try_window (window, it.current.pos);
8244 }
8245 else
8246 {
8247 /* Not much we can do about it. */
a2889657 8248 }
a2889657 8249 }
010494d0 8250
5f5c8ee5
GM
8251 /* Consider the following case: Window starts at BEGV, there is
8252 invisible, intangible text at BEGV, so that display starts at
8253 some point START > BEGV. It can happen that we are called with
8254 PT somewhere between BEGV and START. Try to handle that case. */
8255 if (w->cursor.vpos < 0)
835766b6 8256 {
5f5c8ee5
GM
8257 struct glyph_row *row = w->current_matrix->rows;
8258 if (row->mode_line_p)
8259 ++row;
8260 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 8261 }
5f5c8ee5
GM
8262
8263 make_cursor_line_fully_visible (w);
b5174a51 8264
5f5c8ee5
GM
8265 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8266 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8267 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8268 ? Qt : Qnil);
a2889657 8269
5f5c8ee5 8270 done:
a2889657 8271
5f5c8ee5 8272 /* Display the mode line, if we must. */
e481f960 8273 if ((update_mode_line
aa6d10fa 8274 /* If window not full width, must redo its mode line
5f5c8ee5
GM
8275 if (a) the window to its side is being redone and
8276 (b) we do a frame-based redisplay. This is a consequence
8277 of how inverted lines are drawn in frame-based redisplay. */
8278 || (!just_this_one_p
8279 && !FRAME_WINDOW_P (f)
8280 && !WINDOW_FULL_WIDTH_P (w))
8281 /* Line number to display. */
155ef550 8282 || INTEGERP (w->base_line_pos)
5f5c8ee5 8283 /* Column number is displayed and different from the one displayed. */
155ef550
KH
8284 || (!NILP (w->column_number_displayed)
8285 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
8286 /* This means that the window has a mode line. */
8287 && (WINDOW_WANTS_MODELINE_P (w)
8288 || WINDOW_WANTS_TOP_LINE_P (w)))
5ba50c51 8289 {
5f5c8ee5
GM
8290 display_mode_lines (w);
8291
8292 /* If mode line height has changed, arrange for a thorough
8293 immediate redisplay using the correct mode line height. */
8294 if (WINDOW_WANTS_MODELINE_P (w)
8295 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 8296 {
5f5c8ee5
GM
8297 fonts_changed_p = 1;
8298 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8299 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 8300 }
5f5c8ee5
GM
8301
8302 /* If top line height has changed, arrange for a thorough
8303 immediate redisplay using the correct mode line height. */
8304 if (WINDOW_WANTS_TOP_LINE_P (w)
8305 && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
8306 {
8307 fonts_changed_p = 1;
8308 MATRIX_TOP_LINE_ROW (w->current_matrix)->height
8309 = DESIRED_TOP_LINE_HEIGHT (w);
8310 }
8311
8312 if (fonts_changed_p)
8313 goto restore_buffers;
5ba50c51 8314 }
5f5c8ee5
GM
8315
8316 if (!line_number_displayed
8317 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
8318 {
8319 w->base_line_pos = Qnil;
8320 w->base_line_number = Qnil;
8321 }
a2889657 8322
5f5c8ee5
GM
8323 finish_menu_bars:
8324
7ce2c095 8325 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 8326 if (update_mode_line
5f5c8ee5
GM
8327 && EQ (FRAME_SELECTED_WINDOW (f), window))
8328 {
8329 int redisplay_menu_p = 0;
8330
8331 if (FRAME_WINDOW_P (f))
8332 {
dc937613 8333#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 8334 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 8335#else
5f5c8ee5 8336 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 8337#endif
5f5c8ee5
GM
8338 }
8339 else
8340 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8341
8342 if (redisplay_menu_p)
8343 display_menu_bar (w);
8344
8345#ifdef HAVE_WINDOW_SYSTEM
8346 if (WINDOWP (f->toolbar_window)
8347 && (FRAME_TOOLBAR_LINES (f) > 0
8348 || auto_resize_toolbars_p))
8349 redisplay_toolbar (f);
8350#endif
8351 }
7ce2c095 8352
88f22aff 8353 finish_scroll_bars:
5f5c8ee5 8354
88f22aff 8355 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 8356 {
b1d1124b 8357 int start, end, whole;
30c566e4 8358
b1d1124b 8359 /* Calculate the start and end positions for the current window.
3505ea70
JB
8360 At some point, it would be nice to choose between scrollbars
8361 which reflect the whole buffer size, with special markers
8362 indicating narrowing, and scrollbars which reflect only the
8363 visible region.
8364
5f5c8ee5 8365 Note that mini-buffers sometimes aren't displaying any text. */
b1d1124b 8366 if (! MINI_WINDOW_P (w)
5f5c8ee5
GM
8367 || (w == XWINDOW (minibuf_window)
8368 && !echo_area_glyphs
8369 && !STRINGP (echo_area_message)))
b1d1124b 8370 {
8a9311d7 8371 whole = ZV - BEGV;
4d641a15 8372 start = marker_position (w->start) - BEGV;
b1d1124b
JB
8373 /* I don't think this is guaranteed to be right. For the
8374 moment, we'll pretend it is. */
5f5c8ee5 8375 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 8376
5f5c8ee5
GM
8377 if (end < start)
8378 end = start;
8379 if (whole < (end - start))
8380 whole = end - start;
b1d1124b
JB
8381 }
8382 else
8383 start = end = whole = 0;
30c566e4 8384
88f22aff 8385 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 8386 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 8387
5f5c8ee5
GM
8388 /* Note that we actually used the scroll bar attached to this
8389 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 8390 (*redeem_scroll_bar_hook) (w);
30c566e4 8391 }
b1d1124b 8392
5f5c8ee5
GM
8393 restore_buffers:
8394
8395 /* Restore current_buffer and value of point in it. */
8396 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
5ba50c51 8397 if (really_switched_buffer)
f72df6ac 8398 set_buffer_internal_1 (old);
e481f960
RS
8399 else
8400 set_buffer_temp (old);
5f5c8ee5 8401 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
8402
8403 unbind_to (count, Qnil);
a2889657 8404}
a2889657 8405
5f5c8ee5
GM
8406
8407/* Build the complete desired matrix of WINDOW with a window start
8408 buffer position POS. Value is non-zero if successful. It is zero
8409 if fonts were loaded during redisplay which makes re-adjusting
8410 glyph matrices necessary. */
8411
8412int
a2889657
JB
8413try_window (window, pos)
8414 Lisp_Object window;
5f5c8ee5
GM
8415 struct text_pos pos;
8416{
8417 struct window *w = XWINDOW (window);
8418 struct it it;
8419 struct glyph_row *last_text_row = NULL;
9cbab4ff 8420
5f5c8ee5
GM
8421 /* Make POS the new window start. */
8422 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 8423
5f5c8ee5
GM
8424 /* Mark cursor position as unknown. No overlay arrow seen. */
8425 w->cursor.vpos = -1;
a2889657 8426 overlay_arrow_seen = 0;
642eefc6 8427
5f5c8ee5
GM
8428 /* Initialize iterator and info to start at POS. */
8429 start_display (&it, w, pos);
a2889657 8430
5f5c8ee5
GM
8431 /* Display all lines of W. */
8432 while (it.current_y < it.last_visible_y)
8433 {
8434 if (display_line (&it))
8435 last_text_row = it.glyph_row - 1;
8436 if (fonts_changed_p)
8437 return 0;
8438 }
a2889657 8439
5f5c8ee5
GM
8440 /* If bottom moved off end of frame, change mode line percentage. */
8441 if (XFASTINT (w->window_end_pos) <= 0
8442 && Z != IT_CHARPOS (it))
a2889657
JB
8443 w->update_mode_line = Qt;
8444
5f5c8ee5
GM
8445 /* Set window_end_pos to the offset of the last character displayed
8446 on the window from the end of current_buffer. Set
8447 window_end_vpos to its row number. */
8448 if (last_text_row)
8449 {
8450 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
8451 w->window_end_bytepos
8452 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8453 XSETFASTINT (w->window_end_pos,
8454 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8455 XSETFASTINT (w->window_end_vpos,
8456 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8457 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
8458 ->displays_text_p);
8459 }
8460 else
8461 {
8462 w->window_end_bytepos = 0;
8463 XSETFASTINT (w->window_end_pos, 0);
8464 XSETFASTINT (w->window_end_vpos, 0);
8465 }
8466
a2889657
JB
8467 /* But that is not valid info until redisplay finishes. */
8468 w->window_end_valid = Qnil;
5f5c8ee5 8469 return 1;
a2889657 8470}
5f5c8ee5
GM
8471
8472
a2889657 8473\f
5f5c8ee5
GM
8474/************************************************************************
8475 Window redisplay reusing current matrix when buffer has not changed
8476 ************************************************************************/
8477
8478/* Try redisplay of window W showing an unchanged buffer with a
8479 different window start than the last time it was displayed by
8480 reusing its current matrix. Value is non-zero if successful.
8481 W->start is the new window start. */
a2889657
JB
8482
8483static int
5f5c8ee5
GM
8484try_window_reusing_current_matrix (w)
8485 struct window *w;
a2889657 8486{
5f5c8ee5
GM
8487 struct frame *f = XFRAME (w->frame);
8488 struct glyph_row *row, *bottom_row;
8489 struct it it;
8490 struct run run;
8491 struct text_pos start, new_start;
8492 int nrows_scrolled, i;
8493 struct glyph_row *last_text_row;
8494 struct glyph_row *last_reused_text_row;
8495 struct glyph_row *start_row;
8496 int start_vpos, min_y, max_y;
8497
8498 /* Right now this function doesn't handle terminal frames. */
8499 if (!FRAME_WINDOW_P (f))
8500 return 0;
a2889657 8501
5f5c8ee5
GM
8502 /* Can't do this if region may have changed. */
8503 if ((!NILP (Vtransient_mark_mode)
8504 && !NILP (current_buffer->mark_active))
8505 || !NILP (w->region_showing))
8506 return 0;
a2889657 8507
5f5c8ee5
GM
8508 /* If top-line visibility has changed, give up. */
8509 if (WINDOW_WANTS_TOP_LINE_P (w)
8510 != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
8511 return 0;
8512
8513 /* Give up if old or new display is scrolled vertically. We could
8514 make this function handle this, but right now it doesn't. */
8515 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8516 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
8517 return 0;
8518
8519 /* The variable new_start now holds the new window start. The old
8520 start `start' can be determined from the current matrix. */
8521 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
8522 start = start_row->start.pos;
8523 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 8524
5f5c8ee5
GM
8525 /* Clear the desired matrix for the display below. */
8526 clear_glyph_matrix (w->desired_matrix);
8527
8528 if (CHARPOS (new_start) <= CHARPOS (start))
8529 {
8530 int first_row_y;
8531
8532 IF_DEBUG (debug_method_add (w, "twu1"));
8533
8534 /* Display up to a row that can be reused. The variable
8535 last_text_row is set to the last row displayed that displays
8536 text. */
8537 start_display (&it, w, new_start);
8538 first_row_y = it.current_y;
8539 w->cursor.vpos = -1;
8540 last_text_row = last_reused_text_row = NULL;
8541 while (it.current_y < it.last_visible_y
8542 && IT_CHARPOS (it) < CHARPOS (start)
8543 && !fonts_changed_p)
8544 if (display_line (&it))
8545 last_text_row = it.glyph_row - 1;
8546
8547 /* A value of current_y < last_visible_y means that we stopped
8548 at the previous window start, which in turn means that we
8549 have at least one reusable row. */
8550 if (it.current_y < it.last_visible_y)
a2889657 8551 {
5f5c8ee5
GM
8552 nrows_scrolled = it.vpos;
8553
8554 /* Find PT if not already found in the lines displayed. */
8555 if (w->cursor.vpos < 0)
a2889657 8556 {
5f5c8ee5
GM
8557 int dy = it.current_y - first_row_y;
8558
8559 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8560 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8561 {
8562 if (PT >= MATRIX_ROW_START_CHARPOS (row)
8563 && PT < MATRIX_ROW_END_CHARPOS (row))
8564 {
8565 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
8566 dy, nrows_scrolled);
8567 break;
8568 }
8569
8570 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
8571 break;
8572
8573 ++row;
8574 }
8575
8576 /* Give up if point was not found. This shouldn't
8577 happen often; not more often than with try_window
8578 itself. */
8579 if (w->cursor.vpos < 0)
8580 {
8581 clear_glyph_matrix (w->desired_matrix);
8582 return 0;
8583 }
a2889657 8584 }
5f5c8ee5
GM
8585
8586 /* Scroll the display. Do it before the current matrix is
8587 changed. The problem here is that update has not yet
8588 run, i.e. part of the current matrix is not up to date.
8589 scroll_run_hook will clear the cursor, and use the
8590 current matrix to get the height of the row the cursor is
8591 in. */
8592 run.current_y = first_row_y;
8593 run.desired_y = it.current_y;
8594 run.height = it.last_visible_y - it.current_y;
8595 if (run.height > 0)
a2889657 8596 {
5f5c8ee5
GM
8597 update_begin (f);
8598 rif->update_window_begin_hook (w);
8599 rif->scroll_run_hook (w, &run);
8600 rif->update_window_end_hook (w, 0);
8601 update_end (f);
a2889657 8602 }
5f5c8ee5
GM
8603
8604 /* Shift current matrix down by nrows_scrolled lines. */
8605 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
8606 rotate_matrix (w->current_matrix,
8607 start_vpos,
8608 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
8609 nrows_scrolled);
8610
8611 /* Disable lines not reused. */
8612 for (i = 0; i < it.vpos; ++i)
8613 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
8614
8615 /* Re-compute Y positions. */
8616 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
8617 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8618 max_y = it.last_visible_y;
8619 while (row < bottom_row)
d2f84654 8620 {
5f5c8ee5
GM
8621 row->y = it.current_y;
8622
8623 if (row->y < min_y)
8624 row->visible_height = row->height - (min_y - row->y);
8625 else if (row->y + row->height > max_y)
8626 row->visible_height
8627 = row->height - (row->y + row->height - max_y);
8628 else
8629 row->visible_height = row->height;
8630
8631 it.current_y += row->height;
8632 ++it.vpos;
8633
8634 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8635 last_reused_text_row = row;
8636 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
8637 break;
8638 ++row;
d2f84654 8639 }
a2889657 8640 }
5f5c8ee5
GM
8641
8642 /* Update window_end_pos etc.; last_reused_text_row is the last
8643 reused row from the current matrix containing text, if any.
8644 The value of last_text_row is the last displayed line
8645 containing text. */
8646 if (last_reused_text_row)
a2889657 8647 {
5f5c8ee5
GM
8648 w->window_end_bytepos
8649 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
8650 XSETFASTINT (w->window_end_pos,
8651 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
8652 XSETFASTINT (w->window_end_vpos,
8653 MATRIX_ROW_VPOS (last_reused_text_row,
8654 w->current_matrix));
a2889657 8655 }
5f5c8ee5
GM
8656 else if (last_text_row)
8657 {
8658 w->window_end_bytepos
8659 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8660 XSETFASTINT (w->window_end_pos,
8661 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8662 XSETFASTINT (w->window_end_vpos,
8663 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8664 }
8665 else
8666 {
8667 /* This window must be completely empty. */
8668 w->window_end_bytepos = 0;
8669 XSETFASTINT (w->window_end_pos, 0);
8670 XSETFASTINT (w->window_end_vpos, 0);
8671 }
8672 w->window_end_valid = Qnil;
a2889657 8673
5f5c8ee5
GM
8674 /* Update hint: don't try scrolling again in update_window. */
8675 w->desired_matrix->no_scrolling_p = 1;
8676
8677#if GLYPH_DEBUG
8678 debug_method_add (w, "try_window_reusing_current_matrix 1");
8679#endif
8680 return 1;
a2889657 8681 }
5f5c8ee5
GM
8682 else if (CHARPOS (new_start) > CHARPOS (start))
8683 {
8684 struct glyph_row *pt_row, *row;
8685 struct glyph_row *first_reusable_row;
8686 struct glyph_row *first_row_to_display;
8687 int dy;
8688 int yb = window_text_bottom_y (w);
8689
8690 IF_DEBUG (debug_method_add (w, "twu2"));
8691
8692 /* Find the row starting at new_start, if there is one. Don't
8693 reuse a partially visible line at the end. */
8694 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8695 while (first_reusable_row->enabled_p
8696 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
8697 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
8698 < CHARPOS (new_start)))
8699 ++first_reusable_row;
8700
8701 /* Give up if there is no row to reuse. */
8702 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
8703 || !first_reusable_row->enabled_p)
8704 return 0;
8705
8706 /* The row we found must start at new_start, or else something
8707 is broken. */
8708 xassert (MATRIX_ROW_START_CHARPOS (first_reusable_row)
8709 == CHARPOS (new_start));
8710
8711 /* We can reuse fully visible rows beginning with
8712 first_reusable_row to the end of the window. Set
8713 first_row_to_display to the first row that cannot be reused.
8714 Set pt_row to the row containing point, if there is any. */
8715 first_row_to_display = first_reusable_row;
8716 pt_row = NULL;
8717 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
8718 {
8719 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
8720 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
8721 pt_row = first_row_to_display;
a2889657 8722
5f5c8ee5
GM
8723 ++first_row_to_display;
8724 }
a2889657 8725
5f5c8ee5
GM
8726 /* Start displaying at the start of first_row_to_display. */
8727 xassert (first_row_to_display->y < yb);
8728 init_to_row_start (&it, w, first_row_to_display);
8729 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
8730 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
8731 - nrows_scrolled);
8732 it.current_y = first_row_to_display->y - first_reusable_row->y;
8733
8734 /* Display lines beginning with first_row_to_display in the
8735 desired matrix. Set last_text_row to the last row displayed
8736 that displays text. */
8737 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
8738 if (pt_row == NULL)
8739 w->cursor.vpos = -1;
8740 last_text_row = NULL;
8741 while (it.current_y < it.last_visible_y && !fonts_changed_p)
8742 if (display_line (&it))
8743 last_text_row = it.glyph_row - 1;
8744
8745 /* Give up If point isn't in a row displayed or reused. */
8746 if (w->cursor.vpos < 0)
8747 {
8748 clear_glyph_matrix (w->desired_matrix);
8749 return 0;
8750 }
12adba34 8751
5f5c8ee5
GM
8752 /* If point is in a reused row, adjust y and vpos of the cursor
8753 position. */
8754 if (pt_row)
8755 {
8756 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
8757 w->current_matrix);
8758 w->cursor.y -= first_reusable_row->y;
a2889657
JB
8759 }
8760
5f5c8ee5
GM
8761 /* Scroll the display. */
8762 run.current_y = first_reusable_row->y;
8763 run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8764 run.height = it.last_visible_y - run.current_y;
8765 if (run.height)
8766 {
8767 struct frame *f = XFRAME (WINDOW_FRAME (w));
8768 update_begin (f);
8769 rif->update_window_begin_hook (w);
8770 rif->scroll_run_hook (w, &run);
8771 rif->update_window_end_hook (w, 0);
8772 update_end (f);
8773 }
a2889657 8774
5f5c8ee5
GM
8775 /* Adjust Y positions of reused rows. */
8776 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
8777 row = first_reusable_row;
8778 dy = first_reusable_row->y;
8779 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8780 max_y = it.last_visible_y;
8781 while (row < first_row_to_display)
8782 {
8783 row->y -= dy;
8784 if (row->y < min_y)
8785 row->visible_height = row->height - (min_y - row->y);
8786 else if (row->y + row->height > max_y)
8787 row->visible_height
8788 = row->height - (row->y + row->height - max_y);
8789 else
8790 row->visible_height = row->height;
8791 ++row;
8792 }
a2889657 8793
5f5c8ee5
GM
8794 /* Disable rows not reused. */
8795 while (row < bottom_row)
8796 {
8797 row->enabled_p = 0;
8798 ++row;
8799 }
8800
8801 /* Scroll the current matrix. */
8802 xassert (nrows_scrolled > 0);
8803 rotate_matrix (w->current_matrix,
8804 start_vpos,
8805 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
8806 -nrows_scrolled);
8807
8808 /* Adjust window end. A null value of last_text_row means that
8809 the window end is in reused rows which in turn means that
8810 only its vpos can have changed. */
8811 if (last_text_row)
8812 {
8813 w->window_end_bytepos
8814 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8815 XSETFASTINT (w->window_end_pos,
8816 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8817 XSETFASTINT (w->window_end_vpos,
8818 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8819 }
8820 else
a2889657 8821 {
e8e536a9 8822 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 8823 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 8824 }
5f5c8ee5
GM
8825
8826 w->window_end_valid = Qnil;
8827 w->desired_matrix->no_scrolling_p = 1;
8828
8829#if GLYPH_DEBUG
8830 debug_method_add (w, "try_window_reusing_current_matrix 2");
8831#endif
8832 return 1;
a2889657 8833 }
5f5c8ee5
GM
8834
8835 return 0;
8836}
a2889657 8837
a2889657 8838
5f5c8ee5
GM
8839\f
8840/************************************************************************
8841 Window redisplay reusing current matrix when buffer has changed
8842 ************************************************************************/
8843
8844static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
8845static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
8846 int *, int *));
8847static struct glyph_row *
8848find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
8849 struct glyph_row *));
8850
8851
8852/* Return the last row in MATRIX displaying text. If row START is
8853 non-null, start searching with that row. IT gives the dimensions
8854 of the display. Value is null if matrix is empty; otherwise it is
8855 a pointer to the row found. */
8856
8857static struct glyph_row *
8858find_last_row_displaying_text (matrix, it, start)
8859 struct glyph_matrix *matrix;
8860 struct it *it;
8861 struct glyph_row *start;
8862{
8863 struct glyph_row *row, *row_found;
8864
8865 /* Set row_found to the last row in IT->w's current matrix
8866 displaying text. The loop looks funny but think of partially
8867 visible lines. */
8868 row_found = NULL;
8869 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
8870 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8871 {
8872 xassert (row->enabled_p);
8873 row_found = row;
8874 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
8875 break;
8876 ++row;
a2889657 8877 }
5f5c8ee5
GM
8878
8879 return row_found;
8880}
8881
a2889657 8882
5f5c8ee5
GM
8883/* Return the last row in the current matrix of W that is not affected
8884 by changes at the start of current_buffer that occurred since the
8885 last time W was redisplayed. Value is null if no such row exists.
a2889657 8886
5f5c8ee5
GM
8887 The global variable beg_unchanged has to contain the number of
8888 bytes unchanged at the start of current_buffer. BEG +
8889 beg_unchanged is the buffer position of the first changed byte in
8890 current_buffer. Characters at positions < BEG + beg_unchanged are
8891 at the same buffer positions as they were when the current matrix
8892 was built. */
8893
8894static struct glyph_row *
8895get_last_unchanged_at_beg_row (w)
8896 struct window *w;
8897{
8898 int first_changed_pos = BEG + beg_unchanged;
8899 struct glyph_row *row;
8900 struct glyph_row *row_found = NULL;
8901 int yb = window_text_bottom_y (w);
8902
8903 /* Find the last row displaying unchanged text. */
8904 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8905 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
8906 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 8907 {
5f5c8ee5
GM
8908 if (/* If row ends before first_changed_pos, it is unchanged,
8909 except in some case. */
8910 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
8911 /* When row ends in ZV and we write at ZV it is not
8912 unchanged. */
8913 && !row->ends_at_zv_p
8914 /* When first_changed_pos is the end of a continued line,
8915 row is not unchanged because it may be no longer
8916 continued. */
8917 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
8918 && row->continued_p))
8919 row_found = row;
8920
8921 /* Stop if last visible row. */
8922 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
8923 break;
8924
8925 ++row;
a2889657
JB
8926 }
8927
5f5c8ee5 8928 return row_found;
a2889657 8929}
5f5c8ee5
GM
8930
8931
8932/* Find the first glyph row in the current matrix of W that is not
8933 affected by changes at the end of current_buffer since the last
8934 time the window was redisplayed. Return in *DELTA the number of
8935 bytes by which buffer positions in unchanged text at the end of
8936 current_buffer must be adjusted. Value is null if no such row
8937 exists, i.e. all rows are affected by changes.
8938
8939 The global variable end_unchanged is assumed to contain the number
8940 of unchanged bytes at the end of current_buffer. The buffer
8941 position of the last changed byte in current_buffer is then Z -
8942 end_unchanged. */
8943
8944static struct glyph_row *
8945get_first_unchanged_at_end_row (w, delta, delta_bytes)
8946 struct window *w;
8947 int *delta, *delta_bytes;
a2889657 8948{
5f5c8ee5
GM
8949 struct glyph_row *row;
8950 struct glyph_row *row_found = NULL;
c581d710 8951
5f5c8ee5 8952 *delta = *delta_bytes = 0;
b2a76982 8953
5f5c8ee5
GM
8954 /* A value of window_end_pos >= end_unchanged means that the window
8955 end is in the range of changed text. If so, there is no
8956 unchanged row at the end of W's current matrix. */
8957 xassert (!NILP (w->window_end_valid));
8958 if (XFASTINT (w->window_end_pos) >= end_unchanged)
8959 return NULL;
8960
8961 /* Set row to the last row in W's current matrix displaying text. */
8962 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
8963
8964 /* End vpos should always be on text, except in an entirely empty
8965 matrix. */
8966 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row)
8967 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0);
8968
8969 /* If matrix is entirely empty, no unchanged row exists. */
8970 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8971 {
8972 /* The value of row is the last glyph row in the matrix having a
8973 meaningful buffer position in it. The end position of row
8974 corresponds to window_end_pos. This allows us to translate
8975 buffer positions in the current matrix to current buffer
8976 positions for characters not in changed text. */
8977 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
8978 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
8979 int last_unchanged_pos, last_unchanged_pos_old;
8980 struct glyph_row *first_text_row
8981 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8982
8983 *delta = Z - Z_old;
8984 *delta_bytes = Z_BYTE - Z_BYTE_old;
8985
8986 /* Set last_unchanged_pos to the buffer position of the last
8987 character in the buffer that has not been changed. Z is the
8988 index + 1 of the last byte in current_buffer, i.e. by
8989 subtracting end_unchanged we get the index of the last
8990 unchanged character, and we have to add BEG to get its buffer
8991 position. */
8992 last_unchanged_pos = Z - end_unchanged + BEG;
8993 last_unchanged_pos_old = last_unchanged_pos - *delta;
8994
8995 /* Search backward from ROW for a row displaying a line that
8996 starts at a minimum position >= last_unchanged_pos_old. */
8997 while (row >= first_text_row)
8998 {
8999 xassert (row->enabled_p);
9000 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9001
9002 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9003 row_found = row;
9004 --row;
9005 }
9006 }
9007
9008 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9009 return row_found;
c581d710
RS
9010}
9011
c581d710 9012
5f5c8ee5
GM
9013/* Make sure that glyph rows in the current matrix of window W
9014 reference the same glyph memory as corresponding rows in the
9015 frame's frame matrix. This function is called after scrolling W's
9016 current matrix on a terminal frame in try_window_id and
9017 try_window_reusing_current_matrix. */
9018
9019static void
9020sync_frame_with_window_matrix_rows (w)
9021 struct window *w;
c581d710 9022{
5f5c8ee5
GM
9023 struct frame *f = XFRAME (w->frame);
9024 struct glyph_row *window_row, *window_row_end, *frame_row;
9025
9026 /* Preconditions: W must be a leaf window and full-width. Its frame
9027 must have a frame matrix. */
9028 xassert (NILP (w->hchild) && NILP (w->vchild));
9029 xassert (WINDOW_FULL_WIDTH_P (w));
9030 xassert (!FRAME_WINDOW_P (f));
9031
9032 /* If W is a full-width window, glyph pointers in W's current matrix
9033 have, by definition, to be the same as glyph pointers in the
9034 corresponding frame matrix. */
9035 window_row = w->current_matrix->rows;
9036 window_row_end = window_row + w->current_matrix->nrows;
9037 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9038 while (window_row < window_row_end)
659a218f 9039 {
5f5c8ee5
GM
9040 int area;
9041 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9042 frame_row->glyphs[area] = window_row->glyphs[area];
9043 ++window_row, ++frame_row;
659a218f 9044 }
a2889657 9045}
5f5c8ee5
GM
9046
9047
9048/* Try to redisplay window W by reusing its existing display. W's
9049 current matrix must be up to date when this function is called,
9050 i.e. window_end_valid must not be nil.
9051
9052 Value is
9053
9054 1 if display has been updated
9055 0 if otherwise unsuccessful
9056 -1 if redisplay with same window start is known not to succeed
9057
9058 The following steps are performed:
9059
9060 1. Find the last row in the current matrix of W that is not
9061 affected by changes at the start of current_buffer. If no such row
9062 is found, give up.
9063
9064 2. Find the first row in W's current matrix that is not affected by
9065 changes at the end of current_buffer. Maybe there is no such row.
9066
9067 3. Display lines beginning with the row + 1 found in step 1 to the
9068 row found in step 2 or, if step 2 didn't find a row, to the end of
9069 the window.
9070
9071 4. If cursor is not known to appear on the window, give up.
9072
9073 5. If display stopped at the row found in step 2, scroll the
9074 display and current matrix as needed.
9075
9076 6. Maybe display some lines at the end of W, if we must. This can
9077 happen under various circumstances, like a partially visible line
9078 becoming fully visible, or because newly displayed lines are displayed
9079 in smaller font sizes.
9080
9081 7. Update W's window end information. */
9082
9083 /* Check that window end is what we expect it to be. */
12adba34
RS
9084
9085static int
5f5c8ee5 9086try_window_id (w)
12adba34 9087 struct window *w;
12adba34 9088{
5f5c8ee5
GM
9089 struct frame *f = XFRAME (w->frame);
9090 struct glyph_matrix *current_matrix = w->current_matrix;
9091 struct glyph_matrix *desired_matrix = w->desired_matrix;
9092 struct glyph_row *last_unchanged_at_beg_row;
9093 struct glyph_row *first_unchanged_at_end_row;
9094 struct glyph_row *row;
9095 struct glyph_row *bottom_row;
9096 int bottom_vpos;
9097 struct it it;
9098 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9099 struct text_pos start_pos;
9100 struct run run;
9101 int first_unchanged_at_end_vpos = 0;
9102 struct glyph_row *last_text_row, *last_text_row_at_end;
9103 struct text_pos start;
9104
9105 SET_TEXT_POS_FROM_MARKER (start, w->start);
9106
9107 /* Check pre-conditions. Window end must be valid, otherwise
9108 the current matrix would not be up to date. */
9109 xassert (!NILP (w->window_end_valid));
9110 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9111 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9112
9113 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9114 only if buffer has really changed. The reason is that the gap is
9115 initially at Z for freshly visited files. The code below would
9116 set end_unchanged to 0 in that case. */
9117 if (MODIFF > SAVE_MODIFF)
9118 {
9119 if (GPT - BEG < beg_unchanged)
9120 beg_unchanged = GPT - BEG;
9121 if (Z - GPT < end_unchanged)
9122 end_unchanged = Z - GPT;
9123 }
9124
9125 /* If window starts after a line end, and the last change is in
9126 front of that newline, then changes don't affect the display.
9127 This case happens with stealth-fontification. */
9128 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9129 if (CHARPOS (start) > BEGV
9130 && Z - end_unchanged < CHARPOS (start) - 1
9131 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9132 && PT < MATRIX_ROW_END_CHARPOS (row))
9133 {
9134 /* We have to update window end positions because the buffer's
9135 size has changed. */
9136 w->window_end_pos
9137 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9138 w->window_end_bytepos
9139 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9140 return 1;
9141 }
9142
9143 /* Return quickly if changes are all below what is displayed in the
9144 window, and if PT is in the window. */
9145 if (beg_unchanged > MATRIX_ROW_END_CHARPOS (row)
9146 && PT < MATRIX_ROW_END_CHARPOS (row))
9147 {
9148 /* We have to update window end positions because the buffer's
9149 size has changed. */
9150 w->window_end_pos
9151 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9152 w->window_end_bytepos
9153 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9154 return 1;
9155 }
9156
9157 /* Check that window start agrees with the start of the first glyph
9158 row in its current matrix. Check this after we know the window
9159 start is not in changed text, otherwise positions would not be
9160 comparable. */
9161 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9162 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9163 return 0;
9164
9165 /* Remember beg_unchanged and end_unchanged for debugging purposes. */
9166 IF_DEBUG (debug_beg_unchanged = beg_unchanged;
9167 debug_end_unchanged = end_unchanged);
9168
9169 /* Compute the position at which we have to start displaying new
9170 lines. Some of the lines at the top of the window might be
9171 reusable because they are not displaying changed text. Find the
9172 last row in W's current matrix not affected by changes at the
9173 start of current_buffer. Value is null if changes start in the
9174 first line of window. */
9175 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9176 if (last_unchanged_at_beg_row)
9177 {
9178 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9179 start_pos = it.current.pos;
9180
9181 /* Start displaying new lines in the desired matrix at the same
9182 vpos we would use in the current matrix, i.e. below
9183 last_unchanged_at_beg_row. */
9184 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9185 current_matrix);
9186 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9187 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9188
9189 xassert (it.hpos == 0 && it.current_x == 0);
9190 }
9191 else
9192 {
9193 /* There are no reusable lines at the start of the window.
9194 Start displaying in the first line. */
9195 start_display (&it, w, start);
9196 start_pos = it.current.pos;
9197 }
9198
9199 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
9200 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
9201
9202 /* Find the first row that is not affected by changes at the end of
9203 the buffer. Value will be null if there is no unchanged row, in
9204 which case we must redisplay to the end of the window. delta
9205 will be set to the value by which buffer positions beginning with
9206 first_unchanged_at_end_row have to be adjusted due to text
9207 changes. */
9208 first_unchanged_at_end_row
9209 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9210 IF_DEBUG (debug_delta = delta);
9211 IF_DEBUG (debug_delta_bytes = delta_bytes);
9212
9213 /* Set stop_pos to the buffer position up to which we will have to
9214 display new lines. If first_unchanged_at_end_row != NULL, this
9215 is the buffer position of the start of the line displayed in that
9216 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9217 that we don't stop at a buffer position. */
9218 stop_pos = 0;
9219 if (first_unchanged_at_end_row)
9220 {
9221 xassert (last_unchanged_at_beg_row == NULL
9222 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9223
9224 /* If this is a continuation line, move forward to the next one
9225 that isn't. Changes in lines above affect this line.
9226 Caution: this may move first_unchanged_at_end_row to a row
9227 not displaying text. */
9228 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9229 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9230 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9231 < it.last_visible_y))
9232 ++first_unchanged_at_end_row;
9233
9234 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9235 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9236 >= it.last_visible_y))
9237 first_unchanged_at_end_row = NULL;
9238 else
9239 {
9240 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9241 + delta);
9242 first_unchanged_at_end_vpos
9243 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9244 xassert (stop_pos >= Z - end_unchanged);
9245 }
9246 }
9247 else if (last_unchanged_at_beg_row == NULL)
9248 return 0;
9249
9250
9251#if GLYPH_DEBUG
9252
9253 /* Either there is no unchanged row at the end, or the one we have
9254 now displays text. This is a necessary condition for the window
9255 end pos calculation at the end of this function. */
9256 xassert (first_unchanged_at_end_row == NULL
9257 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9258
9259 debug_last_unchanged_at_beg_vpos
9260 = (last_unchanged_at_beg_row
9261 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9262 : -1);
9263 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9264
9265#endif /* GLYPH_DEBUG != 0 */
9266
9267
9268 /* Display new lines. Set last_text_row to the last new line
9269 displayed which has text on it, i.e. might end up as being the
9270 line where the window_end_vpos is. */
9271 w->cursor.vpos = -1;
9272 last_text_row = NULL;
9273 overlay_arrow_seen = 0;
9274 while (it.current_y < it.last_visible_y
9275 && !fonts_changed_p
9276 && (first_unchanged_at_end_row == NULL
9277 || IT_CHARPOS (it) < stop_pos))
9278 {
9279 if (display_line (&it))
9280 last_text_row = it.glyph_row - 1;
9281 }
9282
9283 if (fonts_changed_p)
9284 return -1;
9285
9286
9287 /* Compute differences in buffer positions, y-positions etc. for
9288 lines reused at the bottom of the window. Compute what we can
9289 scroll. */
9290 if (first_unchanged_at_end_row)
9291 {
9292 dvpos = (it.vpos
9293 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
9294 current_matrix));
9295 dy = it.current_y - first_unchanged_at_end_row->y;
9296 run.current_y = first_unchanged_at_end_row->y;
9297 run.desired_y = run.current_y + dy;
9298 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
9299 }
9300 else
9301 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
9302 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
9303
9304
9305 /* Find the cursor if not already found. We have to decide whether
9306 PT will appear on this window (it sometimes doesn't, but this is
9307 not a very frequent case.) This decision has to be made before
9308 the current matrix is altered. A value of cursor.vpos < 0 means
9309 that PT is either in one of the lines beginning at
9310 first_unchanged_at_end_row or below the window. Don't care for
9311 lines that might be displayed later at the window end; as
9312 mentioned, this is not a frequent case. */
9313 if (w->cursor.vpos < 0)
9314 {
9315 int last_y = min (it.last_visible_y, it.last_visible_y + dy);
9316
9317 /* Cursor in unchanged rows at the top? */
9318 if (PT < CHARPOS (start_pos)
9319 && last_unchanged_at_beg_row)
9320 {
9321 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9322 while (row <= last_unchanged_at_beg_row
9323 && MATRIX_ROW_END_CHARPOS (row) <= PT)
9324 ++row;
9325 xassert (row <= last_unchanged_at_beg_row);
9326 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9327 }
9328
9329 /* Start from first_unchanged_at_end_row looking for PT. */
9330 else if (first_unchanged_at_end_row)
9331 {
9332 row = first_unchanged_at_end_row;
9333 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9334 {
9335 if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
9336 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
9337 {
9338 set_cursor_from_row (w, row, w->current_matrix, delta,
9339 delta_bytes, dy, dvpos);
9340 break;
9341 }
9342 else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
9343 break;
9344 ++row;
9345 }
9346 }
9347
9348 /* Give up if cursor was not found. */
9349 if (w->cursor.vpos < 0)
9350 {
9351 clear_glyph_matrix (w->desired_matrix);
9352 return -1;
9353 }
9354 }
9355
9356 /* Don't let the cursor end in the scroll margins. */
9357 {
9358 int this_scroll_margin, cursor_height;
9359
9360 this_scroll_margin = max (0, scroll_margin);
9361 this_scroll_margin = min (this_scroll_margin,
9362 XFASTINT (w->height) / 4);
9363 this_scroll_margin *= CANON_Y_UNIT (it.f);
9364 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
9365
9366 if ((w->cursor.y < this_scroll_margin
9367 && CHARPOS (start) > BEGV)
9368 /* Don't take scroll margin into account at the bottom because
9369 old redisplay didn't do it either. */
9370 || w->cursor.y + cursor_height > it.last_visible_y)
9371 {
9372 w->cursor.vpos = -1;
9373 clear_glyph_matrix (w->desired_matrix);
9374 return -1;
9375 }
9376 }
9377
9378 /* Scroll the display. Do it before changing the current matrix so
9379 that xterm.c doesn't get confused about where the cursor glyph is
9380 found. */
9381 if (dy)
9382 {
9383 update_begin (f);
9384
9385 if (FRAME_WINDOW_P (f))
9386 {
9387 rif->update_window_begin_hook (w);
9388 rif->scroll_run_hook (w, &run);
9389 rif->update_window_end_hook (w, 0);
9390 }
9391 else
9392 {
9393 /* Terminal frame. In this case, dvpos gives the number of
9394 lines to scroll by; dvpos < 0 means scroll up. */
9395 int first_unchanged_at_end_vpos
9396 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
9397 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
9398 int end = XFASTINT (w->top) + window_internal_height (w);
9399
9400 /* Perform the operation on the screen. */
9401 if (dvpos > 0)
9402 {
9403 /* Scroll last_unchanged_at_beg_row to the end of the
9404 window down dvpos lines. */
9405 set_terminal_window (end);
9406
9407 /* On dumb terminals delete dvpos lines at the end
9408 before inserting dvpos empty lines. */
9409 if (!scroll_region_ok)
9410 ins_del_lines (end - dvpos, -dvpos);
9411
9412 /* Insert dvpos empty lines in front of
9413 last_unchanged_at_beg_row. */
9414 ins_del_lines (from, dvpos);
9415 }
9416 else if (dvpos < 0)
9417 {
9418 /* Scroll up last_unchanged_at_beg_vpos to the end of
9419 the window to last_unchanged_at_beg_vpos - |dvpos|. */
9420 set_terminal_window (end);
9421
9422 /* Delete dvpos lines in front of
9423 last_unchanged_at_beg_vpos. ins_del_lines will set
9424 the cursor to the given vpos and emit |dvpos| delete
9425 line sequences. */
9426 ins_del_lines (from + dvpos, dvpos);
9427
9428 /* On a dumb terminal insert dvpos empty lines at the
9429 end. */
9430 if (!scroll_region_ok)
9431 ins_del_lines (end + dvpos, -dvpos);
9432 }
9433
9434 set_terminal_window (0);
9435 }
9436
9437 update_end (f);
9438 }
9439
9440 /* Shift reused rows of the current matrix to the right position. */
9441 if (dvpos < 0)
9442 {
9443 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
9444 bottom_vpos, dvpos);
9445 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
9446 bottom_vpos, 0);
9447 }
9448 else if (dvpos > 0)
9449 {
9450 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
9451 bottom_vpos, dvpos);
9452 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
9453 first_unchanged_at_end_vpos + dvpos, 0);
9454 }
9455
9456 /* For frame-based redisplay, make sure that current frame and window
9457 matrix are in sync with respect to glyph memory. */
9458 if (!FRAME_WINDOW_P (f))
9459 sync_frame_with_window_matrix_rows (w);
9460
9461 /* Adjust buffer positions in reused rows. */
9462 if (delta)
9463 increment_glyph_matrix_buffer_positions (current_matrix,
9464 first_unchanged_at_end_vpos + dvpos,
9465 bottom_vpos, delta, delta_bytes);
9466
9467 /* Adjust Y positions. */
9468 if (dy)
9469 shift_glyph_matrix (w, current_matrix,
9470 first_unchanged_at_end_vpos + dvpos,
9471 bottom_vpos, dy);
9472
9473 if (first_unchanged_at_end_row)
9474 first_unchanged_at_end_row += dvpos;
9475
9476 /* If scrolling up, there may be some lines to display at the end of
9477 the window. */
9478 last_text_row_at_end = NULL;
9479 if (dy < 0)
9480 {
9481 /* Set last_row to the glyph row in the current matrix where the
9482 window end line is found. It has been moved up or down in
9483 the matrix by dvpos. */
9484 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
9485 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
9486
9487 /* If last_row is the window end line, it should display text. */
9488 xassert (last_row->displays_text_p);
9489
9490 /* If window end line was partially visible before, begin
9491 displaying at that line. Otherwise begin displaying with the
9492 line following it. */
9493 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
9494 {
9495 init_to_row_start (&it, w, last_row);
9496 it.vpos = last_vpos;
9497 it.current_y = last_row->y;
9498 }
9499 else
9500 {
9501 init_to_row_end (&it, w, last_row);
9502 it.vpos = 1 + last_vpos;
9503 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
9504 ++last_row;
9505 }
12adba34 9506
5f5c8ee5
GM
9507 /* We may start in a continuation line. If so, we have to get
9508 the right continuation_lines_width and current_x. */
9509 it.continuation_lines_width = last_row->continuation_lines_width;
9510 it.hpos = it.current_x = 0;
9511
9512 /* Display the rest of the lines at the window end. */
9513 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9514 while (it.current_y < it.last_visible_y
9515 && !fonts_changed_p)
9516 {
9517 /* Is it always sure that the display agrees with lines in
9518 the current matrix? I don't think so, so we mark rows
9519 displayed invalid in the current matrix by setting their
9520 enabled_p flag to zero. */
9521 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
9522 if (display_line (&it))
9523 last_text_row_at_end = it.glyph_row - 1;
9524 }
9525 }
12adba34 9526
5f5c8ee5
GM
9527 /* Update window_end_pos and window_end_vpos. */
9528 if (first_unchanged_at_end_row
9529 && first_unchanged_at_end_row->y < it.last_visible_y
9530 && !last_text_row_at_end)
9531 {
9532 /* Window end line if one of the preserved rows from the current
9533 matrix. Set row to the last row displaying text in current
9534 matrix starting at first_unchanged_at_end_row, after
9535 scrolling. */
9536 xassert (first_unchanged_at_end_row->displays_text_p);
9537 row = find_last_row_displaying_text (w->current_matrix, &it,
9538 first_unchanged_at_end_row);
9539 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
9540
9541 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
9542 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
9543 XSETFASTINT (w->window_end_vpos,
9544 MATRIX_ROW_VPOS (row, w->current_matrix));
9545 }
9546 else if (last_text_row_at_end)
9547 {
9548 XSETFASTINT (w->window_end_pos,
9549 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
9550 w->window_end_bytepos
9551 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
9552 XSETFASTINT (w->window_end_vpos,
9553 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
9554 }
9555 else if (last_text_row)
9556 {
9557 /* We have displayed either to the end of the window or at the
9558 end of the window, i.e. the last row with text is to be found
9559 in the desired matrix. */
9560 XSETFASTINT (w->window_end_pos,
9561 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9562 w->window_end_bytepos
9563 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9564 XSETFASTINT (w->window_end_vpos,
9565 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
9566 }
9567 else if (first_unchanged_at_end_row == NULL
9568 && last_text_row == NULL
9569 && last_text_row_at_end == NULL)
9570 {
9571 /* Displayed to end of window, but no line containing text was
9572 displayed. Lines were deleted at the end of the window. */
9573 int vpos;
9574 int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
9575
9576 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
9577 if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
9578 && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
9579 || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
9580 && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
9581 break;
12adba34 9582
5f5c8ee5
GM
9583 w->window_end_vpos = make_number (vpos);
9584 }
9585 else
9586 abort ();
9587
9588 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
9589 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 9590
5f5c8ee5
GM
9591 /* Record that display has not been completed. */
9592 w->window_end_valid = Qnil;
9593 w->desired_matrix->no_scrolling_p = 1;
9594 return 1;
12adba34 9595}
0f9c0ff0 9596
a2889657 9597
5f5c8ee5
GM
9598\f
9599/***********************************************************************
9600 More debugging support
9601 ***********************************************************************/
a2889657 9602
5f5c8ee5 9603#if GLYPH_DEBUG
a2889657 9604
5f5c8ee5
GM
9605 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
9606static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 9607
31b24551 9608
5f5c8ee5
GM
9609/* Dump the contents of glyph matrix MATRIX on stderr. If
9610 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 9611
5f5c8ee5
GM
9612void
9613dump_glyph_matrix (matrix, with_glyphs_p)
9614 struct glyph_matrix *matrix;
9615 int with_glyphs_p;
9616{
efc63ef0 9617 int i;
5f5c8ee5
GM
9618 for (i = 0; i < matrix->nrows; ++i)
9619 dump_glyph_row (matrix, i, with_glyphs_p);
9620}
31b24551 9621
68a37fa8 9622
5f5c8ee5
GM
9623/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9624 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 9625
5f5c8ee5
GM
9626void
9627dump_glyph_row (matrix, vpos, with_glyphs_p)
9628 struct glyph_matrix *matrix;
9629 int vpos, with_glyphs_p;
9630{
9631 struct glyph_row *row;
9632
9633 if (vpos < 0 || vpos >= matrix->nrows)
9634 return;
9635
9636 row = MATRIX_ROW (matrix, vpos);
9637
9638 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
9639 fprintf (stderr, "=============================================\n");
9640
9641 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",
9642 row - matrix->rows,
9643 MATRIX_ROW_START_CHARPOS (row),
9644 MATRIX_ROW_END_CHARPOS (row),
9645 row->used[TEXT_AREA],
9646 row->contains_overlapping_glyphs_p,
9647 row->enabled_p,
9648 row->inverse_p,
9649 row->truncated_on_left_p,
9650 row->truncated_on_right_p,
9651 row->overlay_arrow_p,
9652 row->continued_p,
9653 MATRIX_ROW_CONTINUATION_LINE_P (row),
9654 row->displays_text_p,
9655 row->ends_at_zv_p,
9656 row->fill_line_p,
9657 row->x,
9658 row->y,
9659 row->pixel_width);
9660 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
9661 row->end.overlay_string_index);
9662 fprintf (stderr, "%9d %5d\n",
9663 CHARPOS (row->start.string_pos),
9664 CHARPOS (row->end.string_pos));
9665 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
9666 row->end.dpvec_index);
9667
9668 if (with_glyphs_p)
bd66d1ba 9669 {
5f5c8ee5
GM
9670 struct glyph *glyph, *glyph_end;
9671 int prev_had_glyphs_p;
9672
9673 glyph = row->glyphs[TEXT_AREA];
9674 glyph_end = glyph + row->used[TEXT_AREA];
9675
9676 /* Glyph for a line end in text. */
9677 if (glyph == glyph_end && glyph->charpos > 0)
9678 ++glyph_end;
9679
9680 if (glyph < glyph_end)
bd66d1ba 9681 {
5f5c8ee5
GM
9682 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
9683 prev_had_glyphs_p = 1;
bd66d1ba
RS
9684 }
9685 else
5f5c8ee5
GM
9686 prev_had_glyphs_p = 0;
9687
9688 while (glyph < glyph_end)
f7b4b63a 9689 {
5f5c8ee5
GM
9690 if (glyph->type == CHAR_GLYPH)
9691 {
9692 fprintf (stderr,
9693 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9694 glyph - row->glyphs[TEXT_AREA],
9695 'C',
9696 glyph->charpos,
9697 glyph->pixel_width,
9698 glyph->u.ch.code,
9699 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
9700 ? glyph->u.ch.code
9701 : '.'),
9702 glyph->u.ch.face_id,
9703 glyph->left_box_line_p,
9704 glyph->right_box_line_p);
9705 }
9706 else if (glyph->type == STRETCH_GLYPH)
9707 {
9708 fprintf (stderr,
9709 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9710 glyph - row->glyphs[TEXT_AREA],
9711 'S',
9712 glyph->charpos,
9713 glyph->pixel_width,
9714 0,
9715 '.',
9716 glyph->u.stretch.face_id,
9717 glyph->left_box_line_p,
9718 glyph->right_box_line_p);
9719 }
9720 else if (glyph->type == IMAGE_GLYPH)
9721 {
9722 fprintf (stderr,
9723 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9724 glyph - row->glyphs[TEXT_AREA],
9725 'I',
9726 glyph->charpos,
9727 glyph->pixel_width,
9728 glyph->u.img.id,
9729 '.',
9730 glyph->u.img.face_id,
9731 glyph->left_box_line_p,
9732 glyph->right_box_line_p);
9733 }
9734 ++glyph;
f7b4b63a 9735 }
f4faa47c 9736 }
5f5c8ee5 9737}
f4faa47c 9738
a2889657 9739
5f5c8ee5
GM
9740DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
9741 Sdump_glyph_matrix, 0, 1, "p",
9742 "Dump the current matrix of the selected window to stderr.\n\
9743Shows contents of glyph row structures. With non-nil optional\n\
9744parameter WITH-GLYPHS-P, dump glyphs as well.")
9745 (with_glyphs_p)
9746{
9747 struct window *w = XWINDOW (selected_window);
9748 struct buffer *buffer = XBUFFER (w->buffer);
9749
9750 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
9751 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
9752 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
9753 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
9754 fprintf (stderr, "=============================================\n");
9755 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
9756 return Qnil;
9757}
1c2250c2 9758
1fca3fae 9759
5f5c8ee5
GM
9760DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
9761 "Dump glyph row ROW to stderr.")
9762 (row)
9763 Lisp_Object row;
9764{
9765 CHECK_NUMBER (row, 0);
9766 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
9767 return Qnil;
9768}
1fca3fae 9769
67481ae5 9770
5f5c8ee5
GM
9771DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
9772 0, 0, "", "")
9773 ()
9774{
9775 struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
9776 ->current_matrix);
9777 dump_glyph_row (m, 0, 1);
9778 return Qnil;
9779}
ca26e1c8 9780
0f9c0ff0 9781
5f5c8ee5
GM
9782DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
9783 Strace_redisplay_toggle, 0, 0, "",
9784 "Toggle tracing of redisplay.")
9785 ()
9786{
9787 trace_redisplay_p = !trace_redisplay_p;
9788 return Qnil;
9789}
9790
9791
9792#endif /* GLYPH_DEBUG */
ca26e1c8 9793
ca26e1c8 9794
5f5c8ee5
GM
9795\f
9796/***********************************************************************
9797 Building Desired Matrix Rows
9798 ***********************************************************************/
a2889657 9799
5f5c8ee5
GM
9800/* Return a temporary glyph row holding the glyphs of an overlay
9801 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 9802
5f5c8ee5
GM
9803static struct glyph_row *
9804get_overlay_arrow_glyph_row (w)
9805 struct window *w;
9806{
9807 struct frame *f = XFRAME (WINDOW_FRAME (w));
9808 struct buffer *buffer = XBUFFER (w->buffer);
9809 struct buffer *old = current_buffer;
9810 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
9811 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
9812 unsigned char *arrow_end = arrow_string + arrow_len;
9813 unsigned char *p;
9814 struct it it;
9815 int multibyte_p;
9816 int n_glyphs_before;
9817
9818 set_buffer_temp (buffer);
9819 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
9820 it.glyph_row->used[TEXT_AREA] = 0;
9821 SET_TEXT_POS (it.position, 0, 0);
9822
9823 multibyte_p = !NILP (buffer->enable_multibyte_characters);
9824 p = arrow_string;
9825 while (p < arrow_end)
9826 {
9827 Lisp_Object face, ilisp;
9828
9829 /* Get the next character. */
9830 if (multibyte_p)
4fdb80f2 9831 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
9832 else
9833 it.c = *p, it.len = 1;
9834 p += it.len;
9835
9836 /* Get its face. */
9837 XSETFASTINT (ilisp, p - arrow_string);
9838 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
9839 it.face_id = compute_char_face (f, it.c, face);
9840
9841 /* Compute its width, get its glyphs. */
9842 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 9843 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
9844 PRODUCE_GLYPHS (&it);
9845
9846 /* If this character doesn't fit any more in the line, we have
9847 to remove some glyphs. */
9848 if (it.current_x > it.last_visible_x)
9849 {
9850 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
9851 break;
9852 }
9853 }
9854
9855 set_buffer_temp (old);
9856 return it.glyph_row;
9857}
ca26e1c8 9858
b0a0fbda 9859
5f5c8ee5
GM
9860/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
9861 glyphs are only inserted for terminal frames since we can't really
9862 win with truncation glyphs when partially visible glyphs are
9863 involved. Which glyphs to insert is determined by
9864 produce_special_glyphs. */
67481ae5 9865
5f5c8ee5
GM
9866static void
9867insert_left_trunc_glyphs (it)
9868 struct it *it;
9869{
9870 struct it truncate_it;
9871 struct glyph *from, *end, *to, *toend;
9872
9873 xassert (!FRAME_WINDOW_P (it->f));
9874
9875 /* Get the truncation glyphs. */
9876 truncate_it = *it;
9877 truncate_it.charset = -1;
9878 truncate_it.current_x = 0;
9879 truncate_it.face_id = DEFAULT_FACE_ID;
9880 truncate_it.glyph_row = &scratch_glyph_row;
9881 truncate_it.glyph_row->used[TEXT_AREA] = 0;
9882 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
9883 truncate_it.object = 0;
9884 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
9885
9886 /* Overwrite glyphs from IT with truncation glyphs. */
9887 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
9888 end = from + truncate_it.glyph_row->used[TEXT_AREA];
9889 to = it->glyph_row->glyphs[TEXT_AREA];
9890 toend = to + it->glyph_row->used[TEXT_AREA];
9891
9892 while (from < end)
9893 *to++ = *from++;
9894
9895 /* There may be padding glyphs left over. Remove them. */
9896 from = to;
9897 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
9898 ++from;
9899 while (from < toend)
9900 *to++ = *from++;
9901
9902 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
9903}
e0bfbde6 9904
e0bfbde6 9905
5f5c8ee5 9906/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 9907
5f5c8ee5
GM
9908 Most of the time, ascent and height of a display line will be equal
9909 to the max_ascent and max_height values of the display iterator
9910 structure. This is not the case if
67481ae5 9911
5f5c8ee5
GM
9912 1. We hit ZV without displaying anything. In this case, max_ascent
9913 and max_height will be zero.
1c9241f5 9914
5f5c8ee5
GM
9915 2. We have some glyphs that don't contribute to the line height.
9916 (The glyph row flag contributes_to_line_height_p is for future
9917 pixmap extensions).
f6fd109b 9918
5f5c8ee5
GM
9919 The first case is easily covered by using default values because in
9920 these cases, the line height does not really matter, except that it
9921 must not be zero. */
67481ae5 9922
5f5c8ee5
GM
9923static void
9924compute_line_metrics (it)
9925 struct it *it;
9926{
9927 struct glyph_row *row = it->glyph_row;
9928 int area, i;
1c2250c2 9929
5f5c8ee5
GM
9930 if (FRAME_WINDOW_P (it->f))
9931 {
9932 int i, top_line_height;
1c2250c2 9933
5f5c8ee5
GM
9934 /* The line may consist of one space only, that was added to
9935 place the cursor on it. If so, the row's height hasn't been
9936 computed yet. */
9937 if (row->height == 0)
9938 {
9939 if (it->max_ascent + it->max_descent == 0)
312246d1 9940 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
9941 row->ascent = it->max_ascent;
9942 row->height = it->max_ascent + it->max_descent;
312246d1
GM
9943 row->phys_ascent = it->max_phys_ascent;
9944 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
9945 }
9946
9947 /* Compute the width of this line. */
9948 row->pixel_width = row->x;
9949 for (i = 0; i < row->used[TEXT_AREA]; ++i)
9950 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
9951
9952 xassert (row->pixel_width >= 0);
9953 xassert (row->ascent >= 0 && row->height > 0);
9954
312246d1
GM
9955 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
9956 || MATRIX_ROW_OVERLAPS_PRED_P (row));
9957
9958 /* If first line's physical ascent is larger than its logical
9959 ascent, use the physical ascent, and make the row taller.
9960 This makes accented characters fully visible. */
9961 if (row == it->w->desired_matrix->rows
9962 && row->phys_ascent > row->ascent)
9963 {
9964 row->height += row->phys_ascent - row->ascent;
9965 row->ascent = row->phys_ascent;
9966 }
9967
5f5c8ee5
GM
9968 /* Compute how much of the line is visible. */
9969 row->visible_height = row->height;
9970
9971 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
9972 if (row->y < top_line_height)
9973 row->visible_height -= top_line_height - row->y;
9974 else
9975 {
9976 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
9977 if (row->y + row->height > max_y)
9978 row->visible_height -= row->y + row->height - max_y;
9979 }
9980 }
9981 else
9982 {
9983 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
9984 row->ascent = row->phys_ascent = 0;
9985 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 9986 }
67481ae5 9987
5f5c8ee5
GM
9988 /* Compute a hash code for this row. */
9989 row->hash = 0;
9990 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9991 for (i = 0; i < row->used[area]; ++i)
9992 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
9993 + row->glyphs[area][i].u.val
9994 + (row->glyphs[area][i].type << 2));
a2889657 9995
5f5c8ee5 9996 it->max_ascent = it->max_descent = 0;
312246d1 9997 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 9998}
12adba34 9999
ca26e1c8 10000
5f5c8ee5
GM
10001/* Append one space to the glyph row of iterator IT if doing a
10002 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10003 space have the default face, otherwise let it have the same face as
10004 IT->face_id. This function is called to make sure that there is
10005 always one glyph at the end of a glyph row that the cursor can be
10006 set on under window-systems. (If there weren't such a glyph we
10007 would not know how wide and tall the cursor should be displayed). */
ca26e1c8 10008
5f5c8ee5
GM
10009static void
10010append_space (it, default_face_p)
10011 struct it *it;
10012 int default_face_p;
10013{
10014 if (FRAME_WINDOW_P (it->f))
10015 {
10016 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 10017
5f5c8ee5
GM
10018 if (it->glyph_row->glyphs[TEXT_AREA] + n
10019 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 10020 {
5f5c8ee5
GM
10021 /* Save some values that must not be changed. */
10022 int saved_x = it->current_x;
10023 struct text_pos saved_pos;
10024 int saved_what = it->what;
10025 int saved_face_id = it->face_id;
10026 int saved_charset = it->charset;
10027 Lisp_Object saved_object;
10028
10029 saved_object = it->object;
10030 saved_pos = it->position;
10031
10032 it->what = IT_CHARACTER;
10033 bzero (&it->position, sizeof it->position);
10034 it->object = 0;
10035 it->c = ' ';
10036 it->len = 1;
10037 it->charset = CHARSET_ASCII;
10038
10039 if (default_face_p)
10040 it->face_id = DEFAULT_FACE_ID;
10041 if (it->multibyte_p)
10042 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10043 else
10044 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
1842fc1a 10045
5f5c8ee5
GM
10046 PRODUCE_GLYPHS (it);
10047
10048 it->current_x = saved_x;
10049 it->object = saved_object;
10050 it->position = saved_pos;
10051 it->what = saved_what;
10052 it->face_id = saved_face_id;
10053 it->charset = saved_charset;
10054 }
10055 }
10056}
12adba34 10057
1842fc1a 10058
5f5c8ee5
GM
10059/* Extend the face of the last glyph in the text area of IT->glyph_row
10060 to the end of the display line. Called from display_line.
10061 If the glyph row is empty, add a space glyph to it so that we
10062 know the face to draw. Set the glyph row flag fill_line_p. */
10063
10064static void
10065extend_face_to_end_of_line (it)
10066 struct it *it;
10067{
10068 struct face *face;
10069 struct frame *f = it->f;
1842fc1a 10070
5f5c8ee5
GM
10071 /* If line is already filled, do nothing. */
10072 if (it->current_x >= it->last_visible_x)
10073 return;
10074
10075 /* Face extension extends the background and box of IT->face_id
10076 to the end of the line. If the background equals the background
10077 of the frame, we haven't to do anything. */
10078 face = FACE_FROM_ID (f, it->face_id);
10079 if (FRAME_WINDOW_P (f)
10080 && face->box == FACE_NO_BOX
10081 && face->background == FRAME_BACKGROUND_PIXEL (f)
10082 && !face->stipple)
10083 return;
1842fc1a 10084
5f5c8ee5
GM
10085 /* Set the glyph row flag indicating that the face of the last glyph
10086 in the text area has to be drawn to the end of the text area. */
10087 it->glyph_row->fill_line_p = 1;
545e04f6 10088
5f5c8ee5
GM
10089 /* If current charset of IT is not ASCII, make sure we have the
10090 ASCII face. This will be automatically undone the next time
10091 get_next_display_element returns a character from a different
10092 charset. Note that the charset will always be ASCII in unibyte
10093 text. */
10094 if (it->charset != CHARSET_ASCII)
10095 {
10096 it->charset = CHARSET_ASCII;
10097 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10098 }
545e04f6 10099
5f5c8ee5
GM
10100 if (FRAME_WINDOW_P (f))
10101 {
10102 /* If the row is empty, add a space with the current face of IT,
10103 so that we know which face to draw. */
10104 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 10105 {
5f5c8ee5
GM
10106 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10107 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10108 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 10109 }
5f5c8ee5
GM
10110 }
10111 else
10112 {
10113 /* Save some values that must not be changed. */
10114 int saved_x = it->current_x;
10115 struct text_pos saved_pos;
10116 Lisp_Object saved_object;
10117 int saved_what = it->what;
10118
10119 saved_object = it->object;
10120 saved_pos = it->position;
10121
10122 it->what = IT_CHARACTER;
10123 bzero (&it->position, sizeof it->position);
10124 it->object = 0;
10125 it->c = ' ';
10126 it->len = 1;
10127
10128 PRODUCE_GLYPHS (it);
10129
10130 while (it->current_x <= it->last_visible_x)
10131 PRODUCE_GLYPHS (it);
10132
10133 /* Don't count these blanks really. It would let us insert a left
10134 truncation glyph below and make us set the cursor on them, maybe. */
10135 it->current_x = saved_x;
10136 it->object = saved_object;
10137 it->position = saved_pos;
10138 it->what = saved_what;
10139 }
10140}
12adba34 10141
545e04f6 10142
5f5c8ee5
GM
10143/* Value is non-zero if text starting at CHARPOS in current_buffer is
10144 trailing whitespace. */
1c9241f5 10145
5f5c8ee5
GM
10146static int
10147trailing_whitespace_p (charpos)
10148 int charpos;
10149{
10150 int bytepos = CHAR_TO_BYTE (charpos);
10151 int c = 0;
7bbe686f 10152
5f5c8ee5
GM
10153 while (bytepos < ZV_BYTE
10154 && (c = FETCH_CHAR (bytepos),
10155 c == ' ' || c == '\t'))
10156 ++bytepos;
0d09d1e6 10157
5f5c8ee5
GM
10158 return bytepos >= ZV_BYTE || c == '\n' || c == '\r';
10159}
31b24551 10160
545e04f6 10161
5f5c8ee5 10162/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 10163
5f5c8ee5
GM
10164void
10165highlight_trailing_whitespace (f, row)
10166 struct frame *f;
10167 struct glyph_row *row;
10168{
10169 int used = row->used[TEXT_AREA];
10170
10171 if (used)
10172 {
10173 struct glyph *start = row->glyphs[TEXT_AREA];
10174 struct glyph *glyph = start + used - 1;
10175
10176 /* Skip over the space glyph inserted to display the
10177 cursor at the end of a line. */
10178 if (glyph->type == CHAR_GLYPH
10179 && glyph->u.ch.code == ' '
10180 && glyph->object == 0)
10181 --glyph;
10182
10183 /* If last glyph is a space or stretch, and it's trailing
10184 whitespace, set the face of all trailing whitespace glyphs in
10185 IT->glyph_row to `trailing-whitespace'. */
10186 if (glyph >= start
10187 && BUFFERP (glyph->object)
10188 && (glyph->type == STRETCH_GLYPH
10189 || (glyph->type == CHAR_GLYPH
10190 && glyph->u.ch.code == ' '))
10191 && trailing_whitespace_p (glyph->charpos))
545e04f6 10192 {
5f5c8ee5
GM
10193 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10194 CHARSET_ASCII);
10195
10196 while (glyph >= start
10197 && BUFFERP (glyph->object)
10198 && (glyph->type == STRETCH_GLYPH
10199 || (glyph->type == CHAR_GLYPH
10200 && glyph->u.ch.code == ' ')))
545e04f6 10201 {
5f5c8ee5
GM
10202 if (glyph->type == STRETCH_GLYPH)
10203 glyph->u.stretch.face_id = face_id;
10204 else
10205 glyph->u.ch.face_id = face_id;
10206 --glyph;
545e04f6
KH
10207 }
10208 }
a2889657 10209 }
5f5c8ee5 10210}
a2889657 10211
5fcbb24d 10212
a2889657 10213
5f5c8ee5
GM
10214/* Construct the glyph row IT->glyph_row in the desired matrix of
10215 IT->w from text at the current position of IT. See dispextern.h
10216 for an overview of struct it. Value is non-zero if
10217 IT->glyph_row displays text, as opposed to a line displaying ZV
10218 only. */
10219
10220static int
10221display_line (it)
10222 struct it *it;
10223{
10224 struct glyph_row *row = it->glyph_row;
10225
10226 /* We always start displaying at hpos zero even if hscrolled. */
10227 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 10228
5f5c8ee5
GM
10229 /* We must not display in a row that's not a text row. */
10230 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10231 < it->w->desired_matrix->nrows);
12adba34 10232
5f5c8ee5
GM
10233 /* Is IT->w showing the region? */
10234 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 10235
5f5c8ee5
GM
10236 /* Clear the result glyph row and enable it. */
10237 prepare_desired_row (row);
12adba34 10238
5f5c8ee5
GM
10239 row->y = it->current_y;
10240 row->start = it->current;
10241 row->continuation_lines_width = it->continuation_lines_width;
10242 row->displays_text_p = 1;
10243
10244 /* Arrange the overlays nicely for our purposes. Usually, we call
10245 display_line on only one line at a time, in which case this
10246 can't really hurt too much, or we call it on lines which appear
10247 one after another in the buffer, in which case all calls to
10248 recenter_overlay_lists but the first will be pretty cheap. */
10249 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10250
10251#if NO_PROMPT_IN_BUFFER
10252 /* Show mini-buffer prompt, if at the beginning of a mini-buffer
10253 window. */
10254 if (MINI_WINDOW_P (it->w)
10255 && MATRIX_ROW_START_CHARPOS (row) == BEG
10256 && it->vpos == 0)
10257 {
10258 if (NILP (minibuf_prompt))
10259 minibuf_prompt_width = minibuf_prompt_pixel_width = 0;
a2889657 10260 else
a2889657 10261 {
5f5c8ee5
GM
10262 /* We would like to truncate the prompt a little bit before
10263 the right margin of the window, so that user input can
10264 start on the first line. Set max_x to this position. */
10265 int max_x = (it->last_visible_x - 4 * CANON_X_UNIT (it->f));
10266
10267 /* We use a temporary iterator different from IT so that
10268 IT's settings are not overwritten when displaying
10269 the prompt. */
10270 struct it ti;
10271
10272 ti = *it;
10273
10274 /* Display the prompt. Set minibuf_prompt_width to the
10275 number of glyphs generated for the prompt, set
10276 minibuf_prompt_pixel_width to its width in pixels. */
10277 xassert (it->current_x == 0);
10278 display_string (NULL, minibuf_prompt, Qnil, 0, 0, &ti,
10279 0, 0, max_x, -1);
10280 minibuf_prompt_width = ti.hpos;
10281 minibuf_prompt_pixel_width = ti.current_x;
10282
10283 /* Transfer pixel and hpos information to IT. */
10284 it->hpos = ti.hpos;
10285 it->current_x = ti.current_x;
10286 }
10287 }
10288#endif /* NO_PROMPT_IN_BUFFER */
10289
10290 /* Move over display elements that are not visible because we are
10291 hscrolled. This may stop at an x-position < IT->first_visible_x
10292 if the first glyph is partially visible or if we hit a line end. */
10293 if (it->current_x < it->first_visible_x)
10294 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10295 MOVE_TO_POS | MOVE_TO_X);
10296
10297 /* Get the initial row height. This is either the height of the
10298 text hscrolled, if there is any, or zero. */
10299 row->ascent = it->max_ascent;
10300 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10301 row->phys_ascent = it->max_phys_ascent;
10302 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10303
10304 /* Loop generating characters. The loop is left with IT on the next
10305 character to display. */
10306 while (1)
10307 {
10308 int n_glyphs_before, hpos_before, x_before;
10309 int x, i, nglyphs;
10310
10311 /* Retrieve the next thing to display. Value is zero if end of
10312 buffer reached. */
10313 if (!get_next_display_element (it))
10314 {
10315 /* Maybe add a space at the end of this line that is used to
10316 display the cursor there under X. */
10317 append_space (it, 1);
10318
10319 /* The position -1 below indicates a blank line not
10320 corresponding to any text, as opposed to an empty line
10321 corresponding to a line end. */
10322 if (row->used[TEXT_AREA] <= 1)
a2889657 10323 {
5f5c8ee5
GM
10324 row->glyphs[TEXT_AREA]->charpos = -1;
10325 row->displays_text_p = 0;
10326
10327 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
10328 row->indicate_empty_line_p = 1;
a2889657 10329 }
5f5c8ee5
GM
10330
10331 it->continuation_lines_width = 0;
10332 row->ends_at_zv_p = 1;
10333 break;
a2889657 10334 }
a2889657 10335
5f5c8ee5
GM
10336 /* Now, get the metrics of what we want to display. This also
10337 generates glyphs in `row' (which is IT->glyph_row). */
10338 n_glyphs_before = row->used[TEXT_AREA];
10339 x = it->current_x;
10340 PRODUCE_GLYPHS (it);
a2889657 10341
5f5c8ee5
GM
10342 /* If this display element was in marginal areas, continue with
10343 the next one. */
10344 if (it->area != TEXT_AREA)
a2889657 10345 {
5f5c8ee5
GM
10346 row->ascent = max (row->ascent, it->max_ascent);
10347 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10348 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10349 row->phys_height = max (row->phys_height,
10350 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10351 set_iterator_to_next (it);
10352 continue;
10353 }
5936754e 10354
5f5c8ee5
GM
10355 /* Does the display element fit on the line? If we truncate
10356 lines, we should draw past the right edge of the window. If
10357 we don't truncate, we want to stop so that we can display the
10358 continuation glyph before the right margin. If lines are
10359 continued, there are two possible strategies for characters
10360 resulting in more than 1 glyph (e.g. tabs): Display as many
10361 glyphs as possible in this line and leave the rest for the
10362 continuation line, or display the whole element in the next
10363 line. Original redisplay did the former, so we do it also. */
10364 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10365 hpos_before = it->hpos;
10366 x_before = x;
10367
10368 if (nglyphs == 1
10369 && it->current_x < it->last_visible_x)
10370 {
10371 ++it->hpos;
10372 row->ascent = max (row->ascent, it->max_ascent);
10373 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10374 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10375 row->phys_height = max (row->phys_height,
10376 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10377 if (it->current_x - it->pixel_width < it->first_visible_x)
10378 row->x = x - it->first_visible_x;
10379 }
10380 else
10381 {
10382 int new_x;
10383 struct glyph *glyph;
10384
10385 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 10386 {
5f5c8ee5
GM
10387 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10388 new_x = x + glyph->pixel_width;
10389
10390 if (/* Lines are continued. */
10391 !it->truncate_lines_p
10392 && (/* Glyph doesn't fit on the line. */
10393 new_x > it->last_visible_x
10394 /* Or it fits exactly on a window system frame. */
10395 || (new_x == it->last_visible_x
10396 && FRAME_WINDOW_P (it->f))))
a2889657 10397 {
5f5c8ee5
GM
10398 /* End of a continued line. */
10399
10400 if (it->hpos == 0
10401 || (new_x == it->last_visible_x
10402 && FRAME_WINDOW_P (it->f)))
10403 {
10404 /* Current glyph fits exactly on the line. We
10405 must continue the line because we can't draw
10406 the cursor after the glyph. */
10407 row->continued_p = 1;
10408 it->current_x = new_x;
10409 it->continuation_lines_width += new_x;
10410 ++it->hpos;
10411 if (i == nglyphs - 1)
10412 set_iterator_to_next (it);
10413 }
10414 else
5936754e 10415 {
5f5c8ee5
GM
10416 /* Display element draws past the right edge of
10417 the window. Restore positions to values
10418 before the element. The next line starts
10419 with current_x before the glyph that could
10420 not be displayed, so that TAB works right. */
10421 row->used[TEXT_AREA] = n_glyphs_before + i;
10422
10423 /* Display continuation glyphs. */
10424 if (!FRAME_WINDOW_P (it->f))
10425 produce_special_glyphs (it, IT_CONTINUATION);
10426 row->continued_p = 1;
10427
10428 it->current_x = x;
10429 it->continuation_lines_width += x;
5936754e 10430 }
5f5c8ee5
GM
10431 break;
10432 }
10433 else if (new_x > it->first_visible_x)
10434 {
10435 /* Increment number of glyphs actually displayed. */
10436 ++it->hpos;
10437
10438 if (x < it->first_visible_x)
10439 /* Glyph is partially visible, i.e. row starts at
10440 negative X position. */
10441 row->x = x - it->first_visible_x;
10442 }
10443 else
10444 {
10445 /* Glyph is completely off the left margin of the
10446 window. This should not happen because of the
10447 move_it_in_display_line at the start of
10448 this function. */
10449 abort ();
a2889657 10450 }
a2889657 10451 }
5f5c8ee5
GM
10452
10453 row->ascent = max (row->ascent, it->max_ascent);
10454 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
10455 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10456 row->phys_height = max (row->phys_height,
10457 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
10458
10459 /* End of this display line if row is continued. */
10460 if (row->continued_p)
10461 break;
a2889657 10462 }
a2889657 10463
5f5c8ee5
GM
10464 /* Is this a line end? If yes, we're also done, after making
10465 sure that a non-default face is extended up to the right
10466 margin of the window. */
10467 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 10468 {
5f5c8ee5
GM
10469 int used_before = row->used[TEXT_AREA];
10470
10471 /* Add a space at the end of the line that is used to
10472 display the cursor there. */
10473 append_space (it, 0);
10474
10475 /* Extend the face to the end of the line. */
10476 extend_face_to_end_of_line (it);
10477
10478 /* Make sure we have the position. */
10479 if (used_before == 0)
10480 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
10481
10482 /* Consume the line end. This skips over invisible lines. */
10483 set_iterator_to_next (it);
10484 it->continuation_lines_width = 0;
10485 break;
1c9241f5 10486 }
a2889657 10487
5f5c8ee5
GM
10488 /* Proceed with next display element. Note that this skips
10489 over lines invisible because of selective display. */
10490 set_iterator_to_next (it);
b1d1124b 10491
5f5c8ee5
GM
10492 /* If we truncate lines, we are done when the last displayed
10493 glyphs reach past the right margin of the window. */
10494 if (it->truncate_lines_p
10495 && (FRAME_WINDOW_P (it->f)
10496 ? (it->current_x >= it->last_visible_x)
10497 : (it->current_x > it->last_visible_x)))
75d13c64 10498 {
5f5c8ee5
GM
10499 /* Maybe add truncation glyphs. */
10500 if (!FRAME_WINDOW_P (it->f))
10501 {
10502 --it->glyph_row->used[TEXT_AREA];
10503 produce_special_glyphs (it, IT_TRUNCATION);
10504 }
10505
10506 row->truncated_on_right_p = 1;
10507 it->continuation_lines_width = 0;
312246d1 10508 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
10509 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
10510 it->hpos = hpos_before;
10511 it->current_x = x_before;
10512 break;
75d13c64 10513 }
a2889657 10514 }
a2889657 10515
5f5c8ee5
GM
10516 /* If line is not empty and hscrolled, maybe insert truncation glyphs
10517 at the left window margin. */
10518 if (it->first_visible_x
10519 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
10520 {
10521 if (!FRAME_WINDOW_P (it->f))
10522 insert_left_trunc_glyphs (it);
10523 row->truncated_on_left_p = 1;
10524 }
a2889657 10525
5f5c8ee5
GM
10526 /* If the start of this line is the overlay arrow-position, then
10527 mark this glyph row as the one containing the overlay arrow.
10528 This is clearly a mess with variable size fonts. It would be
10529 better to let it be displayed like cursors under X. */
e24c997d 10530 if (MARKERP (Voverlay_arrow_position)
a2889657 10531 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
10532 && (MATRIX_ROW_START_CHARPOS (row)
10533 == marker_position (Voverlay_arrow_position))
e24c997d 10534 && STRINGP (Voverlay_arrow_string)
a2889657
JB
10535 && ! overlay_arrow_seen)
10536 {
5f5c8ee5
GM
10537 /* Overlay arrow in window redisplay is a bitmap. */
10538 if (!FRAME_WINDOW_P (it->f))
c4628384 10539 {
5f5c8ee5
GM
10540 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
10541 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
10542 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
10543 struct glyph *p = row->glyphs[TEXT_AREA];
10544 struct glyph *p2, *end;
10545
10546 /* Copy the arrow glyphs. */
10547 while (glyph < arrow_end)
10548 *p++ = *glyph++;
10549
10550 /* Throw away padding glyphs. */
10551 p2 = p;
10552 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
10553 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
10554 ++p2;
10555 if (p2 > p)
212e4f87 10556 {
5f5c8ee5
GM
10557 while (p2 < end)
10558 *p++ = *p2++;
10559 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 10560 }
c4628384 10561 }
5f5c8ee5 10562
a2889657 10563 overlay_arrow_seen = 1;
5f5c8ee5 10564 row->overlay_arrow_p = 1;
a2889657
JB
10565 }
10566
5f5c8ee5
GM
10567 /* Compute pixel dimensions of this line. */
10568 compute_line_metrics (it);
10569
10570 /* Remember the position at which this line ends. */
10571 row->end = it->current;
10572
10573 /* Maybe set the cursor. If you change this, it's probably a good
10574 idea to also change the code in redisplay_window for cursor
10575 movement in an unchanged window. */
10576 if (it->w->cursor.vpos < 0
10577 && PT >= MATRIX_ROW_START_CHARPOS (row)
10578 && MATRIX_ROW_END_CHARPOS (row) >= PT
10579 && !(MATRIX_ROW_END_CHARPOS (row) == PT
10580 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10581 || !row->ends_at_zv_p)))
10582 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
10583
10584 /* Highlight trailing whitespace. */
10585 if (it->show_trailing_whitespace_p)
10586 highlight_trailing_whitespace (it->f, it->glyph_row);
10587
10588 /* Prepare for the next line. This line starts horizontally at (X
10589 HPOS) = (0 0). Vertical positions are incremented. As a
10590 convenience for the caller, IT->glyph_row is set to the next
10591 row to be used. */
10592 it->current_x = it->hpos = 0;
10593 it->current_y += row->height;
10594 ++it->vpos;
10595 ++it->glyph_row;
10596 return row->displays_text_p;
a2889657 10597}
5f5c8ee5
GM
10598
10599
a2889657 10600\f
5f5c8ee5
GM
10601/***********************************************************************
10602 Menu Bar
10603 ***********************************************************************/
10604
10605/* Redisplay the menu bar in the frame for window W.
10606
10607 The menu bar of X frames that don't have X toolkit support is
10608 displayed in a special window W->frame->menu_bar_window.
10609
10610 The menu bar of terminal frames is treated specially as far as
10611 glyph matrices are concerned. Menu bar lines are not part of
10612 windows, so the update is done directly on the frame matrix rows
10613 for the menu bar. */
7ce2c095
RS
10614
10615static void
10616display_menu_bar (w)
10617 struct window *w;
10618{
5f5c8ee5
GM
10619 struct frame *f = XFRAME (WINDOW_FRAME (w));
10620 struct it it;
10621 Lisp_Object items;
8351baf2 10622 int i;
7ce2c095 10623
5f5c8ee5 10624 /* Don't do all this for graphical frames. */
dc937613 10625#ifdef HAVE_NTGUI
d129c4c2
KH
10626 if (!NILP (Vwindow_system))
10627 return;
dc937613 10628#endif
dc937613 10629#ifdef USE_X_TOOLKIT
d3413a53 10630 if (FRAME_X_P (f))
7ce2c095 10631 return;
5f5c8ee5
GM
10632#endif
10633
10634#ifdef USE_X_TOOLKIT
10635 xassert (!FRAME_WINDOW_P (f));
10636 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
10637 it.first_visible_x = 0;
10638 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
10639#else /* not USE_X_TOOLKIT */
10640 if (FRAME_WINDOW_P (f))
10641 {
10642 /* Menu bar lines are displayed in the desired matrix of the
10643 dummy window menu_bar_window. */
10644 struct window *menu_w;
10645 xassert (WINDOWP (f->menu_bar_window));
10646 menu_w = XWINDOW (f->menu_bar_window);
10647 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
10648 MODE_LINE_FACE_ID);
10649 it.first_visible_x = 0;
10650 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
10651 }
10652 else
10653 {
10654 /* This is a TTY frame, i.e. character hpos/vpos are used as
10655 pixel x/y. */
10656 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
10657 MODE_LINE_FACE_ID);
10658 it.first_visible_x = 0;
10659 it.last_visible_x = FRAME_WIDTH (f);
10660 }
10661#endif /* not USE_X_TOOLKIT */
10662
10663 /* Clear all rows of the menu bar. */
10664 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
10665 {
10666 struct glyph_row *row = it.glyph_row + i;
10667 clear_glyph_row (row);
10668 row->enabled_p = 1;
10669 row->full_width_p = 1;
10670 }
7ce2c095 10671
5f5c8ee5
GM
10672 /* Make the first line of the menu bar appear in reverse video. */
10673 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 10674
5f5c8ee5
GM
10675 /* Display all items of the menu bar. */
10676 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 10677 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 10678 {
5f5c8ee5
GM
10679 Lisp_Object string;
10680
10681 /* Stop at nil string. */
8351baf2
RS
10682 string = XVECTOR (items)->contents[i + 1];
10683 if (NILP (string))
10684 break;
2d66ad19 10685
5f5c8ee5
GM
10686 /* Remember where item was displayed. */
10687 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 10688
5f5c8ee5
GM
10689 /* Display the item, pad with one space. */
10690 if (it.current_x < it.last_visible_x)
10691 display_string (NULL, string, Qnil, 0, 0, &it,
10692 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
10693 }
10694
2d66ad19 10695 /* Fill out the line with spaces. */
5f5c8ee5
GM
10696 if (it.current_x < it.last_visible_x)
10697 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 10698
5f5c8ee5
GM
10699 /* Compute the total height of the lines. */
10700 compute_line_metrics (&it);
7ce2c095 10701}
5f5c8ee5
GM
10702
10703
7ce2c095 10704\f
5f5c8ee5
GM
10705/***********************************************************************
10706 Mode Line
10707 ***********************************************************************/
10708
10709/* Display the mode and/or top line of window W. */
a2889657
JB
10710
10711static void
5f5c8ee5 10712display_mode_lines (w)
a2889657
JB
10713 struct window *w;
10714{
5f5c8ee5 10715 /* These will be set while the mode line specs are processed. */
aa6d10fa 10716 line_number_displayed = 0;
155ef550 10717 w->column_number_displayed = Qnil;
aa6d10fa 10718
5f5c8ee5
GM
10719 if (WINDOW_WANTS_MODELINE_P (w))
10720 display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
10721
10722 if (WINDOW_WANTS_TOP_LINE_P (w))
10723 display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
10724}
03b294dc 10725
03b294dc 10726
5f5c8ee5
GM
10727/* Display mode or top line of window W. FACE_ID specifies which line
10728 to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
10729 FORMAT is the mode line format to display. */
03b294dc 10730
5f5c8ee5
GM
10731static void
10732display_mode_line (w, face_id, format)
10733 struct window *w;
10734 enum face_id face_id;
10735 Lisp_Object format;
10736{
10737 struct it it;
10738 struct face *face;
03b294dc 10739
5f5c8ee5
GM
10740 init_iterator (&it, w, -1, -1, NULL, face_id);
10741 prepare_desired_row (it.glyph_row);
10742
10743 /* Temporarily make frame's keyboard the current kboard so that
10744 kboard-local variables in the mode_line_format will get the right
10745 values. */
10746 push_frame_kboard (it.f);
10747 display_mode_element (&it, 0, 0, 0, format);
10748 pop_frame_kboard ();
a2889657 10749
5f5c8ee5
GM
10750 /* Fill up with spaces. */
10751 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
10752
10753 compute_line_metrics (&it);
10754 it.glyph_row->full_width_p = 1;
10755 it.glyph_row->mode_line_p = 1;
10756 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
10757 it.glyph_row->continued_p = 0;
10758 it.glyph_row->truncated_on_left_p = 0;
10759 it.glyph_row->truncated_on_right_p = 0;
10760
10761 /* Make a 3D mode-line have a shadow at its right end. */
10762 face = FACE_FROM_ID (it.f, face_id);
10763 extend_face_to_end_of_line (&it);
10764 if (face->box != FACE_NO_BOX)
d7eb09a0 10765 {
5f5c8ee5
GM
10766 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
10767 + it.glyph_row->used[TEXT_AREA] - 1);
10768 last->right_box_line_p = 1;
d7eb09a0 10769 }
a2889657
JB
10770}
10771
a2889657 10772
5f5c8ee5
GM
10773/* Contribute ELT to the mode line for window IT->w. How it
10774 translates into text depends on its data type.
a2889657 10775
5f5c8ee5 10776 IT describes the display environment in which we display, as usual.
a2889657
JB
10777
10778 DEPTH is the depth in recursion. It is used to prevent
10779 infinite recursion here.
10780
5f5c8ee5
GM
10781 FIELD_WIDTH is the number of characters the display of ELT should
10782 occupy in the mode line, and PRECISION is the maximum number of
10783 characters to display from ELT's representation. See
10784 display_string for details. *
a2889657 10785
5f5c8ee5 10786 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
10787
10788static int
5f5c8ee5
GM
10789display_mode_element (it, depth, field_width, precision, elt)
10790 struct it *it;
a2889657 10791 int depth;
5f5c8ee5
GM
10792 int field_width, precision;
10793 Lisp_Object elt;
a2889657 10794{
5f5c8ee5
GM
10795 int n = 0, field, prec;
10796
a2889657
JB
10797 tail_recurse:
10798 if (depth > 10)
10799 goto invalid;
10800
10801 depth++;
10802
0220c518 10803 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
10804 {
10805 case Lisp_String:
10806 {
10807 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
10808 unsigned char c;
10809 unsigned char *this = XSTRING (elt)->data;
10810 unsigned char *lisp_string = this;
10811
10812 while ((precision <= 0 || n < precision)
10813 && *this
10814 && (frame_title_ptr
10815 || it->current_x < it->last_visible_x))
a2889657
JB
10816 {
10817 unsigned char *last = this;
5f5c8ee5
GM
10818
10819 /* Advance to end of string or next format specifier. */
a2889657
JB
10820 while ((c = *this++) != '\0' && c != '%')
10821 ;
5f5c8ee5 10822
a2889657
JB
10823 if (this - 1 != last)
10824 {
5f5c8ee5
GM
10825 /* Output to end of string or up to '%'. Field width
10826 is length of string. Don't output more than
10827 PRECISION allows us. */
10828 prec = --this - last;
10829 if (precision > 0 && prec > precision - n)
10830 prec = precision - n;
10831
d39b6696 10832 if (frame_title_ptr)
5f5c8ee5 10833 n += store_frame_title (last, prec, prec);
d39b6696 10834 else
5f5c8ee5
GM
10835 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
10836 it, 0, prec, 0, -1);
a2889657
JB
10837 }
10838 else /* c == '%' */
10839 {
5f5c8ee5
GM
10840 unsigned char *percent_position = this;
10841
10842 /* Get the specified minimum width. Zero means
10843 don't pad. */
10844 field = 0;
a2889657 10845 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 10846 field = field * 10 + c - '0';
a2889657 10847
5f5c8ee5
GM
10848 /* Don't pad beyond the total padding allowed. */
10849 if (field_width - n > 0 && field > field_width - n)
10850 field = field_width - n;
a2889657 10851
5f5c8ee5
GM
10852 /* Note that either PRECISION <= 0 or N < PRECISION. */
10853 prec = precision - n;
10854
a2889657 10855 if (c == 'M')
5f5c8ee5
GM
10856 n += display_mode_element (it, depth, field, prec,
10857 Vglobal_mode_string);
a2889657 10858 else if (c != 0)
d39b6696 10859 {
5f5c8ee5
GM
10860 unsigned char *spec
10861 = decode_mode_spec (it->w, c, field, prec);
10862
d39b6696 10863 if (frame_title_ptr)
5f5c8ee5 10864 n += store_frame_title (spec, field, prec);
d39b6696 10865 else
5f5c8ee5
GM
10866 {
10867 int nglyphs_before
10868 = it->glyph_row->used[TEXT_AREA];
10869 int charpos
10870 = percent_position - XSTRING (elt)->data;
10871 int nwritten
10872 = display_string (spec, Qnil, elt, charpos, 0, it,
10873 field, prec, 0, -1);
10874
10875 /* Assign to the glyphs written above the
10876 string where the `%x' came from, position
10877 of the `%'. */
10878 if (nwritten > 0)
10879 {
10880 struct glyph *glyph
10881 = (it->glyph_row->glyphs[TEXT_AREA]
10882 + nglyphs_before);
10883 int i;
10884
10885 for (i = 0; i < nwritten; ++i)
10886 {
10887 glyph[i].object = elt;
10888 glyph[i].charpos = charpos;
10889 }
10890
10891 n += nwritten;
10892 }
10893 }
d39b6696 10894 }
a2889657
JB
10895 }
10896 }
10897 }
10898 break;
10899
10900 case Lisp_Symbol:
10901 /* A symbol: process the value of the symbol recursively
10902 as if it appeared here directly. Avoid error if symbol void.
10903 Special case: if value of symbol is a string, output the string
10904 literally. */
10905 {
10906 register Lisp_Object tem;
10907 tem = Fboundp (elt);
265a9e55 10908 if (!NILP (tem))
a2889657
JB
10909 {
10910 tem = Fsymbol_value (elt);
10911 /* If value is a string, output that string literally:
10912 don't check for % within it. */
e24c997d 10913 if (STRINGP (tem))
d39b6696 10914 {
5f5c8ee5
GM
10915 prec = XSTRING (tem)->size;
10916 if (precision > 0 && prec > precision - n)
10917 prec = precision - n;
d39b6696 10918 if (frame_title_ptr)
5f5c8ee5 10919 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 10920 else
5f5c8ee5
GM
10921 n += display_string (NULL, tem, Qnil, 0, 0, it,
10922 0, prec, 0, -1);
d39b6696 10923 }
a2889657 10924 else if (!EQ (tem, elt))
5f5c8ee5
GM
10925 {
10926 /* Give up right away for nil or t. */
10927 elt = tem;
10928 goto tail_recurse;
10929 }
a2889657
JB
10930 }
10931 }
10932 break;
10933
10934 case Lisp_Cons:
10935 {
10936 register Lisp_Object car, tem;
10937
10938 /* A cons cell: three distinct cases.
10939 If first element is a string or a cons, process all the elements
10940 and effectively concatenate them.
10941 If first element is a negative number, truncate displaying cdr to
10942 at most that many characters. If positive, pad (with spaces)
10943 to at least that many characters.
10944 If first element is a symbol, process the cadr or caddr recursively
10945 according to whether the symbol's value is non-nil or nil. */
10946 car = XCONS (elt)->car;
5f5c8ee5
GM
10947 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
10948 {
10949 /* An element of the form (:eval FORM) means evaluate FORM
10950 and use the result as mode line elements. */
10951 struct gcpro gcpro1;
10952 Lisp_Object spec;
10953
10954 spec = eval_form (XCAR (XCDR (elt)));
10955 GCPRO1 (spec);
10956 n += display_mode_element (it, depth, field_width - n,
10957 precision - n, spec);
10958 UNGCPRO;
10959 }
10960 else if (SYMBOLP (car))
a2889657
JB
10961 {
10962 tem = Fboundp (car);
10963 elt = XCONS (elt)->cdr;
e24c997d 10964 if (!CONSP (elt))
a2889657
JB
10965 goto invalid;
10966 /* elt is now the cdr, and we know it is a cons cell.
10967 Use its car if CAR has a non-nil value. */
265a9e55 10968 if (!NILP (tem))
a2889657
JB
10969 {
10970 tem = Fsymbol_value (car);
265a9e55 10971 if (!NILP (tem))
a2889657
JB
10972 { elt = XCONS (elt)->car; goto tail_recurse; }
10973 }
10974 /* Symbol's value is nil (or symbol is unbound)
10975 Get the cddr of the original list
10976 and if possible find the caddr and use that. */
10977 elt = XCONS (elt)->cdr;
265a9e55 10978 if (NILP (elt))
a2889657 10979 break;
e24c997d 10980 else if (!CONSP (elt))
a2889657
JB
10981 goto invalid;
10982 elt = XCONS (elt)->car;
10983 goto tail_recurse;
10984 }
e24c997d 10985 else if (INTEGERP (car))
a2889657
JB
10986 {
10987 register int lim = XINT (car);
10988 elt = XCONS (elt)->cdr;
10989 if (lim < 0)
5f5c8ee5
GM
10990 {
10991 /* Negative int means reduce maximum width. */
10992 if (precision <= 0)
10993 precision = -lim;
10994 else
10995 precision = min (precision, -lim);
10996 }
a2889657
JB
10997 else if (lim > 0)
10998 {
10999 /* Padding specified. Don't let it be more than
11000 current maximum. */
5f5c8ee5
GM
11001 if (precision > 0)
11002 lim = min (precision, lim);
11003
a2889657
JB
11004 /* If that's more padding than already wanted, queue it.
11005 But don't reduce padding already specified even if
11006 that is beyond the current truncation point. */
5f5c8ee5 11007 field_width = max (lim, field_width);
a2889657
JB
11008 }
11009 goto tail_recurse;
11010 }
e24c997d 11011 else if (STRINGP (car) || CONSP (car))
a2889657
JB
11012 {
11013 register int limit = 50;
5f5c8ee5
GM
11014 /* Limit is to protect against circular lists. */
11015 while (CONSP (elt)
11016 && --limit > 0
11017 && (precision <= 0 || n < precision))
a2889657 11018 {
5f5c8ee5
GM
11019 n += display_mode_element (it, depth, field_width - n,
11020 precision - n, XCONS (elt)->car);
a2889657
JB
11021 elt = XCONS (elt)->cdr;
11022 }
11023 }
11024 }
11025 break;
11026
11027 default:
11028 invalid:
d39b6696 11029 if (frame_title_ptr)
5f5c8ee5 11030 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 11031 else
5f5c8ee5
GM
11032 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11033 precision - n, 0, 0);
11034 return n;
a2889657
JB
11035 }
11036
5f5c8ee5
GM
11037 /* Pad to FIELD_WIDTH. */
11038 if (field_width > 0 && n < field_width)
11039 {
11040 if (frame_title_ptr)
11041 n += store_frame_title ("", field_width - n, 0);
11042 else
11043 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11044 0, 0, 0);
11045 }
11046
11047 return n;
a2889657 11048}
5f5c8ee5
GM
11049
11050
766525bc
RS
11051/* Write a null-terminated, right justified decimal representation of
11052 the positive integer D to BUF using a minimal field width WIDTH. */
11053
11054static void
11055pint2str (buf, width, d)
11056 register char *buf;
11057 register int width;
11058 register int d;
11059{
11060 register char *p = buf;
11061
11062 if (d <= 0)
5f5c8ee5 11063 *p++ = '0';
766525bc 11064 else
5f5c8ee5 11065 {
766525bc 11066 while (d > 0)
5f5c8ee5 11067 {
766525bc
RS
11068 *p++ = d % 10 + '0';
11069 d /= 10;
5f5c8ee5
GM
11070 }
11071 }
11072
11073 for (width -= (int) (p - buf); width > 0; --width)
11074 *p++ = ' ';
766525bc
RS
11075 *p-- = '\0';
11076 while (p > buf)
5f5c8ee5 11077 {
766525bc
RS
11078 d = *buf;
11079 *buf++ = *p;
11080 *p-- = d;
5f5c8ee5 11081 }
766525bc
RS
11082}
11083
5f5c8ee5 11084/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
11085 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11086 type of CODING_SYSTEM. Return updated pointer into BUF. */
11087
6693a99a 11088static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 11089
1c9241f5
KH
11090static char *
11091decode_mode_spec_coding (coding_system, buf, eol_flag)
11092 Lisp_Object coding_system;
11093 register char *buf;
11094 int eol_flag;
11095{
1e1078d6 11096 Lisp_Object val;
916848d8 11097 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
11098 unsigned char *eol_str;
11099 int eol_str_len;
11100 /* The EOL conversion we are using. */
11101 Lisp_Object eoltype;
1e1078d6
RS
11102
11103 val = coding_system;
1c9241f5
KH
11104
11105 if (NILP (val)) /* Not yet decided. */
11106 {
916848d8
RS
11107 if (multibyte)
11108 *buf++ = '-';
21e989e3 11109 if (eol_flag)
302f2b38 11110 eoltype = eol_mnemonic_undecided;
1e1078d6 11111 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
11112 }
11113 else
11114 {
1e1078d6
RS
11115 Lisp_Object eolvalue;
11116
11117 eolvalue = Fget (coding_system, Qeol_type);
11118
1c9241f5 11119 while (!NILP (val) && SYMBOLP (val))
1e1078d6
RS
11120 {
11121 val = Fget (val, Qcoding_system);
11122 if (NILP (eolvalue))
b070c1d7 11123 eolvalue = Fget (val, Qeol_type);
1e1078d6
RS
11124 }
11125
916848d8
RS
11126 if (multibyte)
11127 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11128
1c9241f5
KH
11129 if (eol_flag)
11130 {
1e1078d6
RS
11131 /* The EOL conversion that is normal on this system. */
11132
11133 if (NILP (eolvalue)) /* Not yet decided. */
11134 eoltype = eol_mnemonic_undecided;
11135 else if (VECTORP (eolvalue)) /* Not yet decided. */
11136 eoltype = eol_mnemonic_undecided;
11137 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11138 eoltype = (XFASTINT (eolvalue) == 0
11139 ? eol_mnemonic_unix
11140 : (XFASTINT (eolvalue) == 1
11141 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
11142 }
11143 }
5f5c8ee5 11144
302f2b38
EZ
11145 if (eol_flag)
11146 {
11147 /* Mention the EOL conversion if it is not the usual one. */
11148 if (STRINGP (eoltype))
11149 {
11150 eol_str = XSTRING (eoltype)->data;
11151 eol_str_len = XSTRING (eoltype)->size;
11152 }
f30b3499
KH
11153 else if (INTEGERP (eoltype)
11154 && CHAR_VALID_P (XINT (eoltype), 0))
11155 {
11156 int c = XINT (eoltype);
11157 unsigned char work[4];
11158
11159 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11160 }
302f2b38
EZ
11161 else
11162 {
11163 eol_str = invalid_eol_type;
11164 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 11165 }
f30b3499 11166 bcopy (eol_str, buf, eol_str_len);
302f2b38 11167 buf += eol_str_len;
1c9241f5 11168 }
302f2b38 11169
1c9241f5
KH
11170 return buf;
11171}
11172
a2889657 11173/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
11174 generated by character C. PRECISION >= 0 means don't return a
11175 string longer than that value. FIELD_WIDTH > 0 means pad the
11176 string returned with spaces to that value. */
a2889657 11177
11e82b76
JB
11178static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11179
a2889657 11180static char *
5f5c8ee5 11181decode_mode_spec (w, c, field_width, precision)
a2889657
JB
11182 struct window *w;
11183 register char c;
5f5c8ee5 11184 int field_width, precision;
a2889657 11185{
0b67772d 11186 Lisp_Object obj;
5f5c8ee5
GM
11187 struct frame *f = XFRAME (WINDOW_FRAME (w));
11188 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 11189 struct buffer *b = XBUFFER (w->buffer);
a2889657 11190
0b67772d 11191 obj = Qnil;
a2889657
JB
11192
11193 switch (c)
11194 {
1af9f229
RS
11195 case '*':
11196 if (!NILP (b->read_only))
11197 return "%";
11198 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11199 return "*";
11200 return "-";
11201
11202 case '+':
11203 /* This differs from %* only for a modified read-only buffer. */
11204 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11205 return "*";
11206 if (!NILP (b->read_only))
11207 return "%";
11208 return "-";
11209
11210 case '&':
11211 /* This differs from %* in ignoring read-only-ness. */
11212 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11213 return "*";
11214 return "-";
11215
11216 case '%':
11217 return "%";
11218
11219 case '[':
11220 {
11221 int i;
11222 char *p;
11223
11224 if (command_loop_level > 5)
11225 return "[[[... ";
11226 p = decode_mode_spec_buf;
11227 for (i = 0; i < command_loop_level; i++)
11228 *p++ = '[';
11229 *p = 0;
11230 return decode_mode_spec_buf;
11231 }
11232
11233 case ']':
11234 {
11235 int i;
11236 char *p;
11237
11238 if (command_loop_level > 5)
11239 return " ...]]]";
11240 p = decode_mode_spec_buf;
11241 for (i = 0; i < command_loop_level; i++)
11242 *p++ = ']';
11243 *p = 0;
11244 return decode_mode_spec_buf;
11245 }
11246
11247 case '-':
11248 {
1af9f229 11249 register int i;
5f5c8ee5
GM
11250
11251 /* Let lots_of_dashes be a string of infinite length. */
11252 if (field_width <= 0
11253 || field_width > sizeof (lots_of_dashes))
1af9f229 11254 {
5f5c8ee5
GM
11255 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11256 decode_mode_spec_buf[i] = '-';
11257 decode_mode_spec_buf[i] = '\0';
11258 return decode_mode_spec_buf;
1af9f229 11259 }
5f5c8ee5
GM
11260 else
11261 return lots_of_dashes;
1af9f229
RS
11262 }
11263
a2889657 11264 case 'b':
d39b6696 11265 obj = b->name;
a2889657
JB
11266 break;
11267
1af9f229
RS
11268 case 'c':
11269 {
11270 int col = current_column ();
11271 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 11272 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
11273 return decode_mode_spec_buf;
11274 }
11275
11276 case 'F':
11277 /* %F displays the frame name. */
5f5c8ee5 11278 if (!NILP (f->title))
95184b48 11279 return (char *) XSTRING (f->title)->data;
fd8ff63d 11280 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 11281 return (char *) XSTRING (f->name)->data;
9c6da96f 11282 return "Emacs";
1af9f229 11283
a2889657 11284 case 'f':
d39b6696 11285 obj = b->filename;
a2889657
JB
11286 break;
11287
aa6d10fa
RS
11288 case 'l':
11289 {
12adba34
RS
11290 int startpos = XMARKER (w->start)->charpos;
11291 int startpos_byte = marker_byte_position (w->start);
11292 int line, linepos, linepos_byte, topline;
aa6d10fa 11293 int nlines, junk;
aa6d10fa
RS
11294 int height = XFASTINT (w->height);
11295
11296 /* If we decided that this buffer isn't suitable for line numbers,
11297 don't forget that too fast. */
11298 if (EQ (w->base_line_pos, w->buffer))
766525bc 11299 goto no_value;
5300fd39
RS
11300 /* But do forget it, if the window shows a different buffer now. */
11301 else if (BUFFERP (w->base_line_pos))
11302 w->base_line_pos = Qnil;
aa6d10fa
RS
11303
11304 /* If the buffer is very big, don't waste time. */
d39b6696 11305 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
11306 {
11307 w->base_line_pos = Qnil;
11308 w->base_line_number = Qnil;
766525bc 11309 goto no_value;
aa6d10fa
RS
11310 }
11311
11312 if (!NILP (w->base_line_number)
11313 && !NILP (w->base_line_pos)
12adba34 11314 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
11315 {
11316 line = XFASTINT (w->base_line_number);
11317 linepos = XFASTINT (w->base_line_pos);
12adba34 11318 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
11319 }
11320 else
11321 {
11322 line = 1;
d39b6696 11323 linepos = BUF_BEGV (b);
12adba34 11324 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
11325 }
11326
11327 /* Count lines from base line to window start position. */
12adba34
RS
11328 nlines = display_count_lines (linepos, linepos_byte,
11329 startpos_byte,
11330 startpos, &junk);
aa6d10fa
RS
11331
11332 topline = nlines + line;
11333
11334 /* Determine a new base line, if the old one is too close
11335 or too far away, or if we did not have one.
11336 "Too close" means it's plausible a scroll-down would
11337 go back past it. */
d39b6696 11338 if (startpos == BUF_BEGV (b))
aa6d10fa 11339 {
c2213350
KH
11340 XSETFASTINT (w->base_line_number, topline);
11341 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
11342 }
11343 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 11344 || linepos == BUF_BEGV (b))
aa6d10fa 11345 {
d39b6696 11346 int limit = BUF_BEGV (b);
12adba34 11347 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
11348 int position;
11349 int distance = (height * 2 + 30) * 200;
11350
11351 if (startpos - distance > limit)
12adba34
RS
11352 {
11353 limit = startpos - distance;
11354 limit_byte = CHAR_TO_BYTE (limit);
11355 }
aa6d10fa 11356
12adba34
RS
11357 nlines = display_count_lines (startpos, startpos_byte,
11358 limit_byte,
11359 - (height * 2 + 30),
aa6d10fa
RS
11360 &position);
11361 /* If we couldn't find the lines we wanted within
11362 200 chars per line,
11363 give up on line numbers for this window. */
12adba34 11364 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
11365 {
11366 w->base_line_pos = w->buffer;
11367 w->base_line_number = Qnil;
766525bc 11368 goto no_value;
aa6d10fa
RS
11369 }
11370
c2213350 11371 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 11372 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
11373 }
11374
11375 /* Now count lines from the start pos to point. */
12adba34
RS
11376 nlines = display_count_lines (startpos, startpos_byte,
11377 PT_BYTE, PT, &junk);
aa6d10fa
RS
11378
11379 /* Record that we did display the line number. */
11380 line_number_displayed = 1;
11381
11382 /* Make the string to show. */
5f5c8ee5 11383 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 11384 return decode_mode_spec_buf;
766525bc
RS
11385 no_value:
11386 {
11387 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
11388 int pad = field_width - 2;
11389 while (pad-- > 0)
11390 *p++ = ' ';
11391 *p++ = '?';
11392 *p = '?';
766525bc
RS
11393 return decode_mode_spec_buf;
11394 }
aa6d10fa
RS
11395 }
11396 break;
11397
a2889657 11398 case 'm':
d39b6696 11399 obj = b->mode_name;
a2889657
JB
11400 break;
11401
11402 case 'n':
d39b6696 11403 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
11404 return " Narrow";
11405 break;
11406
a2889657
JB
11407 case 'p':
11408 {
11409 int pos = marker_position (w->start);
d39b6696 11410 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 11411
d39b6696 11412 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 11413 {
d39b6696 11414 if (pos <= BUF_BEGV (b))
a2889657
JB
11415 return "All";
11416 else
11417 return "Bottom";
11418 }
d39b6696 11419 else if (pos <= BUF_BEGV (b))
a2889657
JB
11420 return "Top";
11421 else
11422 {
3c7d31b9
RS
11423 if (total > 1000000)
11424 /* Do it differently for a large value, to avoid overflow. */
11425 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11426 else
11427 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
11428 /* We can't normally display a 3-digit number,
11429 so get us a 2-digit number that is close. */
11430 if (total == 100)
11431 total = 99;
11432 sprintf (decode_mode_spec_buf, "%2d%%", total);
11433 return decode_mode_spec_buf;
11434 }
11435 }
11436
8ffcb79f
RS
11437 /* Display percentage of size above the bottom of the screen. */
11438 case 'P':
11439 {
11440 int toppos = marker_position (w->start);
d39b6696
KH
11441 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
11442 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 11443
d39b6696 11444 if (botpos >= BUF_ZV (b))
8ffcb79f 11445 {
d39b6696 11446 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
11447 return "All";
11448 else
11449 return "Bottom";
11450 }
11451 else
11452 {
3c7d31b9
RS
11453 if (total > 1000000)
11454 /* Do it differently for a large value, to avoid overflow. */
11455 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11456 else
11457 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
11458 /* We can't normally display a 3-digit number,
11459 so get us a 2-digit number that is close. */
11460 if (total == 100)
11461 total = 99;
d39b6696 11462 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
11463 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
11464 else
11465 sprintf (decode_mode_spec_buf, "%2d%%", total);
11466 return decode_mode_spec_buf;
11467 }
11468 }
11469
1af9f229
RS
11470 case 's':
11471 /* status of process */
11472 obj = Fget_buffer_process (w->buffer);
11473 if (NILP (obj))
11474 return "no process";
11475#ifdef subprocesses
11476 obj = Fsymbol_name (Fprocess_status (obj));
11477#endif
11478 break;
d39b6696 11479
1af9f229
RS
11480 case 't': /* indicate TEXT or BINARY */
11481#ifdef MODE_LINE_BINARY_TEXT
11482 return MODE_LINE_BINARY_TEXT (b);
11483#else
11484 return "T";
11485#endif
1c9241f5
KH
11486
11487 case 'z':
11488 /* coding-system (not including end-of-line format) */
11489 case 'Z':
11490 /* coding-system (including end-of-line type) */
11491 {
11492 int eol_flag = (c == 'Z');
539b4d41 11493 char *p = decode_mode_spec_buf;
1c9241f5 11494
d30e754b 11495 if (! FRAME_WINDOW_P (f))
1c9241f5 11496 {
11c52c4f
RS
11497 /* No need to mention EOL here--the terminal never needs
11498 to do EOL conversion. */
11499 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
11500 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 11501 }
f13c925f 11502 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 11503 p, eol_flag);
f13c925f 11504
11c52c4f 11505#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
11506#ifdef subprocesses
11507 obj = Fget_buffer_process (Fcurrent_buffer ());
11508 if (PROCESSP (obj))
11509 {
11510 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
11511 p, eol_flag);
11512 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
11513 p, eol_flag);
11514 }
11515#endif /* subprocesses */
11c52c4f 11516#endif /* 0 */
1c9241f5
KH
11517 *p = 0;
11518 return decode_mode_spec_buf;
11519 }
a2889657 11520 }
d39b6696 11521
e24c997d 11522 if (STRINGP (obj))
a2889657
JB
11523 return (char *) XSTRING (obj)->data;
11524 else
11525 return "";
11526}
5f5c8ee5
GM
11527
11528
12adba34
RS
11529/* Count up to COUNT lines starting from START / START_BYTE.
11530 But don't go beyond LIMIT_BYTE.
11531 Return the number of lines thus found (always nonnegative).
59b49f63 11532
12adba34 11533 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
11534
11535static int
12adba34
RS
11536display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
11537 int start, start_byte, limit_byte, count;
11538 int *byte_pos_ptr;
59b49f63 11539{
59b49f63
RS
11540 register unsigned char *cursor;
11541 unsigned char *base;
11542
11543 register int ceiling;
11544 register unsigned char *ceiling_addr;
12adba34 11545 int orig_count = count;
59b49f63
RS
11546
11547 /* If we are not in selective display mode,
11548 check only for newlines. */
12adba34
RS
11549 int selective_display = (!NILP (current_buffer->selective_display)
11550 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
11551
11552 if (count > 0)
12adba34
RS
11553 {
11554 while (start_byte < limit_byte)
11555 {
11556 ceiling = BUFFER_CEILING_OF (start_byte);
11557 ceiling = min (limit_byte - 1, ceiling);
11558 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
11559 base = (cursor = BYTE_POS_ADDR (start_byte));
11560 while (1)
11561 {
11562 if (selective_display)
11563 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
11564 ;
11565 else
11566 while (*cursor != '\n' && ++cursor != ceiling_addr)
11567 ;
11568
11569 if (cursor != ceiling_addr)
11570 {
11571 if (--count == 0)
11572 {
11573 start_byte += cursor - base + 1;
11574 *byte_pos_ptr = start_byte;
11575 return orig_count;
11576 }
11577 else
11578 if (++cursor == ceiling_addr)
11579 break;
11580 }
11581 else
11582 break;
11583 }
11584 start_byte += cursor - base;
11585 }
11586 }
59b49f63
RS
11587 else
11588 {
12adba34
RS
11589 while (start_byte > limit_byte)
11590 {
11591 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
11592 ceiling = max (limit_byte, ceiling);
11593 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
11594 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
11595 while (1)
11596 {
12adba34
RS
11597 if (selective_display)
11598 while (--cursor != ceiling_addr
11599 && *cursor != '\n' && *cursor != 015)
11600 ;
11601 else
11602 while (--cursor != ceiling_addr && *cursor != '\n')
11603 ;
11604
59b49f63
RS
11605 if (cursor != ceiling_addr)
11606 {
11607 if (++count == 0)
11608 {
12adba34
RS
11609 start_byte += cursor - base + 1;
11610 *byte_pos_ptr = start_byte;
11611 /* When scanning backwards, we should
11612 not count the newline posterior to which we stop. */
11613 return - orig_count - 1;
59b49f63
RS
11614 }
11615 }
11616 else
11617 break;
11618 }
12adba34
RS
11619 /* Here we add 1 to compensate for the last decrement
11620 of CURSOR, which took it past the valid range. */
11621 start_byte += cursor - base + 1;
59b49f63
RS
11622 }
11623 }
11624
12adba34 11625 *byte_pos_ptr = limit_byte;
aa6d10fa 11626
12adba34
RS
11627 if (count < 0)
11628 return - orig_count + count;
11629 return orig_count - count;
aa6d10fa 11630
12adba34 11631}
a2889657 11632
a2889657 11633
5f5c8ee5
GM
11634\f
11635/***********************************************************************
11636 Displaying strings
11637 ***********************************************************************/
278feba9 11638
5f5c8ee5 11639/* Display a NUL-terminated string, starting with index START.
a3788d53 11640
5f5c8ee5
GM
11641 If STRING is non-null, display that C string. Otherwise, the Lisp
11642 string LISP_STRING is displayed.
a2889657 11643
5f5c8ee5
GM
11644 If FACE_STRING is not nil, FACE_STRING_POS is a position in
11645 FACE_STRING. Display STRING or LISP_STRING with the face at
11646 FACE_STRING_POS in FACE_STRING:
a2889657 11647
5f5c8ee5
GM
11648 Display the string in the environment given by IT, but use the
11649 standard display table, temporarily.
a3788d53 11650
5f5c8ee5
GM
11651 FIELD_WIDTH is the minimum number of output glyphs to produce.
11652 If STRING has fewer characters than FIELD_WIDTH, pad to the right
11653 with spaces. If STRING has more characters, more than FIELD_WIDTH
11654 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
11655
11656 PRECISION is the maximum number of characters to output from
11657 STRING. PRECISION < 0 means don't truncate the string.
a2889657 11658
5f5c8ee5 11659 This is roughly equivalent to printf format specifiers:
a2889657 11660
5f5c8ee5
GM
11661 FIELD_WIDTH PRECISION PRINTF
11662 ----------------------------------------
11663 -1 -1 %s
11664 -1 10 %.10s
11665 10 -1 %10s
11666 20 10 %20.10s
a2889657 11667
5f5c8ee5
GM
11668 MULTIBYTE zero means do not display multibyte chars, > 0 means do
11669 display them, and < 0 means obey the current buffer's value of
11670 enable_multibyte_characters.
278feba9 11671
5f5c8ee5 11672 Value is the number of glyphs produced. */
b1d1124b 11673
5f5c8ee5
GM
11674static int
11675display_string (string, lisp_string, face_string, face_string_pos,
11676 start, it, field_width, precision, max_x, multibyte)
11677 unsigned char *string;
11678 Lisp_Object lisp_string;
11679 int start;
11680 struct it *it;
11681 int field_width, precision, max_x;
11682 int multibyte;
11683{
11684 int hpos_at_start = it->hpos;
11685 int saved_face_id = it->face_id;
11686 struct glyph_row *row = it->glyph_row;
11687
11688 /* Initialize the iterator IT for iteration over STRING beginning
11689 with index START. We assume that IT may be modified here (which
11690 means that display_line has to do something when displaying a
11691 mini-buffer prompt, which it does). */
11692 reseat_to_string (it, string, lisp_string, start,
11693 precision, field_width, multibyte);
11694
11695 /* If displaying STRING, set up the face of the iterator
11696 from LISP_STRING, if that's given. */
11697 if (STRINGP (face_string))
11698 {
11699 int endptr;
11700 struct face *face;
11701
11702 it->face_id
11703 = face_at_string_position (it->w, face_string, face_string_pos,
11704 0, it->region_beg_charpos,
11705 it->region_end_charpos,
11706 &endptr, it->base_face_id);
11707 face = FACE_FROM_ID (it->f, it->face_id);
11708 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 11709 }
a2889657 11710
5f5c8ee5
GM
11711 /* Set max_x to the maximum allowed X position. Don't let it go
11712 beyond the right edge of the window. */
11713 if (max_x <= 0)
11714 max_x = it->last_visible_x;
11715 else
11716 max_x = min (max_x, it->last_visible_x);
efc63ef0 11717
5f5c8ee5
GM
11718 /* Skip over display elements that are not visible. because IT->w is
11719 hscrolled. */
11720 if (it->current_x < it->first_visible_x)
11721 move_it_in_display_line_to (it, 100000, it->first_visible_x,
11722 MOVE_TO_POS | MOVE_TO_X);
a2889657 11723
5f5c8ee5
GM
11724 row->ascent = it->max_ascent;
11725 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11726 row->phys_ascent = it->max_phys_ascent;
11727 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 11728
5f5c8ee5
GM
11729 /* This condition is for the case that we are called with current_x
11730 past last_visible_x. */
11731 while (it->current_x < max_x)
a2889657 11732 {
5f5c8ee5 11733 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 11734
5f5c8ee5
GM
11735 /* Get the next display element. */
11736 if (!get_next_display_element (it))
90adcf20 11737 break;
1c9241f5 11738
5f5c8ee5
GM
11739 /* Produce glyphs. */
11740 x_before = it->current_x;
11741 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
11742 PRODUCE_GLYPHS (it);
90adcf20 11743
5f5c8ee5
GM
11744 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
11745 i = 0;
11746 x = x_before;
11747 while (i < nglyphs)
a2889657 11748 {
5f5c8ee5
GM
11749 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11750
11751 if (!it->truncate_lines_p
11752 && x + glyph->pixel_width > max_x)
11753 {
11754 /* End of continued line or max_x reached. */
11755 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
11756 it->current_x = x;
11757 break;
11758 }
11759 else if (x + glyph->pixel_width > it->first_visible_x)
11760 {
11761 /* Glyph is at least partially visible. */
11762 ++it->hpos;
11763 if (x < it->first_visible_x)
11764 it->glyph_row->x = x - it->first_visible_x;
11765 }
11766 else
a2889657 11767 {
5f5c8ee5
GM
11768 /* Glyph is off the left margin of the display area.
11769 Should not happen. */
11770 abort ();
a2889657 11771 }
5f5c8ee5
GM
11772
11773 row->ascent = max (row->ascent, it->max_ascent);
11774 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11775 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11776 row->phys_height = max (row->phys_height,
11777 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11778 x += glyph->pixel_width;
11779 ++i;
a2889657 11780 }
5f5c8ee5
GM
11781
11782 /* Stop if max_x reached. */
11783 if (i < nglyphs)
11784 break;
11785
11786 /* Stop at line ends. */
11787 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 11788 {
5f5c8ee5
GM
11789 it->continuation_lines_width = 0;
11790 break;
a2889657 11791 }
1c9241f5 11792
5f5c8ee5 11793 set_iterator_to_next (it);
a688bb24 11794
5f5c8ee5
GM
11795 /* Stop if truncating at the right edge. */
11796 if (it->truncate_lines_p
11797 && it->current_x >= it->last_visible_x)
11798 {
11799 /* Add truncation mark, but don't do it if the line is
11800 truncated at a padding space. */
11801 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 11802 {
5f5c8ee5
GM
11803 if (!FRAME_WINDOW_P (it->f))
11804 produce_special_glyphs (it, IT_TRUNCATION);
11805 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 11806 }
5f5c8ee5 11807 break;
1c9241f5 11808 }
a2889657
JB
11809 }
11810
5f5c8ee5
GM
11811 /* Maybe insert a truncation at the left. */
11812 if (it->first_visible_x
11813 && IT_CHARPOS (*it) > 0)
a2889657 11814 {
5f5c8ee5
GM
11815 if (!FRAME_WINDOW_P (it->f))
11816 insert_left_trunc_glyphs (it);
11817 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
11818 }
11819
5f5c8ee5
GM
11820 it->face_id = saved_face_id;
11821
11822 /* Value is number of columns displayed. */
11823 return it->hpos - hpos_at_start;
11824}
a2889657 11825
a2889657 11826
a2889657 11827\f
5f5c8ee5
GM
11828/* This is like a combination of memq and assq. Return 1 if PROPVAL
11829 appears as an element of LIST or as the car of an element of LIST.
11830 If PROPVAL is a list, compare each element against LIST in that
11831 way, and return 1 if any element of PROPVAL is found in LIST.
11832 Otherwise return 0. This function cannot quit. */
642eefc6
RS
11833
11834int
11835invisible_p (propval, list)
11836 register Lisp_Object propval;
11837 Lisp_Object list;
11838{
af460d46
RS
11839 register Lisp_Object tail, proptail;
11840 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
11841 {
11842 register Lisp_Object tem;
af460d46 11843 tem = XCONS (tail)->car;
642eefc6
RS
11844 if (EQ (propval, tem))
11845 return 1;
11846 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
11847 return 1;
11848 }
af460d46
RS
11849 if (CONSP (propval))
11850 for (proptail = propval; CONSP (proptail);
11851 proptail = XCONS (proptail)->cdr)
11852 {
11853 Lisp_Object propelt;
11854 propelt = XCONS (proptail)->car;
11855 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11856 {
11857 register Lisp_Object tem;
11858 tem = XCONS (tail)->car;
11859 if (EQ (propelt, tem))
11860 return 1;
11861 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
11862 return 1;
11863 }
11864 }
642eefc6
RS
11865 return 0;
11866}
11867
5f5c8ee5
GM
11868
11869/* Return 1 if PROPVAL appears as the car of an element of LIST and
11870 the cdr of that element is non-nil. If PROPVAL is a list, check
11871 each element of PROPVAL in that way, and the first time some
11872 element is found, return 1 if the cdr of that element is non-nil.
11873 Otherwise return 0. This function cannot quit. */
642eefc6
RS
11874
11875int
11876invisible_ellipsis_p (propval, list)
11877 register Lisp_Object propval;
11878 Lisp_Object list;
11879{
af460d46
RS
11880 register Lisp_Object tail, proptail;
11881 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
11882 {
11883 register Lisp_Object tem;
af460d46 11884 tem = XCONS (tail)->car;
642eefc6
RS
11885 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
11886 return ! NILP (XCONS (tem)->cdr);
11887 }
af460d46
RS
11888 if (CONSP (propval))
11889 for (proptail = propval; CONSP (proptail);
11890 proptail = XCONS (proptail)->cdr)
11891 {
11892 Lisp_Object propelt;
11893 propelt = XCONS (proptail)->car;
11894 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11895 {
11896 register Lisp_Object tem;
11897 tem = XCONS (tail)->car;
11898 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
11899 return ! NILP (XCONS (tem)->cdr);
11900 }
11901 }
642eefc6
RS
11902 return 0;
11903}
5f5c8ee5
GM
11904
11905
642eefc6 11906\f
5f5c8ee5
GM
11907/***********************************************************************
11908 Initialization
11909 ***********************************************************************/
11910
a2889657
JB
11911void
11912syms_of_xdisp ()
11913{
5f5c8ee5
GM
11914 echo_area_message = previous_echo_area_message = Qnil;
11915 staticpro (&echo_area_message);
11916 staticpro (&previous_echo_area_message);
11917
735c094c
KH
11918 staticpro (&Qinhibit_redisplay);
11919 Qinhibit_redisplay = intern ("inhibit-redisplay");
11920
5f5c8ee5
GM
11921#if GLYPH_DEBUG
11922 defsubr (&Sdump_glyph_matrix);
11923 defsubr (&Sdump_glyph_row);
11924 defsubr (&Sdump_toolbar_row);
11925 defsubr (&Strace_redisplay_toggle);
11926#endif
11927
cf074754
RS
11928 staticpro (&Qmenu_bar_update_hook);
11929 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
11930
d46fb96a 11931 staticpro (&Qoverriding_terminal_local_map);
7079aefa 11932 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 11933
399164b4
KH
11934 staticpro (&Qoverriding_local_map);
11935 Qoverriding_local_map = intern ("overriding-local-map");
11936
75c43375
RS
11937 staticpro (&Qwindow_scroll_functions);
11938 Qwindow_scroll_functions = intern ("window-scroll-functions");
11939
e0bfbde6
RS
11940 staticpro (&Qredisplay_end_trigger_functions);
11941 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 11942
2e54982e
RS
11943 staticpro (&Qinhibit_point_motion_hooks);
11944 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
11945
5f5c8ee5
GM
11946 staticpro (&Qdisplay);
11947 Qdisplay = intern ("display");
11948 staticpro (&Qleft_margin);
11949 Qspace_width = intern ("space-width");
11950 staticpro (&Qspace_width);
11951 Qheight = intern ("height");
11952 staticpro (&Qheight);
11953 Qraise = intern ("raise");
11954 staticpro (&Qraise);
11955 Qspace = intern ("space");
11956 staticpro (&Qspace);
11957 Qleft_margin = intern ("left-margin");
11958 staticpro (&Qright_margin);
11959 Qright_margin = intern ("right-margin");
11960 Qalign_to = intern ("align-to");
11961 staticpro (&Qalign_to);
11962 QCalign_to = intern (":align-to");
11963 staticpro (&QCalign_to);
11964 Qwidth = intern ("width");
11965 staticpro (&Qwidth);
11966 Qrelative_width = intern ("relative-width");
11967 staticpro (&Qrelative_width);
11968 QCrelative_width = intern (":relative-width");
11969 staticpro (&QCrelative_width);
11970 QCrelative_height = intern (":relative-height");
11971 staticpro (&QCrelative_height);
11972 QCeval = intern (":eval");
11973 staticpro (&QCeval);
11974 QCwhen = intern (":when");
11975 staticpro (&QCwhen);
11976 Qfontified = intern ("fontified");
11977 staticpro (&Qfontified);
11978 Qfontification_functions = intern ("fontification-functions");
11979 staticpro (&Qfontification_functions);
11980 Qshow_trailing_whitespace = intern ("show-trailing-whitespace");
11981 staticpro (&Qshow_trailing_whitespace);
11982 Qtrailing_whitespace = intern ("trailing-whitespace");
11983 staticpro (&Qtrailing_whitespace);
11984 Qimage = intern ("image");
11985 staticpro (&Qimage);
11986
a2889657
JB
11987 staticpro (&last_arrow_position);
11988 staticpro (&last_arrow_string);
11989 last_arrow_position = Qnil;
11990 last_arrow_string = Qnil;
11991
735c094c
KH
11992 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
11993 "Non-nil means don't actually do any redisplay.\n\
11994This is used for internal purposes.");
11995 Vinhibit_redisplay = Qnil;
11996
a2889657 11997 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 11998 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
11999 Vglobal_mode_string = Qnil;
12000
12001 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12002 "Marker for where to display an arrow on top of the buffer text.\n\
12003This must be the beginning of a line in order to work.\n\
12004See also `overlay-arrow-string'.");
12005 Voverlay_arrow_position = Qnil;
12006
12007 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12008 "String to display as an arrow. See also `overlay-arrow-position'.");
12009 Voverlay_arrow_string = Qnil;
12010
12011 DEFVAR_INT ("scroll-step", &scroll_step,
12012 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
12013If that fails to bring point back on frame, point is centered instead.\n\
12014If this is zero, point is always centered after it moves off frame.");
a2889657 12015
0789adb2
RS
12016 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12017 "*Scroll up to this many lines, to bring point back on screen.");
12018 scroll_conservatively = 0;
12019
9afd2168
RS
12020 DEFVAR_INT ("scroll-margin", &scroll_margin,
12021 "*Number of lines of margin at the top and bottom of a window.\n\
12022Recenter the window whenever point gets within this many lines\n\
12023of the top or bottom of the window.");
12024 scroll_margin = 0;
12025
5f5c8ee5 12026#if GLYPH_DEBUG
a2889657 12027 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 12028#endif
a2889657
JB
12029
12030 DEFVAR_BOOL ("truncate-partial-width-windows",
12031 &truncate_partial_width_windows,
44fa5b1e 12032 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
12033 truncate_partial_width_windows = 1;
12034
12035 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12036 "*Non-nil means use inverse video for the mode line.");
12037 mode_line_inverse_video = 1;
aa6d10fa
RS
12038
12039 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
5f5c8ee5 12040 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 12041If the buffer is bigger than this, the line number does not appear\n\
9f027393 12042in the mode line.");
aa6d10fa 12043 line_number_display_limit = 1000000;
fba9ce76
RS
12044
12045 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12046 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 12047 highlight_nonselected_windows = 0;
d39b6696
KH
12048
12049 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
12050 "Non-nil if more than one frame is visible on this display.\n\
12051Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
12052This variable is not guaranteed to be accurate except while processing\n\
12053`frame-title-format' and `icon-title-format'.");
d39b6696
KH
12054
12055 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 12056 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
12057\(Assuming the window manager supports this feature.)\n\
12058This variable has the same structure as `mode-line-format' (which see),\n\
12059and is used only on frames for which no explicit name has been set\n\
12060\(see `modify-frame-parameters').");
12061 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 12062 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
12063\(Assuming the window manager supports this feature.)\n\
12064This variable has the same structure as `mode-line-format' (which see),\n\
12065and is used only on frames for which no explicit name has been set\n\
12066\(see `modify-frame-parameters').");
12067 Vicon_title_format
12068 = Vframe_title_format
12069 = Fcons (intern ("multiple-frames"),
12070 Fcons (build_string ("%b"),
12071 Fcons (Fcons (build_string (""),
12072 Fcons (intern ("invocation-name"),
12073 Fcons (build_string ("@"),
12074 Fcons (intern ("system-name"),
12075 Qnil)))),
12076 Qnil)));
5992c4f7
KH
12077
12078 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12079 "Maximum number of lines to keep in the message log buffer.\n\
12080If nil, disable message logging. If t, log messages but don't truncate\n\
12081the buffer when it becomes large.");
12082 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
12083
12084 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12085 "Functions called before redisplay, if window sizes have changed.\n\
12086The value should be a list of functions that take one argument.\n\
12087Just before redisplay, for each frame, if any of its windows have changed\n\
12088size since the last redisplay, or have been split or deleted,\n\
12089all the functions in the list are called, with the frame as argument.");
12090 Vwindow_size_change_functions = Qnil;
75c43375
RS
12091
12092 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 12093 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 12094Each function is called with two arguments, the window\n\
8d9583b0
RS
12095and its new display-start position. Note that the value of `window-end'\n\
12096is not valid when these functions are called.");
75c43375 12097 Vwindow_scroll_functions = Qnil;
5f5c8ee5
GM
12098
12099 DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
12100 "*Non-nil means automatically resize toolbars.\n\
12101This increases a toolbar's height if not all toolbar items are visible.\n\
12102It decreases a toolbar's height when it would display blank lines\n\
12103otherwise.");
12104 auto_resize_toolbars_p = 1;
12105
12106 DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
12107 "*Non-nil means raise toolbar buttons when the mouse moves over them.");
12108 auto_raise_toolbar_buttons_p = 1;
12109
12110 DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
12111 "*Margin around toolbar buttons in pixels.");
12112 toolbar_button_margin = 1;
12113
12114 DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
12115 "Relief thickness of toolbar buttons.");
12116 toolbar_button_relief = 3;
12117
12118 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12119 "List of functions to call to fontify regions of text.\n\
12120Each function is called with one argument POS. Functions must\n\
12121fontify a region starting at POS in the current buffer, and give\n\
12122fontified regions the property `fontified'.\n\
12123This variable automatically becomes buffer-local when set.");
12124 Vfontification_functions = Qnil;
12125 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
12126
12127 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
12128 &unibyte_display_via_language_environment,
12129 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
12130Specifically this means that unibyte non-ASCII characters\n\
12131are displayed by converting them to the equivalent multibyte characters\n\
12132according to the current language environment. As a result, they are\n\
12133displayed according to the current fontset.");
12134 unibyte_display_via_language_environment = 0;
a2889657
JB
12135}
12136
5f5c8ee5
GM
12137
12138/* Initialize this module when Emacs starts. */
12139
dfcf069d 12140void
a2889657
JB
12141init_xdisp ()
12142{
12143 Lisp_Object root_window;
5f5c8ee5 12144 struct window *mini_w;
a2889657 12145
5f5c8ee5 12146 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
12147
12148 mini_w = XWINDOW (minibuf_window);
11e82b76 12149 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657
JB
12150
12151 echo_area_glyphs = 0;
12152 previous_echo_glyphs = 0;
5f5c8ee5 12153 echo_area_message = previous_echo_area_message = Qnil;
a2889657
JB
12154
12155 if (!noninteractive)
12156 {
5f5c8ee5
GM
12157 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12158 int i;
12159
12160 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 12161 set_window_height (root_window,
5f5c8ee5 12162 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 12163 0);
c2213350 12164 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
12165 set_window_height (minibuf_window, 1, 0);
12166
c2213350
KH
12167 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12168 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
12169
12170 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12171 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12172 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12173
12174 /* The default ellipsis glyphs `...'. */
12175 for (i = 0; i < 3; ++i)
12176 XSETFASTINT (default_invis_vector[i], '.');
a2889657 12177 }
5f5c8ee5
GM
12178
12179#ifdef HAVE_WINDOW_SYSTEM
12180 {
12181 /* Allocate the buffer for frame titles. */
12182 int size = 100;
12183 frame_title_buf = (char *) xmalloc (size);
12184 frame_title_buf_end = frame_title_buf + size;
12185 frame_title_ptr = NULL;
12186 }
12187#endif /* HAVE_WINDOW_SYSTEM */
a2889657 12188}
5f5c8ee5
GM
12189
12190