(USE_XIM): New define.
[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>
a2889657 172#include "lisp.h"
44fa5b1e 173#include "frame.h"
a2889657
JB
174#include "window.h"
175#include "termchar.h"
176#include "dispextern.h"
177#include "buffer.h"
1c9241f5 178#include "charset.h"
a2889657
JB
179#include "indent.h"
180#include "commands.h"
181#include "macros.h"
182#include "disptab.h"
30c566e4 183#include "termhooks.h"
b0a0fbda 184#include "intervals.h"
fe8b0cf8 185#include "keyboard.h"
1c9241f5
KH
186#include "coding.h"
187#include "process.h"
dfcf069d 188#include "region-cache.h"
0ef75e87 189#include "fontset.h"
dfcf069d 190
6d55d620 191#ifdef HAVE_X_WINDOWS
dfcf069d
AS
192#include "xterm.h"
193#endif
a75dfea0
AI
194#ifdef WINDOWSNT
195#include "w32term.h"
196#endif
a2889657 197
5f5c8ee5
GM
198#define min(a, b) ((a) < (b) ? (a) : (b))
199#define max(a, b) ((a) > (b) ? (a) : (b))
200
201#define INFINITY 10000000
202
8f3343d0 203#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
76412d64 204extern void set_frame_menubar ();
cd6dfed6 205extern int pending_menu_activation;
76412d64
RS
206#endif
207
a2889657
JB
208extern int interrupt_input;
209extern int command_loop_level;
210
b6436d4e
RS
211extern int minibuffer_auto_raise;
212
c4628384
RS
213extern Lisp_Object Qface;
214
399164b4
KH
215extern Lisp_Object Voverriding_local_map;
216extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 217extern Lisp_Object Qmenu_item;
399164b4 218
d46fb96a 219Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 220Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 221Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 222Lisp_Object Qinhibit_point_motion_hooks;
9499d71b 223Lisp_Object QCeval, Qwhen, QCfile, QCdata;
5f5c8ee5
GM
224Lisp_Object Qfontified;
225
226/* Functions called to fontify regions of text. */
227
228Lisp_Object Vfontification_functions;
229Lisp_Object Qfontification_functions;
230
e037b9ec 231/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
232 over them. */
233
e037b9ec 234int auto_raise_tool_bar_buttons_p;
5f5c8ee5 235
e037b9ec 236/* Margin around tool bar buttons in pixels. */
5f5c8ee5 237
e037b9ec 238int tool_bar_button_margin;
5f5c8ee5 239
e037b9ec 240/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 241
e037b9ec 242int tool_bar_button_relief;
5f5c8ee5 243
e037b9ec 244/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
245 items are visible, and no blank lines remain. */
246
e037b9ec 247int auto_resize_tool_bars_p;
399164b4 248
735c094c
KH
249/* Non-nil means don't actually do any redisplay. */
250
251Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
252
5f5c8ee5
GM
253/* Names of text properties relevant for redisplay. */
254
a7e27ef7
DL
255Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
5f5c8ee5
GM
257
258/* Symbols used in text property values. */
259
260Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 261Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 262Lisp_Object Qmargin;
a7e27ef7 263extern Lisp_Object Qheight;
5f5c8ee5 264
8f897821 265/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 266
8f897821 267Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
268
269/* Name of the face used to highlight trailing whitespace. */
270
271Lisp_Object Qtrailing_whitespace;
272
273/* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
275
276Lisp_Object Qimage;
277
278/* Non-zero means print newline to stdout before next mini-buffer
279 message. */
a2889657
JB
280
281int noninteractive_need_newline;
282
5f5c8ee5 283/* Non-zero means print newline to message log before next message. */
f88eb0b6 284
3c6595e0 285static int message_log_need_newline;
f88eb0b6 286
5f5c8ee5
GM
287\f
288/* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
a2889657 292
5f5c8ee5 293static struct text_pos this_line_start_pos;
a2889657 294
5f5c8ee5
GM
295/* Number of characters past the end of the line above, including the
296 terminating newline. */
297
298static struct text_pos this_line_end_pos;
299
300/* The vertical positions and the height of this line. */
a2889657 301
a2889657 302static int this_line_vpos;
5f5c8ee5
GM
303static int this_line_y;
304static int this_line_pixel_height;
305
306/* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
308
309static int this_line_start_x;
a2889657 310
5f5c8ee5 311/* Buffer that this_line_.* variables are referring to. */
a2889657 312
a2889657
JB
313static struct buffer *this_line_buffer;
314
5f5c8ee5
GM
315/* Nonzero means truncate lines in all windows less wide than the
316 frame. */
a2889657 317
a2889657
JB
318int truncate_partial_width_windows;
319
7bbe686f 320/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 321
7bbe686f 322int unibyte_display_via_language_environment;
5f5c8ee5
GM
323
324/* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
7bbe686f 327
d39b6696
KH
328int multiple_frames;
329
a2889657
JB
330Lisp_Object Vglobal_mode_string;
331
332/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 333
a2889657
JB
334Lisp_Object Voverlay_arrow_position;
335
5f5c8ee5
GM
336/* String to display for the arrow. Only used on terminal frames. */
337
a2889657
JB
338Lisp_Object Voverlay_arrow_string;
339
5f5c8ee5
GM
340/* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
343
d45de95b
RS
344static Lisp_Object last_arrow_position, last_arrow_string;
345
5f5c8ee5
GM
346/* Like mode-line-format, but for the title bar on a visible frame. */
347
d39b6696
KH
348Lisp_Object Vframe_title_format;
349
5f5c8ee5
GM
350/* Like mode-line-format, but for the title bar on an iconified frame. */
351
d39b6696
KH
352Lisp_Object Vicon_title_format;
353
08b610e4
RS
354/* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
5f5c8ee5 357
08b610e4
RS
358static Lisp_Object Vwindow_size_change_functions;
359
cf074754
RS
360Lisp_Object Qmenu_bar_update_hook;
361
a2889657 362/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 363
5f5c8ee5 364static int overlay_arrow_seen;
ca26e1c8 365
fba9ce76 366/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 367
5f5c8ee5
GM
368int highlight_nonselected_windows;
369
370/* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
372
14510fee 373static int scroll_step;
a2889657 374
5f5c8ee5
GM
375/* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
377
0789adb2
RS
378static int scroll_conservatively;
379
5f5c8ee5
GM
380/* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
384
9afd2168
RS
385int scroll_margin;
386
5f5c8ee5
GM
387/* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
a2889657 390
a2889657
JB
391int buffer_shared;
392
5f5c8ee5 393/* Vector containing glyphs for an ellipsis `...'. */
a2889657 394
5f5c8ee5 395static Lisp_Object default_invis_vector[3];
a2889657 396
5f5c8ee5 397/* Nonzero means display mode line highlighted. */
a2889657 398
a2889657
JB
399int mode_line_inverse_video;
400
5f5c8ee5
GM
401/* Prompt to display in front of the mini-buffer contents. */
402
8c5b6a0a 403Lisp_Object minibuf_prompt;
a2889657 404
5f5c8ee5
GM
405/* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
407
a2889657 408int minibuf_prompt_width;
5f5c8ee5
GM
409int minibuf_prompt_pixel_width;
410
5f5c8ee5
GM
411/* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
414
73af359d
RS
415Lisp_Object echo_area_window;
416
c6e89d6c
GM
417/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
421
422Lisp_Object Vmessage_stack;
423
a3788d53
RS
424/* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
5f5c8ee5 426
a3788d53
RS
427int message_enable_multibyte;
428
5f5c8ee5
GM
429/* True if we should redraw the mode lines on the next redisplay. */
430
a2889657
JB
431int update_mode_lines;
432
5f5c8ee5
GM
433/* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
435
a2889657
JB
436int windows_or_buffers_changed;
437
5f5c8ee5
GM
438/* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
440
aa6d10fa
RS
441int line_number_displayed;
442
443/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 444
090703f4 445Lisp_Object Vline_number_display_limit;
5992c4f7 446
5d121aec
KH
447/* line width to consider when repostioning for line number display */
448
449static int line_number_display_limit_width;
450
5f5c8ee5
GM
451/* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
453
5992c4f7 454Lisp_Object Vmessage_log_max;
d45de95b 455
c6e89d6c
GM
456/* Current, index 0, and last displayed echo area message. Either
457 buffers from echo_buffers, or nil to indicate no message. */
458
459Lisp_Object echo_area_buffer[2];
460
461/* The buffers referenced from echo_area_buffer. */
462
463static Lisp_Object echo_buffer[2];
464
465/* A vector saved used in with_area_buffer to reduce consing. */
466
467static Lisp_Object Vwith_echo_area_save_vector;
468
469/* Non-zero means display_echo_area should display the last echo area
470 message again. Set by redisplay_preserve_echo_area. */
471
472static int display_last_displayed_message_p;
473
474/* Nonzero if echo area is being used by print; zero if being used by
475 message. */
476
477int message_buf_print;
478
9142dd5b
GM
479/* Maximum height for resizing mini-windows. Either a float
480 specifying a fraction of the available height, or an integer
481 specifying a number of lines. */
c6e89d6c 482
ad4f174e
GM
483Lisp_Object Vmax_mini_window_height;
484
485/* Non-zero means messages should be displayed with truncated
486 lines instead of being continued. */
487
488int message_truncate_lines;
489Lisp_Object Qmessage_truncate_lines;
c6e89d6c 490
d6d26ed3
GM
491/* Non-zero means we want a hollow cursor in windows that are not
492 selected. Zero means there's no cursor in such windows. */
493
494int cursor_in_non_selected_windows;
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
d475bcb8
GM
522/* Non-zero means automatically scroll windows horizontally to make
523 point visible. */
524
525int automatic_hscrolling_p;
526
e00daaa0
GM
527/* A list of symbols, one for each supported image type. */
528
529Lisp_Object Vimage_types;
530
5f5c8ee5
GM
531/* Value returned from text property handlers (see below). */
532
533enum prop_handled
3c6595e0 534{
5f5c8ee5
GM
535 HANDLED_NORMALLY,
536 HANDLED_RECOMPUTE_PROPS,
537 HANDLED_OVERLAY_STRING_CONSUMED,
538 HANDLED_RETURN
539};
3c6595e0 540
5f5c8ee5
GM
541/* A description of text properties that redisplay is interested
542 in. */
3c6595e0 543
5f5c8ee5
GM
544struct props
545{
546 /* The name of the property. */
547 Lisp_Object *name;
90adcf20 548
5f5c8ee5
GM
549 /* A unique index for the property. */
550 enum prop_idx idx;
551
552 /* A handler function called to set up iterator IT from the property
553 at IT's current position. Value is used to steer handle_stop. */
554 enum prop_handled (*handler) P_ ((struct it *it));
555};
556
557static enum prop_handled handle_face_prop P_ ((struct it *));
558static enum prop_handled handle_invisible_prop P_ ((struct it *));
559static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 560static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
561static enum prop_handled handle_overlay_change P_ ((struct it *));
562static enum prop_handled handle_fontified_prop P_ ((struct it *));
563
564/* Properties handled by iterators. */
565
566static struct props it_props[] =
5992c4f7 567{
5f5c8ee5
GM
568 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
569 /* Handle `face' before `display' because some sub-properties of
570 `display' need to know the face. */
571 {&Qface, FACE_PROP_IDX, handle_face_prop},
572 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
573 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 574 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
575 {NULL, 0, NULL}
576};
5992c4f7 577
5f5c8ee5
GM
578/* Value is the position described by X. If X is a marker, value is
579 the marker_position of X. Otherwise, value is X. */
12adba34 580
5f5c8ee5 581#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 582
5f5c8ee5 583/* Enumeration returned by some move_it_.* functions internally. */
12adba34 584
5f5c8ee5
GM
585enum move_it_result
586{
587 /* Not used. Undefined value. */
588 MOVE_UNDEFINED,
bab29e15 589
5f5c8ee5
GM
590 /* Move ended at the requested buffer position or ZV. */
591 MOVE_POS_MATCH_OR_ZV,
bab29e15 592
5f5c8ee5
GM
593 /* Move ended at the requested X pixel position. */
594 MOVE_X_REACHED,
12adba34 595
5f5c8ee5
GM
596 /* Move within a line ended at the end of a line that must be
597 continued. */
598 MOVE_LINE_CONTINUED,
599
600 /* Move within a line ended at the end of a line that would
601 be displayed truncated. */
602 MOVE_LINE_TRUNCATED,
ff6c30e5 603
5f5c8ee5
GM
604 /* Move within a line ended at a line end. */
605 MOVE_NEWLINE_OR_CR
606};
12adba34 607
ff6c30e5 608
5f5c8ee5
GM
609\f
610/* Function prototypes. */
611
06568bbf 612static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 613static void ensure_echo_area_buffers P_ ((void));
e037b9ec
GM
614static struct glyph_row *row_containing_pos P_ ((struct window *, int,
615 struct glyph_row *,
616 struct glyph_row *));
c6e89d6c
GM
617static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
618static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
c6e89d6c
GM
619static void clear_garbaged_frames P_ ((void));
620static int current_message_1 P_ ((Lisp_Object *));
621static int truncate_message_1 P_ ((int));
622static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
623static int display_echo_area P_ ((struct window *));
624static int display_echo_area_1 P_ ((struct window *));
28514cd9 625static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 626static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
627static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
628 struct text_pos));
629static int compute_window_start_on_continuation_line P_ ((struct window *));
630static Lisp_Object eval_handler P_ ((Lisp_Object));
631static Lisp_Object eval_form P_ ((Lisp_Object));
632static void insert_left_trunc_glyphs P_ ((struct it *));
633static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
634static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 635static int append_space P_ ((struct it *, int));
5f5c8ee5
GM
636static void make_cursor_line_fully_visible P_ ((struct window *));
637static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
638static int trailing_whitespace_p P_ ((int));
639static int message_log_check_duplicate P_ ((int, int, int, int));
640int invisible_p P_ ((Lisp_Object, Lisp_Object));
641int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
642static void push_it P_ ((struct it *));
643static void pop_it P_ ((struct it *));
644static void sync_frame_with_window_matrix_rows P_ ((struct window *));
645static void redisplay_internal P_ ((int));
c6e89d6c 646static int echo_area_display P_ ((int));
5f5c8ee5
GM
647static void redisplay_windows P_ ((Lisp_Object));
648static void redisplay_window P_ ((Lisp_Object, int));
649static void update_menu_bar P_ ((struct frame *, int));
650static int try_window_reusing_current_matrix P_ ((struct window *));
651static int try_window_id P_ ((struct window *));
652static int display_line P_ ((struct it *));
653static void display_mode_lines P_ ((struct window *));
654static void display_mode_line P_ ((struct window *, enum face_id,
655 Lisp_Object));
656static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 657static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
658static void display_menu_bar P_ ((struct window *));
659static int display_count_lines P_ ((int, int, int, int, int *));
660static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
661 int, int, struct it *, int, int, int, int));
662static void compute_line_metrics P_ ((struct it *));
663static void run_redisplay_end_trigger_hook P_ ((struct it *));
664static int get_overlay_strings P_ ((struct it *));
665static void next_overlay_string P_ ((struct it *));
666void set_iterator_to_next P_ ((struct it *));
667static void reseat P_ ((struct it *, struct text_pos, int));
668static void reseat_1 P_ ((struct it *, struct text_pos, int));
669static void back_to_previous_visible_line_start P_ ((struct it *));
670static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 671static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
672static int next_element_from_display_vector P_ ((struct it *));
673static int next_element_from_string P_ ((struct it *));
674static int next_element_from_c_string P_ ((struct it *));
675static int next_element_from_buffer P_ ((struct it *));
260a86a0 676static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
677static int next_element_from_image P_ ((struct it *));
678static int next_element_from_stretch P_ ((struct it *));
679static void load_overlay_strings P_ ((struct it *));
680static void init_from_display_pos P_ ((struct it *, struct window *,
681 struct display_pos *));
682static void reseat_to_string P_ ((struct it *, unsigned char *,
683 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
684static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
685 int, int, int));
686void move_it_vertically_backward P_ ((struct it *, int));
687static void init_to_row_start P_ ((struct it *, struct window *,
688 struct glyph_row *));
689static void init_to_row_end P_ ((struct it *, struct window *,
690 struct glyph_row *));
691static void back_to_previous_line_start P_ ((struct it *));
692static void forward_to_next_line_start P_ ((struct it *));
693static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
694 Lisp_Object, int));
695static struct text_pos string_pos P_ ((int, Lisp_Object));
696static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
697static int number_of_chars P_ ((unsigned char *, int));
698static void compute_stop_pos P_ ((struct it *));
699static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
700 Lisp_Object));
701static int face_before_or_after_it_pos P_ ((struct it *, int));
702static int next_overlay_change P_ ((int));
703static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
704 Lisp_Object, struct text_pos *));
705
706#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
707#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 708
5f5c8ee5 709#ifdef HAVE_WINDOW_SYSTEM
12adba34 710
e037b9ec
GM
711static void update_tool_bar P_ ((struct frame *, int));
712static void build_desired_tool_bar_string P_ ((struct frame *f));
713static int redisplay_tool_bar P_ ((struct frame *));
714static void display_tool_bar_line P_ ((struct it *));
12adba34 715
5f5c8ee5 716#endif /* HAVE_WINDOW_SYSTEM */
12adba34 717
5f5c8ee5
GM
718\f
719/***********************************************************************
720 Window display dimensions
721 ***********************************************************************/
12adba34 722
5f5c8ee5
GM
723/* Return the window-relative maximum y + 1 for glyph rows displaying
724 text in window W. This is the height of W minus the height of a
725 mode line, if any. */
726
727INLINE int
728window_text_bottom_y (w)
729 struct window *w;
730{
731 struct frame *f = XFRAME (w->frame);
732 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
733
734 if (WINDOW_WANTS_MODELINE_P (w))
735 height -= CURRENT_MODE_LINE_HEIGHT (w);
736 return height;
f88eb0b6
KH
737}
738
f82aff7c 739
5f5c8ee5
GM
740/* Return the pixel width of display area AREA of window W. AREA < 0
741 means return the total width of W, not including bitmap areas to
742 the left and right of the window. */
ff6c30e5 743
5f5c8ee5
GM
744INLINE int
745window_box_width (w, area)
746 struct window *w;
747 int area;
748{
749 struct frame *f = XFRAME (w->frame);
750 int width = XFASTINT (w->width);
751
752 if (!w->pseudo_window_p)
ff6c30e5 753 {
050d82d7 754 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
755
756 if (area == TEXT_AREA)
757 {
758 if (INTEGERP (w->left_margin_width))
759 width -= XFASTINT (w->left_margin_width);
760 if (INTEGERP (w->right_margin_width))
761 width -= XFASTINT (w->right_margin_width);
762 }
763 else if (area == LEFT_MARGIN_AREA)
764 width = (INTEGERP (w->left_margin_width)
765 ? XFASTINT (w->left_margin_width) : 0);
766 else if (area == RIGHT_MARGIN_AREA)
767 width = (INTEGERP (w->right_margin_width)
768 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 769 }
5f5c8ee5
GM
770
771 return width * CANON_X_UNIT (f);
ff6c30e5 772}
1adc55de 773
1adc55de 774
5f5c8ee5
GM
775/* Return the pixel height of the display area of window W, not
776 including mode lines of W, if any.. */
f88eb0b6 777
5f5c8ee5
GM
778INLINE int
779window_box_height (w)
780 struct window *w;
f88eb0b6 781{
5f5c8ee5
GM
782 struct frame *f = XFRAME (w->frame);
783 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
784
785 if (WINDOW_WANTS_MODELINE_P (w))
786 height -= CURRENT_MODE_LINE_HEIGHT (w);
787
045dee35
GM
788 if (WINDOW_WANTS_HEADER_LINE_P (w))
789 height -= CURRENT_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
790
791 return height;
5992c4f7
KH
792}
793
794
5f5c8ee5
GM
795/* Return the frame-relative coordinate of the left edge of display
796 area AREA of window W. AREA < 0 means return the left edge of the
797 whole window, to the right of any bitmap area at the left side of
798 W. */
5992c4f7 799
5f5c8ee5
GM
800INLINE int
801window_box_left (w, area)
802 struct window *w;
803 int area;
90adcf20 804{
5f5c8ee5
GM
805 struct frame *f = XFRAME (w->frame);
806 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 807
5f5c8ee5 808 if (!w->pseudo_window_p)
90adcf20 809 {
5f5c8ee5 810 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 811 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
812
813 if (area == TEXT_AREA)
814 x += window_box_width (w, LEFT_MARGIN_AREA);
815 else if (area == RIGHT_MARGIN_AREA)
816 x += (window_box_width (w, LEFT_MARGIN_AREA)
817 + window_box_width (w, TEXT_AREA));
90adcf20 818 }
73af359d 819
5f5c8ee5
GM
820 return x;
821}
90adcf20 822
b6436d4e 823
5f5c8ee5
GM
824/* Return the frame-relative coordinate of the right edge of display
825 area AREA of window W. AREA < 0 means return the left edge of the
826 whole window, to the left of any bitmap area at the right side of
827 W. */
ded34426 828
5f5c8ee5
GM
829INLINE int
830window_box_right (w, area)
831 struct window *w;
832 int area;
833{
834 return window_box_left (w, area) + window_box_width (w, area);
835}
836
837
838/* Get the bounding box of the display area AREA of window W, without
839 mode lines, in frame-relative coordinates. AREA < 0 means the
840 whole window, not including bitmap areas to the left and right of
841 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
842 coordinates of the upper-left corner of the box. Return in
843 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
844
845INLINE void
846window_box (w, area, box_x, box_y, box_width, box_height)
847 struct window *w;
848 int area;
849 int *box_x, *box_y, *box_width, *box_height;
850{
851 struct frame *f = XFRAME (w->frame);
852
853 *box_width = window_box_width (w, area);
854 *box_height = window_box_height (w);
855 *box_x = window_box_left (w, area);
856 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
857 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
858 if (WINDOW_WANTS_HEADER_LINE_P (w))
859 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 860}
1adc55de 861
1adc55de 862
5f5c8ee5
GM
863/* Get the bounding box of the display area AREA of window W, without
864 mode lines. AREA < 0 means the whole window, not including bitmap
865 areas to the left and right of the window. Return in *TOP_LEFT_X
866 and TOP_LEFT_Y the frame-relative pixel coordinates of the
867 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
868 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
869 box. */
ded34426 870
5f5c8ee5
GM
871INLINE void
872window_box_edges (w, area, top_left_x, top_left_y,
873 bottom_right_x, bottom_right_y)
874 struct window *w;
875 int area;
876 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 877{
5f5c8ee5
GM
878 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
879 bottom_right_y);
880 *bottom_right_x += *top_left_x;
881 *bottom_right_y += *top_left_y;
48ae5f0a
KH
882}
883
5f5c8ee5
GM
884
885\f
886/***********************************************************************
887 Utilities
888 ***********************************************************************/
889
4fdb80f2
GM
890/* Return the next character from STR which is MAXLEN bytes long.
891 Return in *LEN the length of the character. This is like
892 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 893 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
894 character. */
895
896static INLINE int
7a5b8a93 897string_char_and_length (str, maxlen, len)
4fdb80f2 898 unsigned char *str;
7a5b8a93 899 int maxlen, *len;
4fdb80f2
GM
900{
901 int c;
902
903 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
904 if (!CHAR_VALID_P (c, 1))
905 /* We may not change the length here because other places in Emacs
9ab8560d 906 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
907 characters. */
908 c = '?';
909
910 return c;
911}
912
913
914
5f5c8ee5
GM
915/* Given a position POS containing a valid character and byte position
916 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
917
918static struct text_pos
919string_pos_nchars_ahead (pos, string, nchars)
920 struct text_pos pos;
921 Lisp_Object string;
922 int nchars;
0b1005ef 923{
5f5c8ee5
GM
924 xassert (STRINGP (string) && nchars >= 0);
925
926 if (STRING_MULTIBYTE (string))
927 {
928 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
929 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
930 int len;
931
932 while (nchars--)
933 {
4fdb80f2 934 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
935 p += len, rest -= len;
936 xassert (rest >= 0);
937 CHARPOS (pos) += 1;
938 BYTEPOS (pos) += len;
939 }
940 }
941 else
942 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
943
944 return pos;
0a9dc68b
RS
945}
946
0a9dc68b 947
5f5c8ee5
GM
948/* Value is the text position, i.e. character and byte position,
949 for character position CHARPOS in STRING. */
950
951static INLINE struct text_pos
952string_pos (charpos, string)
953 int charpos;
0a9dc68b 954 Lisp_Object string;
0a9dc68b 955{
5f5c8ee5
GM
956 struct text_pos pos;
957 xassert (STRINGP (string));
958 xassert (charpos >= 0);
959 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
960 return pos;
961}
962
963
964/* Value is a text position, i.e. character and byte position, for
965 character position CHARPOS in C string S. MULTIBYTE_P non-zero
966 means recognize multibyte characters. */
967
968static struct text_pos
969c_string_pos (charpos, s, multibyte_p)
970 int charpos;
971 unsigned char *s;
972 int multibyte_p;
973{
974 struct text_pos pos;
975
976 xassert (s != NULL);
977 xassert (charpos >= 0);
978
979 if (multibyte_p)
0a9dc68b 980 {
5f5c8ee5
GM
981 int rest = strlen (s), len;
982
983 SET_TEXT_POS (pos, 0, 0);
984 while (charpos--)
0a9dc68b 985 {
4fdb80f2 986 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
987 s += len, rest -= len;
988 xassert (rest >= 0);
989 CHARPOS (pos) += 1;
990 BYTEPOS (pos) += len;
0a9dc68b
RS
991 }
992 }
5f5c8ee5
GM
993 else
994 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 995
5f5c8ee5
GM
996 return pos;
997}
0a9dc68b 998
0a9dc68b 999
5f5c8ee5
GM
1000/* Value is the number of characters in C string S. MULTIBYTE_P
1001 non-zero means recognize multibyte characters. */
0a9dc68b 1002
5f5c8ee5
GM
1003static int
1004number_of_chars (s, multibyte_p)
1005 unsigned char *s;
1006 int multibyte_p;
1007{
1008 int nchars;
1009
1010 if (multibyte_p)
1011 {
1012 int rest = strlen (s), len;
1013 unsigned char *p = (unsigned char *) s;
0a9dc68b 1014
5f5c8ee5
GM
1015 for (nchars = 0; rest > 0; ++nchars)
1016 {
4fdb80f2 1017 string_char_and_length (p, rest, &len);
5f5c8ee5 1018 rest -= len, p += len;
0a9dc68b
RS
1019 }
1020 }
5f5c8ee5
GM
1021 else
1022 nchars = strlen (s);
1023
1024 return nchars;
0b1005ef
KH
1025}
1026
5f5c8ee5
GM
1027
1028/* Compute byte position NEWPOS->bytepos corresponding to
1029 NEWPOS->charpos. POS is a known position in string STRING.
1030 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1031
5f5c8ee5
GM
1032static void
1033compute_string_pos (newpos, pos, string)
1034 struct text_pos *newpos, pos;
1035 Lisp_Object string;
76412d64 1036{
5f5c8ee5
GM
1037 xassert (STRINGP (string));
1038 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1039
1040 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1041 *newpos = string_pos_nchars_ahead (pos, string,
1042 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1043 else
1044 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1045}
1046
9c74a0dd 1047
5f5c8ee5
GM
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
045dee35 1164 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1165 displaying the tool-bar.
5f5c8ee5
GM
1166
1167 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1168 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
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;
5f5c8ee5
GM
1180
1181 /* Some precondition checks. */
1182 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1183 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1184
1185 /* If face attributes have been changed since the last redisplay,
1186 free realized faces now because they depend on face definitions
1187 that might have changed. */
1188 if (face_change_count)
1189 {
1190 face_change_count = 0;
1191 free_all_realized_faces (Qnil);
1192 }
1193
1194 /* Use one of the mode line rows of W's desired matrix if
1195 appropriate. */
1196 if (row == NULL)
1197 {
1198 if (base_face_id == MODE_LINE_FACE_ID)
1199 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1200 else if (base_face_id == HEADER_LINE_FACE_ID)
1201 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1202 }
1203
1204 /* Clear IT. */
1205 bzero (it, sizeof *it);
1206 it->current.overlay_string_index = -1;
1207 it->current.dpvec_index = -1;
5f5c8ee5
GM
1208 it->base_face_id = base_face_id;
1209
1210 /* The window in which we iterate over current_buffer: */
1211 XSETWINDOW (it->window, w);
1212 it->w = w;
1213 it->f = XFRAME (w->frame);
1214
d475bcb8
GM
1215 /* Extra space between lines (on window systems only). */
1216 if (base_face_id == DEFAULT_FACE_ID
1217 && FRAME_WINDOW_P (it->f))
1218 {
1219 if (NATNUMP (current_buffer->extra_line_spacing))
1220 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1221 else if (it->f->extra_line_spacing > 0)
1222 it->extra_line_spacing = it->f->extra_line_spacing;
1223 }
1224
5f5c8ee5
GM
1225 /* If realized faces have been removed, e.g. because of face
1226 attribute changes of named faces, recompute them. */
1227 if (FRAME_FACE_CACHE (it->f)->used == 0)
1228 recompute_basic_faces (it->f);
1229
5f5c8ee5
GM
1230 /* Current value of the `space-width', and 'height' properties. */
1231 it->space_width = Qnil;
1232 it->font_height = Qnil;
1233
1234 /* Are control characters displayed as `^C'? */
1235 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1236
1237 /* -1 means everything between a CR and the following line end
1238 is invisible. >0 means lines indented more than this value are
1239 invisible. */
1240 it->selective = (INTEGERP (current_buffer->selective_display)
1241 ? XFASTINT (current_buffer->selective_display)
1242 : (!NILP (current_buffer->selective_display)
1243 ? -1 : 0));
1244 it->selective_display_ellipsis_p
1245 = !NILP (current_buffer->selective_display_ellipses);
1246
1247 /* Display table to use. */
1248 it->dp = window_display_table (w);
1249
1250 /* Are multibyte characters enabled in current_buffer? */
1251 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1252
1253 /* Non-zero if we should highlight the region. */
1254 highlight_region_p
1255 = (!NILP (Vtransient_mark_mode)
1256 && !NILP (current_buffer->mark_active)
1257 && XMARKER (current_buffer->mark)->buffer != 0);
1258
1259 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1260 start and end of a visible region in window IT->w. Set both to
1261 -1 to indicate no region. */
1262 if (highlight_region_p
1263 /* Maybe highlight only in selected window. */
1264 && (/* Either show region everywhere. */
1265 highlight_nonselected_windows
1266 /* Or show region in the selected window. */
1267 || w == XWINDOW (selected_window)
1268 /* Or show the region if we are in the mini-buffer and W is
1269 the window the mini-buffer refers to. */
1270 || (MINI_WINDOW_P (XWINDOW (selected_window))
1271 && w == XWINDOW (Vminibuf_scroll_window))))
1272 {
1273 int charpos = marker_position (current_buffer->mark);
1274 it->region_beg_charpos = min (PT, charpos);
1275 it->region_end_charpos = max (PT, charpos);
1276 }
1277 else
1278 it->region_beg_charpos = it->region_end_charpos = -1;
1279
1280 /* Get the position at which the redisplay_end_trigger hook should
1281 be run, if it is to be run at all. */
1282 if (MARKERP (w->redisplay_end_trigger)
1283 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1284 it->redisplay_end_trigger_charpos
1285 = marker_position (w->redisplay_end_trigger);
1286 else if (INTEGERP (w->redisplay_end_trigger))
1287 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1288
1289 /* Correct bogus values of tab_width. */
1290 it->tab_width = XINT (current_buffer->tab_width);
1291 if (it->tab_width <= 0 || it->tab_width > 1000)
1292 it->tab_width = 8;
1293
1294 /* Are lines in the display truncated? */
1295 it->truncate_lines_p
1296 = (base_face_id != DEFAULT_FACE_ID
1297 || XINT (it->w->hscroll)
1298 || (truncate_partial_width_windows
1299 && !WINDOW_FULL_WIDTH_P (it->w))
1300 || !NILP (current_buffer->truncate_lines));
1301
1302 /* Get dimensions of truncation and continuation glyphs. These are
1303 displayed as bitmaps under X, so we don't need them for such
1304 frames. */
1305 if (!FRAME_WINDOW_P (it->f))
1306 {
1307 if (it->truncate_lines_p)
1308 {
1309 /* We will need the truncation glyph. */
1310 xassert (it->glyph_row == NULL);
1311 produce_special_glyphs (it, IT_TRUNCATION);
1312 it->truncation_pixel_width = it->pixel_width;
1313 }
1314 else
1315 {
1316 /* We will need the continuation glyph. */
1317 xassert (it->glyph_row == NULL);
1318 produce_special_glyphs (it, IT_CONTINUATION);
1319 it->continuation_pixel_width = it->pixel_width;
1320 }
1321
1322 /* Reset these values to zero becaue the produce_special_glyphs
1323 above has changed them. */
1324 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1325 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1326 }
1327
1328 /* Set this after getting the dimensions of truncation and
1329 continuation glyphs, so that we don't produce glyphs when calling
1330 produce_special_glyphs, above. */
1331 it->glyph_row = row;
1332 it->area = TEXT_AREA;
1333
1334 /* Get the dimensions of the display area. The display area
1335 consists of the visible window area plus a horizontally scrolled
1336 part to the left of the window. All x-values are relative to the
1337 start of this total display area. */
1338 if (base_face_id != DEFAULT_FACE_ID)
1339 {
1340 /* Mode lines, menu bar in terminal frames. */
1341 it->first_visible_x = 0;
1342 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1343 }
1344 else
1345 {
1346 it->first_visible_x
1347 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1348 it->last_visible_x = (it->first_visible_x
1349 + window_box_width (w, TEXT_AREA));
1350
1351 /* If we truncate lines, leave room for the truncator glyph(s) at
1352 the right margin. Otherwise, leave room for the continuation
1353 glyph(s). Truncation and continuation glyphs are not inserted
1354 for window-based redisplay. */
1355 if (!FRAME_WINDOW_P (it->f))
1356 {
1357 if (it->truncate_lines_p)
1358 it->last_visible_x -= it->truncation_pixel_width;
1359 else
1360 it->last_visible_x -= it->continuation_pixel_width;
1361 }
1362
045dee35
GM
1363 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1364 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1365 }
1366
1367 /* Leave room for a border glyph. */
1368 if (!FRAME_WINDOW_P (it->f)
1369 && !WINDOW_RIGHTMOST_P (it->w))
1370 it->last_visible_x -= 1;
1371
1372 it->last_visible_y = window_text_bottom_y (w);
1373
1374 /* For mode lines and alike, arrange for the first glyph having a
1375 left box line if the face specifies a box. */
1376 if (base_face_id != DEFAULT_FACE_ID)
1377 {
1378 struct face *face;
1379
1380 it->face_id = base_face_id;
1381
1382 /* If we have a boxed mode line, make the first character appear
1383 with a left box line. */
1384 face = FACE_FROM_ID (it->f, base_face_id);
1385 if (face->box != FACE_NO_BOX)
1386 it->start_of_box_run_p = 1;
1387 }
1388
1389 /* If a buffer position was specified, set the iterator there,
1390 getting overlays and face properties from that position. */
1391 if (charpos > 0)
1392 {
1393 it->end_charpos = ZV;
1394 it->face_id = -1;
1395 IT_CHARPOS (*it) = charpos;
1396
1397 /* Compute byte position if not specified. */
1398 if (bytepos <= 0)
1399 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1400 else
1401 IT_BYTEPOS (*it) = bytepos;
1402
1403 /* Compute faces etc. */
1404 reseat (it, it->current.pos, 1);
1405 }
1406
1407 CHECK_IT (it);
1408}
1409
1410
1411/* Initialize IT for the display of window W with window start POS. */
1412
1413void
1414start_display (it, w, pos)
1415 struct it *it;
1416 struct window *w;
1417 struct text_pos pos;
1418{
1419 int start_at_line_beg_p;
1420 struct glyph_row *row;
045dee35 1421 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1422 int first_y;
1423
1424 row = w->desired_matrix->rows + first_vpos;
1425 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1426 first_y = it->current_y;
1427
1428 /* If window start is not at a line start, move back to the line
1429 start. This makes sure that we take continuation lines into
1430 account. */
1431 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1432 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1433 if (!start_at_line_beg_p)
1434 reseat_at_previous_visible_line_start (it);
1435
5f5c8ee5
GM
1436 /* If window start is not at a line start, skip forward to POS to
1437 get the correct continuation_lines_width and current_x. */
1438 if (!start_at_line_beg_p)
1439 {
1440 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1441
1442 /* If lines are continued, this line may end in the middle of a
1443 multi-glyph character (e.g. a control character displayed as
1444 \003, or in the middle of an overlay string). In this case
1445 move_it_to above will not have taken us to the start of
1446 the continuation line but to the end of the continued line. */
1447 if (!it->truncate_lines_p && it->current_x > 0)
1448 {
1449 if (it->current.dpvec_index >= 0
1450 || it->current.overlay_string_index >= 0)
1451 {
1452 set_iterator_to_next (it);
1453 move_it_in_display_line_to (it, -1, -1, 0);
1454 }
1455 it->continuation_lines_width += it->current_x;
1456 }
1457
1458 it->current_y = first_y;
1459 it->vpos = 0;
1460 it->current_x = it->hpos = 0;
1461 }
1462
1463#if 0 /* Don't assert the following because start_display is sometimes
1464 called intentionally with a window start that is not at a
1465 line start. Please leave this code in as a comment. */
1466
1467 /* Window start should be on a line start, now. */
1468 xassert (it->continuation_lines_width
1469 || IT_CHARPOS (it) == BEGV
1470 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1471#endif /* 0 */
1472}
1473
1474
1475/* Initialize IT for stepping through current_buffer in window W,
1476 starting at position POS that includes overlay string and display
1477 vector/ control character translation position information. */
1478
1479static void
1480init_from_display_pos (it, w, pos)
1481 struct it *it;
1482 struct window *w;
1483 struct display_pos *pos;
1484{
1485 /* Keep in mind: the call to reseat in init_iterator skips invisible
1486 text, so we might end up at a position different from POS. This
1487 is only a problem when POS is a row start after a newline and an
1488 overlay starts there with an after-string, and the overlay has an
1489 invisible property. Since we don't skip invisible text in
1490 display_line and elsewhere immediately after consuming the
1491 newline before the row start, such a POS will not be in a string,
1492 but the call to init_iterator below will move us to the
1493 after-string. */
1494 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1495 NULL, DEFAULT_FACE_ID);
1496
1497 /* If position is within an overlay string, set up IT to
1498 the right overlay string. */
1499 if (pos->overlay_string_index >= 0)
1500 {
1501 int relative_index;
1502
1503 /* We already have the first chunk of overlay strings in
1504 IT->overlay_strings. Load more until the one for
1505 pos->overlay_string_index is in IT->overlay_strings. */
1506 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1507 {
1508 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1509 it->current.overlay_string_index = 0;
1510 while (n--)
1511 {
1512 load_overlay_strings (it);
1513 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1514 }
1515 }
1516
1517 it->current.overlay_string_index = pos->overlay_string_index;
1518 relative_index = (it->current.overlay_string_index
1519 % OVERLAY_STRING_CHUNK_SIZE);
1520 it->string = it->overlay_strings[relative_index];
1521 it->current.string_pos = pos->string_pos;
1522 it->method = next_element_from_string;
1523 }
1524 else if (CHARPOS (pos->string_pos) >= 0)
1525 {
1526 /* Recorded position is not in an overlay string, but in another
1527 string. This can only be a string from a `display' property.
1528 IT should already be filled with that string. */
1529 it->current.string_pos = pos->string_pos;
1530 xassert (STRINGP (it->string));
1531 }
1532
1533 /* Restore position in display vector translations or control
1534 character translations. */
1535 if (pos->dpvec_index >= 0)
1536 {
1537 /* This fills IT->dpvec. */
1538 get_next_display_element (it);
1539 xassert (it->dpvec && it->current.dpvec_index == 0);
1540 it->current.dpvec_index = pos->dpvec_index;
1541 }
1542
1543 CHECK_IT (it);
1544}
1545
1546
1547/* Initialize IT for stepping through current_buffer in window W
1548 starting at ROW->start. */
1549
1550static void
1551init_to_row_start (it, w, row)
1552 struct it *it;
1553 struct window *w;
1554 struct glyph_row *row;
1555{
1556 init_from_display_pos (it, w, &row->start);
1557 it->continuation_lines_width = row->continuation_lines_width;
1558 CHECK_IT (it);
1559}
1560
1561
1562/* Initialize IT for stepping through current_buffer in window W
1563 starting in the line following ROW, i.e. starting at ROW->end. */
1564
1565static void
1566init_to_row_end (it, w, row)
1567 struct it *it;
1568 struct window *w;
1569 struct glyph_row *row;
1570{
1571 init_from_display_pos (it, w, &row->end);
1572
1573 if (row->continued_p)
1574 it->continuation_lines_width = (row->continuation_lines_width
1575 + row->pixel_width);
1576 CHECK_IT (it);
1577}
1578
1579
1580
1581\f
1582/***********************************************************************
1583 Text properties
1584 ***********************************************************************/
1585
1586/* Called when IT reaches IT->stop_charpos. Handle text property and
1587 overlay changes. Set IT->stop_charpos to the next position where
1588 to stop. */
1589
1590static void
1591handle_stop (it)
1592 struct it *it;
1593{
1594 enum prop_handled handled;
1595 int handle_overlay_change_p = 1;
1596 struct props *p;
1597
1598 it->dpvec = NULL;
1599 it->current.dpvec_index = -1;
2970b9be 1600 it->add_overlay_start = 0;
5f5c8ee5
GM
1601
1602 do
1603 {
1604 handled = HANDLED_NORMALLY;
1605
1606 /* Call text property handlers. */
1607 for (p = it_props; p->handler; ++p)
1608 {
1609 handled = p->handler (it);
2970b9be 1610
5f5c8ee5
GM
1611 if (handled == HANDLED_RECOMPUTE_PROPS)
1612 break;
1613 else if (handled == HANDLED_RETURN)
1614 return;
1615 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1616 handle_overlay_change_p = 0;
1617 }
1618
1619 if (handled != HANDLED_RECOMPUTE_PROPS)
1620 {
1621 /* Don't check for overlay strings below when set to deliver
1622 characters from a display vector. */
1623 if (it->method == next_element_from_display_vector)
1624 handle_overlay_change_p = 0;
1625
1626 /* Handle overlay changes. */
1627 if (handle_overlay_change_p)
1628 handled = handle_overlay_change (it);
1629
1630 /* Determine where to stop next. */
1631 if (handled == HANDLED_NORMALLY)
1632 compute_stop_pos (it);
1633 }
1634 }
1635 while (handled == HANDLED_RECOMPUTE_PROPS);
1636}
1637
1638
1639/* Compute IT->stop_charpos from text property and overlay change
1640 information for IT's current position. */
1641
1642static void
1643compute_stop_pos (it)
1644 struct it *it;
1645{
1646 register INTERVAL iv, next_iv;
1647 Lisp_Object object, limit, position;
1648
1649 /* If nowhere else, stop at the end. */
1650 it->stop_charpos = it->end_charpos;
1651
1652 if (STRINGP (it->string))
1653 {
1654 /* Strings are usually short, so don't limit the search for
1655 properties. */
1656 object = it->string;
1657 limit = Qnil;
1658 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1659 }
1660 else
1661 {
1662 int charpos;
1663
1664 /* If next overlay change is in front of the current stop pos
1665 (which is IT->end_charpos), stop there. Note: value of
1666 next_overlay_change is point-max if no overlay change
1667 follows. */
1668 charpos = next_overlay_change (IT_CHARPOS (*it));
1669 if (charpos < it->stop_charpos)
1670 it->stop_charpos = charpos;
1671
1672 /* If showing the region, we have to stop at the region
1673 start or end because the face might change there. */
1674 if (it->region_beg_charpos > 0)
1675 {
1676 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1677 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1678 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1679 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1680 }
1681
1682 /* Set up variables for computing the stop position from text
1683 property changes. */
1684 XSETBUFFER (object, current_buffer);
1685 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1686 XSETFASTINT (position, IT_CHARPOS (*it));
1687
1688 }
1689
1690 /* Get the interval containing IT's position. Value is a null
1691 interval if there isn't such an interval. */
1692 iv = validate_interval_range (object, &position, &position, 0);
1693 if (!NULL_INTERVAL_P (iv))
1694 {
1695 Lisp_Object values_here[LAST_PROP_IDX];
1696 struct props *p;
1697
1698 /* Get properties here. */
1699 for (p = it_props; p->handler; ++p)
1700 values_here[p->idx] = textget (iv->plist, *p->name);
1701
1702 /* Look for an interval following iv that has different
1703 properties. */
1704 for (next_iv = next_interval (iv);
1705 (!NULL_INTERVAL_P (next_iv)
1706 && (NILP (limit)
1707 || XFASTINT (limit) > next_iv->position));
1708 next_iv = next_interval (next_iv))
1709 {
1710 for (p = it_props; p->handler; ++p)
1711 {
1712 Lisp_Object new_value;
1713
1714 new_value = textget (next_iv->plist, *p->name);
1715 if (!EQ (values_here[p->idx], new_value))
1716 break;
1717 }
1718
1719 if (p->handler)
1720 break;
1721 }
1722
1723 if (!NULL_INTERVAL_P (next_iv))
1724 {
1725 if (INTEGERP (limit)
1726 && next_iv->position >= XFASTINT (limit))
1727 /* No text property change up to limit. */
1728 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1729 else
1730 /* Text properties change in next_iv. */
1731 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1732 }
1733 }
1734
1735 xassert (STRINGP (it->string)
1736 || (it->stop_charpos >= BEGV
1737 && it->stop_charpos >= IT_CHARPOS (*it)));
1738}
1739
1740
1741/* Return the position of the next overlay change after POS in
1742 current_buffer. Value is point-max if no overlay change
1743 follows. This is like `next-overlay-change' but doesn't use
1744 xmalloc. */
1745
1746static int
1747next_overlay_change (pos)
1748 int pos;
1749{
1750 int noverlays;
1751 int endpos;
1752 Lisp_Object *overlays;
1753 int len;
1754 int i;
1755
1756 /* Get all overlays at the given position. */
1757 len = 10;
1758 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1759 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1760 if (noverlays > len)
1761 {
1762 len = noverlays;
1763 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1764 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1765 }
1766
1767 /* If any of these overlays ends before endpos,
1768 use its ending point instead. */
1769 for (i = 0; i < noverlays; ++i)
1770 {
1771 Lisp_Object oend;
1772 int oendpos;
1773
1774 oend = OVERLAY_END (overlays[i]);
1775 oendpos = OVERLAY_POSITION (oend);
1776 endpos = min (endpos, oendpos);
1777 }
1778
1779 return endpos;
1780}
1781
1782
1783\f
1784/***********************************************************************
1785 Fontification
1786 ***********************************************************************/
1787
1788/* Handle changes in the `fontified' property of the current buffer by
1789 calling hook functions from Qfontification_functions to fontify
1790 regions of text. */
1791
1792static enum prop_handled
1793handle_fontified_prop (it)
1794 struct it *it;
1795{
1796 Lisp_Object prop, pos;
1797 enum prop_handled handled = HANDLED_NORMALLY;
1798
1799 /* Get the value of the `fontified' property at IT's current buffer
1800 position. (The `fontified' property doesn't have a special
1801 meaning in strings.) If the value is nil, call functions from
1802 Qfontification_functions. */
1803 if (!STRINGP (it->string)
1804 && it->s == NULL
1805 && !NILP (Vfontification_functions)
1806 && (pos = make_number (IT_CHARPOS (*it)),
1807 prop = Fget_char_property (pos, Qfontified, Qnil),
1808 NILP (prop)))
1809 {
1810 Lisp_Object args[2];
1811
1812 /* Run the hook functions. */
1813 args[0] = Qfontification_functions;
1814 args[1] = pos;
6fc556fd 1815 Frun_hook_with_args (2, args);
5f5c8ee5
GM
1816
1817 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1818 something. This avoids an endless loop if they failed to
1819 fontify the text for which reason ever. */
1820 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1821 handled = HANDLED_RECOMPUTE_PROPS;
1822 }
1823
1824 return handled;
1825}
1826
1827
1828\f
1829/***********************************************************************
1830 Faces
1831 ***********************************************************************/
1832
1833/* Set up iterator IT from face properties at its current position.
1834 Called from handle_stop. */
1835
1836static enum prop_handled
1837handle_face_prop (it)
1838 struct it *it;
1839{
1840 int new_face_id, next_stop;
1841
1842 if (!STRINGP (it->string))
1843 {
1844 new_face_id
1845 = face_at_buffer_position (it->w,
1846 IT_CHARPOS (*it),
1847 it->region_beg_charpos,
1848 it->region_end_charpos,
1849 &next_stop,
1850 (IT_CHARPOS (*it)
1851 + TEXT_PROP_DISTANCE_LIMIT),
1852 0);
1853
1854 /* Is this a start of a run of characters with box face?
1855 Caveat: this can be called for a freshly initialized
1856 iterator; face_id is -1 is this case. We know that the new
1857 face will not change until limit, i.e. if the new face has a
1858 box, all characters up to limit will have one. But, as
1859 usual, we don't know whether limit is really the end. */
1860 if (new_face_id != it->face_id)
1861 {
1862 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1863
1864 /* If new face has a box but old face has not, this is
1865 the start of a run of characters with box, i.e. it has
1866 a shadow on the left side. The value of face_id of the
1867 iterator will be -1 if this is the initial call that gets
1868 the face. In this case, we have to look in front of IT's
1869 position and see whether there is a face != new_face_id. */
1870 it->start_of_box_run_p
1871 = (new_face->box != FACE_NO_BOX
1872 && (it->face_id >= 0
1873 || IT_CHARPOS (*it) == BEG
1874 || new_face_id != face_before_it_pos (it)));
1875 it->face_box_p = new_face->box != FACE_NO_BOX;
1876 }
1877 }
1878 else
1879 {
1880 new_face_id
1881 = face_at_string_position (it->w,
1882 it->string,
1883 IT_STRING_CHARPOS (*it),
1884 (it->current.overlay_string_index >= 0
1885 ? IT_CHARPOS (*it)
1886 : 0),
1887 it->region_beg_charpos,
1888 it->region_end_charpos,
1889 &next_stop,
1890 it->base_face_id);
1891
1892#if 0 /* This shouldn't be neccessary. Let's check it. */
1893 /* If IT is used to display a mode line we would really like to
1894 use the mode line face instead of the frame's default face. */
1895 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1896 && new_face_id == DEFAULT_FACE_ID)
1897 new_face_id = MODE_LINE_FACE_ID;
1898#endif
1899
1900 /* Is this a start of a run of characters with box? Caveat:
1901 this can be called for a freshly allocated iterator; face_id
1902 is -1 is this case. We know that the new face will not
1903 change until the next check pos, i.e. if the new face has a
1904 box, all characters up to that position will have a
1905 box. But, as usual, we don't know whether that position
1906 is really the end. */
1907 if (new_face_id != it->face_id)
1908 {
1909 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1910 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1911
1912 /* If new face has a box but old face hasn't, this is the
1913 start of a run of characters with box, i.e. it has a
1914 shadow on the left side. */
1915 it->start_of_box_run_p
1916 = new_face->box && (old_face == NULL || !old_face->box);
1917 it->face_box_p = new_face->box != FACE_NO_BOX;
1918 }
1919 }
1920
1921 it->face_id = new_face_id;
5f5c8ee5
GM
1922 return HANDLED_NORMALLY;
1923}
1924
1925
1926/* Compute the face one character before or after the current position
1927 of IT. BEFORE_P non-zero means get the face in front of IT's
1928 position. Value is the id of the face. */
1929
1930static int
1931face_before_or_after_it_pos (it, before_p)
1932 struct it *it;
1933 int before_p;
1934{
1935 int face_id, limit;
1936 int next_check_charpos;
1937 struct text_pos pos;
1938
1939 xassert (it->s == NULL);
1940
1941 if (STRINGP (it->string))
1942 {
1943 /* No face change past the end of the string (for the case
1944 we are padding with spaces). No face change before the
1945 string start. */
1946 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1947 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1948 return it->face_id;
1949
1950 /* Set pos to the position before or after IT's current position. */
1951 if (before_p)
1952 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1953 else
260a86a0
KH
1954 /* For composition, we must check the character after the
1955 composition. */
1956 pos = (it->what == IT_COMPOSITION
1957 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1958 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5
GM
1959
1960 /* Get the face for ASCII, or unibyte. */
1961 face_id
1962 = face_at_string_position (it->w,
1963 it->string,
1964 CHARPOS (pos),
1965 (it->current.overlay_string_index >= 0
1966 ? IT_CHARPOS (*it)
1967 : 0),
1968 it->region_beg_charpos,
1969 it->region_end_charpos,
1970 &next_check_charpos,
1971 it->base_face_id);
1972
1973 /* Correct the face for charsets different from ASCII. Do it
1974 for the multibyte case only. The face returned above is
1975 suitable for unibyte text if IT->string is unibyte. */
1976 if (STRING_MULTIBYTE (it->string))
1977 {
1978 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1979 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
1980 int c, len;
1981 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 1982
4fdb80f2 1983 c = string_char_and_length (p, rest, &len);
980806b6 1984 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
1985 }
1986 }
1987 else
1988 {
70851746
GM
1989 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1990 || (IT_CHARPOS (*it) <= BEGV && before_p))
1991 return it->face_id;
1992
5f5c8ee5
GM
1993 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1994 pos = it->current.pos;
1995
1996 if (before_p)
c1005d06 1997 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 1998 else
260a86a0
KH
1999 {
2000 if (it->what == IT_COMPOSITION)
2001 /* For composition, we must check the position after the
2002 composition. */
2003 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2004 else
c1005d06 2005 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2006 }
5f5c8ee5
GM
2007 /* Determine face for CHARSET_ASCII, or unibyte. */
2008 face_id = face_at_buffer_position (it->w,
2009 CHARPOS (pos),
2010 it->region_beg_charpos,
2011 it->region_end_charpos,
2012 &next_check_charpos,
2013 limit, 0);
2014
2015 /* Correct the face for charsets different from ASCII. Do it
2016 for the multibyte case only. The face returned above is
2017 suitable for unibyte text if current_buffer is unibyte. */
2018 if (it->multibyte_p)
2019 {
980806b6
KH
2020 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2021 struct face *face = FACE_FROM_ID (it->f, face_id);
2022 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2023 }
2024 }
2025
2026 return face_id;
2027}
2028
2029
2030\f
2031/***********************************************************************
2032 Invisible text
2033 ***********************************************************************/
2034
2035/* Set up iterator IT from invisible properties at its current
2036 position. Called from handle_stop. */
2037
2038static enum prop_handled
2039handle_invisible_prop (it)
2040 struct it *it;
2041{
2042 enum prop_handled handled = HANDLED_NORMALLY;
2043
2044 if (STRINGP (it->string))
2045 {
2046 extern Lisp_Object Qinvisible;
2047 Lisp_Object prop, end_charpos, limit, charpos;
2048
2049 /* Get the value of the invisible text property at the
2050 current position. Value will be nil if there is no such
2051 property. */
2052 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2053 prop = Fget_text_property (charpos, Qinvisible, it->string);
2054
eadc0bf8
GM
2055 if (!NILP (prop)
2056 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2057 {
2058 handled = HANDLED_RECOMPUTE_PROPS;
2059
2060 /* Get the position at which the next change of the
2061 invisible text property can be found in IT->string.
2062 Value will be nil if the property value is the same for
2063 all the rest of IT->string. */
2064 XSETINT (limit, XSTRING (it->string)->size);
2065 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2066 it->string, limit);
2067
2068 /* Text at current position is invisible. The next
2069 change in the property is at position end_charpos.
2070 Move IT's current position to that position. */
2071 if (INTEGERP (end_charpos)
2072 && XFASTINT (end_charpos) < XFASTINT (limit))
2073 {
2074 struct text_pos old;
2075 old = it->current.string_pos;
2076 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2077 compute_string_pos (&it->current.string_pos, old, it->string);
2078 }
2079 else
2080 {
2081 /* The rest of the string is invisible. If this is an
2082 overlay string, proceed with the next overlay string
2083 or whatever comes and return a character from there. */
2084 if (it->current.overlay_string_index >= 0)
2085 {
2086 next_overlay_string (it);
2087 /* Don't check for overlay strings when we just
2088 finished processing them. */
2089 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2090 }
2091 else
2092 {
2093 struct Lisp_String *s = XSTRING (it->string);
2094 IT_STRING_CHARPOS (*it) = s->size;
2095 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2096 }
2097 }
2098 }
2099 }
2100 else
2101 {
2102 int visible_p, newpos, next_stop;
2103 Lisp_Object pos, prop;
2104
2105 /* First of all, is there invisible text at this position? */
2106 XSETFASTINT (pos, IT_CHARPOS (*it));
2107 prop = Fget_char_property (pos, Qinvisible, it->window);
2108
2109 /* If we are on invisible text, skip over it. */
eadc0bf8
GM
2110 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2111 && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2112 {
2113 /* Record whether we have to display an ellipsis for the
2114 invisible text. */
2115 int display_ellipsis_p
2116 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2117
2118 handled = HANDLED_RECOMPUTE_PROPS;
2970b9be 2119 it->add_overlay_start = IT_CHARPOS (*it);
5f5c8ee5
GM
2120
2121 /* Loop skipping over invisible text. The loop is left at
2122 ZV or with IT on the first char being visible again. */
2123 do
2124 {
2125 /* Try to skip some invisible text. Return value is the
2126 position reached which can be equal to IT's position
2127 if there is nothing invisible here. This skips both
2128 over invisible text properties and overlays with
2129 invisible property. */
2130 newpos = skip_invisible (IT_CHARPOS (*it),
2131 &next_stop, ZV, it->window);
2132
2133 /* If we skipped nothing at all we weren't at invisible
2134 text in the first place. If everything to the end of
2135 the buffer was skipped, end the loop. */
2136 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2137 visible_p = 1;
2138 else
2139 {
2140 /* We skipped some characters but not necessarily
2141 all there are. Check if we ended up on visible
2142 text. Fget_char_property returns the property of
2143 the char before the given position, i.e. if we
2144 get visible_p = 1, this means that the char at
2145 newpos is visible. */
2146 XSETFASTINT (pos, newpos);
2147 prop = Fget_char_property (pos, Qinvisible, it->window);
2148 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2149 }
2150
2151 /* If we ended up on invisible text, proceed to
2152 skip starting with next_stop. */
2153 if (!visible_p)
2154 IT_CHARPOS (*it) = next_stop;
2155 }
2156 while (!visible_p);
2157
2158 /* The position newpos is now either ZV or on visible text. */
2159 IT_CHARPOS (*it) = newpos;
2160 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2161
2162 /* Maybe return `...' next for the end of the invisible text. */
2163 if (display_ellipsis_p)
2164 {
2165 if (it->dp
2166 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2167 {
2168 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2169 it->dpvec = v->contents;
2170 it->dpend = v->contents + v->size;
2171 }
2172 else
2173 {
2174 /* Default `...'. */
2175 it->dpvec = default_invis_vector;
2176 it->dpend = default_invis_vector + 3;
2177 }
2178
2179 /* The ellipsis display does not replace the display of
2180 the character at the new position. Indicate this by
2181 setting IT->dpvec_char_len to zero. */
2182 it->dpvec_char_len = 0;
2183
2184 it->current.dpvec_index = 0;
2185 it->method = next_element_from_display_vector;
2186 }
2187 }
2188 }
2189
2190 return handled;
2191}
2192
2193
2194\f
2195/***********************************************************************
2196 'display' property
2197 ***********************************************************************/
2198
2199/* Set up iterator IT from `display' property at its current position.
2200 Called from handle_stop. */
2201
2202static enum prop_handled
2203handle_display_prop (it)
2204 struct it *it;
2205{
2206 Lisp_Object prop, object;
2207 struct text_pos *position;
2208 int space_or_image_found_p;
2209
2210 if (STRINGP (it->string))
2211 {
2212 object = it->string;
2213 position = &it->current.string_pos;
2214 }
2215 else
2216 {
2217 object = Qnil;
2218 position = &it->current.pos;
2219 }
2220
2221 /* Reset those iterator values set from display property values. */
2222 it->font_height = Qnil;
2223 it->space_width = Qnil;
2224 it->voffset = 0;
2225
2226 /* We don't support recursive `display' properties, i.e. string
2227 values that have a string `display' property, that have a string
2228 `display' property etc. */
2229 if (!it->string_from_display_prop_p)
2230 it->area = TEXT_AREA;
2231
2232 prop = Fget_char_property (make_number (position->charpos),
2233 Qdisplay, object);
2234 if (NILP (prop))
2235 return HANDLED_NORMALLY;
2236
2237 space_or_image_found_p = 0;
f3751a65
GM
2238 if (CONSP (prop)
2239 && CONSP (XCAR (prop))
2240 && !EQ (Qmargin, XCAR (XCAR (prop))))
5f5c8ee5 2241 {
f3751a65 2242 /* A list of sub-properties. */
5f5c8ee5
GM
2243 while (CONSP (prop))
2244 {
2245 if (handle_single_display_prop (it, XCAR (prop), object, position))
2246 space_or_image_found_p = 1;
2247 prop = XCDR (prop);
2248 }
2249 }
2250 else if (VECTORP (prop))
2251 {
2252 int i;
2253 for (i = 0; i < XVECTOR (prop)->size; ++i)
2254 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2255 object, position))
2256 space_or_image_found_p = 1;
2257 }
2258 else
2259 {
2260 if (handle_single_display_prop (it, prop, object, position))
2261 space_or_image_found_p = 1;
2262 }
2263
2264 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2265}
2266
2267
6c577098 2268/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2269 at START_POS in OBJECT. */
2270
2271static struct text_pos
2272display_prop_end (it, object, start_pos)
2273 struct it *it;
2274 Lisp_Object object;
2275 struct text_pos start_pos;
2276{
2277 Lisp_Object end;
2278 struct text_pos end_pos;
5f5c8ee5 2279
6c577098
GM
2280 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2281 Qdisplay, object, Qnil);
2282 CHARPOS (end_pos) = XFASTINT (end);
2283 if (STRINGP (object))
5f5c8ee5
GM
2284 compute_string_pos (&end_pos, start_pos, it->string);
2285 else
6c577098 2286 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2287
2288 return end_pos;
2289}
2290
2291
2292/* Set up IT from a single `display' sub-property value PROP. OBJECT
2293 is the object in which the `display' property was found. *POSITION
2294 is the position at which it was found.
2295
2296 If PROP is a `space' or `image' sub-property, set *POSITION to the
2297 end position of the `display' property.
2298
2299 Value is non-zero if a `space' or `image' property value was found. */
2300
2301static int
2302handle_single_display_prop (it, prop, object, position)
2303 struct it *it;
2304 Lisp_Object prop;
2305 Lisp_Object object;
2306 struct text_pos *position;
2307{
2308 Lisp_Object value;
2309 int space_or_image_found_p = 0;
5f5c8ee5
GM
2310 Lisp_Object form;
2311
d3acf96b 2312 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2313 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2314 form = Qt;
d3acf96b 2315 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2316 {
2317 prop = XCDR (prop);
2318 if (!CONSP (prop))
2319 return 0;
2320 form = XCAR (prop);
2321 prop = XCDR (prop);
5f5c8ee5
GM
2322 }
2323
2324 if (!NILP (form) && !EQ (form, Qt))
2325 {
2326 struct gcpro gcpro1;
2327 struct text_pos end_pos, pt;
2328
5f5c8ee5 2329 GCPRO1 (form);
c7aca1ad 2330 end_pos = display_prop_end (it, object, *position);
5f5c8ee5
GM
2331
2332 /* Temporarily set point to the end position, and then evaluate
2333 the form. This makes `(eolp)' work as FORM. */
c7aca1ad
GM
2334 if (BUFFERP (object))
2335 {
2336 CHARPOS (pt) = PT;
2337 BYTEPOS (pt) = PT_BYTE;
2338 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2339 }
2340
5f5c8ee5 2341 form = eval_form (form);
c7aca1ad
GM
2342
2343 if (BUFFERP (object))
2344 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
5f5c8ee5
GM
2345 UNGCPRO;
2346 }
2347
2348 if (NILP (form))
2349 return 0;
2350
2351 if (CONSP (prop)
2352 && EQ (XCAR (prop), Qheight)
2353 && CONSP (XCDR (prop)))
2354 {
2355 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2356 return 0;
2357
2358 /* `(height HEIGHT)'. */
2359 it->font_height = XCAR (XCDR (prop));
2360 if (!NILP (it->font_height))
2361 {
2362 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2363 int new_height = -1;
2364
2365 if (CONSP (it->font_height)
2366 && (EQ (XCAR (it->font_height), Qplus)
2367 || EQ (XCAR (it->font_height), Qminus))
2368 && CONSP (XCDR (it->font_height))
2369 && INTEGERP (XCAR (XCDR (it->font_height))))
2370 {
2371 /* `(+ N)' or `(- N)' where N is an integer. */
2372 int steps = XINT (XCAR (XCDR (it->font_height)));
2373 if (EQ (XCAR (it->font_height), Qplus))
2374 steps = - steps;
2375 it->face_id = smaller_face (it->f, it->face_id, steps);
2376 }
2377 else if (SYMBOLP (it->font_height))
2378 {
2379 /* Call function with current height as argument.
2380 Value is the new height. */
2381 Lisp_Object form, height;
2382 struct gcpro gcpro1;
2383
2384 height = face->lface[LFACE_HEIGHT_INDEX];
2385 form = Fcons (it->font_height, Fcons (height, Qnil));
2386 GCPRO1 (form);
2387 height = eval_form (form);
2388 if (NUMBERP (height))
2389 new_height = XFLOATINT (height);
2390 UNGCPRO;
2391 }
2392 else if (NUMBERP (it->font_height))
2393 {
2394 /* Value is a multiple of the canonical char height. */
2395 struct face *face;
2396
2397 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2398 new_height = (XFLOATINT (it->font_height)
2399 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2400 }
2401 else
2402 {
2403 /* Evaluate IT->font_height with `height' bound to the
2404 current specified height to get the new height. */
2405 Lisp_Object value;
2406 int count = specpdl_ptr - specpdl;
2407
2408 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2409 value = eval_form (it->font_height);
2410 unbind_to (count, Qnil);
2411
2412 if (NUMBERP (value))
2413 new_height = XFLOATINT (value);
2414 }
2415
2416 if (new_height > 0)
2417 it->face_id = face_with_height (it->f, it->face_id, new_height);
2418 }
2419 }
2420 else if (CONSP (prop)
2421 && EQ (XCAR (prop), Qspace_width)
2422 && CONSP (XCDR (prop)))
2423 {
2424 /* `(space_width WIDTH)'. */
2425 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2426 return 0;
2427
2428 value = XCAR (XCDR (prop));
2429 if (NUMBERP (value) && XFLOATINT (value) > 0)
2430 it->space_width = value;
2431 }
2432 else if (CONSP (prop)
2433 && EQ (XCAR (prop), Qraise)
2434 && CONSP (XCDR (prop)))
2435 {
5f5c8ee5
GM
2436 /* `(raise FACTOR)'. */
2437 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2438 return 0;
2439
fb3842a8 2440#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2441 value = XCAR (XCDR (prop));
2442 if (NUMBERP (value))
2443 {
2444 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2445 it->voffset = - (XFLOATINT (value)
1ab3e082 2446 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2447 }
2448#endif /* HAVE_WINDOW_SYSTEM */
2449 }
2450 else if (!it->string_from_display_prop_p)
2451 {
f3751a65
GM
2452 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2453 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2454 Lisp_Object location, value;
2455 struct text_pos start_pos;
2456 int valid_p;
2457
2458 /* Characters having this form of property are not displayed, so
2459 we have to find the end of the property. */
5f5c8ee5
GM
2460 start_pos = *position;
2461 *position = display_prop_end (it, object, start_pos);
15e26c76 2462 value = Qnil;
5f5c8ee5
GM
2463
2464 /* Let's stop at the new position and assume that all
2465 text properties change there. */
2466 it->stop_charpos = position->charpos;
2467
f3751a65
GM
2468 location = Qunbound;
2469 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2470 {
f3751a65
GM
2471 Lisp_Object tem;
2472
5f5c8ee5 2473 value = XCDR (prop);
f3751a65
GM
2474 if (CONSP (value))
2475 value = XCAR (value);
2476
2477 tem = XCAR (prop);
2478 if (EQ (XCAR (tem), Qmargin)
2479 && (tem = XCDR (tem),
2480 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2481 (NILP (tem)
2482 || EQ (tem, Qleft_margin)
2483 || EQ (tem, Qright_margin))))
2484 location = tem;
5f5c8ee5 2485 }
f3751a65
GM
2486
2487 if (EQ (location, Qunbound))
5f5c8ee5
GM
2488 {
2489 location = Qnil;
2490 value = prop;
2491 }
2492
2493#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 2494 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
2495 valid_p = STRINGP (value);
2496 else
2497 valid_p = (STRINGP (value)
2498 || (CONSP (value) && EQ (XCAR (value), Qspace))
2499 || valid_image_p (value));
2500#else /* not HAVE_WINDOW_SYSTEM */
2501 valid_p = STRINGP (value);
2502#endif /* not HAVE_WINDOW_SYSTEM */
2503
2504 if ((EQ (location, Qleft_margin)
2505 || EQ (location, Qright_margin)
2506 || NILP (location))
2507 && valid_p)
2508 {
a21a3943
GM
2509 space_or_image_found_p = 1;
2510
5f5c8ee5
GM
2511 /* Save current settings of IT so that we can restore them
2512 when we are finished with the glyph property value. */
2513 push_it (it);
2514
2515 if (NILP (location))
2516 it->area = TEXT_AREA;
2517 else if (EQ (location, Qleft_margin))
2518 it->area = LEFT_MARGIN_AREA;
2519 else
2520 it->area = RIGHT_MARGIN_AREA;
2521
2522 if (STRINGP (value))
2523 {
2524 it->string = value;
2525 it->multibyte_p = STRING_MULTIBYTE (it->string);
2526 it->current.overlay_string_index = -1;
2527 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2528 it->end_charpos = it->string_nchars
2529 = XSTRING (it->string)->size;
2530 it->method = next_element_from_string;
2531 it->stop_charpos = 0;
2532 it->string_from_display_prop_p = 1;
2533 }
2534 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2535 {
2536 it->method = next_element_from_stretch;
2537 it->object = value;
2538 it->current.pos = it->position = start_pos;
2539 }
2540#ifdef HAVE_WINDOW_SYSTEM
2541 else
2542 {
2543 it->what = IT_IMAGE;
2544 it->image_id = lookup_image (it->f, value);
2545 it->position = start_pos;
2546 it->object = NILP (object) ? it->w->buffer : object;
2547 it->method = next_element_from_image;
2548
02513cdd 2549 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
2550 `display' property yet. The call to pop_it in
2551 set_iterator_to_next will clean this up. */
2552 *position = start_pos;
2553 }
2554#endif /* HAVE_WINDOW_SYSTEM */
2555 }
a21a3943
GM
2556 else
2557 /* Invalid property or property not supported. Restore
2558 the position to what it was before. */
2559 *position = start_pos;
5f5c8ee5
GM
2560 }
2561
2562 return space_or_image_found_p;
2563}
2564
2565
06568bbf
GM
2566/* Check if PROP is a display sub-property value whose text should be
2567 treated as intangible. */
2568
2569static int
2570single_display_prop_intangible_p (prop)
2571 Lisp_Object prop;
2572{
2573 /* Skip over `when FORM'. */
2574 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2575 {
2576 prop = XCDR (prop);
2577 if (!CONSP (prop))
2578 return 0;
2579 prop = XCDR (prop);
2580 }
2581
2582 if (!CONSP (prop))
2583 return 0;
2584
2585 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2586 we don't need to treat text as intangible. */
2587 if (EQ (XCAR (prop), Qmargin))
2588 {
2589 prop = XCDR (prop);
2590 if (!CONSP (prop))
2591 return 0;
2592
2593 prop = XCDR (prop);
2594 if (!CONSP (prop)
2595 || EQ (XCAR (prop), Qleft_margin)
2596 || EQ (XCAR (prop), Qright_margin))
2597 return 0;
2598 }
2599
2600 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2601}
2602
2603
2604/* Check if PROP is a display property value whose text should be
2605 treated as intangible. */
2606
2607int
2608display_prop_intangible_p (prop)
2609 Lisp_Object prop;
2610{
2611 if (CONSP (prop)
2612 && CONSP (XCAR (prop))
2613 && !EQ (Qmargin, XCAR (XCAR (prop))))
2614 {
2615 /* A list of sub-properties. */
2616 while (CONSP (prop))
2617 {
2618 if (single_display_prop_intangible_p (XCAR (prop)))
2619 return 1;
2620 prop = XCDR (prop);
2621 }
2622 }
2623 else if (VECTORP (prop))
2624 {
2625 /* A vector of sub-properties. */
2626 int i;
2627 for (i = 0; i < XVECTOR (prop)->size; ++i)
2628 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2629 return 1;
2630 }
2631 else
2632 return single_display_prop_intangible_p (prop);
2633
2634 return 0;
2635}
2636
5f5c8ee5 2637\f
260a86a0
KH
2638/***********************************************************************
2639 `composition' property
2640 ***********************************************************************/
2641
2642/* Set up iterator IT from `composition' property at its current
2643 position. Called from handle_stop. */
2644
2645static enum prop_handled
2646handle_composition_prop (it)
2647 struct it *it;
2648{
2649 Lisp_Object prop, string;
2650 int pos, pos_byte, end;
2651 enum prop_handled handled = HANDLED_NORMALLY;
2652
2653 if (STRINGP (it->string))
2654 {
2655 pos = IT_STRING_CHARPOS (*it);
2656 pos_byte = IT_STRING_BYTEPOS (*it);
2657 string = it->string;
2658 }
2659 else
2660 {
2661 pos = IT_CHARPOS (*it);
2662 pos_byte = IT_BYTEPOS (*it);
2663 string = Qnil;
2664 }
2665
2666 /* If there's a valid composition and point is not inside of the
2667 composition (in the case that the composition is from the current
2668 buffer), draw a glyph composed from the composition components. */
2669 if (find_composition (pos, -1, &pos, &end, &prop, string)
2670 && COMPOSITION_VALID_P (pos, end, prop)
2671 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2672 {
2673 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2674
2675 if (id >= 0)
2676 {
2677 it->method = next_element_from_composition;
2678 it->cmp_id = id;
2679 it->cmp_len = COMPOSITION_LENGTH (prop);
2680 /* For a terminal, draw only the first character of the
2681 components. */
2682 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2683 it->len = (STRINGP (it->string)
2684 ? string_char_to_byte (it->string, end)
2685 : CHAR_TO_BYTE (end)) - pos_byte;
2686 it->stop_charpos = end;
2687 handled = HANDLED_RETURN;
2688 }
2689 }
2690
2691 return handled;
2692}
2693
2694
2695\f
5f5c8ee5
GM
2696/***********************************************************************
2697 Overlay strings
2698 ***********************************************************************/
2699
2700/* The following structure is used to record overlay strings for
2701 later sorting in load_overlay_strings. */
2702
2703struct overlay_entry
2704{
2970b9be 2705 Lisp_Object overlay;
5f5c8ee5
GM
2706 Lisp_Object string;
2707 int priority;
2708 int after_string_p;
2709};
2710
2711
2712/* Set up iterator IT from overlay strings at its current position.
2713 Called from handle_stop. */
2714
2715static enum prop_handled
2716handle_overlay_change (it)
2717 struct it *it;
2718{
2970b9be
GM
2719 if (!STRINGP (it->string) && get_overlay_strings (it))
2720 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 2721 else
2970b9be 2722 return HANDLED_NORMALLY;
5f5c8ee5
GM
2723}
2724
2725
2726/* Set up the next overlay string for delivery by IT, if there is an
2727 overlay string to deliver. Called by set_iterator_to_next when the
2728 end of the current overlay string is reached. If there are more
2729 overlay strings to display, IT->string and
2730 IT->current.overlay_string_index are set appropriately here.
2731 Otherwise IT->string is set to nil. */
2732
2733static void
2734next_overlay_string (it)
2735 struct it *it;
2736{
2737 ++it->current.overlay_string_index;
2738 if (it->current.overlay_string_index == it->n_overlay_strings)
2739 {
2740 /* No more overlay strings. Restore IT's settings to what
2741 they were before overlay strings were processed, and
2742 continue to deliver from current_buffer. */
2743 pop_it (it);
2744 xassert (it->stop_charpos >= BEGV
2745 && it->stop_charpos <= it->end_charpos);
2746 it->string = Qnil;
2747 it->current.overlay_string_index = -1;
2748 SET_TEXT_POS (it->current.string_pos, -1, -1);
2749 it->n_overlay_strings = 0;
2750 it->method = next_element_from_buffer;
2970b9be
GM
2751
2752 /* If we're at the end of the buffer, record that we have
2753 processed the overlay strings there already, so that
2754 next_element_from_buffer doesn't try it again. */
2755 if (IT_CHARPOS (*it) >= it->end_charpos)
2756 it->overlay_strings_at_end_processed_p = 1;
5f5c8ee5
GM
2757 }
2758 else
2759 {
2760 /* There are more overlay strings to process. If
2761 IT->current.overlay_string_index has advanced to a position
2762 where we must load IT->overlay_strings with more strings, do
2763 it. */
2764 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2765
2766 if (it->current.overlay_string_index && i == 0)
2767 load_overlay_strings (it);
2768
2769 /* Initialize IT to deliver display elements from the overlay
2770 string. */
2771 it->string = it->overlay_strings[i];
2772 it->multibyte_p = STRING_MULTIBYTE (it->string);
2773 SET_TEXT_POS (it->current.string_pos, 0, 0);
2774 it->method = next_element_from_string;
2775 it->stop_charpos = 0;
2776 }
2777
2778 CHECK_IT (it);
2779}
2780
2781
2782/* Compare two overlay_entry structures E1 and E2. Used as a
2783 comparison function for qsort in load_overlay_strings. Overlay
2784 strings for the same position are sorted so that
2785
2970b9be
GM
2786 1. All after-strings come in front of before-strings, except
2787 when they come from the same overlay.
5f5c8ee5
GM
2788
2789 2. Within after-strings, strings are sorted so that overlay strings
2790 from overlays with higher priorities come first.
2791
2792 2. Within before-strings, strings are sorted so that overlay
2793 strings from overlays with higher priorities come last.
2794
2795 Value is analogous to strcmp. */
2796
2797
2798static int
2799compare_overlay_entries (e1, e2)
2800 void *e1, *e2;
2801{
2802 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2803 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2804 int result;
2805
2806 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
2807 {
2808 /* Let after-strings appear in front of before-strings if
2809 they come from different overlays. */
2810 if (EQ (entry1->overlay, entry2->overlay))
2811 result = entry1->after_string_p ? 1 : -1;
2812 else
2813 result = entry1->after_string_p ? -1 : 1;
2814 }
5f5c8ee5
GM
2815 else if (entry1->after_string_p)
2816 /* After-strings sorted in order of decreasing priority. */
2817 result = entry2->priority - entry1->priority;
2818 else
2819 /* Before-strings sorted in order of increasing priority. */
2820 result = entry1->priority - entry2->priority;
2821
2822 return result;
2823}
2824
2825
2826/* Load the vector IT->overlay_strings with overlay strings from IT's
2827 current buffer position. Set IT->n_overlays to the total number of
2828 overlay strings found.
2829
2830 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2831 a time. On entry into load_overlay_strings,
2832 IT->current.overlay_string_index gives the number of overlay
2833 strings that have already been loaded by previous calls to this
2834 function.
2835
2970b9be
GM
2836 IT->add_overlay_start contains an additional overlay start
2837 position to consider for taking overlay strings from, if non-zero.
2838 This position comes into play when the overlay has an `invisible'
2839 property, and both before and after-strings. When we've skipped to
2840 the end of the overlay, because of its `invisible' property, we
2841 nevertheless want its before-string to appear.
2842 IT->add_overlay_start will contain the overlay start position
2843 in this case.
2844
5f5c8ee5
GM
2845 Overlay strings are sorted so that after-string strings come in
2846 front of before-string strings. Within before and after-strings,
2847 strings are sorted by overlay priority. See also function
2848 compare_overlay_entries. */
2849
2850static void
2851load_overlay_strings (it)
2852 struct it *it;
2853{
2854 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2855 Lisp_Object ov, overlay, window, str;
2856 int start, end;
2857 int size = 20;
2858 int n = 0, i, j;
2859 struct overlay_entry *entries
2860 = (struct overlay_entry *) alloca (size * sizeof *entries);
2861
2862 /* Append the overlay string STRING of overlay OVERLAY to vector
2863 `entries' which has size `size' and currently contains `n'
2864 elements. AFTER_P non-zero means STRING is an after-string of
2865 OVERLAY. */
2866#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2867 do \
2868 { \
2869 Lisp_Object priority; \
2870 \
2871 if (n == size) \
2872 { \
2873 int new_size = 2 * size; \
2874 struct overlay_entry *old = entries; \
2875 entries = \
2876 (struct overlay_entry *) alloca (new_size \
2877 * sizeof *entries); \
2878 bcopy (old, entries, size * sizeof *entries); \
2879 size = new_size; \
2880 } \
2881 \
2882 entries[n].string = (STRING); \
2970b9be 2883 entries[n].overlay = (OVERLAY); \
5f5c8ee5 2884 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 2885 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
2886 entries[n].after_string_p = (AFTER_P); \
2887 ++n; \
2888 } \
2889 while (0)
2890
2891 /* Process overlay before the overlay center. */
2970b9be 2892 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 2893 {
9472f927 2894 overlay = XCAR (ov);
5f5c8ee5
GM
2895 xassert (OVERLAYP (overlay));
2896 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2897 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2898
2899 if (end < IT_CHARPOS (*it))
2900 break;
2901
2902 /* Skip this overlay if it doesn't start or end at IT's current
2903 position. */
2970b9be
GM
2904 if (end != IT_CHARPOS (*it)
2905 && start != IT_CHARPOS (*it)
2906 && it->add_overlay_start != IT_CHARPOS (*it))
5f5c8ee5
GM
2907 continue;
2908
2909 /* Skip this overlay if it doesn't apply to IT->w. */
2910 window = Foverlay_get (overlay, Qwindow);
2911 if (WINDOWP (window) && XWINDOW (window) != it->w)
2912 continue;
2913
2914 /* If overlay has a non-empty before-string, record it. */
2970b9be
GM
2915 if ((start == IT_CHARPOS (*it)
2916 || start == it->add_overlay_start)
5f5c8ee5
GM
2917 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2918 && XSTRING (str)->size)
2919 RECORD_OVERLAY_STRING (overlay, str, 0);
2920
2921 /* If overlay has a non-empty after-string, record it. */
2922 if (end == IT_CHARPOS (*it)
2923 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2924 && XSTRING (str)->size)
2925 RECORD_OVERLAY_STRING (overlay, str, 1);
2926 }
2927
2928 /* Process overlays after the overlay center. */
2970b9be 2929 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 2930 {
9472f927 2931 overlay = XCAR (ov);
5f5c8ee5
GM
2932 xassert (OVERLAYP (overlay));
2933 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2934 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2935
2936 if (start > IT_CHARPOS (*it))
2937 break;
2938
2939 /* Skip this overlay if it doesn't start or end at IT's current
2940 position. */
2970b9be
GM
2941 if (end != IT_CHARPOS (*it)
2942 && start != IT_CHARPOS (*it)
2943 && it->add_overlay_start != IT_CHARPOS (*it))
5f5c8ee5
GM
2944 continue;
2945
2946 /* Skip this overlay if it doesn't apply to IT->w. */
2947 window = Foverlay_get (overlay, Qwindow);
2948 if (WINDOWP (window) && XWINDOW (window) != it->w)
2949 continue;
2950
2951 /* If overlay has a non-empty before-string, record it. */
2970b9be
GM
2952 if ((start == IT_CHARPOS (*it)
2953 || start == it->add_overlay_start)
5f5c8ee5
GM
2954 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2955 && XSTRING (str)->size)
2956 RECORD_OVERLAY_STRING (overlay, str, 0);
2957
2958 /* If overlay has a non-empty after-string, record it. */
2959 if (end == IT_CHARPOS (*it)
2960 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2961 && XSTRING (str)->size)
2962 RECORD_OVERLAY_STRING (overlay, str, 1);
2963 }
2964
2965#undef RECORD_OVERLAY_STRING
2966
2967 /* Sort entries. */
2970b9be
GM
2968 if (n)
2969 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
2970
2971 /* Record the total number of strings to process. */
2972 it->n_overlay_strings = n;
2973
2974 /* IT->current.overlay_string_index is the number of overlay strings
2975 that have already been consumed by IT. Copy some of the
2976 remaining overlay strings to IT->overlay_strings. */
2977 i = 0;
2978 j = it->current.overlay_string_index;
2979 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2980 it->overlay_strings[i++] = entries[j++].string;
2970b9be 2981
5f5c8ee5
GM
2982 CHECK_IT (it);
2983}
2984
2985
2986/* Get the first chunk of overlay strings at IT's current buffer
2987 position. Value is non-zero if at least one overlay string was
2988 found. */
2989
2990static int
2991get_overlay_strings (it)
2992 struct it *it;
2993{
2994 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2995 process. This fills IT->overlay_strings with strings, and sets
2996 IT->n_overlay_strings to the total number of strings to process.
2997 IT->pos.overlay_string_index has to be set temporarily to zero
2998 because load_overlay_strings needs this; it must be set to -1
2999 when no overlay strings are found because a zero value would
3000 indicate a position in the first overlay string. */
3001 it->current.overlay_string_index = 0;
3002 load_overlay_strings (it);
3003
3004 /* If we found overlay strings, set up IT to deliver display
3005 elements from the first one. Otherwise set up IT to deliver
3006 from current_buffer. */
3007 if (it->n_overlay_strings)
3008 {
3009 /* Make sure we know settings in current_buffer, so that we can
3010 restore meaningful values when we're done with the overlay
3011 strings. */
3012 compute_stop_pos (it);
3013 xassert (it->face_id >= 0);
3014
3015 /* Save IT's settings. They are restored after all overlay
3016 strings have been processed. */
3017 xassert (it->sp == 0);
3018 push_it (it);
3019
3020 /* Set up IT to deliver display elements from the first overlay
3021 string. */
3022 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3023 it->stop_charpos = 0;
3024 it->string = it->overlay_strings[0];
3025 it->multibyte_p = STRING_MULTIBYTE (it->string);
3026 xassert (STRINGP (it->string));
3027 it->method = next_element_from_string;
3028 }
3029 else
3030 {
3031 it->string = Qnil;
3032 it->current.overlay_string_index = -1;
3033 it->method = next_element_from_buffer;
3034 }
3035
3036 CHECK_IT (it);
3037
3038 /* Value is non-zero if we found at least one overlay string. */
3039 return STRINGP (it->string);
3040}
3041
3042
3043\f
3044/***********************************************************************
3045 Saving and restoring state
3046 ***********************************************************************/
3047
3048/* Save current settings of IT on IT->stack. Called, for example,
3049 before setting up IT for an overlay string, to be able to restore
3050 IT's settings to what they were after the overlay string has been
3051 processed. */
3052
3053static void
3054push_it (it)
3055 struct it *it;
3056{
3057 struct iterator_stack_entry *p;
3058
3059 xassert (it->sp < 2);
3060 p = it->stack + it->sp;
3061
3062 p->stop_charpos = it->stop_charpos;
3063 xassert (it->face_id >= 0);
3064 p->face_id = it->face_id;
3065 p->string = it->string;
3066 p->pos = it->current;
3067 p->end_charpos = it->end_charpos;
3068 p->string_nchars = it->string_nchars;
3069 p->area = it->area;
3070 p->multibyte_p = it->multibyte_p;
3071 p->space_width = it->space_width;
3072 p->font_height = it->font_height;
3073 p->voffset = it->voffset;
3074 p->string_from_display_prop_p = it->string_from_display_prop_p;
3075 ++it->sp;
3076}
3077
3078
3079/* Restore IT's settings from IT->stack. Called, for example, when no
3080 more overlay strings must be processed, and we return to delivering
3081 display elements from a buffer, or when the end of a string from a
3082 `display' property is reached and we return to delivering display
3083 elements from an overlay string, or from a buffer. */
3084
3085static void
3086pop_it (it)
3087 struct it *it;
3088{
3089 struct iterator_stack_entry *p;
3090
3091 xassert (it->sp > 0);
3092 --it->sp;
3093 p = it->stack + it->sp;
3094 it->stop_charpos = p->stop_charpos;
3095 it->face_id = p->face_id;
3096 it->string = p->string;
3097 it->current = p->pos;
3098 it->end_charpos = p->end_charpos;
3099 it->string_nchars = p->string_nchars;
3100 it->area = p->area;
3101 it->multibyte_p = p->multibyte_p;
3102 it->space_width = p->space_width;
3103 it->font_height = p->font_height;
3104 it->voffset = p->voffset;
3105 it->string_from_display_prop_p = p->string_from_display_prop_p;
3106}
3107
3108
3109\f
3110/***********************************************************************
3111 Moving over lines
3112 ***********************************************************************/
3113
3114/* Set IT's current position to the previous line start. */
3115
3116static void
3117back_to_previous_line_start (it)
3118 struct it *it;
3119{
3120 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3121 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3122}
3123
3124
3125/* Set IT's current position to the next line start. */
3126
3127static void
3128forward_to_next_line_start (it)
3129 struct it *it;
3130{
3131 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3133}
3134
3135
3136/* Set IT's current position to the previous visible line start. Skip
3137 invisible text that is so either due to text properties or due to
3138 selective display. Caution: this does not change IT->current_x and
3139 IT->hpos. */
3140
3141static void
3142back_to_previous_visible_line_start (it)
3143 struct it *it;
3144{
3145 int visible_p = 0;
3146
3147 /* Go back one newline if not on BEGV already. */
3148 if (IT_CHARPOS (*it) > BEGV)
3149 back_to_previous_line_start (it);
3150
3151 /* Move over lines that are invisible because of selective display
3152 or text properties. */
3153 while (IT_CHARPOS (*it) > BEGV
3154 && !visible_p)
3155 {
3156 visible_p = 1;
3157
3158 /* If selective > 0, then lines indented more than that values
3159 are invisible. */
3160 if (it->selective > 0
3161 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3162 it->selective))
3163 visible_p = 0;
5f5c8ee5
GM
3164 else
3165 {
3166 Lisp_Object prop;
3167
6fc556fd
KR
3168 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3169 Qinvisible, it->window);
5f5c8ee5
GM
3170 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3171 visible_p = 0;
3172 }
5f5c8ee5
GM
3173
3174 /* Back one more newline if the current one is invisible. */
3175 if (!visible_p)
3176 back_to_previous_line_start (it);
3177 }
3178
3179 xassert (IT_CHARPOS (*it) >= BEGV);
3180 xassert (IT_CHARPOS (*it) == BEGV
3181 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3182 CHECK_IT (it);
3183}
3184
3185
3186/* Reseat iterator IT at the previous visible line start. Skip
3187 invisible text that is so either due to text properties or due to
3188 selective display. At the end, update IT's overlay information,
3189 face information etc. */
3190
3191static void
3192reseat_at_previous_visible_line_start (it)
3193 struct it *it;
3194{
3195 back_to_previous_visible_line_start (it);
3196 reseat (it, it->current.pos, 1);
3197 CHECK_IT (it);
3198}
3199
3200
3201/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3202 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3203 preceding the line start. Skip over invisible text that is so
3204 because of selective display. Compute faces, overlays etc at the
3205 new position. Note that this function does not skip over text that
3206 is invisible because of text properties. */
5f5c8ee5
GM
3207
3208static void
312246d1 3209reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3210 struct it *it;
312246d1 3211 int on_newline_p;
5f5c8ee5
GM
3212{
3213 /* Restore the buffer position when currently not delivering display
3214 elements from the current buffer. This is the case, for example,
3215 when called at the end of a truncated overlay string. */
3216 while (it->sp)
3217 pop_it (it);
3218 it->method = next_element_from_buffer;
3219
3220 /* Otherwise, scan_buffer would not work. */
3221 if (IT_CHARPOS (*it) < ZV)
3222 {
3223 /* If on a newline, advance past it. Otherwise, find the next
3224 newline which automatically gives us the position following
3225 the newline. */
3226 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3227 {
3228 ++IT_CHARPOS (*it);
3229 ++IT_BYTEPOS (*it);
3230 }
3231 else
3232 forward_to_next_line_start (it);
3233
3234 /* We must either have reached the end of the buffer or end up
3235 after a newline. */
3236 xassert (IT_CHARPOS (*it) == ZV
3237 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3238
3239 /* Skip over lines that are invisible because they are indented
3240 more than the value of IT->selective. */
3241 if (it->selective > 0)
3242 while (IT_CHARPOS (*it) < ZV
3243 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3244 it->selective))
3245 forward_to_next_line_start (it);
312246d1
GM
3246
3247 /* Position on the newline if we should. */
13f19968
GM
3248 if (on_newline_p
3249 && IT_CHARPOS (*it) > BEGV
3250 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
312246d1
GM
3251 {
3252 --IT_CHARPOS (*it);
3253 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3254 }
5f5c8ee5
GM
3255
3256 /* Set the iterator there. The 0 as the last parameter of
3257 reseat means don't force a text property lookup. The lookup
3258 is then only done if we've skipped past the iterator's
3259 check_charpos'es. This optimization is important because
3260 text property lookups tend to be expensive. */
3261 reseat (it, it->current.pos, 0);
3262 }
3263
3264 CHECK_IT (it);
3265}
3266
3267
3268\f
3269/***********************************************************************
3270 Changing an iterator's position
3271***********************************************************************/
3272
3273/* Change IT's current position to POS in current_buffer. If FORCE_P
3274 is non-zero, always check for text properties at the new position.
3275 Otherwise, text properties are only looked up if POS >=
3276 IT->check_charpos of a property. */
3277
3278static void
3279reseat (it, pos, force_p)
3280 struct it *it;
3281 struct text_pos pos;
3282 int force_p;
3283{
3284 int original_pos = IT_CHARPOS (*it);
3285
3286 reseat_1 (it, pos, 0);
3287
3288 /* Determine where to check text properties. Avoid doing it
3289 where possible because text property lookup is very expensive. */
3290 if (force_p
3291 || CHARPOS (pos) > it->stop_charpos
3292 || CHARPOS (pos) < original_pos)
3293 handle_stop (it);
3294
3295 CHECK_IT (it);
3296}
3297
3298
3299/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3300 IT->stop_pos to POS, also. */
3301
3302static void
3303reseat_1 (it, pos, set_stop_p)
3304 struct it *it;
3305 struct text_pos pos;
3306 int set_stop_p;
3307{
3308 /* Don't call this function when scanning a C string. */
3309 xassert (it->s == NULL);
3310
3311 /* POS must be a reasonable value. */
3312 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3313
3314 it->current.pos = it->position = pos;
3315 XSETBUFFER (it->object, current_buffer);
3316 it->dpvec = NULL;
3317 it->current.dpvec_index = -1;
3318 it->current.overlay_string_index = -1;
3319 IT_STRING_CHARPOS (*it) = -1;
3320 IT_STRING_BYTEPOS (*it) = -1;
3321 it->string = Qnil;
3322 it->method = next_element_from_buffer;
3323 it->sp = 0;
3324
3325 if (set_stop_p)
3326 it->stop_charpos = CHARPOS (pos);
3327}
3328
3329
3330/* Set up IT for displaying a string, starting at CHARPOS in window W.
3331 If S is non-null, it is a C string to iterate over. Otherwise,
3332 STRING gives a Lisp string to iterate over.
3333
3334 If PRECISION > 0, don't return more then PRECISION number of
3335 characters from the string.
3336
3337 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3338 characters have been returned. FIELD_WIDTH < 0 means an infinite
3339 field width.
3340
3341 MULTIBYTE = 0 means disable processing of multibyte characters,
3342 MULTIBYTE > 0 means enable it,
3343 MULTIBYTE < 0 means use IT->multibyte_p.
3344
3345 IT must be initialized via a prior call to init_iterator before
3346 calling this function. */
3347
3348static void
3349reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3350 struct it *it;
3351 unsigned char *s;
3352 Lisp_Object string;
3353 int charpos;
3354 int precision, field_width, multibyte;
3355{
3356 /* No region in strings. */
3357 it->region_beg_charpos = it->region_end_charpos = -1;
3358
3359 /* No text property checks performed by default, but see below. */
3360 it->stop_charpos = -1;
3361
3362 /* Set iterator position and end position. */
3363 bzero (&it->current, sizeof it->current);
3364 it->current.overlay_string_index = -1;
3365 it->current.dpvec_index = -1;
5f5c8ee5
GM
3366 xassert (charpos >= 0);
3367
3368 /* Use the setting of MULTIBYTE if specified. */
3369 if (multibyte >= 0)
3370 it->multibyte_p = multibyte > 0;
3371
3372 if (s == NULL)
3373 {
3374 xassert (STRINGP (string));
3375 it->string = string;
3376 it->s = NULL;
3377 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3378 it->method = next_element_from_string;
3379 it->current.string_pos = string_pos (charpos, string);
3380 }
3381 else
3382 {
3383 it->s = s;
3384 it->string = Qnil;
3385
3386 /* Note that we use IT->current.pos, not it->current.string_pos,
3387 for displaying C strings. */
3388 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3389 if (it->multibyte_p)
3390 {
3391 it->current.pos = c_string_pos (charpos, s, 1);
3392 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3393 }
3394 else
3395 {
3396 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3397 it->end_charpos = it->string_nchars = strlen (s);
3398 }
3399
3400 it->method = next_element_from_c_string;
3401 }
3402
3403 /* PRECISION > 0 means don't return more than PRECISION characters
3404 from the string. */
3405 if (precision > 0 && it->end_charpos - charpos > precision)
3406 it->end_charpos = it->string_nchars = charpos + precision;
3407
3408 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3409 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3410 FIELD_WIDTH < 0 means infinite field width. This is useful for
3411 padding with `-' at the end of a mode line. */
3412 if (field_width < 0)
3413 field_width = INFINITY;
3414 if (field_width > it->end_charpos - charpos)
3415 it->end_charpos = charpos + field_width;
3416
3417 /* Use the standard display table for displaying strings. */
3418 if (DISP_TABLE_P (Vstandard_display_table))
3419 it->dp = XCHAR_TABLE (Vstandard_display_table);
3420
3421 it->stop_charpos = charpos;
3422 CHECK_IT (it);
3423}
3424
3425
3426\f
3427/***********************************************************************
3428 Iteration
3429 ***********************************************************************/
3430
3431/* Load IT's display element fields with information about the next
3432 display element from the current position of IT. Value is zero if
3433 end of buffer (or C string) is reached. */
3434
3435int
3436get_next_display_element (it)
3437 struct it *it;
3438{
3439 /* Non-zero means that we found an display element. Zero means that
3440 we hit the end of what we iterate over. Performance note: the
3441 function pointer `method' used here turns out to be faster than
3442 using a sequence of if-statements. */
3443 int success_p = (*it->method) (it);
5f5c8ee5
GM
3444
3445 if (it->what == IT_CHARACTER)
3446 {
3447 /* Map via display table or translate control characters.
3448 IT->c, IT->len etc. have been set to the next character by
3449 the function call above. If we have a display table, and it
3450 contains an entry for IT->c, translate it. Don't do this if
3451 IT->c itself comes from a display table, otherwise we could
3452 end up in an infinite recursion. (An alternative could be to
3453 count the recursion depth of this function and signal an
3454 error when a certain maximum depth is reached.) Is it worth
3455 it? */
3456 if (success_p && it->dpvec == NULL)
3457 {
3458 Lisp_Object dv;
3459
3460 if (it->dp
3461 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3462 VECTORP (dv)))
3463 {
3464 struct Lisp_Vector *v = XVECTOR (dv);
3465
3466 /* Return the first character from the display table
3467 entry, if not empty. If empty, don't display the
3468 current character. */
3469 if (v->size)
3470 {
3471 it->dpvec_char_len = it->len;
3472 it->dpvec = v->contents;
3473 it->dpend = v->contents + v->size;
3474 it->current.dpvec_index = 0;
3475 it->method = next_element_from_display_vector;
3476 }
3477
3478 success_p = get_next_display_element (it);
3479 }
3480
3481 /* Translate control characters into `\003' or `^C' form.
3482 Control characters coming from a display table entry are
3483 currently not translated because we use IT->dpvec to hold
3484 the translation. This could easily be changed but I
197516c2
KH
3485 don't believe that it is worth doing.
3486
3487 Non-printable multibyte characters are also translated
3488 octal form. */
5f5c8ee5
GM
3489 else if ((it->c < ' '
3490 && (it->area != TEXT_AREA
c6e89d6c 3491 || (it->c != '\n' && it->c != '\t')))
54c85a23 3492 || (it->c >= 127
197516c2
KH
3493 && it->len == 1)
3494 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
3495 {
3496 /* IT->c is a control character which must be displayed
3497 either as '\003' or as `^C' where the '\\' and '^'
3498 can be defined in the display table. Fill
3499 IT->ctl_chars with glyphs for what we have to
3500 display. Then, set IT->dpvec to these glyphs. */
3501 GLYPH g;
3502
54c85a23 3503 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3504 {
3505 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3506 if (it->dp
3507 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3508 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3509 g = XINT (DISP_CTRL_GLYPH (it->dp));
3510 else
3511 g = FAST_MAKE_GLYPH ('^', 0);
3512 XSETINT (it->ctl_chars[0], g);
3513
3514 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3515 XSETINT (it->ctl_chars[1], g);
3516
3517 /* Set up IT->dpvec and return first character from it. */
3518 it->dpvec_char_len = it->len;
3519 it->dpvec = it->ctl_chars;
3520 it->dpend = it->dpvec + 2;
3521 it->current.dpvec_index = 0;
3522 it->method = next_element_from_display_vector;
3523 get_next_display_element (it);
3524 }
3525 else
3526 {
260a86a0 3527 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 3528 int len;
197516c2
KH
3529 int i;
3530 GLYPH escape_glyph;
3531
5f5c8ee5
GM
3532 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3533 if (it->dp
3534 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3535 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 3536 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 3537 else
197516c2
KH
3538 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3539
c5924f47
KH
3540 if (SINGLE_BYTE_CHAR_P (it->c))
3541 str[0] = it->c, len = 1;
3542 else
3543 len = CHAR_STRING (it->c, str);
3544
197516c2
KH
3545 for (i = 0; i < len; i++)
3546 {
3547 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3548 /* Insert three more glyphs into IT->ctl_chars for
3549 the octal display of the character. */
3550 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3551 XSETINT (it->ctl_chars[i * 4 + 1], g);
3552 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3553 XSETINT (it->ctl_chars[i * 4 + 2], g);
3554 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3555 XSETINT (it->ctl_chars[i * 4 + 3], g);
3556 }
5f5c8ee5
GM
3557
3558 /* Set up IT->dpvec and return the first character
3559 from it. */
3560 it->dpvec_char_len = it->len;
3561 it->dpvec = it->ctl_chars;
197516c2 3562 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
3563 it->current.dpvec_index = 0;
3564 it->method = next_element_from_display_vector;
3565 get_next_display_element (it);
3566 }
3567 }
3568 }
3569
980806b6
KH
3570 /* Adjust face id for a multibyte character. There are no
3571 multibyte character in unibyte text. */
5f5c8ee5
GM
3572 if (it->multibyte_p
3573 && success_p
980806b6 3574 && FRAME_WINDOW_P (it->f))
5f5c8ee5 3575 {
980806b6
KH
3576 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3577 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
3578 }
3579 }
3580
3581 /* Is this character the last one of a run of characters with
3582 box? If yes, set IT->end_of_box_run_p to 1. */
3583 if (it->face_box_p
3584 && it->s == NULL)
3585 {
3586 int face_id;
3587 struct face *face;
3588
3589 it->end_of_box_run_p
3590 = ((face_id = face_after_it_pos (it),
3591 face_id != it->face_id)
3592 && (face = FACE_FROM_ID (it->f, face_id),
3593 face->box == FACE_NO_BOX));
3594 }
3595
3596 /* Value is 0 if end of buffer or string reached. */
3597 return success_p;
3598}
3599
3600
3601/* Move IT to the next display element.
3602
3603 Functions get_next_display_element and set_iterator_to_next are
3604 separate because I find this arrangement easier to handle than a
3605 get_next_display_element function that also increments IT's
3606 position. The way it is we can first look at an iterator's current
3607 display element, decide whether it fits on a line, and if it does,
3608 increment the iterator position. The other way around we probably
3609 would either need a flag indicating whether the iterator has to be
3610 incremented the next time, or we would have to implement a
3611 decrement position function which would not be easy to write. */
3612
3613void
3614set_iterator_to_next (it)
3615 struct it *it;
3616{
3617 if (it->method == next_element_from_buffer)
3618 {
3619 /* The current display element of IT is a character from
3620 current_buffer. Advance in the buffer, and maybe skip over
3621 invisible lines that are so because of selective display. */
3622 if (ITERATOR_AT_END_OF_LINE_P (it))
312246d1 3623 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
3624 else
3625 {
3626 xassert (it->len != 0);
3627 IT_BYTEPOS (*it) += it->len;
3628 IT_CHARPOS (*it) += 1;
3629 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3630 }
3631 }
260a86a0
KH
3632 else if (it->method == next_element_from_composition)
3633 {
3634 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3635 if (STRINGP (it->string))
3636 {
3637 IT_STRING_BYTEPOS (*it) += it->len;
3638 IT_STRING_CHARPOS (*it) += it->cmp_len;
3639 it->method = next_element_from_string;
3640 goto consider_string_end;
3641 }
3642 else
3643 {
3644 IT_BYTEPOS (*it) += it->len;
3645 IT_CHARPOS (*it) += it->cmp_len;
3646 it->method = next_element_from_buffer;
3647 }
3648 }
5f5c8ee5
GM
3649 else if (it->method == next_element_from_c_string)
3650 {
3651 /* Current display element of IT is from a C string. */
3652 IT_BYTEPOS (*it) += it->len;
3653 IT_CHARPOS (*it) += 1;
3654 }
3655 else if (it->method == next_element_from_display_vector)
3656 {
3657 /* Current display element of IT is from a display table entry.
3658 Advance in the display table definition. Reset it to null if
3659 end reached, and continue with characters from buffers/
3660 strings. */
3661 ++it->current.dpvec_index;
286bcbc9 3662
980806b6
KH
3663 /* Restore face of the iterator to what they were before the
3664 display vector entry (these entries may contain faces). */
5f5c8ee5 3665 it->face_id = it->saved_face_id;
286bcbc9 3666
5f5c8ee5
GM
3667 if (it->dpvec + it->current.dpvec_index == it->dpend)
3668 {
3669 if (it->s)
3670 it->method = next_element_from_c_string;
3671 else if (STRINGP (it->string))
3672 it->method = next_element_from_string;
3673 else
3674 it->method = next_element_from_buffer;
3675
3676 it->dpvec = NULL;
3677 it->current.dpvec_index = -1;
3678
312246d1
GM
3679 /* Skip over characters which were displayed via IT->dpvec. */
3680 if (it->dpvec_char_len < 0)
3681 reseat_at_next_visible_line_start (it, 1);
3682 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
3683 {
3684 it->len = it->dpvec_char_len;
3685 set_iterator_to_next (it);
3686 }
3687 }
3688 }
3689 else if (it->method == next_element_from_string)
3690 {
3691 /* Current display element is a character from a Lisp string. */
3692 xassert (it->s == NULL && STRINGP (it->string));
3693 IT_STRING_BYTEPOS (*it) += it->len;
3694 IT_STRING_CHARPOS (*it) += 1;
3695
3696 consider_string_end:
3697
3698 if (it->current.overlay_string_index >= 0)
3699 {
3700 /* IT->string is an overlay string. Advance to the
3701 next, if there is one. */
3702 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3703 next_overlay_string (it);
3704 }
3705 else
3706 {
3707 /* IT->string is not an overlay string. If we reached
3708 its end, and there is something on IT->stack, proceed
3709 with what is on the stack. This can be either another
3710 string, this time an overlay string, or a buffer. */
3711 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3712 && it->sp > 0)
3713 {
3714 pop_it (it);
3715 if (!STRINGP (it->string))
3716 it->method = next_element_from_buffer;
3717 }
3718 }
3719 }
3720 else if (it->method == next_element_from_image
3721 || it->method == next_element_from_stretch)
3722 {
3723 /* The position etc with which we have to proceed are on
3724 the stack. The position may be at the end of a string,
3725 if the `display' property takes up the whole string. */
3726 pop_it (it);
3727 it->image_id = 0;
3728 if (STRINGP (it->string))
3729 {
3730 it->method = next_element_from_string;
3731 goto consider_string_end;
3732 }
3733 else
3734 it->method = next_element_from_buffer;
3735 }
3736 else
3737 /* There are no other methods defined, so this should be a bug. */
3738 abort ();
3739
3740 /* Reset flags indicating start and end of a sequence of
3741 characters with box. */
3742 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3743
3744 xassert (it->method != next_element_from_string
3745 || (STRINGP (it->string)
3746 && IT_STRING_CHARPOS (*it) >= 0));
3747}
3748
3749
3750/* Load IT's display element fields with information about the next
3751 display element which comes from a display table entry or from the
3752 result of translating a control character to one of the forms `^C'
3753 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3754
3755static int
3756next_element_from_display_vector (it)
3757 struct it *it;
3758{
3759 /* Precondition. */
3760 xassert (it->dpvec && it->current.dpvec_index >= 0);
3761
3762 /* Remember the current face id in case glyphs specify faces.
3763 IT's face is restored in set_iterator_to_next. */
3764 it->saved_face_id = it->face_id;
3765
3766 if (INTEGERP (*it->dpvec)
3767 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3768 {
3769 int lface_id;
3770 GLYPH g;
3771
3772 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3773 it->c = FAST_GLYPH_CHAR (g);
c5924f47 3774 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
3775
3776 /* The entry may contain a face id to use. Such a face id is
3777 the id of a Lisp face, not a realized face. A face id of
3778 zero means no face. */
3779 lface_id = FAST_GLYPH_FACE (g);
3780 if (lface_id)
3781 {
3782 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3783 if (face_id >= 0)
3784 {
3785 it->face_id = face_id;
5f5c8ee5
GM
3786 }
3787 }
3788 }
3789 else
3790 /* Display table entry is invalid. Return a space. */
3791 it->c = ' ', it->len = 1;
3792
3793 /* Don't change position and object of the iterator here. They are
3794 still the values of the character that had this display table
3795 entry or was translated, and that's what we want. */
3796 it->what = IT_CHARACTER;
3797 return 1;
3798}
3799
3800
3801/* Load IT with the next display element from Lisp string IT->string.
3802 IT->current.string_pos is the current position within the string.
3803 If IT->current.overlay_string_index >= 0, the Lisp string is an
3804 overlay string. */
3805
3806static int
3807next_element_from_string (it)
3808 struct it *it;
3809{
3810 struct text_pos position;
3811
3812 xassert (STRINGP (it->string));
3813 xassert (IT_STRING_CHARPOS (*it) >= 0);
3814 position = it->current.string_pos;
3815
3816 /* Time to check for invisible text? */
3817 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3818 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3819 {
3820 handle_stop (it);
3821
3822 /* Since a handler may have changed IT->method, we must
3823 recurse here. */
3824 return get_next_display_element (it);
3825 }
3826
3827 if (it->current.overlay_string_index >= 0)
3828 {
3829 /* Get the next character from an overlay string. In overlay
3830 strings, There is no field width or padding with spaces to
3831 do. */
3832 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3833 {
3834 it->what = IT_EOB;
3835 return 0;
3836 }
3837 else if (STRING_MULTIBYTE (it->string))
3838 {
3839 int remaining = (STRING_BYTES (XSTRING (it->string))
3840 - IT_STRING_BYTEPOS (*it));
3841 unsigned char *s = (XSTRING (it->string)->data
3842 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3843 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
3844 }
3845 else
3846 {
3847 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3848 it->len = 1;
3849 }
3850 }
3851 else
3852 {
3853 /* Get the next character from a Lisp string that is not an
3854 overlay string. Such strings come from the mode line, for
3855 example. We may have to pad with spaces, or truncate the
3856 string. See also next_element_from_c_string. */
3857 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3858 {
3859 it->what = IT_EOB;
3860 return 0;
3861 }
3862 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3863 {
3864 /* Pad with spaces. */
3865 it->c = ' ', it->len = 1;
3866 CHARPOS (position) = BYTEPOS (position) = -1;
3867 }
3868 else if (STRING_MULTIBYTE (it->string))
3869 {
3870 int maxlen = (STRING_BYTES (XSTRING (it->string))
3871 - IT_STRING_BYTEPOS (*it));
3872 unsigned char *s = (XSTRING (it->string)->data
3873 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3874 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
3875 }
3876 else
3877 {
3878 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3879 it->len = 1;
3880 }
3881 }
3882
3883 /* Record what we have and where it came from. Note that we store a
3884 buffer position in IT->position although it could arguably be a
3885 string position. */
3886 it->what = IT_CHARACTER;
3887 it->object = it->string;
3888 it->position = position;
3889 return 1;
3890}
3891
3892
3893/* Load IT with next display element from C string IT->s.
3894 IT->string_nchars is the maximum number of characters to return
3895 from the string. IT->end_charpos may be greater than
3896 IT->string_nchars when this function is called, in which case we
3897 may have to return padding spaces. Value is zero if end of string
3898 reached, including padding spaces. */
3899
3900static int
3901next_element_from_c_string (it)
3902 struct it *it;
3903{
3904 int success_p = 1;
3905
3906 xassert (it->s);
3907 it->what = IT_CHARACTER;
3908 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3909 it->object = Qnil;
3910
3911 /* IT's position can be greater IT->string_nchars in case a field
3912 width or precision has been specified when the iterator was
3913 initialized. */
3914 if (IT_CHARPOS (*it) >= it->end_charpos)
3915 {
3916 /* End of the game. */
3917 it->what = IT_EOB;
3918 success_p = 0;
3919 }
3920 else if (IT_CHARPOS (*it) >= it->string_nchars)
3921 {
3922 /* Pad with spaces. */
3923 it->c = ' ', it->len = 1;
3924 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3925 }
3926 else if (it->multibyte_p)
3927 {
3928 /* Implementation note: The calls to strlen apparently aren't a
3929 performance problem because there is no noticeable performance
3930 difference between Emacs running in unibyte or multibyte mode. */
3931 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
3932 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3933 maxlen, &it->len);
5f5c8ee5
GM
3934 }
3935 else
3936 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3937
3938 return success_p;
3939}
3940
3941
3942/* Set up IT to return characters from an ellipsis, if appropriate.
3943 The definition of the ellipsis glyphs may come from a display table
3944 entry. This function Fills IT with the first glyph from the
3945 ellipsis if an ellipsis is to be displayed. */
3946
13f19968 3947static int
5f5c8ee5
GM
3948next_element_from_ellipsis (it)
3949 struct it *it;
3950{
13f19968 3951 if (it->selective_display_ellipsis_p)
5f5c8ee5 3952 {
13f19968
GM
3953 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3954 {
3955 /* Use the display table definition for `...'. Invalid glyphs
3956 will be handled by the method returning elements from dpvec. */
3957 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3958 it->dpvec_char_len = it->len;
3959 it->dpvec = v->contents;
3960 it->dpend = v->contents + v->size;
3961 it->current.dpvec_index = 0;
3962 it->method = next_element_from_display_vector;
3963 }
3964 else
3965 {
3966 /* Use default `...' which is stored in default_invis_vector. */
3967 it->dpvec_char_len = it->len;
3968 it->dpvec = default_invis_vector;
3969 it->dpend = default_invis_vector + 3;
3970 it->current.dpvec_index = 0;
3971 it->method = next_element_from_display_vector;
3972 }
5f5c8ee5 3973 }
13f19968
GM
3974 else
3975 reseat_at_next_visible_line_start (it, 1);
3976
3977 return get_next_display_element (it);
5f5c8ee5
GM
3978}
3979
3980
3981/* Deliver an image display element. The iterator IT is already
3982 filled with image information (done in handle_display_prop). Value
3983 is always 1. */
3984
3985
3986static int
3987next_element_from_image (it)
3988 struct it *it;
3989{
3990 it->what = IT_IMAGE;
3991 return 1;
3992}
3993
3994
3995/* Fill iterator IT with next display element from a stretch glyph
3996 property. IT->object is the value of the text property. Value is
3997 always 1. */
3998
3999static int
4000next_element_from_stretch (it)
4001 struct it *it;
4002{
4003 it->what = IT_STRETCH;
4004 return 1;
4005}
4006
4007
4008/* Load IT with the next display element from current_buffer. Value
4009 is zero if end of buffer reached. IT->stop_charpos is the next
4010 position at which to stop and check for text properties or buffer
4011 end. */
4012
4013static int
4014next_element_from_buffer (it)
4015 struct it *it;
4016{
4017 int success_p = 1;
4018
4019 /* Check this assumption, otherwise, we would never enter the
4020 if-statement, below. */
4021 xassert (IT_CHARPOS (*it) >= BEGV
4022 && IT_CHARPOS (*it) <= it->stop_charpos);
4023
4024 if (IT_CHARPOS (*it) >= it->stop_charpos)
4025 {
4026 if (IT_CHARPOS (*it) >= it->end_charpos)
4027 {
4028 int overlay_strings_follow_p;
4029
4030 /* End of the game, except when overlay strings follow that
4031 haven't been returned yet. */
4032 if (it->overlay_strings_at_end_processed_p)
4033 overlay_strings_follow_p = 0;
4034 else
4035 {
4036 it->overlay_strings_at_end_processed_p = 1;
c880678e 4037 overlay_strings_follow_p = get_overlay_strings (it);
5f5c8ee5
GM
4038 }
4039
4040 if (overlay_strings_follow_p)
4041 success_p = get_next_display_element (it);
4042 else
4043 {
4044 it->what = IT_EOB;
4045 it->position = it->current.pos;
4046 success_p = 0;
4047 }
4048 }
4049 else
4050 {
4051 handle_stop (it);
4052 return get_next_display_element (it);
4053 }
4054 }
4055 else
4056 {
4057 /* No face changes, overlays etc. in sight, so just return a
4058 character from current_buffer. */
4059 unsigned char *p;
4060
4061 /* Maybe run the redisplay end trigger hook. Performance note:
4062 This doesn't seem to cost measurable time. */
4063 if (it->redisplay_end_trigger_charpos
4064 && it->glyph_row
4065 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4066 run_redisplay_end_trigger_hook (it);
4067
4068 /* Get the next character, maybe multibyte. */
4069 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4070 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4071 {
4072 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4073 - IT_BYTEPOS (*it));
4fdb80f2 4074 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4075 }
4076 else
4077 it->c = *p, it->len = 1;
4078
4079 /* Record what we have and where it came from. */
4080 it->what = IT_CHARACTER;;
4081 it->object = it->w->buffer;
4082 it->position = it->current.pos;
4083
4084 /* Normally we return the character found above, except when we
4085 really want to return an ellipsis for selective display. */
4086 if (it->selective)
4087 {
4088 if (it->c == '\n')
4089 {
4090 /* A value of selective > 0 means hide lines indented more
4091 than that number of columns. */
4092 if (it->selective > 0
4093 && IT_CHARPOS (*it) + 1 < ZV
4094 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4095 IT_BYTEPOS (*it) + 1,
4096 it->selective))
312246d1 4097 {
13f19968 4098 success_p = next_element_from_ellipsis (it);
312246d1
GM
4099 it->dpvec_char_len = -1;
4100 }
5f5c8ee5
GM
4101 }
4102 else if (it->c == '\r' && it->selective == -1)
4103 {
4104 /* A value of selective == -1 means that everything from the
4105 CR to the end of the line is invisible, with maybe an
4106 ellipsis displayed for it. */
13f19968 4107 success_p = next_element_from_ellipsis (it);
312246d1 4108 it->dpvec_char_len = -1;
5f5c8ee5
GM
4109 }
4110 }
4111 }
4112
4113 /* Value is zero if end of buffer reached. */
c880678e 4114 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4115 return success_p;
4116}
4117
4118
4119/* Run the redisplay end trigger hook for IT. */
4120
4121static void
4122run_redisplay_end_trigger_hook (it)
4123 struct it *it;
4124{
4125 Lisp_Object args[3];
4126
4127 /* IT->glyph_row should be non-null, i.e. we should be actually
4128 displaying something, or otherwise we should not run the hook. */
4129 xassert (it->glyph_row);
4130
4131 /* Set up hook arguments. */
4132 args[0] = Qredisplay_end_trigger_functions;
4133 args[1] = it->window;
4134 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4135 it->redisplay_end_trigger_charpos = 0;
4136
4137 /* Since we are *trying* to run these functions, don't try to run
4138 them again, even if they get an error. */
4139 it->w->redisplay_end_trigger = Qnil;
4140 Frun_hook_with_args (3, args);
4141
4142 /* Notice if it changed the face of the character we are on. */
4143 handle_face_prop (it);
4144}
4145
4146
260a86a0
KH
4147/* Deliver a composition display element. The iterator IT is already
4148 filled with composition information (done in
4149 handle_composition_prop). Value is always 1. */
4150
4151static int
4152next_element_from_composition (it)
4153 struct it *it;
4154{
4155 it->what = IT_COMPOSITION;
4156 it->position = (STRINGP (it->string)
4157 ? it->current.string_pos
4158 : it->current.pos);
4159 return 1;
4160}
4161
4162
5f5c8ee5
GM
4163\f
4164/***********************************************************************
4165 Moving an iterator without producing glyphs
4166 ***********************************************************************/
4167
4168/* Move iterator IT to a specified buffer or X position within one
4169 line on the display without producing glyphs.
4170
4171 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4172 whichever is reached first.
4173
4174 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4175
4176 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4177 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4178 TO_X includes the amount by which a window is horizontally
4179 scrolled.
4180
4181 Value is
4182
4183 MOVE_POS_MATCH_OR_ZV
4184 - when TO_POS or ZV was reached.
4185
4186 MOVE_X_REACHED
4187 -when TO_X was reached before TO_POS or ZV were reached.
4188
4189 MOVE_LINE_CONTINUED
4190 - when we reached the end of the display area and the line must
4191 be continued.
4192
4193 MOVE_LINE_TRUNCATED
4194 - when we reached the end of the display area and the line is
4195 truncated.
4196
4197 MOVE_NEWLINE_OR_CR
4198 - when we stopped at a line end, i.e. a newline or a CR and selective
4199 display is on. */
4200
701552dd 4201static enum move_it_result
5f5c8ee5
GM
4202move_it_in_display_line_to (it, to_charpos, to_x, op)
4203 struct it *it;
4204 int to_charpos, to_x, op;
4205{
4206 enum move_it_result result = MOVE_UNDEFINED;
4207 struct glyph_row *saved_glyph_row;
4208
4209 /* Don't produce glyphs in produce_glyphs. */
4210 saved_glyph_row = it->glyph_row;
4211 it->glyph_row = NULL;
4212
5f5c8ee5
GM
4213 while (1)
4214 {
4215 int x, i;
4216
4217 /* Stop when ZV or TO_CHARPOS reached. */
4218 if (!get_next_display_element (it)
4219 || ((op & MOVE_TO_POS) != 0
4220 && BUFFERP (it->object)
4221 && IT_CHARPOS (*it) >= to_charpos))
4222 {
4223 result = MOVE_POS_MATCH_OR_ZV;
4224 break;
4225 }
4226
4227 /* The call to produce_glyphs will get the metrics of the
4228 display element IT is loaded with. We record in x the
4229 x-position before this display element in case it does not
4230 fit on the line. */
4231 x = it->current_x;
4232 PRODUCE_GLYPHS (it);
4233
4234 if (it->area != TEXT_AREA)
4235 {
4236 set_iterator_to_next (it);
4237 continue;
4238 }
4239
4240 /* The number of glyphs we get back in IT->nglyphs will normally
4241 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4242 character on a terminal frame, or (iii) a line end. For the
4243 second case, IT->nglyphs - 1 padding glyphs will be present
4244 (on X frames, there is only one glyph produced for a
4245 composite character.
4246
4247 The behavior implemented below means, for continuation lines,
4248 that as many spaces of a TAB as fit on the current line are
4249 displayed there. For terminal frames, as many glyphs of a
4250 multi-glyph character are displayed in the current line, too.
4251 This is what the old redisplay code did, and we keep it that
4252 way. Under X, the whole shape of a complex character must
4253 fit on the line or it will be completely displayed in the
4254 next line.
4255
4256 Note that both for tabs and padding glyphs, all glyphs have
4257 the same width. */
4258 if (it->nglyphs)
4259 {
4260 /* More than one glyph or glyph doesn't fit on line. All
4261 glyphs have the same width. */
4262 int single_glyph_width = it->pixel_width / it->nglyphs;
4263 int new_x;
4264
4265 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4266 {
4267 new_x = x + single_glyph_width;
4268
4269 /* We want to leave anything reaching TO_X to the caller. */
4270 if ((op & MOVE_TO_X) && new_x > to_x)
4271 {
4272 it->current_x = x;
4273 result = MOVE_X_REACHED;
4274 break;
4275 }
4276 else if (/* Lines are continued. */
4277 !it->truncate_lines_p
4278 && (/* And glyph doesn't fit on the line. */
4279 new_x > it->last_visible_x
4280 /* Or it fits exactly and we're on a window
4281 system frame. */
4282 || (new_x == it->last_visible_x
4283 && FRAME_WINDOW_P (it->f))))
4284 {
4285 if (/* IT->hpos == 0 means the very first glyph
4286 doesn't fit on the line, e.g. a wide image. */
4287 it->hpos == 0
4288 || (new_x == it->last_visible_x
4289 && FRAME_WINDOW_P (it->f)))
4290 {
4291 ++it->hpos;
4292 it->current_x = new_x;
4293 if (i == it->nglyphs - 1)
4294 set_iterator_to_next (it);
4295 }
4296 else
4297 it->current_x = x;
4298
4299 result = MOVE_LINE_CONTINUED;
4300 break;
4301 }
4302 else if (new_x > it->first_visible_x)
4303 {
4304 /* Glyph is visible. Increment number of glyphs that
4305 would be displayed. */
4306 ++it->hpos;
4307 }
4308 else
4309 {
4310 /* Glyph is completely off the left margin of the display
4311 area. Nothing to do. */
4312 }
4313 }
4314
4315 if (result != MOVE_UNDEFINED)
4316 break;
4317 }
4318 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4319 {
4320 /* Stop when TO_X specified and reached. This check is
4321 necessary here because of lines consisting of a line end,
4322 only. The line end will not produce any glyphs and we
4323 would never get MOVE_X_REACHED. */
4324 xassert (it->nglyphs == 0);
4325 result = MOVE_X_REACHED;
4326 break;
4327 }
4328
4329 /* Is this a line end? If yes, we're done. */
4330 if (ITERATOR_AT_END_OF_LINE_P (it))
4331 {
4332 result = MOVE_NEWLINE_OR_CR;
4333 break;
4334 }
4335
4336 /* The current display element has been consumed. Advance
4337 to the next. */
4338 set_iterator_to_next (it);
4339
4340 /* Stop if lines are truncated and IT's current x-position is
4341 past the right edge of the window now. */
4342 if (it->truncate_lines_p
4343 && it->current_x >= it->last_visible_x)
4344 {
4345 result = MOVE_LINE_TRUNCATED;
4346 break;
4347 }
4348 }
4349
4350 /* Restore the iterator settings altered at the beginning of this
4351 function. */
4352 it->glyph_row = saved_glyph_row;
4353 return result;
4354}
4355
4356
4357/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4358 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4359 the description of enum move_operation_enum.
4360
4361 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4362 screen line, this function will set IT to the next position >
4363 TO_CHARPOS. */
4364
4365void
4366move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4367 struct it *it;
4368 int to_charpos, to_x, to_y, to_vpos;
4369 int op;
4370{
4371 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4372 int line_height;
4373
5f5c8ee5
GM
4374 while (1)
4375 {
4376 if (op & MOVE_TO_VPOS)
4377 {
4378 /* If no TO_CHARPOS and no TO_X specified, stop at the
4379 start of the line TO_VPOS. */
4380 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4381 {
4382 if (it->vpos == to_vpos)
4383 break;
4384 skip = move_it_in_display_line_to (it, -1, -1, 0);
4385 }
4386 else
4387 {
4388 /* TO_VPOS >= 0 means stop at TO_X in the line at
4389 TO_VPOS, or at TO_POS, whichever comes first. */
4390 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4391
4392 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4393 break;
4394 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4395 {
4396 /* We have reached TO_X but not in the line we want. */
4397 skip = move_it_in_display_line_to (it, to_charpos,
4398 -1, MOVE_TO_POS);
4399 if (skip == MOVE_POS_MATCH_OR_ZV)
4400 break;
4401 }
4402 }
4403 }
4404 else if (op & MOVE_TO_Y)
4405 {
4406 struct it it_backup;
4407 int done_p;
4408
4409 /* TO_Y specified means stop at TO_X in the line containing
4410 TO_Y---or at TO_CHARPOS if this is reached first. The
4411 problem is that we can't really tell whether the line
4412 contains TO_Y before we have completely scanned it, and
4413 this may skip past TO_X. What we do is to first scan to
4414 TO_X.
4415
4416 If TO_X is not specified, use a TO_X of zero. The reason
4417 is to make the outcome of this function more predictable.
4418 If we didn't use TO_X == 0, we would stop at the end of
4419 the line which is probably not what a caller would expect
4420 to happen. */
4421 skip = move_it_in_display_line_to (it, to_charpos,
4422 ((op & MOVE_TO_X)
4423 ? to_x : 0),
4424 (MOVE_TO_X
4425 | (op & MOVE_TO_POS)));
4426
4427 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4428 if (skip == MOVE_POS_MATCH_OR_ZV)
4429 break;
4430
4431 /* If TO_X was reached, we would like to know whether TO_Y
4432 is in the line. This can only be said if we know the
4433 total line height which requires us to scan the rest of
4434 the line. */
4435 done_p = 0;
4436 if (skip == MOVE_X_REACHED)
4437 {
4438 it_backup = *it;
4439 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4440 op & MOVE_TO_POS);
4441 }
4442
4443 /* Now, decide whether TO_Y is in this line. */
4444 line_height = it->max_ascent + it->max_descent;
4445
4446 if (to_y >= it->current_y
4447 && to_y < it->current_y + line_height)
4448 {
4449 if (skip == MOVE_X_REACHED)
4450 /* If TO_Y is in this line and TO_X was reached above,
4451 we scanned too far. We have to restore IT's settings
4452 to the ones before skipping. */
4453 *it = it_backup;
4454 done_p = 1;
4455 }
4456 else if (skip == MOVE_X_REACHED)
4457 {
4458 skip = skip2;
4459 if (skip == MOVE_POS_MATCH_OR_ZV)
4460 done_p = 1;
4461 }
4462
4463 if (done_p)
4464 break;
4465 }
4466 else
4467 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4468
4469 switch (skip)
4470 {
4471 case MOVE_POS_MATCH_OR_ZV:
4472 return;
4473
4474 case MOVE_NEWLINE_OR_CR:
4475 set_iterator_to_next (it);
4476 it->continuation_lines_width = 0;
4477 break;
4478
4479 case MOVE_LINE_TRUNCATED:
4480 it->continuation_lines_width = 0;
312246d1 4481 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4482 if ((op & MOVE_TO_POS) != 0
4483 && IT_CHARPOS (*it) > to_charpos)
4484 goto out;
4485 break;
4486
4487 case MOVE_LINE_CONTINUED:
4488 it->continuation_lines_width += it->current_x;
4489 break;
4490
4491 default:
4492 abort ();
4493 }
4494
4495 /* Reset/increment for the next run. */
4496 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4497 it->current_x = it->hpos = 0;
4498 it->current_y += it->max_ascent + it->max_descent;
4499 ++it->vpos;
4500 last_height = it->max_ascent + it->max_descent;
4501 last_max_ascent = it->max_ascent;
4502 it->max_ascent = it->max_descent = 0;
4503 }
4504 out:;
4505}
4506
4507
4508/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4509
4510 If DY > 0, move IT backward at least that many pixels. DY = 0
4511 means move IT backward to the preceding line start or BEGV. This
4512 function may move over more than DY pixels if IT->current_y - DY
4513 ends up in the middle of a line; in this case IT->current_y will be
4514 set to the top of the line moved to. */
4515
4516void
4517move_it_vertically_backward (it, dy)
4518 struct it *it;
4519 int dy;
4520{
4521 int nlines, h, line_height;
4522 struct it it2;
4523 int start_pos = IT_CHARPOS (*it);
4524
4525 xassert (dy >= 0);
4526
4527 /* Estimate how many newlines we must move back. */
4528 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4529
4530 /* Set the iterator's position that many lines back. */
4531 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4532 back_to_previous_visible_line_start (it);
4533
4534 /* Reseat the iterator here. When moving backward, we don't want
4535 reseat to skip forward over invisible text, set up the iterator
4536 to deliver from overlay strings at the new position etc. So,
4537 use reseat_1 here. */
4538 reseat_1 (it, it->current.pos, 1);
4539
4540 /* We are now surely at a line start. */
4541 it->current_x = it->hpos = 0;
4542
4543 /* Move forward and see what y-distance we moved. First move to the
4544 start of the next line so that we get its height. We need this
4545 height to be able to tell whether we reached the specified
4546 y-distance. */
4547 it2 = *it;
4548 it2.max_ascent = it2.max_descent = 0;
4549 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4550 MOVE_TO_POS | MOVE_TO_VPOS);
4551 xassert (IT_CHARPOS (*it) >= BEGV);
4552 line_height = it2.max_ascent + it2.max_descent;
4553 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4554 xassert (IT_CHARPOS (*it) >= BEGV);
4555 h = it2.current_y - it->current_y;
4556 nlines = it2.vpos - it->vpos;
4557
4558 /* Correct IT's y and vpos position. */
4559 it->vpos -= nlines;
4560 it->current_y -= h;
4561
4562 if (dy == 0)
4563 {
4564 /* DY == 0 means move to the start of the screen line. The
4565 value of nlines is > 0 if continuation lines were involved. */
4566 if (nlines > 0)
4567 move_it_by_lines (it, nlines, 1);
4568 xassert (IT_CHARPOS (*it) <= start_pos);
4569 }
4570 else if (nlines)
4571 {
4572 /* The y-position we try to reach. Note that h has been
4573 subtracted in front of the if-statement. */
4574 int target_y = it->current_y + h - dy;
4575
4576 /* If we did not reach target_y, try to move further backward if
4577 we can. If we moved too far backward, try to move forward. */
4578 if (target_y < it->current_y
4579 && IT_CHARPOS (*it) > BEGV)
4580 {
4581 move_it_vertically (it, target_y - it->current_y);
4582 xassert (IT_CHARPOS (*it) >= BEGV);
4583 }
4584 else if (target_y >= it->current_y + line_height
4585 && IT_CHARPOS (*it) < ZV)
4586 {
4587 move_it_vertically (it, target_y - (it->current_y + line_height));
4588 xassert (IT_CHARPOS (*it) >= BEGV);
4589 }
4590 }
4591}
4592
4593
4594/* Move IT by a specified amount of pixel lines DY. DY negative means
4595 move backwards. DY = 0 means move to start of screen line. At the
4596 end, IT will be on the start of a screen line. */
4597
4598void
4599move_it_vertically (it, dy)
4600 struct it *it;
4601 int dy;
4602{
4603 if (dy <= 0)
4604 move_it_vertically_backward (it, -dy);
4605 else if (dy > 0)
4606 {
4607 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4608 MOVE_TO_POS | MOVE_TO_Y);
4609
4610 /* If buffer ends in ZV without a newline, move to the start of
4611 the line to satisfy the post-condition. */
4612 if (IT_CHARPOS (*it) == ZV
4613 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4614 move_it_by_lines (it, 0, 0);
4615 }
4616}
4617
4618
4619/* Return non-zero if some text between buffer positions START_CHARPOS
4620 and END_CHARPOS is invisible. IT->window is the window for text
4621 property lookup. */
4622
4623static int
4624invisible_text_between_p (it, start_charpos, end_charpos)
4625 struct it *it;
4626 int start_charpos, end_charpos;
4627{
5f5c8ee5
GM
4628 Lisp_Object prop, limit;
4629 int invisible_found_p;
4630
4631 xassert (it != NULL && start_charpos <= end_charpos);
4632
4633 /* Is text at START invisible? */
4634 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4635 it->window);
4636 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4637 invisible_found_p = 1;
4638 else
4639 {
6c577098
GM
4640 limit = next_single_char_property_change (make_number (start_charpos),
4641 Qinvisible, Qnil,
4642 make_number (end_charpos));
5f5c8ee5
GM
4643 invisible_found_p = XFASTINT (limit) < end_charpos;
4644 }
4645
4646 return invisible_found_p;
5f5c8ee5
GM
4647}
4648
4649
4650/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4651 negative means move up. DVPOS == 0 means move to the start of the
4652 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4653 NEED_Y_P is zero, IT->current_y will be left unchanged.
4654
4655 Further optimization ideas: If we would know that IT->f doesn't use
4656 a face with proportional font, we could be faster for
4657 truncate-lines nil. */
4658
4659void
4660move_it_by_lines (it, dvpos, need_y_p)
4661 struct it *it;
4662 int dvpos, need_y_p;
4663{
4664 struct position pos;
4665
4666 if (!FRAME_WINDOW_P (it->f))
4667 {
4668 struct text_pos textpos;
4669
4670 /* We can use vmotion on frames without proportional fonts. */
4671 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4672 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4673 reseat (it, textpos, 1);
4674 it->vpos += pos.vpos;
4675 it->current_y += pos.vpos;
4676 }
4677 else if (dvpos == 0)
4678 {
4679 /* DVPOS == 0 means move to the start of the screen line. */
4680 move_it_vertically_backward (it, 0);
4681 xassert (it->current_x == 0 && it->hpos == 0);
4682 }
4683 else if (dvpos > 0)
4684 {
4685 /* If there are no continuation lines, and if there is no
4686 selective display, try the simple method of moving forward
4687 DVPOS newlines, then see where we are. */
4688 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4689 {
4690 int shortage = 0, charpos;
4691
4692 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4693 charpos = IT_CHARPOS (*it) + 1;
4694 else
4695 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4696 &shortage, 0);
4697
4698 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4699 {
4700 struct text_pos pos;
4701 CHARPOS (pos) = charpos;
4702 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4703 reseat (it, pos, 1);
4704 it->vpos += dvpos - shortage;
4705 it->hpos = it->current_x = 0;
4706 return;
4707 }
4708 }
4709
4710 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4711 }
4712 else
4713 {
4714 struct it it2;
4715 int start_charpos, i;
4716
4717 /* If there are no continuation lines, and if there is no
4718 selective display, try the simple method of moving backward
4719 -DVPOS newlines. */
4720 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4721 {
4722 int shortage;
4723 int charpos = IT_CHARPOS (*it);
4724 int bytepos = IT_BYTEPOS (*it);
4725
4726 /* If in the middle of a line, go to its start. */
4727 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4728 {
4729 charpos = find_next_newline_no_quit (charpos, -1);
4730 bytepos = CHAR_TO_BYTE (charpos);
4731 }
4732
4733 if (charpos == BEGV)
4734 {
4735 struct text_pos pos;
4736 CHARPOS (pos) = charpos;
4737 BYTEPOS (pos) = bytepos;
4738 reseat (it, pos, 1);
4739 it->hpos = it->current_x = 0;
4740 return;
4741 }
4742 else
4743 {
4744 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4745 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4746 {
4747 struct text_pos pos;
4748 CHARPOS (pos) = charpos;
4749 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4750 reseat (it, pos, 1);
4751 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4752 it->hpos = it->current_x = 0;
4753 return;
4754 }
4755 }
4756 }
4757
4758 /* Go back -DVPOS visible lines and reseat the iterator there. */
4759 start_charpos = IT_CHARPOS (*it);
4760 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4761 back_to_previous_visible_line_start (it);
4762 reseat (it, it->current.pos, 1);
4763 it->current_x = it->hpos = 0;
4764
4765 /* Above call may have moved too far if continuation lines
4766 are involved. Scan forward and see if it did. */
4767 it2 = *it;
4768 it2.vpos = it2.current_y = 0;
4769 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4770 it->vpos -= it2.vpos;
4771 it->current_y -= it2.current_y;
4772 it->current_x = it->hpos = 0;
4773
4774 /* If we moved too far, move IT some lines forward. */
4775 if (it2.vpos > -dvpos)
4776 {
4777 int delta = it2.vpos + dvpos;
4778 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4779 }
4780 }
4781}
4782
4783
4784\f
4785/***********************************************************************
4786 Messages
4787 ***********************************************************************/
4788
4789
937248bc
GM
4790/* Add a message with format string FORMAT and arguments ARG1 and ARG2
4791 to *Messages*. */
4792
4793void
4794add_to_log (format, arg1, arg2)
4795 char *format;
4796 Lisp_Object arg1, arg2;
4797{
4798 Lisp_Object args[3];
4799 Lisp_Object msg, fmt;
4800 char *buffer;
4801 int len;
4802 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4803
4804 fmt = msg = Qnil;
4805 GCPRO4 (fmt, msg, arg1, arg2);
4806
4807 args[0] = fmt = build_string (format);
4808 args[1] = arg1;
4809 args[2] = arg2;
6fc556fd 4810 msg = Fformat (3, args);
937248bc
GM
4811
4812 len = STRING_BYTES (XSTRING (msg)) + 1;
4813 buffer = (char *) alloca (len);
4814 strcpy (buffer, XSTRING (msg)->data);
4815
796184bc 4816 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
4817 UNGCPRO;
4818}
4819
4820
5f5c8ee5
GM
4821/* Output a newline in the *Messages* buffer if "needs" one. */
4822
4823void
4824message_log_maybe_newline ()
4825{
4826 if (message_log_need_newline)
4827 message_dolog ("", 0, 1, 0);
4828}
4829
4830
4831/* Add a string M of length LEN to the message log, optionally
4832 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4833 nonzero, means interpret the contents of M as multibyte. This
4834 function calls low-level routines in order to bypass text property
4835 hooks, etc. which might not be safe to run. */
4836
4837void
4838message_dolog (m, len, nlflag, multibyte)
4839 char *m;
4840 int len, nlflag, multibyte;
4841{
4842 if (!NILP (Vmessage_log_max))
4843 {
4844 struct buffer *oldbuf;
4845 Lisp_Object oldpoint, oldbegv, oldzv;
4846 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4847 int point_at_end = 0;
4848 int zv_at_end = 0;
4849 Lisp_Object old_deactivate_mark, tem;
4850 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4851
4852 old_deactivate_mark = Vdeactivate_mark;
4853 oldbuf = current_buffer;
4854 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4855 current_buffer->undo_list = Qt;
4856
4857 oldpoint = Fpoint_marker ();
4858 oldbegv = Fpoint_min_marker ();
4859 oldzv = Fpoint_max_marker ();
4860 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4861
4862 if (PT == Z)
4863 point_at_end = 1;
4864 if (ZV == Z)
4865 zv_at_end = 1;
4866
4867 BEGV = BEG;
4868 BEGV_BYTE = BEG_BYTE;
4869 ZV = Z;
4870 ZV_BYTE = Z_BYTE;
4871 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4872
4873 /* Insert the string--maybe converting multibyte to single byte
4874 or vice versa, so that all the text fits the buffer. */
4875 if (multibyte
4876 && NILP (current_buffer->enable_multibyte_characters))
4877 {
4878 int i, c, nbytes;
4879 unsigned char work[1];
4880
4881 /* Convert a multibyte string to single-byte
4882 for the *Message* buffer. */
4883 for (i = 0; i < len; i += nbytes)
4884 {
4fdb80f2 4885 c = string_char_and_length (m + i, len - i, &nbytes);
5f5c8ee5
GM
4886 work[0] = (SINGLE_BYTE_CHAR_P (c)
4887 ? c
4888 : multibyte_char_to_unibyte (c, Qnil));
4889 insert_1_both (work, 1, 1, 1, 0, 0);
4890 }
4891 }
4892 else if (! multibyte
4893 && ! NILP (current_buffer->enable_multibyte_characters))
4894 {
4895 int i, c, nbytes;
4896 unsigned char *msg = (unsigned char *) m;
260a86a0 4897 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
4898 /* Convert a single-byte string to multibyte
4899 for the *Message* buffer. */
4900 for (i = 0; i < len; i++)
4901 {
4902 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
4903 nbytes = CHAR_STRING (c, str);
4904 insert_1_both (str, 1, nbytes, 1, 0, 0);
5f5c8ee5
GM
4905 }
4906 }
4907 else if (len)
4908 insert_1 (m, len, 1, 0, 0);
4909
4910 if (nlflag)
4911 {
4912 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4913 insert_1 ("\n", 1, 1, 0, 0);
4914
4915 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4916 this_bol = PT;
4917 this_bol_byte = PT_BYTE;
4918
4919 if (this_bol > BEG)
4920 {
4921 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4922 prev_bol = PT;
4923 prev_bol_byte = PT_BYTE;
4924
4925 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4926 this_bol, this_bol_byte);
4927 if (dup)
4928 {
4929 del_range_both (prev_bol, prev_bol_byte,
4930 this_bol, this_bol_byte, 0);
4931 if (dup > 1)
4932 {
4933 char dupstr[40];
4934 int duplen;
4935
4936 /* If you change this format, don't forget to also
4937 change message_log_check_duplicate. */
4938 sprintf (dupstr, " [%d times]", dup);
4939 duplen = strlen (dupstr);
4940 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4941 insert_1 (dupstr, duplen, 1, 0, 1);
4942 }
4943 }
4944 }
4945
4946 if (NATNUMP (Vmessage_log_max))
4947 {
4948 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4949 -XFASTINT (Vmessage_log_max) - 1, 0);
4950 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4951 }
4952 }
4953 BEGV = XMARKER (oldbegv)->charpos;
4954 BEGV_BYTE = marker_byte_position (oldbegv);
4955
4956 if (zv_at_end)
4957 {
4958 ZV = Z;
4959 ZV_BYTE = Z_BYTE;
4960 }
4961 else
4962 {
4963 ZV = XMARKER (oldzv)->charpos;
4964 ZV_BYTE = marker_byte_position (oldzv);
4965 }
4966
4967 if (point_at_end)
4968 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4969 else
4970 /* We can't do Fgoto_char (oldpoint) because it will run some
4971 Lisp code. */
4972 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4973 XMARKER (oldpoint)->bytepos);
4974
4975 UNGCPRO;
4976 free_marker (oldpoint);
4977 free_marker (oldbegv);
4978 free_marker (oldzv);
4979
4980 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4981 set_buffer_internal (oldbuf);
4982 if (NILP (tem))
4983 windows_or_buffers_changed = old_windows_or_buffers_changed;
4984 message_log_need_newline = !nlflag;
4985 Vdeactivate_mark = old_deactivate_mark;
4986 }
4987}
4988
4989
4990/* We are at the end of the buffer after just having inserted a newline.
4991 (Note: We depend on the fact we won't be crossing the gap.)
4992 Check to see if the most recent message looks a lot like the previous one.
4993 Return 0 if different, 1 if the new one should just replace it, or a
4994 value N > 1 if we should also append " [N times]". */
4995
4996static int
4997message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4998 int prev_bol, this_bol;
4999 int prev_bol_byte, this_bol_byte;
5000{
5001 int i;
5002 int len = Z_BYTE - 1 - this_bol_byte;
5003 int seen_dots = 0;
5004 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5005 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5006
5007 for (i = 0; i < len; i++)
5008 {
5009 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5010 && p1[i] != '\n')
5011 seen_dots = 1;
5012 if (p1[i] != p2[i])
5013 return seen_dots;
5014 }
5015 p1 += len;
5016 if (*p1 == '\n')
5017 return 2;
5018 if (*p1++ == ' ' && *p1++ == '[')
5019 {
5020 int n = 0;
5021 while (*p1 >= '0' && *p1 <= '9')
5022 n = n * 10 + *p1++ - '0';
5023 if (strncmp (p1, " times]\n", 8) == 0)
5024 return n+1;
5025 }
5026 return 0;
5027}
5028
5029
5030/* Display an echo area message M with a specified length of LEN
5031 chars. The string may include null characters. If M is 0, clear
5032 out any existing message, and let the mini-buffer text show through.
5033
5034 The buffer M must continue to exist until after the echo area gets
5035 cleared or some other message gets displayed there. This means do
5036 not pass text that is stored in a Lisp string; do not pass text in
5037 a buffer that was alloca'd. */
5038
5039void
5040message2 (m, len, multibyte)
5041 char *m;
5042 int len;
5043 int multibyte;
5044{
5045 /* First flush out any partial line written with print. */
5046 message_log_maybe_newline ();
5047 if (m)
5048 message_dolog (m, len, 1, multibyte);
5049 message2_nolog (m, len, multibyte);
5050}
5051
5052
5053/* The non-logging counterpart of message2. */
5054
5055void
5056message2_nolog (m, len, multibyte)
5057 char *m;
5058 int len;
5059{
886bd6f2 5060 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5061 message_enable_multibyte = multibyte;
5062
5063 if (noninteractive)
5064 {
5065 if (noninteractive_need_newline)
5066 putc ('\n', stderr);
5067 noninteractive_need_newline = 0;
5068 if (m)
5069 fwrite (m, len, 1, stderr);
5070 if (cursor_in_echo_area == 0)
5071 fprintf (stderr, "\n");
5072 fflush (stderr);
5073 }
5074 /* A null message buffer means that the frame hasn't really been
5075 initialized yet. Error messages get reported properly by
5076 cmd_error, so this must be just an informative message; toss it. */
5077 else if (INTERACTIVE
886bd6f2
GM
5078 && sf->glyphs_initialized_p
5079 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5080 {
5081 Lisp_Object mini_window;
5082 struct frame *f;
5083
5084 /* Get the frame containing the mini-buffer
5085 that the selected frame is using. */
886bd6f2 5086 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5087 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5088
5089 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5090 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5091 && ! FRAME_VISIBLE_P (f))
5092 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5093
5094 if (m)
5095 {
c6e89d6c 5096 set_message (m, Qnil, len, multibyte);
5f5c8ee5
GM
5097 if (minibuffer_auto_raise)
5098 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5099 }
5100 else
c6e89d6c 5101 clear_message (1, 1);
5f5c8ee5 5102
c6e89d6c 5103 do_pending_window_change (0);
5f5c8ee5 5104 echo_area_display (1);
c6e89d6c 5105 do_pending_window_change (0);
5f5c8ee5
GM
5106 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5107 (*frame_up_to_date_hook) (f);
5108 }
5109}
5110
5111
c6e89d6c
GM
5112/* Display an echo area message M with a specified length of NBYTES
5113 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5114 string, clear out any existing message, and let the mini-buffer
5115 text show through. */
5116
5117void
c6e89d6c 5118message3 (m, nbytes, multibyte)
5f5c8ee5 5119 Lisp_Object m;
c6e89d6c 5120 int nbytes;
5f5c8ee5
GM
5121 int multibyte;
5122{
5123 struct gcpro gcpro1;
5124
5125 GCPRO1 (m);
5126
5127 /* First flush out any partial line written with print. */
5128 message_log_maybe_newline ();
5129 if (STRINGP (m))
c6e89d6c
GM
5130 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5131 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5132
5133 UNGCPRO;
5134}
5135
5136
5137/* The non-logging version of message3. */
5138
5139void
c6e89d6c 5140message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5141 Lisp_Object m;
c6e89d6c 5142 int nbytes, multibyte;
5f5c8ee5 5143{
886bd6f2 5144 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5145 message_enable_multibyte = multibyte;
5146
5147 if (noninteractive)
5148 {
5149 if (noninteractive_need_newline)
5150 putc ('\n', stderr);
5151 noninteractive_need_newline = 0;
5152 if (STRINGP (m))
c6e89d6c 5153 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5154 if (cursor_in_echo_area == 0)
5155 fprintf (stderr, "\n");
5156 fflush (stderr);
5157 }
5158 /* A null message buffer means that the frame hasn't really been
5159 initialized yet. Error messages get reported properly by
5160 cmd_error, so this must be just an informative message; toss it. */
5161 else if (INTERACTIVE
886bd6f2
GM
5162 && sf->glyphs_initialized_p
5163 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5164 {
5165 Lisp_Object mini_window;
c6e89d6c 5166 Lisp_Object frame;
5f5c8ee5
GM
5167 struct frame *f;
5168
5169 /* Get the frame containing the mini-buffer
5170 that the selected frame is using. */
886bd6f2 5171 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5172 frame = XWINDOW (mini_window)->frame;
5173 f = XFRAME (frame);
5f5c8ee5
GM
5174
5175 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5176 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5177 && !FRAME_VISIBLE_P (f))
5178 Fmake_frame_visible (frame);
5f5c8ee5 5179
c6e89d6c 5180 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5181 {
c6e89d6c 5182 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5183 if (minibuffer_auto_raise)
5184 Fraise_frame (frame);
5f5c8ee5
GM
5185 }
5186 else
c6e89d6c 5187 clear_message (1, 1);
5f5c8ee5 5188
c6e89d6c 5189 do_pending_window_change (0);
5f5c8ee5 5190 echo_area_display (1);
c6e89d6c 5191 do_pending_window_change (0);
5f5c8ee5
GM
5192 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5193 (*frame_up_to_date_hook) (f);
5194 }
5195}
5196
5197
5198/* Display a null-terminated echo area message M. If M is 0, clear
5199 out any existing message, and let the mini-buffer text show through.
5200
5201 The buffer M must continue to exist until after the echo area gets
5202 cleared or some other message gets displayed there. Do not pass
5203 text that is stored in a Lisp string. Do not pass text in a buffer
5204 that was alloca'd. */
5205
5206void
5207message1 (m)
5208 char *m;
5209{
5210 message2 (m, (m ? strlen (m) : 0), 0);
5211}
5212
5213
5214/* The non-logging counterpart of message1. */
5215
5216void
5217message1_nolog (m)
5218 char *m;
5219{
5220 message2_nolog (m, (m ? strlen (m) : 0), 0);
5221}
5222
5223/* Display a message M which contains a single %s
5224 which gets replaced with STRING. */
5225
5226void
5227message_with_string (m, string, log)
5228 char *m;
5229 Lisp_Object string;
5230 int log;
5231{
5232 if (noninteractive)
5233 {
5234 if (m)
5235 {
5236 if (noninteractive_need_newline)
5237 putc ('\n', stderr);
5238 noninteractive_need_newline = 0;
5239 fprintf (stderr, m, XSTRING (string)->data);
5240 if (cursor_in_echo_area == 0)
5241 fprintf (stderr, "\n");
5242 fflush (stderr);
5243 }
5244 }
5245 else if (INTERACTIVE)
5246 {
5247 /* The frame whose minibuffer we're going to display the message on.
5248 It may be larger than the selected frame, so we need
5249 to use its buffer, not the selected frame's buffer. */
5250 Lisp_Object mini_window;
886bd6f2 5251 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5252
5253 /* Get the frame containing the minibuffer
5254 that the selected frame is using. */
886bd6f2 5255 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5256 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5257
5258 /* A null message buffer means that the frame hasn't really been
5259 initialized yet. Error messages get reported properly by
5260 cmd_error, so this must be just an informative message; toss it. */
5261 if (FRAME_MESSAGE_BUF (f))
5262 {
5263 int len;
5264 char *a[1];
5265 a[0] = (char *) XSTRING (string)->data;
5266
5267 len = doprnt (FRAME_MESSAGE_BUF (f),
5268 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5269
5270 if (log)
5271 message2 (FRAME_MESSAGE_BUF (f), len,
5272 STRING_MULTIBYTE (string));
5273 else
5274 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5275 STRING_MULTIBYTE (string));
5276
5277 /* Print should start at the beginning of the message
5278 buffer next time. */
5279 message_buf_print = 0;
5280 }
5281 }
5282}
5283
5284
5f5c8ee5
GM
5285/* Dump an informative message to the minibuf. If M is 0, clear out
5286 any existing message, and let the mini-buffer text show through. */
5287
5288/* VARARGS 1 */
5289void
5290message (m, a1, a2, a3)
5291 char *m;
5292 EMACS_INT a1, a2, a3;
5293{
5294 if (noninteractive)
5295 {
5296 if (m)
5297 {
5298 if (noninteractive_need_newline)
5299 putc ('\n', stderr);
5300 noninteractive_need_newline = 0;
5301 fprintf (stderr, m, a1, a2, a3);
5302 if (cursor_in_echo_area == 0)
5303 fprintf (stderr, "\n");
5304 fflush (stderr);
5305 }
5306 }
5307 else if (INTERACTIVE)
5308 {
5309 /* The frame whose mini-buffer we're going to display the message
5310 on. It may be larger than the selected frame, so we need to
5311 use its buffer, not the selected frame's buffer. */
5312 Lisp_Object mini_window;
886bd6f2 5313 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5314
5315 /* Get the frame containing the mini-buffer
5316 that the selected frame is using. */
886bd6f2 5317 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5318 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5319
5320 /* A null message buffer means that the frame hasn't really been
5321 initialized yet. Error messages get reported properly by
5322 cmd_error, so this must be just an informative message; toss
5323 it. */
5324 if (FRAME_MESSAGE_BUF (f))
5325 {
5326 if (m)
5327 {
5328 int len;
5329#ifdef NO_ARG_ARRAY
5330 char *a[3];
5331 a[0] = (char *) a1;
5332 a[1] = (char *) a2;
5333 a[2] = (char *) a3;
5334
5335 len = doprnt (FRAME_MESSAGE_BUF (f),
5336 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5337#else
5338 len = doprnt (FRAME_MESSAGE_BUF (f),
5339 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5340 (char **) &a1);
5341#endif /* NO_ARG_ARRAY */
5342
5343 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5344 }
5345 else
5346 message1 (0);
5347
5348 /* Print should start at the beginning of the message
5349 buffer next time. */
5350 message_buf_print = 0;
5351 }
5352 }
5353}
5354
5355
5356/* The non-logging version of message. */
5357
5358void
5359message_nolog (m, a1, a2, a3)
5360 char *m;
5361 EMACS_INT a1, a2, a3;
5362{
5363 Lisp_Object old_log_max;
5364 old_log_max = Vmessage_log_max;
5365 Vmessage_log_max = Qnil;
5366 message (m, a1, a2, a3);
5367 Vmessage_log_max = old_log_max;
5368}
5369
5370
c6e89d6c
GM
5371/* Display the current message in the current mini-buffer. This is
5372 only called from error handlers in process.c, and is not time
5373 critical. */
5f5c8ee5
GM
5374
5375void
5376update_echo_area ()
5377{
c6e89d6c
GM
5378 if (!NILP (echo_area_buffer[0]))
5379 {
5380 Lisp_Object string;
5381 string = Fcurrent_message ();
5382 message3 (string, XSTRING (string)->size,
5383 !NILP (current_buffer->enable_multibyte_characters));
5384 }
5385}
5386
5387
5bcfeb49
GM
5388/* Make sure echo area buffers in echo_buffers[] are life. If they
5389 aren't, make new ones. */
5390
5391static void
5392ensure_echo_area_buffers ()
5393{
5394 int i;
5395
5396 for (i = 0; i < 2; ++i)
5397 if (!BUFFERP (echo_buffer[i])
5398 || NILP (XBUFFER (echo_buffer[i])->name))
5399 {
5400 char name[30];
5401 sprintf (name, " *Echo Area %d*", i);
5402 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 5403 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5bcfeb49
GM
5404 }
5405}
5406
5407
c6e89d6c
GM
5408/* Call FN with args A1..A5 with either the current or last displayed
5409 echo_area_buffer as current buffer.
5410
5411 WHICH zero means use the current message buffer
5412 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5413 from echo_buffer[] and clear it.
5414
5415 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5416 suitable buffer from echo_buffer[] and clear it.
5417
5418 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5419 that the current message becomes the last displayed one, make
5420 choose a suitable buffer for echo_area_buffer[0], and clear it.
5421
5422 Value is what FN returns. */
5423
5424static int
62694e5a 5425with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
c6e89d6c
GM
5426 struct window *w;
5427 int which;
5428 int (*fn) ();
62694e5a 5429 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
c6e89d6c
GM
5430{
5431 Lisp_Object buffer;
15e26c76 5432 int this_one, the_other, clear_buffer_p, rc;
c6e89d6c
GM
5433 int count = specpdl_ptr - specpdl;
5434
5435 /* If buffers aren't life, make new ones. */
5bcfeb49 5436 ensure_echo_area_buffers ();
c6e89d6c
GM
5437
5438 clear_buffer_p = 0;
5439
5440 if (which == 0)
5441 this_one = 0, the_other = 1;
5442 else if (which > 0)
5443 this_one = 1, the_other = 0;
5f5c8ee5 5444 else
c6e89d6c
GM
5445 {
5446 this_one = 0, the_other = 1;
5447 clear_buffer_p = 1;
5448
5449 /* We need a fresh one in case the current echo buffer equals
5450 the one containing the last displayed echo area message. */
5451 if (!NILP (echo_area_buffer[this_one])
5452 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5453 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
5454 }
5455
5456 /* Choose a suitable buffer from echo_buffer[] is we don't
5457 have one. */
5458 if (NILP (echo_area_buffer[this_one]))
5459 {
5460 echo_area_buffer[this_one]
5461 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5462 ? echo_buffer[the_other]
5463 : echo_buffer[this_one]);
5464 clear_buffer_p = 1;
5465 }
5466
5467 buffer = echo_area_buffer[this_one];
5468
5469 record_unwind_protect (unwind_with_echo_area_buffer,
5470 with_echo_area_buffer_unwind_data (w));
5471
5472 /* Make the echo area buffer current. Note that for display
5473 purposes, it is not necessary that the displayed window's buffer
5474 == current_buffer, except for text property lookup. So, let's
5475 only set that buffer temporarily here without doing a full
5476 Fset_window_buffer. We must also change w->pointm, though,
5477 because otherwise an assertions in unshow_buffer fails, and Emacs
5478 aborts. */
9142dd5b 5479 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5480 if (w)
5481 {
5482 w->buffer = buffer;
5483 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5484 }
ad4f174e 5485
c6e89d6c
GM
5486 current_buffer->undo_list = Qt;
5487 current_buffer->read_only = Qnil;
5488
5489 if (clear_buffer_p && Z > BEG)
5490 del_range (BEG, Z);
5491
5492 xassert (BEGV >= BEG);
5493 xassert (ZV <= Z && ZV >= BEGV);
5494
5495 rc = fn (a1, a2, a3, a4, a5);
5496
5497 xassert (BEGV >= BEG);
5498 xassert (ZV <= Z && ZV >= BEGV);
5499
5500 unbind_to (count, Qnil);
5501 return rc;
5f5c8ee5
GM
5502}
5503
5504
c6e89d6c
GM
5505/* Save state that should be preserved around the call to the function
5506 FN called in with_echo_area_buffer. */
5f5c8ee5 5507
c6e89d6c
GM
5508static Lisp_Object
5509with_echo_area_buffer_unwind_data (w)
5510 struct window *w;
5f5c8ee5 5511{
c6e89d6c
GM
5512 int i = 0;
5513 Lisp_Object vector;
5f5c8ee5 5514
c6e89d6c
GM
5515 /* Reduce consing by keeping one vector in
5516 Vwith_echo_area_save_vector. */
5517 vector = Vwith_echo_area_save_vector;
5518 Vwith_echo_area_save_vector = Qnil;
5519
5520 if (NILP (vector))
9142dd5b 5521 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5522
5523 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5524 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5525 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5526
5527 if (w)
5528 {
5529 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5530 XVECTOR (vector)->contents[i++] = w->buffer;
5531 XVECTOR (vector)->contents[i++]
5532 = make_number (XMARKER (w->pointm)->charpos);
5533 XVECTOR (vector)->contents[i++]
5534 = make_number (XMARKER (w->pointm)->bytepos);
5535 }
5536 else
5537 {
5538 int end = i + 4;
5539 while (i < end)
5540 XVECTOR (vector)->contents[i++] = Qnil;
5541 }
5f5c8ee5 5542
c6e89d6c
GM
5543 xassert (i == XVECTOR (vector)->size);
5544 return vector;
5545}
5f5c8ee5 5546
5f5c8ee5 5547
c6e89d6c
GM
5548/* Restore global state from VECTOR which was created by
5549 with_echo_area_buffer_unwind_data. */
5550
5551static Lisp_Object
5552unwind_with_echo_area_buffer (vector)
5553 Lisp_Object vector;
5554{
5555 int i = 0;
5556
9142dd5b 5557 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
c6e89d6c
GM
5558 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5559 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
c6e89d6c
GM
5560
5561 if (WINDOWP (XVECTOR (vector)->contents[i]))
5562 {
5563 struct window *w;
5564 Lisp_Object buffer, charpos, bytepos;
5565
5566 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5567 buffer = XVECTOR (vector)->contents[i]; ++i;
5568 charpos = XVECTOR (vector)->contents[i]; ++i;
5569 bytepos = XVECTOR (vector)->contents[i]; ++i;
5570
5571 w->buffer = buffer;
5572 set_marker_both (w->pointm, buffer,
5573 XFASTINT (charpos), XFASTINT (bytepos));
5574 }
5575
5576 Vwith_echo_area_save_vector = vector;
5577 return Qnil;
5578}
5579
5580
5581/* Set up the echo area for use by print functions. MULTIBYTE_P
5582 non-zero means we will print multibyte. */
5583
5584void
5585setup_echo_area_for_printing (multibyte_p)
5586 int multibyte_p;
5587{
5bcfeb49
GM
5588 ensure_echo_area_buffers ();
5589
c6e89d6c
GM
5590 if (!message_buf_print)
5591 {
5592 /* A message has been output since the last time we printed.
5593 Choose a fresh echo area buffer. */
5594 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5595 echo_area_buffer[0] = echo_buffer[1];
5596 else
5597 echo_area_buffer[0] = echo_buffer[0];
5598
5599 /* Switch to that buffer and clear it. */
5600 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5601 if (Z > BEG)
5602 del_range (BEG, Z);
5603 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5604
5605 /* Set up the buffer for the multibyteness we need. */
5606 if (multibyte_p
5607 != !NILP (current_buffer->enable_multibyte_characters))
5608 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5609
5610 /* Raise the frame containing the echo area. */
5611 if (minibuffer_auto_raise)
5612 {
886bd6f2 5613 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 5614 Lisp_Object mini_window;
886bd6f2 5615 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5616 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5617 }
5618
8a4e3c0c 5619 message_log_maybe_newline ();
c6e89d6c
GM
5620 message_buf_print = 1;
5621 }
fa77249f
GM
5622 else
5623 {
5624 if (NILP (echo_area_buffer[0]))
5625 {
5626 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5627 echo_area_buffer[0] = echo_buffer[1];
5628 else
5629 echo_area_buffer[0] = echo_buffer[0];
5630 }
5631
5632 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5633 /* Someone switched buffers between print requests. */
5634 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5635 }
c6e89d6c
GM
5636}
5637
5638
dd2eb166
GM
5639/* Display an echo area message in window W. Value is non-zero if W's
5640 height is changed. If display_last_displayed_message_p is
5641 non-zero, display the message that was last displayed, otherwise
5642 display the current message. */
c6e89d6c
GM
5643
5644static int
5645display_echo_area (w)
5646 struct window *w;
5647{
25edb08f
GM
5648 int i, no_message_p, window_height_changed_p, count;
5649
5650 /* Temporarily disable garbage collections while displaying the echo
5651 area. This is done because a GC can print a message itself.
5652 That message would modify the echo area buffer's contents while a
5653 redisplay of the buffer is going on, and seriously confuse
5654 redisplay. */
5655 count = inhibit_garbage_collection ();
dd2eb166
GM
5656
5657 /* If there is no message, we must call display_echo_area_1
5658 nevertheless because it resizes the window. But we will have to
5659 reset the echo_area_buffer in question to nil at the end because
5660 with_echo_area_buffer will sets it to an empty buffer. */
5661 i = display_last_displayed_message_p ? 1 : 0;
5662 no_message_p = NILP (echo_area_buffer[i]);
5663
5664 window_height_changed_p
5665 = with_echo_area_buffer (w, display_last_displayed_message_p,
5666 (int (*) ()) display_echo_area_1, w);
5667
5668 if (no_message_p)
5669 echo_area_buffer[i] = Qnil;
25edb08f
GM
5670
5671 unbind_to (count, Qnil);
dd2eb166 5672 return window_height_changed_p;
c6e89d6c
GM
5673}
5674
5675
5676/* Helper for display_echo_area. Display the current buffer which
5677 contains the current echo area message in window W, a mini-window.
5678 Change the height of W so that all of the message is displayed.
5679 Value is non-zero if height of W was changed. */
5680
5681static int
5682display_echo_area_1 (w)
5683 struct window *w;
5684{
5685 Lisp_Object window;
c6e89d6c
GM
5686 struct text_pos start;
5687 int window_height_changed_p = 0;
5688
5689 /* Do this before displaying, so that we have a large enough glyph
5690 matrix for the display. */
92a90e89 5691 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
5692
5693 /* Display. */
5694 clear_glyph_matrix (w->desired_matrix);
5695 XSETWINDOW (window, w);
5696 SET_TEXT_POS (start, BEG, BEG_BYTE);
5697 try_window (window, start);
5698
c6e89d6c
GM
5699 return window_height_changed_p;
5700}
5701
5702
92a90e89
GM
5703/* Resize the echo area window to exactly the size needed for the
5704 currently displayed message, if there is one. */
5705
5706void
5707resize_echo_area_axactly ()
5708{
5709 if (BUFFERP (echo_area_buffer[0])
5710 && WINDOWP (echo_area_window))
5711 {
5712 struct window *w = XWINDOW (echo_area_window);
5713 int resized_p;
5714
5715 resized_p = with_echo_area_buffer (w, 0,
5716 (int (*) ()) resize_mini_window,
5717 w, 1);
5718 if (resized_p)
5719 {
5720 ++windows_or_buffers_changed;
5721 ++update_mode_lines;
5722 redisplay_internal (0);
5723 }
5724 }
5725}
5726
5727
5728/* Resize mini-window W to fit the size of its contents. EXACT:P
5729 means size the window exactly to the size needed. Otherwise, it's
5730 only enlarged until W's buffer is empty. Value is non-zero if
5731 the window height has been changed. */
c6e89d6c 5732
9472f927 5733int
92a90e89 5734resize_mini_window (w, exact_p)
c6e89d6c 5735 struct window *w;
92a90e89 5736 int exact_p;
c6e89d6c
GM
5737{
5738 struct frame *f = XFRAME (w->frame);
5739 int window_height_changed_p = 0;
5740
5741 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
5742
5743 /* Nil means don't try to resize. */
00f6d59e
GM
5744 if (NILP (Vmax_mini_window_height)
5745 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 5746 return 0;
c6e89d6c
GM
5747
5748 if (!FRAME_MINIBUF_ONLY_P (f))
5749 {
5750 struct it it;
dd2eb166
GM
5751 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5752 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5753 int height, max_height;
5754 int unit = CANON_Y_UNIT (f);
5755 struct text_pos start;
9142dd5b 5756
c6e89d6c 5757 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 5758
dd2eb166
GM
5759 /* Compute the max. number of lines specified by the user. */
5760 if (FLOATP (Vmax_mini_window_height))
5761 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5762 else if (INTEGERP (Vmax_mini_window_height))
5763 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
5764 else
5765 max_height = total_height / 4;
dd2eb166
GM
5766
5767 /* Correct that max. height if it's bogus. */
5768 max_height = max (1, max_height);
5769 max_height = min (total_height, max_height);
5770
5771 /* Find out the height of the text in the window. */
ad4f174e
GM
5772 if (it.truncate_lines_p)
5773 height = 1;
55b064bd 5774 else
ad4f174e
GM
5775 {
5776 last_height = 0;
5777 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5778 if (it.max_ascent == 0 && it.max_descent == 0)
5779 height = it.current_y + last_height;
5780 else
5781 height = it.current_y + it.max_ascent + it.max_descent;
5782 height = (height + unit - 1) / unit;
5783 }
dd2eb166
GM
5784
5785 /* Compute a suitable window start. */
5786 if (height > max_height)
5787 {
5788 height = max_height;
5789 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5790 move_it_vertically_backward (&it, (height - 1) * unit);
5791 start = it.current.pos;
5792 }
5793 else
5794 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5795 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 5796
9472f927
GM
5797 /* Let it grow only, until we display an empty message, in which
5798 case the window shrinks again. */
da448723 5799 if (height > XFASTINT (w->height))
dd2eb166 5800 {
d2c48e86 5801 int old_height = XFASTINT (w->height);
da448723
GM
5802 freeze_window_starts (f, 1);
5803 grow_mini_window (w, height - XFASTINT (w->height));
5804 window_height_changed_p = XFASTINT (w->height) != old_height;
5805 }
5806 else if (height < XFASTINT (w->height)
5807 && (exact_p || BEGV == ZV))
5808 {
5809 int old_height = XFASTINT (w->height);
5810 freeze_window_starts (f, 0);
5811 shrink_mini_window (w);
d2c48e86 5812 window_height_changed_p = XFASTINT (w->height) != old_height;
9142dd5b 5813 }
c6e89d6c
GM
5814 }
5815
5816 return window_height_changed_p;
5817}
5818
5819
5820/* Value is the current message, a string, or nil if there is no
5821 current message. */
5822
5823Lisp_Object
5824current_message ()
5825{
5826 Lisp_Object msg;
5827
5828 if (NILP (echo_area_buffer[0]))
5829 msg = Qnil;
5830 else
5831 {
5832 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5833 if (NILP (msg))
5834 echo_area_buffer[0] = Qnil;
5835 }
5836
5837 return msg;
5838}
5839
5840
5841static int
5842current_message_1 (msg)
5843 Lisp_Object *msg;
5844{
5845 if (Z > BEG)
5846 *msg = make_buffer_string (BEG, Z, 1);
5847 else
5848 *msg = Qnil;
5849 return 0;
5850}
5851
5852
5853/* Push the current message on Vmessage_stack for later restauration
5854 by restore_message. Value is non-zero if the current message isn't
5855 empty. This is a relatively infrequent operation, so it's not
5856 worth optimizing. */
5857
5858int
5859push_message ()
5860{
5861 Lisp_Object msg;
5862 msg = current_message ();
5863 Vmessage_stack = Fcons (msg, Vmessage_stack);
5864 return STRINGP (msg);
5865}
5866
5867
5868/* Restore message display from the top of Vmessage_stack. */
5869
5870void
5871restore_message ()
5872{
5873 Lisp_Object msg;
5874
5875 xassert (CONSP (Vmessage_stack));
5876 msg = XCAR (Vmessage_stack);
5877 if (STRINGP (msg))
5878 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5879 else
5880 message3_nolog (msg, 0, 0);
5881}
5882
5883
5884/* Pop the top-most entry off Vmessage_stack. */
5885
5886void
5887pop_message ()
5888{
5889 xassert (CONSP (Vmessage_stack));
5890 Vmessage_stack = XCDR (Vmessage_stack);
5891}
5892
5893
5894/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5895 exits. If the stack is not empty, we have a missing pop_message
5896 somewhere. */
5897
5898void
5899check_message_stack ()
5900{
5901 if (!NILP (Vmessage_stack))
5902 abort ();
5903}
5904
5905
5906/* Truncate to NCHARS what will be displayed in the echo area the next
5907 time we display it---but don't redisplay it now. */
5908
5909void
5910truncate_echo_area (nchars)
5911 int nchars;
5912{
5913 if (nchars == 0)
5914 echo_area_buffer[0] = Qnil;
5915 /* A null message buffer means that the frame hasn't really been
5916 initialized yet. Error messages get reported properly by
5917 cmd_error, so this must be just an informative message; toss it. */
5918 else if (!noninteractive
5919 && INTERACTIVE
c6e89d6c 5920 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
5921 {
5922 struct frame *sf = SELECTED_FRAME ();
5923 if (FRAME_MESSAGE_BUF (sf))
5924 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5925 }
c6e89d6c
GM
5926}
5927
5928
5929/* Helper function for truncate_echo_area. Truncate the current
5930 message to at most NCHARS characters. */
5931
5932static int
5933truncate_message_1 (nchars)
5934 int nchars;
5935{
5936 if (BEG + nchars < Z)
5937 del_range (BEG + nchars, Z);
5938 if (Z == BEG)
5939 echo_area_buffer[0] = Qnil;
5940 return 0;
5941}
5942
5943
5944/* Set the current message to a substring of S or STRING.
5945
5946 If STRING is a Lisp string, set the message to the first NBYTES
5947 bytes from STRING. NBYTES zero means use the whole string. If
5948 STRING is multibyte, the message will be displayed multibyte.
5949
5950 If S is not null, set the message to the first LEN bytes of S. LEN
5951 zero means use the whole string. MULTIBYTE_P non-zero means S is
5952 multibyte. Display the message multibyte in that case. */
5953
5954void
5955set_message (s, string, nbytes, multibyte_p)
5956 char *s;
5957 Lisp_Object string;
5958 int nbytes;
5959{
5960 message_enable_multibyte
5961 = ((s && multibyte_p)
5962 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5963
5964 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5965 s, string, nbytes, multibyte_p);
5966 message_buf_print = 0;
5967}
5968
5969
5970/* Helper function for set_message. Arguments have the same meaning
5971 as there. This function is called with the echo area buffer being
5972 current. */
5973
5974static int
5975set_message_1 (s, string, nbytes, multibyte_p)
5976 char *s;
5977 Lisp_Object string;
5978 int nbytes, multibyte_p;
5979{
5980 xassert (BEG == Z);
5981
5982 /* Change multibyteness of the echo buffer appropriately. */
5983 if (message_enable_multibyte
5984 != !NILP (current_buffer->enable_multibyte_characters))
5985 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5986
ad4f174e
GM
5987 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
5988
c6e89d6c
GM
5989 /* Insert new message at BEG. */
5990 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5991
5992 if (STRINGP (string))
5993 {
5994 int nchars;
5995
5996 if (nbytes == 0)
5997 nbytes = XSTRING (string)->size_byte;
5998 nchars = string_byte_to_char (string, nbytes);
5999
6000 /* This function takes care of single/multibyte conversion. We
6001 just have to ensure that the echo area buffer has the right
6002 setting of enable_multibyte_characters. */
6003 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6004 }
6005 else if (s)
6006 {
6007 if (nbytes == 0)
6008 nbytes = strlen (s);
6009
6010 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6011 {
6012 /* Convert from multi-byte to single-byte. */
6013 int i, c, n;
6014 unsigned char work[1];
6015
6016 /* Convert a multibyte string to single-byte. */
6017 for (i = 0; i < nbytes; i += n)
6018 {
6019 c = string_char_and_length (s + i, nbytes - i, &n);
6020 work[0] = (SINGLE_BYTE_CHAR_P (c)
6021 ? c
6022 : multibyte_char_to_unibyte (c, Qnil));
6023 insert_1_both (work, 1, 1, 1, 0, 0);
6024 }
6025 }
6026 else if (!multibyte_p
6027 && !NILP (current_buffer->enable_multibyte_characters))
6028 {
6029 /* Convert from single-byte to multi-byte. */
6030 int i, c, n;
6031 unsigned char *msg = (unsigned char *) s;
260a86a0 6032 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6033
6034 /* Convert a single-byte string to multibyte. */
6035 for (i = 0; i < nbytes; i++)
6036 {
6037 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6038 n = CHAR_STRING (c, str);
6039 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6040 }
6041 }
6042 else
6043 insert_1 (s, nbytes, 1, 0, 0);
6044 }
6045
6046 return 0;
6047}
6048
6049
6050/* Clear messages. CURRENT_P non-zero means clear the current
6051 message. LAST_DISPLAYED_P non-zero means clear the message
6052 last displayed. */
6053
6054void
6055clear_message (current_p, last_displayed_p)
6056 int current_p, last_displayed_p;
6057{
6058 if (current_p)
6059 echo_area_buffer[0] = Qnil;
6060
6061 if (last_displayed_p)
6062 echo_area_buffer[1] = Qnil;
6063
6064 message_buf_print = 0;
6065}
6066
6067/* Clear garbaged frames.
6068
6069 This function is used where the old redisplay called
6070 redraw_garbaged_frames which in turn called redraw_frame which in
6071 turn called clear_frame. The call to clear_frame was a source of
6072 flickering. I believe a clear_frame is not necessary. It should
6073 suffice in the new redisplay to invalidate all current matrices,
6074 and ensure a complete redisplay of all windows. */
6075
6076static void
6077clear_garbaged_frames ()
6078{
5f5c8ee5
GM
6079 if (frame_garbaged)
6080 {
5f5c8ee5
GM
6081 Lisp_Object tail, frame;
6082
6083 FOR_EACH_FRAME (tail, frame)
6084 {
6085 struct frame *f = XFRAME (frame);
6086
6087 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6088 {
6089 clear_current_matrices (f);
6090 f->garbaged = 0;
6091 }
6092 }
6093
6094 frame_garbaged = 0;
6095 ++windows_or_buffers_changed;
6096 }
c6e89d6c 6097}
5f5c8ee5 6098
5f5c8ee5 6099
886bd6f2
GM
6100/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6101 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 6102 mini-windows height has been changed. */
5f5c8ee5 6103
c6e89d6c
GM
6104static int
6105echo_area_display (update_frame_p)
6106 int update_frame_p;
6107{
6108 Lisp_Object mini_window;
6109 struct window *w;
6110 struct frame *f;
6111 int window_height_changed_p = 0;
886bd6f2 6112 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6113
886bd6f2 6114 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6115 w = XWINDOW (mini_window);
6116 f = XFRAME (WINDOW_FRAME (w));
6117
6118 /* Don't display if frame is invisible or not yet initialized. */
6119 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6120 return 0;
5f5c8ee5 6121
1ab3e082 6122#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
6123 /* When Emacs starts, selected_frame may be a visible terminal
6124 frame, even if we run under a window system. If we let this
6125 through, a message would be displayed on the terminal. */
622e3754
GM
6126 if (EQ (selected_frame, Vterminal_frame)
6127 && !NILP (Vwindow_system))
c6e89d6c 6128 return 0;
1ab3e082 6129#endif /* HAVE_WINDOW_SYSTEM */
c6e89d6c
GM
6130
6131 /* Redraw garbaged frames. */
6132 if (frame_garbaged)
6133 clear_garbaged_frames ();
6134
6135 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6136 {
6137 echo_area_window = mini_window;
6138 window_height_changed_p = display_echo_area (w);
5f5c8ee5 6139 w->must_be_updated_p = 1;
c59c668a 6140
5f5c8ee5
GM
6141 if (update_frame_p)
6142 {
c59c668a
GM
6143 /* Not called from redisplay_internal. If we changed
6144 window configuration, we must redisplay thoroughly.
6145 Otherwise, we can do with updating what we displayed
6146 above. */
6147 if (window_height_changed_p)
6148 {
6149 ++windows_or_buffers_changed;
6150 ++update_mode_lines;
6151 redisplay_internal (0);
6152 }
6153 else if (FRAME_WINDOW_P (f))
5f5c8ee5
GM
6154 {
6155 update_single_window (w, 1);
6156 rif->flush_display (f);
6157 }
6158 else
6159 update_frame (f, 1, 1);
6160 }
6161 }
6162 else if (!EQ (mini_window, selected_window))
6163 windows_or_buffers_changed++;
c59c668a
GM
6164
6165 /* Last displayed message is now the current message. */
dd2eb166
GM
6166 echo_area_buffer[1] = echo_area_buffer[0];
6167
5f5c8ee5
GM
6168 /* Prevent redisplay optimization in redisplay_internal by resetting
6169 this_line_start_pos. This is done because the mini-buffer now
6170 displays the message instead of its buffer text. */
6171 if (EQ (mini_window, selected_window))
6172 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
6173
6174 return window_height_changed_p;
5f5c8ee5
GM
6175}
6176
6177
6178\f
6179/***********************************************************************
6180 Frame Titles
6181 ***********************************************************************/
6182
6183
6184#ifdef HAVE_WINDOW_SYSTEM
6185
6186/* A buffer for constructing frame titles in it; allocated from the
6187 heap in init_xdisp and resized as needed in store_frame_title_char. */
6188
6189static char *frame_title_buf;
6190
6191/* The buffer's end, and a current output position in it. */
6192
6193static char *frame_title_buf_end;
6194static char *frame_title_ptr;
6195
6196
6197/* Store a single character C for the frame title in frame_title_buf.
6198 Re-allocate frame_title_buf if necessary. */
6199
6200static void
6201store_frame_title_char (c)
6202 char c;
6203{
6204 /* If output position has reached the end of the allocated buffer,
6205 double the buffer's size. */
6206 if (frame_title_ptr == frame_title_buf_end)
6207 {
6208 int len = frame_title_ptr - frame_title_buf;
6209 int new_size = 2 * len * sizeof *frame_title_buf;
6210 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6211 frame_title_buf_end = frame_title_buf + new_size;
6212 frame_title_ptr = frame_title_buf + len;
6213 }
6214
6215 *frame_title_ptr++ = c;
6216}
6217
6218
6219/* Store part of a frame title in frame_title_buf, beginning at
6220 frame_title_ptr. STR is the string to store. Do not copy more
6221 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6222 the whole string. Pad with spaces until FIELD_WIDTH number of
6223 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6224 Called from display_mode_element when it is used to build a frame
6225 title. */
6226
6227static int
6228store_frame_title (str, field_width, precision)
6229 unsigned char *str;
6230 int field_width, precision;
6231{
6232 int n = 0;
6233
6234 /* Copy at most PRECISION chars from STR. */
6235 while ((precision <= 0 || n < precision)
6236 && *str)
6237 {
6238 store_frame_title_char (*str++);
6239 ++n;
6240 }
6241
6242 /* Fill up with spaces until FIELD_WIDTH reached. */
6243 while (field_width > 0
6244 && n < field_width)
6245 {
6246 store_frame_title_char (' ');
6247 ++n;
6248 }
6249
6250 return n;
6251}
6252
6253
6254/* Set the title of FRAME, if it has changed. The title format is
6255 Vicon_title_format if FRAME is iconified, otherwise it is
6256 frame_title_format. */
6257
6258static void
6259x_consider_frame_title (frame)
6260 Lisp_Object frame;
6261{
6262 struct frame *f = XFRAME (frame);
6263
6264 if (FRAME_WINDOW_P (f)
6265 || FRAME_MINIBUF_ONLY_P (f)
6266 || f->explicit_name)
6267 {
6268 /* Do we have more than one visible frame on this X display? */
6269 Lisp_Object tail;
6270 Lisp_Object fmt;
6271 struct buffer *obuf;
6272 int len;
6273 struct it it;
6274
9472f927 6275 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6276 {
9472f927 6277 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
6278
6279 if (tf != f
6280 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6281 && !FRAME_MINIBUF_ONLY_P (tf)
6282 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6283 break;
6284 }
6285
6286 /* Set global variable indicating that multiple frames exist. */
6287 multiple_frames = CONSP (tail);
6288
6289 /* Switch to the buffer of selected window of the frame. Set up
6290 frame_title_ptr so that display_mode_element will output into it;
6291 then display the title. */
6292 obuf = current_buffer;
6293 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6294 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6295 frame_title_ptr = frame_title_buf;
6296 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6297 NULL, DEFAULT_FACE_ID);
6298 len = display_mode_element (&it, 0, -1, -1, fmt);
6299 frame_title_ptr = NULL;
6300 set_buffer_internal (obuf);
6301
6302 /* Set the title only if it's changed. This avoids consing in
6303 the common case where it hasn't. (If it turns out that we've
6304 already wasted too much time by walking through the list with
6305 display_mode_element, then we might need to optimize at a
6306 higher level than this.) */
6307 if (! STRINGP (f->name)
6308 || STRING_BYTES (XSTRING (f->name)) != len
6309 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6310 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6311 }
6312}
6313
6314#else /* not HAVE_WINDOW_SYSTEM */
6315
6316#define frame_title_ptr ((char *)0)
6317#define store_frame_title(str, mincol, maxcol) 0
6318
6319#endif /* not HAVE_WINDOW_SYSTEM */
6320
6321
6322
6323\f
6324/***********************************************************************
6325 Menu Bars
6326 ***********************************************************************/
6327
6328
6329/* Prepare for redisplay by updating menu-bar item lists when
6330 appropriate. This can call eval. */
6331
6332void
6333prepare_menu_bars ()
6334{
6335 int all_windows;
6336 struct gcpro gcpro1, gcpro2;
6337 struct frame *f;
6338 struct frame *tooltip_frame;
6339
6340#ifdef HAVE_X_WINDOWS
6341 tooltip_frame = tip_frame;
6342#else
6343 tooltip_frame = NULL;
6344#endif
6345
6346 /* Update all frame titles based on their buffer names, etc. We do
6347 this before the menu bars so that the buffer-menu will show the
6348 up-to-date frame titles. */
6349#ifdef HAVE_WINDOW_SYSTEM
6350 if (windows_or_buffers_changed || update_mode_lines)
6351 {
6352 Lisp_Object tail, frame;
6353
6354 FOR_EACH_FRAME (tail, frame)
6355 {
6356 f = XFRAME (frame);
6357 if (f != tooltip_frame
6358 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6359 x_consider_frame_title (frame);
6360 }
6361 }
6362#endif /* HAVE_WINDOW_SYSTEM */
6363
6364 /* Update the menu bar item lists, if appropriate. This has to be
6365 done before any actual redisplay or generation of display lines. */
6366 all_windows = (update_mode_lines
6367 || buffer_shared > 1
6368 || windows_or_buffers_changed);
6369 if (all_windows)
6370 {
6371 Lisp_Object tail, frame;
6372 int count = specpdl_ptr - specpdl;
6373
6374 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6375
6376 FOR_EACH_FRAME (tail, frame)
6377 {
6378 f = XFRAME (frame);
6379
6380 /* Ignore tooltip frame. */
6381 if (f == tooltip_frame)
6382 continue;
6383
6384 /* If a window on this frame changed size, report that to
6385 the user and clear the size-change flag. */
6386 if (FRAME_WINDOW_SIZES_CHANGED (f))
6387 {
6388 Lisp_Object functions;
6389
6390 /* Clear flag first in case we get an error below. */
6391 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6392 functions = Vwindow_size_change_functions;
6393 GCPRO2 (tail, functions);
6394
6395 while (CONSP (functions))
6396 {
6397 call1 (XCAR (functions), frame);
6398 functions = XCDR (functions);
6399 }
6400 UNGCPRO;
6401 }
6402
6403 GCPRO1 (tail);
6404 update_menu_bar (f, 0);
6405#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 6406 update_tool_bar (f, 0);
5f5c8ee5
GM
6407#endif
6408 UNGCPRO;
6409 }
6410
6411 unbind_to (count, Qnil);
6412 }
6413 else
6414 {
886bd6f2
GM
6415 struct frame *sf = SELECTED_FRAME ();
6416 update_menu_bar (sf, 1);
5f5c8ee5 6417#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 6418 update_tool_bar (sf, 1);
5f5c8ee5
GM
6419#endif
6420 }
6421
6422 /* Motif needs this. See comment in xmenu.c. Turn it off when
6423 pending_menu_activation is not defined. */
6424#ifdef USE_X_TOOLKIT
6425 pending_menu_activation = 0;
6426#endif
6427}
6428
6429
6430/* Update the menu bar item list for frame F. This has to be done
6431 before we start to fill in any display lines, because it can call
6432 eval.
6433
6434 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6435
6436static void
6437update_menu_bar (f, save_match_data)
6438 struct frame *f;
6439 int save_match_data;
6440{
6441 Lisp_Object window;
6442 register struct window *w;
6443
6444 window = FRAME_SELECTED_WINDOW (f);
6445 w = XWINDOW (window);
6446
6447 if (update_mode_lines)
6448 w->update_mode_line = Qt;
6449
6450 if (FRAME_WINDOW_P (f)
6451 ?
6452#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6453 FRAME_EXTERNAL_MENU_BAR (f)
6454#else
6455 FRAME_MENU_BAR_LINES (f) > 0
6456#endif
6457 : FRAME_MENU_BAR_LINES (f) > 0)
6458 {
6459 /* If the user has switched buffers or windows, we need to
6460 recompute to reflect the new bindings. But we'll
6461 recompute when update_mode_lines is set too; that means
6462 that people can use force-mode-line-update to request
6463 that the menu bar be recomputed. The adverse effect on
6464 the rest of the redisplay algorithm is about the same as
6465 windows_or_buffers_changed anyway. */
6466 if (windows_or_buffers_changed
6467 || !NILP (w->update_mode_line)
6468 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6469 < BUF_MODIFF (XBUFFER (w->buffer)))
6470 != !NILP (w->last_had_star))
6471 || ((!NILP (Vtransient_mark_mode)
6472 && !NILP (XBUFFER (w->buffer)->mark_active))
6473 != !NILP (w->region_showing)))
6474 {
6475 struct buffer *prev = current_buffer;
6476 int count = specpdl_ptr - specpdl;
6477
6478 set_buffer_internal_1 (XBUFFER (w->buffer));
6479 if (save_match_data)
6480 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6481 if (NILP (Voverriding_local_map_menu_flag))
6482 {
6483 specbind (Qoverriding_terminal_local_map, Qnil);
6484 specbind (Qoverriding_local_map, Qnil);
6485 }
6486
6487 /* Run the Lucid hook. */
6488 call1 (Vrun_hooks, Qactivate_menubar_hook);
6489
6490 /* If it has changed current-menubar from previous value,
6491 really recompute the menu-bar from the value. */
6492 if (! NILP (Vlucid_menu_bar_dirty_flag))
6493 call0 (Qrecompute_lucid_menubar);
6494
6495 safe_run_hooks (Qmenu_bar_update_hook);
6496 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6497
6498 /* Redisplay the menu bar in case we changed it. */
6499#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6500 if (FRAME_WINDOW_P (f))
6501 set_frame_menubar (f, 0, 0);
6502 else
6503 /* On a terminal screen, the menu bar is an ordinary screen
6504 line, and this makes it get updated. */
6505 w->update_mode_line = Qt;
6506#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6507 /* In the non-toolkit version, the menu bar is an ordinary screen
6508 line, and this makes it get updated. */
6509 w->update_mode_line = Qt;
6510#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6511
6512 unbind_to (count, Qnil);
6513 set_buffer_internal_1 (prev);
6514 }
6515 }
6516}
6517
6518
6519\f
6520/***********************************************************************
e037b9ec 6521 Tool-bars
5f5c8ee5
GM
6522 ***********************************************************************/
6523
6524#ifdef HAVE_WINDOW_SYSTEM
6525
e037b9ec 6526/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
6527 before we start to fill in any display lines. Called from
6528 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6529 and restore it here. */
6530
6531static void
e037b9ec 6532update_tool_bar (f, save_match_data)
5f5c8ee5
GM
6533 struct frame *f;
6534 int save_match_data;
6535{
e037b9ec
GM
6536 if (WINDOWP (f->tool_bar_window)
6537 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
6538 {
6539 Lisp_Object window;
6540 struct window *w;
6541
6542 window = FRAME_SELECTED_WINDOW (f);
6543 w = XWINDOW (window);
6544
6545 /* If the user has switched buffers or windows, we need to
6546 recompute to reflect the new bindings. But we'll
6547 recompute when update_mode_lines is set too; that means
6548 that people can use force-mode-line-update to request
6549 that the menu bar be recomputed. The adverse effect on
6550 the rest of the redisplay algorithm is about the same as
6551 windows_or_buffers_changed anyway. */
6552 if (windows_or_buffers_changed
6553 || !NILP (w->update_mode_line)
6554 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6555 < BUF_MODIFF (XBUFFER (w->buffer)))
6556 != !NILP (w->last_had_star))
6557 || ((!NILP (Vtransient_mark_mode)
6558 && !NILP (XBUFFER (w->buffer)->mark_active))
6559 != !NILP (w->region_showing)))
6560 {
6561 struct buffer *prev = current_buffer;
6562 int count = specpdl_ptr - specpdl;
a2889657 6563
5f5c8ee5
GM
6564 /* Set current_buffer to the buffer of the selected
6565 window of the frame, so that we get the right local
6566 keymaps. */
6567 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 6568
5f5c8ee5
GM
6569 /* Save match data, if we must. */
6570 if (save_match_data)
6571 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6572
6573 /* Make sure that we don't accidentally use bogus keymaps. */
6574 if (NILP (Voverriding_local_map_menu_flag))
6575 {
6576 specbind (Qoverriding_terminal_local_map, Qnil);
6577 specbind (Qoverriding_local_map, Qnil);
1f40cad2 6578 }
1f40cad2 6579
e037b9ec
GM
6580 /* Build desired tool-bar items from keymaps. */
6581 f->desired_tool_bar_items
6582 = tool_bar_items (f->desired_tool_bar_items,
6583 &f->n_desired_tool_bar_items);
5f5c8ee5 6584
e037b9ec 6585 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
6586 w->update_mode_line = Qt;
6587
6588 unbind_to (count, Qnil);
6589 set_buffer_internal_1 (prev);
81d478f3 6590 }
a2889657
JB
6591 }
6592}
6593
6c4429a5 6594
e037b9ec
GM
6595/* Set F->desired_tool_bar_string to a Lisp string representing frame
6596 F's desired tool-bar contents. F->desired_tool_bar_items must have
5f5c8ee5
GM
6597 been set up previously by calling prepare_menu_bars. */
6598
a2889657 6599static void
e037b9ec 6600build_desired_tool_bar_string (f)
5f5c8ee5 6601 struct frame *f;
a2889657 6602{
5f5c8ee5
GM
6603 int i, size, size_needed, string_idx;
6604 struct gcpro gcpro1, gcpro2, gcpro3;
6605 Lisp_Object image, plist, props;
a2889657 6606
5f5c8ee5
GM
6607 image = plist = props = Qnil;
6608 GCPRO3 (image, plist, props);
a2889657 6609
e037b9ec 6610 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
6611 Otherwise, make a new string. */
6612
6613 /* The size of the string we might be able to reuse. */
e037b9ec
GM
6614 size = (STRINGP (f->desired_tool_bar_string)
6615 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
6616 : 0);
6617
6618 /* Each image in the string we build is preceded by a space,
6619 and there is a space at the end. */
e037b9ec 6620 size_needed = f->n_desired_tool_bar_items + 1;
5f5c8ee5 6621
e037b9ec 6622 /* Reuse f->desired_tool_bar_string, if possible. */
5f5c8ee5 6623 if (size < size_needed)
6fc556fd
KR
6624 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6625 make_number (' '));
5f5c8ee5
GM
6626 else
6627 {
6628 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6629 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 6630 props, f->desired_tool_bar_string);
5f5c8ee5 6631 }
a2889657 6632
5f5c8ee5 6633 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
6634 put a `menu_item' property on tool-bar items with a value that
6635 is the index of the item in F's tool-bar item vector. */
5f5c8ee5 6636 for (i = 0, string_idx = 0;
e037b9ec 6637 i < f->n_desired_tool_bar_items;
5f5c8ee5 6638 ++i, string_idx += 1)
a2889657 6639 {
5f5c8ee5 6640#define PROP(IDX) \
e037b9ec
GM
6641 (XVECTOR (f->desired_tool_bar_items) \
6642 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
5f5c8ee5 6643
e037b9ec
GM
6644 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6645 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
5f5c8ee5
GM
6646 int margin, relief;
6647 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6648 extern Lisp_Object Qlaplace;
6649
6650 /* If image is a vector, choose the image according to the
6651 button state. */
e037b9ec 6652 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
6653 if (VECTORP (image))
6654 {
e037b9ec 6655 enum tool_bar_item_image idx;
5f5c8ee5
GM
6656
6657 if (enabled_p)
6658 idx = (selected_p
e037b9ec
GM
6659 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6660 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
6661 else
6662 idx = (selected_p
e037b9ec
GM
6663 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6664 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5
GM
6665
6666 xassert (XVECTOR (image)->size >= idx);
6667 image = XVECTOR (image)->contents[idx];
6668 }
6669
6670 /* Ignore invalid image specifications. */
6671 if (!valid_image_p (image))
6672 continue;
6673
e037b9ec 6674 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
6675 plist = Fcopy_sequence (XCDR (image));
6676
6677 /* Compute margin and relief to draw. */
e037b9ec
GM
6678 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6679 margin = relief + max (0, tool_bar_button_margin);
5f5c8ee5 6680
e037b9ec 6681 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
6682 {
6683 /* Add a `:relief' property to the image spec if the item is
6684 selected. */
6685 if (selected_p)
6686 {
6687 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6688 margin -= relief;
6689 }
6690 }
6691 else
6692 {
6693 /* If image is selected, display it pressed, i.e. with a
6694 negative relief. If it's not selected, display it with a
6695 raised relief. */
6696 plist = Fplist_put (plist, QCrelief,
6697 (selected_p
6698 ? make_number (-relief)
6699 : make_number (relief)));
6700 margin -= relief;
6701 }
6702
6703 /* Put a margin around the image. */
6704 if (margin)
6705 plist = Fplist_put (plist, QCmargin, make_number (margin));
6706
6707 /* If button is not enabled, make the image appear disabled by
6708 applying an appropriate algorithm to it. */
6709 if (!enabled_p)
6710 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6711
6712 /* Put a `display' text property on the string for the image to
6713 display. Put a `menu-item' property on the string that gives
e037b9ec 6714 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
6715 vector. */
6716 image = Fcons (Qimage, plist);
6717 props = list4 (Qdisplay, image,
e037b9ec 6718 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
5f5c8ee5
GM
6719 Fadd_text_properties (make_number (string_idx),
6720 make_number (string_idx + 1),
e037b9ec 6721 props, f->desired_tool_bar_string);
5f5c8ee5 6722#undef PROP
a2889657
JB
6723 }
6724
5f5c8ee5
GM
6725 UNGCPRO;
6726}
6727
6728
e037b9ec 6729/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
6730
6731static void
e037b9ec 6732display_tool_bar_line (it)
5f5c8ee5
GM
6733 struct it *it;
6734{
6735 struct glyph_row *row = it->glyph_row;
6736 int max_x = it->last_visible_x;
6737 struct glyph *last;
6738
6739 prepare_desired_row (row);
6740 row->y = it->current_y;
6741
6742 while (it->current_x < max_x)
a2889657 6743 {
5f5c8ee5 6744 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 6745
5f5c8ee5
GM
6746 /* Get the next display element. */
6747 if (!get_next_display_element (it))
6748 break;
73af359d 6749
5f5c8ee5
GM
6750 /* Produce glyphs. */
6751 x_before = it->current_x;
6752 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6753 PRODUCE_GLYPHS (it);
daa37602 6754
5f5c8ee5
GM
6755 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6756 i = 0;
6757 x = x_before;
6758 while (i < nglyphs)
6759 {
6760 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6761
6762 if (x + glyph->pixel_width > max_x)
6763 {
6764 /* Glyph doesn't fit on line. */
6765 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6766 it->current_x = x;
6767 goto out;
6768 }
daa37602 6769
5f5c8ee5
GM
6770 ++it->hpos;
6771 x += glyph->pixel_width;
6772 ++i;
6773 }
6774
6775 /* Stop at line ends. */
6776 if (ITERATOR_AT_END_OF_LINE_P (it))
6777 break;
6778
6779 set_iterator_to_next (it);
a2889657 6780 }
a2889657 6781
5f5c8ee5 6782 out:;
a2889657 6783
5f5c8ee5
GM
6784 row->displays_text_p = row->used[TEXT_AREA] != 0;
6785 extend_face_to_end_of_line (it);
6786 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6787 last->right_box_line_p = 1;
6788 compute_line_metrics (it);
6789
e037b9ec 6790 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
6791 if (!row->displays_text_p)
6792 {
312246d1
GM
6793 row->height = row->phys_height = it->last_visible_y - row->y;
6794 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
6795 }
6796
6797 row->full_width_p = 1;
6798 row->continued_p = 0;
6799 row->truncated_on_left_p = 0;
6800 row->truncated_on_right_p = 0;
6801
6802 it->current_x = it->hpos = 0;
6803 it->current_y += row->height;
6804 ++it->vpos;
6805 ++it->glyph_row;
a2889657 6806}
96a410bc 6807
5f5c8ee5 6808
e037b9ec 6809/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 6810 items of frame F visible. */
96a410bc 6811
d39b6696 6812static int
e037b9ec 6813tool_bar_lines_needed (f)
5f5c8ee5 6814 struct frame *f;
d39b6696 6815{
e037b9ec 6816 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
6817 struct it it;
6818
e037b9ec
GM
6819 /* Initialize an iterator for iteration over
6820 F->desired_tool_bar_string in the tool-bar window of frame F. */
6821 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
6822 it.first_visible_x = 0;
6823 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 6824 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
6825
6826 while (!ITERATOR_AT_END_P (&it))
6827 {
6828 it.glyph_row = w->desired_matrix->rows;
6829 clear_glyph_row (it.glyph_row);
e037b9ec 6830 display_tool_bar_line (&it);
5f5c8ee5
GM
6831 }
6832
6833 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 6834}
96a410bc 6835
5f5c8ee5 6836
e037b9ec 6837/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
6838 height should be changed. */
6839
6840static int
e037b9ec 6841redisplay_tool_bar (f)
5f5c8ee5 6842 struct frame *f;
96a410bc 6843{
5f5c8ee5
GM
6844 struct window *w;
6845 struct it it;
6846 struct glyph_row *row;
6847 int change_height_p = 0;
6848
e037b9ec
GM
6849 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6850 do anything. This means you must start with tool-bar-lines
5f5c8ee5 6851 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
6852 can turn off tool-bars by specifying tool-bar-lines zero. */
6853 if (!WINDOWP (f->tool_bar_window)
6854 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
6855 XFASTINT (w->height) == 0))
6856 return 0;
96a410bc 6857
e037b9ec
GM
6858 /* Set up an iterator for the tool-bar window. */
6859 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
6860 it.first_visible_x = 0;
6861 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6862 row = it.glyph_row;
3450d04c 6863
e037b9ec
GM
6864 /* Build a string that represents the contents of the tool-bar. */
6865 build_desired_tool_bar_string (f);
6866 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 6867
e037b9ec 6868 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 6869 while (it.current_y < it.last_visible_y)
e037b9ec 6870 display_tool_bar_line (&it);
3450d04c 6871
e037b9ec 6872 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
6873 window, so don't do it. */
6874 w->desired_matrix->no_scrolling_p = 1;
6875 w->must_be_updated_p = 1;
3450d04c 6876
e037b9ec 6877 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
6878 {
6879 int nlines;
6880
6881 /* If there are blank lines at the end, except for a partially
6882 visible blank line at the end that is smaller than
e037b9ec 6883 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
6884 row = it.glyph_row - 1;
6885 if (!row->displays_text_p
6886 && row->height >= CANON_Y_UNIT (f))
6887 change_height_p = 1;
6888
e037b9ec
GM
6889 /* If row displays tool-bar items, but is partially visible,
6890 change the tool-bar's height. */
5f5c8ee5
GM
6891 if (row->displays_text_p
6892 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6893 change_height_p = 1;
6894
e037b9ec 6895 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
6896 frame parameter. */
6897 if (change_height_p
e037b9ec 6898 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
6899 nlines != XFASTINT (w->height)))
6900 {
e037b9ec 6901 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5
GM
6902 Lisp_Object frame;
6903
6904 XSETFRAME (frame, f);
6905 clear_glyph_matrix (w->desired_matrix);
6906 Fmodify_frame_parameters (frame,
e037b9ec 6907 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
6908 make_number (nlines)),
6909 Qnil));
6910 fonts_changed_p = 1;
6911 }
6912 }
3450d04c 6913
5f5c8ee5 6914 return change_height_p;
96a410bc 6915}
90adcf20 6916
5f5c8ee5 6917
e037b9ec
GM
6918/* Get information about the tool-bar item which is displayed in GLYPH
6919 on frame F. Return in *PROP_IDX the index where tool-bar item
6920 properties start in F->current_tool_bar_items. Value is zero if
6921 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
6922
6923int
e037b9ec 6924tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
6925 struct frame *f;
6926 struct glyph *glyph;
6927 int *prop_idx;
90adcf20 6928{
5f5c8ee5
GM
6929 Lisp_Object prop;
6930 int success_p;
6931
6932 /* Get the text property `menu-item' at pos. The value of that
6933 property is the start index of this item's properties in
e037b9ec 6934 F->current_tool_bar_items. */
5f5c8ee5 6935 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 6936 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
6937 if (INTEGERP (prop))
6938 {
6939 *prop_idx = XINT (prop);
6940 success_p = 1;
6941 }
6942 else
6943 success_p = 0;
90adcf20 6944
5f5c8ee5
GM
6945 return success_p;
6946}
6947
6948#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6949
feb0c42f 6950
5f5c8ee5
GM
6951\f
6952/************************************************************************
6953 Horizontal scrolling
6954 ************************************************************************/
feb0c42f 6955
5f5c8ee5
GM
6956static int hscroll_window_tree P_ ((Lisp_Object));
6957static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6958
5f5c8ee5
GM
6959/* For all leaf windows in the window tree rooted at WINDOW, set their
6960 hscroll value so that PT is (i) visible in the window, and (ii) so
6961 that it is not within a certain margin at the window's left and
6962 right border. Value is non-zero if any window's hscroll has been
6963 changed. */
6964
6965static int
6966hscroll_window_tree (window)
6967 Lisp_Object window;
6968{
6969 int hscrolled_p = 0;
6970
6971 while (WINDOWP (window))
90adcf20 6972 {
5f5c8ee5
GM
6973 struct window *w = XWINDOW (window);
6974
6975 if (WINDOWP (w->hchild))
6976 hscrolled_p |= hscroll_window_tree (w->hchild);
6977 else if (WINDOWP (w->vchild))
6978 hscrolled_p |= hscroll_window_tree (w->vchild);
6979 else if (w->cursor.vpos >= 0)
6980 {
6981 int hscroll_margin, text_area_x, text_area_y;
6982 int text_area_width, text_area_height;
92a90e89
GM
6983 struct glyph_row *current_cursor_row
6984 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6985 struct glyph_row *desired_cursor_row
6986 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6987 struct glyph_row *cursor_row
6988 = (desired_cursor_row->enabled_p
6989 ? desired_cursor_row
6990 : current_cursor_row);
a2725ab2 6991
5f5c8ee5
GM
6992 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6993 &text_area_width, &text_area_height);
90adcf20 6994
5f5c8ee5
GM
6995 /* Scroll when cursor is inside this scroll margin. */
6996 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6997
6998 if ((XFASTINT (w->hscroll)
6999 && w->cursor.x < hscroll_margin)
92a90e89
GM
7000 || (cursor_row->enabled_p
7001 && cursor_row->truncated_on_right_p
5f5c8ee5 7002 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 7003 {
5f5c8ee5
GM
7004 struct it it;
7005 int hscroll;
7006 struct buffer *saved_current_buffer;
7007 int pt;
7008
7009 /* Find point in a display of infinite width. */
7010 saved_current_buffer = current_buffer;
7011 current_buffer = XBUFFER (w->buffer);
7012
7013 if (w == XWINDOW (selected_window))
7014 pt = BUF_PT (current_buffer);
7015 else
08b610e4 7016 {
5f5c8ee5
GM
7017 pt = marker_position (w->pointm);
7018 pt = max (BEGV, pt);
7019 pt = min (ZV, pt);
7020 }
7021
7022 /* Move iterator to pt starting at cursor_row->start in
7023 a line with infinite width. */
7024 init_to_row_start (&it, w, cursor_row);
7025 it.last_visible_x = INFINITY;
7026 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7027 current_buffer = saved_current_buffer;
7028
7029 /* Center cursor in window. */
7030 hscroll = (max (0, it.current_x - text_area_width / 2)
7031 / CANON_X_UNIT (it.f));
7032
7033 /* Don't call Fset_window_hscroll if value hasn't
7034 changed because it will prevent redisplay
7035 optimizations. */
7036 if (XFASTINT (w->hscroll) != hscroll)
7037 {
7038 Fset_window_hscroll (window, make_number (hscroll));
7039 hscrolled_p = 1;
08b610e4 7040 }
08b610e4 7041 }
08b610e4 7042 }
a2725ab2 7043
5f5c8ee5 7044 window = w->next;
90adcf20 7045 }
cd6dfed6 7046
5f5c8ee5
GM
7047 /* Value is non-zero if hscroll of any leaf window has been changed. */
7048 return hscrolled_p;
7049}
7050
7051
7052/* Set hscroll so that cursor is visible and not inside horizontal
7053 scroll margins for all windows in the tree rooted at WINDOW. See
7054 also hscroll_window_tree above. Value is non-zero if any window's
7055 hscroll has been changed. If it has, desired matrices on the frame
7056 of WINDOW are cleared. */
7057
7058static int
7059hscroll_windows (window)
7060 Lisp_Object window;
7061{
d475bcb8
GM
7062 int hscrolled_p;
7063
7064 if (automatic_hscrolling_p)
7065 {
7066 hscrolled_p = hscroll_window_tree (window);
7067 if (hscrolled_p)
7068 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7069 }
7070 else
7071 hscrolled_p = 0;
5f5c8ee5 7072 return hscrolled_p;
90adcf20 7073}
5f5c8ee5
GM
7074
7075
90adcf20 7076\f
5f5c8ee5
GM
7077/************************************************************************
7078 Redisplay
7079 ************************************************************************/
7080
7081/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7082 to a non-zero value. This is sometimes handy to have in a debugger
7083 session. */
7084
7085#if GLYPH_DEBUG
a2889657 7086
5f5c8ee5
GM
7087/* First and last unchanged row for try_window_id. */
7088
7089int debug_first_unchanged_at_end_vpos;
7090int debug_last_unchanged_at_beg_vpos;
7091
7092/* Delta vpos and y. */
7093
7094int debug_dvpos, debug_dy;
7095
7096/* Delta in characters and bytes for try_window_id. */
7097
7098int debug_delta, debug_delta_bytes;
7099
7100/* Values of window_end_pos and window_end_vpos at the end of
7101 try_window_id. */
7102
7103int debug_end_pos, debug_end_vpos;
7104
7105/* Append a string to W->desired_matrix->method. FMT is a printf
7106 format string. A1...A9 are a supplement for a variable-length
7107 argument list. If trace_redisplay_p is non-zero also printf the
7108 resulting string to stderr. */
7109
7110static void
7111debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7112 struct window *w;
7113 char *fmt;
7114 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7115{
7116 char buffer[512];
7117 char *method = w->desired_matrix->method;
7118 int len = strlen (method);
7119 int size = sizeof w->desired_matrix->method;
7120 int remaining = size - len - 1;
7121
7122 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7123 if (len && remaining)
7124 {
7125 method[len] = '|';
7126 --remaining, ++len;
7127 }
7128
7129 strncpy (method + len, buffer, remaining);
7130
7131 if (trace_redisplay_p)
7132 fprintf (stderr, "%p (%s): %s\n",
7133 w,
7134 ((BUFFERP (w->buffer)
7135 && STRINGP (XBUFFER (w->buffer)->name))
7136 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7137 : "no buffer"),
7138 buffer);
7139}
a2889657 7140
5f5c8ee5 7141#endif /* GLYPH_DEBUG */
90adcf20 7142
a2889657 7143
5f5c8ee5
GM
7144/* This counter is used to clear the face cache every once in a while
7145 in redisplay_internal. It is incremented for each redisplay.
7146 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7147 cleared. */
0d231165 7148
5f5c8ee5 7149#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
7150static int clear_face_cache_count;
7151
20de20dc 7152/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
7153
7154static struct frame *previous_terminal_frame;
7155
7156/* Non-zero while redisplay_internal is in progress. */
7157
7158int redisplaying_p;
7159
7160
7161/* Value is non-zero if all changes in window W, which displays
7162 current_buffer, are in the text between START and END. START is a
7163 buffer position, END is given as a distance from Z. Used in
7164 redisplay_internal for display optimization. */
7165
7166static INLINE int
7167text_outside_line_unchanged_p (w, start, end)
7168 struct window *w;
7169 int start, end;
7170{
7171 int unchanged_p = 1;
7172
7173 /* If text or overlays have changed, see where. */
7174 if (XFASTINT (w->last_modified) < MODIFF
7175 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7176 {
7177 /* Gap in the line? */
7178 if (GPT < start || Z - GPT < end)
7179 unchanged_p = 0;
7180
7181 /* Changes start in front of the line, or end after it? */
7182 if (unchanged_p
9142dd5b
GM
7183 && (BEG_UNCHANGED < start - 1
7184 || END_UNCHANGED < end))
5f5c8ee5
GM
7185 unchanged_p = 0;
7186
7187 /* If selective display, can't optimize if changes start at the
7188 beginning of the line. */
7189 if (unchanged_p
7190 && INTEGERP (current_buffer->selective_display)
7191 && XINT (current_buffer->selective_display) > 0
9142dd5b 7192 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
7193 unchanged_p = 0;
7194 }
7195
7196 return unchanged_p;
7197}
7198
7199
7200/* Do a frame update, taking possible shortcuts into account. This is
7201 the main external entry point for redisplay.
7202
7203 If the last redisplay displayed an echo area message and that message
7204 is no longer requested, we clear the echo area or bring back the
7205 mini-buffer if that is in use. */
20de20dc 7206
a2889657
JB
7207void
7208redisplay ()
e9874cee
RS
7209{
7210 redisplay_internal (0);
7211}
7212
260a86a0
KH
7213/* Return 1 if point moved out of or into a composition. Otherwise
7214 return 0. PREV_BUF and PREV_PT are the last point buffer and
7215 position. BUF and PT are the current point buffer and position. */
7216
7217int
7218check_point_in_composition (prev_buf, prev_pt, buf, pt)
7219 struct buffer *prev_buf, *buf;
7220 int prev_pt, pt;
7221{
7222 int start, end;
7223 Lisp_Object prop;
7224 Lisp_Object buffer;
7225
7226 XSETBUFFER (buffer, buf);
7227 /* Check a composition at the last point if point moved within the
7228 same buffer. */
7229 if (prev_buf == buf)
7230 {
7231 if (prev_pt == pt)
7232 /* Point didn't move. */
7233 return 0;
7234
7235 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7236 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7237 && COMPOSITION_VALID_P (start, end, prop)
7238 && start < prev_pt && end > prev_pt)
7239 /* The last point was within the composition. Return 1 iff
7240 point moved out of the composition. */
7241 return (pt <= start || pt >= end);
7242 }
7243
7244 /* Check a composition at the current point. */
7245 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7246 && find_composition (pt, -1, &start, &end, &prop, buffer)
7247 && COMPOSITION_VALID_P (start, end, prop)
7248 && start < pt && end > pt);
7249}
5f5c8ee5 7250
9142dd5b
GM
7251/* Reconsider the setting of B->clip_changed which is displayed
7252 in window W. */
7253
7254static INLINE void
7255reconsider_clip_changes (w, b)
7256 struct window *w;
7257 struct buffer *b;
7258{
7259 if (b->prevent_redisplay_optimizations_p)
7260 b->clip_changed = 1;
7261 else if (b->clip_changed
7262 && !NILP (w->window_end_valid)
7263 && w->current_matrix->buffer == b
7264 && w->current_matrix->zv == BUF_ZV (b)
7265 && w->current_matrix->begv == BUF_BEGV (b))
7266 b->clip_changed = 0;
260a86a0
KH
7267
7268 /* If display wasn't paused, and W is not a tool bar window, see if
7269 point has been moved into or out of a composition. In that case,
7270 we set b->clip_changed to 1 to force updating the screen. If
7271 b->clip_changed has already been set to 1, we can skip this
7272 check. */
7273 if (!b->clip_changed
7274 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7275 {
7276 int pt;
7277
7278 if (w == XWINDOW (selected_window))
7279 pt = BUF_PT (current_buffer);
7280 else
7281 pt = marker_position (w->pointm);
7282
7283 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 7284 || pt != XINT (w->last_point))
260a86a0 7285 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 7286 XINT (w->last_point),
260a86a0
KH
7287 XBUFFER (w->buffer), pt))
7288 b->clip_changed = 1;
7289 }
9142dd5b
GM
7290}
7291
7292
5f5c8ee5
GM
7293/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7294 response to any user action; therefore, we should preserve the echo
7295 area. (Actually, our caller does that job.) Perhaps in the future
7296 avoid recentering windows if it is not necessary; currently that
7297 causes some problems. */
e9874cee
RS
7298
7299static void
7300redisplay_internal (preserve_echo_area)
7301 int preserve_echo_area;
a2889657 7302{
5f5c8ee5
GM
7303 struct window *w = XWINDOW (selected_window);
7304 struct frame *f = XFRAME (w->frame);
7305 int pause;
a2889657 7306 int must_finish = 0;
5f5c8ee5 7307 struct text_pos tlbufpos, tlendpos;
89819bdd 7308 int number_of_visible_frames;
28514cd9 7309 int count;
886bd6f2 7310 struct frame *sf = SELECTED_FRAME ();
a2889657 7311
5f5c8ee5
GM
7312 /* Non-zero means redisplay has to consider all windows on all
7313 frames. Zero means, only selected_window is considered. */
7314 int consider_all_windows_p;
7315
7316 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7317
7318 /* No redisplay if running in batch mode or frame is not yet fully
7319 initialized, or redisplay is explicitly turned off by setting
7320 Vinhibit_redisplay. */
7321 if (noninteractive
7322 || !NILP (Vinhibit_redisplay)
7323 || !f->glyphs_initialized_p)
a2889657
JB
7324 return;
7325
5f5c8ee5
GM
7326 /* The flag redisplay_performed_directly_p is set by
7327 direct_output_for_insert when it already did the whole screen
7328 update necessary. */
7329 if (redisplay_performed_directly_p)
7330 {
7331 redisplay_performed_directly_p = 0;
7332 if (!hscroll_windows (selected_window))
7333 return;
7334 }
7335
15f0cf78
RS
7336#ifdef USE_X_TOOLKIT
7337 if (popup_activated ())
7338 return;
7339#endif
7340
28514cd9 7341 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 7342 if (redisplaying_p)
735c094c
KH
7343 return;
7344
28514cd9
GM
7345 /* Record a function that resets redisplaying_p to its old value
7346 when we leave this function. */
7347 count = specpdl_ptr - specpdl;
7348 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7349 ++redisplaying_p;
7350
8b32d885
RS
7351 retry:
7352
9142dd5b
GM
7353 reconsider_clip_changes (w, current_buffer);
7354
5f5c8ee5
GM
7355 /* If new fonts have been loaded that make a glyph matrix adjustment
7356 necessary, do it. */
7357 if (fonts_changed_p)
7358 {
7359 adjust_glyphs (NULL);
7360 ++windows_or_buffers_changed;
7361 fonts_changed_p = 0;
7362 }
7363
886bd6f2
GM
7364 if (! FRAME_WINDOW_P (sf)
7365 && previous_terminal_frame != sf)
20de20dc 7366 {
5f5c8ee5
GM
7367 /* Since frames on an ASCII terminal share the same display
7368 area, displaying a different frame means redisplay the whole
7369 thing. */
20de20dc 7370 windows_or_buffers_changed++;
886bd6f2
GM
7371 SET_FRAME_GARBAGED (sf);
7372 XSETFRAME (Vterminal_frame, sf);
20de20dc 7373 }
886bd6f2 7374 previous_terminal_frame = sf;
20de20dc 7375
5f5c8ee5
GM
7376 /* Set the visible flags for all frames. Do this before checking
7377 for resized or garbaged frames; they want to know if their frames
7378 are visible. See the comment in frame.h for
7379 FRAME_SAMPLE_VISIBILITY. */
d724d989 7380 {
35f56f96 7381 Lisp_Object tail, frame;
d724d989 7382
89819bdd
RS
7383 number_of_visible_frames = 0;
7384
35f56f96 7385 FOR_EACH_FRAME (tail, frame)
f82aff7c 7386 {
5f5c8ee5
GM
7387 struct frame *f = XFRAME (frame);
7388
7389 FRAME_SAMPLE_VISIBILITY (f);
7390 if (FRAME_VISIBLE_P (f))
7391 ++number_of_visible_frames;
7392 clear_desired_matrices (f);
f82aff7c 7393 }
d724d989
JB
7394 }
7395
44fa5b1e 7396 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 7397 do_pending_window_change (1);
a2889657 7398
5f5c8ee5 7399 /* Clear frames marked as garbaged. */
44fa5b1e 7400 if (frame_garbaged)
c6e89d6c 7401 clear_garbaged_frames ();
a2889657 7402
e037b9ec 7403 /* Build menubar and tool-bar items. */
f82aff7c
RS
7404 prepare_menu_bars ();
7405
28995e67 7406 if (windows_or_buffers_changed)
a2889657
JB
7407 update_mode_lines++;
7408
538f13d4
RS
7409 /* Detect case that we need to write or remove a star in the mode line. */
7410 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
7411 {
7412 w->update_mode_line = Qt;
7413 if (buffer_shared > 1)
7414 update_mode_lines++;
7415 }
7416
5f5c8ee5 7417 /* If %c is in the mode line, update it if needed. */
28995e67
RS
7418 if (!NILP (w->column_number_displayed)
7419 /* This alternative quickly identifies a common case
7420 where no change is needed. */
7421 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7422 && XFASTINT (w->last_modified) >= MODIFF
7423 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7424 && XFASTINT (w->column_number_displayed) != current_column ())
7425 w->update_mode_line = Qt;
7426
44fa5b1e 7427 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 7428
5f5c8ee5
GM
7429 /* The variable buffer_shared is set in redisplay_window and
7430 indicates that we redisplay a buffer in different windows. See
7431 there. */
7432 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
7433
7434 /* If specs for an arrow have changed, do thorough redisplay
7435 to ensure we remove any arrow that should no longer exist. */
d45de95b 7436 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 7437 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 7438 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 7439
90adcf20
RS
7440 /* Normally the message* functions will have already displayed and
7441 updated the echo area, but the frame may have been trashed, or
7442 the update may have been preempted, so display the echo area
c6e89d6c
GM
7443 again here. Checking both message buffers captures the case that
7444 the echo area should be cleared. */
7445 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 7446 {
c6e89d6c 7447 int window_height_changed_p = echo_area_display (0);
90adcf20 7448 must_finish = 1;
dd2eb166 7449
c6e89d6c
GM
7450 if (fonts_changed_p)
7451 goto retry;
7452 else if (window_height_changed_p)
7453 {
7454 consider_all_windows_p = 1;
7455 ++update_mode_lines;
7456 ++windows_or_buffers_changed;
9142dd5b
GM
7457
7458 /* If window configuration was changed, frames may have been
7459 marked garbaged. Clear them or we will experience
7460 surprises wrt scrolling. */
7461 if (frame_garbaged)
7462 clear_garbaged_frames ();
c6e89d6c 7463 }
90adcf20 7464 }
dd2eb166
GM
7465 else if (w == XWINDOW (minibuf_window)
7466 && (current_buffer->clip_changed
7467 || XFASTINT (w->last_modified) < MODIFF
7468 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 7469 && resize_mini_window (w, 0))
c6e89d6c
GM
7470 {
7471 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
7472 showing if its contents might have changed. */
7473 must_finish = 1;
7474 consider_all_windows_p = 1;
c6e89d6c 7475 ++windows_or_buffers_changed;
dd2eb166 7476 ++update_mode_lines;
9142dd5b
GM
7477
7478 /* If window configuration was changed, frames may have been
7479 marked garbaged. Clear them or we will experience
7480 surprises wrt scrolling. */
7481 if (frame_garbaged)
7482 clear_garbaged_frames ();
c6e89d6c
GM
7483 }
7484
90adcf20 7485
5f5c8ee5
GM
7486 /* If showing the region, and mark has changed, we must redisplay
7487 the whole window. The assignment to this_line_start_pos prevents
7488 the optimization directly below this if-statement. */
bd66d1ba
RS
7489 if (((!NILP (Vtransient_mark_mode)
7490 && !NILP (XBUFFER (w->buffer)->mark_active))
7491 != !NILP (w->region_showing))
82d04750
JB
7492 || (!NILP (w->region_showing)
7493 && !EQ (w->region_showing,
7494 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
7495 CHARPOS (this_line_start_pos) = 0;
7496
7497 /* Optimize the case that only the line containing the cursor in the
7498 selected window has changed. Variables starting with this_ are
7499 set in display_line and record information about the line
7500 containing the cursor. */
7501 tlbufpos = this_line_start_pos;
7502 tlendpos = this_line_end_pos;
7503 if (!consider_all_windows_p
7504 && CHARPOS (tlbufpos) > 0
7505 && NILP (w->update_mode_line)
73af359d 7506 && !current_buffer->clip_changed
44fa5b1e 7507 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 7508 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 7509 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
7510 && this_line_buffer == current_buffer
7511 && current_buffer == XBUFFER (w->buffer)
265a9e55 7512 && NILP (w->force_start)
5f5c8ee5
GM
7513 /* Point must be on the line that we have info recorded about. */
7514 && PT >= CHARPOS (tlbufpos)
7515 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
7516 /* All text outside that line, including its final newline,
7517 must be unchanged */
5f5c8ee5
GM
7518 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7519 CHARPOS (tlendpos)))
7520 {
7521 if (CHARPOS (tlbufpos) > BEGV
7522 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7523 && (CHARPOS (tlbufpos) == ZV
7524 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
7525 /* Former continuation line has disappeared by becoming empty */
7526 goto cancel;
7527 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 7528 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
7529 || MINI_WINDOW_P (w))
7530 {
1c9241f5
KH
7531 /* We have to handle the case of continuation around a
7532 wide-column character (See the comment in indent.c around
7533 line 885).
7534
7535 For instance, in the following case:
7536
7537 -------- Insert --------
7538 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7539 J_I_ ==> J_I_ `^^' are cursors.
7540 ^^ ^^
7541 -------- --------
7542
7543 As we have to redraw the line above, we should goto cancel. */
7544
5f5c8ee5
GM
7545 struct it it;
7546 int line_height_before = this_line_pixel_height;
7547
7548 /* Note that start_display will handle the case that the
7549 line starting at tlbufpos is a continuation lines. */
7550 start_display (&it, w, tlbufpos);
7551
7552 /* Implementation note: It this still necessary? */
7553 if (it.current_x != this_line_start_x)
1c9241f5
KH
7554 goto cancel;
7555
5f5c8ee5
GM
7556 TRACE ((stderr, "trying display optimization 1\n"));
7557 w->cursor.vpos = -1;
a2889657 7558 overlay_arrow_seen = 0;
5f5c8ee5
GM
7559 it.vpos = this_line_vpos;
7560 it.current_y = this_line_y;
7561 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7562 display_line (&it);
7563
a2889657 7564 /* If line contains point, is not continued,
5f5c8ee5
GM
7565 and ends at same distance from eob as before, we win */
7566 if (w->cursor.vpos >= 0
7567 /* Line is not continued, otherwise this_line_start_pos
7568 would have been set to 0 in display_line. */
7569 && CHARPOS (this_line_start_pos)
7570 /* Line ends as before. */
7571 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7572 /* Line has same height as before. Otherwise other lines
7573 would have to be shifted up or down. */
7574 && this_line_pixel_height == line_height_before)
a2889657 7575 {
5f5c8ee5
GM
7576 /* If this is not the window's last line, we must adjust
7577 the charstarts of the lines below. */
7578 if (it.current_y < it.last_visible_y)
7579 {
7580 struct glyph_row *row
7581 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7582 int delta, delta_bytes;
7583
7584 if (Z - CHARPOS (tlendpos) == ZV)
7585 {
7586 /* This line ends at end of (accessible part of)
7587 buffer. There is no newline to count. */
7588 delta = (Z
7589 - CHARPOS (tlendpos)
7590 - MATRIX_ROW_START_CHARPOS (row));
7591 delta_bytes = (Z_BYTE
7592 - BYTEPOS (tlendpos)
7593 - MATRIX_ROW_START_BYTEPOS (row));
7594 }
7595 else
7596 {
7597 /* This line ends in a newline. Must take
7598 account of the newline and the rest of the
7599 text that follows. */
7600 delta = (Z
7601 - CHARPOS (tlendpos)
7602 - MATRIX_ROW_START_CHARPOS (row));
7603 delta_bytes = (Z_BYTE
7604 - BYTEPOS (tlendpos)
7605 - MATRIX_ROW_START_BYTEPOS (row));
7606 }
7607
f2d86d7a
GM
7608 increment_matrix_positions (w->current_matrix,
7609 this_line_vpos + 1,
7610 w->current_matrix->nrows,
7611 delta, delta_bytes);
85bcef6c 7612 }
46db8486 7613
5f5c8ee5
GM
7614 /* If this row displays text now but previously didn't,
7615 or vice versa, w->window_end_vpos may have to be
7616 adjusted. */
7617 if ((it.glyph_row - 1)->displays_text_p)
7618 {
7619 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7620 XSETINT (w->window_end_vpos, this_line_vpos);
7621 }
7622 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7623 && this_line_vpos > 0)
7624 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7625 w->window_end_valid = Qnil;
7626
7627 /* Update hint: No need to try to scroll in update_window. */
7628 w->desired_matrix->no_scrolling_p = 1;
7629
7630#if GLYPH_DEBUG
7631 *w->desired_matrix->method = 0;
7632 debug_method_add (w, "optimization 1");
7633#endif
a2889657
JB
7634 goto update;
7635 }
7636 else
7637 goto cancel;
7638 }
5f5c8ee5
GM
7639 else if (/* Cursor position hasn't changed. */
7640 PT == XFASTINT (w->last_point)
b6f0fe04
RS
7641 /* Make sure the cursor was last displayed
7642 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
7643 && 0 <= w->cursor.vpos
7644 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
7645 {
7646 if (!must_finish)
7647 {
c6e89d6c 7648 do_pending_window_change (1);
5f5c8ee5
GM
7649
7650 /* We used to always goto end_of_redisplay here, but this
7651 isn't enough if we have a blinking cursor. */
7652 if (w->cursor_off_p == w->last_cursor_off_p)
7653 goto end_of_redisplay;
a2889657
JB
7654 }
7655 goto update;
7656 }
8b51f1e3
KH
7657 /* If highlighting the region, or if the cursor is in the echo area,
7658 then we can't just move the cursor. */
bd66d1ba
RS
7659 else if (! (!NILP (Vtransient_mark_mode)
7660 && !NILP (current_buffer->mark_active))
293a54ce
RS
7661 && (w == XWINDOW (current_buffer->last_selected_window)
7662 || highlight_nonselected_windows)
8b51f1e3 7663 && NILP (w->region_showing)
8f897821 7664 && NILP (Vshow_trailing_whitespace)
8b51f1e3 7665 && !cursor_in_echo_area)
a2889657 7666 {
5f5c8ee5
GM
7667 struct it it;
7668 struct glyph_row *row;
7669
7670 /* Skip from tlbufpos to PT and see where it is. Note that
7671 PT may be in invisible text. If so, we will end at the
7672 next visible position. */
7673 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7674 NULL, DEFAULT_FACE_ID);
7675 it.current_x = this_line_start_x;
7676 it.current_y = this_line_y;
7677 it.vpos = this_line_vpos;
7678
7679 /* The call to move_it_to stops in front of PT, but
7680 moves over before-strings. */
7681 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7682
7683 if (it.vpos == this_line_vpos
7684 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7685 row->enabled_p))
a2889657 7686 {
5f5c8ee5
GM
7687 xassert (this_line_vpos == it.vpos);
7688 xassert (this_line_y == it.current_y);
7689 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
7690 goto update;
7691 }
7692 else
7693 goto cancel;
7694 }
5f5c8ee5 7695
a2889657 7696 cancel:
5f5c8ee5
GM
7697 /* Text changed drastically or point moved off of line. */
7698 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
7699 }
7700
5f5c8ee5
GM
7701 CHARPOS (this_line_start_pos) = 0;
7702 consider_all_windows_p |= buffer_shared > 1;
7703 ++clear_face_cache_count;
a2889657 7704
5f5c8ee5
GM
7705
7706 /* Build desired matrices. If consider_all_windows_p is non-zero,
7707 do it for all windows on all frames. Otherwise do it for
7708 selected_window, only. */
463f6b91 7709
5f5c8ee5 7710 if (consider_all_windows_p)
a2889657 7711 {
35f56f96 7712 Lisp_Object tail, frame;
a2889657 7713
5f5c8ee5
GM
7714 /* Clear the face cache eventually. */
7715 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 7716 {
5f5c8ee5 7717 clear_face_cache (0);
463f6b91
RS
7718 clear_face_cache_count = 0;
7719 }
31b24551 7720
5f5c8ee5
GM
7721 /* Recompute # windows showing selected buffer. This will be
7722 incremented each time such a window is displayed. */
a2889657
JB
7723 buffer_shared = 0;
7724
35f56f96 7725 FOR_EACH_FRAME (tail, frame)
30c566e4 7726 {
5f5c8ee5 7727 struct frame *f = XFRAME (frame);
886bd6f2 7728 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 7729 {
5f5c8ee5
GM
7730 /* Mark all the scroll bars to be removed; we'll redeem
7731 the ones we want when we redisplay their windows. */
9769686d
RS
7732 if (condemn_scroll_bars_hook)
7733 (*condemn_scroll_bars_hook) (f);
30c566e4 7734
f21ef775 7735 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 7736 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 7737
5f5c8ee5
GM
7738 /* Any scroll bars which redisplay_windows should have
7739 nuked should now go away. */
9769686d
RS
7740 if (judge_scroll_bars_hook)
7741 (*judge_scroll_bars_hook) (f);
7742 }
30c566e4 7743 }
a2889657 7744 }
886bd6f2
GM
7745 else if (FRAME_VISIBLE_P (sf)
7746 && !FRAME_OBSCURED_P (sf))
5f5c8ee5
GM
7747 redisplay_window (selected_window, 1);
7748
7749
7750 /* Compare desired and current matrices, perform output. */
7751
7752update:
7753
7754 /* If fonts changed, display again. */
7755 if (fonts_changed_p)
7756 goto retry;
a2889657 7757
a2889657
JB
7758 /* Prevent various kinds of signals during display update.
7759 stdio is not robust about handling signals,
7760 which can cause an apparent I/O error. */
7761 if (interrupt_input)
7762 unrequest_sigio ();
7763 stop_polling ();
7764
5f5c8ee5 7765 if (consider_all_windows_p)
a2889657
JB
7766 {
7767 Lisp_Object tail;
92a90e89
GM
7768 struct frame *f;
7769 int hscrolled_p;
a2889657
JB
7770
7771 pause = 0;
92a90e89 7772 hscrolled_p = 0;
a2889657 7773
92a90e89 7774 /* See if we have to hscroll. */
9472f927 7775 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
92a90e89
GM
7776 if (FRAMEP (XCAR (tail)))
7777 {
7778 f = XFRAME (XCAR (tail));
7779
7780 if ((FRAME_WINDOW_P (f)
886bd6f2 7781 || f == sf)
92a90e89
GM
7782 && FRAME_VISIBLE_P (f)
7783 && !FRAME_OBSCURED_P (f)
7784 && hscroll_windows (f->root_window))
7785 hscrolled_p = 1;
7786 }
7787
7788 if (hscrolled_p)
7789 goto retry;
a2889657 7790
92a90e89
GM
7791 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7792 {
9472f927 7793 if (!FRAMEP (XCAR (tail)))
a2889657
JB
7794 continue;
7795
9472f927 7796 f = XFRAME (XCAR (tail));
1af9f229 7797
886bd6f2 7798 if ((FRAME_WINDOW_P (f) || f == sf)
f21ef775 7799 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 7800 {
5f5c8ee5
GM
7801 /* Mark all windows as to be updated. */
7802 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 7803 pause |= update_frame (f, 0, 0);
a2889657 7804 if (!pause)
efc63ef0
RS
7805 {
7806 mark_window_display_accurate (f->root_window, 1);
7807 if (frame_up_to_date_hook != 0)
7808 (*frame_up_to_date_hook) (f);
7809 }
a2889657
JB
7810 }
7811 }
7812 }
7813 else
6e8290aa 7814 {
886bd6f2
GM
7815 if (FRAME_VISIBLE_P (sf)
7816 && !FRAME_OBSCURED_P (sf))
5f5c8ee5 7817 {
92a90e89
GM
7818 if (hscroll_windows (selected_window))
7819 goto retry;
7820
5f5c8ee5 7821 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 7822 pause = update_frame (sf, 0, 0);
5f5c8ee5 7823 }
4d641a15
KH
7824 else
7825 pause = 0;
d724d989 7826
8de2d90b 7827 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
7828 function. If the echo area is on another frame, that may
7829 have put text on a frame other than the selected one, so the
7830 above call to update_frame would not have caught it. Catch
8de2d90b
JB
7831 it here. */
7832 {
84faf44c 7833 Lisp_Object mini_window;
5f5c8ee5 7834 struct frame *mini_frame;
84faf44c 7835
886bd6f2 7836 mini_window = FRAME_MINIBUF_WINDOW (sf);
84faf44c 7837 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 7838
886bd6f2 7839 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
7840 {
7841 XWINDOW (mini_window)->must_be_updated_p = 1;
7842 pause |= update_frame (mini_frame, 0, 0);
7843 if (!pause && hscroll_windows (mini_window))
7844 goto retry;
7845 }
8de2d90b 7846 }
6e8290aa 7847 }
a2889657 7848
5f5c8ee5
GM
7849 /* If display was paused because of pending input, make sure we do a
7850 thorough update the next time. */
a2889657
JB
7851 if (pause)
7852 {
5f5c8ee5
GM
7853 /* Prevent the optimization at the beginning of
7854 redisplay_internal that tries a single-line update of the
7855 line containing the cursor in the selected window. */
7856 CHARPOS (this_line_start_pos) = 0;
7857
7858 /* Let the overlay arrow be updated the next time. */
265a9e55 7859 if (!NILP (last_arrow_position))
a2889657
JB
7860 {
7861 last_arrow_position = Qt;
7862 last_arrow_string = Qt;
7863 }
5f5c8ee5
GM
7864
7865 /* If we pause after scrolling, some rows in the current
7866 matrices of some windows are not valid. */
7867 if (!WINDOW_FULL_WIDTH_P (w)
7868 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
7869 update_mode_lines = 1;
7870 }
7871
5f5c8ee5
GM
7872 /* Now text on frame agrees with windows, so put info into the
7873 windows for partial redisplay to follow. */
a2889657
JB
7874 if (!pause)
7875 {
7876 register struct buffer *b = XBUFFER (w->buffer);
7877
9142dd5b
GM
7878 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7879 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7880 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7881 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 7882
5f5c8ee5 7883 if (consider_all_windows_p)
886bd6f2 7884 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
a2889657
JB
7885 else
7886 {
5f5c8ee5
GM
7887 XSETFASTINT (w->last_point, BUF_PT (b));
7888 w->last_cursor = w->cursor;
7889 w->last_cursor_off_p = w->cursor_off_p;
7890
28995e67 7891 b->clip_changed = 0;
9142dd5b 7892 b->prevent_redisplay_optimizations_p = 0;
a2889657 7893 w->update_mode_line = Qnil;
c2213350 7894 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 7895 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
7896 w->last_had_star
7897 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7898 ? Qt : Qnil);
3ee4159a
RS
7899
7900 /* Record if we are showing a region, so can make sure to
7901 update it fully at next redisplay. */
7902 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
7903 && (w == XWINDOW (current_buffer->last_selected_window)
7904 || highlight_nonselected_windows)
3ee4159a
RS
7905 && !NILP (XBUFFER (w->buffer)->mark_active)
7906 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7907 : Qnil);
7908
d2f84654 7909 w->window_end_valid = w->buffer;
d45de95b 7910 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 7911 last_arrow_string = Voverlay_arrow_string;
efc63ef0 7912 if (frame_up_to_date_hook != 0)
886bd6f2 7913 (*frame_up_to_date_hook) (sf);
9142dd5b
GM
7914
7915 w->current_matrix->buffer = b;
7916 w->current_matrix->begv = BUF_BEGV (b);
7917 w->current_matrix->zv = BUF_ZV (b);
a2889657 7918 }
15e26c76 7919
a2889657
JB
7920 update_mode_lines = 0;
7921 windows_or_buffers_changed = 0;
7922 }
7923
5f5c8ee5
GM
7924 /* Start SIGIO interrupts coming again. Having them off during the
7925 code above makes it less likely one will discard output, but not
7926 impossible, since there might be stuff in the system buffer here.
a2889657 7927 But it is much hairier to try to do anything about that. */
a2889657
JB
7928 if (interrupt_input)
7929 request_sigio ();
7930 start_polling ();
7931
5f5c8ee5
GM
7932 /* If a frame has become visible which was not before, redisplay
7933 again, so that we display it. Expose events for such a frame
7934 (which it gets when becoming visible) don't call the parts of
7935 redisplay constructing glyphs, so simply exposing a frame won't
7936 display anything in this case. So, we have to display these
7937 frames here explicitly. */
11c52c4f
RS
7938 if (!pause)
7939 {
7940 Lisp_Object tail, frame;
7941 int new_count = 0;
7942
7943 FOR_EACH_FRAME (tail, frame)
7944 {
7945 int this_is_visible = 0;
8e83f802
RS
7946
7947 if (XFRAME (frame)->visible)
7948 this_is_visible = 1;
7949 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7950 if (XFRAME (frame)->visible)
7951 this_is_visible = 1;
11c52c4f
RS
7952
7953 if (this_is_visible)
7954 new_count++;
7955 }
7956
89819bdd 7957 if (new_count != number_of_visible_frames)
11c52c4f
RS
7958 windows_or_buffers_changed++;
7959 }
7960
44fa5b1e 7961 /* Change frame size now if a change is pending. */
c6e89d6c 7962 do_pending_window_change (1);
d8e242fd 7963
8b32d885
RS
7964 /* If we just did a pending size change, or have additional
7965 visible frames, redisplay again. */
3c8c72e0 7966 if (windows_or_buffers_changed && !pause)
8b32d885 7967 goto retry;
5f5c8ee5
GM
7968
7969 end_of_redisplay:;
c6e89d6c 7970
28514cd9 7971 unbind_to (count, Qnil);
a2889657
JB
7972}
7973
5f5c8ee5
GM
7974
7975/* Redisplay, but leave alone any recent echo area message unless
7976 another message has been requested in its place.
a2889657
JB
7977
7978 This is useful in situations where you need to redisplay but no
7979 user action has occurred, making it inappropriate for the message
7980 area to be cleared. See tracking_off and
7981 wait_reading_process_input for examples of these situations. */
7982
8991bb31 7983void
a2889657
JB
7984redisplay_preserve_echo_area ()
7985{
c6e89d6c 7986 if (!NILP (echo_area_buffer[1]))
a2889657 7987 {
c6e89d6c
GM
7988 /* We have a previously displayed message, but no current
7989 message. Redisplay the previous message. */
7990 display_last_displayed_message_p = 1;
e9874cee 7991 redisplay_internal (1);
c6e89d6c 7992 display_last_displayed_message_p = 0;
a2889657
JB
7993 }
7994 else
e9874cee 7995 redisplay_internal (1);
a2889657
JB
7996}
7997
5f5c8ee5 7998
28514cd9
GM
7999/* Function registered with record_unwind_protect in
8000 redisplay_internal. Clears the flag indicating that a redisplay is
8001 in progress. */
8002
8003static Lisp_Object
8004unwind_redisplay (old_redisplaying_p)
8005 Lisp_Object old_redisplaying_p;
8006{
8007 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 8008 return Qnil;
28514cd9
GM
8009}
8010
8011
5f5c8ee5
GM
8012/* Mark the display of windows in the window tree rooted at WINDOW as
8013 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8014 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8015 the next time redisplay_internal is called. */
8016
a2889657 8017void
5f5c8ee5 8018mark_window_display_accurate (window, accurate_p)
a2889657 8019 Lisp_Object window;
5f5c8ee5 8020 int accurate_p;
a2889657 8021{
5f5c8ee5
GM
8022 struct window *w;
8023
8024 for (; !NILP (window); window = w->next)
a2889657
JB
8025 {
8026 w = XWINDOW (window);
8027
5f5c8ee5 8028 if (BUFFERP (w->buffer))
bd66d1ba 8029 {
5f5c8ee5
GM
8030 struct buffer *b = XBUFFER (w->buffer);
8031
c2213350 8032 XSETFASTINT (w->last_modified,
5f5c8ee5 8033 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 8034 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
8035 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8036 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8037 ? Qt : Qnil);
bd66d1ba 8038
5f5c8ee5
GM
8039#if 0 /* I don't think this is necessary because display_line does it.
8040 Let's check it. */
bd66d1ba
RS
8041 /* Record if we are showing a region, so can make sure to
8042 update it fully at next redisplay. */
5f5c8ee5
GM
8043 w->region_showing
8044 = (!NILP (Vtransient_mark_mode)
8045 && (w == XWINDOW (current_buffer->last_selected_window)
8046 || highlight_nonselected_windows)
8047 && (!NILP (b->mark_active)
8048 ? Fmarker_position (b->mark)
8049 : Qnil));
8050#endif
8051
8052 if (accurate_p)
8053 {
8054 b->clip_changed = 0;
9142dd5b
GM
8055 b->prevent_redisplay_optimizations_p = 0;
8056 w->current_matrix->buffer = b;
8057 w->current_matrix->begv = BUF_BEGV (b);
8058 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
8059 w->last_cursor = w->cursor;
8060 w->last_cursor_off_p = w->cursor_off_p;
8061 if (w == XWINDOW (selected_window))
6fc556fd 8062 w->last_point = make_number (BUF_PT (b));
5f5c8ee5 8063 else
6fc556fd 8064 w->last_point = make_number (XMARKER (w->pointm)->charpos);
5f5c8ee5 8065 }
bd66d1ba
RS
8066 }
8067
d2f84654 8068 w->window_end_valid = w->buffer;
a2889657
JB
8069 w->update_mode_line = Qnil;
8070
265a9e55 8071 if (!NILP (w->vchild))
5f5c8ee5 8072 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 8073 if (!NILP (w->hchild))
5f5c8ee5 8074 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
8075 }
8076
5f5c8ee5 8077 if (accurate_p)
a2889657 8078 {
d45de95b 8079 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
8080 last_arrow_string = Voverlay_arrow_string;
8081 }
8082 else
8083 {
5f5c8ee5
GM
8084 /* Force a thorough redisplay the next time by setting
8085 last_arrow_position and last_arrow_string to t, which is
8086 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
8087 last_arrow_position = Qt;
8088 last_arrow_string = Qt;
8089 }
8090}
5f5c8ee5
GM
8091
8092
8093/* Return value in display table DP (Lisp_Char_Table *) for character
8094 C. Since a display table doesn't have any parent, we don't have to
8095 follow parent. Do not call this function directly but use the
8096 macro DISP_CHAR_VECTOR. */
8097
8098Lisp_Object
8099disp_char_vector (dp, c)
8100 struct Lisp_Char_Table *dp;
8101 int c;
8102{
8103 int code[4], i;
8104 Lisp_Object val;
8105
8106 if (SINGLE_BYTE_CHAR_P (c))
8107 return (dp->contents[c]);
8108
c5924f47 8109 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
8110 if (code[1] < 32)
8111 code[1] = -1;
8112 else if (code[2] < 32)
8113 code[2] = -1;
5f5c8ee5
GM
8114
8115 /* Here, the possible range of code[0] (== charset ID) is
8116 128..max_charset. Since the top level char table contains data
8117 for multibyte characters after 256th element, we must increment
8118 code[0] by 128 to get a correct index. */
8119 code[0] += 128;
8120 code[3] = -1; /* anchor */
8121
8122 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8123 {
8124 val = dp->contents[code[i]];
8125 if (!SUB_CHAR_TABLE_P (val))
8126 return (NILP (val) ? dp->defalt : val);
8127 }
8128
8129 /* Here, val is a sub char table. We return the default value of
8130 it. */
8131 return (dp->defalt);
8132}
8133
8134
a2889657 8135\f
5f5c8ee5
GM
8136/***********************************************************************
8137 Window Redisplay
8138 ***********************************************************************/
a2725ab2 8139
5f5c8ee5 8140/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
8141
8142static void
5f5c8ee5
GM
8143redisplay_windows (window)
8144 Lisp_Object window;
90adcf20 8145{
5f5c8ee5
GM
8146 while (!NILP (window))
8147 {
8148 struct window *w = XWINDOW (window);
8149
8150 if (!NILP (w->hchild))
8151 redisplay_windows (w->hchild);
8152 else if (!NILP (w->vchild))
8153 redisplay_windows (w->vchild);
8154 else
8155 redisplay_window (window, 0);
a2725ab2 8156
5f5c8ee5
GM
8157 window = w->next;
8158 }
8159}
8160
8161
8162/* Set cursor position of W. PT is assumed to be displayed in ROW.
8163 DELTA is the number of bytes by which positions recorded in ROW
8164 differ from current buffer positions. */
8165
8166void
8167set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8168 struct window *w;
8169 struct glyph_row *row;
8170 struct glyph_matrix *matrix;
8171 int delta, delta_bytes, dy, dvpos;
8172{
8173 struct glyph *glyph = row->glyphs[TEXT_AREA];
8174 struct glyph *end = glyph + row->used[TEXT_AREA];
8175 int x = row->x;
8176 int pt_old = PT - delta;
8177
8178 /* Skip over glyphs not having an object at the start of the row.
8179 These are special glyphs like truncation marks on terminal
8180 frames. */
8181 if (row->displays_text_p)
8182 while (glyph < end
6fc556fd 8183 && INTEGERP (glyph->object)
5f5c8ee5
GM
8184 && glyph->charpos < 0)
8185 {
8186 x += glyph->pixel_width;
8187 ++glyph;
8188 }
8189
8190 while (glyph < end
6fc556fd 8191 && !INTEGERP (glyph->object)
5f5c8ee5
GM
8192 && (!BUFFERP (glyph->object)
8193 || glyph->charpos < pt_old))
8194 {
8195 x += glyph->pixel_width;
8196 ++glyph;
8197 }
8198
8199 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8200 w->cursor.x = x;
8201 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8202 w->cursor.y = row->y + dy;
8203
8204 if (w == XWINDOW (selected_window))
8205 {
8206 if (!row->continued_p
8207 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8208 && row->x == 0)
8209 {
8210 this_line_buffer = XBUFFER (w->buffer);
8211
8212 CHARPOS (this_line_start_pos)
8213 = MATRIX_ROW_START_CHARPOS (row) + delta;
8214 BYTEPOS (this_line_start_pos)
8215 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8216
8217 CHARPOS (this_line_end_pos)
8218 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8219 BYTEPOS (this_line_end_pos)
8220 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8221
8222 this_line_y = w->cursor.y;
8223 this_line_pixel_height = row->height;
8224 this_line_vpos = w->cursor.vpos;
8225 this_line_start_x = row->x;
8226 }
8227 else
8228 CHARPOS (this_line_start_pos) = 0;
8229 }
8230}
8231
8232
8233/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
8234 start STARTP. Sets the window start of WINDOW to that position.
8235
8236 We assume that the window's buffer is really current. */
5f5c8ee5
GM
8237
8238static INLINE struct text_pos
8239run_window_scroll_functions (window, startp)
8240 Lisp_Object window;
8241 struct text_pos startp;
8242{
8243 struct window *w = XWINDOW (window);
8244 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
8245
8246 if (current_buffer != XBUFFER (w->buffer))
8247 abort ();
8248
5f5c8ee5
GM
8249 if (!NILP (Vwindow_scroll_functions))
8250 {
8251 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8252 make_number (CHARPOS (startp)));
8253 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
8254 /* In case the hook functions switch buffers. */
8255 if (current_buffer != XBUFFER (w->buffer))
8256 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8257 }
90adcf20 8258
5f5c8ee5
GM
8259 return startp;
8260}
8261
8262
8263/* Modify the desired matrix of window W and W->vscroll so that the
8264 line containing the cursor is fully visible. */
8265
8266static void
8267make_cursor_line_fully_visible (w)
8268 struct window *w;
8269{
8270 struct glyph_matrix *matrix;
8271 struct glyph_row *row;
045dee35 8272 int header_line_height;
5f5c8ee5
GM
8273
8274 /* It's not always possible to find the cursor, e.g, when a window
8275 is full of overlay strings. Don't do anything in that case. */
8276 if (w->cursor.vpos < 0)
8277 return;
8278
8279 matrix = w->desired_matrix;
8280 row = MATRIX_ROW (matrix, w->cursor.vpos);
8281
a943a5ca
GM
8282 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row)
8283 /* The row may be partially visible at the top because we
8284 already have chosen a vscroll to align the bottom of the
8285 row with the bottom of the window. This happens for rows
8286 taller than the window. */
8287 && row->y + row->height < window_box_height (w))
5f5c8ee5
GM
8288 {
8289 int dy = row->height - row->visible_height;
8290 w->vscroll = 0;
8291 w->cursor.y += dy;
8292 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8293 }
a943a5ca
GM
8294 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)
8295 /* The row may be partially visible at the bottom because
8296 we chose a vscroll to align the row's top with the
8297 window's top. This happens for rows taller than the
8298 window. */
8299 && row->y > WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
8300 {
8301 int dy = - (row->height - row->visible_height);
8302 w->vscroll = dy;
8303 w->cursor.y += dy;
8304 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8305 }
8306
8307 /* When we change the cursor y-position of the selected window,
8308 change this_line_y as well so that the display optimization for
8309 the cursor line of the selected window in redisplay_internal uses
8310 the correct y-position. */
8311 if (w == XWINDOW (selected_window))
8312 this_line_y = w->cursor.y;
8313}
8314
8315
8316/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8317 non-zero means only WINDOW is redisplayed in redisplay_internal.
8318 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8319 in redisplay_window to bring a partially visible line into view in
8320 the case that only the cursor has moved.
8321
8322 Value is
8323
8324 1 if scrolling succeeded
8325
8326 0 if scrolling didn't find point.
8327
8328 -1 if new fonts have been loaded so that we must interrupt
8329 redisplay, adjust glyph matrices, and try again. */
8330
8331static int
8332try_scrolling (window, just_this_one_p, scroll_conservatively,
8333 scroll_step, temp_scroll_step)
8334 Lisp_Object window;
8335 int just_this_one_p;
8336 int scroll_conservatively, scroll_step;
8337 int temp_scroll_step;
8338{
8339 struct window *w = XWINDOW (window);
8340 struct frame *f = XFRAME (w->frame);
8341 struct text_pos scroll_margin_pos;
8342 struct text_pos pos;
8343 struct text_pos startp;
8344 struct it it;
8345 Lisp_Object window_end;
8346 int this_scroll_margin;
8347 int dy = 0;
8348 int scroll_max;
8349 int line_height, rc;
8350 int amount_to_scroll = 0;
8351 Lisp_Object aggressive;
8352 int height;
8353
8354#if GLYPH_DEBUG
8355 debug_method_add (w, "try_scrolling");
78614721 8356#endif
5f5c8ee5
GM
8357
8358 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8359
8360 /* Compute scroll margin height in pixels. We scroll when point is
8361 within this distance from the top or bottom of the window. */
8362 if (scroll_margin > 0)
90adcf20 8363 {
5f5c8ee5
GM
8364 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8365 this_scroll_margin *= CANON_Y_UNIT (f);
8366 }
8367 else
8368 this_scroll_margin = 0;
8369
8370 /* Compute how much we should try to scroll maximally to bring point
8371 into view. */
8372 if (scroll_step)
8373 scroll_max = scroll_step;
8374 else if (scroll_conservatively)
8375 scroll_max = scroll_conservatively;
8376 else if (temp_scroll_step)
8377 scroll_max = temp_scroll_step;
8378 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8379 || NUMBERP (current_buffer->scroll_up_aggressively))
8380 /* We're trying to scroll because of aggressive scrolling
8381 but no scroll_step is set. Choose an arbitrary one. Maybe
8382 there should be a variable for this. */
8383 scroll_max = 10;
8384 else
8385 scroll_max = 0;
8386 scroll_max *= CANON_Y_UNIT (f);
8387
8388 /* Decide whether we have to scroll down. Start at the window end
8389 and move this_scroll_margin up to find the position of the scroll
8390 margin. */
8391 window_end = Fwindow_end (window, Qt);
8392 CHARPOS (scroll_margin_pos) = XINT (window_end);
8393 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8394 if (this_scroll_margin)
8395 {
8396 start_display (&it, w, scroll_margin_pos);
8397 move_it_vertically (&it, - this_scroll_margin);
8398 scroll_margin_pos = it.current.pos;
8399 }
8400
8401 if (PT >= CHARPOS (scroll_margin_pos))
8402 {
8403 int y0;
8404
8405 /* Point is in the scroll margin at the bottom of the window, or
8406 below. Compute a new window start that makes point visible. */
8407
8408 /* Compute the distance from the scroll margin to PT.
8409 Give up if the distance is greater than scroll_max. */
8410 start_display (&it, w, scroll_margin_pos);
8411 y0 = it.current_y;
8412 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8413 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8414 line_height = (it.max_ascent + it.max_descent
8415 ? it.max_ascent + it.max_descent
8416 : last_height);
8417 dy = it.current_y + line_height - y0;
8418 if (dy > scroll_max)
8419 return 0;
8420
8421 /* Move the window start down. If scrolling conservatively,
8422 move it just enough down to make point visible. If
8423 scroll_step is set, move it down by scroll_step. */
8424 start_display (&it, w, startp);
8425
8426 if (scroll_conservatively)
8427 amount_to_scroll = dy;
8428 else if (scroll_step || temp_scroll_step)
8429 amount_to_scroll = scroll_max;
8430 else
90adcf20 8431 {
5f5c8ee5
GM
8432 aggressive = current_buffer->scroll_down_aggressively;
8433 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8434 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8435 if (NUMBERP (aggressive))
8436 amount_to_scroll = XFLOATINT (aggressive) * height;
8437 }
a2725ab2 8438
5f5c8ee5
GM
8439 if (amount_to_scroll <= 0)
8440 return 0;
a2725ab2 8441
5f5c8ee5
GM
8442 move_it_vertically (&it, amount_to_scroll);
8443 startp = it.current.pos;
8444 }
8445 else
8446 {
8447 /* See if point is inside the scroll margin at the top of the
8448 window. */
8449 scroll_margin_pos = startp;
8450 if (this_scroll_margin)
8451 {
8452 start_display (&it, w, startp);
8453 move_it_vertically (&it, this_scroll_margin);
8454 scroll_margin_pos = it.current.pos;
8455 }
8456
8457 if (PT < CHARPOS (scroll_margin_pos))
8458 {
8459 /* Point is in the scroll margin at the top of the window or
8460 above what is displayed in the window. */
8461 int y0;
8462
8463 /* Compute the vertical distance from PT to the scroll
8464 margin position. Give up if distance is greater than
8465 scroll_max. */
8466 SET_TEXT_POS (pos, PT, PT_BYTE);
8467 start_display (&it, w, pos);
8468 y0 = it.current_y;
8469 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8470 it.last_visible_y, -1,
8471 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8472 dy = it.current_y - y0;
8473 if (dy > scroll_max)
8474 return 0;
8475
8476 /* Compute new window start. */
8477 start_display (&it, w, startp);
8478
8479 if (scroll_conservatively)
8480 amount_to_scroll = dy;
8481 else if (scroll_step || temp_scroll_step)
8482 amount_to_scroll = scroll_max;
538f13d4 8483 else
5f5c8ee5
GM
8484 {
8485 aggressive = current_buffer->scroll_up_aggressively;
8486 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8487 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8488 if (NUMBERP (aggressive))
8489 amount_to_scroll = XFLOATINT (aggressive) * height;
8490 }
a2725ab2 8491
5f5c8ee5
GM
8492 if (amount_to_scroll <= 0)
8493 return 0;
8494
8495 move_it_vertically (&it, - amount_to_scroll);
8496 startp = it.current.pos;
90adcf20
RS
8497 }
8498 }
a2889657 8499
5f5c8ee5
GM
8500 /* Run window scroll functions. */
8501 startp = run_window_scroll_functions (window, startp);
90adcf20 8502
5f5c8ee5
GM
8503 /* Display the window. Give up if new fonts are loaded, or if point
8504 doesn't appear. */
8505 if (!try_window (window, startp))
8506 rc = -1;
8507 else if (w->cursor.vpos < 0)
8508 {
8509 clear_glyph_matrix (w->desired_matrix);
8510 rc = 0;
8511 }
8512 else
8513 {
8514 /* Maybe forget recorded base line for line number display. */
8515 if (!just_this_one_p
8516 || current_buffer->clip_changed
9142dd5b 8517 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
8518 w->base_line_number = Qnil;
8519
8520 /* If cursor ends up on a partially visible line, shift display
8521 lines up or down. */
8522 make_cursor_line_fully_visible (w);
8523 rc = 1;
8524 }
8525
8526 return rc;
a2889657
JB
8527}
8528
5f5c8ee5
GM
8529
8530/* Compute a suitable window start for window W if display of W starts
8531 on a continuation line. Value is non-zero if a new window start
8532 was computed.
8533
8534 The new window start will be computed, based on W's width, starting
8535 from the start of the continued line. It is the start of the
8536 screen line with the minimum distance from the old start W->start. */
8537
8538static int
8539compute_window_start_on_continuation_line (w)
8540 struct window *w;
1f1ff51d 8541{
5f5c8ee5
GM
8542 struct text_pos pos, start_pos;
8543 int window_start_changed_p = 0;
1f1ff51d 8544
5f5c8ee5 8545 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 8546
5f5c8ee5
GM
8547 /* If window start is on a continuation line... Window start may be
8548 < BEGV in case there's invisible text at the start of the
8549 buffer (M-x rmail, for example). */
8550 if (CHARPOS (start_pos) > BEGV
8551 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 8552 {
5f5c8ee5
GM
8553 struct it it;
8554 struct glyph_row *row;
f3751a65
GM
8555
8556 /* Handle the case that the window start is out of range. */
8557 if (CHARPOS (start_pos) < BEGV)
8558 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8559 else if (CHARPOS (start_pos) > ZV)
8560 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
8561
8562 /* Find the start of the continued line. This should be fast
8563 because scan_buffer is fast (newline cache). */
045dee35 8564 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
8565 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8566 row, DEFAULT_FACE_ID);
8567 reseat_at_previous_visible_line_start (&it);
8568
8569 /* If the line start is "too far" away from the window start,
8570 say it takes too much time to compute a new window start. */
8571 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8572 < XFASTINT (w->height) * XFASTINT (w->width))
8573 {
8574 int min_distance, distance;
8575
8576 /* Move forward by display lines to find the new window
8577 start. If window width was enlarged, the new start can
8578 be expected to be > the old start. If window width was
8579 decreased, the new window start will be < the old start.
8580 So, we're looking for the display line start with the
8581 minimum distance from the old window start. */
8582 pos = it.current.pos;
8583 min_distance = INFINITY;
8584 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8585 distance < min_distance)
8586 {
8587 min_distance = distance;
8588 pos = it.current.pos;
8589 move_it_by_lines (&it, 1, 0);
8590 }
8591
8592 /* Set the window start there. */
8593 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8594 window_start_changed_p = 1;
8595 }
1f1ff51d 8596 }
5f5c8ee5
GM
8597
8598 return window_start_changed_p;
1f1ff51d
KH
8599}
8600
5f5c8ee5
GM
8601
8602/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8603 selected_window is redisplayed. */
90adcf20 8604
a2889657 8605static void
5f5c8ee5 8606redisplay_window (window, just_this_one_p)
a2889657 8607 Lisp_Object window;
5f5c8ee5 8608 int just_this_one_p;
a2889657 8609{
5f5c8ee5
GM
8610 struct window *w = XWINDOW (window);
8611 struct frame *f = XFRAME (w->frame);
8612 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 8613 struct buffer *old = current_buffer;
5f5c8ee5 8614 struct text_pos lpoint, opoint, startp;
e481f960 8615 int update_mode_line;
5f5c8ee5
GM
8616 int tem;
8617 struct it it;
8618 /* Record it now because it's overwritten. */
8619 int current_matrix_up_to_date_p = 0;
5f5c8ee5 8620 int temp_scroll_step = 0;
2e54982e 8621 int count = specpdl_ptr - specpdl;
a2889657 8622
5f5c8ee5
GM
8623 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8624 opoint = lpoint;
a2889657 8625
5f5c8ee5
GM
8626 /* W must be a leaf window here. */
8627 xassert (!NILP (w->buffer));
8628#if GLYPH_DEBUG
8629 *w->desired_matrix->method = 0;
8630#endif
2e54982e
RS
8631
8632 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
8633
8634 reconsider_clip_changes (w, buffer);
8635
5f5c8ee5
GM
8636 /* Has the mode line to be updated? */
8637 update_mode_line = (!NILP (w->update_mode_line)
8638 || update_mode_lines
8639 || buffer->clip_changed);
8de2d90b
JB
8640
8641 if (MINI_WINDOW_P (w))
8642 {
5f5c8ee5 8643 if (w == XWINDOW (echo_area_window)
c6e89d6c 8644 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
8645 {
8646 if (update_mode_line)
8647 /* We may have to update a tty frame's menu bar or a
e037b9ec 8648 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
8649 goto finish_menu_bars;
8650 else
8651 /* We've already displayed the echo area glyphs in this window. */
8652 goto finish_scroll_bars;
8653 }
73af359d 8654 else if (w != XWINDOW (minibuf_window))
8de2d90b 8655 {
5f5c8ee5
GM
8656 /* W is a mini-buffer window, but it's not the currently
8657 active one, so clear it. */
8658 int yb = window_text_bottom_y (w);
8659 struct glyph_row *row;
8660 int y;
8661
8662 for (y = 0, row = w->desired_matrix->rows;
8663 y < yb;
8664 y += row->height, ++row)
8665 blank_row (w, row, y);
88f22aff 8666 goto finish_scroll_bars;
8de2d90b
JB
8667 }
8668 }
a2889657 8669
5f5c8ee5
GM
8670 /* Otherwise set up data on this window; select its buffer and point
8671 value. */
6a93695f
GM
8672 /* Really select the buffer, for the sake of buffer-local
8673 variables. */
8674 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
8675 SET_TEXT_POS (opoint, PT, PT_BYTE);
8676
8677 current_matrix_up_to_date_p
8678 = (!NILP (w->window_end_valid)
8679 && !current_buffer->clip_changed
8680 && XFASTINT (w->last_modified) >= MODIFF
8681 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 8682
5f5c8ee5
GM
8683 /* When windows_or_buffers_changed is non-zero, we can't rely on
8684 the window end being valid, so set it to nil there. */
8685 if (windows_or_buffers_changed)
8686 {
8687 /* If window starts on a continuation line, maybe adjust the
8688 window start in case the window's width changed. */
8689 if (XMARKER (w->start)->buffer == current_buffer)
8690 compute_window_start_on_continuation_line (w);
8691
8692 w->window_end_valid = Qnil;
8693 }
12adba34 8694
5f5c8ee5
GM
8695 /* Some sanity checks. */
8696 CHECK_WINDOW_END (w);
8697 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 8698 abort ();
5f5c8ee5 8699 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 8700 abort ();
a2889657 8701
28995e67
RS
8702 /* If %c is in mode line, update it if needed. */
8703 if (!NILP (w->column_number_displayed)
8704 /* This alternative quickly identifies a common case
8705 where no change is needed. */
8706 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8707 && XFASTINT (w->last_modified) >= MODIFF
8708 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8709 && XFASTINT (w->column_number_displayed) != current_column ())
8710 update_mode_line = 1;
8711
5f5c8ee5
GM
8712 /* Count number of windows showing the selected buffer. An indirect
8713 buffer counts as its base buffer. */
8714 if (!just_this_one_p)
42640f83
RS
8715 {
8716 struct buffer *current_base, *window_base;
8717 current_base = current_buffer;
8718 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8719 if (current_base->base_buffer)
8720 current_base = current_base->base_buffer;
8721 if (window_base->base_buffer)
8722 window_base = window_base->base_buffer;
8723 if (current_base == window_base)
8724 buffer_shared++;
8725 }
a2889657 8726
5f5c8ee5
GM
8727 /* Point refers normally to the selected window. For any other
8728 window, set up appropriate value. */
a2889657
JB
8729 if (!EQ (window, selected_window))
8730 {
12adba34
RS
8731 int new_pt = XMARKER (w->pointm)->charpos;
8732 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 8733 if (new_pt < BEGV)
a2889657 8734 {
f67a0f51 8735 new_pt = BEGV;
12adba34
RS
8736 new_pt_byte = BEGV_BYTE;
8737 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 8738 }
f67a0f51 8739 else if (new_pt > (ZV - 1))
a2889657 8740 {
f67a0f51 8741 new_pt = ZV;
12adba34
RS
8742 new_pt_byte = ZV_BYTE;
8743 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 8744 }
5f5c8ee5 8745
f67a0f51 8746 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 8747 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
8748 }
8749
f4faa47c 8750 /* If any of the character widths specified in the display table
5f5c8ee5
GM
8751 have changed, invalidate the width run cache. It's true that
8752 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
8753 redisplay goes (non-fatally) haywire when the display table is
8754 changed, so why should we worry about doing any better? */
8755 if (current_buffer->width_run_cache)
8756 {
f908610f 8757 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
8758
8759 if (! disptab_matches_widthtab (disptab,
8760 XVECTOR (current_buffer->width_table)))
8761 {
8762 invalidate_region_cache (current_buffer,
8763 current_buffer->width_run_cache,
8764 BEG, Z);
8765 recompute_width_table (current_buffer, disptab);
8766 }
8767 }
8768
a2889657 8769 /* If window-start is screwed up, choose a new one. */
a2889657
JB
8770 if (XMARKER (w->start)->buffer != current_buffer)
8771 goto recenter;
8772
5f5c8ee5 8773 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 8774
cf0df6ab
RS
8775 /* If someone specified a new starting point but did not insist,
8776 check whether it can be used. */
cfad01b4
GM
8777 if (!NILP (w->optional_new_start)
8778 && CHARPOS (startp) >= BEGV
8779 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
8780 {
8781 w->optional_new_start = Qnil;
5f5c8ee5
GM
8782 /* This takes a mini-buffer prompt into account. */
8783 start_display (&it, w, startp);
8784 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8785 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8786 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
8787 w->force_start = Qt;
8788 }
8789
8de2d90b 8790 /* Handle case where place to start displaying has been specified,
aa6d10fa 8791 unless the specified location is outside the accessible range. */
9472f927
GM
8792 if (!NILP (w->force_start)
8793 || w->frozen_window_start_p)
a2889657 8794 {
e63574d7 8795 w->force_start = Qnil;
5f5c8ee5 8796 w->vscroll = 0;
b5174a51 8797 w->window_end_valid = Qnil;
5f5c8ee5
GM
8798
8799 /* Forget any recorded base line for line number display. */
8800 if (!current_matrix_up_to_date_p
8801 || current_buffer->clip_changed)
8802 w->base_line_number = Qnil;
8803
75c43375
RS
8804 /* Redisplay the mode line. Select the buffer properly for that.
8805 Also, run the hook window-scroll-functions
8806 because we have scrolled. */
e63574d7
RS
8807 /* Note, we do this after clearing force_start because
8808 if there's an error, it is better to forget about force_start
8809 than to get into an infinite loop calling the hook functions
8810 and having them get more errors. */
75c43375
RS
8811 if (!update_mode_line
8812 || ! NILP (Vwindow_scroll_functions))
e481f960 8813 {
e481f960
RS
8814 update_mode_line = 1;
8815 w->update_mode_line = Qt;
5f5c8ee5 8816 startp = run_window_scroll_functions (window, startp);
e481f960 8817 }
5f5c8ee5 8818
c2213350 8819 XSETFASTINT (w->last_modified, 0);
8850a573 8820 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
8821 if (CHARPOS (startp) < BEGV)
8822 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8823 else if (CHARPOS (startp) > ZV)
8824 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8825
8826 /* Redisplay, then check if cursor has been set during the
8827 redisplay. Give up if new fonts were loaded. */
8828 if (!try_window (window, startp))
8829 {
8830 w->force_start = Qt;
8831 clear_glyph_matrix (w->desired_matrix);
8832 goto restore_buffers;
8833 }
8834
9472f927 8835 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5
GM
8836 {
8837 /* If point does not appear, or on a line that is not fully
8838 visible, move point so it does appear. The desired
8839 matrix has been built above, so we can use it. */
8840 int height = window_box_height (w) / 2;
8841 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8842
8843 while (row->y < height)
8844 ++row;
8845
8846 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8847 MATRIX_ROW_START_BYTEPOS (row));
8848
90adcf20 8849 if (w != XWINDOW (selected_window))
12adba34 8850 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
8851 else if (current_buffer == old)
8852 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8853
8854 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8855
8856 /* If we are highlighting the region, then we just changed
8857 the region, so redisplay to show it. */
df0b5ea1
RS
8858 if (!NILP (Vtransient_mark_mode)
8859 && !NILP (current_buffer->mark_active))
6f27fa9b 8860 {
5f5c8ee5
GM
8861 clear_glyph_matrix (w->desired_matrix);
8862 if (!try_window (window, startp))
8863 goto restore_buffers;
6f27fa9b 8864 }
a2889657 8865 }
5f5c8ee5
GM
8866
8867 make_cursor_line_fully_visible (w);
8868#if GLYPH_DEBUG
8869 debug_method_add (w, "forced window start");
8870#endif
a2889657
JB
8871 goto done;
8872 }
8873
5f5c8ee5
GM
8874 /* Handle case where text has not changed, only point, and it has
8875 not moved off the frame. */
8876 if (current_matrix_up_to_date_p
8877 /* Point may be in this window. */
8878 && PT >= CHARPOS (startp)
8879 /* If we don't check this, we are called to move the cursor in a
8880 horizontally split window with a current matrix that doesn't
8881 fit the display. */
8882 && !windows_or_buffers_changed
8883 /* Selective display hasn't changed. */
8884 && !current_buffer->clip_changed
b1aa6cb3
RS
8885 /* If force-mode-line-update was called, really redisplay;
8886 that's how redisplay is forced after e.g. changing
8887 buffer-invisibility-spec. */
632ab665 8888 && NILP (w->update_mode_line)
5f5c8ee5
GM
8889 /* Can't use this case if highlighting a region. When a
8890 region exists, cursor movement has to do more than just
8891 set the cursor. */
8892 && !(!NILP (Vtransient_mark_mode)
8893 && !NILP (current_buffer->mark_active))
bd66d1ba 8894 && NILP (w->region_showing)
8f897821 8895 && NILP (Vshow_trailing_whitespace)
5f5c8ee5
GM
8896 /* Right after splitting windows, last_point may be nil. */
8897 && INTEGERP (w->last_point)
8898 /* This code is not used for mini-buffer for the sake of the case
8899 of redisplaying to replace an echo area message; since in
8900 that case the mini-buffer contents per se are usually
8901 unchanged. This code is of no real use in the mini-buffer
8902 since the handling of this_line_start_pos, etc., in redisplay
8903 handles the same cases. */
d45de95b 8904 && !EQ (window, minibuf_window)
5f5c8ee5
GM
8905 /* When splitting windows or for new windows, it happens that
8906 redisplay is called with a nil window_end_vpos or one being
8907 larger than the window. This should really be fixed in
8908 window.c. I don't have this on my list, now, so we do
8909 approximately the same as the old redisplay code. --gerd. */
8910 && INTEGERP (w->window_end_vpos)
8911 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8912 && (FRAME_WINDOW_P (f)
8913 || !MARKERP (Voverlay_arrow_position)
377dbd97 8914 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 8915 {
5f5c8ee5
GM
8916 int this_scroll_margin;
8917 struct glyph_row *row;
8918 int scroll_p;
a2889657 8919
5f5c8ee5
GM
8920#if GLYPH_DEBUG
8921 debug_method_add (w, "cursor movement");
8922#endif
9afd2168 8923
5f5c8ee5
GM
8924 /* Scroll if point within this distance from the top or bottom
8925 of the window. This is a pixel value. */
8926 this_scroll_margin = max (0, scroll_margin);
8927 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8928 this_scroll_margin *= CANON_Y_UNIT (f);
8929
8930 /* Start with the row the cursor was displayed during the last
8931 not paused redisplay. Give up if that row is not valid. */
8932 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8933 goto try_to_scroll;
8934 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8935 if (row->mode_line_p)
8936 ++row;
8937 if (!row->enabled_p)
8938 goto try_to_scroll;
8939
8940 scroll_p = 0;
8941 if (PT > XFASTINT (w->last_point))
8942 {
8943 /* Point has moved forward. */
8944 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8945
91004049 8946 while (MATRIX_ROW_END_CHARPOS (row) < PT
5f5c8ee5
GM
8947 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8948 {
8949 xassert (row->enabled_p);
8950 ++row;
8951 }
9afd2168 8952
91004049
GM
8953 /* The end position of a row equals the start position of
8954 the next row. If PT is there, we would rather display it
8955 in the next line. Exceptions are when the row ends in
8956 the middle of a character, or ends in ZV. */
8957 if (MATRIX_ROW_BOTTOM_Y (row) < last_y
8958 && MATRIX_ROW_END_CHARPOS (row) == PT
8959 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8960 && !row->ends_at_zv_p)
8961 {
8962 xassert (row->enabled_p);
8963 ++row;
8964 }
8965
5f5c8ee5
GM
8966 /* If within the scroll margin, scroll. Note that
8967 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8968 next line would be drawn, and that this_scroll_margin can
8969 be zero. */
8970 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8971 || PT > MATRIX_ROW_END_CHARPOS (row)
8972 /* Line is completely visible last line in window and PT
8973 is to be set in the next line. */
8974 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8975 && PT == MATRIX_ROW_END_CHARPOS (row)
8976 && !row->ends_at_zv_p
8977 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8978 scroll_p = 1;
8979 }
8980 else if (PT < XFASTINT (w->last_point))
a2889657 8981 {
5f5c8ee5
GM
8982 /* Cursor has to be moved backward. Note that PT >=
8983 CHARPOS (startp) because of the outer if-statement. */
8984 while (!row->mode_line_p
8985 && (MATRIX_ROW_START_CHARPOS (row) > PT
8986 || (MATRIX_ROW_START_CHARPOS (row) == PT
8987 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8988 && (row->y > this_scroll_margin
8989 || CHARPOS (startp) == BEGV))
a2889657 8990 {
5f5c8ee5
GM
8991 xassert (row->enabled_p);
8992 --row;
a2889657 8993 }
abb4c08f 8994
5f5c8ee5
GM
8995 /* Consider the following case: Window starts at BEGV, there
8996 is invisible, intangible text at BEGV, so that display
8997 starts at some point START > BEGV. It can happen that
8998 we are called with PT somewhere between BEGV and START.
8999 Try to handle that case. */
9000 if (row < w->current_matrix->rows
9001 || row->mode_line_p)
9002 {
9003 row = w->current_matrix->rows;
9004 if (row->mode_line_p)
9005 ++row;
9006 }
9007
9008 /* Due to newlines in overlay strings, we may have to skip
9009 forward over overlay strings. */
9010 while (MATRIX_ROW_END_CHARPOS (row) == PT
9011 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
9012 && !row->ends_at_zv_p)
9013 ++row;
9014
9015 /* If within the scroll margin, scroll. */
9016 if (row->y < this_scroll_margin
9017 && CHARPOS (startp) != BEGV)
9018 scroll_p = 1;
9019 }
9020
9021 /* if PT is not in the glyph row, give up. */
9022 if (PT < MATRIX_ROW_START_CHARPOS (row)
9023 || PT > MATRIX_ROW_END_CHARPOS (row))
9024 goto try_to_scroll;
9025
9026 /* If we end up in a partially visible line, let's make it fully
9027 visible. This can be done most easily by using the existing
9028 scrolling code. */
9029 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9030 {
9031 temp_scroll_step = 1;
9032 goto try_to_scroll;
a2889657 9033 }
5f5c8ee5
GM
9034 else if (scroll_p)
9035 goto try_to_scroll;
9036
9037 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9038 goto done;
a2889657 9039 }
5f5c8ee5 9040
a2889657
JB
9041 /* If current starting point was originally the beginning of a line
9042 but no longer is, find a new starting point. */
265a9e55 9043 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
9044 && !(CHARPOS (startp) <= BEGV
9045 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 9046 {
5f5c8ee5
GM
9047#if GLYPH_DEBUG
9048 debug_method_add (w, "recenter 1");
9049#endif
a2889657
JB
9050 goto recenter;
9051 }
5f5c8ee5
GM
9052
9053 /* Try scrolling with try_window_id. */
9142dd5b
GM
9054 else if (/* Windows and buffers haven't changed. */
9055 !windows_or_buffers_changed
5f5c8ee5
GM
9056 /* Window must be either use window-based redisplay or
9057 be full width. */
9058 && (FRAME_WINDOW_P (f)
c59c668a 9059 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
9060 && !MINI_WINDOW_P (w)
9061 /* Point is not known NOT to appear in window. */
9062 && PT >= CHARPOS (startp)
a2889657 9063 && XFASTINT (w->last_modified)
5f5c8ee5
GM
9064 /* Window is not hscrolled. */
9065 && XFASTINT (w->hscroll) == 0
9066 /* Selective display has not changed. */
9067 && !current_buffer->clip_changed
9068 /* Current matrix is up to date. */
9069 && !NILP (w->window_end_valid)
9070 /* Can't use this case if highlighting a region because
9071 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
9072 && !(!NILP (Vtransient_mark_mode)
9073 && !NILP (current_buffer->mark_active))
9074 && NILP (w->region_showing)
8f897821 9075 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 9076 /* Overlay arrow position and string not changed. */
d45de95b 9077 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 9078 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
9079 /* Value is > 0 if update has been done, it is -1 if we
9080 know that the same window start will not work. It is 0
9081 if unsuccessful for some other reason. */
9082 && (tem = try_window_id (w)) != 0)
a2889657 9083 {
5f5c8ee5
GM
9084#if GLYPH_DEBUG
9085 debug_method_add (w, "try_window_id");
9086#endif
9087
9088 if (fonts_changed_p)
9089 goto restore_buffers;
a2889657
JB
9090 if (tem > 0)
9091 goto done;
5f5c8ee5
GM
9092 /* Otherwise try_window_id has returned -1 which means that we
9093 don't want the alternative below this comment to execute. */
a2889657 9094 }
5f5c8ee5
GM
9095 else if (CHARPOS (startp) >= BEGV
9096 && CHARPOS (startp) <= ZV
9097 && PT >= CHARPOS (startp)
9098 && (CHARPOS (startp) < ZV
e9874cee 9099 /* Avoid starting at end of buffer. */
5f5c8ee5 9100 || CHARPOS (startp) == BEGV
8850a573
RS
9101 || (XFASTINT (w->last_modified) >= MODIFF
9102 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 9103 {
5f5c8ee5
GM
9104#if GLYPH_DEBUG
9105 debug_method_add (w, "same window start");
9106#endif
9107
9108 /* Try to redisplay starting at same place as before.
9109 If point has not moved off frame, accept the results. */
9110 if (!current_matrix_up_to_date_p
9111 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
9112 because a window scroll function can have changed the
9113 buffer. */
5f5c8ee5
GM
9114 || !NILP (Vwindow_scroll_functions)
9115 || MINI_WINDOW_P (w)
9116 || !try_window_reusing_current_matrix (w))
9117 {
9118 IF_DEBUG (debug_method_add (w, "1"));
9119 try_window (window, startp);
9120 }
9121
9122 if (fonts_changed_p)
9123 goto restore_buffers;
9124
9125 if (w->cursor.vpos >= 0)
aa6d10fa 9126 {
5f5c8ee5
GM
9127 if (!just_this_one_p
9128 || current_buffer->clip_changed
9142dd5b 9129 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
9130 /* Forget any recorded base line for line number display. */
9131 w->base_line_number = Qnil;
5f5c8ee5
GM
9132
9133 make_cursor_line_fully_visible (w);
aa6d10fa
RS
9134 goto done;
9135 }
a2889657 9136 else
5f5c8ee5 9137 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
9138 }
9139
5f5c8ee5
GM
9140 try_to_scroll:
9141
c2213350 9142 XSETFASTINT (w->last_modified, 0);
8850a573 9143 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 9144
e481f960
RS
9145 /* Redisplay the mode line. Select the buffer properly for that. */
9146 if (!update_mode_line)
9147 {
e481f960
RS
9148 update_mode_line = 1;
9149 w->update_mode_line = Qt;
9150 }
a2889657 9151
5f5c8ee5
GM
9152 /* Try to scroll by specified few lines. */
9153 if ((scroll_conservatively
9154 || scroll_step
9155 || temp_scroll_step
9156 || NUMBERP (current_buffer->scroll_up_aggressively)
9157 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 9158 && !current_buffer->clip_changed
5f5c8ee5
GM
9159 && CHARPOS (startp) >= BEGV
9160 && CHARPOS (startp) <= ZV)
0789adb2 9161 {
5f5c8ee5
GM
9162 /* The function returns -1 if new fonts were loaded, 1 if
9163 successful, 0 if not successful. */
9164 int rc = try_scrolling (window, just_this_one_p,
9165 scroll_conservatively,
9166 scroll_step,
9167 temp_scroll_step);
9168 if (rc > 0)
9169 goto done;
9170 else if (rc < 0)
9171 goto restore_buffers;
9172 }
f9c8af06 9173
5f5c8ee5 9174 /* Finally, just choose place to start which centers point */
5936754e 9175
5f5c8ee5 9176 recenter:
44173109 9177
5f5c8ee5
GM
9178#if GLYPH_DEBUG
9179 debug_method_add (w, "recenter");
9180#endif
0789adb2 9181
5f5c8ee5 9182 /* w->vscroll = 0; */
0789adb2 9183
5f5c8ee5
GM
9184 /* Forget any previously recorded base line for line number display. */
9185 if (!current_matrix_up_to_date_p
9186 || current_buffer->clip_changed)
9187 w->base_line_number = Qnil;
9188
9189 /* Move backward half the height of the window. */
9190 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9191 it.current_y = it.last_visible_y;
9192 move_it_vertically_backward (&it, it.last_visible_y / 2);
9193 xassert (IT_CHARPOS (it) >= BEGV);
9194
9195 /* The function move_it_vertically_backward may move over more
9196 than the specified y-distance. If it->w is small, e.g. a
9197 mini-buffer window, we may end up in front of the window's
9198 display area. Start displaying at the start of the line
9199 containing PT in this case. */
9200 if (it.current_y <= 0)
9201 {
9202 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9203 move_it_vertically (&it, 0);
9204 xassert (IT_CHARPOS (it) <= PT);
9205 it.current_y = 0;
0789adb2
RS
9206 }
9207
5f5c8ee5
GM
9208 it.current_x = it.hpos = 0;
9209
9210 /* Set startp here explicitly in case that helps avoid an infinite loop
9211 in case the window-scroll-functions functions get errors. */
9212 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9213
9214 /* Run scroll hooks. */
9215 startp = run_window_scroll_functions (window, it.current.pos);
9216
9217 /* Redisplay the window. */
9218 if (!current_matrix_up_to_date_p
9219 || windows_or_buffers_changed
9220 /* Don't use try_window_reusing_current_matrix in this case
9221 because it can have changed the buffer. */
9222 || !NILP (Vwindow_scroll_functions)
9223 || !just_this_one_p
9224 || MINI_WINDOW_P (w)
9225 || !try_window_reusing_current_matrix (w))
9226 try_window (window, startp);
9227
9228 /* If new fonts have been loaded (due to fontsets), give up. We
9229 have to start a new redisplay since we need to re-adjust glyph
9230 matrices. */
9231 if (fonts_changed_p)
9232 goto restore_buffers;
9233
9234 /* If cursor did not appear assume that the middle of the window is
9235 in the first line of the window. Do it again with the next line.
9236 (Imagine a window of height 100, displaying two lines of height
9237 60. Moving back 50 from it->last_visible_y will end in the first
9238 line.) */
9239 if (w->cursor.vpos < 0)
a2889657 9240 {
5f5c8ee5
GM
9241 if (!NILP (w->window_end_valid)
9242 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 9243 {
5f5c8ee5
GM
9244 clear_glyph_matrix (w->desired_matrix);
9245 move_it_by_lines (&it, 1, 0);
9246 try_window (window, it.current.pos);
a2889657 9247 }
5f5c8ee5 9248 else if (PT < IT_CHARPOS (it))
a2889657 9249 {
5f5c8ee5
GM
9250 clear_glyph_matrix (w->desired_matrix);
9251 move_it_by_lines (&it, -1, 0);
9252 try_window (window, it.current.pos);
9253 }
9254 else
9255 {
9256 /* Not much we can do about it. */
a2889657 9257 }
a2889657 9258 }
010494d0 9259
5f5c8ee5
GM
9260 /* Consider the following case: Window starts at BEGV, there is
9261 invisible, intangible text at BEGV, so that display starts at
9262 some point START > BEGV. It can happen that we are called with
9263 PT somewhere between BEGV and START. Try to handle that case. */
9264 if (w->cursor.vpos < 0)
835766b6 9265 {
5f5c8ee5
GM
9266 struct glyph_row *row = w->current_matrix->rows;
9267 if (row->mode_line_p)
9268 ++row;
9269 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 9270 }
5f5c8ee5
GM
9271
9272 make_cursor_line_fully_visible (w);
b5174a51 9273
74d481ac
GM
9274 done:
9275
5f5c8ee5
GM
9276 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9277 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9278 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9279 ? Qt : Qnil);
a2889657 9280
5f5c8ee5 9281 /* Display the mode line, if we must. */
e481f960 9282 if ((update_mode_line
aa6d10fa 9283 /* If window not full width, must redo its mode line
5f5c8ee5
GM
9284 if (a) the window to its side is being redone and
9285 (b) we do a frame-based redisplay. This is a consequence
9286 of how inverted lines are drawn in frame-based redisplay. */
9287 || (!just_this_one_p
9288 && !FRAME_WINDOW_P (f)
9289 && !WINDOW_FULL_WIDTH_P (w))
9290 /* Line number to display. */
155ef550 9291 || INTEGERP (w->base_line_pos)
5f5c8ee5 9292 /* Column number is displayed and different from the one displayed. */
155ef550
KH
9293 || (!NILP (w->column_number_displayed)
9294 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
9295 /* This means that the window has a mode line. */
9296 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 9297 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 9298 {
5da11938
GM
9299 Lisp_Object old_selected_frame;
9300
9301 old_selected_frame = selected_frame;
9302
5da11938 9303 XSETFRAME (selected_frame, f);
5f5c8ee5 9304 display_mode_lines (w);
5da11938 9305 selected_frame = old_selected_frame;
5f5c8ee5
GM
9306
9307 /* If mode line height has changed, arrange for a thorough
9308 immediate redisplay using the correct mode line height. */
9309 if (WINDOW_WANTS_MODELINE_P (w)
9310 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 9311 {
5f5c8ee5
GM
9312 fonts_changed_p = 1;
9313 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9314 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 9315 }
5f5c8ee5
GM
9316
9317 /* If top line height has changed, arrange for a thorough
9318 immediate redisplay using the correct mode line height. */
045dee35
GM
9319 if (WINDOW_WANTS_HEADER_LINE_P (w)
9320 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
9321 {
9322 fonts_changed_p = 1;
045dee35
GM
9323 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9324 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9325 }
9326
9327 if (fonts_changed_p)
9328 goto restore_buffers;
5ba50c51 9329 }
5f5c8ee5
GM
9330
9331 if (!line_number_displayed
9332 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
9333 {
9334 w->base_line_pos = Qnil;
9335 w->base_line_number = Qnil;
9336 }
a2889657 9337
5f5c8ee5
GM
9338 finish_menu_bars:
9339
7ce2c095 9340 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 9341 if (update_mode_line
5f5c8ee5
GM
9342 && EQ (FRAME_SELECTED_WINDOW (f), window))
9343 {
9344 int redisplay_menu_p = 0;
9345
9346 if (FRAME_WINDOW_P (f))
9347 {
dc937613 9348#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 9349 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 9350#else
5f5c8ee5 9351 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 9352#endif
5f5c8ee5
GM
9353 }
9354 else
9355 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9356
9357 if (redisplay_menu_p)
9358 display_menu_bar (w);
9359
9360#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
9361 if (WINDOWP (f->tool_bar_window)
9362 && (FRAME_TOOL_BAR_LINES (f) > 0
9363 || auto_resize_tool_bars_p))
9364 redisplay_tool_bar (f);
5f5c8ee5
GM
9365#endif
9366 }
7ce2c095 9367
88f22aff 9368 finish_scroll_bars:
5f5c8ee5 9369
88f22aff 9370 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 9371 {
b1d1124b 9372 int start, end, whole;
30c566e4 9373
b1d1124b 9374 /* Calculate the start and end positions for the current window.
3505ea70
JB
9375 At some point, it would be nice to choose between scrollbars
9376 which reflect the whole buffer size, with special markers
9377 indicating narrowing, and scrollbars which reflect only the
9378 visible region.
9379
5f5c8ee5 9380 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 9381 if (!MINI_WINDOW_P (w)
5f5c8ee5 9382 || (w == XWINDOW (minibuf_window)
c6e89d6c 9383 && NILP (echo_area_buffer[0])))
b1d1124b 9384 {
8a9311d7 9385 whole = ZV - BEGV;
4d641a15 9386 start = marker_position (w->start) - BEGV;
b1d1124b
JB
9387 /* I don't think this is guaranteed to be right. For the
9388 moment, we'll pretend it is. */
5f5c8ee5 9389 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 9390
5f5c8ee5
GM
9391 if (end < start)
9392 end = start;
9393 if (whole < (end - start))
9394 whole = end - start;
b1d1124b
JB
9395 }
9396 else
9397 start = end = whole = 0;
30c566e4 9398
88f22aff 9399 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 9400 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 9401
5f5c8ee5
GM
9402 /* Note that we actually used the scroll bar attached to this
9403 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 9404 (*redeem_scroll_bar_hook) (w);
30c566e4 9405 }
b1d1124b 9406
5f5c8ee5
GM
9407 restore_buffers:
9408
9409 /* Restore current_buffer and value of point in it. */
9410 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 9411 set_buffer_internal_1 (old);
5f5c8ee5 9412 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
9413
9414 unbind_to (count, Qnil);
a2889657 9415}
a2889657 9416
5f5c8ee5
GM
9417
9418/* Build the complete desired matrix of WINDOW with a window start
9419 buffer position POS. Value is non-zero if successful. It is zero
9420 if fonts were loaded during redisplay which makes re-adjusting
9421 glyph matrices necessary. */
9422
9423int
a2889657
JB
9424try_window (window, pos)
9425 Lisp_Object window;
5f5c8ee5
GM
9426 struct text_pos pos;
9427{
9428 struct window *w = XWINDOW (window);
9429 struct it it;
9430 struct glyph_row *last_text_row = NULL;
9cbab4ff 9431
5f5c8ee5
GM
9432 /* Make POS the new window start. */
9433 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 9434
5f5c8ee5
GM
9435 /* Mark cursor position as unknown. No overlay arrow seen. */
9436 w->cursor.vpos = -1;
a2889657 9437 overlay_arrow_seen = 0;
642eefc6 9438
5f5c8ee5
GM
9439 /* Initialize iterator and info to start at POS. */
9440 start_display (&it, w, pos);
a2889657 9441
5f5c8ee5
GM
9442 /* Display all lines of W. */
9443 while (it.current_y < it.last_visible_y)
9444 {
9445 if (display_line (&it))
9446 last_text_row = it.glyph_row - 1;
9447 if (fonts_changed_p)
9448 return 0;
9449 }
a2889657 9450
5f5c8ee5
GM
9451 /* If bottom moved off end of frame, change mode line percentage. */
9452 if (XFASTINT (w->window_end_pos) <= 0
9453 && Z != IT_CHARPOS (it))
a2889657
JB
9454 w->update_mode_line = Qt;
9455
5f5c8ee5
GM
9456 /* Set window_end_pos to the offset of the last character displayed
9457 on the window from the end of current_buffer. Set
9458 window_end_vpos to its row number. */
9459 if (last_text_row)
9460 {
9461 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9462 w->window_end_bytepos
9463 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9464 XSETFASTINT (w->window_end_pos,
9465 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9466 XSETFASTINT (w->window_end_vpos,
9467 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9468 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9469 ->displays_text_p);
9470 }
9471 else
9472 {
9473 w->window_end_bytepos = 0;
9474 XSETFASTINT (w->window_end_pos, 0);
9475 XSETFASTINT (w->window_end_vpos, 0);
9476 }
9477
a2889657
JB
9478 /* But that is not valid info until redisplay finishes. */
9479 w->window_end_valid = Qnil;
5f5c8ee5 9480 return 1;
a2889657 9481}
5f5c8ee5
GM
9482
9483
a2889657 9484\f
5f5c8ee5
GM
9485/************************************************************************
9486 Window redisplay reusing current matrix when buffer has not changed
9487 ************************************************************************/
9488
9489/* Try redisplay of window W showing an unchanged buffer with a
9490 different window start than the last time it was displayed by
9491 reusing its current matrix. Value is non-zero if successful.
9492 W->start is the new window start. */
a2889657
JB
9493
9494static int
5f5c8ee5
GM
9495try_window_reusing_current_matrix (w)
9496 struct window *w;
a2889657 9497{
5f5c8ee5
GM
9498 struct frame *f = XFRAME (w->frame);
9499 struct glyph_row *row, *bottom_row;
9500 struct it it;
9501 struct run run;
9502 struct text_pos start, new_start;
9503 int nrows_scrolled, i;
9504 struct glyph_row *last_text_row;
9505 struct glyph_row *last_reused_text_row;
9506 struct glyph_row *start_row;
9507 int start_vpos, min_y, max_y;
9508
9509 /* Right now this function doesn't handle terminal frames. */
9510 if (!FRAME_WINDOW_P (f))
9511 return 0;
a2889657 9512
5f5c8ee5
GM
9513 /* Can't do this if region may have changed. */
9514 if ((!NILP (Vtransient_mark_mode)
9515 && !NILP (current_buffer->mark_active))
8f897821
GM
9516 || !NILP (w->region_showing)
9517 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 9518 return 0;
a2889657 9519
5f5c8ee5 9520 /* If top-line visibility has changed, give up. */
045dee35
GM
9521 if (WINDOW_WANTS_HEADER_LINE_P (w)
9522 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
9523 return 0;
9524
9525 /* Give up if old or new display is scrolled vertically. We could
9526 make this function handle this, but right now it doesn't. */
9527 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9528 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9529 return 0;
9530
9531 /* The variable new_start now holds the new window start. The old
9532 start `start' can be determined from the current matrix. */
9533 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9534 start = start_row->start.pos;
9535 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 9536
5f5c8ee5
GM
9537 /* Clear the desired matrix for the display below. */
9538 clear_glyph_matrix (w->desired_matrix);
9539
9540 if (CHARPOS (new_start) <= CHARPOS (start))
9541 {
9542 int first_row_y;
9543
9544 IF_DEBUG (debug_method_add (w, "twu1"));
9545
9546 /* Display up to a row that can be reused. The variable
9547 last_text_row is set to the last row displayed that displays
9548 text. */
9549 start_display (&it, w, new_start);
9550 first_row_y = it.current_y;
9551 w->cursor.vpos = -1;
9552 last_text_row = last_reused_text_row = NULL;
9553 while (it.current_y < it.last_visible_y
9554 && IT_CHARPOS (it) < CHARPOS (start)
9555 && !fonts_changed_p)
9556 if (display_line (&it))
9557 last_text_row = it.glyph_row - 1;
9558
9559 /* A value of current_y < last_visible_y means that we stopped
9560 at the previous window start, which in turn means that we
9561 have at least one reusable row. */
9562 if (it.current_y < it.last_visible_y)
a2889657 9563 {
5f5c8ee5
GM
9564 nrows_scrolled = it.vpos;
9565
9566 /* Find PT if not already found in the lines displayed. */
9567 if (w->cursor.vpos < 0)
a2889657 9568 {
5f5c8ee5
GM
9569 int dy = it.current_y - first_row_y;
9570
9571 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9572 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9573 {
9574 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9575 && PT < MATRIX_ROW_END_CHARPOS (row))
9576 {
9577 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9578 dy, nrows_scrolled);
9579 break;
9580 }
9581
9582 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9583 break;
9584
9585 ++row;
9586 }
9587
9588 /* Give up if point was not found. This shouldn't
9589 happen often; not more often than with try_window
9590 itself. */
9591 if (w->cursor.vpos < 0)
9592 {
9593 clear_glyph_matrix (w->desired_matrix);
9594 return 0;
9595 }
a2889657 9596 }
5f5c8ee5
GM
9597
9598 /* Scroll the display. Do it before the current matrix is
9599 changed. The problem here is that update has not yet
9600 run, i.e. part of the current matrix is not up to date.
9601 scroll_run_hook will clear the cursor, and use the
9602 current matrix to get the height of the row the cursor is
9603 in. */
9604 run.current_y = first_row_y;
9605 run.desired_y = it.current_y;
9606 run.height = it.last_visible_y - it.current_y;
15e26c76
GM
9607 if (run.height > 0
9608 && run.current_y != run.desired_y)
a2889657 9609 {
5f5c8ee5
GM
9610 update_begin (f);
9611 rif->update_window_begin_hook (w);
9612 rif->scroll_run_hook (w, &run);
9613 rif->update_window_end_hook (w, 0);
9614 update_end (f);
a2889657 9615 }
5f5c8ee5
GM
9616
9617 /* Shift current matrix down by nrows_scrolled lines. */
9618 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9619 rotate_matrix (w->current_matrix,
9620 start_vpos,
9621 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9622 nrows_scrolled);
9623
9624 /* Disable lines not reused. */
9625 for (i = 0; i < it.vpos; ++i)
9626 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9627
9628 /* Re-compute Y positions. */
9629 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
045dee35 9630 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9631 max_y = it.last_visible_y;
9632 while (row < bottom_row)
d2f84654 9633 {
5f5c8ee5
GM
9634 row->y = it.current_y;
9635
9636 if (row->y < min_y)
9637 row->visible_height = row->height - (min_y - row->y);
9638 else if (row->y + row->height > max_y)
9639 row->visible_height
9640 = row->height - (row->y + row->height - max_y);
9641 else
9642 row->visible_height = row->height;
9643
9644 it.current_y += row->height;
9645 ++it.vpos;
9646
9647 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9648 last_reused_text_row = row;
9649 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9650 break;
9651 ++row;
d2f84654 9652 }
a2889657 9653 }
5f5c8ee5
GM
9654
9655 /* Update window_end_pos etc.; last_reused_text_row is the last
9656 reused row from the current matrix containing text, if any.
9657 The value of last_text_row is the last displayed line
9658 containing text. */
9659 if (last_reused_text_row)
a2889657 9660 {
5f5c8ee5
GM
9661 w->window_end_bytepos
9662 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9663 XSETFASTINT (w->window_end_pos,
9664 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9665 XSETFASTINT (w->window_end_vpos,
9666 MATRIX_ROW_VPOS (last_reused_text_row,
9667 w->current_matrix));
a2889657 9668 }
5f5c8ee5
GM
9669 else if (last_text_row)
9670 {
9671 w->window_end_bytepos
9672 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9673 XSETFASTINT (w->window_end_pos,
9674 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9675 XSETFASTINT (w->window_end_vpos,
9676 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9677 }
9678 else
9679 {
9680 /* This window must be completely empty. */
9681 w->window_end_bytepos = 0;
9682 XSETFASTINT (w->window_end_pos, 0);
9683 XSETFASTINT (w->window_end_vpos, 0);
9684 }
9685 w->window_end_valid = Qnil;
a2889657 9686
5f5c8ee5
GM
9687 /* Update hint: don't try scrolling again in update_window. */
9688 w->desired_matrix->no_scrolling_p = 1;
9689
9690#if GLYPH_DEBUG
9691 debug_method_add (w, "try_window_reusing_current_matrix 1");
9692#endif
9693 return 1;
a2889657 9694 }
5f5c8ee5
GM
9695 else if (CHARPOS (new_start) > CHARPOS (start))
9696 {
9697 struct glyph_row *pt_row, *row;
9698 struct glyph_row *first_reusable_row;
9699 struct glyph_row *first_row_to_display;
9700 int dy;
9701 int yb = window_text_bottom_y (w);
9702
9703 IF_DEBUG (debug_method_add (w, "twu2"));
9704
9705 /* Find the row starting at new_start, if there is one. Don't
9706 reuse a partially visible line at the end. */
9707 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9708 while (first_reusable_row->enabled_p
9709 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9710 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9711 < CHARPOS (new_start)))
9712 ++first_reusable_row;
9713
9714 /* Give up if there is no row to reuse. */
9715 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
9716 || !first_reusable_row->enabled_p
9717 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9718 != CHARPOS (new_start)))
5f5c8ee5
GM
9719 return 0;
9720
5f5c8ee5
GM
9721 /* We can reuse fully visible rows beginning with
9722 first_reusable_row to the end of the window. Set
9723 first_row_to_display to the first row that cannot be reused.
9724 Set pt_row to the row containing point, if there is any. */
9725 first_row_to_display = first_reusable_row;
9726 pt_row = NULL;
9727 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9728 {
9729 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9730 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9731 pt_row = first_row_to_display;
a2889657 9732
5f5c8ee5
GM
9733 ++first_row_to_display;
9734 }
a2889657 9735
5f5c8ee5
GM
9736 /* Start displaying at the start of first_row_to_display. */
9737 xassert (first_row_to_display->y < yb);
9738 init_to_row_start (&it, w, first_row_to_display);
9739 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9740 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9741 - nrows_scrolled);
9742 it.current_y = first_row_to_display->y - first_reusable_row->y;
9743
9744 /* Display lines beginning with first_row_to_display in the
9745 desired matrix. Set last_text_row to the last row displayed
9746 that displays text. */
9747 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9748 if (pt_row == NULL)
9749 w->cursor.vpos = -1;
9750 last_text_row = NULL;
9751 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9752 if (display_line (&it))
9753 last_text_row = it.glyph_row - 1;
9754
9755 /* Give up If point isn't in a row displayed or reused. */
9756 if (w->cursor.vpos < 0)
9757 {
9758 clear_glyph_matrix (w->desired_matrix);
9759 return 0;
9760 }
12adba34 9761
5f5c8ee5
GM
9762 /* If point is in a reused row, adjust y and vpos of the cursor
9763 position. */
9764 if (pt_row)
9765 {
9766 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9767 w->current_matrix);
9768 w->cursor.y -= first_reusable_row->y;
a2889657
JB
9769 }
9770
5f5c8ee5
GM
9771 /* Scroll the display. */
9772 run.current_y = first_reusable_row->y;
045dee35 9773 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9774 run.height = it.last_visible_y - run.current_y;
9775 if (run.height)
9776 {
9777 struct frame *f = XFRAME (WINDOW_FRAME (w));
9778 update_begin (f);
9779 rif->update_window_begin_hook (w);
9780 rif->scroll_run_hook (w, &run);
9781 rif->update_window_end_hook (w, 0);
9782 update_end (f);
9783 }
a2889657 9784
5f5c8ee5
GM
9785 /* Adjust Y positions of reused rows. */
9786 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9787 row = first_reusable_row;
9788 dy = first_reusable_row->y;
045dee35 9789 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9790 max_y = it.last_visible_y;
9791 while (row < first_row_to_display)
9792 {
9793 row->y -= dy;
9794 if (row->y < min_y)
9795 row->visible_height = row->height - (min_y - row->y);
9796 else if (row->y + row->height > max_y)
9797 row->visible_height
9798 = row->height - (row->y + row->height - max_y);
9799 else
9800 row->visible_height = row->height;
9801 ++row;
9802 }
a2889657 9803
5f5c8ee5
GM
9804 /* Disable rows not reused. */
9805 while (row < bottom_row)
9806 {
9807 row->enabled_p = 0;
9808 ++row;
9809 }
9810
9811 /* Scroll the current matrix. */
9812 xassert (nrows_scrolled > 0);
9813 rotate_matrix (w->current_matrix,
9814 start_vpos,
9815 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9816 -nrows_scrolled);
9817
9818 /* Adjust window end. A null value of last_text_row means that
9819 the window end is in reused rows which in turn means that
9820 only its vpos can have changed. */
9821 if (last_text_row)
9822 {
9823 w->window_end_bytepos
9824 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9825 XSETFASTINT (w->window_end_pos,
9826 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9827 XSETFASTINT (w->window_end_vpos,
9828 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9829 }
9830 else
a2889657 9831 {
e8e536a9 9832 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 9833 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 9834 }
5f5c8ee5
GM
9835
9836 w->window_end_valid = Qnil;
9837 w->desired_matrix->no_scrolling_p = 1;
9838
9839#if GLYPH_DEBUG
9840 debug_method_add (w, "try_window_reusing_current_matrix 2");
9841#endif
9842 return 1;
a2889657 9843 }
5f5c8ee5
GM
9844
9845 return 0;
9846}
a2889657 9847
a2889657 9848
5f5c8ee5
GM
9849\f
9850/************************************************************************
9851 Window redisplay reusing current matrix when buffer has changed
9852 ************************************************************************/
9853
9854static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9855static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9856 int *, int *));
9857static struct glyph_row *
9858find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9859 struct glyph_row *));
9860
9861
9862/* Return the last row in MATRIX displaying text. If row START is
9863 non-null, start searching with that row. IT gives the dimensions
9864 of the display. Value is null if matrix is empty; otherwise it is
9865 a pointer to the row found. */
9866
9867static struct glyph_row *
9868find_last_row_displaying_text (matrix, it, start)
9869 struct glyph_matrix *matrix;
9870 struct it *it;
9871 struct glyph_row *start;
9872{
9873 struct glyph_row *row, *row_found;
9874
9875 /* Set row_found to the last row in IT->w's current matrix
9876 displaying text. The loop looks funny but think of partially
9877 visible lines. */
9878 row_found = NULL;
9879 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9880 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9881 {
9882 xassert (row->enabled_p);
9883 row_found = row;
9884 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9885 break;
9886 ++row;
a2889657 9887 }
5f5c8ee5
GM
9888
9889 return row_found;
9890}
9891
a2889657 9892
5f5c8ee5
GM
9893/* Return the last row in the current matrix of W that is not affected
9894 by changes at the start of current_buffer that occurred since the
9895 last time W was redisplayed. Value is null if no such row exists.
a2889657 9896
5f5c8ee5
GM
9897 The global variable beg_unchanged has to contain the number of
9898 bytes unchanged at the start of current_buffer. BEG +
9899 beg_unchanged is the buffer position of the first changed byte in
9900 current_buffer. Characters at positions < BEG + beg_unchanged are
9901 at the same buffer positions as they were when the current matrix
9902 was built. */
9903
9904static struct glyph_row *
9905get_last_unchanged_at_beg_row (w)
9906 struct window *w;
9907{
9142dd5b 9908 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
9909 struct glyph_row *row;
9910 struct glyph_row *row_found = NULL;
9911 int yb = window_text_bottom_y (w);
9912
9913 /* Find the last row displaying unchanged text. */
9914 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9915 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9916 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 9917 {
5f5c8ee5
GM
9918 if (/* If row ends before first_changed_pos, it is unchanged,
9919 except in some case. */
9920 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9921 /* When row ends in ZV and we write at ZV it is not
9922 unchanged. */
9923 && !row->ends_at_zv_p
9924 /* When first_changed_pos is the end of a continued line,
9925 row is not unchanged because it may be no longer
9926 continued. */
9927 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9928 && row->continued_p))
9929 row_found = row;
9930
9931 /* Stop if last visible row. */
9932 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9933 break;
9934
9935 ++row;
a2889657
JB
9936 }
9937
5f5c8ee5 9938 return row_found;
a2889657 9939}
5f5c8ee5
GM
9940
9941
9942/* Find the first glyph row in the current matrix of W that is not
9943 affected by changes at the end of current_buffer since the last
9944 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
9945 chars by which buffer positions in unchanged text at the end of
9946 current_buffer must be adjusted. Return in *DELTA_BYTES the
9947 corresponding number of bytes. Value is null if no such row
9948 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
9949
9950static struct glyph_row *
9951get_first_unchanged_at_end_row (w, delta, delta_bytes)
9952 struct window *w;
9953 int *delta, *delta_bytes;
a2889657 9954{
5f5c8ee5
GM
9955 struct glyph_row *row;
9956 struct glyph_row *row_found = NULL;
c581d710 9957
5f5c8ee5 9958 *delta = *delta_bytes = 0;
b2a76982 9959
5f5c8ee5
GM
9960 /* A value of window_end_pos >= end_unchanged means that the window
9961 end is in the range of changed text. If so, there is no
9962 unchanged row at the end of W's current matrix. */
9963 xassert (!NILP (w->window_end_valid));
9142dd5b 9964 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
9965 return NULL;
9966
9967 /* Set row to the last row in W's current matrix displaying text. */
9968 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9969
5f5c8ee5
GM
9970 /* If matrix is entirely empty, no unchanged row exists. */
9971 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9972 {
9973 /* The value of row is the last glyph row in the matrix having a
9974 meaningful buffer position in it. The end position of row
9975 corresponds to window_end_pos. This allows us to translate
9976 buffer positions in the current matrix to current buffer
9977 positions for characters not in changed text. */
9978 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9979 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9980 int last_unchanged_pos, last_unchanged_pos_old;
9981 struct glyph_row *first_text_row
9982 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9983
9984 *delta = Z - Z_old;
9985 *delta_bytes = Z_BYTE - Z_BYTE_old;
9986
9987 /* Set last_unchanged_pos to the buffer position of the last
9988 character in the buffer that has not been changed. Z is the
9989 index + 1 of the last byte in current_buffer, i.e. by
9990 subtracting end_unchanged we get the index of the last
9991 unchanged character, and we have to add BEG to get its buffer
9992 position. */
9142dd5b 9993 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
9994 last_unchanged_pos_old = last_unchanged_pos - *delta;
9995
9996 /* Search backward from ROW for a row displaying a line that
9997 starts at a minimum position >= last_unchanged_pos_old. */
9998 while (row >= first_text_row)
9999 {
10000 xassert (row->enabled_p);
10001 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
10002
10003 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10004 row_found = row;
10005 --row;
10006 }
10007 }
10008
10009 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
10010 return row_found;
c581d710
RS
10011}
10012
c581d710 10013
5f5c8ee5
GM
10014/* Make sure that glyph rows in the current matrix of window W
10015 reference the same glyph memory as corresponding rows in the
10016 frame's frame matrix. This function is called after scrolling W's
10017 current matrix on a terminal frame in try_window_id and
10018 try_window_reusing_current_matrix. */
10019
10020static void
10021sync_frame_with_window_matrix_rows (w)
10022 struct window *w;
c581d710 10023{
5f5c8ee5
GM
10024 struct frame *f = XFRAME (w->frame);
10025 struct glyph_row *window_row, *window_row_end, *frame_row;
10026
10027 /* Preconditions: W must be a leaf window and full-width. Its frame
10028 must have a frame matrix. */
10029 xassert (NILP (w->hchild) && NILP (w->vchild));
10030 xassert (WINDOW_FULL_WIDTH_P (w));
10031 xassert (!FRAME_WINDOW_P (f));
10032
10033 /* If W is a full-width window, glyph pointers in W's current matrix
10034 have, by definition, to be the same as glyph pointers in the
10035 corresponding frame matrix. */
10036 window_row = w->current_matrix->rows;
10037 window_row_end = window_row + w->current_matrix->nrows;
10038 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10039 while (window_row < window_row_end)
659a218f 10040 {
5f5c8ee5 10041 int area;
f002db93 10042
5f5c8ee5
GM
10043 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10044 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
10045
10046 /* Disable frame rows whose corresponding window rows have
10047 been disabled in try_window_id. */
10048 if (!window_row->enabled_p)
10049 frame_row->enabled_p = 0;
10050
5f5c8ee5 10051 ++window_row, ++frame_row;
659a218f 10052 }
a2889657 10053}
5f5c8ee5
GM
10054
10055
e037b9ec
GM
10056/* Find the glyph row in window W containing CHARPOS. Consider all
10057 rows between START and END (not inclusive). END null means search
10058 all rows to the end of the display area of W. Value is the row
10059 containing CHARPOS or null. */
10060
10061static struct glyph_row *
10062row_containing_pos (w, charpos, start, end)
10063 struct window *w;
10064 int charpos;
10065 struct glyph_row *start, *end;
10066{
10067 struct glyph_row *row = start;
10068 int last_y;
10069
10070 /* If we happen to start on a header-line, skip that. */
10071 if (row->mode_line_p)
10072 ++row;
10073
10074 if ((end && row >= end) || !row->enabled_p)
10075 return NULL;
10076
10077 last_y = window_text_bottom_y (w);
10078
10079 while ((end == NULL || row < end)
10080 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10081 /* The end position of a row equals the start
10082 position of the next row. If CHARPOS is there, we
10083 would rather display it in the next line, except
10084 when this line ends in ZV. */
10085 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10086 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10087 || !row->ends_at_zv_p)))
10088 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10089 ++row;
10090
10091 /* Give up if CHARPOS not found. */
10092 if ((end && row >= end)
10093 || charpos < MATRIX_ROW_START_CHARPOS (row)
10094 || charpos > MATRIX_ROW_END_CHARPOS (row))
10095 row = NULL;
10096
10097 return row;
10098}
10099
10100
5f5c8ee5
GM
10101/* Try to redisplay window W by reusing its existing display. W's
10102 current matrix must be up to date when this function is called,
10103 i.e. window_end_valid must not be nil.
10104
10105 Value is
10106
10107 1 if display has been updated
10108 0 if otherwise unsuccessful
10109 -1 if redisplay with same window start is known not to succeed
10110
10111 The following steps are performed:
10112
10113 1. Find the last row in the current matrix of W that is not
10114 affected by changes at the start of current_buffer. If no such row
10115 is found, give up.
10116
10117 2. Find the first row in W's current matrix that is not affected by
10118 changes at the end of current_buffer. Maybe there is no such row.
10119
10120 3. Display lines beginning with the row + 1 found in step 1 to the
10121 row found in step 2 or, if step 2 didn't find a row, to the end of
10122 the window.
10123
10124 4. If cursor is not known to appear on the window, give up.
10125
10126 5. If display stopped at the row found in step 2, scroll the
10127 display and current matrix as needed.
10128
10129 6. Maybe display some lines at the end of W, if we must. This can
10130 happen under various circumstances, like a partially visible line
10131 becoming fully visible, or because newly displayed lines are displayed
10132 in smaller font sizes.
10133
10134 7. Update W's window end information. */
10135
10136 /* Check that window end is what we expect it to be. */
12adba34
RS
10137
10138static int
5f5c8ee5 10139try_window_id (w)
12adba34 10140 struct window *w;
12adba34 10141{
5f5c8ee5
GM
10142 struct frame *f = XFRAME (w->frame);
10143 struct glyph_matrix *current_matrix = w->current_matrix;
10144 struct glyph_matrix *desired_matrix = w->desired_matrix;
10145 struct glyph_row *last_unchanged_at_beg_row;
10146 struct glyph_row *first_unchanged_at_end_row;
10147 struct glyph_row *row;
10148 struct glyph_row *bottom_row;
10149 int bottom_vpos;
10150 struct it it;
10151 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10152 struct text_pos start_pos;
10153 struct run run;
10154 int first_unchanged_at_end_vpos = 0;
10155 struct glyph_row *last_text_row, *last_text_row_at_end;
10156 struct text_pos start;
10157
10158 SET_TEXT_POS_FROM_MARKER (start, w->start);
10159
10160 /* Check pre-conditions. Window end must be valid, otherwise
10161 the current matrix would not be up to date. */
10162 xassert (!NILP (w->window_end_valid));
10163 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10164 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10165
10166 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10167 only if buffer has really changed. The reason is that the gap is
10168 initially at Z for freshly visited files. The code below would
10169 set end_unchanged to 0 in that case. */
28ee91c0
GM
10170 if (MODIFF > SAVE_MODIFF
10171 /* This seems to happen sometimes after saving a buffer. */
10172 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 10173 {
9142dd5b
GM
10174 if (GPT - BEG < BEG_UNCHANGED)
10175 BEG_UNCHANGED = GPT - BEG;
10176 if (Z - GPT < END_UNCHANGED)
10177 END_UNCHANGED = Z - GPT;
5f5c8ee5 10178 }
bf9249e3 10179
5f5c8ee5
GM
10180 /* If window starts after a line end, and the last change is in
10181 front of that newline, then changes don't affect the display.
f2d86d7a
GM
10182 This case happens with stealth-fontification. Note that although
10183 the display is unchanged, glyph positions in the matrix have to
10184 be adjusted, of course. */
5f5c8ee5
GM
10185 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10186 if (CHARPOS (start) > BEGV
9142dd5b 10187 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
10188 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10189 && PT < MATRIX_ROW_END_CHARPOS (row))
10190 {
f2d86d7a
GM
10191 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10192 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10193
10194 if (delta)
10195 {
10196 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10197 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10198
10199 increment_matrix_positions (w->current_matrix,
10200 MATRIX_ROW_VPOS (r0, current_matrix),
10201 MATRIX_ROW_VPOS (r1, current_matrix),
10202 delta, delta_bytes);
10203 }
10204
10205#if 0 /* If changes are all in front of the window start, the
10206 distance of the last displayed glyph from Z hasn't
10207 changed. */
5f5c8ee5
GM
10208 w->window_end_pos
10209 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10210 w->window_end_bytepos
6fc556fd 10211 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
f2d86d7a
GM
10212#endif
10213
5f5c8ee5
GM
10214 return 1;
10215 }
10216
10217 /* Return quickly if changes are all below what is displayed in the
10218 window, and if PT is in the window. */
9142dd5b 10219 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
10220 && PT < MATRIX_ROW_END_CHARPOS (row))
10221 {
10222 /* We have to update window end positions because the buffer's
10223 size has changed. */
10224 w->window_end_pos
10225 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10226 w->window_end_bytepos
6fc556fd 10227 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
5f5c8ee5
GM
10228 return 1;
10229 }
10230
10231 /* Check that window start agrees with the start of the first glyph
10232 row in its current matrix. Check this after we know the window
10233 start is not in changed text, otherwise positions would not be
10234 comparable. */
10235 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10236 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10237 return 0;
10238
5f5c8ee5
GM
10239 /* Compute the position at which we have to start displaying new
10240 lines. Some of the lines at the top of the window might be
10241 reusable because they are not displaying changed text. Find the
10242 last row in W's current matrix not affected by changes at the
10243 start of current_buffer. Value is null if changes start in the
10244 first line of window. */
10245 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10246 if (last_unchanged_at_beg_row)
10247 {
10248 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10249 start_pos = it.current.pos;
10250
10251 /* Start displaying new lines in the desired matrix at the same
10252 vpos we would use in the current matrix, i.e. below
10253 last_unchanged_at_beg_row. */
10254 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10255 current_matrix);
10256 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10257 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10258
10259 xassert (it.hpos == 0 && it.current_x == 0);
10260 }
10261 else
10262 {
10263 /* There are no reusable lines at the start of the window.
10264 Start displaying in the first line. */
10265 start_display (&it, w, start);
10266 start_pos = it.current.pos;
10267 }
10268
5f5c8ee5
GM
10269 /* Find the first row that is not affected by changes at the end of
10270 the buffer. Value will be null if there is no unchanged row, in
10271 which case we must redisplay to the end of the window. delta
10272 will be set to the value by which buffer positions beginning with
10273 first_unchanged_at_end_row have to be adjusted due to text
10274 changes. */
10275 first_unchanged_at_end_row
10276 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10277 IF_DEBUG (debug_delta = delta);
10278 IF_DEBUG (debug_delta_bytes = delta_bytes);
10279
10280 /* Set stop_pos to the buffer position up to which we will have to
10281 display new lines. If first_unchanged_at_end_row != NULL, this
10282 is the buffer position of the start of the line displayed in that
10283 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10284 that we don't stop at a buffer position. */
10285 stop_pos = 0;
10286 if (first_unchanged_at_end_row)
10287 {
10288 xassert (last_unchanged_at_beg_row == NULL
10289 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10290
10291 /* If this is a continuation line, move forward to the next one
10292 that isn't. Changes in lines above affect this line.
10293 Caution: this may move first_unchanged_at_end_row to a row
10294 not displaying text. */
10295 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10296 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10297 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10298 < it.last_visible_y))
10299 ++first_unchanged_at_end_row;
10300
10301 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10302 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10303 >= it.last_visible_y))
10304 first_unchanged_at_end_row = NULL;
10305 else
10306 {
10307 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10308 + delta);
10309 first_unchanged_at_end_vpos
10310 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 10311 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
10312 }
10313 }
10314 else if (last_unchanged_at_beg_row == NULL)
10315 return 0;
10316
10317
10318#if GLYPH_DEBUG
10319
10320 /* Either there is no unchanged row at the end, or the one we have
10321 now displays text. This is a necessary condition for the window
10322 end pos calculation at the end of this function. */
10323 xassert (first_unchanged_at_end_row == NULL
10324 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10325
10326 debug_last_unchanged_at_beg_vpos
10327 = (last_unchanged_at_beg_row
10328 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10329 : -1);
10330 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10331
10332#endif /* GLYPH_DEBUG != 0 */
10333
10334
10335 /* Display new lines. Set last_text_row to the last new line
10336 displayed which has text on it, i.e. might end up as being the
10337 line where the window_end_vpos is. */
10338 w->cursor.vpos = -1;
10339 last_text_row = NULL;
10340 overlay_arrow_seen = 0;
10341 while (it.current_y < it.last_visible_y
10342 && !fonts_changed_p
10343 && (first_unchanged_at_end_row == NULL
10344 || IT_CHARPOS (it) < stop_pos))
10345 {
10346 if (display_line (&it))
10347 last_text_row = it.glyph_row - 1;
10348 }
10349
10350 if (fonts_changed_p)
10351 return -1;
10352
10353
10354 /* Compute differences in buffer positions, y-positions etc. for
10355 lines reused at the bottom of the window. Compute what we can
10356 scroll. */
ca42b2e8
GM
10357 if (first_unchanged_at_end_row
10358 /* No lines reused because we displayed everything up to the
10359 bottom of the window. */
10360 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
10361 {
10362 dvpos = (it.vpos
10363 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10364 current_matrix));
10365 dy = it.current_y - first_unchanged_at_end_row->y;
10366 run.current_y = first_unchanged_at_end_row->y;
10367 run.desired_y = run.current_y + dy;
10368 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10369 }
10370 else
ca42b2e8
GM
10371 {
10372 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10373 first_unchanged_at_end_row = NULL;
10374 }
5f5c8ee5
GM
10375 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10376
8f8ba186 10377
5f5c8ee5
GM
10378 /* Find the cursor if not already found. We have to decide whether
10379 PT will appear on this window (it sometimes doesn't, but this is
10380 not a very frequent case.) This decision has to be made before
10381 the current matrix is altered. A value of cursor.vpos < 0 means
10382 that PT is either in one of the lines beginning at
10383 first_unchanged_at_end_row or below the window. Don't care for
10384 lines that might be displayed later at the window end; as
10385 mentioned, this is not a frequent case. */
10386 if (w->cursor.vpos < 0)
10387 {
5f5c8ee5
GM
10388 /* Cursor in unchanged rows at the top? */
10389 if (PT < CHARPOS (start_pos)
10390 && last_unchanged_at_beg_row)
10391 {
e037b9ec
GM
10392 row = row_containing_pos (w, PT,
10393 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10394 last_unchanged_at_beg_row + 1);
10395 xassert (row && row <= last_unchanged_at_beg_row);
5f5c8ee5
GM
10396 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10397 }
10398
10399 /* Start from first_unchanged_at_end_row looking for PT. */
10400 else if (first_unchanged_at_end_row)
10401 {
e037b9ec
GM
10402 row = row_containing_pos (w, PT - delta,
10403 first_unchanged_at_end_row, NULL);
10404 if (row)
468155d7
GM
10405 set_cursor_from_row (w, row, w->current_matrix, delta,
10406 delta_bytes, dy, dvpos);
5f5c8ee5
GM
10407 }
10408
10409 /* Give up if cursor was not found. */
10410 if (w->cursor.vpos < 0)
10411 {
10412 clear_glyph_matrix (w->desired_matrix);
10413 return -1;
10414 }
10415 }
10416
10417 /* Don't let the cursor end in the scroll margins. */
10418 {
10419 int this_scroll_margin, cursor_height;
10420
10421 this_scroll_margin = max (0, scroll_margin);
10422 this_scroll_margin = min (this_scroll_margin,
10423 XFASTINT (w->height) / 4);
10424 this_scroll_margin *= CANON_Y_UNIT (it.f);
10425 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10426
10427 if ((w->cursor.y < this_scroll_margin
10428 && CHARPOS (start) > BEGV)
10429 /* Don't take scroll margin into account at the bottom because
10430 old redisplay didn't do it either. */
10431 || w->cursor.y + cursor_height > it.last_visible_y)
10432 {
10433 w->cursor.vpos = -1;
10434 clear_glyph_matrix (w->desired_matrix);
10435 return -1;
10436 }
10437 }
10438
10439 /* Scroll the display. Do it before changing the current matrix so
10440 that xterm.c doesn't get confused about where the cursor glyph is
10441 found. */
fa77249f 10442 if (dy && run.height)
5f5c8ee5
GM
10443 {
10444 update_begin (f);
10445
10446 if (FRAME_WINDOW_P (f))
10447 {
10448 rif->update_window_begin_hook (w);
10449 rif->scroll_run_hook (w, &run);
10450 rif->update_window_end_hook (w, 0);
10451 }
10452 else
10453 {
10454 /* Terminal frame. In this case, dvpos gives the number of
10455 lines to scroll by; dvpos < 0 means scroll up. */
10456 int first_unchanged_at_end_vpos
10457 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10458 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10459 int end = XFASTINT (w->top) + window_internal_height (w);
10460
10461 /* Perform the operation on the screen. */
10462 if (dvpos > 0)
10463 {
10464 /* Scroll last_unchanged_at_beg_row to the end of the
10465 window down dvpos lines. */
10466 set_terminal_window (end);
10467
10468 /* On dumb terminals delete dvpos lines at the end
10469 before inserting dvpos empty lines. */
10470 if (!scroll_region_ok)
10471 ins_del_lines (end - dvpos, -dvpos);
10472
10473 /* Insert dvpos empty lines in front of
10474 last_unchanged_at_beg_row. */
10475 ins_del_lines (from, dvpos);
10476 }
10477 else if (dvpos < 0)
10478 {
10479 /* Scroll up last_unchanged_at_beg_vpos to the end of
10480 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10481 set_terminal_window (end);
10482
10483 /* Delete dvpos lines in front of
10484 last_unchanged_at_beg_vpos. ins_del_lines will set
10485 the cursor to the given vpos and emit |dvpos| delete
10486 line sequences. */
10487 ins_del_lines (from + dvpos, dvpos);
10488
10489 /* On a dumb terminal insert dvpos empty lines at the
10490 end. */
10491 if (!scroll_region_ok)
10492 ins_del_lines (end + dvpos, -dvpos);
10493 }
10494
10495 set_terminal_window (0);
10496 }
10497
10498 update_end (f);
10499 }
10500
ca42b2e8
GM
10501 /* Shift reused rows of the current matrix to the right position.
10502 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10503 text. */
10504 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10505 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
10506 if (dvpos < 0)
10507 {
10508 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10509 bottom_vpos, dvpos);
10510 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10511 bottom_vpos, 0);
10512 }
10513 else if (dvpos > 0)
10514 {
10515 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10516 bottom_vpos, dvpos);
10517 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10518 first_unchanged_at_end_vpos + dvpos, 0);
10519 }
10520
10521 /* For frame-based redisplay, make sure that current frame and window
10522 matrix are in sync with respect to glyph memory. */
10523 if (!FRAME_WINDOW_P (f))
10524 sync_frame_with_window_matrix_rows (w);
10525
10526 /* Adjust buffer positions in reused rows. */
10527 if (delta)
f2d86d7a
GM
10528 increment_matrix_positions (current_matrix,
10529 first_unchanged_at_end_vpos + dvpos,
10530 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
10531
10532 /* Adjust Y positions. */
10533 if (dy)
10534 shift_glyph_matrix (w, current_matrix,
10535 first_unchanged_at_end_vpos + dvpos,
10536 bottom_vpos, dy);
10537
10538 if (first_unchanged_at_end_row)
10539 first_unchanged_at_end_row += dvpos;
10540
10541 /* If scrolling up, there may be some lines to display at the end of
10542 the window. */
10543 last_text_row_at_end = NULL;
10544 if (dy < 0)
10545 {
10546 /* Set last_row to the glyph row in the current matrix where the
10547 window end line is found. It has been moved up or down in
10548 the matrix by dvpos. */
10549 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10550 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10551
10552 /* If last_row is the window end line, it should display text. */
10553 xassert (last_row->displays_text_p);
10554
10555 /* If window end line was partially visible before, begin
10556 displaying at that line. Otherwise begin displaying with the
10557 line following it. */
10558 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10559 {
10560 init_to_row_start (&it, w, last_row);
10561 it.vpos = last_vpos;
10562 it.current_y = last_row->y;
10563 }
10564 else
10565 {
10566 init_to_row_end (&it, w, last_row);
10567 it.vpos = 1 + last_vpos;
10568 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10569 ++last_row;
10570 }
12adba34 10571
5f5c8ee5
GM
10572 /* We may start in a continuation line. If so, we have to get
10573 the right continuation_lines_width and current_x. */
10574 it.continuation_lines_width = last_row->continuation_lines_width;
10575 it.hpos = it.current_x = 0;
10576
10577 /* Display the rest of the lines at the window end. */
10578 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10579 while (it.current_y < it.last_visible_y
10580 && !fonts_changed_p)
10581 {
10582 /* Is it always sure that the display agrees with lines in
10583 the current matrix? I don't think so, so we mark rows
10584 displayed invalid in the current matrix by setting their
10585 enabled_p flag to zero. */
10586 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10587 if (display_line (&it))
10588 last_text_row_at_end = it.glyph_row - 1;
10589 }
10590 }
12adba34 10591
5f5c8ee5
GM
10592 /* Update window_end_pos and window_end_vpos. */
10593 if (first_unchanged_at_end_row
10594 && first_unchanged_at_end_row->y < it.last_visible_y
10595 && !last_text_row_at_end)
10596 {
10597 /* Window end line if one of the preserved rows from the current
10598 matrix. Set row to the last row displaying text in current
10599 matrix starting at first_unchanged_at_end_row, after
10600 scrolling. */
10601 xassert (first_unchanged_at_end_row->displays_text_p);
10602 row = find_last_row_displaying_text (w->current_matrix, &it,
10603 first_unchanged_at_end_row);
10604 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10605
10606 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10607 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10608 XSETFASTINT (w->window_end_vpos,
10609 MATRIX_ROW_VPOS (row, w->current_matrix));
10610 }
10611 else if (last_text_row_at_end)
10612 {
10613 XSETFASTINT (w->window_end_pos,
10614 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10615 w->window_end_bytepos
10616 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10617 XSETFASTINT (w->window_end_vpos,
10618 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10619 }
10620 else if (last_text_row)
10621 {
10622 /* We have displayed either to the end of the window or at the
10623 end of the window, i.e. the last row with text is to be found
10624 in the desired matrix. */
10625 XSETFASTINT (w->window_end_pos,
10626 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10627 w->window_end_bytepos
10628 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10629 XSETFASTINT (w->window_end_vpos,
10630 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10631 }
10632 else if (first_unchanged_at_end_row == NULL
10633 && last_text_row == NULL
10634 && last_text_row_at_end == NULL)
10635 {
10636 /* Displayed to end of window, but no line containing text was
10637 displayed. Lines were deleted at the end of the window. */
10638 int vpos;
045dee35 10639 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
10640
10641 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
045dee35
GM
10642 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10643 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10644 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10645 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
5f5c8ee5 10646 break;
12adba34 10647
5f5c8ee5
GM
10648 w->window_end_vpos = make_number (vpos);
10649 }
10650 else
10651 abort ();
10652
10653 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10654 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 10655
5f5c8ee5
GM
10656 /* Record that display has not been completed. */
10657 w->window_end_valid = Qnil;
10658 w->desired_matrix->no_scrolling_p = 1;
10659 return 1;
12adba34 10660}
0f9c0ff0 10661
a2889657 10662
5f5c8ee5
GM
10663\f
10664/***********************************************************************
10665 More debugging support
10666 ***********************************************************************/
a2889657 10667
5f5c8ee5 10668#if GLYPH_DEBUG
a2889657 10669
5f5c8ee5
GM
10670 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10671static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 10672
31b24551 10673
5f5c8ee5
GM
10674/* Dump the contents of glyph matrix MATRIX on stderr. If
10675 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 10676
5f85fa4e 10677static void
5f5c8ee5
GM
10678dump_glyph_matrix (matrix, with_glyphs_p)
10679 struct glyph_matrix *matrix;
10680 int with_glyphs_p;
10681{
efc63ef0 10682 int i;
5f5c8ee5
GM
10683 for (i = 0; i < matrix->nrows; ++i)
10684 dump_glyph_row (matrix, i, with_glyphs_p);
10685}
31b24551 10686
68a37fa8 10687
5f5c8ee5
GM
10688/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10689 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 10690
5f5c8ee5
GM
10691void
10692dump_glyph_row (matrix, vpos, with_glyphs_p)
10693 struct glyph_matrix *matrix;
10694 int vpos, with_glyphs_p;
10695{
10696 struct glyph_row *row;
10697
10698 if (vpos < 0 || vpos >= matrix->nrows)
10699 return;
10700
10701 row = MATRIX_ROW (matrix, vpos);
10702
91004049 10703 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W\n");
5f5c8ee5
GM
10704 fprintf (stderr, "=============================================\n");
10705
91004049 10706 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%1.1d%1.1d %4d %4d %4d\n",
5f5c8ee5
GM
10707 row - matrix->rows,
10708 MATRIX_ROW_START_CHARPOS (row),
10709 MATRIX_ROW_END_CHARPOS (row),
10710 row->used[TEXT_AREA],
10711 row->contains_overlapping_glyphs_p,
10712 row->enabled_p,
10713 row->inverse_p,
10714 row->truncated_on_left_p,
10715 row->truncated_on_right_p,
10716 row->overlay_arrow_p,
10717 row->continued_p,
10718 MATRIX_ROW_CONTINUATION_LINE_P (row),
10719 row->displays_text_p,
10720 row->ends_at_zv_p,
10721 row->fill_line_p,
91004049
GM
10722 row->ends_in_middle_of_char_p,
10723 row->starts_in_middle_of_char_p,
5f5c8ee5
GM
10724 row->x,
10725 row->y,
10726 row->pixel_width);
10727 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10728 row->end.overlay_string_index);
10729 fprintf (stderr, "%9d %5d\n",
10730 CHARPOS (row->start.string_pos),
10731 CHARPOS (row->end.string_pos));
10732 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10733 row->end.dpvec_index);
10734
10735 if (with_glyphs_p)
bd66d1ba 10736 {
5f5c8ee5
GM
10737 struct glyph *glyph, *glyph_end;
10738 int prev_had_glyphs_p;
10739
10740 glyph = row->glyphs[TEXT_AREA];
10741 glyph_end = glyph + row->used[TEXT_AREA];
10742
10743 /* Glyph for a line end in text. */
10744 if (glyph == glyph_end && glyph->charpos > 0)
10745 ++glyph_end;
10746
10747 if (glyph < glyph_end)
bd66d1ba 10748 {
06568bbf 10749 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 10750 prev_had_glyphs_p = 1;
bd66d1ba
RS
10751 }
10752 else
5f5c8ee5
GM
10753 prev_had_glyphs_p = 0;
10754
10755 while (glyph < glyph_end)
f7b4b63a 10756 {
5f5c8ee5
GM
10757 if (glyph->type == CHAR_GLYPH)
10758 {
10759 fprintf (stderr,
06568bbf 10760 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
10761 glyph - row->glyphs[TEXT_AREA],
10762 'C',
10763 glyph->charpos,
06568bbf
GM
10764 (BUFFERP (glyph->object)
10765 ? 'B'
10766 : (STRINGP (glyph->object)
10767 ? 'S'
10768 : '-')),
5f5c8ee5 10769 glyph->pixel_width,
43d120d8
KH
10770 glyph->u.ch,
10771 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10772 ? glyph->u.ch
5f5c8ee5 10773 : '.'),
43d120d8 10774 glyph->face_id,
5f5c8ee5
GM
10775 glyph->left_box_line_p,
10776 glyph->right_box_line_p);
10777 }
10778 else if (glyph->type == STRETCH_GLYPH)
10779 {
10780 fprintf (stderr,
06568bbf 10781 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
10782 glyph - row->glyphs[TEXT_AREA],
10783 'S',
10784 glyph->charpos,
06568bbf
GM
10785 (BUFFERP (glyph->object)
10786 ? 'B'
10787 : (STRINGP (glyph->object)
10788 ? 'S'
10789 : '-')),
5f5c8ee5
GM
10790 glyph->pixel_width,
10791 0,
10792 '.',
bcdda9a4 10793 glyph->face_id,
5f5c8ee5
GM
10794 glyph->left_box_line_p,
10795 glyph->right_box_line_p);
10796 }
10797 else if (glyph->type == IMAGE_GLYPH)
10798 {
10799 fprintf (stderr,
06568bbf 10800 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
10801 glyph - row->glyphs[TEXT_AREA],
10802 'I',
10803 glyph->charpos,
06568bbf
GM
10804 (BUFFERP (glyph->object)
10805 ? 'B'
10806 : (STRINGP (glyph->object)
10807 ? 'S'
10808 : '-')),
5f5c8ee5 10809 glyph->pixel_width,
43d120d8 10810 glyph->u.img_id,
5f5c8ee5 10811 '.',
bcdda9a4 10812 glyph->face_id,
5f5c8ee5
GM
10813 glyph->left_box_line_p,
10814 glyph->right_box_line_p);
10815 }
10816 ++glyph;
f7b4b63a 10817 }
f4faa47c 10818 }
5f5c8ee5 10819}
f4faa47c 10820
a2889657 10821
5f5c8ee5
GM
10822DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10823 Sdump_glyph_matrix, 0, 1, "p",
10824 "Dump the current matrix of the selected window to stderr.\n\
10825Shows contents of glyph row structures. With non-nil optional\n\
10826parameter WITH-GLYPHS-P, dump glyphs as well.")
10827 (with_glyphs_p)
02513cdd 10828 Lisp_Object with_glyphs_p;
5f5c8ee5
GM
10829{
10830 struct window *w = XWINDOW (selected_window);
10831 struct buffer *buffer = XBUFFER (w->buffer);
10832
10833 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10834 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10835 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10836 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10837 fprintf (stderr, "=============================================\n");
10838 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10839 return Qnil;
10840}
1c2250c2 10841
1fca3fae 10842
5f5c8ee5
GM
10843DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10844 "Dump glyph row ROW to stderr.")
10845 (row)
10846 Lisp_Object row;
10847{
10848 CHECK_NUMBER (row, 0);
10849 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10850 return Qnil;
10851}
1fca3fae 10852
67481ae5 10853
e037b9ec 10854DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
5f5c8ee5
GM
10855 0, 0, "", "")
10856 ()
10857{
886bd6f2
GM
10858 struct frame *sf = SELECTED_FRAME ();
10859 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
5f5c8ee5
GM
10860 ->current_matrix);
10861 dump_glyph_row (m, 0, 1);
10862 return Qnil;
10863}
ca26e1c8 10864
0f9c0ff0 10865
5f5c8ee5
GM
10866DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10867 Strace_redisplay_toggle, 0, 0, "",
10868 "Toggle tracing of redisplay.")
10869 ()
10870{
10871 trace_redisplay_p = !trace_redisplay_p;
10872 return Qnil;
10873}
bf9249e3
GM
10874
10875
10876DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10877 "Print STRING to stderr.")
10878 (string)
10879 Lisp_Object string;
10880{
10881 CHECK_STRING (string, 0);
10882 fprintf (stderr, "%s", XSTRING (string)->data);
10883 return Qnil;
10884}
5f5c8ee5
GM
10885
10886#endif /* GLYPH_DEBUG */
ca26e1c8 10887
ca26e1c8 10888
5f5c8ee5
GM
10889\f
10890/***********************************************************************
10891 Building Desired Matrix Rows
10892 ***********************************************************************/
a2889657 10893
5f5c8ee5
GM
10894/* Return a temporary glyph row holding the glyphs of an overlay
10895 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 10896
5f5c8ee5
GM
10897static struct glyph_row *
10898get_overlay_arrow_glyph_row (w)
10899 struct window *w;
10900{
10901 struct frame *f = XFRAME (WINDOW_FRAME (w));
10902 struct buffer *buffer = XBUFFER (w->buffer);
10903 struct buffer *old = current_buffer;
10904 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10905 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10906 unsigned char *arrow_end = arrow_string + arrow_len;
10907 unsigned char *p;
10908 struct it it;
10909 int multibyte_p;
10910 int n_glyphs_before;
10911
10912 set_buffer_temp (buffer);
10913 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10914 it.glyph_row->used[TEXT_AREA] = 0;
10915 SET_TEXT_POS (it.position, 0, 0);
10916
10917 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10918 p = arrow_string;
10919 while (p < arrow_end)
10920 {
10921 Lisp_Object face, ilisp;
10922
10923 /* Get the next character. */
10924 if (multibyte_p)
4fdb80f2 10925 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
10926 else
10927 it.c = *p, it.len = 1;
10928 p += it.len;
10929
10930 /* Get its face. */
10931 XSETFASTINT (ilisp, p - arrow_string);
10932 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10933 it.face_id = compute_char_face (f, it.c, face);
10934
10935 /* Compute its width, get its glyphs. */
10936 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 10937 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
10938 PRODUCE_GLYPHS (&it);
10939
10940 /* If this character doesn't fit any more in the line, we have
10941 to remove some glyphs. */
10942 if (it.current_x > it.last_visible_x)
10943 {
10944 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10945 break;
10946 }
10947 }
10948
10949 set_buffer_temp (old);
10950 return it.glyph_row;
10951}
ca26e1c8 10952
b0a0fbda 10953
5f5c8ee5
GM
10954/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10955 glyphs are only inserted for terminal frames since we can't really
10956 win with truncation glyphs when partially visible glyphs are
10957 involved. Which glyphs to insert is determined by
10958 produce_special_glyphs. */
67481ae5 10959
5f5c8ee5
GM
10960static void
10961insert_left_trunc_glyphs (it)
10962 struct it *it;
10963{
10964 struct it truncate_it;
10965 struct glyph *from, *end, *to, *toend;
10966
10967 xassert (!FRAME_WINDOW_P (it->f));
10968
10969 /* Get the truncation glyphs. */
10970 truncate_it = *it;
5f5c8ee5
GM
10971 truncate_it.current_x = 0;
10972 truncate_it.face_id = DEFAULT_FACE_ID;
10973 truncate_it.glyph_row = &scratch_glyph_row;
10974 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10975 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 10976 truncate_it.object = make_number (0);
5f5c8ee5
GM
10977 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10978
10979 /* Overwrite glyphs from IT with truncation glyphs. */
10980 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10981 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10982 to = it->glyph_row->glyphs[TEXT_AREA];
10983 toend = to + it->glyph_row->used[TEXT_AREA];
10984
10985 while (from < end)
10986 *to++ = *from++;
10987
10988 /* There may be padding glyphs left over. Remove them. */
10989 from = to;
10990 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10991 ++from;
10992 while (from < toend)
10993 *to++ = *from++;
10994
10995 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10996}
e0bfbde6 10997
e0bfbde6 10998
5f5c8ee5 10999/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 11000
5f5c8ee5
GM
11001 Most of the time, ascent and height of a display line will be equal
11002 to the max_ascent and max_height values of the display iterator
11003 structure. This is not the case if
67481ae5 11004
5f5c8ee5
GM
11005 1. We hit ZV without displaying anything. In this case, max_ascent
11006 and max_height will be zero.
1c9241f5 11007
5f5c8ee5
GM
11008 2. We have some glyphs that don't contribute to the line height.
11009 (The glyph row flag contributes_to_line_height_p is for future
11010 pixmap extensions).
f6fd109b 11011
5f5c8ee5
GM
11012 The first case is easily covered by using default values because in
11013 these cases, the line height does not really matter, except that it
11014 must not be zero. */
67481ae5 11015
5f5c8ee5
GM
11016static void
11017compute_line_metrics (it)
11018 struct it *it;
11019{
11020 struct glyph_row *row = it->glyph_row;
11021 int area, i;
1c2250c2 11022
5f5c8ee5
GM
11023 if (FRAME_WINDOW_P (it->f))
11024 {
045dee35 11025 int i, header_line_height;
1c2250c2 11026
5f5c8ee5
GM
11027 /* The line may consist of one space only, that was added to
11028 place the cursor on it. If so, the row's height hasn't been
11029 computed yet. */
11030 if (row->height == 0)
11031 {
11032 if (it->max_ascent + it->max_descent == 0)
312246d1 11033 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
11034 row->ascent = it->max_ascent;
11035 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11036 row->phys_ascent = it->max_phys_ascent;
11037 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
11038 }
11039
11040 /* Compute the width of this line. */
11041 row->pixel_width = row->x;
11042 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11043 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11044
11045 xassert (row->pixel_width >= 0);
11046 xassert (row->ascent >= 0 && row->height > 0);
11047
312246d1
GM
11048 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11049 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11050
11051 /* If first line's physical ascent is larger than its logical
11052 ascent, use the physical ascent, and make the row taller.
11053 This makes accented characters fully visible. */
11054 if (row == it->w->desired_matrix->rows
11055 && row->phys_ascent > row->ascent)
11056 {
11057 row->height += row->phys_ascent - row->ascent;
11058 row->ascent = row->phys_ascent;
11059 }
11060
5f5c8ee5
GM
11061 /* Compute how much of the line is visible. */
11062 row->visible_height = row->height;
11063
045dee35
GM
11064 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11065 if (row->y < header_line_height)
11066 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
11067 else
11068 {
11069 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11070 if (row->y + row->height > max_y)
11071 row->visible_height -= row->y + row->height - max_y;
11072 }
11073 }
11074 else
11075 {
11076 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
11077 row->ascent = row->phys_ascent = 0;
11078 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 11079 }
67481ae5 11080
5f5c8ee5
GM
11081 /* Compute a hash code for this row. */
11082 row->hash = 0;
11083 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11084 for (i = 0; i < row->used[area]; ++i)
11085 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11086 + row->glyphs[area][i].u.val
43d120d8
KH
11087 + row->glyphs[area][i].face_id
11088 + row->glyphs[area][i].padding_p
5f5c8ee5 11089 + (row->glyphs[area][i].type << 2));
a2889657 11090
5f5c8ee5 11091 it->max_ascent = it->max_descent = 0;
312246d1 11092 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 11093}
12adba34 11094
ca26e1c8 11095
5f5c8ee5
GM
11096/* Append one space to the glyph row of iterator IT if doing a
11097 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11098 space have the default face, otherwise let it have the same face as
80c6cb1f 11099 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
11100
11101 This function is called to make sure that there is always one glyph
11102 at the end of a glyph row that the cursor can be set on under
11103 window-systems. (If there weren't such a glyph we would not know
11104 how wide and tall a box cursor should be displayed).
11105
11106 At the same time this space let's a nicely handle clearing to the
11107 end of the line if the row ends in italic text. */
ca26e1c8 11108
80c6cb1f 11109static int
5f5c8ee5
GM
11110append_space (it, default_face_p)
11111 struct it *it;
11112 int default_face_p;
11113{
11114 if (FRAME_WINDOW_P (it->f))
11115 {
11116 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 11117
5f5c8ee5
GM
11118 if (it->glyph_row->glyphs[TEXT_AREA] + n
11119 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 11120 {
5f5c8ee5
GM
11121 /* Save some values that must not be changed. */
11122 int saved_x = it->current_x;
11123 struct text_pos saved_pos;
11124 int saved_what = it->what;
11125 int saved_face_id = it->face_id;
5f5c8ee5 11126 Lisp_Object saved_object;
980806b6 11127 struct face *face;
5f5c8ee5
GM
11128
11129 saved_object = it->object;
11130 saved_pos = it->position;
11131
11132 it->what = IT_CHARACTER;
11133 bzero (&it->position, sizeof it->position);
6fc556fd 11134 it->object = make_number (0);
5f5c8ee5
GM
11135 it->c = ' ';
11136 it->len = 1;
5f5c8ee5
GM
11137
11138 if (default_face_p)
11139 it->face_id = DEFAULT_FACE_ID;
980806b6
KH
11140 face = FACE_FROM_ID (it->f, it->face_id);
11141 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 11142
5f5c8ee5
GM
11143 PRODUCE_GLYPHS (it);
11144
11145 it->current_x = saved_x;
11146 it->object = saved_object;
11147 it->position = saved_pos;
11148 it->what = saved_what;
11149 it->face_id = saved_face_id;
80c6cb1f 11150 return 1;
5f5c8ee5
GM
11151 }
11152 }
80c6cb1f
GM
11153
11154 return 0;
5f5c8ee5 11155}
12adba34 11156
1842fc1a 11157
5f5c8ee5
GM
11158/* Extend the face of the last glyph in the text area of IT->glyph_row
11159 to the end of the display line. Called from display_line.
11160 If the glyph row is empty, add a space glyph to it so that we
11161 know the face to draw. Set the glyph row flag fill_line_p. */
11162
11163static void
11164extend_face_to_end_of_line (it)
11165 struct it *it;
11166{
11167 struct face *face;
11168 struct frame *f = it->f;
1842fc1a 11169
5f5c8ee5
GM
11170 /* If line is already filled, do nothing. */
11171 if (it->current_x >= it->last_visible_x)
11172 return;
11173
11174 /* Face extension extends the background and box of IT->face_id
11175 to the end of the line. If the background equals the background
11176 of the frame, we haven't to do anything. */
11177 face = FACE_FROM_ID (f, it->face_id);
11178 if (FRAME_WINDOW_P (f)
11179 && face->box == FACE_NO_BOX
11180 && face->background == FRAME_BACKGROUND_PIXEL (f)
11181 && !face->stipple)
11182 return;
1842fc1a 11183
5f5c8ee5
GM
11184 /* Set the glyph row flag indicating that the face of the last glyph
11185 in the text area has to be drawn to the end of the text area. */
11186 it->glyph_row->fill_line_p = 1;
545e04f6 11187
980806b6
KH
11188 /* If current character of IT is not ASCII, make sure we have the
11189 ASCII face. This will be automatically undone the next time
11190 get_next_display_element returns a multibyte character. Note
11191 that the character will always be single byte in unibyte text. */
11192 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 11193 {
980806b6 11194 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 11195 }
545e04f6 11196
5f5c8ee5
GM
11197 if (FRAME_WINDOW_P (f))
11198 {
11199 /* If the row is empty, add a space with the current face of IT,
11200 so that we know which face to draw. */
11201 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 11202 {
5f5c8ee5 11203 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 11204 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 11205 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 11206 }
5f5c8ee5
GM
11207 }
11208 else
11209 {
11210 /* Save some values that must not be changed. */
11211 int saved_x = it->current_x;
11212 struct text_pos saved_pos;
11213 Lisp_Object saved_object;
11214 int saved_what = it->what;
11215
11216 saved_object = it->object;
11217 saved_pos = it->position;
11218
11219 it->what = IT_CHARACTER;
11220 bzero (&it->position, sizeof it->position);
6fc556fd 11221 it->object = make_number (0);
5f5c8ee5
GM
11222 it->c = ' ';
11223 it->len = 1;
11224
11225 PRODUCE_GLYPHS (it);
11226
11227 while (it->current_x <= it->last_visible_x)
11228 PRODUCE_GLYPHS (it);
11229
11230 /* Don't count these blanks really. It would let us insert a left
11231 truncation glyph below and make us set the cursor on them, maybe. */
11232 it->current_x = saved_x;
11233 it->object = saved_object;
11234 it->position = saved_pos;
11235 it->what = saved_what;
11236 }
11237}
12adba34 11238
545e04f6 11239
5f5c8ee5
GM
11240/* Value is non-zero if text starting at CHARPOS in current_buffer is
11241 trailing whitespace. */
1c9241f5 11242
5f5c8ee5
GM
11243static int
11244trailing_whitespace_p (charpos)
11245 int charpos;
11246{
11247 int bytepos = CHAR_TO_BYTE (charpos);
11248 int c = 0;
7bbe686f 11249
5f5c8ee5
GM
11250 while (bytepos < ZV_BYTE
11251 && (c = FETCH_CHAR (bytepos),
11252 c == ' ' || c == '\t'))
11253 ++bytepos;
0d09d1e6 11254
8f897821
GM
11255 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11256 {
11257 if (bytepos != PT_BYTE)
11258 return 1;
11259 }
11260 return 0;
5f5c8ee5 11261}
31b24551 11262
545e04f6 11263
5f5c8ee5 11264/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 11265
5f5c8ee5
GM
11266void
11267highlight_trailing_whitespace (f, row)
11268 struct frame *f;
11269 struct glyph_row *row;
11270{
11271 int used = row->used[TEXT_AREA];
11272
11273 if (used)
11274 {
11275 struct glyph *start = row->glyphs[TEXT_AREA];
11276 struct glyph *glyph = start + used - 1;
11277
11278 /* Skip over the space glyph inserted to display the
11279 cursor at the end of a line. */
11280 if (glyph->type == CHAR_GLYPH
43d120d8 11281 && glyph->u.ch == ' '
6fc556fd 11282 && INTEGERP (glyph->object))
5f5c8ee5
GM
11283 --glyph;
11284
11285 /* If last glyph is a space or stretch, and it's trailing
11286 whitespace, set the face of all trailing whitespace glyphs in
11287 IT->glyph_row to `trailing-whitespace'. */
11288 if (glyph >= start
11289 && BUFFERP (glyph->object)
11290 && (glyph->type == STRETCH_GLYPH
11291 || (glyph->type == CHAR_GLYPH
43d120d8 11292 && glyph->u.ch == ' '))
5f5c8ee5 11293 && trailing_whitespace_p (glyph->charpos))
545e04f6 11294 {
980806b6 11295 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
11296
11297 while (glyph >= start
11298 && BUFFERP (glyph->object)
11299 && (glyph->type == STRETCH_GLYPH
11300 || (glyph->type == CHAR_GLYPH
43d120d8
KH
11301 && glyph->u.ch == ' ')))
11302 (glyph--)->face_id = face_id;
545e04f6 11303 }
a2889657 11304 }
5f5c8ee5 11305}
a2889657 11306
5fcbb24d 11307
5f5c8ee5
GM
11308/* Construct the glyph row IT->glyph_row in the desired matrix of
11309 IT->w from text at the current position of IT. See dispextern.h
11310 for an overview of struct it. Value is non-zero if
11311 IT->glyph_row displays text, as opposed to a line displaying ZV
11312 only. */
11313
11314static int
11315display_line (it)
11316 struct it *it;
11317{
11318 struct glyph_row *row = it->glyph_row;
11319
11320 /* We always start displaying at hpos zero even if hscrolled. */
11321 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 11322
5f5c8ee5
GM
11323 /* We must not display in a row that's not a text row. */
11324 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11325 < it->w->desired_matrix->nrows);
12adba34 11326
5f5c8ee5
GM
11327 /* Is IT->w showing the region? */
11328 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 11329
5f5c8ee5
GM
11330 /* Clear the result glyph row and enable it. */
11331 prepare_desired_row (row);
12adba34 11332
5f5c8ee5
GM
11333 row->y = it->current_y;
11334 row->start = it->current;
11335 row->continuation_lines_width = it->continuation_lines_width;
11336 row->displays_text_p = 1;
91004049
GM
11337 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11338 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
11339
11340 /* Arrange the overlays nicely for our purposes. Usually, we call
11341 display_line on only one line at a time, in which case this
11342 can't really hurt too much, or we call it on lines which appear
11343 one after another in the buffer, in which case all calls to
11344 recenter_overlay_lists but the first will be pretty cheap. */
11345 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11346
5f5c8ee5
GM
11347 /* Move over display elements that are not visible because we are
11348 hscrolled. This may stop at an x-position < IT->first_visible_x
11349 if the first glyph is partially visible or if we hit a line end. */
11350 if (it->current_x < it->first_visible_x)
11351 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11352 MOVE_TO_POS | MOVE_TO_X);
11353
11354 /* Get the initial row height. This is either the height of the
11355 text hscrolled, if there is any, or zero. */
11356 row->ascent = it->max_ascent;
11357 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11358 row->phys_ascent = it->max_phys_ascent;
11359 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
11360
11361 /* Loop generating characters. The loop is left with IT on the next
11362 character to display. */
11363 while (1)
11364 {
11365 int n_glyphs_before, hpos_before, x_before;
11366 int x, i, nglyphs;
e6819faf 11367 int ascent, descent, phys_ascent, phys_descent;
5f5c8ee5
GM
11368
11369 /* Retrieve the next thing to display. Value is zero if end of
11370 buffer reached. */
11371 if (!get_next_display_element (it))
11372 {
11373 /* Maybe add a space at the end of this line that is used to
1b335865
GM
11374 display the cursor there under X. Set the charpos of the
11375 first glyph of blank lines not corresponding to any text
11376 to -1. */
11377 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11378 || row->used[TEXT_AREA] == 0)
a2889657 11379 {
5f5c8ee5
GM
11380 row->glyphs[TEXT_AREA]->charpos = -1;
11381 row->displays_text_p = 0;
11382
11383 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11384 row->indicate_empty_line_p = 1;
a2889657 11385 }
5f5c8ee5
GM
11386
11387 it->continuation_lines_width = 0;
11388 row->ends_at_zv_p = 1;
11389 break;
a2889657 11390 }
a2889657 11391
5f5c8ee5
GM
11392 /* Now, get the metrics of what we want to display. This also
11393 generates glyphs in `row' (which is IT->glyph_row). */
11394 n_glyphs_before = row->used[TEXT_AREA];
11395 x = it->current_x;
e6819faf
GM
11396
11397 /* Remember the line height so far in case the next element doesn't
11398 fit on the line. */
11399 if (!it->truncate_lines_p)
11400 {
11401 ascent = it->max_ascent;
11402 descent = it->max_descent;
11403 phys_ascent = it->max_phys_ascent;
11404 phys_descent = it->max_phys_descent;
11405 }
11406
5f5c8ee5 11407 PRODUCE_GLYPHS (it);
a2889657 11408
5f5c8ee5
GM
11409 /* If this display element was in marginal areas, continue with
11410 the next one. */
11411 if (it->area != TEXT_AREA)
a2889657 11412 {
5f5c8ee5
GM
11413 row->ascent = max (row->ascent, it->max_ascent);
11414 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11415 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11416 row->phys_height = max (row->phys_height,
11417 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11418 set_iterator_to_next (it);
11419 continue;
11420 }
5936754e 11421
5f5c8ee5
GM
11422 /* Does the display element fit on the line? If we truncate
11423 lines, we should draw past the right edge of the window. If
11424 we don't truncate, we want to stop so that we can display the
11425 continuation glyph before the right margin. If lines are
11426 continued, there are two possible strategies for characters
11427 resulting in more than 1 glyph (e.g. tabs): Display as many
11428 glyphs as possible in this line and leave the rest for the
11429 continuation line, or display the whole element in the next
11430 line. Original redisplay did the former, so we do it also. */
11431 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11432 hpos_before = it->hpos;
11433 x_before = x;
11434
11435 if (nglyphs == 1
11436 && it->current_x < it->last_visible_x)
11437 {
11438 ++it->hpos;
11439 row->ascent = max (row->ascent, it->max_ascent);
11440 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11441 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11442 row->phys_height = max (row->phys_height,
11443 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11444 if (it->current_x - it->pixel_width < it->first_visible_x)
11445 row->x = x - it->first_visible_x;
11446 }
11447 else
11448 {
11449 int new_x;
11450 struct glyph *glyph;
11451
11452 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 11453 {
5f5c8ee5
GM
11454 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11455 new_x = x + glyph->pixel_width;
11456
11457 if (/* Lines are continued. */
11458 !it->truncate_lines_p
11459 && (/* Glyph doesn't fit on the line. */
11460 new_x > it->last_visible_x
11461 /* Or it fits exactly on a window system frame. */
11462 || (new_x == it->last_visible_x
11463 && FRAME_WINDOW_P (it->f))))
a2889657 11464 {
5f5c8ee5
GM
11465 /* End of a continued line. */
11466
11467 if (it->hpos == 0
11468 || (new_x == it->last_visible_x
11469 && FRAME_WINDOW_P (it->f)))
11470 {
e6819faf
GM
11471 /* Current glyph is the only one on the line or
11472 fits exactly on the line. We must continue
11473 the line because we can't draw the cursor
11474 after the glyph. */
5f5c8ee5
GM
11475 row->continued_p = 1;
11476 it->current_x = new_x;
11477 it->continuation_lines_width += new_x;
11478 ++it->hpos;
11479 if (i == nglyphs - 1)
11480 set_iterator_to_next (it);
11481 }
3b3c4bf0
GM
11482 else if (CHAR_GLYPH_PADDING_P (*glyph)
11483 && !FRAME_WINDOW_P (it->f))
11484 {
11485 /* A padding glyph that doesn't fit on this line.
11486 This means the whole character doesn't fit
11487 on the line. */
11488 row->used[TEXT_AREA] = n_glyphs_before;
11489
11490 /* Fill the rest of the row with continuation
11491 glyphs like in 20.x. */
11492 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11493 < row->glyphs[1 + TEXT_AREA])
11494 produce_special_glyphs (it, IT_CONTINUATION);
11495
11496 row->continued_p = 1;
11497 it->current_x = x_before;
11498 it->continuation_lines_width += x_before;
11499
11500 /* Restore the height to what it was before the
11501 element not fitting on the line. */
11502 it->max_ascent = ascent;
11503 it->max_descent = descent;
11504 it->max_phys_ascent = phys_ascent;
11505 it->max_phys_descent = phys_descent;
11506 }
5f5c8ee5 11507 else
5936754e 11508 {
5f5c8ee5
GM
11509 /* Display element draws past the right edge of
11510 the window. Restore positions to values
11511 before the element. The next line starts
11512 with current_x before the glyph that could
11513 not be displayed, so that TAB works right. */
11514 row->used[TEXT_AREA] = n_glyphs_before + i;
11515
11516 /* Display continuation glyphs. */
11517 if (!FRAME_WINDOW_P (it->f))
11518 produce_special_glyphs (it, IT_CONTINUATION);
11519 row->continued_p = 1;
11520
11521 it->current_x = x;
11522 it->continuation_lines_width += x;
91004049
GM
11523 if (nglyphs > 1 && i > 0)
11524 {
11525 row->ends_in_middle_of_char_p = 1;
11526 it->starts_in_middle_of_char_p = 1;
11527 }
e6819faf
GM
11528
11529 /* Restore the height to what it was before the
11530 element not fitting on the line. */
11531 it->max_ascent = ascent;
11532 it->max_descent = descent;
11533 it->max_phys_ascent = phys_ascent;
11534 it->max_phys_descent = phys_descent;
5936754e 11535 }
e6819faf 11536
5f5c8ee5
GM
11537 break;
11538 }
11539 else if (new_x > it->first_visible_x)
11540 {
11541 /* Increment number of glyphs actually displayed. */
11542 ++it->hpos;
11543
11544 if (x < it->first_visible_x)
11545 /* Glyph is partially visible, i.e. row starts at
11546 negative X position. */
11547 row->x = x - it->first_visible_x;
11548 }
11549 else
11550 {
11551 /* Glyph is completely off the left margin of the
11552 window. This should not happen because of the
11553 move_it_in_display_line at the start of
11554 this function. */
11555 abort ();
a2889657 11556 }
a2889657 11557 }
5f5c8ee5
GM
11558
11559 row->ascent = max (row->ascent, it->max_ascent);
11560 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11561 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11562 row->phys_height = max (row->phys_height,
11563 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11564
11565 /* End of this display line if row is continued. */
11566 if (row->continued_p)
11567 break;
a2889657 11568 }
a2889657 11569
5f5c8ee5
GM
11570 /* Is this a line end? If yes, we're also done, after making
11571 sure that a non-default face is extended up to the right
11572 margin of the window. */
11573 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 11574 {
5f5c8ee5
GM
11575 int used_before = row->used[TEXT_AREA];
11576
11577 /* Add a space at the end of the line that is used to
11578 display the cursor there. */
11579 append_space (it, 0);
11580
11581 /* Extend the face to the end of the line. */
11582 extend_face_to_end_of_line (it);
11583
11584 /* Make sure we have the position. */
11585 if (used_before == 0)
11586 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11587
11588 /* Consume the line end. This skips over invisible lines. */
11589 set_iterator_to_next (it);
11590 it->continuation_lines_width = 0;
11591 break;
1c9241f5 11592 }
a2889657 11593
5f5c8ee5
GM
11594 /* Proceed with next display element. Note that this skips
11595 over lines invisible because of selective display. */
11596 set_iterator_to_next (it);
b1d1124b 11597
5f5c8ee5
GM
11598 /* If we truncate lines, we are done when the last displayed
11599 glyphs reach past the right margin of the window. */
11600 if (it->truncate_lines_p
11601 && (FRAME_WINDOW_P (it->f)
11602 ? (it->current_x >= it->last_visible_x)
11603 : (it->current_x > it->last_visible_x)))
75d13c64 11604 {
5f5c8ee5
GM
11605 /* Maybe add truncation glyphs. */
11606 if (!FRAME_WINDOW_P (it->f))
11607 {
11608 --it->glyph_row->used[TEXT_AREA];
11609 produce_special_glyphs (it, IT_TRUNCATION);
11610 }
11611
11612 row->truncated_on_right_p = 1;
11613 it->continuation_lines_width = 0;
312246d1 11614 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
11615 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11616 it->hpos = hpos_before;
11617 it->current_x = x_before;
11618 break;
75d13c64 11619 }
a2889657 11620 }
a2889657 11621
5f5c8ee5
GM
11622 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11623 at the left window margin. */
11624 if (it->first_visible_x
11625 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11626 {
11627 if (!FRAME_WINDOW_P (it->f))
11628 insert_left_trunc_glyphs (it);
11629 row->truncated_on_left_p = 1;
11630 }
a2889657 11631
5f5c8ee5
GM
11632 /* If the start of this line is the overlay arrow-position, then
11633 mark this glyph row as the one containing the overlay arrow.
11634 This is clearly a mess with variable size fonts. It would be
11635 better to let it be displayed like cursors under X. */
e24c997d 11636 if (MARKERP (Voverlay_arrow_position)
a2889657 11637 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
11638 && (MATRIX_ROW_START_CHARPOS (row)
11639 == marker_position (Voverlay_arrow_position))
e24c997d 11640 && STRINGP (Voverlay_arrow_string)
a2889657
JB
11641 && ! overlay_arrow_seen)
11642 {
5f5c8ee5
GM
11643 /* Overlay arrow in window redisplay is a bitmap. */
11644 if (!FRAME_WINDOW_P (it->f))
c4628384 11645 {
5f5c8ee5
GM
11646 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11647 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11648 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11649 struct glyph *p = row->glyphs[TEXT_AREA];
11650 struct glyph *p2, *end;
11651
11652 /* Copy the arrow glyphs. */
11653 while (glyph < arrow_end)
11654 *p++ = *glyph++;
11655
11656 /* Throw away padding glyphs. */
11657 p2 = p;
11658 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11659 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11660 ++p2;
11661 if (p2 > p)
212e4f87 11662 {
5f5c8ee5
GM
11663 while (p2 < end)
11664 *p++ = *p2++;
11665 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 11666 }
c4628384 11667 }
5f5c8ee5 11668
a2889657 11669 overlay_arrow_seen = 1;
5f5c8ee5 11670 row->overlay_arrow_p = 1;
a2889657
JB
11671 }
11672
5f5c8ee5
GM
11673 /* Compute pixel dimensions of this line. */
11674 compute_line_metrics (it);
11675
11676 /* Remember the position at which this line ends. */
11677 row->end = it->current;
11678
173cbca8 11679 /* Maybe set the cursor. */
5f5c8ee5
GM
11680 if (it->w->cursor.vpos < 0
11681 && PT >= MATRIX_ROW_START_CHARPOS (row)
173cbca8
GM
11682 && PT <= MATRIX_ROW_END_CHARPOS (row))
11683 {
11684 /* Also see redisplay_window, case cursor movement in unchanged
11685 window. */
11686 if (MATRIX_ROW_END_CHARPOS (row) == PT
11687 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11688 && !row->ends_at_zv_p)
11689 ;
11690 else
11691 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11692 }
5f5c8ee5
GM
11693
11694 /* Highlight trailing whitespace. */
8f897821 11695 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
11696 highlight_trailing_whitespace (it->f, it->glyph_row);
11697
11698 /* Prepare for the next line. This line starts horizontally at (X
11699 HPOS) = (0 0). Vertical positions are incremented. As a
11700 convenience for the caller, IT->glyph_row is set to the next
11701 row to be used. */
11702 it->current_x = it->hpos = 0;
11703 it->current_y += row->height;
11704 ++it->vpos;
11705 ++it->glyph_row;
11706 return row->displays_text_p;
a2889657 11707}
5f5c8ee5
GM
11708
11709
a2889657 11710\f
5f5c8ee5
GM
11711/***********************************************************************
11712 Menu Bar
11713 ***********************************************************************/
11714
11715/* Redisplay the menu bar in the frame for window W.
11716
11717 The menu bar of X frames that don't have X toolkit support is
11718 displayed in a special window W->frame->menu_bar_window.
11719
11720 The menu bar of terminal frames is treated specially as far as
11721 glyph matrices are concerned. Menu bar lines are not part of
11722 windows, so the update is done directly on the frame matrix rows
11723 for the menu bar. */
7ce2c095
RS
11724
11725static void
11726display_menu_bar (w)
11727 struct window *w;
11728{
5f5c8ee5
GM
11729 struct frame *f = XFRAME (WINDOW_FRAME (w));
11730 struct it it;
11731 Lisp_Object items;
8351baf2 11732 int i;
7ce2c095 11733
5f5c8ee5 11734 /* Don't do all this for graphical frames. */
dc937613 11735#ifdef HAVE_NTGUI
d129c4c2
KH
11736 if (!NILP (Vwindow_system))
11737 return;
dc937613 11738#endif
dc937613 11739#ifdef USE_X_TOOLKIT
d3413a53 11740 if (FRAME_X_P (f))
7ce2c095 11741 return;
5f5c8ee5
GM
11742#endif
11743
11744#ifdef USE_X_TOOLKIT
11745 xassert (!FRAME_WINDOW_P (f));
52377a47 11746 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
11747 it.first_visible_x = 0;
11748 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11749#else /* not USE_X_TOOLKIT */
11750 if (FRAME_WINDOW_P (f))
11751 {
11752 /* Menu bar lines are displayed in the desired matrix of the
11753 dummy window menu_bar_window. */
11754 struct window *menu_w;
11755 xassert (WINDOWP (f->menu_bar_window));
11756 menu_w = XWINDOW (f->menu_bar_window);
11757 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 11758 MENU_FACE_ID);
5f5c8ee5
GM
11759 it.first_visible_x = 0;
11760 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11761 }
11762 else
11763 {
11764 /* This is a TTY frame, i.e. character hpos/vpos are used as
11765 pixel x/y. */
11766 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 11767 MENU_FACE_ID);
5f5c8ee5
GM
11768 it.first_visible_x = 0;
11769 it.last_visible_x = FRAME_WIDTH (f);
11770 }
11771#endif /* not USE_X_TOOLKIT */
11772
11773 /* Clear all rows of the menu bar. */
11774 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11775 {
11776 struct glyph_row *row = it.glyph_row + i;
11777 clear_glyph_row (row);
11778 row->enabled_p = 1;
11779 row->full_width_p = 1;
11780 }
7ce2c095 11781
5f5c8ee5
GM
11782 /* Make the first line of the menu bar appear in reverse video. */
11783 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 11784
5f5c8ee5
GM
11785 /* Display all items of the menu bar. */
11786 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 11787 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 11788 {
5f5c8ee5
GM
11789 Lisp_Object string;
11790
11791 /* Stop at nil string. */
8351baf2
RS
11792 string = XVECTOR (items)->contents[i + 1];
11793 if (NILP (string))
11794 break;
2d66ad19 11795
5f5c8ee5
GM
11796 /* Remember where item was displayed. */
11797 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 11798
5f5c8ee5
GM
11799 /* Display the item, pad with one space. */
11800 if (it.current_x < it.last_visible_x)
11801 display_string (NULL, string, Qnil, 0, 0, &it,
11802 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
11803 }
11804
2d66ad19 11805 /* Fill out the line with spaces. */
5f5c8ee5
GM
11806 if (it.current_x < it.last_visible_x)
11807 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 11808
5f5c8ee5
GM
11809 /* Compute the total height of the lines. */
11810 compute_line_metrics (&it);
7ce2c095 11811}
5f5c8ee5
GM
11812
11813
7ce2c095 11814\f
5f5c8ee5
GM
11815/***********************************************************************
11816 Mode Line
11817 ***********************************************************************/
11818
11819/* Display the mode and/or top line of window W. */
a2889657
JB
11820
11821static void
5f5c8ee5 11822display_mode_lines (w)
a2889657
JB
11823 struct window *w;
11824{
5f5c8ee5 11825 /* These will be set while the mode line specs are processed. */
aa6d10fa 11826 line_number_displayed = 0;
155ef550 11827 w->column_number_displayed = Qnil;
aa6d10fa 11828
5f5c8ee5 11829 if (WINDOW_WANTS_MODELINE_P (w))
045dee35
GM
11830 display_mode_line (w, MODE_LINE_FACE_ID,
11831 current_buffer->mode_line_format);
5f5c8ee5 11832
045dee35
GM
11833 if (WINDOW_WANTS_HEADER_LINE_P (w))
11834 display_mode_line (w, HEADER_LINE_FACE_ID,
11835 current_buffer->header_line_format);
5f5c8ee5 11836}
03b294dc 11837
03b294dc 11838
5f5c8ee5 11839/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 11840 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
5f5c8ee5 11841 FORMAT is the mode line format to display. */
03b294dc 11842
5f5c8ee5
GM
11843static void
11844display_mode_line (w, face_id, format)
11845 struct window *w;
11846 enum face_id face_id;
11847 Lisp_Object format;
11848{
11849 struct it it;
11850 struct face *face;
03b294dc 11851
5f5c8ee5
GM
11852 init_iterator (&it, w, -1, -1, NULL, face_id);
11853 prepare_desired_row (it.glyph_row);
11854
11855 /* Temporarily make frame's keyboard the current kboard so that
11856 kboard-local variables in the mode_line_format will get the right
11857 values. */
11858 push_frame_kboard (it.f);
11859 display_mode_element (&it, 0, 0, 0, format);
11860 pop_frame_kboard ();
a2889657 11861
5f5c8ee5
GM
11862 /* Fill up with spaces. */
11863 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11864
11865 compute_line_metrics (&it);
11866 it.glyph_row->full_width_p = 1;
11867 it.glyph_row->mode_line_p = 1;
11868 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11869 it.glyph_row->continued_p = 0;
11870 it.glyph_row->truncated_on_left_p = 0;
11871 it.glyph_row->truncated_on_right_p = 0;
11872
11873 /* Make a 3D mode-line have a shadow at its right end. */
11874 face = FACE_FROM_ID (it.f, face_id);
11875 extend_face_to_end_of_line (&it);
11876 if (face->box != FACE_NO_BOX)
d7eb09a0 11877 {
5f5c8ee5
GM
11878 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11879 + it.glyph_row->used[TEXT_AREA] - 1);
11880 last->right_box_line_p = 1;
d7eb09a0 11881 }
a2889657
JB
11882}
11883
a2889657 11884
5f5c8ee5
GM
11885/* Contribute ELT to the mode line for window IT->w. How it
11886 translates into text depends on its data type.
a2889657 11887
5f5c8ee5 11888 IT describes the display environment in which we display, as usual.
a2889657
JB
11889
11890 DEPTH is the depth in recursion. It is used to prevent
11891 infinite recursion here.
11892
5f5c8ee5
GM
11893 FIELD_WIDTH is the number of characters the display of ELT should
11894 occupy in the mode line, and PRECISION is the maximum number of
11895 characters to display from ELT's representation. See
11896 display_string for details. *
a2889657 11897
5f5c8ee5 11898 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
11899
11900static int
5f5c8ee5
GM
11901display_mode_element (it, depth, field_width, precision, elt)
11902 struct it *it;
a2889657 11903 int depth;
5f5c8ee5
GM
11904 int field_width, precision;
11905 Lisp_Object elt;
a2889657 11906{
5f5c8ee5
GM
11907 int n = 0, field, prec;
11908
a2889657
JB
11909 tail_recurse:
11910 if (depth > 10)
11911 goto invalid;
11912
11913 depth++;
11914
0220c518 11915 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
11916 {
11917 case Lisp_String:
11918 {
11919 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
11920 unsigned char c;
11921 unsigned char *this = XSTRING (elt)->data;
11922 unsigned char *lisp_string = this;
11923
11924 while ((precision <= 0 || n < precision)
11925 && *this
11926 && (frame_title_ptr
11927 || it->current_x < it->last_visible_x))
a2889657
JB
11928 {
11929 unsigned char *last = this;
5f5c8ee5
GM
11930
11931 /* Advance to end of string or next format specifier. */
a2889657
JB
11932 while ((c = *this++) != '\0' && c != '%')
11933 ;
5f5c8ee5 11934
a2889657
JB
11935 if (this - 1 != last)
11936 {
5f5c8ee5
GM
11937 /* Output to end of string or up to '%'. Field width
11938 is length of string. Don't output more than
11939 PRECISION allows us. */
11940 prec = --this - last;
11941 if (precision > 0 && prec > precision - n)
11942 prec = precision - n;
11943
d39b6696 11944 if (frame_title_ptr)
5f5c8ee5 11945 n += store_frame_title (last, prec, prec);
d39b6696 11946 else
5f5c8ee5
GM
11947 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11948 it, 0, prec, 0, -1);
a2889657
JB
11949 }
11950 else /* c == '%' */
11951 {
5f5c8ee5
GM
11952 unsigned char *percent_position = this;
11953
11954 /* Get the specified minimum width. Zero means
11955 don't pad. */
11956 field = 0;
a2889657 11957 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 11958 field = field * 10 + c - '0';
a2889657 11959
5f5c8ee5
GM
11960 /* Don't pad beyond the total padding allowed. */
11961 if (field_width - n > 0 && field > field_width - n)
11962 field = field_width - n;
a2889657 11963
5f5c8ee5
GM
11964 /* Note that either PRECISION <= 0 or N < PRECISION. */
11965 prec = precision - n;
11966
a2889657 11967 if (c == 'M')
5f5c8ee5
GM
11968 n += display_mode_element (it, depth, field, prec,
11969 Vglobal_mode_string);
a2889657 11970 else if (c != 0)
d39b6696 11971 {
5f5c8ee5
GM
11972 unsigned char *spec
11973 = decode_mode_spec (it->w, c, field, prec);
11974
d39b6696 11975 if (frame_title_ptr)
5f5c8ee5 11976 n += store_frame_title (spec, field, prec);
d39b6696 11977 else
5f5c8ee5
GM
11978 {
11979 int nglyphs_before
11980 = it->glyph_row->used[TEXT_AREA];
11981 int charpos
11982 = percent_position - XSTRING (elt)->data;
11983 int nwritten
11984 = display_string (spec, Qnil, elt, charpos, 0, it,
11985 field, prec, 0, -1);
11986
11987 /* Assign to the glyphs written above the
11988 string where the `%x' came from, position
11989 of the `%'. */
11990 if (nwritten > 0)
11991 {
11992 struct glyph *glyph
11993 = (it->glyph_row->glyphs[TEXT_AREA]
11994 + nglyphs_before);
11995 int i;
11996
11997 for (i = 0; i < nwritten; ++i)
11998 {
11999 glyph[i].object = elt;
12000 glyph[i].charpos = charpos;
12001 }
12002
12003 n += nwritten;
12004 }
12005 }
d39b6696 12006 }
a2889657
JB
12007 }
12008 }
12009 }
12010 break;
12011
12012 case Lisp_Symbol:
12013 /* A symbol: process the value of the symbol recursively
12014 as if it appeared here directly. Avoid error if symbol void.
12015 Special case: if value of symbol is a string, output the string
12016 literally. */
12017 {
12018 register Lisp_Object tem;
12019 tem = Fboundp (elt);
265a9e55 12020 if (!NILP (tem))
a2889657
JB
12021 {
12022 tem = Fsymbol_value (elt);
12023 /* If value is a string, output that string literally:
12024 don't check for % within it. */
e24c997d 12025 if (STRINGP (tem))
d39b6696 12026 {
5f5c8ee5
GM
12027 prec = XSTRING (tem)->size;
12028 if (precision > 0 && prec > precision - n)
12029 prec = precision - n;
d39b6696 12030 if (frame_title_ptr)
5f5c8ee5 12031 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 12032 else
5f5c8ee5
GM
12033 n += display_string (NULL, tem, Qnil, 0, 0, it,
12034 0, prec, 0, -1);
d39b6696 12035 }
a2889657 12036 else if (!EQ (tem, elt))
5f5c8ee5
GM
12037 {
12038 /* Give up right away for nil or t. */
12039 elt = tem;
12040 goto tail_recurse;
12041 }
a2889657
JB
12042 }
12043 }
12044 break;
12045
12046 case Lisp_Cons:
12047 {
12048 register Lisp_Object car, tem;
12049
12050 /* A cons cell: three distinct cases.
12051 If first element is a string or a cons, process all the elements
12052 and effectively concatenate them.
12053 If first element is a negative number, truncate displaying cdr to
12054 at most that many characters. If positive, pad (with spaces)
12055 to at least that many characters.
12056 If first element is a symbol, process the cadr or caddr recursively
12057 according to whether the symbol's value is non-nil or nil. */
9472f927 12058 car = XCAR (elt);
5f5c8ee5
GM
12059 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12060 {
12061 /* An element of the form (:eval FORM) means evaluate FORM
12062 and use the result as mode line elements. */
12063 struct gcpro gcpro1;
12064 Lisp_Object spec;
12065
12066 spec = eval_form (XCAR (XCDR (elt)));
12067 GCPRO1 (spec);
12068 n += display_mode_element (it, depth, field_width - n,
12069 precision - n, spec);
12070 UNGCPRO;
12071 }
12072 else if (SYMBOLP (car))
a2889657
JB
12073 {
12074 tem = Fboundp (car);
9472f927 12075 elt = XCDR (elt);
e24c997d 12076 if (!CONSP (elt))
a2889657
JB
12077 goto invalid;
12078 /* elt is now the cdr, and we know it is a cons cell.
12079 Use its car if CAR has a non-nil value. */
265a9e55 12080 if (!NILP (tem))
a2889657
JB
12081 {
12082 tem = Fsymbol_value (car);
265a9e55 12083 if (!NILP (tem))
9472f927
GM
12084 {
12085 elt = XCAR (elt);
12086 goto tail_recurse;
12087 }
a2889657
JB
12088 }
12089 /* Symbol's value is nil (or symbol is unbound)
12090 Get the cddr of the original list
12091 and if possible find the caddr and use that. */
9472f927 12092 elt = XCDR (elt);
265a9e55 12093 if (NILP (elt))
a2889657 12094 break;
e24c997d 12095 else if (!CONSP (elt))
a2889657 12096 goto invalid;
9472f927 12097 elt = XCAR (elt);
a2889657
JB
12098 goto tail_recurse;
12099 }
e24c997d 12100 else if (INTEGERP (car))
a2889657
JB
12101 {
12102 register int lim = XINT (car);
9472f927 12103 elt = XCDR (elt);
a2889657 12104 if (lim < 0)
5f5c8ee5
GM
12105 {
12106 /* Negative int means reduce maximum width. */
12107 if (precision <= 0)
12108 precision = -lim;
12109 else
12110 precision = min (precision, -lim);
12111 }
a2889657
JB
12112 else if (lim > 0)
12113 {
12114 /* Padding specified. Don't let it be more than
12115 current maximum. */
5f5c8ee5
GM
12116 if (precision > 0)
12117 lim = min (precision, lim);
12118
a2889657
JB
12119 /* If that's more padding than already wanted, queue it.
12120 But don't reduce padding already specified even if
12121 that is beyond the current truncation point. */
5f5c8ee5 12122 field_width = max (lim, field_width);
a2889657
JB
12123 }
12124 goto tail_recurse;
12125 }
e24c997d 12126 else if (STRINGP (car) || CONSP (car))
a2889657
JB
12127 {
12128 register int limit = 50;
5f5c8ee5
GM
12129 /* Limit is to protect against circular lists. */
12130 while (CONSP (elt)
12131 && --limit > 0
12132 && (precision <= 0 || n < precision))
a2889657 12133 {
5f5c8ee5 12134 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
12135 precision - n, XCAR (elt));
12136 elt = XCDR (elt);
a2889657
JB
12137 }
12138 }
12139 }
12140 break;
12141
12142 default:
12143 invalid:
d39b6696 12144 if (frame_title_ptr)
5f5c8ee5 12145 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 12146 else
5f5c8ee5
GM
12147 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12148 precision - n, 0, 0);
12149 return n;
a2889657
JB
12150 }
12151
5f5c8ee5
GM
12152 /* Pad to FIELD_WIDTH. */
12153 if (field_width > 0 && n < field_width)
12154 {
12155 if (frame_title_ptr)
12156 n += store_frame_title ("", field_width - n, 0);
12157 else
12158 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12159 0, 0, 0);
12160 }
12161
12162 return n;
a2889657 12163}
5f5c8ee5
GM
12164
12165
766525bc
RS
12166/* Write a null-terminated, right justified decimal representation of
12167 the positive integer D to BUF using a minimal field width WIDTH. */
12168
12169static void
12170pint2str (buf, width, d)
12171 register char *buf;
12172 register int width;
12173 register int d;
12174{
12175 register char *p = buf;
12176
12177 if (d <= 0)
5f5c8ee5 12178 *p++ = '0';
766525bc 12179 else
5f5c8ee5 12180 {
766525bc 12181 while (d > 0)
5f5c8ee5 12182 {
766525bc
RS
12183 *p++ = d % 10 + '0';
12184 d /= 10;
5f5c8ee5
GM
12185 }
12186 }
12187
12188 for (width -= (int) (p - buf); width > 0; --width)
12189 *p++ = ' ';
766525bc
RS
12190 *p-- = '\0';
12191 while (p > buf)
5f5c8ee5 12192 {
766525bc
RS
12193 d = *buf;
12194 *buf++ = *p;
12195 *p-- = d;
5f5c8ee5 12196 }
766525bc
RS
12197}
12198
5f5c8ee5 12199/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
12200 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12201 type of CODING_SYSTEM. Return updated pointer into BUF. */
12202
6693a99a 12203static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 12204
1c9241f5
KH
12205static char *
12206decode_mode_spec_coding (coding_system, buf, eol_flag)
12207 Lisp_Object coding_system;
12208 register char *buf;
12209 int eol_flag;
12210{
1e1078d6 12211 Lisp_Object val;
916848d8 12212 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
12213 unsigned char *eol_str;
12214 int eol_str_len;
12215 /* The EOL conversion we are using. */
12216 Lisp_Object eoltype;
1e1078d6 12217
4a09dee0 12218 val = Fget (coding_system, Qcoding_system);
1c9241f5 12219
4a09dee0 12220 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 12221 {
916848d8
RS
12222 if (multibyte)
12223 *buf++ = '-';
21e989e3 12224 if (eol_flag)
302f2b38 12225 eoltype = eol_mnemonic_undecided;
1e1078d6 12226 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
12227 }
12228 else
12229 {
1e1078d6
RS
12230 Lisp_Object eolvalue;
12231
12232 eolvalue = Fget (coding_system, Qeol_type);
12233
916848d8
RS
12234 if (multibyte)
12235 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12236
1c9241f5
KH
12237 if (eol_flag)
12238 {
1e1078d6
RS
12239 /* The EOL conversion that is normal on this system. */
12240
12241 if (NILP (eolvalue)) /* Not yet decided. */
12242 eoltype = eol_mnemonic_undecided;
12243 else if (VECTORP (eolvalue)) /* Not yet decided. */
12244 eoltype = eol_mnemonic_undecided;
12245 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12246 eoltype = (XFASTINT (eolvalue) == 0
12247 ? eol_mnemonic_unix
12248 : (XFASTINT (eolvalue) == 1
12249 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
12250 }
12251 }
5f5c8ee5 12252
302f2b38
EZ
12253 if (eol_flag)
12254 {
12255 /* Mention the EOL conversion if it is not the usual one. */
12256 if (STRINGP (eoltype))
12257 {
12258 eol_str = XSTRING (eoltype)->data;
12259 eol_str_len = XSTRING (eoltype)->size;
12260 }
f30b3499
KH
12261 else if (INTEGERP (eoltype)
12262 && CHAR_VALID_P (XINT (eoltype), 0))
12263 {
4a09dee0 12264 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 12265 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 12266 }
302f2b38
EZ
12267 else
12268 {
12269 eol_str = invalid_eol_type;
12270 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 12271 }
f30b3499 12272 bcopy (eol_str, buf, eol_str_len);
302f2b38 12273 buf += eol_str_len;
1c9241f5 12274 }
302f2b38 12275
1c9241f5
KH
12276 return buf;
12277}
12278
a2889657 12279/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
12280 generated by character C. PRECISION >= 0 means don't return a
12281 string longer than that value. FIELD_WIDTH > 0 means pad the
12282 string returned with spaces to that value. */
a2889657 12283
11e82b76
JB
12284static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12285
a2889657 12286static char *
5f5c8ee5 12287decode_mode_spec (w, c, field_width, precision)
a2889657 12288 struct window *w;
68c45bf0 12289 register int c;
5f5c8ee5 12290 int field_width, precision;
a2889657 12291{
0b67772d 12292 Lisp_Object obj;
5f5c8ee5
GM
12293 struct frame *f = XFRAME (WINDOW_FRAME (w));
12294 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 12295 struct buffer *b = XBUFFER (w->buffer);
a2889657 12296
0b67772d 12297 obj = Qnil;
a2889657
JB
12298
12299 switch (c)
12300 {
1af9f229
RS
12301 case '*':
12302 if (!NILP (b->read_only))
12303 return "%";
12304 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12305 return "*";
12306 return "-";
12307
12308 case '+':
12309 /* This differs from %* only for a modified read-only buffer. */
12310 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12311 return "*";
12312 if (!NILP (b->read_only))
12313 return "%";
12314 return "-";
12315
12316 case '&':
12317 /* This differs from %* in ignoring read-only-ness. */
12318 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12319 return "*";
12320 return "-";
12321
12322 case '%':
12323 return "%";
12324
12325 case '[':
12326 {
12327 int i;
12328 char *p;
12329
12330 if (command_loop_level > 5)
12331 return "[[[... ";
12332 p = decode_mode_spec_buf;
12333 for (i = 0; i < command_loop_level; i++)
12334 *p++ = '[';
12335 *p = 0;
12336 return decode_mode_spec_buf;
12337 }
12338
12339 case ']':
12340 {
12341 int i;
12342 char *p;
12343
12344 if (command_loop_level > 5)
12345 return " ...]]]";
12346 p = decode_mode_spec_buf;
12347 for (i = 0; i < command_loop_level; i++)
12348 *p++ = ']';
12349 *p = 0;
12350 return decode_mode_spec_buf;
12351 }
12352
12353 case '-':
12354 {
1af9f229 12355 register int i;
5f5c8ee5
GM
12356
12357 /* Let lots_of_dashes be a string of infinite length. */
12358 if (field_width <= 0
12359 || field_width > sizeof (lots_of_dashes))
1af9f229 12360 {
5f5c8ee5
GM
12361 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12362 decode_mode_spec_buf[i] = '-';
12363 decode_mode_spec_buf[i] = '\0';
12364 return decode_mode_spec_buf;
1af9f229 12365 }
5f5c8ee5
GM
12366 else
12367 return lots_of_dashes;
1af9f229
RS
12368 }
12369
a2889657 12370 case 'b':
d39b6696 12371 obj = b->name;
a2889657
JB
12372 break;
12373
1af9f229
RS
12374 case 'c':
12375 {
12376 int col = current_column ();
12377 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 12378 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
12379 return decode_mode_spec_buf;
12380 }
12381
12382 case 'F':
12383 /* %F displays the frame name. */
5f5c8ee5 12384 if (!NILP (f->title))
95184b48 12385 return (char *) XSTRING (f->title)->data;
fd8ff63d 12386 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 12387 return (char *) XSTRING (f->name)->data;
9c6da96f 12388 return "Emacs";
1af9f229 12389
a2889657 12390 case 'f':
d39b6696 12391 obj = b->filename;
a2889657
JB
12392 break;
12393
aa6d10fa
RS
12394 case 'l':
12395 {
12adba34
RS
12396 int startpos = XMARKER (w->start)->charpos;
12397 int startpos_byte = marker_byte_position (w->start);
12398 int line, linepos, linepos_byte, topline;
aa6d10fa 12399 int nlines, junk;
aa6d10fa
RS
12400 int height = XFASTINT (w->height);
12401
12402 /* If we decided that this buffer isn't suitable for line numbers,
12403 don't forget that too fast. */
12404 if (EQ (w->base_line_pos, w->buffer))
766525bc 12405 goto no_value;
5300fd39
RS
12406 /* But do forget it, if the window shows a different buffer now. */
12407 else if (BUFFERP (w->base_line_pos))
12408 w->base_line_pos = Qnil;
aa6d10fa
RS
12409
12410 /* If the buffer is very big, don't waste time. */
37d034d3 12411 if (INTEGERP (Vline_number_display_limit)
090703f4 12412 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
12413 {
12414 w->base_line_pos = Qnil;
12415 w->base_line_number = Qnil;
766525bc 12416 goto no_value;
aa6d10fa
RS
12417 }
12418
12419 if (!NILP (w->base_line_number)
12420 && !NILP (w->base_line_pos)
12adba34 12421 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
12422 {
12423 line = XFASTINT (w->base_line_number);
12424 linepos = XFASTINT (w->base_line_pos);
12adba34 12425 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
12426 }
12427 else
12428 {
12429 line = 1;
d39b6696 12430 linepos = BUF_BEGV (b);
12adba34 12431 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
12432 }
12433
12434 /* Count lines from base line to window start position. */
12adba34
RS
12435 nlines = display_count_lines (linepos, linepos_byte,
12436 startpos_byte,
12437 startpos, &junk);
aa6d10fa
RS
12438
12439 topline = nlines + line;
12440
12441 /* Determine a new base line, if the old one is too close
12442 or too far away, or if we did not have one.
12443 "Too close" means it's plausible a scroll-down would
12444 go back past it. */
d39b6696 12445 if (startpos == BUF_BEGV (b))
aa6d10fa 12446 {
c2213350
KH
12447 XSETFASTINT (w->base_line_number, topline);
12448 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
12449 }
12450 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 12451 || linepos == BUF_BEGV (b))
aa6d10fa 12452 {
d39b6696 12453 int limit = BUF_BEGV (b);
12adba34 12454 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 12455 int position;
5d121aec 12456 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
12457
12458 if (startpos - distance > limit)
12adba34
RS
12459 {
12460 limit = startpos - distance;
12461 limit_byte = CHAR_TO_BYTE (limit);
12462 }
aa6d10fa 12463
12adba34
RS
12464 nlines = display_count_lines (startpos, startpos_byte,
12465 limit_byte,
12466 - (height * 2 + 30),
aa6d10fa
RS
12467 &position);
12468 /* If we couldn't find the lines we wanted within
5d121aec 12469 line_number_display_limit_width chars per line,
aa6d10fa 12470 give up on line numbers for this window. */
12adba34 12471 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
12472 {
12473 w->base_line_pos = w->buffer;
12474 w->base_line_number = Qnil;
766525bc 12475 goto no_value;
aa6d10fa
RS
12476 }
12477
c2213350 12478 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 12479 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
12480 }
12481
12482 /* Now count lines from the start pos to point. */
12adba34
RS
12483 nlines = display_count_lines (startpos, startpos_byte,
12484 PT_BYTE, PT, &junk);
aa6d10fa
RS
12485
12486 /* Record that we did display the line number. */
12487 line_number_displayed = 1;
12488
12489 /* Make the string to show. */
5f5c8ee5 12490 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 12491 return decode_mode_spec_buf;
766525bc
RS
12492 no_value:
12493 {
12494 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
12495 int pad = field_width - 2;
12496 while (pad-- > 0)
12497 *p++ = ' ';
12498 *p++ = '?';
b357b9d4
KR
12499 *p++ = '?';
12500 *p = '\0';
766525bc
RS
12501 return decode_mode_spec_buf;
12502 }
aa6d10fa
RS
12503 }
12504 break;
12505
a2889657 12506 case 'm':
d39b6696 12507 obj = b->mode_name;
a2889657
JB
12508 break;
12509
12510 case 'n':
d39b6696 12511 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
12512 return " Narrow";
12513 break;
12514
a2889657
JB
12515 case 'p':
12516 {
12517 int pos = marker_position (w->start);
d39b6696 12518 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 12519
d39b6696 12520 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 12521 {
d39b6696 12522 if (pos <= BUF_BEGV (b))
a2889657
JB
12523 return "All";
12524 else
12525 return "Bottom";
12526 }
d39b6696 12527 else if (pos <= BUF_BEGV (b))
a2889657
JB
12528 return "Top";
12529 else
12530 {
3c7d31b9
RS
12531 if (total > 1000000)
12532 /* Do it differently for a large value, to avoid overflow. */
12533 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12534 else
12535 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
12536 /* We can't normally display a 3-digit number,
12537 so get us a 2-digit number that is close. */
12538 if (total == 100)
12539 total = 99;
12540 sprintf (decode_mode_spec_buf, "%2d%%", total);
12541 return decode_mode_spec_buf;
12542 }
12543 }
12544
8ffcb79f
RS
12545 /* Display percentage of size above the bottom of the screen. */
12546 case 'P':
12547 {
12548 int toppos = marker_position (w->start);
d39b6696
KH
12549 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12550 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 12551
d39b6696 12552 if (botpos >= BUF_ZV (b))
8ffcb79f 12553 {
d39b6696 12554 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12555 return "All";
12556 else
12557 return "Bottom";
12558 }
12559 else
12560 {
3c7d31b9
RS
12561 if (total > 1000000)
12562 /* Do it differently for a large value, to avoid overflow. */
12563 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12564 else
12565 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
12566 /* We can't normally display a 3-digit number,
12567 so get us a 2-digit number that is close. */
12568 if (total == 100)
12569 total = 99;
d39b6696 12570 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12571 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12572 else
12573 sprintf (decode_mode_spec_buf, "%2d%%", total);
12574 return decode_mode_spec_buf;
12575 }
12576 }
12577
1af9f229
RS
12578 case 's':
12579 /* status of process */
12580 obj = Fget_buffer_process (w->buffer);
12581 if (NILP (obj))
12582 return "no process";
12583#ifdef subprocesses
12584 obj = Fsymbol_name (Fprocess_status (obj));
12585#endif
12586 break;
d39b6696 12587
1af9f229
RS
12588 case 't': /* indicate TEXT or BINARY */
12589#ifdef MODE_LINE_BINARY_TEXT
12590 return MODE_LINE_BINARY_TEXT (b);
12591#else
12592 return "T";
12593#endif
1c9241f5
KH
12594
12595 case 'z':
12596 /* coding-system (not including end-of-line format) */
12597 case 'Z':
12598 /* coding-system (including end-of-line type) */
12599 {
12600 int eol_flag = (c == 'Z');
539b4d41 12601 char *p = decode_mode_spec_buf;
1c9241f5 12602
d30e754b 12603 if (! FRAME_WINDOW_P (f))
1c9241f5 12604 {
11c52c4f
RS
12605 /* No need to mention EOL here--the terminal never needs
12606 to do EOL conversion. */
12607 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12608 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 12609 }
f13c925f 12610 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 12611 p, eol_flag);
f13c925f 12612
11c52c4f 12613#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
12614#ifdef subprocesses
12615 obj = Fget_buffer_process (Fcurrent_buffer ());
12616 if (PROCESSP (obj))
12617 {
12618 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12619 p, eol_flag);
12620 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12621 p, eol_flag);
12622 }
12623#endif /* subprocesses */
11c52c4f 12624#endif /* 0 */
1c9241f5
KH
12625 *p = 0;
12626 return decode_mode_spec_buf;
12627 }
a2889657 12628 }
d39b6696 12629
e24c997d 12630 if (STRINGP (obj))
a2889657
JB
12631 return (char *) XSTRING (obj)->data;
12632 else
12633 return "";
12634}
5f5c8ee5
GM
12635
12636
12adba34
RS
12637/* Count up to COUNT lines starting from START / START_BYTE.
12638 But don't go beyond LIMIT_BYTE.
12639 Return the number of lines thus found (always nonnegative).
59b49f63 12640
12adba34 12641 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
12642
12643static int
12adba34
RS
12644display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12645 int start, start_byte, limit_byte, count;
12646 int *byte_pos_ptr;
59b49f63 12647{
59b49f63
RS
12648 register unsigned char *cursor;
12649 unsigned char *base;
12650
12651 register int ceiling;
12652 register unsigned char *ceiling_addr;
12adba34 12653 int orig_count = count;
59b49f63
RS
12654
12655 /* If we are not in selective display mode,
12656 check only for newlines. */
12adba34
RS
12657 int selective_display = (!NILP (current_buffer->selective_display)
12658 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
12659
12660 if (count > 0)
12adba34
RS
12661 {
12662 while (start_byte < limit_byte)
12663 {
12664 ceiling = BUFFER_CEILING_OF (start_byte);
12665 ceiling = min (limit_byte - 1, ceiling);
12666 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12667 base = (cursor = BYTE_POS_ADDR (start_byte));
12668 while (1)
12669 {
12670 if (selective_display)
12671 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12672 ;
12673 else
12674 while (*cursor != '\n' && ++cursor != ceiling_addr)
12675 ;
12676
12677 if (cursor != ceiling_addr)
12678 {
12679 if (--count == 0)
12680 {
12681 start_byte += cursor - base + 1;
12682 *byte_pos_ptr = start_byte;
12683 return orig_count;
12684 }
12685 else
12686 if (++cursor == ceiling_addr)
12687 break;
12688 }
12689 else
12690 break;
12691 }
12692 start_byte += cursor - base;
12693 }
12694 }
59b49f63
RS
12695 else
12696 {
12adba34
RS
12697 while (start_byte > limit_byte)
12698 {
12699 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12700 ceiling = max (limit_byte, ceiling);
12701 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12702 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
12703 while (1)
12704 {
12adba34
RS
12705 if (selective_display)
12706 while (--cursor != ceiling_addr
12707 && *cursor != '\n' && *cursor != 015)
12708 ;
12709 else
12710 while (--cursor != ceiling_addr && *cursor != '\n')
12711 ;
12712
59b49f63
RS
12713 if (cursor != ceiling_addr)
12714 {
12715 if (++count == 0)
12716 {
12adba34
RS
12717 start_byte += cursor - base + 1;
12718 *byte_pos_ptr = start_byte;
12719 /* When scanning backwards, we should
12720 not count the newline posterior to which we stop. */
12721 return - orig_count - 1;
59b49f63
RS
12722 }
12723 }
12724 else
12725 break;
12726 }
12adba34
RS
12727 /* Here we add 1 to compensate for the last decrement
12728 of CURSOR, which took it past the valid range. */
12729 start_byte += cursor - base + 1;
59b49f63
RS
12730 }
12731 }
12732
12adba34 12733 *byte_pos_ptr = limit_byte;
aa6d10fa 12734
12adba34
RS
12735 if (count < 0)
12736 return - orig_count + count;
12737 return orig_count - count;
aa6d10fa 12738
12adba34 12739}
a2889657 12740
a2889657 12741
5f5c8ee5
GM
12742\f
12743/***********************************************************************
12744 Displaying strings
12745 ***********************************************************************/
278feba9 12746
5f5c8ee5 12747/* Display a NUL-terminated string, starting with index START.
a3788d53 12748
5f5c8ee5
GM
12749 If STRING is non-null, display that C string. Otherwise, the Lisp
12750 string LISP_STRING is displayed.
a2889657 12751
5f5c8ee5
GM
12752 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12753 FACE_STRING. Display STRING or LISP_STRING with the face at
12754 FACE_STRING_POS in FACE_STRING:
a2889657 12755
5f5c8ee5
GM
12756 Display the string in the environment given by IT, but use the
12757 standard display table, temporarily.
a3788d53 12758
5f5c8ee5
GM
12759 FIELD_WIDTH is the minimum number of output glyphs to produce.
12760 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12761 with spaces. If STRING has more characters, more than FIELD_WIDTH
12762 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12763
12764 PRECISION is the maximum number of characters to output from
12765 STRING. PRECISION < 0 means don't truncate the string.
a2889657 12766
5f5c8ee5 12767 This is roughly equivalent to printf format specifiers:
a2889657 12768
5f5c8ee5
GM
12769 FIELD_WIDTH PRECISION PRINTF
12770 ----------------------------------------
12771 -1 -1 %s
12772 -1 10 %.10s
12773 10 -1 %10s
12774 20 10 %20.10s
a2889657 12775
5f5c8ee5
GM
12776 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12777 display them, and < 0 means obey the current buffer's value of
12778 enable_multibyte_characters.
278feba9 12779
5f5c8ee5 12780 Value is the number of glyphs produced. */
b1d1124b 12781
5f5c8ee5
GM
12782static int
12783display_string (string, lisp_string, face_string, face_string_pos,
12784 start, it, field_width, precision, max_x, multibyte)
12785 unsigned char *string;
12786 Lisp_Object lisp_string;
68c45bf0
PE
12787 Lisp_Object face_string;
12788 int face_string_pos;
5f5c8ee5
GM
12789 int start;
12790 struct it *it;
12791 int field_width, precision, max_x;
12792 int multibyte;
12793{
12794 int hpos_at_start = it->hpos;
12795 int saved_face_id = it->face_id;
12796 struct glyph_row *row = it->glyph_row;
12797
12798 /* Initialize the iterator IT for iteration over STRING beginning
12799 with index START. We assume that IT may be modified here (which
12800 means that display_line has to do something when displaying a
12801 mini-buffer prompt, which it does). */
12802 reseat_to_string (it, string, lisp_string, start,
12803 precision, field_width, multibyte);
12804
12805 /* If displaying STRING, set up the face of the iterator
12806 from LISP_STRING, if that's given. */
12807 if (STRINGP (face_string))
12808 {
12809 int endptr;
12810 struct face *face;
12811
12812 it->face_id
12813 = face_at_string_position (it->w, face_string, face_string_pos,
12814 0, it->region_beg_charpos,
12815 it->region_end_charpos,
12816 &endptr, it->base_face_id);
12817 face = FACE_FROM_ID (it->f, it->face_id);
12818 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 12819 }
a2889657 12820
5f5c8ee5
GM
12821 /* Set max_x to the maximum allowed X position. Don't let it go
12822 beyond the right edge of the window. */
12823 if (max_x <= 0)
12824 max_x = it->last_visible_x;
12825 else
12826 max_x = min (max_x, it->last_visible_x);
efc63ef0 12827
5f5c8ee5
GM
12828 /* Skip over display elements that are not visible. because IT->w is
12829 hscrolled. */
12830 if (it->current_x < it->first_visible_x)
12831 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12832 MOVE_TO_POS | MOVE_TO_X);
a2889657 12833
5f5c8ee5
GM
12834 row->ascent = it->max_ascent;
12835 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12836 row->phys_ascent = it->max_phys_ascent;
12837 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 12838
5f5c8ee5
GM
12839 /* This condition is for the case that we are called with current_x
12840 past last_visible_x. */
12841 while (it->current_x < max_x)
a2889657 12842 {
5f5c8ee5 12843 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 12844
5f5c8ee5
GM
12845 /* Get the next display element. */
12846 if (!get_next_display_element (it))
90adcf20 12847 break;
1c9241f5 12848
5f5c8ee5
GM
12849 /* Produce glyphs. */
12850 x_before = it->current_x;
12851 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12852 PRODUCE_GLYPHS (it);
90adcf20 12853
5f5c8ee5
GM
12854 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12855 i = 0;
12856 x = x_before;
12857 while (i < nglyphs)
a2889657 12858 {
5f5c8ee5
GM
12859 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12860
12861 if (!it->truncate_lines_p
12862 && x + glyph->pixel_width > max_x)
12863 {
12864 /* End of continued line or max_x reached. */
12865 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12866 it->current_x = x;
12867 break;
12868 }
12869 else if (x + glyph->pixel_width > it->first_visible_x)
12870 {
12871 /* Glyph is at least partially visible. */
12872 ++it->hpos;
12873 if (x < it->first_visible_x)
12874 it->glyph_row->x = x - it->first_visible_x;
12875 }
12876 else
a2889657 12877 {
5f5c8ee5
GM
12878 /* Glyph is off the left margin of the display area.
12879 Should not happen. */
12880 abort ();
a2889657 12881 }
5f5c8ee5
GM
12882
12883 row->ascent = max (row->ascent, it->max_ascent);
12884 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12885 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12886 row->phys_height = max (row->phys_height,
12887 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12888 x += glyph->pixel_width;
12889 ++i;
a2889657 12890 }
5f5c8ee5
GM
12891
12892 /* Stop if max_x reached. */
12893 if (i < nglyphs)
12894 break;
12895
12896 /* Stop at line ends. */
12897 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 12898 {
5f5c8ee5
GM
12899 it->continuation_lines_width = 0;
12900 break;
a2889657 12901 }
1c9241f5 12902
5f5c8ee5 12903 set_iterator_to_next (it);
a688bb24 12904
5f5c8ee5
GM
12905 /* Stop if truncating at the right edge. */
12906 if (it->truncate_lines_p
12907 && it->current_x >= it->last_visible_x)
12908 {
12909 /* Add truncation mark, but don't do it if the line is
12910 truncated at a padding space. */
12911 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 12912 {
5f5c8ee5
GM
12913 if (!FRAME_WINDOW_P (it->f))
12914 produce_special_glyphs (it, IT_TRUNCATION);
12915 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 12916 }
5f5c8ee5 12917 break;
1c9241f5 12918 }
a2889657
JB
12919 }
12920
5f5c8ee5
GM
12921 /* Maybe insert a truncation at the left. */
12922 if (it->first_visible_x
12923 && IT_CHARPOS (*it) > 0)
a2889657 12924 {
5f5c8ee5
GM
12925 if (!FRAME_WINDOW_P (it->f))
12926 insert_left_trunc_glyphs (it);
12927 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
12928 }
12929
5f5c8ee5
GM
12930 it->face_id = saved_face_id;
12931
12932 /* Value is number of columns displayed. */
12933 return it->hpos - hpos_at_start;
12934}
a2889657 12935
a2889657 12936
a2889657 12937\f
5f5c8ee5
GM
12938/* This is like a combination of memq and assq. Return 1 if PROPVAL
12939 appears as an element of LIST or as the car of an element of LIST.
12940 If PROPVAL is a list, compare each element against LIST in that
12941 way, and return 1 if any element of PROPVAL is found in LIST.
12942 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12943
12944int
12945invisible_p (propval, list)
12946 register Lisp_Object propval;
12947 Lisp_Object list;
12948{
af460d46 12949 register Lisp_Object tail, proptail;
9472f927 12950 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
12951 {
12952 register Lisp_Object tem;
9472f927 12953 tem = XCAR (tail);
642eefc6
RS
12954 if (EQ (propval, tem))
12955 return 1;
9472f927 12956 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
12957 return 1;
12958 }
af460d46
RS
12959 if (CONSP (propval))
12960 for (proptail = propval; CONSP (proptail);
9472f927 12961 proptail = XCDR (proptail))
af460d46
RS
12962 {
12963 Lisp_Object propelt;
9472f927
GM
12964 propelt = XCAR (proptail);
12965 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
12966 {
12967 register Lisp_Object tem;
9472f927 12968 tem = XCAR (tail);
af460d46
RS
12969 if (EQ (propelt, tem))
12970 return 1;
9472f927 12971 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
af460d46
RS
12972 return 1;
12973 }
12974 }
642eefc6
RS
12975 return 0;
12976}
12977
5f5c8ee5
GM
12978
12979/* Return 1 if PROPVAL appears as the car of an element of LIST and
12980 the cdr of that element is non-nil. If PROPVAL is a list, check
12981 each element of PROPVAL in that way, and the first time some
12982 element is found, return 1 if the cdr of that element is non-nil.
12983 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12984
12985int
12986invisible_ellipsis_p (propval, list)
12987 register Lisp_Object propval;
12988 Lisp_Object list;
12989{
af460d46 12990 register Lisp_Object tail, proptail;
9472f927
GM
12991
12992 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
12993 {
12994 register Lisp_Object tem;
9472f927
GM
12995 tem = XCAR (tail);
12996 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12997 return ! NILP (XCDR (tem));
642eefc6 12998 }
9472f927 12999
af460d46 13000 if (CONSP (propval))
9472f927 13001 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
13002 {
13003 Lisp_Object propelt;
9472f927
GM
13004 propelt = XCAR (proptail);
13005 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
13006 {
13007 register Lisp_Object tem;
9472f927
GM
13008 tem = XCAR (tail);
13009 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13010 return ! NILP (XCDR (tem));
af460d46
RS
13011 }
13012 }
9472f927 13013
642eefc6
RS
13014 return 0;
13015}
5f5c8ee5
GM
13016
13017
642eefc6 13018\f
5f5c8ee5
GM
13019/***********************************************************************
13020 Initialization
13021 ***********************************************************************/
13022
a2889657
JB
13023void
13024syms_of_xdisp ()
13025{
c6e89d6c
GM
13026 Vwith_echo_area_save_vector = Qnil;
13027 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 13028
c6e89d6c
GM
13029 Vmessage_stack = Qnil;
13030 staticpro (&Vmessage_stack);
13031
735c094c 13032 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 13033 staticpro (&Qinhibit_redisplay);
735c094c 13034
5f5c8ee5
GM
13035#if GLYPH_DEBUG
13036 defsubr (&Sdump_glyph_matrix);
13037 defsubr (&Sdump_glyph_row);
e037b9ec 13038 defsubr (&Sdump_tool_bar_row);
5f5c8ee5 13039 defsubr (&Strace_redisplay_toggle);
bf9249e3 13040 defsubr (&Strace_to_stderr);
5f5c8ee5
GM
13041#endif
13042
cf074754
RS
13043 staticpro (&Qmenu_bar_update_hook);
13044 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13045
d46fb96a 13046 staticpro (&Qoverriding_terminal_local_map);
7079aefa 13047 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 13048
399164b4
KH
13049 staticpro (&Qoverriding_local_map);
13050 Qoverriding_local_map = intern ("overriding-local-map");
13051
75c43375
RS
13052 staticpro (&Qwindow_scroll_functions);
13053 Qwindow_scroll_functions = intern ("window-scroll-functions");
13054
e0bfbde6
RS
13055 staticpro (&Qredisplay_end_trigger_functions);
13056 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 13057
2e54982e
RS
13058 staticpro (&Qinhibit_point_motion_hooks);
13059 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13060
9499d71b
GM
13061 QCdata = intern (":data");
13062 staticpro (&QCdata);
5f5c8ee5 13063 Qdisplay = intern ("display");
f3751a65 13064 staticpro (&Qdisplay);
5f5c8ee5
GM
13065 Qspace_width = intern ("space-width");
13066 staticpro (&Qspace_width);
5f5c8ee5
GM
13067 Qraise = intern ("raise");
13068 staticpro (&Qraise);
13069 Qspace = intern ("space");
13070 staticpro (&Qspace);
f3751a65
GM
13071 Qmargin = intern ("margin");
13072 staticpro (&Qmargin);
5f5c8ee5 13073 Qleft_margin = intern ("left-margin");
f3751a65 13074 staticpro (&Qleft_margin);
5f5c8ee5 13075 Qright_margin = intern ("right-margin");
f3751a65 13076 staticpro (&Qright_margin);
5f5c8ee5
GM
13077 Qalign_to = intern ("align-to");
13078 staticpro (&Qalign_to);
13079 QCalign_to = intern (":align-to");
13080 staticpro (&QCalign_to);
5f5c8ee5
GM
13081 Qrelative_width = intern ("relative-width");
13082 staticpro (&Qrelative_width);
13083 QCrelative_width = intern (":relative-width");
13084 staticpro (&QCrelative_width);
13085 QCrelative_height = intern (":relative-height");
13086 staticpro (&QCrelative_height);
13087 QCeval = intern (":eval");
13088 staticpro (&QCeval);
d3acf96b 13089 Qwhen = intern ("when");
f3751a65 13090 staticpro (&Qwhen);
886bd6f2
GM
13091 QCfile = intern (":file");
13092 staticpro (&QCfile);
5f5c8ee5
GM
13093 Qfontified = intern ("fontified");
13094 staticpro (&Qfontified);
13095 Qfontification_functions = intern ("fontification-functions");
13096 staticpro (&Qfontification_functions);
5f5c8ee5
GM
13097 Qtrailing_whitespace = intern ("trailing-whitespace");
13098 staticpro (&Qtrailing_whitespace);
13099 Qimage = intern ("image");
13100 staticpro (&Qimage);
ad4f174e
GM
13101 Qmessage_truncate_lines = intern ("message-truncate-lines");
13102 staticpro (&Qmessage_truncate_lines);
5f5c8ee5 13103
a2889657
JB
13104 last_arrow_position = Qnil;
13105 last_arrow_string = Qnil;
f3751a65
GM
13106 staticpro (&last_arrow_position);
13107 staticpro (&last_arrow_string);
c6e89d6c
GM
13108
13109 echo_buffer[0] = echo_buffer[1] = Qnil;
13110 staticpro (&echo_buffer[0]);
13111 staticpro (&echo_buffer[1]);
13112
13113 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13114 staticpro (&echo_area_buffer[0]);
13115 staticpro (&echo_area_buffer[1]);
a2889657 13116
8f897821
GM
13117 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13118 "Non-nil means highlight trailing whitespace.\n\
13119The face used for trailing whitespace is `trailing-whitespace'.");
13120 Vshow_trailing_whitespace = Qnil;
13121
735c094c
KH
13122 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13123 "Non-nil means don't actually do any redisplay.\n\
13124This is used for internal purposes.");
13125 Vinhibit_redisplay = Qnil;
13126
a2889657 13127 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 13128 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
13129 Vglobal_mode_string = Qnil;
13130
13131 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13132 "Marker for where to display an arrow on top of the buffer text.\n\
13133This must be the beginning of a line in order to work.\n\
13134See also `overlay-arrow-string'.");
13135 Voverlay_arrow_position = Qnil;
13136
13137 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13138 "String to display as an arrow. See also `overlay-arrow-position'.");
13139 Voverlay_arrow_string = Qnil;
13140
13141 DEFVAR_INT ("scroll-step", &scroll_step,
13142 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
13143If that fails to bring point back on frame, point is centered instead.\n\
13144If this is zero, point is always centered after it moves off frame.");
a2889657 13145
0789adb2 13146 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
090f7baa
GM
13147 "*Scroll up to this many lines, to bring point back on screen.\n\
13148A value of zero means to scroll the text to center point vertically\n\
13149in the window.");
0789adb2
RS
13150 scroll_conservatively = 0;
13151
9afd2168
RS
13152 DEFVAR_INT ("scroll-margin", &scroll_margin,
13153 "*Number of lines of margin at the top and bottom of a window.\n\
13154Recenter the window whenever point gets within this many lines\n\
13155of the top or bottom of the window.");
13156 scroll_margin = 0;
13157
5f5c8ee5 13158#if GLYPH_DEBUG
a2889657 13159 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 13160#endif
a2889657
JB
13161
13162 DEFVAR_BOOL ("truncate-partial-width-windows",
13163 &truncate_partial_width_windows,
44fa5b1e 13164 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
13165 truncate_partial_width_windows = 1;
13166
13167 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13168 "*Non-nil means use inverse video for the mode line.");
13169 mode_line_inverse_video = 1;
aa6d10fa 13170
090703f4 13171 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
5f5c8ee5 13172 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 13173If the buffer is bigger than this, the line number does not appear\n\
090703f4
GM
13174in the mode line. A value of nil means no limit.");
13175 Vline_number_display_limit = Qnil;
fba9ce76 13176
090703f4
GM
13177 DEFVAR_INT ("line-number-display-limit-width",
13178 &line_number_display_limit_width,
5d121aec
KH
13179 "*Maximum line width (in characters) for line number display.\n\
13180If the average length of the lines near point is bigger than this, then the\n\
13181line number may be omitted from the mode line.");
13182 line_number_display_limit_width = 200;
13183
fba9ce76
RS
13184 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13185 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 13186 highlight_nonselected_windows = 0;
d39b6696
KH
13187
13188 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
13189 "Non-nil if more than one frame is visible on this display.\n\
13190Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
13191This variable is not guaranteed to be accurate except while processing\n\
13192`frame-title-format' and `icon-title-format'.");
d39b6696
KH
13193
13194 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 13195 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
13196\(Assuming the window manager supports this feature.)\n\
13197This variable has the same structure as `mode-line-format' (which see),\n\
13198and is used only on frames for which no explicit name has been set\n\
13199\(see `modify-frame-parameters').");
13200 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 13201 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
13202\(Assuming the window manager supports this feature.)\n\
13203This variable has the same structure as `mode-line-format' (which see),\n\
13204and is used only on frames for which no explicit name has been set\n\
13205\(see `modify-frame-parameters').");
13206 Vicon_title_format
13207 = Vframe_title_format
13208 = Fcons (intern ("multiple-frames"),
13209 Fcons (build_string ("%b"),
13210 Fcons (Fcons (build_string (""),
13211 Fcons (intern ("invocation-name"),
13212 Fcons (build_string ("@"),
13213 Fcons (intern ("system-name"),
13214 Qnil)))),
13215 Qnil)));
5992c4f7
KH
13216
13217 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13218 "Maximum number of lines to keep in the message log buffer.\n\
13219If nil, disable message logging. If t, log messages but don't truncate\n\
13220the buffer when it becomes large.");
13221 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
13222
13223 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13224 "Functions called before redisplay, if window sizes have changed.\n\
13225The value should be a list of functions that take one argument.\n\
13226Just before redisplay, for each frame, if any of its windows have changed\n\
13227size since the last redisplay, or have been split or deleted,\n\
13228all the functions in the list are called, with the frame as argument.");
13229 Vwindow_size_change_functions = Qnil;
75c43375
RS
13230
13231 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 13232 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 13233Each function is called with two arguments, the window\n\
8d9583b0
RS
13234and its new display-start position. Note that the value of `window-end'\n\
13235is not valid when these functions are called.");
75c43375 13236 Vwindow_scroll_functions = Qnil;
5f5c8ee5 13237
e037b9ec
GM
13238 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13239 "*Non-nil means automatically resize tool-bars.\n\
13240This increases a tool-bar's height if not all tool-bar items are visible.\n\
13241It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 13242otherwise.");
e037b9ec 13243 auto_resize_tool_bars_p = 1;
5f5c8ee5 13244
e037b9ec
GM
13245 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13246 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13247 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 13248
e037b9ec
GM
13249 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13250 "*Margin around tool-bar buttons in pixels.");
13251 tool_bar_button_margin = 1;
5f5c8ee5 13252
e037b9ec
GM
13253 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13254 "Relief thickness of tool-bar buttons.");
13255 tool_bar_button_relief = 3;
5f5c8ee5
GM
13256
13257 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13258 "List of functions to call to fontify regions of text.\n\
13259Each function is called with one argument POS. Functions must\n\
13260fontify a region starting at POS in the current buffer, and give\n\
13261fontified regions the property `fontified'.\n\
13262This variable automatically becomes buffer-local when set.");
13263 Vfontification_functions = Qnil;
13264 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
13265
13266 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
13267 &unibyte_display_via_language_environment,
13268 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
13269Specifically this means that unibyte non-ASCII characters\n\
13270are displayed by converting them to the equivalent multibyte characters\n\
13271according to the current language environment. As a result, they are\n\
13272displayed according to the current fontset.");
13273 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
13274
13275 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13276 "*Maximum height for resizing mini-windows.\n\
13277If a float, it specifies a fraction of the mini-window frame's height.\n\
97cafc0f
GM
13278If an integer, it specifies a number of lines.\n\
13279If nil, don't resize.");
c6e89d6c 13280 Vmax_mini_window_height = make_float (0.25);
d6d26ed3
GM
13281
13282 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13283 &cursor_in_non_selected_windows,
13284 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13285Nil means don't display a cursor there.");
13286 cursor_in_non_selected_windows = 1;
d475bcb8
GM
13287
13288 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13289 "*Non-nil means scroll the display automatically to make point visible.");
13290 automatic_hscrolling_p = 1;
e00daaa0
GM
13291
13292 DEFVAR_LISP ("image-types", &Vimage_types,
ad4f174e 13293 "List of supported image types.\n\
e00daaa0
GM
13294Each element of the list is a symbol for a supported image type.");
13295 Vimage_types = Qnil;
ad4f174e
GM
13296
13297 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13298 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13299Bind this around calls to `message' to let it take effect.");
13300 message_truncate_lines = 0;
a2889657
JB
13301}
13302
5f5c8ee5
GM
13303
13304/* Initialize this module when Emacs starts. */
13305
dfcf069d 13306void
a2889657
JB
13307init_xdisp ()
13308{
13309 Lisp_Object root_window;
5f5c8ee5 13310 struct window *mini_w;
a2889657 13311
5f5c8ee5 13312 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
13313
13314 mini_w = XWINDOW (minibuf_window);
11e82b76 13315 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 13316
a2889657
JB
13317 if (!noninteractive)
13318 {
5f5c8ee5
GM
13319 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13320 int i;
13321
13322 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 13323 set_window_height (root_window,
5f5c8ee5 13324 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 13325 0);
c2213350 13326 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
13327 set_window_height (minibuf_window, 1, 0);
13328
c2213350
KH
13329 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13330 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
13331
13332 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13333 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13334 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13335
13336 /* The default ellipsis glyphs `...'. */
13337 for (i = 0; i < 3; ++i)
13338 XSETFASTINT (default_invis_vector[i], '.');
a2889657 13339 }
5f5c8ee5
GM
13340
13341#ifdef HAVE_WINDOW_SYSTEM
13342 {
13343 /* Allocate the buffer for frame titles. */
13344 int size = 100;
13345 frame_title_buf = (char *) xmalloc (size);
13346 frame_title_buf_end = frame_title_buf + size;
13347 frame_title_ptr = NULL;
13348 }
13349#endif /* HAVE_WINDOW_SYSTEM */
a2889657 13350}
5f5c8ee5
GM
13351
13352