(window_loop) <GET_BUFFER_WINDOW>: Prefer to return
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
098ec84a 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
5f5c8ee5 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"
546a4f00 173#include "keyboard.h"
44fa5b1e 174#include "frame.h"
a2889657
JB
175#include "window.h"
176#include "termchar.h"
177#include "dispextern.h"
178#include "buffer.h"
1c9241f5 179#include "charset.h"
a2889657
JB
180#include "indent.h"
181#include "commands.h"
182#include "macros.h"
183#include "disptab.h"
30c566e4 184#include "termhooks.h"
b0a0fbda 185#include "intervals.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
1a578e9b
AC
197#ifdef macintosh
198#include "macterm.h"
199#endif
a2889657 200
5f5c8ee5
GM
201#define min(a, b) ((a) < (b) ? (a) : (b))
202#define max(a, b) ((a) > (b) ? (a) : (b))
203
204#define INFINITY 10000000
205
1a578e9b 206#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
2e621225 207extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 208extern int pending_menu_activation;
76412d64
RS
209#endif
210
a2889657
JB
211extern int interrupt_input;
212extern int command_loop_level;
213
b6436d4e
RS
214extern int minibuffer_auto_raise;
215
c4628384
RS
216extern Lisp_Object Qface;
217
399164b4
KH
218extern Lisp_Object Voverriding_local_map;
219extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 220extern Lisp_Object Qmenu_item;
399164b4 221
d46fb96a 222Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 223Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 224Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 225Lisp_Object Qinhibit_point_motion_hooks;
9499d71b 226Lisp_Object QCeval, Qwhen, QCfile, QCdata;
5f5c8ee5 227Lisp_Object Qfontified;
6422c1d7 228Lisp_Object Qgrow_only;
5f5c8ee5
GM
229
230/* Functions called to fontify regions of text. */
231
232Lisp_Object Vfontification_functions;
233Lisp_Object Qfontification_functions;
234
e037b9ec 235/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
236 over them. */
237
e037b9ec 238int auto_raise_tool_bar_buttons_p;
5f5c8ee5 239
e037b9ec 240/* Margin around tool bar buttons in pixels. */
5f5c8ee5 241
35a41507 242Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 243
e037b9ec 244/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 245
e037b9ec 246int tool_bar_button_relief;
5f5c8ee5 247
e037b9ec 248/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
249 items are visible, and no blank lines remain. */
250
e037b9ec 251int auto_resize_tool_bars_p;
399164b4 252
735c094c
KH
253/* Non-nil means don't actually do any redisplay. */
254
255Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
5f5c8ee5
GM
257/* Names of text properties relevant for redisplay. */
258
a7e27ef7
DL
259Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
5f5c8ee5
GM
261
262/* Symbols used in text property values. */
263
264Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 265Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 266Lisp_Object Qmargin;
a7e27ef7 267extern Lisp_Object Qheight;
5f5c8ee5 268
8f897821 269/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 270
8f897821 271Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
272
273/* Name of the face used to highlight trailing whitespace. */
274
275Lisp_Object Qtrailing_whitespace;
276
277/* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
279
280Lisp_Object Qimage;
281
282/* Non-zero means print newline to stdout before next mini-buffer
283 message. */
a2889657
JB
284
285int noninteractive_need_newline;
286
5f5c8ee5 287/* Non-zero means print newline to message log before next message. */
f88eb0b6 288
3c6595e0 289static int message_log_need_newline;
f88eb0b6 290
5f5c8ee5
GM
291\f
292/* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
a2889657 296
5f5c8ee5 297static struct text_pos this_line_start_pos;
a2889657 298
5f5c8ee5
GM
299/* Number of characters past the end of the line above, including the
300 terminating newline. */
301
302static struct text_pos this_line_end_pos;
303
304/* The vertical positions and the height of this line. */
a2889657 305
a2889657 306static int this_line_vpos;
5f5c8ee5
GM
307static int this_line_y;
308static int this_line_pixel_height;
309
310/* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
312
313static int this_line_start_x;
a2889657 314
5f5c8ee5 315/* Buffer that this_line_.* variables are referring to. */
a2889657 316
a2889657
JB
317static struct buffer *this_line_buffer;
318
5f5c8ee5
GM
319/* Nonzero means truncate lines in all windows less wide than the
320 frame. */
a2889657 321
a2889657
JB
322int truncate_partial_width_windows;
323
7bbe686f 324/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 325
7bbe686f 326int unibyte_display_via_language_environment;
5f5c8ee5
GM
327
328/* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
7bbe686f 331
d39b6696
KH
332int multiple_frames;
333
a2889657
JB
334Lisp_Object Vglobal_mode_string;
335
336/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 337
a2889657
JB
338Lisp_Object Voverlay_arrow_position;
339
5f5c8ee5
GM
340/* String to display for the arrow. Only used on terminal frames. */
341
a2889657
JB
342Lisp_Object Voverlay_arrow_string;
343
5f5c8ee5
GM
344/* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
347
d45de95b
RS
348static Lisp_Object last_arrow_position, last_arrow_string;
349
5f5c8ee5
GM
350/* Like mode-line-format, but for the title bar on a visible frame. */
351
d39b6696
KH
352Lisp_Object Vframe_title_format;
353
5f5c8ee5
GM
354/* Like mode-line-format, but for the title bar on an iconified frame. */
355
d39b6696
KH
356Lisp_Object Vicon_title_format;
357
08b610e4
RS
358/* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
5f5c8ee5 361
08b610e4
RS
362static Lisp_Object Vwindow_size_change_functions;
363
0bca8940 364Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 365
a2889657 366/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 367
5f5c8ee5 368static int overlay_arrow_seen;
ca26e1c8 369
fba9ce76 370/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 371
5f5c8ee5
GM
372int highlight_nonselected_windows;
373
374/* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
376
14510fee 377static int scroll_step;
a2889657 378
5f5c8ee5
GM
379/* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
381
0789adb2
RS
382static int scroll_conservatively;
383
5f5c8ee5
GM
384/* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
388
9afd2168
RS
389int scroll_margin;
390
5f5c8ee5
GM
391/* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
a2889657 394
a2889657
JB
395int buffer_shared;
396
5f5c8ee5 397/* Vector containing glyphs for an ellipsis `...'. */
a2889657 398
5f5c8ee5 399static Lisp_Object default_invis_vector[3];
a2889657 400
1862a24e
MB
401/* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
404
405 This variable is deprecated. */
a2889657 406
a2889657
JB
407int mode_line_inverse_video;
408
5f5c8ee5
GM
409/* Prompt to display in front of the mini-buffer contents. */
410
8c5b6a0a 411Lisp_Object minibuf_prompt;
a2889657 412
5f5c8ee5
GM
413/* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
415
a2889657 416int minibuf_prompt_width;
5f5c8ee5
GM
417int minibuf_prompt_pixel_width;
418
5f5c8ee5
GM
419/* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
73af359d
RS
423Lisp_Object echo_area_window;
424
c6e89d6c
GM
425/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430Lisp_Object Vmessage_stack;
431
a3788d53
RS
432/* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
5f5c8ee5 434
a3788d53
RS
435int message_enable_multibyte;
436
5f5c8ee5
GM
437/* True if we should redraw the mode lines on the next redisplay. */
438
a2889657
JB
439int update_mode_lines;
440
5f5c8ee5
GM
441/* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
443
a2889657
JB
444int windows_or_buffers_changed;
445
5f5c8ee5
GM
446/* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
448
aa6d10fa
RS
449int line_number_displayed;
450
451/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 452
090703f4 453Lisp_Object Vline_number_display_limit;
5992c4f7 454
5d121aec
KH
455/* line width to consider when repostioning for line number display */
456
457static int line_number_display_limit_width;
458
5f5c8ee5
GM
459/* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
461
5992c4f7 462Lisp_Object Vmessage_log_max;
d45de95b 463
6a94510a
GM
464/* The name of the *Messages* buffer, a string. */
465
466static Lisp_Object Vmessages_buffer_name;
467
c6e89d6c
GM
468/* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
470
471Lisp_Object echo_area_buffer[2];
472
473/* The buffers referenced from echo_area_buffer. */
474
475static Lisp_Object echo_buffer[2];
476
477/* A vector saved used in with_area_buffer to reduce consing. */
478
479static Lisp_Object Vwith_echo_area_save_vector;
480
481/* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
483
484static int display_last_displayed_message_p;
485
486/* Nonzero if echo area is being used by print; zero if being used by
487 message. */
488
489int message_buf_print;
490
9142dd5b
GM
491/* Maximum height for resizing mini-windows. Either a float
492 specifying a fraction of the available height, or an integer
493 specifying a number of lines. */
c6e89d6c 494
ad4f174e
GM
495Lisp_Object Vmax_mini_window_height;
496
497/* Non-zero means messages should be displayed with truncated
498 lines instead of being continued. */
499
500int message_truncate_lines;
501Lisp_Object Qmessage_truncate_lines;
c6e89d6c 502
d6d26ed3
GM
503/* Non-zero means we want a hollow cursor in windows that are not
504 selected. Zero means there's no cursor in such windows. */
505
506int cursor_in_non_selected_windows;
507
5f5c8ee5
GM
508/* A scratch glyph row with contents used for generating truncation
509 glyphs. Also used in direct_output_for_insert. */
12adba34 510
5f5c8ee5
GM
511#define MAX_SCRATCH_GLYPHS 100
512struct glyph_row scratch_glyph_row;
513static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 514
5f5c8ee5
GM
515/* Ascent and height of the last line processed by move_it_to. */
516
517static int last_max_ascent, last_height;
518
21fdfb65
GM
519/* Non-zero if there's a help-echo in the echo area. */
520
521int help_echo_showing_p;
522
04612a64
GM
523/* If >= 0, computed, exact values of mode-line and header-line height
524 to use in the macros CURRENT_MODE_LINE_HEIGHT and
525 CURRENT_HEADER_LINE_HEIGHT. */
526
527int current_mode_line_height, current_header_line_height;
528
5f5c8ee5
GM
529/* The maximum distance to look ahead for text properties. Values
530 that are too small let us call compute_char_face and similar
531 functions too often which is expensive. Values that are too large
532 let us call compute_char_face and alike too often because we
533 might not be interested in text properties that far away. */
534
535#define TEXT_PROP_DISTANCE_LIMIT 100
536
47589c8c
GM
537#if GLYPH_DEBUG
538
5f5c8ee5
GM
539/* Non-zero means print traces of redisplay if compiled with
540 GLYPH_DEBUG != 0. */
541
5f5c8ee5 542int trace_redisplay_p;
47589c8c 543
546a4f00 544#endif /* GLYPH_DEBUG */
47589c8c 545
546a4f00
AI
546#ifdef DEBUG_TRACE_MOVE
547/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
548int trace_move;
549
47589c8c
GM
550#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
551#else
91c3f500 552#define TRACE_MOVE(x) (void) 0
5f5c8ee5 553#endif
546a4f00 554
d475bcb8
GM
555/* Non-zero means automatically scroll windows horizontally to make
556 point visible. */
557
558int automatic_hscrolling_p;
559
e00daaa0
GM
560/* A list of symbols, one for each supported image type. */
561
562Lisp_Object Vimage_types;
563
6422c1d7 564/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 565 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
566 display. If `grow-only', let mini-windows grow only until they
567 become empty. */
568
569Lisp_Object Vresize_mini_windows;
570
5f5c8ee5
GM
571/* Value returned from text property handlers (see below). */
572
573enum prop_handled
3c6595e0 574{
5f5c8ee5
GM
575 HANDLED_NORMALLY,
576 HANDLED_RECOMPUTE_PROPS,
577 HANDLED_OVERLAY_STRING_CONSUMED,
578 HANDLED_RETURN
579};
3c6595e0 580
5f5c8ee5
GM
581/* A description of text properties that redisplay is interested
582 in. */
3c6595e0 583
5f5c8ee5
GM
584struct props
585{
586 /* The name of the property. */
587 Lisp_Object *name;
90adcf20 588
5f5c8ee5
GM
589 /* A unique index for the property. */
590 enum prop_idx idx;
591
592 /* A handler function called to set up iterator IT from the property
593 at IT's current position. Value is used to steer handle_stop. */
594 enum prop_handled (*handler) P_ ((struct it *it));
595};
596
597static enum prop_handled handle_face_prop P_ ((struct it *));
598static enum prop_handled handle_invisible_prop P_ ((struct it *));
599static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 600static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
601static enum prop_handled handle_overlay_change P_ ((struct it *));
602static enum prop_handled handle_fontified_prop P_ ((struct it *));
603
604/* Properties handled by iterators. */
605
606static struct props it_props[] =
5992c4f7 607{
5f5c8ee5
GM
608 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
609 /* Handle `face' before `display' because some sub-properties of
610 `display' need to know the face. */
611 {&Qface, FACE_PROP_IDX, handle_face_prop},
612 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
613 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 614 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
615 {NULL, 0, NULL}
616};
5992c4f7 617
5f5c8ee5
GM
618/* Value is the position described by X. If X is a marker, value is
619 the marker_position of X. Otherwise, value is X. */
12adba34 620
5f5c8ee5 621#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 622
5f5c8ee5 623/* Enumeration returned by some move_it_.* functions internally. */
12adba34 624
5f5c8ee5
GM
625enum move_it_result
626{
627 /* Not used. Undefined value. */
628 MOVE_UNDEFINED,
bab29e15 629
5f5c8ee5
GM
630 /* Move ended at the requested buffer position or ZV. */
631 MOVE_POS_MATCH_OR_ZV,
bab29e15 632
5f5c8ee5
GM
633 /* Move ended at the requested X pixel position. */
634 MOVE_X_REACHED,
12adba34 635
5f5c8ee5
GM
636 /* Move within a line ended at the end of a line that must be
637 continued. */
638 MOVE_LINE_CONTINUED,
639
640 /* Move within a line ended at the end of a line that would
641 be displayed truncated. */
642 MOVE_LINE_TRUNCATED,
ff6c30e5 643
5f5c8ee5
GM
644 /* Move within a line ended at a line end. */
645 MOVE_NEWLINE_OR_CR
646};
12adba34 647
ff6c30e5 648
5f5c8ee5
GM
649\f
650/* Function prototypes. */
651
cafafe0b 652static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 653static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225
GM
654static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
655static int invisible_text_between_p P_ ((struct it *, int, int));
656static int next_element_from_ellipsis P_ ((struct it *));
657static void pint2str P_ ((char *, int, int));
658static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
659 struct text_pos));
660static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
661static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
662static void store_frame_title_char P_ ((char));
663static int store_frame_title P_ ((unsigned char *, int, int));
664static void x_consider_frame_title P_ ((Lisp_Object));
665static void handle_stop P_ ((struct it *));
666static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 667static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 668static void ensure_echo_area_buffers P_ ((void));
e037b9ec
GM
669static struct glyph_row *row_containing_pos P_ ((struct window *, int,
670 struct glyph_row *,
671 struct glyph_row *));
c6e89d6c
GM
672static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
673static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 674static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
675 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
676 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 677static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
678static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 681static int display_echo_area P_ ((struct window *));
23dd2d97
KR
682static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
683static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 684static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 685static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
686static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
687 struct text_pos));
688static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 689static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
690static void insert_left_trunc_glyphs P_ ((struct it *));
691static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
692static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 693static int append_space P_ ((struct it *, int));
5f5c8ee5
GM
694static void make_cursor_line_fully_visible P_ ((struct window *));
695static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
47589c8c 696static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
697static int trailing_whitespace_p P_ ((int));
698static int message_log_check_duplicate P_ ((int, int, int, int));
699int invisible_p P_ ((Lisp_Object, Lisp_Object));
700int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
701static void push_it P_ ((struct it *));
702static void pop_it P_ ((struct it *));
703static void sync_frame_with_window_matrix_rows P_ ((struct window *));
704static void redisplay_internal P_ ((int));
c6e89d6c 705static int echo_area_display P_ ((int));
5f5c8ee5
GM
706static void redisplay_windows P_ ((Lisp_Object));
707static void redisplay_window P_ ((Lisp_Object, int));
708static void update_menu_bar P_ ((struct frame *, int));
709static int try_window_reusing_current_matrix P_ ((struct window *));
710static int try_window_id P_ ((struct window *));
711static int display_line P_ ((struct it *));
715e84c9 712static int display_mode_lines P_ ((struct window *));
04612a64 713static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
5f5c8ee5 714static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 715static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
716static void display_menu_bar P_ ((struct window *));
717static int display_count_lines P_ ((int, int, int, int, int *));
718static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
719 int, int, struct it *, int, int, int, int));
720static void compute_line_metrics P_ ((struct it *));
721static void run_redisplay_end_trigger_hook P_ ((struct it *));
722static int get_overlay_strings P_ ((struct it *));
723static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
724static void reseat P_ ((struct it *, struct text_pos, int));
725static void reseat_1 P_ ((struct it *, struct text_pos, int));
726static void back_to_previous_visible_line_start P_ ((struct it *));
727static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 728static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
729static int next_element_from_display_vector P_ ((struct it *));
730static int next_element_from_string P_ ((struct it *));
731static int next_element_from_c_string P_ ((struct it *));
732static int next_element_from_buffer P_ ((struct it *));
260a86a0 733static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
734static int next_element_from_image P_ ((struct it *));
735static int next_element_from_stretch P_ ((struct it *));
736static void load_overlay_strings P_ ((struct it *));
737static void init_from_display_pos P_ ((struct it *, struct window *,
738 struct display_pos *));
739static void reseat_to_string P_ ((struct it *, unsigned char *,
740 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
741static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
742 int, int, int));
743void move_it_vertically_backward P_ ((struct it *, int));
744static void init_to_row_start P_ ((struct it *, struct window *,
745 struct glyph_row *));
746static void init_to_row_end P_ ((struct it *, struct window *,
747 struct glyph_row *));
748static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 749static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
750static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
751 Lisp_Object, int));
752static struct text_pos string_pos P_ ((int, Lisp_Object));
753static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
754static int number_of_chars P_ ((unsigned char *, int));
755static void compute_stop_pos P_ ((struct it *));
756static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
757 Lisp_Object));
758static int face_before_or_after_it_pos P_ ((struct it *, int));
759static int next_overlay_change P_ ((int));
760static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
761 Lisp_Object, struct text_pos *));
06a12811 762static int underlying_face_id P_ ((struct it *));
5f5c8ee5
GM
763
764#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
765#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 766
5f5c8ee5 767#ifdef HAVE_WINDOW_SYSTEM
12adba34 768
e037b9ec
GM
769static void update_tool_bar P_ ((struct frame *, int));
770static void build_desired_tool_bar_string P_ ((struct frame *f));
771static int redisplay_tool_bar P_ ((struct frame *));
772static void display_tool_bar_line P_ ((struct it *));
12adba34 773
5f5c8ee5 774#endif /* HAVE_WINDOW_SYSTEM */
12adba34 775
5f5c8ee5
GM
776\f
777/***********************************************************************
778 Window display dimensions
779 ***********************************************************************/
12adba34 780
5f5c8ee5
GM
781/* Return the window-relative maximum y + 1 for glyph rows displaying
782 text in window W. This is the height of W minus the height of a
783 mode line, if any. */
784
785INLINE int
786window_text_bottom_y (w)
787 struct window *w;
788{
789 struct frame *f = XFRAME (w->frame);
790 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 791
5f5c8ee5
GM
792 if (WINDOW_WANTS_MODELINE_P (w))
793 height -= CURRENT_MODE_LINE_HEIGHT (w);
794 return height;
f88eb0b6
KH
795}
796
f82aff7c 797
5f5c8ee5
GM
798/* Return the pixel width of display area AREA of window W. AREA < 0
799 means return the total width of W, not including bitmap areas to
800 the left and right of the window. */
ff6c30e5 801
5f5c8ee5
GM
802INLINE int
803window_box_width (w, area)
804 struct window *w;
805 int area;
806{
807 struct frame *f = XFRAME (w->frame);
808 int width = XFASTINT (w->width);
809
810 if (!w->pseudo_window_p)
ff6c30e5 811 {
050d82d7 812 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
813
814 if (area == TEXT_AREA)
815 {
816 if (INTEGERP (w->left_margin_width))
817 width -= XFASTINT (w->left_margin_width);
818 if (INTEGERP (w->right_margin_width))
819 width -= XFASTINT (w->right_margin_width);
820 }
821 else if (area == LEFT_MARGIN_AREA)
822 width = (INTEGERP (w->left_margin_width)
823 ? XFASTINT (w->left_margin_width) : 0);
824 else if (area == RIGHT_MARGIN_AREA)
825 width = (INTEGERP (w->right_margin_width)
826 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 827 }
5f5c8ee5
GM
828
829 return width * CANON_X_UNIT (f);
ff6c30e5 830}
1adc55de 831
1adc55de 832
5f5c8ee5
GM
833/* Return the pixel height of the display area of window W, not
834 including mode lines of W, if any.. */
f88eb0b6 835
5f5c8ee5
GM
836INLINE int
837window_box_height (w)
838 struct window *w;
f88eb0b6 839{
5f5c8ee5
GM
840 struct frame *f = XFRAME (w->frame);
841 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
842
843 xassert (height >= 0);
5f5c8ee5 844
d9c9e99c
MB
845 /* Note: the code below that determines the mode-line/header-line
846 height is essentially the same as that contained in the macro
847 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
848 the appropriate glyph row has its `mode_line_p' flag set,
849 and if it doesn't, uses estimate_mode_line_height instead. */
850
5f5c8ee5 851 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
852 {
853 struct glyph_row *ml_row
854 = (w->current_matrix && w->current_matrix->rows
855 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
856 : 0);
857 if (ml_row && ml_row->mode_line_p)
858 height -= ml_row->height;
859 else
860 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
861 }
5f5c8ee5 862
045dee35 863 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
864 {
865 struct glyph_row *hl_row
866 = (w->current_matrix && w->current_matrix->rows
867 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
868 : 0);
869 if (hl_row && hl_row->mode_line_p)
870 height -= hl_row->height;
871 else
872 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
873 }
5f5c8ee5
GM
874
875 return height;
5992c4f7
KH
876}
877
878
5f5c8ee5
GM
879/* Return the frame-relative coordinate of the left edge of display
880 area AREA of window W. AREA < 0 means return the left edge of the
881 whole window, to the right of any bitmap area at the left side of
882 W. */
5992c4f7 883
5f5c8ee5
GM
884INLINE int
885window_box_left (w, area)
886 struct window *w;
887 int area;
90adcf20 888{
5f5c8ee5
GM
889 struct frame *f = XFRAME (w->frame);
890 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 891
5f5c8ee5 892 if (!w->pseudo_window_p)
90adcf20 893 {
5f5c8ee5 894 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 895 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
896
897 if (area == TEXT_AREA)
898 x += window_box_width (w, LEFT_MARGIN_AREA);
899 else if (area == RIGHT_MARGIN_AREA)
900 x += (window_box_width (w, LEFT_MARGIN_AREA)
901 + window_box_width (w, TEXT_AREA));
90adcf20 902 }
73af359d 903
5f5c8ee5
GM
904 return x;
905}
90adcf20 906
b6436d4e 907
5f5c8ee5
GM
908/* Return the frame-relative coordinate of the right edge of display
909 area AREA of window W. AREA < 0 means return the left edge of the
910 whole window, to the left of any bitmap area at the right side of
911 W. */
ded34426 912
5f5c8ee5
GM
913INLINE int
914window_box_right (w, area)
915 struct window *w;
916 int area;
917{
918 return window_box_left (w, area) + window_box_width (w, area);
919}
920
921
922/* Get the bounding box of the display area AREA of window W, without
923 mode lines, in frame-relative coordinates. AREA < 0 means the
924 whole window, not including bitmap areas to the left and right of
925 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
926 coordinates of the upper-left corner of the box. Return in
927 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
928
929INLINE void
930window_box (w, area, box_x, box_y, box_width, box_height)
931 struct window *w;
932 int area;
933 int *box_x, *box_y, *box_width, *box_height;
934{
935 struct frame *f = XFRAME (w->frame);
936
937 *box_width = window_box_width (w, area);
938 *box_height = window_box_height (w);
939 *box_x = window_box_left (w, area);
940 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
941 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
942 if (WINDOW_WANTS_HEADER_LINE_P (w))
943 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 944}
1adc55de 945
1adc55de 946
5f5c8ee5
GM
947/* Get the bounding box of the display area AREA of window W, without
948 mode lines. AREA < 0 means the whole window, not including bitmap
949 areas to the left and right of the window. Return in *TOP_LEFT_X
950 and TOP_LEFT_Y the frame-relative pixel coordinates of the
951 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
952 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
953 box. */
ded34426 954
5f5c8ee5
GM
955INLINE void
956window_box_edges (w, area, top_left_x, top_left_y,
957 bottom_right_x, bottom_right_y)
958 struct window *w;
959 int area;
960 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 961{
5f5c8ee5
GM
962 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
963 bottom_right_y);
964 *bottom_right_x += *top_left_x;
965 *bottom_right_y += *top_left_y;
48ae5f0a
KH
966}
967
5f5c8ee5
GM
968
969\f
970/***********************************************************************
971 Utilities
972 ***********************************************************************/
973
0db95684
GM
974/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
975 1 if POS is visible and the line containing POS is fully visible.
976 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
977 and header-lines heights. */
3a641a69
GM
978
979int
04612a64 980pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 981 struct window *w;
04612a64 982 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
983{
984 struct it it;
985 struct text_pos top;
fcab1954
GM
986 int visible_p;
987 struct buffer *old_buffer = NULL;
988
989 if (XBUFFER (w->buffer) != current_buffer)
990 {
991 old_buffer = current_buffer;
992 set_buffer_internal_1 (XBUFFER (w->buffer));
993 }
3a641a69
GM
994
995 *fully = visible_p = 0;
996 SET_TEXT_POS_FROM_MARKER (top, w->start);
04612a64
GM
997
998 /* Compute exact mode line heights, if requested. */
999 if (exact_mode_line_heights_p)
1000 {
1001 if (WINDOW_WANTS_MODELINE_P (w))
1002 current_mode_line_height
1003 = display_mode_line (w, MODE_LINE_FACE_ID,
1004 current_buffer->mode_line_format);
1005
1006 if (WINDOW_WANTS_HEADER_LINE_P (w))
1007 current_header_line_height
1008 = display_mode_line (w, HEADER_LINE_FACE_ID,
1009 current_buffer->header_line_format);
1010 }
3a641a69 1011
04612a64 1012 start_display (&it, w, top);
3a641a69
GM
1013 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1014 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1015
1016 /* Note that we may overshoot because of invisible text. */
1017 if (IT_CHARPOS (it) >= charpos)
3a641a69 1018 {
fcab1954
GM
1019 int line_height, line_bottom_y;
1020 int line_top_y = it.current_y;
1021 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
3a641a69 1022
fcab1954
GM
1023 line_height = it.max_ascent + it.max_descent;
1024 if (line_height == 0)
1025 {
1026 if (last_height)
1027 line_height = last_height;
cac94de6 1028 else if (charpos < ZV)
fcab1954
GM
1029 {
1030 move_it_by_lines (&it, 1, 1);
1031 line_height = (it.max_ascent || it.max_descent
1032 ? it.max_ascent + it.max_descent
1033 : last_height);
1034 }
100b593b
GM
1035 else
1036 {
1037 /* Use the default character height. */
1038 it.what = IT_CHARACTER;
1039 it.c = ' ';
1040 it.len = 1;
1041 PRODUCE_GLYPHS (&it);
1042 line_height = it.ascent + it.descent;
1043 }
fcab1954
GM
1044 }
1045 line_bottom_y = line_top_y + line_height;
1046
1047 if (line_top_y < window_top_y)
1048 visible_p = line_bottom_y > window_top_y;
1049 else if (line_top_y < it.last_visible_y)
1050 {
1051 visible_p = 1;
1052 *fully = line_bottom_y <= it.last_visible_y;
1053 }
3a641a69
GM
1054 }
1055 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1056 {
1057 move_it_by_lines (&it, 1, 0);
1058 if (charpos < IT_CHARPOS (it))
1059 {
1060 visible_p = 1;
1061 *fully = 0;
1062 }
1063 }
fcab1954
GM
1064
1065 if (old_buffer)
1066 set_buffer_internal_1 (old_buffer);
04612a64
GM
1067
1068 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1069 return visible_p;
1070}
1071
1072
4fdb80f2
GM
1073/* Return the next character from STR which is MAXLEN bytes long.
1074 Return in *LEN the length of the character. This is like
1075 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1076 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1077 character. */
1078
1079static INLINE int
7a5b8a93 1080string_char_and_length (str, maxlen, len)
4fdb80f2 1081 unsigned char *str;
7a5b8a93 1082 int maxlen, *len;
4fdb80f2
GM
1083{
1084 int c;
1085
1086 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1087 if (!CHAR_VALID_P (c, 1))
1088 /* We may not change the length here because other places in Emacs
9ab8560d 1089 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1090 characters. */
1091 c = '?';
1092
1093 return c;
1094}
1095
1096
1097
5f5c8ee5
GM
1098/* Given a position POS containing a valid character and byte position
1099 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1100
1101static struct text_pos
1102string_pos_nchars_ahead (pos, string, nchars)
1103 struct text_pos pos;
1104 Lisp_Object string;
1105 int nchars;
0b1005ef 1106{
5f5c8ee5
GM
1107 xassert (STRINGP (string) && nchars >= 0);
1108
1109 if (STRING_MULTIBYTE (string))
1110 {
1111 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1112 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1113 int len;
1114
1115 while (nchars--)
1116 {
4fdb80f2 1117 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1118 p += len, rest -= len;
1119 xassert (rest >= 0);
1120 CHARPOS (pos) += 1;
1121 BYTEPOS (pos) += len;
1122 }
1123 }
1124 else
1125 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1126
1127 return pos;
0a9dc68b
RS
1128}
1129
0a9dc68b 1130
5f5c8ee5
GM
1131/* Value is the text position, i.e. character and byte position,
1132 for character position CHARPOS in STRING. */
1133
1134static INLINE struct text_pos
1135string_pos (charpos, string)
1136 int charpos;
0a9dc68b 1137 Lisp_Object string;
0a9dc68b 1138{
5f5c8ee5
GM
1139 struct text_pos pos;
1140 xassert (STRINGP (string));
1141 xassert (charpos >= 0);
1142 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1143 return pos;
1144}
1145
1146
1147/* Value is a text position, i.e. character and byte position, for
1148 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1149 means recognize multibyte characters. */
1150
1151static struct text_pos
1152c_string_pos (charpos, s, multibyte_p)
1153 int charpos;
1154 unsigned char *s;
1155 int multibyte_p;
1156{
1157 struct text_pos pos;
1158
1159 xassert (s != NULL);
1160 xassert (charpos >= 0);
1161
1162 if (multibyte_p)
0a9dc68b 1163 {
5f5c8ee5
GM
1164 int rest = strlen (s), len;
1165
1166 SET_TEXT_POS (pos, 0, 0);
1167 while (charpos--)
0a9dc68b 1168 {
4fdb80f2 1169 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1170 s += len, rest -= len;
1171 xassert (rest >= 0);
1172 CHARPOS (pos) += 1;
1173 BYTEPOS (pos) += len;
0a9dc68b
RS
1174 }
1175 }
5f5c8ee5
GM
1176 else
1177 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1178
5f5c8ee5
GM
1179 return pos;
1180}
0a9dc68b 1181
0a9dc68b 1182
5f5c8ee5
GM
1183/* Value is the number of characters in C string S. MULTIBYTE_P
1184 non-zero means recognize multibyte characters. */
0a9dc68b 1185
5f5c8ee5
GM
1186static int
1187number_of_chars (s, multibyte_p)
1188 unsigned char *s;
1189 int multibyte_p;
1190{
1191 int nchars;
1192
1193 if (multibyte_p)
1194 {
1195 int rest = strlen (s), len;
1196 unsigned char *p = (unsigned char *) s;
0a9dc68b 1197
5f5c8ee5
GM
1198 for (nchars = 0; rest > 0; ++nchars)
1199 {
4fdb80f2 1200 string_char_and_length (p, rest, &len);
5f5c8ee5 1201 rest -= len, p += len;
0a9dc68b
RS
1202 }
1203 }
5f5c8ee5
GM
1204 else
1205 nchars = strlen (s);
1206
1207 return nchars;
0b1005ef
KH
1208}
1209
5f5c8ee5
GM
1210
1211/* Compute byte position NEWPOS->bytepos corresponding to
1212 NEWPOS->charpos. POS is a known position in string STRING.
1213 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1214
5f5c8ee5
GM
1215static void
1216compute_string_pos (newpos, pos, string)
1217 struct text_pos *newpos, pos;
1218 Lisp_Object string;
76412d64 1219{
5f5c8ee5
GM
1220 xassert (STRINGP (string));
1221 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1222
1223 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1224 *newpos = string_pos_nchars_ahead (pos, string,
1225 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1226 else
1227 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1228}
1229
9c74a0dd 1230
5f5c8ee5
GM
1231\f
1232/***********************************************************************
1233 Lisp form evaluation
1234 ***********************************************************************/
1235
116d6f5c 1236/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1237
1238static Lisp_Object
116d6f5c 1239safe_eval_handler (arg)
5f5c8ee5
GM
1240 Lisp_Object arg;
1241{
0dbf9fd2 1242 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1243 return Qnil;
1244}
1245
1246
1247/* Evaluate SEXPR and return the result, or nil if something went
1248 wrong. */
1249
71e5b1b8 1250Lisp_Object
116d6f5c 1251safe_eval (sexpr)
5f5c8ee5
GM
1252 Lisp_Object sexpr;
1253{
2d27dae2 1254 int count = BINDING_STACK_SIZE ();
0d8b31c0 1255 struct gcpro gcpro1;
5f5c8ee5 1256 Lisp_Object val;
0d8b31c0
GM
1257
1258 GCPRO1 (sexpr);
5f5c8ee5 1259 specbind (Qinhibit_redisplay, Qt);
116d6f5c 1260 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
0d8b31c0
GM
1261 UNGCPRO;
1262 return unbind_to (count, val);
1263}
1264
1265
1266/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1267 Return the result, or nil if something went wrong. */
1268
1269Lisp_Object
116d6f5c 1270safe_call (nargs, args)
0d8b31c0
GM
1271 int nargs;
1272 Lisp_Object *args;
1273{
2d27dae2 1274 int count = BINDING_STACK_SIZE ();
0d8b31c0
GM
1275 Lisp_Object val;
1276 struct gcpro gcpro1;
1277
1278 GCPRO1 (args[0]);
1279 gcpro1.nvars = nargs;
1280 specbind (Qinhibit_redisplay, Qt);
1281 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
116d6f5c 1282 safe_eval_handler);
0d8b31c0 1283 UNGCPRO;
5f5c8ee5
GM
1284 return unbind_to (count, val);
1285}
1286
1287
116d6f5c
GM
1288/* Call function FN with one argument ARG.
1289 Return the result, or nil if something went wrong. */
1290
1291Lisp_Object
1292safe_call1 (fn, arg)
1293 Lisp_Object fn, arg;
1294{
1295 Lisp_Object args[2];
1296 args[0] = fn;
1297 args[1] = arg;
1298 return safe_call (2, args);
1299}
1300
1301
5f5c8ee5
GM
1302\f
1303/***********************************************************************
1304 Debugging
1305 ***********************************************************************/
1306
1307#if 0
1308
1309/* Define CHECK_IT to perform sanity checks on iterators.
1310 This is for debugging. It is too slow to do unconditionally. */
1311
1312static void
1313check_it (it)
1314 struct it *it;
1315{
1316 if (it->method == next_element_from_string)
a2889657 1317 {
5f5c8ee5
GM
1318 xassert (STRINGP (it->string));
1319 xassert (IT_STRING_CHARPOS (*it) >= 0);
1320 }
1321 else if (it->method == next_element_from_buffer)
1322 {
1323 /* Check that character and byte positions agree. */
1324 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1325 }
73af359d 1326
5f5c8ee5
GM
1327 if (it->dpvec)
1328 xassert (it->current.dpvec_index >= 0);
1329 else
1330 xassert (it->current.dpvec_index < 0);
1331}
1f40cad2 1332
5f5c8ee5
GM
1333#define CHECK_IT(IT) check_it ((IT))
1334
1335#else /* not 0 */
1336
1337#define CHECK_IT(IT) (void) 0
1338
1339#endif /* not 0 */
1340
1341
1342#if GLYPH_DEBUG
1343
1344/* Check that the window end of window W is what we expect it
1345 to be---the last row in the current matrix displaying text. */
1346
1347static void
1348check_window_end (w)
1349 struct window *w;
1350{
1351 if (!MINI_WINDOW_P (w)
1352 && !NILP (w->window_end_valid))
1353 {
1354 struct glyph_row *row;
1355 xassert ((row = MATRIX_ROW (w->current_matrix,
1356 XFASTINT (w->window_end_vpos)),
1357 !row->enabled_p
1358 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1359 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1360 }
1361}
1362
1363#define CHECK_WINDOW_END(W) check_window_end ((W))
1364
1365#else /* not GLYPH_DEBUG */
1366
1367#define CHECK_WINDOW_END(W) (void) 0
1368
1369#endif /* not GLYPH_DEBUG */
1370
1371
1372\f
1373/***********************************************************************
1374 Iterator initialization
1375 ***********************************************************************/
1376
1377/* Initialize IT for displaying current_buffer in window W, starting
1378 at character position CHARPOS. CHARPOS < 0 means that no buffer
1379 position is specified which is useful when the iterator is assigned
1380 a position later. BYTEPOS is the byte position corresponding to
1381 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1382
1383 If ROW is not null, calls to produce_glyphs with IT as parameter
1384 will produce glyphs in that row.
1385
1386 BASE_FACE_ID is the id of a base face to use. It must be one of
1387 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
045dee35 1388 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1389 displaying the tool-bar.
5f5c8ee5
GM
1390
1391 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1392 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
1393 corresponding mode line glyph row of the desired matrix of W. */
1394
1395void
1396init_iterator (it, w, charpos, bytepos, row, base_face_id)
1397 struct it *it;
1398 struct window *w;
1399 int charpos, bytepos;
1400 struct glyph_row *row;
1401 enum face_id base_face_id;
1402{
1403 int highlight_region_p;
5f5c8ee5
GM
1404
1405 /* Some precondition checks. */
1406 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1407 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1408
1409 /* If face attributes have been changed since the last redisplay,
1410 free realized faces now because they depend on face definitions
1411 that might have changed. */
1412 if (face_change_count)
1413 {
1414 face_change_count = 0;
1415 free_all_realized_faces (Qnil);
1416 }
1417
1418 /* Use one of the mode line rows of W's desired matrix if
1419 appropriate. */
1420 if (row == NULL)
1421 {
1422 if (base_face_id == MODE_LINE_FACE_ID)
1423 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1424 else if (base_face_id == HEADER_LINE_FACE_ID)
1425 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1426 }
1427
1428 /* Clear IT. */
1429 bzero (it, sizeof *it);
1430 it->current.overlay_string_index = -1;
1431 it->current.dpvec_index = -1;
5f5c8ee5
GM
1432 it->base_face_id = base_face_id;
1433
1434 /* The window in which we iterate over current_buffer: */
1435 XSETWINDOW (it->window, w);
1436 it->w = w;
1437 it->f = XFRAME (w->frame);
1438
d475bcb8
GM
1439 /* Extra space between lines (on window systems only). */
1440 if (base_face_id == DEFAULT_FACE_ID
1441 && FRAME_WINDOW_P (it->f))
1442 {
1443 if (NATNUMP (current_buffer->extra_line_spacing))
1444 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1445 else if (it->f->extra_line_spacing > 0)
1446 it->extra_line_spacing = it->f->extra_line_spacing;
1447 }
1448
5f5c8ee5 1449 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1450 attribute changes of named faces, recompute them. When running
1451 in batch mode, the face cache of Vterminal_frame is null. If
1452 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1453 if (
1454#ifndef WINDOWSNT
1455 noninteractive &&
1456#endif
1457 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1458 init_frame_faces (it->f);
5f5c8ee5
GM
1459 if (FRAME_FACE_CACHE (it->f)->used == 0)
1460 recompute_basic_faces (it->f);
1461
5f5c8ee5
GM
1462 /* Current value of the `space-width', and 'height' properties. */
1463 it->space_width = Qnil;
1464 it->font_height = Qnil;
1465
1466 /* Are control characters displayed as `^C'? */
1467 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1468
1469 /* -1 means everything between a CR and the following line end
1470 is invisible. >0 means lines indented more than this value are
1471 invisible. */
1472 it->selective = (INTEGERP (current_buffer->selective_display)
1473 ? XFASTINT (current_buffer->selective_display)
1474 : (!NILP (current_buffer->selective_display)
1475 ? -1 : 0));
1476 it->selective_display_ellipsis_p
1477 = !NILP (current_buffer->selective_display_ellipses);
1478
1479 /* Display table to use. */
1480 it->dp = window_display_table (w);
1481
1482 /* Are multibyte characters enabled in current_buffer? */
1483 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1484
1485 /* Non-zero if we should highlight the region. */
1486 highlight_region_p
1487 = (!NILP (Vtransient_mark_mode)
1488 && !NILP (current_buffer->mark_active)
1489 && XMARKER (current_buffer->mark)->buffer != 0);
1490
1491 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1492 start and end of a visible region in window IT->w. Set both to
1493 -1 to indicate no region. */
1494 if (highlight_region_p
1495 /* Maybe highlight only in selected window. */
1496 && (/* Either show region everywhere. */
1497 highlight_nonselected_windows
1498 /* Or show region in the selected window. */
1499 || w == XWINDOW (selected_window)
1500 /* Or show the region if we are in the mini-buffer and W is
1501 the window the mini-buffer refers to. */
1502 || (MINI_WINDOW_P (XWINDOW (selected_window))
1503 && w == XWINDOW (Vminibuf_scroll_window))))
1504 {
1505 int charpos = marker_position (current_buffer->mark);
1506 it->region_beg_charpos = min (PT, charpos);
1507 it->region_end_charpos = max (PT, charpos);
1508 }
1509 else
1510 it->region_beg_charpos = it->region_end_charpos = -1;
1511
1512 /* Get the position at which the redisplay_end_trigger hook should
1513 be run, if it is to be run at all. */
1514 if (MARKERP (w->redisplay_end_trigger)
1515 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1516 it->redisplay_end_trigger_charpos
1517 = marker_position (w->redisplay_end_trigger);
1518 else if (INTEGERP (w->redisplay_end_trigger))
1519 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1520
1521 /* Correct bogus values of tab_width. */
1522 it->tab_width = XINT (current_buffer->tab_width);
1523 if (it->tab_width <= 0 || it->tab_width > 1000)
1524 it->tab_width = 8;
1525
1526 /* Are lines in the display truncated? */
1527 it->truncate_lines_p
1528 = (base_face_id != DEFAULT_FACE_ID
1529 || XINT (it->w->hscroll)
1530 || (truncate_partial_width_windows
1531 && !WINDOW_FULL_WIDTH_P (it->w))
1532 || !NILP (current_buffer->truncate_lines));
1533
1534 /* Get dimensions of truncation and continuation glyphs. These are
1535 displayed as bitmaps under X, so we don't need them for such
1536 frames. */
1537 if (!FRAME_WINDOW_P (it->f))
1538 {
1539 if (it->truncate_lines_p)
1540 {
1541 /* We will need the truncation glyph. */
1542 xassert (it->glyph_row == NULL);
1543 produce_special_glyphs (it, IT_TRUNCATION);
1544 it->truncation_pixel_width = it->pixel_width;
1545 }
1546 else
1547 {
1548 /* We will need the continuation glyph. */
1549 xassert (it->glyph_row == NULL);
1550 produce_special_glyphs (it, IT_CONTINUATION);
1551 it->continuation_pixel_width = it->pixel_width;
1552 }
1553
1554 /* Reset these values to zero becaue the produce_special_glyphs
1555 above has changed them. */
1556 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1557 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1558 }
1559
1560 /* Set this after getting the dimensions of truncation and
1561 continuation glyphs, so that we don't produce glyphs when calling
1562 produce_special_glyphs, above. */
1563 it->glyph_row = row;
1564 it->area = TEXT_AREA;
1565
1566 /* Get the dimensions of the display area. The display area
1567 consists of the visible window area plus a horizontally scrolled
1568 part to the left of the window. All x-values are relative to the
1569 start of this total display area. */
1570 if (base_face_id != DEFAULT_FACE_ID)
1571 {
1572 /* Mode lines, menu bar in terminal frames. */
1573 it->first_visible_x = 0;
1574 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1575 }
1576 else
1577 {
1578 it->first_visible_x
1579 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1580 it->last_visible_x = (it->first_visible_x
1581 + window_box_width (w, TEXT_AREA));
1582
1583 /* If we truncate lines, leave room for the truncator glyph(s) at
1584 the right margin. Otherwise, leave room for the continuation
1585 glyph(s). Truncation and continuation glyphs are not inserted
1586 for window-based redisplay. */
1587 if (!FRAME_WINDOW_P (it->f))
1588 {
1589 if (it->truncate_lines_p)
1590 it->last_visible_x -= it->truncation_pixel_width;
1591 else
1592 it->last_visible_x -= it->continuation_pixel_width;
1593 }
1594
045dee35
GM
1595 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1596 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1597 }
1598
1599 /* Leave room for a border glyph. */
1600 if (!FRAME_WINDOW_P (it->f)
1601 && !WINDOW_RIGHTMOST_P (it->w))
1602 it->last_visible_x -= 1;
1603
1604 it->last_visible_y = window_text_bottom_y (w);
1605
1606 /* For mode lines and alike, arrange for the first glyph having a
1607 left box line if the face specifies a box. */
1608 if (base_face_id != DEFAULT_FACE_ID)
1609 {
1610 struct face *face;
1611
1612 it->face_id = base_face_id;
1613
1614 /* If we have a boxed mode line, make the first character appear
1615 with a left box line. */
1616 face = FACE_FROM_ID (it->f, base_face_id);
1617 if (face->box != FACE_NO_BOX)
1618 it->start_of_box_run_p = 1;
1619 }
1620
1621 /* If a buffer position was specified, set the iterator there,
1622 getting overlays and face properties from that position. */
1623 if (charpos > 0)
1624 {
1625 it->end_charpos = ZV;
1626 it->face_id = -1;
1627 IT_CHARPOS (*it) = charpos;
1628
1629 /* Compute byte position if not specified. */
1630 if (bytepos <= 0)
1631 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1632 else
1633 IT_BYTEPOS (*it) = bytepos;
1634
1635 /* Compute faces etc. */
1636 reseat (it, it->current.pos, 1);
1637 }
1638
1639 CHECK_IT (it);
1640}
1641
1642
1643/* Initialize IT for the display of window W with window start POS. */
1644
1645void
1646start_display (it, w, pos)
1647 struct it *it;
1648 struct window *w;
1649 struct text_pos pos;
1650{
1651 int start_at_line_beg_p;
1652 struct glyph_row *row;
045dee35 1653 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1654 int first_y;
1655
1656 row = w->desired_matrix->rows + first_vpos;
1657 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1658 first_y = it->current_y;
1659
1660 /* If window start is not at a line start, move back to the line
1661 start. This makes sure that we take continuation lines into
1662 account. */
1663 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1664 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1665 if (!start_at_line_beg_p)
1666 reseat_at_previous_visible_line_start (it);
1667
5f5c8ee5
GM
1668 /* If window start is not at a line start, skip forward to POS to
1669 get the correct continuation_lines_width and current_x. */
1670 if (!start_at_line_beg_p)
1671 {
1672 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1673
1674 /* If lines are continued, this line may end in the middle of a
1675 multi-glyph character (e.g. a control character displayed as
1676 \003, or in the middle of an overlay string). In this case
1677 move_it_to above will not have taken us to the start of
1678 the continuation line but to the end of the continued line. */
b28cb6ed 1679 if (!it->truncate_lines_p)
5f5c8ee5 1680 {
b28cb6ed 1681 if (it->current_x > 0)
5f5c8ee5 1682 {
b28cb6ed
GM
1683 if (it->current.dpvec_index >= 0
1684 || it->current.overlay_string_index >= 0)
1685 {
cafafe0b 1686 set_iterator_to_next (it, 1);
b28cb6ed
GM
1687 move_it_in_display_line_to (it, -1, -1, 0);
1688 }
1689
1690 it->continuation_lines_width += it->current_x;
5f5c8ee5 1691 }
b28cb6ed
GM
1692
1693 /* We're starting a new display line, not affected by the
1694 height of the continued line, so clear the appropriate
1695 fields in the iterator structure. */
1696 it->max_ascent = it->max_descent = 0;
1697 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5
GM
1698 }
1699
1700 it->current_y = first_y;
1701 it->vpos = 0;
1702 it->current_x = it->hpos = 0;
1703 }
1704
1705#if 0 /* Don't assert the following because start_display is sometimes
1706 called intentionally with a window start that is not at a
1707 line start. Please leave this code in as a comment. */
1708
1709 /* Window start should be on a line start, now. */
1710 xassert (it->continuation_lines_width
1711 || IT_CHARPOS (it) == BEGV
1712 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1713#endif /* 0 */
1714}
1715
1716
1717/* Initialize IT for stepping through current_buffer in window W,
1718 starting at position POS that includes overlay string and display
1719 vector/ control character translation position information. */
1720
1721static void
1722init_from_display_pos (it, w, pos)
1723 struct it *it;
1724 struct window *w;
1725 struct display_pos *pos;
1726{
1727 /* Keep in mind: the call to reseat in init_iterator skips invisible
1728 text, so we might end up at a position different from POS. This
1729 is only a problem when POS is a row start after a newline and an
1730 overlay starts there with an after-string, and the overlay has an
1731 invisible property. Since we don't skip invisible text in
1732 display_line and elsewhere immediately after consuming the
1733 newline before the row start, such a POS will not be in a string,
1734 but the call to init_iterator below will move us to the
1735 after-string. */
1736 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1737 NULL, DEFAULT_FACE_ID);
1738
1739 /* If position is within an overlay string, set up IT to
1740 the right overlay string. */
1741 if (pos->overlay_string_index >= 0)
1742 {
1743 int relative_index;
1744
1745 /* We already have the first chunk of overlay strings in
1746 IT->overlay_strings. Load more until the one for
1747 pos->overlay_string_index is in IT->overlay_strings. */
1748 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1749 {
1750 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1751 it->current.overlay_string_index = 0;
1752 while (n--)
1753 {
1754 load_overlay_strings (it);
1755 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1756 }
1757 }
1758
1759 it->current.overlay_string_index = pos->overlay_string_index;
1760 relative_index = (it->current.overlay_string_index
1761 % OVERLAY_STRING_CHUNK_SIZE);
1762 it->string = it->overlay_strings[relative_index];
cafafe0b 1763 xassert (STRINGP (it->string));
5f5c8ee5
GM
1764 it->current.string_pos = pos->string_pos;
1765 it->method = next_element_from_string;
1766 }
2be8f184
GM
1767 else if (it->current.overlay_string_index >= 0)
1768 {
1769 /* If POS says we're already after an overlay string ending at
1770 POS, make sure to pop the iterator because it will be in
1771 front of that overlay string. When POS is ZV, we've thereby
1772 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1773 while (it->sp)
1774 pop_it (it);
2be8f184
GM
1775 it->current.overlay_string_index = -1;
1776 it->method = next_element_from_buffer;
1777 if (CHARPOS (pos->pos) == ZV)
1778 it->overlay_strings_at_end_processed_p = 1;
1779 }
1780
1781 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1782 {
1783 /* Recorded position is not in an overlay string, but in another
1784 string. This can only be a string from a `display' property.
1785 IT should already be filled with that string. */
1786 it->current.string_pos = pos->string_pos;
1787 xassert (STRINGP (it->string));
1788 }
1789
1790 /* Restore position in display vector translations or control
1791 character translations. */
1792 if (pos->dpvec_index >= 0)
1793 {
1794 /* This fills IT->dpvec. */
1795 get_next_display_element (it);
1796 xassert (it->dpvec && it->current.dpvec_index == 0);
1797 it->current.dpvec_index = pos->dpvec_index;
1798 }
1799
1800 CHECK_IT (it);
1801}
1802
1803
1804/* Initialize IT for stepping through current_buffer in window W
1805 starting at ROW->start. */
1806
1807static void
1808init_to_row_start (it, w, row)
1809 struct it *it;
1810 struct window *w;
1811 struct glyph_row *row;
1812{
1813 init_from_display_pos (it, w, &row->start);
1814 it->continuation_lines_width = row->continuation_lines_width;
1815 CHECK_IT (it);
1816}
1817
1818
1819/* Initialize IT for stepping through current_buffer in window W
1820 starting in the line following ROW, i.e. starting at ROW->end. */
1821
1822static void
1823init_to_row_end (it, w, row)
1824 struct it *it;
1825 struct window *w;
1826 struct glyph_row *row;
1827{
1828 init_from_display_pos (it, w, &row->end);
1829
1830 if (row->continued_p)
1831 it->continuation_lines_width = (row->continuation_lines_width
1832 + row->pixel_width);
1833 CHECK_IT (it);
1834}
1835
1836
1837
1838\f
1839/***********************************************************************
1840 Text properties
1841 ***********************************************************************/
1842
1843/* Called when IT reaches IT->stop_charpos. Handle text property and
1844 overlay changes. Set IT->stop_charpos to the next position where
1845 to stop. */
1846
1847static void
1848handle_stop (it)
1849 struct it *it;
1850{
1851 enum prop_handled handled;
1852 int handle_overlay_change_p = 1;
1853 struct props *p;
1854
1855 it->dpvec = NULL;
1856 it->current.dpvec_index = -1;
1857
1858 do
1859 {
1860 handled = HANDLED_NORMALLY;
1861
1862 /* Call text property handlers. */
1863 for (p = it_props; p->handler; ++p)
1864 {
1865 handled = p->handler (it);
2970b9be 1866
5f5c8ee5
GM
1867 if (handled == HANDLED_RECOMPUTE_PROPS)
1868 break;
1869 else if (handled == HANDLED_RETURN)
1870 return;
1871 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1872 handle_overlay_change_p = 0;
1873 }
1874
1875 if (handled != HANDLED_RECOMPUTE_PROPS)
1876 {
1877 /* Don't check for overlay strings below when set to deliver
1878 characters from a display vector. */
1879 if (it->method == next_element_from_display_vector)
1880 handle_overlay_change_p = 0;
1881
1882 /* Handle overlay changes. */
1883 if (handle_overlay_change_p)
1884 handled = handle_overlay_change (it);
1885
1886 /* Determine where to stop next. */
1887 if (handled == HANDLED_NORMALLY)
1888 compute_stop_pos (it);
1889 }
1890 }
1891 while (handled == HANDLED_RECOMPUTE_PROPS);
1892}
1893
1894
1895/* Compute IT->stop_charpos from text property and overlay change
1896 information for IT's current position. */
1897
1898static void
1899compute_stop_pos (it)
1900 struct it *it;
1901{
1902 register INTERVAL iv, next_iv;
1903 Lisp_Object object, limit, position;
1904
1905 /* If nowhere else, stop at the end. */
1906 it->stop_charpos = it->end_charpos;
1907
1908 if (STRINGP (it->string))
1909 {
1910 /* Strings are usually short, so don't limit the search for
1911 properties. */
1912 object = it->string;
1913 limit = Qnil;
1914 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1915 }
1916 else
1917 {
1918 int charpos;
1919
1920 /* If next overlay change is in front of the current stop pos
1921 (which is IT->end_charpos), stop there. Note: value of
1922 next_overlay_change is point-max if no overlay change
1923 follows. */
1924 charpos = next_overlay_change (IT_CHARPOS (*it));
1925 if (charpos < it->stop_charpos)
1926 it->stop_charpos = charpos;
1927
1928 /* If showing the region, we have to stop at the region
1929 start or end because the face might change there. */
1930 if (it->region_beg_charpos > 0)
1931 {
1932 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1933 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1934 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1935 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1936 }
1937
1938 /* Set up variables for computing the stop position from text
1939 property changes. */
1940 XSETBUFFER (object, current_buffer);
1941 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1942 XSETFASTINT (position, IT_CHARPOS (*it));
1943
1944 }
1945
1946 /* Get the interval containing IT's position. Value is a null
1947 interval if there isn't such an interval. */
1948 iv = validate_interval_range (object, &position, &position, 0);
1949 if (!NULL_INTERVAL_P (iv))
1950 {
1951 Lisp_Object values_here[LAST_PROP_IDX];
1952 struct props *p;
1953
1954 /* Get properties here. */
1955 for (p = it_props; p->handler; ++p)
1956 values_here[p->idx] = textget (iv->plist, *p->name);
1957
1958 /* Look for an interval following iv that has different
1959 properties. */
1960 for (next_iv = next_interval (iv);
1961 (!NULL_INTERVAL_P (next_iv)
1962 && (NILP (limit)
1963 || XFASTINT (limit) > next_iv->position));
1964 next_iv = next_interval (next_iv))
1965 {
1966 for (p = it_props; p->handler; ++p)
1967 {
1968 Lisp_Object new_value;
1969
1970 new_value = textget (next_iv->plist, *p->name);
1971 if (!EQ (values_here[p->idx], new_value))
1972 break;
1973 }
1974
1975 if (p->handler)
1976 break;
1977 }
1978
1979 if (!NULL_INTERVAL_P (next_iv))
1980 {
1981 if (INTEGERP (limit)
1982 && next_iv->position >= XFASTINT (limit))
1983 /* No text property change up to limit. */
1984 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1985 else
1986 /* Text properties change in next_iv. */
1987 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1988 }
1989 }
1990
1991 xassert (STRINGP (it->string)
1992 || (it->stop_charpos >= BEGV
1993 && it->stop_charpos >= IT_CHARPOS (*it)));
1994}
1995
1996
1997/* Return the position of the next overlay change after POS in
1998 current_buffer. Value is point-max if no overlay change
1999 follows. This is like `next-overlay-change' but doesn't use
2000 xmalloc. */
2001
2002static int
2003next_overlay_change (pos)
2004 int pos;
2005{
2006 int noverlays;
2007 int endpos;
2008 Lisp_Object *overlays;
2009 int len;
2010 int i;
2011
2012 /* Get all overlays at the given position. */
2013 len = 10;
2014 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2015 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2016 if (noverlays > len)
2017 {
2018 len = noverlays;
2019 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2020 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2021 }
2022
2023 /* If any of these overlays ends before endpos,
2024 use its ending point instead. */
2025 for (i = 0; i < noverlays; ++i)
2026 {
2027 Lisp_Object oend;
2028 int oendpos;
2029
2030 oend = OVERLAY_END (overlays[i]);
2031 oendpos = OVERLAY_POSITION (oend);
2032 endpos = min (endpos, oendpos);
2033 }
2034
2035 return endpos;
2036}
2037
2038
2039\f
2040/***********************************************************************
2041 Fontification
2042 ***********************************************************************/
2043
2044/* Handle changes in the `fontified' property of the current buffer by
2045 calling hook functions from Qfontification_functions to fontify
2046 regions of text. */
2047
2048static enum prop_handled
2049handle_fontified_prop (it)
2050 struct it *it;
2051{
2052 Lisp_Object prop, pos;
2053 enum prop_handled handled = HANDLED_NORMALLY;
2054
2055 /* Get the value of the `fontified' property at IT's current buffer
2056 position. (The `fontified' property doesn't have a special
2057 meaning in strings.) If the value is nil, call functions from
2058 Qfontification_functions. */
2059 if (!STRINGP (it->string)
2060 && it->s == NULL
2061 && !NILP (Vfontification_functions)
085536c2 2062 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2063 && (pos = make_number (IT_CHARPOS (*it)),
2064 prop = Fget_char_property (pos, Qfontified, Qnil),
2065 NILP (prop)))
2066 {
2d27dae2 2067 int count = BINDING_STACK_SIZE ();
085536c2
GM
2068 Lisp_Object val;
2069
2070 val = Vfontification_functions;
2071 specbind (Qfontification_functions, Qnil);
2072 specbind (Qafter_change_functions, Qnil);
2073
2074 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2075 safe_call1 (val, pos);
085536c2
GM
2076 else
2077 {
2078 Lisp_Object globals, fn;
2079 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2080
085536c2
GM
2081 globals = Qnil;
2082 GCPRO2 (val, globals);
2083
2084 for (; CONSP (val); val = XCDR (val))
2085 {
2086 fn = XCAR (val);
2087
2088 if (EQ (fn, Qt))
2089 {
2090 /* A value of t indicates this hook has a local
2091 binding; it means to run the global binding too.
2092 In a global value, t should not occur. If it
2093 does, we must ignore it to avoid an endless
2094 loop. */
2095 for (globals = Fdefault_value (Qfontification_functions);
2096 CONSP (globals);
2097 globals = XCDR (globals))
2098 {
2099 fn = XCAR (globals);
2100 if (!EQ (fn, Qt))
116d6f5c 2101 safe_call1 (fn, pos);
085536c2
GM
2102 }
2103 }
2104 else
116d6f5c 2105 safe_call1 (fn, pos);
085536c2
GM
2106 }
2107
2108 UNGCPRO;
2109 }
2110
2111 unbind_to (count, Qnil);
5f5c8ee5
GM
2112
2113 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2114 something. This avoids an endless loop if they failed to
2115 fontify the text for which reason ever. */
2116 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2117 handled = HANDLED_RECOMPUTE_PROPS;
2118 }
2119
2120 return handled;
2121}
2122
2123
2124\f
2125/***********************************************************************
2126 Faces
2127 ***********************************************************************/
2128
2129/* Set up iterator IT from face properties at its current position.
2130 Called from handle_stop. */
2131
2132static enum prop_handled
2133handle_face_prop (it)
2134 struct it *it;
2135{
2136 int new_face_id, next_stop;
2137
2138 if (!STRINGP (it->string))
2139 {
2140 new_face_id
2141 = face_at_buffer_position (it->w,
2142 IT_CHARPOS (*it),
2143 it->region_beg_charpos,
2144 it->region_end_charpos,
2145 &next_stop,
2146 (IT_CHARPOS (*it)
2147 + TEXT_PROP_DISTANCE_LIMIT),
2148 0);
2149
2150 /* Is this a start of a run of characters with box face?
2151 Caveat: this can be called for a freshly initialized
2152 iterator; face_id is -1 is this case. We know that the new
2153 face will not change until limit, i.e. if the new face has a
2154 box, all characters up to limit will have one. But, as
2155 usual, we don't know whether limit is really the end. */
2156 if (new_face_id != it->face_id)
2157 {
2158 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2159
2160 /* If new face has a box but old face has not, this is
2161 the start of a run of characters with box, i.e. it has
2162 a shadow on the left side. The value of face_id of the
2163 iterator will be -1 if this is the initial call that gets
2164 the face. In this case, we have to look in front of IT's
2165 position and see whether there is a face != new_face_id. */
2166 it->start_of_box_run_p
2167 = (new_face->box != FACE_NO_BOX
2168 && (it->face_id >= 0
2169 || IT_CHARPOS (*it) == BEG
2170 || new_face_id != face_before_it_pos (it)));
2171 it->face_box_p = new_face->box != FACE_NO_BOX;
2172 }
2173 }
2174 else
2175 {
06a12811
GM
2176 int base_face_id, bufpos;
2177
2178 if (it->current.overlay_string_index >= 0)
2179 bufpos = IT_CHARPOS (*it);
2180 else
2181 bufpos = 0;
2182
2183 /* For strings from a buffer, i.e. overlay strings or strings
2184 from a `display' property, use the face at IT's current
2185 buffer position as the base face to merge with, so that
2186 overlay strings appear in the same face as surrounding
2187 text, unless they specify their own faces. */
2188 base_face_id = underlying_face_id (it);
2189
2190 new_face_id = face_at_string_position (it->w,
2191 it->string,
2192 IT_STRING_CHARPOS (*it),
2193 bufpos,
2194 it->region_beg_charpos,
2195 it->region_end_charpos,
2196 &next_stop,
2197 base_face_id);
5f5c8ee5
GM
2198
2199#if 0 /* This shouldn't be neccessary. Let's check it. */
2200 /* If IT is used to display a mode line we would really like to
2201 use the mode line face instead of the frame's default face. */
2202 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2203 && new_face_id == DEFAULT_FACE_ID)
2204 new_face_id = MODE_LINE_FACE_ID;
2205#endif
2206
2207 /* Is this a start of a run of characters with box? Caveat:
2208 this can be called for a freshly allocated iterator; face_id
2209 is -1 is this case. We know that the new face will not
2210 change until the next check pos, i.e. if the new face has a
2211 box, all characters up to that position will have a
2212 box. But, as usual, we don't know whether that position
2213 is really the end. */
2214 if (new_face_id != it->face_id)
2215 {
2216 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2217 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2218
2219 /* If new face has a box but old face hasn't, this is the
2220 start of a run of characters with box, i.e. it has a
2221 shadow on the left side. */
2222 it->start_of_box_run_p
2223 = new_face->box && (old_face == NULL || !old_face->box);
2224 it->face_box_p = new_face->box != FACE_NO_BOX;
2225 }
2226 }
2227
2228 it->face_id = new_face_id;
5f5c8ee5
GM
2229 return HANDLED_NORMALLY;
2230}
2231
2232
06a12811
GM
2233/* Return the ID of the face ``underlying'' IT's current position,
2234 which is in a string. If the iterator is associated with a
2235 buffer, return the face at IT's current buffer position.
2236 Otherwise, use the iterator's base_face_id. */
2237
2238static int
2239underlying_face_id (it)
2240 struct it *it;
2241{
2242 int face_id = it->base_face_id, i;
2243
2244 xassert (STRINGP (it->string));
2245
2246 for (i = it->sp - 1; i >= 0; --i)
2247 if (NILP (it->stack[i].string))
2248 face_id = it->stack[i].face_id;
2249
2250 return face_id;
2251}
2252
2253
5f5c8ee5
GM
2254/* Compute the face one character before or after the current position
2255 of IT. BEFORE_P non-zero means get the face in front of IT's
2256 position. Value is the id of the face. */
2257
2258static int
2259face_before_or_after_it_pos (it, before_p)
2260 struct it *it;
2261 int before_p;
2262{
2263 int face_id, limit;
2264 int next_check_charpos;
2265 struct text_pos pos;
2266
2267 xassert (it->s == NULL);
2268
2269 if (STRINGP (it->string))
2270 {
06a12811
GM
2271 int bufpos, base_face_id;
2272
5f5c8ee5
GM
2273 /* No face change past the end of the string (for the case
2274 we are padding with spaces). No face change before the
2275 string start. */
2276 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2277 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2278 return it->face_id;
2279
2280 /* Set pos to the position before or after IT's current position. */
2281 if (before_p)
2282 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2283 else
260a86a0
KH
2284 /* For composition, we must check the character after the
2285 composition. */
2286 pos = (it->what == IT_COMPOSITION
2287 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2288 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2289
06a12811
GM
2290 if (it->current.overlay_string_index >= 0)
2291 bufpos = IT_CHARPOS (*it);
2292 else
2293 bufpos = 0;
2294
2295 base_face_id = underlying_face_id (it);
2296
5f5c8ee5 2297 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2298 face_id = face_at_string_position (it->w,
2299 it->string,
2300 CHARPOS (pos),
2301 bufpos,
2302 it->region_beg_charpos,
2303 it->region_end_charpos,
2304 &next_check_charpos,
2305 base_face_id);
5f5c8ee5
GM
2306
2307 /* Correct the face for charsets different from ASCII. Do it
2308 for the multibyte case only. The face returned above is
2309 suitable for unibyte text if IT->string is unibyte. */
2310 if (STRING_MULTIBYTE (it->string))
2311 {
2312 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2313 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
2314 int c, len;
2315 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 2316
4fdb80f2 2317 c = string_char_and_length (p, rest, &len);
980806b6 2318 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2319 }
2320 }
2321 else
2322 {
70851746
GM
2323 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2324 || (IT_CHARPOS (*it) <= BEGV && before_p))
2325 return it->face_id;
2326
5f5c8ee5
GM
2327 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2328 pos = it->current.pos;
2329
2330 if (before_p)
c1005d06 2331 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2332 else
260a86a0
KH
2333 {
2334 if (it->what == IT_COMPOSITION)
2335 /* For composition, we must check the position after the
2336 composition. */
2337 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2338 else
c1005d06 2339 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2340 }
06a12811 2341
5f5c8ee5
GM
2342 /* Determine face for CHARSET_ASCII, or unibyte. */
2343 face_id = face_at_buffer_position (it->w,
2344 CHARPOS (pos),
2345 it->region_beg_charpos,
2346 it->region_end_charpos,
2347 &next_check_charpos,
2348 limit, 0);
2349
2350 /* Correct the face for charsets different from ASCII. Do it
2351 for the multibyte case only. The face returned above is
2352 suitable for unibyte text if current_buffer is unibyte. */
2353 if (it->multibyte_p)
2354 {
980806b6
KH
2355 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2356 struct face *face = FACE_FROM_ID (it->f, face_id);
2357 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2358 }
2359 }
2360
2361 return face_id;
2362}
2363
2364
2365\f
2366/***********************************************************************
2367 Invisible text
2368 ***********************************************************************/
2369
2370/* Set up iterator IT from invisible properties at its current
2371 position. Called from handle_stop. */
2372
2373static enum prop_handled
2374handle_invisible_prop (it)
2375 struct it *it;
2376{
2377 enum prop_handled handled = HANDLED_NORMALLY;
2378
2379 if (STRINGP (it->string))
2380 {
2381 extern Lisp_Object Qinvisible;
2382 Lisp_Object prop, end_charpos, limit, charpos;
2383
2384 /* Get the value of the invisible text property at the
2385 current position. Value will be nil if there is no such
2386 property. */
2387 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2388 prop = Fget_text_property (charpos, Qinvisible, it->string);
2389
eadc0bf8
GM
2390 if (!NILP (prop)
2391 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2392 {
2393 handled = HANDLED_RECOMPUTE_PROPS;
2394
2395 /* Get the position at which the next change of the
2396 invisible text property can be found in IT->string.
2397 Value will be nil if the property value is the same for
2398 all the rest of IT->string. */
2399 XSETINT (limit, XSTRING (it->string)->size);
2400 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2401 it->string, limit);
2402
2403 /* Text at current position is invisible. The next
2404 change in the property is at position end_charpos.
2405 Move IT's current position to that position. */
2406 if (INTEGERP (end_charpos)
2407 && XFASTINT (end_charpos) < XFASTINT (limit))
2408 {
2409 struct text_pos old;
2410 old = it->current.string_pos;
2411 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2412 compute_string_pos (&it->current.string_pos, old, it->string);
2413 }
2414 else
2415 {
2416 /* The rest of the string is invisible. If this is an
2417 overlay string, proceed with the next overlay string
2418 or whatever comes and return a character from there. */
2419 if (it->current.overlay_string_index >= 0)
2420 {
2421 next_overlay_string (it);
2422 /* Don't check for overlay strings when we just
2423 finished processing them. */
2424 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2425 }
2426 else
2427 {
2428 struct Lisp_String *s = XSTRING (it->string);
2429 IT_STRING_CHARPOS (*it) = s->size;
2430 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2431 }
2432 }
2433 }
2434 }
2435 else
2436 {
2437 int visible_p, newpos, next_stop;
2438 Lisp_Object pos, prop;
2439
2440 /* First of all, is there invisible text at this position? */
2441 XSETFASTINT (pos, IT_CHARPOS (*it));
2442 prop = Fget_char_property (pos, Qinvisible, it->window);
2443
2444 /* If we are on invisible text, skip over it. */
eadc0bf8
GM
2445 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2446 && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2447 {
2448 /* Record whether we have to display an ellipsis for the
2449 invisible text. */
2450 int display_ellipsis_p
2451 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2452
2453 handled = HANDLED_RECOMPUTE_PROPS;
2454
2455 /* Loop skipping over invisible text. The loop is left at
2456 ZV or with IT on the first char being visible again. */
2457 do
2458 {
2459 /* Try to skip some invisible text. Return value is the
2460 position reached which can be equal to IT's position
2461 if there is nothing invisible here. This skips both
2462 over invisible text properties and overlays with
2463 invisible property. */
2464 newpos = skip_invisible (IT_CHARPOS (*it),
2465 &next_stop, ZV, it->window);
2466
2467 /* If we skipped nothing at all we weren't at invisible
2468 text in the first place. If everything to the end of
2469 the buffer was skipped, end the loop. */
2470 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2471 visible_p = 1;
2472 else
2473 {
2474 /* We skipped some characters but not necessarily
2475 all there are. Check if we ended up on visible
2476 text. Fget_char_property returns the property of
2477 the char before the given position, i.e. if we
2478 get visible_p = 1, this means that the char at
2479 newpos is visible. */
2480 XSETFASTINT (pos, newpos);
2481 prop = Fget_char_property (pos, Qinvisible, it->window);
2482 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2483 }
2484
2485 /* If we ended up on invisible text, proceed to
2486 skip starting with next_stop. */
2487 if (!visible_p)
2488 IT_CHARPOS (*it) = next_stop;
2489 }
2490 while (!visible_p);
2491
2492 /* The position newpos is now either ZV or on visible text. */
2493 IT_CHARPOS (*it) = newpos;
2494 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2495
2496 /* Maybe return `...' next for the end of the invisible text. */
2497 if (display_ellipsis_p)
2498 {
2499 if (it->dp
2500 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2501 {
2502 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2503 it->dpvec = v->contents;
2504 it->dpend = v->contents + v->size;
2505 }
2506 else
2507 {
2508 /* Default `...'. */
2509 it->dpvec = default_invis_vector;
2510 it->dpend = default_invis_vector + 3;
2511 }
2512
2513 /* The ellipsis display does not replace the display of
2514 the character at the new position. Indicate this by
2515 setting IT->dpvec_char_len to zero. */
2516 it->dpvec_char_len = 0;
2517
2518 it->current.dpvec_index = 0;
2519 it->method = next_element_from_display_vector;
2520 }
2521 }
2522 }
2523
2524 return handled;
2525}
2526
2527
2528\f
2529/***********************************************************************
2530 'display' property
2531 ***********************************************************************/
2532
2533/* Set up iterator IT from `display' property at its current position.
2534 Called from handle_stop. */
2535
2536static enum prop_handled
2537handle_display_prop (it)
2538 struct it *it;
2539{
2540 Lisp_Object prop, object;
2541 struct text_pos *position;
2542 int space_or_image_found_p;
2543
2544 if (STRINGP (it->string))
2545 {
2546 object = it->string;
2547 position = &it->current.string_pos;
2548 }
2549 else
2550 {
2551 object = Qnil;
2552 position = &it->current.pos;
2553 }
2554
2555 /* Reset those iterator values set from display property values. */
2556 it->font_height = Qnil;
2557 it->space_width = Qnil;
2558 it->voffset = 0;
2559
2560 /* We don't support recursive `display' properties, i.e. string
2561 values that have a string `display' property, that have a string
2562 `display' property etc. */
2563 if (!it->string_from_display_prop_p)
2564 it->area = TEXT_AREA;
2565
2566 prop = Fget_char_property (make_number (position->charpos),
2567 Qdisplay, object);
2568 if (NILP (prop))
2569 return HANDLED_NORMALLY;
2570
2571 space_or_image_found_p = 0;
f3751a65
GM
2572 if (CONSP (prop)
2573 && CONSP (XCAR (prop))
2574 && !EQ (Qmargin, XCAR (XCAR (prop))))
5f5c8ee5 2575 {
f3751a65 2576 /* A list of sub-properties. */
5f5c8ee5
GM
2577 while (CONSP (prop))
2578 {
2579 if (handle_single_display_prop (it, XCAR (prop), object, position))
2580 space_or_image_found_p = 1;
2581 prop = XCDR (prop);
2582 }
2583 }
2584 else if (VECTORP (prop))
2585 {
2586 int i;
2587 for (i = 0; i < XVECTOR (prop)->size; ++i)
2588 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2589 object, position))
2590 space_or_image_found_p = 1;
2591 }
2592 else
2593 {
2594 if (handle_single_display_prop (it, prop, object, position))
2595 space_or_image_found_p = 1;
2596 }
2597
2598 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2599}
2600
2601
6c577098 2602/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2603 at START_POS in OBJECT. */
2604
2605static struct text_pos
2606display_prop_end (it, object, start_pos)
2607 struct it *it;
2608 Lisp_Object object;
2609 struct text_pos start_pos;
2610{
2611 Lisp_Object end;
2612 struct text_pos end_pos;
5f5c8ee5 2613
016b5642
MB
2614 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2615 Qdisplay, object, Qnil);
6c577098
GM
2616 CHARPOS (end_pos) = XFASTINT (end);
2617 if (STRINGP (object))
5f5c8ee5
GM
2618 compute_string_pos (&end_pos, start_pos, it->string);
2619 else
6c577098 2620 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2621
2622 return end_pos;
2623}
2624
2625
2626/* Set up IT from a single `display' sub-property value PROP. OBJECT
2627 is the object in which the `display' property was found. *POSITION
2628 is the position at which it was found.
2629
2630 If PROP is a `space' or `image' sub-property, set *POSITION to the
2631 end position of the `display' property.
2632
2633 Value is non-zero if a `space' or `image' property value was found. */
2634
2635static int
2636handle_single_display_prop (it, prop, object, position)
2637 struct it *it;
2638 Lisp_Object prop;
2639 Lisp_Object object;
2640 struct text_pos *position;
2641{
2642 Lisp_Object value;
2643 int space_or_image_found_p = 0;
5f5c8ee5
GM
2644 Lisp_Object form;
2645
d3acf96b 2646 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2647 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2648 form = Qt;
d3acf96b 2649 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2650 {
2651 prop = XCDR (prop);
2652 if (!CONSP (prop))
2653 return 0;
2654 form = XCAR (prop);
2655 prop = XCDR (prop);
5f5c8ee5
GM
2656 }
2657
2658 if (!NILP (form) && !EQ (form, Qt))
2659 {
2660 struct gcpro gcpro1;
2661 struct text_pos end_pos, pt;
2662
5f5c8ee5 2663 GCPRO1 (form);
c7aca1ad 2664 end_pos = display_prop_end (it, object, *position);
5f5c8ee5
GM
2665
2666 /* Temporarily set point to the end position, and then evaluate
2667 the form. This makes `(eolp)' work as FORM. */
c7aca1ad
GM
2668 if (BUFFERP (object))
2669 {
2670 CHARPOS (pt) = PT;
2671 BYTEPOS (pt) = PT_BYTE;
2672 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2673 }
2674
116d6f5c 2675 form = safe_eval (form);
c7aca1ad
GM
2676
2677 if (BUFFERP (object))
2678 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
5f5c8ee5
GM
2679 UNGCPRO;
2680 }
2681
2682 if (NILP (form))
2683 return 0;
2684
2685 if (CONSP (prop)
2686 && EQ (XCAR (prop), Qheight)
2687 && CONSP (XCDR (prop)))
2688 {
e4093f38 2689 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5
GM
2690 return 0;
2691
2692 /* `(height HEIGHT)'. */
2693 it->font_height = XCAR (XCDR (prop));
2694 if (!NILP (it->font_height))
2695 {
2696 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2697 int new_height = -1;
2698
2699 if (CONSP (it->font_height)
2700 && (EQ (XCAR (it->font_height), Qplus)
2701 || EQ (XCAR (it->font_height), Qminus))
2702 && CONSP (XCDR (it->font_height))
2703 && INTEGERP (XCAR (XCDR (it->font_height))))
2704 {
2705 /* `(+ N)' or `(- N)' where N is an integer. */
2706 int steps = XINT (XCAR (XCDR (it->font_height)));
2707 if (EQ (XCAR (it->font_height), Qplus))
2708 steps = - steps;
2709 it->face_id = smaller_face (it->f, it->face_id, steps);
2710 }
0d8b31c0 2711 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2712 {
2713 /* Call function with current height as argument.
2714 Value is the new height. */
116d6f5c
GM
2715 Lisp_Object height;
2716 height = safe_call1 (it->font_height,
2717 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2718 if (NUMBERP (height))
2719 new_height = XFLOATINT (height);
5f5c8ee5
GM
2720 }
2721 else if (NUMBERP (it->font_height))
2722 {
2723 /* Value is a multiple of the canonical char height. */
2724 struct face *face;
2725
2726 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2727 new_height = (XFLOATINT (it->font_height)
2728 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2729 }
2730 else
2731 {
2732 /* Evaluate IT->font_height with `height' bound to the
2733 current specified height to get the new height. */
2734 Lisp_Object value;
2d27dae2 2735 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
2736
2737 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2738 value = safe_eval (it->font_height);
5f5c8ee5
GM
2739 unbind_to (count, Qnil);
2740
2741 if (NUMBERP (value))
2742 new_height = XFLOATINT (value);
2743 }
2744
2745 if (new_height > 0)
2746 it->face_id = face_with_height (it->f, it->face_id, new_height);
2747 }
2748 }
2749 else if (CONSP (prop)
2750 && EQ (XCAR (prop), Qspace_width)
2751 && CONSP (XCDR (prop)))
2752 {
2753 /* `(space_width WIDTH)'. */
e4093f38 2754 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2755 return 0;
5f5c8ee5
GM
2756
2757 value = XCAR (XCDR (prop));
2758 if (NUMBERP (value) && XFLOATINT (value) > 0)
2759 it->space_width = value;
2760 }
2761 else if (CONSP (prop)
2762 && EQ (XCAR (prop), Qraise)
2763 && CONSP (XCDR (prop)))
2764 {
5f5c8ee5 2765 /* `(raise FACTOR)'. */
e4093f38 2766 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2767 return 0;
5f5c8ee5 2768
fb3842a8 2769#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2770 value = XCAR (XCDR (prop));
2771 if (NUMBERP (value))
2772 {
2773 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2774 it->voffset = - (XFLOATINT (value)
1ab3e082 2775 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2776 }
2777#endif /* HAVE_WINDOW_SYSTEM */
2778 }
2779 else if (!it->string_from_display_prop_p)
2780 {
f3751a65
GM
2781 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2782 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2783 Lisp_Object location, value;
2784 struct text_pos start_pos;
2785 int valid_p;
2786
2787 /* Characters having this form of property are not displayed, so
2788 we have to find the end of the property. */
5f5c8ee5
GM
2789 start_pos = *position;
2790 *position = display_prop_end (it, object, start_pos);
15e26c76 2791 value = Qnil;
5f5c8ee5
GM
2792
2793 /* Let's stop at the new position and assume that all
2794 text properties change there. */
2795 it->stop_charpos = position->charpos;
2796
f3751a65
GM
2797 location = Qunbound;
2798 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2799 {
f3751a65
GM
2800 Lisp_Object tem;
2801
5f5c8ee5 2802 value = XCDR (prop);
f3751a65
GM
2803 if (CONSP (value))
2804 value = XCAR (value);
2805
2806 tem = XCAR (prop);
2807 if (EQ (XCAR (tem), Qmargin)
2808 && (tem = XCDR (tem),
2809 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2810 (NILP (tem)
2811 || EQ (tem, Qleft_margin)
2812 || EQ (tem, Qright_margin))))
2813 location = tem;
5f5c8ee5 2814 }
f3751a65
GM
2815
2816 if (EQ (location, Qunbound))
5f5c8ee5
GM
2817 {
2818 location = Qnil;
2819 value = prop;
2820 }
2821
2822#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 2823 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
2824 valid_p = STRINGP (value);
2825 else
2826 valid_p = (STRINGP (value)
2827 || (CONSP (value) && EQ (XCAR (value), Qspace))
2828 || valid_image_p (value));
2829#else /* not HAVE_WINDOW_SYSTEM */
2830 valid_p = STRINGP (value);
2831#endif /* not HAVE_WINDOW_SYSTEM */
2832
2833 if ((EQ (location, Qleft_margin)
2834 || EQ (location, Qright_margin)
2835 || NILP (location))
2836 && valid_p)
2837 {
a21a3943
GM
2838 space_or_image_found_p = 1;
2839
5f5c8ee5
GM
2840 /* Save current settings of IT so that we can restore them
2841 when we are finished with the glyph property value. */
2842 push_it (it);
2843
2844 if (NILP (location))
2845 it->area = TEXT_AREA;
2846 else if (EQ (location, Qleft_margin))
2847 it->area = LEFT_MARGIN_AREA;
2848 else
2849 it->area = RIGHT_MARGIN_AREA;
2850
2851 if (STRINGP (value))
2852 {
2853 it->string = value;
2854 it->multibyte_p = STRING_MULTIBYTE (it->string);
2855 it->current.overlay_string_index = -1;
2856 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2857 it->end_charpos = it->string_nchars
2858 = XSTRING (it->string)->size;
2859 it->method = next_element_from_string;
2860 it->stop_charpos = 0;
2861 it->string_from_display_prop_p = 1;
2862 }
2863 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2864 {
2865 it->method = next_element_from_stretch;
2866 it->object = value;
2867 it->current.pos = it->position = start_pos;
2868 }
2869#ifdef HAVE_WINDOW_SYSTEM
2870 else
2871 {
2872 it->what = IT_IMAGE;
2873 it->image_id = lookup_image (it->f, value);
2874 it->position = start_pos;
2875 it->object = NILP (object) ? it->w->buffer : object;
2876 it->method = next_element_from_image;
2877
02513cdd 2878 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
2879 `display' property yet. The call to pop_it in
2880 set_iterator_to_next will clean this up. */
2881 *position = start_pos;
2882 }
2883#endif /* HAVE_WINDOW_SYSTEM */
2884 }
a21a3943
GM
2885 else
2886 /* Invalid property or property not supported. Restore
2887 the position to what it was before. */
2888 *position = start_pos;
5f5c8ee5
GM
2889 }
2890
2891 return space_or_image_found_p;
2892}
2893
2894
06568bbf
GM
2895/* Check if PROP is a display sub-property value whose text should be
2896 treated as intangible. */
2897
2898static int
2899single_display_prop_intangible_p (prop)
2900 Lisp_Object prop;
2901{
2902 /* Skip over `when FORM'. */
2903 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2904 {
2905 prop = XCDR (prop);
2906 if (!CONSP (prop))
2907 return 0;
2908 prop = XCDR (prop);
2909 }
2910
2911 if (!CONSP (prop))
2912 return 0;
2913
2914 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2915 we don't need to treat text as intangible. */
2916 if (EQ (XCAR (prop), Qmargin))
2917 {
2918 prop = XCDR (prop);
2919 if (!CONSP (prop))
2920 return 0;
2921
2922 prop = XCDR (prop);
2923 if (!CONSP (prop)
2924 || EQ (XCAR (prop), Qleft_margin)
2925 || EQ (XCAR (prop), Qright_margin))
2926 return 0;
2927 }
2928
2929 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2930}
2931
2932
2933/* Check if PROP is a display property value whose text should be
2934 treated as intangible. */
2935
2936int
2937display_prop_intangible_p (prop)
2938 Lisp_Object prop;
2939{
2940 if (CONSP (prop)
2941 && CONSP (XCAR (prop))
2942 && !EQ (Qmargin, XCAR (XCAR (prop))))
2943 {
2944 /* A list of sub-properties. */
2945 while (CONSP (prop))
2946 {
2947 if (single_display_prop_intangible_p (XCAR (prop)))
2948 return 1;
2949 prop = XCDR (prop);
2950 }
2951 }
2952 else if (VECTORP (prop))
2953 {
2954 /* A vector of sub-properties. */
2955 int i;
2956 for (i = 0; i < XVECTOR (prop)->size; ++i)
2957 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2958 return 1;
2959 }
2960 else
2961 return single_display_prop_intangible_p (prop);
2962
2963 return 0;
2964}
2965
5f5c8ee5 2966\f
260a86a0
KH
2967/***********************************************************************
2968 `composition' property
2969 ***********************************************************************/
2970
2971/* Set up iterator IT from `composition' property at its current
2972 position. Called from handle_stop. */
2973
2974static enum prop_handled
2975handle_composition_prop (it)
2976 struct it *it;
2977{
2978 Lisp_Object prop, string;
2979 int pos, pos_byte, end;
2980 enum prop_handled handled = HANDLED_NORMALLY;
2981
2982 if (STRINGP (it->string))
2983 {
2984 pos = IT_STRING_CHARPOS (*it);
2985 pos_byte = IT_STRING_BYTEPOS (*it);
2986 string = it->string;
2987 }
2988 else
2989 {
2990 pos = IT_CHARPOS (*it);
2991 pos_byte = IT_BYTEPOS (*it);
2992 string = Qnil;
2993 }
2994
2995 /* If there's a valid composition and point is not inside of the
2996 composition (in the case that the composition is from the current
2997 buffer), draw a glyph composed from the composition components. */
2998 if (find_composition (pos, -1, &pos, &end, &prop, string)
2999 && COMPOSITION_VALID_P (pos, end, prop)
3000 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3001 {
3002 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3003
3004 if (id >= 0)
3005 {
3006 it->method = next_element_from_composition;
3007 it->cmp_id = id;
3008 it->cmp_len = COMPOSITION_LENGTH (prop);
3009 /* For a terminal, draw only the first character of the
3010 components. */
3011 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3012 it->len = (STRINGP (it->string)
3013 ? string_char_to_byte (it->string, end)
3014 : CHAR_TO_BYTE (end)) - pos_byte;
3015 it->stop_charpos = end;
3016 handled = HANDLED_RETURN;
3017 }
3018 }
3019
3020 return handled;
3021}
3022
3023
3024\f
5f5c8ee5
GM
3025/***********************************************************************
3026 Overlay strings
3027 ***********************************************************************/
3028
3029/* The following structure is used to record overlay strings for
3030 later sorting in load_overlay_strings. */
3031
3032struct overlay_entry
3033{
2970b9be 3034 Lisp_Object overlay;
5f5c8ee5
GM
3035 Lisp_Object string;
3036 int priority;
3037 int after_string_p;
3038};
3039
3040
3041/* Set up iterator IT from overlay strings at its current position.
3042 Called from handle_stop. */
3043
3044static enum prop_handled
3045handle_overlay_change (it)
3046 struct it *it;
3047{
2970b9be
GM
3048 if (!STRINGP (it->string) && get_overlay_strings (it))
3049 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3050 else
2970b9be 3051 return HANDLED_NORMALLY;
5f5c8ee5
GM
3052}
3053
3054
3055/* Set up the next overlay string for delivery by IT, if there is an
3056 overlay string to deliver. Called by set_iterator_to_next when the
3057 end of the current overlay string is reached. If there are more
3058 overlay strings to display, IT->string and
3059 IT->current.overlay_string_index are set appropriately here.
3060 Otherwise IT->string is set to nil. */
3061
3062static void
3063next_overlay_string (it)
3064 struct it *it;
3065{
3066 ++it->current.overlay_string_index;
3067 if (it->current.overlay_string_index == it->n_overlay_strings)
3068 {
3069 /* No more overlay strings. Restore IT's settings to what
3070 they were before overlay strings were processed, and
3071 continue to deliver from current_buffer. */
3072 pop_it (it);
3073 xassert (it->stop_charpos >= BEGV
3074 && it->stop_charpos <= it->end_charpos);
3075 it->string = Qnil;
3076 it->current.overlay_string_index = -1;
3077 SET_TEXT_POS (it->current.string_pos, -1, -1);
3078 it->n_overlay_strings = 0;
3079 it->method = next_element_from_buffer;
2970b9be
GM
3080
3081 /* If we're at the end of the buffer, record that we have
3082 processed the overlay strings there already, so that
3083 next_element_from_buffer doesn't try it again. */
3084 if (IT_CHARPOS (*it) >= it->end_charpos)
3085 it->overlay_strings_at_end_processed_p = 1;
5f5c8ee5
GM
3086 }
3087 else
3088 {
3089 /* There are more overlay strings to process. If
3090 IT->current.overlay_string_index has advanced to a position
3091 where we must load IT->overlay_strings with more strings, do
3092 it. */
3093 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3094
3095 if (it->current.overlay_string_index && i == 0)
3096 load_overlay_strings (it);
3097
3098 /* Initialize IT to deliver display elements from the overlay
3099 string. */
3100 it->string = it->overlay_strings[i];
3101 it->multibyte_p = STRING_MULTIBYTE (it->string);
3102 SET_TEXT_POS (it->current.string_pos, 0, 0);
3103 it->method = next_element_from_string;
3104 it->stop_charpos = 0;
3105 }
3106
3107 CHECK_IT (it);
3108}
3109
3110
3111/* Compare two overlay_entry structures E1 and E2. Used as a
3112 comparison function for qsort in load_overlay_strings. Overlay
3113 strings for the same position are sorted so that
3114
2970b9be
GM
3115 1. All after-strings come in front of before-strings, except
3116 when they come from the same overlay.
5f5c8ee5
GM
3117
3118 2. Within after-strings, strings are sorted so that overlay strings
3119 from overlays with higher priorities come first.
3120
3121 2. Within before-strings, strings are sorted so that overlay
3122 strings from overlays with higher priorities come last.
3123
3124 Value is analogous to strcmp. */
3125
3126
3127static int
3128compare_overlay_entries (e1, e2)
3129 void *e1, *e2;
3130{
3131 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3132 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3133 int result;
3134
3135 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3136 {
3137 /* Let after-strings appear in front of before-strings if
3138 they come from different overlays. */
3139 if (EQ (entry1->overlay, entry2->overlay))
3140 result = entry1->after_string_p ? 1 : -1;
3141 else
3142 result = entry1->after_string_p ? -1 : 1;
3143 }
5f5c8ee5
GM
3144 else if (entry1->after_string_p)
3145 /* After-strings sorted in order of decreasing priority. */
3146 result = entry2->priority - entry1->priority;
3147 else
3148 /* Before-strings sorted in order of increasing priority. */
3149 result = entry1->priority - entry2->priority;
3150
3151 return result;
3152}
3153
3154
3155/* Load the vector IT->overlay_strings with overlay strings from IT's
3156 current buffer position. Set IT->n_overlays to the total number of
3157 overlay strings found.
3158
3159 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3160 a time. On entry into load_overlay_strings,
3161 IT->current.overlay_string_index gives the number of overlay
3162 strings that have already been loaded by previous calls to this
3163 function.
3164
2970b9be
GM
3165 IT->add_overlay_start contains an additional overlay start
3166 position to consider for taking overlay strings from, if non-zero.
3167 This position comes into play when the overlay has an `invisible'
3168 property, and both before and after-strings. When we've skipped to
3169 the end of the overlay, because of its `invisible' property, we
3170 nevertheless want its before-string to appear.
3171 IT->add_overlay_start will contain the overlay start position
3172 in this case.
3173
5f5c8ee5
GM
3174 Overlay strings are sorted so that after-string strings come in
3175 front of before-string strings. Within before and after-strings,
3176 strings are sorted by overlay priority. See also function
3177 compare_overlay_entries. */
3178
3179static void
3180load_overlay_strings (it)
3181 struct it *it;
3182{
3183 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3184 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3185 int start, end;
3186 int size = 20;
cafafe0b 3187 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3188 struct overlay_entry *entries
3189 = (struct overlay_entry *) alloca (size * sizeof *entries);
cafafe0b 3190 int charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3191
3192 /* Append the overlay string STRING of overlay OVERLAY to vector
3193 `entries' which has size `size' and currently contains `n'
3194 elements. AFTER_P non-zero means STRING is an after-string of
3195 OVERLAY. */
3196#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3197 do \
3198 { \
3199 Lisp_Object priority; \
3200 \
3201 if (n == size) \
3202 { \
3203 int new_size = 2 * size; \
3204 struct overlay_entry *old = entries; \
3205 entries = \
3206 (struct overlay_entry *) alloca (new_size \
3207 * sizeof *entries); \
3208 bcopy (old, entries, size * sizeof *entries); \
3209 size = new_size; \
3210 } \
3211 \
3212 entries[n].string = (STRING); \
2970b9be 3213 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3214 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3215 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3216 entries[n].after_string_p = (AFTER_P); \
3217 ++n; \
3218 } \
3219 while (0)
3220
3221 /* Process overlay before the overlay center. */
2970b9be 3222 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3223 {
9472f927 3224 overlay = XCAR (ov);
5f5c8ee5
GM
3225 xassert (OVERLAYP (overlay));
3226 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3227 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3228
cafafe0b 3229 if (end < charpos)
5f5c8ee5
GM
3230 break;
3231
3232 /* Skip this overlay if it doesn't start or end at IT's current
3233 position. */
cafafe0b 3234 if (end != charpos && start != charpos)
5f5c8ee5
GM
3235 continue;
3236
3237 /* Skip this overlay if it doesn't apply to IT->w. */
3238 window = Foverlay_get (overlay, Qwindow);
3239 if (WINDOWP (window) && XWINDOW (window) != it->w)
3240 continue;
3241
cafafe0b
GM
3242 /* If the text ``under'' the overlay is invisible, both before-
3243 and after-strings from this overlay are visible; start and
3244 end position are indistinguishable. */
3245 invisible = Foverlay_get (overlay, Qinvisible);
3246 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3247
5f5c8ee5 3248 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3249 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3250 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3251 && XSTRING (str)->size)
3252 RECORD_OVERLAY_STRING (overlay, str, 0);
3253
3254 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3255 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3256 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3257 && XSTRING (str)->size)
3258 RECORD_OVERLAY_STRING (overlay, str, 1);
3259 }
3260
3261 /* Process overlays after the overlay center. */
2970b9be 3262 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3263 {
9472f927 3264 overlay = XCAR (ov);
5f5c8ee5
GM
3265 xassert (OVERLAYP (overlay));
3266 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3267 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3268
cafafe0b 3269 if (start > charpos)
5f5c8ee5
GM
3270 break;
3271
3272 /* Skip this overlay if it doesn't start or end at IT's current
3273 position. */
cafafe0b 3274 if (end != charpos && start != charpos)
5f5c8ee5
GM
3275 continue;
3276
3277 /* Skip this overlay if it doesn't apply to IT->w. */
3278 window = Foverlay_get (overlay, Qwindow);
3279 if (WINDOWP (window) && XWINDOW (window) != it->w)
3280 continue;
3281
cafafe0b
GM
3282 /* If the text ``under'' the overlay is invisible, it has a zero
3283 dimension, and both before- and after-strings apply. */
3284 invisible = Foverlay_get (overlay, Qinvisible);
3285 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3286
5f5c8ee5 3287 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3288 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3289 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3290 && XSTRING (str)->size)
3291 RECORD_OVERLAY_STRING (overlay, str, 0);
3292
3293 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3294 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3295 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3296 && XSTRING (str)->size)
3297 RECORD_OVERLAY_STRING (overlay, str, 1);
3298 }
3299
3300#undef RECORD_OVERLAY_STRING
3301
3302 /* Sort entries. */
cafafe0b 3303 if (n > 1)
2970b9be 3304 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3305
3306 /* Record the total number of strings to process. */
3307 it->n_overlay_strings = n;
3308
3309 /* IT->current.overlay_string_index is the number of overlay strings
3310 that have already been consumed by IT. Copy some of the
3311 remaining overlay strings to IT->overlay_strings. */
3312 i = 0;
3313 j = it->current.overlay_string_index;
3314 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3315 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3316
5f5c8ee5
GM
3317 CHECK_IT (it);
3318}
3319
3320
3321/* Get the first chunk of overlay strings at IT's current buffer
3322 position. Value is non-zero if at least one overlay string was
3323 found. */
3324
3325static int
3326get_overlay_strings (it)
3327 struct it *it;
3328{
3329 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3330 process. This fills IT->overlay_strings with strings, and sets
3331 IT->n_overlay_strings to the total number of strings to process.
3332 IT->pos.overlay_string_index has to be set temporarily to zero
3333 because load_overlay_strings needs this; it must be set to -1
3334 when no overlay strings are found because a zero value would
3335 indicate a position in the first overlay string. */
3336 it->current.overlay_string_index = 0;
3337 load_overlay_strings (it);
3338
3339 /* If we found overlay strings, set up IT to deliver display
3340 elements from the first one. Otherwise set up IT to deliver
3341 from current_buffer. */
3342 if (it->n_overlay_strings)
3343 {
3344 /* Make sure we know settings in current_buffer, so that we can
3345 restore meaningful values when we're done with the overlay
3346 strings. */
3347 compute_stop_pos (it);
3348 xassert (it->face_id >= 0);
3349
3350 /* Save IT's settings. They are restored after all overlay
3351 strings have been processed. */
3352 xassert (it->sp == 0);
3353 push_it (it);
3354
3355 /* Set up IT to deliver display elements from the first overlay
3356 string. */
3357 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3358 it->stop_charpos = 0;
3359 it->string = it->overlay_strings[0];
3360 it->multibyte_p = STRING_MULTIBYTE (it->string);
3361 xassert (STRINGP (it->string));
3362 it->method = next_element_from_string;
3363 }
3364 else
3365 {
3366 it->string = Qnil;
3367 it->current.overlay_string_index = -1;
3368 it->method = next_element_from_buffer;
3369 }
3370
3371 CHECK_IT (it);
3372
3373 /* Value is non-zero if we found at least one overlay string. */
3374 return STRINGP (it->string);
3375}
3376
3377
3378\f
3379/***********************************************************************
3380 Saving and restoring state
3381 ***********************************************************************/
3382
3383/* Save current settings of IT on IT->stack. Called, for example,
3384 before setting up IT for an overlay string, to be able to restore
3385 IT's settings to what they were after the overlay string has been
3386 processed. */
3387
3388static void
3389push_it (it)
3390 struct it *it;
3391{
3392 struct iterator_stack_entry *p;
3393
3394 xassert (it->sp < 2);
3395 p = it->stack + it->sp;
3396
3397 p->stop_charpos = it->stop_charpos;
3398 xassert (it->face_id >= 0);
3399 p->face_id = it->face_id;
3400 p->string = it->string;
3401 p->pos = it->current;
3402 p->end_charpos = it->end_charpos;
3403 p->string_nchars = it->string_nchars;
3404 p->area = it->area;
3405 p->multibyte_p = it->multibyte_p;
3406 p->space_width = it->space_width;
3407 p->font_height = it->font_height;
3408 p->voffset = it->voffset;
3409 p->string_from_display_prop_p = it->string_from_display_prop_p;
3410 ++it->sp;
3411}
3412
3413
3414/* Restore IT's settings from IT->stack. Called, for example, when no
3415 more overlay strings must be processed, and we return to delivering
3416 display elements from a buffer, or when the end of a string from a
3417 `display' property is reached and we return to delivering display
3418 elements from an overlay string, or from a buffer. */
3419
3420static void
3421pop_it (it)
3422 struct it *it;
3423{
3424 struct iterator_stack_entry *p;
3425
3426 xassert (it->sp > 0);
3427 --it->sp;
3428 p = it->stack + it->sp;
3429 it->stop_charpos = p->stop_charpos;
3430 it->face_id = p->face_id;
3431 it->string = p->string;
3432 it->current = p->pos;
3433 it->end_charpos = p->end_charpos;
3434 it->string_nchars = p->string_nchars;
3435 it->area = p->area;
3436 it->multibyte_p = p->multibyte_p;
3437 it->space_width = p->space_width;
3438 it->font_height = p->font_height;
3439 it->voffset = p->voffset;
3440 it->string_from_display_prop_p = p->string_from_display_prop_p;
3441}
3442
3443
3444\f
3445/***********************************************************************
3446 Moving over lines
3447 ***********************************************************************/
3448
3449/* Set IT's current position to the previous line start. */
3450
3451static void
3452back_to_previous_line_start (it)
3453 struct it *it;
3454{
3455 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3456 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3457}
3458
3459
cafafe0b
GM
3460/* Move IT to the next line start.
3461
3462 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3463 we skipped over part of the text (as opposed to moving the iterator
3464 continuously over the text). Otherwise, don't change the value
3465 of *SKIPPED_P.
3466
3467 Newlines may come from buffer text, overlay strings, or strings
3468 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3469 simply use find_next_newline_no_quit.
3470
3471 Note that this function may not skip over invisible text that is so
3472 because of text properties and immediately follows a newline. If
3473 it would, function reseat_at_next_visible_line_start, when called
3474 from set_iterator_to_next, would effectively make invisible
3475 characters following a newline part of the wrong glyph row, which
3476 leads to wrong cursor motion. */
5f5c8ee5 3477
cafafe0b
GM
3478static int
3479forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3480 struct it *it;
cafafe0b 3481 int *skipped_p;
5f5c8ee5 3482{
54918e2b 3483 int old_selective, newline_found_p, n;
cafafe0b
GM
3484 const int MAX_NEWLINE_DISTANCE = 500;
3485
0fd37545
GM
3486 /* If already on a newline, just consume it to avoid unintended
3487 skipping over invisible text below. */
d02f1cb8 3488 if (it->what == IT_CHARACTER && it->c == '\n')
0fd37545
GM
3489 {
3490 set_iterator_to_next (it, 0);
a77dc1ec 3491 it->c = 0;
0fd37545
GM
3492 return 1;
3493 }
3494
54918e2b 3495 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3496 unnecessary because it's done by the caller, and (b) leads to an
3497 infinite recursion because next_element_from_ellipsis indirectly
3498 calls this function. */
54918e2b
GM
3499 old_selective = it->selective;
3500 it->selective = 0;
3501
cafafe0b
GM
3502 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3503 from buffer text. */
3aec8722
GM
3504 for (n = newline_found_p = 0;
3505 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3506 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3507 {
8692ca92
GM
3508 if (!get_next_display_element (it))
3509 break;
d02f1cb8 3510 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3511 set_iterator_to_next (it, 0);
cafafe0b
GM
3512 }
3513
3514 /* If we didn't find a newline near enough, see if we can use a
3515 short-cut. */
3aec8722 3516 if (n == MAX_NEWLINE_DISTANCE)
cafafe0b
GM
3517 {
3518 int start = IT_CHARPOS (*it);
3519 int limit = find_next_newline_no_quit (start, 1);
3520 Lisp_Object pos;
3521
3522 xassert (!STRINGP (it->string));
3523
3524 /* If there isn't any `display' property in sight, and no
3525 overlays, we can just use the position of the newline in
3526 buffer text. */
3527 if (it->stop_charpos >= limit
3528 || ((pos = Fnext_single_property_change (make_number (start),
3529 Qdisplay,
3530 Qnil, make_number (limit)),
3531 NILP (pos))
3532 && next_overlay_change (start) == ZV))
3533 {
3534 IT_CHARPOS (*it) = limit;
3535 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3536 *skipped_p = newline_found_p = 1;
3537 }
3538 else
3539 {
3540 while (get_next_display_element (it)
3541 && !newline_found_p)
3542 {
3543 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3544 set_iterator_to_next (it, 0);
3545 }
3546 }
3547 }
3548
54918e2b 3549 it->selective = old_selective;
cafafe0b 3550 return newline_found_p;
5f5c8ee5
GM
3551}
3552
3553
3554/* Set IT's current position to the previous visible line start. Skip
3555 invisible text that is so either due to text properties or due to
3556 selective display. Caution: this does not change IT->current_x and
3557 IT->hpos. */
3558
3559static void
3560back_to_previous_visible_line_start (it)
3561 struct it *it;
3562{
3563 int visible_p = 0;
3564
3565 /* Go back one newline if not on BEGV already. */
3566 if (IT_CHARPOS (*it) > BEGV)
3567 back_to_previous_line_start (it);
3568
3569 /* Move over lines that are invisible because of selective display
3570 or text properties. */
3571 while (IT_CHARPOS (*it) > BEGV
3572 && !visible_p)
3573 {
3574 visible_p = 1;
3575
3576 /* If selective > 0, then lines indented more than that values
3577 are invisible. */
3578 if (it->selective > 0
3579 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3580 it->selective))
3581 visible_p = 0;
5f5c8ee5
GM
3582 else
3583 {
3584 Lisp_Object prop;
3585
6fc556fd
KR
3586 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3587 Qinvisible, it->window);
5f5c8ee5
GM
3588 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3589 visible_p = 0;
3590 }
5f5c8ee5
GM
3591
3592 /* Back one more newline if the current one is invisible. */
3593 if (!visible_p)
3594 back_to_previous_line_start (it);
3595 }
3596
3597 xassert (IT_CHARPOS (*it) >= BEGV);
3598 xassert (IT_CHARPOS (*it) == BEGV
3599 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3600 CHECK_IT (it);
3601}
3602
3603
3604/* Reseat iterator IT at the previous visible line start. Skip
3605 invisible text that is so either due to text properties or due to
3606 selective display. At the end, update IT's overlay information,
3607 face information etc. */
3608
3609static void
3610reseat_at_previous_visible_line_start (it)
3611 struct it *it;
3612{
3613 back_to_previous_visible_line_start (it);
3614 reseat (it, it->current.pos, 1);
3615 CHECK_IT (it);
3616}
3617
3618
3619/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3620 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3621 preceding the line start. Skip over invisible text that is so
3622 because of selective display. Compute faces, overlays etc at the
3623 new position. Note that this function does not skip over text that
3624 is invisible because of text properties. */
5f5c8ee5
GM
3625
3626static void
312246d1 3627reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3628 struct it *it;
312246d1 3629 int on_newline_p;
5f5c8ee5 3630{
cafafe0b
GM
3631 int newline_found_p, skipped_p = 0;
3632
3633 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3634
3635 /* Skip over lines that are invisible because they are indented
3636 more than the value of IT->selective. */
3637 if (it->selective > 0)
3638 while (IT_CHARPOS (*it) < ZV
a77dc1ec 3639 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
cafafe0b 3640 it->selective))
a77dc1ec
GM
3641 {
3642 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3643 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3644 }
cafafe0b
GM
3645
3646 /* Position on the newline if that's what's requested. */
3647 if (on_newline_p && newline_found_p)
5f5c8ee5 3648 {
cafafe0b 3649 if (STRINGP (it->string))
5f5c8ee5 3650 {
cafafe0b
GM
3651 if (IT_STRING_CHARPOS (*it) > 0)
3652 {
3653 --IT_STRING_CHARPOS (*it);
3654 --IT_STRING_BYTEPOS (*it);
3655 }
5f5c8ee5 3656 }
cafafe0b 3657 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
3658 {
3659 --IT_CHARPOS (*it);
cafafe0b
GM
3660 --IT_BYTEPOS (*it);
3661 reseat (it, it->current.pos, 0);
312246d1 3662 }
5f5c8ee5 3663 }
cafafe0b
GM
3664 else if (skipped_p)
3665 reseat (it, it->current.pos, 0);
5f5c8ee5
GM
3666
3667 CHECK_IT (it);
3668}
3669
3670
3671\f
3672/***********************************************************************
3673 Changing an iterator's position
3674***********************************************************************/
3675
3676/* Change IT's current position to POS in current_buffer. If FORCE_P
3677 is non-zero, always check for text properties at the new position.
3678 Otherwise, text properties are only looked up if POS >=
3679 IT->check_charpos of a property. */
3680
3681static void
3682reseat (it, pos, force_p)
3683 struct it *it;
3684 struct text_pos pos;
3685 int force_p;
3686{
3687 int original_pos = IT_CHARPOS (*it);
3688
3689 reseat_1 (it, pos, 0);
3690
3691 /* Determine where to check text properties. Avoid doing it
3692 where possible because text property lookup is very expensive. */
3693 if (force_p
3694 || CHARPOS (pos) > it->stop_charpos
3695 || CHARPOS (pos) < original_pos)
3696 handle_stop (it);
3697
3698 CHECK_IT (it);
3699}
3700
3701
3702/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3703 IT->stop_pos to POS, also. */
3704
3705static void
3706reseat_1 (it, pos, set_stop_p)
3707 struct it *it;
3708 struct text_pos pos;
3709 int set_stop_p;
3710{
3711 /* Don't call this function when scanning a C string. */
3712 xassert (it->s == NULL);
3713
3714 /* POS must be a reasonable value. */
3715 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3716
3717 it->current.pos = it->position = pos;
3718 XSETBUFFER (it->object, current_buffer);
3719 it->dpvec = NULL;
3720 it->current.dpvec_index = -1;
3721 it->current.overlay_string_index = -1;
3722 IT_STRING_CHARPOS (*it) = -1;
3723 IT_STRING_BYTEPOS (*it) = -1;
3724 it->string = Qnil;
3725 it->method = next_element_from_buffer;
3726 it->sp = 0;
4aad61f8 3727 it->face_before_selective_p = 0;
5f5c8ee5
GM
3728
3729 if (set_stop_p)
3730 it->stop_charpos = CHARPOS (pos);
3731}
3732
3733
3734/* Set up IT for displaying a string, starting at CHARPOS in window W.
3735 If S is non-null, it is a C string to iterate over. Otherwise,
3736 STRING gives a Lisp string to iterate over.
3737
3738 If PRECISION > 0, don't return more then PRECISION number of
3739 characters from the string.
3740
3741 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3742 characters have been returned. FIELD_WIDTH < 0 means an infinite
3743 field width.
3744
3745 MULTIBYTE = 0 means disable processing of multibyte characters,
3746 MULTIBYTE > 0 means enable it,
3747 MULTIBYTE < 0 means use IT->multibyte_p.
3748
3749 IT must be initialized via a prior call to init_iterator before
3750 calling this function. */
3751
3752static void
3753reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3754 struct it *it;
3755 unsigned char *s;
3756 Lisp_Object string;
3757 int charpos;
3758 int precision, field_width, multibyte;
3759{
3760 /* No region in strings. */
3761 it->region_beg_charpos = it->region_end_charpos = -1;
3762
3763 /* No text property checks performed by default, but see below. */
3764 it->stop_charpos = -1;
3765
3766 /* Set iterator position and end position. */
3767 bzero (&it->current, sizeof it->current);
3768 it->current.overlay_string_index = -1;
3769 it->current.dpvec_index = -1;
5f5c8ee5
GM
3770 xassert (charpos >= 0);
3771
3772 /* Use the setting of MULTIBYTE if specified. */
3773 if (multibyte >= 0)
3774 it->multibyte_p = multibyte > 0;
3775
3776 if (s == NULL)
3777 {
3778 xassert (STRINGP (string));
3779 it->string = string;
3780 it->s = NULL;
3781 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3782 it->method = next_element_from_string;
3783 it->current.string_pos = string_pos (charpos, string);
3784 }
3785 else
3786 {
3787 it->s = s;
3788 it->string = Qnil;
3789
3790 /* Note that we use IT->current.pos, not it->current.string_pos,
3791 for displaying C strings. */
3792 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3793 if (it->multibyte_p)
3794 {
3795 it->current.pos = c_string_pos (charpos, s, 1);
3796 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3797 }
3798 else
3799 {
3800 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3801 it->end_charpos = it->string_nchars = strlen (s);
3802 }
3803
3804 it->method = next_element_from_c_string;
3805 }
3806
3807 /* PRECISION > 0 means don't return more than PRECISION characters
3808 from the string. */
3809 if (precision > 0 && it->end_charpos - charpos > precision)
3810 it->end_charpos = it->string_nchars = charpos + precision;
3811
3812 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3813 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3814 FIELD_WIDTH < 0 means infinite field width. This is useful for
3815 padding with `-' at the end of a mode line. */
3816 if (field_width < 0)
3817 field_width = INFINITY;
3818 if (field_width > it->end_charpos - charpos)
3819 it->end_charpos = charpos + field_width;
3820
3821 /* Use the standard display table for displaying strings. */
3822 if (DISP_TABLE_P (Vstandard_display_table))
3823 it->dp = XCHAR_TABLE (Vstandard_display_table);
3824
3825 it->stop_charpos = charpos;
3826 CHECK_IT (it);
3827}
3828
3829
3830\f
3831/***********************************************************************
3832 Iteration
3833 ***********************************************************************/
3834
3835/* Load IT's display element fields with information about the next
3836 display element from the current position of IT. Value is zero if
3837 end of buffer (or C string) is reached. */
3838
3839int
3840get_next_display_element (it)
3841 struct it *it;
3842{
3843 /* Non-zero means that we found an display element. Zero means that
3844 we hit the end of what we iterate over. Performance note: the
3845 function pointer `method' used here turns out to be faster than
3846 using a sequence of if-statements. */
3847 int success_p = (*it->method) (it);
5f5c8ee5
GM
3848
3849 if (it->what == IT_CHARACTER)
3850 {
3851 /* Map via display table or translate control characters.
3852 IT->c, IT->len etc. have been set to the next character by
3853 the function call above. If we have a display table, and it
3854 contains an entry for IT->c, translate it. Don't do this if
3855 IT->c itself comes from a display table, otherwise we could
3856 end up in an infinite recursion. (An alternative could be to
3857 count the recursion depth of this function and signal an
3858 error when a certain maximum depth is reached.) Is it worth
3859 it? */
3860 if (success_p && it->dpvec == NULL)
3861 {
3862 Lisp_Object dv;
3863
3864 if (it->dp
3865 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3866 VECTORP (dv)))
3867 {
3868 struct Lisp_Vector *v = XVECTOR (dv);
3869
3870 /* Return the first character from the display table
3871 entry, if not empty. If empty, don't display the
3872 current character. */
3873 if (v->size)
3874 {
3875 it->dpvec_char_len = it->len;
3876 it->dpvec = v->contents;
3877 it->dpend = v->contents + v->size;
3878 it->current.dpvec_index = 0;
3879 it->method = next_element_from_display_vector;
7eee47cc
GM
3880 success_p = get_next_display_element (it);
3881 }
3882 else
3883 {
3884 set_iterator_to_next (it, 0);
3885 success_p = get_next_display_element (it);
5f5c8ee5 3886 }
5f5c8ee5
GM
3887 }
3888
3889 /* Translate control characters into `\003' or `^C' form.
3890 Control characters coming from a display table entry are
3891 currently not translated because we use IT->dpvec to hold
3892 the translation. This could easily be changed but I
197516c2
KH
3893 don't believe that it is worth doing.
3894
3895 Non-printable multibyte characters are also translated
3896 octal form. */
5f5c8ee5
GM
3897 else if ((it->c < ' '
3898 && (it->area != TEXT_AREA
c6e89d6c 3899 || (it->c != '\n' && it->c != '\t')))
54c85a23 3900 || (it->c >= 127
197516c2
KH
3901 && it->len == 1)
3902 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
3903 {
3904 /* IT->c is a control character which must be displayed
3905 either as '\003' or as `^C' where the '\\' and '^'
3906 can be defined in the display table. Fill
3907 IT->ctl_chars with glyphs for what we have to
3908 display. Then, set IT->dpvec to these glyphs. */
3909 GLYPH g;
3910
54c85a23 3911 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3912 {
3913 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3914 if (it->dp
3915 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3916 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3917 g = XINT (DISP_CTRL_GLYPH (it->dp));
3918 else
3919 g = FAST_MAKE_GLYPH ('^', 0);
3920 XSETINT (it->ctl_chars[0], g);
3921
3922 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3923 XSETINT (it->ctl_chars[1], g);
3924
3925 /* Set up IT->dpvec and return first character from it. */
3926 it->dpvec_char_len = it->len;
3927 it->dpvec = it->ctl_chars;
3928 it->dpend = it->dpvec + 2;
3929 it->current.dpvec_index = 0;
3930 it->method = next_element_from_display_vector;
3931 get_next_display_element (it);
3932 }
3933 else
3934 {
260a86a0 3935 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 3936 int len;
197516c2
KH
3937 int i;
3938 GLYPH escape_glyph;
3939
5f5c8ee5
GM
3940 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3941 if (it->dp
3942 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3943 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 3944 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 3945 else
197516c2
KH
3946 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3947
c5924f47
KH
3948 if (SINGLE_BYTE_CHAR_P (it->c))
3949 str[0] = it->c, len = 1;
3950 else
3951 len = CHAR_STRING (it->c, str);
3952
197516c2
KH
3953 for (i = 0; i < len; i++)
3954 {
3955 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3956 /* Insert three more glyphs into IT->ctl_chars for
3957 the octal display of the character. */
3958 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3959 XSETINT (it->ctl_chars[i * 4 + 1], g);
3960 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3961 XSETINT (it->ctl_chars[i * 4 + 2], g);
3962 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3963 XSETINT (it->ctl_chars[i * 4 + 3], g);
3964 }
5f5c8ee5
GM
3965
3966 /* Set up IT->dpvec and return the first character
3967 from it. */
3968 it->dpvec_char_len = it->len;
3969 it->dpvec = it->ctl_chars;
197516c2 3970 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
3971 it->current.dpvec_index = 0;
3972 it->method = next_element_from_display_vector;
3973 get_next_display_element (it);
3974 }
3975 }
3976 }
3977
980806b6
KH
3978 /* Adjust face id for a multibyte character. There are no
3979 multibyte character in unibyte text. */
5f5c8ee5
GM
3980 if (it->multibyte_p
3981 && success_p
980806b6 3982 && FRAME_WINDOW_P (it->f))
5f5c8ee5 3983 {
980806b6
KH
3984 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3985 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
3986 }
3987 }
3988
3989 /* Is this character the last one of a run of characters with
3990 box? If yes, set IT->end_of_box_run_p to 1. */
3991 if (it->face_box_p
3992 && it->s == NULL)
3993 {
3994 int face_id;
3995 struct face *face;
3996
3997 it->end_of_box_run_p
3998 = ((face_id = face_after_it_pos (it),
3999 face_id != it->face_id)
4000 && (face = FACE_FROM_ID (it->f, face_id),
4001 face->box == FACE_NO_BOX));
4002 }
4003
4004 /* Value is 0 if end of buffer or string reached. */
4005 return success_p;
4006}
4007
4008
4009/* Move IT to the next display element.
4010
cafafe0b
GM
4011 RESEAT_P non-zero means if called on a newline in buffer text,
4012 skip to the next visible line start.
4013
5f5c8ee5
GM
4014 Functions get_next_display_element and set_iterator_to_next are
4015 separate because I find this arrangement easier to handle than a
4016 get_next_display_element function that also increments IT's
4017 position. The way it is we can first look at an iterator's current
4018 display element, decide whether it fits on a line, and if it does,
4019 increment the iterator position. The other way around we probably
4020 would either need a flag indicating whether the iterator has to be
4021 incremented the next time, or we would have to implement a
4022 decrement position function which would not be easy to write. */
4023
4024void
cafafe0b 4025set_iterator_to_next (it, reseat_p)
5f5c8ee5 4026 struct it *it;
cafafe0b 4027 int reseat_p;
5f5c8ee5 4028{
483de32b
GM
4029 /* Reset flags indicating start and end of a sequence of characters
4030 with box. Reset them at the start of this function because
4031 moving the iterator to a new position might set them. */
4032 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4033
5f5c8ee5
GM
4034 if (it->method == next_element_from_buffer)
4035 {
4036 /* The current display element of IT is a character from
4037 current_buffer. Advance in the buffer, and maybe skip over
4038 invisible lines that are so because of selective display. */
cafafe0b 4039 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4040 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4041 else
4042 {
4043 xassert (it->len != 0);
4044 IT_BYTEPOS (*it) += it->len;
4045 IT_CHARPOS (*it) += 1;
4046 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4047 }
4048 }
260a86a0
KH
4049 else if (it->method == next_element_from_composition)
4050 {
4051 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4052 if (STRINGP (it->string))
4053 {
4054 IT_STRING_BYTEPOS (*it) += it->len;
4055 IT_STRING_CHARPOS (*it) += it->cmp_len;
4056 it->method = next_element_from_string;
4057 goto consider_string_end;
4058 }
4059 else
4060 {
4061 IT_BYTEPOS (*it) += it->len;
4062 IT_CHARPOS (*it) += it->cmp_len;
4063 it->method = next_element_from_buffer;
4064 }
4065 }
5f5c8ee5
GM
4066 else if (it->method == next_element_from_c_string)
4067 {
4068 /* Current display element of IT is from a C string. */
4069 IT_BYTEPOS (*it) += it->len;
4070 IT_CHARPOS (*it) += 1;
4071 }
4072 else if (it->method == next_element_from_display_vector)
4073 {
4074 /* Current display element of IT is from a display table entry.
4075 Advance in the display table definition. Reset it to null if
4076 end reached, and continue with characters from buffers/
4077 strings. */
4078 ++it->current.dpvec_index;
286bcbc9 4079
980806b6
KH
4080 /* Restore face of the iterator to what they were before the
4081 display vector entry (these entries may contain faces). */
5f5c8ee5 4082 it->face_id = it->saved_face_id;
286bcbc9 4083
5f5c8ee5
GM
4084 if (it->dpvec + it->current.dpvec_index == it->dpend)
4085 {
4086 if (it->s)
4087 it->method = next_element_from_c_string;
4088 else if (STRINGP (it->string))
4089 it->method = next_element_from_string;
4090 else
4091 it->method = next_element_from_buffer;
4092
4093 it->dpvec = NULL;
4094 it->current.dpvec_index = -1;
4095
312246d1
GM
4096 /* Skip over characters which were displayed via IT->dpvec. */
4097 if (it->dpvec_char_len < 0)
4098 reseat_at_next_visible_line_start (it, 1);
4099 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4100 {
4101 it->len = it->dpvec_char_len;
cafafe0b 4102 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4103 }
4104 }
4105 }
4106 else if (it->method == next_element_from_string)
4107 {
4108 /* Current display element is a character from a Lisp string. */
4109 xassert (it->s == NULL && STRINGP (it->string));
4110 IT_STRING_BYTEPOS (*it) += it->len;
4111 IT_STRING_CHARPOS (*it) += 1;
4112
4113 consider_string_end:
4114
4115 if (it->current.overlay_string_index >= 0)
4116 {
4117 /* IT->string is an overlay string. Advance to the
4118 next, if there is one. */
4119 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4120 next_overlay_string (it);
4121 }
4122 else
4123 {
4124 /* IT->string is not an overlay string. If we reached
4125 its end, and there is something on IT->stack, proceed
4126 with what is on the stack. This can be either another
4127 string, this time an overlay string, or a buffer. */
4128 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4129 && it->sp > 0)
4130 {
4131 pop_it (it);
4132 if (!STRINGP (it->string))
4133 it->method = next_element_from_buffer;
4134 }
4135 }
4136 }
4137 else if (it->method == next_element_from_image
4138 || it->method == next_element_from_stretch)
4139 {
4140 /* The position etc with which we have to proceed are on
4141 the stack. The position may be at the end of a string,
4142 if the `display' property takes up the whole string. */
4143 pop_it (it);
4144 it->image_id = 0;
4145 if (STRINGP (it->string))
4146 {
4147 it->method = next_element_from_string;
4148 goto consider_string_end;
4149 }
4150 else
4151 it->method = next_element_from_buffer;
4152 }
4153 else
4154 /* There are no other methods defined, so this should be a bug. */
4155 abort ();
4156
5f5c8ee5
GM
4157 xassert (it->method != next_element_from_string
4158 || (STRINGP (it->string)
4159 && IT_STRING_CHARPOS (*it) >= 0));
4160}
4161
4162
4163/* Load IT's display element fields with information about the next
4164 display element which comes from a display table entry or from the
4165 result of translating a control character to one of the forms `^C'
4166 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4167
4168static int
4169next_element_from_display_vector (it)
4170 struct it *it;
4171{
4172 /* Precondition. */
4173 xassert (it->dpvec && it->current.dpvec_index >= 0);
4174
4175 /* Remember the current face id in case glyphs specify faces.
4176 IT's face is restored in set_iterator_to_next. */
4177 it->saved_face_id = it->face_id;
4178
4179 if (INTEGERP (*it->dpvec)
4180 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4181 {
4182 int lface_id;
4183 GLYPH g;
4184
4185 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4186 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4187 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4188
4189 /* The entry may contain a face id to use. Such a face id is
4190 the id of a Lisp face, not a realized face. A face id of
969065c3 4191 zero means no face is specified. */
5f5c8ee5
GM
4192 lface_id = FAST_GLYPH_FACE (g);
4193 if (lface_id)
4194 {
969065c3 4195 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4196 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4197 if (face_id >= 0)
969065c3 4198 it->face_id = face_id;
5f5c8ee5
GM
4199 }
4200 }
4201 else
4202 /* Display table entry is invalid. Return a space. */
4203 it->c = ' ', it->len = 1;
4204
4205 /* Don't change position and object of the iterator here. They are
4206 still the values of the character that had this display table
4207 entry or was translated, and that's what we want. */
4208 it->what = IT_CHARACTER;
4209 return 1;
4210}
4211
4212
4213/* Load IT with the next display element from Lisp string IT->string.
4214 IT->current.string_pos is the current position within the string.
4215 If IT->current.overlay_string_index >= 0, the Lisp string is an
4216 overlay string. */
4217
4218static int
4219next_element_from_string (it)
4220 struct it *it;
4221{
4222 struct text_pos position;
4223
4224 xassert (STRINGP (it->string));
4225 xassert (IT_STRING_CHARPOS (*it) >= 0);
4226 position = it->current.string_pos;
4227
4228 /* Time to check for invisible text? */
4229 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4230 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4231 {
4232 handle_stop (it);
4233
4234 /* Since a handler may have changed IT->method, we must
4235 recurse here. */
4236 return get_next_display_element (it);
4237 }
4238
4239 if (it->current.overlay_string_index >= 0)
4240 {
4241 /* Get the next character from an overlay string. In overlay
4242 strings, There is no field width or padding with spaces to
4243 do. */
4244 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4245 {
4246 it->what = IT_EOB;
4247 return 0;
4248 }
4249 else if (STRING_MULTIBYTE (it->string))
4250 {
4251 int remaining = (STRING_BYTES (XSTRING (it->string))
4252 - IT_STRING_BYTEPOS (*it));
4253 unsigned char *s = (XSTRING (it->string)->data
4254 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4255 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4256 }
4257 else
4258 {
4259 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4260 it->len = 1;
4261 }
4262 }
4263 else
4264 {
4265 /* Get the next character from a Lisp string that is not an
4266 overlay string. Such strings come from the mode line, for
4267 example. We may have to pad with spaces, or truncate the
4268 string. See also next_element_from_c_string. */
4269 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4270 {
4271 it->what = IT_EOB;
4272 return 0;
4273 }
4274 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4275 {
4276 /* Pad with spaces. */
4277 it->c = ' ', it->len = 1;
4278 CHARPOS (position) = BYTEPOS (position) = -1;
4279 }
4280 else if (STRING_MULTIBYTE (it->string))
4281 {
4282 int maxlen = (STRING_BYTES (XSTRING (it->string))
4283 - IT_STRING_BYTEPOS (*it));
4284 unsigned char *s = (XSTRING (it->string)->data
4285 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4286 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4287 }
4288 else
4289 {
4290 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4291 it->len = 1;
4292 }
4293 }
4294
4295 /* Record what we have and where it came from. Note that we store a
4296 buffer position in IT->position although it could arguably be a
4297 string position. */
4298 it->what = IT_CHARACTER;
4299 it->object = it->string;
4300 it->position = position;
4301 return 1;
4302}
4303
4304
4305/* Load IT with next display element from C string IT->s.
4306 IT->string_nchars is the maximum number of characters to return
4307 from the string. IT->end_charpos may be greater than
4308 IT->string_nchars when this function is called, in which case we
4309 may have to return padding spaces. Value is zero if end of string
4310 reached, including padding spaces. */
4311
4312static int
4313next_element_from_c_string (it)
4314 struct it *it;
4315{
4316 int success_p = 1;
4317
4318 xassert (it->s);
4319 it->what = IT_CHARACTER;
4320 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4321 it->object = Qnil;
4322
4323 /* IT's position can be greater IT->string_nchars in case a field
4324 width or precision has been specified when the iterator was
4325 initialized. */
4326 if (IT_CHARPOS (*it) >= it->end_charpos)
4327 {
4328 /* End of the game. */
4329 it->what = IT_EOB;
4330 success_p = 0;
4331 }
4332 else if (IT_CHARPOS (*it) >= it->string_nchars)
4333 {
4334 /* Pad with spaces. */
4335 it->c = ' ', it->len = 1;
4336 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4337 }
4338 else if (it->multibyte_p)
4339 {
4340 /* Implementation note: The calls to strlen apparently aren't a
4341 performance problem because there is no noticeable performance
4342 difference between Emacs running in unibyte or multibyte mode. */
4343 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4344 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4345 maxlen, &it->len);
5f5c8ee5
GM
4346 }
4347 else
4348 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4349
4350 return success_p;
4351}
4352
4353
4354/* Set up IT to return characters from an ellipsis, if appropriate.
4355 The definition of the ellipsis glyphs may come from a display table
4356 entry. This function Fills IT with the first glyph from the
4357 ellipsis if an ellipsis is to be displayed. */
4358
13f19968 4359static int
5f5c8ee5
GM
4360next_element_from_ellipsis (it)
4361 struct it *it;
4362{
13f19968 4363 if (it->selective_display_ellipsis_p)
5f5c8ee5 4364 {
13f19968
GM
4365 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4366 {
4367 /* Use the display table definition for `...'. Invalid glyphs
4368 will be handled by the method returning elements from dpvec. */
4369 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4370 it->dpvec_char_len = it->len;
4371 it->dpvec = v->contents;
4372 it->dpend = v->contents + v->size;
4373 it->current.dpvec_index = 0;
4374 it->method = next_element_from_display_vector;
4375 }
4376 else
4377 {
4378 /* Use default `...' which is stored in default_invis_vector. */
4379 it->dpvec_char_len = it->len;
4380 it->dpvec = default_invis_vector;
4381 it->dpend = default_invis_vector + 3;
4382 it->current.dpvec_index = 0;
4383 it->method = next_element_from_display_vector;
4384 }
5f5c8ee5 4385 }
13f19968 4386 else
54918e2b 4387 {
4aad61f8
GM
4388 /* The face at the current position may be different from the
4389 face we find after the invisible text. Remember what it
4390 was in IT->saved_face_id, and signal that it's there by
4391 setting face_before_selective_p. */
4392 it->saved_face_id = it->face_id;
54918e2b
GM
4393 it->method = next_element_from_buffer;
4394 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4395 it->face_before_selective_p = 1;
54918e2b 4396 }
13f19968
GM
4397
4398 return get_next_display_element (it);
5f5c8ee5
GM
4399}
4400
4401
4402/* Deliver an image display element. The iterator IT is already
4403 filled with image information (done in handle_display_prop). Value
4404 is always 1. */
4405
4406
4407static int
4408next_element_from_image (it)
4409 struct it *it;
4410{
4411 it->what = IT_IMAGE;
4412 return 1;
4413}
4414
4415
4416/* Fill iterator IT with next display element from a stretch glyph
4417 property. IT->object is the value of the text property. Value is
4418 always 1. */
4419
4420static int
4421next_element_from_stretch (it)
4422 struct it *it;
4423{
4424 it->what = IT_STRETCH;
4425 return 1;
4426}
4427
4428
4429/* Load IT with the next display element from current_buffer. Value
4430 is zero if end of buffer reached. IT->stop_charpos is the next
4431 position at which to stop and check for text properties or buffer
4432 end. */
4433
4434static int
4435next_element_from_buffer (it)
4436 struct it *it;
4437{
4438 int success_p = 1;
4439
4440 /* Check this assumption, otherwise, we would never enter the
4441 if-statement, below. */
4442 xassert (IT_CHARPOS (*it) >= BEGV
4443 && IT_CHARPOS (*it) <= it->stop_charpos);
4444
4445 if (IT_CHARPOS (*it) >= it->stop_charpos)
4446 {
4447 if (IT_CHARPOS (*it) >= it->end_charpos)
4448 {
4449 int overlay_strings_follow_p;
4450
4451 /* End of the game, except when overlay strings follow that
4452 haven't been returned yet. */
4453 if (it->overlay_strings_at_end_processed_p)
4454 overlay_strings_follow_p = 0;
4455 else
4456 {
4457 it->overlay_strings_at_end_processed_p = 1;
c880678e 4458 overlay_strings_follow_p = get_overlay_strings (it);
5f5c8ee5
GM
4459 }
4460
4461 if (overlay_strings_follow_p)
4462 success_p = get_next_display_element (it);
4463 else
4464 {
4465 it->what = IT_EOB;
4466 it->position = it->current.pos;
4467 success_p = 0;
4468 }
4469 }
4470 else
4471 {
4472 handle_stop (it);
4473 return get_next_display_element (it);
4474 }
4475 }
4476 else
4477 {
4478 /* No face changes, overlays etc. in sight, so just return a
4479 character from current_buffer. */
4480 unsigned char *p;
4481
4482 /* Maybe run the redisplay end trigger hook. Performance note:
4483 This doesn't seem to cost measurable time. */
4484 if (it->redisplay_end_trigger_charpos
4485 && it->glyph_row
4486 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4487 run_redisplay_end_trigger_hook (it);
4488
4489 /* Get the next character, maybe multibyte. */
4490 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4491 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4492 {
4493 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4494 - IT_BYTEPOS (*it));
4fdb80f2 4495 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4496 }
4497 else
4498 it->c = *p, it->len = 1;
4499
4500 /* Record what we have and where it came from. */
4501 it->what = IT_CHARACTER;;
4502 it->object = it->w->buffer;
4503 it->position = it->current.pos;
4504
4505 /* Normally we return the character found above, except when we
4506 really want to return an ellipsis for selective display. */
4507 if (it->selective)
4508 {
4509 if (it->c == '\n')
4510 {
4511 /* A value of selective > 0 means hide lines indented more
4512 than that number of columns. */
4513 if (it->selective > 0
4514 && IT_CHARPOS (*it) + 1 < ZV
4515 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4516 IT_BYTEPOS (*it) + 1,
4517 it->selective))
312246d1 4518 {
13f19968 4519 success_p = next_element_from_ellipsis (it);
312246d1
GM
4520 it->dpvec_char_len = -1;
4521 }
5f5c8ee5
GM
4522 }
4523 else if (it->c == '\r' && it->selective == -1)
4524 {
4525 /* A value of selective == -1 means that everything from the
4526 CR to the end of the line is invisible, with maybe an
4527 ellipsis displayed for it. */
13f19968 4528 success_p = next_element_from_ellipsis (it);
312246d1 4529 it->dpvec_char_len = -1;
5f5c8ee5
GM
4530 }
4531 }
4532 }
4533
4534 /* Value is zero if end of buffer reached. */
c880678e 4535 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4536 return success_p;
4537}
4538
4539
4540/* Run the redisplay end trigger hook for IT. */
4541
4542static void
4543run_redisplay_end_trigger_hook (it)
4544 struct it *it;
4545{
4546 Lisp_Object args[3];
4547
4548 /* IT->glyph_row should be non-null, i.e. we should be actually
4549 displaying something, or otherwise we should not run the hook. */
4550 xassert (it->glyph_row);
4551
4552 /* Set up hook arguments. */
4553 args[0] = Qredisplay_end_trigger_functions;
4554 args[1] = it->window;
4555 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4556 it->redisplay_end_trigger_charpos = 0;
4557
4558 /* Since we are *trying* to run these functions, don't try to run
4559 them again, even if they get an error. */
4560 it->w->redisplay_end_trigger = Qnil;
4561 Frun_hook_with_args (3, args);
4562
4563 /* Notice if it changed the face of the character we are on. */
4564 handle_face_prop (it);
4565}
4566
4567
260a86a0
KH
4568/* Deliver a composition display element. The iterator IT is already
4569 filled with composition information (done in
4570 handle_composition_prop). Value is always 1. */
4571
4572static int
4573next_element_from_composition (it)
4574 struct it *it;
4575{
4576 it->what = IT_COMPOSITION;
4577 it->position = (STRINGP (it->string)
4578 ? it->current.string_pos
4579 : it->current.pos);
4580 return 1;
4581}
4582
4583
5f5c8ee5
GM
4584\f
4585/***********************************************************************
4586 Moving an iterator without producing glyphs
4587 ***********************************************************************/
4588
4589/* Move iterator IT to a specified buffer or X position within one
4590 line on the display without producing glyphs.
4591
4592 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4593 whichever is reached first.
4594
4595 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4596
4597 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4598 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4599 TO_X includes the amount by which a window is horizontally
4600 scrolled.
4601
4602 Value is
4603
4604 MOVE_POS_MATCH_OR_ZV
4605 - when TO_POS or ZV was reached.
4606
4607 MOVE_X_REACHED
4608 -when TO_X was reached before TO_POS or ZV were reached.
4609
4610 MOVE_LINE_CONTINUED
4611 - when we reached the end of the display area and the line must
4612 be continued.
4613
4614 MOVE_LINE_TRUNCATED
4615 - when we reached the end of the display area and the line is
4616 truncated.
4617
4618 MOVE_NEWLINE_OR_CR
4619 - when we stopped at a line end, i.e. a newline or a CR and selective
4620 display is on. */
4621
701552dd 4622static enum move_it_result
5f5c8ee5
GM
4623move_it_in_display_line_to (it, to_charpos, to_x, op)
4624 struct it *it;
4625 int to_charpos, to_x, op;
4626{
4627 enum move_it_result result = MOVE_UNDEFINED;
4628 struct glyph_row *saved_glyph_row;
4629
4630 /* Don't produce glyphs in produce_glyphs. */
4631 saved_glyph_row = it->glyph_row;
4632 it->glyph_row = NULL;
4633
5f5c8ee5
GM
4634 while (1)
4635 {
ae26e27d 4636 int x, i, ascent = 0, descent = 0;
5f5c8ee5
GM
4637
4638 /* Stop when ZV or TO_CHARPOS reached. */
4639 if (!get_next_display_element (it)
4640 || ((op & MOVE_TO_POS) != 0
4641 && BUFFERP (it->object)
4642 && IT_CHARPOS (*it) >= to_charpos))
4643 {
4644 result = MOVE_POS_MATCH_OR_ZV;
4645 break;
4646 }
4647
4648 /* The call to produce_glyphs will get the metrics of the
4649 display element IT is loaded with. We record in x the
4650 x-position before this display element in case it does not
4651 fit on the line. */
4652 x = it->current_x;
47589c8c
GM
4653
4654 /* Remember the line height so far in case the next element doesn't
4655 fit on the line. */
4656 if (!it->truncate_lines_p)
4657 {
4658 ascent = it->max_ascent;
4659 descent = it->max_descent;
4660 }
4661
5f5c8ee5
GM
4662 PRODUCE_GLYPHS (it);
4663
4664 if (it->area != TEXT_AREA)
4665 {
cafafe0b 4666 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4667 continue;
4668 }
4669
4670 /* The number of glyphs we get back in IT->nglyphs will normally
4671 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4672 character on a terminal frame, or (iii) a line end. For the
4673 second case, IT->nglyphs - 1 padding glyphs will be present
4674 (on X frames, there is only one glyph produced for a
4675 composite character.
4676
4677 The behavior implemented below means, for continuation lines,
4678 that as many spaces of a TAB as fit on the current line are
4679 displayed there. For terminal frames, as many glyphs of a
4680 multi-glyph character are displayed in the current line, too.
4681 This is what the old redisplay code did, and we keep it that
4682 way. Under X, the whole shape of a complex character must
4683 fit on the line or it will be completely displayed in the
4684 next line.
4685
4686 Note that both for tabs and padding glyphs, all glyphs have
4687 the same width. */
4688 if (it->nglyphs)
4689 {
4690 /* More than one glyph or glyph doesn't fit on line. All
4691 glyphs have the same width. */
4692 int single_glyph_width = it->pixel_width / it->nglyphs;
4693 int new_x;
4694
4695 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4696 {
4697 new_x = x + single_glyph_width;
4698
4699 /* We want to leave anything reaching TO_X to the caller. */
4700 if ((op & MOVE_TO_X) && new_x > to_x)
4701 {
4702 it->current_x = x;
4703 result = MOVE_X_REACHED;
4704 break;
4705 }
4706 else if (/* Lines are continued. */
4707 !it->truncate_lines_p
4708 && (/* And glyph doesn't fit on the line. */
4709 new_x > it->last_visible_x
4710 /* Or it fits exactly and we're on a window
4711 system frame. */
4712 || (new_x == it->last_visible_x
4713 && FRAME_WINDOW_P (it->f))))
4714 {
4715 if (/* IT->hpos == 0 means the very first glyph
4716 doesn't fit on the line, e.g. a wide image. */
4717 it->hpos == 0
4718 || (new_x == it->last_visible_x
4719 && FRAME_WINDOW_P (it->f)))
4720 {
4721 ++it->hpos;
4722 it->current_x = new_x;
4723 if (i == it->nglyphs - 1)
cafafe0b 4724 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4725 }
4726 else
47589c8c
GM
4727 {
4728 it->current_x = x;
4729 it->max_ascent = ascent;
4730 it->max_descent = descent;
4731 }
4732
4733 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4734 IT_CHARPOS (*it)));
5f5c8ee5
GM
4735 result = MOVE_LINE_CONTINUED;
4736 break;
4737 }
4738 else if (new_x > it->first_visible_x)
4739 {
4740 /* Glyph is visible. Increment number of glyphs that
4741 would be displayed. */
4742 ++it->hpos;
4743 }
4744 else
4745 {
4746 /* Glyph is completely off the left margin of the display
4747 area. Nothing to do. */
4748 }
4749 }
4750
4751 if (result != MOVE_UNDEFINED)
4752 break;
4753 }
4754 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4755 {
4756 /* Stop when TO_X specified and reached. This check is
4757 necessary here because of lines consisting of a line end,
4758 only. The line end will not produce any glyphs and we
4759 would never get MOVE_X_REACHED. */
4760 xassert (it->nglyphs == 0);
4761 result = MOVE_X_REACHED;
4762 break;
4763 }
4764
4765 /* Is this a line end? If yes, we're done. */
4766 if (ITERATOR_AT_END_OF_LINE_P (it))
4767 {
4768 result = MOVE_NEWLINE_OR_CR;
4769 break;
4770 }
4771
4772 /* The current display element has been consumed. Advance
4773 to the next. */
cafafe0b 4774 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4775
4776 /* Stop if lines are truncated and IT's current x-position is
4777 past the right edge of the window now. */
4778 if (it->truncate_lines_p
4779 && it->current_x >= it->last_visible_x)
4780 {
4781 result = MOVE_LINE_TRUNCATED;
4782 break;
4783 }
4784 }
4785
4786 /* Restore the iterator settings altered at the beginning of this
4787 function. */
4788 it->glyph_row = saved_glyph_row;
4789 return result;
4790}
4791
4792
4793/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4794 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4795 the description of enum move_operation_enum.
4796
4797 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4798 screen line, this function will set IT to the next position >
4799 TO_CHARPOS. */
4800
4801void
4802move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4803 struct it *it;
4804 int to_charpos, to_x, to_y, to_vpos;
4805 int op;
4806{
4807 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4808 int line_height;
47589c8c 4809 int reached = 0;
5f5c8ee5 4810
47589c8c 4811 for (;;)
5f5c8ee5
GM
4812 {
4813 if (op & MOVE_TO_VPOS)
4814 {
4815 /* If no TO_CHARPOS and no TO_X specified, stop at the
4816 start of the line TO_VPOS. */
4817 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4818 {
4819 if (it->vpos == to_vpos)
47589c8c
GM
4820 {
4821 reached = 1;
4822 break;
4823 }
4824 else
4825 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
4826 }
4827 else
4828 {
4829 /* TO_VPOS >= 0 means stop at TO_X in the line at
4830 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
4831 if (it->vpos == to_vpos)
4832 {
4833 reached = 2;
4834 break;
4835 }
4836
5f5c8ee5
GM
4837 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4838
4839 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
4840 {
4841 reached = 3;
4842 break;
4843 }
5f5c8ee5
GM
4844 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4845 {
4846 /* We have reached TO_X but not in the line we want. */
4847 skip = move_it_in_display_line_to (it, to_charpos,
4848 -1, MOVE_TO_POS);
4849 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
4850 {
4851 reached = 4;
4852 break;
4853 }
5f5c8ee5
GM
4854 }
4855 }
4856 }
4857 else if (op & MOVE_TO_Y)
4858 {
4859 struct it it_backup;
5f5c8ee5
GM
4860
4861 /* TO_Y specified means stop at TO_X in the line containing
4862 TO_Y---or at TO_CHARPOS if this is reached first. The
4863 problem is that we can't really tell whether the line
4864 contains TO_Y before we have completely scanned it, and
4865 this may skip past TO_X. What we do is to first scan to
4866 TO_X.
4867
4868 If TO_X is not specified, use a TO_X of zero. The reason
4869 is to make the outcome of this function more predictable.
4870 If we didn't use TO_X == 0, we would stop at the end of
4871 the line which is probably not what a caller would expect
4872 to happen. */
4873 skip = move_it_in_display_line_to (it, to_charpos,
4874 ((op & MOVE_TO_X)
4875 ? to_x : 0),
4876 (MOVE_TO_X
4877 | (op & MOVE_TO_POS)));
4878
4879 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4880 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
4881 {
4882 reached = 5;
4883 break;
4884 }
5f5c8ee5
GM
4885
4886 /* If TO_X was reached, we would like to know whether TO_Y
4887 is in the line. This can only be said if we know the
4888 total line height which requires us to scan the rest of
4889 the line. */
5f5c8ee5
GM
4890 if (skip == MOVE_X_REACHED)
4891 {
4892 it_backup = *it;
47589c8c 4893 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
4894 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4895 op & MOVE_TO_POS);
47589c8c 4896 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
4897 }
4898
4899 /* Now, decide whether TO_Y is in this line. */
4900 line_height = it->max_ascent + it->max_descent;
47589c8c 4901 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5f5c8ee5
GM
4902
4903 if (to_y >= it->current_y
4904 && to_y < it->current_y + line_height)
4905 {
4906 if (skip == MOVE_X_REACHED)
4907 /* If TO_Y is in this line and TO_X was reached above,
4908 we scanned too far. We have to restore IT's settings
4909 to the ones before skipping. */
4910 *it = it_backup;
47589c8c 4911 reached = 6;
5f5c8ee5
GM
4912 }
4913 else if (skip == MOVE_X_REACHED)
4914 {
4915 skip = skip2;
4916 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 4917 reached = 7;
5f5c8ee5
GM
4918 }
4919
47589c8c 4920 if (reached)
5f5c8ee5
GM
4921 break;
4922 }
4923 else
4924 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4925
4926 switch (skip)
4927 {
4928 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
4929 reached = 8;
4930 goto out;
5f5c8ee5
GM
4931
4932 case MOVE_NEWLINE_OR_CR:
cafafe0b 4933 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4934 it->continuation_lines_width = 0;
4935 break;
4936
4937 case MOVE_LINE_TRUNCATED:
4938 it->continuation_lines_width = 0;
312246d1 4939 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4940 if ((op & MOVE_TO_POS) != 0
4941 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
4942 {
4943 reached = 9;
4944 goto out;
4945 }
5f5c8ee5
GM
4946 break;
4947
4948 case MOVE_LINE_CONTINUED:
4949 it->continuation_lines_width += it->current_x;
4950 break;
4951
4952 default:
4953 abort ();
4954 }
4955
4956 /* Reset/increment for the next run. */
4957 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4958 it->current_x = it->hpos = 0;
4959 it->current_y += it->max_ascent + it->max_descent;
4960 ++it->vpos;
4961 last_height = it->max_ascent + it->max_descent;
4962 last_max_ascent = it->max_ascent;
4963 it->max_ascent = it->max_descent = 0;
4964 }
47589c8c
GM
4965
4966 out:
4967
4968 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
4969}
4970
4971
4972/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4973
4974 If DY > 0, move IT backward at least that many pixels. DY = 0
4975 means move IT backward to the preceding line start or BEGV. This
4976 function may move over more than DY pixels if IT->current_y - DY
4977 ends up in the middle of a line; in this case IT->current_y will be
4978 set to the top of the line moved to. */
4979
4980void
4981move_it_vertically_backward (it, dy)
4982 struct it *it;
4983 int dy;
4984{
4985 int nlines, h, line_height;
4986 struct it it2;
4987 int start_pos = IT_CHARPOS (*it);
4988
4989 xassert (dy >= 0);
4990
4991 /* Estimate how many newlines we must move back. */
4992 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4993
4994 /* Set the iterator's position that many lines back. */
4995 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4996 back_to_previous_visible_line_start (it);
4997
4998 /* Reseat the iterator here. When moving backward, we don't want
4999 reseat to skip forward over invisible text, set up the iterator
5000 to deliver from overlay strings at the new position etc. So,
5001 use reseat_1 here. */
5002 reseat_1 (it, it->current.pos, 1);
5003
5004 /* We are now surely at a line start. */
5005 it->current_x = it->hpos = 0;
5006
5007 /* Move forward and see what y-distance we moved. First move to the
5008 start of the next line so that we get its height. We need this
5009 height to be able to tell whether we reached the specified
5010 y-distance. */
5011 it2 = *it;
5012 it2.max_ascent = it2.max_descent = 0;
5013 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5014 MOVE_TO_POS | MOVE_TO_VPOS);
5015 xassert (IT_CHARPOS (*it) >= BEGV);
5016 line_height = it2.max_ascent + it2.max_descent;
47589c8c 5017
5f5c8ee5
GM
5018 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5019 xassert (IT_CHARPOS (*it) >= BEGV);
5020 h = it2.current_y - it->current_y;
5021 nlines = it2.vpos - it->vpos;
5022
5023 /* Correct IT's y and vpos position. */
5024 it->vpos -= nlines;
5025 it->current_y -= h;
5026
5027 if (dy == 0)
5028 {
5029 /* DY == 0 means move to the start of the screen line. The
5030 value of nlines is > 0 if continuation lines were involved. */
5031 if (nlines > 0)
5032 move_it_by_lines (it, nlines, 1);
5033 xassert (IT_CHARPOS (*it) <= start_pos);
5034 }
5035 else if (nlines)
5036 {
5037 /* The y-position we try to reach. Note that h has been
5038 subtracted in front of the if-statement. */
5039 int target_y = it->current_y + h - dy;
5040
5041 /* If we did not reach target_y, try to move further backward if
5042 we can. If we moved too far backward, try to move forward. */
5043 if (target_y < it->current_y
5044 && IT_CHARPOS (*it) > BEGV)
5045 {
5046 move_it_vertically (it, target_y - it->current_y);
5047 xassert (IT_CHARPOS (*it) >= BEGV);
5048 }
5049 else if (target_y >= it->current_y + line_height
5050 && IT_CHARPOS (*it) < ZV)
5051 {
5052 move_it_vertically (it, target_y - (it->current_y + line_height));
5053 xassert (IT_CHARPOS (*it) >= BEGV);
5054 }
5055 }
5056}
5057
5058
5059/* Move IT by a specified amount of pixel lines DY. DY negative means
5060 move backwards. DY = 0 means move to start of screen line. At the
5061 end, IT will be on the start of a screen line. */
5062
5063void
5064move_it_vertically (it, dy)
5065 struct it *it;
5066 int dy;
5067{
5068 if (dy <= 0)
5069 move_it_vertically_backward (it, -dy);
5070 else if (dy > 0)
5071 {
47589c8c 5072 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5073 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5074 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5075 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5076
5077 /* If buffer ends in ZV without a newline, move to the start of
5078 the line to satisfy the post-condition. */
5079 if (IT_CHARPOS (*it) == ZV
5080 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5081 move_it_by_lines (it, 0, 0);
5082 }
5083}
5084
5085
47fc2c10
GM
5086/* Move iterator IT past the end of the text line it is in. */
5087
5088void
5089move_it_past_eol (it)
5090 struct it *it;
5091{
5092 enum move_it_result rc;
5093
5094 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5095 if (rc == MOVE_NEWLINE_OR_CR)
5096 set_iterator_to_next (it, 0);
5097}
5098
5099
2c79b732
GM
5100#if 0 /* Currently not used. */
5101
5f5c8ee5
GM
5102/* Return non-zero if some text between buffer positions START_CHARPOS
5103 and END_CHARPOS is invisible. IT->window is the window for text
5104 property lookup. */
5105
5106static int
5107invisible_text_between_p (it, start_charpos, end_charpos)
5108 struct it *it;
5109 int start_charpos, end_charpos;
5110{
5f5c8ee5
GM
5111 Lisp_Object prop, limit;
5112 int invisible_found_p;
5113
5114 xassert (it != NULL && start_charpos <= end_charpos);
5115
5116 /* Is text at START invisible? */
5117 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5118 it->window);
5119 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5120 invisible_found_p = 1;
5121 else
5122 {
016b5642
MB
5123 limit = Fnext_single_char_property_change (make_number (start_charpos),
5124 Qinvisible, Qnil,
5125 make_number (end_charpos));
5f5c8ee5
GM
5126 invisible_found_p = XFASTINT (limit) < end_charpos;
5127 }
5128
5129 return invisible_found_p;
5f5c8ee5
GM
5130}
5131
2c79b732
GM
5132#endif /* 0 */
5133
5f5c8ee5
GM
5134
5135/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5136 negative means move up. DVPOS == 0 means move to the start of the
5137 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5138 NEED_Y_P is zero, IT->current_y will be left unchanged.
5139
5140 Further optimization ideas: If we would know that IT->f doesn't use
5141 a face with proportional font, we could be faster for
5142 truncate-lines nil. */
5143
5144void
5145move_it_by_lines (it, dvpos, need_y_p)
5146 struct it *it;
5147 int dvpos, need_y_p;
5148{
5149 struct position pos;
5150
5151 if (!FRAME_WINDOW_P (it->f))
5152 {
5153 struct text_pos textpos;
5154
5155 /* We can use vmotion on frames without proportional fonts. */
5156 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5157 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5158 reseat (it, textpos, 1);
5159 it->vpos += pos.vpos;
5160 it->current_y += pos.vpos;
5161 }
5162 else if (dvpos == 0)
5163 {
5164 /* DVPOS == 0 means move to the start of the screen line. */
5165 move_it_vertically_backward (it, 0);
5166 xassert (it->current_x == 0 && it->hpos == 0);
5167 }
5168 else if (dvpos > 0)
2c79b732 5169 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5170 else
5171 {
5172 struct it it2;
5173 int start_charpos, i;
2c79b732 5174
5f5c8ee5
GM
5175 /* Go back -DVPOS visible lines and reseat the iterator there. */
5176 start_charpos = IT_CHARPOS (*it);
5177 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5178 back_to_previous_visible_line_start (it);
5179 reseat (it, it->current.pos, 1);
5180 it->current_x = it->hpos = 0;
5181
5182 /* Above call may have moved too far if continuation lines
5183 are involved. Scan forward and see if it did. */
5184 it2 = *it;
5185 it2.vpos = it2.current_y = 0;
5186 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5187 it->vpos -= it2.vpos;
5188 it->current_y -= it2.current_y;
5189 it->current_x = it->hpos = 0;
5190
5191 /* If we moved too far, move IT some lines forward. */
5192 if (it2.vpos > -dvpos)
5193 {
5194 int delta = it2.vpos + dvpos;
5195 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5196 }
5197 }
5198}
5199
5200
5201\f
5202/***********************************************************************
5203 Messages
5204 ***********************************************************************/
5205
5206
937248bc
GM
5207/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5208 to *Messages*. */
5209
5210void
5211add_to_log (format, arg1, arg2)
5212 char *format;
5213 Lisp_Object arg1, arg2;
5214{
5215 Lisp_Object args[3];
5216 Lisp_Object msg, fmt;
5217 char *buffer;
5218 int len;
5219 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5220
5221 fmt = msg = Qnil;
5222 GCPRO4 (fmt, msg, arg1, arg2);
5223
5224 args[0] = fmt = build_string (format);
5225 args[1] = arg1;
5226 args[2] = arg2;
6fc556fd 5227 msg = Fformat (3, args);
937248bc
GM
5228
5229 len = STRING_BYTES (XSTRING (msg)) + 1;
5230 buffer = (char *) alloca (len);
5231 strcpy (buffer, XSTRING (msg)->data);
5232
796184bc 5233 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5234 UNGCPRO;
5235}
5236
5237
5f5c8ee5
GM
5238/* Output a newline in the *Messages* buffer if "needs" one. */
5239
5240void
5241message_log_maybe_newline ()
5242{
5243 if (message_log_need_newline)
5244 message_dolog ("", 0, 1, 0);
5245}
5246
5247
1e313f28 5248/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5249 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5250 nonzero, means interpret the contents of M as multibyte. This
5251 function calls low-level routines in order to bypass text property
5252 hooks, etc. which might not be safe to run. */
5253
5254void
1e313f28 5255message_dolog (m, nbytes, nlflag, multibyte)
5f5c8ee5 5256 char *m;
1e313f28 5257 int nbytes, nlflag, multibyte;
5f5c8ee5
GM
5258{
5259 if (!NILP (Vmessage_log_max))
5260 {
5261 struct buffer *oldbuf;
5262 Lisp_Object oldpoint, oldbegv, oldzv;
5263 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5264 int point_at_end = 0;
5265 int zv_at_end = 0;
5266 Lisp_Object old_deactivate_mark, tem;
5267 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5268
5269 old_deactivate_mark = Vdeactivate_mark;
5270 oldbuf = current_buffer;
6a94510a 5271 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5272 current_buffer->undo_list = Qt;
5273
5274 oldpoint = Fpoint_marker ();
5275 oldbegv = Fpoint_min_marker ();
5276 oldzv = Fpoint_max_marker ();
5277 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5278
5279 if (PT == Z)
5280 point_at_end = 1;
5281 if (ZV == Z)
5282 zv_at_end = 1;
5283
5284 BEGV = BEG;
5285 BEGV_BYTE = BEG_BYTE;
5286 ZV = Z;
5287 ZV_BYTE = Z_BYTE;
5288 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5289
5290 /* Insert the string--maybe converting multibyte to single byte
5291 or vice versa, so that all the text fits the buffer. */
5292 if (multibyte
5293 && NILP (current_buffer->enable_multibyte_characters))
5294 {
1e313f28 5295 int i, c, char_bytes;
5f5c8ee5
GM
5296 unsigned char work[1];
5297
5298 /* Convert a multibyte string to single-byte
5299 for the *Message* buffer. */
1e313f28 5300 for (i = 0; i < nbytes; i += nbytes)
5f5c8ee5 5301 {
1e313f28 5302 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5303 work[0] = (SINGLE_BYTE_CHAR_P (c)
5304 ? c
5305 : multibyte_char_to_unibyte (c, Qnil));
5306 insert_1_both (work, 1, 1, 1, 0, 0);
5307 }
5308 }
5309 else if (! multibyte
5310 && ! NILP (current_buffer->enable_multibyte_characters))
5311 {
1e313f28 5312 int i, c, char_bytes;
5f5c8ee5 5313 unsigned char *msg = (unsigned char *) m;
260a86a0 5314 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5315 /* Convert a single-byte string to multibyte
5316 for the *Message* buffer. */
1e313f28 5317 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5318 {
5319 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5320 char_bytes = CHAR_STRING (c, str);
5321 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5322 }
5323 }
1e313f28
GM
5324 else if (nbytes)
5325 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5326
5327 if (nlflag)
5328 {
5329 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5330 insert_1 ("\n", 1, 1, 0, 0);
5331
5332 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5333 this_bol = PT;
5334 this_bol_byte = PT_BYTE;
5335
5336 if (this_bol > BEG)
5337 {
5338 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5339 prev_bol = PT;
5340 prev_bol_byte = PT_BYTE;
5341
5342 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5343 this_bol, this_bol_byte);
5344 if (dup)
5345 {
5346 del_range_both (prev_bol, prev_bol_byte,
5347 this_bol, this_bol_byte, 0);
5348 if (dup > 1)
5349 {
5350 char dupstr[40];
5351 int duplen;
5352
5353 /* If you change this format, don't forget to also
5354 change message_log_check_duplicate. */
5355 sprintf (dupstr, " [%d times]", dup);
5356 duplen = strlen (dupstr);
5357 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5358 insert_1 (dupstr, duplen, 1, 0, 1);
5359 }
5360 }
5361 }
5362
5363 if (NATNUMP (Vmessage_log_max))
5364 {
5365 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5366 -XFASTINT (Vmessage_log_max) - 1, 0);
5367 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5368 }
5369 }
5370 BEGV = XMARKER (oldbegv)->charpos;
5371 BEGV_BYTE = marker_byte_position (oldbegv);
5372
5373 if (zv_at_end)
5374 {
5375 ZV = Z;
5376 ZV_BYTE = Z_BYTE;
5377 }
5378 else
5379 {
5380 ZV = XMARKER (oldzv)->charpos;
5381 ZV_BYTE = marker_byte_position (oldzv);
5382 }
5383
5384 if (point_at_end)
5385 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5386 else
5387 /* We can't do Fgoto_char (oldpoint) because it will run some
5388 Lisp code. */
5389 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5390 XMARKER (oldpoint)->bytepos);
5391
5392 UNGCPRO;
5393 free_marker (oldpoint);
5394 free_marker (oldbegv);
5395 free_marker (oldzv);
5396
5397 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5398 set_buffer_internal (oldbuf);
5399 if (NILP (tem))
5400 windows_or_buffers_changed = old_windows_or_buffers_changed;
5401 message_log_need_newline = !nlflag;
5402 Vdeactivate_mark = old_deactivate_mark;
5403 }
5404}
5405
5406
5407/* We are at the end of the buffer after just having inserted a newline.
5408 (Note: We depend on the fact we won't be crossing the gap.)
5409 Check to see if the most recent message looks a lot like the previous one.
5410 Return 0 if different, 1 if the new one should just replace it, or a
5411 value N > 1 if we should also append " [N times]". */
5412
5413static int
5414message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5415 int prev_bol, this_bol;
5416 int prev_bol_byte, this_bol_byte;
5417{
5418 int i;
5419 int len = Z_BYTE - 1 - this_bol_byte;
5420 int seen_dots = 0;
5421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5423
5424 for (i = 0; i < len; i++)
5425 {
509633e3 5426 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5427 seen_dots = 1;
5428 if (p1[i] != p2[i])
5429 return seen_dots;
5430 }
5431 p1 += len;
5432 if (*p1 == '\n')
5433 return 2;
5434 if (*p1++ == ' ' && *p1++ == '[')
5435 {
5436 int n = 0;
5437 while (*p1 >= '0' && *p1 <= '9')
5438 n = n * 10 + *p1++ - '0';
5439 if (strncmp (p1, " times]\n", 8) == 0)
5440 return n+1;
5441 }
5442 return 0;
5443}
5444
5445
1e313f28
GM
5446/* Display an echo area message M with a specified length of NBYTES
5447 bytes. The string may include null characters. If M is 0, clear
5448 out any existing message, and let the mini-buffer text show
5449 through.
5f5c8ee5
GM
5450
5451 The buffer M must continue to exist until after the echo area gets
5452 cleared or some other message gets displayed there. This means do
5453 not pass text that is stored in a Lisp string; do not pass text in
5454 a buffer that was alloca'd. */
5455
5456void
1e313f28 5457message2 (m, nbytes, multibyte)
5f5c8ee5 5458 char *m;
1e313f28 5459 int nbytes;
5f5c8ee5
GM
5460 int multibyte;
5461{
5462 /* First flush out any partial line written with print. */
5463 message_log_maybe_newline ();
5464 if (m)
1e313f28
GM
5465 message_dolog (m, nbytes, 1, multibyte);
5466 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5467}
5468
5469
5470/* The non-logging counterpart of message2. */
5471
5472void
1e313f28 5473message2_nolog (m, nbytes, multibyte)
5f5c8ee5 5474 char *m;
1e313f28 5475 int nbytes;
5f5c8ee5 5476{
886bd6f2 5477 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5478 message_enable_multibyte = multibyte;
5479
5480 if (noninteractive)
5481 {
5482 if (noninteractive_need_newline)
5483 putc ('\n', stderr);
5484 noninteractive_need_newline = 0;
5485 if (m)
1e313f28 5486 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
5487 if (cursor_in_echo_area == 0)
5488 fprintf (stderr, "\n");
5489 fflush (stderr);
5490 }
5491 /* A null message buffer means that the frame hasn't really been
5492 initialized yet. Error messages get reported properly by
5493 cmd_error, so this must be just an informative message; toss it. */
5494 else if (INTERACTIVE
886bd6f2
GM
5495 && sf->glyphs_initialized_p
5496 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5497 {
5498 Lisp_Object mini_window;
5499 struct frame *f;
5500
5501 /* Get the frame containing the mini-buffer
5502 that the selected frame is using. */
886bd6f2 5503 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5504 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5505
5506 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5507 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5508 && ! FRAME_VISIBLE_P (f))
5509 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5510
5511 if (m)
5512 {
1e313f28 5513 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5514 if (minibuffer_auto_raise)
5515 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5516 }
5517 else
c6e89d6c 5518 clear_message (1, 1);
5f5c8ee5 5519
c6e89d6c 5520 do_pending_window_change (0);
5f5c8ee5 5521 echo_area_display (1);
c6e89d6c 5522 do_pending_window_change (0);
5f5c8ee5
GM
5523 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5524 (*frame_up_to_date_hook) (f);
5525 }
5526}
5527
5528
c6e89d6c
GM
5529/* Display an echo area message M with a specified length of NBYTES
5530 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5531 string, clear out any existing message, and let the mini-buffer
5532 text show through. */
5533
5534void
c6e89d6c 5535message3 (m, nbytes, multibyte)
5f5c8ee5 5536 Lisp_Object m;
c6e89d6c 5537 int nbytes;
5f5c8ee5
GM
5538 int multibyte;
5539{
5540 struct gcpro gcpro1;
5541
5542 GCPRO1 (m);
5543
5544 /* First flush out any partial line written with print. */
5545 message_log_maybe_newline ();
5546 if (STRINGP (m))
c6e89d6c
GM
5547 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5548 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5549
5550 UNGCPRO;
5551}
5552
5553
5554/* The non-logging version of message3. */
5555
5556void
c6e89d6c 5557message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5558 Lisp_Object m;
c6e89d6c 5559 int nbytes, multibyte;
5f5c8ee5 5560{
886bd6f2 5561 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5562 message_enable_multibyte = multibyte;
5563
5564 if (noninteractive)
5565 {
5566 if (noninteractive_need_newline)
5567 putc ('\n', stderr);
5568 noninteractive_need_newline = 0;
5569 if (STRINGP (m))
c6e89d6c 5570 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5571 if (cursor_in_echo_area == 0)
5572 fprintf (stderr, "\n");
5573 fflush (stderr);
5574 }
5575 /* A null message buffer means that the frame hasn't really been
5576 initialized yet. Error messages get reported properly by
5577 cmd_error, so this must be just an informative message; toss it. */
5578 else if (INTERACTIVE
886bd6f2
GM
5579 && sf->glyphs_initialized_p
5580 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5581 {
5582 Lisp_Object mini_window;
c6e89d6c 5583 Lisp_Object frame;
5f5c8ee5
GM
5584 struct frame *f;
5585
5586 /* Get the frame containing the mini-buffer
5587 that the selected frame is using. */
886bd6f2 5588 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5589 frame = XWINDOW (mini_window)->frame;
5590 f = XFRAME (frame);
5f5c8ee5
GM
5591
5592 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5593 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5594 && !FRAME_VISIBLE_P (f))
5595 Fmake_frame_visible (frame);
5f5c8ee5 5596
c6e89d6c 5597 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5598 {
c6e89d6c 5599 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5600 if (minibuffer_auto_raise)
5601 Fraise_frame (frame);
5f5c8ee5
GM
5602 }
5603 else
c6e89d6c 5604 clear_message (1, 1);
5f5c8ee5 5605
c6e89d6c 5606 do_pending_window_change (0);
5f5c8ee5 5607 echo_area_display (1);
c6e89d6c 5608 do_pending_window_change (0);
5f5c8ee5
GM
5609 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5610 (*frame_up_to_date_hook) (f);
5611 }
5612}
5613
5614
5615/* Display a null-terminated echo area message M. If M is 0, clear
5616 out any existing message, and let the mini-buffer text show through.
5617
5618 The buffer M must continue to exist until after the echo area gets
5619 cleared or some other message gets displayed there. Do not pass
5620 text that is stored in a Lisp string. Do not pass text in a buffer
5621 that was alloca'd. */
5622
5623void
5624message1 (m)
5625 char *m;
5626{
5627 message2 (m, (m ? strlen (m) : 0), 0);
5628}
5629
5630
5631/* The non-logging counterpart of message1. */
5632
5633void
5634message1_nolog (m)
5635 char *m;
5636{
5637 message2_nolog (m, (m ? strlen (m) : 0), 0);
5638}
5639
5640/* Display a message M which contains a single %s
5641 which gets replaced with STRING. */
5642
5643void
5644message_with_string (m, string, log)
5645 char *m;
5646 Lisp_Object string;
5647 int log;
5648{
5649 if (noninteractive)
5650 {
5651 if (m)
5652 {
5653 if (noninteractive_need_newline)
5654 putc ('\n', stderr);
5655 noninteractive_need_newline = 0;
5656 fprintf (stderr, m, XSTRING (string)->data);
5657 if (cursor_in_echo_area == 0)
5658 fprintf (stderr, "\n");
5659 fflush (stderr);
5660 }
5661 }
5662 else if (INTERACTIVE)
5663 {
5664 /* The frame whose minibuffer we're going to display the message on.
5665 It may be larger than the selected frame, so we need
5666 to use its buffer, not the selected frame's buffer. */
5667 Lisp_Object mini_window;
886bd6f2 5668 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5669
5670 /* Get the frame containing the minibuffer
5671 that the selected frame is using. */
886bd6f2 5672 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5673 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5674
5675 /* A null message buffer means that the frame hasn't really been
5676 initialized yet. Error messages get reported properly by
5677 cmd_error, so this must be just an informative message; toss it. */
5678 if (FRAME_MESSAGE_BUF (f))
5679 {
5680 int len;
5681 char *a[1];
5682 a[0] = (char *) XSTRING (string)->data;
5683
5684 len = doprnt (FRAME_MESSAGE_BUF (f),
5685 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5686
5687 if (log)
5688 message2 (FRAME_MESSAGE_BUF (f), len,
5689 STRING_MULTIBYTE (string));
5690 else
5691 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5692 STRING_MULTIBYTE (string));
5693
5694 /* Print should start at the beginning of the message
5695 buffer next time. */
5696 message_buf_print = 0;
5697 }
5698 }
5699}
5700
5701
5f5c8ee5
GM
5702/* Dump an informative message to the minibuf. If M is 0, clear out
5703 any existing message, and let the mini-buffer text show through. */
5704
5705/* VARARGS 1 */
5706void
5707message (m, a1, a2, a3)
5708 char *m;
5709 EMACS_INT a1, a2, a3;
5710{
5711 if (noninteractive)
5712 {
5713 if (m)
5714 {
5715 if (noninteractive_need_newline)
5716 putc ('\n', stderr);
5717 noninteractive_need_newline = 0;
5718 fprintf (stderr, m, a1, a2, a3);
5719 if (cursor_in_echo_area == 0)
5720 fprintf (stderr, "\n");
5721 fflush (stderr);
5722 }
5723 }
5724 else if (INTERACTIVE)
5725 {
5726 /* The frame whose mini-buffer we're going to display the message
5727 on. It may be larger than the selected frame, so we need to
5728 use its buffer, not the selected frame's buffer. */
5729 Lisp_Object mini_window;
886bd6f2 5730 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5731
5732 /* Get the frame containing the mini-buffer
5733 that the selected frame is using. */
886bd6f2 5734 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5735 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5736
5737 /* A null message buffer means that the frame hasn't really been
5738 initialized yet. Error messages get reported properly by
5739 cmd_error, so this must be just an informative message; toss
5740 it. */
5741 if (FRAME_MESSAGE_BUF (f))
5742 {
5743 if (m)
5744 {
5745 int len;
5746#ifdef NO_ARG_ARRAY
5747 char *a[3];
5748 a[0] = (char *) a1;
5749 a[1] = (char *) a2;
5750 a[2] = (char *) a3;
5751
5752 len = doprnt (FRAME_MESSAGE_BUF (f),
5753 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5754#else
5755 len = doprnt (FRAME_MESSAGE_BUF (f),
5756 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5757 (char **) &a1);
5758#endif /* NO_ARG_ARRAY */
5759
5760 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5761 }
5762 else
5763 message1 (0);
5764
5765 /* Print should start at the beginning of the message
5766 buffer next time. */
5767 message_buf_print = 0;
5768 }
5769 }
5770}
5771
5772
5773/* The non-logging version of message. */
5774
5775void
5776message_nolog (m, a1, a2, a3)
5777 char *m;
5778 EMACS_INT a1, a2, a3;
5779{
5780 Lisp_Object old_log_max;
5781 old_log_max = Vmessage_log_max;
5782 Vmessage_log_max = Qnil;
5783 message (m, a1, a2, a3);
5784 Vmessage_log_max = old_log_max;
5785}
5786
5787
c6e89d6c
GM
5788/* Display the current message in the current mini-buffer. This is
5789 only called from error handlers in process.c, and is not time
5790 critical. */
5f5c8ee5
GM
5791
5792void
5793update_echo_area ()
5794{
c6e89d6c
GM
5795 if (!NILP (echo_area_buffer[0]))
5796 {
5797 Lisp_Object string;
5798 string = Fcurrent_message ();
5799 message3 (string, XSTRING (string)->size,
5800 !NILP (current_buffer->enable_multibyte_characters));
5801 }
5802}
5803
5804
5bcfeb49
GM
5805/* Make sure echo area buffers in echo_buffers[] are life. If they
5806 aren't, make new ones. */
5807
5808static void
5809ensure_echo_area_buffers ()
5810{
5811 int i;
5812
5813 for (i = 0; i < 2; ++i)
5814 if (!BUFFERP (echo_buffer[i])
5815 || NILP (XBUFFER (echo_buffer[i])->name))
5816 {
5817 char name[30];
ff3d9573
GM
5818 Lisp_Object old_buffer;
5819 int j;
5820
5821 old_buffer = echo_buffer[i];
5bcfeb49
GM
5822 sprintf (name, " *Echo Area %d*", i);
5823 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 5824 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
5825
5826 for (j = 0; j < 2; ++j)
5827 if (EQ (old_buffer, echo_area_buffer[j]))
5828 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
5829 }
5830}
5831
5832
23a96c77 5833/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
5834 echo_area_buffer as current buffer.
5835
5836 WHICH zero means use the current message buffer
5837 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5838 from echo_buffer[] and clear it.
5839
5840 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5841 suitable buffer from echo_buffer[] and clear it.
5842
5843 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5844 that the current message becomes the last displayed one, make
5845 choose a suitable buffer for echo_area_buffer[0], and clear it.
5846
5847 Value is what FN returns. */
5848
5849static int
23a96c77 5850with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
5851 struct window *w;
5852 int which;
23dd2d97
KR
5853 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5854 EMACS_INT a1;
5855 Lisp_Object a2;
5856 EMACS_INT a3, a4;
c6e89d6c
GM
5857{
5858 Lisp_Object buffer;
15e26c76 5859 int this_one, the_other, clear_buffer_p, rc;
2d27dae2 5860 int count = BINDING_STACK_SIZE ();
c6e89d6c
GM
5861
5862 /* If buffers aren't life, make new ones. */
5bcfeb49 5863 ensure_echo_area_buffers ();
c6e89d6c
GM
5864
5865 clear_buffer_p = 0;
5866
5867 if (which == 0)
5868 this_one = 0, the_other = 1;
5869 else if (which > 0)
5870 this_one = 1, the_other = 0;
5f5c8ee5 5871 else
c6e89d6c
GM
5872 {
5873 this_one = 0, the_other = 1;
5874 clear_buffer_p = 1;
5875
5876 /* We need a fresh one in case the current echo buffer equals
5877 the one containing the last displayed echo area message. */
5878 if (!NILP (echo_area_buffer[this_one])
5879 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5880 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
5881 }
5882
5883 /* Choose a suitable buffer from echo_buffer[] is we don't
5884 have one. */
5885 if (NILP (echo_area_buffer[this_one]))
5886 {
5887 echo_area_buffer[this_one]
5888 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5889 ? echo_buffer[the_other]
5890 : echo_buffer[this_one]);
5891 clear_buffer_p = 1;
5892 }
5893
5894 buffer = echo_area_buffer[this_one];
5895
5896 record_unwind_protect (unwind_with_echo_area_buffer,
5897 with_echo_area_buffer_unwind_data (w));
5898
5899 /* Make the echo area buffer current. Note that for display
5900 purposes, it is not necessary that the displayed window's buffer
5901 == current_buffer, except for text property lookup. So, let's
5902 only set that buffer temporarily here without doing a full
5903 Fset_window_buffer. We must also change w->pointm, though,
5904 because otherwise an assertions in unshow_buffer fails, and Emacs
5905 aborts. */
9142dd5b 5906 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5907 if (w)
5908 {
5909 w->buffer = buffer;
5910 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5911 }
ad4f174e 5912
c6e89d6c
GM
5913 current_buffer->undo_list = Qt;
5914 current_buffer->read_only = Qnil;
bbbf6d06 5915 specbind (Qinhibit_read_only, Qt);
c6e89d6c
GM
5916
5917 if (clear_buffer_p && Z > BEG)
5918 del_range (BEG, Z);
5919
5920 xassert (BEGV >= BEG);
5921 xassert (ZV <= Z && ZV >= BEGV);
5922
23a96c77 5923 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
5924
5925 xassert (BEGV >= BEG);
5926 xassert (ZV <= Z && ZV >= BEGV);
5927
5928 unbind_to (count, Qnil);
5929 return rc;
5f5c8ee5
GM
5930}
5931
5932
c6e89d6c
GM
5933/* Save state that should be preserved around the call to the function
5934 FN called in with_echo_area_buffer. */
5f5c8ee5 5935
c6e89d6c
GM
5936static Lisp_Object
5937with_echo_area_buffer_unwind_data (w)
5938 struct window *w;
5f5c8ee5 5939{
c6e89d6c
GM
5940 int i = 0;
5941 Lisp_Object vector;
5f5c8ee5 5942
c6e89d6c
GM
5943 /* Reduce consing by keeping one vector in
5944 Vwith_echo_area_save_vector. */
5945 vector = Vwith_echo_area_save_vector;
5946 Vwith_echo_area_save_vector = Qnil;
5947
5948 if (NILP (vector))
9142dd5b 5949 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5950
5951 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5952 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5953 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5954
5955 if (w)
5956 {
5957 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5958 XVECTOR (vector)->contents[i++] = w->buffer;
5959 XVECTOR (vector)->contents[i++]
5960 = make_number (XMARKER (w->pointm)->charpos);
5961 XVECTOR (vector)->contents[i++]
5962 = make_number (XMARKER (w->pointm)->bytepos);
5963 }
5964 else
5965 {
5966 int end = i + 4;
5967 while (i < end)
5968 XVECTOR (vector)->contents[i++] = Qnil;
5969 }
5f5c8ee5 5970
c6e89d6c
GM
5971 xassert (i == XVECTOR (vector)->size);
5972 return vector;
5973}
5f5c8ee5 5974
5f5c8ee5 5975
c6e89d6c
GM
5976/* Restore global state from VECTOR which was created by
5977 with_echo_area_buffer_unwind_data. */
5978
5979static Lisp_Object
5980unwind_with_echo_area_buffer (vector)
5981 Lisp_Object vector;
5982{
bbbf6d06
GM
5983 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
5984 Vdeactivate_mark = AREF (vector, 1);
5985 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 5986
bbbf6d06 5987 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
5988 {
5989 struct window *w;
5990 Lisp_Object buffer, charpos, bytepos;
5991
bbbf6d06
GM
5992 w = XWINDOW (AREF (vector, 3));
5993 buffer = AREF (vector, 4);
5994 charpos = AREF (vector, 5);
5995 bytepos = AREF (vector, 6);
c6e89d6c
GM
5996
5997 w->buffer = buffer;
5998 set_marker_both (w->pointm, buffer,
5999 XFASTINT (charpos), XFASTINT (bytepos));
6000 }
6001
6002 Vwith_echo_area_save_vector = vector;
6003 return Qnil;
6004}
6005
6006
6007/* Set up the echo area for use by print functions. MULTIBYTE_P
6008 non-zero means we will print multibyte. */
6009
6010void
6011setup_echo_area_for_printing (multibyte_p)
6012 int multibyte_p;
6013{
5bcfeb49
GM
6014 ensure_echo_area_buffers ();
6015
c6e89d6c
GM
6016 if (!message_buf_print)
6017 {
6018 /* A message has been output since the last time we printed.
6019 Choose a fresh echo area buffer. */
6020 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6021 echo_area_buffer[0] = echo_buffer[1];
6022 else
6023 echo_area_buffer[0] = echo_buffer[0];
6024
6025 /* Switch to that buffer and clear it. */
6026 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
bbbf6d06 6027
c6e89d6c 6028 if (Z > BEG)
bbbf6d06
GM
6029 {
6030 int count = BINDING_STACK_SIZE ();
6031 specbind (Qinhibit_read_only, Qt);
6032 del_range (BEG, Z);
6033 unbind_to (count, Qnil);
6034 }
c6e89d6c
GM
6035 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6036
6037 /* Set up the buffer for the multibyteness we need. */
6038 if (multibyte_p
6039 != !NILP (current_buffer->enable_multibyte_characters))
6040 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6041
6042 /* Raise the frame containing the echo area. */
6043 if (minibuffer_auto_raise)
6044 {
886bd6f2 6045 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6046 Lisp_Object mini_window;
886bd6f2 6047 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6048 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6049 }
6050
8a4e3c0c 6051 message_log_maybe_newline ();
c6e89d6c
GM
6052 message_buf_print = 1;
6053 }
fa77249f
GM
6054 else
6055 {
6056 if (NILP (echo_area_buffer[0]))
6057 {
6058 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6059 echo_area_buffer[0] = echo_buffer[1];
6060 else
6061 echo_area_buffer[0] = echo_buffer[0];
6062 }
6063
6064 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6065 /* Someone switched buffers between print requests. */
6066 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6067 }
c6e89d6c
GM
6068}
6069
6070
dd2eb166
GM
6071/* Display an echo area message in window W. Value is non-zero if W's
6072 height is changed. If display_last_displayed_message_p is
6073 non-zero, display the message that was last displayed, otherwise
6074 display the current message. */
c6e89d6c
GM
6075
6076static int
6077display_echo_area (w)
6078 struct window *w;
6079{
25edb08f
GM
6080 int i, no_message_p, window_height_changed_p, count;
6081
6082 /* Temporarily disable garbage collections while displaying the echo
6083 area. This is done because a GC can print a message itself.
6084 That message would modify the echo area buffer's contents while a
6085 redisplay of the buffer is going on, and seriously confuse
6086 redisplay. */
6087 count = inhibit_garbage_collection ();
dd2eb166
GM
6088
6089 /* If there is no message, we must call display_echo_area_1
6090 nevertheless because it resizes the window. But we will have to
6091 reset the echo_area_buffer in question to nil at the end because
6092 with_echo_area_buffer will sets it to an empty buffer. */
6093 i = display_last_displayed_message_p ? 1 : 0;
6094 no_message_p = NILP (echo_area_buffer[i]);
6095
6096 window_height_changed_p
6097 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6098 display_echo_area_1,
23dd2d97 6099 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6100
6101 if (no_message_p)
6102 echo_area_buffer[i] = Qnil;
25edb08f
GM
6103
6104 unbind_to (count, Qnil);
dd2eb166 6105 return window_height_changed_p;
c6e89d6c
GM
6106}
6107
6108
6109/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6110 contains the current echo area message in window W, a mini-window,
6111 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6112 Change the height of W so that all of the message is displayed.
6113 Value is non-zero if height of W was changed. */
6114
6115static int
23a96c77 6116display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6117 EMACS_INT a1;
6118 Lisp_Object a2;
6119 EMACS_INT a3, a4;
c6e89d6c 6120{
23a96c77 6121 struct window *w = (struct window *) a1;
c6e89d6c 6122 Lisp_Object window;
c6e89d6c
GM
6123 struct text_pos start;
6124 int window_height_changed_p = 0;
6125
6126 /* Do this before displaying, so that we have a large enough glyph
6127 matrix for the display. */
92a90e89 6128 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6129
6130 /* Display. */
6131 clear_glyph_matrix (w->desired_matrix);
6132 XSETWINDOW (window, w);
6133 SET_TEXT_POS (start, BEG, BEG_BYTE);
6134 try_window (window, start);
6135
c6e89d6c
GM
6136 return window_height_changed_p;
6137}
6138
6139
92a90e89
GM
6140/* Resize the echo area window to exactly the size needed for the
6141 currently displayed message, if there is one. */
6142
6143void
6144resize_echo_area_axactly ()
6145{
6146 if (BUFFERP (echo_area_buffer[0])
6147 && WINDOWP (echo_area_window))
6148 {
6149 struct window *w = XWINDOW (echo_area_window);
6150 int resized_p;
6151
23a96c77 6152 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
23dd2d97 6153 (EMACS_INT) w, Qnil, 0, 0);
92a90e89
GM
6154 if (resized_p)
6155 {
6156 ++windows_or_buffers_changed;
6157 ++update_mode_lines;
6158 redisplay_internal (0);
6159 }
6160 }
6161}
6162
6163
23a96c77
GM
6164/* Callback function for with_echo_area_buffer, when used from
6165 resize_echo_area_axactly. A1 contains a pointer to the window to
6166 resize, A2 to A4 are not used. Value is what resize_mini_window
6167 returns. */
6168
6169static int
6170resize_mini_window_1 (a1, a2, a3, a4)
23dd2d97
KR
6171 EMACS_INT a1;
6172 Lisp_Object a2;
6173 EMACS_INT a3, a4;
23a96c77
GM
6174{
6175 return resize_mini_window ((struct window *) a1, 1);
6176}
6177
6178
92a90e89
GM
6179/* Resize mini-window W to fit the size of its contents. EXACT:P
6180 means size the window exactly to the size needed. Otherwise, it's
6181 only enlarged until W's buffer is empty. Value is non-zero if
6182 the window height has been changed. */
c6e89d6c 6183
9472f927 6184int
92a90e89 6185resize_mini_window (w, exact_p)
c6e89d6c 6186 struct window *w;
92a90e89 6187 int exact_p;
c6e89d6c
GM
6188{
6189 struct frame *f = XFRAME (w->frame);
6190 int window_height_changed_p = 0;
6191
6192 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
6193
6194 /* Nil means don't try to resize. */
6422c1d7 6195 if (NILP (Vresize_mini_windows)
00f6d59e 6196 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6197 return 0;
c6e89d6c
GM
6198
6199 if (!FRAME_MINIBUF_ONLY_P (f))
6200 {
6201 struct it it;
dd2eb166
GM
6202 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6203 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6204 int height, max_height;
6205 int unit = CANON_Y_UNIT (f);
6206 struct text_pos start;
1bfdbe43
GM
6207 struct buffer *old_current_buffer = NULL;
6208
6209 if (current_buffer != XBUFFER (w->buffer))
6210 {
6211 old_current_buffer = current_buffer;
6212 set_buffer_internal (XBUFFER (w->buffer));
6213 }
9142dd5b 6214
c6e89d6c 6215 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6216
dd2eb166
GM
6217 /* Compute the max. number of lines specified by the user. */
6218 if (FLOATP (Vmax_mini_window_height))
6219 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6220 else if (INTEGERP (Vmax_mini_window_height))
6221 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6222 else
6223 max_height = total_height / 4;
dd2eb166
GM
6224
6225 /* Correct that max. height if it's bogus. */
6226 max_height = max (1, max_height);
6227 max_height = min (total_height, max_height);
6228
6229 /* Find out the height of the text in the window. */
ad4f174e
GM
6230 if (it.truncate_lines_p)
6231 height = 1;
55b064bd 6232 else
ad4f174e
GM
6233 {
6234 last_height = 0;
6235 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6236 if (it.max_ascent == 0 && it.max_descent == 0)
6237 height = it.current_y + last_height;
6238 else
6239 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6240 height -= it.extra_line_spacing;
ad4f174e
GM
6241 height = (height + unit - 1) / unit;
6242 }
dd2eb166
GM
6243
6244 /* Compute a suitable window start. */
6245 if (height > max_height)
6246 {
6247 height = max_height;
6248 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6249 move_it_vertically_backward (&it, (height - 1) * unit);
6250 start = it.current.pos;
6251 }
6252 else
6253 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6254 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6255
6422c1d7 6256 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6257 {
6422c1d7
GM
6258 /* Let it grow only, until we display an empty message, in which
6259 case the window shrinks again. */
6260 if (height > XFASTINT (w->height))
6261 {
6262 int old_height = XFASTINT (w->height);
6263 freeze_window_starts (f, 1);
6264 grow_mini_window (w, height - XFASTINT (w->height));
6265 window_height_changed_p = XFASTINT (w->height) != old_height;
6266 }
6267 else if (height < XFASTINT (w->height)
6268 && (exact_p || BEGV == ZV))
6269 {
6270 int old_height = XFASTINT (w->height);
6271 freeze_window_starts (f, 0);
6272 shrink_mini_window (w);
6273 window_height_changed_p = XFASTINT (w->height) != old_height;
6274 }
da448723 6275 }
6422c1d7 6276 else
da448723 6277 {
6422c1d7
GM
6278 /* Always resize to exact size needed. */
6279 if (height > XFASTINT (w->height))
6280 {
6281 int old_height = XFASTINT (w->height);
6282 freeze_window_starts (f, 1);
6283 grow_mini_window (w, height - XFASTINT (w->height));
6284 window_height_changed_p = XFASTINT (w->height) != old_height;
6285 }
6286 else if (height < XFASTINT (w->height))
6287 {
6288 int old_height = XFASTINT (w->height);
6289 freeze_window_starts (f, 0);
6290 shrink_mini_window (w);
6291
6292 if (height)
6293 {
6294 freeze_window_starts (f, 1);
6295 grow_mini_window (w, height - XFASTINT (w->height));
6296 }
6297
6298 window_height_changed_p = XFASTINT (w->height) != old_height;
6299 }
9142dd5b 6300 }
1bfdbe43
GM
6301
6302 if (old_current_buffer)
6303 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6304 }
6305
6306 return window_height_changed_p;
6307}
6308
6309
6310/* Value is the current message, a string, or nil if there is no
6311 current message. */
6312
6313Lisp_Object
6314current_message ()
6315{
6316 Lisp_Object msg;
6317
6318 if (NILP (echo_area_buffer[0]))
6319 msg = Qnil;
6320 else
6321 {
23a96c77 6322 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6323 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6324 if (NILP (msg))
6325 echo_area_buffer[0] = Qnil;
6326 }
6327
6328 return msg;
6329}
6330
6331
6332static int
23a96c77 6333current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6334 EMACS_INT a1;
6335 Lisp_Object a2;
6336 EMACS_INT a3, a4;
c6e89d6c 6337{
23a96c77
GM
6338 Lisp_Object *msg = (Lisp_Object *) a1;
6339
c6e89d6c
GM
6340 if (Z > BEG)
6341 *msg = make_buffer_string (BEG, Z, 1);
6342 else
6343 *msg = Qnil;
6344 return 0;
6345}
6346
6347
6348/* Push the current message on Vmessage_stack for later restauration
6349 by restore_message. Value is non-zero if the current message isn't
6350 empty. This is a relatively infrequent operation, so it's not
6351 worth optimizing. */
6352
6353int
6354push_message ()
6355{
6356 Lisp_Object msg;
6357 msg = current_message ();
6358 Vmessage_stack = Fcons (msg, Vmessage_stack);
6359 return STRINGP (msg);
6360}
6361
6362
098ec84a
GM
6363/* Handler for record_unwind_protect calling pop_message. */
6364
6365Lisp_Object
6366push_message_unwind (dummy)
6367 Lisp_Object dummy;
6368{
6369 pop_message ();
6370 return Qnil;
6371}
6372
6373
c6e89d6c
GM
6374/* Restore message display from the top of Vmessage_stack. */
6375
6376void
6377restore_message ()
6378{
6379 Lisp_Object msg;
6380
6381 xassert (CONSP (Vmessage_stack));
6382 msg = XCAR (Vmessage_stack);
6383 if (STRINGP (msg))
6384 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6385 else
6386 message3_nolog (msg, 0, 0);
6387}
6388
6389
6390/* Pop the top-most entry off Vmessage_stack. */
6391
6392void
6393pop_message ()
6394{
6395 xassert (CONSP (Vmessage_stack));
6396 Vmessage_stack = XCDR (Vmessage_stack);
6397}
6398
6399
6400/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6401 exits. If the stack is not empty, we have a missing pop_message
6402 somewhere. */
6403
6404void
6405check_message_stack ()
6406{
6407 if (!NILP (Vmessage_stack))
6408 abort ();
6409}
6410
6411
6412/* Truncate to NCHARS what will be displayed in the echo area the next
6413 time we display it---but don't redisplay it now. */
6414
6415void
6416truncate_echo_area (nchars)
6417 int nchars;
6418{
6419 if (nchars == 0)
6420 echo_area_buffer[0] = Qnil;
6421 /* A null message buffer means that the frame hasn't really been
6422 initialized yet. Error messages get reported properly by
6423 cmd_error, so this must be just an informative message; toss it. */
6424 else if (!noninteractive
6425 && INTERACTIVE
c6e89d6c 6426 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6427 {
6428 struct frame *sf = SELECTED_FRAME ();
6429 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6430 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6431 }
c6e89d6c
GM
6432}
6433
6434
6435/* Helper function for truncate_echo_area. Truncate the current
6436 message to at most NCHARS characters. */
6437
6438static int
23a96c77 6439truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6440 EMACS_INT nchars;
6441 Lisp_Object a2;
6442 EMACS_INT a3, a4;
c6e89d6c
GM
6443{
6444 if (BEG + nchars < Z)
6445 del_range (BEG + nchars, Z);
6446 if (Z == BEG)
6447 echo_area_buffer[0] = Qnil;
6448 return 0;
6449}
6450
6451
6452/* Set the current message to a substring of S or STRING.
6453
6454 If STRING is a Lisp string, set the message to the first NBYTES
6455 bytes from STRING. NBYTES zero means use the whole string. If
6456 STRING is multibyte, the message will be displayed multibyte.
6457
6458 If S is not null, set the message to the first LEN bytes of S. LEN
6459 zero means use the whole string. MULTIBYTE_P non-zero means S is
6460 multibyte. Display the message multibyte in that case. */
6461
6462void
6463set_message (s, string, nbytes, multibyte_p)
6464 char *s;
6465 Lisp_Object string;
6466 int nbytes;
6467{
6468 message_enable_multibyte
6469 = ((s && multibyte_p)
6470 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6471
23a96c77
GM
6472 with_echo_area_buffer (0, -1, set_message_1,
6473 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6474 message_buf_print = 0;
21fdfb65 6475 help_echo_showing_p = 0;
c6e89d6c
GM
6476}
6477
6478
6479/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6480 as there, with A1 corresponding to S and A2 corresponding to STRING
6481 This function is called with the echo area buffer being
c6e89d6c
GM
6482 current. */
6483
6484static int
23a96c77 6485set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6486 EMACS_INT a1;
6487 Lisp_Object a2;
6488 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6489{
23a96c77 6490 char *s = (char *) a1;
23dd2d97 6491 Lisp_Object string = a2;
23a96c77 6492
c6e89d6c
GM
6493 xassert (BEG == Z);
6494
6495 /* Change multibyteness of the echo buffer appropriately. */
6496 if (message_enable_multibyte
6497 != !NILP (current_buffer->enable_multibyte_characters))
6498 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6499
ad4f174e
GM
6500 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6501
c6e89d6c
GM
6502 /* Insert new message at BEG. */
6503 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6504
6505 if (STRINGP (string))
6506 {
6507 int nchars;
6508
6509 if (nbytes == 0)
6510 nbytes = XSTRING (string)->size_byte;
6511 nchars = string_byte_to_char (string, nbytes);
6512
6513 /* This function takes care of single/multibyte conversion. We
6514 just have to ensure that the echo area buffer has the right
6515 setting of enable_multibyte_characters. */
6516 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6517 }
6518 else if (s)
6519 {
6520 if (nbytes == 0)
6521 nbytes = strlen (s);
6522
6523 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6524 {
6525 /* Convert from multi-byte to single-byte. */
6526 int i, c, n;
6527 unsigned char work[1];
6528
6529 /* Convert a multibyte string to single-byte. */
6530 for (i = 0; i < nbytes; i += n)
6531 {
6532 c = string_char_and_length (s + i, nbytes - i, &n);
6533 work[0] = (SINGLE_BYTE_CHAR_P (c)
6534 ? c
6535 : multibyte_char_to_unibyte (c, Qnil));
6536 insert_1_both (work, 1, 1, 1, 0, 0);
6537 }
6538 }
6539 else if (!multibyte_p
6540 && !NILP (current_buffer->enable_multibyte_characters))
6541 {
6542 /* Convert from single-byte to multi-byte. */
6543 int i, c, n;
6544 unsigned char *msg = (unsigned char *) s;
260a86a0 6545 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6546
6547 /* Convert a single-byte string to multibyte. */
6548 for (i = 0; i < nbytes; i++)
6549 {
6550 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6551 n = CHAR_STRING (c, str);
6552 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6553 }
6554 }
6555 else
6556 insert_1 (s, nbytes, 1, 0, 0);
6557 }
6558
6559 return 0;
6560}
6561
6562
6563/* Clear messages. CURRENT_P non-zero means clear the current
6564 message. LAST_DISPLAYED_P non-zero means clear the message
6565 last displayed. */
6566
6567void
6568clear_message (current_p, last_displayed_p)
6569 int current_p, last_displayed_p;
6570{
6571 if (current_p)
6572 echo_area_buffer[0] = Qnil;
6573
6574 if (last_displayed_p)
6575 echo_area_buffer[1] = Qnil;
6576
6577 message_buf_print = 0;
6578}
6579
6580/* Clear garbaged frames.
6581
6582 This function is used where the old redisplay called
6583 redraw_garbaged_frames which in turn called redraw_frame which in
6584 turn called clear_frame. The call to clear_frame was a source of
6585 flickering. I believe a clear_frame is not necessary. It should
6586 suffice in the new redisplay to invalidate all current matrices,
6587 and ensure a complete redisplay of all windows. */
6588
6589static void
6590clear_garbaged_frames ()
6591{
5f5c8ee5
GM
6592 if (frame_garbaged)
6593 {
5f5c8ee5
GM
6594 Lisp_Object tail, frame;
6595
6596 FOR_EACH_FRAME (tail, frame)
6597 {
6598 struct frame *f = XFRAME (frame);
6599
6600 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6601 {
6602 clear_current_matrices (f);
6603 f->garbaged = 0;
6604 }
6605 }
6606
6607 frame_garbaged = 0;
6608 ++windows_or_buffers_changed;
6609 }
c6e89d6c 6610}
5f5c8ee5 6611
5f5c8ee5 6612
886bd6f2
GM
6613/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6614 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 6615 mini-windows height has been changed. */
5f5c8ee5 6616
c6e89d6c
GM
6617static int
6618echo_area_display (update_frame_p)
6619 int update_frame_p;
6620{
6621 Lisp_Object mini_window;
6622 struct window *w;
6623 struct frame *f;
6624 int window_height_changed_p = 0;
886bd6f2 6625 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6626
886bd6f2 6627 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6628 w = XWINDOW (mini_window);
6629 f = XFRAME (WINDOW_FRAME (w));
6630
6631 /* Don't display if frame is invisible or not yet initialized. */
6632 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6633 return 0;
5f5c8ee5 6634
1a578e9b
AC
6635/* The terminal frame is used as the first Emacs frame on the Mac OS. */
6636#ifndef macintosh
1ab3e082 6637#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
6638 /* When Emacs starts, selected_frame may be a visible terminal
6639 frame, even if we run under a window system. If we let this
6640 through, a message would be displayed on the terminal. */
622e3754
GM
6641 if (EQ (selected_frame, Vterminal_frame)
6642 && !NILP (Vwindow_system))
c6e89d6c 6643 return 0;
1ab3e082 6644#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 6645#endif
c6e89d6c
GM
6646
6647 /* Redraw garbaged frames. */
6648 if (frame_garbaged)
6649 clear_garbaged_frames ();
6650
6651 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6652 {
6653 echo_area_window = mini_window;
6654 window_height_changed_p = display_echo_area (w);
5f5c8ee5 6655 w->must_be_updated_p = 1;
c59c668a 6656
d4358b37
GM
6657 /* Update the display, unless called from redisplay_internal.
6658 Also don't update the screen during redisplay itself. The
6659 update will happen at the end of redisplay, and an update
6660 here could cause confusion. */
6661 if (update_frame_p && !redisplaying_p)
5f5c8ee5 6662 {
715e84c9 6663 int n = 0;
edc68111 6664
715e84c9
GM
6665 /* If the display update has been interrupted by pending
6666 input, update mode lines in the frame. Due to the
6667 pending input, it might have been that redisplay hasn't
6668 been called, so that mode lines above the echo area are
6669 garbaged. This looks odd, so we prevent it here. */
6670 if (!display_completed)
6671 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6672
8af1308e
GM
6673 if (window_height_changed_p
6674 /* Don't do this if Emacs is shutting down. Redisplay
6675 needs to run hooks. */
6676 && !NILP (Vrun_hooks))
c59c668a 6677 {
c06017fb
GM
6678 /* Must update other windows. Likewise as in other
6679 cases, don't let this update be interrupted by
6680 pending input. */
6681 int count = BINDING_STACK_SIZE ();
6682 specbind (Qredisplay_dont_pause, Qt);
edc68111 6683 windows_or_buffers_changed = 1;
c59c668a 6684 redisplay_internal (0);
c06017fb 6685 unbind_to (count, Qnil);
c59c668a 6686 }
715e84c9 6687 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 6688 {
edc68111 6689 /* Window configuration is the same as before.
715e84c9
GM
6690 Can do with a display update of the echo area,
6691 unless we displayed some mode lines. */
5f5c8ee5
GM
6692 update_single_window (w, 1);
6693 rif->flush_display (f);
6694 }
6695 else
6696 update_frame (f, 1, 1);
31b6671b
GM
6697
6698 /* If cursor is in the echo area, make sure that the next
6699 redisplay displays the minibuffer, so that the cursor will
6700 be replaced with what the minibuffer wants. */
6701 if (cursor_in_echo_area)
6702 ++windows_or_buffers_changed;
5f5c8ee5
GM
6703 }
6704 }
6705 else if (!EQ (mini_window, selected_window))
6706 windows_or_buffers_changed++;
c59c668a
GM
6707
6708 /* Last displayed message is now the current message. */
dd2eb166
GM
6709 echo_area_buffer[1] = echo_area_buffer[0];
6710
5f5c8ee5
GM
6711 /* Prevent redisplay optimization in redisplay_internal by resetting
6712 this_line_start_pos. This is done because the mini-buffer now
6713 displays the message instead of its buffer text. */
6714 if (EQ (mini_window, selected_window))
6715 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
6716
6717 return window_height_changed_p;
5f5c8ee5
GM
6718}
6719
6720
6721\f
6722/***********************************************************************
6723 Frame Titles
6724 ***********************************************************************/
6725
6726
6727#ifdef HAVE_WINDOW_SYSTEM
6728
6729/* A buffer for constructing frame titles in it; allocated from the
6730 heap in init_xdisp and resized as needed in store_frame_title_char. */
6731
6732static char *frame_title_buf;
6733
6734/* The buffer's end, and a current output position in it. */
6735
6736static char *frame_title_buf_end;
6737static char *frame_title_ptr;
6738
6739
6740/* Store a single character C for the frame title in frame_title_buf.
6741 Re-allocate frame_title_buf if necessary. */
6742
6743static void
6744store_frame_title_char (c)
6745 char c;
6746{
6747 /* If output position has reached the end of the allocated buffer,
6748 double the buffer's size. */
6749 if (frame_title_ptr == frame_title_buf_end)
6750 {
6751 int len = frame_title_ptr - frame_title_buf;
6752 int new_size = 2 * len * sizeof *frame_title_buf;
6753 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6754 frame_title_buf_end = frame_title_buf + new_size;
6755 frame_title_ptr = frame_title_buf + len;
6756 }
6757
6758 *frame_title_ptr++ = c;
6759}
6760
6761
6762/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
6763 frame_title_ptr. STR is the string to store. Do not copy
6764 characters that yield more columns than PRECISION; PRECISION <= 0
6765 means copy the whole string. Pad with spaces until FIELD_WIDTH
6766 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6767 pad. Called from display_mode_element when it is used to build a
6768 frame title. */
5f5c8ee5
GM
6769
6770static int
6771store_frame_title (str, field_width, precision)
6772 unsigned char *str;
6773 int field_width, precision;
6774{
6775 int n = 0;
d26b89b8 6776 int dummy, nbytes, width;
5f5c8ee5
GM
6777
6778 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
6779 nbytes = strlen (str);
6780 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6781 while (nbytes--)
6782 store_frame_title_char (*str++);
5f5c8ee5
GM
6783
6784 /* Fill up with spaces until FIELD_WIDTH reached. */
6785 while (field_width > 0
6786 && n < field_width)
6787 {
6788 store_frame_title_char (' ');
6789 ++n;
6790 }
6791
6792 return n;
6793}
6794
6795
6796/* Set the title of FRAME, if it has changed. The title format is
6797 Vicon_title_format if FRAME is iconified, otherwise it is
6798 frame_title_format. */
6799
6800static void
6801x_consider_frame_title (frame)
6802 Lisp_Object frame;
6803{
6804 struct frame *f = XFRAME (frame);
6805
6806 if (FRAME_WINDOW_P (f)
6807 || FRAME_MINIBUF_ONLY_P (f)
6808 || f->explicit_name)
6809 {
6810 /* Do we have more than one visible frame on this X display? */
6811 Lisp_Object tail;
6812 Lisp_Object fmt;
6813 struct buffer *obuf;
6814 int len;
6815 struct it it;
6816
9472f927 6817 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6818 {
9472f927 6819 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
6820
6821 if (tf != f
6822 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6823 && !FRAME_MINIBUF_ONLY_P (tf)
6824 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6825 break;
6826 }
6827
6828 /* Set global variable indicating that multiple frames exist. */
6829 multiple_frames = CONSP (tail);
6830
6831 /* Switch to the buffer of selected window of the frame. Set up
6832 frame_title_ptr so that display_mode_element will output into it;
6833 then display the title. */
6834 obuf = current_buffer;
6835 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6836 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6837 frame_title_ptr = frame_title_buf;
6838 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6839 NULL, DEFAULT_FACE_ID);
d26b89b8
KH
6840 display_mode_element (&it, 0, -1, -1, fmt);
6841 len = frame_title_ptr - frame_title_buf;
5f5c8ee5
GM
6842 frame_title_ptr = NULL;
6843 set_buffer_internal (obuf);
6844
6845 /* Set the title only if it's changed. This avoids consing in
6846 the common case where it hasn't. (If it turns out that we've
6847 already wasted too much time by walking through the list with
6848 display_mode_element, then we might need to optimize at a
6849 higher level than this.) */
6850 if (! STRINGP (f->name)
6851 || STRING_BYTES (XSTRING (f->name)) != len
6852 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6853 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6854 }
6855}
6856
6857#else /* not HAVE_WINDOW_SYSTEM */
6858
6859#define frame_title_ptr ((char *)0)
6860#define store_frame_title(str, mincol, maxcol) 0
6861
6862#endif /* not HAVE_WINDOW_SYSTEM */
6863
6864
6865
6866\f
6867/***********************************************************************
6868 Menu Bars
6869 ***********************************************************************/
6870
6871
6872/* Prepare for redisplay by updating menu-bar item lists when
6873 appropriate. This can call eval. */
6874
6875void
6876prepare_menu_bars ()
6877{
6878 int all_windows;
6879 struct gcpro gcpro1, gcpro2;
6880 struct frame *f;
6b5c4794 6881 Lisp_Object tooltip_frame;
5f5c8ee5
GM
6882
6883#ifdef HAVE_X_WINDOWS
6884 tooltip_frame = tip_frame;
6885#else
6b5c4794 6886 tooltip_frame = Qnil;
5f5c8ee5
GM
6887#endif
6888
6889 /* Update all frame titles based on their buffer names, etc. We do
6890 this before the menu bars so that the buffer-menu will show the
6891 up-to-date frame titles. */
6892#ifdef HAVE_WINDOW_SYSTEM
6893 if (windows_or_buffers_changed || update_mode_lines)
6894 {
6895 Lisp_Object tail, frame;
6896
6897 FOR_EACH_FRAME (tail, frame)
6898 {
6899 f = XFRAME (frame);
6b5c4794 6900 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
6901 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6902 x_consider_frame_title (frame);
6903 }
6904 }
6905#endif /* HAVE_WINDOW_SYSTEM */
6906
6907 /* Update the menu bar item lists, if appropriate. This has to be
6908 done before any actual redisplay or generation of display lines. */
6909 all_windows = (update_mode_lines
6910 || buffer_shared > 1
6911 || windows_or_buffers_changed);
6912 if (all_windows)
6913 {
6914 Lisp_Object tail, frame;
2d27dae2 6915 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
6916
6917 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6918
6919 FOR_EACH_FRAME (tail, frame)
6920 {
6921 f = XFRAME (frame);
6922
6923 /* Ignore tooltip frame. */
6b5c4794 6924 if (EQ (frame, tooltip_frame))
5f5c8ee5
GM
6925 continue;
6926
6927 /* If a window on this frame changed size, report that to
6928 the user and clear the size-change flag. */
6929 if (FRAME_WINDOW_SIZES_CHANGED (f))
6930 {
6931 Lisp_Object functions;
6932
6933 /* Clear flag first in case we get an error below. */
6934 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6935 functions = Vwindow_size_change_functions;
6936 GCPRO2 (tail, functions);
6937
6938 while (CONSP (functions))
6939 {
6940 call1 (XCAR (functions), frame);
6941 functions = XCDR (functions);
6942 }
6943 UNGCPRO;
6944 }
6945
6946 GCPRO1 (tail);
6947 update_menu_bar (f, 0);
6948#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 6949 update_tool_bar (f, 0);
5f5c8ee5
GM
6950#endif
6951 UNGCPRO;
6952 }
6953
6954 unbind_to (count, Qnil);
6955 }
6956 else
6957 {
886bd6f2
GM
6958 struct frame *sf = SELECTED_FRAME ();
6959 update_menu_bar (sf, 1);
5f5c8ee5 6960#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 6961 update_tool_bar (sf, 1);
5f5c8ee5
GM
6962#endif
6963 }
6964
6965 /* Motif needs this. See comment in xmenu.c. Turn it off when
6966 pending_menu_activation is not defined. */
6967#ifdef USE_X_TOOLKIT
6968 pending_menu_activation = 0;
6969#endif
6970}
6971
6972
6973/* Update the menu bar item list for frame F. This has to be done
6974 before we start to fill in any display lines, because it can call
6975 eval.
6976
6977 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6978
6979static void
6980update_menu_bar (f, save_match_data)
6981 struct frame *f;
6982 int save_match_data;
6983{
6984 Lisp_Object window;
6985 register struct window *w;
6986
6987 window = FRAME_SELECTED_WINDOW (f);
6988 w = XWINDOW (window);
6989
6990 if (update_mode_lines)
6991 w->update_mode_line = Qt;
6992
6993 if (FRAME_WINDOW_P (f)
6994 ?
1a578e9b 6995#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5
GM
6996 FRAME_EXTERNAL_MENU_BAR (f)
6997#else
6998 FRAME_MENU_BAR_LINES (f) > 0
6999#endif
7000 : FRAME_MENU_BAR_LINES (f) > 0)
7001 {
7002 /* If the user has switched buffers or windows, we need to
7003 recompute to reflect the new bindings. But we'll
7004 recompute when update_mode_lines is set too; that means
7005 that people can use force-mode-line-update to request
7006 that the menu bar be recomputed. The adverse effect on
7007 the rest of the redisplay algorithm is about the same as
7008 windows_or_buffers_changed anyway. */
7009 if (windows_or_buffers_changed
7010 || !NILP (w->update_mode_line)
7011 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7012 < BUF_MODIFF (XBUFFER (w->buffer)))
7013 != !NILP (w->last_had_star))
7014 || ((!NILP (Vtransient_mark_mode)
7015 && !NILP (XBUFFER (w->buffer)->mark_active))
7016 != !NILP (w->region_showing)))
7017 {
7018 struct buffer *prev = current_buffer;
2d27dae2 7019 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
7020
7021 set_buffer_internal_1 (XBUFFER (w->buffer));
7022 if (save_match_data)
7023 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7024 if (NILP (Voverriding_local_map_menu_flag))
7025 {
7026 specbind (Qoverriding_terminal_local_map, Qnil);
7027 specbind (Qoverriding_local_map, Qnil);
7028 }
7029
7030 /* Run the Lucid hook. */
7031 call1 (Vrun_hooks, Qactivate_menubar_hook);
7032
7033 /* If it has changed current-menubar from previous value,
7034 really recompute the menu-bar from the value. */
7035 if (! NILP (Vlucid_menu_bar_dirty_flag))
7036 call0 (Qrecompute_lucid_menubar);
7037
7038 safe_run_hooks (Qmenu_bar_update_hook);
7039 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7040
7041 /* Redisplay the menu bar in case we changed it. */
1a578e9b
AC
7042#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7043 if (FRAME_WINDOW_P (f)
7044#if defined (macintosh)
7045 /* All frames on Mac OS share the same menubar. So only the
7046 selected frame should be allowed to set it. */
7047 && f == SELECTED_FRAME ()
7048#endif
7049 )
5f5c8ee5
GM
7050 set_frame_menubar (f, 0, 0);
7051 else
7052 /* On a terminal screen, the menu bar is an ordinary screen
7053 line, and this makes it get updated. */
7054 w->update_mode_line = Qt;
7055#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7056 /* In the non-toolkit version, the menu bar is an ordinary screen
7057 line, and this makes it get updated. */
7058 w->update_mode_line = Qt;
7059#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7060
7061 unbind_to (count, Qnil);
7062 set_buffer_internal_1 (prev);
7063 }
7064 }
7065}
7066
7067
7068\f
7069/***********************************************************************
e037b9ec 7070 Tool-bars
5f5c8ee5
GM
7071 ***********************************************************************/
7072
7073#ifdef HAVE_WINDOW_SYSTEM
7074
e037b9ec 7075/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7076 before we start to fill in any display lines. Called from
7077 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7078 and restore it here. */
7079
7080static void
e037b9ec 7081update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7082 struct frame *f;
7083 int save_match_data;
7084{
e037b9ec
GM
7085 if (WINDOWP (f->tool_bar_window)
7086 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
7087 {
7088 Lisp_Object window;
7089 struct window *w;
7090
7091 window = FRAME_SELECTED_WINDOW (f);
7092 w = XWINDOW (window);
7093
7094 /* If the user has switched buffers or windows, we need to
7095 recompute to reflect the new bindings. But we'll
7096 recompute when update_mode_lines is set too; that means
7097 that people can use force-mode-line-update to request
7098 that the menu bar be recomputed. The adverse effect on
7099 the rest of the redisplay algorithm is about the same as
7100 windows_or_buffers_changed anyway. */
7101 if (windows_or_buffers_changed
7102 || !NILP (w->update_mode_line)
7103 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7104 < BUF_MODIFF (XBUFFER (w->buffer)))
7105 != !NILP (w->last_had_star))
7106 || ((!NILP (Vtransient_mark_mode)
7107 && !NILP (XBUFFER (w->buffer)->mark_active))
7108 != !NILP (w->region_showing)))
7109 {
7110 struct buffer *prev = current_buffer;
2d27dae2 7111 int count = BINDING_STACK_SIZE ();
a2889657 7112
5f5c8ee5
GM
7113 /* Set current_buffer to the buffer of the selected
7114 window of the frame, so that we get the right local
7115 keymaps. */
7116 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7117
5f5c8ee5
GM
7118 /* Save match data, if we must. */
7119 if (save_match_data)
7120 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7121
7122 /* Make sure that we don't accidentally use bogus keymaps. */
7123 if (NILP (Voverriding_local_map_menu_flag))
7124 {
7125 specbind (Qoverriding_terminal_local_map, Qnil);
7126 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7127 }
1f40cad2 7128
e037b9ec 7129 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7130 f->tool_bar_items
7131 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
5f5c8ee5 7132
e037b9ec 7133 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7134 w->update_mode_line = Qt;
7135
7136 unbind_to (count, Qnil);
7137 set_buffer_internal_1 (prev);
81d478f3 7138 }
a2889657
JB
7139 }
7140}
7141
6c4429a5 7142
e037b9ec 7143/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7144 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7145 been set up previously by calling prepare_menu_bars. */
7146
a2889657 7147static void
e037b9ec 7148build_desired_tool_bar_string (f)
5f5c8ee5 7149 struct frame *f;
a2889657 7150{
a23887b9 7151 int i, size, size_needed;
5f5c8ee5
GM
7152 struct gcpro gcpro1, gcpro2, gcpro3;
7153 Lisp_Object image, plist, props;
a2889657 7154
5f5c8ee5
GM
7155 image = plist = props = Qnil;
7156 GCPRO3 (image, plist, props);
a2889657 7157
e037b9ec 7158 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
7159 Otherwise, make a new string. */
7160
7161 /* The size of the string we might be able to reuse. */
e037b9ec
GM
7162 size = (STRINGP (f->desired_tool_bar_string)
7163 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
7164 : 0);
7165
b94c0d9c 7166 /* We need one space in the string for each image. */
a23887b9 7167 size_needed = f->n_tool_bar_items;
b94c0d9c
GM
7168
7169 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7170 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7171 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7172 make_number (' '));
5f5c8ee5
GM
7173 else
7174 {
7175 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7176 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7177 props, f->desired_tool_bar_string);
5f5c8ee5 7178 }
a2889657 7179
5f5c8ee5 7180 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7181 put a `menu_item' property on tool-bar items with a value that
7182 is the index of the item in F's tool-bar item vector. */
a23887b9 7183 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7184 {
7464726e 7185#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7186
e037b9ec
GM
7187 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7188 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7189 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7190 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7191 extern Lisp_Object Qlaplace;
7192
7193 /* If image is a vector, choose the image according to the
7194 button state. */
e037b9ec 7195 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7196 if (VECTORP (image))
7197 {
5f5c8ee5
GM
7198 if (enabled_p)
7199 idx = (selected_p
e037b9ec
GM
7200 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7201 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7202 else
7203 idx = (selected_p
e037b9ec
GM
7204 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7205 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5 7206
37e4e482
GM
7207 xassert (ASIZE (image) >= idx);
7208 image = AREF (image, idx);
5f5c8ee5 7209 }
37e4e482
GM
7210 else
7211 idx = -1;
5f5c8ee5
GM
7212
7213 /* Ignore invalid image specifications. */
7214 if (!valid_image_p (image))
7215 continue;
7216
e037b9ec 7217 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7218 plist = Fcopy_sequence (XCDR (image));
7219
7220 /* Compute margin and relief to draw. */
c3d76173
GM
7221 relief = (tool_bar_button_relief > 0
7222 ? tool_bar_button_relief
7223 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7224 hmargin = vmargin = relief;
7225
7226 if (INTEGERP (Vtool_bar_button_margin)
7227 && XINT (Vtool_bar_button_margin) > 0)
7228 {
7229 hmargin += XFASTINT (Vtool_bar_button_margin);
7230 vmargin += XFASTINT (Vtool_bar_button_margin);
7231 }
7232 else if (CONSP (Vtool_bar_button_margin))
7233 {
7234 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7235 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7236 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7237
7238 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7239 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7240 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7241 }
5f5c8ee5 7242
e037b9ec 7243 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7244 {
7245 /* Add a `:relief' property to the image spec if the item is
7246 selected. */
7247 if (selected_p)
7248 {
7249 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7250 hmargin -= relief;
7251 vmargin -= relief;
5f5c8ee5
GM
7252 }
7253 }
7254 else
7255 {
7256 /* If image is selected, display it pressed, i.e. with a
7257 negative relief. If it's not selected, display it with a
7258 raised relief. */
7259 plist = Fplist_put (plist, QCrelief,
7260 (selected_p
7261 ? make_number (-relief)
7262 : make_number (relief)));
35a41507
GM
7263 hmargin -= relief;
7264 vmargin -= relief;
5f5c8ee5
GM
7265 }
7266
7267 /* Put a margin around the image. */
35a41507
GM
7268 if (hmargin || vmargin)
7269 {
7270 if (hmargin == vmargin)
7271 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7272 else
7273 plist = Fplist_put (plist, QCmargin,
7274 Fcons (make_number (hmargin),
7275 make_number (vmargin)));
7276 }
5f5c8ee5 7277
37e4e482
GM
7278 /* If button is not enabled, and we don't have special images
7279 for the disabled state, make the image appear disabled by
5f5c8ee5 7280 applying an appropriate algorithm to it. */
37e4e482 7281 if (!enabled_p && idx < 0)
ecd01a0e 7282 plist = Fplist_put (plist, QCconversion, Qdisabled);
5f5c8ee5
GM
7283
7284 /* Put a `display' text property on the string for the image to
7285 display. Put a `menu-item' property on the string that gives
e037b9ec 7286 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7287 vector. */
7288 image = Fcons (Qimage, plist);
7289 props = list4 (Qdisplay, image,
a23887b9
GM
7290 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7291
7292 /* Let the last image hide all remaining spaces in the tool bar
7293 string. The string can be longer than needed when we reuse a
7294 previous string. */
7295 if (i + 1 == f->n_tool_bar_items)
7296 end = XSTRING (f->desired_tool_bar_string)->size;
7297 else
7298 end = i + 1;
7299 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7300 props, f->desired_tool_bar_string);
5f5c8ee5 7301#undef PROP
a2889657
JB
7302 }
7303
5f5c8ee5
GM
7304 UNGCPRO;
7305}
7306
7307
e037b9ec 7308/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7309
7310static void
e037b9ec 7311display_tool_bar_line (it)
5f5c8ee5
GM
7312 struct it *it;
7313{
7314 struct glyph_row *row = it->glyph_row;
7315 int max_x = it->last_visible_x;
7316 struct glyph *last;
7317
7318 prepare_desired_row (row);
7319 row->y = it->current_y;
90aa2856
GM
7320
7321 /* Note that this isn't made use of if the face hasn't a box,
7322 so there's no need to check the face here. */
7323 it->start_of_box_run_p = 1;
5f5c8ee5
GM
7324
7325 while (it->current_x < max_x)
a2889657 7326 {
5f5c8ee5 7327 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7328
5f5c8ee5
GM
7329 /* Get the next display element. */
7330 if (!get_next_display_element (it))
7331 break;
73af359d 7332
5f5c8ee5
GM
7333 /* Produce glyphs. */
7334 x_before = it->current_x;
7335 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7336 PRODUCE_GLYPHS (it);
daa37602 7337
5f5c8ee5
GM
7338 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7339 i = 0;
7340 x = x_before;
7341 while (i < nglyphs)
7342 {
7343 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7344
7345 if (x + glyph->pixel_width > max_x)
7346 {
7347 /* Glyph doesn't fit on line. */
7348 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7349 it->current_x = x;
7350 goto out;
7351 }
daa37602 7352
5f5c8ee5
GM
7353 ++it->hpos;
7354 x += glyph->pixel_width;
7355 ++i;
7356 }
7357
7358 /* Stop at line ends. */
7359 if (ITERATOR_AT_END_OF_LINE_P (it))
7360 break;
7361
cafafe0b 7362 set_iterator_to_next (it, 1);
a2889657 7363 }
a2889657 7364
5f5c8ee5 7365 out:;
a2889657 7366
5f5c8ee5
GM
7367 row->displays_text_p = row->used[TEXT_AREA] != 0;
7368 extend_face_to_end_of_line (it);
7369 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7370 last->right_box_line_p = 1;
90aa2856
GM
7371 if (last == row->glyphs[TEXT_AREA])
7372 last->left_box_line_p = 1;
5f5c8ee5
GM
7373 compute_line_metrics (it);
7374
e037b9ec 7375 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7376 if (!row->displays_text_p)
7377 {
312246d1
GM
7378 row->height = row->phys_height = it->last_visible_y - row->y;
7379 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
7380 }
7381
7382 row->full_width_p = 1;
7383 row->continued_p = 0;
7384 row->truncated_on_left_p = 0;
7385 row->truncated_on_right_p = 0;
7386
7387 it->current_x = it->hpos = 0;
7388 it->current_y += row->height;
7389 ++it->vpos;
7390 ++it->glyph_row;
a2889657 7391}
96a410bc 7392
5f5c8ee5 7393
e037b9ec 7394/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7395 items of frame F visible. */
96a410bc 7396
d39b6696 7397static int
e037b9ec 7398tool_bar_lines_needed (f)
5f5c8ee5 7399 struct frame *f;
d39b6696 7400{
e037b9ec 7401 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
7402 struct it it;
7403
e037b9ec
GM
7404 /* Initialize an iterator for iteration over
7405 F->desired_tool_bar_string in the tool-bar window of frame F. */
7406 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7407 it.first_visible_x = 0;
7408 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7409 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7410
7411 while (!ITERATOR_AT_END_P (&it))
7412 {
7413 it.glyph_row = w->desired_matrix->rows;
7414 clear_glyph_row (it.glyph_row);
e037b9ec 7415 display_tool_bar_line (&it);
5f5c8ee5
GM
7416 }
7417
7418 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7419}
96a410bc 7420
5f5c8ee5 7421
57c28064
GM
7422DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7423 0, 1, 0,
7424 "Return the number of lines occupied by the tool bar of FRAME.")
7425 (frame)
7426 Lisp_Object frame;
7427{
7428 struct frame *f;
7429 struct window *w;
7430 int nlines = 0;
7431
7432 if (NILP (frame))
7433 frame = selected_frame;
7434 else
7435 CHECK_FRAME (frame, 0);
7436 f = XFRAME (frame);
7437
7438 if (WINDOWP (f->tool_bar_window)
7439 || (w = XWINDOW (f->tool_bar_window),
7440 XFASTINT (w->height) > 0))
7441 {
7442 update_tool_bar (f, 1);
7443 if (f->n_tool_bar_items)
7444 {
7445 build_desired_tool_bar_string (f);
7446 nlines = tool_bar_lines_needed (f);
7447 }
7448 }
7449
7450 return make_number (nlines);
7451}
7452
7453
e037b9ec 7454/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7455 height should be changed. */
7456
7457static int
e037b9ec 7458redisplay_tool_bar (f)
5f5c8ee5 7459 struct frame *f;
96a410bc 7460{
5f5c8ee5
GM
7461 struct window *w;
7462 struct it it;
7463 struct glyph_row *row;
7464 int change_height_p = 0;
7465
e037b9ec
GM
7466 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7467 do anything. This means you must start with tool-bar-lines
5f5c8ee5 7468 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
7469 can turn off tool-bars by specifying tool-bar-lines zero. */
7470 if (!WINDOWP (f->tool_bar_window)
7471 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
7472 XFASTINT (w->height) == 0))
7473 return 0;
96a410bc 7474
e037b9ec
GM
7475 /* Set up an iterator for the tool-bar window. */
7476 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7477 it.first_visible_x = 0;
7478 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7479 row = it.glyph_row;
3450d04c 7480
e037b9ec
GM
7481 /* Build a string that represents the contents of the tool-bar. */
7482 build_desired_tool_bar_string (f);
7483 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 7484
e037b9ec 7485 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 7486 while (it.current_y < it.last_visible_y)
e037b9ec 7487 display_tool_bar_line (&it);
3450d04c 7488
e037b9ec 7489 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
7490 window, so don't do it. */
7491 w->desired_matrix->no_scrolling_p = 1;
7492 w->must_be_updated_p = 1;
3450d04c 7493
e037b9ec 7494 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
7495 {
7496 int nlines;
6e33e6f0
GM
7497
7498 /* If we couldn't display everything, change the tool-bar's
7499 height. */
7500 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7501 change_height_p = 1;
5f5c8ee5
GM
7502
7503 /* If there are blank lines at the end, except for a partially
7504 visible blank line at the end that is smaller than
e037b9ec 7505 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
7506 row = it.glyph_row - 1;
7507 if (!row->displays_text_p
7508 && row->height >= CANON_Y_UNIT (f))
7509 change_height_p = 1;
7510
e037b9ec
GM
7511 /* If row displays tool-bar items, but is partially visible,
7512 change the tool-bar's height. */
5f5c8ee5
GM
7513 if (row->displays_text_p
7514 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7515 change_height_p = 1;
7516
e037b9ec 7517 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
7518 frame parameter. */
7519 if (change_height_p
e037b9ec 7520 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
7521 nlines != XFASTINT (w->height)))
7522 {
e037b9ec 7523 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 7524 Lisp_Object frame;
e85ee976 7525 int old_height = XFASTINT (w->height);
5f5c8ee5
GM
7526
7527 XSETFRAME (frame, f);
7528 clear_glyph_matrix (w->desired_matrix);
7529 Fmodify_frame_parameters (frame,
e037b9ec 7530 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
7531 make_number (nlines)),
7532 Qnil));
e85ee976
GM
7533 if (XFASTINT (w->height) != old_height)
7534 fonts_changed_p = 1;
5f5c8ee5
GM
7535 }
7536 }
3450d04c 7537
5f5c8ee5 7538 return change_height_p;
96a410bc 7539}
90adcf20 7540
5f5c8ee5 7541
e037b9ec
GM
7542/* Get information about the tool-bar item which is displayed in GLYPH
7543 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 7544 properties start in F->tool_bar_items. Value is zero if
e037b9ec 7545 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
7546
7547int
e037b9ec 7548tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
7549 struct frame *f;
7550 struct glyph *glyph;
7551 int *prop_idx;
90adcf20 7552{
5f5c8ee5
GM
7553 Lisp_Object prop;
7554 int success_p;
7555
7556 /* Get the text property `menu-item' at pos. The value of that
7557 property is the start index of this item's properties in
7464726e 7558 F->tool_bar_items. */
5f5c8ee5 7559 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 7560 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
7561 if (INTEGERP (prop))
7562 {
7563 *prop_idx = XINT (prop);
7564 success_p = 1;
7565 }
7566 else
7567 success_p = 0;
90adcf20 7568
5f5c8ee5
GM
7569 return success_p;
7570}
7571
7572#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 7573
feb0c42f 7574
5f5c8ee5
GM
7575\f
7576/************************************************************************
7577 Horizontal scrolling
7578 ************************************************************************/
feb0c42f 7579
5f5c8ee5
GM
7580static int hscroll_window_tree P_ ((Lisp_Object));
7581static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 7582
5f5c8ee5
GM
7583/* For all leaf windows in the window tree rooted at WINDOW, set their
7584 hscroll value so that PT is (i) visible in the window, and (ii) so
7585 that it is not within a certain margin at the window's left and
7586 right border. Value is non-zero if any window's hscroll has been
7587 changed. */
7588
7589static int
7590hscroll_window_tree (window)
7591 Lisp_Object window;
7592{
7593 int hscrolled_p = 0;
7594
7595 while (WINDOWP (window))
90adcf20 7596 {
5f5c8ee5
GM
7597 struct window *w = XWINDOW (window);
7598
7599 if (WINDOWP (w->hchild))
7600 hscrolled_p |= hscroll_window_tree (w->hchild);
7601 else if (WINDOWP (w->vchild))
7602 hscrolled_p |= hscroll_window_tree (w->vchild);
7603 else if (w->cursor.vpos >= 0)
7604 {
7605 int hscroll_margin, text_area_x, text_area_y;
7606 int text_area_width, text_area_height;
92a90e89
GM
7607 struct glyph_row *current_cursor_row
7608 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7609 struct glyph_row *desired_cursor_row
7610 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7611 struct glyph_row *cursor_row
7612 = (desired_cursor_row->enabled_p
7613 ? desired_cursor_row
7614 : current_cursor_row);
a2725ab2 7615
5f5c8ee5
GM
7616 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7617 &text_area_width, &text_area_height);
90adcf20 7618
5f5c8ee5
GM
7619 /* Scroll when cursor is inside this scroll margin. */
7620 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7621
7622 if ((XFASTINT (w->hscroll)
7623 && w->cursor.x < hscroll_margin)
92a90e89
GM
7624 || (cursor_row->enabled_p
7625 && cursor_row->truncated_on_right_p
5f5c8ee5 7626 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 7627 {
5f5c8ee5
GM
7628 struct it it;
7629 int hscroll;
7630 struct buffer *saved_current_buffer;
7631 int pt;
7632
7633 /* Find point in a display of infinite width. */
7634 saved_current_buffer = current_buffer;
7635 current_buffer = XBUFFER (w->buffer);
7636
7637 if (w == XWINDOW (selected_window))
7638 pt = BUF_PT (current_buffer);
7639 else
08b610e4 7640 {
5f5c8ee5
GM
7641 pt = marker_position (w->pointm);
7642 pt = max (BEGV, pt);
7643 pt = min (ZV, pt);
7644 }
7645
7646 /* Move iterator to pt starting at cursor_row->start in
7647 a line with infinite width. */
7648 init_to_row_start (&it, w, cursor_row);
7649 it.last_visible_x = INFINITY;
7650 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7651 current_buffer = saved_current_buffer;
7652
7653 /* Center cursor in window. */
7654 hscroll = (max (0, it.current_x - text_area_width / 2)
7655 / CANON_X_UNIT (it.f));
3172f70b 7656 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
7657
7658 /* Don't call Fset_window_hscroll if value hasn't
7659 changed because it will prevent redisplay
7660 optimizations. */
7661 if (XFASTINT (w->hscroll) != hscroll)
7662 {
3172f70b
GM
7663 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7664 w->hscroll = make_number (hscroll);
5f5c8ee5 7665 hscrolled_p = 1;
08b610e4 7666 }
08b610e4 7667 }
08b610e4 7668 }
a2725ab2 7669
5f5c8ee5 7670 window = w->next;
90adcf20 7671 }
cd6dfed6 7672
5f5c8ee5
GM
7673 /* Value is non-zero if hscroll of any leaf window has been changed. */
7674 return hscrolled_p;
7675}
7676
7677
7678/* Set hscroll so that cursor is visible and not inside horizontal
7679 scroll margins for all windows in the tree rooted at WINDOW. See
7680 also hscroll_window_tree above. Value is non-zero if any window's
7681 hscroll has been changed. If it has, desired matrices on the frame
7682 of WINDOW are cleared. */
7683
7684static int
7685hscroll_windows (window)
7686 Lisp_Object window;
7687{
d475bcb8
GM
7688 int hscrolled_p;
7689
7690 if (automatic_hscrolling_p)
7691 {
7692 hscrolled_p = hscroll_window_tree (window);
7693 if (hscrolled_p)
7694 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7695 }
7696 else
7697 hscrolled_p = 0;
5f5c8ee5 7698 return hscrolled_p;
90adcf20 7699}
5f5c8ee5
GM
7700
7701
90adcf20 7702\f
5f5c8ee5
GM
7703/************************************************************************
7704 Redisplay
7705 ************************************************************************/
7706
7707/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7708 to a non-zero value. This is sometimes handy to have in a debugger
7709 session. */
7710
7711#if GLYPH_DEBUG
a2889657 7712
5f5c8ee5
GM
7713/* First and last unchanged row for try_window_id. */
7714
7715int debug_first_unchanged_at_end_vpos;
7716int debug_last_unchanged_at_beg_vpos;
7717
7718/* Delta vpos and y. */
7719
7720int debug_dvpos, debug_dy;
7721
7722/* Delta in characters and bytes for try_window_id. */
7723
7724int debug_delta, debug_delta_bytes;
7725
7726/* Values of window_end_pos and window_end_vpos at the end of
7727 try_window_id. */
7728
7729int debug_end_pos, debug_end_vpos;
7730
7731/* Append a string to W->desired_matrix->method. FMT is a printf
7732 format string. A1...A9 are a supplement for a variable-length
7733 argument list. If trace_redisplay_p is non-zero also printf the
7734 resulting string to stderr. */
7735
7736static void
7737debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7738 struct window *w;
7739 char *fmt;
7740 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7741{
7742 char buffer[512];
7743 char *method = w->desired_matrix->method;
7744 int len = strlen (method);
7745 int size = sizeof w->desired_matrix->method;
7746 int remaining = size - len - 1;
7747
7748 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7749 if (len && remaining)
7750 {
7751 method[len] = '|';
7752 --remaining, ++len;
7753 }
7754
7755 strncpy (method + len, buffer, remaining);
7756
7757 if (trace_redisplay_p)
7758 fprintf (stderr, "%p (%s): %s\n",
7759 w,
7760 ((BUFFERP (w->buffer)
7761 && STRINGP (XBUFFER (w->buffer)->name))
7762 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7763 : "no buffer"),
7764 buffer);
7765}
a2889657 7766
5f5c8ee5 7767#endif /* GLYPH_DEBUG */
90adcf20 7768
a2889657 7769
5f5c8ee5
GM
7770/* This counter is used to clear the face cache every once in a while
7771 in redisplay_internal. It is incremented for each redisplay.
7772 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7773 cleared. */
0d231165 7774
5f5c8ee5 7775#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
7776static int clear_face_cache_count;
7777
20de20dc 7778/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
7779
7780static struct frame *previous_terminal_frame;
7781
7782/* Non-zero while redisplay_internal is in progress. */
7783
7784int redisplaying_p;
7785
7786
7787/* Value is non-zero if all changes in window W, which displays
7788 current_buffer, are in the text between START and END. START is a
7789 buffer position, END is given as a distance from Z. Used in
7790 redisplay_internal for display optimization. */
7791
7792static INLINE int
7793text_outside_line_unchanged_p (w, start, end)
7794 struct window *w;
7795 int start, end;
7796{
7797 int unchanged_p = 1;
7798
7799 /* If text or overlays have changed, see where. */
7800 if (XFASTINT (w->last_modified) < MODIFF
7801 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7802 {
7803 /* Gap in the line? */
7804 if (GPT < start || Z - GPT < end)
7805 unchanged_p = 0;
7806
7807 /* Changes start in front of the line, or end after it? */
7808 if (unchanged_p
9142dd5b
GM
7809 && (BEG_UNCHANGED < start - 1
7810 || END_UNCHANGED < end))
5f5c8ee5
GM
7811 unchanged_p = 0;
7812
7813 /* If selective display, can't optimize if changes start at the
7814 beginning of the line. */
7815 if (unchanged_p
7816 && INTEGERP (current_buffer->selective_display)
7817 && XINT (current_buffer->selective_display) > 0
9142dd5b 7818 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
7819 unchanged_p = 0;
7820 }
7821
7822 return unchanged_p;
7823}
7824
7825
7826/* Do a frame update, taking possible shortcuts into account. This is
7827 the main external entry point for redisplay.
7828
7829 If the last redisplay displayed an echo area message and that message
7830 is no longer requested, we clear the echo area or bring back the
7831 mini-buffer if that is in use. */
20de20dc 7832
a2889657
JB
7833void
7834redisplay ()
e9874cee
RS
7835{
7836 redisplay_internal (0);
7837}
7838
260a86a0
KH
7839/* Return 1 if point moved out of or into a composition. Otherwise
7840 return 0. PREV_BUF and PREV_PT are the last point buffer and
7841 position. BUF and PT are the current point buffer and position. */
7842
7843int
7844check_point_in_composition (prev_buf, prev_pt, buf, pt)
7845 struct buffer *prev_buf, *buf;
7846 int prev_pt, pt;
7847{
7848 int start, end;
7849 Lisp_Object prop;
7850 Lisp_Object buffer;
7851
7852 XSETBUFFER (buffer, buf);
7853 /* Check a composition at the last point if point moved within the
7854 same buffer. */
7855 if (prev_buf == buf)
7856 {
7857 if (prev_pt == pt)
7858 /* Point didn't move. */
7859 return 0;
7860
7861 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7862 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7863 && COMPOSITION_VALID_P (start, end, prop)
7864 && start < prev_pt && end > prev_pt)
7865 /* The last point was within the composition. Return 1 iff
7866 point moved out of the composition. */
7867 return (pt <= start || pt >= end);
7868 }
7869
7870 /* Check a composition at the current point. */
7871 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7872 && find_composition (pt, -1, &start, &end, &prop, buffer)
7873 && COMPOSITION_VALID_P (start, end, prop)
7874 && start < pt && end > pt);
7875}
5f5c8ee5 7876
9142dd5b
GM
7877/* Reconsider the setting of B->clip_changed which is displayed
7878 in window W. */
7879
7880static INLINE void
7881reconsider_clip_changes (w, b)
7882 struct window *w;
7883 struct buffer *b;
7884{
7885 if (b->prevent_redisplay_optimizations_p)
7886 b->clip_changed = 1;
7887 else if (b->clip_changed
7888 && !NILP (w->window_end_valid)
7889 && w->current_matrix->buffer == b
7890 && w->current_matrix->zv == BUF_ZV (b)
7891 && w->current_matrix->begv == BUF_BEGV (b))
7892 b->clip_changed = 0;
260a86a0
KH
7893
7894 /* If display wasn't paused, and W is not a tool bar window, see if
7895 point has been moved into or out of a composition. In that case,
7896 we set b->clip_changed to 1 to force updating the screen. If
7897 b->clip_changed has already been set to 1, we can skip this
7898 check. */
7899 if (!b->clip_changed
7900 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7901 {
7902 int pt;
7903
7904 if (w == XWINDOW (selected_window))
7905 pt = BUF_PT (current_buffer);
7906 else
7907 pt = marker_position (w->pointm);
7908
7909 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 7910 || pt != XINT (w->last_point))
260a86a0 7911 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 7912 XINT (w->last_point),
260a86a0
KH
7913 XBUFFER (w->buffer), pt))
7914 b->clip_changed = 1;
7915 }
9142dd5b
GM
7916}
7917
7918
5f5c8ee5
GM
7919/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7920 response to any user action; therefore, we should preserve the echo
7921 area. (Actually, our caller does that job.) Perhaps in the future
7922 avoid recentering windows if it is not necessary; currently that
7923 causes some problems. */
e9874cee
RS
7924
7925static void
7926redisplay_internal (preserve_echo_area)
7927 int preserve_echo_area;
a2889657 7928{
5f5c8ee5
GM
7929 struct window *w = XWINDOW (selected_window);
7930 struct frame *f = XFRAME (w->frame);
7931 int pause;
a2889657 7932 int must_finish = 0;
5f5c8ee5 7933 struct text_pos tlbufpos, tlendpos;
89819bdd 7934 int number_of_visible_frames;
28514cd9 7935 int count;
886bd6f2 7936 struct frame *sf = SELECTED_FRAME ();
a2889657 7937
5f5c8ee5
GM
7938 /* Non-zero means redisplay has to consider all windows on all
7939 frames. Zero means, only selected_window is considered. */
7940 int consider_all_windows_p;
7941
7942 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7943
7944 /* No redisplay if running in batch mode or frame is not yet fully
7945 initialized, or redisplay is explicitly turned off by setting
7946 Vinhibit_redisplay. */
7947 if (noninteractive
7948 || !NILP (Vinhibit_redisplay)
7949 || !f->glyphs_initialized_p)
a2889657
JB
7950 return;
7951
5f5c8ee5
GM
7952 /* The flag redisplay_performed_directly_p is set by
7953 direct_output_for_insert when it already did the whole screen
7954 update necessary. */
7955 if (redisplay_performed_directly_p)
7956 {
7957 redisplay_performed_directly_p = 0;
7958 if (!hscroll_windows (selected_window))
7959 return;
7960 }
7961
15f0cf78
RS
7962#ifdef USE_X_TOOLKIT
7963 if (popup_activated ())
7964 return;
7965#endif
7966
28514cd9 7967 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 7968 if (redisplaying_p)
735c094c
KH
7969 return;
7970
28514cd9
GM
7971 /* Record a function that resets redisplaying_p to its old value
7972 when we leave this function. */
2d27dae2 7973 count = BINDING_STACK_SIZE ();
28514cd9
GM
7974 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7975 ++redisplaying_p;
7976
8b32d885 7977 retry:
bd9d0f3f 7978 pause = 0;
9142dd5b
GM
7979 reconsider_clip_changes (w, current_buffer);
7980
5f5c8ee5
GM
7981 /* If new fonts have been loaded that make a glyph matrix adjustment
7982 necessary, do it. */
7983 if (fonts_changed_p)
7984 {
7985 adjust_glyphs (NULL);
7986 ++windows_or_buffers_changed;
7987 fonts_changed_p = 0;
7988 }
7989
886bd6f2
GM
7990 if (! FRAME_WINDOW_P (sf)
7991 && previous_terminal_frame != sf)
20de20dc 7992 {
5f5c8ee5
GM
7993 /* Since frames on an ASCII terminal share the same display
7994 area, displaying a different frame means redisplay the whole
7995 thing. */
20de20dc 7996 windows_or_buffers_changed++;
886bd6f2
GM
7997 SET_FRAME_GARBAGED (sf);
7998 XSETFRAME (Vterminal_frame, sf);
20de20dc 7999 }
886bd6f2 8000 previous_terminal_frame = sf;
20de20dc 8001
5f5c8ee5
GM
8002 /* Set the visible flags for all frames. Do this before checking
8003 for resized or garbaged frames; they want to know if their frames
8004 are visible. See the comment in frame.h for
8005 FRAME_SAMPLE_VISIBILITY. */
d724d989 8006 {
35f56f96 8007 Lisp_Object tail, frame;
d724d989 8008
89819bdd
RS
8009 number_of_visible_frames = 0;
8010
35f56f96 8011 FOR_EACH_FRAME (tail, frame)
f82aff7c 8012 {
5f5c8ee5
GM
8013 struct frame *f = XFRAME (frame);
8014
8015 FRAME_SAMPLE_VISIBILITY (f);
8016 if (FRAME_VISIBLE_P (f))
8017 ++number_of_visible_frames;
8018 clear_desired_matrices (f);
f82aff7c 8019 }
d724d989
JB
8020 }
8021
44fa5b1e 8022 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8023 do_pending_window_change (1);
a2889657 8024
5f5c8ee5 8025 /* Clear frames marked as garbaged. */
44fa5b1e 8026 if (frame_garbaged)
c6e89d6c 8027 clear_garbaged_frames ();
a2889657 8028
e037b9ec 8029 /* Build menubar and tool-bar items. */
f82aff7c
RS
8030 prepare_menu_bars ();
8031
28995e67 8032 if (windows_or_buffers_changed)
a2889657
JB
8033 update_mode_lines++;
8034
538f13d4
RS
8035 /* Detect case that we need to write or remove a star in the mode line. */
8036 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8037 {
8038 w->update_mode_line = Qt;
8039 if (buffer_shared > 1)
8040 update_mode_lines++;
8041 }
8042
5f5c8ee5 8043 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8044 if (!NILP (w->column_number_displayed)
8045 /* This alternative quickly identifies a common case
8046 where no change is needed. */
8047 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8048 && XFASTINT (w->last_modified) >= MODIFF
8049 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8050 && XFASTINT (w->column_number_displayed) != current_column ())
8051 w->update_mode_line = Qt;
8052
44fa5b1e 8053 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8054
5f5c8ee5
GM
8055 /* The variable buffer_shared is set in redisplay_window and
8056 indicates that we redisplay a buffer in different windows. See
8057 there. */
8058 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
8059
8060 /* If specs for an arrow have changed, do thorough redisplay
8061 to ensure we remove any arrow that should no longer exist. */
d45de95b 8062 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8063 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8064 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8065
90adcf20
RS
8066 /* Normally the message* functions will have already displayed and
8067 updated the echo area, but the frame may have been trashed, or
8068 the update may have been preempted, so display the echo area
c6e89d6c
GM
8069 again here. Checking both message buffers captures the case that
8070 the echo area should be cleared. */
8071 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 8072 {
c6e89d6c 8073 int window_height_changed_p = echo_area_display (0);
90adcf20 8074 must_finish = 1;
dd2eb166 8075
c6e89d6c
GM
8076 if (fonts_changed_p)
8077 goto retry;
8078 else if (window_height_changed_p)
8079 {
8080 consider_all_windows_p = 1;
8081 ++update_mode_lines;
8082 ++windows_or_buffers_changed;
9142dd5b
GM
8083
8084 /* If window configuration was changed, frames may have been
8085 marked garbaged. Clear them or we will experience
8086 surprises wrt scrolling. */
8087 if (frame_garbaged)
8088 clear_garbaged_frames ();
c6e89d6c 8089 }
90adcf20 8090 }
97a36635 8091 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
8092 && (current_buffer->clip_changed
8093 || XFASTINT (w->last_modified) < MODIFF
8094 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 8095 && resize_mini_window (w, 0))
c6e89d6c
GM
8096 {
8097 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
8098 showing if its contents might have changed. */
8099 must_finish = 1;
8100 consider_all_windows_p = 1;
c6e89d6c 8101 ++windows_or_buffers_changed;
dd2eb166 8102 ++update_mode_lines;
9142dd5b
GM
8103
8104 /* If window configuration was changed, frames may have been
8105 marked garbaged. Clear them or we will experience
8106 surprises wrt scrolling. */
8107 if (frame_garbaged)
8108 clear_garbaged_frames ();
c6e89d6c
GM
8109 }
8110
90adcf20 8111
5f5c8ee5
GM
8112 /* If showing the region, and mark has changed, we must redisplay
8113 the whole window. The assignment to this_line_start_pos prevents
8114 the optimization directly below this if-statement. */
bd66d1ba
RS
8115 if (((!NILP (Vtransient_mark_mode)
8116 && !NILP (XBUFFER (w->buffer)->mark_active))
8117 != !NILP (w->region_showing))
82d04750
JB
8118 || (!NILP (w->region_showing)
8119 && !EQ (w->region_showing,
8120 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
8121 CHARPOS (this_line_start_pos) = 0;
8122
8123 /* Optimize the case that only the line containing the cursor in the
8124 selected window has changed. Variables starting with this_ are
8125 set in display_line and record information about the line
8126 containing the cursor. */
8127 tlbufpos = this_line_start_pos;
8128 tlendpos = this_line_end_pos;
8129 if (!consider_all_windows_p
8130 && CHARPOS (tlbufpos) > 0
8131 && NILP (w->update_mode_line)
73af359d 8132 && !current_buffer->clip_changed
44fa5b1e 8133 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 8134 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 8135 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
8136 && this_line_buffer == current_buffer
8137 && current_buffer == XBUFFER (w->buffer)
265a9e55 8138 && NILP (w->force_start)
5f5c8ee5
GM
8139 /* Point must be on the line that we have info recorded about. */
8140 && PT >= CHARPOS (tlbufpos)
8141 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
8142 /* All text outside that line, including its final newline,
8143 must be unchanged */
5f5c8ee5
GM
8144 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8145 CHARPOS (tlendpos)))
8146 {
8147 if (CHARPOS (tlbufpos) > BEGV
8148 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8149 && (CHARPOS (tlbufpos) == ZV
8150 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
8151 /* Former continuation line has disappeared by becoming empty */
8152 goto cancel;
8153 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 8154 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
8155 || MINI_WINDOW_P (w))
8156 {
1c9241f5
KH
8157 /* We have to handle the case of continuation around a
8158 wide-column character (See the comment in indent.c around
8159 line 885).
8160
8161 For instance, in the following case:
8162
8163 -------- Insert --------
8164 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8165 J_I_ ==> J_I_ `^^' are cursors.
8166 ^^ ^^
8167 -------- --------
8168
8169 As we have to redraw the line above, we should goto cancel. */
8170
5f5c8ee5
GM
8171 struct it it;
8172 int line_height_before = this_line_pixel_height;
8173
8174 /* Note that start_display will handle the case that the
8175 line starting at tlbufpos is a continuation lines. */
8176 start_display (&it, w, tlbufpos);
8177
8178 /* Implementation note: It this still necessary? */
8179 if (it.current_x != this_line_start_x)
1c9241f5
KH
8180 goto cancel;
8181
5f5c8ee5
GM
8182 TRACE ((stderr, "trying display optimization 1\n"));
8183 w->cursor.vpos = -1;
a2889657 8184 overlay_arrow_seen = 0;
5f5c8ee5
GM
8185 it.vpos = this_line_vpos;
8186 it.current_y = this_line_y;
8187 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8188 display_line (&it);
8189
a2889657 8190 /* If line contains point, is not continued,
5f5c8ee5
GM
8191 and ends at same distance from eob as before, we win */
8192 if (w->cursor.vpos >= 0
8193 /* Line is not continued, otherwise this_line_start_pos
8194 would have been set to 0 in display_line. */
8195 && CHARPOS (this_line_start_pos)
8196 /* Line ends as before. */
8197 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8198 /* Line has same height as before. Otherwise other lines
8199 would have to be shifted up or down. */
8200 && this_line_pixel_height == line_height_before)
a2889657 8201 {
5f5c8ee5
GM
8202 /* If this is not the window's last line, we must adjust
8203 the charstarts of the lines below. */
8204 if (it.current_y < it.last_visible_y)
8205 {
8206 struct glyph_row *row
8207 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8208 int delta, delta_bytes;
8209
8210 if (Z - CHARPOS (tlendpos) == ZV)
8211 {
8212 /* This line ends at end of (accessible part of)
8213 buffer. There is no newline to count. */
8214 delta = (Z
8215 - CHARPOS (tlendpos)
8216 - MATRIX_ROW_START_CHARPOS (row));
8217 delta_bytes = (Z_BYTE
8218 - BYTEPOS (tlendpos)
8219 - MATRIX_ROW_START_BYTEPOS (row));
8220 }
8221 else
8222 {
8223 /* This line ends in a newline. Must take
8224 account of the newline and the rest of the
8225 text that follows. */
8226 delta = (Z
8227 - CHARPOS (tlendpos)
8228 - MATRIX_ROW_START_CHARPOS (row));
8229 delta_bytes = (Z_BYTE
8230 - BYTEPOS (tlendpos)
8231 - MATRIX_ROW_START_BYTEPOS (row));
8232 }
8233
f2d86d7a
GM
8234 increment_matrix_positions (w->current_matrix,
8235 this_line_vpos + 1,
8236 w->current_matrix->nrows,
8237 delta, delta_bytes);
85bcef6c 8238 }
46db8486 8239
5f5c8ee5
GM
8240 /* If this row displays text now but previously didn't,
8241 or vice versa, w->window_end_vpos may have to be
8242 adjusted. */
8243 if ((it.glyph_row - 1)->displays_text_p)
8244 {
8245 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8246 XSETINT (w->window_end_vpos, this_line_vpos);
8247 }
8248 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8249 && this_line_vpos > 0)
8250 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8251 w->window_end_valid = Qnil;
8252
8253 /* Update hint: No need to try to scroll in update_window. */
8254 w->desired_matrix->no_scrolling_p = 1;
8255
8256#if GLYPH_DEBUG
8257 *w->desired_matrix->method = 0;
8258 debug_method_add (w, "optimization 1");
8259#endif
a2889657
JB
8260 goto update;
8261 }
8262 else
8263 goto cancel;
8264 }
5f5c8ee5
GM
8265 else if (/* Cursor position hasn't changed. */
8266 PT == XFASTINT (w->last_point)
b6f0fe04
RS
8267 /* Make sure the cursor was last displayed
8268 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
8269 && 0 <= w->cursor.vpos
8270 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
8271 {
8272 if (!must_finish)
8273 {
c6e89d6c 8274 do_pending_window_change (1);
5f5c8ee5
GM
8275
8276 /* We used to always goto end_of_redisplay here, but this
8277 isn't enough if we have a blinking cursor. */
8278 if (w->cursor_off_p == w->last_cursor_off_p)
8279 goto end_of_redisplay;
a2889657
JB
8280 }
8281 goto update;
8282 }
8b51f1e3
KH
8283 /* If highlighting the region, or if the cursor is in the echo area,
8284 then we can't just move the cursor. */
bd66d1ba
RS
8285 else if (! (!NILP (Vtransient_mark_mode)
8286 && !NILP (current_buffer->mark_active))
97a36635 8287 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 8288 || highlight_nonselected_windows)
8b51f1e3 8289 && NILP (w->region_showing)
8f897821 8290 && NILP (Vshow_trailing_whitespace)
8b51f1e3 8291 && !cursor_in_echo_area)
a2889657 8292 {
5f5c8ee5
GM
8293 struct it it;
8294 struct glyph_row *row;
8295
8296 /* Skip from tlbufpos to PT and see where it is. Note that
8297 PT may be in invisible text. If so, we will end at the
8298 next visible position. */
8299 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8300 NULL, DEFAULT_FACE_ID);
8301 it.current_x = this_line_start_x;
8302 it.current_y = this_line_y;
8303 it.vpos = this_line_vpos;
8304
8305 /* The call to move_it_to stops in front of PT, but
8306 moves over before-strings. */
8307 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8308
8309 if (it.vpos == this_line_vpos
8310 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8311 row->enabled_p))
a2889657 8312 {
5f5c8ee5
GM
8313 xassert (this_line_vpos == it.vpos);
8314 xassert (this_line_y == it.current_y);
8315 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
8316 goto update;
8317 }
8318 else
8319 goto cancel;
8320 }
5f5c8ee5 8321
a2889657 8322 cancel:
5f5c8ee5
GM
8323 /* Text changed drastically or point moved off of line. */
8324 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
8325 }
8326
5f5c8ee5
GM
8327 CHARPOS (this_line_start_pos) = 0;
8328 consider_all_windows_p |= buffer_shared > 1;
8329 ++clear_face_cache_count;
a2889657 8330
5f5c8ee5 8331
bd9d0f3f
GM
8332 /* Build desired matrices, and update the display. If
8333 consider_all_windows_p is non-zero, do it for all windows on all
8334 frames. Otherwise do it for selected_window, only. */
463f6b91 8335
5f5c8ee5 8336 if (consider_all_windows_p)
a2889657 8337 {
35f56f96 8338 Lisp_Object tail, frame;
a2889657 8339
5f5c8ee5
GM
8340 /* Clear the face cache eventually. */
8341 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 8342 {
5f5c8ee5 8343 clear_face_cache (0);
463f6b91
RS
8344 clear_face_cache_count = 0;
8345 }
31b24551 8346
5f5c8ee5
GM
8347 /* Recompute # windows showing selected buffer. This will be
8348 incremented each time such a window is displayed. */
a2889657
JB
8349 buffer_shared = 0;
8350
35f56f96 8351 FOR_EACH_FRAME (tail, frame)
30c566e4 8352 {
5f5c8ee5 8353 struct frame *f = XFRAME (frame);
bd9d0f3f 8354
886bd6f2 8355 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 8356 {
5f5c8ee5
GM
8357 /* Mark all the scroll bars to be removed; we'll redeem
8358 the ones we want when we redisplay their windows. */
9769686d 8359 if (condemn_scroll_bars_hook)
504454e8 8360 condemn_scroll_bars_hook (f);
30c566e4 8361
f21ef775 8362 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 8363 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 8364
5f5c8ee5
GM
8365 /* Any scroll bars which redisplay_windows should have
8366 nuked should now go away. */
9769686d 8367 if (judge_scroll_bars_hook)
504454e8 8368 judge_scroll_bars_hook (f);
bd9d0f3f
GM
8369
8370 /* If fonts changed, display again. */
8371 if (fonts_changed_p)
8372 goto retry;
8373
8374 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8375 {
8376 /* See if we have to hscroll. */
8377 if (hscroll_windows (f->root_window))
8378 goto retry;
8379
8380 /* Prevent various kinds of signals during display
8381 update. stdio is not robust about handling
8382 signals, which can cause an apparent I/O
8383 error. */
8384 if (interrupt_input)
8385 unrequest_sigio ();
8386 stop_polling ();
8387
8388 /* Update the display. */
8389 set_window_update_flags (XWINDOW (f->root_window), 1);
8390 pause |= update_frame (f, 0, 0);
8391 if (pause)
8392 break;
8393
8394 mark_window_display_accurate (f->root_window, 1);
8395 if (frame_up_to_date_hook)
8396 frame_up_to_date_hook (f);
8397 }
9769686d 8398 }
30c566e4 8399 }
a2889657 8400 }
bd9d0f3f
GM
8401 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8402 {
8403 Lisp_Object mini_window;
8404 struct frame *mini_frame;
5f5c8ee5 8405
bd9d0f3f 8406 redisplay_window (selected_window, 1);
5f5c8ee5 8407
bd9d0f3f
GM
8408 /* Compare desired and current matrices, perform output. */
8409 update:
5f5c8ee5 8410
bd9d0f3f
GM
8411 /* If fonts changed, display again. */
8412 if (fonts_changed_p)
92a90e89 8413 goto retry;
a2889657 8414
bd9d0f3f
GM
8415 /* Prevent various kinds of signals during display update.
8416 stdio is not robust about handling signals,
8417 which can cause an apparent I/O error. */
8418 if (interrupt_input)
8419 unrequest_sigio ();
8420 stop_polling ();
1af9f229 8421
bd9d0f3f 8422 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 8423 {
92a90e89
GM
8424 if (hscroll_windows (selected_window))
8425 goto retry;
8426
5f5c8ee5 8427 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 8428 pause = update_frame (sf, 0, 0);
5f5c8ee5 8429 }
d724d989 8430
8de2d90b 8431 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
8432 function. If the echo area is on another frame, that may
8433 have put text on a frame other than the selected one, so the
8434 above call to update_frame would not have caught it. Catch
8de2d90b 8435 it here. */
bd9d0f3f
GM
8436 mini_window = FRAME_MINIBUF_WINDOW (sf);
8437 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 8438
bd9d0f3f
GM
8439 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8440 {
8441 XWINDOW (mini_window)->must_be_updated_p = 1;
8442 pause |= update_frame (mini_frame, 0, 0);
8443 if (!pause && hscroll_windows (mini_window))
8444 goto retry;
8445 }
6e8290aa 8446 }
a2889657 8447
5f5c8ee5
GM
8448 /* If display was paused because of pending input, make sure we do a
8449 thorough update the next time. */
a2889657
JB
8450 if (pause)
8451 {
5f5c8ee5
GM
8452 /* Prevent the optimization at the beginning of
8453 redisplay_internal that tries a single-line update of the
8454 line containing the cursor in the selected window. */
8455 CHARPOS (this_line_start_pos) = 0;
8456
8457 /* Let the overlay arrow be updated the next time. */
265a9e55 8458 if (!NILP (last_arrow_position))
a2889657
JB
8459 {
8460 last_arrow_position = Qt;
8461 last_arrow_string = Qt;
8462 }
5f5c8ee5
GM
8463
8464 /* If we pause after scrolling, some rows in the current
8465 matrices of some windows are not valid. */
8466 if (!WINDOW_FULL_WIDTH_P (w)
8467 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
8468 update_mode_lines = 1;
8469 }
8470
5f5c8ee5
GM
8471 /* Now text on frame agrees with windows, so put info into the
8472 windows for partial redisplay to follow. */
a2889657
JB
8473 if (!pause)
8474 {
8475 register struct buffer *b = XBUFFER (w->buffer);
8476
9142dd5b
GM
8477 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8478 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8479 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8480 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 8481
5f5c8ee5 8482 if (consider_all_windows_p)
886bd6f2 8483 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
a2889657
JB
8484 else
8485 {
5f5c8ee5
GM
8486 XSETFASTINT (w->last_point, BUF_PT (b));
8487 w->last_cursor = w->cursor;
8488 w->last_cursor_off_p = w->cursor_off_p;
8489
28995e67 8490 b->clip_changed = 0;
9142dd5b 8491 b->prevent_redisplay_optimizations_p = 0;
a2889657 8492 w->update_mode_line = Qnil;
c2213350 8493 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 8494 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
8495 w->last_had_star
8496 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8497 ? Qt : Qnil);
3ee4159a
RS
8498
8499 /* Record if we are showing a region, so can make sure to
8500 update it fully at next redisplay. */
8501 w->region_showing = (!NILP (Vtransient_mark_mode)
97a36635
GM
8502 && (EQ (selected_window,
8503 current_buffer->last_selected_window)
293a54ce 8504 || highlight_nonselected_windows)
3ee4159a
RS
8505 && !NILP (XBUFFER (w->buffer)->mark_active)
8506 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8507 : Qnil);
8508
d2f84654 8509 w->window_end_valid = w->buffer;
d45de95b 8510 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 8511 last_arrow_string = Voverlay_arrow_string;
efc63ef0 8512 if (frame_up_to_date_hook != 0)
886bd6f2 8513 (*frame_up_to_date_hook) (sf);
9142dd5b
GM
8514
8515 w->current_matrix->buffer = b;
8516 w->current_matrix->begv = BUF_BEGV (b);
8517 w->current_matrix->zv = BUF_ZV (b);
a2889657 8518 }
15e26c76 8519
a2889657
JB
8520 update_mode_lines = 0;
8521 windows_or_buffers_changed = 0;
8522 }
8523
5f5c8ee5
GM
8524 /* Start SIGIO interrupts coming again. Having them off during the
8525 code above makes it less likely one will discard output, but not
8526 impossible, since there might be stuff in the system buffer here.
a2889657 8527 But it is much hairier to try to do anything about that. */
a2889657
JB
8528 if (interrupt_input)
8529 request_sigio ();
8530 start_polling ();
8531
5f5c8ee5
GM
8532 /* If a frame has become visible which was not before, redisplay
8533 again, so that we display it. Expose events for such a frame
8534 (which it gets when becoming visible) don't call the parts of
8535 redisplay constructing glyphs, so simply exposing a frame won't
8536 display anything in this case. So, we have to display these
8537 frames here explicitly. */
11c52c4f
RS
8538 if (!pause)
8539 {
8540 Lisp_Object tail, frame;
8541 int new_count = 0;
8542
8543 FOR_EACH_FRAME (tail, frame)
8544 {
8545 int this_is_visible = 0;
8e83f802
RS
8546
8547 if (XFRAME (frame)->visible)
8548 this_is_visible = 1;
8549 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8550 if (XFRAME (frame)->visible)
8551 this_is_visible = 1;
11c52c4f
RS
8552
8553 if (this_is_visible)
8554 new_count++;
8555 }
8556
89819bdd 8557 if (new_count != number_of_visible_frames)
11c52c4f
RS
8558 windows_or_buffers_changed++;
8559 }
8560
44fa5b1e 8561 /* Change frame size now if a change is pending. */
c6e89d6c 8562 do_pending_window_change (1);
d8e242fd 8563
8b32d885
RS
8564 /* If we just did a pending size change, or have additional
8565 visible frames, redisplay again. */
3c8c72e0 8566 if (windows_or_buffers_changed && !pause)
8b32d885 8567 goto retry;
5f5c8ee5
GM
8568
8569 end_of_redisplay:;
c6e89d6c 8570
28514cd9 8571 unbind_to (count, Qnil);
a2889657
JB
8572}
8573
5f5c8ee5
GM
8574
8575/* Redisplay, but leave alone any recent echo area message unless
8576 another message has been requested in its place.
a2889657
JB
8577
8578 This is useful in situations where you need to redisplay but no
8579 user action has occurred, making it inappropriate for the message
8580 area to be cleared. See tracking_off and
9bf76936
GM
8581 wait_reading_process_input for examples of these situations.
8582
8583 FROM_WHERE is an integer saying from where this function was
8584 called. This is useful for debugging. */
a2889657 8585
8991bb31 8586void
9bf76936
GM
8587redisplay_preserve_echo_area (from_where)
8588 int from_where;
a2889657 8589{
9bf76936
GM
8590 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8591
c6e89d6c 8592 if (!NILP (echo_area_buffer[1]))
a2889657 8593 {
c6e89d6c
GM
8594 /* We have a previously displayed message, but no current
8595 message. Redisplay the previous message. */
8596 display_last_displayed_message_p = 1;
e9874cee 8597 redisplay_internal (1);
c6e89d6c 8598 display_last_displayed_message_p = 0;
a2889657
JB
8599 }
8600 else
e9874cee 8601 redisplay_internal (1);
a2889657
JB
8602}
8603
5f5c8ee5 8604
28514cd9
GM
8605/* Function registered with record_unwind_protect in
8606 redisplay_internal. Clears the flag indicating that a redisplay is
8607 in progress. */
8608
8609static Lisp_Object
8610unwind_redisplay (old_redisplaying_p)
8611 Lisp_Object old_redisplaying_p;
8612{
8613 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 8614 return Qnil;
28514cd9
GM
8615}
8616
8617
5f5c8ee5
GM
8618/* Mark the display of windows in the window tree rooted at WINDOW as
8619 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8620 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8621 the next time redisplay_internal is called. */
8622
a2889657 8623void
5f5c8ee5 8624mark_window_display_accurate (window, accurate_p)
a2889657 8625 Lisp_Object window;
5f5c8ee5 8626 int accurate_p;
a2889657 8627{
5f5c8ee5
GM
8628 struct window *w;
8629
8630 for (; !NILP (window); window = w->next)
a2889657
JB
8631 {
8632 w = XWINDOW (window);
8633
5f5c8ee5 8634 if (BUFFERP (w->buffer))
bd66d1ba 8635 {
5f5c8ee5
GM
8636 struct buffer *b = XBUFFER (w->buffer);
8637
c2213350 8638 XSETFASTINT (w->last_modified,
5f5c8ee5 8639 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 8640 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
8641 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8642 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8643 ? Qt : Qnil);
bd66d1ba 8644
5f5c8ee5
GM
8645#if 0 /* I don't think this is necessary because display_line does it.
8646 Let's check it. */
bd66d1ba
RS
8647 /* Record if we are showing a region, so can make sure to
8648 update it fully at next redisplay. */
5f5c8ee5
GM
8649 w->region_showing
8650 = (!NILP (Vtransient_mark_mode)
8651 && (w == XWINDOW (current_buffer->last_selected_window)
8652 || highlight_nonselected_windows)
8653 && (!NILP (b->mark_active)
8654 ? Fmarker_position (b->mark)
8655 : Qnil));
8656#endif
8657
8658 if (accurate_p)
8659 {
8660 b->clip_changed = 0;
9142dd5b
GM
8661 b->prevent_redisplay_optimizations_p = 0;
8662 w->current_matrix->buffer = b;
8663 w->current_matrix->begv = BUF_BEGV (b);
8664 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
8665 w->last_cursor = w->cursor;
8666 w->last_cursor_off_p = w->cursor_off_p;
8667 if (w == XWINDOW (selected_window))
6fc556fd 8668 w->last_point = make_number (BUF_PT (b));
5f5c8ee5 8669 else
6fc556fd 8670 w->last_point = make_number (XMARKER (w->pointm)->charpos);
5f5c8ee5 8671 }
bd66d1ba
RS
8672 }
8673
d2f84654 8674 w->window_end_valid = w->buffer;
a2889657
JB
8675 w->update_mode_line = Qnil;
8676
265a9e55 8677 if (!NILP (w->vchild))
5f5c8ee5 8678 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 8679 if (!NILP (w->hchild))
5f5c8ee5 8680 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
8681 }
8682
5f5c8ee5 8683 if (accurate_p)
a2889657 8684 {
d45de95b 8685 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
8686 last_arrow_string = Voverlay_arrow_string;
8687 }
8688 else
8689 {
5f5c8ee5
GM
8690 /* Force a thorough redisplay the next time by setting
8691 last_arrow_position and last_arrow_string to t, which is
8692 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
8693 last_arrow_position = Qt;
8694 last_arrow_string = Qt;
8695 }
8696}
5f5c8ee5
GM
8697
8698
8699/* Return value in display table DP (Lisp_Char_Table *) for character
8700 C. Since a display table doesn't have any parent, we don't have to
8701 follow parent. Do not call this function directly but use the
8702 macro DISP_CHAR_VECTOR. */
8703
8704Lisp_Object
8705disp_char_vector (dp, c)
8706 struct Lisp_Char_Table *dp;
8707 int c;
8708{
8709 int code[4], i;
8710 Lisp_Object val;
8711
8712 if (SINGLE_BYTE_CHAR_P (c))
8713 return (dp->contents[c]);
8714
c5924f47 8715 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
8716 if (code[1] < 32)
8717 code[1] = -1;
8718 else if (code[2] < 32)
8719 code[2] = -1;
5f5c8ee5
GM
8720
8721 /* Here, the possible range of code[0] (== charset ID) is
8722 128..max_charset. Since the top level char table contains data
8723 for multibyte characters after 256th element, we must increment
8724 code[0] by 128 to get a correct index. */
8725 code[0] += 128;
8726 code[3] = -1; /* anchor */
8727
8728 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8729 {
8730 val = dp->contents[code[i]];
8731 if (!SUB_CHAR_TABLE_P (val))
8732 return (NILP (val) ? dp->defalt : val);
8733 }
8734
8735 /* Here, val is a sub char table. We return the default value of
8736 it. */
8737 return (dp->defalt);
8738}
8739
8740
a2889657 8741\f
5f5c8ee5
GM
8742/***********************************************************************
8743 Window Redisplay
8744 ***********************************************************************/
a2725ab2 8745
5f5c8ee5 8746/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
8747
8748static void
5f5c8ee5
GM
8749redisplay_windows (window)
8750 Lisp_Object window;
90adcf20 8751{
5f5c8ee5
GM
8752 while (!NILP (window))
8753 {
8754 struct window *w = XWINDOW (window);
8755
8756 if (!NILP (w->hchild))
8757 redisplay_windows (w->hchild);
8758 else if (!NILP (w->vchild))
8759 redisplay_windows (w->vchild);
8760 else
8761 redisplay_window (window, 0);
a2725ab2 8762
5f5c8ee5
GM
8763 window = w->next;
8764 }
8765}
8766
8767
8768/* Set cursor position of W. PT is assumed to be displayed in ROW.
8769 DELTA is the number of bytes by which positions recorded in ROW
8770 differ from current buffer positions. */
8771
8772void
8773set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8774 struct window *w;
8775 struct glyph_row *row;
8776 struct glyph_matrix *matrix;
8777 int delta, delta_bytes, dy, dvpos;
8778{
8779 struct glyph *glyph = row->glyphs[TEXT_AREA];
8780 struct glyph *end = glyph + row->used[TEXT_AREA];
8781 int x = row->x;
8782 int pt_old = PT - delta;
8783
8784 /* Skip over glyphs not having an object at the start of the row.
8785 These are special glyphs like truncation marks on terminal
8786 frames. */
8787 if (row->displays_text_p)
8788 while (glyph < end
6fc556fd 8789 && INTEGERP (glyph->object)
5f5c8ee5
GM
8790 && glyph->charpos < 0)
8791 {
8792 x += glyph->pixel_width;
8793 ++glyph;
8794 }
8795
8796 while (glyph < end
6fc556fd 8797 && !INTEGERP (glyph->object)
5f5c8ee5
GM
8798 && (!BUFFERP (glyph->object)
8799 || glyph->charpos < pt_old))
8800 {
8801 x += glyph->pixel_width;
8802 ++glyph;
8803 }
8804
8805 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8806 w->cursor.x = x;
8807 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8808 w->cursor.y = row->y + dy;
8809
8810 if (w == XWINDOW (selected_window))
8811 {
8812 if (!row->continued_p
8813 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8814 && row->x == 0)
8815 {
8816 this_line_buffer = XBUFFER (w->buffer);
8817
8818 CHARPOS (this_line_start_pos)
8819 = MATRIX_ROW_START_CHARPOS (row) + delta;
8820 BYTEPOS (this_line_start_pos)
8821 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8822
8823 CHARPOS (this_line_end_pos)
8824 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8825 BYTEPOS (this_line_end_pos)
8826 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8827
8828 this_line_y = w->cursor.y;
8829 this_line_pixel_height = row->height;
8830 this_line_vpos = w->cursor.vpos;
8831 this_line_start_x = row->x;
8832 }
8833 else
8834 CHARPOS (this_line_start_pos) = 0;
8835 }
8836}
8837
8838
8839/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
8840 start STARTP. Sets the window start of WINDOW to that position.
8841
8842 We assume that the window's buffer is really current. */
5f5c8ee5
GM
8843
8844static INLINE struct text_pos
8845run_window_scroll_functions (window, startp)
8846 Lisp_Object window;
8847 struct text_pos startp;
8848{
8849 struct window *w = XWINDOW (window);
8850 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
8851
8852 if (current_buffer != XBUFFER (w->buffer))
8853 abort ();
8854
5f5c8ee5
GM
8855 if (!NILP (Vwindow_scroll_functions))
8856 {
8857 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8858 make_number (CHARPOS (startp)));
8859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
8860 /* In case the hook functions switch buffers. */
8861 if (current_buffer != XBUFFER (w->buffer))
8862 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8863 }
90adcf20 8864
5f5c8ee5
GM
8865 return startp;
8866}
8867
8868
8869/* Modify the desired matrix of window W and W->vscroll so that the
8870 line containing the cursor is fully visible. */
8871
8872static void
8873make_cursor_line_fully_visible (w)
8874 struct window *w;
8875{
8876 struct glyph_matrix *matrix;
8877 struct glyph_row *row;
478d746b 8878 int window_height;
5f5c8ee5
GM
8879
8880 /* It's not always possible to find the cursor, e.g, when a window
8881 is full of overlay strings. Don't do anything in that case. */
8882 if (w->cursor.vpos < 0)
8883 return;
8884
8885 matrix = w->desired_matrix;
8886 row = MATRIX_ROW (matrix, w->cursor.vpos);
8887
b28cb6ed
GM
8888 /* If the cursor row is not partially visible, there's nothing
8889 to do. */
8890 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8891 return;
8892
8893 /* If the row the cursor is in is taller than the window's height,
8894 it's not clear what to do, so do nothing. */
8895 window_height = window_box_height (w);
8896 if (row->height >= window_height)
8897 return;
8898
8899 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
8900 {
8901 int dy = row->height - row->visible_height;
8902 w->vscroll = 0;
8903 w->cursor.y += dy;
8904 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8905 }
b28cb6ed 8906 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
8907 {
8908 int dy = - (row->height - row->visible_height);
8909 w->vscroll = dy;
8910 w->cursor.y += dy;
8911 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8912 }
b28cb6ed 8913
5f5c8ee5
GM
8914 /* When we change the cursor y-position of the selected window,
8915 change this_line_y as well so that the display optimization for
8916 the cursor line of the selected window in redisplay_internal uses
8917 the correct y-position. */
8918 if (w == XWINDOW (selected_window))
8919 this_line_y = w->cursor.y;
8920}
8921
8922
8923/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8924 non-zero means only WINDOW is redisplayed in redisplay_internal.
8925 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8926 in redisplay_window to bring a partially visible line into view in
8927 the case that only the cursor has moved.
8928
8929 Value is
8930
8931 1 if scrolling succeeded
8932
8933 0 if scrolling didn't find point.
8934
8935 -1 if new fonts have been loaded so that we must interrupt
8936 redisplay, adjust glyph matrices, and try again. */
8937
8938static int
8939try_scrolling (window, just_this_one_p, scroll_conservatively,
8940 scroll_step, temp_scroll_step)
8941 Lisp_Object window;
8942 int just_this_one_p;
8943 int scroll_conservatively, scroll_step;
8944 int temp_scroll_step;
8945{
8946 struct window *w = XWINDOW (window);
8947 struct frame *f = XFRAME (w->frame);
8948 struct text_pos scroll_margin_pos;
8949 struct text_pos pos;
8950 struct text_pos startp;
8951 struct it it;
8952 Lisp_Object window_end;
8953 int this_scroll_margin;
8954 int dy = 0;
8955 int scroll_max;
b8a63ccb 8956 int rc;
5f5c8ee5
GM
8957 int amount_to_scroll = 0;
8958 Lisp_Object aggressive;
8959 int height;
8960
8961#if GLYPH_DEBUG
8962 debug_method_add (w, "try_scrolling");
78614721 8963#endif
5f5c8ee5
GM
8964
8965 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8966
8967 /* Compute scroll margin height in pixels. We scroll when point is
8968 within this distance from the top or bottom of the window. */
8969 if (scroll_margin > 0)
90adcf20 8970 {
5f5c8ee5
GM
8971 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8972 this_scroll_margin *= CANON_Y_UNIT (f);
8973 }
8974 else
8975 this_scroll_margin = 0;
8976
8977 /* Compute how much we should try to scroll maximally to bring point
8978 into view. */
0894e696
SM
8979 if (scroll_step || scroll_conservatively || temp_scroll_step)
8980 scroll_max = max (scroll_step,
8981 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
8982 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8983 || NUMBERP (current_buffer->scroll_up_aggressively))
8984 /* We're trying to scroll because of aggressive scrolling
8985 but no scroll_step is set. Choose an arbitrary one. Maybe
8986 there should be a variable for this. */
8987 scroll_max = 10;
8988 else
8989 scroll_max = 0;
8990 scroll_max *= CANON_Y_UNIT (f);
8991
8992 /* Decide whether we have to scroll down. Start at the window end
8993 and move this_scroll_margin up to find the position of the scroll
8994 margin. */
8995 window_end = Fwindow_end (window, Qt);
8996 CHARPOS (scroll_margin_pos) = XINT (window_end);
8997 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8998 if (this_scroll_margin)
8999 {
9000 start_display (&it, w, scroll_margin_pos);
9001 move_it_vertically (&it, - this_scroll_margin);
9002 scroll_margin_pos = it.current.pos;
9003 }
9004
9005 if (PT >= CHARPOS (scroll_margin_pos))
9006 {
9007 int y0;
9008
9009 /* Point is in the scroll margin at the bottom of the window, or
9010 below. Compute a new window start that makes point visible. */
47589c8c 9011
5f5c8ee5
GM
9012 /* Compute the distance from the scroll margin to PT.
9013 Give up if the distance is greater than scroll_max. */
9014 start_display (&it, w, scroll_margin_pos);
9015 y0 = it.current_y;
9016 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9017 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
e89aaabd 9018
82e274d1
GM
9019 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9020 end, which is one line below the window. The iterator's
9021 current_y will be same as y0 in that case, but we have to
9022 scroll a line to make PT visible. That's the reason why 1 is
9023 added below. */
9024 dy = 1 + it.current_y - y0;
47589c8c 9025
5f5c8ee5
GM
9026 if (dy > scroll_max)
9027 return 0;
9028
9029 /* Move the window start down. If scrolling conservatively,
9030 move it just enough down to make point visible. If
9031 scroll_step is set, move it down by scroll_step. */
9032 start_display (&it, w, startp);
9033
9034 if (scroll_conservatively)
e89aaabd
GM
9035 amount_to_scroll
9036 = max (max (dy, CANON_Y_UNIT (f)),
9037 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9038 else if (scroll_step || temp_scroll_step)
9039 amount_to_scroll = scroll_max;
9040 else
90adcf20 9041 {
5f5c8ee5
GM
9042 aggressive = current_buffer->scroll_down_aggressively;
9043 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9044 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9045 if (NUMBERP (aggressive))
9046 amount_to_scroll = XFLOATINT (aggressive) * height;
9047 }
a2725ab2 9048
5f5c8ee5
GM
9049 if (amount_to_scroll <= 0)
9050 return 0;
a2725ab2 9051
5f5c8ee5
GM
9052 move_it_vertically (&it, amount_to_scroll);
9053 startp = it.current.pos;
9054 }
9055 else
9056 {
9057 /* See if point is inside the scroll margin at the top of the
9058 window. */
9059 scroll_margin_pos = startp;
9060 if (this_scroll_margin)
9061 {
9062 start_display (&it, w, startp);
9063 move_it_vertically (&it, this_scroll_margin);
9064 scroll_margin_pos = it.current.pos;
9065 }
9066
9067 if (PT < CHARPOS (scroll_margin_pos))
9068 {
9069 /* Point is in the scroll margin at the top of the window or
9070 above what is displayed in the window. */
9071 int y0;
9072
9073 /* Compute the vertical distance from PT to the scroll
9074 margin position. Give up if distance is greater than
9075 scroll_max. */
9076 SET_TEXT_POS (pos, PT, PT_BYTE);
9077 start_display (&it, w, pos);
9078 y0 = it.current_y;
9079 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9080 it.last_visible_y, -1,
9081 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9082 dy = it.current_y - y0;
9083 if (dy > scroll_max)
9084 return 0;
9085
9086 /* Compute new window start. */
9087 start_display (&it, w, startp);
9088
9089 if (scroll_conservatively)
0894e696
SM
9090 amount_to_scroll =
9091 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9092 else if (scroll_step || temp_scroll_step)
9093 amount_to_scroll = scroll_max;
538f13d4 9094 else
5f5c8ee5
GM
9095 {
9096 aggressive = current_buffer->scroll_up_aggressively;
9097 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9098 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9099 if (NUMBERP (aggressive))
9100 amount_to_scroll = XFLOATINT (aggressive) * height;
9101 }
a2725ab2 9102
5f5c8ee5
GM
9103 if (amount_to_scroll <= 0)
9104 return 0;
9105
9106 move_it_vertically (&it, - amount_to_scroll);
9107 startp = it.current.pos;
90adcf20
RS
9108 }
9109 }
a2889657 9110
5f5c8ee5
GM
9111 /* Run window scroll functions. */
9112 startp = run_window_scroll_functions (window, startp);
90adcf20 9113
5f5c8ee5
GM
9114 /* Display the window. Give up if new fonts are loaded, or if point
9115 doesn't appear. */
9116 if (!try_window (window, startp))
9117 rc = -1;
9118 else if (w->cursor.vpos < 0)
9119 {
9120 clear_glyph_matrix (w->desired_matrix);
9121 rc = 0;
9122 }
9123 else
9124 {
9125 /* Maybe forget recorded base line for line number display. */
9126 if (!just_this_one_p
9127 || current_buffer->clip_changed
9142dd5b 9128 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
9129 w->base_line_number = Qnil;
9130
9131 /* If cursor ends up on a partially visible line, shift display
9132 lines up or down. */
9133 make_cursor_line_fully_visible (w);
9134 rc = 1;
9135 }
9136
9137 return rc;
a2889657
JB
9138}
9139
5f5c8ee5
GM
9140
9141/* Compute a suitable window start for window W if display of W starts
9142 on a continuation line. Value is non-zero if a new window start
9143 was computed.
9144
9145 The new window start will be computed, based on W's width, starting
9146 from the start of the continued line. It is the start of the
9147 screen line with the minimum distance from the old start W->start. */
9148
9149static int
9150compute_window_start_on_continuation_line (w)
9151 struct window *w;
1f1ff51d 9152{
5f5c8ee5
GM
9153 struct text_pos pos, start_pos;
9154 int window_start_changed_p = 0;
1f1ff51d 9155
5f5c8ee5 9156 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 9157
5f5c8ee5
GM
9158 /* If window start is on a continuation line... Window start may be
9159 < BEGV in case there's invisible text at the start of the
9160 buffer (M-x rmail, for example). */
9161 if (CHARPOS (start_pos) > BEGV
9162 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 9163 {
5f5c8ee5
GM
9164 struct it it;
9165 struct glyph_row *row;
f3751a65
GM
9166
9167 /* Handle the case that the window start is out of range. */
9168 if (CHARPOS (start_pos) < BEGV)
9169 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9170 else if (CHARPOS (start_pos) > ZV)
9171 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
9172
9173 /* Find the start of the continued line. This should be fast
9174 because scan_buffer is fast (newline cache). */
045dee35 9175 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
9176 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9177 row, DEFAULT_FACE_ID);
9178 reseat_at_previous_visible_line_start (&it);
9179
9180 /* If the line start is "too far" away from the window start,
9181 say it takes too much time to compute a new window start. */
9182 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9183 < XFASTINT (w->height) * XFASTINT (w->width))
9184 {
9185 int min_distance, distance;
9186
9187 /* Move forward by display lines to find the new window
9188 start. If window width was enlarged, the new start can
9189 be expected to be > the old start. If window width was
9190 decreased, the new window start will be < the old start.
9191 So, we're looking for the display line start with the
9192 minimum distance from the old window start. */
9193 pos = it.current.pos;
9194 min_distance = INFINITY;
9195 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9196 distance < min_distance)
9197 {
9198 min_distance = distance;
9199 pos = it.current.pos;
9200 move_it_by_lines (&it, 1, 0);
9201 }
9202
9203 /* Set the window start there. */
9204 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9205 window_start_changed_p = 1;
9206 }
1f1ff51d 9207 }
5f5c8ee5
GM
9208
9209 return window_start_changed_p;
1f1ff51d
KH
9210}
9211
5f5c8ee5 9212
47589c8c
GM
9213/* Try cursor movement in case text has not changes in window WINDOW,
9214 with window start STARTP. Value is
9215
9216 1 if successful
9217
9218 0 if this method cannot be used
9219
9220 -1 if we know we have to scroll the display. *SCROLL_STEP is
9221 set to 1, under certain circumstances, if we want to scroll as
9222 if scroll-step were set to 1. See the code. */
9223
9224static int
9225try_cursor_movement (window, startp, scroll_step)
9226 Lisp_Object window;
9227 struct text_pos startp;
9228 int *scroll_step;
9229{
9230 struct window *w = XWINDOW (window);
9231 struct frame *f = XFRAME (w->frame);
9232 int rc = 0;
9233
9234 /* Handle case where text has not changed, only point, and it has
9235 not moved off the frame. */
9236 if (/* Point may be in this window. */
9237 PT >= CHARPOS (startp)
47589c8c
GM
9238 /* Selective display hasn't changed. */
9239 && !current_buffer->clip_changed
4db87380
GM
9240 /* Function force-mode-line-update is used to force a thorough
9241 redisplay. It sets either windows_or_buffers_changed or
9242 update_mode_lines. So don't take a shortcut here for these
9243 cases. */
9244 && !update_mode_lines
9245 && !windows_or_buffers_changed
47589c8c
GM
9246 /* Can't use this case if highlighting a region. When a
9247 region exists, cursor movement has to do more than just
9248 set the cursor. */
9249 && !(!NILP (Vtransient_mark_mode)
9250 && !NILP (current_buffer->mark_active))
9251 && NILP (w->region_showing)
9252 && NILP (Vshow_trailing_whitespace)
9253 /* Right after splitting windows, last_point may be nil. */
9254 && INTEGERP (w->last_point)
9255 /* This code is not used for mini-buffer for the sake of the case
9256 of redisplaying to replace an echo area message; since in
9257 that case the mini-buffer contents per se are usually
9258 unchanged. This code is of no real use in the mini-buffer
9259 since the handling of this_line_start_pos, etc., in redisplay
9260 handles the same cases. */
9261 && !EQ (window, minibuf_window)
9262 /* When splitting windows or for new windows, it happens that
9263 redisplay is called with a nil window_end_vpos or one being
9264 larger than the window. This should really be fixed in
9265 window.c. I don't have this on my list, now, so we do
9266 approximately the same as the old redisplay code. --gerd. */
9267 && INTEGERP (w->window_end_vpos)
9268 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9269 && (FRAME_WINDOW_P (f)
9270 || !MARKERP (Voverlay_arrow_position)
9271 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9272 {
9273 int this_scroll_margin;
9274 struct glyph_row *row;
9275
9276#if GLYPH_DEBUG
9277 debug_method_add (w, "cursor movement");
9278#endif
9279
9280 /* Scroll if point within this distance from the top or bottom
9281 of the window. This is a pixel value. */
9282 this_scroll_margin = max (0, scroll_margin);
9283 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9284 this_scroll_margin *= CANON_Y_UNIT (f);
9285
9286 /* Start with the row the cursor was displayed during the last
9287 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
9288 if (w->last_cursor.vpos < 0
9289 || w->last_cursor.vpos >= w->current_matrix->nrows)
47589c8c
GM
9290 rc = -1;
9291 else
9292 {
9293 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9294 if (row->mode_line_p)
9295 ++row;
9296 if (!row->enabled_p)
9297 rc = -1;
9298 }
9299
9300 if (rc == 0)
9301 {
9302 int scroll_p = 0;
68c5d1db
GM
9303 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9304
47589c8c
GM
9305 if (PT > XFASTINT (w->last_point))
9306 {
9307 /* Point has moved forward. */
47589c8c
GM
9308 while (MATRIX_ROW_END_CHARPOS (row) < PT
9309 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9310 {
9311 xassert (row->enabled_p);
9312 ++row;
9313 }
9314
9315 /* The end position of a row equals the start position
9316 of the next row. If PT is there, we would rather
cafafe0b
GM
9317 display it in the next line. */
9318 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9319 && MATRIX_ROW_END_CHARPOS (row) == PT
9320 && !cursor_row_p (w, row))
9321 ++row;
47589c8c
GM
9322
9323 /* If within the scroll margin, scroll. Note that
9324 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9325 the next line would be drawn, and that
9326 this_scroll_margin can be zero. */
9327 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9328 || PT > MATRIX_ROW_END_CHARPOS (row)
9329 /* Line is completely visible last line in window
9330 and PT is to be set in the next line. */
9331 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9332 && PT == MATRIX_ROW_END_CHARPOS (row)
9333 && !row->ends_at_zv_p
9334 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9335 scroll_p = 1;
9336 }
9337 else if (PT < XFASTINT (w->last_point))
9338 {
9339 /* Cursor has to be moved backward. Note that PT >=
9340 CHARPOS (startp) because of the outer
9341 if-statement. */
9342 while (!row->mode_line_p
9343 && (MATRIX_ROW_START_CHARPOS (row) > PT
9344 || (MATRIX_ROW_START_CHARPOS (row) == PT
9345 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9346 && (row->y > this_scroll_margin
9347 || CHARPOS (startp) == BEGV))
9348 {
9349 xassert (row->enabled_p);
9350 --row;
9351 }
9352
9353 /* Consider the following case: Window starts at BEGV,
9354 there is invisible, intangible text at BEGV, so that
9355 display starts at some point START > BEGV. It can
9356 happen that we are called with PT somewhere between
9357 BEGV and START. Try to handle that case. */
9358 if (row < w->current_matrix->rows
9359 || row->mode_line_p)
9360 {
9361 row = w->current_matrix->rows;
9362 if (row->mode_line_p)
9363 ++row;
9364 }
9365
9366 /* Due to newlines in overlay strings, we may have to
9367 skip forward over overlay strings. */
68c5d1db
GM
9368 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9369 && MATRIX_ROW_END_CHARPOS (row) == PT
9370 && !cursor_row_p (w, row))
47589c8c
GM
9371 ++row;
9372
9373 /* If within the scroll margin, scroll. */
9374 if (row->y < this_scroll_margin
9375 && CHARPOS (startp) != BEGV)
9376 scroll_p = 1;
9377 }
9378
9379 if (PT < MATRIX_ROW_START_CHARPOS (row)
9380 || PT > MATRIX_ROW_END_CHARPOS (row))
9381 {
9382 /* if PT is not in the glyph row, give up. */
9383 rc = -1;
9384 }
440fc135 9385 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 9386 {
8de4aaf8
GM
9387 if (PT == MATRIX_ROW_END_CHARPOS (row)
9388 && !row->ends_at_zv_p
9389 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
3f7e3031
GM
9390 rc = -1;
9391 else if (row->height > window_box_height (w))
440fc135 9392 {
8de4aaf8
GM
9393 /* If we end up in a partially visible line, let's
9394 make it fully visible, except when it's taller
9395 than the window, in which case we can't do much
9396 about it. */
440fc135
GM
9397 *scroll_step = 1;
9398 rc = -1;
9399 }
9400 else
9401 {
9402 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9403 try_window (window, startp);
9404 make_cursor_line_fully_visible (w);
9405 rc = 1;
9406 }
47589c8c
GM
9407 }
9408 else if (scroll_p)
9409 rc = -1;
9410 else
9411 {
9412 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9413 rc = 1;
9414 }
9415 }
9416 }
9417
9418 return rc;
9419}
9420
9421
5f5c8ee5
GM
9422/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9423 selected_window is redisplayed. */
90adcf20 9424
a2889657 9425static void
5f5c8ee5 9426redisplay_window (window, just_this_one_p)
a2889657 9427 Lisp_Object window;
5f5c8ee5 9428 int just_this_one_p;
a2889657 9429{
5f5c8ee5
GM
9430 struct window *w = XWINDOW (window);
9431 struct frame *f = XFRAME (w->frame);
9432 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 9433 struct buffer *old = current_buffer;
5f5c8ee5 9434 struct text_pos lpoint, opoint, startp;
e481f960 9435 int update_mode_line;
5f5c8ee5
GM
9436 int tem;
9437 struct it it;
9438 /* Record it now because it's overwritten. */
9439 int current_matrix_up_to_date_p = 0;
5f5c8ee5 9440 int temp_scroll_step = 0;
2d27dae2 9441 int count = BINDING_STACK_SIZE ();
47589c8c 9442 int rc;
a2889657 9443
5f5c8ee5
GM
9444 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9445 opoint = lpoint;
a2889657 9446
5f5c8ee5
GM
9447 /* W must be a leaf window here. */
9448 xassert (!NILP (w->buffer));
9449#if GLYPH_DEBUG
9450 *w->desired_matrix->method = 0;
9451#endif
2e54982e
RS
9452
9453 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
9454
9455 reconsider_clip_changes (w, buffer);
9456
5f5c8ee5
GM
9457 /* Has the mode line to be updated? */
9458 update_mode_line = (!NILP (w->update_mode_line)
9459 || update_mode_lines
9460 || buffer->clip_changed);
8de2d90b
JB
9461
9462 if (MINI_WINDOW_P (w))
9463 {
5f5c8ee5 9464 if (w == XWINDOW (echo_area_window)
c6e89d6c 9465 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
9466 {
9467 if (update_mode_line)
9468 /* We may have to update a tty frame's menu bar or a
e037b9ec 9469 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
9470 goto finish_menu_bars;
9471 else
9472 /* We've already displayed the echo area glyphs in this window. */
9473 goto finish_scroll_bars;
9474 }
73af359d 9475 else if (w != XWINDOW (minibuf_window))
8de2d90b 9476 {
5f5c8ee5
GM
9477 /* W is a mini-buffer window, but it's not the currently
9478 active one, so clear it. */
9479 int yb = window_text_bottom_y (w);
9480 struct glyph_row *row;
9481 int y;
9482
9483 for (y = 0, row = w->desired_matrix->rows;
9484 y < yb;
9485 y += row->height, ++row)
9486 blank_row (w, row, y);
88f22aff 9487 goto finish_scroll_bars;
8de2d90b
JB
9488 }
9489 }
a2889657 9490
5f5c8ee5
GM
9491 /* Otherwise set up data on this window; select its buffer and point
9492 value. */
6a93695f
GM
9493 /* Really select the buffer, for the sake of buffer-local
9494 variables. */
9495 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
9496 SET_TEXT_POS (opoint, PT, PT_BYTE);
9497
9498 current_matrix_up_to_date_p
9499 = (!NILP (w->window_end_valid)
9500 && !current_buffer->clip_changed
9501 && XFASTINT (w->last_modified) >= MODIFF
9502 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 9503
5f5c8ee5
GM
9504 /* When windows_or_buffers_changed is non-zero, we can't rely on
9505 the window end being valid, so set it to nil there. */
9506 if (windows_or_buffers_changed)
9507 {
9508 /* If window starts on a continuation line, maybe adjust the
9509 window start in case the window's width changed. */
9510 if (XMARKER (w->start)->buffer == current_buffer)
9511 compute_window_start_on_continuation_line (w);
9512
9513 w->window_end_valid = Qnil;
9514 }
12adba34 9515
5f5c8ee5
GM
9516 /* Some sanity checks. */
9517 CHECK_WINDOW_END (w);
9518 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 9519 abort ();
5f5c8ee5 9520 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 9521 abort ();
a2889657 9522
28995e67
RS
9523 /* If %c is in mode line, update it if needed. */
9524 if (!NILP (w->column_number_displayed)
9525 /* This alternative quickly identifies a common case
9526 where no change is needed. */
9527 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9528 && XFASTINT (w->last_modified) >= MODIFF
9529 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
9530 && XFASTINT (w->column_number_displayed) != current_column ())
9531 update_mode_line = 1;
9532
5f5c8ee5
GM
9533 /* Count number of windows showing the selected buffer. An indirect
9534 buffer counts as its base buffer. */
9535 if (!just_this_one_p)
42640f83
RS
9536 {
9537 struct buffer *current_base, *window_base;
9538 current_base = current_buffer;
9539 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9540 if (current_base->base_buffer)
9541 current_base = current_base->base_buffer;
9542 if (window_base->base_buffer)
9543 window_base = window_base->base_buffer;
9544 if (current_base == window_base)
9545 buffer_shared++;
9546 }
a2889657 9547
5f5c8ee5
GM
9548 /* Point refers normally to the selected window. For any other
9549 window, set up appropriate value. */
a2889657
JB
9550 if (!EQ (window, selected_window))
9551 {
12adba34
RS
9552 int new_pt = XMARKER (w->pointm)->charpos;
9553 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 9554 if (new_pt < BEGV)
a2889657 9555 {
f67a0f51 9556 new_pt = BEGV;
12adba34
RS
9557 new_pt_byte = BEGV_BYTE;
9558 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 9559 }
f67a0f51 9560 else if (new_pt > (ZV - 1))
a2889657 9561 {
f67a0f51 9562 new_pt = ZV;
12adba34
RS
9563 new_pt_byte = ZV_BYTE;
9564 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 9565 }
5f5c8ee5 9566
f67a0f51 9567 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 9568 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
9569 }
9570
f4faa47c 9571 /* If any of the character widths specified in the display table
5f5c8ee5
GM
9572 have changed, invalidate the width run cache. It's true that
9573 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
9574 redisplay goes (non-fatally) haywire when the display table is
9575 changed, so why should we worry about doing any better? */
9576 if (current_buffer->width_run_cache)
9577 {
f908610f 9578 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
9579
9580 if (! disptab_matches_widthtab (disptab,
9581 XVECTOR (current_buffer->width_table)))
9582 {
9583 invalidate_region_cache (current_buffer,
9584 current_buffer->width_run_cache,
9585 BEG, Z);
9586 recompute_width_table (current_buffer, disptab);
9587 }
9588 }
9589
a2889657 9590 /* If window-start is screwed up, choose a new one. */
a2889657
JB
9591 if (XMARKER (w->start)->buffer != current_buffer)
9592 goto recenter;
9593
5f5c8ee5 9594 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 9595
cf0df6ab
RS
9596 /* If someone specified a new starting point but did not insist,
9597 check whether it can be used. */
cfad01b4
GM
9598 if (!NILP (w->optional_new_start)
9599 && CHARPOS (startp) >= BEGV
9600 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
9601 {
9602 w->optional_new_start = Qnil;
5f5c8ee5
GM
9603 start_display (&it, w, startp);
9604 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9605 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9606 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
9607 w->force_start = Qt;
9608 }
9609
8de2d90b 9610 /* Handle case where place to start displaying has been specified,
aa6d10fa 9611 unless the specified location is outside the accessible range. */
9472f927
GM
9612 if (!NILP (w->force_start)
9613 || w->frozen_window_start_p)
a2889657 9614 {
e63574d7 9615 w->force_start = Qnil;
5f5c8ee5 9616 w->vscroll = 0;
b5174a51 9617 w->window_end_valid = Qnil;
5f5c8ee5
GM
9618
9619 /* Forget any recorded base line for line number display. */
9620 if (!current_matrix_up_to_date_p
9621 || current_buffer->clip_changed)
9622 w->base_line_number = Qnil;
9623
75c43375
RS
9624 /* Redisplay the mode line. Select the buffer properly for that.
9625 Also, run the hook window-scroll-functions
9626 because we have scrolled. */
e63574d7
RS
9627 /* Note, we do this after clearing force_start because
9628 if there's an error, it is better to forget about force_start
9629 than to get into an infinite loop calling the hook functions
9630 and having them get more errors. */
75c43375
RS
9631 if (!update_mode_line
9632 || ! NILP (Vwindow_scroll_functions))
e481f960 9633 {
e481f960
RS
9634 update_mode_line = 1;
9635 w->update_mode_line = Qt;
5f5c8ee5 9636 startp = run_window_scroll_functions (window, startp);
e481f960 9637 }
5f5c8ee5 9638
c2213350 9639 XSETFASTINT (w->last_modified, 0);
8850a573 9640 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
9641 if (CHARPOS (startp) < BEGV)
9642 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9643 else if (CHARPOS (startp) > ZV)
9644 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9645
9646 /* Redisplay, then check if cursor has been set during the
9647 redisplay. Give up if new fonts were loaded. */
9648 if (!try_window (window, startp))
9649 {
9650 w->force_start = Qt;
9651 clear_glyph_matrix (w->desired_matrix);
504454e8 9652 goto finish_scroll_bars;
5f5c8ee5
GM
9653 }
9654
9472f927 9655 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 9656 {
b28cb6ed
GM
9657 /* If point does not appear, try to move point so it does
9658 appear. The desired matrix has been built above, so we
9659 can use it here. */
9660 int window_height;
9661 struct glyph_row *row;
9662
9663 window_height = window_box_height (w) / 2;
9664 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9665 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
5f5c8ee5
GM
9666 ++row;
9667
9668 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9669 MATRIX_ROW_START_BYTEPOS (row));
9670
90adcf20 9671 if (w != XWINDOW (selected_window))
12adba34 9672 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
9673 else if (current_buffer == old)
9674 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9675
9676 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9677
9678 /* If we are highlighting the region, then we just changed
9679 the region, so redisplay to show it. */
df0b5ea1
RS
9680 if (!NILP (Vtransient_mark_mode)
9681 && !NILP (current_buffer->mark_active))
6f27fa9b 9682 {
5f5c8ee5
GM
9683 clear_glyph_matrix (w->desired_matrix);
9684 if (!try_window (window, startp))
504454e8 9685 goto finish_scroll_bars;
6f27fa9b 9686 }
a2889657 9687 }
5f5c8ee5
GM
9688
9689 make_cursor_line_fully_visible (w);
9690#if GLYPH_DEBUG
9691 debug_method_add (w, "forced window start");
9692#endif
a2889657
JB
9693 goto done;
9694 }
9695
5f5c8ee5
GM
9696 /* Handle case where text has not changed, only point, and it has
9697 not moved off the frame. */
9698 if (current_matrix_up_to_date_p
47589c8c
GM
9699 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9700 rc != 0))
a2889657 9701 {
47589c8c 9702 if (rc == -1)
5f5c8ee5 9703 goto try_to_scroll;
47589c8c
GM
9704 else
9705 goto done;
a2889657
JB
9706 }
9707 /* If current starting point was originally the beginning of a line
9708 but no longer is, find a new starting point. */
265a9e55 9709 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
9710 && !(CHARPOS (startp) <= BEGV
9711 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 9712 {
5f5c8ee5
GM
9713#if GLYPH_DEBUG
9714 debug_method_add (w, "recenter 1");
9715#endif
a2889657
JB
9716 goto recenter;
9717 }
5f5c8ee5
GM
9718
9719 /* Try scrolling with try_window_id. */
9142dd5b
GM
9720 else if (/* Windows and buffers haven't changed. */
9721 !windows_or_buffers_changed
5f5c8ee5
GM
9722 /* Window must be either use window-based redisplay or
9723 be full width. */
9724 && (FRAME_WINDOW_P (f)
c59c668a 9725 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
9726 && !MINI_WINDOW_P (w)
9727 /* Point is not known NOT to appear in window. */
9728 && PT >= CHARPOS (startp)
a2889657 9729 && XFASTINT (w->last_modified)
5f5c8ee5
GM
9730 /* Window is not hscrolled. */
9731 && XFASTINT (w->hscroll) == 0
9732 /* Selective display has not changed. */
9733 && !current_buffer->clip_changed
9734 /* Current matrix is up to date. */
9735 && !NILP (w->window_end_valid)
9736 /* Can't use this case if highlighting a region because
9737 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
9738 && !(!NILP (Vtransient_mark_mode)
9739 && !NILP (current_buffer->mark_active))
9740 && NILP (w->region_showing)
8f897821 9741 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 9742 /* Overlay arrow position and string not changed. */
d45de95b 9743 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 9744 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
9745 /* Value is > 0 if update has been done, it is -1 if we
9746 know that the same window start will not work. It is 0
9747 if unsuccessful for some other reason. */
9748 && (tem = try_window_id (w)) != 0)
a2889657 9749 {
5f5c8ee5 9750#if GLYPH_DEBUG
ef121659 9751 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
9752#endif
9753
9754 if (fonts_changed_p)
504454e8 9755 goto finish_scroll_bars;
a2889657
JB
9756 if (tem > 0)
9757 goto done;
ef121659 9758
5f5c8ee5
GM
9759 /* Otherwise try_window_id has returned -1 which means that we
9760 don't want the alternative below this comment to execute. */
a2889657 9761 }
5f5c8ee5
GM
9762 else if (CHARPOS (startp) >= BEGV
9763 && CHARPOS (startp) <= ZV
9764 && PT >= CHARPOS (startp)
9765 && (CHARPOS (startp) < ZV
e9874cee 9766 /* Avoid starting at end of buffer. */
5f5c8ee5 9767 || CHARPOS (startp) == BEGV
8850a573
RS
9768 || (XFASTINT (w->last_modified) >= MODIFF
9769 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 9770 {
5f5c8ee5
GM
9771#if GLYPH_DEBUG
9772 debug_method_add (w, "same window start");
9773#endif
9774
9775 /* Try to redisplay starting at same place as before.
9776 If point has not moved off frame, accept the results. */
9777 if (!current_matrix_up_to_date_p
9778 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
9779 because a window scroll function can have changed the
9780 buffer. */
5f5c8ee5
GM
9781 || !NILP (Vwindow_scroll_functions)
9782 || MINI_WINDOW_P (w)
9783 || !try_window_reusing_current_matrix (w))
9784 {
9785 IF_DEBUG (debug_method_add (w, "1"));
9786 try_window (window, startp);
9787 }
9788
9789 if (fonts_changed_p)
504454e8 9790 goto finish_scroll_bars;
5f5c8ee5
GM
9791
9792 if (w->cursor.vpos >= 0)
aa6d10fa 9793 {
5f5c8ee5
GM
9794 if (!just_this_one_p
9795 || current_buffer->clip_changed
9142dd5b 9796 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
9797 /* Forget any recorded base line for line number display. */
9798 w->base_line_number = Qnil;
5f5c8ee5
GM
9799
9800 make_cursor_line_fully_visible (w);
aa6d10fa
RS
9801 goto done;
9802 }
a2889657 9803 else
5f5c8ee5 9804 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
9805 }
9806
5f5c8ee5
GM
9807 try_to_scroll:
9808
c2213350 9809 XSETFASTINT (w->last_modified, 0);
8850a573 9810 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 9811
e481f960
RS
9812 /* Redisplay the mode line. Select the buffer properly for that. */
9813 if (!update_mode_line)
9814 {
e481f960
RS
9815 update_mode_line = 1;
9816 w->update_mode_line = Qt;
9817 }
a2889657 9818
5f5c8ee5
GM
9819 /* Try to scroll by specified few lines. */
9820 if ((scroll_conservatively
9821 || scroll_step
9822 || temp_scroll_step
9823 || NUMBERP (current_buffer->scroll_up_aggressively)
9824 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 9825 && !current_buffer->clip_changed
5f5c8ee5
GM
9826 && CHARPOS (startp) >= BEGV
9827 && CHARPOS (startp) <= ZV)
0789adb2 9828 {
5f5c8ee5
GM
9829 /* The function returns -1 if new fonts were loaded, 1 if
9830 successful, 0 if not successful. */
9831 int rc = try_scrolling (window, just_this_one_p,
9832 scroll_conservatively,
9833 scroll_step,
9834 temp_scroll_step);
9835 if (rc > 0)
9836 goto done;
9837 else if (rc < 0)
504454e8 9838 goto finish_scroll_bars;
5f5c8ee5 9839 }
f9c8af06 9840
5f5c8ee5 9841 /* Finally, just choose place to start which centers point */
5936754e 9842
5f5c8ee5 9843 recenter:
44173109 9844
5f5c8ee5
GM
9845#if GLYPH_DEBUG
9846 debug_method_add (w, "recenter");
9847#endif
0789adb2 9848
5f5c8ee5 9849 /* w->vscroll = 0; */
0789adb2 9850
5f5c8ee5
GM
9851 /* Forget any previously recorded base line for line number display. */
9852 if (!current_matrix_up_to_date_p
9853 || current_buffer->clip_changed)
9854 w->base_line_number = Qnil;
9855
9856 /* Move backward half the height of the window. */
9857 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9858 it.current_y = it.last_visible_y;
9859 move_it_vertically_backward (&it, it.last_visible_y / 2);
9860 xassert (IT_CHARPOS (it) >= BEGV);
9861
9862 /* The function move_it_vertically_backward may move over more
9863 than the specified y-distance. If it->w is small, e.g. a
9864 mini-buffer window, we may end up in front of the window's
9865 display area. Start displaying at the start of the line
9866 containing PT in this case. */
9867 if (it.current_y <= 0)
9868 {
9869 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9870 move_it_vertically (&it, 0);
9871 xassert (IT_CHARPOS (it) <= PT);
9872 it.current_y = 0;
0789adb2
RS
9873 }
9874
5f5c8ee5
GM
9875 it.current_x = it.hpos = 0;
9876
9877 /* Set startp here explicitly in case that helps avoid an infinite loop
9878 in case the window-scroll-functions functions get errors. */
9879 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9880
9881 /* Run scroll hooks. */
9882 startp = run_window_scroll_functions (window, it.current.pos);
9883
9884 /* Redisplay the window. */
9885 if (!current_matrix_up_to_date_p
9886 || windows_or_buffers_changed
9887 /* Don't use try_window_reusing_current_matrix in this case
9888 because it can have changed the buffer. */
9889 || !NILP (Vwindow_scroll_functions)
9890 || !just_this_one_p
9891 || MINI_WINDOW_P (w)
9892 || !try_window_reusing_current_matrix (w))
9893 try_window (window, startp);
9894
9895 /* If new fonts have been loaded (due to fontsets), give up. We
9896 have to start a new redisplay since we need to re-adjust glyph
9897 matrices. */
9898 if (fonts_changed_p)
504454e8 9899 goto finish_scroll_bars;
5f5c8ee5
GM
9900
9901 /* If cursor did not appear assume that the middle of the window is
9902 in the first line of the window. Do it again with the next line.
9903 (Imagine a window of height 100, displaying two lines of height
9904 60. Moving back 50 from it->last_visible_y will end in the first
9905 line.) */
9906 if (w->cursor.vpos < 0)
a2889657 9907 {
5f5c8ee5
GM
9908 if (!NILP (w->window_end_valid)
9909 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 9910 {
5f5c8ee5
GM
9911 clear_glyph_matrix (w->desired_matrix);
9912 move_it_by_lines (&it, 1, 0);
9913 try_window (window, it.current.pos);
a2889657 9914 }
5f5c8ee5 9915 else if (PT < IT_CHARPOS (it))
a2889657 9916 {
5f5c8ee5
GM
9917 clear_glyph_matrix (w->desired_matrix);
9918 move_it_by_lines (&it, -1, 0);
9919 try_window (window, it.current.pos);
9920 }
9921 else
9922 {
9923 /* Not much we can do about it. */
a2889657 9924 }
a2889657 9925 }
010494d0 9926
5f5c8ee5
GM
9927 /* Consider the following case: Window starts at BEGV, there is
9928 invisible, intangible text at BEGV, so that display starts at
9929 some point START > BEGV. It can happen that we are called with
9930 PT somewhere between BEGV and START. Try to handle that case. */
9931 if (w->cursor.vpos < 0)
835766b6 9932 {
5f5c8ee5
GM
9933 struct glyph_row *row = w->current_matrix->rows;
9934 if (row->mode_line_p)
9935 ++row;
9936 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 9937 }
5f5c8ee5
GM
9938
9939 make_cursor_line_fully_visible (w);
b5174a51 9940
74d481ac
GM
9941 done:
9942
5f5c8ee5
GM
9943 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9944 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9945 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9946 ? Qt : Qnil);
a2889657 9947
5f5c8ee5 9948 /* Display the mode line, if we must. */
e481f960 9949 if ((update_mode_line
aa6d10fa 9950 /* If window not full width, must redo its mode line
5f5c8ee5
GM
9951 if (a) the window to its side is being redone and
9952 (b) we do a frame-based redisplay. This is a consequence
9953 of how inverted lines are drawn in frame-based redisplay. */
9954 || (!just_this_one_p
9955 && !FRAME_WINDOW_P (f)
9956 && !WINDOW_FULL_WIDTH_P (w))
9957 /* Line number to display. */
155ef550 9958 || INTEGERP (w->base_line_pos)
5f5c8ee5 9959 /* Column number is displayed and different from the one displayed. */
155ef550
KH
9960 || (!NILP (w->column_number_displayed)
9961 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
9962 /* This means that the window has a mode line. */
9963 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 9964 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 9965 {
5da11938
GM
9966 Lisp_Object old_selected_frame;
9967
9968 old_selected_frame = selected_frame;
9969
5da11938 9970 XSETFRAME (selected_frame, f);
5f5c8ee5 9971 display_mode_lines (w);
5da11938 9972 selected_frame = old_selected_frame;
5f5c8ee5
GM
9973
9974 /* If mode line height has changed, arrange for a thorough
9975 immediate redisplay using the correct mode line height. */
9976 if (WINDOW_WANTS_MODELINE_P (w)
9977 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 9978 {
5f5c8ee5
GM
9979 fonts_changed_p = 1;
9980 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9981 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 9982 }
5f5c8ee5
GM
9983
9984 /* If top line height has changed, arrange for a thorough
9985 immediate redisplay using the correct mode line height. */
045dee35
GM
9986 if (WINDOW_WANTS_HEADER_LINE_P (w)
9987 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
9988 {
9989 fonts_changed_p = 1;
045dee35
GM
9990 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9991 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9992 }
9993
9994 if (fonts_changed_p)
504454e8 9995 goto finish_scroll_bars;
5ba50c51 9996 }
5f5c8ee5
GM
9997
9998 if (!line_number_displayed
9999 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
10000 {
10001 w->base_line_pos = Qnil;
10002 w->base_line_number = Qnil;
10003 }
a2889657 10004
5f5c8ee5
GM
10005 finish_menu_bars:
10006
7ce2c095 10007 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 10008 if (update_mode_line
5f5c8ee5
GM
10009 && EQ (FRAME_SELECTED_WINDOW (f), window))
10010 {
10011 int redisplay_menu_p = 0;
10012
10013 if (FRAME_WINDOW_P (f))
10014 {
1a578e9b 10015#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5 10016 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 10017#else
5f5c8ee5 10018 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 10019#endif
5f5c8ee5
GM
10020 }
10021 else
10022 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10023
10024 if (redisplay_menu_p)
10025 display_menu_bar (w);
10026
10027#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
10028 if (WINDOWP (f->tool_bar_window)
10029 && (FRAME_TOOL_BAR_LINES (f) > 0
10030 || auto_resize_tool_bars_p))
10031 redisplay_tool_bar (f);
5f5c8ee5
GM
10032#endif
10033 }
7ce2c095 10034
88f22aff 10035 finish_scroll_bars:
5f5c8ee5 10036
88f22aff 10037 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 10038 {
b1d1124b 10039 int start, end, whole;
30c566e4 10040
b1d1124b 10041 /* Calculate the start and end positions for the current window.
3505ea70
JB
10042 At some point, it would be nice to choose between scrollbars
10043 which reflect the whole buffer size, with special markers
10044 indicating narrowing, and scrollbars which reflect only the
10045 visible region.
10046
5f5c8ee5 10047 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 10048 if (!MINI_WINDOW_P (w)
5f5c8ee5 10049 || (w == XWINDOW (minibuf_window)
c6e89d6c 10050 && NILP (echo_area_buffer[0])))
b1d1124b 10051 {
8a9311d7 10052 whole = ZV - BEGV;
4d641a15 10053 start = marker_position (w->start) - BEGV;
b1d1124b
JB
10054 /* I don't think this is guaranteed to be right. For the
10055 moment, we'll pretend it is. */
5f5c8ee5 10056 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 10057
5f5c8ee5
GM
10058 if (end < start)
10059 end = start;
10060 if (whole < (end - start))
10061 whole = end - start;
b1d1124b
JB
10062 }
10063 else
10064 start = end = whole = 0;
30c566e4 10065
88f22aff 10066 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 10067 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 10068
5f5c8ee5
GM
10069 /* Note that we actually used the scroll bar attached to this
10070 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 10071 redeem_scroll_bar_hook (w);
30c566e4 10072 }
b1d1124b 10073
5f5c8ee5
GM
10074 /* Restore current_buffer and value of point in it. */
10075 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 10076 set_buffer_internal_1 (old);
5f5c8ee5 10077 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
10078
10079 unbind_to (count, Qnil);
a2889657 10080}
a2889657 10081
5f5c8ee5
GM
10082
10083/* Build the complete desired matrix of WINDOW with a window start
10084 buffer position POS. Value is non-zero if successful. It is zero
10085 if fonts were loaded during redisplay which makes re-adjusting
10086 glyph matrices necessary. */
10087
10088int
a2889657
JB
10089try_window (window, pos)
10090 Lisp_Object window;
5f5c8ee5
GM
10091 struct text_pos pos;
10092{
10093 struct window *w = XWINDOW (window);
10094 struct it it;
10095 struct glyph_row *last_text_row = NULL;
9cbab4ff 10096
5f5c8ee5
GM
10097 /* Make POS the new window start. */
10098 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 10099
5f5c8ee5
GM
10100 /* Mark cursor position as unknown. No overlay arrow seen. */
10101 w->cursor.vpos = -1;
a2889657 10102 overlay_arrow_seen = 0;
642eefc6 10103
5f5c8ee5
GM
10104 /* Initialize iterator and info to start at POS. */
10105 start_display (&it, w, pos);
a2889657 10106
5f5c8ee5
GM
10107 /* Display all lines of W. */
10108 while (it.current_y < it.last_visible_y)
10109 {
10110 if (display_line (&it))
10111 last_text_row = it.glyph_row - 1;
10112 if (fonts_changed_p)
10113 return 0;
10114 }
a2889657 10115
5f5c8ee5
GM
10116 /* If bottom moved off end of frame, change mode line percentage. */
10117 if (XFASTINT (w->window_end_pos) <= 0
10118 && Z != IT_CHARPOS (it))
a2889657
JB
10119 w->update_mode_line = Qt;
10120
5f5c8ee5
GM
10121 /* Set window_end_pos to the offset of the last character displayed
10122 on the window from the end of current_buffer. Set
10123 window_end_vpos to its row number. */
10124 if (last_text_row)
10125 {
10126 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10127 w->window_end_bytepos
10128 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10129 XSETFASTINT (w->window_end_pos,
10130 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10131 XSETFASTINT (w->window_end_vpos,
10132 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10133 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10134 ->displays_text_p);
10135 }
10136 else
10137 {
10138 w->window_end_bytepos = 0;
10139 XSETFASTINT (w->window_end_pos, 0);
10140 XSETFASTINT (w->window_end_vpos, 0);
10141 }
10142
a2889657
JB
10143 /* But that is not valid info until redisplay finishes. */
10144 w->window_end_valid = Qnil;
5f5c8ee5 10145 return 1;
a2889657 10146}
5f5c8ee5
GM
10147
10148
a2889657 10149\f
5f5c8ee5
GM
10150/************************************************************************
10151 Window redisplay reusing current matrix when buffer has not changed
10152 ************************************************************************/
10153
10154/* Try redisplay of window W showing an unchanged buffer with a
10155 different window start than the last time it was displayed by
10156 reusing its current matrix. Value is non-zero if successful.
10157 W->start is the new window start. */
a2889657
JB
10158
10159static int
5f5c8ee5
GM
10160try_window_reusing_current_matrix (w)
10161 struct window *w;
a2889657 10162{
5f5c8ee5
GM
10163 struct frame *f = XFRAME (w->frame);
10164 struct glyph_row *row, *bottom_row;
10165 struct it it;
10166 struct run run;
10167 struct text_pos start, new_start;
10168 int nrows_scrolled, i;
10169 struct glyph_row *last_text_row;
10170 struct glyph_row *last_reused_text_row;
10171 struct glyph_row *start_row;
10172 int start_vpos, min_y, max_y;
d18354b6
GM
10173
10174 if (/* This function doesn't handle terminal frames. */
10175 !FRAME_WINDOW_P (f)
10176 /* Don't try to reuse the display if windows have been split
10177 or such. */
10178 || windows_or_buffers_changed)
5f5c8ee5 10179 return 0;
a2889657 10180
5f5c8ee5
GM
10181 /* Can't do this if region may have changed. */
10182 if ((!NILP (Vtransient_mark_mode)
10183 && !NILP (current_buffer->mark_active))
8f897821
GM
10184 || !NILP (w->region_showing)
10185 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 10186 return 0;
a2889657 10187
5f5c8ee5 10188 /* If top-line visibility has changed, give up. */
045dee35
GM
10189 if (WINDOW_WANTS_HEADER_LINE_P (w)
10190 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
10191 return 0;
10192
10193 /* Give up if old or new display is scrolled vertically. We could
10194 make this function handle this, but right now it doesn't. */
10195 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10196 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10197 return 0;
10198
10199 /* The variable new_start now holds the new window start. The old
10200 start `start' can be determined from the current matrix. */
10201 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10202 start = start_row->start.pos;
10203 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 10204
5f5c8ee5
GM
10205 /* Clear the desired matrix for the display below. */
10206 clear_glyph_matrix (w->desired_matrix);
10207
10208 if (CHARPOS (new_start) <= CHARPOS (start))
10209 {
10210 int first_row_y;
10211
10212 IF_DEBUG (debug_method_add (w, "twu1"));
10213
10214 /* Display up to a row that can be reused. The variable
10215 last_text_row is set to the last row displayed that displays
b48f74cb
GM
10216 text. Note that it.vpos == 0 if or if not there is a
10217 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
10218 start_display (&it, w, new_start);
10219 first_row_y = it.current_y;
10220 w->cursor.vpos = -1;
10221 last_text_row = last_reused_text_row = NULL;
b48f74cb 10222
5f5c8ee5
GM
10223 while (it.current_y < it.last_visible_y
10224 && IT_CHARPOS (it) < CHARPOS (start)
10225 && !fonts_changed_p)
10226 if (display_line (&it))
10227 last_text_row = it.glyph_row - 1;
10228
10229 /* A value of current_y < last_visible_y means that we stopped
10230 at the previous window start, which in turn means that we
10231 have at least one reusable row. */
10232 if (it.current_y < it.last_visible_y)
a2889657 10233 {
b48f74cb 10234 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5
GM
10235 nrows_scrolled = it.vpos;
10236
10237 /* Find PT if not already found in the lines displayed. */
10238 if (w->cursor.vpos < 0)
a2889657 10239 {
5f5c8ee5
GM
10240 int dy = it.current_y - first_row_y;
10241
10242 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10243 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10244 {
10245 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10246 && PT < MATRIX_ROW_END_CHARPOS (row))
10247 {
10248 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10249 dy, nrows_scrolled);
10250 break;
10251 }
10252
10253 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10254 break;
10255
10256 ++row;
10257 }
10258
10259 /* Give up if point was not found. This shouldn't
10260 happen often; not more often than with try_window
10261 itself. */
10262 if (w->cursor.vpos < 0)
10263 {
10264 clear_glyph_matrix (w->desired_matrix);
10265 return 0;
10266 }
a2889657 10267 }
5f5c8ee5
GM
10268
10269 /* Scroll the display. Do it before the current matrix is
10270 changed. The problem here is that update has not yet
10271 run, i.e. part of the current matrix is not up to date.
10272 scroll_run_hook will clear the cursor, and use the
10273 current matrix to get the height of the row the cursor is
10274 in. */
10275 run.current_y = first_row_y;
10276 run.desired_y = it.current_y;
10277 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
10278
10279 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 10280 {
5f5c8ee5
GM
10281 update_begin (f);
10282 rif->update_window_begin_hook (w);
64d1e7d3 10283 rif->clear_mouse_face (w);
5f5c8ee5 10284 rif->scroll_run_hook (w, &run);
64d1e7d3 10285 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 10286 update_end (f);
a2889657 10287 }
5f5c8ee5
GM
10288
10289 /* Shift current matrix down by nrows_scrolled lines. */
10290 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10291 rotate_matrix (w->current_matrix,
10292 start_vpos,
10293 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10294 nrows_scrolled);
10295
9c8b8382 10296 /* Disable lines that must be updated. */
5f5c8ee5 10297 for (i = 0; i < it.vpos; ++i)
b48f74cb 10298 (start_row + i)->enabled_p = 0;
9c8b8382 10299
5f5c8ee5 10300 /* Re-compute Y positions. */
045dee35 10301 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10302 max_y = it.last_visible_y;
b48f74cb
GM
10303 for (row = start_row + nrows_scrolled;
10304 row < bottom_row;
10305 ++row)
d2f84654 10306 {
5f5c8ee5
GM
10307 row->y = it.current_y;
10308
10309 if (row->y < min_y)
10310 row->visible_height = row->height - (min_y - row->y);
10311 else if (row->y + row->height > max_y)
10312 row->visible_height
10313 = row->height - (row->y + row->height - max_y);
10314 else
10315 row->visible_height = row->height;
10316
10317 it.current_y += row->height;
5f5c8ee5
GM
10318
10319 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10320 last_reused_text_row = row;
10321 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10322 break;
d2f84654 10323 }
9c8b8382
GM
10324
10325 /* Disable lines in the current matrix which are now
10326 below the window. */
bafb434c 10327 for (++row; row < bottom_row; ++row)
9c8b8382 10328 row->enabled_p = 0;
a2889657 10329 }
5f5c8ee5
GM
10330
10331 /* Update window_end_pos etc.; last_reused_text_row is the last
10332 reused row from the current matrix containing text, if any.
10333 The value of last_text_row is the last displayed line
10334 containing text. */
10335 if (last_reused_text_row)
a2889657 10336 {
5f5c8ee5
GM
10337 w->window_end_bytepos
10338 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10339 XSETFASTINT (w->window_end_pos,
10340 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10341 XSETFASTINT (w->window_end_vpos,
10342 MATRIX_ROW_VPOS (last_reused_text_row,
10343 w->current_matrix));
a2889657 10344 }
5f5c8ee5
GM
10345 else if (last_text_row)
10346 {
10347 w->window_end_bytepos
10348 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10349 XSETFASTINT (w->window_end_pos,
10350 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10351 XSETFASTINT (w->window_end_vpos,
10352 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10353 }
10354 else
10355 {
10356 /* This window must be completely empty. */
10357 w->window_end_bytepos = 0;
10358 XSETFASTINT (w->window_end_pos, 0);
10359 XSETFASTINT (w->window_end_vpos, 0);
10360 }
10361 w->window_end_valid = Qnil;
a2889657 10362
5f5c8ee5
GM
10363 /* Update hint: don't try scrolling again in update_window. */
10364 w->desired_matrix->no_scrolling_p = 1;
10365
10366#if GLYPH_DEBUG
10367 debug_method_add (w, "try_window_reusing_current_matrix 1");
10368#endif
10369 return 1;
a2889657 10370 }
5f5c8ee5
GM
10371 else if (CHARPOS (new_start) > CHARPOS (start))
10372 {
10373 struct glyph_row *pt_row, *row;
10374 struct glyph_row *first_reusable_row;
10375 struct glyph_row *first_row_to_display;
10376 int dy;
10377 int yb = window_text_bottom_y (w);
10378
10379 IF_DEBUG (debug_method_add (w, "twu2"));
10380
10381 /* Find the row starting at new_start, if there is one. Don't
10382 reuse a partially visible line at the end. */
b48f74cb 10383 first_reusable_row = start_row;
5f5c8ee5
GM
10384 while (first_reusable_row->enabled_p
10385 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10386 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10387 < CHARPOS (new_start)))
10388 ++first_reusable_row;
10389
10390 /* Give up if there is no row to reuse. */
10391 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
10392 || !first_reusable_row->enabled_p
10393 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10394 != CHARPOS (new_start)))
5f5c8ee5
GM
10395 return 0;
10396
5f5c8ee5
GM
10397 /* We can reuse fully visible rows beginning with
10398 first_reusable_row to the end of the window. Set
10399 first_row_to_display to the first row that cannot be reused.
10400 Set pt_row to the row containing point, if there is any. */
10401 first_row_to_display = first_reusable_row;
10402 pt_row = NULL;
10403 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10404 {
10405 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10406 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10407 pt_row = first_row_to_display;
a2889657 10408
5f5c8ee5
GM
10409 ++first_row_to_display;
10410 }
a2889657 10411
5f5c8ee5
GM
10412 /* Start displaying at the start of first_row_to_display. */
10413 xassert (first_row_to_display->y < yb);
10414 init_to_row_start (&it, w, first_row_to_display);
b48f74cb
GM
10415 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10416 - start_vpos);
5f5c8ee5
GM
10417 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10418 - nrows_scrolled);
b48f74cb
GM
10419 it.current_y = (first_row_to_display->y - first_reusable_row->y
10420 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
10421
10422 /* Display lines beginning with first_row_to_display in the
10423 desired matrix. Set last_text_row to the last row displayed
10424 that displays text. */
10425 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10426 if (pt_row == NULL)
10427 w->cursor.vpos = -1;
10428 last_text_row = NULL;
10429 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10430 if (display_line (&it))
10431 last_text_row = it.glyph_row - 1;
10432
10433 /* Give up If point isn't in a row displayed or reused. */
10434 if (w->cursor.vpos < 0)
10435 {
10436 clear_glyph_matrix (w->desired_matrix);
10437 return 0;
10438 }
12adba34 10439
5f5c8ee5
GM
10440 /* If point is in a reused row, adjust y and vpos of the cursor
10441 position. */
10442 if (pt_row)
10443 {
10444 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10445 w->current_matrix);
10446 w->cursor.y -= first_reusable_row->y;
a2889657
JB
10447 }
10448
5f5c8ee5
GM
10449 /* Scroll the display. */
10450 run.current_y = first_reusable_row->y;
045dee35 10451 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10452 run.height = it.last_visible_y - run.current_y;
4da61803
GM
10453 dy = run.current_y - run.desired_y;
10454
5f5c8ee5
GM
10455 if (run.height)
10456 {
10457 struct frame *f = XFRAME (WINDOW_FRAME (w));
10458 update_begin (f);
10459 rif->update_window_begin_hook (w);
64d1e7d3 10460 rif->clear_mouse_face (w);
5f5c8ee5 10461 rif->scroll_run_hook (w, &run);
64d1e7d3 10462 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
10463 update_end (f);
10464 }
a2889657 10465
5f5c8ee5
GM
10466 /* Adjust Y positions of reused rows. */
10467 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 10468 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10469 max_y = it.last_visible_y;
b48f74cb 10470 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
10471 {
10472 row->y -= dy;
10473 if (row->y < min_y)
10474 row->visible_height = row->height - (min_y - row->y);
10475 else if (row->y + row->height > max_y)
10476 row->visible_height
10477 = row->height - (row->y + row->height - max_y);
10478 else
10479 row->visible_height = row->height;
5f5c8ee5 10480 }
a2889657 10481
5f5c8ee5
GM
10482 /* Disable rows not reused. */
10483 while (row < bottom_row)
10484 {
10485 row->enabled_p = 0;
10486 ++row;
10487 }
10488
10489 /* Scroll the current matrix. */
10490 xassert (nrows_scrolled > 0);
10491 rotate_matrix (w->current_matrix,
10492 start_vpos,
10493 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10494 -nrows_scrolled);
10495
10496 /* Adjust window end. A null value of last_text_row means that
10497 the window end is in reused rows which in turn means that
10498 only its vpos can have changed. */
10499 if (last_text_row)
10500 {
10501 w->window_end_bytepos
10502 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10503 XSETFASTINT (w->window_end_pos,
10504 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10505 XSETFASTINT (w->window_end_vpos,
10506 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10507 }
10508 else
a2889657 10509 {
e8e536a9 10510 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 10511 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 10512 }
5f5c8ee5
GM
10513
10514 w->window_end_valid = Qnil;
10515 w->desired_matrix->no_scrolling_p = 1;
10516
10517#if GLYPH_DEBUG
10518 debug_method_add (w, "try_window_reusing_current_matrix 2");
10519#endif
10520 return 1;
a2889657 10521 }
5f5c8ee5
GM
10522
10523 return 0;
10524}
a2889657 10525
a2889657 10526
5f5c8ee5
GM
10527\f
10528/************************************************************************
10529 Window redisplay reusing current matrix when buffer has changed
10530 ************************************************************************/
10531
1ec185cb
GM
10532static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10533static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
10534 int *, int *));
10535static struct glyph_row *
10536find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10537 struct glyph_row *));
10538
10539
10540/* Return the last row in MATRIX displaying text. If row START is
10541 non-null, start searching with that row. IT gives the dimensions
10542 of the display. Value is null if matrix is empty; otherwise it is
10543 a pointer to the row found. */
10544
10545static struct glyph_row *
10546find_last_row_displaying_text (matrix, it, start)
10547 struct glyph_matrix *matrix;
10548 struct it *it;
10549 struct glyph_row *start;
10550{
10551 struct glyph_row *row, *row_found;
10552
10553 /* Set row_found to the last row in IT->w's current matrix
10554 displaying text. The loop looks funny but think of partially
10555 visible lines. */
10556 row_found = NULL;
10557 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10558 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10559 {
10560 xassert (row->enabled_p);
10561 row_found = row;
10562 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10563 break;
10564 ++row;
a2889657 10565 }
5f5c8ee5
GM
10566
10567 return row_found;
10568}
10569
a2889657 10570
5f5c8ee5
GM
10571/* Return the last row in the current matrix of W that is not affected
10572 by changes at the start of current_buffer that occurred since the
10573 last time W was redisplayed. Value is null if no such row exists.
a2889657 10574
5f5c8ee5
GM
10575 The global variable beg_unchanged has to contain the number of
10576 bytes unchanged at the start of current_buffer. BEG +
10577 beg_unchanged is the buffer position of the first changed byte in
10578 current_buffer. Characters at positions < BEG + beg_unchanged are
10579 at the same buffer positions as they were when the current matrix
10580 was built. */
10581
10582static struct glyph_row *
1ec185cb 10583find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
10584 struct window *w;
10585{
9142dd5b 10586 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
10587 struct glyph_row *row;
10588 struct glyph_row *row_found = NULL;
10589 int yb = window_text_bottom_y (w);
10590
10591 /* Find the last row displaying unchanged text. */
10592 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10593 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10594 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 10595 {
5f5c8ee5
GM
10596 if (/* If row ends before first_changed_pos, it is unchanged,
10597 except in some case. */
10598 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10599 /* When row ends in ZV and we write at ZV it is not
10600 unchanged. */
10601 && !row->ends_at_zv_p
10602 /* When first_changed_pos is the end of a continued line,
10603 row is not unchanged because it may be no longer
10604 continued. */
10605 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10606 && row->continued_p))
10607 row_found = row;
10608
10609 /* Stop if last visible row. */
10610 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10611 break;
10612
10613 ++row;
a2889657
JB
10614 }
10615
5f5c8ee5 10616 return row_found;
a2889657 10617}
5f5c8ee5
GM
10618
10619
10620/* Find the first glyph row in the current matrix of W that is not
10621 affected by changes at the end of current_buffer since the last
10622 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
10623 chars by which buffer positions in unchanged text at the end of
10624 current_buffer must be adjusted. Return in *DELTA_BYTES the
10625 corresponding number of bytes. Value is null if no such row
10626 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
10627
10628static struct glyph_row *
1ec185cb 10629find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
10630 struct window *w;
10631 int *delta, *delta_bytes;
a2889657 10632{
5f5c8ee5
GM
10633 struct glyph_row *row;
10634 struct glyph_row *row_found = NULL;
c581d710 10635
5f5c8ee5 10636 *delta = *delta_bytes = 0;
b2a76982 10637
1ec185cb
GM
10638 /* Display must not have been paused, otherwise the current matrix
10639 is not up to date. */
10640 if (NILP (w->window_end_valid))
10641 abort ();
10642
10643 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
10644 end is in the range of changed text. If so, there is no
10645 unchanged row at the end of W's current matrix. */
9142dd5b 10646 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
10647 return NULL;
10648
10649 /* Set row to the last row in W's current matrix displaying text. */
10650 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10651
5f5c8ee5
GM
10652 /* If matrix is entirely empty, no unchanged row exists. */
10653 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10654 {
10655 /* The value of row is the last glyph row in the matrix having a
10656 meaningful buffer position in it. The end position of row
10657 corresponds to window_end_pos. This allows us to translate
10658 buffer positions in the current matrix to current buffer
10659 positions for characters not in changed text. */
10660 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10661 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10662 int last_unchanged_pos, last_unchanged_pos_old;
10663 struct glyph_row *first_text_row
10664 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10665
10666 *delta = Z - Z_old;
10667 *delta_bytes = Z_BYTE - Z_BYTE_old;
10668
10669 /* Set last_unchanged_pos to the buffer position of the last
10670 character in the buffer that has not been changed. Z is the
10671 index + 1 of the last byte in current_buffer, i.e. by
10672 subtracting end_unchanged we get the index of the last
10673 unchanged character, and we have to add BEG to get its buffer
10674 position. */
9142dd5b 10675 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
10676 last_unchanged_pos_old = last_unchanged_pos - *delta;
10677
10678 /* Search backward from ROW for a row displaying a line that
10679 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 10680 for (; row > first_text_row; --row)
5f5c8ee5 10681 {
1ec185cb
GM
10682 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10683 abort ();
5f5c8ee5
GM
10684
10685 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10686 row_found = row;
5f5c8ee5
GM
10687 }
10688 }
10689
1ec185cb
GM
10690 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10691 abort ();
10692
5f5c8ee5 10693 return row_found;
c581d710
RS
10694}
10695
c581d710 10696
5f5c8ee5
GM
10697/* Make sure that glyph rows in the current matrix of window W
10698 reference the same glyph memory as corresponding rows in the
10699 frame's frame matrix. This function is called after scrolling W's
10700 current matrix on a terminal frame in try_window_id and
10701 try_window_reusing_current_matrix. */
10702
10703static void
10704sync_frame_with_window_matrix_rows (w)
10705 struct window *w;
c581d710 10706{
5f5c8ee5
GM
10707 struct frame *f = XFRAME (w->frame);
10708 struct glyph_row *window_row, *window_row_end, *frame_row;
10709
10710 /* Preconditions: W must be a leaf window and full-width. Its frame
10711 must have a frame matrix. */
10712 xassert (NILP (w->hchild) && NILP (w->vchild));
10713 xassert (WINDOW_FULL_WIDTH_P (w));
10714 xassert (!FRAME_WINDOW_P (f));
10715
10716 /* If W is a full-width window, glyph pointers in W's current matrix
10717 have, by definition, to be the same as glyph pointers in the
10718 corresponding frame matrix. */
10719 window_row = w->current_matrix->rows;
10720 window_row_end = window_row + w->current_matrix->nrows;
10721 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10722 while (window_row < window_row_end)
659a218f 10723 {
5f5c8ee5 10724 int area;
f002db93 10725
5f5c8ee5
GM
10726 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10727 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
10728
10729 /* Disable frame rows whose corresponding window rows have
10730 been disabled in try_window_id. */
10731 if (!window_row->enabled_p)
10732 frame_row->enabled_p = 0;
10733
5f5c8ee5 10734 ++window_row, ++frame_row;
659a218f 10735 }
a2889657 10736}
5f5c8ee5
GM
10737
10738
e037b9ec
GM
10739/* Find the glyph row in window W containing CHARPOS. Consider all
10740 rows between START and END (not inclusive). END null means search
10741 all rows to the end of the display area of W. Value is the row
10742 containing CHARPOS or null. */
10743
10744static struct glyph_row *
10745row_containing_pos (w, charpos, start, end)
10746 struct window *w;
10747 int charpos;
10748 struct glyph_row *start, *end;
10749{
10750 struct glyph_row *row = start;
10751 int last_y;
10752
10753 /* If we happen to start on a header-line, skip that. */
10754 if (row->mode_line_p)
10755 ++row;
10756
10757 if ((end && row >= end) || !row->enabled_p)
10758 return NULL;
10759
10760 last_y = window_text_bottom_y (w);
10761
10762 while ((end == NULL || row < end)
10763 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10764 /* The end position of a row equals the start
10765 position of the next row. If CHARPOS is there, we
10766 would rather display it in the next line, except
10767 when this line ends in ZV. */
10768 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10769 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10770 || !row->ends_at_zv_p)))
10771 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10772 ++row;
10773
10774 /* Give up if CHARPOS not found. */
10775 if ((end && row >= end)
10776 || charpos < MATRIX_ROW_START_CHARPOS (row)
10777 || charpos > MATRIX_ROW_END_CHARPOS (row))
10778 row = NULL;
10779
10780 return row;
10781}
10782
10783
5f5c8ee5
GM
10784/* Try to redisplay window W by reusing its existing display. W's
10785 current matrix must be up to date when this function is called,
10786 i.e. window_end_valid must not be nil.
10787
10788 Value is
10789
10790 1 if display has been updated
10791 0 if otherwise unsuccessful
10792 -1 if redisplay with same window start is known not to succeed
10793
10794 The following steps are performed:
10795
10796 1. Find the last row in the current matrix of W that is not
10797 affected by changes at the start of current_buffer. If no such row
10798 is found, give up.
10799
10800 2. Find the first row in W's current matrix that is not affected by
10801 changes at the end of current_buffer. Maybe there is no such row.
10802
10803 3. Display lines beginning with the row + 1 found in step 1 to the
10804 row found in step 2 or, if step 2 didn't find a row, to the end of
10805 the window.
10806
10807 4. If cursor is not known to appear on the window, give up.
10808
10809 5. If display stopped at the row found in step 2, scroll the
10810 display and current matrix as needed.
10811
10812 6. Maybe display some lines at the end of W, if we must. This can
10813 happen under various circumstances, like a partially visible line
10814 becoming fully visible, or because newly displayed lines are displayed
10815 in smaller font sizes.
10816
10817 7. Update W's window end information. */
10818
10819 /* Check that window end is what we expect it to be. */
12adba34
RS
10820
10821static int
5f5c8ee5 10822try_window_id (w)
12adba34 10823 struct window *w;
12adba34 10824{
5f5c8ee5
GM
10825 struct frame *f = XFRAME (w->frame);
10826 struct glyph_matrix *current_matrix = w->current_matrix;
10827 struct glyph_matrix *desired_matrix = w->desired_matrix;
10828 struct glyph_row *last_unchanged_at_beg_row;
10829 struct glyph_row *first_unchanged_at_end_row;
10830 struct glyph_row *row;
10831 struct glyph_row *bottom_row;
10832 int bottom_vpos;
10833 struct it it;
10834 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10835 struct text_pos start_pos;
10836 struct run run;
10837 int first_unchanged_at_end_vpos = 0;
10838 struct glyph_row *last_text_row, *last_text_row_at_end;
10839 struct text_pos start;
10840
10841 SET_TEXT_POS_FROM_MARKER (start, w->start);
10842
10843 /* Check pre-conditions. Window end must be valid, otherwise
10844 the current matrix would not be up to date. */
10845 xassert (!NILP (w->window_end_valid));
10846 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10847 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10848
10849 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10850 only if buffer has really changed. The reason is that the gap is
10851 initially at Z for freshly visited files. The code below would
10852 set end_unchanged to 0 in that case. */
28ee91c0
GM
10853 if (MODIFF > SAVE_MODIFF
10854 /* This seems to happen sometimes after saving a buffer. */
10855 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 10856 {
9142dd5b
GM
10857 if (GPT - BEG < BEG_UNCHANGED)
10858 BEG_UNCHANGED = GPT - BEG;
10859 if (Z - GPT < END_UNCHANGED)
10860 END_UNCHANGED = Z - GPT;
5f5c8ee5 10861 }
bf9249e3 10862
5f5c8ee5
GM
10863 /* If window starts after a line end, and the last change is in
10864 front of that newline, then changes don't affect the display.
f2d86d7a
GM
10865 This case happens with stealth-fontification. Note that although
10866 the display is unchanged, glyph positions in the matrix have to
10867 be adjusted, of course. */
5f5c8ee5
GM
10868 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10869 if (CHARPOS (start) > BEGV
9142dd5b 10870 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
10871 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10872 && PT < MATRIX_ROW_END_CHARPOS (row))
10873 {
f2d86d7a
GM
10874 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10875 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10876
10877 if (delta)
10878 {
10879 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10880 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10881
10882 increment_matrix_positions (w->current_matrix,
10883 MATRIX_ROW_VPOS (r0, current_matrix),
10884 MATRIX_ROW_VPOS (r1, current_matrix),
10885 delta, delta_bytes);
10886 }
10887
10888#if 0 /* If changes are all in front of the window start, the
10889 distance of the last displayed glyph from Z hasn't
10890 changed. */
5f5c8ee5
GM
10891 w->window_end_pos
10892 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10893 w->window_end_bytepos
6fc556fd 10894 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
f2d86d7a
GM
10895#endif
10896
e5959a9a
GM
10897 row = row_containing_pos (w, PT, r0, NULL);
10898 if (row == NULL)
10899 return 0;
10900
10901 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
10902 return 1;
10903 }
10904
10905 /* Return quickly if changes are all below what is displayed in the
10906 window, and if PT is in the window. */
9142dd5b 10907 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
10908 && PT < MATRIX_ROW_END_CHARPOS (row))
10909 {
10910 /* We have to update window end positions because the buffer's
10911 size has changed. */
10912 w->window_end_pos
10913 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10914 w->window_end_bytepos
6fc556fd 10915 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ef121659
GM
10916
10917 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10918 row = row_containing_pos (w, PT, row, NULL);
10919 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10920 return 2;
5f5c8ee5
GM
10921 }
10922
10923 /* Check that window start agrees with the start of the first glyph
10924 row in its current matrix. Check this after we know the window
10925 start is not in changed text, otherwise positions would not be
10926 comparable. */
10927 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10928 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10929 return 0;
10930
5f5c8ee5
GM
10931 /* Compute the position at which we have to start displaying new
10932 lines. Some of the lines at the top of the window might be
10933 reusable because they are not displaying changed text. Find the
10934 last row in W's current matrix not affected by changes at the
10935 start of current_buffer. Value is null if changes start in the
10936 first line of window. */
1ec185cb 10937 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
10938 if (last_unchanged_at_beg_row)
10939 {
67f1cf4c
GM
10940 /* Avoid starting to display in the moddle of a character, a TAB
10941 for instance. This is easier than to set up the iterator
10942 exactly, and it's not a frequent case, so the additional
10943 effort wouldn't really pay off. */
10944 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
10945 && last_unchanged_at_beg_row > w->current_matrix->rows)
10946 --last_unchanged_at_beg_row;
10947
10948 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
10949 return 0;
10950
5f5c8ee5
GM
10951 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10952 start_pos = it.current.pos;
10953
10954 /* Start displaying new lines in the desired matrix at the same
10955 vpos we would use in the current matrix, i.e. below
10956 last_unchanged_at_beg_row. */
10957 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10958 current_matrix);
10959 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10960 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10961
10962 xassert (it.hpos == 0 && it.current_x == 0);
10963 }
10964 else
10965 {
10966 /* There are no reusable lines at the start of the window.
10967 Start displaying in the first line. */
10968 start_display (&it, w, start);
10969 start_pos = it.current.pos;
10970 }
10971
5f5c8ee5
GM
10972 /* Find the first row that is not affected by changes at the end of
10973 the buffer. Value will be null if there is no unchanged row, in
10974 which case we must redisplay to the end of the window. delta
10975 will be set to the value by which buffer positions beginning with
10976 first_unchanged_at_end_row have to be adjusted due to text
10977 changes. */
10978 first_unchanged_at_end_row
1ec185cb 10979 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
10980 IF_DEBUG (debug_delta = delta);
10981 IF_DEBUG (debug_delta_bytes = delta_bytes);
10982
10983 /* Set stop_pos to the buffer position up to which we will have to
10984 display new lines. If first_unchanged_at_end_row != NULL, this
10985 is the buffer position of the start of the line displayed in that
10986 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10987 that we don't stop at a buffer position. */
10988 stop_pos = 0;
10989 if (first_unchanged_at_end_row)
10990 {
10991 xassert (last_unchanged_at_beg_row == NULL
10992 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10993
10994 /* If this is a continuation line, move forward to the next one
10995 that isn't. Changes in lines above affect this line.
10996 Caution: this may move first_unchanged_at_end_row to a row
10997 not displaying text. */
10998 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10999 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11000 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11001 < it.last_visible_y))
11002 ++first_unchanged_at_end_row;
11003
11004 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11005 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11006 >= it.last_visible_y))
11007 first_unchanged_at_end_row = NULL;
11008 else
11009 {
11010 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11011 + delta);
11012 first_unchanged_at_end_vpos
11013 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 11014 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
11015 }
11016 }
11017 else if (last_unchanged_at_beg_row == NULL)
11018 return 0;
11019
11020
11021#if GLYPH_DEBUG
11022
11023 /* Either there is no unchanged row at the end, or the one we have
11024 now displays text. This is a necessary condition for the window
11025 end pos calculation at the end of this function. */
11026 xassert (first_unchanged_at_end_row == NULL
11027 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11028
11029 debug_last_unchanged_at_beg_vpos
11030 = (last_unchanged_at_beg_row
11031 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11032 : -1);
11033 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11034
11035#endif /* GLYPH_DEBUG != 0 */
11036
11037
11038 /* Display new lines. Set last_text_row to the last new line
11039 displayed which has text on it, i.e. might end up as being the
11040 line where the window_end_vpos is. */
11041 w->cursor.vpos = -1;
11042 last_text_row = NULL;
11043 overlay_arrow_seen = 0;
11044 while (it.current_y < it.last_visible_y
11045 && !fonts_changed_p
11046 && (first_unchanged_at_end_row == NULL
11047 || IT_CHARPOS (it) < stop_pos))
11048 {
11049 if (display_line (&it))
11050 last_text_row = it.glyph_row - 1;
11051 }
11052
11053 if (fonts_changed_p)
11054 return -1;
11055
11056
11057 /* Compute differences in buffer positions, y-positions etc. for
11058 lines reused at the bottom of the window. Compute what we can
11059 scroll. */
ca42b2e8
GM
11060 if (first_unchanged_at_end_row
11061 /* No lines reused because we displayed everything up to the
11062 bottom of the window. */
11063 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
11064 {
11065 dvpos = (it.vpos
11066 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11067 current_matrix));
11068 dy = it.current_y - first_unchanged_at_end_row->y;
11069 run.current_y = first_unchanged_at_end_row->y;
11070 run.desired_y = run.current_y + dy;
11071 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11072 }
11073 else
ca42b2e8
GM
11074 {
11075 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11076 first_unchanged_at_end_row = NULL;
11077 }
5f5c8ee5
GM
11078 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11079
8f8ba186 11080
5f5c8ee5
GM
11081 /* Find the cursor if not already found. We have to decide whether
11082 PT will appear on this window (it sometimes doesn't, but this is
11083 not a very frequent case.) This decision has to be made before
11084 the current matrix is altered. A value of cursor.vpos < 0 means
11085 that PT is either in one of the lines beginning at
11086 first_unchanged_at_end_row or below the window. Don't care for
11087 lines that might be displayed later at the window end; as
11088 mentioned, this is not a frequent case. */
11089 if (w->cursor.vpos < 0)
11090 {
5f5c8ee5
GM
11091 /* Cursor in unchanged rows at the top? */
11092 if (PT < CHARPOS (start_pos)
11093 && last_unchanged_at_beg_row)
11094 {
e037b9ec
GM
11095 row = row_containing_pos (w, PT,
11096 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11097 last_unchanged_at_beg_row + 1);
bfe0ee88
GM
11098 if (row)
11099 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
11100 }
11101
11102 /* Start from first_unchanged_at_end_row looking for PT. */
11103 else if (first_unchanged_at_end_row)
11104 {
e037b9ec
GM
11105 row = row_containing_pos (w, PT - delta,
11106 first_unchanged_at_end_row, NULL);
11107 if (row)
468155d7
GM
11108 set_cursor_from_row (w, row, w->current_matrix, delta,
11109 delta_bytes, dy, dvpos);
5f5c8ee5
GM
11110 }
11111
11112 /* Give up if cursor was not found. */
11113 if (w->cursor.vpos < 0)
11114 {
11115 clear_glyph_matrix (w->desired_matrix);
11116 return -1;
11117 }
11118 }
11119
11120 /* Don't let the cursor end in the scroll margins. */
11121 {
11122 int this_scroll_margin, cursor_height;
11123
11124 this_scroll_margin = max (0, scroll_margin);
11125 this_scroll_margin = min (this_scroll_margin,
11126 XFASTINT (w->height) / 4);
11127 this_scroll_margin *= CANON_Y_UNIT (it.f);
11128 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11129
11130 if ((w->cursor.y < this_scroll_margin
11131 && CHARPOS (start) > BEGV)
11132 /* Don't take scroll margin into account at the bottom because
11133 old redisplay didn't do it either. */
11134 || w->cursor.y + cursor_height > it.last_visible_y)
11135 {
11136 w->cursor.vpos = -1;
11137 clear_glyph_matrix (w->desired_matrix);
11138 return -1;
11139 }
11140 }
11141
11142 /* Scroll the display. Do it before changing the current matrix so
11143 that xterm.c doesn't get confused about where the cursor glyph is
11144 found. */
fa77249f 11145 if (dy && run.height)
5f5c8ee5
GM
11146 {
11147 update_begin (f);
11148
11149 if (FRAME_WINDOW_P (f))
11150 {
11151 rif->update_window_begin_hook (w);
64d1e7d3 11152 rif->clear_mouse_face (w);
5f5c8ee5 11153 rif->scroll_run_hook (w, &run);
64d1e7d3 11154 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11155 }
11156 else
11157 {
11158 /* Terminal frame. In this case, dvpos gives the number of
11159 lines to scroll by; dvpos < 0 means scroll up. */
11160 int first_unchanged_at_end_vpos
11161 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11162 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11163 int end = XFASTINT (w->top) + window_internal_height (w);
11164
11165 /* Perform the operation on the screen. */
11166 if (dvpos > 0)
11167 {
11168 /* Scroll last_unchanged_at_beg_row to the end of the
11169 window down dvpos lines. */
11170 set_terminal_window (end);
11171
11172 /* On dumb terminals delete dvpos lines at the end
11173 before inserting dvpos empty lines. */
11174 if (!scroll_region_ok)
11175 ins_del_lines (end - dvpos, -dvpos);
11176
11177 /* Insert dvpos empty lines in front of
11178 last_unchanged_at_beg_row. */
11179 ins_del_lines (from, dvpos);
11180 }
11181 else if (dvpos < 0)
11182 {
11183 /* Scroll up last_unchanged_at_beg_vpos to the end of
11184 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11185 set_terminal_window (end);
11186
11187 /* Delete dvpos lines in front of
11188 last_unchanged_at_beg_vpos. ins_del_lines will set
11189 the cursor to the given vpos and emit |dvpos| delete
11190 line sequences. */
11191 ins_del_lines (from + dvpos, dvpos);
11192
11193 /* On a dumb terminal insert dvpos empty lines at the
11194 end. */
11195 if (!scroll_region_ok)
11196 ins_del_lines (end + dvpos, -dvpos);
11197 }
11198
11199 set_terminal_window (0);
11200 }
11201
11202 update_end (f);
11203 }
11204
ca42b2e8
GM
11205 /* Shift reused rows of the current matrix to the right position.
11206 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11207 text. */
11208 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11209 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
11210 if (dvpos < 0)
11211 {
11212 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11213 bottom_vpos, dvpos);
11214 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11215 bottom_vpos, 0);
11216 }
11217 else if (dvpos > 0)
11218 {
11219 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11220 bottom_vpos, dvpos);
11221 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11222 first_unchanged_at_end_vpos + dvpos, 0);
11223 }
11224
11225 /* For frame-based redisplay, make sure that current frame and window
11226 matrix are in sync with respect to glyph memory. */
11227 if (!FRAME_WINDOW_P (f))
11228 sync_frame_with_window_matrix_rows (w);
11229
11230 /* Adjust buffer positions in reused rows. */
11231 if (delta)
f2d86d7a
GM
11232 increment_matrix_positions (current_matrix,
11233 first_unchanged_at_end_vpos + dvpos,
11234 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
11235
11236 /* Adjust Y positions. */
11237 if (dy)
11238 shift_glyph_matrix (w, current_matrix,
11239 first_unchanged_at_end_vpos + dvpos,
11240 bottom_vpos, dy);
11241
11242 if (first_unchanged_at_end_row)
11243 first_unchanged_at_end_row += dvpos;
11244
11245 /* If scrolling up, there may be some lines to display at the end of
11246 the window. */
11247 last_text_row_at_end = NULL;
11248 if (dy < 0)
11249 {
11250 /* Set last_row to the glyph row in the current matrix where the
11251 window end line is found. It has been moved up or down in
11252 the matrix by dvpos. */
11253 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11254 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11255
11256 /* If last_row is the window end line, it should display text. */
11257 xassert (last_row->displays_text_p);
11258
11259 /* If window end line was partially visible before, begin
11260 displaying at that line. Otherwise begin displaying with the
11261 line following it. */
11262 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11263 {
11264 init_to_row_start (&it, w, last_row);
11265 it.vpos = last_vpos;
11266 it.current_y = last_row->y;
11267 }
11268 else
11269 {
11270 init_to_row_end (&it, w, last_row);
11271 it.vpos = 1 + last_vpos;
11272 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11273 ++last_row;
11274 }
12adba34 11275
5f5c8ee5
GM
11276 /* We may start in a continuation line. If so, we have to get
11277 the right continuation_lines_width and current_x. */
11278 it.continuation_lines_width = last_row->continuation_lines_width;
11279 it.hpos = it.current_x = 0;
11280
11281 /* Display the rest of the lines at the window end. */
11282 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11283 while (it.current_y < it.last_visible_y
11284 && !fonts_changed_p)
11285 {
11286 /* Is it always sure that the display agrees with lines in
11287 the current matrix? I don't think so, so we mark rows
11288 displayed invalid in the current matrix by setting their
11289 enabled_p flag to zero. */
11290 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11291 if (display_line (&it))
11292 last_text_row_at_end = it.glyph_row - 1;
11293 }
11294 }
12adba34 11295
5f5c8ee5
GM
11296 /* Update window_end_pos and window_end_vpos. */
11297 if (first_unchanged_at_end_row
11298 && first_unchanged_at_end_row->y < it.last_visible_y
11299 && !last_text_row_at_end)
11300 {
11301 /* Window end line if one of the preserved rows from the current
11302 matrix. Set row to the last row displaying text in current
11303 matrix starting at first_unchanged_at_end_row, after
11304 scrolling. */
11305 xassert (first_unchanged_at_end_row->displays_text_p);
11306 row = find_last_row_displaying_text (w->current_matrix, &it,
11307 first_unchanged_at_end_row);
11308 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11309
11310 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11311 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11312 XSETFASTINT (w->window_end_vpos,
11313 MATRIX_ROW_VPOS (row, w->current_matrix));
11314 }
11315 else if (last_text_row_at_end)
11316 {
11317 XSETFASTINT (w->window_end_pos,
11318 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11319 w->window_end_bytepos
11320 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11321 XSETFASTINT (w->window_end_vpos,
11322 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11323 }
11324 else if (last_text_row)
11325 {
11326 /* We have displayed either to the end of the window or at the
11327 end of the window, i.e. the last row with text is to be found
11328 in the desired matrix. */
11329 XSETFASTINT (w->window_end_pos,
11330 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11331 w->window_end_bytepos
11332 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11333 XSETFASTINT (w->window_end_vpos,
11334 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11335 }
11336 else if (first_unchanged_at_end_row == NULL
11337 && last_text_row == NULL
11338 && last_text_row_at_end == NULL)
11339 {
11340 /* Displayed to end of window, but no line containing text was
11341 displayed. Lines were deleted at the end of the window. */
11342 int vpos;
045dee35 11343 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
11344
11345 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
045dee35
GM
11346 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11347 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11348 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11349 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
5f5c8ee5 11350 break;
12adba34 11351
5f5c8ee5
GM
11352 w->window_end_vpos = make_number (vpos);
11353 }
11354 else
11355 abort ();
11356
11357 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11358 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 11359
5f5c8ee5
GM
11360 /* Record that display has not been completed. */
11361 w->window_end_valid = Qnil;
11362 w->desired_matrix->no_scrolling_p = 1;
ef121659 11363 return 3;
12adba34 11364}
0f9c0ff0 11365
a2889657 11366
5f5c8ee5
GM
11367\f
11368/***********************************************************************
11369 More debugging support
11370 ***********************************************************************/
a2889657 11371
5f5c8ee5 11372#if GLYPH_DEBUG
a2889657 11373
9ab436b9
GM
11374void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11375void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 11376
31b24551 11377
9ab436b9 11378/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 11379
9ab436b9
GM
11380 GLYPHS 0 means don't show glyph contents.
11381 GLYPHS 1 means show glyphs in short form
11382 GLYPHS > 1 means show glyphs in long form. */
11383
11384void
11385dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 11386 struct glyph_matrix *matrix;
9ab436b9 11387 int glyphs;
5f5c8ee5 11388{
efc63ef0 11389 int i;
5f5c8ee5 11390 for (i = 0; i < matrix->nrows; ++i)
9ab436b9 11391 dump_glyph_row (matrix, i, glyphs);
5f5c8ee5 11392}
31b24551 11393
68a37fa8 11394
5f5c8ee5 11395/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
11396 GLYPHS 0 means don't show glyph contents.
11397 GLYPHS 1 means show glyphs in short form
11398 GLYPHS > 1 means show glyphs in long form. */
a2889657 11399
5f5c8ee5 11400void
9ab436b9 11401dump_glyph_row (matrix, vpos, glyphs)
5f5c8ee5 11402 struct glyph_matrix *matrix;
9ab436b9 11403 int vpos, glyphs;
5f5c8ee5
GM
11404{
11405 struct glyph_row *row;
11406
11407 if (vpos < 0 || vpos >= matrix->nrows)
11408 return;
11409
11410 row = MATRIX_ROW (matrix, vpos);
9ab436b9
GM
11411
11412 if (glyphs != 1)
11413 {
b94c0d9c 11414 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 11415 fprintf (stderr, "=======================================================================\n");
5f5c8ee5 11416
9ab436b9 11417 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
b94c0d9c 11418%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
9ab436b9
GM
11419 row - matrix->rows,
11420 MATRIX_ROW_START_CHARPOS (row),
11421 MATRIX_ROW_END_CHARPOS (row),
11422 row->used[TEXT_AREA],
11423 row->contains_overlapping_glyphs_p,
11424 row->enabled_p,
11425 row->inverse_p,
11426 row->truncated_on_left_p,
11427 row->truncated_on_right_p,
11428 row->overlay_arrow_p,
11429 row->continued_p,
11430 MATRIX_ROW_CONTINUATION_LINE_P (row),
11431 row->displays_text_p,
11432 row->ends_at_zv_p,
11433 row->fill_line_p,
11434 row->ends_in_middle_of_char_p,
11435 row->starts_in_middle_of_char_p,
b94c0d9c 11436 row->mouse_face_p,
9ab436b9
GM
11437 row->x,
11438 row->y,
11439 row->pixel_width,
11440 row->height,
11441 row->visible_height,
11442 row->ascent,
11443 row->phys_ascent);
11444 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11445 row->end.overlay_string_index);
11446 fprintf (stderr, "%9d %5d\n",
11447 CHARPOS (row->start.string_pos),
11448 CHARPOS (row->end.string_pos));
11449 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11450 row->end.dpvec_index);
11451 }
5f5c8ee5 11452
9ab436b9 11453 if (glyphs > 1)
bd66d1ba 11454 {
5f5c8ee5
GM
11455 struct glyph *glyph, *glyph_end;
11456 int prev_had_glyphs_p;
11457
11458 glyph = row->glyphs[TEXT_AREA];
11459 glyph_end = glyph + row->used[TEXT_AREA];
11460
11461 /* Glyph for a line end in text. */
11462 if (glyph == glyph_end && glyph->charpos > 0)
11463 ++glyph_end;
11464
11465 if (glyph < glyph_end)
bd66d1ba 11466 {
06568bbf 11467 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 11468 prev_had_glyphs_p = 1;
bd66d1ba
RS
11469 }
11470 else
5f5c8ee5
GM
11471 prev_had_glyphs_p = 0;
11472
11473 while (glyph < glyph_end)
f7b4b63a 11474 {
5f5c8ee5
GM
11475 if (glyph->type == CHAR_GLYPH)
11476 {
11477 fprintf (stderr,
06568bbf 11478 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
11479 glyph - row->glyphs[TEXT_AREA],
11480 'C',
11481 glyph->charpos,
06568bbf
GM
11482 (BUFFERP (glyph->object)
11483 ? 'B'
11484 : (STRINGP (glyph->object)
11485 ? 'S'
11486 : '-')),
5f5c8ee5 11487 glyph->pixel_width,
43d120d8
KH
11488 glyph->u.ch,
11489 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11490 ? glyph->u.ch
5f5c8ee5 11491 : '.'),
43d120d8 11492 glyph->face_id,
5f5c8ee5
GM
11493 glyph->left_box_line_p,
11494 glyph->right_box_line_p);
11495 }
11496 else if (glyph->type == STRETCH_GLYPH)
11497 {
11498 fprintf (stderr,
06568bbf 11499 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
11500 glyph - row->glyphs[TEXT_AREA],
11501 'S',
11502 glyph->charpos,
06568bbf
GM
11503 (BUFFERP (glyph->object)
11504 ? 'B'
11505 : (STRINGP (glyph->object)
11506 ? 'S'
11507 : '-')),
5f5c8ee5
GM
11508 glyph->pixel_width,
11509 0,
11510 '.',
bcdda9a4 11511 glyph->face_id,
5f5c8ee5
GM
11512 glyph->left_box_line_p,
11513 glyph->right_box_line_p);
11514 }
11515 else if (glyph->type == IMAGE_GLYPH)
11516 {
11517 fprintf (stderr,
06568bbf 11518 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
5f5c8ee5
GM
11519 glyph - row->glyphs[TEXT_AREA],
11520 'I',
11521 glyph->charpos,
06568bbf
GM
11522 (BUFFERP (glyph->object)
11523 ? 'B'
11524 : (STRINGP (glyph->object)
11525 ? 'S'
11526 : '-')),
5f5c8ee5 11527 glyph->pixel_width,
43d120d8 11528 glyph->u.img_id,
5f5c8ee5 11529 '.',
bcdda9a4 11530 glyph->face_id,
5f5c8ee5
GM
11531 glyph->left_box_line_p,
11532 glyph->right_box_line_p);
11533 }
11534 ++glyph;
f7b4b63a 11535 }
f4faa47c 11536 }
9ab436b9
GM
11537 else if (glyphs == 1)
11538 {
11539 char *s = (char *) alloca (row->used[TEXT_AREA] + 1);
11540 int i;
11541
11542 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11543 {
11544 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
11545 if (glyph->type == CHAR_GLYPH
11546 && glyph->u.ch < 0x80
11547 && glyph->u.ch >= ' ')
11548 s[i] = glyph->u.ch;
11549 else
11550 s[i] = '.';
11551 }
11552
11553 s[i] = '\0';
11554 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11555 }
5f5c8ee5 11556}
f4faa47c 11557
a2889657 11558
5f5c8ee5
GM
11559DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11560 Sdump_glyph_matrix, 0, 1, "p",
11561 "Dump the current matrix of the selected window to stderr.\n\
9ab436b9
GM
11562Shows contents of glyph row structures. With non-nil\n\
11563parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11564glyphs in short form, otherwise show glyphs in long form.")
11565 (glyphs)
11566 Lisp_Object glyphs;
5f5c8ee5
GM
11567{
11568 struct window *w = XWINDOW (selected_window);
11569 struct buffer *buffer = XBUFFER (w->buffer);
11570
11571 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11572 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11573 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11574 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11575 fprintf (stderr, "=============================================\n");
9ab436b9
GM
11576 dump_glyph_matrix (w->current_matrix,
11577 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
11578 return Qnil;
11579}
1c2250c2 11580
1fca3fae 11581
e9abc296
GM
11582DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11583 "Dump glyph row ROW to stderr.\n\
11584GLYPH 0 means don't dump glyphs.\n\
11585GLYPH 1 means dump glyphs in short form.\n\
7ef18b28 11586GLYPH > 1 or omitted means dump glyphs in long form.")
e9abc296
GM
11587 (row, glyphs)
11588 Lisp_Object row, glyphs;
5f5c8ee5
GM
11589{
11590 CHECK_NUMBER (row, 0);
e9abc296
GM
11591 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11592 XINT (row),
11593 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
11594 return Qnil;
11595}
1fca3fae 11596
67481ae5 11597
b94c0d9c
GM
11598DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11599 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11600GLYPH 0 means don't dump glyphs.\n\
11601GLYPH 1 means dump glyphs in short form.\n\
11602GLYPH > 1 or omitted means dump glyphs in long form.")
11603 (row, glyphs)
11604 Lisp_Object row, glyphs;
5f5c8ee5 11605{
886bd6f2 11606 struct frame *sf = SELECTED_FRAME ();
b94c0d9c
GM
11607 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11608 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
11609 return Qnil;
11610}
ca26e1c8 11611
0f9c0ff0 11612
5f5c8ee5
GM
11613DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11614 Strace_redisplay_toggle, 0, 0, "",
11615 "Toggle tracing of redisplay.")
11616 ()
11617{
11618 trace_redisplay_p = !trace_redisplay_p;
11619 return Qnil;
11620}
bf9249e3
GM
11621
11622
11623DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11624 "Print STRING to stderr.")
11625 (string)
11626 Lisp_Object string;
11627{
11628 CHECK_STRING (string, 0);
11629 fprintf (stderr, "%s", XSTRING (string)->data);
11630 return Qnil;
11631}
5f5c8ee5
GM
11632
11633#endif /* GLYPH_DEBUG */
ca26e1c8 11634
ca26e1c8 11635
5f5c8ee5
GM
11636\f
11637/***********************************************************************
11638 Building Desired Matrix Rows
11639 ***********************************************************************/
a2889657 11640
5f5c8ee5
GM
11641/* Return a temporary glyph row holding the glyphs of an overlay
11642 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 11643
5f5c8ee5
GM
11644static struct glyph_row *
11645get_overlay_arrow_glyph_row (w)
11646 struct window *w;
11647{
11648 struct frame *f = XFRAME (WINDOW_FRAME (w));
11649 struct buffer *buffer = XBUFFER (w->buffer);
11650 struct buffer *old = current_buffer;
11651 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11652 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11653 unsigned char *arrow_end = arrow_string + arrow_len;
11654 unsigned char *p;
11655 struct it it;
11656 int multibyte_p;
11657 int n_glyphs_before;
11658
11659 set_buffer_temp (buffer);
11660 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11661 it.glyph_row->used[TEXT_AREA] = 0;
11662 SET_TEXT_POS (it.position, 0, 0);
11663
11664 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11665 p = arrow_string;
11666 while (p < arrow_end)
11667 {
11668 Lisp_Object face, ilisp;
11669
11670 /* Get the next character. */
11671 if (multibyte_p)
4fdb80f2 11672 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
11673 else
11674 it.c = *p, it.len = 1;
11675 p += it.len;
11676
11677 /* Get its face. */
11678 XSETFASTINT (ilisp, p - arrow_string);
11679 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11680 it.face_id = compute_char_face (f, it.c, face);
11681
11682 /* Compute its width, get its glyphs. */
11683 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 11684 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
11685 PRODUCE_GLYPHS (&it);
11686
11687 /* If this character doesn't fit any more in the line, we have
11688 to remove some glyphs. */
11689 if (it.current_x > it.last_visible_x)
11690 {
11691 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11692 break;
11693 }
11694 }
11695
11696 set_buffer_temp (old);
11697 return it.glyph_row;
11698}
ca26e1c8 11699
b0a0fbda 11700
5f5c8ee5
GM
11701/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11702 glyphs are only inserted for terminal frames since we can't really
11703 win with truncation glyphs when partially visible glyphs are
11704 involved. Which glyphs to insert is determined by
11705 produce_special_glyphs. */
67481ae5 11706
5f5c8ee5
GM
11707static void
11708insert_left_trunc_glyphs (it)
11709 struct it *it;
11710{
11711 struct it truncate_it;
11712 struct glyph *from, *end, *to, *toend;
11713
11714 xassert (!FRAME_WINDOW_P (it->f));
11715
11716 /* Get the truncation glyphs. */
11717 truncate_it = *it;
5f5c8ee5
GM
11718 truncate_it.current_x = 0;
11719 truncate_it.face_id = DEFAULT_FACE_ID;
11720 truncate_it.glyph_row = &scratch_glyph_row;
11721 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11722 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 11723 truncate_it.object = make_number (0);
5f5c8ee5
GM
11724 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11725
11726 /* Overwrite glyphs from IT with truncation glyphs. */
11727 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11728 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11729 to = it->glyph_row->glyphs[TEXT_AREA];
11730 toend = to + it->glyph_row->used[TEXT_AREA];
11731
11732 while (from < end)
11733 *to++ = *from++;
11734
37be86f2
KH
11735 /* There may be padding glyphs left over. Overwrite them too. */
11736 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11737 {
11738 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11739 while (from < end)
11740 *to++ = *from++;
11741 }
5f5c8ee5 11742
37be86f2
KH
11743 if (to > toend)
11744 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 11745}
e0bfbde6 11746
e0bfbde6 11747
5f5c8ee5 11748/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 11749
5f5c8ee5
GM
11750 Most of the time, ascent and height of a display line will be equal
11751 to the max_ascent and max_height values of the display iterator
11752 structure. This is not the case if
67481ae5 11753
5f5c8ee5
GM
11754 1. We hit ZV without displaying anything. In this case, max_ascent
11755 and max_height will be zero.
1c9241f5 11756
5f5c8ee5
GM
11757 2. We have some glyphs that don't contribute to the line height.
11758 (The glyph row flag contributes_to_line_height_p is for future
11759 pixmap extensions).
f6fd109b 11760
5f5c8ee5
GM
11761 The first case is easily covered by using default values because in
11762 these cases, the line height does not really matter, except that it
11763 must not be zero. */
67481ae5 11764
5f5c8ee5
GM
11765static void
11766compute_line_metrics (it)
11767 struct it *it;
11768{
11769 struct glyph_row *row = it->glyph_row;
11770 int area, i;
1c2250c2 11771
5f5c8ee5
GM
11772 if (FRAME_WINDOW_P (it->f))
11773 {
045dee35 11774 int i, header_line_height;
1c2250c2 11775
5f5c8ee5
GM
11776 /* The line may consist of one space only, that was added to
11777 place the cursor on it. If so, the row's height hasn't been
11778 computed yet. */
11779 if (row->height == 0)
11780 {
11781 if (it->max_ascent + it->max_descent == 0)
312246d1 11782 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
11783 row->ascent = it->max_ascent;
11784 row->height = it->max_ascent + it->max_descent;
312246d1
GM
11785 row->phys_ascent = it->max_phys_ascent;
11786 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
11787 }
11788
11789 /* Compute the width of this line. */
11790 row->pixel_width = row->x;
11791 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11792 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11793
11794 xassert (row->pixel_width >= 0);
11795 xassert (row->ascent >= 0 && row->height > 0);
11796
312246d1
GM
11797 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11798 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11799
11800 /* If first line's physical ascent is larger than its logical
11801 ascent, use the physical ascent, and make the row taller.
11802 This makes accented characters fully visible. */
b28cb6ed 11803 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
11804 && row->phys_ascent > row->ascent)
11805 {
11806 row->height += row->phys_ascent - row->ascent;
11807 row->ascent = row->phys_ascent;
11808 }
11809
5f5c8ee5
GM
11810 /* Compute how much of the line is visible. */
11811 row->visible_height = row->height;
11812
045dee35
GM
11813 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11814 if (row->y < header_line_height)
11815 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
11816 else
11817 {
11818 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11819 if (row->y + row->height > max_y)
11820 row->visible_height -= row->y + row->height - max_y;
11821 }
11822 }
11823 else
11824 {
11825 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
11826 row->ascent = row->phys_ascent = 0;
11827 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 11828 }
67481ae5 11829
5f5c8ee5
GM
11830 /* Compute a hash code for this row. */
11831 row->hash = 0;
11832 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11833 for (i = 0; i < row->used[area]; ++i)
11834 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11835 + row->glyphs[area][i].u.val
43d120d8
KH
11836 + row->glyphs[area][i].face_id
11837 + row->glyphs[area][i].padding_p
5f5c8ee5 11838 + (row->glyphs[area][i].type << 2));
a2889657 11839
5f5c8ee5 11840 it->max_ascent = it->max_descent = 0;
312246d1 11841 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 11842}
12adba34 11843
ca26e1c8 11844
5f5c8ee5
GM
11845/* Append one space to the glyph row of iterator IT if doing a
11846 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11847 space have the default face, otherwise let it have the same face as
80c6cb1f 11848 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
11849
11850 This function is called to make sure that there is always one glyph
11851 at the end of a glyph row that the cursor can be set on under
11852 window-systems. (If there weren't such a glyph we would not know
11853 how wide and tall a box cursor should be displayed).
11854
11855 At the same time this space let's a nicely handle clearing to the
11856 end of the line if the row ends in italic text. */
ca26e1c8 11857
80c6cb1f 11858static int
5f5c8ee5
GM
11859append_space (it, default_face_p)
11860 struct it *it;
11861 int default_face_p;
11862{
11863 if (FRAME_WINDOW_P (it->f))
11864 {
11865 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 11866
5f5c8ee5
GM
11867 if (it->glyph_row->glyphs[TEXT_AREA] + n
11868 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 11869 {
cafafe0b
GM
11870 /* Save some values that must not be changed.
11871 Must save IT->c and IT->len because otherwise
11872 ITERATOR_AT_END_P wouldn't work anymore after
11873 append_space has been called. */
478d746b 11874 enum display_element_type saved_what = it->what;
cafafe0b
GM
11875 int saved_c = it->c, saved_len = it->len;
11876 int saved_x = it->current_x;
5f5c8ee5 11877 int saved_face_id = it->face_id;
cafafe0b 11878 struct text_pos saved_pos;
5f5c8ee5 11879 Lisp_Object saved_object;
980806b6 11880 struct face *face;
5f5c8ee5
GM
11881
11882 saved_object = it->object;
11883 saved_pos = it->position;
11884
11885 it->what = IT_CHARACTER;
11886 bzero (&it->position, sizeof it->position);
6fc556fd 11887 it->object = make_number (0);
5f5c8ee5
GM
11888 it->c = ' ';
11889 it->len = 1;
5f5c8ee5
GM
11890
11891 if (default_face_p)
11892 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
11893 else if (it->face_before_selective_p)
11894 it->face_id = it->saved_face_id;
980806b6
KH
11895 face = FACE_FROM_ID (it->f, it->face_id);
11896 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 11897
5f5c8ee5
GM
11898 PRODUCE_GLYPHS (it);
11899
11900 it->current_x = saved_x;
11901 it->object = saved_object;
11902 it->position = saved_pos;
11903 it->what = saved_what;
11904 it->face_id = saved_face_id;
cafafe0b
GM
11905 it->len = saved_len;
11906 it->c = saved_c;
80c6cb1f 11907 return 1;
5f5c8ee5
GM
11908 }
11909 }
80c6cb1f
GM
11910
11911 return 0;
5f5c8ee5 11912}
12adba34 11913
1842fc1a 11914
5f5c8ee5
GM
11915/* Extend the face of the last glyph in the text area of IT->glyph_row
11916 to the end of the display line. Called from display_line.
11917 If the glyph row is empty, add a space glyph to it so that we
11918 know the face to draw. Set the glyph row flag fill_line_p. */
11919
11920static void
11921extend_face_to_end_of_line (it)
11922 struct it *it;
11923{
11924 struct face *face;
11925 struct frame *f = it->f;
1842fc1a 11926
5f5c8ee5
GM
11927 /* If line is already filled, do nothing. */
11928 if (it->current_x >= it->last_visible_x)
11929 return;
11930
11931 /* Face extension extends the background and box of IT->face_id
11932 to the end of the line. If the background equals the background
4aad61f8
GM
11933 of the frame, we don't have to do anything. */
11934 if (it->face_before_selective_p)
11935 face = FACE_FROM_ID (it->f, it->saved_face_id);
11936 else
11937 face = FACE_FROM_ID (f, it->face_id);
11938
5f5c8ee5
GM
11939 if (FRAME_WINDOW_P (f)
11940 && face->box == FACE_NO_BOX
11941 && face->background == FRAME_BACKGROUND_PIXEL (f)
11942 && !face->stipple)
11943 return;
1842fc1a 11944
5f5c8ee5
GM
11945 /* Set the glyph row flag indicating that the face of the last glyph
11946 in the text area has to be drawn to the end of the text area. */
11947 it->glyph_row->fill_line_p = 1;
545e04f6 11948
980806b6
KH
11949 /* If current character of IT is not ASCII, make sure we have the
11950 ASCII face. This will be automatically undone the next time
11951 get_next_display_element returns a multibyte character. Note
11952 that the character will always be single byte in unibyte text. */
11953 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 11954 {
980806b6 11955 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 11956 }
545e04f6 11957
5f5c8ee5
GM
11958 if (FRAME_WINDOW_P (f))
11959 {
11960 /* If the row is empty, add a space with the current face of IT,
11961 so that we know which face to draw. */
11962 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 11963 {
5f5c8ee5 11964 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 11965 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 11966 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 11967 }
5f5c8ee5
GM
11968 }
11969 else
11970 {
11971 /* Save some values that must not be changed. */
11972 int saved_x = it->current_x;
11973 struct text_pos saved_pos;
11974 Lisp_Object saved_object;
478d746b 11975 enum display_element_type saved_what = it->what;
4aad61f8 11976 int saved_face_id = it->face_id;
5f5c8ee5
GM
11977
11978 saved_object = it->object;
11979 saved_pos = it->position;
11980
11981 it->what = IT_CHARACTER;
11982 bzero (&it->position, sizeof it->position);
6fc556fd 11983 it->object = make_number (0);
5f5c8ee5
GM
11984 it->c = ' ';
11985 it->len = 1;
4aad61f8 11986 it->face_id = face->id;
5f5c8ee5
GM
11987
11988 PRODUCE_GLYPHS (it);
11989
11990 while (it->current_x <= it->last_visible_x)
11991 PRODUCE_GLYPHS (it);
11992
11993 /* Don't count these blanks really. It would let us insert a left
11994 truncation glyph below and make us set the cursor on them, maybe. */
11995 it->current_x = saved_x;
11996 it->object = saved_object;
11997 it->position = saved_pos;
11998 it->what = saved_what;
4aad61f8 11999 it->face_id = saved_face_id;
5f5c8ee5
GM
12000 }
12001}
12adba34 12002
545e04f6 12003
5f5c8ee5
GM
12004/* Value is non-zero if text starting at CHARPOS in current_buffer is
12005 trailing whitespace. */
1c9241f5 12006
5f5c8ee5
GM
12007static int
12008trailing_whitespace_p (charpos)
12009 int charpos;
12010{
12011 int bytepos = CHAR_TO_BYTE (charpos);
12012 int c = 0;
7bbe686f 12013
5f5c8ee5
GM
12014 while (bytepos < ZV_BYTE
12015 && (c = FETCH_CHAR (bytepos),
12016 c == ' ' || c == '\t'))
12017 ++bytepos;
0d09d1e6 12018
8f897821
GM
12019 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12020 {
12021 if (bytepos != PT_BYTE)
12022 return 1;
12023 }
12024 return 0;
5f5c8ee5 12025}
31b24551 12026
545e04f6 12027
5f5c8ee5 12028/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 12029
5f5c8ee5
GM
12030void
12031highlight_trailing_whitespace (f, row)
12032 struct frame *f;
12033 struct glyph_row *row;
12034{
12035 int used = row->used[TEXT_AREA];
12036
12037 if (used)
12038 {
12039 struct glyph *start = row->glyphs[TEXT_AREA];
12040 struct glyph *glyph = start + used - 1;
12041
12042 /* Skip over the space glyph inserted to display the
12043 cursor at the end of a line. */
12044 if (glyph->type == CHAR_GLYPH
43d120d8 12045 && glyph->u.ch == ' '
6fc556fd 12046 && INTEGERP (glyph->object))
5f5c8ee5
GM
12047 --glyph;
12048
12049 /* If last glyph is a space or stretch, and it's trailing
12050 whitespace, set the face of all trailing whitespace glyphs in
12051 IT->glyph_row to `trailing-whitespace'. */
12052 if (glyph >= start
12053 && BUFFERP (glyph->object)
12054 && (glyph->type == STRETCH_GLYPH
12055 || (glyph->type == CHAR_GLYPH
43d120d8 12056 && glyph->u.ch == ' '))
5f5c8ee5 12057 && trailing_whitespace_p (glyph->charpos))
545e04f6 12058 {
980806b6 12059 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
12060
12061 while (glyph >= start
12062 && BUFFERP (glyph->object)
12063 && (glyph->type == STRETCH_GLYPH
12064 || (glyph->type == CHAR_GLYPH
43d120d8
KH
12065 && glyph->u.ch == ' ')))
12066 (glyph--)->face_id = face_id;
545e04f6 12067 }
a2889657 12068 }
5f5c8ee5 12069}
a2889657 12070
5fcbb24d 12071
cafafe0b 12072/* Value is non-zero if glyph row ROW in window W should be
0fd37545 12073 used to hold the cursor. */
cafafe0b
GM
12074
12075static int
12076cursor_row_p (w, row)
12077 struct window *w;
12078 struct glyph_row *row;
12079{
9a038881
GM
12080 int cursor_row_p = 1;
12081
cafafe0b
GM
12082 if (PT == MATRIX_ROW_END_CHARPOS (row))
12083 {
9a038881
GM
12084 /* If the row ends with a newline from a string, we don't want
12085 the cursor there (if the row is continued it doesn't end in a
12086 newline). */
12087 if (CHARPOS (row->end.string_pos) >= 0
12088 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12089 cursor_row_p = row->continued_p;
12090
12091 /* If the row ends at ZV, display the cursor at the end of that
12092 row instead of at the start of the row below. */
12093 else if (row->ends_at_zv_p)
12094 cursor_row_p = 1;
12095 else
12096 cursor_row_p = 0;
cafafe0b
GM
12097 }
12098
9a038881 12099 return cursor_row_p;
cafafe0b
GM
12100}
12101
12102
5f5c8ee5
GM
12103/* Construct the glyph row IT->glyph_row in the desired matrix of
12104 IT->w from text at the current position of IT. See dispextern.h
12105 for an overview of struct it. Value is non-zero if
12106 IT->glyph_row displays text, as opposed to a line displaying ZV
12107 only. */
12108
12109static int
12110display_line (it)
12111 struct it *it;
12112{
12113 struct glyph_row *row = it->glyph_row;
12114
12115 /* We always start displaying at hpos zero even if hscrolled. */
12116 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 12117
5f5c8ee5
GM
12118 /* We must not display in a row that's not a text row. */
12119 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12120 < it->w->desired_matrix->nrows);
12adba34 12121
5f5c8ee5
GM
12122 /* Is IT->w showing the region? */
12123 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 12124
5f5c8ee5
GM
12125 /* Clear the result glyph row and enable it. */
12126 prepare_desired_row (row);
12adba34 12127
5f5c8ee5
GM
12128 row->y = it->current_y;
12129 row->start = it->current;
12130 row->continuation_lines_width = it->continuation_lines_width;
12131 row->displays_text_p = 1;
91004049
GM
12132 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12133 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
12134
12135 /* Arrange the overlays nicely for our purposes. Usually, we call
12136 display_line on only one line at a time, in which case this
12137 can't really hurt too much, or we call it on lines which appear
12138 one after another in the buffer, in which case all calls to
12139 recenter_overlay_lists but the first will be pretty cheap. */
12140 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12141
5f5c8ee5
GM
12142 /* Move over display elements that are not visible because we are
12143 hscrolled. This may stop at an x-position < IT->first_visible_x
12144 if the first glyph is partially visible or if we hit a line end. */
12145 if (it->current_x < it->first_visible_x)
12146 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12147 MOVE_TO_POS | MOVE_TO_X);
12148
12149 /* Get the initial row height. This is either the height of the
12150 text hscrolled, if there is any, or zero. */
12151 row->ascent = it->max_ascent;
12152 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12153 row->phys_ascent = it->max_phys_ascent;
12154 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12155
12156 /* Loop generating characters. The loop is left with IT on the next
12157 character to display. */
12158 while (1)
12159 {
12160 int n_glyphs_before, hpos_before, x_before;
12161 int x, i, nglyphs;
ae26e27d 12162 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 12163
5f5c8ee5
GM
12164 /* Retrieve the next thing to display. Value is zero if end of
12165 buffer reached. */
12166 if (!get_next_display_element (it))
12167 {
12168 /* Maybe add a space at the end of this line that is used to
1b335865
GM
12169 display the cursor there under X. Set the charpos of the
12170 first glyph of blank lines not corresponding to any text
12171 to -1. */
12172 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12173 || row->used[TEXT_AREA] == 0)
a2889657 12174 {
5f5c8ee5
GM
12175 row->glyphs[TEXT_AREA]->charpos = -1;
12176 row->displays_text_p = 0;
12177
12178 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12179 row->indicate_empty_line_p = 1;
a2889657 12180 }
5f5c8ee5
GM
12181
12182 it->continuation_lines_width = 0;
12183 row->ends_at_zv_p = 1;
12184 break;
a2889657 12185 }
a2889657 12186
5f5c8ee5
GM
12187 /* Now, get the metrics of what we want to display. This also
12188 generates glyphs in `row' (which is IT->glyph_row). */
12189 n_glyphs_before = row->used[TEXT_AREA];
12190 x = it->current_x;
e6819faf
GM
12191
12192 /* Remember the line height so far in case the next element doesn't
12193 fit on the line. */
12194 if (!it->truncate_lines_p)
12195 {
12196 ascent = it->max_ascent;
12197 descent = it->max_descent;
12198 phys_ascent = it->max_phys_ascent;
12199 phys_descent = it->max_phys_descent;
12200 }
12201
5f5c8ee5 12202 PRODUCE_GLYPHS (it);
a2889657 12203
5f5c8ee5
GM
12204 /* If this display element was in marginal areas, continue with
12205 the next one. */
12206 if (it->area != TEXT_AREA)
a2889657 12207 {
5f5c8ee5
GM
12208 row->ascent = max (row->ascent, it->max_ascent);
12209 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12210 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12211 row->phys_height = max (row->phys_height,
12212 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 12213 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12214 continue;
12215 }
5936754e 12216
5f5c8ee5
GM
12217 /* Does the display element fit on the line? If we truncate
12218 lines, we should draw past the right edge of the window. If
12219 we don't truncate, we want to stop so that we can display the
12220 continuation glyph before the right margin. If lines are
12221 continued, there are two possible strategies for characters
12222 resulting in more than 1 glyph (e.g. tabs): Display as many
12223 glyphs as possible in this line and leave the rest for the
12224 continuation line, or display the whole element in the next
12225 line. Original redisplay did the former, so we do it also. */
12226 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12227 hpos_before = it->hpos;
12228 x_before = x;
4dcd74e6
GM
12229
12230 if (/* Not a newline. */
12231 nglyphs > 0
202c1b5b 12232 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
12233 && it->current_x < it->last_visible_x)
12234 {
37be86f2 12235 it->hpos += nglyphs;
5f5c8ee5
GM
12236 row->ascent = max (row->ascent, it->max_ascent);
12237 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12238 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12239 row->phys_height = max (row->phys_height,
12240 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12241 if (it->current_x - it->pixel_width < it->first_visible_x)
12242 row->x = x - it->first_visible_x;
12243 }
12244 else
12245 {
12246 int new_x;
12247 struct glyph *glyph;
12248
12249 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 12250 {
5f5c8ee5
GM
12251 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12252 new_x = x + glyph->pixel_width;
12253
12254 if (/* Lines are continued. */
12255 !it->truncate_lines_p
12256 && (/* Glyph doesn't fit on the line. */
12257 new_x > it->last_visible_x
12258 /* Or it fits exactly on a window system frame. */
12259 || (new_x == it->last_visible_x
12260 && FRAME_WINDOW_P (it->f))))
a2889657 12261 {
5f5c8ee5
GM
12262 /* End of a continued line. */
12263
12264 if (it->hpos == 0
12265 || (new_x == it->last_visible_x
12266 && FRAME_WINDOW_P (it->f)))
12267 {
e6819faf
GM
12268 /* Current glyph is the only one on the line or
12269 fits exactly on the line. We must continue
12270 the line because we can't draw the cursor
12271 after the glyph. */
5f5c8ee5
GM
12272 row->continued_p = 1;
12273 it->current_x = new_x;
12274 it->continuation_lines_width += new_x;
12275 ++it->hpos;
12276 if (i == nglyphs - 1)
cafafe0b 12277 set_iterator_to_next (it, 1);
5f5c8ee5 12278 }
3b3c4bf0
GM
12279 else if (CHAR_GLYPH_PADDING_P (*glyph)
12280 && !FRAME_WINDOW_P (it->f))
12281 {
12282 /* A padding glyph that doesn't fit on this line.
12283 This means the whole character doesn't fit
12284 on the line. */
12285 row->used[TEXT_AREA] = n_glyphs_before;
12286
12287 /* Fill the rest of the row with continuation
12288 glyphs like in 20.x. */
12289 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12290 < row->glyphs[1 + TEXT_AREA])
12291 produce_special_glyphs (it, IT_CONTINUATION);
12292
12293 row->continued_p = 1;
12294 it->current_x = x_before;
12295 it->continuation_lines_width += x_before;
12296
12297 /* Restore the height to what it was before the
12298 element not fitting on the line. */
12299 it->max_ascent = ascent;
12300 it->max_descent = descent;
12301 it->max_phys_ascent = phys_ascent;
12302 it->max_phys_descent = phys_descent;
12303 }
5f5c8ee5 12304 else
5936754e 12305 {
5f5c8ee5
GM
12306 /* Display element draws past the right edge of
12307 the window. Restore positions to values
12308 before the element. The next line starts
12309 with current_x before the glyph that could
12310 not be displayed, so that TAB works right. */
12311 row->used[TEXT_AREA] = n_glyphs_before + i;
12312
12313 /* Display continuation glyphs. */
12314 if (!FRAME_WINDOW_P (it->f))
12315 produce_special_glyphs (it, IT_CONTINUATION);
12316 row->continued_p = 1;
12317
12318 it->current_x = x;
12319 it->continuation_lines_width += x;
91004049
GM
12320 if (nglyphs > 1 && i > 0)
12321 {
12322 row->ends_in_middle_of_char_p = 1;
12323 it->starts_in_middle_of_char_p = 1;
12324 }
e6819faf
GM
12325
12326 /* Restore the height to what it was before the
12327 element not fitting on the line. */
12328 it->max_ascent = ascent;
12329 it->max_descent = descent;
12330 it->max_phys_ascent = phys_ascent;
12331 it->max_phys_descent = phys_descent;
5936754e 12332 }
e6819faf 12333
5f5c8ee5
GM
12334 break;
12335 }
12336 else if (new_x > it->first_visible_x)
12337 {
12338 /* Increment number of glyphs actually displayed. */
12339 ++it->hpos;
12340
12341 if (x < it->first_visible_x)
12342 /* Glyph is partially visible, i.e. row starts at
12343 negative X position. */
12344 row->x = x - it->first_visible_x;
12345 }
12346 else
12347 {
12348 /* Glyph is completely off the left margin of the
12349 window. This should not happen because of the
12350 move_it_in_display_line at the start of
12351 this function. */
12352 abort ();
a2889657 12353 }
a2889657 12354 }
5f5c8ee5
GM
12355
12356 row->ascent = max (row->ascent, it->max_ascent);
12357 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12358 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12359 row->phys_height = max (row->phys_height,
12360 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12361
12362 /* End of this display line if row is continued. */
12363 if (row->continued_p)
12364 break;
a2889657 12365 }
a2889657 12366
5f5c8ee5
GM
12367 /* Is this a line end? If yes, we're also done, after making
12368 sure that a non-default face is extended up to the right
12369 margin of the window. */
12370 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 12371 {
5f5c8ee5
GM
12372 int used_before = row->used[TEXT_AREA];
12373
12374 /* Add a space at the end of the line that is used to
12375 display the cursor there. */
12376 append_space (it, 0);
12377
12378 /* Extend the face to the end of the line. */
12379 extend_face_to_end_of_line (it);
12380
12381 /* Make sure we have the position. */
12382 if (used_before == 0)
12383 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12384
12385 /* Consume the line end. This skips over invisible lines. */
cafafe0b 12386 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12387 it->continuation_lines_width = 0;
12388 break;
1c9241f5 12389 }
a2889657 12390
5f5c8ee5
GM
12391 /* Proceed with next display element. Note that this skips
12392 over lines invisible because of selective display. */
cafafe0b 12393 set_iterator_to_next (it, 1);
b1d1124b 12394
5f5c8ee5
GM
12395 /* If we truncate lines, we are done when the last displayed
12396 glyphs reach past the right margin of the window. */
12397 if (it->truncate_lines_p
12398 && (FRAME_WINDOW_P (it->f)
12399 ? (it->current_x >= it->last_visible_x)
12400 : (it->current_x > it->last_visible_x)))
75d13c64 12401 {
5f5c8ee5
GM
12402 /* Maybe add truncation glyphs. */
12403 if (!FRAME_WINDOW_P (it->f))
12404 {
a98b5ed9
GM
12405 int i, n;
12406
12407 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12408 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12409 break;
12410
12411 for (n = row->used[TEXT_AREA]; i < n; ++i)
12412 {
12413 row->used[TEXT_AREA] = i;
12414 produce_special_glyphs (it, IT_TRUNCATION);
12415 }
5f5c8ee5
GM
12416 }
12417
12418 row->truncated_on_right_p = 1;
12419 it->continuation_lines_width = 0;
312246d1 12420 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
12421 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12422 it->hpos = hpos_before;
12423 it->current_x = x_before;
12424 break;
75d13c64 12425 }
a2889657 12426 }
a2889657 12427
5f5c8ee5
GM
12428 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12429 at the left window margin. */
12430 if (it->first_visible_x
12431 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12432 {
12433 if (!FRAME_WINDOW_P (it->f))
12434 insert_left_trunc_glyphs (it);
12435 row->truncated_on_left_p = 1;
12436 }
a2889657 12437
5f5c8ee5
GM
12438 /* If the start of this line is the overlay arrow-position, then
12439 mark this glyph row as the one containing the overlay arrow.
12440 This is clearly a mess with variable size fonts. It would be
12441 better to let it be displayed like cursors under X. */
e24c997d 12442 if (MARKERP (Voverlay_arrow_position)
a2889657 12443 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
12444 && (MATRIX_ROW_START_CHARPOS (row)
12445 == marker_position (Voverlay_arrow_position))
e24c997d 12446 && STRINGP (Voverlay_arrow_string)
a2889657
JB
12447 && ! overlay_arrow_seen)
12448 {
5f5c8ee5
GM
12449 /* Overlay arrow in window redisplay is a bitmap. */
12450 if (!FRAME_WINDOW_P (it->f))
c4628384 12451 {
5f5c8ee5
GM
12452 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12453 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12454 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12455 struct glyph *p = row->glyphs[TEXT_AREA];
12456 struct glyph *p2, *end;
12457
12458 /* Copy the arrow glyphs. */
12459 while (glyph < arrow_end)
12460 *p++ = *glyph++;
12461
12462 /* Throw away padding glyphs. */
12463 p2 = p;
12464 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12465 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12466 ++p2;
12467 if (p2 > p)
212e4f87 12468 {
5f5c8ee5
GM
12469 while (p2 < end)
12470 *p++ = *p2++;
12471 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 12472 }
c4628384 12473 }
5f5c8ee5 12474
a2889657 12475 overlay_arrow_seen = 1;
5f5c8ee5 12476 row->overlay_arrow_p = 1;
a2889657
JB
12477 }
12478
5f5c8ee5
GM
12479 /* Compute pixel dimensions of this line. */
12480 compute_line_metrics (it);
12481
12482 /* Remember the position at which this line ends. */
12483 row->end = it->current;
12484
173cbca8 12485 /* Maybe set the cursor. */
5f5c8ee5
GM
12486 if (it->w->cursor.vpos < 0
12487 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
12488 && PT <= MATRIX_ROW_END_CHARPOS (row)
12489 && cursor_row_p (it->w, row))
12490 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
12491
12492 /* Highlight trailing whitespace. */
8f897821 12493 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
12494 highlight_trailing_whitespace (it->f, it->glyph_row);
12495
12496 /* Prepare for the next line. This line starts horizontally at (X
12497 HPOS) = (0 0). Vertical positions are incremented. As a
12498 convenience for the caller, IT->glyph_row is set to the next
12499 row to be used. */
12500 it->current_x = it->hpos = 0;
12501 it->current_y += row->height;
12502 ++it->vpos;
12503 ++it->glyph_row;
12504 return row->displays_text_p;
a2889657 12505}
5f5c8ee5
GM
12506
12507
a2889657 12508\f
5f5c8ee5
GM
12509/***********************************************************************
12510 Menu Bar
12511 ***********************************************************************/
12512
12513/* Redisplay the menu bar in the frame for window W.
12514
12515 The menu bar of X frames that don't have X toolkit support is
12516 displayed in a special window W->frame->menu_bar_window.
12517
12518 The menu bar of terminal frames is treated specially as far as
12519 glyph matrices are concerned. Menu bar lines are not part of
12520 windows, so the update is done directly on the frame matrix rows
12521 for the menu bar. */
7ce2c095
RS
12522
12523static void
12524display_menu_bar (w)
12525 struct window *w;
12526{
5f5c8ee5
GM
12527 struct frame *f = XFRAME (WINDOW_FRAME (w));
12528 struct it it;
12529 Lisp_Object items;
8351baf2 12530 int i;
7ce2c095 12531
5f5c8ee5 12532 /* Don't do all this for graphical frames. */
dc937613 12533#ifdef HAVE_NTGUI
d129c4c2
KH
12534 if (!NILP (Vwindow_system))
12535 return;
dc937613 12536#endif
dc937613 12537#ifdef USE_X_TOOLKIT
d3413a53 12538 if (FRAME_X_P (f))
7ce2c095 12539 return;
5f5c8ee5 12540#endif
1a578e9b
AC
12541#ifdef macintosh
12542 if (FRAME_MAC_P (f))
12543 return;
12544#endif
5f5c8ee5
GM
12545
12546#ifdef USE_X_TOOLKIT
12547 xassert (!FRAME_WINDOW_P (f));
52377a47 12548 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
12549 it.first_visible_x = 0;
12550 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12551#else /* not USE_X_TOOLKIT */
12552 if (FRAME_WINDOW_P (f))
12553 {
12554 /* Menu bar lines are displayed in the desired matrix of the
12555 dummy window menu_bar_window. */
12556 struct window *menu_w;
12557 xassert (WINDOWP (f->menu_bar_window));
12558 menu_w = XWINDOW (f->menu_bar_window);
12559 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 12560 MENU_FACE_ID);
5f5c8ee5
GM
12561 it.first_visible_x = 0;
12562 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12563 }
12564 else
12565 {
12566 /* This is a TTY frame, i.e. character hpos/vpos are used as
12567 pixel x/y. */
12568 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 12569 MENU_FACE_ID);
5f5c8ee5
GM
12570 it.first_visible_x = 0;
12571 it.last_visible_x = FRAME_WIDTH (f);
12572 }
12573#endif /* not USE_X_TOOLKIT */
12574
1862a24e
MB
12575 if (! mode_line_inverse_video)
12576 /* Force the menu-bar to be displayed in the default face. */
12577 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12578
5f5c8ee5
GM
12579 /* Clear all rows of the menu bar. */
12580 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12581 {
12582 struct glyph_row *row = it.glyph_row + i;
12583 clear_glyph_row (row);
12584 row->enabled_p = 1;
12585 row->full_width_p = 1;
12586 }
7ce2c095 12587
5f5c8ee5
GM
12588 /* Display all items of the menu bar. */
12589 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 12590 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 12591 {
5f5c8ee5
GM
12592 Lisp_Object string;
12593
12594 /* Stop at nil string. */
8351baf2
RS
12595 string = XVECTOR (items)->contents[i + 1];
12596 if (NILP (string))
12597 break;
2d66ad19 12598
5f5c8ee5
GM
12599 /* Remember where item was displayed. */
12600 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 12601
5f5c8ee5
GM
12602 /* Display the item, pad with one space. */
12603 if (it.current_x < it.last_visible_x)
12604 display_string (NULL, string, Qnil, 0, 0, &it,
12605 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
12606 }
12607
2d66ad19 12608 /* Fill out the line with spaces. */
5f5c8ee5
GM
12609 if (it.current_x < it.last_visible_x)
12610 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 12611
5f5c8ee5
GM
12612 /* Compute the total height of the lines. */
12613 compute_line_metrics (&it);
7ce2c095 12614}
5f5c8ee5
GM
12615
12616
7ce2c095 12617\f
5f5c8ee5
GM
12618/***********************************************************************
12619 Mode Line
12620 ***********************************************************************/
12621
715e84c9
GM
12622/* Redisplay mode lines in the window tree whose root is WINDOW. If
12623 FORCE is non-zero, redisplay mode lines unconditionally.
12624 Otherwise, redisplay only mode lines that are garbaged. Value is
12625 the number of windows whose mode lines were redisplayed. */
a2889657 12626
715e84c9
GM
12627static int
12628redisplay_mode_lines (window, force)
12629 Lisp_Object window;
12630 int force;
12631{
12632 int nwindows = 0;
12633
12634 while (!NILP (window))
12635 {
12636 struct window *w = XWINDOW (window);
12637
12638 if (WINDOWP (w->hchild))
12639 nwindows += redisplay_mode_lines (w->hchild, force);
12640 else if (WINDOWP (w->vchild))
12641 nwindows += redisplay_mode_lines (w->vchild, force);
12642 else if (force
12643 || FRAME_GARBAGED_P (XFRAME (w->frame))
12644 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12645 {
12646 Lisp_Object old_selected_frame;
12647 struct text_pos lpoint;
12648 struct buffer *old = current_buffer;
12649
12650 /* Set the window's buffer for the mode line display. */
12651 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12652 set_buffer_internal_1 (XBUFFER (w->buffer));
12653
12654 /* Point refers normally to the selected window. For any
12655 other window, set up appropriate value. */
12656 if (!EQ (window, selected_window))
12657 {
12658 struct text_pos pt;
12659
12660 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12661 if (CHARPOS (pt) < BEGV)
12662 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12663 else if (CHARPOS (pt) > (ZV - 1))
12664 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12665 else
12666 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12667 }
12668
12669 /* Temporarily set up the selected frame. */
12670 old_selected_frame = selected_frame;
12671 selected_frame = w->frame;
12672
12673 /* Display mode lines. */
12674 clear_glyph_matrix (w->desired_matrix);
12675 if (display_mode_lines (w))
12676 {
12677 ++nwindows;
12678 w->must_be_updated_p = 1;
12679 }
12680
12681 /* Restore old settings. */
12682 selected_frame = old_selected_frame;
12683 set_buffer_internal_1 (old);
12684 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12685 }
12686
12687 window = w->next;
12688 }
12689
12690 return nwindows;
12691}
12692
12693
12694/* Display the mode and/or top line of window W. Value is the number
12695 of mode lines displayed. */
12696
12697static int
5f5c8ee5 12698display_mode_lines (w)
a2889657
JB
12699 struct window *w;
12700{
715e84c9
GM
12701 int n = 0;
12702
5f5c8ee5 12703 /* These will be set while the mode line specs are processed. */
aa6d10fa 12704 line_number_displayed = 0;
155ef550 12705 w->column_number_displayed = Qnil;
aa6d10fa 12706
5f5c8ee5 12707 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9
GM
12708 {
12709 display_mode_line (w, MODE_LINE_FACE_ID,
12710 current_buffer->mode_line_format);
12711 ++n;
12712 }
5f5c8ee5 12713
045dee35 12714 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
12715 {
12716 display_mode_line (w, HEADER_LINE_FACE_ID,
12717 current_buffer->header_line_format);
12718 ++n;
12719 }
12720
12721 return n;
5f5c8ee5 12722}
03b294dc 12723
03b294dc 12724
5f5c8ee5 12725/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 12726 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
12727 FORMAT is the mode line format to display. Value is the pixel
12728 height of the mode line displayed. */
03b294dc 12729
04612a64 12730static int
5f5c8ee5
GM
12731display_mode_line (w, face_id, format)
12732 struct window *w;
12733 enum face_id face_id;
12734 Lisp_Object format;
12735{
12736 struct it it;
12737 struct face *face;
03b294dc 12738
5f5c8ee5
GM
12739 init_iterator (&it, w, -1, -1, NULL, face_id);
12740 prepare_desired_row (it.glyph_row);
12741
1862a24e
MB
12742 if (! mode_line_inverse_video)
12743 /* Force the mode-line to be displayed in the default face. */
12744 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12745
5f5c8ee5
GM
12746 /* Temporarily make frame's keyboard the current kboard so that
12747 kboard-local variables in the mode_line_format will get the right
12748 values. */
12749 push_frame_kboard (it.f);
12750 display_mode_element (&it, 0, 0, 0, format);
12751 pop_frame_kboard ();
a2889657 12752
5f5c8ee5
GM
12753 /* Fill up with spaces. */
12754 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12755
12756 compute_line_metrics (&it);
12757 it.glyph_row->full_width_p = 1;
12758 it.glyph_row->mode_line_p = 1;
1862a24e 12759 it.glyph_row->inverse_p = 0;
5f5c8ee5
GM
12760 it.glyph_row->continued_p = 0;
12761 it.glyph_row->truncated_on_left_p = 0;
12762 it.glyph_row->truncated_on_right_p = 0;
12763
12764 /* Make a 3D mode-line have a shadow at its right end. */
12765 face = FACE_FROM_ID (it.f, face_id);
12766 extend_face_to_end_of_line (&it);
12767 if (face->box != FACE_NO_BOX)
d7eb09a0 12768 {
5f5c8ee5
GM
12769 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12770 + it.glyph_row->used[TEXT_AREA] - 1);
12771 last->right_box_line_p = 1;
d7eb09a0 12772 }
04612a64
GM
12773
12774 return it.glyph_row->height;
a2889657
JB
12775}
12776
a2889657 12777
5f5c8ee5
GM
12778/* Contribute ELT to the mode line for window IT->w. How it
12779 translates into text depends on its data type.
a2889657 12780
5f5c8ee5 12781 IT describes the display environment in which we display, as usual.
a2889657
JB
12782
12783 DEPTH is the depth in recursion. It is used to prevent
12784 infinite recursion here.
12785
5f5c8ee5
GM
12786 FIELD_WIDTH is the number of characters the display of ELT should
12787 occupy in the mode line, and PRECISION is the maximum number of
12788 characters to display from ELT's representation. See
12789 display_string for details. *
a2889657 12790
5f5c8ee5 12791 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
12792
12793static int
5f5c8ee5
GM
12794display_mode_element (it, depth, field_width, precision, elt)
12795 struct it *it;
a2889657 12796 int depth;
5f5c8ee5
GM
12797 int field_width, precision;
12798 Lisp_Object elt;
a2889657 12799{
5f5c8ee5
GM
12800 int n = 0, field, prec;
12801
a2889657
JB
12802 tail_recurse:
12803 if (depth > 10)
12804 goto invalid;
12805
12806 depth++;
12807
0220c518 12808 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
12809 {
12810 case Lisp_String:
12811 {
12812 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
12813 unsigned char c;
12814 unsigned char *this = XSTRING (elt)->data;
12815 unsigned char *lisp_string = this;
12816
12817 while ((precision <= 0 || n < precision)
12818 && *this
12819 && (frame_title_ptr
12820 || it->current_x < it->last_visible_x))
a2889657
JB
12821 {
12822 unsigned char *last = this;
5f5c8ee5
GM
12823
12824 /* Advance to end of string or next format specifier. */
a2889657
JB
12825 while ((c = *this++) != '\0' && c != '%')
12826 ;
5f5c8ee5 12827
a2889657
JB
12828 if (this - 1 != last)
12829 {
5f5c8ee5
GM
12830 /* Output to end of string or up to '%'. Field width
12831 is length of string. Don't output more than
12832 PRECISION allows us. */
d26b89b8
KH
12833 --this;
12834 prec = strwidth (last, this - last);
5f5c8ee5
GM
12835 if (precision > 0 && prec > precision - n)
12836 prec = precision - n;
12837
d39b6696 12838 if (frame_title_ptr)
d26b89b8 12839 n += store_frame_title (last, 0, prec);
d39b6696 12840 else
5f5c8ee5
GM
12841 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12842 it, 0, prec, 0, -1);
a2889657
JB
12843 }
12844 else /* c == '%' */
12845 {
5f5c8ee5
GM
12846 unsigned char *percent_position = this;
12847
12848 /* Get the specified minimum width. Zero means
12849 don't pad. */
12850 field = 0;
a2889657 12851 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 12852 field = field * 10 + c - '0';
a2889657 12853
5f5c8ee5
GM
12854 /* Don't pad beyond the total padding allowed. */
12855 if (field_width - n > 0 && field > field_width - n)
12856 field = field_width - n;
a2889657 12857
5f5c8ee5
GM
12858 /* Note that either PRECISION <= 0 or N < PRECISION. */
12859 prec = precision - n;
12860
a2889657 12861 if (c == 'M')
5f5c8ee5
GM
12862 n += display_mode_element (it, depth, field, prec,
12863 Vglobal_mode_string);
a2889657 12864 else if (c != 0)
d39b6696 12865 {
5f5c8ee5
GM
12866 unsigned char *spec
12867 = decode_mode_spec (it->w, c, field, prec);
12868
d39b6696 12869 if (frame_title_ptr)
5f5c8ee5 12870 n += store_frame_title (spec, field, prec);
d39b6696 12871 else
5f5c8ee5
GM
12872 {
12873 int nglyphs_before
12874 = it->glyph_row->used[TEXT_AREA];
12875 int charpos
12876 = percent_position - XSTRING (elt)->data;
12877 int nwritten
12878 = display_string (spec, Qnil, elt, charpos, 0, it,
12879 field, prec, 0, -1);
12880
12881 /* Assign to the glyphs written above the
12882 string where the `%x' came from, position
12883 of the `%'. */
12884 if (nwritten > 0)
12885 {
12886 struct glyph *glyph
12887 = (it->glyph_row->glyphs[TEXT_AREA]
12888 + nglyphs_before);
12889 int i;
12890
12891 for (i = 0; i < nwritten; ++i)
12892 {
12893 glyph[i].object = elt;
12894 glyph[i].charpos = charpos;
12895 }
12896
12897 n += nwritten;
12898 }
12899 }
d39b6696 12900 }
a2889657
JB
12901 }
12902 }
12903 }
12904 break;
12905
12906 case Lisp_Symbol:
12907 /* A symbol: process the value of the symbol recursively
12908 as if it appeared here directly. Avoid error if symbol void.
12909 Special case: if value of symbol is a string, output the string
12910 literally. */
12911 {
12912 register Lisp_Object tem;
12913 tem = Fboundp (elt);
265a9e55 12914 if (!NILP (tem))
a2889657
JB
12915 {
12916 tem = Fsymbol_value (elt);
12917 /* If value is a string, output that string literally:
12918 don't check for % within it. */
e24c997d 12919 if (STRINGP (tem))
d39b6696 12920 {
d26b89b8 12921 prec = precision - n;
d39b6696 12922 if (frame_title_ptr)
5f5c8ee5 12923 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 12924 else
5f5c8ee5
GM
12925 n += display_string (NULL, tem, Qnil, 0, 0, it,
12926 0, prec, 0, -1);
d39b6696 12927 }
a2889657 12928 else if (!EQ (tem, elt))
5f5c8ee5
GM
12929 {
12930 /* Give up right away for nil or t. */
12931 elt = tem;
12932 goto tail_recurse;
12933 }
a2889657
JB
12934 }
12935 }
12936 break;
12937
12938 case Lisp_Cons:
12939 {
12940 register Lisp_Object car, tem;
12941
12942 /* A cons cell: three distinct cases.
12943 If first element is a string or a cons, process all the elements
12944 and effectively concatenate them.
12945 If first element is a negative number, truncate displaying cdr to
12946 at most that many characters. If positive, pad (with spaces)
12947 to at least that many characters.
12948 If first element is a symbol, process the cadr or caddr recursively
12949 according to whether the symbol's value is non-nil or nil. */
9472f927 12950 car = XCAR (elt);
5f5c8ee5
GM
12951 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12952 {
12953 /* An element of the form (:eval FORM) means evaluate FORM
12954 and use the result as mode line elements. */
12955 struct gcpro gcpro1;
12956 Lisp_Object spec;
12957
116d6f5c 12958 spec = safe_eval (XCAR (XCDR (elt)));
5f5c8ee5
GM
12959 GCPRO1 (spec);
12960 n += display_mode_element (it, depth, field_width - n,
12961 precision - n, spec);
12962 UNGCPRO;
12963 }
12964 else if (SYMBOLP (car))
a2889657
JB
12965 {
12966 tem = Fboundp (car);
9472f927 12967 elt = XCDR (elt);
e24c997d 12968 if (!CONSP (elt))
a2889657
JB
12969 goto invalid;
12970 /* elt is now the cdr, and we know it is a cons cell.
12971 Use its car if CAR has a non-nil value. */
265a9e55 12972 if (!NILP (tem))
a2889657
JB
12973 {
12974 tem = Fsymbol_value (car);
265a9e55 12975 if (!NILP (tem))
9472f927
GM
12976 {
12977 elt = XCAR (elt);
12978 goto tail_recurse;
12979 }
a2889657
JB
12980 }
12981 /* Symbol's value is nil (or symbol is unbound)
12982 Get the cddr of the original list
12983 and if possible find the caddr and use that. */
9472f927 12984 elt = XCDR (elt);
265a9e55 12985 if (NILP (elt))
a2889657 12986 break;
e24c997d 12987 else if (!CONSP (elt))
a2889657 12988 goto invalid;
9472f927 12989 elt = XCAR (elt);
a2889657
JB
12990 goto tail_recurse;
12991 }
e24c997d 12992 else if (INTEGERP (car))
a2889657
JB
12993 {
12994 register int lim = XINT (car);
9472f927 12995 elt = XCDR (elt);
a2889657 12996 if (lim < 0)
5f5c8ee5
GM
12997 {
12998 /* Negative int means reduce maximum width. */
12999 if (precision <= 0)
13000 precision = -lim;
13001 else
13002 precision = min (precision, -lim);
13003 }
a2889657
JB
13004 else if (lim > 0)
13005 {
13006 /* Padding specified. Don't let it be more than
13007 current maximum. */
5f5c8ee5
GM
13008 if (precision > 0)
13009 lim = min (precision, lim);
13010
a2889657
JB
13011 /* If that's more padding than already wanted, queue it.
13012 But don't reduce padding already specified even if
13013 that is beyond the current truncation point. */
5f5c8ee5 13014 field_width = max (lim, field_width);
a2889657
JB
13015 }
13016 goto tail_recurse;
13017 }
e24c997d 13018 else if (STRINGP (car) || CONSP (car))
a2889657
JB
13019 {
13020 register int limit = 50;
5f5c8ee5
GM
13021 /* Limit is to protect against circular lists. */
13022 while (CONSP (elt)
13023 && --limit > 0
13024 && (precision <= 0 || n < precision))
a2889657 13025 {
5f5c8ee5 13026 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
13027 precision - n, XCAR (elt));
13028 elt = XCDR (elt);
a2889657
JB
13029 }
13030 }
13031 }
13032 break;
13033
13034 default:
13035 invalid:
d39b6696 13036 if (frame_title_ptr)
5f5c8ee5 13037 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 13038 else
5f5c8ee5
GM
13039 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13040 precision - n, 0, 0);
13041 return n;
a2889657
JB
13042 }
13043
5f5c8ee5
GM
13044 /* Pad to FIELD_WIDTH. */
13045 if (field_width > 0 && n < field_width)
13046 {
13047 if (frame_title_ptr)
13048 n += store_frame_title ("", field_width - n, 0);
13049 else
13050 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13051 0, 0, 0);
13052 }
13053
13054 return n;
a2889657 13055}
5f5c8ee5
GM
13056
13057
766525bc
RS
13058/* Write a null-terminated, right justified decimal representation of
13059 the positive integer D to BUF using a minimal field width WIDTH. */
13060
13061static void
13062pint2str (buf, width, d)
13063 register char *buf;
13064 register int width;
13065 register int d;
13066{
13067 register char *p = buf;
13068
13069 if (d <= 0)
5f5c8ee5 13070 *p++ = '0';
766525bc 13071 else
5f5c8ee5 13072 {
766525bc 13073 while (d > 0)
5f5c8ee5 13074 {
766525bc
RS
13075 *p++ = d % 10 + '0';
13076 d /= 10;
5f5c8ee5
GM
13077 }
13078 }
13079
13080 for (width -= (int) (p - buf); width > 0; --width)
13081 *p++ = ' ';
766525bc
RS
13082 *p-- = '\0';
13083 while (p > buf)
5f5c8ee5 13084 {
766525bc
RS
13085 d = *buf;
13086 *buf++ = *p;
13087 *p-- = d;
5f5c8ee5 13088 }
766525bc
RS
13089}
13090
5f5c8ee5 13091/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
13092 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13093 type of CODING_SYSTEM. Return updated pointer into BUF. */
13094
6693a99a 13095static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 13096
1c9241f5
KH
13097static char *
13098decode_mode_spec_coding (coding_system, buf, eol_flag)
13099 Lisp_Object coding_system;
13100 register char *buf;
13101 int eol_flag;
13102{
1e1078d6 13103 Lisp_Object val;
916848d8 13104 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
13105 unsigned char *eol_str;
13106 int eol_str_len;
13107 /* The EOL conversion we are using. */
13108 Lisp_Object eoltype;
1e1078d6 13109
4a09dee0 13110 val = Fget (coding_system, Qcoding_system);
085536c2 13111 eoltype = Qnil;
1c9241f5 13112
4a09dee0 13113 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 13114 {
916848d8
RS
13115 if (multibyte)
13116 *buf++ = '-';
21e989e3 13117 if (eol_flag)
302f2b38 13118 eoltype = eol_mnemonic_undecided;
1e1078d6 13119 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
13120 }
13121 else
13122 {
1e1078d6
RS
13123 Lisp_Object eolvalue;
13124
13125 eolvalue = Fget (coding_system, Qeol_type);
13126
916848d8
RS
13127 if (multibyte)
13128 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
13129
1c9241f5
KH
13130 if (eol_flag)
13131 {
1e1078d6
RS
13132 /* The EOL conversion that is normal on this system. */
13133
13134 if (NILP (eolvalue)) /* Not yet decided. */
13135 eoltype = eol_mnemonic_undecided;
13136 else if (VECTORP (eolvalue)) /* Not yet decided. */
13137 eoltype = eol_mnemonic_undecided;
13138 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13139 eoltype = (XFASTINT (eolvalue) == 0
13140 ? eol_mnemonic_unix
13141 : (XFASTINT (eolvalue) == 1
13142 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
13143 }
13144 }
5f5c8ee5 13145
302f2b38
EZ
13146 if (eol_flag)
13147 {
13148 /* Mention the EOL conversion if it is not the usual one. */
13149 if (STRINGP (eoltype))
13150 {
13151 eol_str = XSTRING (eoltype)->data;
13152 eol_str_len = XSTRING (eoltype)->size;
13153 }
f30b3499
KH
13154 else if (INTEGERP (eoltype)
13155 && CHAR_VALID_P (XINT (eoltype), 0))
13156 {
4a09dee0 13157 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 13158 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 13159 }
302f2b38
EZ
13160 else
13161 {
13162 eol_str = invalid_eol_type;
13163 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 13164 }
f30b3499 13165 bcopy (eol_str, buf, eol_str_len);
302f2b38 13166 buf += eol_str_len;
1c9241f5 13167 }
302f2b38 13168
1c9241f5
KH
13169 return buf;
13170}
13171
a2889657 13172/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
13173 generated by character C. PRECISION >= 0 means don't return a
13174 string longer than that value. FIELD_WIDTH > 0 means pad the
13175 string returned with spaces to that value. */
a2889657 13176
11e82b76
JB
13177static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13178
a2889657 13179static char *
5f5c8ee5 13180decode_mode_spec (w, c, field_width, precision)
a2889657 13181 struct window *w;
68c45bf0 13182 register int c;
5f5c8ee5 13183 int field_width, precision;
a2889657 13184{
0b67772d 13185 Lisp_Object obj;
5f5c8ee5
GM
13186 struct frame *f = XFRAME (WINDOW_FRAME (w));
13187 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 13188 struct buffer *b = XBUFFER (w->buffer);
a2889657 13189
0b67772d 13190 obj = Qnil;
a2889657
JB
13191
13192 switch (c)
13193 {
1af9f229
RS
13194 case '*':
13195 if (!NILP (b->read_only))
13196 return "%";
13197 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13198 return "*";
13199 return "-";
13200
13201 case '+':
13202 /* This differs from %* only for a modified read-only buffer. */
13203 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13204 return "*";
13205 if (!NILP (b->read_only))
13206 return "%";
13207 return "-";
13208
13209 case '&':
13210 /* This differs from %* in ignoring read-only-ness. */
13211 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13212 return "*";
13213 return "-";
13214
13215 case '%':
13216 return "%";
13217
13218 case '[':
13219 {
13220 int i;
13221 char *p;
13222
13223 if (command_loop_level > 5)
13224 return "[[[... ";
13225 p = decode_mode_spec_buf;
13226 for (i = 0; i < command_loop_level; i++)
13227 *p++ = '[';
13228 *p = 0;
13229 return decode_mode_spec_buf;
13230 }
13231
13232 case ']':
13233 {
13234 int i;
13235 char *p;
13236
13237 if (command_loop_level > 5)
13238 return " ...]]]";
13239 p = decode_mode_spec_buf;
13240 for (i = 0; i < command_loop_level; i++)
13241 *p++ = ']';
13242 *p = 0;
13243 return decode_mode_spec_buf;
13244 }
13245
13246 case '-':
13247 {
1af9f229 13248 register int i;
5f5c8ee5
GM
13249
13250 /* Let lots_of_dashes be a string of infinite length. */
13251 if (field_width <= 0
13252 || field_width > sizeof (lots_of_dashes))
1af9f229 13253 {
5f5c8ee5
GM
13254 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13255 decode_mode_spec_buf[i] = '-';
13256 decode_mode_spec_buf[i] = '\0';
13257 return decode_mode_spec_buf;
1af9f229 13258 }
5f5c8ee5
GM
13259 else
13260 return lots_of_dashes;
1af9f229
RS
13261 }
13262
a2889657 13263 case 'b':
d39b6696 13264 obj = b->name;
a2889657
JB
13265 break;
13266
1af9f229
RS
13267 case 'c':
13268 {
13269 int col = current_column ();
13270 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 13271 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
13272 return decode_mode_spec_buf;
13273 }
13274
13275 case 'F':
13276 /* %F displays the frame name. */
5f5c8ee5 13277 if (!NILP (f->title))
95184b48 13278 return (char *) XSTRING (f->title)->data;
fd8ff63d 13279 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 13280 return (char *) XSTRING (f->name)->data;
9c6da96f 13281 return "Emacs";
1af9f229 13282
a2889657 13283 case 'f':
d39b6696 13284 obj = b->filename;
a2889657
JB
13285 break;
13286
aa6d10fa
RS
13287 case 'l':
13288 {
12adba34
RS
13289 int startpos = XMARKER (w->start)->charpos;
13290 int startpos_byte = marker_byte_position (w->start);
13291 int line, linepos, linepos_byte, topline;
aa6d10fa 13292 int nlines, junk;
aa6d10fa
RS
13293 int height = XFASTINT (w->height);
13294
13295 /* If we decided that this buffer isn't suitable for line numbers,
13296 don't forget that too fast. */
13297 if (EQ (w->base_line_pos, w->buffer))
766525bc 13298 goto no_value;
5300fd39
RS
13299 /* But do forget it, if the window shows a different buffer now. */
13300 else if (BUFFERP (w->base_line_pos))
13301 w->base_line_pos = Qnil;
aa6d10fa
RS
13302
13303 /* If the buffer is very big, don't waste time. */
37d034d3 13304 if (INTEGERP (Vline_number_display_limit)
090703f4 13305 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
13306 {
13307 w->base_line_pos = Qnil;
13308 w->base_line_number = Qnil;
766525bc 13309 goto no_value;
aa6d10fa
RS
13310 }
13311
13312 if (!NILP (w->base_line_number)
13313 && !NILP (w->base_line_pos)
12adba34 13314 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
13315 {
13316 line = XFASTINT (w->base_line_number);
13317 linepos = XFASTINT (w->base_line_pos);
12adba34 13318 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
13319 }
13320 else
13321 {
13322 line = 1;
d39b6696 13323 linepos = BUF_BEGV (b);
12adba34 13324 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
13325 }
13326
13327 /* Count lines from base line to window start position. */
12adba34
RS
13328 nlines = display_count_lines (linepos, linepos_byte,
13329 startpos_byte,
13330 startpos, &junk);
aa6d10fa
RS
13331
13332 topline = nlines + line;
13333
13334 /* Determine a new base line, if the old one is too close
13335 or too far away, or if we did not have one.
13336 "Too close" means it's plausible a scroll-down would
13337 go back past it. */
d39b6696 13338 if (startpos == BUF_BEGV (b))
aa6d10fa 13339 {
c2213350
KH
13340 XSETFASTINT (w->base_line_number, topline);
13341 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
13342 }
13343 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 13344 || linepos == BUF_BEGV (b))
aa6d10fa 13345 {
d39b6696 13346 int limit = BUF_BEGV (b);
12adba34 13347 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 13348 int position;
5d121aec 13349 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
13350
13351 if (startpos - distance > limit)
12adba34
RS
13352 {
13353 limit = startpos - distance;
13354 limit_byte = CHAR_TO_BYTE (limit);
13355 }
aa6d10fa 13356
12adba34
RS
13357 nlines = display_count_lines (startpos, startpos_byte,
13358 limit_byte,
13359 - (height * 2 + 30),
aa6d10fa
RS
13360 &position);
13361 /* If we couldn't find the lines we wanted within
5d121aec 13362 line_number_display_limit_width chars per line,
aa6d10fa 13363 give up on line numbers for this window. */
12adba34 13364 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
13365 {
13366 w->base_line_pos = w->buffer;
13367 w->base_line_number = Qnil;
766525bc 13368 goto no_value;
aa6d10fa
RS
13369 }
13370
c2213350 13371 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 13372 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
13373 }
13374
13375 /* Now count lines from the start pos to point. */
12adba34
RS
13376 nlines = display_count_lines (startpos, startpos_byte,
13377 PT_BYTE, PT, &junk);
aa6d10fa
RS
13378
13379 /* Record that we did display the line number. */
13380 line_number_displayed = 1;
13381
13382 /* Make the string to show. */
5f5c8ee5 13383 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 13384 return decode_mode_spec_buf;
766525bc
RS
13385 no_value:
13386 {
13387 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
13388 int pad = field_width - 2;
13389 while (pad-- > 0)
13390 *p++ = ' ';
13391 *p++ = '?';
b357b9d4
KR
13392 *p++ = '?';
13393 *p = '\0';
766525bc
RS
13394 return decode_mode_spec_buf;
13395 }
aa6d10fa
RS
13396 }
13397 break;
13398
a2889657 13399 case 'm':
d39b6696 13400 obj = b->mode_name;
a2889657
JB
13401 break;
13402
13403 case 'n':
d39b6696 13404 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
13405 return " Narrow";
13406 break;
13407
a2889657
JB
13408 case 'p':
13409 {
13410 int pos = marker_position (w->start);
d39b6696 13411 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 13412
d39b6696 13413 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 13414 {
d39b6696 13415 if (pos <= BUF_BEGV (b))
a2889657
JB
13416 return "All";
13417 else
13418 return "Bottom";
13419 }
d39b6696 13420 else if (pos <= BUF_BEGV (b))
a2889657
JB
13421 return "Top";
13422 else
13423 {
3c7d31b9
RS
13424 if (total > 1000000)
13425 /* Do it differently for a large value, to avoid overflow. */
13426 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13427 else
13428 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
13429 /* We can't normally display a 3-digit number,
13430 so get us a 2-digit number that is close. */
13431 if (total == 100)
13432 total = 99;
13433 sprintf (decode_mode_spec_buf, "%2d%%", total);
13434 return decode_mode_spec_buf;
13435 }
13436 }
13437
8ffcb79f
RS
13438 /* Display percentage of size above the bottom of the screen. */
13439 case 'P':
13440 {
13441 int toppos = marker_position (w->start);
d39b6696
KH
13442 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13443 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 13444
d39b6696 13445 if (botpos >= BUF_ZV (b))
8ffcb79f 13446 {
d39b6696 13447 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
13448 return "All";
13449 else
13450 return "Bottom";
13451 }
13452 else
13453 {
3c7d31b9
RS
13454 if (total > 1000000)
13455 /* Do it differently for a large value, to avoid overflow. */
13456 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13457 else
13458 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
13459 /* We can't normally display a 3-digit number,
13460 so get us a 2-digit number that is close. */
13461 if (total == 100)
13462 total = 99;
d39b6696 13463 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
13464 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13465 else
13466 sprintf (decode_mode_spec_buf, "%2d%%", total);
13467 return decode_mode_spec_buf;
13468 }
13469 }
13470
1af9f229
RS
13471 case 's':
13472 /* status of process */
13473 obj = Fget_buffer_process (w->buffer);
13474 if (NILP (obj))
13475 return "no process";
13476#ifdef subprocesses
13477 obj = Fsymbol_name (Fprocess_status (obj));
13478#endif
13479 break;
d39b6696 13480
1af9f229
RS
13481 case 't': /* indicate TEXT or BINARY */
13482#ifdef MODE_LINE_BINARY_TEXT
13483 return MODE_LINE_BINARY_TEXT (b);
13484#else
13485 return "T";
13486#endif
1c9241f5
KH
13487
13488 case 'z':
13489 /* coding-system (not including end-of-line format) */
13490 case 'Z':
13491 /* coding-system (including end-of-line type) */
13492 {
13493 int eol_flag = (c == 'Z');
539b4d41 13494 char *p = decode_mode_spec_buf;
1c9241f5 13495
d30e754b 13496 if (! FRAME_WINDOW_P (f))
1c9241f5 13497 {
11c52c4f
RS
13498 /* No need to mention EOL here--the terminal never needs
13499 to do EOL conversion. */
13500 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13501 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 13502 }
f13c925f 13503 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 13504 p, eol_flag);
f13c925f 13505
11c52c4f 13506#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
13507#ifdef subprocesses
13508 obj = Fget_buffer_process (Fcurrent_buffer ());
13509 if (PROCESSP (obj))
13510 {
13511 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13512 p, eol_flag);
13513 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13514 p, eol_flag);
13515 }
13516#endif /* subprocesses */
11c52c4f 13517#endif /* 0 */
1c9241f5
KH
13518 *p = 0;
13519 return decode_mode_spec_buf;
13520 }
a2889657 13521 }
d39b6696 13522
e24c997d 13523 if (STRINGP (obj))
a2889657
JB
13524 return (char *) XSTRING (obj)->data;
13525 else
13526 return "";
13527}
5f5c8ee5
GM
13528
13529
12adba34
RS
13530/* Count up to COUNT lines starting from START / START_BYTE.
13531 But don't go beyond LIMIT_BYTE.
13532 Return the number of lines thus found (always nonnegative).
59b49f63 13533
12adba34 13534 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
13535
13536static int
12adba34
RS
13537display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13538 int start, start_byte, limit_byte, count;
13539 int *byte_pos_ptr;
59b49f63 13540{
59b49f63
RS
13541 register unsigned char *cursor;
13542 unsigned char *base;
13543
13544 register int ceiling;
13545 register unsigned char *ceiling_addr;
12adba34 13546 int orig_count = count;
59b49f63
RS
13547
13548 /* If we are not in selective display mode,
13549 check only for newlines. */
12adba34
RS
13550 int selective_display = (!NILP (current_buffer->selective_display)
13551 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
13552
13553 if (count > 0)
12adba34
RS
13554 {
13555 while (start_byte < limit_byte)
13556 {
13557 ceiling = BUFFER_CEILING_OF (start_byte);
13558 ceiling = min (limit_byte - 1, ceiling);
13559 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13560 base = (cursor = BYTE_POS_ADDR (start_byte));
13561 while (1)
13562 {
13563 if (selective_display)
13564 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13565 ;
13566 else
13567 while (*cursor != '\n' && ++cursor != ceiling_addr)
13568 ;
13569
13570 if (cursor != ceiling_addr)
13571 {
13572 if (--count == 0)
13573 {
13574 start_byte += cursor - base + 1;
13575 *byte_pos_ptr = start_byte;
13576 return orig_count;
13577 }
13578 else
13579 if (++cursor == ceiling_addr)
13580 break;
13581 }
13582 else
13583 break;
13584 }
13585 start_byte += cursor - base;
13586 }
13587 }
59b49f63
RS
13588 else
13589 {
12adba34
RS
13590 while (start_byte > limit_byte)
13591 {
13592 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13593 ceiling = max (limit_byte, ceiling);
13594 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13595 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
13596 while (1)
13597 {
12adba34
RS
13598 if (selective_display)
13599 while (--cursor != ceiling_addr
13600 && *cursor != '\n' && *cursor != 015)
13601 ;
13602 else
13603 while (--cursor != ceiling_addr && *cursor != '\n')
13604 ;
13605
59b49f63
RS
13606 if (cursor != ceiling_addr)
13607 {
13608 if (++count == 0)
13609 {
12adba34
RS
13610 start_byte += cursor - base + 1;
13611 *byte_pos_ptr = start_byte;
13612 /* When scanning backwards, we should
13613 not count the newline posterior to which we stop. */
13614 return - orig_count - 1;
59b49f63
RS
13615 }
13616 }
13617 else
13618 break;
13619 }
12adba34
RS
13620 /* Here we add 1 to compensate for the last decrement
13621 of CURSOR, which took it past the valid range. */
13622 start_byte += cursor - base + 1;
59b49f63
RS
13623 }
13624 }
13625
12adba34 13626 *byte_pos_ptr = limit_byte;
aa6d10fa 13627
12adba34
RS
13628 if (count < 0)
13629 return - orig_count + count;
13630 return orig_count - count;
aa6d10fa 13631
12adba34 13632}
a2889657 13633
a2889657 13634
5f5c8ee5
GM
13635\f
13636/***********************************************************************
13637 Displaying strings
13638 ***********************************************************************/
278feba9 13639
5f5c8ee5 13640/* Display a NUL-terminated string, starting with index START.
a3788d53 13641
5f5c8ee5
GM
13642 If STRING is non-null, display that C string. Otherwise, the Lisp
13643 string LISP_STRING is displayed.
a2889657 13644
5f5c8ee5
GM
13645 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13646 FACE_STRING. Display STRING or LISP_STRING with the face at
13647 FACE_STRING_POS in FACE_STRING:
a2889657 13648
5f5c8ee5
GM
13649 Display the string in the environment given by IT, but use the
13650 standard display table, temporarily.
a3788d53 13651
5f5c8ee5
GM
13652 FIELD_WIDTH is the minimum number of output glyphs to produce.
13653 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13654 with spaces. If STRING has more characters, more than FIELD_WIDTH
13655 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13656
13657 PRECISION is the maximum number of characters to output from
13658 STRING. PRECISION < 0 means don't truncate the string.
a2889657 13659
5f5c8ee5 13660 This is roughly equivalent to printf format specifiers:
a2889657 13661
5f5c8ee5
GM
13662 FIELD_WIDTH PRECISION PRINTF
13663 ----------------------------------------
13664 -1 -1 %s
13665 -1 10 %.10s
13666 10 -1 %10s
13667 20 10 %20.10s
a2889657 13668
5f5c8ee5
GM
13669 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13670 display them, and < 0 means obey the current buffer's value of
13671 enable_multibyte_characters.
278feba9 13672
5f5c8ee5 13673 Value is the number of glyphs produced. */
b1d1124b 13674
5f5c8ee5
GM
13675static int
13676display_string (string, lisp_string, face_string, face_string_pos,
13677 start, it, field_width, precision, max_x, multibyte)
13678 unsigned char *string;
13679 Lisp_Object lisp_string;
68c45bf0
PE
13680 Lisp_Object face_string;
13681 int face_string_pos;
5f5c8ee5
GM
13682 int start;
13683 struct it *it;
13684 int field_width, precision, max_x;
13685 int multibyte;
13686{
13687 int hpos_at_start = it->hpos;
13688 int saved_face_id = it->face_id;
13689 struct glyph_row *row = it->glyph_row;
13690
13691 /* Initialize the iterator IT for iteration over STRING beginning
13692 with index START. We assume that IT may be modified here (which
13693 means that display_line has to do something when displaying a
13694 mini-buffer prompt, which it does). */
13695 reseat_to_string (it, string, lisp_string, start,
13696 precision, field_width, multibyte);
13697
13698 /* If displaying STRING, set up the face of the iterator
13699 from LISP_STRING, if that's given. */
13700 if (STRINGP (face_string))
13701 {
13702 int endptr;
13703 struct face *face;
13704
13705 it->face_id
13706 = face_at_string_position (it->w, face_string, face_string_pos,
13707 0, it->region_beg_charpos,
13708 it->region_end_charpos,
13709 &endptr, it->base_face_id);
13710 face = FACE_FROM_ID (it->f, it->face_id);
13711 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 13712 }
a2889657 13713
5f5c8ee5
GM
13714 /* Set max_x to the maximum allowed X position. Don't let it go
13715 beyond the right edge of the window. */
13716 if (max_x <= 0)
13717 max_x = it->last_visible_x;
13718 else
13719 max_x = min (max_x, it->last_visible_x);
efc63ef0 13720
5f5c8ee5
GM
13721 /* Skip over display elements that are not visible. because IT->w is
13722 hscrolled. */
13723 if (it->current_x < it->first_visible_x)
13724 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13725 MOVE_TO_POS | MOVE_TO_X);
a2889657 13726
5f5c8ee5
GM
13727 row->ascent = it->max_ascent;
13728 row->height = it->max_ascent + it->max_descent;
312246d1
GM
13729 row->phys_ascent = it->max_phys_ascent;
13730 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 13731
5f5c8ee5
GM
13732 /* This condition is for the case that we are called with current_x
13733 past last_visible_x. */
13734 while (it->current_x < max_x)
a2889657 13735 {
5f5c8ee5 13736 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 13737
5f5c8ee5
GM
13738 /* Get the next display element. */
13739 if (!get_next_display_element (it))
90adcf20 13740 break;
1c9241f5 13741
5f5c8ee5
GM
13742 /* Produce glyphs. */
13743 x_before = it->current_x;
13744 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13745 PRODUCE_GLYPHS (it);
90adcf20 13746
5f5c8ee5
GM
13747 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13748 i = 0;
13749 x = x_before;
13750 while (i < nglyphs)
a2889657 13751 {
5f5c8ee5
GM
13752 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13753
13754 if (!it->truncate_lines_p
13755 && x + glyph->pixel_width > max_x)
13756 {
13757 /* End of continued line or max_x reached. */
37be86f2
KH
13758 if (CHAR_GLYPH_PADDING_P (*glyph))
13759 {
13760 /* A wide character is unbreakable. */
13761 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
13762 it->current_x = x_before;
13763 }
13764 else
13765 {
13766 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13767 it->current_x = x;
13768 }
5f5c8ee5
GM
13769 break;
13770 }
13771 else if (x + glyph->pixel_width > it->first_visible_x)
13772 {
13773 /* Glyph is at least partially visible. */
13774 ++it->hpos;
13775 if (x < it->first_visible_x)
13776 it->glyph_row->x = x - it->first_visible_x;
13777 }
13778 else
a2889657 13779 {
5f5c8ee5
GM
13780 /* Glyph is off the left margin of the display area.
13781 Should not happen. */
13782 abort ();
a2889657 13783 }
5f5c8ee5
GM
13784
13785 row->ascent = max (row->ascent, it->max_ascent);
13786 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
13787 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13788 row->phys_height = max (row->phys_height,
13789 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
13790 x += glyph->pixel_width;
13791 ++i;
a2889657 13792 }
5f5c8ee5
GM
13793
13794 /* Stop if max_x reached. */
13795 if (i < nglyphs)
13796 break;
13797
13798 /* Stop at line ends. */
13799 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 13800 {
5f5c8ee5
GM
13801 it->continuation_lines_width = 0;
13802 break;
a2889657 13803 }
1c9241f5 13804
cafafe0b 13805 set_iterator_to_next (it, 1);
a688bb24 13806
5f5c8ee5
GM
13807 /* Stop if truncating at the right edge. */
13808 if (it->truncate_lines_p
13809 && it->current_x >= it->last_visible_x)
13810 {
13811 /* Add truncation mark, but don't do it if the line is
13812 truncated at a padding space. */
13813 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 13814 {
5f5c8ee5 13815 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
13816 {
13817 int i, n;
13818
9299cb15 13819 if (it->current_x > it->last_visible_x)
37be86f2 13820 {
9299cb15
KH
13821 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13822 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13823 break;
13824 for (n = row->used[TEXT_AREA]; i < n; ++i)
13825 {
13826 row->used[TEXT_AREA] = i;
13827 produce_special_glyphs (it, IT_TRUNCATION);
13828 }
37be86f2 13829 }
9299cb15 13830 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 13831 }
5f5c8ee5 13832 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 13833 }
5f5c8ee5 13834 break;
1c9241f5 13835 }
a2889657
JB
13836 }
13837
5f5c8ee5
GM
13838 /* Maybe insert a truncation at the left. */
13839 if (it->first_visible_x
13840 && IT_CHARPOS (*it) > 0)
a2889657 13841 {
5f5c8ee5
GM
13842 if (!FRAME_WINDOW_P (it->f))
13843 insert_left_trunc_glyphs (it);
13844 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
13845 }
13846
5f5c8ee5
GM
13847 it->face_id = saved_face_id;
13848
13849 /* Value is number of columns displayed. */
13850 return it->hpos - hpos_at_start;
13851}
a2889657 13852
a2889657 13853
a2889657 13854\f
5f5c8ee5
GM
13855/* This is like a combination of memq and assq. Return 1 if PROPVAL
13856 appears as an element of LIST or as the car of an element of LIST.
13857 If PROPVAL is a list, compare each element against LIST in that
13858 way, and return 1 if any element of PROPVAL is found in LIST.
13859 Otherwise return 0. This function cannot quit. */
642eefc6
RS
13860
13861int
13862invisible_p (propval, list)
13863 register Lisp_Object propval;
13864 Lisp_Object list;
13865{
af460d46 13866 register Lisp_Object tail, proptail;
715e84c9 13867
9472f927 13868 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
13869 {
13870 register Lisp_Object tem;
9472f927 13871 tem = XCAR (tail);
642eefc6
RS
13872 if (EQ (propval, tem))
13873 return 1;
9472f927 13874 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
13875 return 1;
13876 }
715e84c9 13877
af460d46 13878 if (CONSP (propval))
715e84c9
GM
13879 {
13880 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13881 {
13882 Lisp_Object propelt;
13883 propelt = XCAR (proptail);
13884 for (tail = list; CONSP (tail); tail = XCDR (tail))
13885 {
13886 register Lisp_Object tem;
13887 tem = XCAR (tail);
13888 if (EQ (propelt, tem))
13889 return 1;
13890 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13891 return 1;
13892 }
13893 }
13894 }
13895
642eefc6
RS
13896 return 0;
13897}
13898
5f5c8ee5
GM
13899
13900/* Return 1 if PROPVAL appears as the car of an element of LIST and
13901 the cdr of that element is non-nil. If PROPVAL is a list, check
13902 each element of PROPVAL in that way, and the first time some
13903 element is found, return 1 if the cdr of that element is non-nil.
13904 Otherwise return 0. This function cannot quit. */
642eefc6
RS
13905
13906int
13907invisible_ellipsis_p (propval, list)
13908 register Lisp_Object propval;
13909 Lisp_Object list;
13910{
af460d46 13911 register Lisp_Object tail, proptail;
9472f927
GM
13912
13913 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
13914 {
13915 register Lisp_Object tem;
9472f927
GM
13916 tem = XCAR (tail);
13917 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13918 return ! NILP (XCDR (tem));
642eefc6 13919 }
9472f927 13920
af460d46 13921 if (CONSP (propval))
9472f927 13922 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
13923 {
13924 Lisp_Object propelt;
9472f927
GM
13925 propelt = XCAR (proptail);
13926 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
13927 {
13928 register Lisp_Object tem;
9472f927
GM
13929 tem = XCAR (tail);
13930 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13931 return ! NILP (XCDR (tem));
af460d46
RS
13932 }
13933 }
9472f927 13934
642eefc6
RS
13935 return 0;
13936}
5f5c8ee5
GM
13937
13938
642eefc6 13939\f
5f5c8ee5
GM
13940/***********************************************************************
13941 Initialization
13942 ***********************************************************************/
13943
a2889657
JB
13944void
13945syms_of_xdisp ()
13946{
c6e89d6c
GM
13947 Vwith_echo_area_save_vector = Qnil;
13948 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 13949
c6e89d6c
GM
13950 Vmessage_stack = Qnil;
13951 staticpro (&Vmessage_stack);
13952
735c094c 13953 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 13954 staticpro (&Qinhibit_redisplay);
735c094c 13955
5f5c8ee5
GM
13956#if GLYPH_DEBUG
13957 defsubr (&Sdump_glyph_matrix);
13958 defsubr (&Sdump_glyph_row);
e037b9ec 13959 defsubr (&Sdump_tool_bar_row);
5f5c8ee5 13960 defsubr (&Strace_redisplay_toggle);
bf9249e3 13961 defsubr (&Strace_to_stderr);
5f5c8ee5 13962#endif
99a5de87 13963#ifdef HAVE_WINDOW_SYSTEM
57c28064 13964 defsubr (&Stool_bar_lines_needed);
99a5de87 13965#endif
5f5c8ee5 13966
cf074754
RS
13967 staticpro (&Qmenu_bar_update_hook);
13968 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13969
d46fb96a 13970 staticpro (&Qoverriding_terminal_local_map);
7079aefa 13971 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 13972
399164b4
KH
13973 staticpro (&Qoverriding_local_map);
13974 Qoverriding_local_map = intern ("overriding-local-map");
13975
75c43375
RS
13976 staticpro (&Qwindow_scroll_functions);
13977 Qwindow_scroll_functions = intern ("window-scroll-functions");
13978
e0bfbde6
RS
13979 staticpro (&Qredisplay_end_trigger_functions);
13980 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 13981
2e54982e
RS
13982 staticpro (&Qinhibit_point_motion_hooks);
13983 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13984
9499d71b
GM
13985 QCdata = intern (":data");
13986 staticpro (&QCdata);
5f5c8ee5 13987 Qdisplay = intern ("display");
f3751a65 13988 staticpro (&Qdisplay);
5f5c8ee5
GM
13989 Qspace_width = intern ("space-width");
13990 staticpro (&Qspace_width);
5f5c8ee5
GM
13991 Qraise = intern ("raise");
13992 staticpro (&Qraise);
13993 Qspace = intern ("space");
13994 staticpro (&Qspace);
f3751a65
GM
13995 Qmargin = intern ("margin");
13996 staticpro (&Qmargin);
5f5c8ee5 13997 Qleft_margin = intern ("left-margin");
f3751a65 13998 staticpro (&Qleft_margin);
5f5c8ee5 13999 Qright_margin = intern ("right-margin");
f3751a65 14000 staticpro (&Qright_margin);
5f5c8ee5
GM
14001 Qalign_to = intern ("align-to");
14002 staticpro (&Qalign_to);
14003 QCalign_to = intern (":align-to");
14004 staticpro (&QCalign_to);
5f5c8ee5
GM
14005 Qrelative_width = intern ("relative-width");
14006 staticpro (&Qrelative_width);
14007 QCrelative_width = intern (":relative-width");
14008 staticpro (&QCrelative_width);
14009 QCrelative_height = intern (":relative-height");
14010 staticpro (&QCrelative_height);
14011 QCeval = intern (":eval");
14012 staticpro (&QCeval);
d3acf96b 14013 Qwhen = intern ("when");
f3751a65 14014 staticpro (&Qwhen);
886bd6f2
GM
14015 QCfile = intern (":file");
14016 staticpro (&QCfile);
5f5c8ee5
GM
14017 Qfontified = intern ("fontified");
14018 staticpro (&Qfontified);
14019 Qfontification_functions = intern ("fontification-functions");
14020 staticpro (&Qfontification_functions);
5f5c8ee5
GM
14021 Qtrailing_whitespace = intern ("trailing-whitespace");
14022 staticpro (&Qtrailing_whitespace);
14023 Qimage = intern ("image");
14024 staticpro (&Qimage);
ad4f174e
GM
14025 Qmessage_truncate_lines = intern ("message-truncate-lines");
14026 staticpro (&Qmessage_truncate_lines);
6422c1d7
GM
14027 Qgrow_only = intern ("grow-only");
14028 staticpro (&Qgrow_only);
5f5c8ee5 14029
a2889657
JB
14030 last_arrow_position = Qnil;
14031 last_arrow_string = Qnil;
f3751a65
GM
14032 staticpro (&last_arrow_position);
14033 staticpro (&last_arrow_string);
c6e89d6c
GM
14034
14035 echo_buffer[0] = echo_buffer[1] = Qnil;
14036 staticpro (&echo_buffer[0]);
14037 staticpro (&echo_buffer[1]);
14038
14039 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14040 staticpro (&echo_area_buffer[0]);
14041 staticpro (&echo_area_buffer[1]);
a2889657 14042
6a94510a
GM
14043 Vmessages_buffer_name = build_string ("*Messages*");
14044 staticpro (&Vmessages_buffer_name);
14045
8f897821
GM
14046 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14047 "Non-nil means highlight trailing whitespace.\n\
14048The face used for trailing whitespace is `trailing-whitespace'.");
14049 Vshow_trailing_whitespace = Qnil;
14050
735c094c
KH
14051 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14052 "Non-nil means don't actually do any redisplay.\n\
14053This is used for internal purposes.");
14054 Vinhibit_redisplay = Qnil;
14055
a2889657 14056 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 14057 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
14058 Vglobal_mode_string = Qnil;
14059
14060 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14061 "Marker for where to display an arrow on top of the buffer text.\n\
14062This must be the beginning of a line in order to work.\n\
14063See also `overlay-arrow-string'.");
14064 Voverlay_arrow_position = Qnil;
14065
14066 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14067 "String to display as an arrow. See also `overlay-arrow-position'.");
14068 Voverlay_arrow_string = Qnil;
14069
14070 DEFVAR_INT ("scroll-step", &scroll_step,
14071 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e 14072If that fails to bring point back on frame, point is centered instead.\n\
0894e696
SM
14073If this is zero, point is always centered after it moves off frame.\n\
14074If you want scrolling to always be a line at a time, you should set\n\
14075 `scroll-conservatively' to a large value rather than set this to 1.");
a2889657 14076
0789adb2 14077 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
090f7baa
GM
14078 "*Scroll up to this many lines, to bring point back on screen.\n\
14079A value of zero means to scroll the text to center point vertically\n\
14080in the window.");
0789adb2
RS
14081 scroll_conservatively = 0;
14082
9afd2168
RS
14083 DEFVAR_INT ("scroll-margin", &scroll_margin,
14084 "*Number of lines of margin at the top and bottom of a window.\n\
14085Recenter the window whenever point gets within this many lines\n\
14086of the top or bottom of the window.");
14087 scroll_margin = 0;
14088
5f5c8ee5 14089#if GLYPH_DEBUG
a2889657 14090 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 14091#endif
a2889657
JB
14092
14093 DEFVAR_BOOL ("truncate-partial-width-windows",
14094 &truncate_partial_width_windows,
44fa5b1e 14095 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
14096 truncate_partial_width_windows = 1;
14097
14098 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
1862a24e
MB
14099 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14100Any other value means to use the appropriate face, `mode-line',\n\
14101`header-line', or `menu' respectively.\n\
1b6022cf 14102\n\
1862a24e
MB
14103This variable is deprecated; please change the above faces instead.");
14104 mode_line_inverse_video = 1;
aa6d10fa 14105
090703f4 14106 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
5f5c8ee5 14107 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 14108If the buffer is bigger than this, the line number does not appear\n\
090703f4
GM
14109in the mode line. A value of nil means no limit.");
14110 Vline_number_display_limit = Qnil;
fba9ce76 14111
090703f4
GM
14112 DEFVAR_INT ("line-number-display-limit-width",
14113 &line_number_display_limit_width,
5d121aec
KH
14114 "*Maximum line width (in characters) for line number display.\n\
14115If the average length of the lines near point is bigger than this, then the\n\
14116line number may be omitted from the mode line.");
14117 line_number_display_limit_width = 200;
14118
fba9ce76
RS
14119 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14120 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 14121 highlight_nonselected_windows = 0;
d39b6696
KH
14122
14123 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
14124 "Non-nil if more than one frame is visible on this display.\n\
14125Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
14126This variable is not guaranteed to be accurate except while processing\n\
14127`frame-title-format' and `icon-title-format'.");
d39b6696
KH
14128
14129 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 14130 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
14131\(Assuming the window manager supports this feature.)\n\
14132This variable has the same structure as `mode-line-format' (which see),\n\
14133and is used only on frames for which no explicit name has been set\n\
14134\(see `modify-frame-parameters').");
14135 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 14136 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
14137\(Assuming the window manager supports this feature.)\n\
14138This variable has the same structure as `mode-line-format' (which see),\n\
14139and is used only on frames for which no explicit name has been set\n\
14140\(see `modify-frame-parameters').");
14141 Vicon_title_format
14142 = Vframe_title_format
14143 = Fcons (intern ("multiple-frames"),
14144 Fcons (build_string ("%b"),
14145 Fcons (Fcons (build_string (""),
14146 Fcons (intern ("invocation-name"),
14147 Fcons (build_string ("@"),
14148 Fcons (intern ("system-name"),
14149 Qnil)))),
14150 Qnil)));
5992c4f7
KH
14151
14152 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14153 "Maximum number of lines to keep in the message log buffer.\n\
14154If nil, disable message logging. If t, log messages but don't truncate\n\
14155the buffer when it becomes large.");
14156 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
14157
14158 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14159 "Functions called before redisplay, if window sizes have changed.\n\
14160The value should be a list of functions that take one argument.\n\
14161Just before redisplay, for each frame, if any of its windows have changed\n\
14162size since the last redisplay, or have been split or deleted,\n\
14163all the functions in the list are called, with the frame as argument.");
14164 Vwindow_size_change_functions = Qnil;
75c43375
RS
14165
14166 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 14167 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 14168Each function is called with two arguments, the window\n\
8d9583b0
RS
14169and its new display-start position. Note that the value of `window-end'\n\
14170is not valid when these functions are called.");
75c43375 14171 Vwindow_scroll_functions = Qnil;
5f5c8ee5 14172
e037b9ec
GM
14173 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14174 "*Non-nil means automatically resize tool-bars.\n\
14175This increases a tool-bar's height if not all tool-bar items are visible.\n\
14176It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 14177otherwise.");
e037b9ec 14178 auto_resize_tool_bars_p = 1;
5f5c8ee5 14179
e037b9ec
GM
14180 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14181 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14182 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 14183
35a41507
GM
14184 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14185 "*Margin around tool-bar buttons in pixels.\n\
14186If an integer, use that for both horizontal and vertical margins.\n\
14187Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14188HORZ specifying the horizontal margin, and VERT specifying the\n\
14189vertical margin.");
c3d76173 14190 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 14191
e037b9ec
GM
14192 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14193 "Relief thickness of tool-bar buttons.");
c3d76173 14194 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5
GM
14195
14196 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14197 "List of functions to call to fontify regions of text.\n\
14198Each function is called with one argument POS. Functions must\n\
14199fontify a region starting at POS in the current buffer, and give\n\
14200fontified regions the property `fontified'.\n\
14201This variable automatically becomes buffer-local when set.");
14202 Vfontification_functions = Qnil;
6b9f0906 14203 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
14204
14205 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
14206 &unibyte_display_via_language_environment,
14207 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
14208Specifically this means that unibyte non-ASCII characters\n\
14209are displayed by converting them to the equivalent multibyte characters\n\
14210according to the current language environment. As a result, they are\n\
14211displayed according to the current fontset.");
14212 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
14213
14214 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14215 "*Maximum height for resizing mini-windows.\n\
14216If a float, it specifies a fraction of the mini-window frame's height.\n\
6422c1d7 14217If an integer, it specifies a number of lines.");
c6e89d6c 14218 Vmax_mini_window_height = make_float (0.25);
6422c1d7
GM
14219
14220 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
504454e8 14221 "*How to resize mini-windows.\n\
6422c1d7 14222A value of nil means don't automatically resize mini-windows.\n\
504454e8 14223A value of t means resize them to fit the text displayed in them.\n\
6422c1d7 14224A value of `grow-only', the default, means let mini-windows grow\n\
504454e8
GM
14225only, until their display becomes empty, at which point the windows\n\
14226go back to their normal size.");
6422c1d7
GM
14227 Vresize_mini_windows = Qgrow_only;
14228
d6d26ed3 14229 DEFVAR_BOOL ("cursor-in-non-selected-windows",
f8156156 14230 &cursor_in_non_selected_windows,
d6d26ed3
GM
14231 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14232Nil means don't display a cursor there.");
14233 cursor_in_non_selected_windows = 1;
d475bcb8
GM
14234
14235 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14236 "*Non-nil means scroll the display automatically to make point visible.");
14237 automatic_hscrolling_p = 1;
e00daaa0
GM
14238
14239 DEFVAR_LISP ("image-types", &Vimage_types,
ad4f174e 14240 "List of supported image types.\n\
e00daaa0
GM
14241Each element of the list is a symbol for a supported image type.");
14242 Vimage_types = Qnil;
ad4f174e
GM
14243
14244 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14245 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14246Bind this around calls to `message' to let it take effect.");
14247 message_truncate_lines = 0;
0bca8940
DL
14248
14249 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14250 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14251Can be used to update submenus whose contents should vary.");
6422c1d7 14252 Vmenu_bar_update_hook = Qnil;
a2889657
JB
14253}
14254
5f5c8ee5
GM
14255
14256/* Initialize this module when Emacs starts. */
14257
dfcf069d 14258void
a2889657
JB
14259init_xdisp ()
14260{
14261 Lisp_Object root_window;
5f5c8ee5 14262 struct window *mini_w;
a2889657 14263
04612a64
GM
14264 current_header_line_height = current_mode_line_height = -1;
14265
5f5c8ee5 14266 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
14267
14268 mini_w = XWINDOW (minibuf_window);
11e82b76 14269 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 14270
a2889657
JB
14271 if (!noninteractive)
14272 {
5f5c8ee5
GM
14273 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14274 int i;
14275
14276 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 14277 set_window_height (root_window,
5f5c8ee5 14278 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 14279 0);
c2213350 14280 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
14281 set_window_height (minibuf_window, 1, 0);
14282
c2213350
KH
14283 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
14284 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
14285
14286 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14287 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14288 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14289
14290 /* The default ellipsis glyphs `...'. */
14291 for (i = 0; i < 3; ++i)
14292 XSETFASTINT (default_invis_vector[i], '.');
a2889657 14293 }
5f5c8ee5
GM
14294
14295#ifdef HAVE_WINDOW_SYSTEM
14296 {
14297 /* Allocate the buffer for frame titles. */
14298 int size = 100;
14299 frame_title_buf = (char *) xmalloc (size);
14300 frame_title_buf_end = frame_title_buf + size;
14301 frame_title_ptr = NULL;
14302 }
14303#endif /* HAVE_WINDOW_SYSTEM */
21fdfb65
GM
14304
14305 help_echo_showing_p = 0;
a2889657 14306}
5f5c8ee5
GM
14307
14308